Skip to content

Conversation

@lfarv
Copy link
Contributor

@lfarv lfarv commented Aug 15, 2024

As discussed here, the origin of the energy parameters in the integrators is ambiguous. To make that simpler and clearer, the strategy in this PR is the following:

  1. For the entry point common to Matlab and python (trackFunction): this is the only entry point in python, and the one used for lattice tracking in Matlab.

    • python: the energy comes from the parameters structure and the element's energy attribute is always ignored,
    • Matlab: the energy comes from the parameters structure if available, and otherwise from the element's energy attribute.

    This is implemented by a new atEnergy macro and is generalized in all the integrators requiring energy. The previous behavior was giving priority to the element's attribute.
    The energy is taken from the parameters on each call, rather than being stored on the elements parameters, which make it suitable for ramping (Energy ramp #810).

  2. For the Matlab-specific entry point (MexFunction): it is used in Matlab for tracking in a single element (findelemm66, findelemm44). It is now necessary to provide the energy to the function. So a 3rd optional argument is introduced, as a structure containing Energy, Particle, and possibly others in the future. Unfortunately, allowing this 3rd argument implied the modification of all integrators (nrhs >= 2 instead of nrhs == 2), though only some of them use this 3rd argument. On top of that, a new Matlab function is created: rout=elempass(elem,rin), with optional keyword arguments Energy and Particle. It is similar to the python element_track function.

Consequences:

  • the behavior is unchanged in python: the energy was already always provided by the Lattice energy. But the change in priority can affect cases where there was a conflict between element and lattice energies. These are considered as erroneous cases, and the new new behavior of completely ignoring the element's energy is safer.
  • in Matlab, the behavior is similar to python if there is a RIngParam element. If not, the energy is taken from elements, as before.

Removing the Energy attribute of all elements was tested in both Matlab and python. Such a new lattice is added in the automatic test sequence.

Though the modifications are moderate, any intervention on integrators is critical and requires a lot of testing. Please help!

@lfarv lfarv added enhancement Matlab For Matlab/Octave AT code Python For python AT code C For C code / pass methods labels Aug 15, 2024
@lfarv lfarv marked this pull request as draft August 15, 2024 15:51
@lfarv
Copy link
Contributor Author

lfarv commented Aug 15, 2024

Added 2 new test lattices identical to hmba:

  • noenergy.mat has no energy field anywhere (except in RF cavities: see below)
  • noringparam.mat has no RingParam element
    This is to ensure that the element's Energy attribute is no more necessary, but is still scanned when reading the file to guess the lattice energy if there is no RingParam

Note: the Energy field is still required to construct a RFCavity element. It will be set as optional in another PR.

@lfarv
Copy link
Contributor Author

lfarv commented Aug 18, 2024

I'll be away for 2 weeks. There is still a Matlab function quantumDiff.m which needs te Energy element attribute. I'll upgrade it later. In the mean time, this branch should be fully operational. To summarise:

  • Everything should work as before,
  • In addition, the Energy element attribute can be removed in python, and in Matlab if there is a RingParam attribute

Please test…

@carmignani
Copy link
Member

Hello Laurent,
I'm testing your modifications on Matlab and I have a few comments.
From the pull request #623, the energy in the elements with rad pass methods became not mandatory. In the track function, this line
Energy=atGetDouble(ElemData,"Energy"); check_error();
has been replaced with this
Energy=atGetOptionalDouble(ElemData,"Energy",Param->energy); check_error();
It is not clear to me what should happen in case there is no "Energy" field and there is no RingParam element in the lattice. I think there should be an error in that case, but currently there is no error. Both Elem->Energy and Param->energy are 0 and I don't get any error message.
This behaviour would not be modified by your recent pull request, where you call the atEnergy function.
Let me know if I misunderstood something. I think in python there is not such problem, because the energy lattice parameter is mandatory.

@carmignani
Copy link
Member

And my second comment is about the mexFunction. I think we could consider to get rid completely of the mexFunctions. The elempass Matlab function is a nice development, but it could probably be written using the standard atpass.c and so using the trackFunction instead of the mexfunction. We could insert a ringparam element in the Matlab function elempass, in front of the element we want to track through, so we would have the energy available.

@lfarv
Copy link
Contributor Author

lfarv commented Aug 21, 2024 via email

@lfarv
Copy link
Contributor Author

lfarv commented Aug 21, 2024 via email

@carmignani
Copy link
Member

I modified the atEnergy for Matlab, but I wrote it as a function, not within the #define. If energy is 0.0 we get an error.

@lfarv lfarv force-pushed the energy-in-integrators branch 2 times, most recently from c3383f4 to 0e28dbb Compare September 2, 2024 09:57
@lfarv lfarv marked this pull request as ready for review September 2, 2024 13:43
@lfarv
Copy link
Contributor Author

lfarv commented Sep 2, 2024

@carmignani : the 2 Matlab functions computing the diffusion matrix (ohmienvelope and quantumDiff) are now using the same code in atdiffmat.

I added automatic tests for emittances and damping rates. Everything looks correct for me.

@swhite2401
Copy link
Contributor

@lfarv @carmignani this look OK for me, shall we merge this? I think it is a pre-requisit for #810 right?

@lfarv lfarv force-pushed the energy-in-integrators branch from 9fe3eb6 to e235a78 Compare October 29, 2024 15:06
@lfarv
Copy link
Contributor Author

lfarv commented Oct 29, 2024

I rebased this branch on master, and backported a few evolutions from #742. Since the number of modified files is very large, here is a summary of the modifications:

  • All *RadPass and *CavityPass methods are modified to handle the ring energy, always taken from the lattice in python, and preferably from the lattice in Matlab. Some other methods also using the energy are modified in the same way,
  • All other passmethod are slightly modified in the Matlab-only part to allow additional input parameters in the MexFunction (necessary to provide the energy and particle to the MexFunction),
  • In Matlab, 2 new functions are created: elempass to track through a single element (still uses the MexFunction entry point), and atdiffmat which factorises the computation of diffusion matrices. A number of functions findm44, ohmienvelope, atx,… are modified to provide an "energy" entry,
  • in python, almost nothing changes since the strategy for providing the energy in unchanged.

For me, it's ready for merging.

@carmignani
Copy link
Member

Hello, I had a look again to the modifications and all seem good to me.

@lfarv lfarv merged commit 37414b8 into master Nov 6, 2024
@lfarv lfarv deleted the energy-in-integrators branch November 6, 2024 13:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C For C code / pass methods enhancement Matlab For Matlab/Octave AT code Python For python AT code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants