CAMP 1.0.0
Chemistry Across Multiple Phases
|
Header file for solver functions. More...
Go to the source code of this file.
Functions | |
void * | solver_new (int n_state_var, int n_cells, int *var_type, int n_rxn, int n_rxn_int_param, int n_rxn_float_param, int n_rxn_env_param, int n_aero_phase, int n_aero_phase_int_param, int n_aero_phase_float_param, int n_aero_rep, int n_aero_rep_int_param, int n_aero_rep_float_param, int n_aero_rep_env_param, int n_sub_model, int n_sub_model_int_param, int n_sub_model_float_param, int n_sub_model_env_param) |
Get a new solver object. | |
void | solver_initialize (void *solver_data, double *abs_tol, double rel_tol, int max_steps, int max_conv_fails) |
Solver initialization. | |
int | solver_run (void *solver_data, double *state, double *env, double t_initial, double t_final) |
Solve for a given timestep. | |
void | solver_get_statistics (void *solver_data, int *solver_flag, int *num_steps, int *RHS_evals, int *LS_setups, int *error_test_fails, int *NLS_iters, int *NLS_convergence_fails, int *DLS_Jac_evals, int *DLS_RHS_evals, double *last_time_step__s, double *next_time_step__s, int *Jac_eval_fails, int *RHS_evals_total, int *Jac_evals_total, double *RHS_time__s, double *Jac_time__s, double *max_loss_precision) |
Get solver statistics after an integration attempt. | |
void | solver_free (void *solver_data) |
Free a SolverData object. | |
void | model_free (ModelData model_data) |
Free a ModelData object. | |
int | f (realtype t, N_Vector y, N_Vector deriv, void *model_data) |
Compute the time derivative f(t,y) | |
int | Jac (realtype t, N_Vector y, N_Vector deriv, SUNMatrix J, void *model_data, N_Vector tmp1, N_Vector tmp2, N_Vector tmp3) |
Compute the Jacobian. | |
int | guess_helper (const realtype t_n, const realtype h_n, N_Vector y_n, N_Vector y_n1, N_Vector hf, void *solver_data, N_Vector tmp1, N_Vector corr) |
void | error_handler (int error_code, const char *module, const char *function, char *msg, void *sd) |
Custom error handling function. | |
int | camp_solver_update_model_state (N_Vector solver_state, ModelData *model_data, realtype threshhold, realtype replacement_value) |
Update the model state from the current solver state. | |
SUNMatrix | get_jac_init (SolverData *solver_data) |
Try to improve guesses of y sent to the linear solver. | |
bool | check_Jac (realtype t, N_Vector y, SUNMatrix J, N_Vector deriv, N_Vector tmp, N_Vector tmp1, void *solver_data) |
Check a Jacobian for accuracy. | |
int | check_flag (void *flag_value, char *func_name, int opt) |
Check the return value of a SUNDIALS function. | |
void | check_flag_fail (void *flag_value, char *func_name, int opt) |
Check the return value of a SUNDIALS function and exit on failure. | |
void | solver_reset_timers (void *solver_data) |
Reset the timers for solver functions. | |
static void | solver_print_stats (void *cvode_mem) |
static void | print_data_sizes (ModelData *md) |
static void | print_jacobian (SUNMatrix M) |
static void | print_derivative (N_Vector deriv) |
bool | is_anything_going_on_here (SolverData *sd, realtype t_initial, realtype t_final) |
Determine if there is anything to solve. | |
Header file for solver functions.
Definition in file camp_solver.h.
int camp_solver_update_model_state | ( | N_Vector | solver_state, |
ModelData * | model_data, | ||
realtype | threshhold, | ||
realtype | replacement_value | ||
) |
Update the model state from the current solver state.
solver_state | Solver state vector |
model_data | Pointer to the model data (including the state array) |
threshhold | A lower limit for model concentrations below which the solver value is replaced with a replacement value |
replacement_value | Replacement value for low concentrations |
Definition at line 795 of file camp_solver.c.
int check_flag | ( | void * | flag_value, |
char * | func_name, | ||
int | opt | ||
) |
Check the return value of a SUNDIALS function.
flag_value | A pointer to check (either for NULL, or as an int pointer giving the flag value |
func_name | A string giving the function name returning this result code |
opt | A flag indicating the type of check to perform (0 for NULL pointer check; 1 for integer flag check) |
Definition at line 1584 of file camp_solver.c.
void check_flag_fail | ( | void * | flag_value, |
char * | func_name, | ||
int | opt | ||
) |
Check the return value of a SUNDIALS function and exit on failure.
flag_value | A pointer to check (either for NULL, or as an int pointer giving the flag value |
func_name | A string giving the function name returning this result code |
opt | A flag indicating the type of check to perform (0 for NULL pointer check; 1 for integer flag check) |
Definition at line 1614 of file camp_solver.c.
bool check_Jac | ( | realtype | t, |
N_Vector | y, | ||
SUNMatrix | J, | ||
N_Vector | deriv, | ||
N_Vector | tmp, | ||
N_Vector | tmp1, | ||
void * | solver_data | ||
) |
Check a Jacobian for accuracy.
This function compares Jacobian elements against differences in derivative calculations for small changes to the state array:
\[ J_{ij}(x) = \frac{f_i(x+\sum_j e_j) - f_i(x)}{\epsilon} \]
where \(\epsilon_j = 10^{-8} \left|x_j\right|\)
t | Current time [s] |
y | Current state array |
J | Jacobian matrix to evaluate |
deriv | Current derivative \(f(y)\) |
tmp | Working array the size of \(y\) |
tmp1 | Working array the size of \(y\) |
solver_data | Solver data |
Definition at line 1136 of file camp_solver.c.
void error_handler | ( | int | error_code, |
const char * | module, | ||
const char * | function, | ||
char * | msg, | ||
void * | sd | ||
) |
Custom error handling function.
This is used for quiet operation. Solver failures are returned with a flag from the solver_run() function.
Definition at line 1767 of file camp_solver.c.
int f | ( | realtype | t, |
N_Vector | y, | ||
N_Vector | deriv, | ||
void * | solver_data | ||
) |
Compute the time derivative f(t,y)
t | Current model time (s) |
y | Dependent variable array |
deriv | Time derivative vector f(t,y) to calculate |
solver_data | Pointer to the solver data |
Definition at line 833 of file camp_solver.c.
SUNMatrix get_jac_init | ( | SolverData * | solver_data | ) |
Try to improve guesses of y sent to the linear solver.
This function checks if there are any negative guessed concentrations, and if there are it calculates a set of initial corrections to the guessed state using the state at time \(t_{n-1}\) and the derivative \(f_{n-1}\) and advancing the state according to:
\[ y_n = y_{n-1} + \sum_{j=1}^m h_j * f_j \]
where \(h_j\) is the largest timestep possible where
\[ y_{j-1} + h_j * f_j > 0 \]
and
\[ t_n = t_{n-1} + \sum_{j=1}^m h_j \]
t_n | Current time [s] |
h_n | Current time step size [s] If this is set to zero, the change hf is assumed to be an adjustment where y_n = y_n1 + hf |
y_n | Current guess for \(y(t_n)\) |
y_n1 | \(y(t_{n-1})\) |
hf | Current guess for change in \(y\) from \(t_{n-1}\) to \(t_n\) [input/output] |
solver_data | Solver data |
tmp1 | Temporary vector for calculations |
corr | Vector of calculated adjustments to \(y(t_n)\) [output] |
Create a sparse Jacobian matrix based on model data
solver_data | A pointer to the SolverData |
Definition at line 1278 of file camp_solver.c.
int guess_helper | ( | const realtype | t_n, |
const realtype | h_n, | ||
N_Vector | y_n, | ||
N_Vector | y_n1, | ||
N_Vector | hf, | ||
void * | solver_data, | ||
N_Vector | tmp1, | ||
N_Vector | corr | ||
) |
bool is_anything_going_on_here | ( | SolverData * | sd, |
realtype | t_initial, | ||
realtype | t_final | ||
) |
Determine if there is anything to solve.
If the solver state concentrations and the derivative vector are very small, there is no point running the solver
Definition at line 1736 of file camp_solver.c.
int Jac | ( | realtype | t, |
N_Vector | y, | ||
N_Vector | deriv, | ||
SUNMatrix | J, | ||
void * | solver_data, | ||
N_Vector | tmp1, | ||
N_Vector | tmp2, | ||
N_Vector | tmp3 | ||
) |
Compute the Jacobian.
t | Current model time (s) |
y | Dependent variable array |
deriv | Time derivative vector f(t,y) |
J | Jacobian to calculate |
solver_data | Pointer to the solver data |
tmp1 | Unused vector |
tmp2 | Unused vector |
tmp3 | Unused vector |
Definition at line 962 of file camp_solver.c.
void model_free | ( | ModelData | model_data | ) |
Free a ModelData object.
model_data | Pointer to the ModelData object to free |
Definition at line 1776 of file camp_solver.c.
|
static |
|
static |
|
static |
void solver_free | ( | void * | solver_data | ) |
Free a SolverData object.
solver_data | Pointer to the SolverData object to free |
Definition at line 1693 of file camp_solver.c.
void solver_get_statistics | ( | void * | solver_data, |
int * | solver_flag, | ||
int * | num_steps, | ||
int * | RHS_evals, | ||
int * | LS_setups, | ||
int * | error_test_fails, | ||
int * | NLS_iters, | ||
int * | NLS_convergence_fails, | ||
int * | DLS_Jac_evals, | ||
int * | DLS_RHS_evals, | ||
double * | last_time_step__s, | ||
double * | next_time_step__s, | ||
int * | Jac_eval_fails, | ||
int * | RHS_evals_total, | ||
int * | Jac_evals_total, | ||
double * | RHS_time__s, | ||
double * | Jac_time__s, | ||
double * | max_loss_precision | ||
) |
Get solver statistics after an integration attempt.
solver_data | Pointer to the solver data |
solver_flag | Last flag returned by the solver |
num_steps | Pointer to set to the number of integration steps |
RHS_evals | Pointer to set to the number of right-hand side evaluations |
LS_setups | Pointer to set to the number of linear solver setups |
error_test_fails | Pointer to set to the number of error test failures |
NLS_iters | Pointer to set to the non-linear solver iterations |
NLS_convergence_fails | Pointer to set to the non-linear solver convergence failures |
DLS_Jac_evals | Pointer to set to the direct linear solver Jacobian evaluations |
DLS_RHS_evals | Pointer to set to the direct linear solver right-hand side evaluations |
last_time_step__s | Pointer to set to the last time step size [s] |
next_time_step__s | Pointer to set to the next time step size [s] |
Jac_eval_fails | Number of Jacobian evaluation failures |
RHS_evals_total | Total calls to f() |
Jac_evals_total | Total calls to Jac() |
RHS_time__s | Compute time for calls to f() [s] |
Jac_time__s | Compute time for calls to Jac() [s] |
max_loss_precision | Indicators of loss of precision in derivative calculation for each species |
Definition at line 715 of file camp_solver.c.
void solver_initialize | ( | void * | solver_data, |
double * | abs_tol, | ||
double | rel_tol, | ||
int | max_steps, | ||
int | max_conv_fails | ||
) |
Solver initialization.
Allocate and initialize solver objects
solver_data | Pointer to a SolverData object |
abs_tol | Pointer to array of absolute tolerances |
rel_tol | Relative integration tolerance |
max_steps | Maximum number of internal integration steps |
max_conv_fails | Maximum number of convergence failures |
Definition at line 364 of file camp_solver.c.
void * solver_new | ( | int | n_state_var, |
int | n_cells, | ||
int * | var_type, | ||
int | n_rxn, | ||
int | n_rxn_int_param, | ||
int | n_rxn_float_param, | ||
int | n_rxn_env_param, | ||
int | n_aero_phase, | ||
int | n_aero_phase_int_param, | ||
int | n_aero_phase_float_param, | ||
int | n_aero_rep, | ||
int | n_aero_rep_int_param, | ||
int | n_aero_rep_float_param, | ||
int | n_aero_rep_env_param, | ||
int | n_sub_model, | ||
int | n_sub_model_int_param, | ||
int | n_sub_model_float_param, | ||
int | n_sub_model_env_param | ||
) |
Get a new solver object.
Return a pointer to a new SolverData object
n_state_var | Number of variables on the state array per grid cell |
n_cells | Number of grid cells to solve simultaneously |
var_type | Pointer to array of state variable types (solver, constant, PSSA) |
n_rxn | Number of reactions to include |
n_rxn_int_param | Total number of integer reaction parameters |
n_rxn_float_param | Total number of floating-point reaction parameters |
n_rxn_env_param | Total number of environment-dependent reaction parameters |
n_aero_phase | Number of aerosol phases |
n_aero_phase_int_param | Total number of integer aerosol phase parameters |
n_aero_phase_float_param | Total number of floating-point aerosol phase parameters |
n_aero_rep | Number of aerosol representations |
n_aero_rep_int_param | Total number of integer aerosol representation parameters |
n_aero_rep_float_param | Total number of floating-point aerosol representation parameters |
n_aero_rep_env_param | Total number of environment-dependent aerosol representation parameters |
n_sub_model | Number of sub models |
n_sub_model_int_param | Total number of integer sub model parameters |
n_sub_model_float_param | Total number of floating-point sub model parameters |
n_sub_model_env_param | Total number of environment-dependent sub model parameters |
Definition at line 73 of file camp_solver.c.
|
static |
void solver_reset_timers | ( | void * | solver_data | ) |
Reset the timers for solver functions.
solver_data | Pointer to the SolverData object with timers to reset |
Definition at line 1625 of file camp_solver.c.
int solver_run | ( | void * | solver_data, |
double * | state, | ||
double * | env, | ||
double | t_initial, | ||
double | t_final | ||
) |
Solve for a given timestep.
solver_data | A pointer to the initialized solver data |
state | A pointer to the full state array (all grid cells) |
env | A pointer to the full array of environmental conditions (all grid cells) |
t_initial | Initial time (s) |
t_final | (s) |
Definition at line 534 of file camp_solver.c.