Skip to content

Conversation

EZoni
Copy link
Member

@EZoni EZoni commented Jul 21, 2025

This PR renames Line as BeamLine, which I think is consistent with the current version of PALS, see, e.g.,

- fodo_cell:
    kind: BeamLine
    line:
    - drift1
    - quad1
    - drift2:
        kind: Drift
        length: 0.5
    - quad2:
        inherit: quad1
        MagneticMultipoleP:
          Bn1: -1.0
    - drift1

extracted from https://github.com/campa-consortium/pals/blob/main/examples/fodo.pals.yaml.

@EZoni EZoni requested a review from ax3l July 21, 2025 21:49
@EZoni EZoni mentioned this pull request Jul 23, 2025
3 tasks
@EZoni
Copy link
Member Author

EZoni commented Jul 24, 2025

I would merge this after #16, since it's easier to rebase, rather than the other way around.

@EZoni EZoni self-assigned this Aug 7, 2025
@EZoni EZoni merged commit 4380ec0 into campa-consortium:main Aug 7, 2025
3 checks passed
@EZoni EZoni deleted the beamline branch August 7, 2025 16:52
@EZoni EZoni mentioned this pull request Aug 7, 2025
ax3l pushed a commit that referenced this pull request Aug 7, 2025
Push a new patch for the PyPI package, to include #15.
ax3l pushed a commit that referenced this pull request Oct 15, 2025
### Overview

With the implementation in the main branch, if we add a `name` attribute
to the `line` object created in the FODO example, the attribute is
ignored and the output is
```yaml
kind: Line
line:
- drift1:
    kind: Drift
    length: 0.25
- quad1:
    MagneticMultipoleP:
      Bn1: 1.0
    kind: Quadrupole
    length: 1.0
- drift2:
    kind: Drift
    length: 0.5
- quad2:
    MagneticMultipoleP:
      Bn1: -1.0
    kind: Quadrupole
    length: 1.0
- drift3:
    kind: Drift
    length: 0.5
```

This is not consistent with the example in
https://github.com/campa-consortium/pals/blob/main/examples/fodo.pals.yaml.

With the implementation in this branch, if we add a `name` attribute to
the `line` object created in the FODO example, the attribute is stored
and the output is
```yaml
fodo_cell:
  kind: BeamLine
  line:
  - drift1:
      kind: Drift
      length: 0.25
  - quad1:
      MagneticMultipoleP:
        Bn1: 1.0
      kind: Quadrupole
      length: 1.0
  - drift2:
      kind: Drift
      length: 0.5
  - quad2:
      MagneticMultipoleP:
        Bn1: -1.0
      kind: Quadrupole
      length: 1.0
  - drift3:
      kind: Drift
      length: 0.5
```
which is consistent with the example in
https://github.com/campa-consortium/pals/blob/main/examples/fodo.pals.yaml.

This is achieved primarily by deriving the `Line` (`BeamLine` after #15)
class from the base element class `BaseElement` instead of deriving it
from Pydantic's base model class `BaseModel`.

I also merged the custom field validation of the `line` field with the
custom model validation, since we needed one anyways to fix the
deserialization. I don't see why they should be separate.

I wonder if the right way to do this is using `@model_serializer`
instead of `@model_validator`...

### To do
- [x] Serialization (fixed with
[c391e43](c391e43),
very few lines changed)
- [x] Deserialization (fixed with the custom model validator, many more
lines changed)
- [x] Cleaning
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants