// Copyright (C) Stichting Deltares 2016. 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.FileImporters; using Ringtoets.Piping.Data; using Ringtoets.Piping.IO.Properties; using Ringtoets.Piping.IO.Readers; using Ringtoets.Piping.IO.Schema; using Ringtoets.Piping.Primitives; using RingtoetsCommonIOResources = Ringtoets.Common.IO.Properties.Resources; namespace Ringtoets.Piping.IO.Importers { /// /// 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(ReadPipingCalculation readCalculation) { var pipingCalculation = new PipingCalculationScenario(new GeneralPipingInput()) { Name = readCalculation.Name }; if (!ReadHydraulicBoundaryData(readCalculation, pipingCalculation)) { return null; } if (!ReadSurfaceLine(readCalculation, pipingCalculation)) { return null; } if (!ReadEntryExitPoint(readCalculation, pipingCalculation)) { return null; } if (!ReadStochasticSoilModel(readCalculation, pipingCalculation)) { return null; } if (!ReadStochasticSoilProfile(readCalculation, pipingCalculation)) { return null; } if (!ReadStochasts(readCalculation, pipingCalculation)) { return null; } return pipingCalculation; } /// /// 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 ReadHydraulicBoundaryData(ReadPipingCalculation readCalculation, PipingCalculationScenario pipingCalculation) { if (readCalculation.HydraulicBoundaryLocation != null) { HydraulicBoundaryLocation location = availableHydraulicBoundaryLocations .FirstOrDefault(l => l.Name == readCalculation.HydraulicBoundaryLocation); if (location == null) { LogReadCalculationConversionError( string.Format( RingtoetsCommonIOResources.CalculationConfigurationImporter_ReadHydraulicBoundaryLocation_Hydraulic_boundary_location_0_does_not_exist, readCalculation.HydraulicBoundaryLocation), pipingCalculation.Name); return false; } pipingCalculation.InputParameters.HydraulicBoundaryLocation = location; } else if (readCalculation.AssessmentLevel.HasValue) { pipingCalculation.InputParameters.UseAssessmentLevelManualInput = true; pipingCalculation.InputParameters.AssessmentLevel = (RoundedDouble) readCalculation.AssessmentLevel; } 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 ReadSurfaceLine(ReadPipingCalculation readCalculation, PipingCalculationScenario pipingCalculation) { if (readCalculation.SurfaceLine != null) { RingtoetsPipingSurfaceLine surfaceLine = failureMechanism.SurfaceLines .FirstOrDefault(sl => sl.Name == readCalculation.SurfaceLine); if (surfaceLine == null) { LogReadCalculationConversionError(string.Format( Resources.PipingCalculationConfigurationImporter_ReadSurfaceLine_SurfaceLine_0_does_not_exist, readCalculation.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 ReadEntryExitPoint(ReadPipingCalculation readCalculation, PipingCalculationScenario pipingCalculation) { bool hasEntryPoint = readCalculation.EntryPointL.HasValue; bool hasExitPoint = readCalculation.ExitPointL.HasValue; if (readCalculation.SurfaceLine == null && (hasEntryPoint || hasExitPoint)) { LogReadCalculationConversionError(Resources.PipingCalculationConfigurationImporter_ReadSurfaceLine_EntryPointL_or_ExitPointL_defined_without_SurfaceLine, pipingCalculation.Name); return false; } if (hasEntryPoint) { var entryPoint = (double) readCalculation.EntryPointL; try { pipingCalculation.InputParameters.EntryPointL = (RoundedDouble) entryPoint; } catch (ArgumentOutOfRangeException e) { LogOutOfRangeException(string.Format(Resources.PipingCalculationConfigurationImporter_ReadEntryExitPoint_Entry_point_invalid, entryPoint), pipingCalculation.Name, e); return false; } } if (hasExitPoint) { var exitPoint = (double) readCalculation.ExitPointL; try { pipingCalculation.InputParameters.ExitPointL = (RoundedDouble)exitPoint; } catch (ArgumentOutOfRangeException e) { 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 ReadStochasticSoilModel(ReadPipingCalculation readCalculation, PipingCalculationScenario pipingCalculation) { if (readCalculation.StochasticSoilModel != null) { StochasticSoilModel soilModel = failureMechanism.StochasticSoilModels .FirstOrDefault(ssm => ssm.Name == readCalculation.StochasticSoilModel); if (soilModel == null) { LogReadCalculationConversionError(string.Format( Resources.PipingCalculationConfigurationImporter_ReadStochasticSoilModel_Stochastische_soil_model_0_does_not_exist, readCalculation.StochasticSoilModel), pipingCalculation.Name); return false; } if (pipingCalculation.InputParameters.SurfaceLine != null && !soilModel.IntersectsWithSurfaceLineGeometry(pipingCalculation.InputParameters.SurfaceLine)) { LogReadCalculationConversionError(string.Format( Resources.PipingCalculationConfigurationImporter_ReadStochasticSoilModel_Stochastische_soil_model_0_does_not_intersect_with_surfaceLine_1, readCalculation.StochasticSoilModel, readCalculation.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 ReadStochasticSoilProfile(ReadPipingCalculation readCalculation, PipingCalculationScenario pipingCalculation) { if (readCalculation.StochasticSoilProfile != null) { if (pipingCalculation.InputParameters.StochasticSoilModel == null) { LogReadCalculationConversionError(string.Format( Resources.PipingCalculationConfigurationImporter_ReadStochasticSoilProfile_No_soil_model_provided_for_soil_profile_with_name_0, readCalculation.StochasticSoilProfile), pipingCalculation.Name); return false; } StochasticSoilProfile soilProfile = pipingCalculation.InputParameters.StochasticSoilModel.StochasticSoilProfiles .FirstOrDefault(ssp => ssp.SoilProfile.Name == readCalculation.StochasticSoilProfile); if (soilProfile == null) { LogReadCalculationConversionError(string.Format( Resources.PipingCalculationConfigurationImporter_ReadStochasticSoilProfile_Stochastic_soil_profile_0_does_not_exist_within_soil_model_1, readCalculation.StochasticSoilProfile, readCalculation.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 ReadStochasts(ReadPipingCalculation readCalculation, PipingCalculationScenario pipingCalculation) { if (readCalculation.PhreaticLevelExitMean.HasValue && readCalculation.PhreaticLevelExitStandardDeviation.HasValue) { var normalDistribution = new NormalDistribution(); var mean = (double) readCalculation.PhreaticLevelExitMean; try { normalDistribution.Mean = (RoundedDouble) mean; } catch (ArgumentOutOfRangeException e) { LogOutOfRangeException(string.Format( Resources.PipingCalculationConfigurationImporter_ReadStochasts_Invalid_mean_0_for_stochast_with_name_1, mean, PipingCalculationConfigurationSchemaIdentifiers.PhreaticLevelExitStochastName), pipingCalculation.Name, e); return false; } var standardDeviation = (double)readCalculation.PhreaticLevelExitStandardDeviation; try { normalDistribution.StandardDeviation = (RoundedDouble) standardDeviation; } catch (ArgumentOutOfRangeException e) { LogOutOfRangeException(string.Format( Resources.PipingCalculationConfigurationImporter_ReadStochasts_Invalid_standard_deviation_0_for_stochast_with_name_1, standardDeviation, PipingCalculationConfigurationSchemaIdentifiers.PhreaticLevelExitStochastName), pipingCalculation.Name, e); return false; } pipingCalculation.InputParameters.PhreaticLevelExit = normalDistribution; } if (readCalculation.DampingFactorExitMean.HasValue && readCalculation.DampingFactorExitStandardDeviation.HasValue) { var logNormalDistribution = new LogNormalDistribution(); var mean = (double)readCalculation.DampingFactorExitMean; try { logNormalDistribution.Mean = (RoundedDouble)mean; } catch (ArgumentOutOfRangeException e) { LogOutOfRangeException(string.Format( Resources.PipingCalculationConfigurationImporter_ReadStochasts_Invalid_mean_0_for_stochast_with_name_1, mean, PipingCalculationConfigurationSchemaIdentifiers.DampingFactorExitStochastName), pipingCalculation.Name, e); return false; } var standardDeviation = (double)readCalculation.DampingFactorExitStandardDeviation; try { logNormalDistribution.StandardDeviation = (RoundedDouble)standardDeviation; } catch (ArgumentOutOfRangeException e) { LogOutOfRangeException(string.Format( Resources.PipingCalculationConfigurationImporter_ReadStochasts_Invalid_standard_deviation_0_for_stochast_with_name_1, standardDeviation, PipingCalculationConfigurationSchemaIdentifiers.DampingFactorExitStochastName), pipingCalculation.Name, e); return false; } pipingCalculation.InputParameters.DampingFactorExit = logNormalDistribution; } return true; } } }