Skip to content

Commit

Permalink
Wrap function num_real_comp
Browse files Browse the repository at this point in the history
  • Loading branch information
RemiLehe committed Jul 14, 2023
1 parent 1a83d4c commit 3d8ab0c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 32 deletions.
46 changes: 24 additions & 22 deletions Python/pywarpx/_libwarpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,10 @@ def get_nattr_species(self, species_name):
species_name: str
Name of the species
'''

return self.libwarpx_so.warpx_nCompsSpecies(
ctypes.c_char_p(species_name.encode('utf-8')))
warpx = self.libwarpx_so.get_instance()
mpc = warpx.multi_particle_container()
pc = mpc.get_particle_container_from_name(species_name)
return pc.num_real_comps()

def amrex_init(self, argv, mpi_comm=None):
if mpi_comm is None or MPI is None:
Expand Down Expand Up @@ -421,24 +422,22 @@ def add_particles(self, species_name, x=None, y=None, z=None, ux=None, uy=None,
# --- Broadcast scalars into appropriate length arrays
# --- If the parameter was not supplied, use the default value
if lenx == 1:
x = np.full(maxlen, (x or 0.), self._numpy_particlereal_dtype)
x = np.full(maxlen, (x or 0.))
if leny == 1:
y = np.full(maxlen, (y or 0.), self._numpy_particlereal_dtype)
y = np.full(maxlen, (y or 0.))
if lenz == 1:
z = np.full(maxlen, (z or 0.), self._numpy_particlereal_dtype)
z = np.full(maxlen, (z or 0.))
if lenux == 1:
ux = np.full(maxlen, (ux or 0.), self._numpy_particlereal_dtype)
ux = np.full(maxlen, (ux or 0.))
if lenuy == 1:
uy = np.full(maxlen, (uy or 0.), self._numpy_particlereal_dtype)
uy = np.full(maxlen, (uy or 0.))
if lenuz == 1:
uz = np.full(maxlen, (uz or 0.), self._numpy_particlereal_dtype)
uz = np.full(maxlen, (uz or 0.))
if lenw == 1:
w = np.full(maxlen, (w or 0.), self._numpy_particlereal_dtype)
w = np.full(maxlen, (w or 0.))
for key, val in kwargs.items():
if np.size(val) == 1:
kwargs[key] = np.full(
maxlen, val, self._numpy_particlereal_dtype
)
kwargs[key] = np.full(maxlen, val)

# --- The number of built in attributes
# --- The three velocities
Expand All @@ -449,7 +448,7 @@ def add_particles(self, species_name, x=None, y=None, z=None, ux=None, uy=None,

# --- The number of extra attributes (including the weight)
nattr = self.get_nattr_species(species_name) - built_in_attrs
attr = np.zeros((maxlen, nattr), self._numpy_particlereal_dtype)
attr = np.zeros((maxlen, nattr))
attr[:,0] = w

# --- Note that the velocities are handled separately and not included in attr
Expand All @@ -460,14 +459,14 @@ def add_particles(self, species_name, x=None, y=None, z=None, ux=None, uy=None,
nattr_int = 0
attr_int = np.empty([0], ctypes.c_int)

# Iff x/y/z/ux/uy/uz are not numpy arrays of the correct dtype, new
# array copies are made with the correct dtype
x = x.astype(self._numpy_particlereal_dtype, copy=False)
y = y.astype(self._numpy_particlereal_dtype, copy=False)
z = z.astype(self._numpy_particlereal_dtype, copy=False)
ux = ux.astype(self._numpy_particlereal_dtype, copy=False)
uy = uy.astype(self._numpy_particlereal_dtype, copy=False)
uz = uz.astype(self._numpy_particlereal_dtype, copy=False)
# TODO: expose ParticleReal through pyAMReX
# and cast arrays to the correct types, before calling add_n_particles
# x = x.astype(self._numpy_particlereal_dtype, copy=False)
# y = y.astype(self._numpy_particlereal_dtype, copy=False)
# z = z.astype(self._numpy_particlereal_dtype, copy=False)
# ux = ux.astype(self._numpy_particlereal_dtype, copy=False)
# 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()
Expand Down Expand Up @@ -763,6 +762,9 @@ 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()
pc = mpc.get_particle_container_from_name(species_name)

return self.libwarpx_so.warpx_getParticleCompIndex(
ctypes.c_char_p(species_name.encode('utf-8')),
Expand Down
1 change: 1 addition & 0 deletions Source/Python/WarpXParticleContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ void init_WarpXParticleContainer (py::module& m)
py::arg("offset"), py::arg("np_to_depose"),
py::arg("thread_num"), py::arg("lev"), py::arg("depos_lev")
)
.def("num_real_comps", &WarpXParticleContainer::NumRealComps)
;
}
2 changes: 0 additions & 2 deletions Source/Python/WarpXWrappers.H
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ extern "C" {

int warpx_nComps();

int warpx_nCompsSpecies(const char* char_species_name);

int warpx_SpaceDim();

void amrex_init (int argc, char* argv[]);
Expand Down
8 changes: 0 additions & 8 deletions Source/Python/WarpXWrappers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,6 @@
return PIdx::nattribs;
}

int warpx_nCompsSpecies(const char* char_species_name)
{
auto & mypc = WarpX::GetInstance().GetPartContainer();
const std::string species_name(char_species_name);
auto & myspc = mypc.GetParticleContainerFromName(species_name);
return myspc.NumRealComps();
}

int warpx_SpaceDim()
{
return AMREX_SPACEDIM;
Expand Down

0 comments on commit 3d8ab0c

Please sign in to comment.