// Copyright (C) Stichting Deltares 2016. 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.Collections.Generic;
using System.Linq;
using Core.Common.Base.Data;
using log4net;
using Ringtoets.Common.Data.AssessmentSection;
using Ringtoets.Common.Data.FailureMechanism;
using Ringtoets.Common.Service;
using Ringtoets.HeightStructures.Data;
using Ringtoets.HeightStructures.Service.Properties;
using Ringtoets.HydraRing.Calculation.Calculator;
using Ringtoets.HydraRing.Calculation.Calculator.Factory;
using Ringtoets.HydraRing.Calculation.Data;
using Ringtoets.HydraRing.Calculation.Data.Input.Structures;
using Ringtoets.HydraRing.Calculation.Parsers;
using Ringtoets.HydraRing.IO;
using RingtoetsCommonServiceResources = Ringtoets.Common.Service.Properties.Resources;
using RingtoetsCommonDataResources = Ringtoets.Common.Data.Properties.Resources;
namespace Ringtoets.HeightStructures.Service
{
///
/// Service that provides methods for performing Hydra-Ring calculations for height structures calculations.
///
public class HeightStructuresCalculationService
{
private static readonly ILog log = LogManager.GetLogger(typeof(HeightStructuresCalculationService));
private IStructuresOvertoppingCalculator calculator;
private bool canceled;
///
/// 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.
/// The for which to validate the values.
/// Truec> if has no validation errors; Falsec> otherwise.
public bool Validate(HeightStructuresCalculation calculation, IAssessmentSection assessmentSection)
{
CalculationServiceHelper.LogValidationBeginTime(calculation.Name);
var messages = ValidateInput(calculation.InputParameters, assessmentSection);
CalculationServiceHelper.LogMessagesAsError(RingtoetsCommonServiceResources.Error_in_validation_0, messages);
CalculationServiceHelper.LogValidationEndTime(calculation.Name);
return !messages.Any();
}
///
/// Cancels any currently running height structures calculation.
///
public void Cancel()
{
if (calculator != null)
{
calculator.Cancel();
}
canceled = true;
}
///
/// Performs a height structures calculation based on the supplied and sets
/// if the calculation was successful. Error and status information is logged during the execution of the operation.
///
/// The that holds all the information required to perform the calculation.
/// The that holds information about the norm used in the calculation.
/// The to create input with.
/// The to create the input with for the calculation.
/// The amount of contribution for this failure mechanism in the assessment section.
/// The directory of the HLCD file that should be used for performing the calculation.
internal void Calculate(HeightStructuresCalculation calculation,
IAssessmentSection assessmentSection,
FailureMechanismSection failureMechanismSection,
GeneralHeightStructuresInput generalInput,
double failureMechanismContribution,
string hlcdDirectory)
{
var calculationName = calculation.Name;
StructuresOvertoppingCalculationInput input = CreateInput(calculation, failureMechanismSection, generalInput);
calculator = HydraRingCalculatorFactory.Instance.CreateStructuresOvertoppingCalculator(hlcdDirectory, assessmentSection.Id);
CalculationServiceHelper.LogCalculationBeginTime(calculationName);
try
{
calculator.Calculate(input);
if (!canceled)
{
calculation.Output = ProbabilityAssessmentService.Calculate(assessmentSection.FailureMechanismContribution.Norm,
failureMechanismContribution,
generalInput.N,
calculator.ExceedanceProbabilityBeta);
}
}
catch (HydraRingFileParserException)
{
if (!canceled)
{
log.ErrorFormat(Resources.HeightStructuresCalculationService_Calculate_Error_in_height_structures_0_calculation, calculationName);
throw;
}
}
finally
{
log.InfoFormat(Resources.HeightStructuresCalculationService_Calculate_Calculation_report_Click_details_for_full_report, calculator.OutputFileContent);
CalculationServiceHelper.LogCalculationEndTime(calculationName);
}
}
private static StructuresOvertoppingCalculationInput CreateInput(HeightStructuresCalculation calculation, FailureMechanismSection failureMechanismSection, GeneralHeightStructuresInput generalInput)
{
return new StructuresOvertoppingCalculationInput(
calculation.InputParameters.HydraulicBoundaryLocation.Id,
new HydraRingSection(1, failureMechanismSection.GetSectionLength(), calculation.InputParameters.StructureNormalOrientation),
ParseForeshore(calculation.InputParameters),
ParseBreakWater(calculation.InputParameters),
generalInput.GravitationalAcceleration,
generalInput.ModelFactorOvertoppingFlow.Mean, generalInput.ModelFactorOvertoppingFlow.StandardDeviation,
calculation.InputParameters.LevelCrestStructure.Mean, calculation.InputParameters.LevelCrestStructure.StandardDeviation,
calculation.InputParameters.StructureNormalOrientation,
calculation.InputParameters.ModelFactorSuperCriticalFlow.Mean, calculation.InputParameters.ModelFactorSuperCriticalFlow.StandardDeviation,
calculation.InputParameters.AllowedLevelIncreaseStorage.Mean, calculation.InputParameters.AllowedLevelIncreaseStorage.StandardDeviation,
generalInput.ModelFactorStorageVolume.Mean, generalInput.ModelFactorStorageVolume.StandardDeviation,
calculation.InputParameters.StorageStructureArea.Mean, calculation.InputParameters.StorageStructureArea.CoefficientOfVariation,
generalInput.ModelFactorInflowVolume,
calculation.InputParameters.FlowWidthAtBottomProtection.Mean, calculation.InputParameters.FlowWidthAtBottomProtection.StandardDeviation,
calculation.InputParameters.CriticalOvertoppingDischarge.Mean, calculation.InputParameters.CriticalOvertoppingDischarge.CoefficientOfVariation,
calculation.InputParameters.FailureProbabilityStructureWithErosion,
calculation.InputParameters.WidthFlowApertures.Mean, calculation.InputParameters.WidthFlowApertures.CoefficientOfVariation,
calculation.InputParameters.DeviationWaveDirection,
calculation.InputParameters.StormDuration.Mean, calculation.InputParameters.StormDuration.CoefficientOfVariation);
}
private static IEnumerable ParseForeshore(HeightStructuresInput input)
{
return input.UseForeshore ? input.ForeshoreGeometry.Select(c => new HydraRingForelandPoint(c.X, c.Y)) : new HydraRingForelandPoint[0];
}
private static HydraRingBreakWater ParseBreakWater(HeightStructuresInput input)
{
return input.UseBreakWater ? new HydraRingBreakWater((int) input.BreakWater.Type, input.BreakWater.Height) : null;
}
private static string[] ValidateInput(HeightStructuresInput inputParameters, IAssessmentSection assessmentSection)
{
var validationResult = new List();
var validationProblem = HydraulicDatabaseHelper.ValidatePathForCalculation(assessmentSection.HydraulicBoundaryDatabase.FilePath);
if (!string.IsNullOrEmpty(validationProblem))
{
validationResult.Add(validationProblem);
return validationResult.ToArray();
}
if (inputParameters.HydraulicBoundaryLocation == null)
{
validationResult.Add(RingtoetsCommonServiceResources.CalculationService_ValidateInput_No_hydraulic_boundary_location_selected);
}
if (inputParameters.Structure == null)
{
// TODO: put in resource
validationResult.Add("Er is geen kunstwerk geselecteerd.");
}
else
{
validationResult.AddRange(DistributionValidationService.ValidateDistribution(inputParameters.StormDuration, "stormduur"));
if (IsInvalidNumber(inputParameters.DeviationWaveDirection))
{
validationResult.Add(string.Format("De waarde voor '{0}' moet een geldig getal zijn.", "afwijking golfrichting"));
}
validationResult.AddRange(DistributionValidationService.ValidateDistribution(inputParameters.ModelFactorSuperCriticalFlow, "modelfactor overloopdebiet volkomen overlaat"));
if (IsInvalidNumber(inputParameters.StructureNormalOrientation))
{
validationResult.Add(RingtoetsCommonDataResources.Orientation_Value_needs_to_be_between_0_and_360);
}
validationResult.AddRange(DistributionValidationService.ValidateDistribution(inputParameters.FlowWidthAtBottomProtection, "stroomvoerende breedte bodembescherming"));
validationResult.AddRange(DistributionValidationService.ValidateDistribution(inputParameters.WidthFlowApertures, "breedte van doorstroomopening"));
validationResult.AddRange(DistributionValidationService.ValidateDistribution(inputParameters.StorageStructureArea, "kombergend oppervlak"));
validationResult.AddRange(DistributionValidationService.ValidateDistribution(inputParameters.AllowedLevelIncreaseStorage, "toegestane peilverhoging komberging"));
validationResult.AddRange(DistributionValidationService.ValidateDistribution(inputParameters.LevelCrestStructure, "kerende hoogte"));
validationResult.AddRange(DistributionValidationService.ValidateDistribution(inputParameters.CriticalOvertoppingDischarge, "kritiek instromend debiet"));
// Probability structure given erosion
}
return validationResult.ToArray();
}
private static bool IsInvalidNumber(RoundedDouble value)
{
return double.IsNaN(value) || double.IsInfinity(value);
}
}
}