ocelot-collab / ocelot

OCELOT is a multiphysics simulation toolkit designed for studying FEL and storage ring-based light sources.
GNU General Public License v3.0
84 stars 58 forks source link

p_array dump at multiple locations #144

Closed jonasbjorklundsvensson closed 2 years ago

jonasbjorklundsvensson commented 2 years ago

Hi,

To get a better overview of the bunch evolution through my simulations, I might want to dump the bunch particles at multiple locations, for example before and after each bunch compressor as well as at the end of the beamline. In Elegant, which I'm used to since before, I would just define a bunch of watchpoints and place them around. At the moment, I can't really see a better way in Ocelot than making a loop, using a list of elements to define where I want to output the particles, tracking between each pair of elements and using the dumped p_array from the previous iteration as input for the next iteration in the loop.

This seems overly clunky to me, and it also seems like it might be a lot more hassle to plot the lattice and beam functions along the full simulation.

Am I missing something here? Best regards, Jonas

st-walker commented 2 years ago

Yes you are missing something. Attach a PhysicsProc to one or more Marker instances in the sequence (unique for each marker) : SaveBeam (to immediately write to file) or CopyBeam (To simply store ParticleArray instance in the CopyBeam instance for later retrieval) in ocelot.cpbd.physics_proc.

There is also a function that exists in ocelot for helping to add markers to sequences more easily (i.e. not by hand). I can't remember its name.

jonasbjorklundsvensson commented 2 years ago

Hi and thanks for the quick reply!

Ok, I had evidently missed this functionality completely. At the moment I don't need to actually save anything to drive because I'm just testing stuff out, so CopyBeam looks good - however, I can't find this function, only SaveBeam. Is this a new function? I'm running the latest version of Ocelot available through conda, but it's a lower version number than the latest here on Github.

Also, I'm not very experienced in either Python or Ocelot, so it's not obvious to me how to 'attach' something to an object -advice would be much appreciated. Wouldn't it make more sense to give this information as input to, for example, the MagneticLattice function when building the lattice? One already gives the start and stop elements, seems like it would make sense to define all output locations there, for example through a list of elements?

Best regards, Jonas

st-walker commented 2 years ago

I recommend you check the tutorials here:

https://github.com/ocelot-collab/ocelot#tutorials

For example a tutorial involving attaching a physics process:

http://nbviewer.jupyter.org/github/ocelot-collab/ocelot/blob/master/demos/ipython_tutorials/5_CSR.ipynb

To be clear "attach" is not a Python term, it is perhaps not even an ocelot term, it's just a term I used which is perhaps slightly ambiguous. The way to add/attach a physics process to one or more elements is by calling the add_physics_proc(physics_process_instance, start_element, _end_element) on the Navigator instance of choice. Each Navigator has a MagneticLattice instance and each MagneticLattice instance has a sequence (just any iterable of elements actually).

With regards CopyBeam, it's on dev branch. I recommend you use this branch rather than master, at least that's what I do. I've never installed Ocelot via conda, I always cloned the repo and installed the cloned repo using pip.

Wouldn't it make more sense to give this information as input to, for example, the MagneticLattice function when building the lattice? One already gives the start and stop elements, seems like it would make sense to define all output locations there, for example through a list of elements?

Perhaps, but the choice was made that there's nothing special about writing or saving ParticleArray—it's just another physics process. And in that sense it would not make sense to include it in the MagneticLattice instance, because physics processes are the purview of the Navigator alone. I prefer it this way for other reasons, basically it's a nicer design and more modifiable.

Anyway if you want help to insert markers into the sequence, then look in ocelot.cpbd.magnetic_lattice: there you will find such useful functions as insert_markers_by_name, insert_markers_by_type and insert_markers_by_predicate. They return the Marker instances that can then be fed into add_physics_proc method. Read the documentation to understand how they work. The alternative is that you do it "by hand" when the sequence is defined. Also this is on dev branch.

Anyway just look at the demos and adapt them for what you want them.

jonasbjorklundsvensson commented 2 years ago

Hi,

Many thanks for the thorough explanations, and insight into why certain choices were made! I find that such "behind-the-scenes" bits of info do help to understand a code.

I'll copy the dev branch and start implementing CopyBeam the way you suggest right away. Best regards, Jonas

jonasbjorklundsvensson commented 2 years ago

Hi again @st-walker

Seems like something might be broken in the latest version. I run pip install git+https://github.com/ocelot-collab/ocelot.git@dev to install, but when I afterwards try to import ocelot I get this error message image

Also, the version number still says 20.11 and not 21.12.

Any suggestions? -- Jonas

st-walker commented 2 years ago

What version of Python are you using? I didn't write that bit of code but using native types (e.g. tuple[float, float]) for type hiting is not supported until python 3.9. So I guess we have an implicit minimum dependency of 3.9 and have effectively deprecated previous versions @sergey-tomin ? I am probably guilty of writing code such as this myself. But in any case use 3.9 at least for now, if you can.

jonasbjorklundsvensson commented 2 years ago

@st-walker I downgraded my original install to Python 3.8 to be able to install Ocelot in the first place, the version from conda didn't support anything higher. I guess I can change the Python version.

st-walker commented 2 years ago

OK understood. I would advise simply cloning the repo and installing it that way. Conda is nice but not truly necessary for ocelot, in that it has no non-python external dependencies (which is what conda is greatest for).

st-walker commented 2 years ago

Did you get it to work @jonasbjorklundsvensson ? I will close the issue if so...

jonasbjorklundsvensson commented 2 years ago

Hi,

Yeah, I got around to doing it just this morning and it seems like everything is working out, so consider the issue closed. Thanks again!

-- Jonas