// Copyright (C) Stichting Deltares 2017. All rights reserved.
//
// This file is part of the Dam Engine.
//
// The Dam Engine is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see .
//
// All names, logos, and references to "Deltares" are registered trademarks of
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using Deltares.DamEngine.Data.Design;
using Deltares.DamEngine.Data.General;
using Deltares.DamEngine.Data.Geometry;
using Deltares.DamEngine.Data.Geotechnics;
using Deltares.DamEngine.Io.XmlInput;
using Location = Deltares.DamEngine.Data.General.Location;
using Segment = Deltares.DamEngine.Data.General.Segment;
using Soil = Deltares.DamEngine.Io.XmlInput.Soil;
using SoilProfile1D = Deltares.DamEngine.Data.Geotechnics.SoilProfile1D;
using SoilProfile2D = Deltares.DamEngine.Data.Geotechnics.SoilProfile2D;
namespace Deltares.DamEngine.Interface
{
///
/// Fill Dam Engine data object from Input serializer object
///
public class FillDamFromXmlInput
{
///
/// Creates the dam project data from an Input serializer object
///
/// The input.
///
public static DamProjectData CreateDamProjectData(Input input)
{
var damProjectData = new DamProjectData
{
DamProjectType = ConversionHelper.ConvertToDamProjectType(input.DamProjectType),
Dike = new Dike()
};
Dike dike = damProjectData.Dike;
TransferSurfaceLines(input.SurfaceLines, dike.SurfaceLines2);
dike.SoilList = new SoilList();
TransferSoils(input.Soils, dike.SoilList.Soils);
TransferLocations(input.Locations, dike.Locations, dike.SurfaceLines2);
TransferSoilProfiles1D(input.SoilProfiles1D, dike.SoilProfiles, dike.SoilList);
//TransferSegments(input.Segments, damProjectData.Segments); To be implemented at other side.
PostProcess(input, damProjectData);
return damProjectData;
}
///
/// Postprocess: connect all objects
///
/// The input.
/// The project.
private static void PostProcess(Input input, DamProjectData project)
{
// find proper profiles for segments
foreach (var segment in project.Segments)
{
foreach (var segmentSoilProfileProbability in segment.SoilProfileProbabilities)
{
if (segmentSoilProfileProbability.SoilGeometryType == SoilGeometryType.SoilGeometry1D)
{
segmentSoilProfileProbability.SoilProfile = FindSoilProfile1DByName(project.Dike.SoilProfiles, segmentSoilProfileProbability.SoilGeometry1DName);
}
else
{
segmentSoilProfileProbability.SoilProfile2D = FindSoilProfile2DByName(segmentSoilProfileProbability.SoilGeometry2DName);
}
}
}
}
private static void TransferSoils(Soil[] inputSoils, List soils)
{
for (int i = 0; i < inputSoils.Length; i++)
{
var soil = new Data.Geotechnics.Soil();
var inputSoil = inputSoils[i];
soil.Name = inputSoil.Name;
soil.BeddingAngle = inputSoil.BeddingAngleSpecified ? inputSoil.BeddingAngle : Double.NaN;
soil.DiameterD70 = inputSoil.DiameterD70Specified ? inputSoil.DiameterD70 : Double.NaN;
soil.DiameterD90 = inputSoil.DiameterD90Specified ? inputSoil.DiameterD90 : Double.NaN;
soil.PermeabKx = inputSoil.PermeabKxSpecified ? inputSoil.PermeabKx : Double.NaN;
soil.WhitesConstant = inputSoil.WhitesConstantSpecified ? inputSoil.WhitesConstant : Double.NaN;
soils.Add(soil);
}
}
private static void TransferSurfaceLines(SurfaceLine[] inputSurfaceLines, IList dikeSurfaceLines)
{
for (int i = 0; i < inputSurfaceLines.Length; i++)
{
var surfaceLine = new SurfaceLine2();
var inputSurfaceLine = inputSurfaceLines[i];
surfaceLine.Name = inputSurfaceLine.Name;
surfaceLine.CharacteristicPoints.Geometry = surfaceLine.Geometry;
AddPointsToSurfaceLine(inputSurfaceLine, surfaceLine);
dikeSurfaceLines.Add(surfaceLine);
}
}
private static void AddPointsToSurfaceLine(SurfaceLine inputSurfaceLine, SurfaceLine2 surfaceLine)
{
surfaceLine.Geometry = new GeometryPointString();
for (int j = 0; j < inputSurfaceLine.Points.Length; j++)
{
var inputPoint = inputSurfaceLine.Points[j];
var geometryPoint = new GeometryPoint()
{
X = inputPoint.X,
Y = 0.0,
Z = inputPoint.Z
};
surfaceLine.AddCharacteristicPoint(geometryPoint, ConversionHelper.ConvertToDamPointType(inputPoint.PointType));
}
}
private static void TransferLocations(Io.XmlInput.Location[] inputLocations, IList dikeLocations, IList dikeSurfaceLines)
{
for (int i = 0; i < inputLocations.Length; i++)
{
var location = new Location();
var inputLocation = inputLocations[i];
var waternetOptions = inputLocation.WaternetOptions;
location.ModelParametersForPLLines.PLLineCreationMethod = ConversionHelper.ConvertToPhreaticLineCreationMethod(waternetOptions.PhreaticLineCreationMethod);
location.IntrusionVerticalWaterPressure = ConversionHelper.ConvertToIntrusionVerticalWaterPressure(waternetOptions.IntrusionVerticalWaterPressure);
location.PolderLevel = waternetOptions.PolderLevel;
location.ModelParametersForPLLines.DampingFactorPL4 = waternetOptions.DampingFactorPL3;
location.ModelParametersForPLLines.DampingFactorPL3 = waternetOptions.DampingFactorPL4;
location.ModelParametersForPLLines.PenetrationLength = waternetOptions.PenetrationLength;
location.PlLineOffsetBelowDikeCrestMiddle = (waternetOptions.Pl1BelowCrestMiddleSpecified ? (double?) waternetOptions.Pl1BelowCrestMiddle : null);
location.UsePlLineOffsetFactorBelowShoulderCrest = waternetOptions.Pl1FactorBelowShoulderCrestSpecified;
location.PlLineOffsetFactorBelowShoulderCrest = waternetOptions.Pl1FactorBelowShoulderCrest;
location.PlLineOffsetDryBelowDikeCrestMiddle = (waternetOptions.DryPl1BelowCrestMiddleSpecified ? (double?) waternetOptions.DryPl1BelowCrestMiddle : null);
location.UsePlLineOffsetDryFactorBelowShoulderCrest = waternetOptions.DryPl1FactorBelowShoulderCrestSpecified;
location.PlLineOffsetDryFactorBelowShoulderCrest = waternetOptions.DryPl1FactorBelowShoulderCrest;
location.HeadPl2 = (waternetOptions.HeadPl2Specified ? (double?) waternetOptions.HeadPl2 : null);
location.HeadPl3 = (waternetOptions.HeadPl3Specified ? (double?) waternetOptions.HeadPl3 : null);
location.HeadPl4 = (waternetOptions.HeadPl4Specified ? (double?) waternetOptions.HeadPl4 : null);
location.SlopeDampingPiezometricHeightPolderSide = waternetOptions.SlopeDampingFactor;
location.PlLineOffsetBelowDikeTopAtRiver = waternetOptions.Pl1BelowCrestRiverside;
location.PlLineOffsetBelowDikeTopAtPolder = waternetOptions.Pl1BelowCrestPolderside;
location.PlLineOffsetBelowShoulderBaseInside = waternetOptions.Pl1BelowShoulderCrestPolderside;
location.PlLineOffsetBelowDikeToeAtPolder = waternetOptions.Pl1BelowToeDikePolderside;
SurfaceLine2 surfaceLine = dikeSurfaceLines.First(n => n.Name.Equals(inputLocation.SurfaceLineName));
location.SurfaceLine2 = surfaceLine;
if (inputLocation.DesignScenarios != null)
{
for (int j = 0; j < inputLocation.DesignScenarios.Length; j++)
{
var designScenario = new DesignScenario();
var inputLocationDesignScenario = inputLocation.DesignScenarios[j];
designScenario.RiverLevel = inputLocationDesignScenario.RiverLevel;
designScenario.RiverLevelLow = (inputLocationDesignScenario.RiverLevelLowSpecified ? (double?)inputLocationDesignScenario.RiverLevelLow : null);
designScenario.DikeTableHeight = (inputLocationDesignScenario.DikeTableHeightSpecified ? (double?)inputLocationDesignScenario.DikeTableHeight : null);
designScenario.PlLineOffsetBelowDikeTopAtRiver = (inputLocationDesignScenario.PlLineOffsetBelowDikeTopAtRiverSpecified ? (double?)inputLocationDesignScenario.PlLineOffsetBelowDikeTopAtRiver : null);
designScenario.PlLineOffsetBelowDikeTopAtPolder = (inputLocationDesignScenario.PlLineOffsetBelowDikeTopAtPolderSpecified ? (double?)inputLocationDesignScenario.PlLineOffsetBelowDikeTopAtPolder : null);
designScenario.PlLineOffsetBelowShoulderBaseInside = (inputLocationDesignScenario.PlLineOffsetBelowShoulderBaseInsideSpecified ? (double?)inputLocationDesignScenario.PlLineOffsetBelowShoulderBaseInside : null);
designScenario.PlLineOffsetBelowDikeToeAtPolder = (inputLocationDesignScenario.PlLineOffsetBelowDikeToeAtPolderSpecified ? (double?)inputLocationDesignScenario.PlLineOffsetBelowDikeToeAtPolder : null);
designScenario.UsePlLineOffsetBelowDikeCrestMiddle = inputLocationDesignScenario.PlLineOffsetBelowDikeCrestMiddleSpecified;
designScenario.PlLineOffsetBelowDikeCrestMiddle = inputLocationDesignScenario.PlLineOffsetBelowDikeCrestMiddle;
designScenario.UsePlLineOffsetFactorBelowShoulderCrest = inputLocationDesignScenario.PlLineOffsetFactorBelowShoulderCrestSpecified;
designScenario.PlLineOffsetFactorBelowShoulderCrest = inputLocationDesignScenario.PlLineOffsetFactorBelowShoulderCrest;
designScenario.HeadPl3 = (inputLocationDesignScenario.HeadPl3Specified ? (double?)inputLocationDesignScenario.HeadPl3 : null);
designScenario.HeadPl4 = (inputLocationDesignScenario.HeadPl4Specified ? (double?)inputLocationDesignScenario.HeadPl4 : null);
designScenario.UpliftCriterionStability = (inputLocationDesignScenario.UpliftCriterionStabilitySpecified ? (double?)inputLocationDesignScenario.UpliftCriterionStability : null);
designScenario.UpliftCriterionPiping = (inputLocationDesignScenario.UpliftCriterionStabilitySpecified ? (double?)inputLocationDesignScenario.UpliftCriterionPiping : null);
designScenario.RequiredSafetyFactorStabilityInnerSlope = (inputLocationDesignScenario.RequiredSafetyFactorStabilityInnerSlopeSpecified ? (double?)inputLocationDesignScenario.RequiredSafetyFactorStabilityInnerSlope : null);
designScenario.RequiredSafetyFactorStabilityOuterSlope = (inputLocationDesignScenario.RequiredSafetyFactorStabilityOuterSlopeSpecified ? (double?)inputLocationDesignScenario.RequiredSafetyFactorStabilityOuterSlope : null);
designScenario.RequiredSafetyFactorPiping = (inputLocationDesignScenario.RequiredSafetyFactorPipingSpecified ? (double?)inputLocationDesignScenario.RequiredSafetyFactorPiping : null);
location.Scenarios.Add(designScenario);
}
}
dikeLocations.Add(location);
}
}
private static void TransferSoilProfiles1D(Io.XmlInput.SoilProfile1D[] inputSoilProfiles1D,
IList dikeSoilProfiles, SoilList soils)
{
if (inputSoilProfiles1D != null)
{
for (int i = 0; i < inputSoilProfiles1D.Length; i++)
{
var soilProfile1D = new SoilProfile1D();
var inputSoilProfile1D = inputSoilProfiles1D[i];
soilProfile1D.Name = inputSoilProfile1D.Name;
soilProfile1D.BottomLevel = inputSoilProfile1D.BottomLevel;
// AddPreconsolidationStresses(inputSoilProfile1D, soilProfile1D); for 2D only
AddLayers(inputSoilProfile1D, soilProfile1D, soils);
}
}
}
// private static void AddPreconsolidationStresses(Io.XmlInput.SoilProfile1D inputSoilProfile1D , SoilProfile1D soilProfile1D)
// {
// for (int i = 0; i < inputSoilProfile1D.PreconsolidationStresses.Length; i++)
// {
// var precon = new PreConsolidationStress();
// var inputPrecon = inputSoilProfile1D.PreconsolidationStresses[i];
// precon.Name = inputPrecon.Name;
// precon.StressValue = inputPrecon.StressValue;
// precon.X = inputPrecon.X;
// precon.Z = inputPrecon.Z;
// }
// } To be used for 2D
private static void AddLayers(Io.XmlInput.SoilProfile1D inputSoilProfile1D, SoilProfile1D soilProfile1D, SoilList soils)
{
for (int i = 0; i < inputSoilProfile1D.Layers.Length; i++)
{
var layer = new SoilLayer1D();
var inputLayer = inputSoilProfile1D.Layers[i];
layer.Name = inputLayer.Name;
layer.Soil = soils.GetSoilByName(inputLayer.SoilName);
layer.TopLevel = inputLayer.TopLevel;
layer.IsAquifer = inputLayer.IsAquifer;
layer.WaterpressureInterpolationModel = ConversionHelper.ConvertToWaterpressureInterpolationModel(
inputLayer.WaterpressureInterpolationModel);
}
}
private static void TransferSegments(Io.XmlInput.Segment[] inputSegments, IList segments )
{
for (int i = 0; i < inputSegments.Length; i++)
{
var segment = new Segment();
var inputSegment = inputSegments[i];
segment.Name = inputSegment.Name;
AddSoilProfileProbabilities(inputSegment, segment);
segments.Add(segment);
}
}
private static void AddSoilProfileProbabilities(Io.XmlInput.Segment inputSegment, Segment segment)
{
for (int i = 0; i < inputSegment.SoilGeometryProbability.Length; i++)
{
var soilGeometryProbability = new SoilGeometryProbability();
var inputSoilGeometryProbability = inputSegment.SoilGeometryProbability[i];
soilGeometryProbability.Probability = inputSoilGeometryProbability.Probability;
if (inputSoilGeometryProbability.FailureMechanismSystemTypeSpecified)
{
//soilGeometryProbability.SegmentFailureMechanismType = inputSoilGeometryProbability.FailureMechanismSystemType;
}
if (inputSoilGeometryProbability.SoilGeometryType == SegmentSoilGeometryProbabilitySoilGeometryType.SoilGeometry1D)
{
soilGeometryProbability.SoilGeometry1DName = inputSoilGeometryProbability.SoilGeometryName;
}
else
{
soilGeometryProbability.SoilGeometry2DName = inputSoilGeometryProbability.SoilGeometryName;
}
}
}
private static SoilProfile1D FindSoilProfile1DByName(IList soilProfiles, string name)
{
foreach (var soilProfile1D in soilProfiles)
{
if (soilProfile1D.Name == name)
return soilProfile1D;
}
return null;
}
private static SoilProfile2D FindSoilProfile2DByName(string name)
{
// Todo Implement
return new SoilProfile2D();
}
}
}