CAMP 1.0.0
Chemistry Across Multiple Phases
rxn_emission.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 * Emission reaction solver functions
6 *
7 */
8/** \file
9 * \brief Emission 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 SPECIES_ (int_data[1] - 1)
22#define DERIV_ID_ int_data[2]
23#define SCALING_ float_data[0]
24#define RATE_ (rxn_env_data[0])
25#define BASE_RATE_ (rxn_env_data[1])
26#define NUM_INT_PROP_ 3
27#define NUM_FLOAT_PROP_ 1
28#define NUM_ENV_PARAM_ 2
29
30/** \brief Flag Jacobian elements used by this reaction
31 *
32 * \param rxn_int_data Pointer to the reaction integer data
33 * \param rxn_float_data Pointer to the reaction floating-point data
34 * \param jac Jacobian
35 */
36void rxn_emission_get_used_jac_elem(int *rxn_int_data, double *rxn_float_data,
37 Jacobian *jac) {
38 int *int_data = rxn_int_data;
39 double *float_data = rxn_float_data;
40
41 return;
42}
43
44/** \brief Update the time derivative and Jacbobian array indices
45 *
46 * \param model_data Pointer to the model data
47 * \param deriv_ids Id of each state variable in the derivative array
48 * \param jac Jacobian
49 * \param rxn_int_data Pointer to the reaction integer data
50 * \param rxn_float_data Pointer to the reaction floating-point data
51 */
52void rxn_emission_update_ids(ModelData *model_data, int *deriv_ids,
53 Jacobian jac, int *rxn_int_data,
54 double *rxn_float_data) {
55 int *int_data = rxn_int_data;
56 double *float_data = rxn_float_data;
57
58 // Update the time derivative id
59 DERIV_ID_ = deriv_ids[SPECIES_];
60
61 return;
62}
63
64/** \brief Update reaction data
65 *
66 * Emission reactions can have their base (pre-scaling) rates updated from the
67 * host model based on the calculations of an external module. The structure
68 * of the update data is:
69 *
70 * - \b int rxn_id (Id of one or more emission reactions set by the
71 * host model using the
72 * \c camp_rxn_emission::rxn_emission_t::set_rxn_id
73 * function prior to initializing the solver.)
74 * - \b double rate (New pre-scaling rate.)
75 *
76 * \param update_data Pointer to the updated reaction data
77 * \param rxn_int_data Pointer to the reaction integer data
78 * \param rxn_float_data Pointer to the reaction floating-point data
79 * \param rxn_env_data Pointer to the environment-dependent data
80 * \return Flag indicating whether this is the reaction to update
81 */
82bool rxn_emission_update_data(void *update_data, int *rxn_int_data,
83 double *rxn_float_data, double *rxn_env_data) {
84 int *int_data = rxn_int_data;
85 double *float_data = rxn_float_data;
86
87 int *rxn_id = (int *)update_data;
88 double *base_rate = (double *)&(rxn_id[1]);
89
90 // Set the base emission rate for matching reactions
91 if (*rxn_id == RXN_ID_ && RXN_ID_ > 0) {
92 BASE_RATE_ = (double)*base_rate;
94 return true;
95 }
96
97 return false;
98}
99
100/** \brief Update reaction data for new environmental conditions
101 *
102 * For emission reactions this only involves recalculating the rate.
103 *
104 * \param model_data Pointer to the model data
105 * \param rxn_int_data Pointer to the reaction integer data
106 * \param rxn_float_data Pointer to the reaction floating-point data
107 * \param rxn_env_data Pointer to the environment-dependent parameters
108 */
109void rxn_emission_update_env_state(ModelData *model_data, int *rxn_int_data,
110 double *rxn_float_data,
111 double *rxn_env_data) {
112 int *int_data = rxn_int_data;
113 double *float_data = rxn_float_data;
114 double *env_data = model_data->grid_cell_env;
115
116 // Calculate the rate constant in (concentration_units/s)
118
119 return;
120}
121
122/** \brief Calculate contributions to the time derivative \f$f(t,y)\f$ from
123 * this reaction.
124 *
125 * \param model_data Pointer to the model data, including the state array
126 * \param time_deriv TimeDerivative object
127 * \param rxn_int_data Pointer to the reaction integer data
128 * \param rxn_float_data Pointer to the reaction floating-point data
129 * \param rxn_env_data Pointer to the environment-dependent parameters
130 * \param time_step Current time step being computed (s)
131 */
132#ifdef CAMP_USE_SUNDIALS
134 TimeDerivative time_deriv,
135 int *rxn_int_data, double *rxn_float_data,
136 double *rxn_env_data, realtype time_step) {
137 int *int_data = rxn_int_data;
138 double *float_data = rxn_float_data;
139 double *state = model_data->grid_cell_state;
140 double *env_data = model_data->grid_cell_env;
141
142 // Add contributions to the time derivative
143 if (DERIV_ID_ >= 0)
144 time_derivative_add_value(time_deriv, DERIV_ID_, (long double)RATE_);
145
146 return;
147}
148#endif
149
150/** \brief Calculate contributions to the Jacobian from this reaction
151 *
152 * \param model_data Pointer to the model data
153 * \param jac Reaction Jacobian
154 * \param rxn_int_data Pointer to the reaction integer data
155 * \param rxn_float_data Pointer to the reaction floating-point data
156 * \param rxn_env_data Pointer to the environment-dependent parameters
157 * \param time_step Current time step being calculated (s)
158 */
159#ifdef CAMP_USE_SUNDIALS
161 int *rxn_int_data, double *rxn_float_data,
162 double *rxn_env_data, realtype time_step) {
163 int *int_data = rxn_int_data;
164 double *float_data = rxn_float_data;
165 double *state = model_data->grid_cell_state;
166 double *env_data = model_data->grid_cell_env;
167
168 // No Jacobian contributions from 0th order emissions
169
170 return;
171}
172#endif
173
174/** \brief Print the reaction parameters
175 *
176 * \param rxn_int_data Pointer to the reaction integer data
177 * \param rxn_float_data Pointer to the reaction floating-point data
178 */
179void rxn_emission_print(int *rxn_int_data, double *rxn_float_data) {
180 int *int_data = rxn_int_data;
181 double *float_data = rxn_float_data;
182
183 printf("\n\nEmission reaction\n");
184
185 return;
186}
187
188/** \brief Create update data for new emission rates
189 *
190 * \return Pointer to a new rate update data object
191 */
193 int *update_data = (int *)malloc(sizeof(int) + sizeof(double));
194 if (update_data == NULL) {
195 printf("\n\nERROR allocating space for emission update data\n\n");
196 exit(1);
197 }
198 return (void *)update_data;
199}
200
201/** \brief Set rate update data
202 *
203 * \param update_data Pointer to an allocated rate update data object
204 * \param rxn_id Id of emission reactions to update
205 * \param base_rate New pre-scaling emission rate
206 */
207void rxn_emission_set_rate_update_data(void *update_data, int rxn_id,
208 double base_rate) {
209 int *new_rxn_id = (int *)update_data;
210 double *new_base_rate = (double *)&(new_rxn_id[1]);
211 *new_rxn_id = rxn_id;
212 *new_base_rate = base_rate;
213}
void rxn_emission_get_used_jac_elem(int *rxn_int_data, double *rxn_float_data, Jacobian *jac)
Flag Jacobian elements used by this reaction.
void rxn_emission_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_emission_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.
#define SCALING_
void rxn_emission_set_rate_update_data(void *update_data, int rxn_id, double base_rate)
Set rate update data.
void rxn_emission_print(int *rxn_int_data, double *rxn_float_data)
Print the reaction parameters.
bool rxn_emission_update_data(void *update_data, int *rxn_int_data, double *rxn_float_data, double *rxn_env_data)
Update reaction data.
#define BASE_RATE_
#define RXN_ID_
void rxn_emission_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 RATE_
#define DERIV_ID_
#define SPECIES_
void * rxn_emission_create_rate_update_data()
Create update data for new emission rates.
void rxn_emission_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.
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.