Skip to content

Commit 21def14

Browse files
jcurtis2slayoo
andauthored
add ability to add and remove particles from an AeroState (#322)
Co-authored-by: Sylwester Arabas <[email protected]> Co-authored-by: Sylwester Arabas <[email protected]>
1 parent 6b18c05 commit 21def14

File tree

4 files changed

+148
-0
lines changed

4 files changed

+148
-0
lines changed

src/aero_state.F90

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,4 +482,56 @@ subroutine f_aero_state_add_aero_dist_sample(ptr_c, ptr_aero_data_c, &
482482

483483
end subroutine
484484

485+
subroutine f_aero_state_add_particle(ptr_c, ptr_aero_data_c, &
486+
ptr_aero_particle_c) bind(C)
487+
488+
type(c_ptr) :: ptr_c, ptr_aero_data_c, ptr_aero_particle_c
489+
type(aero_state_t), pointer :: ptr_f => null()
490+
type(aero_data_t), pointer :: ptr_aero_data_f => null()
491+
type(aero_particle_t), pointer :: ptr_aero_particle_f => null()
492+
493+
call c_f_pointer(ptr_c, ptr_f)
494+
call c_f_pointer(ptr_aero_data_c, ptr_aero_data_f)
495+
call c_f_pointer(ptr_aero_particle_c, ptr_aero_particle_f)
496+
497+
call aero_state_add_particle(ptr_f, ptr_aero_particle_f, ptr_aero_data_f, &
498+
.true.)
499+
500+
end subroutine
501+
502+
subroutine f_aero_state_copy_weight(ptr_c, ptr_aero_state_to_c) bind(C)
503+
504+
type(c_ptr) :: ptr_c, ptr_aero_state_to_c
505+
type(aero_state_t), pointer :: ptr_f => null()
506+
type(aero_state_t), pointer :: ptr_aero_state_to_f => null()
507+
508+
call c_f_pointer(ptr_c, ptr_f)
509+
call c_f_pointer(ptr_aero_state_to_c, ptr_aero_state_to_f)
510+
511+
call aero_state_copy_weight(ptr_f, ptr_aero_state_to_f)
512+
513+
end subroutine
514+
515+
subroutine f_aero_state_remove_particle(ptr_c, i_part) bind(C)
516+
517+
type(c_ptr) :: ptr_c
518+
type(aero_state_t), pointer :: ptr_f => null()
519+
integer(c_int) :: i_part
520+
521+
call c_f_pointer(ptr_c, ptr_f)
522+
523+
call aero_state_remove_particle_no_info(ptr_f, i_part + 1)
524+
525+
end subroutine
526+
527+
subroutine f_aero_state_zero(ptr_c) bind(C)
528+
type(c_ptr) :: ptr_c
529+
type(aero_state_t), pointer :: ptr_f => null()
530+
531+
call c_f_pointer(ptr_c, ptr_f)
532+
533+
call aero_state_zero(ptr_f)
534+
535+
end subroutine
536+
485537
end module

src/aero_state.hpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,25 @@ extern "C" void f_aero_state_add_aero_dist_sample(
143143
int *n_part_add
144144
) noexcept;
145145

146+
extern "C" void f_aero_state_add_particle(
147+
void *ptr_c,
148+
const void *ptr_aero_data_c,
149+
const void *ptr_aero_particle_c
150+
) noexcept;
151+
152+
extern "C" void f_aero_state_copy_weight(
153+
const void *ptr_c,
154+
void *ptr_aero_state_to_c
155+
) noexcept;
156+
157+
extern "C" void f_aero_state_remove_particle(
158+
void *ptr_c,
159+
const int *i_part
160+
) noexcept;
161+
162+
extern "C" void f_aero_state_zero(
163+
void *ptr_c
164+
) noexcept;
146165

147166
template <typename arr_t, typename arg_t>
148167
auto pointer_vec_magic(arr_t &data_vec, const arg_t &arg) {
@@ -485,4 +504,38 @@ struct AeroState {
485504
return n_part_add;
486505
}
487506

507+
static void add_particle(
508+
AeroState &self,
509+
const AeroParticle &particle
510+
) {
511+
512+
f_aero_state_add_particle(self.ptr.f_arg_non_const(),
513+
self.aero_data->ptr.f_arg(),
514+
particle.ptr.f_arg()
515+
);
516+
517+
}
518+
519+
static void copy_weight(
520+
AeroState &self,
521+
const AeroState &aero_state_from
522+
) {
523+
524+
f_aero_state_copy_weight(aero_state_from.ptr.f_arg(),
525+
self.ptr.f_arg_non_const()
526+
);
527+
}
528+
529+
static void remove_particle(
530+
AeroState &self,
531+
const int &i_part
532+
) {
533+
f_aero_state_remove_particle(self.ptr.f_arg_non_const(), &i_part);
534+
}
535+
536+
static void zero(
537+
AeroState &self
538+
) {
539+
f_aero_state_zero(self.ptr.f_arg_non_const());
540+
}
488541
};

src/pypartmc.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,12 @@ PYBIND11_MODULE(_PyPartMC, m) {
235235
"sample particles for AeroState from an AeroDist",
236236
py::arg("AeroDist"), py::arg("sample_prop") = 1.0, py::arg("create_time") = 0.0,
237237
py::arg("allow_doubling") = true, py::arg("allow_halving") = true)
238+
.def("add_particle", AeroState::add_particle, "add a particle to an AeroState")
239+
.def("copy_weight", AeroState::copy_weight,
240+
"copy weighting from another AeroState")
241+
.def("remove_particle", AeroState::remove_particle,
242+
"remove particle of a given index")
243+
.def("zero", AeroState::zero, "remove all particles from an AeroState")
238244
;
239245

240246
py::class_<GasData, std::shared_ptr<GasData>>(m, "GasData",

tests/test_aero_state.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,43 @@ def test_get_particle_out_of_range(
322322
# assert
323323
assert False
324324

325+
@staticmethod
326+
def test_remove_particle(sut_minimal): # pylint: disable=redefined-outer-name
327+
diameters = sut_minimal.diameters()
328+
sut_minimal.remove_particle(len(sut_minimal) - 1)
329+
330+
assert diameters[0:-1] == sut_minimal.diameters()
331+
332+
@staticmethod
333+
def test_zero(sut_minimal): # pylint: disable=redefined-outer-name
334+
# act
335+
sut_minimal.zero()
336+
337+
# assert
338+
assert len(sut_minimal) == 0
339+
340+
@staticmethod
341+
def test_add_particle(sut_minimal): # pylint: disable=redefined-outer-name
342+
particle = sut_minimal.particle(1)
343+
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
344+
sut = ppmc.AeroState(aero_data, *AERO_STATE_CTOR_ARG_MINIMAL)
345+
346+
sut.add_particle(particle)
347+
348+
assert len(sut) == 1
349+
assert sut.particle(0).diameter == sut_minimal.particle(1).diameter
350+
351+
@staticmethod
352+
def test_copy_weight(sut_minimal): # pylint: disable=redefined-outer-name
353+
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
354+
sut = ppmc.AeroState(aero_data, *AERO_STATE_CTOR_ARG_MINIMAL)
355+
356+
sut.copy_weight(sut_minimal)
357+
sut.add_particle(sut_minimal.particle(0))
358+
359+
# pylint: disable=unsubscriptable-object
360+
assert sut.num_concs[0] == sut_minimal.num_concs[0]
361+
325362
@staticmethod
326363
@pytest.mark.parametrize(
327364
"args",

0 commit comments

Comments
 (0)