CAMP 1.0.0
Chemistry Across Multiple Phases
aero_rep_single_particle.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 * Single particle aerosol representation functions
6 *
7 */
8/** \file
9 * \brief Single particle aerosol representation functions
10 */
11#include <stdio.h>
12#include <stdlib.h>
13#include <camp/aero_phase_solver.h>
14#include <camp/aero_reps.h>
15#include <camp/camp_solver.h>
16
17// TODO Lookup environmental indicies during initialization
18#define TEMPERATURE_K_ env_data[0]
19#define PRESSURE_PA_ env_data[1]
20
21#define UPDATE_NUMBER 0
22
23#define NUM_LAYERS_ int_data[0]
24#define AERO_REP_ID_ int_data[1]
25#define MAX_PARTICLES_ int_data[2]
26#define PARTICLE_STATE_SIZE_ int_data[3]
27#define NUMBER_CONC_(x) aero_rep_env_data[x]
28#define NUM_INT_PROP_ 4
29#define NUM_FLOAT_PROP_ 0
30#define NUM_ENV_PARAM_ MAX_PARTICLES_
31#define LAYER_PHASE_START_(l) (int_data[NUM_INT_PROP_+l]-1)
32#define LAYER_PHASE_END_(l) (int_data[NUM_INT_PROP_+NUM_LAYERS_+l]-1)
33#define TOTAL_NUM_PHASES_ (LAYER_PHASE_END_(NUM_LAYERS_-1)-LAYER_PHASE_START_(0)+1)
34#define NUM_PHASES_(l) (LAYER_PHASE_END_(l)-LAYER_PHASE_START_(l)+1)
35#define PHASE_STATE_ID_(l,p) (int_data[NUM_INT_PROP_+2*NUM_LAYERS_+LAYER_PHASE_START_(l)+p]-1)
36#define PHASE_MODEL_DATA_ID_(l,p) (int_data[NUM_INT_PROP_+2*NUM_LAYERS_+TOTAL_NUM_PHASES_+LAYER_PHASE_START_(l)+p]-1)
37#define PHASE_NUM_JAC_ELEM_(l,p) int_data[NUM_INT_PROP_+2*NUM_LAYERS_+2*TOTAL_NUM_PHASES_+LAYER_PHASE_START_(l)+p]
38
39/** \brief Flag Jacobian elements used in calcualtions of mass and volume
40 *
41 * \param aero_rep_int_data Pointer to the aerosol representation integer data
42 * \param aero_rep_float_data Pointer to the aerosol representation
43 * floating-point data
44 * \param model_data Pointer to the model data
45 * \param aero_phase_idx Index of the aerosol phase to find elements for
46 * \param jac_struct 1D array of flags indicating potentially non-zero
47 * Jacobian elements. (The dependent variable should have
48 * been chosen by the calling function.)
49 * \return Number of Jacobian elements flagged
50 */
51
53 int aero_phase_idx,
54 int *aero_rep_int_data,
55 double *aero_rep_float_data,
56 bool *jac_struct) {
57 int *int_data = aero_rep_int_data;
58 double *float_data = aero_rep_float_data;
59 int n_jac_elem = 0;
60 int i_part = aero_phase_idx / TOTAL_NUM_PHASES_;
61
62 // Each phase in a single particle has the same jac elements
63 // (one for each species in each phase in the particle)
64 for (int i_layer = 0; i_layer < NUM_LAYERS_; ++i_layer) {
65 for (int i_phase = 0; i_phase < NUM_PHASES_(i_layer); ++i_phase) {
67 model_data, PHASE_MODEL_DATA_ID_(i_layer,i_phase),
68 i_part * PARTICLE_STATE_SIZE_ + PHASE_STATE_ID_(i_layer,i_phase), jac_struct);
69 n_jac_elem += PHASE_NUM_JAC_ELEM_(i_layer,i_phase);
70 }
71 }
72 return n_jac_elem;
73}
74
75/** \brief Flag elements on the state array used by this aerosol representation
76 *
77 * The single particle aerosol representation functions do not use state array
78 * values
79 *
80 * \param aero_rep_int_data Pointer to the aerosol representation integer data
81 * \param aero_rep_float_data Pointer to the aerosol representation
82 * floating-point data
83 * \param state_flags Array of flags indicating state array elements used
84 */
85
87 double *aero_rep_float_data,
88 bool *state_flags) {
89 int *int_data = aero_rep_int_data;
90 double *float_data = aero_rep_float_data;
91
92 return;
93}
94
95/** \brief Update aerosol representation data for new environmental conditions
96 *
97 * The single particle aerosol representation does not use environmental
98 * conditions
99 *
100 * \param model_data Pointer to the model data
101 * \param aero_rep_int_data Pointer to the aerosol representation integer data
102 * \param aero_rep_float_data Pointer to the aerosol representation
103 * floating-point data
104 * \param aero_rep_env_data Pointer to the aerosol representation
105 * environment-dependent parameters
106 */
107
109 int *aero_rep_int_data,
110 double *aero_rep_float_data,
111 double *aero_rep_env_data) {
112 int *int_data = aero_rep_int_data;
113 double *float_data = aero_rep_float_data;
114 double *env_data = model_data->grid_cell_env;
115
116 return;
117}
118
119/** \brief Update aerosol representation data for a new state
120 *
121 * \param model_data Pointer to the model data, include the state array
122 * \param aero_rep_int_data Pointer to the aerosol representation integer data
123 * \param aero_rep_float_data Pointer to the aerosol representation
124 * floating-point data
125 * \param aero_rep_env_data Pointer to the aerosol representation
126 * environment-dependent parameters
127 */
128
129void aero_rep_single_particle_update_state(ModelData *model_data,
130 int *aero_rep_int_data,
131 double *aero_rep_float_data,
132 double *aero_rep_env_data) {
133 int *int_data = aero_rep_int_data;
134 double *float_data = aero_rep_float_data;
135
136 return;
137}
138
139/** \brief Get the effective radius of a specified layer \f$r_{layer}\f$ (m)
140 *
141 * \param model_data Pointer to the model data, including the state array
142 * \param aero_phase_idx Index of the aerosol phase within the representation
143 * \param layer_radius Effective layer radius (m)
144 * \param partial_deriv \f$\frac{\partial r_{eff}}{\partial y}\f$ where \f$y\f$
145 * are species on the state array
146 * \param aero_rep_int_data Pointer to the aerosol representation integer data
147 * \param aero_rep_float_data Pointer to the aerosol representation
148 * floating-point data
149 * \param aero_rep_env_data Pointer to the aerosol representation
150 * environment-dependent parameters
151 */
152
154 ModelData *model_data, int aero_phase_idx, double *layer_radius,
155 double *partial_deriv, int *aero_rep_int_data, double *aero_rep_float_data,
156 double *aero_rep_env_data) {
157
158 int *int_data = aero_rep_int_data;
159 double *float_data = aero_rep_float_data;
160 int i_part = aero_phase_idx / TOTAL_NUM_PHASES_;
161 double *curr_partial = NULL;
162 int aero_phase_idx_temp = aero_phase_idx;
163 aero_phase_idx_temp -= i_part * TOTAL_NUM_PHASES_;
164
165 int i_layer_radius = -1;
166 for (int i_layer = 0; i_layer < NUM_LAYERS_; ++i_layer) {
167 if (LAYER_PHASE_START_(i_layer) <= aero_phase_idx_temp &&
168 aero_phase_idx_temp <= LAYER_PHASE_END_(i_layer)) {
169 i_layer_radius = i_layer;
170 break;
171 }
172 }
173
174 *layer_radius = 0.0;
175 if (partial_deriv) curr_partial = partial_deriv;
176 for (int i_layer = 0; i_layer <= i_layer_radius; ++i_layer) {
177 for (int i_phase = 0; i_phase < NUM_PHASES_(i_layer); ++i_phase) {
178 double *state = (double *)(model_data->grid_cell_state);
179 state += i_part * PARTICLE_STATE_SIZE_ + PHASE_STATE_ID_(i_layer,i_phase);
180 double volume;
181 aero_phase_get_volume__m3_m3(model_data, PHASE_MODEL_DATA_ID_(i_layer,i_phase),
182 state, &(volume), curr_partial);
183 if (partial_deriv) curr_partial += PHASE_NUM_JAC_ELEM_(i_layer,i_phase);
184 *layer_radius += volume;
185 }
186 }
187 *layer_radius = pow(((*layer_radius) * 3.0 / 4.0 / 3.14159265359), 1.0 / 3.0);
188 if (!partial_deriv) return;
189 for (int i_layer = 0; i_layer <= i_layer_radius; ++i_layer) {
190 for (int i_phase = 0; i_phase < NUM_PHASES_(i_layer); ++i_phase) {
191 for (int i_spec = 0; i_spec < PHASE_NUM_JAC_ELEM_(i_layer,i_phase); ++i_spec) {
192 *partial_deriv =
193 1.0 / 4.0 / 3.14159265359 * pow(*layer_radius, -2.0) * (*partial_deriv);
194 ++partial_deriv;
195 }
196 }
197 }
198 return;
199}
200
201/** \brief Get the effective particle radius \f$r_{eff}\f$ (m)
202 * Finds the radius of the largest layer in specified particle.
203 *
204 * \param model_data Pointer to the model data, including the state array
205 * \param aero_phase_idx Index of the aerosol phase within the representation
206 * \param radius Effective particle radius (m)
207 * \param partial_deriv \f$\frac{\partial r_{eff}}{\partial y}\f$ where \f$y\f$
208 * are species on the state array
209 * \param aero_rep_int_data Pointer to the aerosol representation integer data
210 * \param aero_rep_float_data Pointer to the aerosol representation
211 * floating-point data
212 * \param aero_rep_env_data Pointer to the aerosol representation
213 * environment-dependent parameters
214 */
215
217 ModelData *model_data, int aero_phase_idx, double *radius,
218 double *partial_deriv, int *aero_rep_int_data, double *aero_rep_float_data,
219 double *aero_rep_env_data) {
220
221 int *int_data = aero_rep_int_data;
222 double *float_data = aero_rep_float_data;
223 double *curr_partial = NULL;
224
225 // Adjust aero_phase_idx to the last phase in the particle.
226 // Add 1 to aero_phase_idx/TOTAL_NUM_PHASES_ to account for c indexing starting at 0.
227 int offset = (TOTAL_NUM_PHASES_*((aero_phase_idx / TOTAL_NUM_PHASES_)+1)) - aero_phase_idx;
228 aero_phase_idx += offset;
230 model_data,
231 aero_phase_idx-1, // Adjusted to last phase in particle.
232 radius,
233 partial_deriv,
234 int_data,
235 float_data,
236 aero_rep_env_data);
237
238 return;
239}
240
241/** \brief Get the surface area of specified particle layer \f$r_{eff}\f$ (m)
242 *
243 * Solve for the surface area of the interfacial layer that exists between the
244 * two phases considered in aerosol phase mass tranfer between layers. When more
245 * than one phase exists in a layer, a "fractional volume overlap" configuration
246 * is applied (see the single particle description in the Fortran script and
247 * CAMP Github Documentation for details).
248 *
249 * \param model_data Pointer to the model data, including the state array
250 * \param aero_phase_idx_first Index of the first aerosol phase within the representation
251 * \param aero_phase_idx_second Index of the second aerosol phase within the representation
252 * \param surface_area Pointer to surface area of inner layer (m^2)
253 * \param partial_deriv \f$\frac{\partial sa_{eff}}{\partial y}\f$ where \f$y\f$
254 * are species on the state array
255 * \param aero_rep_int_data Pointer to the aerosol representation integer data
256 * \param aero_rep_float_data Pointer to the aerosol representation
257 * floating-point data
258 * \param aero_rep_env_data Pointer to the aerosol representation
259 * environment-dependent parameters
260 * \param surface_area Surface area of specified layer (m2)
261 */
262
264 ModelData *model_data, int aero_phase_idx_first, int aero_phase_idx_second,
265 double *surface_area, double *partial_deriv,
266 int *aero_rep_int_data, double *aero_rep_float_data, double *aero_rep_env_data) {
267
268 int *int_data = aero_rep_int_data;
269 double *float_data = aero_rep_float_data;
270 double *curr_partial = NULL;
271 int layer_first = -1;
272 int layer_second = -1;
273 int layer_interface = -1;
274 int phase_model_data_id_first = -1;
275 int phase_model_data_id_second = -1;
276 double radius;
277 int i_part = aero_phase_idx_first / TOTAL_NUM_PHASES_;
278 aero_phase_idx_first -= i_part * TOTAL_NUM_PHASES_;
279 aero_phase_idx_second -= i_part * TOTAL_NUM_PHASES_;
280
281 // Find the layer each phase (first and second) exist in
282 int i_phase_count = 0;
283 for (int i_layer = 0; i_layer < NUM_LAYERS_; ++i_layer) {
284 for (int i_phase = 0; i_phase < NUM_PHASES_(i_layer); ++i_phase) {
285 if (LAYER_PHASE_START_(i_layer) <= aero_phase_idx_first &&
286 aero_phase_idx_first <= LAYER_PHASE_END_(i_layer) &&
287 i_phase_count == aero_phase_idx_first) {
288 layer_first = i_layer;
289 phase_model_data_id_first = PHASE_MODEL_DATA_ID_(i_layer, i_phase);
290 } else if (LAYER_PHASE_START_(i_layer) <= aero_phase_idx_second &&
291 aero_phase_idx_second <= LAYER_PHASE_END_(i_layer) &&
292 i_phase_count == aero_phase_idx_second) {
293 layer_second = i_layer;
294 phase_model_data_id_second = PHASE_MODEL_DATA_ID_(i_layer, i_phase);
295 }
296 ++i_phase_count;
297 }
298 }
299 // Find the interface between the first and second layer
300 layer_interface = layer_first > layer_second ? layer_second : layer_first;
301
302 /* Solve for the total volume, total volume of the layer with the first phase,
303 * total volume of the layer with the second phase, volume of first phase (within
304 * first layer) and volume of second phase (within second layer).
305 */
306 double interface_volume = 0.0;
307 double total_volume_layer_first = 0.0;
308 double total_volume_layer_second = 0.0;
309 double volume_phase_first = 0.0;
310 double volume_phase_second = 0.0;
311 i_phase_count = 0;
312 if (partial_deriv) curr_partial = partial_deriv;
313 for (int i_layer = 0; i_layer < NUM_LAYERS_; ++i_layer) {
314 for (int i_phase = 0; i_phase < NUM_PHASES_(i_layer); ++i_phase) {
315 double *state = (double *)(model_data->grid_cell_state);
316 state += PARTICLE_STATE_SIZE_ + PHASE_STATE_ID_(i_layer,i_phase);
317 double volume;
318 aero_phase_get_volume__m3_m3(model_data, PHASE_MODEL_DATA_ID_(i_layer,i_phase),
319 state, &(volume), curr_partial);
320 if (i_layer == layer_first) total_volume_layer_first += volume;
321 if (i_phase_count == aero_phase_idx_first &&
322 PHASE_MODEL_DATA_ID_(i_layer, i_phase) ==
323 phase_model_data_id_first) volume_phase_first = volume;
324 if (i_layer == layer_second) total_volume_layer_second += volume;
325 if (i_phase_count == aero_phase_idx_second &&
326 PHASE_MODEL_DATA_ID_(i_layer, i_phase) ==
327 phase_model_data_id_second) volume_phase_second = volume;
328 if (i_layer <= layer_interface) interface_volume += volume;
329 if (partial_deriv) curr_partial += PHASE_NUM_JAC_ELEM_(i_layer,i_phase);
330 ++i_phase_count;
331 }
332 }
333
334 /* Calculate the fractional volume of first and second phase in their
335 * assocaited layers. Calculate the radius and surface area of the interface
336 * between the first and second layer.
337 */
338 double f_first = volume_phase_first / total_volume_layer_first;
339 double f_second = volume_phase_second / total_volume_layer_second;
340 radius = pow(((interface_volume) * 3.0 / 4.0 / M_PI), 1.0 / 3.0);
341 *surface_area = f_first * f_second * 4 * M_PI * pow(radius, 2.0);
342
343
344 // Calculate the partial derivatives for each layer/phase combination.
345 if (!partial_deriv) return;
346 i_phase_count = 0;
347 for (int i_layer = 0; i_layer < NUM_LAYERS_; ++i_layer) {
348 for (int i_phase = 0; i_phase < NUM_PHASES_(i_layer); ++i_phase) {
349 double *state = (double *)(model_data->grid_cell_state);
350 state += PARTICLE_STATE_SIZE_ + PHASE_STATE_ID_(i_layer,i_phase);
351 double volume_phase;
352 aero_phase_get_volume__m3_m3(model_data, PHASE_MODEL_DATA_ID_(i_layer,i_phase),
353 state, &(volume_phase), NULL);
354 for (int i_spec = 0; i_spec < PHASE_NUM_JAC_ELEM_(i_layer,i_phase); ++i_spec) {
355 // layer = layer_first, phase = aero_phase_idx_first
356 if (i_layer == layer_first && i_phase_count == aero_phase_idx_first) {
357 *partial_deriv =
358 (((total_volume_layer_first - volume_phase_first) *
359 pow(total_volume_layer_first, -2.0) * f_second * (*surface_area)) +
360 2.0 * f_first * f_second * pow(radius, -1.0)) * (*partial_deriv);
361 ++partial_deriv;
362 }
363 // layer = layer_first, phase != aero_phase_idx_first
364 else if (i_layer == layer_first && i_phase_count != aero_phase_idx_first) {
365 *partial_deriv =
366 (((-1 * volume_phase) * pow(total_volume_layer_first, -2.0) *
367 f_second * (*surface_area)) + 2.0 * f_first * f_second *
368 pow(radius, -1.0)) * (*partial_deriv);
369 ++partial_deriv;
370 }
371 // layer = layer_second, phase = aero_phase_idx_second
372 else if (i_layer == layer_second && i_phase_count == aero_phase_idx_second) {
373 *partial_deriv =
374 (((total_volume_layer_second - volume_phase_second) *
375 pow(total_volume_layer_second, -2.0) * f_first * (*surface_area)) +
376 2.0 * f_first * f_second * pow(radius, -1.0)) * (*partial_deriv);
377 ++partial_deriv;
378 }
379 // layer = layer_second, phase != aero_phase_idx_second
380 else if (i_layer == layer_second && i_phase_count != aero_phase_idx_second) {
381 *partial_deriv =
382 (((-1 * volume_phase) * pow(total_volume_layer_second, -2.0) *
383 f_first * (*surface_area)) + 2.0 * f_first * f_second *
384 pow(radius, -1.0)) * (*partial_deriv);
385 ++partial_deriv;
386 }
387 // Set partial_derivative = 0 for all other layers.
388 else if (i_layer != layer_first && i_layer != layer_second) {
389 *(partial_deriv++) = ZERO;
390 }
391 else {
392 printf("\n\nERROR No conditions met for surface area partial derivative.\n\n");
393 exit(1);
394 }
395 }
396 ++i_phase_count;
397 }
398 }
399 return;
400}
401
402/** \brief Get the thickness of a particle layer (m)
403 *
404 * \param model_data Pointer to the model data, including the state array
405 * \param aero_phase_idx Index of the aerosol phase within the representation
406 * \param layer_thickness Effective layer thickness (m)
407 * \param partial_deriv \f$\frac{\partial r_{eff}}{\partial y}\f$ where \f$y\f$
408 * are species on the state array
409 * \param aero_rep_int_data Pointer to the aerosol representation integer data
410 * \param aero_rep_float_data Pointer to the aerosol representation
411 * floating-point data
412 * \param aero_rep_env_data Pointer to the aerosol representation
413 * environment-dependent parameters
414 */
415
417 ModelData *model_data, int aero_phase_idx, double *layer_thickness,
418 double *partial_deriv, int *aero_rep_int_data, double *aero_rep_float_data,
419 double *aero_rep_env_data) {
420
421 int *int_data = aero_rep_int_data;
422 double *float_data = aero_rep_float_data;
423 int jac_size = PARTICLE_STATE_SIZE_;
424 double radius_inner, radius_outer;
425 int i_part = aero_phase_idx / TOTAL_NUM_PHASES_;
426 int aero_phase_idx_temp = aero_phase_idx;
427 aero_phase_idx_temp -= i_part * TOTAL_NUM_PHASES_;
428
429 // Temporary Jacobians
430 double *jac_inner = NULL;
431 double *jac_outer = NULL;
432
433 if (partial_deriv) {
434 jac_inner = (double *)calloc(jac_size, sizeof(double));
435 jac_outer = (double *)calloc(jac_size, sizeof(double));
436 }
437
438 int i_layer_inner = -1;
439 int i_layer_outer = -1;
440 int i_phase_inner = -1;
441 int i_phase_outer = -1;
442 for (int i_layer = 0; i_layer < NUM_LAYERS_; ++i_layer) {
443 if (LAYER_PHASE_START_(i_layer) <= aero_phase_idx_temp &&
444 aero_phase_idx_temp <= LAYER_PHASE_END_(i_layer)) {
445 i_layer_outer = i_layer;
446 if (i_layer - 1 >= 0 ) {
447 i_layer_inner = i_layer - 1;
448 } else if (i_layer - 1 < 0 ) {
449 i_layer_inner = i_layer;
450 }
451 }
452 }
453 int offset = aero_phase_idx_temp - (i_part * LAYER_PHASE_START_(i_layer_outer));
454 int aero_phase_idx_inner = -1;
455 if (i_layer_inner == i_layer_outer) {
456 aero_phase_idx_inner = aero_phase_idx;
457 } else {
458 aero_phase_idx_inner = aero_phase_idx - (offset+1);
459 }
460
462 model_data,
463 aero_phase_idx,
464 &radius_outer,
465 jac_outer,
466 int_data,
467 float_data,
468 aero_rep_env_data);
469
471 model_data,
472 aero_phase_idx_inner,
473 &radius_inner,
474 jac_inner,
475 int_data,
476 float_data,
477 aero_rep_env_data);
478
479 if (i_layer_inner == i_layer_outer) {
480 *layer_thickness = radius_inner;
481 } else {
482 *layer_thickness = radius_outer - radius_inner;
483 }
484
485 if (partial_deriv) {
486 for (int i = 0; i < jac_size; ++i) {
487 partial_deriv[i] = jac_outer[i] - jac_inner[i];
488 }
489 }
490
491 free(jac_inner);
492 free(jac_outer);
493 return;
494}
495
496/** \brief Get the particle number concentration \f$n\f$
497 * (\f$\mbox{\si{\#\per\cubic\metre}}\f$)
498 *
499 * This single particle number concentration is set by the aerosol model prior
500 * to solving the chemistry. Thus, all \f$\frac{\partial n}{\partial y}\f$ are
501 * zero. Also, there is only one set of particles in the single particle
502 * representation, so the phase index is not used.
503 *
504 * \param model_data Pointer to the model data, including the state array
505 * \param aero_phase_idx Index of the aerosol phase within the representation
506 * (not used)
507 * \param number_conc Particle number concentration, \f$n\f$
508 * (\f$\mbox{\si{\#\per\cubic\metre}}\f$)
509 * \param partial_deriv \f$\frac{\partial n}{\partial y}\f$ where \f$y\f$ are
510 * the species on the state array
511 * \param aero_rep_int_data Pointer to the aerosol representation integer data
512 * \param aero_rep_float_data Pointer to the aerosol representation
513 * floating-point data
514 * \param aero_rep_env_data Pointer to the aerosol representation
515 * environment-dependent parameters
516 */
517
519 ModelData *model_data, int aero_phase_idx, double *number_conc,
520 double *partial_deriv, int *aero_rep_int_data, double *aero_rep_float_data,
521 double *aero_rep_env_data) {
522
523
524 int *int_data = aero_rep_int_data;
525 double *float_data = aero_rep_float_data;
526 int i_part = aero_phase_idx / TOTAL_NUM_PHASES_;
527
528 *number_conc = NUMBER_CONC_(i_part);
529
530 if (partial_deriv) {
531 for (int i_layer = 0; i_layer < NUM_LAYERS_; ++i_layer) {
532 for (int i_phase = 0; i_phase < NUM_PHASES_(i_layer); ++i_phase) {
533 for (int i_spec = 0; i_spec < PHASE_NUM_JAC_ELEM_(i_layer,i_phase); ++i_spec)
534 *(partial_deriv++) = ZERO;
535 }
536 }
537 }
538 return;
539}
540
541/** \brief Get the type of aerosol concentration used.
542 *
543 * Single particle concentrations are per-particle.
544 *
545 * \param aero_phase_idx Index of the aerosol phase within the representation
546 * \param aero_conc_type Pointer to int that will hold the concentration type
547 * code (0 = per particle mass concentrations;
548 * 1 = total particle mass concentrations)
549 * \param aero_rep_int_data Pointer to the aerosol representation integer data
550 * \param aero_rep_float_data Pointer to the aerosol representation
551 * floating-point data
552 * \param aero_rep_env_data Pointer to the aerosol representation
553 * environment-dependent parameters
554 */
555
557 int *aero_conc_type,
558 int *aero_rep_int_data,
559 double *aero_rep_float_data,
560 double *aero_rep_env_data) {
561 int *int_data = aero_rep_int_data;
562 double *float_data = aero_rep_float_data;
563
564 *aero_conc_type = 0;
565
566 return;
567}
568
569/** \brief Get the total mass in an aerosol phase \f$m\f$
570 * (\f$\mbox{\si{\kilogram\per\cubic\metre}}\f$)
571 *
572 * The single particle mass is set for each new state as the sum of the masses
573 * of the aerosol phases that compose the particle
574 *
575 * \param model_data Pointer to the model data, including the state array
576 * \param aero_phase_idx Index of the aerosol phase within the representation
577 * \param aero_phase_mass Total mass in the aerosol phase, \f$m\f$
578 * (\f$\mbox{\si{\kilogram\per\cubic\metre}}\f$)
579 * \param partial_deriv \f$\frac{\partial m}{\partial y}\f$ where \f$y\f$ are
580 * the species on the state array
581 * \param aero_rep_int_data Pointer to the aerosol representation integer data
582 * \param aero_rep_float_data Pointer to the aerosol representation
583 * floating-point data
584 * \param aero_rep_env_data Pointer to the aerosol representation
585 * environment-dependent parameters
586 */
587
589 ModelData *model_data, int aero_phase_idx, double *aero_phase_mass,
590 double *partial_deriv, int *aero_rep_int_data, double *aero_rep_float_data,
591 double *aero_rep_env_data) {
592
593 int *int_data = aero_rep_int_data;
594 double *float_data = aero_rep_float_data;
595 int i_part = aero_phase_idx / TOTAL_NUM_PHASES_;
596 aero_phase_idx -= i_part * TOTAL_NUM_PHASES_;
597
598 int i_total_phase = 0;
599 for (int i_layer = 0; i_layer < NUM_LAYERS_; ++i_layer) {
600 for (int i_phase = 0; i_phase < NUM_PHASES_(i_layer); ++i_phase) {
601 if ( i_total_phase == aero_phase_idx ) {
602 double *state = (double *)(model_data->grid_cell_state);
603 state += i_part * PARTICLE_STATE_SIZE_ + PHASE_STATE_ID_(i_layer,i_phase);
604 double mw;
605 aero_phase_get_mass__kg_m3(model_data, PHASE_MODEL_DATA_ID_(i_layer,i_phase),
606 state, aero_phase_mass, &mw, partial_deriv, NULL);
607 if (partial_deriv) partial_deriv += PHASE_NUM_JAC_ELEM_(i_layer,i_phase);
608 } else if (partial_deriv) {
609 for (int i_spec = 0; i_spec < PHASE_NUM_JAC_ELEM_(i_layer,i_phase); ++i_spec)
610 *(partial_deriv++) = ZERO;
611 }
612 ++i_total_phase;
613 }
614 }
615 return;
616}
617
618/** \brief Get the average molecular weight in an aerosol phase
619 ** \f$m\f$ (\f$\mbox{\si{\kilo\gram\per\mol}}\f$)
620 *
621 * The single particle mass is set for each new state as the sum of the masses
622 * of the aerosol phases that compose the particle
623 *
624 * \param model_data Pointer to the model data, including the state array
625 * \param aero_phase_idx Index of the aerosol phase within the representation
626 * \param aero_phase_avg_MW Average molecular weight in the aerosol phase
627 * (\f$\mbox{\si{\kilogram\per\mole}}\f$)
628 * \param partial_deriv \f$\frac{\partial m}{\partial y}\f$ where \f$y\f$ are
629 * the species on the state array
630 * \param aero_rep_int_data Pointer to the aerosol representation integer data
631 * \param aero_rep_float_data Pointer to the aerosol representation
632 * floating-point data
633 * \param aero_rep_env_data Pointer to the aerosol representation
634 * environment-dependent parameters
635 */
636
638 ModelData *model_data, int aero_phase_idx, double *aero_phase_avg_MW,
639 double *partial_deriv, int *aero_rep_int_data, double *aero_rep_float_data,
640 double *aero_rep_env_data) {
641
642 int *int_data = aero_rep_int_data;
643 double *float_data = aero_rep_float_data;
644 int i_part = aero_phase_idx / TOTAL_NUM_PHASES_;
645 aero_phase_idx -= i_part * TOTAL_NUM_PHASES_;
646
647 int i_total_phase = 0;
648 for (int i_layer = 0; i_layer < NUM_LAYERS_; ++i_layer) {
649 for (int i_phase = 0; i_phase < NUM_PHASES_(i_layer); ++i_phase) {
650 if ( i_total_phase == aero_phase_idx ) {
651 double *state = (double *)(model_data->grid_cell_state);
652 state += i_part * PARTICLE_STATE_SIZE_ + PHASE_STATE_ID_(i_layer,i_phase);
653 double mass;
654 aero_phase_get_mass__kg_m3(model_data, PHASE_MODEL_DATA_ID_(i_layer,i_phase),
655 state, &mass, aero_phase_avg_MW, NULL, partial_deriv);
656 if (partial_deriv) partial_deriv += PHASE_NUM_JAC_ELEM_(i_layer,i_phase);
657 } else if (partial_deriv) {
658 for (int i_spec = 0; i_spec < PHASE_NUM_JAC_ELEM_(i_layer,i_phase); ++i_spec)
659 *(partial_deriv++) = ZERO;
660 }
661 ++i_total_phase;
662 }
663 }
664 return;
665}
666
667/** \brief Update aerosol representation data
668 *
669 * Single particle aerosol representation update data is structured as follows:
670 *
671 * - \b int aero_rep_id (Id of one or more aerosol representations set by the
672 * host model using the
673 * camp_aero_rep_single_particle::aero_rep_single_particle_t::set_id
674 * function prior to initializing the solver.)
675 * - \b int update_type (Type of update to perform. Can be UPDATE_NUMBER
676 * only.)
677 * - \b double new_value (Either the new radius (m) or the new number
678 * concentration (\f$\mbox{\si{\#\per\cubic\centi\metre}}\f$).)
679 *
680 * \param update_data Pointer to the updated aerosol representation data
681 * \param aero_rep_int_data Pointer to the aerosol representation integer data
682 * \param aero_rep_float_data Pointer to the aerosol representation
683 * floating-point data
684 * \param aero_rep_env_data Pointer to the aerosol representation
685 * environment-dependent parameters
686 * \return Flag indicating whether this is the aerosol representation to update
687 */
688
690 int *aero_rep_int_data,
691 double *aero_rep_float_data,
692 double *aero_rep_env_data) {
693 int *int_data = aero_rep_int_data;
694 double *float_data = aero_rep_float_data;
695
696 int *aero_rep_id = (int *)update_data;
697 int *update_type = (int *)&(aero_rep_id[1]);
698 int *particle_id = (int *)&(update_type[1]);
699 double *new_value = (double *)&(update_type[2]);
700
701 // Set the new radius or number concentration for matching aerosol
702 // representations
703 if (*aero_rep_id == AERO_REP_ID_ && AERO_REP_ID_ != 0) {
704 if (*update_type == UPDATE_NUMBER) {
705 NUMBER_CONC_(*particle_id) = (double)*new_value;
706 return true;
707 }
708 }
709
710 return false;
711}
712
713/** \brief Print the Single Particle reaction parameters
714 *
715 * \param aero_rep_int_data Pointer to the aerosol representation integer data
716 * \param aero_rep_float_data Pointer to the aerosol representation
717 * floating-point data
718 */
719
720void aero_rep_single_particle_print(int *aero_rep_int_data,
721 double *aero_rep_float_data) {
722 int *int_data = aero_rep_int_data;
723 double *float_data = aero_rep_float_data;
724
725 printf("\n\nSingle particle aerosol representation\n");
726 printf("\nNumber of phases: %d", TOTAL_NUM_PHASES_);
727 printf("\nAerosol representation id: %d", AERO_REP_ID_);
728 printf("\nMax computational particles: %d", MAX_PARTICLES_);
729 printf("\nParticle state size: %d", PARTICLE_STATE_SIZE_);
730 for(int i_layer = 0; i_layer < NUM_LAYERS_; ++i_layer){
731 printf("\nLayer: %d", i_layer);
732 printf("\n Start phase: %d End phase: %d", LAYER_PHASE_START_(i_layer), LAYER_PHASE_END_(i_layer));
733 printf("\n Number of phases: %d", NUM_PHASES_(i_layer));
734 printf("\n\n - Phases -");
735 for (int i_phase = 0; i_phase < NUM_PHASES_(i_layer); ++i_phase) {
736 printf("\n state id: %d model data id: %d num Jac elements: %d",
737 PHASE_STATE_ID_(i_layer,i_phase), PHASE_MODEL_DATA_ID_(i_layer,i_phase),
738 PHASE_NUM_JAC_ELEM_(i_layer,i_phase));
739 }
740 }
741 printf("\n\nEnd single particle aerosol representation\n");
742 return;
743}
744
745
746/** \brief Create update data for new particle number
747 *
748 * \return Pointer to a new number update data object
749 */
751 int *update_data = (int *)malloc(3 * sizeof(int) + sizeof(double));
752 if (update_data == NULL) {
753 printf("\n\nERROR allocating space for number update data\n\n");
754 exit(1);
755 }
756 return (void *)update_data;
757}
758
759/** \brief Set number update data (#/m3)
760 *
761 * \param update_data Pointer to an allocated number update data object
762 * \param aero_rep_id Id of the aerosol representation(s) to update
763 * \param particle_id Id of the computational particle
764 * \param number_conc New particle number (#/m3)
765 */
767 int aero_rep_id,
768 int particle_id,
769 double number_conc) {
770 int *new_aero_rep_id = (int *)update_data;
771 int *update_type = (int *)&(new_aero_rep_id[1]);
772 int *new_particle_id = (int *)&(update_type[1]);
773 double *new_number_conc = (double *)&(update_type[2]);
774 *new_aero_rep_id = aero_rep_id;
775 *update_type = UPDATE_NUMBER;
776 *new_particle_id = particle_id;
777 *new_number_conc = number_conc;
778}
779
void aero_phase_get_volume__m3_m3(ModelData *model_data, int aero_phase_idx, double *state_var, double *volume, double *jac_elem)
Get the volume of an aerosol phase.
int aero_phase_get_used_jac_elem(ModelData *model_data, int aero_phase_idx, int state_var_id, bool *jac_struct)
Flag Jacobian elements used in calculations of mass and volume.
void aero_phase_get_mass__kg_m3(ModelData *model_data, int aero_phase_idx, double *state_var, double *mass, double *MW, double *jac_elem_mass, double *jac_elem_MW)
Get the mass and average MW in an aerosol phase.
int aero_rep_single_particle_get_used_jac_elem(ModelData *model_data, int aero_phase_idx, int *aero_rep_int_data, double *aero_rep_float_data, bool *jac_struct)
Flag Jacobian elements used in calcualtions of mass and volume.
void aero_rep_single_particle_get_aero_phase_avg_MW__kg_mol(ModelData *model_data, int aero_phase_idx, double *aero_phase_avg_MW, double *partial_deriv, int *aero_rep_int_data, double *aero_rep_float_data, double *aero_rep_env_data)
Get the average molecular weight in an aerosol phase ( )
#define NUM_PHASES_(l)
void aero_rep_single_particle_get_effective_radius__m(ModelData *model_data, int aero_phase_idx, double *radius, double *partial_deriv, int *aero_rep_int_data, double *aero_rep_float_data, double *aero_rep_env_data)
Get the effective particle radius (m) Finds the radius of the largest layer in specified particle.
#define PARTICLE_STATE_SIZE_
void aero_rep_single_particle_get_aero_conc_type(int aero_phase_idx, int *aero_conc_type, int *aero_rep_int_data, double *aero_rep_float_data, double *aero_rep_env_data)
Get the type of aerosol concentration used.
void * aero_rep_single_particle_create_number_update_data()
Create update data for new particle number.
#define PHASE_NUM_JAC_ELEM_(l, p)
void aero_rep_single_particle_get_interface_surface_area__m2(ModelData *model_data, int aero_phase_idx_first, int aero_phase_idx_second, double *surface_area, double *partial_deriv, int *aero_rep_int_data, double *aero_rep_float_data, double *aero_rep_env_data)
Get the surface area of specified particle layer (m)
#define AERO_REP_ID_
void aero_rep_single_particle_print(int *aero_rep_int_data, double *aero_rep_float_data)
Print the Single Particle reaction parameters.
#define MAX_PARTICLES_
#define LAYER_PHASE_START_(l)
#define PHASE_MODEL_DATA_ID_(l, p)
#define TOTAL_NUM_PHASES_
#define PHASE_STATE_ID_(l, p)
void aero_rep_single_particle_update_env_state(ModelData *model_data, int *aero_rep_int_data, double *aero_rep_float_data, double *aero_rep_env_data)
Update aerosol representation data for new environmental conditions.
void aero_rep_single_particle_get_layer_thickness__m(ModelData *model_data, int aero_phase_idx, double *layer_thickness, double *partial_deriv, int *aero_rep_int_data, double *aero_rep_float_data, double *aero_rep_env_data)
Get the thickness of a particle layer (m)
bool aero_rep_single_particle_update_data(void *update_data, int *aero_rep_int_data, double *aero_rep_float_data, double *aero_rep_env_data)
Update aerosol representation data.
void aero_rep_single_particle_get_dependencies(int *aero_rep_int_data, double *aero_rep_float_data, bool *state_flags)
Flag elements on the state array used by this aerosol representation.
#define UPDATE_NUMBER
#define NUM_LAYERS_
void aero_rep_single_particle_get_aero_phase_mass__kg_m3(ModelData *model_data, int aero_phase_idx, double *aero_phase_mass, double *partial_deriv, int *aero_rep_int_data, double *aero_rep_float_data, double *aero_rep_env_data)
Get the total mass in an aerosol phase ( )
#define LAYER_PHASE_END_(l)
void aero_rep_single_particle_get_effective_layer_radius__m(ModelData *model_data, int aero_phase_idx, double *layer_radius, double *partial_deriv, int *aero_rep_int_data, double *aero_rep_float_data, double *aero_rep_env_data)
Get the effective radius of a specified layer (m)
#define NUMBER_CONC_(x)
void aero_rep_single_particle_get_number_conc__n_m3(ModelData *model_data, int aero_phase_idx, double *number_conc, double *partial_deriv, int *aero_rep_int_data, double *aero_rep_float_data, double *aero_rep_env_data)
Get the particle number concentration ( )
void aero_rep_single_particle_set_number_update_data__n_m3(void *update_data, int aero_rep_id, int particle_id, double number_conc)
Set number update data (#/m3)
void aero_rep_single_particle_update_state(ModelData *model_data, int *aero_rep_int_data, double *aero_rep_float_data, double *aero_rep_env_data)
Update aerosol representation data for a new state.