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 */
261
263 ModelData *model_data, int aero_phase_idx_first, int aero_phase_idx_second,
264 double *surface_area, double *partial_deriv,
265 int *aero_rep_int_data, double *aero_rep_float_data, double *aero_rep_env_data) {
266
267 int *int_data = aero_rep_int_data;
268 double *float_data = aero_rep_float_data;
269 double *curr_partial = NULL;
270 int layer_first = -1;
271 int layer_second = -1;
272 int layer_interface = -1;
273 int phase_model_data_id_first = -1;
274 int phase_model_data_id_second = -1;
275 double radius;
276 int i_part = aero_phase_idx_first / TOTAL_NUM_PHASES_;
277 aero_phase_idx_first -= i_part * TOTAL_NUM_PHASES_;
278 aero_phase_idx_second -= i_part * TOTAL_NUM_PHASES_;
279
280 // Find the layer each phase (first and second) exist in
281 int i_phase_count = 0;
282 for (int i_layer = 0; i_layer < NUM_LAYERS_; ++i_layer) {
283 for (int i_phase = 0; i_phase < NUM_PHASES_(i_layer); ++i_phase) {
284 if (LAYER_PHASE_START_(i_layer) <= aero_phase_idx_first &&
285 aero_phase_idx_first <= LAYER_PHASE_END_(i_layer) &&
286 i_phase_count == aero_phase_idx_first) {
287 layer_first = i_layer;
288 phase_model_data_id_first = PHASE_MODEL_DATA_ID_(i_layer, i_phase);
289 } else if (LAYER_PHASE_START_(i_layer) <= aero_phase_idx_second &&
290 aero_phase_idx_second <= LAYER_PHASE_END_(i_layer) &&
291 i_phase_count == aero_phase_idx_second) {
292 layer_second = i_layer;
293 phase_model_data_id_second = PHASE_MODEL_DATA_ID_(i_layer, i_phase);
294 }
295 ++i_phase_count;
296 }
297 }
298 // Find the interface between the first and second layer
299 layer_interface = layer_first > layer_second ? layer_second : layer_first;
300
301 /* Solve for the total volume, total volume of the layer with the first phase,
302 * total volume of the layer with the second phase, volume of first phase (within
303 * first layer) and volume of second phase (within second layer).
304 */
305 double interface_volume = 0.0;
306 double total_volume_layer_first = 0.0;
307 double total_volume_layer_second = 0.0;
308 double volume_phase_first = 0.0;
309 double volume_phase_second = 0.0;
310 i_phase_count = 0;
311 if (partial_deriv) curr_partial = partial_deriv;
312 for (int i_layer = 0; i_layer < NUM_LAYERS_; ++i_layer) {
313 for (int i_phase = 0; i_phase < NUM_PHASES_(i_layer); ++i_phase) {
314 double *state = (double *)(model_data->grid_cell_state);
315 state += PARTICLE_STATE_SIZE_ + PHASE_STATE_ID_(i_layer,i_phase);
316 double volume;
317 aero_phase_get_volume__m3_m3(model_data, PHASE_MODEL_DATA_ID_(i_layer,i_phase),
318 state, &(volume), curr_partial);
319 if (i_layer == layer_first) total_volume_layer_first += volume;
320 if (i_phase_count == aero_phase_idx_first &&
321 PHASE_MODEL_DATA_ID_(i_layer, i_phase) ==
322 phase_model_data_id_first) volume_phase_first = volume;
323 if (i_layer == layer_second) total_volume_layer_second += volume;
324 if (i_phase_count == aero_phase_idx_second &&
325 PHASE_MODEL_DATA_ID_(i_layer, i_phase) ==
326 phase_model_data_id_second) volume_phase_second = volume;
327 if (i_layer <= layer_interface) interface_volume += volume;
328 if (partial_deriv) curr_partial += PHASE_NUM_JAC_ELEM_(i_layer,i_phase);
329 ++i_phase_count;
330 }
331 }
332
333 /* Calculate the fractional volume of first and second phase in their
334 * assocaited layers. Calculate the radius and surface area of the interface
335 * between the first and second layer.
336 */
337 double f_first = volume_phase_first / total_volume_layer_first;
338 double f_second = volume_phase_second / total_volume_layer_second;
339 radius = pow(((interface_volume) * 3.0 / 4.0 / M_PI), 1.0 / 3.0);
340 *surface_area = f_first * f_second * 4 * M_PI * pow(radius, 2.0);
341
342
343 // Calculate the partial derivatives for each layer/phase combination.
344 if (!partial_deriv) return;
345 i_phase_count = 0;
346 for (int i_layer = 0; i_layer < NUM_LAYERS_; ++i_layer) {
347 for (int i_phase = 0; i_phase < NUM_PHASES_(i_layer); ++i_phase) {
348 double *state = (double *)(model_data->grid_cell_state);
349 state += PARTICLE_STATE_SIZE_ + PHASE_STATE_ID_(i_layer,i_phase);
350 double volume_phase;
351 aero_phase_get_volume__m3_m3(model_data, PHASE_MODEL_DATA_ID_(i_layer,i_phase),
352 state, &(volume_phase), NULL);
353 for (int i_spec = 0; i_spec < PHASE_NUM_JAC_ELEM_(i_layer,i_phase); ++i_spec) {
354 // layer = layer_first, phase = aero_phase_idx_first
355 if (i_layer == layer_first && i_phase_count == aero_phase_idx_first) {
356 *partial_deriv =
357 (((total_volume_layer_first - volume_phase_first) *
358 pow(total_volume_layer_first, -2.0) * f_second * (*surface_area)) +
359 2.0 * f_first * f_second * pow(radius, -1.0)) * (*partial_deriv);
360 ++partial_deriv;
361 }
362 // layer = layer_first, phase != aero_phase_idx_first
363 else if (i_layer == layer_first && i_phase_count != aero_phase_idx_first) {
364 *partial_deriv =
365 (((-1 * volume_phase) * pow(total_volume_layer_first, -2.0) *
366 f_second * (*surface_area)) + 2.0 * f_first * f_second *
367 pow(radius, -1.0)) * (*partial_deriv);
368 ++partial_deriv;
369 }
370 // layer = layer_second, phase = aero_phase_idx_second
371 else if (i_layer == layer_second && i_phase_count == aero_phase_idx_second) {
372 *partial_deriv =
373 (((total_volume_layer_second - volume_phase_second) *
374 pow(total_volume_layer_second, -2.0) * f_first * (*surface_area)) +
375 2.0 * f_first * f_second * pow(radius, -1.0)) * (*partial_deriv);
376 ++partial_deriv;
377 }
378 // layer = layer_second, phase != aero_phase_idx_second
379 else if (i_layer == layer_second && i_phase_count != aero_phase_idx_second) {
380 *partial_deriv =
381 (((-1 * volume_phase) * pow(total_volume_layer_second, -2.0) *
382 f_first * (*surface_area)) + 2.0 * f_first * f_second *
383 pow(radius, -1.0)) * (*partial_deriv);
384 ++partial_deriv;
385 }
386 // Set partial_derivative = 0 for all other layers.
387 else if (i_layer != layer_first && i_layer != layer_second) {
388 *(partial_deriv++) = ZERO;
389 }
390 else {
391 printf("\n\nERROR No conditions met for surface area partial derivative.\n\n");
392 exit(1);
393 }
394 }
395 ++i_phase_count;
396 }
397 }
398 return;
399}
400
401/** \brief Get the thickness of a particle layer (m)
402 *
403 * \param model_data Pointer to the model data, including the state array
404 * \param aero_phase_idx Index of the aerosol phase within the representation
405 * \param layer_thickness Effective layer thickness (m)
406 * \param partial_deriv \f$\frac{\partial r_{eff}}{\partial y}\f$ where \f$y\f$
407 * are species on the state array
408 * \param aero_rep_int_data Pointer to the aerosol representation integer data
409 * \param aero_rep_float_data Pointer to the aerosol representation
410 * floating-point data
411 * \param aero_rep_env_data Pointer to the aerosol representation
412 * environment-dependent parameters
413 */
414
416 ModelData *model_data, int aero_phase_idx, double *layer_thickness,
417 double *partial_deriv, int *aero_rep_int_data, double *aero_rep_float_data,
418 double *aero_rep_env_data) {
419
420 int *int_data = aero_rep_int_data;
421 double *float_data = aero_rep_float_data;
422 int jac_size = PARTICLE_STATE_SIZE_;
423 double radius_inner, radius_outer;
424 int i_part = aero_phase_idx / TOTAL_NUM_PHASES_;
425 int aero_phase_idx_temp = aero_phase_idx;
426 aero_phase_idx_temp -= i_part * TOTAL_NUM_PHASES_;
427
428 // Temporary Jacobians
429 double *jac_inner = NULL;
430 double *jac_outer = NULL;
431
432 if (partial_deriv) {
433 jac_inner = (double *)calloc(jac_size, sizeof(double));
434 jac_outer = (double *)calloc(jac_size, sizeof(double));
435 }
436
437 int i_layer_inner = -1;
438 int i_layer_outer = -1;
439 int i_phase_inner = -1;
440 int i_phase_outer = -1;
441 for (int i_layer = 0; i_layer < NUM_LAYERS_; ++i_layer) {
442 if (LAYER_PHASE_START_(i_layer) <= aero_phase_idx_temp &&
443 aero_phase_idx_temp <= LAYER_PHASE_END_(i_layer)) {
444 i_layer_outer = i_layer;
445 if (i_layer - 1 >= 0 ) {
446 i_layer_inner = i_layer - 1;
447 } else if (i_layer - 1 < 0 ) {
448 i_layer_inner = i_layer;
449 }
450 }
451 }
452 int offset = aero_phase_idx_temp - (i_part * LAYER_PHASE_START_(i_layer_outer));
453 int aero_phase_idx_inner = -1;
454 if (i_layer_inner == i_layer_outer) {
455 aero_phase_idx_inner = aero_phase_idx;
456 } else {
457 aero_phase_idx_inner = aero_phase_idx - (offset+1);
458 }
459
461 model_data,
462 aero_phase_idx,
463 &radius_outer,
464 jac_outer,
465 int_data,
466 float_data,
467 aero_rep_env_data);
468
470 model_data,
471 aero_phase_idx_inner,
472 &radius_inner,
473 jac_inner,
474 int_data,
475 float_data,
476 aero_rep_env_data);
477
478 if (i_layer_inner == i_layer_outer) {
479 *layer_thickness = radius_inner;
480 } else {
481 *layer_thickness = radius_outer - radius_inner;
482 }
483
484 if (partial_deriv) {
485 for (int i = 0; i < jac_size; ++i) {
486 partial_deriv[i] = jac_outer[i] - jac_inner[i];
487 }
488 }
489
490 free(jac_inner);
491 free(jac_outer);
492 return;
493}
494
495/** \brief Get the particle number concentration \f$n\f$
496 * (\f$\mbox{\si{\#\per\cubic\metre}}\f$)
497 *
498 * This single particle number concentration is set by the aerosol model prior
499 * to solving the chemistry. Thus, all \f$\frac{\partial n}{\partial y}\f$ are
500 * zero. Also, there is only one set of particles in the single particle
501 * representation, so the phase index is not used.
502 *
503 * \param model_data Pointer to the model data, including the state array
504 * \param aero_phase_idx Index of the aerosol phase within the representation
505 * (not used)
506 * \param number_conc Particle number concentration, \f$n\f$
507 * (\f$\mbox{\si{\#\per\cubic\metre}}\f$)
508 * \param partial_deriv \f$\frac{\partial n}{\partial y}\f$ where \f$y\f$ are
509 * the species on the state array
510 * \param aero_rep_int_data Pointer to the aerosol representation integer data
511 * \param aero_rep_float_data Pointer to the aerosol representation
512 * floating-point data
513 * \param aero_rep_env_data Pointer to the aerosol representation
514 * environment-dependent parameters
515 */
516
518 ModelData *model_data, int aero_phase_idx, double *number_conc,
519 double *partial_deriv, int *aero_rep_int_data, double *aero_rep_float_data,
520 double *aero_rep_env_data) {
521
522
523 int *int_data = aero_rep_int_data;
524 double *float_data = aero_rep_float_data;
525 int i_part = aero_phase_idx / TOTAL_NUM_PHASES_;
526
527 *number_conc = NUMBER_CONC_(i_part);
528
529 if (partial_deriv) {
530 for (int i_layer = 0; i_layer < NUM_LAYERS_; ++i_layer) {
531 for (int i_phase = 0; i_phase < NUM_PHASES_(i_layer); ++i_phase) {
532 for (int i_spec = 0; i_spec < PHASE_NUM_JAC_ELEM_(i_layer,i_phase); ++i_spec)
533 *(partial_deriv++) = ZERO;
534 }
535 }
536 }
537 return;
538}
539
540/** \brief Get the type of aerosol concentration used.
541 *
542 * Single particle concentrations are per-particle.
543 *
544 * \param aero_phase_idx Index of the aerosol phase within the representation
545 * \param aero_conc_type Pointer to int that will hold the concentration type
546 * code (0 = per particle mass concentrations;
547 * 1 = total particle mass concentrations)
548 * \param aero_rep_int_data Pointer to the aerosol representation integer data
549 * \param aero_rep_float_data Pointer to the aerosol representation
550 * floating-point data
551 * \param aero_rep_env_data Pointer to the aerosol representation
552 * environment-dependent parameters
553 */
554
556 int *aero_conc_type,
557 int *aero_rep_int_data,
558 double *aero_rep_float_data,
559 double *aero_rep_env_data) {
560 int *int_data = aero_rep_int_data;
561 double *float_data = aero_rep_float_data;
562
563 *aero_conc_type = 0;
564
565 return;
566}
567
568/** \brief Get the total mass in an aerosol phase \f$m\f$
569 * (\f$\mbox{\si{\kilogram\per\cubic\metre}}\f$)
570 *
571 * The single particle mass is set for each new state as the sum of the masses
572 * of the aerosol phases that compose the particle
573 *
574 * \param model_data Pointer to the model data, including the state array
575 * \param aero_phase_idx Index of the aerosol phase within the representation
576 * \param aero_phase_mass Total mass in the aerosol phase, \f$m\f$
577 * (\f$\mbox{\si{\kilogram\per\cubic\metre}}\f$)
578 * \param partial_deriv \f$\frac{\partial m}{\partial y}\f$ where \f$y\f$ are
579 * the species on the state array
580 * \param aero_rep_int_data Pointer to the aerosol representation integer data
581 * \param aero_rep_float_data Pointer to the aerosol representation
582 * floating-point data
583 * \param aero_rep_env_data Pointer to the aerosol representation
584 * environment-dependent parameters
585 */
586
588 ModelData *model_data, int aero_phase_idx, double *aero_phase_mass,
589 double *partial_deriv, int *aero_rep_int_data, double *aero_rep_float_data,
590 double *aero_rep_env_data) {
591
592 int *int_data = aero_rep_int_data;
593 double *float_data = aero_rep_float_data;
594 int i_part = aero_phase_idx / TOTAL_NUM_PHASES_;
595 aero_phase_idx -= i_part * TOTAL_NUM_PHASES_;
596
597 int i_total_phase = 0;
598 for (int i_layer = 0; i_layer < NUM_LAYERS_; ++i_layer) {
599 for (int i_phase = 0; i_phase < NUM_PHASES_(i_layer); ++i_phase) {
600 if ( i_total_phase == aero_phase_idx ) {
601 double *state = (double *)(model_data->grid_cell_state);
602 state += i_part * PARTICLE_STATE_SIZE_ + PHASE_STATE_ID_(i_layer,i_phase);
603 double mw;
604 aero_phase_get_mass__kg_m3(model_data, PHASE_MODEL_DATA_ID_(i_layer,i_phase),
605 state, aero_phase_mass, &mw, partial_deriv, NULL);
606 if (partial_deriv) partial_deriv += PHASE_NUM_JAC_ELEM_(i_layer,i_phase);
607 } else if (partial_deriv) {
608 for (int i_spec = 0; i_spec < PHASE_NUM_JAC_ELEM_(i_layer,i_phase); ++i_spec)
609 *(partial_deriv++) = ZERO;
610 }
611 ++i_total_phase;
612 }
613 }
614 return;
615}
616
617/** \brief Get the average molecular weight in an aerosol phase
618 ** \f$m\f$ (\f$\mbox{\si{\kilo\gram\per\mol}}\f$)
619 *
620 * The single particle mass is set for each new state as the sum of the masses
621 * of the aerosol phases that compose the particle
622 *
623 * \param model_data Pointer to the model data, including the state array
624 * \param aero_phase_idx Index of the aerosol phase within the representation
625 * \param aero_phase_avg_MW Average molecular weight in the aerosol phase
626 * (\f$\mbox{\si{\kilogram\per\mole}}\f$)
627 * \param partial_deriv \f$\frac{\partial m}{\partial y}\f$ where \f$y\f$ are
628 * the species on the state array
629 * \param aero_rep_int_data Pointer to the aerosol representation integer data
630 * \param aero_rep_float_data Pointer to the aerosol representation
631 * floating-point data
632 * \param aero_rep_env_data Pointer to the aerosol representation
633 * environment-dependent parameters
634 */
635
637 ModelData *model_data, int aero_phase_idx, double *aero_phase_avg_MW,
638 double *partial_deriv, int *aero_rep_int_data, double *aero_rep_float_data,
639 double *aero_rep_env_data) {
640
641 int *int_data = aero_rep_int_data;
642 double *float_data = aero_rep_float_data;
643 int i_part = aero_phase_idx / TOTAL_NUM_PHASES_;
644 aero_phase_idx -= i_part * TOTAL_NUM_PHASES_;
645
646 int i_total_phase = 0;
647 for (int i_layer = 0; i_layer < NUM_LAYERS_; ++i_layer) {
648 for (int i_phase = 0; i_phase < NUM_PHASES_(i_layer); ++i_phase) {
649 if ( i_total_phase == aero_phase_idx ) {
650 double *state = (double *)(model_data->grid_cell_state);
651 state += i_part * PARTICLE_STATE_SIZE_ + PHASE_STATE_ID_(i_layer,i_phase);
652 double mass;
653 aero_phase_get_mass__kg_m3(model_data, PHASE_MODEL_DATA_ID_(i_layer,i_phase),
654 state, &mass, aero_phase_avg_MW, NULL, partial_deriv);
655 if (partial_deriv) partial_deriv += PHASE_NUM_JAC_ELEM_(i_layer,i_phase);
656 } else if (partial_deriv) {
657 for (int i_spec = 0; i_spec < PHASE_NUM_JAC_ELEM_(i_layer,i_phase); ++i_spec)
658 *(partial_deriv++) = ZERO;
659 }
660 ++i_total_phase;
661 }
662 }
663 return;
664}
665
666/** \brief Update aerosol representation data
667 *
668 * Single particle aerosol representation update data is structured as follows:
669 *
670 * - \b int aero_rep_id (Id of one or more aerosol representations set by the
671 * host model using the
672 * camp_aero_rep_single_particle::aero_rep_single_particle_t::set_id
673 * function prior to initializing the solver.)
674 * - \b int update_type (Type of update to perform. Can be UPDATE_NUMBER
675 * only.)
676 * - \b double new_value (Either the new radius (m) or the new number
677 * concentration (\f$\mbox{\si{\#\per\cubic\centi\metre}}\f$).)
678 *
679 * \param update_data Pointer to the updated aerosol representation data
680 * \param aero_rep_int_data Pointer to the aerosol representation integer data
681 * \param aero_rep_float_data Pointer to the aerosol representation
682 * floating-point data
683 * \param aero_rep_env_data Pointer to the aerosol representation
684 * environment-dependent parameters
685 * \return Flag indicating whether this is the aerosol representation to update
686 */
687
689 int *aero_rep_int_data,
690 double *aero_rep_float_data,
691 double *aero_rep_env_data) {
692 int *int_data = aero_rep_int_data;
693 double *float_data = aero_rep_float_data;
694
695 int *aero_rep_id = (int *)update_data;
696 int *update_type = (int *)&(aero_rep_id[1]);
697 int *particle_id = (int *)&(update_type[1]);
698 double *new_value = (double *)&(update_type[2]);
699
700 // Set the new radius or number concentration for matching aerosol
701 // representations
702 if (*aero_rep_id == AERO_REP_ID_ && AERO_REP_ID_ != 0) {
703 if (*update_type == UPDATE_NUMBER) {
704 NUMBER_CONC_(*particle_id) = (double)*new_value;
705 return true;
706 }
707 }
708
709 return false;
710}
711
712/** \brief Print the Single Particle reaction parameters
713 *
714 * \param aero_rep_int_data Pointer to the aerosol representation integer data
715 * \param aero_rep_float_data Pointer to the aerosol representation
716 * floating-point data
717 */
718
719void aero_rep_single_particle_print(int *aero_rep_int_data,
720 double *aero_rep_float_data) {
721 int *int_data = aero_rep_int_data;
722 double *float_data = aero_rep_float_data;
723
724 printf("\n\nSingle particle aerosol representation\n");
725 printf("\nNumber of phases: %d", TOTAL_NUM_PHASES_);
726 printf("\nAerosol representation id: %d", AERO_REP_ID_);
727 printf("\nMax computational particles: %d", MAX_PARTICLES_);
728 printf("\nParticle state size: %d", PARTICLE_STATE_SIZE_);
729 for(int i_layer = 0; i_layer < NUM_LAYERS_; ++i_layer){
730 printf("\nLayer: %d", i_layer);
731 printf("\n Start phase: %d End phase: %d", LAYER_PHASE_START_(i_layer), LAYER_PHASE_END_(i_layer));
732 printf("\n Number of phases: %d", NUM_PHASES_(i_layer));
733 printf("\n\n - Phases -");
734 for (int i_phase = 0; i_phase < NUM_PHASES_(i_layer); ++i_phase) {
735 printf("\n state id: %d model data id: %d num Jac elements: %d",
736 PHASE_STATE_ID_(i_layer,i_phase), PHASE_MODEL_DATA_ID_(i_layer,i_phase),
737 PHASE_NUM_JAC_ELEM_(i_layer,i_phase));
738 }
739 }
740 printf("\n\nEnd single particle aerosol representation\n");
741 return;
742}
743
744
745/** \brief Create update data for new particle number
746 *
747 * \return Pointer to a new number update data object
748 */
750 int *update_data = (int *)malloc(3 * sizeof(int) + sizeof(double));
751 if (update_data == NULL) {
752 printf("\n\nERROR allocating space for number update data\n\n");
753 exit(1);
754 }
755 return (void *)update_data;
756}
757
758/** \brief Set number update data (#/m3)
759 *
760 * \param update_data Pointer to an allocated number update data object
761 * \param aero_rep_id Id of the aerosol representation(s) to update
762 * \param particle_id Id of the computational particle
763 * \param number_conc New particle number (#/m3)
764 */
766 int aero_rep_id,
767 int particle_id,
768 double number_conc) {
769 int *new_aero_rep_id = (int *)update_data;
770 int *update_type = (int *)&(new_aero_rep_id[1]);
771 int *new_particle_id = (int *)&(update_type[1]);
772 double *new_number_conc = (double *)&(update_type[2]);
773 *new_aero_rep_id = aero_rep_id;
774 *update_type = UPDATE_NUMBER;
775 *new_particle_id = particle_id;
776 *new_number_conc = number_conc;
777}
778
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.