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

Atavedata #678

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open

Atavedata #678

wants to merge 11 commits into from

Conversation

ZeusMarti
Copy link
Contributor

The file has been changed to:

  1. Avoid large errors on small gradient focusing elements (like quad trim coils in sextupoles).
  2. Include orbit generated feed-down effects (orbit at sextupoles).
  3. Include rotation and displacement effects on the focusing strength (displaced sextupoles or rotated quadrupoles).
  4. Include dispersion formula for general combined function magnets (Formula from A.W.Chao Handbook of Accelerator Physics and Engineering Ed.2002, The Wiedemann formula is wrong).

This tries to solve issue #399

@swhite2401
Copy link
Contributor

Hello @ZeusMarti , this looks nice should we add this to the python too?

@ZeusMarti
Copy link
Contributor Author

Hi @swhite2401, sure, I can take care of it but I'm not python fluent and we are quite busy with alba-II, it can take some time. Don't take me wrong, I would be happy, for me it would be good a way to force myself to get more familiar with pyat. If you don't feel like waiting for me to go through the exercise please feel free to add the changes yourself.
Let me know!

@swhite2401
Copy link
Contributor

No rush! If you want to train yourself it is very good! Let me know in case you would need any help!

@ZeusMarti
Copy link
Contributor Author

I tested the two implementations in Matlab and Python. Please take a look at it, it should be ready to be reviewed.

Python avllinopt:

Python_avlinopt

Matlab atavedata:
matlab_atavedata_test

@lfarv
Copy link
Contributor

lfarv commented Dec 14, 2023

Results look nice. Concerning the code, please look at the review above !

@ZeusMarti
Copy link
Contributor Author

Results look nice. Concerning the code, please look at the review above !

Sorry Laurent, I do not see the review!

@oscarxblanco
Copy link
Contributor

Dear @lfarv , neither I see your reviews or requests.
You might need to finish the review to make it public (something similar happened to me while reviewing a few weeks ago)

Comment on lines 99 to 107
function avebeta=betadrift(beta0,alpha0,L)
gamma0=(alpha0.*alpha0+1)./beta0;
avebeta=0.5*(beta0+beta1)-gamma0.*L.*L/6;
avebeta=beta0-alpha0.*L+gamma0.*L.*L/3;
end

function avebeta=betafoc(beta1,alpha0,alpha1,K,L)
gamma1=(alpha1.*alpha1+1)./beta1;
avebeta=0.5*((gamma1+K.*beta1).*L+alpha1-alpha0)./K./L;
function avebeta=betafoc(beta0,alpha0,K,L)
gamma0=(alpha0.*alpha0+1)./beta0;
avebeta=((beta0+gamma0./K).*L+(beta0-gamma0./K).*sin(2.0*sqrt(K).*L)./sqrt(K)/2.0+(cos(2.0*sqrt(K).*L)-1.0).*alpha0./K)./L/2.0;
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change the average beta functions: using final values gives simpler (and faster) expressions. These are analytically correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See issue 399, using final values diverges for small K values.

atmat/atphysics/atavedata.m Show resolved Hide resolved
atmat/atphysics/atavedata.m Show resolved Hide resolved
pyat/at/physics/linear.py Outdated Show resolved Hide resolved
@lfarv
Copy link
Contributor

lfarv commented Dec 15, 2023

Hi @ZeusMarti and @oscarxblanco. Is it better now? I was confused because for me it appears in the conversation… Sorry.

@oscarxblanco
Copy link
Contributor

oscarxblanco commented Dec 18, 2023

Dear @lfarv , yes. Now the review is visible to me.

Comment on lines 1233 to 1241
L = numpy.array([el.Length for el in ring_selected])
K = numpy.array([get_strength(el) for el in ring_selected])
sext_strength = numpy.array([get_sext_strength(el) for el in ring_selected])
roll = numpy.array([get_roll(el) for el in ring_selected])
ba = numpy.array([get_bendingangle(el) for el in ring_selected])
e1 = numpy.array([get_e1(el) for el in ring_selected])
Fint = numpy.array([get_fint(el) for el in ring_selected])
gap = numpy.array([get_gap(el) for el in ring_selected])
dx = numpy.array([get_dx(el) for el in ring_selected])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sext_strength, roll and dx are not used anywhere, apparently

