-
Notifications
You must be signed in to change notification settings - Fork 3
Implement ElectricMultipoleParameters #49
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
Changes from all commits
3313a3a
05d626a
ec21f26
b4b4957
14f0f74
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,11 +1,63 @@ | ||||||
| from pydantic import BaseModel, ConfigDict | ||||||
| from pydantic import BaseModel, ConfigDict, model_validator | ||||||
| from typing import Any | ||||||
|
|
||||||
| # Valid parameter prefixes, their expected format and description | ||||||
| _PARAMETER_PREFIXES = { | ||||||
| "tilt": ("tiltN", "Tilt"), | ||||||
| "En": ("EnN", "Normal component"), | ||||||
| "Es": ("EsN", "Skew component"), | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| def _validate_order( | ||||||
| key_num: str, parameter_name: str, prefix: str, expected_format: str | ||||||
| ) -> None: | ||||||
| """Validate that the order number is a non-negative integer without leading zeros.""" | ||||||
| error_msg = ( | ||||||
| f"Invalid {parameter_name}: '{prefix}{key_num}'. " | ||||||
| f"Parameter must be of the form '{expected_format}', where 'N' is a non-negative integer without leading zeros." | ||||||
| ) | ||||||
| if not key_num.isdigit() or (key_num.startswith("0") and key_num != "0"): | ||||||
| raise ValueError(error_msg) | ||||||
|
|
||||||
|
|
||||||
| class ElectricMultipoleParameters(BaseModel): | ||||||
| """Electric multipole parameters""" | ||||||
| """Electric multipole parameters | ||||||
| Valid parameter formats: | ||||||
| - tiltN: Tilt of Nth order multipole | ||||||
| - EnN: Normal component of Nth order multipole | ||||||
| - EsN: Skew component of Nth order multipole | ||||||
| - *NL: Length-integrated versions of components (e.g., En3L, EsNL) | ||||||
|
Comment on lines
+27
to
+31
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. Oh interesting, is this a PALS defect that we have no normalized parameters for the pals-python/src/pals/parameters/MagneticMultipoleParameters.py Lines 33 to 34 in 35ed73a
I would expect this to be identical as for the magnetic parameters.
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. I've never seen normalized electric field strengths used which is why I left them out. If someone actually does use them then they should be put in.
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. Ok, perfect! Thanks for checking David! 🙏 |
||||||
| Where N is a positive integer without leading zeros (except "0" itself). | ||||||
| """ | ||||||
|
|
||||||
| # Allow arbitrary fields (TODO: remove this) | ||||||
| model_config = ConfigDict(extra="allow") | ||||||
|
|
||||||
| # TODO: add ElectricMultipoleParameters in a follow-up RP | ||||||
| # https://pals-project.readthedocs.io/en/latest/element-parameters.html#electricmultipolep-electric-multipole-parameters | ||||||
| @model_validator(mode="before") | ||||||
| @classmethod | ||||||
| def validate(cls, values: dict[str, Any]) -> dict[str, Any]: | ||||||
| """Validate all parameter names match the expected multipole format.""" | ||||||
| for key in values: | ||||||
| # Check if key ends with 'L' for length-integrated values | ||||||
| is_length_integrated = key.endswith("L") | ||||||
| base_key = key[:-1] if is_length_integrated else key | ||||||
|
|
||||||
| # No length-integrated values allowed for tilt parameter | ||||||
| if is_length_integrated and base_key.startswith("tilt"): | ||||||
| raise ValueError(f"Invalid electric multipole parameter: '{key}'. ") | ||||||
|
|
||||||
| # Find matching prefix | ||||||
| for prefix, (expected_format, description) in _PARAMETER_PREFIXES.items(): | ||||||
| if base_key.startswith(prefix): | ||||||
| key_num = base_key[len(prefix) :] | ||||||
| _validate_order(key_num, description, prefix, expected_format) | ||||||
| break | ||||||
| else: | ||||||
| raise ValueError( | ||||||
| f"Invalid electric multipole parameter: '{key}'. " | ||||||
| f"Parameters must be of the form 'tiltN', 'EnN', or 'EsN' " | ||||||
| f"(with optional 'L' suffix for length-integrated), where 'N' is a non-negative integer." | ||||||
| ) | ||||||
| return values | ||||||
Uh oh!
There was an error while loading. Please reload this page.