Skip to content

Commit

Permalink
ParticleContainer: More Pythonic Properties
Browse files Browse the repository at this point in the history
Rename a couple of properties to be more pythonic.
  • Loading branch information
ax3l committed Dec 5, 2023
1 parent 420cb42 commit cf6cc8b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 20 deletions.
25 changes: 19 additions & 6 deletions src/Particle/ParticleContainer.H
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,28 @@ void make_ParticleContainer_and_Iterators (py::module &m, std::string allocstr)
.def_property_readonly_static("NArrayReal", [](const py::object&){return ParticleContainerType::NArrayReal; })
.def_property_readonly_static("NArrayInt", [](const py::object&){return ParticleContainerType::NArrayInt; })

.def_property_readonly("NumRealComps", &ParticleContainerType::NumRealComps,
.def_property_readonly("num_real_comps", &ParticleContainerType::NumRealComps,
"The number of compile-time and runtime Real components in SoA")
.def_property_readonly("NumIntComps", &ParticleContainerType::NumIntComps,
.def_property_readonly("num_int_comps", &ParticleContainerType::NumIntComps,
"The number of compile-time and runtime int components in SoA")
.def_property_readonly("NumRuntimeRealComps", &ParticleContainerType::NumRuntimeRealComps,
.def_property_readonly("num_runtime_real_comps", &ParticleContainerType::NumRuntimeRealComps,
"The number of runtime Real components in SoA")
.def_property_readonly("NumRuntimeIntComps", &ParticleContainerType::NumRuntimeIntComps,
.def_property_readonly("num_runtime_int_comps", &ParticleContainerType::NumRuntimeIntComps,
"The number of runtime Int components in SoA")

// runtime components
.def("add_real_comp", &ParticleContainerType::template AddRealComp<bool>,
py::arg("communicate")=true, "add a new runtime component with type Real")
.def("add_int_comp", &ParticleContainerType::template AddIntComp<bool>,
py::arg("communicate")=true, "add a new runtime component with type Int")

.def("resize_runtime_real_comp", &ParticleContainerType::ResizeRuntimeRealComp,
py::arg("new_size"), py::arg("communicate"),
"Resize the Real runtime components (SoA)")
.def("resize_runtime_int_comp", &ParticleContainerType::ResizeRuntimeIntComp,
py::arg("new_size"), py::arg("communicate"),
"Resize the Int runtime components (SoA)")

.def_property_readonly("finest_level", &ParticleContainerBase::finestLevel)

// ParticleContainer ( const ParticleContainer &) = delete;
Expand Down Expand Up @@ -231,8 +244,8 @@ void make_ParticleContainer_and_Iterators (py::module &m, std::string allocstr)

.def("numLocalTilesAtLevel", &ParticleContainerType::numLocalTilesAtLevel)

.def("reserveData", &ParticleContainerType::reserveData)
.def("resizeData", &ParticleContainerType::resizeData)
.def("reserve_data", &ParticleContainerType::reserveData)
.def("resize_data", &ParticleContainerType::resizeData)

// void InitFromAsciiFile (const std::string& file, int extradata,
// const IntVect* Nrep = nullptr);
Expand Down
4 changes: 2 additions & 2 deletions src/Particle/StructOfArrays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ void make_StructOfArrays(py::module &m, std::string allocstr)
py::class_<SOAType>(m, soa_name.c_str())
.def(py::init())
.def("define", &SOAType::define)
.def("NumRealComps", &SOAType::NumRealComps,
.def_property_readonly("num_real_comps", &SOAType::NumRealComps,
"Get the number of compile-time + runtime Real components")
.def("NumIntComps", &SOAType::NumIntComps,
.def_property_readonly("num_int_comps", &SOAType::NumIntComps,
"Get the number of compile-time + runtime Int components")

// compile-time components
Expand Down
8 changes: 4 additions & 4 deletions src/amrex/StructOfArrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ def soa_to_numpy(self, copy=False):
if self.size() == 0:
raise ValueError("SoA is empty.")

for idx_real in range(self.NumRealComps()):
for idx_real in range(self.num_real_comps):
soa_view.real.append(self.GetRealData(idx_real).to_numpy(copy=copy))

for idx_int in range(self.NumIntComps()):
for idx_int in range(self.num_int_comps):
soa_view.int.append(self.GetIntData(idx_int).to_numpy(copy=copy))

return soa_view
Expand Down Expand Up @@ -70,10 +70,10 @@ def soa_to_cupy(self, copy=False):
if self.size() == 0:
raise ValueError("SoA is empty.")

for idx_real in range(self.NumRealComps()):
for idx_real in range(self.num_real_comps):
soa_view.real.append(self.GetRealData(idx_real).to_cupy(copy=copy))

for idx_int in range(self.NumIntComps()):
for idx_int in range(self.num_int_comps):
soa_view.int.append(self.GetIntData(idx_int).to_cupy(copy=copy))

return soa_view
Expand Down
4 changes: 2 additions & 2 deletions tests/test_particleContainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def test_pc_init():
# lvl = 0
for lvl in range(pc.finest_level + 1):
print(f"at level {lvl}:")
for pti in amr.ParIter_1_1_2_1_default(pc, level=lvl):
for pti in pc.iterator(pc, level=lvl):
print("...")
assert pti.num_particles == 1
assert pti.num_real_particles == 1
Expand Down Expand Up @@ -158,7 +158,7 @@ def test_pc_init():

# read-only
for lvl in range(pc.finest_level + 1):
for pti in amr.ParConstIter_1_1_2_1_default(pc, level=lvl):
for pti in pc.const_iterator(pc, level=lvl):
assert pti.num_particles == 1
assert pti.num_real_particles == 1
assert pti.num_neighbor_particles == 0
Expand Down
12 changes: 6 additions & 6 deletions tests/test_soa.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
def test_soa_init():
soa = amr.StructOfArrays_2_1_default()
print("--test init --")
print("num real components", soa.NumRealComps())
print("num int components", soa.NumIntComps())
assert soa.NumRealComps() == 2 and soa.NumIntComps() == 1
print("num real components", soa.num_real_comps)
print("num int components", soa.num_int_comps)
assert soa.num_real_comps == 2 and soa.num_int_comps == 1

soa.define(1, 3)
print("--test define --")
print("num real components", soa.NumRealComps())
print("num int components", soa.NumIntComps())
assert soa.NumRealComps() == 3 and soa.NumIntComps() == 4
print("num real components", soa.num_real_comps)
print("num int components", soa.num_int_comps)
assert soa.num_real_comps == 3 and soa.num_int_comps == 4
print("num particles", soa.numParticles())
print("num real particles", soa.numRealParticles())
print("num totalparticles", soa.numTotalParticles())
Expand Down

0 comments on commit cf6cc8b

Please sign in to comment.