Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F102424441
chloride_vars.cpp
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Thu, Feb 20, 14:22
Size
9 KB
Mime Type
text/x-c++
Expires
Sat, Feb 22, 14:22 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
24354387
Attached To
rSPECMICP SpecMiCP / ReactMiCP
chloride_vars.cpp
View Options
/* =============================================================================
Copyright (c) 2014-2017 F. Georget <fabieng@princeton.edu> Princeton University
Copyright (c) 2017-2018 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 chloride_vars.cpp
//! \brief Example of variable initialization plugin for chloride system
//!
#include "specmicp_common/plugins/plugin_base.hpp"
#include "specmicp_common/plugins/plugin_interface.h"
#include "reactmicp/reactmicp_chloride.hpp"
#include "specmicp/io/configuration.hpp"
#include "specmicp_database/database.hpp"
#include "dfpm/mesh.hpp"
#include "specmicp_common/io/safe_config.hpp"
#include "specmicp_common/io/config_yaml_sections.h"
#include "specmicp/problem_solver/smart_solver.hpp"
#include "specmicp/problem_solver/reactant_box.hpp"
#include "specmicp_database/unknown_class.hpp"
#include <iostream>
using namespace specmicp;
using namespace specmicp::reactmicp;
using namespace specmicp::reactmicp::systems::chloride;
class SPECMICP_DLL_PUBLIC ChlorideVariablesFactory: public InitializeVariables
{
public:
static ChlorideVariablesFactory* get() {
return new ChlorideVariablesFactory();
}
virtual std::shared_ptr<ChlorideVariables> initialize_variables(
std::shared_ptr<database::DataContainer> the_database,
std::shared_ptr<mesh::Mesh1D> the_mesh,
const units::UnitsSet& the_units,
io::YAMLConfigHandle& configuration
) override;
};
AdimensionalSystemSolution initialize_initial_cond(
std::shared_ptr<database::DataContainer> raw_data,
const units::UnitsSet& the_units,
io::YAMLConfigHandle& configuration
);
AdimensionalSystemSolution initialize_upstream_cond(
std::shared_ptr<database::DataContainer> raw_data,
const units::UnitsSet& the_units,
io::YAMLConfigHandle& configuration
);
AdimensionalSystemSolution initialize_downstream_cond(
std::shared_ptr<database::DataContainer> raw_data,
const units::UnitsSet& the_units,
io::YAMLConfigHandle& configuration
);
class SPECMICP_DLL_PUBLIC ChlorideVariablesPlugin: public specmicp::plugins::PluginBase
{
public:
ChlorideVariablesPlugin():
PluginBase()
{
set_api_version(0, 1, 0);
}
bool initialize(const specmicp::plugins::PluginManagerServices& services) override
{
auto retcode = services.register_object(
"reactmicp/chloride/variables", "chloride_variables",
&ChlorideVariablesFactory::get
);
if (not retcode) {
return false;
}
return true;
}
};
SPECMICP_PLUGIN(ChlorideVariablesPlugin)
std::shared_ptr<ChlorideVariables> ChlorideVariablesFactory::initialize_variables(
std::shared_ptr<database::DataContainer> the_database,
std::shared_ptr<mesh::Mesh1D> the_mesh,
const units::UnitsSet& the_units,
io::YAMLConfigHandle& configuration
)
{
index_t upstream = 0;
index_t downstream = the_mesh->nb_nodes()-1;
auto chem_conf = configuration.get_section("reactant_boxes");
std::vector<AdimensionalSystemSolution> solutions(3);
solutions[0] = initialize_initial_cond(the_database, the_units, chem_conf);
solutions[1] = initialize_upstream_cond(the_database, the_units, chem_conf);
solutions[2] = initialize_downstream_cond(the_database, the_units, chem_conf);
ChlorideVariablesInterface var_interface(the_database, the_mesh, the_units);
std::vector<index_t> indices(the_mesh->nb_nodes(), 0);
indices[upstream] = 1;
indices[downstream] = 2;
var_interface.set_adim_solutions(indices, solutions);
auto pot_conf = configuration.get_section("potential");
auto pot_upstream = pot_conf.get_required_attribute<scalar_t>("upstream");
var_interface.set_potential(upstream, pot_upstream);
auto pot_downstream = pot_conf.get_required_attribute<scalar_t>("downstream");
var_interface.set_potential(downstream, pot_downstream);
auto diff_conf = configuration.get_section("diffusion_coefficients");
auto default_di = diff_conf.get_required_attribute<scalar_t>("default");
var_interface.set_default_diffusion_coefficient_component(default_di);
auto default_dj = diff_conf.get_optional_attribute<scalar_t>("default_aqueous", default_di);
var_interface.set_default_diffusion_coefficient_aqueous(default_dj);
auto res_factor = Vector(the_mesh->nb_nodes());
res_factor.setConstant(1e-3);
res_factor(upstream) = 1.0;
res_factor(downstream) = 1.0;
var_interface.set_diffusion_resistance_factors(res_factor);
database::SpeciesTypeFinder finder(the_database);
for (auto it=diff_conf.map_begin(); it!=diff_conf.map_end(); ++it) {
auto res = it.get_as<scalar_t>();
if (res.first == "default" or res.first == "default_aqueous") {
continue;
}
auto aq_class = finder.find_aqueous_species(res.first);
if (aq_class.type == database::AqueousSpeciesClass::Component) {
var_interface.set_diffusion_coefficient_component(aq_class.id, res.second);
} else if (aq_class.type == database::AqueousSpeciesClass::Aqueous) {
var_interface.set_diffusion_coefficient_aqueous(aq_class.id, res.second);
} else {
throw std::runtime_error("Unknown aqueous species : '"+res.first+"'.");
}
}
std::cout << var_interface.get_raw_variables()->main_variables() << std::endl;
return var_interface.get_variables();
}
AdimensionalSystemSolution initialize_initial_cond(
std::shared_ptr<database::DataContainer> raw_data,
const units::UnitsSet& the_units,
io::YAMLConfigHandle& configuration
)
{
auto conf = configuration.get_section("initial_condition");
AdimensionalSystemSolverOptions opts;
opts.units_set = the_units;
if (conf.has_section(SPC_CF_S_SPECMICP))
{
io::configure_specmicp_options(opts, the_units, conf.get_section(SPC_CF_S_SPECMICP));
}
auto reactant_box = io::configure_specmicp_reactant_box(raw_data, the_units, std::move(conf));
auto constraints = reactant_box.get_constraints(true);
SmartAdimSolver tsolver(raw_data, constraints, opts);
if (not tsolver.solve())
{
throw std::runtime_error("Failed to solve initial condition");
}
return tsolver.get_solution();
}
AdimensionalSystemSolution initialize_upstream_cond(
std::shared_ptr<database::DataContainer> raw_data,
const units::UnitsSet& the_units,
io::YAMLConfigHandle& configuration
)
{
auto conf = configuration.get_section("upstream");
AdimensionalSystemSolverOptions opts;
opts.units_set = the_units;
if (conf.has_section(SPC_CF_S_SPECMICP))
{
io::configure_specmicp_options(opts, the_units, conf.get_section(SPC_CF_S_SPECMICP));
}
auto reactant_box = io::configure_specmicp_reactant_box(raw_data, the_units, std::move(conf));
auto constraints = reactant_box.get_constraints(false);
SmartAdimSolver tsolver(raw_data, constraints, opts);
if (not tsolver.solve())
{
throw std::runtime_error("Failed to solve upstream");
}
return tsolver.get_solution();
}
AdimensionalSystemSolution initialize_downstream_cond(
std::shared_ptr<database::DataContainer> raw_data,
const units::UnitsSet& the_units,
io::YAMLConfigHandle& configuration
)
{
auto conf = configuration.get_section("downstream");
AdimensionalSystemSolverOptions opts;
opts.units_set = the_units;
if (conf.has_section(SPC_CF_S_SPECMICP))
{
io::configure_specmicp_options(opts, the_units, conf.get_section(SPC_CF_S_SPECMICP));
}
auto reactant_box = io::configure_specmicp_reactant_box(raw_data, the_units, std::move(conf));
auto constraints = reactant_box.get_constraints(false);
SmartAdimSolver tsolver(raw_data, constraints, opts);
if (not tsolver.solve())
{
throw std::runtime_error("Failed to solve upstream");
}
return tsolver.get_solution();
}
Event Timeline
Log In to Comment