// Copyright (C) Stichting Deltares 2019. 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.Diagnostics; using System.IO; using Deltares.DamEngine.Calculators.General; using Deltares.DamEngine.Calculators.KernelWrappers.Common; using Deltares.DamEngine.Calculators.KernelWrappers.Interfaces; using Deltares.DamEngine.Calculators.Properties; using Deltares.DamEngine.Data.Design; using Deltares.DamEngine.Data.General; using Deltares.DamEngine.Data.General.Results; using Deltares.DamEngine.Data.Geotechnics; using Deltares.DamEngine.Data.Standard; using Deltares.DamEngine.Data.Standard.Calculation; using Deltares.DamEngine.Data.Standard.Logging; namespace Deltares.DamEngine.Calculators.DikesDesign { /// /// The Dam Engine design calculator /// public class DesignCalculator { private ProgressDelegate progressDelegate; /// /// Performs the design calculation /// /// The dam project data. /// public void Execute(DamProjectData damProjectData) { progressDelegate?.Invoke(0); damProjectData.DesignCalculations = new List(); if (damProjectData.CalculationMessages == null) { damProjectData.CalculationMessages = new List(); } else { damProjectData.CalculationMessages.Clear(); } // Prepare the designCalculatorTasks var designCalculatorTasks = new List(); var relevantCombinationsCount = 0; for (int locationIndex = 0; locationIndex < damProjectData.Dike.Locations.Count; locationIndex++) { var location = damProjectData.Dike.Locations[locationIndex].Copy(); if (location.Segment.SoilProfileProbabilities.Count == 0) { var message = new LogMessage(); message.Message = string.Format(Resources.DesignCalculatorNoSoilProfilesAvailableForLocation, location.Name); message.MessageType = LogMessageType.Warning; damProjectData.CalculationMessages.Add(message); } else { for (int subSoilScenarioIndex = 0; subSoilScenarioIndex < location.Segment.SoilProfileProbabilities.Count; subSoilScenarioIndex++) { if (location.Segment.SoilProfileProbabilities[subSoilScenarioIndex].SegmentFailureMechanismType == ConversionHelper.ConvertToSegmentFailureMechanismType(damProjectData.DamProjectCalculationSpecification.CurrentSpecification.FailureMechanismSystemType)) { var soiProfileProbability = location.Segment.SoilProfileProbabilities[subSoilScenarioIndex]; for (int designScenarioIndex = 0; designScenarioIndex < location.Scenarios.Count; designScenarioIndex++) { DesignScenario designScenario = location.Scenarios[designScenarioIndex]; designScenario.Location = location; var projectPath = damProjectData.ProjectPath != "" ? damProjectData.ProjectPath : Directory.GetCurrentDirectory(); var designResults = new List(); var calculationMessages = new List(); designCalculatorTasks.Add(new DesignCalculatorTask() { Location = location, SoiProfileProbability = soiProfileProbability, DesignScenario = designScenario, ProjectPath = projectPath, CalculationMap = damProjectData.CalculationMap, FailureMechanismeCalculationSpecification = damProjectData.DamProjectCalculationSpecification.CurrentSpecification.Copy(), DesignResults = designResults, CalculationMessages = calculationMessages }); } relevantCombinationsCount++; } } } } if (relevantCombinationsCount > 0) { // Perform the calculation Parallel.Run(designCalculatorTasks, RunDesignCalculatorTask, progressDelegate, damProjectData.MaxCalculationCores); foreach (var desigCalculatorTask in designCalculatorTasks) { damProjectData.CalculationMessages.AddRange(desigCalculatorTask.CalculationMessages); damProjectData.DesignCalculations.AddRange(desigCalculatorTask.DesignResults); } } else { var logMessage = new LogMessage { MessageType = LogMessageType.Error, Subject = null, Message = string.Format(Resources.DesignCalculatorNoSegmentsWithFailureMechanismTypePresent, damProjectData.DamProjectCalculationSpecification.CurrentSpecification.FailureMechanismSystemType.ToString()) }; damProjectData.CalculationMessages.Add(logMessage); } } private void RunDesignCalculatorTask(object designCalculatorTask) { DesignCalculatorTask task = (DesignCalculatorTask)designCalculatorTask; Debug.WriteLine("Start calculation Location '{0}', soilprofile '{1}'", task.Location, task.SoiProfileProbability); CalculateOneScenario(task.Location, task.SoiProfileProbability, task.DesignScenario, task.ProjectPath, task.CalculationMap, task.FailureMechanismeCalculationSpecification, task.DesignResults, task.CalculationMessages); Debug.WriteLine("End calculation Location '{0}', soilprofile '{1}'", task.Location, task.SoiProfileProbability); } private void CalculateOneScenario(Location location, SoilGeometryProbability soiProfileProbability, DesignScenario designScenario, string projectPath, string calculationMap, DamFailureMechanismeCalculationSpecification damFailureMechanismeCalculationSpecification, List designResults, List calculationMessages) { try { // Prepare input var damKernelInput = new DamKernelInput(); damKernelInput.ProjectDir = projectPath; damKernelInput.CalculationDir = Path.Combine(projectPath, calculationMap); damKernelInput.Location = location; damKernelInput.SubSoilScenario = soiProfileProbability; damKernelInput.DamFailureMechanismeCalculationSpecification = damFailureMechanismeCalculationSpecification; damKernelInput.RiverLevelHigh = designScenario.RiverLevel; damKernelInput.RiverLevelLow = designScenario.RiverLevelLow; damKernelInput.FilenamePrefix = String.Format("Loc({0})_Sce({1})", location.Name, designScenario.LocationScenarioID); AnalysisType analysisType = DamProjectCalculationSpecification.SelectedAnalysisType; SynchronizeScenarioDataWithLocationData(designScenario, location); IKernelDataInput kernelDataInput; IKernelDataOutput kernelDataOutput; // Create kernelwrapper IKernelWrapper kernelWrapper = KernelWrapperHelper.CreateKernelWrapper(damFailureMechanismeCalculationSpecification); if (kernelWrapper == null) { throw new NotImplementedException(Resources.DesignCalculatorKernelNotImplemented); } PrepareResult prepareResult = kernelWrapper.Prepare(damKernelInput, 0, out kernelDataInput, out kernelDataOutput); // Sometimes the kernelDataInput is not created (p.e when soilprofileprobablility is meant for // stability where Piping calc is wanted). In that case, do nothing but just skip. if (prepareResult == PrepareResult.Successful) { switch (analysisType) { case AnalysisType.AdaptGeometry: PerformDesignCalculation( kernelWrapper, kernelDataInput, kernelDataOutput, damKernelInput, designScenario, calculationMessages, designResults); break; case AnalysisType.NoAdaption: DesignCalculatorSingle.PerformSingleCalculation( kernelWrapper, kernelDataInput, kernelDataOutput, damKernelInput, designScenario, calculationMessages, designResults); break; } } else { if (prepareResult == PrepareResult.NotRelevant) { // Do nothing. Calculation will be skipped. } if (prepareResult == PrepareResult.Failed) { calculationMessages.Add(new LogMessage(LogMessageType.Error, null, string.Format(Resources.DesignCalculatorPrepareError, location.Name, soiProfileProbability, designScenario.LocationScenarioID))); } } } catch (Exception e) { calculationMessages.Add(new LogMessage(LogMessageType.Error, null, string.Format(Resources.DesignCalculatorGeneralException, location.Name, soiProfileProbability, designScenario.LocationScenarioID, e.Message))); } } /// /// Performs the design calculation /// /// The kernel wrapper. /// The kernel data input. /// The kernel data output. /// The dam kernel input. /// The design scenario. /// The calculation messages. /// The design calculations. public static void PerformDesignCalculation( IKernelWrapper kernelWrapper, IKernelDataInput kernelDataInput, IKernelDataOutput kernelDataOutput, DamKernelInput damKernelInput, DesignScenario designScenario, List calculationMessages, List designCalculations) { // During the design calculation the location.SurfaceLine is adapted; so store a copy to restore it after the calculation Location location = damKernelInput.Location; SoilGeometryProbability subSoilScenario = damKernelInput.SubSoilScenario; SurfaceLine2 orgLocationSurfaceLine = location.SurfaceLine.FullDeepClone(); try { if (damKernelInput.Location.RedesignDikeHeight) { double? dikeHeight = location.SurfaceLine.GetDikeHeight(); if (dikeHeight.HasValue) { if (designScenario.DikeTableHeight > dikeHeight.Value) { // Redesign the surfaceline to the desired Dike Tabel Height var surfaceLineHeightAdapter = new SurfaceLineHeightAdapter(location.SurfaceLine, location); SurfaceLine2 adaptedSurfaceLine = surfaceLineHeightAdapter.ConstructNewSurfaceLine(designScenario.DikeTableHeight ?? location.SurfaceLine.GetDefaultDikeTableHeight() ?? 0); designScenario.SetRedesignedSurfaceLine(subSoilScenario.SoilProfile1D, subSoilScenario.StiFileName, adaptedSurfaceLine); } } } if (damKernelInput.Location.RedesignDikeShoulder) { // Redesign the surfaceline with shoulder and or slope adaption switch (kernelWrapper.GetDesignStrategy(damKernelInput)) { case DesignStrategy.ShoulderPerPoint: DesignCalculatorShoulderPerPoint.PerformDesignCalculationShoulderPerPoint( kernelWrapper, kernelDataInput, kernelDataOutput, damKernelInput, designScenario, calculationMessages, designCalculations); break; case DesignStrategy.NoDesignPossible: throw new NotImplementedException("No design is possible for this failure mechanism"); case DesignStrategy.SlopeAdaptionBeforeShoulderAdaption: DesignCalculatorFirstSlopeAdaptionThenShoulderAdaption.PerformDesignCalculationFirstSlopeAdaptionThenShoulderAdaption( kernelWrapper, kernelDataInput, kernelDataOutput, damKernelInput, designScenario, calculationMessages, designCalculations); break; case DesignStrategy.OptimizedSlopeAndShoulderAdaption: DesignCalculatorCombinedSlopeAndShoulderAdaption.PerformDesignCalculationCombinedSlopeAdaptionAndShoulderAdaption( kernelWrapper, kernelDataInput, kernelDataOutput, damKernelInput, designScenario, calculationMessages, designCalculations); break; } } } finally { // Restore the original surfaceline location.SurfaceLine = orgLocationSurfaceLine; } } /// /// Synchronizes the scenario data with location data. /// Note that scenario data is leading when available. /// /// The scenario. /// The location. private void SynchronizeScenarioDataWithLocationData(DesignScenario designScenario, Location location) { // Synchronize PlLine parameters if (designScenario.PlLineOffsetBelowDikeToeAtPolder.HasValue) { location.PlLineOffsetBelowDikeToeAtPolder = designScenario.PlLineOffsetBelowDikeToeAtPolder.Value; } if (designScenario.PlLineOffsetBelowDikeTopAtPolder.HasValue) { location.PlLineOffsetBelowDikeTopAtPolder = designScenario.PlLineOffsetBelowDikeTopAtPolder.Value; } if (designScenario.PlLineOffsetBelowDikeTopAtRiver.HasValue) { location.PlLineOffsetBelowDikeTopAtRiver = designScenario.PlLineOffsetBelowDikeTopAtRiver.Value; } if (designScenario.PlLineOffsetBelowShoulderBaseInside.HasValue) { location.PlLineOffsetBelowShoulderBaseInside = designScenario.PlLineOffsetBelowShoulderBaseInside.Value; } if (designScenario.PlLineOffsetBelowDikeCrestMiddle.HasValue) { location.PlLineOffsetBelowDikeCrestMiddle = designScenario.PlLineOffsetBelowDikeCrestMiddle; } if (designScenario.PlLineOffsetFactorBelowShoulderCrest.HasValue) { location.PlLineOffsetFactorBelowShoulderCrest = designScenario.PlLineOffsetFactorBelowShoulderCrest; } if (designScenario.UsePlLineOffsetBelowDikeCrestMiddle.HasValue) { location.UsePlLineOffsetBelowDikeCrestMiddle = designScenario.UsePlLineOffsetBelowDikeCrestMiddle; } if (designScenario.UsePlLineOffsetFactorBelowShoulderCrest.HasValue) { location.UsePlLineOffsetFactorBelowShoulderCrest = designScenario.UsePlLineOffsetFactorBelowShoulderCrest; } if (designScenario.HeadPl3.HasValue) { location.HeadPl3 = designScenario.HeadPl3.Value; } if (designScenario.HeadPl4.HasValue) { location.HeadPl4 = designScenario.HeadPl4.Value; } // Synchronize piping design parameters if (designScenario.UpliftCriterionPiping.HasValue) { location.UpliftCriterionPiping = designScenario.UpliftCriterionPiping.Value; } if (designScenario.RequiredSafetyFactorPiping.HasValue) { location.ModelFactors.RequiredSafetyFactorPiping = designScenario.RequiredSafetyFactorPiping.Value; } // Synchronize stability design parameters if (designScenario.UpliftCriterionStability.HasValue) { location.UpliftCriterionStability = designScenario.UpliftCriterionStability.Value; } if (designScenario.RequiredSafetyFactorStabilityInnerSlope.HasValue) { location.ModelFactors.RequiredSafetyFactorStabilityInnerSlope = designScenario.RequiredSafetyFactorStabilityInnerSlope.Value; } if (designScenario.RequiredSafetyFactorStabilityOuterSlope.HasValue) { location.ModelFactors.RequiredSafetyFactorStabilityOuterSlope = designScenario.RequiredSafetyFactorStabilityOuterSlope.Value; } if (designScenario.DikeTableHeight.HasValue) { location.DikeTableHeight = designScenario.DikeTableHeight ?? location.SurfaceLine.GetDefaultDikeTableHeight() ?? 0; } } /// /// Registers the progress. /// /// The progress delegate. /// public CalculationResult RegisterProgress(ProgressDelegate aProgressDelegate) { progressDelegate = aProgressDelegate; return CalculationResult.Succeeded; } } }