Skip to content

Commit 074eaa6

Browse files
jcurtis2slayoo
andauthored
Add access to densities in AeroData (#181)
Co-authored-by: Sylwester Arabas <[email protected]>
1 parent 32f4d8b commit 074eaa6

File tree

4 files changed

+120
-19
lines changed

4 files changed

+120
-19
lines changed

src/aero_data.F90

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ subroutine f_aero_data_set_frac_dim(ptr_c, frac_dim) bind(C)
6969

7070
ptr_f%fractal%frac_dim = frac_dim
7171

72-
end subroutine
72+
end subroutine
7373

74-
subroutine f_aero_data_get_frac_dim(ptr_c, frac_dim) bind(C)
74+
subroutine f_aero_data_get_frac_dim(ptr_c, frac_dim) bind(C)
7575
type(aero_data_t), pointer :: ptr_f => null()
7676
type(c_ptr), intent(in) :: ptr_c
7777
real(c_double), intent(out) :: frac_dim
@@ -80,9 +80,9 @@ subroutine f_aero_data_get_frac_dim(ptr_c, frac_dim) bind(C)
8080

8181
frac_dim = ptr_f%fractal%frac_dim
8282

83-
end subroutine
83+
end subroutine
8484

85-
subroutine f_aero_data_set_vol_fill_factor(ptr_c, vol_fill_factor) bind(C)
85+
subroutine f_aero_data_set_vol_fill_factor(ptr_c, vol_fill_factor) bind(C)
8686
type(aero_data_t), pointer :: ptr_f => null()
8787
type(c_ptr), intent(in) :: ptr_c
8888
real(c_double), intent(in) :: vol_fill_factor
@@ -91,9 +91,9 @@ subroutine f_aero_data_set_vol_fill_factor(ptr_c, vol_fill_factor) bind(C)
9191

9292
ptr_f%fractal%vol_fill_factor = vol_fill_factor
9393

94-
end subroutine
94+
end subroutine
9595

96-
subroutine f_aero_data_get_vol_fill_factor(ptr_c, vol_fill_factor) bind(C)
96+
subroutine f_aero_data_get_vol_fill_factor(ptr_c, vol_fill_factor) bind(C)
9797
type(aero_data_t), pointer :: ptr_f => null()
9898
type(c_ptr), intent(in) :: ptr_c
9999
real(c_double), intent(out) :: vol_fill_factor
@@ -102,9 +102,9 @@ subroutine f_aero_data_get_vol_fill_factor(ptr_c, vol_fill_factor) bind(C)
102102

103103
vol_fill_factor = ptr_f%fractal%vol_fill_factor
104104

105-
end subroutine
105+
end subroutine
106106

107-
subroutine f_aero_data_set_prime_radius(ptr_c, prime_radius) bind(C)
107+
subroutine f_aero_data_set_prime_radius(ptr_c, prime_radius) bind(C)
108108
type(aero_data_t), pointer :: ptr_f => null()
109109
type(c_ptr), intent(in) :: ptr_c
110110
real(c_double), intent(in) :: prime_radius
@@ -113,9 +113,9 @@ subroutine f_aero_data_set_prime_radius(ptr_c, prime_radius) bind(C)
113113

114114
ptr_f%fractal%prime_radius = prime_radius
115115

116-
end subroutine
116+
end subroutine
117117

118-
subroutine f_aero_data_get_prime_radius(ptr_c, prime_radius) bind(C)
118+
subroutine f_aero_data_get_prime_radius(ptr_c, prime_radius) bind(C)
119119
type(aero_data_t), pointer :: ptr_f => null()
120120
type(c_ptr), intent(in) :: ptr_c
121121
real(c_double), intent(out) :: prime_radius
@@ -124,9 +124,9 @@ subroutine f_aero_data_get_prime_radius(ptr_c, prime_radius) bind(C)
124124

125125
prime_radius = ptr_f%fractal%prime_radius
126126

127-
end subroutine
127+
end subroutine
128128

129-
subroutine f_aero_data_rad2vol(ptr_c, radius, vol) bind(C)
129+
subroutine f_aero_data_rad2vol(ptr_c, radius, vol) bind(C)
130130
type(aero_data_t), pointer :: ptr_f => null()
131131
type(c_ptr), intent(in) :: ptr_c
132132
real(c_double), intent(in) :: radius
@@ -136,9 +136,9 @@ subroutine f_aero_data_rad2vol(ptr_c, radius, vol) bind(C)
136136

137137
vol = aero_data_rad2vol(ptr_f, radius)
138138

139-
end subroutine
139+
end subroutine
140140

141-
subroutine f_aero_data_vol2rad(ptr_c, vol, radius) bind(C)
141+
subroutine f_aero_data_vol2rad(ptr_c, vol, radius) bind(C)
142142
type(aero_data_t), pointer :: ptr_f => null()
143143
type(c_ptr), intent(in) :: ptr_c
144144
real(c_double), intent(in) :: vol
@@ -148,9 +148,9 @@ subroutine f_aero_data_vol2rad(ptr_c, vol, radius) bind(C)
148148

149149
radius = aero_data_vol2rad(ptr_f, vol)
150150

151-
end subroutine
151+
end subroutine
152152

153-
subroutine f_aero_data_diam2vol(ptr_c, diam, vol) bind(C)
153+
subroutine f_aero_data_diam2vol(ptr_c, diam, vol) bind(C)
154154
type(aero_data_t), pointer :: ptr_f => null()
155155
type(c_ptr), intent(in) :: ptr_c
156156
real(c_double), intent(in) :: diam
@@ -160,9 +160,9 @@ subroutine f_aero_data_diam2vol(ptr_c, diam, vol) bind(C)
160160

161161
vol = aero_data_diam2vol(ptr_f, diam)
162162

163-
end subroutine
163+
end subroutine
164164

165-
subroutine f_aero_data_vol2diam(ptr_c, vol, diam) bind(C)
165+
subroutine f_aero_data_vol2diam(ptr_c, vol, diam) bind(C)
166166
type(aero_data_t), pointer :: ptr_f => null()
167167
type(c_ptr), intent(in) :: ptr_c
168168
real(c_double), intent(in) :: vol
@@ -172,6 +172,17 @@ subroutine f_aero_data_vol2diam(ptr_c, vol, diam) bind(C)
172172

173173
diam = aero_data_vol2diam(ptr_f, vol)
174174

175-
end subroutine
175+
end subroutine
176+
177+
subroutine f_aero_data_get_species_density(ptr_c, idx, val) bind(C)
178+
type(aero_data_t), pointer :: ptr_f => null()
179+
type(c_ptr), intent(in) :: ptr_c
180+
integer(c_int), intent(in) :: idx
181+
real(c_double), intent(out) :: val
182+
183+
call c_f_pointer(ptr_c, ptr_f)
184+
val = ptr_f%density(idx+1)
185+
186+
end subroutine
176187

177188
end module

src/aero_data.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ extern "C" void f_aero_data_rad2vol(const void *ptr, const double*, double*) noe
2424
extern "C" void f_aero_data_vol2rad(const void *ptr, const double*, double*) noexcept;
2525
extern "C" void f_aero_data_diam2vol(const void *ptr, const double*, double*) noexcept;
2626
extern "C" void f_aero_data_vol2diam(const void *ptr, const double*, double*) noexcept;
27+
extern "C" void f_aero_data_get_species_density(const void *ptr, const int *idx, double *val) noexcept;
2728

2829
struct AeroData {
2930
PMCResource ptr;
@@ -104,5 +105,27 @@ struct AeroData {
104105
return diam;
105106
}
106107

108+
static std::valarray<double> densities(const AeroData &self) {
109+
int len;
110+
f_aero_data_len(&self.ptr, &len);
111+
std::valarray<double> data(len);
112+
113+
for (int idx = 0; idx < len; idx++) {
114+
f_aero_data_get_species_density(&self.ptr, &idx, &data[idx]);
115+
}
116+
return data;
117+
}
118+
119+
static double density(const AeroData &self, const std::string &name) {
120+
int idx;
121+
double data;
122+
const int name_size = name.size();
123+
f_aero_data_spec_by_name(&self.ptr, &idx, name.c_str(), &name_size);
124+
if (idx==0) throw std::runtime_error("Element not found.");
125+
idx--;
126+
f_aero_data_get_species_density(&self.ptr, &idx, &data);
127+
return data;
128+
}
129+
107130
};
108131

src/pypartmc.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ PYBIND11_MODULE(_PyPartMC, m) {
7474
.def_property("frac_dim", &AeroData::get_frac_dim, &AeroData::set_frac_dim)
7575
.def_property("vol_fill_factor", &AeroData::get_vol_fill_factor, &AeroData::set_vol_fill_factor)
7676
.def_property("prime_radius", &AeroData::get_prime_radius, &AeroData::set_prime_radius)
77+
.def_property_readonly("densities", &AeroData::densities,
78+
"Return array of aerosol species densities")
79+
.def("density", &AeroData::density, "Return density of an aerosol species")
7780
.def("rad2vol", AeroData::rad2vol,
7881
"Convert geometric radius (m) to mass-equivalent volume (m^3).")
7982
.def("vol2rad", AeroData::vol2rad,

tests/test_aero_data.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,67 @@ def test_vol2diam_fractal(aero_data_params: dict):
289289
** (1 / sut.frac_dim)
290290
),
291291
)
292+
293+
@staticmethod
294+
def test_aero_data_densities():
295+
# arrange
296+
densities = [1800, 1400, 1800, 1000]
297+
sut = ppmc.AeroData(
298+
(
299+
{
300+
"SO4": [
301+
densities[0] * si.kg / si.m**3,
302+
1,
303+
96.0 * si.g / si.mol,
304+
0.00,
305+
]
306+
},
307+
{
308+
"OC": [
309+
densities[1] * si.kg / si.m**3,
310+
0,
311+
1.0 * si.g / si.mol,
312+
0.10,
313+
]
314+
},
315+
{
316+
"BC": [
317+
densities[2] * si.kg / si.m**3,
318+
0,
319+
1.0 * si.g / si.mol,
320+
0.00,
321+
]
322+
},
323+
{
324+
"H2O": [
325+
densities[3] * si.kg / si.m**3,
326+
0,
327+
18.0 * si.g / si.mol,
328+
0.00,
329+
]
330+
},
331+
)
332+
)
333+
334+
# act
335+
aero_data_densities = sut.densities
336+
337+
# assert
338+
assert aero_data_densities == densities
339+
340+
@staticmethod
341+
def test_aero_data_density():
342+
# arrange
343+
sut = ppmc.AeroData(AERO_DATA_CTOR_ARG_FULL)
344+
345+
for item in AERO_DATA_CTOR_ARG_FULL:
346+
keys = item.keys()
347+
assert len(keys) == 1
348+
key = tuple(keys)[0]
349+
val = tuple(item.values())[0]
350+
351+
# act
352+
density = sut.density(key)
353+
354+
# assert
355+
assert density == val[0]

0 commit comments

Comments
 (0)