Skip to content

Commit e7654d9

Browse files
committed
binding aero_particle_species_masses()
1 parent 6c30586 commit e7654d9

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

src/aero_particle.F90

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,17 @@ subroutine f_aero_particle_species_mass(aero_particle_ptr_c, i_spec, aero_data_p
156156
mass = aero_particle_species_mass(aero_particle_ptr_f, i_spec+1, aero_data_ptr_f)
157157
end subroutine
158158

159+
subroutine f_aero_particle_species_masses(aero_particle_ptr_c, aero_data_ptr_c, size_masses, masses) bind(C)
160+
type(aero_particle_t), pointer :: aero_particle_ptr_f => null()
161+
type(aero_data_t), pointer :: aero_data_ptr_f => null()
162+
type(c_ptr), intent(in) :: aero_particle_ptr_c, aero_data_ptr_c
163+
integer(c_int), intent(in) :: size_masses
164+
real(c_double), dimension(size_masses), intent(out) :: masses
165+
166+
call c_f_pointer(aero_particle_ptr_c, aero_particle_ptr_f)
167+
call c_f_pointer(aero_data_ptr_c, aero_data_ptr_f)
168+
169+
masses = aero_particle_species_masses(aero_particle_ptr_f, aero_data_ptr_f)
170+
end subroutine
171+
159172
end module

src/aero_particle.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ extern "C" void f_aero_particle_diameter(const void *aero_particle_ptr, const vo
2323
extern "C" void f_aero_particle_dry_diameter(const void *aero_particle_ptr, const void * aero_data_ptr, double *diameter) noexcept;
2424
extern "C" void f_aero_particle_mass(const void *aero_particle_ptr, const void *aero_data_ptr, double *mass) noexcept;
2525
extern "C" void f_aero_particle_species_mass(const void *aero_particle_ptr, const int *i_spec, const void *aero_data_ptr, double *mass) noexcept;
26+
extern "C" void f_aero_particle_species_masses(const void *aero_particle_ptr, const void *aero_data_ptr, const int *size_masses, void *masses) noexcept;
2627

2728

2829
namespace py = pybind11;
@@ -102,5 +103,12 @@ struct AeroParticle {
102103
return mass;
103104
}
104105

106+
static std::valarray<double> species_masses(const AeroParticle &self) {
107+
int len = AeroData::__len__(self.aero_data);
108+
std::valarray<double> masses(len);
109+
f_aero_particle_species_masses(&self.ptr, &self.aero_data, &len, begin(masses));
110+
return masses;
111+
}
112+
105113
};
106114

src/pypartmc.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ PYBIND11_MODULE(_PyPartMC, m) {
113113
"Total mass of the particle (kg).")
114114
.def("species_mass", AeroParticle::species_mass,
115115
"Mass of a single species in the particle (kg).")
116+
.def_property_readonly("species_masses", AeroParticle::species_masses,
117+
"Mass of all species in the particle (kg).")
116118
;
117119

118120
py::class_<AeroState>(m, "AeroState",

tests/test_aero_particle.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,25 @@ def test_species_mass():
214214

215215
# assert
216216
assert sodium_mass == check
217+
218+
@staticmethod
219+
def test_species_masses():
220+
# arrange
221+
aero_data_arg = (
222+
{"H2O": [1000 * si.kg / si.m**3, 0, 18e-3 * si.kg / si.mol, 0]},
223+
{"Cl": [2200 * si.kg / si.m**3, 1, 35.5e-3* si.kg / si.mol, 0]},
224+
{"Na": [2200 * si.kg / si.m**3, 1, 23e-3 * si.kg / si.mol, 0]}
225+
)
226+
aero_data = ppmc.AeroData(aero_data_arg)
227+
volumes = [1, 2, 3]
228+
sut = ppmc.AeroParticle(aero_data, volumes)
229+
230+
# act
231+
masses = sut.species_masses
232+
check = []
233+
for i, spec in enumerate(aero_data_arg):
234+
key = list(spec)[0]
235+
check.append(spec[key][0] * volumes[i])
236+
237+
# assert
238+
assert masses == check

0 commit comments

Comments
 (0)