This class contains methods to solve the algebraic system in GeochemicalSystem. More...
#include <GeochemicalSolver.h>
Public Member Functions | |
GeochemicalSolver (unsigned num_basis, unsigned num_kin, GeochemistryIonicStrength &is, Real abs_tol, Real rel_tol, unsigned max_iter, Real max_initial_residual, Real swap_threshold, unsigned max_swaps_allowed, const std::vector< std::string > &prevent_precipitation, Real max_ionic_strength, unsigned ramp_max_ionic_strength, bool evaluate_kin_always) | |
Construct and check for sensible arguments. More... | |
void | solveSystem (GeochemicalSystem &egs, std::stringstream &ss, unsigned &tot_iter, Real &abs_residual, Real dt, DenseVector< Real > &mole_additions, DenseMatrix< Real > &dmole_additions) |
Solve the system. More... | |
void | setMaxInitialResidual (Real max_initial_residual) |
Set value for max_initial_residual. More... | |
Real | getMaxInitialResidual () const |
Get value for max_initial_residual. More... | |
void | setRampMaxIonicStrength (unsigned ramp_max_ionic_strength) |
Sets the value of _ramp_max_ionic_strength. More... | |
unsigned | getRampMaxIonicStrength () const |
Gets the value of _ramp_max_ionic_strength. More... | |
Private Member Functions | |
Real | computeResidual (const GeochemicalSystem &egs, DenseVector< Real > &residual, const DenseVector< Real > &mole_additions) const |
Builds the residual of the algebraic system. More... | |
void | solveAndUnderrelax (const GeochemicalSystem &egs, DenseMatrix< Real > &jacobian, DenseVector< Real > &new_mol) const |
Solves _jacobian * neg_change_mol = _residual for neg_change_mol, then performs an underrelaxation to get new_mol. More... | |
bool | swapNeeded (const GeochemicalSystem &egs, unsigned &swap_out_of_basis, unsigned &swap_into_basis, std::stringstream &ss) const |
Check if a basis swap is needed. More... | |
bool | reduceInitialResidual (GeochemicalSystem &egs, Real dt, DenseVector< Real > &mole_additions, DenseMatrix< Real > &dmole_additions) |
Progressively alter the initial-guess molalities for the algebraic system to attempt to reduce the residual. More... | |
Private Attributes | |
GeochemistryIonicStrength & | _is |
The ionic-strength calculator. More... | |
const unsigned | _num_basis |
Number of species in the basis. More... | |
const unsigned | _num_kin |
Number of kinetic species. More... | |
unsigned | _num_basis_in_algebraic_system |
Number of basis molalities (and potentially solvent water mass) in the algebraic system. More... | |
unsigned | _num_in_algebraic_system |
Number of unknowns (molalities and surface potentials) in the algebraic system. More... | |
DenseVector< Real > | _residual |
residual of the algebraic system we wish to solve More... | |
Real | _abs_residual |
L1 norm of residual. More... | |
DenseMatrix< Real > | _jacobian |
jacobian of the algebraic system More... | |
DenseVector< Real > | _new_mol |
the new molality after finding the solution of _jacobian * neg_change_mol = _residual More... | |
const Real | _abs_tol |
If the residual of the algebraic system falls below this value, the Newton process has converged. More... | |
const Real | _rel_tol |
If the residual of the algebraic system falls below this value times the initial residual, the Newton process has converged. More... | |
Real | _res0_times_rel |
_res0_times_rel = _rel_tol * initial residual More... | |
const unsigned | _max_iter |
maximum number of iterations allowed during an inner solve More... | |
Real | _max_initial_residual |
maximum desired initial residual More... | |
const Real | _swap_threshold |
If a basis molality < swap_threshold, we attempt to swap it out of the basis. More... | |
const unsigned | _max_swaps_allowed |
Maximum number of swaps allowed before the solve aborts. More... | |
const std::vector< std::string > | _prevent_precipitation |
The minerals named in this list can have positive saturation indices and will not precipitate. More... | |
const Real | _max_ionic_strength |
Maximum ionic strength allowed. More... | |
unsigned | _ramp_max_ionic_strength |
Number of iterations over which to increase the maximum ionic strength to _max_ionic_strength. More... | |
bool | _evaluate_kin_always |
When to compute the kinetic rates: if true then evaluate before every residual calculation, otherwise evaluate only at the start of the solve process (using, eg, the old molality values) More... | |
DenseVector< Real > | _input_mole_additions |
the mole_additions for the basis and kinetic species as specified in the argument of solveSystem More... | |
DenseMatrix< Real > | _input_dmole_additions |
d(mole_additions)/d(species) as specified in the argument of solveSystem More... | |
This class contains methods to solve the algebraic system in GeochemicalSystem.
Definition at line 18 of file GeochemicalSolver.h.
GeochemicalSolver::GeochemicalSolver | ( | unsigned | num_basis, |
unsigned | num_kin, | ||
GeochemistryIonicStrength & | is, | ||
Real | abs_tol, | ||
Real | rel_tol, | ||
unsigned | max_iter, | ||
Real | max_initial_residual, | ||
Real | swap_threshold, | ||
unsigned | max_swaps_allowed, | ||
const std::vector< std::string > & | prevent_precipitation, | ||
Real | max_ionic_strength, | ||
unsigned | ramp_max_ionic_strength, | ||
bool | evaluate_kin_always | ||
) |
Construct and check for sensible arguments.
num_basis | Number of basis species in the system |
mum_kin | Number of kinetic species in the system |
is | The object to compute ionic strengths. GeochemicalSolver changes the maximum value of ionic strength as it progresses towards the solution |
abs_tol | The Newton solution process is deemed to have converged if the L1 norm of the residual < abs_tol |
rel_tol | The Newton solution process is deemed to have converged if the L1 norm of the residual < rel_tol * residual_0 |
max_iter | Only max_iter Newton iterations are allowed in solving the system |
max_initial_residual | egs guesses the initial configuration, but sometimes this results in a very large initial residual. GeochemicalSolver will attempt to alter the initial guesses for molalities so that the initial residual is less than max_initial_residual. This parameter may be varied during runtime which might be useful in time-dependent simulations if the first residual is poor but the remainder are deemed likely to be OK |
swap_threshold | If a basis molality < swap_threshold at the end of the Newton process, GeochemicalSolver attempts to swap it out of the basis |
max_swaps_allowed | Maximum number of swaps allowed before the solve process aborts |
prevent_precipitation | The minerals named in this vector will not be allowed to precipitate, even if their saturation indices are positive |
max_ionic_strength | Maximum ionic strength ever allowed |
ramp_max_ionic_strength | The maximum ionic strength is ramped from 0 to max_ionic_strength over the course of the first ramp_max_ionic_strength Newton iterations. This can help with convergence. |
evaluate_kin_always | If true then evaluate the kinetic rates before each residual calculation, otherwise evaluate them only at the start of the solve process (which will be using the molalities, etc from the previous time-step) |
Definition at line 13 of file GeochemicalSolver.C.
|
private |
Builds the residual of the algebraic system.
egs | The geochemical system to use to compute the residual |
residual | The residual components will be placed here |
mole_additions | the increment of mole number of each basis species and kinetic species since the last timestep. |
Definition at line 78 of file GeochemicalSolver.C.
Referenced by reduceInitialResidual(), and solveSystem().
Real GeochemicalSolver::getMaxInitialResidual | ( | ) | const |
Get value for max_initial_residual.
Definition at line 72 of file GeochemicalSolver.C.
unsigned GeochemicalSolver::getRampMaxIonicStrength | ( | ) | const |
Gets the value of _ramp_max_ionic_strength.
Definition at line 470 of file GeochemicalSolver.C.
|
private |
Progressively alter the initial-guess molalities for the algebraic system to attempt to reduce the residual.
egs | The GeochemicalSystem we're trying to solve |
dt | time-step size (used for determining kinetic rates) |
mole_additions | the increment of mole number of each basis species and kinetic species since the last timestep. This may change during this function as kinetic rates depend on molalities |
dmole_additions | d(mole_additions)/(molality and kinetic moles) |
Definition at line 241 of file GeochemicalSolver.C.
Referenced by solveSystem().
Set value for max_initial_residual.
Definition at line 64 of file GeochemicalSolver.C.
Referenced by TEST().
void GeochemicalSolver::setRampMaxIonicStrength | ( | unsigned | ramp_max_ionic_strength | ) |
Sets the value of _ramp_max_ionic_strength.
Definition at line 462 of file GeochemicalSolver.C.
Referenced by GeochemistryTimeDependentReactor::execute(), and GeochemistrySpatialReactor::execute().
|
private |
Solves _jacobian * neg_change_mol = _residual for neg_change_mol, then performs an underrelaxation to get new_mol.
egs | The geochemical system that we're trying to solve |
jacobian | the jacobian of the system |
new_mol | upon exit, this will be the new molality (and surface potential values, if any) values according to the underrelaxed Newton process |
Definition at line 88 of file GeochemicalSolver.C.
Referenced by solveSystem().
void GeochemicalSolver::solveSystem | ( | GeochemicalSystem & | egs, |
std::stringstream & | ss, | ||
unsigned & | tot_iter, | ||
Real & | abs_residual, | ||
Real | dt, | ||
DenseVector< Real > & | mole_additions, | ||
DenseMatrix< Real > & | dmole_additions | ||
) |
Solve the system.
egs | The geochemical system that needs to be solved. This gets modified as the solve progresses |
ss | Textual information such as (iteration, residual) and swap information is written to this stringstream |
tot_iter | The total number of iterations used in the solve |
abs_residual | The residual of the algebraic system as this method exits |
mole_additions | the increment of mole number of each basis species and kinetic species since the last timestep. This must have size _num_basis + _num_kin. For the basis species, this is the amount of each species being injected into the system over the timestep. For the kinetic species, this is -dt*reaction_rate. Please note: do not decompose the kinetic-species additions into basis components and add them to the first slots of mole_additions! This method does that decomposition automatically. The first _num_basis slots of mole_additions contain kinetic-independent additions, while the last _num_kin slots contain kinetic-rate contributions. This may change during the solution process as kinetic rates depend on molalities. |
dmole_additions | dmole_additions(a, b) = d(mole_additions(a))/d(basis_molality(b)) for b < _num_basis and d(mole_additions(a))/d(kinetic_moles(b - _num_basis)) otherwise. |
Definition at line 316 of file GeochemicalSolver.C.
Referenced by GeochemistryTimeDependentReactor::execute(), GeochemistrySpatialReactor::execute(), GeochemistryTimeIndependentReactor::initialSetup(), GeochemistryTimeDependentReactor::initialSetup(), GeochemistrySpatialReactor::initialSetup(), GeochemistryTimeDependentReactor::postSolveExchanger(), and TEST().
|
private |
Check if a basis swap is needed.
It is needed if:
egs | Geochemical system that we're trying to solve |
swap_out_of_basis | the index of the species in the basis that will be removed from the basis |
swap_into_basis | the index of the equilibrium mineneral that will be added to the basis |
Definition at line 107 of file GeochemicalSolver.C.
Referenced by solveSystem().
|
private |
L1 norm of residual.
Definition at line 118 of file GeochemicalSolver.h.
Referenced by reduceInitialResidual(), solveSystem(), and swapNeeded().
|
private |
If the residual of the algebraic system falls below this value, the Newton process has converged.
Definition at line 124 of file GeochemicalSolver.h.
Referenced by solveSystem(), and swapNeeded().
|
private |
When to compute the kinetic rates: if true then evaluate before every residual calculation, otherwise evaluate only at the start of the solve process (using, eg, the old molality values)
Definition at line 147 of file GeochemicalSolver.h.
Referenced by reduceInitialResidual(), and solveSystem().
|
private |
d(mole_additions)/d(species) as specified in the argument of solveSystem
Definition at line 151 of file GeochemicalSolver.h.
Referenced by reduceInitialResidual(), and solveSystem().
|
private |
the mole_additions for the basis and kinetic species as specified in the argument of solveSystem
Definition at line 149 of file GeochemicalSolver.h.
Referenced by reduceInitialResidual(), and solveSystem().
|
private |
The ionic-strength calculator.
Definition at line 106 of file GeochemicalSolver.h.
Referenced by solveSystem().
|
private |
jacobian of the algebraic system
Definition at line 120 of file GeochemicalSolver.h.
Referenced by solveSystem().
|
private |
maximum desired initial residual
Definition at line 132 of file GeochemicalSolver.h.
Referenced by getMaxInitialResidual(), reduceInitialResidual(), setMaxInitialResidual(), and solveSystem().
|
private |
Maximum ionic strength allowed.
Definition at line 140 of file GeochemicalSolver.h.
Referenced by solveSystem().
|
private |
maximum number of iterations allowed during an inner solve
Definition at line 130 of file GeochemicalSolver.h.
Referenced by GeochemicalSolver(), setRampMaxIonicStrength(), and solveSystem().
|
private |
Maximum number of swaps allowed before the solve aborts.
Definition at line 136 of file GeochemicalSolver.h.
Referenced by solveSystem().
|
private |
the new molality after finding the solution of _jacobian * neg_change_mol = _residual
Definition at line 122 of file GeochemicalSolver.h.
Referenced by solveSystem().
|
private |
Number of species in the basis.
Definition at line 108 of file GeochemicalSolver.h.
Referenced by solveSystem(), and swapNeeded().
|
private |
Number of basis molalities (and potentially solvent water mass) in the algebraic system.
Definition at line 112 of file GeochemicalSolver.h.
Referenced by reduceInitialResidual(), and solveSystem().
|
private |
Number of unknowns (molalities and surface potentials) in the algebraic system.
Definition at line 114 of file GeochemicalSolver.h.
Referenced by computeResidual(), solveAndUnderrelax(), and solveSystem().
|
private |
Number of kinetic species.
Definition at line 110 of file GeochemicalSolver.h.
Referenced by solveSystem().
|
private |
The minerals named in this list can have positive saturation indices and will not precipitate.
Definition at line 138 of file GeochemicalSolver.h.
Referenced by swapNeeded().
|
private |
Number of iterations over which to increase the maximum ionic strength to _max_ionic_strength.
Definition at line 142 of file GeochemicalSolver.h.
Referenced by GeochemicalSolver(), getRampMaxIonicStrength(), setRampMaxIonicStrength(), and solveSystem().
|
private |
If the residual of the algebraic system falls below this value times the initial residual, the Newton process has converged.
Definition at line 126 of file GeochemicalSolver.h.
Referenced by solveSystem().
|
private |
_res0_times_rel = _rel_tol * initial residual
Definition at line 128 of file GeochemicalSolver.h.
Referenced by solveSystem(), and swapNeeded().
|
private |
residual of the algebraic system we wish to solve
Definition at line 116 of file GeochemicalSolver.h.
Referenced by reduceInitialResidual(), solveAndUnderrelax(), and solveSystem().
|
private |
If a basis molality < swap_threshold, we attempt to swap it out of the basis.
Definition at line 134 of file GeochemicalSolver.h.
Referenced by solveSystem(), and swapNeeded().