// Copyright (C) Stichting Deltares 2024. All rights reserved.
//
// This file is part of the Dam Engine.
//
// The Dam Engine is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero 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 Deltares.DamEngine.Calculators.Uplift;
using Deltares.DamEngine.Data.General;
using Deltares.DamEngine.Data.General.PlLines;
using Deltares.DamEngine.Data.General.Sensors;
using Deltares.DamEngine.Data.Geotechnics;
namespace Deltares.DamEngine.Calculators.KernelWrappers.Common;
///
/// Helper for Uplift
///
public class UpliftHelper
{
///
/// Determines the pl lines for stability.
///
/// The dam kernel input.
/// if set to true [use rivel level low].
/// The uplift situation.
/// pl lines
public PlLines DeterminePlLinesForStability(DamKernelInput damKernelInput, bool useRivelLevelLow,
out UpliftSituation upliftSituation)
{
double riverLevel = damKernelInput.RiverLevelHigh;
double? riverLevelLow = null;
if (useRivelLevelLow)
{
riverLevelLow = damKernelInput.RiverLevelLow;
}
var sensorPlLineCreatorSettings = new SensorPlLineCreatorSettings
{
SensorLocation = damKernelInput.Location.SensorLocation,
DateTime = damKernelInput.TimeStepDateTime
};
PlLines plLines = PlLinesHelper.CreatePlLinesForStability(damKernelInput.Location, damKernelInput.SubSoilScenario, riverLevel,
riverLevelLow, sensorPlLineCreatorSettings, out upliftSituation);
upliftSituation.IsUplift = DetermineIsUplift(plLines, damKernelInput.Location, damKernelInput.SubSoilScenario, out double xCoordLowestUpliftFactor);
upliftSituation.XCoordinateLowestUpliftFactorPoint = xCoordLowestUpliftFactor;
return plLines;
}
///
/// Determines if there is uplift.
///
/// The pl lines.
/// The location.
/// The sub soil scenario.
/// The X co-ordinate of the point with the lowest uplift factor.
/// true if there is uplift
private static bool DetermineIsUplift(PlLines plLines, Location location, SoilGeometryProbability subSoilScenario, out double xCoordLowestUpliftFactor)
{
if (plLines == null)
{
xCoordLowestUpliftFactor = double.NaN;
return false;
}
double upliftCriterion = location.ModelFactors.UpliftCriterionStability;
SurfaceLine2 surfaceLineWithNewHeight = location.SurfaceLine;
double? upliftFactor = GetLowestUpliftFactor(surfaceLineWithNewHeight,
subSoilScenario.SoilProfile1D, subSoilScenario.SoilProfile2D, plLines,
location, out xCoordLowestUpliftFactor);
return upliftFactor < upliftCriterion;
}
private static double? GetLowestUpliftFactor(SurfaceLine2 surfaceLine, SoilProfile1D soilProfile1D,
SoilProfile2D soilProfile2D, PlLines plLines, Location location,
out double xCoordLowestUpliftFactor)
{
var upliftLocationDeterminator = new UpliftLocationDeterminator
{
SurfaceLine = surfaceLine,
SoilProfile = soilProfile1D,
SoilProfile2D = soilProfile2D,
SoilList = location.SoilList,
DikeEmbankmentMaterial = location.GetDikeEmbankmentSoil(),
PlLines = plLines,
XSoilGeometry2DOrigin = location.XSoilGeometry2DOrigin
};
UpliftLocationAndResult upliftLocationAndResult = upliftLocationDeterminator.GetLocationWithLowestUpliftFactor();
xCoordLowestUpliftFactor = upliftLocationAndResult?.X ?? double.NaN;
return upliftLocationAndResult?.UpliftFactor;
}
}