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

matching twiss parameters using drift length #210

Closed SchroederSa closed 6 months ago

SchroederSa commented 7 months ago

Hi, I'd like to use the match function to get specific twiss parameters at the end of a focusing section. To achieve this, I'd like to vary quadrupole strengths but also drift lengths. The match function seems to facilitate this functionality, however, it crashes with the error message:

AttributeError: Drift object has no attribute create_tm

What is line 108 (vars[i].create_tm()) in match.py supposed to do and is it actually needed?

Thank you for your help! Best Sarah

For reference my code looks like this:

spot_size = 1e-6 #m
constr = {"periodic":False, END_OF_LAST_DRIFT:{'beta_x': spot_size**2 / tws_0.emit_x, 'beta_y': spot_size**2 / tws_0.emit_y, 'alpha_x':0, 'alpha_y':0}, 'total_len': 1}
quads = getComponentType('quad', lat) # Function that returns a list of all quads of an ocelot lattice
drifts = getComponentType('drift', lat) # Function that returns a list of all drifts of an ocelot lattice
res = match(lat=lat, constr=constr, vars=drifts+quads, tw=tws_0, verbose=True, max_iter=1000, method='simplex')
sergey-tomin commented 7 months ago

Hi,

Thanks for reporting this bug. The problem is that line 108 is leftover after major refactoring, and since I did not have unit tests that cover this case (drift length as a variable), I did not catch this bug. You can simply comment out this line or wait a bit until I commit the bug fix and a unit test to prevent this problem from occurring again.

Sergey

SchroederSa commented 6 months ago

Hi Sergey, There is another little piece missing in this function. After matching, when updating the twiss parameters via tws_matched = twiss(lat, tws, nPoints=10000) the code crashes with the error message:

ocelot/cpbd/optics.py:113), in trace_z(lattice, obj0, z_array)
IndexError: list index out of range

I figured out that this is due to the total lattice length not being updated.

Adding the following lines to the end of the match.py function solve the issue:

    '''
    Update total lattice length
    '''
    lat.totalLen = 0
    for i, el in enumerate(lat.sequence):
        lat.totalLen += el.l

Best Sarah

sergey-tomin commented 6 months ago

Hi Sarah,

thanks for reporting this issue. Indeed we had a problem here. I made a bug fix in dev branch https://github.com/ocelot-collab/ocelot/commit/99dbbaf737383c8328fea22dceaf55477ad6ca3f

Cheers, Sergey.