Index: DamEngine/trunk/src/Deltares.DamEngine.Calculators/KernelWrappers/MacroStabilityCommon/MacroStabilityIo/InterfaceConversionHelper.cs =================================================================== diff -u --- DamEngine/trunk/src/Deltares.DamEngine.Calculators/KernelWrappers/MacroStabilityCommon/MacroStabilityIo/InterfaceConversionHelper.cs (revision 0) +++ DamEngine/trunk/src/Deltares.DamEngine.Calculators/KernelWrappers/MacroStabilityCommon/MacroStabilityIo/InterfaceConversionHelper.cs (revision 6322) @@ -0,0 +1,176 @@ +// Copyright (C) Stichting Deltares 2024. 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 Deltares.DamEngine.Data.General; +using Deltares.DamEngine.Data.Geotechnics; +using Deltares.MacroStability.Io.XmlInput; +using CharacteristicPointType = Deltares.MacroStability.Io.XmlInput.CharacteristicPointType; + +namespace Deltares.DamEngine.Calculators.KernelWrappers.MacroStabilityCommon.MacroStabilityIo; + +public class InterfaceConversionHelper +{ + /// Converts SearchAlgorithm to MStabSearchMethod. + /// The MacroStability search algorithm. + /// + /// + public static StabilitySearchMethod ConvertToDamSearchMethod(SearchAlgorithmType searchAlgorithm) + { + if (searchAlgorithm != SearchAlgorithmType.Grid && searchAlgorithm != SearchAlgorithmType.Genetic && searchAlgorithm != SearchAlgorithmType.BeeswarmAndLevenbergMarquardt) + { + throw new ArgumentException($"Unsupported search algorithm: {searchAlgorithm}"); + } + + var translationTable = new Dictionary + { + { + SearchAlgorithmType.Grid, StabilitySearchMethod.Grid + }, + { + SearchAlgorithmType.BeeswarmAndLevenbergMarquardt, StabilitySearchMethod.BeeSwarm + } + }; + return translationTable[searchAlgorithm]; + } + + /// Converts MStabSearchMethod to SearchAlgorithm. + /// The Dam search algorithm. + /// + /// + public static SearchAlgorithmType ConvertToMacroStabilitySearchMethod(StabilitySearchMethod stabilitySearchMethod) + { + var translationTable = new Dictionary + { + { + StabilitySearchMethod.Grid, SearchAlgorithmType.Grid + }, + { + StabilitySearchMethod.BeeSwarm, SearchAlgorithmType.BeeswarmAndLevenbergMarquardt + } + }; + return translationTable[stabilitySearchMethod]; + } + + /// Converts to ModelOptions type. + /// the MStabModelType. + /// the MacroStability ModelOption + /// + public static StabilityModelOption ConvertToModelOptions(StabilityModelType stabilityModelType) + { + // For the Macrostability kernel, the only supported options for now are Bishop and UpliftVan. + if (stabilityModelType != StabilityModelType.UpliftVan && stabilityModelType != StabilityModelType.Bishop) + { + throw new ArgumentException($"Unsupported MStabModelType: {stabilityModelType}"); + } + + var translationTable = new Dictionary + { + { + StabilityModelType.UpliftVan, StabilityModelOption.UpliftVan + }, + { + StabilityModelType.Bishop, StabilityModelOption.Bishop + } + }; + return translationTable[stabilityModelType]; + } + + /// Converts to GridOrientation. + /// The MStabGridPosition. + /// + public static OrientationType ConvertToGridOrientation(StabilityGridPosition stabilityGridPosition) + { + var translationTable = new Dictionary + { + { + StabilityGridPosition.Right, OrientationType.Inwards + }, + { + StabilityGridPosition.Left, OrientationType.Outwards + } + }; + return translationTable[stabilityGridPosition]; + } + + /// Converts the type of to macro stability characteristic point. + /// Type of the dam characteristic point. + /// + public static CharacteristicPointType ConvertToMacroStabilityCharacteristicPointType(Deltares.DamEngine.Data.Geotechnics.CharacteristicPointType damCharacteristicPointType) + { + var translationTable = new Dictionary + { + { + Deltares.DamEngine.Data.Geotechnics.CharacteristicPointType.BottomDitchPolderSide, CharacteristicPointType.BottomDitchPolderSide + }, + { + Deltares.DamEngine.Data.Geotechnics.CharacteristicPointType.BottomDitchDikeSide, CharacteristicPointType.BottomDitchDikeSide + }, + { + Deltares.DamEngine.Data.Geotechnics.CharacteristicPointType.DikeToeAtPolder, CharacteristicPointType.DikeToeAtPolder + }, + { + Deltares.DamEngine.Data.Geotechnics.CharacteristicPointType.DikeToeAtRiver, CharacteristicPointType.DikeToeAtRiver + }, + { + Deltares.DamEngine.Data.Geotechnics.CharacteristicPointType.DikeTopAtPolder, CharacteristicPointType.DikeTopAtPolder + }, + { + Deltares.DamEngine.Data.Geotechnics.CharacteristicPointType.DikeTopAtRiver, CharacteristicPointType.DikeTopAtRiver + }, + { + Deltares.DamEngine.Data.Geotechnics.CharacteristicPointType.DitchDikeSide, CharacteristicPointType.DitchDikeSide + }, + { + Deltares.DamEngine.Data.Geotechnics.CharacteristicPointType.DitchPolderSide, CharacteristicPointType.DitchPolderSide + }, + { + Deltares.DamEngine.Data.Geotechnics.CharacteristicPointType.None, CharacteristicPointType.None + }, + { + Deltares.DamEngine.Data.Geotechnics.CharacteristicPointType.ShoulderBaseInside, CharacteristicPointType.ShoulderBaseInside + }, + { + Deltares.DamEngine.Data.Geotechnics.CharacteristicPointType.ShoulderBaseOutside, CharacteristicPointType.ShoulderBaseOutside + }, + { + Deltares.DamEngine.Data.Geotechnics.CharacteristicPointType.ShoulderTopInside, CharacteristicPointType.ShoulderTopInside + }, + { + Deltares.DamEngine.Data.Geotechnics.CharacteristicPointType.ShoulderTopOutside, CharacteristicPointType.ShoulderTopOutside + }, + { + Deltares.DamEngine.Data.Geotechnics.CharacteristicPointType.SurfaceLevelInside, CharacteristicPointType.SurfaceLevelInside + }, + { + Deltares.DamEngine.Data.Geotechnics.CharacteristicPointType.SurfaceLevelOutside, CharacteristicPointType.SurfaceLevelOutside + }, + { + Deltares.DamEngine.Data.Geotechnics.CharacteristicPointType.TrafficLoadInside, CharacteristicPointType.TrafficLoadInside + }, + { + Deltares.DamEngine.Data.Geotechnics.CharacteristicPointType.TrafficLoadOutside, CharacteristicPointType.TrafficLoadOutside + } + }; + return translationTable[damCharacteristicPointType]; + } +} \ No newline at end of file Index: DamEngine/trunk/src/Deltares.DamEngine.Calculators/KernelWrappers/MacroStabilityCommon/MacroStabilityIo/FillMacroStabilityInterfaceInputFromEngine.cs =================================================================== diff -u --- DamEngine/trunk/src/Deltares.DamEngine.Calculators/KernelWrappers/MacroStabilityCommon/MacroStabilityIo/FillMacroStabilityInterfaceInputFromEngine.cs (revision 0) +++ DamEngine/trunk/src/Deltares.DamEngine.Calculators/KernelWrappers/MacroStabilityCommon/MacroStabilityIo/FillMacroStabilityInterfaceInputFromEngine.cs (revision 6322) @@ -0,0 +1,248 @@ +// Copyright (C) Stichting Deltares 2024. 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.Concurrent; +using System.Collections.Generic; +using System.Linq; +using Deltares.DamEngine.Data.General; +using Deltares.DamEngine.Data.Geometry; +using Deltares.DamEngine.Data.Geotechnics; +using Deltares.DamEngine.Data.Standard; +using Deltares.Geo.Common.Geometry; +using Deltares.DamEngine.Calculators.KernelWrappers.Common; +using Deltares.MacroStability.Io.XmlInput; +using Soil = Deltares.Geo.Common.Geometry.Soil; +using SurfaceLine2 = Deltares.DamEngine.Data.Geotechnics.SurfaceLine2; +using Waternet = Deltares.DamEngine.Data.Geometry.Waternet; + +namespace Deltares.DamEngine.Calculators.KernelWrappers.MacroStabilityCommon.MacroStabilityIo; + +internal class ObjectRegistry +{ + private readonly ConcurrentDictionary objects = new ConcurrentDictionary(); + private int currentId; + + public int GetId(object @object) + { + AddsObjectToRegistryIfNotExistsInRegistry(@object); + return objects[@object]; + } + + public object GetObject(int id) + { + return objects.First>((Func, bool>) (x => x.Value == id)).Key; + } + + private void AddsObjectToRegistryIfNotExistsInRegistry(object @object) + { + if (IsObjectInRegistry(@object)) + return; + AddObjectToRegistry(@object); + } + + private bool IsObjectInRegistry(object @object) => this.objects.ContainsKey(@object); + + private void AddObjectToRegistry(object @object) + { + objects.AddOrUpdate(@object, currentId, (Func) ((o, i) => objects.Count + 1)); + ++currentId; + } +} + +public class FillMacroStabilityInterfaceInputFromEngine +{ + /// Gets or sets the UpliftVan calculation grid. + /// The uplift van calculation grid. + public UpliftVanCalculationGrid UpliftVanCalculationGrid { get; set; } + + /// Gets or sets the Bishop calculation grid. + /// The bishop calculation grid. + public BishopCalculationGrid BishopCalculationGrid { get; set; } + + /// Gets or sets the traffic load. + /// The traffic load. + public TrafficLoad TrafficLoad { get; set; } + + + public MacroStability.Interface.MacroStabilityInterface CreateMacroStabilityInterface(DamKernelInput damKernelInput, + MStabParameters mStabParameters, Waternet waterNet) + { + FullInputModelType fullInputModelType = FillFullInputModelType(damKernelInput, mStabParameters, waterNet); + + var macroStabilityInterface = new MacroStability.Interface.MacroStabilityInterface(fullInputModelType); + return macroStabilityInterface; + + // macroStabilityInput.StabilityModel.UpliftVanCalculationGrid = new KernelUpliftVanCalculationGrid(); + // macroStabilityInput.StabilityModel.ConstructionStages.Add(new ConstructionStage()); + // ConstructionStage lastStage = macroStabilityInput.StabilityModel.ConstructionStages.Last(); + // + // TransferStabilityModelProperties(mStabParameters, macroStabilityInput.StabilityModel); + // TransferSlipPlaneConstraints(damKernelInput.Location, macroStabilityInput.StabilityModel.SlipPlaneConstraints); + // TransferSoils(damKernelInput.Location.SoilList, macroStabilityInput.StabilityModel.Soils, lastStage.FixedSoilStresses); + // + // lastStage.SoilProfile = new SoilProfile(); + // lastStage.PreconsolidationStresses = new List(); + // lastStage.WaterDefinition = WaterDefinition.WaterNet; + // TransferSoilProfile(damKernelInput.SubSoilScenario.SoilProfile2D, lastStage.SoilProfile, lastStage.PreconsolidationStresses); + // + // macroStabilityInput.PreprocessingInput.PreConstructionStages.Add(new PreConstructionStage()); + // PreConstructionStage preConstructionLastStage = macroStabilityInput.PreprocessingInput.PreConstructionStages.Last(); + // preConstructionLastStage.SurfaceLine = new SurfaceLine(); + // TransferSurfaceLine(damKernelInput.Location.SurfaceLine, preConstructionLastStage.SurfaceLine); + // + // lastStage.Waternet = new KernelWaternet(); + // TransferWaternet(waterNet, lastStage.Waternet); + // + // SearchAreaConditions preprocessingSearchAreaConditions = macroStabilityInput.PreprocessingInput.SearchAreaConditions; + // switch (mStabParameters.Model) + // { + // case StabilityModelType.Bishop: + // TransferBishopSearchAreaSettings(preprocessingSearchAreaConditions); + // TransferBishopCalculationGrid(macroStabilityInput.StabilityModel.BishopCalculationCircle); + // break; + // case StabilityModelType.UpliftVan: + // TransferUpliftVanSearchAreaSettings(preprocessingSearchAreaConditions); + // TransferUpliftVanCalculationGrid(macroStabilityInput.StabilityModel.UpliftVanCalculationGrid); + // break; + // default: + // throw new NotImplementedException(nameof(mStabParameters.Model)); + // } + // + // lastStage.UniformLoads = new List(); + // TransferUniformLoads(TrafficLoad, lastStage.UniformLoads); + // TransferTrafficLoadDegreeOfConsolidation(damKernelInput.Location.TrafficLoadDegreeOfConsolidations, lastStage); + // return macroStabilityInput; + } + + private FullInputModelType FillFullInputModelType(DamKernelInput damKernelInput, MStabParameters mStabParameters, Waternet waterNet) + { + // The objectregistry is to be used to keep track of all keys of some of the referenced objects, see the C#wrapper for where and when to use it (Creators) + var objectRegistry = new ObjectRegistry(); + var fullInputModelType = new FullInputModelType + { + PreprocessingInput = TransferPreprocessingInput(damKernelInput), + StabilityModel = TransferStabilityModel(damKernelInput, mStabParameters, objectRegistry), + VersionInfo = TransferVersionInfo() + }; + return fullInputModelType; + } + + private PreprocessingInputType TransferPreprocessingInput(DamKernelInput damKernelInput) + { + if (damKernelInput == null) + return null; + + var preprocessingInputType = new PreprocessingInputType(); + preprocessingInputType.PreConstructionStages = new PreConstructionStageType[1]; + preprocessingInputType.PreConstructionStages[0] = new PreConstructionStageType(); + PreConstructionStageType preConstructionLastStage = preprocessingInputType.PreConstructionStages.Last(); + preConstructionLastStage.Surfaceline = new SurfaceLineType(); + preConstructionLastStage.CreateWaternet = false; // #Bka: I can NOT find any use of this at the engine side so this must be false!? + TransferSurfaceLine(damKernelInput.Location.SurfaceLine, preConstructionLastStage.Surfaceline); + //preprocessingInputType.SearchAreaConditions = + return preprocessingInputType; + } + + private void TransferSurfaceLine(SurfaceLine2 surfaceLineInput, SurfaceLineType surfaceLine) + { + surfaceLine.CharacteristicPoints = new SurfaceLineTypeCharacteristicPoint[surfaceLine.CharacteristicPoints.Length]; + int i = 0; + foreach (var damCharPoint in surfaceLineInput.CharacteristicPoints) + { + var kernelCharPoint = new SurfaceLineTypeCharacteristicPoint + { + CharacteristicPointType = InterfaceConversionHelper.ConvertToMacroStabilityCharacteristicPointType(damCharPoint.CharacteristicPointType), + GeometryPoint = new Point2DType + { + X = damCharPoint.Point.X, + Z = damCharPoint.Point.Z + } + }; + surfaceLine.CharacteristicPoints[i] = kernelCharPoint; + i++; + } + } + + private StabilityInputType TransferStabilityModel(DamKernelInput damKernelInput, MStabParameters mStabParameters, ObjectRegistry objectRegistry) + { + if (damKernelInput == null || mStabParameters == null) + return null; + + var stabilityInputType = new StabilityInputType + { + UpliftVanCalculationGrid = new UpliftVanCalculationGridType() + }; + stabilityInputType.ConstructionStages.AddRange([new ConstructionStageInputType()]); + ConstructionStageInputType lastStage = stabilityInputType.ConstructionStages.Last(); + + TransferStabilityModelProperties(mStabParameters, stabilityInputType); + + //stabilityInputType.StabilityModel = damKernelInput.StabilityModel; + return stabilityInputType; + } + + + + private VersionInfoType TransferVersionInfo() + { + var versionInfoType = new VersionInfoType + { + FileVersion = 2, + AssemblyVersion = "2.0.0.0", + AssemblyName = "Deltares.DamEngine" + }; + return versionInfoType; + } + + private void TransferStabilityModelProperties(MStabParameters mStabParameters, StabilityInputType kernelStabilityInput) + { + kernelStabilityInput.MoveGrid = true; // is not in DamEngine but MUST be true as we use the brute force approach. + kernelStabilityInput.MaximumSliceWidth = 1.0; // is not in DamEngine datamodel + // For Bishop, only Grid is possible however if Bishop/UpliftVan was selected, then the SearchAlgorithm concerns only Uplift-Van + kernelStabilityInput.SearchAlgorithm = mStabParameters.Model == StabilityModelType.Bishop ? SearchAlgorithmType.Grid : InterfaceConversionHelper.ConvertToMacroStabilitySearchMethod(mStabParameters.SearchMethod); + if (kernelStabilityInput.SearchAlgorithm == SearchAlgorithmType.BeeswarmAndLevenbergMarquardt) + { + CreateDefaultBeeSwarmOptions(kernelStabilityInput); + } + + kernelStabilityInput.ModelOption = InterfaceConversionHelper.ConvertToModelOptions(mStabParameters.Model); + kernelStabilityInput.Orientation = InterfaceConversionHelper.ConvertToGridOrientation(mStabParameters.GridPosition); + kernelStabilityInput.NumberOfRefinementsGrid = 2; + kernelStabilityInput.NumberOfRefinementsTangentLines = 2; + } + + private void CreateDefaultBeeSwarmOptions(StabilityInputType kernelStabilityInput) + { + kernelStabilityInput.BeeswarmAlgorithmOptions = new BeeSwarmAlgorithmOptionsType() + { + EliteCount = 2, + Seed = 1, + CrossOver = 0.3, + Beta = 0.4, + Delta = 0.7, + DifferentialWeight = 0.3, + MaximumNonImprovingGenerations = 10, + PopulationCount = 200, + GenerationCount = 40 + }; + } +} \ No newline at end of file