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

Add twiss parameter calculations #63

Merged
merged 22 commits into from
Sep 5, 2023

Conversation

jank324
Copy link
Member

@jank324 jank324 commented Sep 3, 2023

Adding twiss parameters of properties of the Beam class. This way they can easily be a computed from tracked ParameterBeam and ParticleBeam.

@jank324 jank324 linked an issue Sep 3, 2023 that may be closed by this pull request
@jank324 jank324 self-assigned this Sep 3, 2023
@jank324 jank324 added enhancement New feature or request help wanted Extra attention is needed labels Sep 3, 2023
@jank324 jank324 requested a review from cr-xu September 3, 2023 14:34
@jank324
Copy link
Member Author

jank324 commented Sep 3, 2023

So this seems to work now. Here are a couple points we might need to think about:

  • I think right now this doesn't work for ParameterBeam, but it absolutely should. The reason being that the emittance computation uses the actual particles:
@property
def emittance_x(self) -> torch.Tensor:
    return torch.sqrt(
        self.sigma_x**2 * self.sigma_xp**2
        - torch.mean((self.xs - self.mu_x) * (self.xps - self.mu_xp)) ** 2
    )

the second part of this after the - this is inspired by Ocelot's

tws.xpx = np.mean((x - tws.x) * (px - tws.px))

which I thought is the same as beam.sigma_x * beam.sigma_xp in Cheetah, but apparently it's not.

  • Is the twiss parameter computation from the Astra beam compared to Ocelot enough testing of this?
  • We should probably also implement creating either type of beam from twiss parameters.

@jank324 jank324 marked this pull request as ready for review September 3, 2023 16:40
@cr-xu
Copy link
Member

cr-xu commented Sep 4, 2023

So this seems to work now. Here are a couple points we might need to think about:

  • I think right now this doesn't work for ParameterBeam, but it absolutely should. The reason being that the emittance computation uses the actual particles:
@property
def emittance_x(self) -> torch.Tensor:
    return torch.sqrt(
        self.sigma_x**2 * self.sigma_xp**2
        - torch.mean((self.xs - self.mu_x) * (self.xps - self.mu_xp)) ** 2
    )

the second part of this after the - this is inspired by Ocelot's

tws.xpx = np.mean((x - tws.x) * (px - tws.px))

which I thought is the same as beam.sigma_x * beam.sigma_xp in Cheetah, but apparently it's not.

Now this is correct.
$\epsilon^2 = \det \begin{pmatrix} \sigma_{x}^2 & \sigma_{xx'}\ \sigma_{xx'} & \sigma_{x'}^2\end{pmatrix}$

where $\sigma_{xx'} = <{} x,x'>{} = \mathrm{E}[(x-\bar{x})(x'-\bar{x'})]$

  • Is the twiss parameter computation from the Astra beam compared to Ocelot enough testing of this?

I think it is sufficient for now.

  • We should probably also implement creating either type of beam from twiss parameters.

Well... I'm not sure we are supposed to do that, because twiss is actually a function/property of the lattice, not the particle distribution. On the other hand, we can use it to generate a beam that is matched to the lattice.
On a second thought, let's do that!

cheetah/particles.py Outdated Show resolved Hide resolved
cheetah/particles.py Show resolved Hide resolved
cheetah/particles.py Outdated Show resolved Hide resolved
@cr-xu
Copy link
Member

cr-xu commented Sep 4, 2023

So this seems to work now. Here are a couple points we might need to think about:

  • I think right now this doesn't work for ParameterBeam, but it absolutely should. The reason being that the emittance computation uses the actual particles:
@property
def emittance_x(self) -> torch.Tensor:
    return torch.sqrt(
        self.sigma_x**2 * self.sigma_xp**2
        - torch.mean((self.xs - self.mu_x) * (self.xps - self.mu_xp)) ** 2
    )

But this term $\sigma_{xx'}$ is actually just the cor_x in the covariance matrix used to initialize a ParameterBeam
One just needs to properly set it to a realistic value
c.f. here

cov=torch.tensor(
                [
                    [sigma_x**2, cor_x, 0, 0, 0, 0, 0],
                    [cor_x, sigma_xp**2, 0, 0, 0, 0, 0],
                    [0, 0, sigma_y**2, cor_y, 0, 0, 0],
                    [0, 0, cor_y, sigma_yp**2, 0, 0, 0],
                    [0, 0, 0, 0, sigma_s**2, cor_s, 0],
                    [0, 0, 0, 0, cor_s, sigma_p**2, 0],
                    [0, 0, 0, 0, 0, 0, 0],
                ],
                dtype=torch.float32,
            ),

jank324 and others added 2 commits September 5, 2023 13:59
Make partial results of twiss parameter computations properties of their own

Co-authored-by: Chenran Xu <[email protected]>
@cr-xu
Copy link
Member

cr-xu commented Sep 5, 2023

oops somehow the suggestion feature in GitHub messes up the indentation, now it's hopefully fixed

@jank324
Copy link
Member Author

jank324 commented Sep 5, 2023

Okay ... so I think all that's missing is to create beams from twiss parameters.

@cr-xu
Copy link
Member

cr-xu commented Sep 5, 2023

Now the from_twiss() method should be fixed, the error was due to missing the cor_x and cor_y component, i.e. cov[0,1]= $\sigma{}_{xx'}$

@jank324 jank324 merged commit 8281956 into master Sep 5, 2023
9 checks passed
@jank324 jank324 deleted the 62-add-twiss-parameter-calculations branch September 5, 2023 17:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add twiss parameter calculations
2 participants