Page MenuHomec4science

reaction_path.cpp
No OneTemporary

File Metadata

Created
Wed, Jun 12, 01:34

reaction_path.cpp

/*-------------------------------------------------------
- Module : specmicp
- File : reaction_path.cpp
- Author : Fabien Georget
Copyright (c) 2014, Fabien 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:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* 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.
* Neither the name of the Princeton University 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 OWNER 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 "reaction_path.hpp"
#include "micpsolver/micpsolver.hpp"
#include "utils/log.hpp"
#include <iostream>
namespace specmicp {
void ReactionPathDriver::read_database()
{
specmicp::database::Database database(m_model->database_path);
database.make_canonical();
m_data = database.get_database();
}
//! \brief initialize
void ReactionPathDriver::init_solution(Eigen::VectorXd& x)
{
x.resize(m_data->nb_component+m_data->nb_mineral); // Set the right size
x.setZero();
m_must_init = true; // the solution vector must be initiated by the solver
// prepare the solution
dissolve_to_components();
}
void ReactionPathDriver::dissolve_to_components()
{
m_tot_conc = Eigen::VectorXd::Zero(m_data->nb_component);
m_tot_conc_increase = Eigen::VectorXd::Zero(m_data->nb_component);
// everything is dissolved ...
for (auto it=m_model->amount_aqueous.begin(); it!=m_model->amount_aqueous.end(); ++it)
{
try
{
int id = database::Database::safe_label_to_id(it->first, m_data->labels_basis);
m_tot_conc(id) += it->second.first;
m_tot_conc_increase(id) += it->second.second;
}
catch(std::invalid_argument& e)
{
int id = database::Database::safe_label_to_id(it->first, m_data->labels_aqueous);
for (int idc=0; idc<m_data->nb_component; ++idc)
{
m_tot_conc(idc) += m_data->nu_aqueous(id, idc)*it->second.first;
m_tot_conc_increase(id) += m_data->nu_aqueous(id, idc)*it->second.second;
}
}
}
for (auto it=m_model->amount_minerals.begin(); it!=m_model->amount_minerals.end(); ++it)
{
try
{
int id = database::Database::safe_label_to_id(it->first, m_data->labels_minerals);
for (int idc=0; idc<m_data->nb_component; ++idc)
{
if (m_data->nu_mineral(id, idc) == 0.0) continue;
m_tot_conc(idc) += m_data->nu_mineral(id, idc)*it->second.first;
m_tot_conc_increase(idc) += m_data->nu_mineral(id, idc)*it->second.second;
}
}
catch(std::invalid_argument& e)
{
int id = database::Database::safe_label_to_id(it->first, m_data->labels_minerals_kinetic);
for (int idc=0; idc<m_data->nb_component; ++idc)
{
if (m_data->nu_mineral_kinetic(id, idc) == 0.0) continue;
m_tot_conc(idc) += m_data->nu_mineral_kinetic(id, idc)*it->second.first;
m_tot_conc_increase(idc) += m_data->nu_mineral_kinetic(id, idc)*it->second.second;
}
}
}
// Do we need to remove some components ?
std::vector<int> to_remove;
to_remove.reserve(10);
int new_i = 0;
for (int i=0; i<m_data->nb_component; ++i)
{
if (m_tot_conc(i) == 0 and m_tot_conc_increase(i) == 0)
{
to_remove.push_back(i);
continue;
}
m_tot_conc(new_i) = m_tot_conc(i);
m_tot_conc_increase(new_i) = m_tot_conc_increase(i);
++new_i;
}
m_tot_conc.conservativeResize(new_i);
m_tot_conc_increase.conservativeResize(new_i);
specmicp::database::Database database(m_data);
if (to_remove.size() > 0) database.remove_components(to_remove);
if (m_model->minerals_to_keep.size() != 0)
{
database.minerals_keep_only(m_model->minerals_to_keep);
}
}
micpsolver::MiCPPerformance ReactionPathDriver::one_step(Eigen::VectorXd& x, bool init)
{
ReducedSystemSolver solver;
if (m_current_solution.is_valid())
{
solver = ReducedSystemSolver(m_data, m_tot_conc, m_current_solution);
}
else
{
solver = ReducedSystemSolver(m_data, m_tot_conc);
}
if (m_must_init == true) {init = true;}
solver.get_options() = m_solver_options;
micpsolver::MiCPPerformance perf = solver.solve(x, init);
if (perf.return_code != micpsolver::MiCPSolverReturnCode::ResidualMinimized)
{
ERROR << "Failed to solve the system ! (most probably)";
}
m_current_solution = solver.get_solution(x);
m_tot_conc += m_tot_conc_increase;
m_must_init = false;
return perf;
}
void ReactionPathDriver::run_all()
{
Eigen::VectorXd x;
// first step -> we initialize
one_step(x, true);
// then normal step
for (int i=1; i<m_model->nb_step; ++i)
{
one_step(x);
}
}
} // end namespace specmicp

Event Timeline