CAMP 1.0.0
Chemistry Across Multiple Phases
camp_common.h
Go to the documentation of this file.
1/* Copyright (C) 2021 Barcelona Supercomputing Center and University of
2 * Illinois at Urbana-Champaign
3 * SPDX-License-Identifier: MIT
4 *
5 * Header file for common constants and structures
6 *
7 */
8/** \file
9 * \brief Header file for common constants and structures
10 */
11#ifndef CAMP_COMMON_H
12#define CAMP_COMMON_H
13
14#include <stdbool.h>
15#include <time.h>
16#include "Jacobian.h"
17#include "time_derivative.h"
18
19/* SUNDIALS Header files with a description of contents used */
20#ifdef CAMP_USE_SUNDIALS
21#include <cvode/cvode.h> /* Protoypes for CVODE fcts., consts. */
22#include <cvode/cvode_direct.h> /* CVDls interface */
23#ifdef CAMP_CUSTOM_CVODE
24# include <cvode/cvode_impl.h> /* CVodeMem structure */
25#endif
26#include <nvector/nvector_serial.h> /* Serial N_Vector types, fcts, macros */
27#include <sundials/sundials_math.h> /* SUNDIALS math function macros */
28#include <sundials/sundials_types.h> /* definition of types */
29#include <sunlinsol/sunlinsol_klu.h> /* KLU SUNLinearSolver */
30#include <sunmatrix/sunmatrix_sparse.h> /* sparse SUNMatrix */
31#endif
32
33// State variable types (Must match parameters defined in camp_chem_spec_data
34// module)
35#define CHEM_SPEC_UNKNOWN_TYPE 0
36#define CHEM_SPEC_VARIABLE 1
37#define CHEM_SPEC_CONSTANT 2
38#define CHEM_SPEC_PSSA 3
39#define CHEM_SPEC_ACTIVITY_COEFF 4
40
41/* Math constants */
42#define ZERO 0.0
43#define ONE 1.0
44#define HALF 0.5
45#define SMALL 1.0e-30
46#define TINY 1.0e-60
47#ifndef M_PI
48#define M_PI 3.14159265358979323846
49#endif
50
51/* Number of environmental parameters */
52#define CAMP_NUM_ENV_PARAM_ 2 // !!! Must match the value in camp_state.f90 !!!
53
54/* Jacobian map */
55typedef struct {
56 int solver_id; // solver Jacobian id
57 int rxn_id; // reaction Jacobian id
58 int param_id; // sub model Jacobian id
59} JacMap;
60
61/* Model data structure */
62typedef struct {
63 int n_per_cell_state_var; // number of state variables per grid cell
64 int n_per_cell_dep_var; // number of solver variables per grid cell
65 int n_per_cell_rxn_jac_elem; // number of potentially non-zero
66 // reaction Jacobian elements
67 int n_per_cell_param_jac_elem; // number of potentially non-zero
68 // parameter Jacobian elements
69 int n_per_cell_solver_jac_elem; // number of potentially non-zero
70 // solver Jacobian elements
71 int n_cells; // number of cells to compute simultaneously
72 double *abs_tol; // pointer to array of state variable absolute
73 // integration tolerances
74 int *var_type; // pointer to array of state variable types (solver,
75 // constant, PSSA)
76#ifdef CAMP_USE_SUNDIALS
77 SUNMatrix J_init; // sparse solver Jacobian matrix with used elements
78 // initialized to 1.0
79 SUNMatrix J_rxn; // Matrix for Jacobian contributions from reactions
80 SUNMatrix J_params; // Matrix for Jacobian contributions from sub model
81 // parameter calculations
82 SUNMatrix J_solver; // Solver Jacobian
83 N_Vector J_state; // Last state used to calculate the Jacobian
84 N_Vector J_deriv; // Last derivative used to calculate the Jacobian
85 N_Vector J_tmp; // Working vector (size of J_state and J_deriv)
86 N_Vector J_tmp2; // Working vector (size of J_state and J_deriv)
87#endif
88 JacMap *jac_map; // Array of Jacobian mapping elements
89 JacMap *jac_map_params; // Array of Jacobian mapping elements to account for
90 // sub-model interdependence. If sub-model parameter
91 // i_dep depends on sub-model parameter i_ind, and
92 // j_ind is a dependency (variable or parameter) of
93 // i_ind, then:
94 // solver_id = jac_id[i_dep][j_ind]
95 // rxn_id = jac_id[i_dep][i_ind]
96 // param_id = jac_id[i_ind][j_ind]
97 int n_mapped_values; // Number of Jacobian map elements
98 int n_mapped_params; // Number of Jacobian map elements for sub models
99
100 int grid_cell_id; // Index of the current grid cell
101 double *grid_cell_state; // Pointer to the current grid cell being solved
102 // on the total_state array
103 double *total_state; // Total (multi-cell) state array
104 double *grid_cell_env; // Pointer to the current grid cell being solved
105 // on the total_env state array
106 double *total_env; // Total (multi-cell) environmental state array
107 double *grid_cell_rxn_env_data; // Environment-dependent parameters for the
108 // current grid cell
109 double *rxn_env_data; // Total (multi-cell) reaction environment-
110 // dependent parameters
112 // Environment-dependent parameters for the
113 // current grid cell
114 double *aero_rep_env_data; // Total (multi-cell) aerosol representation
115 // environment-dependent parameters
117 // Environment-dependent parameters for the
118 // current grid cell
119 double *sub_model_env_data; // Total (multi-cell) sub-model environment-
120 // dependent parameters
121
122 int n_rxn; // Number of reactions
123 int n_added_rxns; // The number of reactions whose data has been
124 // added to the reaction data arrays
125 int *rxn_int_data; // Pointer to the reaction integer parameters
126 double *rxn_float_data; // Pointer to the reaction floating-point
127 // parameters
128 int *rxn_int_indices; // Array of indices of integer data
129 int *rxn_float_indices; // Array of indices of float data
130 int *rxn_env_idx; // Array of offsets for the environment-
131 // dependent data for each reaction from the
132 // beginning of the environmental dependent data
133 // for the current grid cell
134 int n_rxn_env_data; // Number of reaction environmental parameters
135 // from all reactions
136 int n_aero_phase; // Number of aerosol phases
137 int n_added_aero_phases; // The number of aerosol phases whose data has
138 // been added to the aerosol phase data arrays
139 int *aero_phase_int_data; // Pointer to the aerosol phase integer parameters
140 double *aero_phase_float_data; // Pointer to the aerosol phase floating-point
141 // parameters
142 int *aero_phase_int_indices; // Array of indices of integer data
143 int *aero_phase_float_indices; // Array of indices of float data
144 int n_aero_rep; // Number of aerosol representations
145 int n_added_aero_reps; // The number of aerosol representations whose
146 // data has been added to the aerosol
147 // representation data arrays
148 int *aero_rep_int_data; // Pointer to the aerosol representation integer
149 // parameters
150 double *aero_rep_float_data; // Pointer to the aerosol representation
151 // floating-point parameters
152 int *aero_rep_int_indices; // Array of indices of integer data
153 int *aero_rep_float_indices; // Array of indices of float data
154 int *aero_rep_env_idx; // Array of offsets for the environment-
155 // dependent data for each aerosol representation
156 // from the beginning of the environment-
157 // dependent data for the current grid cell
158 int n_aero_rep_env_data; // Number of aerosol representation environmental
159 // parameters for all aerosol representations
160 int n_sub_model; // Number of sub models
161 int n_added_sub_models; // The number of sub models whose data has been
162 // added to the sub model data arrays
163 int *sub_model_int_data; // Pointer to sub model integer parameters
164 double
165 *sub_model_float_data; // Pointer to sub model floating-point parameters
166 int *sub_model_int_indices; // Array of indices of integer data
167 int *sub_model_float_indices; // Array of indices of float data
168 int *sub_model_env_idx; // Array of offsets for the environment-
169 // dependent data for each sub model from the
170 // beginning of the environment-dependent data
171 // for the current grid cell
172 int n_sub_model_env_data; // Number of sub model environmental parameters
173 // from all sub models
174} ModelData;
175
176/* Solver data structure */
177typedef struct {
178#ifdef CAMP_USE_SUNDIALS
179 N_Vector abs_tol_nv; // abosolute tolerance vector
180 N_Vector y; // vector of solver variables
181 SUNLinearSolver ls; // linear solver
182 TimeDerivative time_deriv; // CAMP derivative structure for use in
183 // calculating deriv
184 Jacobian jac; // CAMP Jacobian structure for use in
185 // calculating the Jacobian
186 N_Vector deriv; // used to calculate the derivative outside the solver
187 SUNMatrix J; // Jacobian matrix
188 SUNMatrix J_guess; // Jacobian matrix for improving guesses sent to linear
189 // solver
190 bool curr_J_guess; // Flag indicating the Jacobian used by the guess helper
191 // is current
192 realtype J_guess_t; // Last time (t) for which J_guess was calculated
193 int Jac_eval_fails; // Number of Jacobian evaluation failures
194 int solver_flag; // Last flag returned by a call to CVode()
195 int output_precision; // Flag indicating whether to output precision loss
196 int use_deriv_est; // Flag indicating whether to use an estimated
197 // derivative in the f() calculations
198#ifdef CAMP_DEBUG
199 booleantype debug_out; // Output debugging information during solving
200 booleantype eval_Jac; // Evalute Jacobian data during solving
201 int counterDeriv; // Total calls to f()
202 int counterJac; // Total calls to Jac()
203 clock_t timeDeriv; // Compute time for calls to f()
204 clock_t timeJac; // Compute time for calls to Jac()
205 double
206 max_loss_precision; // Maximum loss of precision during last call to f()
207#endif
208#endif
209 void *cvode_mem; // CVodeMem object
210 ModelData model_data; // Model data (used during initialization and solving)
211 bool no_solve; // Flag to indicate whether to run the solver needs to be
212 // run. Set to true when no reactions are present.
213 double init_time_step; // Initial time step (s)
214} SolverData;
215
216#endif
Header for the Jacobian structure and related functions.
int param_id
Definition camp_common.h:58
int rxn_id
Definition camp_common.h:57
int solver_id
Definition camp_common.h:56
int * rxn_int_indices
int * sub_model_int_data
int * aero_rep_env_idx
int * aero_phase_float_indices
int n_aero_rep_env_data
int * sub_model_int_indices
int n_per_cell_state_var
Definition camp_common.h:63
int n_added_aero_reps
int * var_type
Definition camp_common.h:74
double * rxn_env_data
int * aero_rep_int_indices
int * rxn_float_indices
double * total_env
N_Vector J_state
Definition camp_common.h:83
SUNMatrix J_rxn
Definition camp_common.h:79
int n_mapped_params
Definition camp_common.h:98
double * aero_rep_env_data
int n_added_sub_models
JacMap * jac_map
Definition camp_common.h:88
int n_per_cell_rxn_jac_elem
Definition camp_common.h:65
int * rxn_env_idx
int * aero_phase_int_indices
N_Vector J_tmp2
Definition camp_common.h:86
double * grid_cell_rxn_env_data
int n_sub_model
double * aero_rep_float_data
N_Vector J_deriv
Definition camp_common.h:84
double * grid_cell_env
N_Vector J_tmp
Definition camp_common.h:85
int n_rxn_env_data
SUNMatrix J_init
Definition camp_common.h:77
int * rxn_int_data
int n_per_cell_param_jac_elem
Definition camp_common.h:67
double * sub_model_float_data
int grid_cell_id
double * grid_cell_state
int n_added_rxns
int * aero_rep_float_indices
double * abs_tol
Definition camp_common.h:72
double * total_state
int n_per_cell_solver_jac_elem
Definition camp_common.h:69
int * aero_rep_int_data
double * rxn_float_data
double * aero_phase_float_data
double * grid_cell_aero_rep_env_data
SUNMatrix J_params
Definition camp_common.h:80
double * grid_cell_sub_model_env_data
int n_mapped_values
Definition camp_common.h:97
double * sub_model_env_data
JacMap * jac_map_params
Definition camp_common.h:89
SUNMatrix J_solver
Definition camp_common.h:82
int n_sub_model_env_data
int * sub_model_env_idx
int * aero_phase_int_data
int n_per_cell_dep_var
Definition camp_common.h:64
int n_added_aero_phases
int * sub_model_float_indices
int n_aero_phase
SUNLinearSolver ls
Jacobian jac
double init_time_step
SUNMatrix J
bool curr_J_guess
SUNMatrix J_guess
int output_precision
realtype J_guess_t
TimeDerivative time_deriv
int use_deriv_est
N_Vector deriv
N_Vector abs_tol_nv
ModelData model_data
int Jac_eval_fails
void * cvode_mem
N_Vector y
Header for the time derivative structure and related functions.