// 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 Ringtoets.Common.Data.Calculation; using Ringtoets.Common.Data.Hydraulics; using Ringtoets.Common.Data.Probabilistics; using Ringtoets.Common.IO.Configurations.Helpers; using Ringtoets.Common.IO.Configurations.Import; using Ringtoets.Piping.Data; using Ringtoets.Piping.IO.Properties; using Ringtoets.Piping.Primitives; namespace Ringtoets.Piping.IO.Configurations { /// /// Imports a piping calculation configuration from an XML file and stores it on a /// . /// public class PipingCalculationConfigurationImporter : CalculationConfigurationImporter { private readonly IEnumerable availableHydraulicBoundaryLocations; private readonly PipingFailureMechanism failureMechanism; /// /// Creates a new instance of . /// /// The path to the XML file to import from. /// The calculation group to update. /// The hydraulic boundary locations /// used to check if the imported objects contain the right location. /// The piping failure mechanism used to check /// if the imported objects contain the right data. /// Thrown when any parameter is /// null. public PipingCalculationConfigurationImporter(string xmlFilePath, CalculationGroup importTarget, IEnumerable availableHydraulicBoundaryLocations, PipingFailureMechanism failureMechanism) : base(xmlFilePath, importTarget) { if (availableHydraulicBoundaryLocations == null) { throw new ArgumentNullException(nameof(availableHydraulicBoundaryLocations)); } if (failureMechanism == null) { throw new ArgumentNullException(nameof(failureMechanism)); } this.availableHydraulicBoundaryLocations = availableHydraulicBoundaryLocations; this.failureMechanism = failureMechanism; } protected override PipingCalculationConfigurationReader CreateCalculationConfigurationReader(string xmlFilePath) { return new PipingCalculationConfigurationReader(xmlFilePath); } protected override ICalculation ParseReadCalculation(PipingCalculationConfiguration calculationConfiguration) { var pipingCalculation = new PipingCalculationScenario(new GeneralPipingInput()) { Name = calculationConfiguration.Name }; if (TryReadHydraulicBoundaryData(calculationConfiguration, pipingCalculation) && TryReadSurfaceLine(calculationConfiguration, pipingCalculation) && TryReadEntryExitPoint(calculationConfiguration, pipingCalculation) && TryReadStochasticSoilModel(calculationConfiguration, pipingCalculation) && TryReadStochasticSoilProfile(calculationConfiguration, pipingCalculation) && TryReadStochasts(calculationConfiguration, pipingCalculation)) { return pipingCalculation; } return null; } /// /// Reads the hydraulic boundary location or the assessment level that is manually set. /// /// The calculation read from the imported file. /// The calculation to configure. /// false when the has a /// set which is not available in , true otherwise. private bool TryReadHydraulicBoundaryData(PipingCalculationConfiguration calculationConfiguration, PipingCalculationScenario pipingCalculation) { HydraulicBoundaryLocation location; bool locationRead = TryReadHydraulicBoundaryLocation(calculationConfiguration.HydraulicBoundaryLocation, calculationConfiguration.Name, availableHydraulicBoundaryLocations, out location); if (!locationRead) { return false; } if (location != null) { pipingCalculation.InputParameters.HydraulicBoundaryLocation = location; } else if (calculationConfiguration.AssessmentLevel.HasValue) { pipingCalculation.InputParameters.UseAssessmentLevelManualInput = true; pipingCalculation.InputParameters.AssessmentLevel = (RoundedDouble) calculationConfiguration.AssessmentLevel.Value; } return true; } /// /// Reads the surface line. /// /// The calculation read from the imported file. /// The calculation to configure. /// false when the has a /// set which is not available in , true otherwise. private bool TryReadSurfaceLine(PipingCalculationConfiguration calculationConfiguration, PipingCalculationScenario pipingCalculation) { if (calculationConfiguration.SurfaceLine != null) { RingtoetsPipingSurfaceLine surfaceLine = failureMechanism.SurfaceLines .FirstOrDefault(sl => sl.Name == calculationConfiguration.SurfaceLine); if (surfaceLine == null) { Log.LogCalculationConversionError(string.Format( Resources.PipingCalculationConfigurationImporter_ReadSurfaceLine_SurfaceLine_0_does_not_exist, calculationConfiguration.SurfaceLine), pipingCalculation.Name); return false; } pipingCalculation.InputParameters.SurfaceLine = surfaceLine; } return true; } /// /// Reads the entry point and exit point. /// /// The calculation read from the imported file. /// The calculation to configure. /// false when entry or exit point is set without , /// or when entry or exit point is invalid, true otherwise. private bool TryReadEntryExitPoint(PipingCalculationConfiguration calculationConfiguration, PipingCalculationScenario pipingCalculation) { bool hasEntryPoint = calculationConfiguration.EntryPointL.HasValue; bool hasExitPoint = calculationConfiguration.ExitPointL.HasValue; if (calculationConfiguration.SurfaceLine == null && (hasEntryPoint || hasExitPoint)) { Log.LogCalculationConversionError(Resources.PipingCalculationConfigurationImporter_ReadSurfaceLine_EntryPointL_or_ExitPointL_defined_without_SurfaceLine, pipingCalculation.Name); return false; } if (hasEntryPoint) { double entryPoint = calculationConfiguration.EntryPointL.Value; try { pipingCalculation.InputParameters.EntryPointL = (RoundedDouble) entryPoint; } catch (ArgumentOutOfRangeException e) { Log.LogOutOfRangeException(string.Format(Resources.PipingCalculationConfigurationImporter_ReadEntryExitPoint_Entry_point_invalid, entryPoint), pipingCalculation.Name, e); return false; } } if (hasExitPoint) { double exitPoint = calculationConfiguration.ExitPointL.Value; try { pipingCalculation.InputParameters.ExitPointL = (RoundedDouble) exitPoint; } catch (ArgumentOutOfRangeException e) { Log.LogOutOfRangeException(string.Format(Resources.PipingCalculationConfigurationImporter_ReadEntryExitPoint_Exit_point_invalid, exitPoint), pipingCalculation.Name, e); return false; } } return true; } /// /// Reads the stochastic soil model. /// /// The calculation read from the imported file. /// The calculation to configure. /// false when /// /// the has a set /// which is not available in the failure mechanism. /// The does not intersect with the /// when this is set. /// /// true otherwise. private bool TryReadStochasticSoilModel(PipingCalculationConfiguration calculationConfiguration, PipingCalculationScenario pipingCalculation) { if (calculationConfiguration.StochasticSoilModel != null) { StochasticSoilModel soilModel = failureMechanism.StochasticSoilModels .FirstOrDefault(ssm => ssm.Name == calculationConfiguration.StochasticSoilModel); if (soilModel == null) { Log.LogCalculationConversionError(string.Format( Resources.PipingCalculationConfigurationImporter_ReadStochasticSoilModel_Stochastische_soil_model_0_does_not_exist, calculationConfiguration.StochasticSoilModel), pipingCalculation.Name); return false; } if (pipingCalculation.InputParameters.SurfaceLine != null && !soilModel.IntersectsWithSurfaceLineGeometry(pipingCalculation.InputParameters.SurfaceLine)) { Log.LogCalculationConversionError(string.Format( Resources.PipingCalculationConfigurationImporter_ReadStochasticSoilModel_Stochastische_soil_model_0_does_not_intersect_with_surfaceLine_1, calculationConfiguration.StochasticSoilModel, calculationConfiguration.SurfaceLine), pipingCalculation.Name); return false; } pipingCalculation.InputParameters.StochasticSoilModel = soilModel; } return true; } /// /// Reads the stochastic soil profile. /// /// The calculation read from the imported file. /// The calculation to configure. /// false when the has: /// /// a set but no is specified; /// a set which is not available in the . /// /// true otherwise. private bool TryReadStochasticSoilProfile(PipingCalculationConfiguration calculationConfiguration, PipingCalculationScenario pipingCalculation) { if (calculationConfiguration.StochasticSoilProfile != null) { if (pipingCalculation.InputParameters.StochasticSoilModel == null) { Log.LogCalculationConversionError(string.Format( Resources.PipingCalculationConfigurationImporter_ReadStochasticSoilProfile_No_soil_model_provided_for_soil_profile_with_name_0, calculationConfiguration.StochasticSoilProfile), pipingCalculation.Name); return false; } StochasticSoilProfile soilProfile = pipingCalculation.InputParameters.StochasticSoilModel.StochasticSoilProfiles .FirstOrDefault(ssp => ssp.SoilProfile.Name == calculationConfiguration.StochasticSoilProfile); if (soilProfile == null) { Log.LogCalculationConversionError(string.Format( Resources.PipingCalculationConfigurationImporter_ReadStochasticSoilProfile_Stochastic_soil_profile_0_does_not_exist_within_soil_model_1, calculationConfiguration.StochasticSoilProfile, calculationConfiguration.StochasticSoilModel), pipingCalculation.Name); return false; } pipingCalculation.InputParameters.StochasticSoilProfile = soilProfile; } return true; } /// /// Reads the stochasts. /// /// The calculation read from the imported file. /// The calculation to configure. /// false when a stochast value (mean or standard deviation) is invalid, true otherwise. private bool TryReadStochasts(PipingCalculationConfiguration calculationConfiguration, PipingCalculationScenario pipingCalculation) { return TryReadPhreaticLevelExit(calculationConfiguration, pipingCalculation) && TryReadDampingFactorExit(calculationConfiguration, pipingCalculation); } private bool TryReadDampingFactorExit(PipingCalculationConfiguration calculationConfiguration, PipingCalculationScenario pipingCalculation) { return ConfigurationImportHelper.TrySetStandardDeviationStochast( PipingCalculationConfigurationSchemaIdentifiers.DampingFactorExitStochastName, pipingCalculation.Name, pipingCalculation.InputParameters, calculationConfiguration.DampingFactorExit, i => i.DampingFactorExit, (i,s) => i.DampingFactorExit = s, Log); } private bool TryReadPhreaticLevelExit(PipingCalculationConfiguration calculationConfiguration, PipingCalculationScenario pipingCalculation) { return ConfigurationImportHelper.TrySetStandardDeviationStochast( PipingCalculationConfigurationSchemaIdentifiers.PhreaticLevelExitStochastName, pipingCalculation.Name, pipingCalculation.InputParameters, calculationConfiguration.PhreaticLevelExit, i => i.PhreaticLevelExit, (i, s) => i.PhreaticLevelExit = s, Log); } } }