Comment on lines 1264 to 1265
ClosedOrbit=lindata.closed_orbit.copy()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ClosedOrbit is not used

@lfarv
Copy link
Contributor

lfarv commented Dec 19, 2023

@ZeusMarti : I get more that 100 warnings about code not respecting PEP8. Most of them are:

PEP 8: E225 missing whitespace around operator
PEP 8: E303 too many blank lines (3)
PEP 8: E231 missing whitespace after ','
PEP 8: E128 continuation line under-indented for visual indent
PEP 8: E501 line too long (106 > 88 characters)

You should use an IDE which warns about that (ex. PyCharm or VSCode)

@ZeusMarti
Copy link
Contributor Author

@lfarv , I implemented your last comments.

I realized that I have not properly tested it adding magnet movements, rolls and closed orbit errors, I'll do it after holidays.

Also, the code logic is a bit cumbersome, there are different behaviors for thin or long, focusing or not, drifts. Any suggestions on how to make it more clear? Maybe it just needs more comments?

@lfarv
Copy link
Contributor

lfarv commented Dec 20, 2023

Also, the code logic is a bit cumbersome...

Agreed, but since you are at the moment the most familiar with this code, go on… My only concern is about the "epsilon" trick for the focusing strength. Is not it possible to correctly treat the 0-strength quadrupoles?

I realized that I have not properly tested it adding magnet movements, rolls and closed orbit errors, I'll do it after holidays.

Excellent !

@ZeusMarti
Copy link
Contributor Author

Hi @lfarv,

I'm sorry I have not been able to advance much, however I noticed a peculiar behaviour of the function, both in Matlab and python in the master branch and this branch. Since the input parameter refpts is used only to create a bolean array, if refpts is not sorted that information is lost in the calculation and the results are given always assuming a sorted order.

It was the cause of some trouble for me so I'm wondering if this behavior should be kept. We could simply unsort the arrays at the end or alternatively generate a warning in case of non sorted refpts and output the sorted refpts. Which is your opinion on this?

@lfarv
Copy link
Contributor

lfarv commented Mar 11, 2024

Hi @ZeusMarti : the refpts argument, if integer, should always be ordered, and the behaviour you get should be similar in many functions. This is specified in the documentation:

**Selecting elements in a lattice:**

The *refpts* argument allows functions to select locations in the lattice. The
location is defined as the entrance of the selected element. *refpts* may be:

#. an integer in the range *[-len(lattice), len(lattice)-1]*
   selecting an element according to the python indexing rules.
   As a special case, *len(lattice)* is allowed and refers to the end
   of the last element,
#. an ordered list of such integers without duplicates,
#. a numpy array of booleans of maximum length *len(lattice)+1*,
   where selected elements are :py:obj:`True`,
...

The problem is that it would be difficult (and time consuming) to check the validity of refpts everywhere…

Now, for this PR, it's up to you !

@ZeusMarti
Copy link
Contributor Author

Ah, sorry! My bad. Then I see no point in implementing a different behavior for atavedata.m. hopefully I can address the final details of this PR shortly.

@ZeusMarti
Copy link
Contributor Author

Sorry it is taking me so long, we have been very busy lately, I tested the scrips again with errors (up to 100um in x-y and 100urad rotation) and the averages hold very well both in Matlab and Python. I think it is ready fore some more review from your side.
Matlab:
Python_avlinopt_errors
Python:
matlab_atavedata_test_errors

Copy link
Contributor

@lfarv lfarv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks very good! Any comment, @swhite2401?

Copy link
Contributor

@swhite2401 swhite2401 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good for me

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.

None yet

4 participants