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

Open
wants to merge 2 commits into
base: development
Choose a base branch
from

Conversation

archermarx
Copy link
Contributor

@archermarx archermarx commented Oct 24, 2024

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

@RemiLehe RemiLehe self-assigned this Oct 28, 2024
@RemiLehe RemiLehe self-requested a review October 28, 2024 23:03
@dpgrote
Copy link
Member

dpgrote commented Nov 5, 2024

I would suggest handling this in a different way. Now, the issue is that the libwarpx.warpx is accessed in the __init__ routine. A change would be to only access libwarpx.warpx when it is needed. Perhaps make particle_container a property that gets the underlying particle container when needed. The user can then create the ParticleContainerWrapper at any time. The message this PR adds would then go in the particle_container property get method. This is how the field wrappers are setup.

@archermarx
Copy link
Contributor Author

Got it, thanks for the feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants