diff --git a/src/reactmicp/io/configuration.cpp b/src/reactmicp/io/configuration.cpp index 4d8dbe7..13dae68 100644 --- a/src/reactmicp/io/configuration.cpp +++ b/src/reactmicp/io/configuration.cpp @@ -1,285 +1,332 @@ /* ============================================================================= Copyright (c) 2014 - 2016 F. Georget Princeton University All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ============================================================================= */ #include "configuration.hpp" #include "../../utils/io/yaml.hpp" #include "../../dfpmsolver/parabolic_structs.hpp" #include "../../reactmicp/solver/runner.hpp" #include "../../reactmicp/solver/reactive_transport_solver_structs.hpp" #include "saturated_react.hpp" #include "../../database/database.hpp" #include #include #define S_DFPM "transport_options" #define S_DFPM_A_MAX_ITER "maximum_iteration" #define S_DFPM_A_RES_TOL "residual_tolerance" #define S_DFPM_A_ABS_TOL "absolute_tolerance" #define S_DFPM_A_STEP_TOL "step_tolerance" #define S_DFPM_A_TRSHOLD_STATIONARY "threshold_stationary" #define S_DFPM_A_MAX_STEP_LENGTH "maximum_step_length" #define S_DFPM_A_MAX_STEP_MAX_ITER "maximum_step_maximum_iteration" #define S_DFPM_A_SPARSE_SOLVER "sparse_solver" #define S_DFPM_A_LINESEARCH "linesearch" #define S_DFPM_A_QUASI_NEWTON "quasi_newton" #define S_REACTMICP "reactmicp_options" #define S_REACTMICP_A_RES_TOL "residual_tolerance" #define S_REACTMICP_A_ABS_TOL "absolute_tolerance" #define S_REACTMICP_A_STEP_TOL "step_tolerance" #define S_REACTMICP_A_GOOD_ENOUGH_TOL "good_enough_tolerance" #define S_REACTMICP_A_MAX_ITER "maximum_iteration" #define S_REACTMICP_A_IMPL_UPSCALING "implicit_upscaling" #define S_OUTPUT "reactmicp_output" #define S_OUTPUT_A_POROSITY "porosity" #define S_OUTPUT_A_PH "ph" #define S_OUTPUT_A_DIFFUSIVITY "diffusivity" #define S_OUTPUT_A_VOLFRAC_MINERAL "volume_fraction_solid" #define S_OUTPUT_A_TOT_AQ_CONC "total_aqueous_concentration" #define S_OUTPUT_A_TOT_S_CONC "total_solid_concentration" #define S_OUTPUT_A_TOT_CONC "total_concentration" #define S_OUTPUT_A_SATINDEX "saturation_index" #define S_SIMULINFO "simulation" #define S_SIMULINFO_A_NAME "name" #define S_SIMULINFO_A_OUTPUTPREFIX "output_prefix" #define S_SIMULINFO_A_PRINTITER "print_iter_info" #define S_SIMULINFO_A_OUTPUTSTEP "output_step" namespace specmicp { namespace io { std::string clean_label(std::string label); void configure_transport_options( dfpmsolver::ParabolicDriverOptions& options, const YAML::Node& configuration ) { check_mandatory_yaml_node(configuration, S_DFPM, "__main__"); const YAML::Node& conf = configuration[S_DFPM]; options.absolute_tolerance = get_yaml_optional(conf, S_DFPM_A_ABS_TOL, S_DFPM, DFPM_DEFAULT_ABS_TOL); options.residuals_tolerance = get_yaml_optional(conf, S_DFPM_A_RES_TOL, S_DFPM, DFPM_DEFAULT_RES_TOL); options.step_tolerance = get_yaml_optional(conf, S_DFPM_A_STEP_TOL, S_DFPM, DFPM_DEFAULT_STEP_TOL); options.maximum_iterations = get_yaml_optional(conf, S_DFPM_A_MAX_ITER, S_DFPM, DFPM_DEFAULT_MAX_ITER); options.maximum_step_length = get_yaml_optional(conf, S_DFPM_A_MAX_STEP_LENGTH, S_DFPM, DFPM_DEFAULT_MAX_STEP_LENGTH); options.max_iterations_at_max_length = get_yaml_optional(conf, S_DFPM_A_MAX_STEP_MAX_ITER, S_DFPM, DFPM_DEFAULT_MAX_ITER_MAX_LENGTH); options.threshold_stationary_point = get_yaml_optional(conf, S_DFPM_A_TRSHOLD_STATIONARY, S_DFPM, DFPM_DEFAULT_TRSHOLD_STATIONARY); options.quasi_newton = get_yaml_optional(conf, S_DFPM_A_QUASI_NEWTON, S_DFPM, DFPM_PARABOLIC_DEFAULT_QUASI_NEWTON); if (conf[S_DFPM_A_SPARSE_SOLVER]) { std::string sparse_solver = conf[S_DFPM_A_SPARSE_SOLVER].as(); if (sparse_solver == "LU") options.sparse_solver = sparse_solvers::SparseSolver::SparseLU; else if (sparse_solver == "QR") options.sparse_solver = sparse_solvers::SparseSolver::SparseQR; else if (sparse_solver == "GMRES") options.sparse_solver = sparse_solvers::SparseSolver::GMRES; else if (sparse_solver == "BiCGSTAB") options.sparse_solver = sparse_solvers::SparseSolver::BiCGSTAB; else throw std::runtime_error("Invalid argument for the sparse solver : '"+sparse_solver+"'.\n" +"Available solvers : 'LU' / 'QR' / 'GMRES' / 'BiCGSTAB'."); } if (conf[S_DFPM_A_LINESEARCH]) { std::string linesearch = conf[S_DFPM_A_LINESEARCH].as(); if (linesearch == "backtracking") options.linesearch = dfpmsolver::ParabolicLinesearch::Bactracking; else if (linesearch == "strang") options.linesearch = dfpmsolver::ParabolicLinesearch::Strang; else throw std::runtime_error("Invalid argument for the linesearch : '"+linesearch+"'.\n" +"Available options : 'bactracking' / 'strang'."); } } +#define get_attribute(attribute, var, type) \ + if (config[attribute]) { \ + options. var = get_yaml_attribute(config, attribute, S_DFPM); \ + } + +void configure_transport_options_conservative( + dfpmsolver::ParabolicDriverOptions& options, + const YAML::Node& config + ) +{ + get_attribute(S_DFPM_A_ABS_TOL, absolute_tolerance, double) + get_attribute(S_DFPM_A_RES_TOL, step_tolerance, double) + get_attribute(S_DFPM_A_MAX_ITER, maximum_iterations, int) + get_attribute(S_DFPM_A_MAX_STEP_LENGTH, maximum_step_length, double) + get_attribute(S_DFPM_A_MAX_STEP_MAX_ITER, max_iterations_at_max_length, int) + get_attribute(S_DFPM_A_TRSHOLD_STATIONARY, threshold_stationary_point, double) + get_attribute(S_DFPM_A_QUASI_NEWTON, quasi_newton, int) + + if (config[S_DFPM_A_SPARSE_SOLVER]) + { + std::string sparse_solver = config[S_DFPM_A_SPARSE_SOLVER].as(); + if (sparse_solver == "LU") + options.sparse_solver = sparse_solvers::SparseSolver::SparseLU; + else if (sparse_solver == "QR") + options.sparse_solver = sparse_solvers::SparseSolver::SparseQR; + else if (sparse_solver == "GMRES") + options.sparse_solver = sparse_solvers::SparseSolver::GMRES; + else if (sparse_solver == "BiCGSTAB") + options.sparse_solver = sparse_solvers::SparseSolver::BiCGSTAB; + else + throw std::runtime_error("Invalid argument for the sparse solver : '"+sparse_solver+"'.\n" + +"Available solvers : 'LU' / 'QR' / 'GMRES' / 'BiCGSTAB'."); + } + if (config[S_DFPM_A_LINESEARCH]) + { + std::string linesearch = config[S_DFPM_A_LINESEARCH].as(); + if (linesearch == "backtracking") + options.linesearch = dfpmsolver::ParabolicLinesearch::Bactracking; + else if (linesearch == "strang") + options.linesearch = dfpmsolver::ParabolicLinesearch::Strang; + else + throw std::runtime_error("Invalid argument for the linesearch : '"+linesearch+"'.\n" + +"Available options : 'bactracking' / 'strang'."); + } +} + + void configure_reactmicp_options( reactmicp::solver::ReactiveTransportOptions& options, const YAML::Node& configuration ) { check_mandatory_yaml_node(configuration, S_REACTMICP, "__main__"); const YAML::Node& conf = configuration[S_REACTMICP]; options.residuals_tolerance = get_yaml_optional(conf, S_REACTMICP_A_RES_TOL, S_REACTMICP, REACTMICP_DEFAULT_RES_TOL); options.absolute_residuals_tolerance = get_yaml_optional(conf, S_REACTMICP_A_ABS_TOL, S_REACTMICP, REACTMICP_DEFAULT_ABS_TOL); options.step_tolerance = get_yaml_optional(conf, S_REACTMICP_A_STEP_TOL, S_REACTMICP, REACTMICP_DEFAULT_STEP_TOL); options.good_enough_tolerance = get_yaml_optional(conf, S_REACTMICP_A_GOOD_ENOUGH_TOL, S_REACTMICP, REACTMICP_DEFAULT_GOOD_ENOUTH_TOL); options.implicit_upscaling = get_yaml_optional(conf, S_REACTMICP_A_IMPL_UPSCALING, S_REACTMICP, REACTMICP_DEFAULT_IMPL_UPSCALING); options.maximum_iterations = get_yaml_optional(conf, S_REACTMICP_A_MAX_ITER, S_REACTMICP, REACTMICP_DEFAULT_MAX_ITER); } void SPECMICP_DLL_PUBLIC configure_reactmicp_output( io::OutputNodalVariables& output_policy, const reactmicp::solver::SimulationInformation& simul_info, const YAML::Node& configuration ) { if (not configuration[S_OUTPUT]) return; const YAML::Node& conf = configuration[S_OUTPUT]; database::Database db_manager(output_policy.get_database()); if (conf[S_OUTPUT_A_PH] and conf[S_OUTPUT_A_PH].as()) { output_policy.register_pH(simul_info.complete_filepath("ph", "dat")); } if (conf[S_OUTPUT_A_POROSITY] and conf[S_OUTPUT_A_POROSITY].as()) { output_policy.register_porosity(simul_info.complete_filepath("porosity", "dat")); } if (conf[S_OUTPUT_A_DIFFUSIVITY] and conf[S_OUTPUT_A_DIFFUSIVITY].as()) { output_policy.register_diffusion_coefficient(simul_info.complete_filepath("diffusivity", "dat")); } if (conf[S_OUTPUT_A_VOLFRAC_MINERAL]) { for (auto it: conf[S_OUTPUT_A_VOLFRAC_MINERAL]) { auto label = it.as(); index_t id = db_manager.safe_mineral_label_to_id(label); auto simple_label = clean_label(label); output_policy.register_volume_fraction_mineral( id, simul_info.complete_filepath("phi_"+simple_label, "dat")); } } if (conf[S_OUTPUT_A_TOT_CONC]) { for (auto it: conf[S_OUTPUT_A_TOT_CONC]) { auto label = it.as(); index_t id = db_manager.safe_component_label_to_id(label); auto simple_label = clean_label(label); output_policy.register_total_concentration( id, simul_info.complete_filepath("t_"+simple_label, "dat")); } } if (conf[S_OUTPUT_A_TOT_AQ_CONC]) { for (auto it: conf[S_OUTPUT_A_TOT_AQ_CONC]) { auto label = it.as(); index_t id = db_manager.safe_component_label_to_id(label); auto simple_label = clean_label(label); output_policy.register_total_aqueous_concentration( id, simul_info.complete_filepath("c_"+simple_label, "dat")); } } if (conf[S_OUTPUT_A_TOT_S_CONC]) { for (auto it: conf[S_OUTPUT_A_TOT_S_CONC]) { auto label = it.as(); index_t id = db_manager.safe_component_label_to_id(label); auto simple_label = clean_label(label); output_policy.register_total_solid_concentration( id, simul_info.complete_filepath("s_"+simple_label, "dat")); } } if (conf[S_OUTPUT_A_SATINDEX]) { for (auto it: conf[S_OUTPUT_A_SATINDEX]) { auto label = it.as(); index_t id = db_manager.safe_mineral_kinetic_label_to_id(label); auto simple_label = clean_label(label); output_policy.register_saturation_index_mineral_kinetic( id, simul_info.complete_filepath("si_"+simple_label, "dat")); } } } reactmicp::solver::SimulationInformation configure_simulation_information( const YAML::Node& configuration ) { check_mandatory_yaml_node(configuration, S_SIMULINFO, "__main__"); const YAML::Node& conf = configuration[S_SIMULINFO]; reactmicp::solver::SimulationInformation simul_info( get_yaml_mandatory(conf, S_SIMULINFO_A_NAME, S_SIMULINFO), get_yaml_mandatory(conf, S_SIMULINFO_A_OUTPUTSTEP, S_SIMULINFO) ); if (conf[S_SIMULINFO_A_OUTPUTPREFIX]) simul_info.output_prefix = conf[S_SIMULINFO_A_OUTPUTPREFIX].as(); if (conf[S_SIMULINFO_A_PRINTITER]) simul_info.print_iter_info = conf[S_SIMULINFO_A_PRINTITER].as(); return simul_info; } std::string clean_label(std::string label) { static std::vector orig {']', '[', '(', ')', '+', '-', ','}; static std::vector repl {'_', '_', '_', '_', 'p', 'm', '_'}; for (auto it = label.begin(); it!=label.end(); ++it) { auto itf = std::find(orig.begin(), orig.end(), *it); if (itf != orig.end()) { *it = repl[itf - orig.begin()]; } } std::transform(label.begin(), label.end(), label.begin(), ::tolower); return label; } } //end namespace io } //end namespace specmicp diff --git a/src/reactmicp/io/configuration.hpp b/src/reactmicp/io/configuration.hpp index bb8d367..74ef305 100644 --- a/src/reactmicp/io/configuration.hpp +++ b/src/reactmicp/io/configuration.hpp @@ -1,89 +1,94 @@ /* ============================================================================= Copyright (c) 2014 - 2016 F. Georget Princeton University All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ============================================================================= */ #ifndef SPECMICP_REACTMICP_IO_CONFIGURATION_HPP #define SPECMICP_REACTMICP_IO_CONFIGURATION_HPP #include "../../types.hpp" namespace YAML { class Node; } //end namespace YAML namespace specmicp { struct AdimensionalSystemSolverOptions; namespace dfpmsolver { struct ParabolicDriverOptions; } //end namespace dfpmsolver namespace reactmicp { namespace solver { struct ReactiveTransportOptions; struct SimulationInformation; } //end namespace solver } //end namespace reactmicp namespace io { class OutputNodalVariables; void SPECMICP_DLL_PUBLIC configure_transport_options( dfpmsolver::ParabolicDriverOptions& options, const YAML::Node& configuration ); +void SPECMICP_DLL_PUBLIC configure_transport_options_conservative( + dfpmsolver::ParabolicDriverOptions& options, + const YAML::Node& configuration + ); + void SPECMICP_DLL_PUBLIC configure_reactmicp_options( reactmicp::solver::ReactiveTransportOptions& options, const YAML::Node& configuration ); void SPECMICP_DLL_PUBLIC configure_reactmicp_output( io::OutputNodalVariables& output_policy, const reactmicp::solver::SimulationInformation& simul_info, const YAML::Node& configuration ); reactmicp::solver::SimulationInformation SPECMICP_DLL_PUBLIC configure_simulation_information(const YAML::Node& configuration); } //end namespace io } //end namespace specmicp #endif // SPECMICP_REACTMICP_IO_CONFIGURATION_HPP