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