Skip to content

Commit 555f435

Browse files
Python: useful error when initialize_warpx not called before creating ParticleContainerWrapper (#5412)
Currently, WarpX is initialized when `sim.step` is called, or when the user calls `initialize_warpx`. However, if the user tries to create a `ParticleContainerWrapper` before this point, they get an error along the following lines: ``` File "/home/marksta/projects/warpx-ionization/picmi.py", line 185, in <module> sim.run() File "/home/marksta/projects/warpx-ionization/picmi.py", line 179, in run self.elec_wrapper = particle_containers.ParticleContainerWrapper(self.electrons.name) File "/home/marksta/.local/lib/python3.10/site-packages/pywarpx/particle_containers.py", line 29, in __init__ mypc = libwarpx.warpx.multi_particle_container() File "/home/marksta/.local/lib/python3.10/site-packages/pywarpx/_libwarpx.py", line 46, in __getattr__ return self.__getattribute__(attribute) AttributeError: 'LibWarpX' object has no attribute 'warpx' ``` This is confusing. When I got this, I assumed I had maybe installed WarpX wrong. I added a catch for this exception that re-raises it with some additional context that should help direct the user to call `initialize_warpx` --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 16347e6 commit 555f435

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

Python/pywarpx/particle_containers.py

+26-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,21 @@ class ParticleContainerWrapper(object):
2424

2525
def __init__(self, species_name):
2626
self.name = species_name
27+
self._particle_container = None
28+
29+
@property
30+
def particle_container(self):
31+
if self._particle_container is None:
32+
try:
33+
mypc = libwarpx.warpx.multi_particle_container()
34+
self._particle_container = mypc.get_particle_container_from_name(
35+
self.name
36+
)
37+
except AttributeError as e:
38+
msg = "This is likely caused by attempting to access a ParticleContainerWrapper before initialize_warpx has been called"
39+
raise AttributeError(msg) from e
2740

28-
# grab the desired particle container
29-
mypc = libwarpx.warpx.multi_particle_container()
30-
self.particle_container = mypc.get_particle_container_from_name(self.name)
41+
return self._particle_container
3142

3243
def add_particles(
3344
self,
@@ -758,7 +769,18 @@ class ParticleBoundaryBufferWrapper(object):
758769
"""
759770

760771
def __init__(self):
761-
self.particle_buffer = libwarpx.warpx.get_particle_boundary_buffer()
772+
self._particle_buffer = None
773+
774+
@property
775+
def particle_buffer(self):
776+
if self._particle_buffer is None:
777+
try:
778+
self._particle_buffer = libwarpx.warpx.get_particle_boundary_buffer()
779+
except AttributeError as e:
780+
msg = "This is likely caused by attempting to access a ParticleBoundaryBufferWrapper before initialize_warpx has been called"
781+
raise AttributeError(msg) from e
782+
783+
return self._particle_buffer
762784

763785
def get_particle_boundary_buffer_size(self, species_name, boundary, local=False):
764786
"""

0 commit comments

Comments
 (0)