// 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.Collections.Generic;
using Ringtoets.Common.Service;
using Ringtoets.Common.Service.ValidationRules;
using Ringtoets.MacroStabilityInwards.Data;
using Ringtoets.MacroStabilityInwards.KernelWrapper;
using Ringtoets.MacroStabilityInwards.KernelWrapper.SubCalculator;
using Ringtoets.MacroStabilityInwards.Service.Properties;
using RingtoetsCommonServiceResources = Ringtoets.Common.Service.Properties.Resources;
using RingtoetsCommonFormsResources = Ringtoets.Common.Forms.Properties.Resources;
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 = ValidateInput(calculation.InputParameters).ToArray();
if (inputValidationResults.Length > 0)
{
CalculationServiceHelper.LogMessagesAsError(RingtoetsCommonServiceResources.Error_in_validation_0, inputValidationResults);
CalculationServiceHelper.LogValidationEnd();
return false;
}
List validationResults = new MacroStabilityInwardsCalculator(CreateInputFromData(calculation.InputParameters), MacroStabilityInwardsSubCalculatorFactory.Instance).Validate();
CalculationServiceHelper.LogMessagesAsError(RingtoetsCommonServiceResources.Error_in_validation_0, validationResults.ToArray());
CalculationServiceHelper.LogValidationEnd();
return validationResults.Count == 0;
}
///
/// 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.
public static void Calculate(MacroStabilityInwardsCalculation calculation)
{
if (calculation == null)
{
throw new ArgumentNullException(nameof(calculation));
}
CalculationServiceHelper.LogCalculationBegin();
try
{
MacroStabilityInwardsCalculatorResult macroStabilityInwardsResult = new MacroStabilityInwardsCalculator(CreateInputFromData(calculation.InputParameters), MacroStabilityInwardsSubCalculatorFactory.Instance).Calculate();
calculation.Output = new MacroStabilityInwardsOutput();
}
finally
{
CalculationServiceHelper.LogCalculationEnd();
}
}
private static List ValidateInput(MacroStabilityInwardsInput inputParameters)
{
var validationResults = new List();
validationResults.AddRange(ValidateHydraulics(inputParameters));
IEnumerable coreValidationError = ValidateCoreSurfaceLineAndSoilProfileProperties(inputParameters);
validationResults.AddRange(coreValidationError);
return validationResults;
}
private static IEnumerable ValidateHydraulics(MacroStabilityInwardsInput inputParameters)
{
var validationResults = new List();
if (!inputParameters.UseAssessmentLevelManualInput && inputParameters.HydraulicBoundaryLocation == null)
{
validationResults.Add(Resources.MacroStabilityInwardsCalculationService_ValidateInput_No_HydraulicBoundaryLocation_selected);
}
else
{
validationResults.AddRange(ValidateAssessmentLevel(inputParameters));
}
return validationResults;
}
private static IEnumerable ValidateAssessmentLevel(MacroStabilityInwardsInput inputParameters)
{
var validationResult = new List();
if (inputParameters.UseAssessmentLevelManualInput)
{
validationResult.AddRange(new NumericInputRule(inputParameters.AssessmentLevel, ParameterNameExtractor.GetFromDisplayName(RingtoetsCommonFormsResources.AssessmentLevel_DisplayName)).Validate());
}
else
{
if (double.IsNaN(inputParameters.AssessmentLevel))
{
validationResult.Add(Resources.MacroStabilityInwardsCalculationService_ValidateInput_Cannot_determine_AssessmentLevel);
}
}
return validationResult;
}
private static IEnumerable ValidateCoreSurfaceLineAndSoilProfileProperties(MacroStabilityInwardsInput inputParameters)
{
var validationResults = new List();
if (inputParameters.SurfaceLine == null)
{
validationResults.Add(Resources.MacroStabilityInwardsCalculationService_ValidateInput_No_SurfaceLine_selected);
}
if (inputParameters.StochasticSoilProfile == null)
{
validationResults.Add(Resources.MacroStabilityInwardsCalculationService_ValidateInput_No_StochasticSoilProfile_selected);
}
return validationResults;
}
private static MacroStabilityInwardsCalculatorInput CreateInputFromData(MacroStabilityInwardsInput inputParameters)
{
return new MacroStabilityInwardsCalculatorInput(
new MacroStabilityInwardsCalculatorInput.ConstructionProperties
{
AssessmentLevel = inputParameters.AssessmentLevel,
SurfaceLine = inputParameters.SurfaceLine,
SoilProfile = inputParameters.SoilProfileUnderSurfaceLine,
DikeSoilScenario = inputParameters.DikeSoilScenario,
WaterLevelRiverAverage = inputParameters.WaterLevelRiverAverage,
WaterLevelPolder = inputParameters.WaterLevelPolder,
XCoordinateDrainageConstruction = inputParameters.XCoordinateDrainageConstruction,
ZCoordinateDrainageConstruction = inputParameters.ZCoordinateDrainageConstruction,
MinimumLevelPhreaticLineAtDikeTopRiver = inputParameters.MinimumLevelPhreaticLineAtDikeTopRiver,
MinimumLevelPhreaticLineAtDikeTopPolder = inputParameters.MinimumLevelPhreaticLineAtDikeTopPolder,
PhreaticLineOffsetBelowDikeTopAtRiver = inputParameters.PhreaticLineOffsetBelowDikeTopAtRiver,
PhreaticLineOffsetBelowDikeTopAtPolder = inputParameters.PhreaticLineOffsetBelowDikeTopAtPolder,
PhreaticLineOffsetBelowShoulderBaseInside = inputParameters.PhreaticLineOffsetBelowShoulderBaseInside,
PhreaticLineOffsetBelowDikeToeAtPolder = inputParameters.PhreaticLineOffsetBelowDikeToeAtPolder,
LeakageLengthOutwardsPhreaticLine3 = inputParameters.LeakageLengthOutwardsPhreaticLine3,
LeakageLengthInwardsPhreaticLine3 = inputParameters.LeakageLengthInwardsPhreaticLine3,
LeakageLengthOutwardsPhreaticLine4 = inputParameters.LeakageLengthOutwardsPhreaticLine4,
LeakageLengthInwardsPhreaticLine4 = inputParameters.LeakageLengthInwardsPhreaticLine4,
PiezometricHeadPhreaticLine2Outwards = inputParameters.PiezometricHeadPhreaticLine2Outwards,
PiezometricHeadPhreaticLine2Inwards = inputParameters.PiezometricHeadPhreaticLine2Inwards,
PenetrationLength = inputParameters.PenetrationLength,
DrainageConstructionPresent = inputParameters.DrainageConstructionPresent,
AdjustPhreaticLine3And4ForUplift = inputParameters.AdjustPhreaticLine3And4ForUplift,
UseDefaultOffsets = inputParameters.UseDefaultOffsets,
MoveGrid = inputParameters.MoveGrid,
MaximumSliceWidth = inputParameters.MaximumSliceWidth,
GridAutomaticDetermined = inputParameters.GridDetermination == MacroStabilityInwardsGridDetermination.Automatic,
LeftGrid = inputParameters.LeftGrid,
RightGrid = inputParameters.RightGrid,
TangentLineAutomaticAtBoundaries = inputParameters.TangentLineDetermination == MacroStabilityInwardsTangentLineDetermination.LayerSeparated,
TangentLineZTop = inputParameters.TangentLineZTop,
TangentLineZBottom = inputParameters.TangentLineZBottom
});
}
}
}