Page MenuHomec4science

configuration.cpp
No OneTemporary

File Metadata

Created
Fri, May 17, 23:29

configuration.cpp

/*-------------------------------------------------------------------------------
Copyright (c) 2015 F. Georget <fabieng@princeton.edu>, 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 <string>
#include <algorithm>
#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_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<double>(conf, S_DFPM_A_ABS_TOL, S_DFPM, DFPM_DEFAULT_ABS_TOL);
options.residuals_tolerance = get_yaml_optional<double>(conf, S_DFPM_A_RES_TOL, S_DFPM, DFPM_DEFAULT_RES_TOL);
options.step_tolerance = get_yaml_optional<double>(conf, S_DFPM_A_STEP_TOL, S_DFPM, DFPM_DEFAULT_STEP_TOL);
options.maximum_iterations = get_yaml_optional<double>(conf, S_DFPM_A_MAX_ITER, S_DFPM, DFPM_DEFAULT_MAX_ITER);
options.maximum_step_length = get_yaml_optional<double>(conf, S_DFPM_A_MAX_STEP_LENGTH, S_DFPM, DFPM_DEFAULT_MAX_STEP_LENGTH);
options.max_iterations_at_max_length = get_yaml_optional<double>(conf,
S_DFPM_A_MAX_STEP_MAX_ITER, S_DFPM, DFPM_DEFAULT_MAX_ITER_MAX_LENGTH);
options.threshold_stationary_point = get_yaml_optional<double>(conf,
S_DFPM_A_TRSHOLD_STATIONARY, S_DFPM, DFPM_DEFAULT_TRSHOLD_STATIONARY);
options.quasi_newton = get_yaml_optional<int>(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<std::string>();
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<std::string>();
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<double>(conf,
S_REACTMICP_A_RES_TOL, S_REACTMICP, REACTMICP_DEFAULT_RES_TOL);
options.absolute_residuals_tolerance = get_yaml_optional<double>(conf,
S_REACTMICP_A_ABS_TOL, S_REACTMICP, REACTMICP_DEFAULT_ABS_TOL);
options.step_tolerance = get_yaml_optional<double>(conf,
S_REACTMICP_A_STEP_TOL, S_REACTMICP, REACTMICP_DEFAULT_STEP_TOL);
options.good_enough_tolerance = get_yaml_optional<double>(conf,
S_REACTMICP_A_GOOD_ENOUGH_TOL, S_REACTMICP, REACTMICP_DEFAULT_GOOD_ENOUTH_TOL);
options.implicit_upscaling = get_yaml_optional<bool>(conf,
S_REACTMICP_A_IMPL_UPSCALING, S_REACTMICP, REACTMICP_DEFAULT_IMPL_UPSCALING);
options.maximum_iterations = get_yaml_optional<int>(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<bool>())
{
output_policy.register_pH(simul_info.complete_filepath("ph", "dat"));
}
if (conf[S_OUTPUT_A_POROSITY] and conf[S_OUTPUT_A_POROSITY].as<bool>())
{
output_policy.register_porosity(simul_info.complete_filepath("porosity", "dat"));
}
if (conf[S_OUTPUT_A_DIFFUSIVITY] and conf[S_OUTPUT_A_DIFFUSIVITY].as<bool>())
{
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<std::string>();
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<std::string>();
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<std::string>();
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<std::string>();
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"));
}
}
}
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<std::string>(conf, S_SIMULINFO_A_NAME, S_SIMULINFO),
get_yaml_mandatory<scalar_t>(conf, S_SIMULINFO_A_OUTPUTSTEP, S_SIMULINFO)
);
if (conf[S_SIMULINFO_A_OUTPUTPREFIX])
simul_info.output_prefix = conf[S_SIMULINFO_A_OUTPUTPREFIX].as<std::string>();
if (conf[S_SIMULINFO_A_PRINTITER])
simul_info.print_iter_info = conf[S_SIMULINFO_A_PRINTITER].as<bool>();
return simul_info;
}
std::string clean_label(std::string label)
{
// FIXME better loop
std::replace(label.begin(), label.end(), '[', '_');
std::replace(label.begin(), label.end(), ']', '_');
std::replace(label.begin(), label.end(), '(', '_');
std::replace(label.begin(), label.end(), ')', '_');
std::replace(label.begin(), label.end(), '+', 'p');
std::replace(label.begin(), label.end(), '-', 'm');
std::replace(label.begin(), label.end(), ',', '_');
std::transform(label.begin(), label.end(), label.begin(), ::tolower);
return label;
}
} //end namespace io
} //end namespace specmicp

Event Timeline