-
Notifications
You must be signed in to change notification settings - Fork 33
PALS Python 0.3.0, Unit Convention for Quad #1357
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
Open
cemitch99
wants to merge
7
commits into
BLAST-ImpactX:development
Choose a base branch
from
cemitch99:update_pals_support
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+113
−49
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
71441e2
Fix unit convention for PALS Quad support.
cemitch99 fdf0fd7
Update PALS example to clarify that normalized quad gradient is needed.
cemitch99 6235ad1
Add branching for PALS Quad.
cemitch99 49c82ce
Merge remote-tracking branch 'mainline/development' into update_pals_…
ax3l fea17bf
Python: PALS Schema to 0.3.0
ax3l 0548307
PALS: Update FODO Example
ax3l f1309b9
Python: Update PALS Translation
ax3l File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,33 @@ | ||
| 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.25 | ||
| PALS: | ||
| version: null # the PALS schema is not yet versioned | ||
|
|
||
| facility: | ||
| - fodo_cell: | ||
| kind: BeamLine | ||
| line: | ||
| - drift1: | ||
| kind: Drift | ||
| length: 0.25 | ||
| - quad1: | ||
| MagneticMultipoleP: | ||
| Kn1: 1.0 | ||
| kind: Quadrupole | ||
| length: 1.0 | ||
| - drift2: | ||
| kind: Drift | ||
| length: 0.5 | ||
| - quad2: | ||
| MagneticMultipoleP: | ||
| Kn1: -1.0 | ||
| kind: Quadrupole | ||
| length: 1.0 | ||
| - drift3: | ||
| kind: Drift | ||
| length: 0.25 | ||
|
|
||
| - fodo_lattice: | ||
| kind: Lattice | ||
| branches: | ||
| - fodo_cell | ||
|
|
||
| - use: fodo_lattice |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| numpy>=1.15 | ||
| pals-schema~=0.2.0 | ||
| pals-schema~=0.3.0 | ||
| quantiphy~=2.19 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -181,59 +181,112 @@ def load_file(self, filename, nslice=1): | |
| return | ||
|
|
||
| elif extension_inner == ".pals": | ||
| from pals.BeamLine import BeamLine | ||
| from pals import load as load_pals_file | ||
|
|
||
| # examples: fodo.pals.yaml, fodo.pals.json | ||
| with open(filename, "r") as file: | ||
| if extension == ".json": | ||
| import json | ||
|
|
||
| pals_data = json.loads(file.read()) | ||
| elif extension == ".yaml": | ||
| import yaml | ||
|
|
||
| pals_data = yaml.safe_load(file) | ||
| # TODO: toml, xml | ||
| else: | ||
| raise RuntimeError( | ||
| f"load_file: No support for PALS file {filename} with extension {extension} yet." | ||
| ) | ||
|
|
||
| # Parse the data dictionary back into a PALS `BeamLine` object. | ||
| # The automatically PALS data validation happens here. | ||
| self.from_pals(BeamLine(**pals_data), nslice) | ||
| self.from_pals(load_pals_file(filename), nslice) | ||
| return | ||
|
|
||
| raise RuntimeError( | ||
| f"load_file: No support for file {filename} with extension {extension} yet." | ||
| ) | ||
|
|
||
|
|
||
| def flatten_pals(pals_data, registry=None): | ||
| """Flatten a PALS root, lattice, or beamline to a list of PALS elements. | ||
|
|
||
| Placeholder references are resolved from the root facility definitions. | ||
| """ | ||
| from pals import BeamLine, Lattice, PALSroot, PlaceholderName | ||
|
|
||
| if registry is None: | ||
| registry = {} | ||
|
|
||
| if isinstance(pals_data, PALSroot): | ||
| registry = { | ||
| item.name: item | ||
| for item in pals_data.facility | ||
| if not isinstance(item, PlaceholderName) and hasattr(item, "name") | ||
| } | ||
|
|
||
| if len(pals_data.facility) == 1: | ||
|
Comment on lines
+194
to
+211
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| return flatten_pals(pals_data.facility[0], registry) | ||
|
|
||
| active_entry = pals_data.facility[-1] | ||
| if not isinstance(active_entry, PlaceholderName): | ||
| raise RuntimeError( | ||
| "from_pals: PALS roots with multiple facility entries must " | ||
| "select the active lattice or beamline with a final 'use' entry." | ||
| ) | ||
| return flatten_pals(active_entry, registry) | ||
|
|
||
| if isinstance(pals_data, PlaceholderName): | ||
| if pals_data.element is not None: | ||
| return flatten_pals(pals_data.element, registry) | ||
| if pals_data.name not in registry: | ||
| raise RuntimeError( | ||
| f"from_pals: Cannot resolve PALS element reference {pals_data.name!r}." | ||
| ) | ||
| return flatten_pals(registry[pals_data.name], registry) | ||
|
|
||
| if isinstance(pals_data, Lattice): | ||
| if len(pals_data.branches) != 1: | ||
| raise RuntimeError( | ||
| "from_pals: ImpactX currently supports PALS lattices with exactly " | ||
| f"one branch, but got {len(pals_data.branches)}." | ||
| ) | ||
| return flatten_pals(pals_data.branches[0], registry) | ||
|
|
||
| if isinstance(pals_data, BeamLine): | ||
| pals_elements = [] | ||
| for element in pals_data.line: | ||
| pals_elements.extend(flatten_pals(element, registry)) | ||
| return pals_elements | ||
|
|
||
| return [pals_data] | ||
|
|
||
|
|
||
| def from_pals(self, pals_beamline, nslice=1): | ||
| """Load and append a lattice from a Particle Accelerator Lattice Standard (PALS) Python BeamLine. | ||
| """Load and append a lattice from a Particle Accelerator Lattice Standard (PALS) object. | ||
|
|
||
| https://github.com/campa-consortium/pals-python | ||
| """ | ||
| from pals.Drift import Drift | ||
| from pals.Quadrupole import Quadrupole | ||
| from pals import Drift, Quadrupole | ||
|
|
||
| pals_elements = flatten_pals(pals_beamline) | ||
|
|
||
| # Loop over the pals_beamline and create a new ImpactX KnownElementsList from it. | ||
| # Use self.extend(...) on the latter. | ||
| ix_beamline = [] | ||
| for pals_element in pals_beamline.line: | ||
| for pals_element in pals_elements: | ||
| if isinstance(pals_element, Drift): | ||
| ix_beamline.append( | ||
| elements.Drift( | ||
| name=pals_element.name, ds=pals_element.length, nslice=nslice | ||
| ) | ||
| ) | ||
| elif isinstance(pals_element, Quadrupole): | ||
| magnetic_multipole = pals_element.MagneticMultipoleP | ||
| if magnetic_multipole is None: | ||
| raise RuntimeError( | ||
| f"from_pals: No magnetic multipole input provided for element of kind {type(pals_element)}." | ||
| ) | ||
|
|
||
| if getattr(magnetic_multipole, "Bn1", None) is not None: | ||
| k_quad = magnetic_multipole.Bn1 | ||
| unit_quad = 1 | ||
| elif getattr(magnetic_multipole, "Kn1", None) is not None: | ||
| k_quad = magnetic_multipole.Kn1 | ||
| unit_quad = 0 | ||
| else: | ||
| raise RuntimeError( | ||
| f"from_pals: No gradient input provided for element of kind {type(pals_element)}." | ||
| ) | ||
| ix_beamline.append( | ||
| elements.ChrQuad( | ||
| name=pals_element.name, | ||
| ds=pals_element.length, | ||
| k=pals_element.MagneticMultipoleP.Bn1, | ||
| unit=0, | ||
| k=k_quad, | ||
| unit=unit_quad, | ||
| nslice=nslice, | ||
| ) | ||
| ) | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
flatten_palscould be a helper routine in pals-python, for the next release of it.I am surprised that we still have placeholders at this point in the logic, we probably should replace placeholders / do lattice expansion already in
pals.load()-- to check if as an optional argument (i.e.lattice_expansion=True). To check lattice and branch expansion.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the next release (not this PR): pals-project/pals-python#70