CAMP 1.0.0
Chemistry Across Multiple Phases
rxn_first_order_loss.c
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 * First-Order loss reaction solver functions
6 *
7 */
8/** \file
9 * \brief First-Order loss reaction solver functions
10 */
11#include <math.h>
12#include <stdio.h>
13#include <stdlib.h>
14#include "../rxns.h"
15
16// TODO Lookup environmental indicies during initialization
17#define TEMPERATURE_K_ env_data[0]
18#define PRESSURE_PA_ env_data[1]
19
20#define RXN_ID_ (int_data[0])
21#define REACT_ (int_data[1] - 1)
22#define DERIV_ID_ int_data[2]
23#define JAC_ID_ int_data[3]
24#define SCALING_ float_data[0]
25#define RATE_CONSTANT_ (rxn_env_data[0])
26#define BASE_RATE_ (rxn_env_data[1])
27#define NUM_INT_PROP_ 4
28#define NUM_FLOAT_PROP_ 1
29#define NUM_ENV_PARAM_ 2
30
31/** \brief Flag Jacobian elements used by this reaction
32 *
33 * \param rxn_int_data Pointer to the reaction integer data
34 * \param rxn_float_data Pointer to the reaction floating-point data
35 * \param jac Jacobian
36 */
38 double *rxn_float_data,
39 Jacobian *jac) {
40 int *int_data = rxn_int_data;
41 double *float_data = rxn_float_data;
42
44
45 return;
46}
47
48/** \brief Update the time derivative and Jacbobian array indices
49 *
50 * \param model_data Pointer to the model data
51 * \param deriv_ids Id of each state variable in the derivative array
52 * \param jac Jacobian
53 * \param rxn_int_data Pointer to the reaction integer data
54 * \param rxn_float_data Pointer to the reaction floating-point data
55 */
56void rxn_first_order_loss_update_ids(ModelData *model_data, int *deriv_ids,
57 Jacobian jac, int *rxn_int_data,
58 double *rxn_float_data) {
59 int *int_data = rxn_int_data;
60 double *float_data = rxn_float_data;
61
62 // Update the time derivative id
63 DERIV_ID_ = deriv_ids[REACT_];
64
65 // Update the Jacobian id
67
68 return;
69}
70
71/** \brief Update reaction data
72 *
73 * First-Order loss reactions can have their base (pre-scaling) rate constants
74 * updated from the host model based on the calculations of an external
75 * module. The structure of the update data is:
76 *
77 * - \b int rxn_id (Id of one or more first-order loss reactions set by the
78 * host model using the
79 * \c camp_rxn_first_order_loss::rxn_first_order_loss_t::set_rxn_id
80 * function prior to initializing the solver.)
81 * - \b double rate_const (New pre-scaling rate constant.)
82 *
83 * \param update_data Pointer to the updated reaction data
84 * \param rxn_int_data Pointer to the reaction integer data
85 * \param rxn_float_data Pointer to the reaction floating-point data
86 * \param rxn_env_data Pointer to the environment-dependent data
87 * \return Flag indicating whether this is the reaction to update
88 */
89bool rxn_first_order_loss_update_data(void *update_data, int *rxn_int_data,
90 double *rxn_float_data,
91 double *rxn_env_data) {
92 int *int_data = rxn_int_data;
93 double *float_data = rxn_float_data;
94
95 int *rxn_id = (int *)update_data;
96 double *base_rate = (double *)&(rxn_id[1]);
97
98 // Set the base first-order loss rate constants for matching reactions
99 if (*rxn_id == RXN_ID_ && RXN_ID_ > 0) {
100 BASE_RATE_ = (double)*base_rate;
102 return true;
103 }
104
105 return false;
106}
107
108/** \brief Update reaction data for new environmental conditions
109 *
110 * For first-order loss reactions this only involves recalculating the rate
111 * constant.
112 *
113 * \param model_data Pointer to the model data
114 * \param rxn_int_data Pointer to the reaction integer data
115 * \param rxn_float_data Pointer to the reaction floating-point data
116 * \param rxn_env_data Pointer to the environment-dependent parameters
117 */
119 int *rxn_int_data,
120 double *rxn_float_data,
121 double *rxn_env_data) {
122 int *int_data = rxn_int_data;
123 double *float_data = rxn_float_data;
124 double *env_data = model_data->grid_cell_env;
125
126 // Calculate the rate constant in (1/s)
128
129 return;
130}
131
132/** \brief Calculate contributions to the time derivative \f$f(t,y)\f$ from
133 * this reaction.
134 *
135 * \param model_data Pointer to the model data, including the state array
136 * \param time_deriv TimeDerivative object
137 * \param rxn_int_data Pointer to the reaction integer data
138 * \param rxn_float_data Pointer to the reaction floating-point data
139 * \param rxn_env_data Pointer to the environment-dependent parameters
140 * \param time_step Current time step being computed (s)
141 */
142#ifdef CAMP_USE_SUNDIALS
144 ModelData *model_data, TimeDerivative time_deriv, int *rxn_int_data,
145 double *rxn_float_data, double *rxn_env_data, realtype time_step) {
146 int *int_data = rxn_int_data;
147 double *float_data = rxn_float_data;
148 double *state = model_data->grid_cell_state;
149 double *env_data = model_data->grid_cell_env;
150
151 // Calculate the reaction rate
152 long double rate = RATE_CONSTANT_ * state[REACT_];
153
154 // Add contributions to the time derivative
155 if (DERIV_ID_ >= 0) time_derivative_add_value(time_deriv, DERIV_ID_, -rate);
156
157 return;
158}
159#endif
160
161/** \brief Calculate contributions to the Jacobian from this reaction
162 *
163 * \param model_data Pointer to the model data
164 * \param jac Reaction Jacobian
165 * \param rxn_int_data Pointer to the reaction integer data
166 * \param rxn_float_data Pointer to the reaction floating-point data
167 * \param rxn_env_data Pointer to the environment-dependent parameters
168 * \param time_step Current time step being calculated (s)
169 */
170#ifdef CAMP_USE_SUNDIALS
172 int *rxn_int_data,
173 double *rxn_float_data,
174 double *rxn_env_data,
175 realtype time_step) {
176 int *int_data = rxn_int_data;
177 double *float_data = rxn_float_data;
178 double *state = model_data->grid_cell_state;
179 double *env_data = model_data->grid_cell_env;
180
181 // Add contributions to the Jacobian
182 if (JAC_ID_ >= 0)
183 jacobian_add_value(jac, (unsigned int)JAC_ID_, JACOBIAN_LOSS,
185
186 return;
187}
188#endif
189
190/** \brief Print the reaction parameters
191 *
192 * \param rxn_int_data Pointer to the reaction integer data
193 * \param rxn_float_data Pointer to the reaction floating-point data
194 */
195void rxn_first_order_loss_print(int *rxn_int_data, double *rxn_float_data) {
196 int *int_data = rxn_int_data;
197 double *float_data = rxn_float_data;
198
199 printf("\n\nFirst-Order loss reaction\n");
200
201 return;
202}
203
204/** \brief Create update data for new first-order loss rates
205 *
206 * \return Pointer to a new rate update data object
207 */
209 int *update_data = (int *)malloc(sizeof(int) + sizeof(double));
210 if (update_data == NULL) {
211 printf("\n\nERROR allocating space for first-order loss update data\n\n");
212 exit(1);
213 }
214 return (void *)update_data;
215}
216
217/** \brief Set rate update data
218 *
219 * \param update_data Pointer to an allocated rate update data object
220 * \param rxn_id Id of first-order loss reactions to update
221 * \param base_rate New pre-scaling first-order loss rate
222 */
223void rxn_first_order_loss_set_rate_update_data(void *update_data, int rxn_id,
224 double base_rate) {
225 int *new_rxn_id = (int *)update_data;
226 double *new_base_rate = (double *)&(new_rxn_id[1]);
227 *new_rxn_id = rxn_id;
228 *new_base_rate = base_rate;
229}
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
void rxn_first_order_loss_calc_jac_contrib(ModelData *model_data, Jacobian jac, int *rxn_int_data, double *rxn_float_data, double *rxn_env_data, realtype time_step)
Calculate contributions to the Jacobian from this reaction.
#define SCALING_
#define REACT_
#define JAC_ID_
void rxn_first_order_loss_print(int *rxn_int_data, double *rxn_float_data)
Print the reaction parameters.
#define RATE_CONSTANT_
void rxn_first_order_loss_get_used_jac_elem(int *rxn_int_data, double *rxn_float_data, Jacobian *jac)
Flag Jacobian elements used by this reaction.
void rxn_first_order_loss_set_rate_update_data(void *update_data, int rxn_id, double base_rate)
Set rate update data.
#define BASE_RATE_
#define RXN_ID_
void * rxn_first_order_loss_create_rate_update_data()
Create update data for new first-order loss rates.
void rxn_first_order_loss_calc_deriv_contrib(ModelData *model_data, TimeDerivative time_deriv, int *rxn_int_data, double *rxn_float_data, double *rxn_env_data, realtype time_step)
Calculate contributions to the time derivative from this reaction.
void rxn_first_order_loss_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.
#define DERIV_ID_
void rxn_first_order_loss_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.
bool rxn_first_order_loss_update_data(void *update_data, int *rxn_int_data, double *rxn_float_data, double *rxn_env_data)
Update reaction data.
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.