// 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.Linq;
using Core.Common.Base.Data;
using Core.Common.Base.Geometry;
using Ringtoets.Common.Data.Probabilistics;
using Ringtoets.Common.IO.Exceptions;
using Ringtoets.Common.IO.SoilProfile;
using Ringtoets.MacroStabilityInwards.IO.Properties;
using Ringtoets.MacroStabilityInwards.Primitives;
using RingtoetsCommonIOResources = Ringtoets.Common.IO.Properties.Resources;
namespace Ringtoets.MacroStabilityInwards.IO.SoilProfiles
{
///
/// Transforms generic into
/// or .
///
internal static class MacroStabilityInwardsSoilLayerTransformer
{
private const double tolerance = 1e-6;
///
/// Transforms the generic into a
/// .
///
/// The soil layer to use in the transformation.
/// A based on the given data.
/// Thrown when is
/// null.
/// Thrown when transformation would not result
/// in a valid transformed instance.
public static MacroStabilityInwardsSoilLayer1D Transform(SoilLayer1D soilLayer)
{
if (soilLayer == null)
{
throw new ArgumentNullException(nameof(soilLayer));
}
ValidateStochasticParameters(soilLayer);
var layer = new MacroStabilityInwardsSoilLayer1D(soilLayer.Top);
SetProperties(soilLayer, layer.Properties);
return layer;
}
///
/// Transforms the generic into a
/// .
///
/// The soil layer to use in the transformation.
/// A based on the given data.
/// Thrown when is
/// null.
/// Thrown when transformation would not result
/// in a valid transformed instance.
public static MacroStabilityInwardsSoilLayer2D Transform(SoilLayer2D soilLayer)
{
if (soilLayer == null)
{
throw new ArgumentNullException(nameof(soilLayer));
}
ValidateStochasticParameters(soilLayer);
if (soilLayer.OuterLoop == null)
{
throw new ImportedDataTransformException();
}
Ring outerRing = TransformSegmentToRing(soilLayer.OuterLoop);
Ring[] innerRings = soilLayer.InnerLoops.Select(TransformSegmentToRing).ToArray();
var layer = new MacroStabilityInwardsSoilLayer2D(outerRing, innerRings);
SetProperties(soilLayer, layer.Properties);
return layer;
}
///
/// Sets the properties of the .
///
/// The soil layer to get the properties from.
/// The properties to set the data upon.
/// Thrown when transformation would not result
/// in a valid transformed instance.
private static void SetProperties(SoilLayerBase soilLayer, MacroStabilityInwardsSoilLayerProperties properties)
{
properties.ShearStrengthModel = TransformShearStrengthModel(soilLayer.ShearStrengthModel);
properties.UsePop = TransformUsePop(soilLayer.UsePop);
properties.MaterialName = soilLayer.MaterialName;
properties.IsAquifer = TransformIsAquifer(soilLayer.IsAquifer);
properties.Color = SoilLayerColorConverter.Convert(soilLayer.Color);
properties.AbovePhreaticLevel = new VariationCoefficientLogNormalDistribution
{
Mean = (RoundedDouble) soilLayer.AbovePhreaticLevelMean,
CoefficientOfVariation = (RoundedDouble) soilLayer.AbovePhreaticLevelCoefficientOfVariation,
Shift = (RoundedDouble) soilLayer.AbovePhreaticLevelShift
};
properties.BelowPhreaticLevel = new VariationCoefficientLogNormalDistribution
{
Mean = (RoundedDouble) soilLayer.BelowPhreaticLevelMean,
CoefficientOfVariation = (RoundedDouble) soilLayer.BelowPhreaticLevelCoefficientOfVariation,
Shift = (RoundedDouble) soilLayer.BelowPhreaticLevelShift
};
properties.Cohesion = new VariationCoefficientLogNormalDistribution
{
Mean = (RoundedDouble) soilLayer.CohesionMean,
CoefficientOfVariation = (RoundedDouble) soilLayer.CohesionCoefficientOfVariation
};
properties.FrictionAngle = new VariationCoefficientLogNormalDistribution
{
Mean = (RoundedDouble) soilLayer.FrictionAngleMean,
CoefficientOfVariation = (RoundedDouble) soilLayer.FrictionAngleCoefficientOfVariation
};
properties.ShearStrengthRatio = new VariationCoefficientLogNormalDistribution
{
Mean = (RoundedDouble) soilLayer.ShearStrengthRatioMean,
CoefficientOfVariation = (RoundedDouble) soilLayer.ShearStrengthRatioCoefficientOfVariation
};
properties.StrengthIncreaseExponent = new VariationCoefficientLogNormalDistribution
{
Mean = (RoundedDouble) soilLayer.StrengthIncreaseExponentMean,
CoefficientOfVariation = (RoundedDouble) soilLayer.StrengthIncreaseExponentCoefficientOfVariation
};
properties.Pop = new VariationCoefficientLogNormalDistribution
{
Mean = (RoundedDouble) soilLayer.PopMean,
CoefficientOfVariation = (RoundedDouble) soilLayer.PopCoefficientOfVariation
};
}
///
/// Transforms a to a for the
/// is aquifer property of soil layers.
///
/// The value to transform.
/// A based on .
/// Thrown when
/// could not be transformed.
private static bool TransformIsAquifer(double? isAquifer)
{
try
{
return SoilLayerIsAquiferConverter.Convert(isAquifer);
}
catch (NotSupportedException)
{
throw new ImportedDataTransformException(string.Format(RingtoetsCommonIOResources.Transform_Invalid_value_ParameterName_0,
RingtoetsCommonIOResources.SoilLayerProperties_IsAquifer_DisplayName));
}
}
///
/// Transforms a to a for the
/// use POP property of soil layers.
///
/// The value to transform.
/// A based on .
/// Thrown when
/// could not be transformed.
private static bool TransformUsePop(double? usePop)
{
if (!usePop.HasValue)
{
return true;
}
if (Math.Abs(usePop.Value) < tolerance)
{
return false;
}
throw new ImportedDataTransformException(string.Format(RingtoetsCommonIOResources.Transform_Invalid_value_ParameterName_0,
Resources.SoilLayerProperties_UsePop_Description));
}
///
/// Transforms a to a for the
/// shear strength model of soil layers.
///
/// The value to transform.
/// A based
/// on .
/// Thrown when
/// could not be transformed.
private static MacroStabilityInwardsShearStrengthModel TransformShearStrengthModel(double? shearStrengthModel)
{
if (!shearStrengthModel.HasValue)
{
return MacroStabilityInwardsShearStrengthModel.CPhi;
}
if (Math.Abs(shearStrengthModel.Value - 6) < tolerance)
{
return MacroStabilityInwardsShearStrengthModel.SuCalculated;
}
if (Math.Abs(shearStrengthModel.Value - 9) < tolerance)
{
return MacroStabilityInwardsShearStrengthModel.CPhiOrSuCalculated;
}
if (Math.Abs(shearStrengthModel.Value - 1) < tolerance)
{
throw new ImportedDataTransformException(Resources.MacroStabilityInwardsSoilLayerTransformer_TransformShearStrengthModel_No_MacroStabilityInwardsShearStrengthModel);
}
throw new ImportedDataTransformException(string.Format(RingtoetsCommonIOResources.Transform_Invalid_value_ParameterName_0,
Resources.SoilLayerProperties_ShearStrengthModel_Description));
}
///
/// Transforms a collection of to .
///
/// The segments to transform.
/// A based on .
/// Thrown when
/// could not be transformed to a .
private static Ring TransformSegmentToRing(IEnumerable segments)
{
try
{
var points = new List();
foreach (Segment2D segment in segments)
{
points.AddRange(new[]
{
segment.FirstPoint,
segment.SecondPoint
});
}
return new Ring(points.Distinct());
}
catch (ArgumentException e)
{
throw new ImportedDataTransformException(Resources.MacroStabilityInwardsSoilLayerTransformer_TransformSegmentToRing_Invalid_geometry_for_Ring, e);
}
}
///
/// Validates whether the values of the distribution and shift for the stochastic parameters
/// are correct for creating a soil layer.
///
/// The soil layer to validate.
/// Thrown when any of the distributions of the
/// stochastic parameters is not defined as lognormal or is shifted when it should not be.
private static void ValidateStochasticParameters(SoilLayerBase soilLayer)
{
DistributionHelper.ValidateIsLogNormal(soilLayer.AbovePhreaticLevelDistributionType,
Resources.SoilLayerProperties_AbovePhreaticLevelDistribution_Description);
DistributionHelper.ValidateIsLogNormal(
soilLayer.BelowPhreaticLevelDistributionType,
Resources.SoilLayerProperties_BelowPhreaticLevelDistribution_DisplayName);
DistributionHelper.ValidateIsNonShiftedLogNormal(
soilLayer.CohesionDistributionType,
soilLayer.CohesionShift,
Resources.SoilLayerProperties_CohesionDistribution_DisplayName);
DistributionHelper.ValidateIsNonShiftedLogNormal(
soilLayer.FrictionAngleDistributionType,
soilLayer.FrictionAngleShift,
Resources.SoilLayerProperties_FrictionAngleDistribution_DisplayName);
DistributionHelper.ValidateIsNonShiftedLogNormal(
soilLayer.ShearStrengthRatioDistributionType,
soilLayer.ShearStrengthRatioShift,
Resources.SoilLayerProperties_ShearStrengthRatioDistribution_DisplayName);
DistributionHelper.ValidateIsNonShiftedLogNormal(
soilLayer.StrengthIncreaseExponentDistributionType,
soilLayer.StrengthIncreaseExponentShift,
Resources.SoilLayerProperties_StrengthIncreaseExponentDistribution_DisplayName);
DistributionHelper.ValidateIsNonShiftedLogNormal(
soilLayer.PopDistributionType,
soilLayer.PopShift,
Resources.SoilLayerProperties_PopDistribution_DisplayName);
}
}
}