Skip to content

Commit 6322c6d

Browse files
jcurtis2slayoo
andauthored
Add particle weighting setters and getters (#460)
Co-authored-by: Sylwester Arabas <sylwester.arabas@agh.edu.pl>
1 parent 96d54c8 commit 6322c6d

File tree

4 files changed

+169
-0
lines changed

4 files changed

+169
-0
lines changed

src/aero_particle.F90

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,4 +570,75 @@ subroutine f_aero_particle_refract_core( &
570570

571571
end subroutine
572572

573+
subroutine f_aero_particle_set_weight_class( &
574+
aero_particle_ptr_c, &
575+
weight_class &
576+
) bind(C)
577+
578+
type(aero_particle_t), pointer :: aero_particle_ptr_f => null()
579+
type(c_ptr), intent(in) :: aero_particle_ptr_c
580+
integer(c_int), intent(in) :: weight_class
581+
582+
call c_f_pointer(aero_particle_ptr_c, aero_particle_ptr_f)
583+
584+
call aero_particle_set_weight(aero_particle_ptr_f, i_class=weight_class)
585+
586+
end subroutine
587+
588+
subroutine f_aero_particle_get_weight_class( &
589+
aero_particle_ptr_c, &
590+
weight_class &
591+
) bind(C)
592+
593+
type(aero_particle_t), pointer :: aero_particle_ptr_f => null()
594+
type(c_ptr), intent(in) :: aero_particle_ptr_c
595+
integer(c_int), intent(out) :: weight_class
596+
597+
call c_f_pointer(aero_particle_ptr_c, aero_particle_ptr_f)
598+
599+
weight_class = aero_particle_ptr_f%weight_class
600+
601+
end subroutine
602+
603+
subroutine f_aero_particle_set_weight_group( &
604+
aero_particle_ptr_c, &
605+
weight_group &
606+
) bind(C)
607+
608+
type(aero_particle_t), pointer :: aero_particle_ptr_f => null()
609+
type(c_ptr), intent(in) :: aero_particle_ptr_c
610+
integer(c_int), intent(in) :: weight_group
611+
612+
call c_f_pointer(aero_particle_ptr_c, aero_particle_ptr_f)
613+
614+
call aero_particle_set_weight(aero_particle_ptr_f, i_group=weight_group)
615+
616+
end subroutine
617+
618+
subroutine f_aero_particle_get_weight_group( &
619+
aero_particle_ptr_c, &
620+
weight_group &
621+
) bind(C)
622+
623+
type(aero_particle_t), pointer :: aero_particle_ptr_f => null()
624+
type(c_ptr), intent(in) :: aero_particle_ptr_c
625+
integer(c_int), intent(out) :: weight_group
626+
627+
call c_f_pointer(aero_particle_ptr_c, aero_particle_ptr_f)
628+
629+
weight_group = aero_particle_ptr_f%weight_group
630+
631+
end subroutine
632+
633+
subroutine f_aero_particle_new_id(aero_particle_ptr_c) bind(C)
634+
635+
type(aero_particle_t), pointer :: aero_particle_ptr_f => null()
636+
type(c_ptr), intent(in) :: aero_particle_ptr_c
637+
638+
call c_f_pointer(aero_particle_ptr_c, aero_particle_ptr_f)
639+
640+
call aero_particle_new_id(aero_particle_ptr_f)
641+
642+
end subroutine
643+
573644
end module

src/aero_particle.hpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ extern "C" void f_aero_particle_id(const void *aero_particle_ptr, int64_t *val)
4545
extern "C" void f_aero_particle_frozen(const void *aero_particle_ptr, int *val) noexcept;
4646
extern "C" void f_aero_particle_refract_shell(const void *aero_particle_ptr, std::complex<double> *val, const int *arr_size) noexcept;
4747
extern "C" void f_aero_particle_refract_core(const void *aero_particle_ptr, std::complex<double> *val, const int *arr_size) noexcept;
48+
extern "C" void f_aero_particle_set_weight_class(void *ptr, const int *weight_class) noexcept;
49+
extern "C" void f_aero_particle_get_weight_class(const void *ptr, int *weight_class) noexcept;
50+
extern "C" void f_aero_particle_set_weight_group(void *ptr, const int *weight_group) noexcept;
51+
extern "C" void f_aero_particle_get_weight_group(const void *ptr, int *weight_group) noexcept;
52+
extern "C" void f_aero_particle_new_id(void *ptr) noexcept;
4853

4954
struct AeroParticle {
5055
PMCResource ptr;
@@ -408,4 +413,43 @@ struct AeroParticle {
408413
);
409414
return refract_core;
410415
}
416+
417+
static void set_weight_class(AeroParticle &self, const int weight_class) {
418+
f_aero_particle_set_weight_class(
419+
self.ptr.f_arg_non_const(),
420+
&weight_class
421+
);
422+
}
423+
424+
static auto get_weight_class(const AeroParticle &self) {
425+
int weight_class;
426+
427+
f_aero_particle_get_weight_class(
428+
self.ptr.f_arg(),
429+
&weight_class
430+
);
431+
return weight_class;
432+
}
433+
434+
static void set_weight_group(AeroParticle &self, const int weight_group) {
435+
f_aero_particle_set_weight_group(
436+
self.ptr.f_arg_non_const(),
437+
&weight_group
438+
);
439+
}
440+
441+
static auto get_weight_group(const AeroParticle &self) {
442+
int weight_group;
443+
444+
f_aero_particle_get_weight_group(
445+
self.ptr.f_arg(),
446+
&weight_group
447+
);
448+
return weight_group;
449+
}
450+
451+
static void new_id(AeroParticle &self) {
452+
453+
f_aero_particle_new_id(self.ptr.f_arg_non_const());
454+
}
411455
};

src/pypartmc.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,11 @@ NB_MODULE(_PyPartMC, m) {
283283
"Reset an aero_particle to be zero.")
284284
.def("set_vols", AeroParticle::set_vols,
285285
"Set the aerosol particle volumes.")
286+
.def_prop_rw("weight_group", AeroParticle::get_weight_group, AeroParticle::set_weight_group,
287+
"Weighting function group number.")
288+
.def_prop_rw("weight_class", AeroParticle::get_weight_class, AeroParticle::set_weight_class,
289+
"Weighting function class number.")
290+
.def("new_id", AeroParticle::new_id, "Assigns a new unique particle ID")
286291
;
287292

288293
nb::class_<AeroState>(m, "AeroState",

tests/test_aero_particle.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,26 @@ def test_set_vols():
415415
# assert
416416
assert sut.volumes == [3, 2, 1]
417417

418+
@staticmethod
419+
def test_set_weighting():
420+
# arrange
421+
aero_data = ppmc.AeroData(aero_data_arg)
422+
volumes = [1, 2, 3]
423+
sut = ppmc.AeroParticle(aero_data, volumes)
424+
aero_data = None
425+
volumes = None
426+
gc.collect()
427+
428+
# act
429+
weight_class = 1
430+
weight_group = 2
431+
sut.weight_class = weight_class
432+
sut.weight_group = weight_group
433+
434+
# assert
435+
assert sut.weight_class == weight_class
436+
assert sut.weight_group == weight_group
437+
418438
@staticmethod
419439
def test_absorb_cross_sect():
420440
# arrange
@@ -490,6 +510,22 @@ def test_sources():
490510
assert len(sources) == aero_dist.n_mode
491511
assert isinstance(sources[0], int)
492512

513+
@staticmethod
514+
def test_get_weighting():
515+
# arrange
516+
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
517+
aero_dist = ppmc.AeroDist(aero_data, AERO_DIST_CTOR_ARG_MINIMAL)
518+
aero_state = ppmc.AeroState(aero_data, *AERO_STATE_CTOR_ARG_MINIMAL)
519+
_ = aero_state.dist_sample(aero_dist, 1.0, 0.0)
520+
sut = aero_state.particle(0)
521+
# act
522+
weight_class = sut.weight_class
523+
weight_group = sut.weight_group
524+
525+
# assert
526+
assert weight_class > 0
527+
assert weight_group > 0
528+
493529
@staticmethod
494530
def test_least_create_time():
495531
# arrange
@@ -544,6 +580,19 @@ def test_id():
544580
assert min(ids) > 0
545581
assert len(np.unique(ids)) == len(aero_state)
546582

583+
@staticmethod
584+
def test_new_id():
585+
# arrange
586+
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
587+
sut = ppmc.AeroParticle(aero_data, [123])
588+
589+
# act
590+
id_orig = sut.id
591+
sut.new_id()
592+
593+
# assert
594+
assert sut.id != id_orig
595+
547596
@staticmethod
548597
def test_is_frozen():
549598
# arrange

0 commit comments

Comments
 (0)