desy-ml / cheetah

Fast and differentiable particle accelerator optics simulation for reinforcement learning and optimisation applications.
https://cheetah-accelerator.readthedocs.io
GNU General Public License v3.0
25 stars 12 forks source link

simple example not working #174

Closed alexdennis312 closed 2 weeks ago

alexdennis312 commented 2 weeks ago

the simple example under docs didn't work for me when trying to run. It gives me the error "ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (1024,) + inhomogeneous part."

Here is my code:

from cheetah import (
    BPM,
    Drift,
    HorizontalCorrector,
    ParameterBeam,
    Segment,
    VerticalCorrector,
    ParticleBeam,
    Element
)

import torch
import math
import numpy as np

segment = Segment(
    elements=[
        BPM(name="BPM1SMATCH"),
        Drift(length=torch.tensor([1.0])),
        BPM(name="BPM6SMATCH"),
        Drift(length=torch.tensor([1.0])),
        VerticalCorrector(length=torch.tensor([0.3]), name="V7SMATCH"),
        Drift(length=torch.tensor([0.2])),
        HorizontalCorrector(length=torch.tensor([0.3]), name="H10SMATCH"),
        Drift(length=torch.tensor([7.0])),
        HorizontalCorrector(length=torch.tensor([0.3]), name="H12SMATCH"),
        Drift(length=torch.tensor([0.05])),
        BPM(name="BPM13SMATCH"),
    ]
)

segment.V7SMATCH.angle = torch.tensor([3.142e-3])
incoming_beam = ParticleBeam.from_astra("../../ACHIP_EA1_2021.1351.001")

outgoing_beam = segment.track(incoming_beam)

segment.plot_overview(beam=incoming_beam)

Everything seems to work except for the segment.plot_overview(beam=incoming_beam) line, which is also where the error appears.

jank324 commented 2 weeks ago

Which version of Cheetah are you running?

alexdennis312 commented 2 weeks ago

Which version of Cheetah are you running?

I'm using version 0.6.3

jank324 commented 2 weeks ago

Which version of Cheetah are you running?

I'm using version 0.6.3

Then your issue is that the code example you are using is from the current master branch, which is a pre-release version of 0.7.0. There is a breaking change going from 0.6.* to 0.7, which changes how you pass parameters to Cheetah Elements (see the CHANGELOG.md file and #116).

If you use the code example from the v0.6.3 tag, everything should work. Alternatively you could also directly install 0.7 from master with

pip install git+https://github.com/desy-ml/cheetah.git

and use the code example you have been using.

Let me know if this works for you.

alexdennis312 commented 2 weeks ago

Which version of Cheetah are you running?

I'm using version 0.6.3

Then your issue is that the code example you are using is from the current master branch, which is a pre-release version of 0.7.0. There is a breaking change going from 0.6.* to 0.7, which changes how you pass parameters to Cheetah Elements (see the CHANGELOG.md file and #116).

If you use the code example from the v0.6.3 tag, everything should work. Alternatively you could also directly install 0.7 from master with

pip install git+https://github.com/desy-ml/cheetah.git

and use the code example you have been using.

Let me know if this works for you.

This worked! Thank you so much for your help!