Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/pals/kinds/Lattice.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pydantic import model_validator
from typing import Literal, Self
from pydantic import Field, model_validator
from typing import Annotated, Literal, Self

from .BeamLine import BeamLine
from .PlaceholderName import PlaceholderName
Expand All @@ -12,7 +12,9 @@ class Lattice(BaseElement):

kind: Literal["Lattice"] = "Lattice"

branches: list[BeamLine | PlaceholderName]
branches: list[
Annotated[BeamLine | PlaceholderName, Field(union_mode="left_to_right")]
]

@model_validator(mode="before")
@classmethod
Expand Down
9 changes: 7 additions & 2 deletions src/pals/kinds/all_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
avoiding duplication between BeamLine.line and UnionEle.elements.
"""

from typing import Union
from typing import Annotated, Union

from pydantic import Field

from .ACKicker import ACKicker
from .BeamBeam import BeamBeam
Expand Down Expand Up @@ -89,6 +91,9 @@ def get_all_elements_as_annotation(extra_types: tuple | None = None):
Pydantic will still properly validate the union by trying each type in order in
our unpack_element_list_structure method.
"""
types = get_all_element_types(extra_types) + (PlaceholderName,)
element_types = Union[get_all_element_types(extra_types)]
types = Annotated[
element_types | PlaceholderName, Field(union_mode="left_to_right")
]
# We can't use discriminator with PlaceholderName in the union since it has no 'kind' field
return Union[types]
16 changes: 16 additions & 0 deletions src/pals/parameters/ApertureParameters.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
import builtins

from annotated_types import Ge
from typing import Annotated, Literal
from pydantic import BaseModel, Field, field_validator


class Vertex(BaseModel):
point: tuple[float, float]
radius: float | tuple[float, float] | None = None
convex: bool = True
tilt: float = 0


class Vertices(BaseModel):
center: tuple[float, float] = Field(default=(0, 0))
absolute_vertices: bool = False
list: builtins.list[Vertex] = Field(default_factory=builtins.list)


class ApertureParameters(BaseModel):
"""Aperture parameters"""

Expand All @@ -26,3 +41,4 @@ def validate_limits(cls, v):
thickness: Annotated[float, Ge(0.0)] = 0.0
aperture_shifts_with_body: bool = False
aperture_active: bool = True
vertices: Vertices | None = None
12 changes: 9 additions & 3 deletions tests/test_serialization.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

import pals
from pals.parameters.ApertureParameters import Vertex, Vertices


def test_yaml(tmp_path):
Expand Down Expand Up @@ -147,7 +148,12 @@ def test_comprehensive_lattice(tmp_path):
name="sextupole1",
length=0.2,
MagneticMultipoleP=pals.MagneticMultipoleParameters(Bn2=1.0),
ApertureP=pals.ApertureParameters(x_limits=[-0.1, 0.1]),
ApertureP=pals.ApertureParameters(
shape="VERTICES",
vertices=Vertices(
list=[Vertex(point=(-0.1, 0), radius=0.1), Vertex(point=(0.1, 0))]
),
),
)

# Solenoid
Expand Down Expand Up @@ -247,7 +253,7 @@ def test_comprehensive_lattice(tmp_path):

# Test that parameter groups are correctly deserialized
assert sextupole_loaded.MagneticMultipoleP.Bn2 == 1.0
assert sextupole_loaded.ApertureP.x_limits == [-0.1, 0.1]
assert sextupole_loaded.ApertureP.vertices.list[1].point == (0.1, 0)

assert octupole_loaded.ElectricMultipoleP.En3 == 0.5
assert octupole_loaded.MetaP.alias == "octupole_test"
Expand Down Expand Up @@ -305,7 +311,7 @@ def test_comprehensive_lattice(tmp_path):

# Test that parameter groups are correctly deserialized
assert sextupole_loaded_json.MagneticMultipoleP.Bn2 == 1.0
assert sextupole_loaded_json.ApertureP.x_limits == [-0.1, 0.1]
assert sextupole_loaded_json.ApertureP.vertices.list[1].point == (0.1, 0)

assert octupole_loaded_json.ElectricMultipoleP.En3 == 0.5
assert octupole_loaded_json.MetaP.alias == "octupole_test"
Expand Down