Skip to content

Commit

Permalink
added binding for ParticleBoundaryBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
roelof-groenewald committed Jul 20, 2023
1 parent 624ac7a commit d40e431
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 27 deletions.
32 changes: 14 additions & 18 deletions Python/pywarpx/_libwarpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def initialize(self, argv=None, mpi_comm=None):
self.libwarpx_so.read_BC_params()
if self.geometry_dim == 'rz':
self.libwarpx_so.check_gridding_for_RZ_spectral()
self.warpx = self.libwarpx_so.WarpX()
self.warpx = self.libwarpx_so.get_instance()
self.warpx.initialize_data()
self.libwarpx_so.execute_python_callback("afterinit");
self.libwarpx_so.execute_python_callback("particleloader");
Expand Down Expand Up @@ -233,8 +233,7 @@ def getistep(self, level=0):
The refinement level to reference
'''

warpx = self.libwarpx_so.get_instance()
return warpx.getistep(level)
return self.warpx.getistep(level)

def gett_new(self, level=0):
'''
Expand All @@ -248,8 +247,7 @@ def gett_new(self, level=0):
The refinement level to reference
'''

warpx = self.libwarpx_so.get_instance()
return warpx.gett_new(level)
return self.warpx.gett_new(level)

def evolve(self, num_steps=-1):
'''
Expand Down Expand Up @@ -468,8 +466,7 @@ def add_particles(self, species_name, x=None, y=None, z=None, ux=None, uy=None,
# uy = uy.astype(self._numpy_particlereal_dtype, copy=False)
# uz = uz.astype(self._numpy_particlereal_dtype, copy=False)

warpx = self.libwarpx_so.get_instance()
mpc = warpx.multi_particle_container()
mpc = self.warpx.multi_particle_container()
pc = mpc.get_particle_container_from_name(species_name)
pc.add_n_particles(0, x.size, x, y, z, ux, uy, uz,
nattr, attr, nattr_int, attr_int, unique_particles)
Expand All @@ -495,8 +492,7 @@ def get_particle_count(self, species_name, local=False):
An integer count of the number of particles
'''

warpx = self.libwarpx_so.get_instance()
mypc = warpx.multi_particle_container()
mypc = self.warpx.multi_particle_container()
myspc = mypc.get_particle_container_from_name(species_name)
return myspc.total_number_of_particles(True, local)

Expand Down Expand Up @@ -525,8 +521,7 @@ def get_particle_structs(self, species_name, level):
The requested particle struct data
'''

warpx = self.libwarpx_so.get_instance()
mypc = warpx.multi_particle_container()
mypc = self.warpx.multi_particle_container()
myspc = mypc.get_particle_container_from_name(species_name)

particle_data = []
Expand Down Expand Up @@ -755,8 +750,8 @@ def get_particle_comp_index(self, species_name, pid_name):
int
Integer corresponding to the index of the requested attribute
'''
warpx = self.libwarpx_so.get_instance()
mpc = warpx.multi_particle_container()

mpc = self.warpx.multi_particle_container()
pc = mpc.get_particle_container_from_name(species_name)

return self.libwarpx_so.warpx_getParticleCompIndex(
Expand All @@ -780,8 +775,8 @@ def add_real_comp(self, species_name, pid_name, comm=True):
comm : bool
Should the component be communicated
'''
warpx = self.libwarpx_so.get_instance()
mpc = warpx.multi_particle_container()

mpc = self.warpx.multi_particle_container()
pc = mpc.get_particle_container_from_name(species_name)
pc.add_real_comp(pid_name, comm)

Expand Down Expand Up @@ -823,9 +818,10 @@ def get_particle_boundary_buffer_size(self, species_name, boundary, local=False)
processor's buffer
'''

return self.libwarpx_so.warpx_getParticleBoundaryBufferSize(
ctypes.c_char_p(species_name.encode('utf-8')),
self._get_boundary_number(boundary), local
particle_buffer = self.warpx.get_particle_boundary_buffer()
return particle_buffer.get_num_particles_in_container(
species_name, self._get_boundary_number(boundary),
local=local
)

def get_particle_boundary_buffer_structs(self, species_name, boundary, level):
Expand Down
12 changes: 12 additions & 0 deletions Source/Particles/ParticleBoundaryBuffer.H
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ class ParticleBoundaryBuffer
public:
ParticleBoundaryBuffer ();

~ParticleBoundaryBuffer() {}

/** Copy constructor for ParticleBoundaryBuffer */
ParticleBoundaryBuffer ( const ParticleBoundaryBuffer &) = delete;
/** Copy operator for ParticleBoundaryBuffer */
ParticleBoundaryBuffer& operator= ( const ParticleBoundaryBuffer & ) = delete;

/** Move constructor for NamedComponentParticleContainer */
ParticleBoundaryBuffer ( ParticleBoundaryBuffer && ) = default;
/** Move operator for NamedComponentParticleContainer */
ParticleBoundaryBuffer& operator= ( ParticleBoundaryBuffer && ) = default;

int numSpecies() const { return getSpeciesNames().size(); }

const std::vector<std::string>& getSpeciesNames() const {
Expand Down
1 change: 1 addition & 0 deletions Source/Python/Particles/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ foreach(D IN LISTS WarpX_DIMS)
target_sources(pyWarpX_${SD}
PRIVATE
# pybind11
ParticleBoundaryBuffer.cpp
MultiParticleContainer.cpp
WarpXParticleContainer.cpp
)
Expand Down
1 change: 1 addition & 0 deletions Source/Python/Particles/MultiParticleContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace py = pybind11;


void init_MultiParticleContainer (py::module& m)
{
py::class_<MultiParticleContainer> mpc(m, "MultiParticleContainer");
Expand Down
27 changes: 27 additions & 0 deletions Source/Python/Particles/ParticleBoundaryBuffer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* Copyright 2021-2023 The WarpX Community
*
* Authors: Axel Huebl, Remi Lehe, Roelof Groenewald
* License: BSD-3-Clause-LBNL
*/

#include <Particles/ParticleBoundaryBuffer.H>
#include <Particles/MultiParticleContainer.H>
#include <Python/pyWarpX.H>

namespace py = pybind11;


void init_ParticleBoundaryBuffer (py::module& m)
{
py::class_<ParticleBoundaryBuffer>(m, "ParticleBoundaryBuffer")
.def(py::init<>())
.def("get_num_particles_in_container",
[](ParticleBoundaryBuffer& pbc,
const std::string species_name, int boundary, bool local)
{
return pbc.getNumParticlesInContainer(species_name, boundary, local);
},
py::arg("species_name"), py::arg("boundary"), py::arg("local")=true
)
;
}
8 changes: 7 additions & 1 deletion Source/Python/WarpX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
#include "pyWarpX.H"
#include "Particles/MultiParticleContainer.H"
#include "Particles/ParticleBoundaryBuffer.H"

#include <WarpX.H>

Expand Down Expand Up @@ -66,7 +67,12 @@ void init_WarpX (py::module& m)
py::return_value_policy::reference_internal,
"Return MultiFabs by name, e.g., 'Efield_aux[x][l=0]', 'Efield_cp[x][l=0]', ..."
)
.def("multi_particle_container", &WarpX::GetPartContainer,
.def("multi_particle_container",
[](WarpX& wx){ return &wx.GetPartContainer(); },
py::return_value_policy::reference_internal
)
.def("get_particle_boundary_buffer",
[](WarpX& wx){ return &wx.GetParticleBoundaryBuffer(); },
py::return_value_policy::reference_internal
)

Expand Down
7 changes: 0 additions & 7 deletions Source/Python/WarpXWrappers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,6 @@
return myspc.sumParticleCharge(local);
}

int warpx_getParticleBoundaryBufferSize(const char* species_name, int boundary, bool local)
{
const std::string name(species_name);
auto& particle_buffers = WarpX::GetInstance().GetParticleBoundaryBuffer();
return particle_buffers.getNumParticlesInContainer(species_name, boundary, local);
}

int** warpx_getParticleBoundaryBufferScrapedSteps(const char* species_name, int boundary, int lev,
int* num_tiles, int** particles_per_tile)
{
Expand Down
4 changes: 3 additions & 1 deletion Source/Python/pyWarpX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ namespace py = pybind11;


// forward declarations of exposed classes
void init_MultiParticleContainer (py::module&);
void init_ParticleBoundaryBuffer (py::module&);
void init_WarpXParIter (py::module&);
void init_WarpXParticleContainer (py::module&);
void init_MultiParticleContainer (py::module&);
void init_WarpX(py::module&);

PYBIND11_MODULE(PYWARPX_MODULE_NAME, m) {
Expand All @@ -58,6 +59,7 @@ PYBIND11_MODULE(PYWARPX_MODULE_NAME, m) {
// note: order from parent to child classes
init_WarpXParticleContainer(m);
init_WarpXParIter(m);
init_ParticleBoundaryBuffer(m);
init_MultiParticleContainer(m);
init_WarpX(m);

Expand Down

0 comments on commit d40e431

Please sign in to comment.