// Copyright (C) Stichting Deltares 2024. All rights reserved. // // This file is part of the D-Soil Model application. // // The D-Soil Model application 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 Deltares.Geotechnics.Soils; using Deltares.Probabilistic; using Deltares.Standard; using Deltares.Standard.Reflection; namespace Deltares.DSoilModel.Data { /// /// Conversion between measured and design values for DSoilModel /// note: in DSoilModel the default transformation of design values is replaced by ActualValue field of Stochast /// /// public class DSoilModelTransformer : ITransformer { private bool IsSupported(object obj) { return obj is Soil || obj is PreConsolidationStress; } private Stochast FindAutoStochastPropertyInPreConsolidationStress(PreConsolidationStress pcs, string property) { // currect exception from the rule dictated by huge amount of dependency on original property names and // changing it is way to complex and affects a lot of legacy projects return pcs.GetMemberName(x => x.StressValue) == property ? pcs.StressStochast : null; } /// /// Gets a stochast by associated property name /// /// private Stochast FindAutoStochastProperty(object obj, string property) { if (obj is PreConsolidationStress) // exceptional case { return FindAutoStochastPropertyInPreConsolidationStress((PreConsolidationStress) obj, property); } // AutoStochastProperty convention, in fact, is the naming convention "{property name}Stochast" only // so, for best performance we can locate the Stochast by name rather than looping over all stochasts checking their property association if (!String.IsNullOrEmpty(property)) { var propertyInfo = obj.GetType().GetProperty(property + "Stochast"); if (propertyInfo != null) { var stochast = (Stochast)propertyInfo.GetValue(obj, null); if (stochast.AssociatedPropertyName == property || stochast.Name == property) { return stochast; } } } return null; } /// /// Project a given property to its 'design value', possibly dependent on the associated /// random behavior of that parameter. /// /// The owner of the property.The parameter property name.The default value. /// /// Returns when given property has no stochastic /// behavior, or the design value when it does and is handled by this transformer. /// public double GetTransformedValue(object owner, string property, double measuredValue) { if (IsSupported(owner)) { var stochast = FindAutoStochastProperty(owner, property); if (stochast != null) { return stochast.ActualValue; } } return measuredValue; } /// /// Projects a given 'design value' back to the parameter, updating the parameterization /// of the random variable associated with that parameter if required. /// /// The owner of the property.The parameter property name.The new 'design value'. /// /// Returns when given property has no stochastic /// behavior, or the back projected value when it does and is handled by this transformer. /// public double SetTransformedValue(object owner, string property, double transformedValue) { if (IsSupported(owner)) { var stochast = FindAutoStochastProperty(owner, property); if (stochast != null) { stochast.ActualValue = transformedValue; // In property setter, the return value of this method is used to set the stochast mean return stochast.Mean; } } return transformedValue; } } }