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

Python: useful error when initialize_warpx not called before creating ParticleContainerWrapper #5412

Merged
merged 8 commits into from
Jan 11, 2025
30 changes: 26 additions & 4 deletions Python/pywarpx/particle_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,21 @@ class ParticleContainerWrapper(object):

def __init__(self, species_name):
self.name = species_name
self._particle_container = None

@property
def particle_container(self):
if self._particle_container is None:
try:
mypc = libwarpx.warpx.multi_particle_container()
self._particle_container = mypc.get_particle_container_from_name(
self.name
)
except AttributeError as e:
msg = "This is likely caused by attempting to access a ParticleContainerWrapper before initialize_warpx has been called"
Copy link
Member

@ax3l ax3l Jan 16, 2025

Choose a reason for hiding this comment

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

Thank you for this PR! Sorry for just seeing it now, I was just on parental leave when the PR was opened.

I thought we could also add one more fallback, right in the library loader, where we assign the libwarpx.warpx attribute.

In essence, we could assign an initial attribute that throws when accessed with a more distinct error message ("initialize_warpx was not yet called!"), to catch all. More specific exceptions like this one are great to add and keep.

One initialization, we then just overwrite the attribute.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh neat idea! I can open a new PR shortly!

raise AttributeError(msg) from e

# grab the desired particle container
mypc = libwarpx.warpx.multi_particle_container()
self.particle_container = mypc.get_particle_container_from_name(self.name)
return self._particle_container

def add_particles(
self,
Expand Down Expand Up @@ -758,7 +769,18 @@ class ParticleBoundaryBufferWrapper(object):
"""

def __init__(self):
self.particle_buffer = libwarpx.warpx.get_particle_boundary_buffer()
self._particle_buffer = None

@property
def particle_buffer(self):
if self._particle_buffer is None:
try:
self._particle_buffer = libwarpx.warpx.get_particle_boundary_buffer()
except AttributeError as e:
msg = "This is likely caused by attempting to access a ParticleBoundaryBufferWrapper before initialize_warpx has been called"
raise AttributeError(msg) from e

return self._particle_buffer

def get_particle_boundary_buffer_size(self, species_name, boundary, local=False):
"""
Expand Down
Loading