Skip to content

Commit

Permalink
Extend IonMixture to validate input ion symbols.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 714750426
  • Loading branch information
jcitrin authored and Torax team committed Jan 12, 2025
1 parent 5b14d8a commit e446144
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
10 changes: 10 additions & 0 deletions torax/config/plasma_composition.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from collections.abc import Mapping
import dataclasses
import logging
import typing

import chex
import numpy as np
Expand Down Expand Up @@ -61,6 +62,15 @@ def __post_init__(self):
if not isinstance(self.species, Mapping):
raise ValueError('species must be a Mapping')

# Iterate through species keys and check if they are in the allowed list.
allowed_symbols = typing.get_args(constants.ION_SYMBOLS)
for ion_symbol in self.species:
if ion_symbol not in allowed_symbols:
raise ValueError(
f'Invalid ion symbol: {ion_symbol}. Allowed symbols are:'
f' {allowed_symbols}'
)

time_arrays = []
fraction_arrays = []

Expand Down
1 change: 1 addition & 0 deletions torax/config/tests/plasma_composition.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ class IonMixtureTest(parameterized.TestCase):
('valid_tolerance', {'D': 0.49999999, 'T': 0.5}, False),
('invalid_tolerance', {'D': 0.4999, 'T': 0.5}, True),
('invalid_not_mapping', 'D', True),
('invalid_ion_symbol', {'De': 0.5, 'Tr': 0.5}, True),
)
def test_ion_mixture_constructor(self, input_species, should_raise):
"""Tests various cases of IonMixture construction."""
Expand Down

0 comments on commit e446144

Please sign in to comment.