// Copyright (C) Stichting Deltares 2017. All rights reserved.
//
// This file is part of Ringtoets.
//
// Ringtoets is free software: you can redistribute it and/or modify
// it under the terms of the GNU 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 General Public License for more details.
//
// You should have received a copy of the GNU 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.Linq;
using Ringtoets.Common.Service;
using Ringtoets.MacroStabilityInwards.CalculatedInput.Converters;
using Ringtoets.MacroStabilityInwards.Data;
using Ringtoets.MacroStabilityInwards.KernelWrapper.Calculators;
using Ringtoets.MacroStabilityInwards.KernelWrapper.Calculators.Input;
using Ringtoets.MacroStabilityInwards.KernelWrapper.Calculators.UpliftVan;
using Ringtoets.MacroStabilityInwards.KernelWrapper.Calculators.UpliftVan.Input;
using Ringtoets.MacroStabilityInwards.KernelWrapper.Calculators.UpliftVan.Output;
using Ringtoets.MacroStabilityInwards.KernelWrapper.Kernels;
using Ringtoets.MacroStabilityInwards.Service.Converters;
using Ringtoets.MacroStabilityInwards.Service.Properties;
namespace Ringtoets.MacroStabilityInwards.Service
{
///
/// This class is responsible for invoking operations on the . Error and status information is
/// logged during the execution of the operation.
///
public static class MacroStabilityInwardsCalculationService
{
///
/// Performs validation over the values on the given . Error and status information is logged during
/// the execution of the operation.
///
/// The for which to validate the values.
/// false if contains validation errors; true otherwise.
/// Thrown when is null.
public static bool Validate(MacroStabilityInwardsCalculation calculation)
{
if (calculation == null)
{
throw new ArgumentNullException(nameof(calculation));
}
CalculationServiceHelper.LogValidationBegin();
string[] inputValidationResults = MacroStabilityInwardsInputValidator.Validate(calculation.InputParameters).ToArray();
if (inputValidationResults.Length > 0)
{
CalculationServiceHelper.LogMessagesAsError(inputValidationResults);
CalculationServiceHelper.LogValidationEnd();
return false;
}
UpliftVanCalculatorInput upliftVanCalculatorInput = CreateInputFromData(calculation.InputParameters);
IUpliftVanCalculator calculator = MacroStabilityInwardsCalculatorFactory.Instance.CreateUpliftVanCalculator(upliftVanCalculatorInput, MacroStabilityInwardsKernelWrapperFactory.Instance);
UpliftVanKernelMessage[] kernelMessages;
try
{
kernelMessages = calculator.Validate().ToArray();
}
catch (UpliftVanCalculatorException e)
{
CalculationServiceHelper.LogExceptionAsError(Resources.MacroStabilityInwardsCalculationService_Validate_Error_in_MacroStabilityInwards_validation, e);
CalculationServiceHelper.LogValidationEnd();
return false;
}
CalculationServiceHelper.LogMessagesAsError(kernelMessages.Where(msg => msg.ResultType == UpliftVanKernelMessageType.Error)
.Select(msg => msg.Message).ToArray());
CalculationServiceHelper.LogMessagesAsWarning(kernelMessages.Where(msg => msg.ResultType == UpliftVanKernelMessageType.Warning)
.Select(msg => msg.Message).ToArray());
CalculationServiceHelper.LogValidationEnd();
return kernelMessages.All(r => r.ResultType != UpliftVanKernelMessageType.Error);
}
///
/// Performs a macro stability inwards calculation based on the supplied and sets
/// based on the result if the calculation was successful. Error and status information is logged during
/// the execution of the operation.
///
/// The to base the input for the calculation upon.
/// Thrown when is null.
/// Consider calling first to see if calculation is possible.
/// Thrown when an error occurred during the calculation.
public static void Calculate(MacroStabilityInwardsCalculation calculation)
{
if (calculation == null)
{
throw new ArgumentNullException(nameof(calculation));
}
CalculationServiceHelper.LogCalculationBegin();
try
{
IUpliftVanCalculator calculator = MacroStabilityInwardsCalculatorFactory.Instance.CreateUpliftVanCalculator(
CreateInputFromData(calculation.InputParameters),
MacroStabilityInwardsKernelWrapperFactory.Instance);
UpliftVanCalculatorResult macroStabilityInwardsResult = calculator.Calculate();
if (macroStabilityInwardsResult.CalculationMessages.Any(cm => cm.ResultType == UpliftVanKernelMessageType.Error))
{
CalculationServiceHelper.LogMessagesAsError(macroStabilityInwardsResult.CalculationMessages
.Where(cm => cm.ResultType == UpliftVanKernelMessageType.Error)
.Select(cm => cm.Message).ToArray());
}
else
{
calculation.Output = new MacroStabilityInwardsOutput(
MacroStabilityInwardsSlidingCurveConverter.Convert(macroStabilityInwardsResult.SlidingCurveResult),
MacroStabilityInwardsSlipPlaneUpliftVanConverter.Convert(macroStabilityInwardsResult.CalculationGridResult),
new MacroStabilityInwardsOutput.ConstructionProperties
{
FactorOfStability = macroStabilityInwardsResult.FactorOfStability,
ZValue = macroStabilityInwardsResult.ZValue,
ForbiddenZonesXEntryMin = macroStabilityInwardsResult.ForbiddenZonesXEntryMin,
ForbiddenZonesXEntryMax = macroStabilityInwardsResult.ForbiddenZonesXEntryMax
});
}
if (macroStabilityInwardsResult.CalculationMessages.Any(cm => cm.ResultType == UpliftVanKernelMessageType.Warning))
{
CalculationServiceHelper.LogMessagesAsWarning(new[]
{
Resources.MacroStabilityInwardsCalculationService_Calculate_Warnings_in_MacroStabilityInwards_calculation + Environment.NewLine +
macroStabilityInwardsResult.CalculationMessages
.Where(cm => cm.ResultType == UpliftVanKernelMessageType.Warning)
.Aggregate(string.Empty, (current, logMessage) => current + $"* {logMessage.Message}{Environment.NewLine}").Trim()
});
}
}
catch (UpliftVanCalculatorException e)
{
CalculationServiceHelper.LogExceptionAsError(Resources.MacroStabilityInwardsCalculationService_Calculate_Error_in_MacroStabilityInwards_calculation, e);
throw;
}
finally
{
CalculationServiceHelper.LogCalculationEnd();
}
}
private static UpliftVanCalculatorInput CreateInputFromData(MacroStabilityInwardsInput inputParameters)
{
return new UpliftVanCalculatorInput(
new UpliftVanCalculatorInput.ConstructionProperties
{
WaternetCreationMode = WaternetCreationMode.CreateWaternet,
PlLineCreationMethod = PlLineCreationMethod.RingtoetsWti2017,
AssessmentLevel = inputParameters.AssessmentLevel,
LandwardDirection = LandwardDirection.PositiveX,
SurfaceLine = inputParameters.SurfaceLine,
SoilProfile = SoilProfileConverter.Convert(inputParameters.SoilProfileUnderSurfaceLine),
DrainageConstruction = DrainageConstructionConverter.Convert(inputParameters),
PhreaticLineOffsetsExtreme = PhreaticLineOffsetsConverter.Convert(inputParameters.LocationInputExtreme),
PhreaticLineOffsetsDaily = PhreaticLineOffsetsConverter.Convert(inputParameters.LocationInputDaily),
SlipPlane = UpliftVanSlipPlaneConverter.Convert(inputParameters),
DikeSoilScenario = inputParameters.DikeSoilScenario,
WaterLevelRiverAverage = inputParameters.WaterLevelRiverAverage,
WaterLevelPolderExtreme = inputParameters.LocationInputExtreme.WaterLevelPolder,
WaterLevelPolderDaily = inputParameters.LocationInputDaily.WaterLevelPolder,
MinimumLevelPhreaticLineAtDikeTopRiver = inputParameters.MinimumLevelPhreaticLineAtDikeTopRiver,
MinimumLevelPhreaticLineAtDikeTopPolder = inputParameters.MinimumLevelPhreaticLineAtDikeTopPolder,
LeakageLengthOutwardsPhreaticLine3 = inputParameters.LeakageLengthOutwardsPhreaticLine3,
LeakageLengthInwardsPhreaticLine3 = inputParameters.LeakageLengthInwardsPhreaticLine3,
LeakageLengthOutwardsPhreaticLine4 = inputParameters.LeakageLengthOutwardsPhreaticLine4,
LeakageLengthInwardsPhreaticLine4 = inputParameters.LeakageLengthInwardsPhreaticLine4,
PiezometricHeadPhreaticLine2Outwards = inputParameters.PiezometricHeadPhreaticLine2Outwards,
PiezometricHeadPhreaticLine2Inwards = inputParameters.PiezometricHeadPhreaticLine2Inwards,
PenetrationLengthExtreme = inputParameters.LocationInputExtreme.PenetrationLength,
PenetrationLengthDaily = inputParameters.LocationInputDaily.PenetrationLength,
AdjustPhreaticLine3And4ForUplift = inputParameters.AdjustPhreaticLine3And4ForUplift,
MoveGrid = inputParameters.MoveGrid,
MaximumSliceWidth = inputParameters.MaximumSliceWidth,
CreateZones = inputParameters.CreateZones,
AutomaticForbiddenZones = inputParameters.ZoningBoundariesDeterminationType == MacroStabilityInwardsZoningBoundariesDeterminationType.Automatic,
SlipPlaneMinimumDepth = inputParameters.SlipPlaneMinimumDepth,
SlipPlaneMinimumLength = inputParameters.SlipPlaneMinimumLength
});
}
}
}