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