-
Notifications
You must be signed in to change notification settings - Fork 5
Add CU HXR ZFEL virtual accelerator backend #56
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
hong274SLAC
wants to merge
11
commits into
slaclab:main
Choose a base branch
from
hong274SLAC:cu-hxr-zfel
base: main
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.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9ef8dd2
Add ZFEL backend and CU HXR model
hong274SLAC d14e3d8
Integrate CU HXR ZFEL runner
hong274SLAC 22cddc2
Add tests for CU HXR ZFEL virtual accelerator
hong274SLAC 6a5b028
Add ZFEL optional dependency and documentation
hong274SLAC e721316
Apply pre-commit formatting and lint fixes
hong274SLAC 32d8345
Simplify ZFEL model layering
hong274SLAC 890ef6a
Use upstream ZFEL dependency
hong274SLAC 6ee0ef7
Remove VA prefix from CU HXR ZFEL PVs
hong274SLAC 71ae70d
Added eval id
hong274SLAC 300d606
update zfel runner construction
hong274SLAC 91d1275
Expose machine PV names in ZFEL model
hong274SLAC 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
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
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 |
|---|---|---|
| @@ -0,0 +1,314 @@ | ||
| from typing import Any | ||
|
|
||
| import numpy as np | ||
|
|
||
| from lume.model import LUMEModel | ||
| from lume.variables import ScalarVariable | ||
|
|
||
| from virtual_accelerator.zfel.undulator_mapping import HXR_CELLS | ||
| from virtual_accelerator.zfel.model import ZFELBackend | ||
|
|
||
|
|
||
| class ZFELPVModel(LUMEModel): | ||
| """ | ||
| Scalar EPICS-facing wrapper around ZFELModel. | ||
|
|
||
| EPICS-facing controls: | ||
| KAct_14, DSKAct_14, ..., KAct_47, DSKAct_47 | ||
|
|
||
| Internal physics model: | ||
| KAct[32], DSKact[32] -> zfel | ||
| """ | ||
|
|
||
| def __init__(self): | ||
| self._backend = ZFELBackend() | ||
|
|
||
| backend_state = self._backend.get( | ||
| [ | ||
| "KAct", | ||
| "DSKAct", | ||
| "power_max", | ||
| "exit_power", | ||
| "pulse_energy", | ||
| ] | ||
| ) | ||
|
|
||
| kact = np.asarray( | ||
| backend_state["KAct"], | ||
| dtype=float, | ||
| ) | ||
|
|
||
| dskact = np.asarray( | ||
| backend_state["DSKAct"], | ||
| dtype=float, | ||
| ) | ||
|
|
||
| self._state: dict[str, Any] = {} | ||
| self._pv_aliases = {} | ||
| self._variables = {} | ||
| self._state["model_eval_id"] = 0.0 | ||
|
|
||
| # ------------------------------------------------------ | ||
| # Scalar, real-machine-like undulator controls | ||
| # ------------------------------------------------------ | ||
|
|
||
| for index, cell in enumerate(HXR_CELLS): | ||
| kact_name = f"KAct_{cell}" | ||
| dskact_name = f"DSKAct_{cell}" | ||
|
|
||
| self._state[kact_name] = float(kact[index]) | ||
| self._state[dskact_name] = float(dskact[index]) | ||
|
|
||
| kact_pv = f"USEG:UNDH:{cell}50:KAct" | ||
| dskact_pv = f"USEG:UNDH:{cell}50:DSKAct" | ||
|
|
||
| self._pv_aliases[kact_pv] = kact_name | ||
| self._pv_aliases[dskact_pv] = dskact_name | ||
|
|
||
| self._variables[kact_name] = ScalarVariable( | ||
| name=kact_name, | ||
| default_value=float(kact[index]), | ||
| value_range=(0.0, 5.0), | ||
| unit="dimensionless", | ||
| read_only=False, | ||
| ) | ||
|
|
||
| self._variables[dskact_name] = ScalarVariable( | ||
| name=dskact_name, | ||
| default_value=float(dskact[index]), | ||
| value_range=(0.0, 5.0), | ||
| unit="dimensionless", | ||
| read_only=False, | ||
| ) | ||
|
|
||
| self._variables[kact_pv] = ScalarVariable( | ||
| name=kact_pv, | ||
| default_value=float(kact[index]), | ||
| value_range=(0.0, 5.0), | ||
| unit="dimensionless", | ||
| read_only=False, | ||
| ) | ||
|
|
||
| self._variables[dskact_pv] = ScalarVariable( | ||
| name=dskact_pv, | ||
| default_value=float(dskact[index]), | ||
| value_range=(0.0, 5.0), | ||
| unit="dimensionless", | ||
| read_only=False, | ||
| ) | ||
|
|
||
| # ------------------------------------------------------ | ||
| # Read-only FEL diagnostics | ||
| # ------------------------------------------------------ | ||
|
|
||
| self._variables.update( | ||
| { | ||
| "power_max": ScalarVariable( | ||
| name="power_max", | ||
| default_value=0.0, | ||
| unit="W", | ||
| read_only=True, | ||
| ), | ||
| "exit_power": ScalarVariable( | ||
| name="exit_power", | ||
| default_value=0.0, | ||
| unit="W", | ||
| read_only=True, | ||
| ), | ||
| "pulse_energy": ScalarVariable( | ||
| name="pulse_energy", | ||
| default_value=0.0, | ||
| unit="J", | ||
| read_only=True, | ||
| ), | ||
| "pulse_intensity_mean": ScalarVariable( | ||
| name="pulse_intensity_mean", | ||
| default_value=0.0, | ||
| unit="J", | ||
| read_only=True, | ||
| ), | ||
| "pulse_intensity_p80": ScalarVariable( | ||
| name="pulse_intensity_p80", | ||
| default_value=0.0, | ||
| unit="J", | ||
| read_only=True, | ||
| ), | ||
| "pulse_intensity_std_relative": ScalarVariable( | ||
| name="pulse_intensity_std_relative", | ||
| default_value=0.0, | ||
| unit="dimensionless", | ||
| read_only=True, | ||
| ), | ||
| "model_eval_id": ScalarVariable( | ||
| name="model_eval_id", | ||
| default_value=0.0, | ||
| unit="", | ||
| read_only=True, | ||
| ), | ||
| } | ||
| ) | ||
|
|
||
| self._sync_from_backend(include_controls=True) | ||
|
|
||
| self._pv_aliases.update( | ||
| { | ||
| "GDET:FEE1:361:ENRC": "pulse_intensity_mean", | ||
| "GDET:FEE1:361:ENRCHSTCUHBR": "pulse_intensity_p80", | ||
| "ZFEL:POWER_MAX": "power_max", | ||
| "ZFEL:EXIT_POWER": "exit_power", | ||
| "ZFEL:PULSE_ENERGY": "pulse_energy", | ||
| "ZFEL:PULSE_INTENSITY_STD_REL": | ||
| "pulse_intensity_std_relative", | ||
| "ZFEL:MODEL_EVAL_ID": "model_eval_id", | ||
| } | ||
| ) | ||
|
|
||
| self._variables.update( | ||
| { | ||
| "GDET:FEE1:361:ENRC": ScalarVariable( | ||
| name="GDET:FEE1:361:ENRC", | ||
| default_value=0.0, | ||
| unit="J", | ||
| read_only=True, | ||
| ), | ||
| "GDET:FEE1:361:ENRCHSTCUHBR": ScalarVariable( | ||
| name="GDET:FEE1:361:ENRCHSTCUHBR", | ||
| default_value=0.0, | ||
| unit="J", | ||
| read_only=True, | ||
| ), | ||
| "ZFEL:POWER_MAX": ScalarVariable( | ||
| name="ZFEL:POWER_MAX", | ||
| default_value=0.0, | ||
| unit="W", | ||
| read_only=True, | ||
| ), | ||
| "ZFEL:EXIT_POWER": ScalarVariable( | ||
| name="ZFEL:EXIT_POWER", | ||
| default_value=0.0, | ||
| unit="W", | ||
| read_only=True, | ||
| ), | ||
| "ZFEL:PULSE_ENERGY": ScalarVariable( | ||
| name="ZFEL:PULSE_ENERGY", | ||
| default_value=0.0, | ||
| unit="J", | ||
| read_only=True, | ||
| ), | ||
| "ZFEL:PULSE_INTENSITY_STD_REL": ScalarVariable( | ||
| name="ZFEL:PULSE_INTENSITY_STD_REL", | ||
| default_value=0.0, | ||
| unit="dimensionless", | ||
| read_only=True, | ||
| ), | ||
| "ZFEL:MODEL_EVAL_ID": ScalarVariable( | ||
| name="ZFEL:MODEL_EVAL_ID", | ||
| default_value=0.0, | ||
| unit="", | ||
| read_only=True, | ||
| ), | ||
| } | ||
| ) | ||
|
|
||
| @property | ||
| def supported_variables(self): | ||
| return self._variables | ||
|
|
||
| def _get(self, names): | ||
| return { | ||
| name: self._state[self._pv_aliases.get(name, name)] | ||
| for name in names | ||
| } | ||
|
|
||
| def _set(self, values: dict[str, Any]) -> None: | ||
| """ | ||
| Update any scalar KAct/DSKAct values, then run zfel once. | ||
|
|
||
| Lume-PVA batches multiple PV writes before calling this, | ||
| according to Runner update_rate. | ||
| """ | ||
|
|
||
| if not values: | ||
| self._sync_from_backend(include_controls=True) | ||
| return | ||
|
|
||
| for name, value in values.items(): | ||
| state_name = self._pv_aliases.get(name, name) | ||
| self._state[state_name] = float(value) | ||
|
|
||
| kact = np.asarray( | ||
| [self._state[f"KAct_{cell}"] for cell in HXR_CELLS], | ||
| dtype=float, | ||
| ) | ||
|
|
||
| dskact = np.asarray( | ||
| [self._state[f"DSKAct_{cell}"] for cell in HXR_CELLS], | ||
| dtype=float, | ||
| ) | ||
|
|
||
| self._backend.set( | ||
| { | ||
| "KAct": kact, | ||
| "DSKAct": dskact, | ||
| } | ||
| ) | ||
|
|
||
| self._sync_from_backend(include_controls=True) | ||
|
|
||
| self._state["model_eval_id"] += 1.0 | ||
|
|
||
| def _sync_from_backend( | ||
| self, | ||
| *, | ||
| include_controls: bool, | ||
| ) -> None: | ||
| backend_state = self._backend.get( | ||
| [ | ||
| "KAct", | ||
| "DSKAct", | ||
| "power_max", | ||
| "exit_power", | ||
| "pulse_energy", | ||
| ] | ||
| ) | ||
|
|
||
| if include_controls: | ||
| kact = np.asarray( | ||
| backend_state["KAct"], | ||
| dtype=float, | ||
| ) | ||
|
|
||
| dskact = np.asarray( | ||
| backend_state["DSKAct"], | ||
| dtype=float, | ||
| ) | ||
|
|
||
| for index, cell in enumerate(HXR_CELLS): | ||
| self._state[f"KAct_{cell}"] = float(kact[index]) | ||
|
|
||
| self._state[f"DSKAct_{cell}"] = float(dskact[index]) | ||
|
|
||
| pulse_energy = float(backend_state["pulse_energy"]) | ||
|
|
||
| self._state["power_max"] = float(backend_state["power_max"]) | ||
|
|
||
| self._state["exit_power"] = float(backend_state["exit_power"]) | ||
|
|
||
| self._state["pulse_energy"] = pulse_energy | ||
|
|
||
| # Fixed-seed deterministic model for now. | ||
| self._state["pulse_intensity_mean"] = pulse_energy | ||
| self._state["pulse_intensity_p80"] = pulse_energy | ||
| self._state["pulse_intensity_std_relative"] = 0.0 | ||
|
|
||
| def reset(self) -> None: | ||
| self._backend.reset() | ||
|
|
||
| self._sync_from_backend(include_controls=True) | ||
|
|
||
|
|
||
| def get_cu_hxr_zfel_model() -> ZFELPVModel: | ||
| """ | ||
| Construct the CU HXR ZFEL virtual-accelerator model. | ||
| """ | ||
| return ZFELPVModel() | ||
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.