Page MenuHomec4science

single.cpp
No OneTemporary

File Metadata

Created
Thu, May 9, 13:52

single.cpp

/* =============================================================================
Copyright (c)
2014-2017 F. Georget <fabieng@princeton.edu> Princeton University
2017-2021 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. *
============================================================================= */
#include "reactmicp/reactmicp_single.hpp"
#include "reactmicp/solver/runner.hpp"
#include <string>
#include <iostream>
#include <fstream>
#include "specmicp_common/io/safe_config.hpp"
#include <sys/stat.h>
#include <cmath>
using namespace specmicp;
using namespace specmicp::io;
using namespace specmicp::reactmicp::systems::single;
struct SingleSimpleConf {
scalar_t rate_factor;
scalar_t diffusion_coeff;
scalar_t freund_coeff;
scalar_t coeff_1;
scalar_t coeff_2;
scalar_t freund_exp;
scalar_t porosity;
scalar_t saturation;
std::string binding_law;
};
std::shared_ptr<SingleSimpleConf>
configure_single_simple(
YAMLConfigHandle&& configuration
)
{
auto conf = std::make_shared<SingleSimpleConf>();
conf->rate_factor = configuration.get_required_attribute<scalar_t>("rate_factor");
conf->binding_law = configuration.get_required_attribute<std::string>("binding_law");
conf->freund_coeff = configuration.get_optional_attribute<scalar_t>("freund_coeff", 0);
conf->coeff_1 = configuration.get_optional_attribute<scalar_t>("coeff_1", 0);
conf->coeff_2 = configuration.get_optional_attribute<scalar_t>("coeff_2", 0);
conf->freund_exp = configuration.get_required_attribute<scalar_t>("freund_exp");
conf->diffusion_coeff = configuration.get_required_attribute<scalar_t>("diffusion_coeff");
conf->porosity = configuration.get_required_attribute<scalar_t>("porosity");
conf->saturation = configuration.get_required_attribute<scalar_t>("saturation");
return conf;
}
scalar_t binding_law(scalar_t conc, scalar_t coeff, scalar_t exp)
{
return coeff*std::pow(conc, exp);
}
scalar_t ph_dependent_binding_law(scalar_t conc, scalar_t coeff1, scalar_t coeff2, scalar_t exp)
{
return (2*(coeff2-coeff1)*conc+coeff2)*std::pow(conc, exp);
}
scalar_t dph_dependent_binding_law(scalar_t conc, scalar_t coeff1, scalar_t coeff2, scalar_t exp)
{
return (2*(coeff2-coeff1)*(1+exp)*std::pow(conc, exp)+exp*coeff2*std::pow(conc, exp-1));
}
scalar_t simple_newton(
std::function<scalar_t (scalar_t)> f,
std::function<scalar_t (scalar_t)> df,
scalar_t b, scalar_t tol, scalar_t x=0, bool print=false)
{
auto max_it = 200;
scalar_t error = f(x)-b;
auto uerror = tol+1;
auto it = 0;
if (print) {
std::cout << error << " - " << it << ";" << std::endl;
}
while (std::fabs(error) > tol and uerror > tol and it < max_it) {
scalar_t sk = error/df(x);
scalar_t x1 = 0.0;
if ((x-sk) < 0) {
x1 = 0.1*x;
} else {
x1 = x-sk;
}
//auto x1 = x-0.01*error/df(x);
//if (x1 < 0) {x1 = 0.01;}
//if (print) {
// std::cout << x1 << std::endl;
//}
uerror = std::fabs(x1-x);
error = f(x1)-b;
x = x1;
++it;
}
if (print) {
std::cout << error << " - " << it << ";" << std::endl;
}
return x;
}
class RateFunctor
{
public:
RateFunctor(scalar_t coeff, scalar_t exp, scalar_t rate):
m_coeff(coeff), m_exp(exp), m_rate(rate)
{}
scalar_t operator()(index_t node, scalar_t dt, SingleVariables * const var) {
auto c_eq = std::pow(var->bound_concentration(node)/m_coeff, 1/m_exp);
auto rate = m_rate * (var->fluid_concentration(node) - c_eq);
//if (node == 1) {
// std::cout << "rate : " << rate << " - " << c_eq << std::endl;
//}
if (rate < 0) {
rate = 0;
}
auto factor = var->porosity(node)*var->saturation(node);
auto new_c = var->fluid_concentration(node)-rate/factor*dt;
if (new_c < 0) {
rate = factor*var->fluid_concentration(node)/dt;
}
return rate;
}
private:
scalar_t m_coeff;
scalar_t m_exp;
scalar_t m_rate;
};
class LeachingRateFunctor
{
public:
LeachingRateFunctor(scalar_t coeff1, scalar_t coeff2, scalar_t exp, scalar_t rate):
m_coeff1(coeff1), m_coeff2(coeff2), m_exp(exp), m_rate(rate)
{
}
scalar_t operator()(index_t node, scalar_t dt, SingleVariables * const var) {
auto print = false;
//if (node == 1) {print = true;}
auto c_eq = invert(var->bound_concentration(node), print);
auto rate = m_rate * (var->fluid_concentration(node) - c_eq);
//if (node == 1) {
// std::cout << "rate : " << rate << " - " << c_eq << std::endl;
//}
if (rate < 0) {
rate = 0;
}
auto factor = var->porosity(node)*var->saturation(node);
auto new_c = var->fluid_concentration(node)-rate/factor*dt;
if (new_c < 0) {
rate = factor*var->fluid_concentration(node)/dt;
}
return rate;
}
scalar_t func(scalar_t conc) {
return ph_dependent_binding_law(conc, m_coeff1, m_coeff2, m_exp);
}
scalar_t dfunc(scalar_t conc) {
return dph_dependent_binding_law(conc, m_coeff1, m_coeff2, m_exp);
}
scalar_t invert(scalar_t bound_conc, bool print) {
auto val = simple_newton(std::bind(&LeachingRateFunctor::func, this, std::placeholders::_1),
std::bind(&LeachingRateFunctor::dfunc, this, std::placeholders::_1),
bound_conc, 1e-7, 0.1, print);
//if (print) {std::cout << val << std::endl;}
return val;
}
private:
scalar_t m_coeff1;
scalar_t m_coeff2;
scalar_t m_exp;
scalar_t m_rate;
std::function<scalar_t (scalar_t)> m_f;
std::function<scalar_t (scalar_t)> m_df;
//Vector concentrations;
//Vector bindings;
};
void output(scalar_t time, specmicp::reactmicp::solver::VariablesBasePtr variables)
{
auto vars = static_cast<specmicp::reactmicp::systems::single::SingleVariables*>(variables.get());
auto mesh = vars->get_mesh();
//Eigen::Matrix<scalar_t, Eigen::Dynamic, 2> toplot(mesh->nb_nodes());
std::ofstream ofile;
ofile.open("output/"+std::to_string(time)+".csv");
for (int i=0;i<mesh->nb_nodes();++i) {
ofile << mesh->get_position(i) << ", " << vars->fluid_concentration(i) << ", " << vars->bound_concentration(i) << "\n";
}
ofile.close();
}
class OutputPolicy
{
public:
OutputPolicy(std::string folder):
m_folder(folder)
{
// make folder
int status = mkdir(folder.c_str(),0777);
if ((status < 0) && (errno != EEXIST)) {
throw std::runtime_error("Could not create folder: " + folder);
}
}
void operator()(scalar_t time, specmicp::reactmicp::solver::VariablesBasePtr variables)
{
auto vars = static_cast<specmicp::reactmicp::systems::single::SingleVariables*>(variables.get());
auto mesh = vars->get_mesh();
std::ofstream ofile;
ofile.open(m_folder+"/"+std::to_string(time)+".csv");
for (int i=0;i<mesh->nb_nodes();++i) {
ofile << mesh->get_position(i) << ", " << vars->fluid_concentration(i) << ", " << vars->bound_concentration(i) << "\n";
}
ofile.close();
}
private:
std::string m_folder;
};
int main(int argc, char *argv[])
{
if (argc == 1) {
std::cerr << "Error: expected one argument" << std::endl;
return 1;
}
std::string config_filepath(argv[1]);
auto config = YAMLConfigFile::load(config_filepath);
// specmicp::mesh::Uniform1DMeshGeometry geom;
// geom.nb_nodes = 200;
// geom.dx = 0.05;
// geom.section = 5.0;
specmicp::mesh::Mesh1DPtr the_mesh = specmicp::io::configure_mesh(config.get_section("mesh"));
// specmicp::mesh::uniform_mesh1d(geom);
auto params = configure_single_simple(config.get_section("parameters"));
std::vector<index_t> list_fixed_nodes = {0,};
Vector init_cond = Vector::Zero(the_mesh->nb_nodes());
init_cond(0) = 0.5;
Vector init_cond_solid = Vector::Zero(the_mesh->nb_nodes());
auto variables = init_variables(the_mesh, list_fixed_nodes, init_cond, init_cond_solid);
for (int i=1;i<the_mesh->nb_nodes();++i) {
variables->diffusion_coefficient(i) = params->diffusion_coeff;
variables->porosity(i) = params->porosity;
variables->saturation(i) = params->saturation;
}
variables->diffusion_coefficient(0) = 1;
variables->porosity(0) = 1;
variables->saturation(0) = 1;
// transport stagger
auto transport_stagger =
std::make_shared<SingleTransportStagger>(variables, list_fixed_nodes);
auto& options = transport_stagger->options_solver();
configure_transport_options(options, config.get_section("transport_options"));
options.alpha = 1.0;
//options.residuals_tolerance = 1e-10;
//options.quasi_newton = 3;
// chemistry stagger
std::shared_ptr<ChemistryProgram> chem_program = nullptr;
if (params->binding_law == "leaching") {
auto functor = LeachingRateFunctor(params->coeff_1, params->coeff_2, params->freund_exp, params->rate_factor);
std::cout << "Leaching" << std::endl;
chem_program = std::make_shared<DefaultChemistryProgram>(functor);
} else {
auto functor = RateFunctor(params->freund_coeff, params->freund_exp, params->rate_factor);
chem_program = std::make_shared<DefaultChemistryProgram>(functor);
}
auto chemistry_stagger = std::make_shared<ChemistryStagger>(chem_program);
// upscaling stagger
auto upscaling_stagger = std::make_shared<SingleUpscalingStagger>();
// reactmicp solver
auto solver = specmicp::reactmicp::solver::ReactiveTransportSolver(transport_stagger, chemistry_stagger, upscaling_stagger);
auto& opts = solver.get_options();
specmicp::io::configure_reactmicp_options(opts, config.get_section("reactmicp_options"));
//opts.maximum_iterations = 51;
//opts.residuals_tolerance = 1e-5;
//opts.
// runner
auto conf_runner = config.get_section("runner");
auto info = specmicp::reactmicp::solver::SimulationInformation("ex_reactmicp_single", conf_runner.get_required_attribute<scalar_t>("save_time"));
auto runner = std::make_shared<specmicp::reactmicp::solver::ReactiveTransportRunner>(
solver,
conf_runner.get_required_attribute<scalar_t>("min_timestep"),
conf_runner.get_required_attribute<scalar_t>("max_timestep"),
info);
auto output_policy = OutputPolicy(conf_runner.get_required_attribute<std::string>("output_folder"));
runner->set_output_policy(output_policy);
runner->run_until(conf_runner.get_required_attribute<scalar_t>("run_time"), variables);
for (int i=0; i<the_mesh->nb_nodes(); ++i) {
std::cout << variables->fluid_concentration(i) << "\n";
}
std::cout << "----------\n" << std::endl;
for (int i=0; i<the_mesh->nb_nodes(); ++i) {
std::cout << variables->bound_concentration(i) << "\n";
}
std::cout << std::endl;
return 0;
}

Event Timeline