// 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 System.Data; using System.IO; using Deltares.DamEngine.Calculators.DikesDesign; using Deltares.DamEngine.Calculators.KernelWrappers.Common; using Deltares.DamEngine.Calculators.KernelWrappers.Interfaces; using Deltares.DamEngine.Calculators.KernelWrappers.MacroStabilityCommon; using Deltares.DamEngine.Calculators.KernelWrappers.MacroStabilityCommon.MacroStabilityIo; using Deltares.DamEngine.Calculators.KernelWrappers.MacroStabilityInwards; using Deltares.DamEngine.Data.Design; using Deltares.DamEngine.Data.General; using Deltares.DamEngine.Data.General.PlLines; using Deltares.DamEngine.Data.General.Results; using Deltares.DamEngine.Data.Geometry; using Deltares.DamEngine.Data.Geotechnics; using Deltares.DamEngine.Data.Standard; using Deltares.DamEngine.Data.Standard.Calculation; using Deltares.DamEngine.Data.Standard.Logging; using Deltares.MacroStability.CSharpWrapper; using Point2D = Deltares.DamEngine.Data.Geometry.Point2D; namespace Deltares.DamEngine.Calculators.KernelWrappers.MacroStabilityOutwards; /// /// Class for the wrapper around the Stability Outwards calculator /// /// public class MacroStabilityOutwardsKernelWrapper : IKernelWrapper { readonly MacroStabilityCommonHelper macroStabilityCommonHelper; private Calculator stabilityCalculator; private string fileNameForCalculation; /// /// Initializes a new instance of the class. /// public MacroStabilityOutwardsKernelWrapper() { macroStabilityCommonHelper = new MacroStabilityCommonHelper(); } /// /// Gets or sets the failure mechanism parameters for mstab. /// /// /// The failure mechanism parameters mstab. /// public FailureMechanismParametersMStab FailureMechanismParametersMStab { get; set; } /// /// Prepares the specified dam kernel input. /// /// The dam kernel input. /// The number of the current iteration. /// The kernel data input. /// The kernel data output. /// /// Result of the preparation /// public PrepareResult Prepare(DamKernelInput damKernelInput, int iterationIndex, out IKernelDataInput kernelDataInput, out IKernelDataOutput kernelDataOutput) { fileNameForCalculation = ""; var macroStabilityInput = new MacroStabilityKernelDataInput(); kernelDataInput = macroStabilityInput; var macroStabilityOutput = new MacroStabilityOutput { CalculationResult = CalculationResult.NoRun }; kernelDataOutput = macroStabilityOutput; if (damKernelInput.SubSoilScenario.SegmentFailureMechanismType != null && damKernelInput.SubSoilScenario.SegmentFailureMechanismType.Value.In(SegmentFailureMechanismType.Stability, SegmentFailureMechanismType.All)) { try { if (FailureMechanismParametersMStab.MStabParameters.Model != StabilityModelType.Bishop) { throw new NotImplementedException(); } if (damKernelInput.OriginalSoilProfile2D != null) { damKernelInput.SubSoilScenario.SoilProfile2D = damKernelInput.OriginalSoilProfile2D.Clone(); } MacroStabilityCommonHelper.CombineSoilProfileWithSurfaceLine(damKernelInput.SubSoilScenario, damKernelInput.Location.SurfaceLine, damKernelInput.CurrentEmbankmentSoil); const bool useRiverLevelLow = true; // Determine PL-lines and create waternet var upliftHelper = new UpliftHelper(); PlLines plLines = upliftHelper.DeterminePlLinesForStability(damKernelInput, useRiverLevelLow, out _); Waternet waterNet = MacroStabilityCommonHelper.CreateWaternet(damKernelInput, plLines); var fillMacroStabilityWrapperFromEngine = new FillMacroStabilityWrapperInputFromEngine { TrafficLoad = MacroStabilityCommonHelper.FillTrafficLoad(damKernelInput) }; FailureMechanismParametersMStab.MStabParameters.GridPosition = StabilityGridPosition.Left; damKernelInput.DamFailureMechanismeCalculationSpecification.FailureMechanismParametersMStab .MStabParameters.GridPosition = StabilityGridPosition.Left; fillMacroStabilityWrapperFromEngine.BishopCalculationGrid = MacroStabilityCommonHelper.FillBishopCalculationGrid(damKernelInput); macroStabilityInput.Input = fillMacroStabilityWrapperFromEngine.CreateMacroStabilityInput(damKernelInput, FailureMechanismParametersMStab.MStabParameters, waterNet); fileNameForCalculation = MacroStabilityCommonHelper.GetStabilityInputFileName(damKernelInput, iterationIndex, FailureMechanismParametersMStab.MStabParameters.Model); stabilityCalculator = new Calculator(macroStabilityInput.Input); PrepareResult firstPrepareResult = MacroStabilityCommonHelper.PrepareKernel(stabilityCalculator, fileNameForCalculation); return firstPrepareResult; } catch (Exception e) { macroStabilityOutput.Message = new LogMessage { MessageType = LogMessageType.FatalError, Message = e.Message }; kernelDataOutput = macroStabilityOutput; return PrepareResult.Failed; } } kernelDataInput = null; return PrepareResult.NotRelevant; } /// /// Validates the specified kernel data input. /// /// The kernel data input. /// The kernel data output. /// The return messages. /// /// Zero when there are no errors, one when there are errors that prevent a calculation /// public int Validate(IKernelDataInput kernelDataInput, IKernelDataOutput kernelDataOutput, out List messages) { return macroStabilityCommonHelper.Validate(kernelDataInput, kernelDataOutput, out messages); } /// /// Executes the kernel. /// /// The kernel data input. /// The kernel data output. /// The return messages. public void Execute(IKernelDataInput kernelDataInput, IKernelDataOutput kernelDataOutput, out List messages) { var macroStabilityKernelDataInput = (MacroStabilityKernelDataInput) kernelDataInput; var macroStabilityOutput = (MacroStabilityOutput) kernelDataOutput; MacroStabilityCommonHelper.ThrowWhenMacroStabilityKernelInputNull(macroStabilityKernelDataInput); MacroStabilityCommonHelper.ThrowWhenMacroStabilityKernelOutputNull(macroStabilityOutput); macroStabilityCommonHelper.PerformStabilityCalculation(macroStabilityKernelDataInput.Input, macroStabilityOutput, fileNameForCalculation, stabilityCalculator, out messages); string fileName = Path.GetFileNameWithoutExtension(fileNameForCalculation); foreach (LogMessage logMessage in messages) { logMessage.Message = fileName + ": " + logMessage.Message; } } /// /// Fills the design results with the kernel output. /// /// The dam kernel input. /// The kernel data output. /// The design scenario /// The result message. /// The design results. /// public void PostProcess(DamKernelInput damKernelInput, IKernelDataOutput kernelDataOutput, DesignScenario designScenario, string resultMessage, out List designResults) { MacroStabilityCommonHelper.ThrowWhenMacroStabilityDamKernelInputNull(damKernelInput); var macroStabilityOutput = kernelDataOutput as MacroStabilityOutput; MacroStabilityCommonHelper.ThrowWhenMacroStabilityKernelOutputNull(macroStabilityOutput); designResults = new List(); if (macroStabilityOutput?.StabilityOutputItems != null && macroStabilityOutput.StabilityOutputItems.Count > 0) { MacroStabilityOutputItem macroStabilityOutputItem = macroStabilityOutput.StabilityOutputItems[0]; if (macroStabilityOutputItem != null) { DesignResult designResult = MacroStabilityCommonHelper.NewDesignResult(damKernelInput, designScenario); MacroStabilityCommonHelper.FillDesignResult(macroStabilityOutputItem, designResult); designResult.StabilityDesignResults.NumberOfIterations = 0; designResult.StabilityDesignResults.UpliftSituation = macroStabilityOutput.UpliftSituation; designResults.Add(designResult); } } } /// /// Calculates the design at point. /// /// The dam kernel input. /// The kernel data input. /// The kernel data output. /// The point. /// The messages. /// /// public ShoulderDesign CalculateDesignAtPoint(DamKernelInput damKernelInput, IKernelDataInput kernelDataInput, IKernelDataOutput kernelDataOutput, Point2D point, out List messages) { throw new NotImplementedException(); } /// /// Evaluates the design (current factor greater than desired factor) /// /// The dam kernel input. /// The kernel data input. /// The kernel data output. /// The design advise. /// The evaluation message. /// /// if the design was succesful /// /// public bool EvaluateDesign(DamKernelInput damKernelInput, IKernelDataInput kernelDataInput, IKernelDataOutput kernelDataOutput, out DesignAdvise designAdvise, out string evaluationMessage) { throw new NotImplementedException(); } public void PrepareDesign(IKernelDataInput kernelDataInput, IKernelDataOutput kernelDataOutput, DamKernelInput damKernelInput, int iterationIndex, out EmbankmentDesignParameters embankmentDesignParameters) { throw new NotImplementedException(); } /// /// Gets the design strategy /// /// /// public DesignStrategy GetDesignStrategy(DamKernelInput damKernelInput) { return DesignStrategy.NoDesignPossible; } }