// 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.IO;
using System.Linq;
using Deltares.DamEngine.Calculators.KernelWrappers.Common;
using Deltares.DamEngine.Calculators.KernelWrappers.DamMacroStabilityCommon;
using Deltares.DamEngine.Calculators.KernelWrappers.Interfaces;
using Deltares.DamEngine.Calculators.Properties;
using Deltares.DamEngine.Data.General;
using Deltares.DamEngine.Data.General.Results;
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) { }
}
///
/// Class to perform the calculation of reginal scenarios
///
///
public class RegionalScenariosCalculation : ICalculation
{
private DamProjectData damProjectData;
private EvaluationJob evaluationJob;
private GetValuesDelegate getValuesDelegate;
private ProgressDelegate progressDelegate;
private SendMessageDelegate sendMessageDelegate;
private int maxCalculationCores = 255;
private readonly Dictionary runningJobs = new Dictionary();
private bool isSkipStabilityCalculation;
private PipingModelType pipingModelType;
private MStabParameters mStabParameters;
///
/// Executes the specified a dam project data.
///
/// a dam project data.
///
public List Execute(DamProjectData aDamProjectData)
{
damProjectData = aDamProjectData;
maxCalculationCores = damProjectData.MaxCalculationCores;
mStabParameters = damProjectData.DamProjectCalculationSpecification.CurrentSpecification.FailureMechanismParametersMStab.MStabParameters;
pipingModelType = damProjectData.DamProjectCalculationSpecification.CurrentSpecification.PipingModelType;
var dj = damProjectData.DikeJob;
foreach (var locationJob in damProjectData.LocationJobs)
{
locationJob.Location.DamType = DamType.Regional;
}
evaluationJob = damProjectData.GetEvaluationJob();
Run();
evaluationJob.AttachResults(aDamProjectData.LocationJobs);
return new List();
}
#region ICalculation Members
///
/// Gets the results.
///
///
public EvaluationJob GetResults()
{
return evaluationJob;
}
///
/// Registers the get values.
///
/// The get values delegate.
///
public CalculationResult RegisterGetValues(GetValuesDelegate aGetValuesDelegate)
{
getValuesDelegate = aGetValuesDelegate;
return CalculationResult.Succeeded;
}
///
/// Registers the progress.
///
/// The progress delegate.
///
public CalculationResult RegisterProgress(ProgressDelegate aProgressDelegate)
{
progressDelegate = aProgressDelegate;
return CalculationResult.Succeeded;
}
///
/// Registers the send debug information.
///
/// The send debug information delegate.
///
public CalculationResult RegisterSendDebugInfo(SendDebugInfodelegate sendDebugInfoDelegate)
{
return CalculationResult.Succeeded;
}
///
/// Registers the send message.
///
/// The send message delegate.
///
public CalculationResult RegisterSendMessage(SendMessageDelegate aSendMessageDelegate)
{
sendMessageDelegate = aSendMessageDelegate;
return CalculationResult.Succeeded;
}
///
/// Registers the set values.
///
/// The set values delegate.
///
public CalculationResult RegisterSetValues(SetValuesDelegate setValuesDelegate)
{
return CalculationResult.Succeeded;
}
///
/// Registers the user abort.
///
/// The user abort delegate.
///
public CalculationResult RegisterUserAbort(UserAbortDelegate userAbortDelegate)
{
return CalculationResult.Succeeded;
}
///
/// Runs this instance.
///
///
public CalculationResult Run()
{
try
{
List tasks = FillQueue();
General.Parallel.Run(tasks, RunTask, progressDelegate, maxCalculationCores);
FillResults(tasks);
return CalculationResult.Succeeded;
}
catch(Exception exception)
{
sendMessageDelegate(new LogMessage(LogMessageType.Warning, null, "Unexpected error:" + exception.Message));
throw;
}
}
///
/// Validates this instance.
///
///
public CalculationResult Validate()
{
return CalculationResult.Succeeded;
}
#endregion
///
/// Gets the version.
///
///
/// The version.
///
public string Version
{
get { return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); }
}
///
/// Gets or sets a value indicating whether this instance is skip stability calculation.
/// Only used for test puposes.
///
///
/// true if this instance is skip stability calculation; otherwise, false.
///
public bool IsSkipStabilityCalculation
{
get { return isSkipStabilityCalculation; }
set { isSkipStabilityCalculation = value; }
}
private List FillQueue()
{
List tasks = new List();
evaluationJob.FailedEvaluatedLocations = new List();
foreach (var location in evaluationJob.Locations)
{
if (location.Segment == null)
{
// Add this location to the failed locations
if (evaluationJob.FailedEvaluatedLocations.IndexOf(location) < 0)
{
evaluationJob.FailedEvaluatedLocations.Add(location);
var locationHasNoSegment = LocalizationManager.GetTranslatedText(GetType(), "LocationHasNoSegment");
if (sendMessageDelegate != null)
{
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)
{
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)
{
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 regionalScenarios;
try
{
regionalScenarios = GetRegionalScenarios(location, soilGeometryProbability);
}
catch (Exception e)
{
regionalScenarios = null;
// Add this location to the failed locations
if (evaluationJob.FailedEvaluatedLocations.IndexOf(location) < 0)
{
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 (regionalScenarios != null)
{
foreach (RegionalScenarioProfileResult job in regionalScenarios)
{
tasks.Add(job);
}
}
}
}
}
}
}
return tasks;
}
private IList GetRegionalScenarios(Location location, SoilGeometryProbability soilGeometryProbability)
{
RegionalScenarioSelector selector = new RegionalScenarioSelector
{
PipingModelType = pipingModelType,
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;
if (sendMessageDelegate != null)
{
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 Stability
///
///
private void ProcessJobStability(RegionalScenarioProfileResult job)
{
var currentSpecification = damProjectData.DamProjectCalculationSpecification.CurrentSpecification; // must this be a copy?
currentSpecification.FailureMechanismSystemType = job.FailureMechanismType;
currentSpecification.FailureMechanismParametersMStab.MStabParameters.Model = job.MstabModelOption;
currentSpecification.AssessmentScenarioJobSettings.LoadSituation = job.LoadSituation;
currentSpecification.AssessmentScenarioJobSettings.DikeDrySensitivity = job.DikeDrySensitivity;
currentSpecification.AssessmentScenarioJobSettings.HydraulicShortcutType = job.HydraulicShortcutType;
var calculator = CreateKernelWrapper(currentSpecification);
var boezemLevelHbp = job.Location.BoezemLevelHbp;
var boezemLevelLbp = job.Location.BoezemLevelLbp;
// Adapt used waterlevel(s) when needed
if (job.LoadSituation == LoadSituation.Wet)
{
job.Location.BoezemLevelHbp = job.Location.BoezemLevelTp;
job.Location.BoezemLevelLbp = job.Location.BoezemLevelTp;
}
PerformJob(job, calculator, currentSpecification);
job.Location.BoezemLevelHbp = boezemLevelHbp;
job.Location.BoezemLevelLbp = boezemLevelLbp;
}
///
/// Process a job for failuremechanism Piping
///
///
private void ProcessJobPiping(RegionalScenarioProfileResult job)
{
if (job.Location.ModelFactors.UpliftCriterionPiping.HasValue)
{
var currentSpecification = damProjectData.DamProjectCalculationSpecification.CurrentSpecification; // must this be a copy?
currentSpecification.FailureMechanismSystemType = job.FailureMechanismType;
currentSpecification.PipingModelType = job.PipingModelOption;
currentSpecification.AssessmentScenarioJobSettings.LoadSituation = job.LoadSituation;
currentSpecification.AssessmentScenarioJobSettings.DikeDrySensitivity = job.DikeDrySensitivity;
currentSpecification.AssessmentScenarioJobSettings.HydraulicShortcutType = job.HydraulicShortcutType;
var calculator = CreateKernelWrapper(currentSpecification);
// For piping waterlevel needs to be set here. Piping kernel only uses waterlevelhigh which is set from BoezemLevelHbp.
var boezemLevelHbp = job.Location.BoezemLevelHbp;
switch (job.LoadSituation)
{
case LoadSituation.Dry:
job.Location.BoezemLevelHbp = job.Location.BoezemLevelLbp;
break;
case LoadSituation.Wet:
job.Location.BoezemLevelHbp = job.Location.BoezemLevelTp;
break;
}
PerformJob(job, calculator, currentSpecification);
job.Location.BoezemLevelHbp = boezemLevelHbp;
}
else
{
throw new RegionalScenariosCalculationException(String.Format("Uplift criterion not defined for location {0}", job.Location.Name));
}
}
private IKernelWrapper CreateKernelWrapper(DamFailureMechanismeCalculationSpecification currentSpecification)
{
IKernelWrapper kernelWrapper = KernelWrapperHelper.CreateKernelWrapper(currentSpecification);
if (kernelWrapper == null)
{
throw new NotImplementedException(Resources.DesignCalculatorKernelNotImplemented);
}
return kernelWrapper;
}
private void PerformJob(RegionalScenarioProfileResult job, IKernelWrapper calculator, DamFailureMechanismeCalculationSpecification currentSpecification)
{
IKernelDataInput kernelDataInput;
IKernelDataOutput kernelDataOutput;
DamKernelInput damKernelInput;
lock (runningJobs)
{
runningJobs[calculator] = job;
}
var calculationCreationMessages = CreateKernelWrapperforJob(calculator, job.FailureMechanismType, job.ScenarioType, job.Location, job.SoilGeometryProbability,
out kernelDataInput, out kernelDataOutput, out damKernelInput);
// Check if prepare is ok
var errorFound = false;
foreach (var calculationCreationMessage in calculationCreationMessages)
{
if (calculationCreationMessage.MessageType == LogMessageType.Error)
{
job.CalculationResult = CalculationResult.InvalidInputData;
errorFound = true;
break;
}
}
if (!errorFound && calculationCreationMessages.Count > 0)
{
job.CalculationResult = CalculationResult.NoRun;
}
// if preparation has error or shows not relevant, then skip this
if (!errorFound && calculationCreationMessages.Count == 0)
{
List messages;
calculator.Execute(kernelDataInput, kernelDataOutput, out messages);
job.CalculationResult = CalculationResult.Succeeded;
foreach (var logMessage in messages)
{
if (logMessage.MessageType == LogMessageType.Error || logMessage.MessageType == LogMessageType.FatalError)
{
job.CalculationResult = CalculationResult.UnexpectedError;
}
}
if (job.CalculationResult == CalculationResult.Succeeded)
{
string resultMessage = "";
List results;
calculator.PostProcess(damKernelInput, kernelDataOutput, null, resultMessage, out results);
var stbOutput = kernelDataOutput as DamMacroStabilityOutput;
if (stbOutput != null)
{
var inputfile = stbOutput.StabilityOutputItems[0].ProjectFileName;
inputfile = inputfile.Replace(damKernelInput.CalculationDir, @"").Replace(".sti", "");
job.BaseFileName = inputfile;
}
if (results[0].SafetyFactor != null)
{
job.SafetyFactor = results[0].SafetyFactor.Value;
}
job.CalculationResult = results[0].CalculationResult;
job.RegionalResultType = RegionalResultType.SafetyFactor;
job.ProbabilityOfFailure = double.NaN;
}
else
{
if (job.FailureMechanismType == FailureMechanismSystemType.Piping)
{
job.RegionalResultType = RegionalResultType.SafetyFactor;
}
else
{
job.RegionalResultType = (currentSpecification.FailureMechanismParametersMStab.MStabParameters.IsProbabilistic ? RegionalResultType.ProbabilityOfFailure : RegionalResultType.SafetyFactor);
}
job.SafetyFactor = double.NaN;
job.ProbabilityOfFailure = double.NaN;
}
}
lock (runningJobs)
{
runningJobs.Remove(calculator);
}
}
private List CreateKernelWrapperforJob(IKernelWrapper kernelWrapper, FailureMechanismSystemType failureMechanismType, ScenarioType scenarioType,
Location location, SoilGeometryProbability soilGeometryProbability, out IKernelDataInput kernelDataInput,
out IKernelDataOutput kernelDataOutput, out DamKernelInput damKernelInput)
{
// Todo Bka: eliminate damProjectData usages.
var calculationMessages = new List();
// Prepare input
damKernelInput = new DamKernelInput();
var projectPath = damProjectData.ProjectPath != "" ? damProjectData.ProjectPath : Directory.GetCurrentDirectory();
damKernelInput.ProjectDir = projectPath;
damKernelInput.CalculationDir = Path.Combine(projectPath, damProjectData.CalculationMap);
damKernelInput.Location = location;
damKernelInput.SubSoilScenario = soilGeometryProbability;
damKernelInput.DamFailureMechanismeCalculationSpecification = damProjectData.DamProjectCalculationSpecification.CurrentSpecification;
damKernelInput.RiverLevelHigh = damKernelInput.Location.BoezemLevelHbp;
damKernelInput.RiverLevelLow = damKernelInput.Location.BoezemLevelLbp;
damKernelInput.FilenamePrefix = string.Format("Loc({0})_Sce({1})", location.Name, scenarioType);
damKernelInput.SubSoilScenario.SegmentFailureMechanismType = failureMechanismType;
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)
{
}
else
{
if (prepareResult == PrepareResult.NotRelevant)
{
calculationMessages.Add(new LogMessage(LogMessageType.Info, null,
string.Format(Resources.DesignCalculatorIrrelevant, location.Name,
soilGeometryProbability.ToString(), ""))); // TODO #The correct regional scenario ID?
}
if (prepareResult == PrepareResult.Failed)
{
calculationMessages.Add(new LogMessage(LogMessageType.Error, null,
string.Format(Resources.DesignCalculatorPrepareError, location.Name,
soilGeometryProbability.ToString(), ""))); // TODO #The correct regional scenario ID?
}
}
return calculationMessages;
}
///
/// Log messages
///
///
private void SendStabilityMessage(LogMessage logMessage)
{
lock (runningJobs)
{
if (logMessage.Subject != null)
{
var job = (RegionalScenarioProfileResult)runningJobs[(ICalculation)logMessage.Subject];
logMessage.Subject = job.Location;
}
}
sendMessageDelegate(logMessage);
}
///
/// Fill the results for the scenarios
///
private void FillResults(List tasks)
{
// Fill scenariosResult structure with jobs just run
foreach (Location location in evaluationJob.Locations)
{
try
{
RegionalScenariosResult scenariosResult = new RegionalScenariosResult();
if (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
{
ScenarioType = job.ScenarioType
};
scenariosResult.RegionalScenarioResults.Add(scenarioResult);
}
scenarioResult.RegionalScenarioProfileResults.Add(job);
}
}
// Combine results
foreach (var scenarioResult in scenariosResult.RegionalScenarioResults)
{
CombineProfiles(scenarioResult);
}
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.
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;
}
}
}
}
}