// 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.Properties;
using Ringtoets.Common.Data.AssessmentSection;
using Ringtoets.Common.Data.FailureMechanism;
using Ringtoets.Common.Service;
using Ringtoets.GrassCoverErosionInwards.Data;
using Ringtoets.HydraRing.Calculation.Data;
using Ringtoets.HydraRing.Calculation.Data.Input.Overtopping;
using Ringtoets.HydraRing.Calculation.Data.Output;
using Ringtoets.HydraRing.Calculation.Services;
using Ringtoets.HydraRing.IO;
using RingtoetsCommonServiceResources = Ringtoets.Common.Service.Properties.Resources;
namespace Ringtoets.GrassCoverErosionInwards.Service
{
///
/// Service that provides methods for performing Hydra-Ring calculations for grass cover erosion inwards calculations.
///
internal static class GrassCoverErosionInwardsCalculationService
{
///
/// 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.
/// False if contains validation errors; True otherwise.
internal static bool Validate(GrassCoverErosionInwardsCalculation 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 to base the input for the calculation upon.
/// The directory of the HLCD file that should be used for performing the calculation.
/// The to create input with.
/// The id of the ring to perform the calculation for.
/// The to create the input with for the calculation.
/// A on a successful calculation, null otherwise.
internal static ExceedanceProbabilityCalculationOutput Calculate(GrassCoverErosionInwardsCalculation calculation,
string hlcdDirectory, FailureMechanismSection failureMechanismSection,
string ringId, GeneralGrassCoverErosionInwardsInput generalInput)
{
var input = CreateInput(calculation, failureMechanismSection, generalInput);
return CalculationServiceHelper.PerformCalculation(calculation.Name,
() => HydraRingCalculationService.PerformCalculation(hlcdDirectory, ringId, HydraRingTimeIntegrationSchemeType.FBC, HydraRingUncertaintiesType.All, input),
Resources.GrassCoverErosionInwardsCalculationService_Calculate_Error_in_grass_cover_erosion_inwards_0_calculation);
}
private static OvertoppingCalculationInput CreateInput(GrassCoverErosionInwardsCalculation calculation, FailureMechanismSection failureMechanismSection, GeneralGrassCoverErosionInwardsInput generalInput)
{
return new OvertoppingCalculationInput((int) calculation.InputParameters.HydraulicBoundaryLocation.Id,
new HydraRingSection(1, failureMechanismSection.Name, failureMechanismSection.GetSectionLength(), calculation.InputParameters.Orientation),
calculation.InputParameters.DikeHeight,
generalInput.CriticalOvertoppingModelFactor,
generalInput.FbFactor.Mean, generalInput.FbFactor.StandardDeviation,
generalInput.FnFactor.Mean, generalInput.FnFactor.StandardDeviation,
generalInput.OvertoppingModelFactor,
calculation.InputParameters.CriticalFlowRate.Mean, calculation.InputParameters.CriticalFlowRate.StandardDeviation,
generalInput.FrunupModelFactor.Mean, generalInput.FrunupModelFactor.StandardDeviation,
generalInput.FshallowModelFactor.Mean, generalInput.FshallowModelFactor.StandardDeviation,
ParseProfilePoints(calculation.InputParameters.DikeGeometry),
ParseForeshore(calculation.InputParameters),
ParseBreakWater(calculation.InputParameters));
}
private static HydraRingBreakWater ParseBreakWater(GrassCoverErosionInwardsInput input)
{
return input.UseBreakWater ? new HydraRingBreakWater((int)input.BreakWater.Type, input.BreakWater.Height) : null;
}
private static IEnumerable ParseForeshore(GrassCoverErosionInwardsInput input)
{
var firstProfileSection = input.ForeshoreGeometry.FirstOrDefault();
if (!input.UseForeshore || firstProfileSection == null)
{
yield break;
}
yield return new HydraRingForelandPoint(firstProfileSection.StartingPoint.X, firstProfileSection.StartingPoint.Y);
foreach (var foreshore in input.ForeshoreGeometry)
{
yield return new HydraRingForelandPoint(foreshore.EndingPoint.X, foreshore.EndingPoint.Y);
}
}
private static IEnumerable ParseProfilePoints(IEnumerable profileSections)
{
var firstProfileSection = profileSections.FirstOrDefault();
if (firstProfileSection == null)
{
yield break;
}
// By default, the roughness is 1.0 (no reduction due to bed friction).
yield return new HydraRingRoughnessProfilePoint(firstProfileSection.StartingPoint.X, firstProfileSection.StartingPoint.Y, 1);
foreach (var profileSection in profileSections)
{
yield return new HydraRingRoughnessProfilePoint(profileSection.EndingPoint.X, profileSection.EndingPoint.Y, profileSection.Roughness);
}
}
private static List ValidateInput(GrassCoverErosionInwardsInput 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;
}
}
}