// Copyright (C) Stichting Deltares 2017. All rights reserved.
//
// This file is part of the application DAM - UI.
//
// DAM - UI is free software: you can redistribute it and/or modify
// it under the terms of the GNU 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 General Public License for more details.
//
// You should have received a copy of the GNU 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.Io.XmlInput;
using Deltares.Geotechnics.SurfaceLines;
using Soil = Deltares.Geotechnics.Soils.Soil;
using SurfaceLine = Deltares.DamEngine.Io.XmlInput.SurfaceLine;
namespace Deltares.Dam.Data.DamEngineIo
{
///
/// Create XML Input object from DAM UI project
///
public class FillXmlInputFromDamUi
{
///
/// Creates the input.
///
/// The dam project data.
///
public static Input CreateInput(DamProjectData damProjectData)
{
ValidateDamProjectData(damProjectData);
Input input = new Input();
input.DamProjectType = ConversionHelper.ConvertToInputDamProjectType(damProjectData.DamProjectType);
Dike dike = damProjectData.WaterBoard.Dikes[0];
// Process locations
input.Locations = new DamEngine.Io.XmlInput.Location[dike.Locations.Count];
TransferLocations(dike.Locations, input.Locations);
// Process surfacelines
List localSurfaceLines = new List();
for (int i = 0; i < dike.Locations.Count; i++)
{
var crtSurfaceLine = dike.Locations[i].LocalXZSurfaceLine2;
if (!localSurfaceLines.Any(sl => sl.Name.Equals(crtSurfaceLine.Name)))
{
localSurfaceLines.Add(crtSurfaceLine);
}
}
input.SurfaceLines = new SurfaceLine[localSurfaceLines.Count];
TransferSurfaceLines(localSurfaceLines, input.SurfaceLines);
// Process soils
input.Soils = new DamEngine.Io.XmlInput.Soil[dike.SoilList.Soils.Count];
TransferSoils(dike.SoilList.Soils, input.Soils);
// Process soilprofiles
if (dike.SoilProfiles != null)
{
var profilesCount = dike.SoilProfiles.Count;
input.SoilProfiles1D = new DamEngine.Io.XmlInput.SoilProfile1D[profilesCount];
TransferSoilProfiles1D(dike.SoilProfiles, input.SoilProfiles1D);
}
// Process segments
if (damProjectData.WaterBoard.Segments != null)
{
input.Segments = new DamEngine.Io.XmlInput.Segment[damProjectData.WaterBoard.Segments.Count];
TransferSegments(damProjectData.WaterBoard.Segments, input.Segments);
}
return input;
}
private static void ValidateDamProjectData(DamProjectData damProjectData)
{
if (damProjectData.WaterBoard == null)
{
throw new NullReferenceException("No dike defined in this project");
}
if (damProjectData.WaterBoard.Dikes == null || damProjectData.WaterBoard.Dikes.Count != 1)
{
throw new NullReferenceException("The project should contain exactly 1 dike");
}
var dike = damProjectData.WaterBoard.Dikes[0];
if (dike.Locations == null)
{
throw new NullReferenceException("No locations defined in this project");
}
foreach (Location location in dike.Locations)
{
if (location.Scenarios == null)
{
throw new NullReferenceException("No scenarios defined in location");
}
}
if (dike.SurfaceLines2 == null)
{
throw new NullReferenceException("No surfacelines defined in this project");
}
if (dike.SoilList == null)
{
throw new NullReferenceException("No soils defined in this project");
}
if (dike.SoilList.Soils == null)
{
throw new NullReferenceException("No soils defined in this project");
}
if (dike.SoilProfiles == null)
{
throw new NullReferenceException("No soilprofiles1D defined in this project");
}
}
private static void TransferSoils(List soils, DamEngine.Io.XmlInput.Soil[] inputSoils)
{
for (int i = 0; i < soils.Count; i++)
{
var soil = soils[i];
var inputSoil = new DamEngine.Io.XmlInput.Soil();
inputSoil.Name = soil.Name;
inputSoil.BeddingAngleSpecified = !Double.IsNaN(soil.BeddingAngle);
inputSoil.BeddingAngle = soil.BeddingAngle;
inputSoil.DiameterD70Specified = !Double.IsNaN(soil.DiameterD70);
inputSoil.DiameterD70 = soil.DiameterD70;
inputSoil.DiameterD90Specified = !Double.IsNaN(soil.DiameterD70);
inputSoil.DiameterD90 = soil.DiameterD90;
inputSoil.PermeabKxSpecified = !Double.IsNaN(soil.DiameterD90);
inputSoil.PermeabKx = soil.PermeabKx;
inputSoil.WhitesConstantSpecified = !Double.IsNaN(soil.WhitesConstant);
inputSoil.WhitesConstant = soil.WhitesConstant;
inputSoils[i] = inputSoil;
}
}
private static void TransferSurfaceLines(IList dikeSurfaceLines, SurfaceLine[] inputSurfaceLines)
{
for (int i = 0; i < dikeSurfaceLines.Count; i++)
{
var surfaceLine = dikeSurfaceLines[i];
var inputSurfaceLine = new SurfaceLine();
inputSurfaceLine.Name = surfaceLine.Name;
inputSurfaceLine.Points = new SurfaceLinePoint[surfaceLine.CharacteristicPoints.Count];
for (int j = 0; j < surfaceLine.CharacteristicPoints.Count; j++)
{
var characteristicPoint = surfaceLine.CharacteristicPoints[j];
var inputPoint = new SurfaceLinePoint()
{
PointType = ConversionHelper.ConvertToInputPointType(characteristicPoint.CharacteristicPointType),
X = characteristicPoint.X,
Z = characteristicPoint.Z
};
inputSurfaceLine.Points[j] = inputPoint;
}
inputSurfaceLines[i] = inputSurfaceLine;
}
}
private static void TransferLocations(IList dikeLocations, DamEngine.Io.XmlInput.Location[] inputLocations)
{
for (int i = 0; i < dikeLocations.Count; i++)
{
var location = dikeLocations[i];
var inputLocation = new DamEngine.Io.XmlInput.Location();
inputLocation.SurfaceLineName = location.LocalXZSurfaceLine2.Name;
inputLocation.SegmentName = location.Segment.Name;
var waternetOptions = new LocationWaternetOptions();
waternetOptions.PhreaticLineCreationMethod = ConversionHelper.ConvertToInputPhreaticLineCreationMethod(location.PLLineCreationMethod);
waternetOptions.IntrusionVerticalWaterPressure = ConversionHelper.ConvertToInputIntrusionVerticalWaterPressure(location.IntrusionVerticalWaterPressure ?? IntrusionVerticalWaterPressureType.Standard);
waternetOptions.PolderLevel = location.PolderLevel;
waternetOptions.DampingFactorPL3 = location.DampingFactorPL4;
waternetOptions.DampingFactorPL4 = location.DampingFactorPL3;
waternetOptions.PenetrationLength = location.PenetrationLength;
waternetOptions.Pl1BelowCrestMiddleSpecified = location.PlLineOffsetBelowDikeCrestMiddle.HasValue;
waternetOptions.Pl1BelowCrestMiddle = location.PlLineOffsetBelowDikeCrestMiddle ?? 0.0;
waternetOptions.Pl1FactorBelowShoulderCrestSpecified = location.UsePlLineOffsetFactorBelowShoulderCrest ?? false;
waternetOptions.Pl1FactorBelowShoulderCrest = location.PlLineOffsetFactorBelowShoulderCrest ?? 0.0;
waternetOptions.DryPl1BelowCrestMiddleSpecified = location.PlLineOffsetDryBelowDikeCrestMiddle.HasValue;
waternetOptions.DryPl1BelowCrestMiddle = location.PlLineOffsetDryBelowDikeCrestMiddle ?? 0.0;
waternetOptions.DryPl1FactorBelowShoulderCrestSpecified = location.UsePlLineOffsetDryFactorBelowShoulderCrest ?? false;
waternetOptions.DryPl1FactorBelowShoulderCrest = location.PlLineOffsetDryFactorBelowShoulderCrest ?? 0.0;
waternetOptions.HeadPl2Specified = location.HeadPL2.HasValue;
waternetOptions.HeadPl2 = location.HeadPL2 ?? 0.0;
waternetOptions.HeadPl3Specified = location.HeadPl3.HasValue;
waternetOptions.HeadPl3 = location.HeadPl3 ?? 0.0;
waternetOptions.HeadPl4Specified = location.HeadPl4.HasValue;
waternetOptions.HeadPl4 = location.HeadPl4 ?? 0.0;
waternetOptions.SlopeDampingFactor = location.SlopeDampingPiezometricHeightPolderSide;
waternetOptions.Pl1BelowCrestRiverside = location.PlLineOffsetBelowDikeTopAtRiver;
waternetOptions.Pl1BelowCrestPolderside = location.PlLineOffsetBelowDikeTopAtPolder;
waternetOptions.Pl1BelowShoulderCrestPolderside = location.PlLineOffsetBelowShoulderBaseInside;
waternetOptions.Pl1BelowToeDikePolderside = location.PlLineOffsetBelowDikeToeAtPolder;
inputLocation.WaternetOptions = waternetOptions;
inputLocation.DesignScenarios = new LocationDesignScenario[location.Scenarios.Count];
for (int j = 0; j < location.Scenarios.Count; j++)
{
var designScenario = location.Scenarios[j];
var inputDesignScenario = new LocationDesignScenario();
inputDesignScenario.RiverLevel = designScenario.RiverLevel;
inputDesignScenario.RiverLevelLowSpecified = designScenario.RiverLevelLow.HasValue;
inputDesignScenario.RiverLevelLow = designScenario.RiverLevelLow ?? 0.0;
inputDesignScenario.DikeTableHeightSpecified = designScenario.DikeTableHeight.HasValue;
inputDesignScenario.DikeTableHeight = designScenario.DikeTableHeight ?? 0.0;
inputDesignScenario.PlLineOffsetBelowDikeTopAtRiverSpecified = designScenario.PlLineOffsetBelowDikeTopAtRiver.HasValue;
inputDesignScenario.PlLineOffsetBelowDikeTopAtRiver = designScenario.PlLineOffsetBelowDikeTopAtRiver ?? 0.0;
inputDesignScenario.PlLineOffsetBelowDikeTopAtPolderSpecified = designScenario.PlLineOffsetBelowDikeTopAtPolder.HasValue;
inputDesignScenario.PlLineOffsetBelowDikeTopAtPolder = designScenario.PlLineOffsetBelowDikeTopAtPolder ?? 0.0;
inputDesignScenario.PlLineOffsetBelowShoulderBaseInsideSpecified = designScenario.PlLineOffsetBelowShoulderBaseInside.HasValue;
inputDesignScenario.PlLineOffsetBelowShoulderBaseInside = designScenario.PlLineOffsetBelowShoulderBaseInside ?? 0.0;
inputDesignScenario.PlLineOffsetBelowDikeToeAtPolderSpecified = designScenario.PlLineOffsetBelowDikeToeAtPolder.HasValue;
inputDesignScenario.PlLineOffsetBelowDikeToeAtPolder = designScenario.PlLineOffsetBelowDikeToeAtPolder ?? 0.0;
inputDesignScenario.PlLineOffsetBelowDikeCrestMiddleSpecified = designScenario.UsePlLineOffsetBelowDikeCrestMiddle?? false;
inputDesignScenario.PlLineOffsetBelowDikeCrestMiddle = designScenario.PlLineOffsetBelowDikeCrestMiddle ?? 0.0;
inputDesignScenario.PlLineOffsetFactorBelowShoulderCrestSpecified = designScenario.UsePlLineOffsetFactorBelowShoulderCrest ?? false;
inputDesignScenario.PlLineOffsetFactorBelowShoulderCrest = designScenario.PlLineOffsetFactorBelowShoulderCrest ?? 0.0;
inputDesignScenario.HeadPl3Specified = designScenario.HeadPl3.HasValue;
inputDesignScenario.HeadPl3 = designScenario.HeadPl3 ?? 0.0;
inputDesignScenario.HeadPl4Specified = designScenario.HeadPl4.HasValue;
inputDesignScenario.HeadPl4 = designScenario.HeadPl4 ?? 0.0;
inputDesignScenario.UpliftCriterionStabilitySpecified = designScenario.UpliftCriterionStability.HasValue;
inputDesignScenario.UpliftCriterionStability = designScenario.UpliftCriterionStability ?? 0.0;
inputDesignScenario.UpliftCriterionPipingSpecified = designScenario.UpliftCriterionPiping.HasValue;
inputDesignScenario.UpliftCriterionPiping = designScenario.UpliftCriterionPiping ?? 0.0;
inputDesignScenario.RequiredSafetyFactorStabilityInnerSlopeSpecified = designScenario.RequiredSafetyFactorStabilityInnerSlope.HasValue;
inputDesignScenario.RequiredSafetyFactorStabilityInnerSlope = designScenario.RequiredSafetyFactorStabilityInnerSlope ?? 0.0;
inputDesignScenario.RequiredSafetyFactorStabilityOuterSlopeSpecified = designScenario.RequiredSafetyFactorStabilityOuterSlope.HasValue;
inputDesignScenario.RequiredSafetyFactorStabilityOuterSlope = designScenario.RequiredSafetyFactorStabilityOuterSlope ?? 0.0;
inputDesignScenario.RequiredSafetyFactorPipingSpecified = designScenario.RequiredSafetyFactorPiping.HasValue;
inputDesignScenario.RequiredSafetyFactorPiping = designScenario.RequiredSafetyFactorPiping ?? 0.0;
inputLocation.DesignScenarios[j] = inputDesignScenario;
}
inputLocations[i] = inputLocation;
}
}
private static void TransferSoilProfiles1D(IList dikeSoilProfiles, DamEngine.Io.XmlInput.SoilProfile1D[] inputSoilProfiles1D)
{
var profilesCount = dikeSoilProfiles.Count;
for (int i = 0; i < profilesCount; i++)
{
var soilProfile1D = dikeSoilProfiles[i];
var inputSoilProfile1D = new DamEngine.Io.XmlInput.SoilProfile1D
{
Name = soilProfile1D.Name,
BottomLevel = soilProfile1D.BottomLevel,
Layers1D = new SoilProfile1DLayer1D[soilProfile1D.LayerCount]
};
AddLayers1D(soilProfile1D, inputSoilProfile1D);
inputSoilProfiles1D[i] = inputSoilProfile1D;
}
}
private static void AddLayers1D(Deltares.Geotechnics.Soils.SoilProfile1D soilProfile1D, DamEngine.Io.XmlInput.SoilProfile1D inputSoilProfile1D)
{
for (int i = 0; i < soilProfile1D.LayerCount; i++)
{
var layer = soilProfile1D.Layers[i];
var inputLayer = new SoilProfile1DLayer1D
{
Name = layer.Name,
SoilName = layer.Soil.Name,
TopLevel = layer.TopLevel,
IsAquifer = layer.IsAquifer,
WaterpressureInterpolationModel = ConversionHelper.ConvertToInputWaterpressureInterpolationModel(
layer.WaterpressureInterpolationModel)
};
inputSoilProfile1D.Layers1D[i] = inputLayer;
}
}
private static void TransferSegments(IList segments, DamEngine.Io.XmlInput.Segment[] inputSegments)
{
for (int i = 0; i < segments.Count; i++)
{
var segment = segments[i];
var inputSegment = new DamEngine.Io.XmlInput.Segment
{
Name = segment.Name
};
AddSoilProfileProbabilities(segment, inputSegment);
inputSegments[i] = inputSegment;
}
}
private static void AddSoilProfileProbabilities(Segment segment, DamEngine.Io.XmlInput.Segment inputSegment)
{
if (segment.SoilProfileProbabilities != null)
{
var probabilityCount = segment.SoilProfileProbabilities.Count;
inputSegment.SoilGeometryProbability = new SegmentSoilGeometryProbability[probabilityCount];
for (int i = 0; i < probabilityCount; i++)
{
var soilGeometryProbability = segment.SoilProfileProbabilities[i];
var inputSoilGeometryProbability = new SegmentSoilGeometryProbability();
inputSoilGeometryProbability.Probability = soilGeometryProbability.Probability;
if (soilGeometryProbability.SegmentFailureMechanismType.HasValue)
{
inputSoilGeometryProbability.FailureMechanismSystemType = ConversionHelper.ConvertToInputFailureMechanismSystemType(soilGeometryProbability.SegmentFailureMechanismType.Value);
inputSoilGeometryProbability.FailureMechanismSystemTypeSpecified = true;
}
else
{
inputSoilGeometryProbability.FailureMechanismSystemTypeSpecified = false;
}
if (soilGeometryProbability.SoilGeometryType == SoilGeometryType.SoilGeometry1D)
{
inputSoilGeometryProbability.SoilProfileName = soilGeometryProbability.SoilGeometryName;
}
else
{
inputSoilGeometryProbability.SoilProfileName = soilGeometryProbability.SoilGeometry2DName;
}
inputSoilGeometryProbability.SoilProfileType = ConversionHelper.ConvertToInputSoilGeometryType(soilGeometryProbability.SoilGeometryType);
inputSegment.SoilGeometryProbability[i] = inputSoilGeometryProbability;
}
}
}
}
}