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

Plot of transverse phase space #231

Closed dmargarone closed 4 months ago

dmargarone commented 4 months ago

Dear All,

I want to make a plot of x-px.

Do you think the commands below is correct for making such plots ?

X = p_array.x()                                  # x-coordinate of particles

Px = p_array.px()                                # normalized transverse momentum of the particles

plt.scatter( X, Px, c=Px, s=3, cmap='hot')       # scatter plot

Thank you. Daniela

sergey-tomin commented 4 months ago

Hi,

I did not use plt.scatter for awhile but I think it is correct. you can also use function show_density(x, y) function in from ocelot.gui.accelrator (https://github.com/ocelot-collab/ocelot/blob/master/ocelot/gui/accelerator.py)

Cheers, Sergey.

dmargarone commented 4 months ago

Hi @sergey-tomin

show density gave me different result than the expected one

x_Px
st-walker commented 4 months ago

Do you expect them to be the same? Look at the code of show_density, it is very basic matplotlib stuff. There's not much room for error unless it is somehow related to comparing histograms to scatter plots (one is binning the other is not). otherwise the problem cannot be in plotting.

dmargarone commented 4 months ago

Hi @st-walker,

Thanks a lot for your kind reply. Even scatter plot gives the same result as obtained with show_density.

I made plot with

X = p_array_init.x()
Px = p_array_init.px()
plt.scatter(X, Px, c=Px, s=3)

I am expecting that the x-Px plot should show a tilt. I used the following electron beam distribution:

p_array_init = generate_parray(sigma_x=20.0e-6, sigma_y=20.0e-6, sigma_tau=0.6e-3,
                               sigma_px=5.1e-4, energy=0.055, sigma_p=3.0e-3, chirp=0.008,
                               nparticles=20000, charge=500.0e-12)
sergey-tomin commented 4 months ago

Hi, why you are expecting tilt? scatter and show_density show the same result. There is no correlation between x and px. Function generate_parra() would introduce correlation in transverse plane if Twiss parameters are defined. But if chirp is not zero you can see correlation in longitudinal phase space (tau-p).

Screenshot 2024-05-01 at 19 49 04

dmargarone commented 4 months ago

Hi @st-walker,

Here is the reason why I was expecting a tilt in the x-px plot. I found a literature about beam compression and I was trying to see if I can reproduce some of the results or not. I have attached the literature file. I was trying to make similar figures like Fig. 2.22, Fig. 2.23, Fig. 2.24 and Fig. 2.25.

dmargarone commented 4 months ago

Hi @st-walker,

did you see anything wrong in Fig. 2.22, Fig. 2.23, Fig. 2.24 and Fig. 2.25. in the beamcompression.pdf file. All these plots are generated with ELEGANT code. I tried to reproduce these figures.

st-walker commented 4 months ago

OK but where is the data for the plot coming from? Do you have the beam data file for the same plot as 2.22?

you say you are trying to reproduce a result, but the plot is just an "initial phase space"? So there is no result, it is the input, nothing has happened yet I would imagine. I did not look at the pdf very closely though.

I don't understand why you expect your plot to match the 2.22 of the pdf, unless you have the same data file.

dmargarone commented 4 months ago

Hi @st-walker ,

the initial parameters are given in Table 2.6 (energy and charge are same through out the simulation: E = 55 MeV and charge 0.5 nC, as given in Table 2.2).

The momentum chirp for figures 2.22-2.30 is 10 per meter.

st-walker commented 4 months ago

Then your problem is that you are somehow not generating the bunch properly from Table 2.6.

Is this the code you are using? If so, it is clear to me that it will not do it correctly:

p_array_init = generate_parray(sigma_x=20.0e-6, sigma_y=20.0e-6, sigma_tau=0.6e-3,
                               sigma_px=5.1e-4, energy=0.055, sigma_p=3.0e-4, chirp=0.008,
                               nparticles=20000, charge=500.0e-12)

You need to set the tws kwarg to make sure that the correlations are computed correctly when generating the beam from the Twiss parameters.

Alternatively use cov_matrix_from_twiss and cov_matrix_to_parray from the cpbd.beam module together (I wrote these two functions so I am confident that they work).

dmargarone commented 4 months ago

Hi @st-walker,

thank you so so much for helping. I am trying to understand these ,but I received an error: name 'ex' is not defined. Please, could you suggest me how to fix this error.

st-walker commented 4 months ago

yeah you need to define ex, which is the geometric emittance. eg is the geometric emittance.

just write ex = ey = eg underneath the eg definition and itll work i guess or at least crash later on

dmargarone commented 4 months ago

Hi @st-walker,

Thank you. I will proceed as you suggested. Hope I can learn something today.

dmargarone commented 4 months ago

Hi @st-walker,

does parray0s is the initial beam distribution ?

If I want to see into it with print(parray0s) it gives <ocelot.cpbd.beam.ParticleArray object at 0x1360d1810.

st-walker commented 4 months ago

ok but what is in the interesting_bunch_lengths?

parray0s is a list, it cnanot give what it says you give.

you can plot each one, you can track each one.. it is up to you. if you only want one remove the list comprehension and just pick a single constant sigma_tau.

dmargarone commented 4 months ago

Hi @st-walker

I want to creat a particle array from twiss parameters given above. If I do print(np.size(parray0s)) it gives me the correct value equal to nparticles.

When I used the p_array_init = generate_parray( ...... ) I can directly print the p_array_init values. And can use the p_array_init for further analysis.

But here, I got stuck after that.

st-walker commented 4 months ago

yes in Python [x for x in Y] notation is a list comprehension, so here parray0s is a list.

dmargarone commented 4 months ago

Hi @st-walker

I couldn't get it well. But let me try, if I fail I will return back to you.

st-walker commented 4 months ago

good luck, to be honest, it seems some of these questions are not ocelot questions but rather Python questions. if you sit with repl (i recommend ipython) all day and go line by line I am sure you will figure it all out and learn a lot.

dmargarone commented 4 months ago

Hi @st-walker

I think I have a good news to share with you. Finally, I am able to reproduce the tilted figures.

All credit goes to you. You are so helpful and kind. A very big thanks to you.

Now, only one thing is remaining. How to include energy or momentum chirp ?

st-walker commented 4 months ago

How to include energy or momentum chirp

You have the particle array, you have the longitudinal positions of the particles parray.tau() and you have the momentum offsets of the particles parray.p() so you need to just modulate the last row (the momentum) of the parray.rparticles array based on each particle's longitudinal position (the second from last row of parray.rparticles).

dmargarone commented 4 months ago

Hi @st-walker,

although the concept is simple (p = p0 + chirp*tau), this part looks confusing to me. Anyways, I will try and if I get completely messed up, I may need your advice again.

dmargarone commented 4 months ago

Hi @st-walker,

Maybe now I could follow your suggestion. I could make the figures as expected. Also, this means p_array_init.rparticles re-present total phase space (x, px , y, py, tau, p) .. is it right ?

dmargarone commented 4 months ago

Hi @st-walker @sergey-tomin ,

Thank you so much to both of you for being so kind and helpful every time.

with lots of best wishes, Daniela