desy-ml / cheetah

Fast and differentiable particle accelerator optics simulation for reinforcement learning and optimisation applications.
https://cheetah-accelerator.readthedocs.io
GNU General Public License v3.0
27 stars 12 forks source link

What is the purpose of Beam.empty? #93

Open cr-xu opened 8 months ago

cr-xu commented 8 months ago

In the ParticleBeam, there are some locations where it's checked whether self is not Beam.empty, e.g. https://github.com/desy-ml/cheetah/blob/b92502966f01797889697f1dcba46a2667328f8c/cheetah/particles.py#L1023

But this is never the case, since one cannot initialize the ParticleBeam without any argument. Also, the Beam.empty is simply a str, rather than an actual empty Beam object. This is very confusing:

>>> emptybeam = ParticleBeam(torch.empty(1,7), energy=torch.tensor([])). # The best one can do to initialize ParticleBeam without anything?
>>> emptybeam is Beam.empty
False
jank324 commented 8 months ago

I implemented this at some point, maybe as a quick fix for some issue I had. I agree that an empty tensor makes more sense ... we could then have a property is_empty to make checking for it nice and readable. With ParameterBeam, we could add a variable is_empty which would be set to True by an active screen or such, i.e. the elements that obviously block the entire beam. Does that make sense?

cr-xu commented 8 months ago

I was just considering the empty beam as a placeholder for creating a beam object but with nothing filled. Hmm... It's true that it can actually be used to see if the beam survived tracking.

However, this makes things kind of complicated..? I would assume that one still likes to observe the beam simultaneously at multiple screens, as it's just a simulation.

jank324 commented 8 months ago

Ah ... an empty constructor actually makes sense to have as well.

I see both use cases. At the time, I was imagining that RL agents may need to know about that they are blocking the beam with a screen. If we want both features, we could add an is_blocking property to screens. Then maybe we would have a method with Segment that would change the use case mode and adjust all is_blocking properties accordingly.