// 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 System.ComponentModel;
using System.Linq;
using Core.Common.Base.Data;
using Core.Common.Base.Geometry;
using Ringtoets.Common.Data.AssessmentSection;
using Ringtoets.Common.Data.Calculation;
namespace Ringtoets.MacroStabilityInwards.Data
{
///
/// Extension methods for obtaining level 2a results from output for an assessment of the macro stability inwards failure mechanism.
///
public static class MacroStabilityInwardsFailureMechanismSection2aAssessmentResultExtensions
{
///
/// Gets the value for the detailed assessment of safety per failure mechanism section as a probability.
///
/// The section result to get the assessment layer 2A for.
/// All calculations in the failure mechanism.
/// The failure mechanism the calculations belong to.
/// The assessment section the calculations belong to.
/// The calculated assessment layer 2A; or when there are no
/// performed calculations.
/// Thrown when any parameter is null.
public static double GetAssessmentLayerTwoA(this MacroStabilityInwardsFailureMechanismSectionResult sectionResult,
IEnumerable calculations,
MacroStabilityInwardsFailureMechanism failureMechanism,
IAssessmentSection assessmentSection)
{
if (sectionResult == null)
{
throw new ArgumentNullException(nameof(sectionResult));
}
if (calculations == null)
{
throw new ArgumentNullException(nameof(calculations));
}
if (failureMechanism == null)
{
throw new ArgumentNullException(nameof(failureMechanism));
}
if (assessmentSection == null)
{
throw new ArgumentNullException(nameof(assessmentSection));
}
List calculationScenarios = sectionResult
.GetCalculationScenarios(calculations)
.Where(cs => cs.Status == CalculationScenarioStatus.Done)
.ToList();
if (calculationScenarios.Any())
{
double totalAssessmentLayerTwoA = 0;
foreach (MacroStabilityInwardsCalculationScenario scenario in calculationScenarios)
{
var derivedOutput = DerivedMacroStabilityInwardsOutputFactory.Create(scenario.Output,
failureMechanism.MacroStabilityInwardsProbabilityAssessmentInput,
assessmentSection.FailureMechanismContribution.Norm,
failureMechanism.Contribution);
totalAssessmentLayerTwoA += derivedOutput.MacroStabilityInwardsProbability * (double) scenario.Contribution;
}
return totalAssessmentLayerTwoA;
}
return double.NaN;
}
///
/// Gets the contribution of all relevant together.
///
/// The result to get the result for.
/// All calculations in the failure mechanism.
public static RoundedDouble GetTotalContribution(this MacroStabilityInwardsFailureMechanismSectionResult macroStabilityInwardsFailureMechanismSectionResult,
IEnumerable calculations)
{
return (RoundedDouble) macroStabilityInwardsFailureMechanismSectionResult
.GetCalculationScenarios(calculations)
.Aggregate(0, (current, calculationScenario) => current + calculationScenario.Contribution);
}
///
/// Gets a list of the relevant .
///
/// The result to get the result for.
/// All calculations in the failure mechanism.
public static IEnumerable GetCalculationScenarios(this MacroStabilityInwardsFailureMechanismSectionResult macroStabilityInwardsFailureMechanismSectionResult,
IEnumerable calculations)
{
IEnumerable lineSegments = Math2D.ConvertLinePointsToLineSegments(macroStabilityInwardsFailureMechanismSectionResult.Section.Points);
return calculations
.Where(pc => pc.IsRelevant && pc.IsSurfaceLineIntersectionWithReferenceLineInSection(lineSegments));
}
///
/// Gets the status of the section result depending on the relevant calculation scenarios.
///
/// The result to get the result for.
/// All calculations in the failure mechanism.
/// Thrown when any of the relevant calculations
/// in has an invalid .
/// Thrown when any of the relevant scenarios has an unsupported value of .
public static CalculationScenarioStatus GetCalculationScenarioStatus(
this MacroStabilityInwardsFailureMechanismSectionResult macroStabilityInwardsFailureMechanismSectionResult,
IEnumerable calculations)
{
var failed = false;
var notCalculated = false;
foreach (MacroStabilityInwardsCalculationScenario calculationScenario in macroStabilityInwardsFailureMechanismSectionResult.GetCalculationScenarios(calculations).Where(cs => cs.IsRelevant))
{
CalculationScenarioStatus calculationScenarioStatus = calculationScenario.Status;
if (!Enum.IsDefined(typeof(CalculationScenarioStatus), calculationScenarioStatus))
{
throw new InvalidEnumArgumentException(nameof(macroStabilityInwardsFailureMechanismSectionResult),
(int)calculationScenarioStatus,
typeof(CalculationScenarioStatus));
}
switch (calculationScenario.Status)
{
case CalculationScenarioStatus.Failed:
failed = true;
break;
case CalculationScenarioStatus.NotCalculated:
notCalculated = true;
break;
case CalculationScenarioStatus.Done:
continue;
default:
throw new NotSupportedException($"The enum value {nameof(CalculationScenarioStatus)}.{calculationScenarioStatus} is not supported.");
}
}
if (failed)
{
return CalculationScenarioStatus.Failed;
}
if (notCalculated)
{
return CalculationScenarioStatus.NotCalculated;
}
return CalculationScenarioStatus.Done;
}
}
}