CAMP 1.0.0
Chemistry Across Multiple Phases
rxn_wennberg_tunneling.c
Go to the documentation of this file.
1/* Copyright (C) 2021 Barcelona Supercomputing Center,
2 * University of Illinois at Urbana-Champaign, and
3 * National Center for Atmospheric Research
4 * SPDX-License-Identifier: MIT
5 *
6 * Wennberg tunneling reaction solver functions
7 *
8 */
9/** \file
10 * \brief Wennberg tunneling reaction solver functions
11 */
12#include <math.h>
13#include <stdio.h>
14#include <stdlib.h>
15#include "../rxns.h"
16
17// TODO Lookup environmental indices during initialization
18#define TEMPERATURE_K_ env_data[0]
19#define PRESSURE_PA_ env_data[1]
20
21#define NUM_REACT_ int_data[0]
22#define NUM_PROD_ int_data[1]
23#define A_ float_data[0]
24#define B_ float_data[1]
25#define C_ float_data[2]
26#define CONV_ float_data[3]
27#define RATE_CONSTANT_ rxn_env_data[0]
28#define NUM_INT_PROP_ 2
29#define NUM_FLOAT_PROP_ 4
30#define NUM_ENV_PARAM_ 1
31#define REACT_(x) (int_data[NUM_INT_PROP_ + x] - 1)
32#define PROD_(x) (int_data[NUM_INT_PROP_ + NUM_REACT_ + x] - 1)
33#define DERIV_ID_(x) int_data[NUM_INT_PROP_ + NUM_REACT_ + NUM_PROD_ + x]
34#define JAC_ID_(x) int_data[NUM_INT_PROP_ + 2 * (NUM_REACT_ + NUM_PROD_) + x]
35#define YIELD_(x) float_data[NUM_FLOAT_PROP_ + x]
36
37/** \brief Flag Jacobian elements used by this reaction
38 *
39 * \param rxn_int_data Pointer to the reaction integer data
40 * \param rxn_float_data Pointer to the reaction floating-point data
41 * \param jac Jacobian
42 */
44 double *rxn_float_data,
45 Jacobian *jac) {
46 int *int_data = rxn_int_data;
47 double *float_data = rxn_float_data;
48
49 for (int i_ind = 0; i_ind < NUM_REACT_; i_ind++) {
50 for (int i_dep = 0; i_dep < NUM_REACT_; i_dep++) {
51 jacobian_register_element(jac, REACT_(i_dep), REACT_(i_ind));
52 }
53 for (int i_dep = 0; i_dep < NUM_PROD_; i_dep++) {
54 jacobian_register_element(jac, PROD_(i_dep), REACT_(i_ind));
55 }
56 }
57
58 return;
59}
60
61/** \brief Update the time derivative and Jacbobian array indices
62 *
63 * \param model_data Pointer to the model data
64 * \param deriv_ids Id of each state variable in the derivative array
65 * \param jac Jacobian
66 * \param rxn_int_data Pointer to the reaction integer data
67 * \param rxn_float_data Pointer to the reaction floating-point data
68 */
69void rxn_wennberg_tunneling_update_ids(ModelData *model_data, int *deriv_ids,
70 Jacobian jac, int *rxn_int_data,
71 double *rxn_float_data) {
72 int *int_data = rxn_int_data;
73 double *float_data = rxn_float_data;
74
75 // Update the time derivative ids
76 for (int i = 0; i < NUM_REACT_; i++) DERIV_ID_(i) = deriv_ids[REACT_(i)];
77 for (int i = 0; i < NUM_PROD_; i++)
78 DERIV_ID_(i + NUM_REACT_) = deriv_ids[PROD_(i)];
79
80 // Update the Jacobian ids
81 int i_jac = 0;
82 for (int i_ind = 0; i_ind < NUM_REACT_; i_ind++) {
83 for (int i_dep = 0; i_dep < NUM_REACT_; i_dep++)
84 JAC_ID_(i_jac++) =
85 jacobian_get_element_id(jac, REACT_(i_dep), REACT_(i_ind));
86 for (int i_dep = 0; i_dep < NUM_PROD_; i_dep++)
87 JAC_ID_(i_jac++) =
88 jacobian_get_element_id(jac, PROD_(i_dep), REACT_(i_ind));
89 }
90 return;
91}
92
93/** \brief Update reaction data for new environmental conditions
94 *
95 * For Wennberg tunneling reaction this only involves recalculating the rate
96 * constant.
97 *
98 * \param model_data Pointer to the model data
99 * \param rxn_int_data Pointer to the reaction integer data
100 * \param rxn_float_data Pointer to the reaction floating-point data
101 * \param rxn_env_data Pointer to the environment-dependent parameters
102 */
104 int *rxn_int_data,
105 double *rxn_float_data,
106 double *rxn_env_data) {
107 int *int_data = rxn_int_data;
108 double *float_data = rxn_float_data;
109 double *env_data = model_data->grid_cell_env;
110
111 // Calculate the rate constant in (#/cc)
112 // k = A * exp(-B/T) * exp(C/T^3)
113 RATE_CONSTANT_ = A_ * exp(-B_ / TEMPERATURE_K_) *
114 exp(C_ / pow(TEMPERATURE_K_, 3)) *
116
117 return;
118}
119
120/** \brief Calculate contributions to the time derivative \f$f(t,y)\f$ from
121 * this reaction.
122 *
123 * \param model_data Model data
124 * \param time_deriv TimeDerivative object
125 * \param rxn_int_data Pointer to the reaction integer data
126 * \param rxn_float_data Pointer to the reaction floating-point data
127 * \param rxn_env_data Pointer to the environment-dependent parameters
128 * \param time_step Current time step being computed (s)
129 */
130#ifdef CAMP_USE_SUNDIALS
132 ModelData *model_data, TimeDerivative time_deriv, int *rxn_int_data,
133 double *rxn_float_data, double *rxn_env_data, double time_step) {
134 int *int_data = rxn_int_data;
135 double *float_data = rxn_float_data;
136 double *state = model_data->grid_cell_state;
137 double *env_data = model_data->grid_cell_env;
138
139 // Calculate the reaction rate
140 long double rate = RATE_CONSTANT_;
141 for (int i_spec = 0; i_spec < NUM_REACT_; i_spec++)
142 rate *= state[REACT_(i_spec)];
143
144 // Add contributions to the time derivative
145 if (rate != ZERO) {
146 int i_dep_var = 0;
147 for (int i_spec = 0; i_spec < NUM_REACT_; i_spec++, i_dep_var++) {
148 if (DERIV_ID_(i_dep_var) < 0) continue;
149 time_derivative_add_value(time_deriv, DERIV_ID_(i_dep_var), -rate);
150 }
151 for (int i_spec = 0; i_spec < NUM_PROD_; i_spec++, i_dep_var++) {
152 if (DERIV_ID_(i_dep_var) < 0) continue;
153
154 // Negative yields are allowed, but prevented from causing negative
155 // concentrations that lead to solver failures
156 if (-rate * YIELD_(i_spec) * time_step <= state[PROD_(i_spec)]) {
157 time_derivative_add_value(time_deriv, DERIV_ID_(i_dep_var),
158 rate * YIELD_(i_spec));
159 }
160 }
161 }
162
163 return;
164}
165#endif
166
167/** \brief Calculate contributions to the Jacobian from this reaction
168 *
169 * \param model_data Model data
170 * \param jac Reaction Jacobian
171 * \param rxn_int_data Pointer to the reaction integer data
172 * \param rxn_float_data Pointer to the reaction floating-point data
173 * \param rxn_env_data Pointer to the environment-dependent parameters
174 * \param time_step Current time step being calculated (s)
175 */
176#ifdef CAMP_USE_SUNDIALS
178 Jacobian jac, int *rxn_int_data,
179 double *rxn_float_data,
180 double *rxn_env_data,
181 double time_step) {
182 int *int_data = rxn_int_data;
183 double *float_data = rxn_float_data;
184 double *state = model_data->grid_cell_state;
185 double *env_data = model_data->grid_cell_env;
186
187 // Add contributions to the Jacobian
188 int i_elem = 0;
189 for (int i_ind = 0; i_ind < NUM_REACT_; i_ind++) {
190 // Calculate d_rate / d_i_ind
191 realtype rate = RATE_CONSTANT_;
192 for (int i_spec = 0; i_spec < NUM_REACT_; i_spec++)
193 if (i_spec != i_ind) rate *= state[REACT_(i_spec)];
194
195 for (int i_dep = 0; i_dep < NUM_REACT_; i_dep++, i_elem++) {
196 if (JAC_ID_(i_elem) < 0) continue;
197 jacobian_add_value(jac, (unsigned int)JAC_ID_(i_elem), JACOBIAN_LOSS,
198 rate);
199 }
200 for (int i_dep = 0; i_dep < NUM_PROD_; i_dep++, i_elem++) {
201 if (JAC_ID_(i_elem) < 0) continue;
202 // Negative yields are allowed, but prevented from causing negative
203 // concentrations that lead to solver failures
204 if (-rate * state[REACT_(i_ind)] * YIELD_(i_dep) * time_step <=
205 state[PROD_(i_dep)]) {
206 jacobian_add_value(jac, (unsigned int)JAC_ID_(i_elem),
207 JACOBIAN_PRODUCTION, YIELD_(i_dep) * rate);
208 }
209 }
210 }
211
212 return;
213}
214#endif
215
216/** \brief Print the Wennberg tunneling reaction parameters
217 *
218 * \param rxn_int_data Pointer to the reaction integer data
219 * \param rxn_float_data Pointer to the reaction floating-point data
220 */
221void rxn_wennberg_tunneling_print(int *rxn_int_data, double *rxn_float_data) {
222 int *int_data = rxn_int_data;
223 double *float_data = rxn_float_data;
224
225 printf("\n\nWennberg tunneling reaction\n");
226
227 return;
228}
unsigned int jacobian_get_element_id(Jacobian jac, unsigned int dep_id, unsigned int ind_id)
Get an element id in the Jacobian data arrays.
Definition Jacobian.c:200
void jacobian_add_value(Jacobian jac, unsigned int elem_id, unsigned int prod_or_loss, long double jac_contribution)
Add a contribution to the Jacobian.
Definition Jacobian.c:234
void jacobian_register_element(Jacobian *jac, unsigned int dep_id, unsigned int ind_id)
Adds an element to the sparse matrix.
Definition Jacobian.c:105
#define JACOBIAN_LOSS
Definition Jacobian.h:19
#define JACOBIAN_PRODUCTION
Definition Jacobian.h:18
#define ZERO
Definition camp_common.h:42
#define NUM_REACT_
#define PRESSURE_PA_
void rxn_wennberg_tunneling_update_ids(ModelData *model_data, int *deriv_ids, Jacobian jac, int *rxn_int_data, double *rxn_float_data)
Update the time derivative and Jacbobian array indices.
void rxn_wennberg_tunneling_print(int *rxn_int_data, double *rxn_float_data)
Print the Wennberg tunneling reaction parameters.
void rxn_wennberg_tunneling_calc_jac_contrib(ModelData *model_data, Jacobian jac, int *rxn_int_data, double *rxn_float_data, double *rxn_env_data, double time_step)
Calculate contributions to the Jacobian from this reaction.
#define RATE_CONSTANT_
#define A_
#define CONV_
#define B_
#define YIELD_(x)
#define PROD_(x)
#define TEMPERATURE_K_
#define JAC_ID_(x)
#define NUM_PROD_
#define C_
void rxn_wennberg_tunneling_update_env_state(ModelData *model_data, int *rxn_int_data, double *rxn_float_data, double *rxn_env_data)
Update reaction data for new environmental conditions.
void rxn_wennberg_tunneling_calc_deriv_contrib(ModelData *model_data, TimeDerivative time_deriv, int *rxn_int_data, double *rxn_float_data, double *rxn_env_data, double time_step)
Calculate contributions to the time derivative from this reaction.
#define REACT_(x)
void rxn_wennberg_tunneling_get_used_jac_elem(int *rxn_int_data, double *rxn_float_data, Jacobian *jac)
Flag Jacobian elements used by this reaction.
#define DERIV_ID_(x)
double * grid_cell_env
double * grid_cell_state
void time_derivative_add_value(TimeDerivative time_deriv, unsigned int spec_id, long double rate_contribution)
Add a contribution to the time derivative.