Page MenuHomec4science

reduced_system_solver.cpp
No OneTemporary

File Metadata

Created
Wed, Jun 26, 12:09

reduced_system_solver.cpp

/*-------------------------------------------------------
- Module : specmicp
- File : reduced_system_solver
- 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 <iostream>
#include "reduced_system_solver.hpp"
#include "micpsolver/micpsolver.hpp"
#include "micpsolver/micpsolverold.hpp"
//#include "micpsolver/micpsolver_min.hpp"
#include "equilibrium_data.hpp"
#include "simulation_box.hpp"
namespace specmicp {
ReducedSystemSolver::ReducedSystemSolver(
RawDatabasePtr data,
const Vector& totconc,
const SimulationBox& simulbox,
ReducedSystemSolverOptions options
):
m_options(options),
m_data(data),
m_system(std::make_shared<ReducedSystem>(
data,
totconc,
simulbox,
options.conservation_water,
options.charge_keeper
)),
m_var(Vector::Zero(data->nb_component+data->nb_mineral))
{}
ReducedSystemSolver::ReducedSystemSolver(
RawDatabasePtr data,
const Vector& totconc,
const SimulationBox& simulbox,
const EquilibriumState& previous_solution,
ReducedSystemSolverOptions options
):
m_options(options),
m_data(data),
m_system(std::make_shared<ReducedSystem>(
data,
totconc,
simulbox,
previous_solution,
options.conservation_water,
options.charge_keeper
)),
m_var(Vector::Zero(data->nb_component+data->nb_mineral))
{}
ReducedSystemSolver::ReducedSystemSolver(
RawDatabasePtr data,
const Vector& totconc,
ReducedSystemSolverOptions options
):
m_options(options),
m_data(data),
m_system(std::make_shared<ReducedSystem>(
data,
totconc,
options.conservation_water,
options.charge_keeper
)),
m_var(Vector::Zero(data->nb_component+data->nb_mineral))
{}
ReducedSystemSolver::ReducedSystemSolver(
RawDatabasePtr data,
const Vector& totconc,
const EquilibriumState& previous_solution,
ReducedSystemSolverOptions options
):
m_options(options),
m_data(data),
m_system(std::make_shared<ReducedSystem>(
data,
totconc,
SimulationBox(),
previous_solution,
options.conservation_water,
options.charge_keeper
)),
m_var(Vector::Zero(data->nb_component+data->nb_mineral))
{}
EquilibriumState ReducedSystemSolver::get_solution(const Eigen::VectorXd& x)
{
set_true_variable_vector(x);
return m_system->get_solution(x, m_var);
}
micpsolver::MiCPPerformance ReducedSystemSolver::solve(Vector& x, bool init)
{
if (init)
{
if (get_options().ncp_function == micpsolver::NCPfunction::min)
m_system->reasonable_starting_guess(x, true);
else
m_system->reasonable_starting_guess(x, false);
}
set_true_variable_vector(x);
micpsolver::MiCPPerformance perf = solve();
if (perf.return_code != micpsolver::MiCPSolverReturnCode::ResidualMinimized and get_options().allow_restart)
{
WARNING << "Failed to solve the system ! - We shake it up and start again";
const micpsolver::NCPfunction save_ncp = get_options().ncp_function;
const scalar_t save_penalization_factor = get_options().solver_options.penalization_factor;
if (save_ncp == micpsolver::NCPfunction::min)
{
get_options().ncp_function = micpsolver::NCPfunction::penalizedFB;
m_system->reasonable_restarting_guess(x);
}
else
{
if (save_penalization_factor == 1)
get_options().solver_options.penalization_factor = 0.8;
set_return_vector(x);
m_system->reasonable_restarting_guess(x);
}
set_true_variable_vector(x);
micpsolver::MiCPPerformance perf2 = solve();
get_options().ncp_function = save_ncp;
get_options().solver_options.penalization_factor = save_penalization_factor;
perf += perf2;
}
if (perf.return_code > micpsolver::MiCPSolverReturnCode::NotConvergedYet) set_return_vector(x);
return perf;
}
void ReducedSystemSolver::set_true_variable_vector(const Vector& x)
{
const std::vector<index_t>& non_active_component = m_system->get_non_active_component();
if (non_active_component.size() == 0)
{
// we still copy the data, if we failed to solve the problem, we can restart
if (m_options.conservation_water)
m_var = x; // direct copy
else
m_var = x.block(1, 0, x.rows()-1, 1);
for (int i=0; i<m_var.rows(); ++i)
{
if (m_var(i) == -HUGE_VAL) // check for previously undefined value
{
m_var(i) = -4;
}
}
}
else // remove the dof that are not part of the problem
{
uindex_t new_i = 0;
if (m_options.conservation_water)
{
m_var(0) = x(0);
++new_i;
}
for (index_t i: m_data->range_aqueous_component())
{
auto it = std::find(non_active_component.begin(), non_active_component.end(),i);
if (it != non_active_component.end()) continue;
m_var(new_i) = x(i);
++new_i;
}
for (index_t m: m_data->range_mineral())
{
for (auto it=non_active_component.begin(); it!=non_active_component.end(); ++it)
{
if (m_data->nu_mineral(m, *it) != 0) continue;
m_var(new_i) = x(m_data->nb_component+m);
++new_i;
}
}
m_var.conservativeResize(new_i);
}
m_system->reduce_mineral_problem(m_var);
}
micpsolver::MiCPPerformance ReducedSystemSolver::solve()
{
if (get_options().ncp_function == micpsolver::NCPfunction::penalizedFB)
{
// the default
micpsolver::MiCPSolver<ReducedSystem> solver(m_system);
solver.set_options(get_options().solver_options);
solver.solve(m_var);
return solver.get_performance();
}
else
{
micpsolver::MiCPSolverOLD<ReducedSystem, micpsolver::NCPfunction::min> solver(m_system);
solver.set_options(get_options().solver_options);
solver.solve(m_var);
return solver.get_performance();
}
}
void ReducedSystemSolver::set_return_vector(Vector& x)
{
const std::vector<index_t>& non_active_component = m_system->get_non_active_component();
if (non_active_component.size() == 0)
{
if (m_options.conservation_water)
x = m_var; //direct copy
else
x.block(1, 0, x.rows()-1, 1) = m_var;
// at that point we should have the correct solution
}
else
{
uindex_t new_i = 0;
if (m_options.conservation_water)
{
x(0) = m_var(new_i);
++new_i;
}
for (index_t i: m_data->range_aqueous_component())
{
auto it = std::find(non_active_component.begin(), non_active_component.end(),i);
if (it != non_active_component.end())
{
x(i) = -HUGE_VAL;
continue;
}
x(i) = m_var(new_i) ;
++new_i;
}
for (index_t m: m_data->range_mineral())
{
for (auto it=non_active_component.begin(); it!=non_active_component.end(); ++it)
{
if (m_data->nu_mineral(m, *it) != 0)
{
x(m_data->nb_component+m) = 0;
continue;
}
x(m_data->nb_component+m) =m_var(new_i);
++new_i;
}
}
}
m_system->reset_mineral_system(m_var, x);
}
} // end namespace specmicp

Event Timeline