// Copyright (C) Stichting Deltares 2018. 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.Linq; using System.Text.RegularExpressions; using Deltares.DamEngine.Data.General; using Deltares.DamEngine.Data.Geotechnics; using Deltares.DamEngine.Data.RegionalAssessmentResults; using Deltares.DamEngine.Data.Standard.Calculation; using Deltares.DamEngine.Data.Standard.Language; using Deltares.DamEngine.Data.Standard.Logging; namespace Deltares.DamEngine.Calculators.DikesAssessmentRegional { /// /// Exception for RegionalScenariosCalculation class /// public class RegionalScenariosCalculationException : ApplicationException { public RegionalScenariosCalculationException() { } public RegionalScenariosCalculationException(string message) : base(message) { } } public class RegionalScenariosCalculation : ICalculation { private EvaluationJob evaluationJob = null; private GetValuesDelegate getValuesDelegate = null; private ProgressDelegate progressDelegate = null; private SendMessageDelegate sendMessageDelegate = null; private string mstabExePath = @".\DGeoStability.exe"; private int maxCalculationCores = 255; private Dictionary runningJobs = new Dictionary(); private bool isSkipStabilityCalculation = false; public PipingModelType PipingModelType { get; set; } public MStabParameters MStabParameters { get; set; } public RegionalScenariosCalculation() { } #region ICalculation Members public CalculationResult GetResults(ref string results) { // try // { // XmlSerializer serializer = new XmlSerializer(); // results = serializer.SerializeToString(this.evaluationJob); // return CalculationResult.Succeeded; // } // catch // { // return CalculationResult.UnexpectedError; // }##Bka return CalculationResult.UnexpectedError; } public CalculationResult Load(string input) { // try // { // XmlDeserializer deserializer = new XmlDeserializer(); // this.evaluationJob = (EvaluationJob)deserializer.XmlDeserializeFromString(input, typeof(EvaluationJob), new DefaultClassFactory()); // return CalculationResult.Succeeded; // } // catch // { // return CalculationResult.UnexpectedError; // }##Bka return CalculationResult.UnexpectedError; } public CalculationResult RegisterGetValues(GetValuesDelegate getValuesDelegate) { this.getValuesDelegate = getValuesDelegate; return CalculationResult.Succeeded; } public CalculationResult RegisterProgress(ProgressDelegate progressDelegate) { this.progressDelegate = progressDelegate; return CalculationResult.Succeeded; } public CalculationResult RegisterSendDebugInfo(SendDebugInfodelegate sendDebugInfoDelegate) { return CalculationResult.Succeeded; } public CalculationResult RegisterSendMessage(SendMessageDelegate sendMessageDelegate) { this.sendMessageDelegate = sendMessageDelegate; return CalculationResult.Succeeded; } public CalculationResult RegisterSetValues(SetValuesDelegate setValuesDelegate) { return CalculationResult.Succeeded; } public CalculationResult RegisterUserAbort(UserAbortDelegate userAbortDelegate) { return CalculationResult.Succeeded; } public CalculationResult Run() { try { List tasks = this.FillQueue(); General.Parallel.Run(tasks, this.RunTask, this.progressDelegate, this.MaxCalculationCores); this.FillResults(tasks); return CalculationResult.Succeeded; } catch(Exception exception) { sendMessageDelegate(new LogMessage(LogMessageType.Warning, null, "Unexpected error:" + exception.Message)); throw exception; } } public CalculationResult Validate() { return CalculationResult.Succeeded; } #endregion public string Version { get { return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); } } public string MStabExePath { get { return mstabExePath; } set { mstabExePath = value; } } public bool IsSkipStabilityCalculation { get { return isSkipStabilityCalculation; } set { isSkipStabilityCalculation = value; } } public int MaxCalculationCores { get { return maxCalculationCores; } set { maxCalculationCores = value; } } private List FillQueue() { List tasks = new List(); this.evaluationJob.FailedEvaluatedLocations = new List(); foreach (Location location in this.evaluationJob.Locations) { if (location.Segment == null) { // Add this location to the failed locations if (this.evaluationJob.FailedEvaluatedLocations.IndexOf(location) < 0) { this.evaluationJob.FailedEvaluatedLocations.Add(location); var locationHasNoSegment = LocalizationManager.GetTranslatedText(this.GetType(), "LocationHasNoSegment"); sendMessageDelegate(new LogMessage(LogMessageType.Error, location, locationHasNoSegment)); } } else { // TODO: Ask Erik Vastenburg how to handle piping and stability soilprofiles when determining RWScenarios // For now we only use the stability profiles. var soilGeometryProbabilities = location.Segment.SoilProfileProbabilities.Where(s => (s.SegmentFailureMechanismType == null) || (s.SegmentFailureMechanismType.Value == FailureMechanismSystemType.StabilityInside)).ToList(); if (soilGeometryProbabilities.Count == 0) { this.evaluationJob.FailedEvaluatedLocations.Add(location); sendMessageDelegate( new LogMessage(LogMessageType.Warning, location, String.Format("Location has no soilprofiles: ") + String.Format("Segment: {0}", location.Segment.Name))); } else { foreach (SoilGeometryProbability soilGeometryProbability in soilGeometryProbabilities) { if (soilGeometryProbability.SoilProfileType == SoilProfileType.ProfileType2D || soilGeometryProbability.SoilProfileType == SoilProfileType.ProfileTypeStiFile) { this.evaluationJob.FailedEvaluatedLocations.Add(location); sendMessageDelegate(new LogMessage(LogMessageType.Warning, location, LocalizationManager.GetTranslatedText(this, "Geometry2DNotSupportedInRegionalAssessment") + String.Format("Segment: {0}", location.Segment.Name))); } else { SoilProfile soilProfile = soilGeometryProbability.SoilProfile1D; IList rwScenarios = null; try { rwScenarios = this.GetRWScenarios(location, soilGeometryProbability); } catch (Exception e) { rwScenarios = null; // Add this location to the failed locations if (this.evaluationJob.FailedEvaluatedLocations.IndexOf(location) < 0) { this.evaluationJob.FailedEvaluatedLocations.Add(location); sendMessageDelegate( new LogMessage(LogMessageType.Warning, location, String.Format("Cannot generate scenarios: {0}", e.Message) + String.Format("Soilprofile: {0}", soilProfile.Name))); } } if (rwScenarios != null) { foreach (RegionalScenarioProfileResult job in rwScenarios) { tasks.Add(job); } } } } } } } return tasks; } private IList GetRWScenarios(Location location, SoilGeometryProbability soilGeometryProbability) { RegionalScenarioSelector selector = new RegionalScenarioSelector(); selector.PipingModelType = PipingModelType; selector.MStabParameters = MStabParameters; return selector.GetScenarios(location, soilGeometryProbability); } private void RunTask(object task) { var job = (RegionalScenarioProfileResult)task; try { if (!IsSkipStabilityCalculation) { ProcessJob(job); } else { job.CalculationResult = CalculationResult.NoRun; } } catch (Exception e) { job.CalculationResult = CalculationResult.UnexpectedError; sendMessageDelegate(new LogMessage(LogMessageType.Warning, job, String.Format(job.LocationName + " Error: {0}", e.Message))); } } /// /// Select which job processor to use, depending on failuremechanism /// /// private void ProcessJob(RegionalScenarioProfileResult job) { Debug.WriteLine(String.Format("Job {0}, location {1}, Scenario {2}", job.FailureMechanismType.ToString(), job.LocationName, job.ScenarioType.ToString())); switch (job.FailureMechanismType) { case FailureMechanismSystemType.StabilityInside: ProcessJobStability(job); break; case FailureMechanismSystemType.Piping: ProcessJobPiping(job); break; default: throw new RegionalScenariosCalculationException(String.Format("Failuremechanism {0} not yet implemented for scenario calculation", job.FailureMechanismType)); } } /// /// Process a job for failuremechanism Piping /// /// private void ProcessJobPiping(RegionalScenarioProfileResult job) { // if (job.Location.ModelFactors.UpliftCriterionPiping.HasValue) // { // var modelParametersForPLLines = new ModelParametersForPlLines(); // var calculator = GetCalculatorForPipingModel(job, modelParametersForPLLines); // double waterLevel; // switch (job.LoadSituation) // { // case LoadSituation.Dry: // waterLevel = job.Location.BoezemLevelLbp; // break; // default: // LoadSituation.Wet // waterLevel = job.Location.BoezemLevelTp; // break; // } // //job.SoilGeometryProbability.SoilProfile1D.EnsureUniqueLayerIds(); // var calculationName = GetCalculationNameForPipingCalculator(job); // calculator.FilenameCalculation = Path.Combine(Path.Combine(DamProjectData.ProjectWorkingPath, job.FailureMechanismType.ToString()), calculationName); // calculator.IsHydraulicShortcut = (job.HydraulicShortcutType == HydraulicShortcutType.HydraulicShortcut); // double? pipingFactor = calculator.CalculatePipingFactor(job.Location, job.Location.LocalXZSurfaceLine2, job.SoilGeometryProbability.SoilProfile1D, waterLevel); // job.BaseFileName = calculator.FilenameCalculation; // // job.RegionalResultType = RegionalResultType.SafetyFactor; // if (pipingFactor.HasValue) // { // job.SafetyFactor = pipingFactor.Value; // job.CalculationResult = CalculationResult.Succeeded; // job.ProbabilityOfFailure = double.NaN; // job.RegionalResultType = RegionalResultType.SafetyFactor; // } // // else // { // job.SafetyFactor = double.NaN; // job.CalculationResult = CalculationResult.RunFailed; // } // } // else // { // throw new RegionalScenariosCalculationException(String.Format("Uplift criterion not defined for location {0}", job.Location.Name)); // } //##Bka } private string GetCalculationNameForPipingCalculator(RegionalScenarioProfileResult job) { string calculationName; switch (job.PipingModelOption) { case PipingModelType.SellmeijerVnk : calculationName = String.Format("Calc(SellmeijerVnk)_Loc({0})_Pro({1}))", job.LocationName, job.SoilProfileName); break; case PipingModelType.Sellmeijer4Forces: calculationName = String.Format("Calc(Sellmeijer4Forces)_Loc({0})_Pro({1}))", job.LocationName, job.SoilProfileName); break; // Set Sellmeijer4Forces as default. default: calculationName = String.Format("Calc(Sellmeijer4Forces)_Loc({0})_Pro({1}))", job.LocationName, job.SoilProfileName); break; } calculationName = Regex.Replace(calculationName, @"[\\\/:\*\?""'<>|.]", "_"); return calculationName; } /// /// Determines the proper calculator for pipng /// /// /// /// proper piping calculator // private PipingCalculator GetCalculatorForPipingModel(RWScenarioProfileResult job, ModelParametersForPlLines modelParametersForPLLines) // { // PipingCalculator calculator; // switch (job.PipingModelOption) // { // case PipingModelType.SellmeijerVnk: calculator = new PipingCalculatorSellmeijer(modelParametersForPLLines, // 1.0, null, null, null, job.Location.ModelFactors.UpliftCriterionPiping.Value); // break; // case PipingModelType.Sellmeijer2Forces: calculator = new PipingCalculatorSellmeijer2Forces(modelParametersForPLLines, // 1.0, null, null, job.Location.ModelFactors.UpliftCriterionPiping.Value); // break; // case PipingModelType.Sellmeijer4Forces: calculator = new PipingCalculatorSellmeijer4Forces(modelParametersForPLLines, // 1.0, null, null, job.Location.ModelFactors.UpliftCriterionPiping.Value); // break; // case PipingModelType.Bligh: calculator = new PipingCalculatorBligh(modelParametersForPLLines, // 1.0, null, null, job.Location.ModelFactors.UpliftCriterionPiping.Value); // break; // default: // throw new RegionalScenariosCalculationException(String.Format("Piping model {0} not yet implemented for scenario calculation", job.PipingModelOption)); // } // return calculator; // } /// /// Process a job for failuremechanism Stability /// /// private void ProcessJobStability(RegionalScenarioProfileResult job) { // StabilityCalculation calculator = new StabilityCalculation(); // lock (runningJobs) // { // runningJobs[calculator] = job; // } // calculator.MStabExePath = this.MStabExePath; // calculator.RegisterSendMessage(this.SendStabilityMessage); // // string soilDatabaseName = job.Location.StabilityOptions.SoilDatabaseName; // DamFailureMechanismeCalculationSpecification damCalculation = // calculator.GetSpecification(this.evaluationJob.DikeName, soilDatabaseName, job.Location, new SoilGeometry(job.SoilGeometryProbability.SoilProfile1D, null), // (MStabModelType)job.MstabModelOption, job.LoadSituation, job.DikeDrySensitivity, job.HydraulicShortcutType, MStabParameters); // // calculator.SaveToFile(damCalculation.FailureMechanismParametersMStab); // string inputFile = damCalculation.FailureMechanismParametersMStab.MStabParameters.ProjectFileName; // // job.BaseFileName = inputFile.Replace(DamProject.ProjectWorkingPath, @"").Replace(".sti", ""); // calculator.Load(inputFile); // job.CalculationResult = calculator.Run(); if (job.CalculationResult == CalculationResult.Succeeded) { string results = ""; // job.CalculationResult = calculator.GetResults(ref results); //XmlDeserializer deserializer = new XmlDeserializer(); // RegionalResult result = (RegionalResult)deserializer.XmlDeserializeFromString(results, typeof(RegionalResult)); // job.SafetyFactor = result.SafetyFactor; // job.ProbabilityOfFailure = result.ProbabilityOfFailure; // job.RegionalResultType = result.RegionalResultType; // job.CalculationResult = result.CalculationResult; } else { // job.RegionalResultType = (damCalculation.FailureMechanismParametersMStab.MStabParameters.IsProbabilistic ? RegionalResultType.ProbabilityOfFailure : RegionalResultType.SafetyFactor); job.SafetyFactor = double.NaN; job.ProbabilityOfFailure = double.NaN; } lock (runningJobs) { // runningJobs.Remove(calculator); } } /// /// Log messages /// /// private void SendStabilityMessage(LogMessage logMessage) { lock (runningJobs) { if (logMessage.Subject != null) { var job = (RegionalScenarioProfileResult)runningJobs[(ICalculation)logMessage.Subject]; logMessage.Subject = job.Location; } } this.sendMessageDelegate(logMessage); } /// /// Fill the results for the scenarios /// private void FillResults(List tasks) { // Fill scenariosResult structure with jobs just run foreach (Location location in this.evaluationJob.Locations) { try { RegionalScenariosResult scenariosResult = new RegionalScenariosResult(); if (this.evaluationJob.FailedEvaluatedLocations.IndexOf(location) < 0) { // scenarios were succesfully created, so results are available foreach (RegionalScenarioProfileResult job in tasks) { if (job.LocationName.Equals(location.Name)) { RegionalScenarioResult scenarioResult = null; foreach (var existingScenarioResult in scenariosResult.RegionalScenarioResults) { if (existingScenarioResult.ScenarioType == job.ScenarioType) { scenarioResult = existingScenarioResult; } } if (scenarioResult == null) { scenarioResult = new RegionalScenarioResult(); scenarioResult.ScenarioType = job.ScenarioType; scenariosResult.RegionalScenarioResults.Add(scenarioResult); } scenarioResult.RegionalScenarioProfileResults.Add(job); } } // Combine results foreach (var scenarioResult in scenariosResult.RegionalScenarioResults) { this.CombineProfiles(scenarioResult); } this.CombineScenarios(scenariosResult); } else { // scenarios were not succesfully created, so results are not available // no succesful calculations found scenariosResult.CalculationResult = CalculationResult.RunFailed; scenariosResult.SafetyFactor = double.NaN; } // scenariosResult are the results of all scenarios for one location. this.evaluationJob.Results.Add(scenariosResult); } catch (Exception e) { RegionalScenariosResult scenariosResult = new RegionalScenariosResult { CalculationResult = CalculationResult.RunFailed, SafetyFactor = double.NaN }; sendMessageDelegate(new LogMessage(LogMessageType.Warning, location, String.Format("Error in location {0}: {1}", location.Name, e.Message))); } } } private void CombineProfiles(RegionalScenarioResult scenarioResult) { // combine results of profiles scenarioResult.SafetyFactor = Double.MaxValue; foreach (var profileResult in scenarioResult.RegionalScenarioProfileResults) { if (profileResult.CalculationResult == CalculationResult.Succeeded) { if (profileResult.SafetyFactor < scenarioResult.SafetyFactor) { scenarioResult.SafetyFactor = profileResult.SafetyFactor; } scenarioResult.CalculationResult = CalculationResult.Succeeded; } } if (scenarioResult.CalculationResult != CalculationResult.Succeeded) { // no succesful calculations found scenarioResult.CalculationResult = scenarioResult.RegionalScenarioProfileResults[0].CalculationResult; scenarioResult.SafetyFactor = scenarioResult.RegionalScenarioProfileResults[0].SafetyFactor; } } private void CombineScenarios(RegionalScenariosResult scenariosResult) { // combine results of scenarios scenariosResult.SafetyFactor = Double.MaxValue; foreach (var scenarioResult in scenariosResult.RegionalScenarioResults) { if (scenarioResult.CalculationResult == CalculationResult.Succeeded) { if (scenarioResult.SafetyFactor < scenariosResult.SafetyFactor) { scenariosResult.SafetyFactor = scenarioResult.SafetyFactor; } scenariosResult.CalculationResult = CalculationResult.Succeeded; } } } } }