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 a new combined Corrector element #207

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

Conversation

ansantam
Copy link
Collaborator

@ansantam ansantam commented Jun 28, 2024

Description

Implementation of a new class Corrector.
To build the Corrector class I took the old HorizontalCorrector class and added an extra entry in the transfer matrix to include yp (vertical angle). Corrector has the horizontal_angle and vertical_angle properties.

  • xp is in position (1,6) of tm
  • yp is in position (3,6) of tm

I then derived HorizontalCorrector and VerticalCorrector from the Corrector class.

  • I originally removed either vertical_angle or horizontal_angle as property and kept the other, but this was a breaking change.
  • Now angle can be used for HorizontalCorrector and VerticalCorrector, but creates additional problems (see tests)

What solution is best?

Motivation and Context

The motivation is because I need it to continue with the MAD-X converter, using the AWAKE lattice which contains combined correctors. Probably also useful to other people in the future.

  • I have raised an issue to propose this change (required for new features and bug fixes)
    Closes issue New Corrector class #203

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation (update in the documentation)

Checklist

  • I have updated the changelog accordingly (required).
  • My change requires a change to the documentation.
  • I have updated the tests accordingly (required for a bug fix or a new feature).
  • I have updated the documentation accordingly.
  • I have reformatted the code and checked that formatting passes (required).
  • I have have fixed all issues found by flake8 (required).
  • I have ensured that all pytest tests pass (required).
  • I have run pytest on a machine with a CUDA GPU and made sure all tests pass (required).
  • I have checked that the documentation builds (required).

Note: We are using a maximum length of 88 characters per line

Implementing a corrector with kicks in horizontal and vertical planes
Derived from the Corrector class, tests are included
@ansantam ansantam added the enhancement New feature or request label Jun 28, 2024
@ansantam ansantam requested a review from jank324 June 28, 2024 14:03
@ansantam ansantam self-assigned this Jun 28, 2024
@jank324 jank324 changed the title 203 new corrector class Add a new combined Corrector element Jul 3, 2024
@jank324
Copy link
Member

jank324 commented Jul 3, 2024

So ... changing the failing test as in the following works:

def test_ea_magnets():
    """
    Test that gradients are tracking when the magnet settings in the ARES experimental
    area require grad.
    """
    ea = cheetah.Segment.from_ocelot(ares.cell, warnings=False).subcell(
        "AREASOLA1", "AREABSCR1"
    )
    incoming_beam = cheetah.ParticleBeam.from_astra(
        "tests/resources/ACHIP_EA1_2021.1351.001"
    )

    ea.AREAMQZM2.k1 = nn.Parameter(ea.AREAMQZM2.k1)
    ea.AREAMQZM1.k1 = nn.Parameter(ea.AREAMQZM1.k1)
    # ea.AREAMCVM1.angle = nn.Parameter(ea.AREAMCVM1.angle)
    ea.AREAMCVM1.horizontal_angle = nn.Parameter(ea.AREAMCVM1.horizontal_angle)
    ea.AREAMQZM3.k1 = nn.Parameter(ea.AREAMQZM3.k1)
    # ea.AREAMCHM1.angle = nn.Parameter(ea.AREAMCHM1.angle)
    ea.AREAMCHM1.horizontal_angle = nn.Parameter(ea.AREAMCHM1.horizontal_angle)

    outgoing_beam = ea.track(incoming_beam)

    assert outgoing_beam.particles.grad_fn is not None
    ```
    
 This pretty much confirms the issue. So the question is, do we solve the `Parameter` / `Buffer` assignment issue as a whole now before this can be merged, or do we merge this as a hack?
    
In terms of a hack: Just ignoring the test would in some potentially unforeseen situations break differentiability. I think a better hack would be just to make three different classes. There is even a case for that not being a hack. (Maybe a corrector transfer map function and three different classes use it (kind of like `base_rmatrix`.)

Comment on lines 34 to 51
def test_vertical_angle_property():
try:
HorizontalCorrector(
length=torch.tensor([0.3]),
vertical_angle=torch.tensor([0.0]),
)
except TypeError:
pass


def test_horizontal_angle_property():
try:
HorizontalCorrector(
length=torch.tensor([0.3]),
vertical_angle=torch.tensor([0.0]),
)
except TypeError:
pass
Copy link
Member

Choose a reason for hiding this comment

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

@ansantam, what is this? 🦦

@jank324 jank324 self-requested a review July 9, 2024 12:57
Copy link
Member

@jank324 jank324 left a comment

Choose a reason for hiding this comment

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

Change to matrix generating function instead of inheritance as we talked about.

@jank324 jank324 mentioned this pull request Jul 10, 2024
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants