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

Need a way to save particle state in a dict #3916

Closed
RudolfWeeber opened this issue Sep 28, 2020 · 1 comment · Fixed by #4060
Closed

Need a way to save particle state in a dict #3916

RudolfWeeber opened this issue Sep 28, 2020 · 1 comment · Fixed by #4060

Comments

@RudolfWeeber
Copy link
Contributor

For MC simulations, it would be useful to be able to save all writable particle properties in a dict. Then, it would be easy to restore the particle later, if an MC move is rejected.
Came up during Gibbs ensemble work.

p = random_particle()
old_part_state = p.state_as_dict()
...
p.update(old_part_state)
@RudolfWeeber
Copy link
Contributor Author

RudolfWeeber commented Dec 7, 2020

The necessary code actually exists in ParticleHandle.__getstate__() in src/python/espressomd/particle_handle.pyx.
To resolve this ticket:

  • Rename the function to to_dict() or similar (adapt the docstring, mention in doc). Let the particle id appear in the dict.
  • Write a new __getstate__() that returns the result of to_dict() with the id key removed.
  • Write a test which checks that
p = system.part.add(...) # lots of particle properties
dict = p.to_dict()
p.remove()
system.part.add(dict)

results in a particle with the same properties and id

@kodiakhq kodiakhq bot closed this as completed in #4060 Dec 21, 2020
kodiakhq bot added a commit that referenced this issue Dec 21, 2020
Fixes #3916

Add functions: `ParticleHandle.to_dict()` and `ParticleSlice.to_dict()`

Description of changes:
Returns the particles attributes as a dictionary, such that it can be used to save the particle data and recover it by using

```python
p = system.part.add(...)
particle_dict = p.to_dict()
system.part.add(particle_dict)
```
Codes are mainly written by @RiccardoFrenner
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants