Page MenuHomec4science

chloride.cpp
No OneTemporary

File Metadata

Created
Mon, Nov 11, 15:48

chloride.cpp

/* =============================================================================
Copyright (c) 2014-2017 F. Georget <fabieng@princeton.edu> Princeton University
Copyright (c) 2017-2019 F. Georget <fabien.georget@epfl.ch> EPFL
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. *
============================================================================= */
//! \file bin/reactmicp/chloride.cpp
//! \brief Main file for the chloride system executable
#include "app_core.hpp"
#include "specmicp_common/types.hpp"
#include "specmicp_common/plugins/plugin_manager.hpp"
#include "specmicp_common/io/yaml.hpp"
#include "specmicp_common/io/safe_config.hpp"
#include "specmicp_common/compat.hpp"
#include "specmicp_common/log.hpp"
#include "specmicp_common/filesystem.hpp"
#include "specmicp_common/string_algorithms.hpp"
#include "specmicp_common/openmp_support.hpp"
#include "specmicp_common/io/all_io_files.hpp"
#include "specmicp_common/physics/io/configuration.hpp"
#include "specmicp_database/io/configuration.hpp"
#include "dfpm/io/configuration.hpp"
#include "specmicp_common/io/configuration.hpp"
#include "reactmicp/io/configuration_common.hpp"
#include "specmicp/io/print.hpp"
#include "dfpm/io/print.hpp"
#include "specmicp_database/io/print.hpp"
#include "reactmicp/reactmicp_chloride.hpp"
#include "dfpm/solver/parabolic_structs.hpp"
#include "specmicp_common/config.h"
#include <string>
#include <memory>
#include <stdexcept>
#include <iostream>
#include "specmicp_common/io/config_yaml_sections.h"
#include "specmicp_common/plugins/plugin_modules_names.h"
using namespace specmicp;
using namespace specmicp::reactmicp;
using namespace specmicp::reactmicp::systems::chloride;
//! \brief Headers printed in console
const char* welcome =
R"(===============================
= Reactmicp - Chloride =
===============================
(C) copyright 2014-2017 F.Georget, Princeton University
(C) copyright 2017-2019 F.Georget, EPFL
version: not good enough yet
)";
//! \brief Main loop
int main (int argc, char* argv[]);
//! \brief Specialization of SystemApp for the Unsaturated system
class SystemAppChloride:
public SystemApp
{
public:
SystemAppChloride():
SystemApp()
{}
void print_welcome() override {
std::cout << welcome << std::endl;
}
std::string driver_identification() {
return "ReactMiCP Chloride System";
}
void init_plugin_manager() override;
void init_simul_data(const std::vector<std::string> &db_dirs) override;
void init_staggers() override;
void check_init_data() override;
void init_output(const std::string& working_dir);
solver::VariablesBasePtr get_variables() override {
return m_simul_data.variables;
}
private:
void init_variables();
void init_transport_stagger(io::YAMLConfigHandle& config_staggers);
void init_chem_stagger(io::YAMLConfigHandle& config_staggers);
void init_upscaling_stagger(io::YAMLConfigHandle& config_staggers);
SimulationData m_simul_data;
std::unique_ptr<io::ChlorideHDF5Saver> m_saver;
};
// Main
int main(int argc, char *argv[])
{
MainApp app;
SystemAppChloride system_app;
int retcode = app.run(system_app, argc, argv);
return retcode;
}
//
void SystemAppChloride::init_plugin_manager()
{
init_plugin_manager_base({
SPC_PLUGIN_REACTMICP_CHLORIDE_VARIABLES,
SPC_PLUGIN_REACTMICP_CHLORIDE_UPSCALING
});
}
void SystemAppChloride::init_simul_data(const std::vector<std::string> &db_dirs)
{
m_simul_data.units = get_units_from_config();
m_simul_data.raw_data = get_database_from_config(db_dirs);
m_simul_data.mesh1d = get_mesh_from_config();
init_variables();
m_simul_data.raw_data->freeze_db();
io::print_db_to_conf_log(m_simul_data.raw_data.get());
m_simul_data.bcs = io::configure_chloride_boundary_conditions(
m_simul_data.mesh1d,
m_simul_data.raw_data,
get_config().get_section(SPC_CF_S_BOUNDARYCONDITIONS)
);
for (auto dof: m_simul_data.bcs->get_fixed_dofs()) {
std::cout << " - " << dof << std::endl;
}
m_all_io_files->sync();
}
void SystemAppChloride::init_staggers()
{
SPC_CONF_LOG << "\n\n" << SPC_CONF_LOG_SECTION
<< "\n= Staggers initialization =\n"
<< SPC_CONF_LOG_SECTION;
auto config_staggers = get_config().get_section(SPC_CF_S_STAGGERS);
auto* vars = m_simul_data.variables.get();
init_upscaling_stagger(config_staggers);
m_staggers.upscaling->initialize(vars);
init_chem_stagger(config_staggers);
m_staggers.chemistry->initialize(vars);
init_transport_stagger(config_staggers);
auto tc_stagger = std::static_pointer_cast<ChlorideTransportStagger>(m_staggers.transport);
tc_stagger->register_chemistry_stagger(
std::static_pointer_cast<ChlorideChemistryStaggerBase>(m_staggers.chemistry));
m_staggers.transport->initialize(vars);
m_staggers.upscaling->register_transport_stagger(m_staggers.transport);
m_staggers.upscaling->register_chemistry_stagger(m_staggers.chemistry);
}
void SystemAppChloride::check_init_data()
{
// todo
}
void SystemAppChloride::init_output(const std::string& working_dir)
{
if (m_config->has_section(SPC_CF_S_REACTOUTPUT)) {
auto conf = m_config->get_section(SPC_CF_S_REACTOUTPUT);
// main output file
auto type = conf.get_optional_attribute<std::string>(
SPC_CF_S_REACTOUTPUT_A_TYPE, SPC_CF_S_REACTOUTPUT_A_TYPE_V_HDF5);
if (type == SPC_CF_V_DEFAULT) type = SPC_CF_S_REACTOUTPUT_A_TYPE_V_HDF5;
if (type != SPC_CF_S_REACTOUTPUT_A_TYPE_V_HDF5) {
conf.report_error(
io::YAMLConfigError::InvalidArgument,
"Only hdf5 is accepted for attribute "
SPC_CF_S_REACTOUTPUT_A_TYPE " for now.");
}
auto filepath = conf.get_required_attribute<std::string>(
SPC_CF_S_REACTOUTPUT_A_FILEPATH);
if (not working_dir.empty() and not utils::is_path_absolute(filepath)) {
filepath = utils::complete_path(working_dir, filepath);
}
m_all_io_files->add_solution(io::output_file(IO_FILE_OUTPUT, filepath));
m_saver = make_unique<io::ChlorideHDF5Saver>(
filepath, m_simul_data.variables);
auto* saver_ptr = m_saver.get();
auto out_pol = [saver_ptr](
scalar_t timestep,
solver::VariablesBasePtr _) {
saver_ptr->save_timestep(timestep);
};
m_saver->save_timestep(0.0);
m_runner->set_output_policy(out_pol);
// database output
database::Database db_manager(m_simul_data.raw_data);
auto db_path = conf.get_attribute<std::string>(SPC_CF_S_REACTOUTPUT_A_DATABASE);
if (not working_dir.empty() and not utils::is_path_absolute(db_path)) {
db_path = utils::complete_path(working_dir, db_path);
}
db_manager.save(db_path);
m_all_io_files->add_database(io::output_file(IO_FILE_WORK_DB, db_path));
}
const auto iter_path = m_runner->get_iter_file_path();
if (not iter_path.empty()) {
m_all_io_files->add_log_file(
io::output_file(IO_ITER_FILE, iter_path));
}
m_all_io_files->sync();
}
void SystemAppChloride::init_variables()
{
SPC_CONF_LOG << "Variables initialization\n"
<< SPC_CONF_LOG_SECTION;
auto config = get_config().get_section(SPC_CF_S_REACTMICPVARIABLES);
auto type = config.get_optional_attribute<std::string>(
SPC_CF_S_REACTMICPVARIABLES_A_TYPE, SPC_CF_V_PLUGIN);
if (type == SPC_CF_V_DEFAULT) type = SPC_CF_V_PLUGIN;
if (type != SPC_CF_V_PLUGIN) {
config.report_error(
io::YAMLConfigError::InvalidArgument,
"Only accepted value for attribute '"
SPC_CF_S_REACTMICPVARIABLES_A_TYPE
"' is '" SPC_CF_V_PLUGIN "'."
);
}
auto plugin_name = get_plugin_name(config);
auto& plugin_manager = plugins::get_plugin_manager();
std::unique_ptr<InitializeVariables> initializer =
plugin_manager.get_object<InitializeVariables>(
SPC_PLUGIN_REACTMICP_CHLORIDE_VARIABLES,
plugin_name
);
if (initializer == nullptr) {
CRITICAL << "No variable initializer found";
throw std::runtime_error("No variable initializer found");
}
m_simul_data.variables = initializer->initialize_variables(
m_simul_data.raw_data,
m_simul_data.mesh1d,
m_simul_data.units,
config
);
}
void SystemAppChloride::init_transport_stagger(io::YAMLConfigHandle& config_staggers)
{
SPC_CONF_LOG << "\n\n --> Transport stagger <-- \n"
<< SPC_CONF_LOG_SECTION;
// No conf
// -------
if (not config_staggers.has_section(SPC_CF_S_STAGGERS_SS_TRANSPORTSTAGGER)) {
m_staggers.transport = ChlorideTransportStagger::make(
m_simul_data.variables,
m_simul_data.bcs
);
return;
}
// Conf is given
auto transport_conf = config_staggers.get_section(
SPC_CF_S_STAGGERS_SS_TRANSPORTSTAGGER);
std::shared_ptr<ChlorideTransportStagger> stagger = ChlorideTransportStagger::make(
m_simul_data.variables,
m_simul_data.bcs
);
dfpmsolver::ParabolicDriverOptions default_opts = stagger->get_options();
if (transport_conf.has_section(SPC_CF_S_STAGGERS_SS_TRANSPORTSTAGGER_SS_DEFAULT_OPTS))
{
io::configure_transport_options(
default_opts,
transport_conf.get_section(SPC_CF_S_STAGGERS_SS_TRANSPORTSTAGGER_SS_DEFAULT_OPTS)
);
}
SPC_CONF_LOG << "\nDefault transport options\n"
<< SPC_CONF_LOG_HLINE;
std::ostringstream msg;
io::print_transport_options(default_opts, msg);
SPC_CONF_LOG << msg.str() << SPC_CONF_LOG_HLINE;
stagger->get_options() = default_opts;
m_staggers.transport = stagger;
}
void SystemAppChloride::init_chem_stagger(io::YAMLConfigHandle& config_staggers)
{
SPC_CONF_LOG << "\n\n --> Chemistry stagger <-- \n"
<< SPC_CONF_LOG_SECTION;
// no conf
// -------
if (not config_staggers.has_section(SPC_CF_S_STAGGERS_SS_CHEMISTRYSTAGGER)) {
auto opts = systems::EquilibriumOptionsVector::make(
m_simul_data.mesh1d->nb_nodes());
opts->get("default").units_set = m_simul_data.units;
m_staggers.chemistry = ChlorideEquilibriumStagger::make(
m_simul_data.variables,
m_simul_data.bcs,
opts
);
return;
}
// conf is given
// --------------
auto conf_chem = config_staggers.get_section(SPC_CF_S_STAGGERS_SS_CHEMISTRYSTAGGER);
auto type = conf_chem.get_optional_attribute<std::string>(
SPC_CF_S_STAGGERS_SS_CHEMISTRYSTAGGER_A_TYPE,
SPC_CF_S_STAGGERS_SS_CHEMISTRYSTAGGER_A_TYPE_V_EQUILIBRIUM);
if (type == SPC_CF_V_DEFAULT)
type = SPC_CF_S_STAGGERS_SS_CHEMISTRYSTAGGER_A_TYPE_V_EQUILIBRIUM;
if (type !=SPC_CF_S_STAGGERS_SS_CHEMISTRYSTAGGER_A_TYPE_V_EQUILIBRIUM) {
CRITICAL << "Unknown chemistry stagger";
throw std::invalid_argument("Only equilibrium stagger supported.");
}
SPC_CONF_LOG << "Use equilibrium stagger";
std::shared_ptr<systems::EquilibriumOptionsVector> opts {nullptr};
if (conf_chem.has_section(SPC_CF_S_STAGGERS_SS_CHEMISTRYSTAGGER_SS_EQUILIBRIUM_OPTS)) {
opts = io::configure_equilibrium_options(
m_simul_data.mesh1d->nb_nodes(),
m_simul_data.units,
conf_chem.get_section(SPC_CF_S_STAGGERS_SS_CHEMISTRYSTAGGER_SS_EQUILIBRIUM_OPTS)
);
} else {
SPC_CONF_LOG << " Use default options ";
opts = systems::EquilibriumOptionsVector::make(m_simul_data.mesh1d->nb_nodes());
SPC_CONF_LOG << "Default SpecMiCP options : ";
std::ostringstream msg;
io::print_specmicp_options(msg, opts->get("default"));
SPC_CONF_LOG << msg.str() << "\n" << SPC_CONF_LOG_HLINE;
}
SPC_CONF_LOG << SPC_CONF_LOG_HLINE;
m_staggers.chemistry = ChlorideEquilibriumStagger::make(
m_simul_data.variables,
m_simul_data.bcs,
opts
);
}
void SystemAppChloride::init_upscaling_stagger(io::YAMLConfigHandle& config_staggers)
{
SPC_CONF_LOG << "\n\n --> Upscaling stagger <-- \n"
<< SPC_CONF_LOG_SECTION;
auto conf = config_staggers.get_section(SPC_CF_S_STAGGERS_SS_UPSCALINGSTAGGER);
auto type = conf.get_optional_attribute<std::string>(
SPC_CF_S_STAGGERS_SS_UPSCALINGSTAGGER_A_TYPE,
SPC_CF_V_PLUGIN);
// only plugin accepted for now
if (type == SPC_CF_V_DEFAULT) type = SPC_CF_V_PLUGIN;
if (type != SPC_CF_V_PLUGIN) {
conf.report_error(
io::YAMLConfigError::InvalidArgument,
"Invalid argument for attribute '"
SPC_CF_A_TYPE
"' only value accepted is '" SPC_CF_V_PLUGIN "'.");
}
auto plugin_name = get_plugin_name(conf);
auto& plugin_manager = plugins::get_plugin_manager();
auto upscaling_factory = plugin_manager.get_object<UpscalingStaggerFactory>(
SPC_PLUGIN_REACTMICP_CHLORIDE_UPSCALING, plugin_name);
if (upscaling_factory == nullptr) {
CRITICAL << "No upscaling stagger factory found";
throw std::runtime_error("No upscaling stagger factory found");
}
SPC_CONF_LOG << SPC_CONF_LOG_HLINE;
m_staggers.upscaling = upscaling_factory->get_upscaling_stagger(
m_simul_data, std::move(conf));
}

Event Timeline