// Copyright (C) Stichting Deltares 2017. 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.Text;
using Deltares.DamEngine.Calculators.KernelWrappers.Common;
using Deltares.DamEngine.Calculators.KernelWrappers.DamMacroStability;
using Deltares.DamEngine.Calculators.KernelWrappers.DamMacroStabilityOutwards;
using Deltares.DamEngine.Calculators.KernelWrappers.DamPipingBligh;
using Deltares.DamEngine.Calculators.KernelWrappers.DamPipingSellmeijer4Forces;
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.Standard.Logging;
namespace Deltares.DamEngine.Calculators.Dikes_Design
{
///
/// The Dam Engine design calculator
///
public class DesignCalculator
{
///
/// Performs the design calculation
///
/// The dam project data.
///
public List Execute(DamProjectData damProjectData)
{
IKernelWrapper kernelWrapper = CreateKernelWrapper(damProjectData.DamProjectCalculationSpecification.CurrentSpecification);
if (kernelWrapper == null)
{
throw new NotImplementedException(Resources.DesignCalculatorKernelNotImplemented);
}
damProjectData.DesignCalculations = new List();
var calculationMessages = new List();
for (int locationIndex = 0; locationIndex < damProjectData.Dike.Locations.Count; locationIndex++)
{
var location = damProjectData.Dike.Locations[locationIndex];
for (int subSoilScenarioIndex = 0; subSoilScenarioIndex < location.Segment.SoilProfileProbabilities.Count; subSoilScenarioIndex++)
{
var soiProfileProbability = location.Segment.SoilProfileProbabilities[subSoilScenarioIndex];
for (int designScenarioIndex = 0; designScenarioIndex < location.Scenarios.Count; designScenarioIndex++)
{
// Prepare input
var damKernelInput = new DamKernelInput();
damKernelInput.Location = location;
damKernelInput.SubSoilScenario = soiProfileProbability;
damKernelInput.DesignScenario = location.Scenarios[designScenarioIndex];
IKernelDataInput kernelDataInput;
IKernelDataOutput kernelDataOutput;
PrepareResult prepareResult = kernelWrapper.Prepare(damKernelInput, 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)
{
// Perform validation
List locationCalculationMessages = new List();
List validationMessages;
int errorCount = kernelWrapper.Validate(kernelDataInput, kernelDataOutput, out validationMessages);
if (errorCount > 0)
{
locationCalculationMessages.Add(new LogMessage(LogMessageType.Error, null,
string.Format(Resources.DesignCalculatorValidationFailed,
location.Name,
soiProfileProbability.ToString(),
damKernelInput.DesignScenario.LocationScenarioID)));
locationCalculationMessages.AddRange(validationMessages);
}
else
{
// Perform calculation
kernelWrapper.Execute(kernelDataInput, kernelDataOutput, out locationCalculationMessages);
}
// Process output
calculationMessages.AddRange(locationCalculationMessages);
DesignResult designResult;
StringBuilder sb = new StringBuilder();
foreach (var message in locationCalculationMessages)
{
sb.Append(message.Message + Environment.NewLine);
}
string resultMessage = sb.ToString();
kernelWrapper.PostProcess(damKernelInput, kernelDataOutput, resultMessage, out designResult);
damProjectData.DesignCalculations.Add(designResult);
}
}
}
}
return calculationMessages;
}
private IKernelWrapper CreateKernelWrapper(DamFailureMechanismeCalculationSpecification currentSpecification)
{
IKernelWrapper kernelWrapper = null;
switch (currentSpecification.FailureMechanismSystemType)
{
case FailureMechanismSystemType.HorizontalBalance:
throw new NotImplementedException(Resources.DesignCalculatorKernelNotImplemented);
case FailureMechanismSystemType.StabilityOutside:
kernelWrapper = new DamMacroStabilityOutwardsKernelWrapper();
var damMacroStabilityOutwardsKernelWrapper = (DamMacroStabilityOutwardsKernelWrapper) kernelWrapper;
damMacroStabilityOutwardsKernelWrapper.FailureMechanismeParamatersMStab = currentSpecification.FailureMechanismeParamatersMStab;
break;
case FailureMechanismSystemType.StabilityInside:
kernelWrapper = new DamMacroStabilityKernelWrapper();
var damMacroStabilityKernelWrapper = (DamMacroStabilityKernelWrapper) kernelWrapper;
damMacroStabilityKernelWrapper.FailureMechanismeParamatersMStab = currentSpecification.FailureMechanismeParamatersMStab;
break;
case FailureMechanismSystemType.Piping:
switch (currentSpecification.PipingModelType)
{
case PipingModelType.SellmeijerVnk:
case PipingModelType.Wti2017:
throw new NotImplementedException(Resources.DesignCalculatorKernelNotImplemented);
case PipingModelType.Bligh:
kernelWrapper = new DamPipingBlighKernelWrapper();
break;
case PipingModelType.Sellmeijer4Forces:
kernelWrapper = new DamPipingSellmeijer4ForcesKernelWrapper();
break;
}
break;
}
return kernelWrapper;
}
}
}