// 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 Core.Common.Base.IO;
using log4net;
using Ringtoets.Common.Data.AssessmentSection;
using Ringtoets.Common.Data.FailureMechanism;
using Ringtoets.Common.Data.Probabilistics;
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.Data.Output;
using Ringtoets.HydraRing.Calculation.Parsers;
using Ringtoets.HydraRing.Calculation.Services;
using Ringtoets.HydraRing.IO;
using RingtoetsCommonServiceResources = Ringtoets.Common.Service.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)
{
return CalculationServiceHelper.PerformValidation(calculation.Name, () => ValidateInput(calculation.InputParameters, assessmentSection));
}
///
/// 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.
/// A on a successful calculation, null otherwise.
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("Hoogte kunstwerken berekeningsverslag. Klik op details voor meer informatie.\n{0}", calculator.OutputFileContent);
CalculationServiceHelper.LogCalculationEndTime(calculationName);
}
}
public void Cancel()
{
if (calculator != null)
{
calculator.Cancel();
}
canceled = true;
}
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),
new HydraRingForelandPoint[0],
null,
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.GetVariationCoefficient(),
generalInput.ModelFactorInflowVolume,
calculation.InputParameters.FlowWidthAtBottomProtection.Mean, calculation.InputParameters.FlowWidthAtBottomProtection.StandardDeviation,
calculation.InputParameters.CriticalOvertoppingDischarge.Mean, calculation.InputParameters.CriticalOvertoppingDischarge.GetVariationCoefficient(),
calculation.InputParameters.FailureProbabilityStructureWithErosion,
calculation.InputParameters.WidthOfFlowApertures.Mean, calculation.InputParameters.WidthOfFlowApertures.GetVariationCoefficient(),
calculation.InputParameters.DeviationOfTheWaveDirection,
calculation.InputParameters.StormDuration.Mean, calculation.InputParameters.StormDuration.GetVariationCoefficient());
}
private static string[] ValidateInput(HeightStructuresInput inputParameters, IAssessmentSection assessmentSection)
{
List validationResult = new List();
if (inputParameters.HydraulicBoundaryLocation == null)
{
validationResult.Add(RingtoetsCommonServiceResources.CalculationService_ValidateInput_No_hydraulic_boundary_location_selected);
}
var validationProblem = HydraulicDatabaseHelper.ValidatePathForCalculation(assessmentSection.HydraulicBoundaryDatabase.FilePath);
if (!string.IsNullOrEmpty(validationProblem))
{
validationResult.Add(validationProblem);
}
return validationResult.ToArray();
}
}
}