Skip to content

Commit f03d19c

Browse files
authored
Access kappa and molecular weights from AeroData (#336)
1 parent 05e5fa0 commit f03d19c

File tree

4 files changed

+158
-0
lines changed

4 files changed

+158
-0
lines changed

src/aero_data.F90

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,28 @@ subroutine f_aero_data_get_species_density(ptr_c, idx, val) bind(C)
185185

186186
end subroutine
187187

188+
subroutine f_aero_data_get_species_kappa(ptr_c, idx, val) bind(C)
189+
type(aero_data_t), pointer :: ptr_f => null()
190+
type(c_ptr), intent(in) :: ptr_c
191+
integer(c_int), intent(in) :: idx
192+
real(c_double), intent(out) :: val
193+
194+
call c_f_pointer(ptr_c, ptr_f)
195+
val = ptr_f%kappa(idx+1)
196+
197+
end subroutine
198+
199+
subroutine f_aero_data_get_species_molecular_weight(ptr_c, idx, val) bind(C)
200+
type(aero_data_t), pointer :: ptr_f => null()
201+
type(c_ptr), intent(in) :: ptr_c
202+
integer(c_int), intent(in) :: idx
203+
real(c_double), intent(out) :: val
204+
205+
call c_f_pointer(ptr_c, ptr_f)
206+
val = ptr_f%molec_weight(idx+1)
207+
208+
end subroutine
209+
188210
subroutine f_aero_data_n_source(ptr_c, n_source) bind(C)
189211
type(aero_data_t), pointer :: ptr_f => null()
190212
type(c_ptr), intent(in) :: ptr_c

src/aero_data.hpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ extern "C" void f_aero_data_vol2rad(const void *ptr, const double*, double*) noe
2828
extern "C" void f_aero_data_diam2vol(const void *ptr, const double*, double*) noexcept;
2929
extern "C" void f_aero_data_vol2diam(const void *ptr, const double*, double*) noexcept;
3030
extern "C" void f_aero_data_get_species_density(const void *ptr, const int *idx, double *val) noexcept;
31+
extern "C" void f_aero_data_get_species_kappa(const void *ptr, const int *idx, double *val) noexcept;
32+
extern "C" void f_aero_data_get_species_molecular_weight(const void *ptr, const int *idx, double *val) noexcept;
3133
extern "C" void f_aero_data_source_name_by_index(const void *ptr, const int *i_source,
3234
char *name_data) noexcept;
3335
extern "C" void f_aero_data_spec_name_by_index(const void *ptr, const int *i_spec,
@@ -252,5 +254,41 @@ struct AeroData {
252254
}
253255
return names;
254256
}
257+
258+
static auto kappa(const AeroData &self) {
259+
int len;
260+
f_aero_data_len(
261+
self.ptr.f_arg(),
262+
&len
263+
);
264+
std::valarray<double> data(len);
265+
266+
for (int idx = 0; idx < len; idx++) {
267+
f_aero_data_get_species_kappa(
268+
self.ptr.f_arg(),
269+
&idx,
270+
&data[idx]
271+
);
272+
}
273+
return data;
274+
}
275+
276+
static auto molecular_weights(const AeroData &self) {
277+
int len;
278+
f_aero_data_len(
279+
self.ptr.f_arg(),
280+
&len
281+
);
282+
std::valarray<double> data(len);
283+
284+
for (int idx = 0; idx < len; idx++) {
285+
f_aero_data_get_species_molecular_weight(
286+
self.ptr.f_arg(),
287+
&idx,
288+
&data[idx]
289+
);
290+
}
291+
return data;
292+
}
255293
};
256294

src/pypartmc.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ PYBIND11_MODULE(_PyPartMC, m) {
9797
"Radius of primary particles (m)")
9898
.def_property_readonly("densities", &AeroData::densities,
9999
"Return array of aerosol species densities")
100+
.def_property_readonly("kappa", &AeroData::kappa,
101+
"Returns array of aerosol species hygroscopicity parameter kappa")
102+
.def_property_readonly("molecular_weights", &AeroData::molecular_weights,
103+
"Returns array of aerosol species molecular weights")
100104
.def("density", &AeroData::density, "Return density of an aerosol species")
101105
.def("rad2vol", AeroData::rad2vol,
102106
"Convert geometric radius (m) to mass-equivalent volume (m^3).")

tests/test_aero_data.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,100 @@ def test_aero_data_densities():
339339
# assert
340340
assert aero_data_densities == densities
341341

342+
@staticmethod
343+
def test_aero_data_kappa():
344+
# arrange
345+
kappa = [0.65, 0.1, 0.0, 0.0]
346+
sut = ppmc.AeroData(
347+
(
348+
{
349+
"SO4": [
350+
1800.0 * si.kg / si.m**3,
351+
0,
352+
96.0 * si.g / si.mol,
353+
kappa[0],
354+
]
355+
},
356+
{
357+
"OC": [
358+
1400.0 * si.kg / si.m**3,
359+
0,
360+
1.0 * si.g / si.mol,
361+
kappa[1],
362+
]
363+
},
364+
{
365+
"BC": [
366+
1800.0 * si.kg / si.m**3,
367+
0,
368+
1.0 * si.g / si.mol,
369+
kappa[2],
370+
]
371+
},
372+
{
373+
"H2O": [
374+
1000.0 * si.kg / si.m**3,
375+
0,
376+
18.0 * si.g / si.mol,
377+
kappa[3],
378+
]
379+
},
380+
)
381+
)
382+
383+
# act
384+
aero_data_kappa = sut.kappa
385+
386+
# assert
387+
assert aero_data_kappa == kappa
388+
389+
@staticmethod
390+
def test_aero_data_molecular_weight():
391+
# arrange
392+
molec_weight = [96.0, 1.0, 1.0, 18.0]
393+
sut = ppmc.AeroData(
394+
(
395+
{
396+
"SO4": [
397+
1800.0 * si.kg / si.m**3,
398+
0,
399+
molec_weight[0] * si.kg / si.mol,
400+
0.65,
401+
]
402+
},
403+
{
404+
"OC": [
405+
1400.0 * si.kg / si.m**3,
406+
0,
407+
molec_weight[1] * si.kg / si.mol,
408+
0.1,
409+
]
410+
},
411+
{
412+
"BC": [
413+
1800.0 * si.kg / si.m**3,
414+
0,
415+
molec_weight[2] * si.kg / si.mol,
416+
0.0,
417+
]
418+
},
419+
{
420+
"H2O": [
421+
1000.0 * si.kg / si.m**3,
422+
0,
423+
molec_weight[3] * si.kg / si.mol,
424+
0.0,
425+
]
426+
},
427+
)
428+
)
429+
430+
# act
431+
aero_data_molec_weight = sut.molecular_weights
432+
433+
# assert
434+
assert aero_data_molec_weight == molec_weight
435+
342436
@staticmethod
343437
def test_aero_data_density():
344438
# arrange

0 commit comments

Comments
 (0)