Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Particle Container: Runtime Arguments #222

Merged
merged 4 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Base/PODVector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ void make_PODVector(py::module &m, std::string typestr, std::string allocstr)
.def(py::init<>())
.def(py::init<std::size_t>(), py::arg("size"))
.def(py::init<PODVector_type&>(), py::arg("other"))
.def("assign", [](PODVector_type & pv, T const & value){
pv.assign(pv.size(), value);
}, py::arg("value"), "assign the same value to every element")
Comment on lines +64 to +66
Copy link
Member Author

@ax3l ax3l Nov 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overloaded because vector.assign(vector.size(), 42) to set a value is a bit cumbersome.
https://github.com/AMReX-Codes/amrex/blob/47347f785f5c26f1f9e65bef8e10f2d383406ef7/Src/Base/AMReX_PODVector.H#L480-L513

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could also call this set_value in symmetry with MultiFab, please let me know what you prefer @WeiqunZhang @atmyers

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.def("push_back", py::overload_cast<const T&>(&PODVector_type::push_back))
.def("pop_back", &PODVector_type::pop_back)
.def("clear", &PODVector_type::clear)
Expand Down
6 changes: 6 additions & 0 deletions src/Particle/ParticleContainer.H
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ void make_ParticleContainer_and_Iterators (py::module &m, std::string allocstr)
.def_property_readonly("num_position_components", [](const py::object&){ return AMREX_SPACEDIM; })
.def_property_readonly("byte_spread", &ParticleContainerType::ByteSpread)

// 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_property_readonly("finest_level", &ParticleContainerBase::finestLevel)

// ParticleContainer ( const ParticleContainer &) = delete;
Expand Down
34 changes: 34 additions & 0 deletions tests/test_particleContainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ def particle_container(Npart, std_geometry, distmap, boxarr, std_real_box):

iseed = 1
pc.init_random(Npart, iseed, myt, False, std_real_box)

# add runtime components: 1 real 2 int
pc.add_real_comp(True)
pc.add_int_comp(True)
pc.add_int_comp(True)

# assign some values to runtime components
for lvl in range(pc.finest_level + 1):
for pti in pc.iterator(pc, level=lvl):
soa = pti.soa()
soa.get_real_data(2).assign(1.2345)
soa.get_int_data(1).assign(42)
soa.get_int_data(2).assign(33)

return pc


Expand All @@ -58,6 +72,20 @@ def soa_particle_container(Npart, std_geometry, distmap, boxarr, std_real_box):

iseed = 1
pc.init_random(Npart, iseed, myt, False, std_real_box)

# add runtime components: 1 real 2 int
pc.add_real_comp(True)
pc.add_int_comp(True)
pc.add_int_comp(True)

# assign some values to runtime components
for lvl in range(pc.finest_level + 1):
for pti in pc.iterator(pc, level=lvl):
soa = pti.soa()
soa.get_real_data(8).assign(1.2345)
soa.get_int_data(0).assign(42)
soa.get_int_data(1).assign(33)

return pc


Expand Down Expand Up @@ -315,6 +343,7 @@ def test_per_cell(empty_particle_container, std_geometry, std_particle):
def test_soa_pc_numpy(soa_particle_container, Npart):
"""Used in docs/source/usage/compute.rst"""
pc = soa_particle_container
assert pc.number_of_particles_at_level(0) == Npart

class Config:
have_gpu = False
Expand Down Expand Up @@ -358,6 +387,7 @@ class Config:
def test_pc_numpy(particle_container, Npart):
"""Used in docs/source/usage/compute.rst"""
pc = particle_container
assert pc.number_of_particles_at_level(0) == Npart

class Config:
have_gpu = False
Expand Down Expand Up @@ -414,6 +444,8 @@ def test_pc_df(particle_container, Npart):
print(df.columns)
print(df)

assert len(df.columns) == 14


@pytest.mark.skipif(
importlib.util.find_spec("pandas") is None, reason="pandas is not available"
Expand Down Expand Up @@ -502,3 +534,5 @@ def test_pc_df_mpi(particle_container, Npart):
# only rank 0
print(df.columns)
print(df)

assert len(df.columns) == 14
Loading