Skip to content
Draft
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
43 changes: 43 additions & 0 deletions torax/_src/imas_tools/input/core_profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,24 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""Useful functions to load IMAS core_profiles or plasma_profiles IDSs."""
import logging
from typing import Any, Mapping

from imas import ids_toplevel
import numpy as np

_PROFILE_CONDITIONS_REQUIRED_FIELDS = {
"global_quantities": ["ip", "v_loop"],
"profiles_1d": [
"time",
"grid.rho_tor_norm",
"grid.psi",
"electrons.temperature",
"t_i_average",
"electrons.density",
],
}


# pylint: disable=invalid-name
def profile_conditions_from_IMAS(
Expand Down Expand Up @@ -109,3 +122,33 @@ def profile_conditions_from_IMAS(
"normalize_n_e_to_nbar": False,
"v_loop_lcfs": v_loop_lcfs,
}

def _validate_core_profiles_ids_for_profile_conditions(
ids: ids_toplevel.IDSToplevel,
):
"""Checks that all expected attributes exist in the IDS."""
profiles_1d = ids.profiles_1d
global_quantities = ids.global_quantities
logged_fields = []
for field in _PROFILE_CONDITIONS_REQUIRED_FIELDS["global_quantities"]:
if not getattr(global_quantities, field):
# Warning or critical ?
logging.critical(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we'd want to raise an error here with an informative error message that the expected field is missing.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not entirely sure about this. For geometry loader I think we'd want to raise an error but with core_profiles not necessarily. There might be cases where we want to load partially some profiles but not all from an IDS, like only electrons ones or ions ones and get the rest from elsewhere / prescribe it. In this case, it does not matter that the other fields are empty in the input IDS. Do you agree ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok, yeah that makes sense. I think it would be good to converge on a minimal set of required attributes for using the profile_conditions_from_IMAS function though as that makes assumptions on certain fields being present?

f"The IDS is missing global_quantities.{field} to build"
" profile_conditions. \n Please Check that your IDS is properly"
" filled."
)
for profile in profiles_1d:
for field in _PROFILE_CONDITIONS_REQUIRED_FIELDS["profiles_1d"]:
leaf = profile
for node in field.split("."):
leaf = getattr(leaf, node)
if not leaf:
if field not in logged_fields:
# Warning or critical ?
logging.critical(
f"The IDS is missing profiles_1d.{field} to build"
" profile_conditions. \n Please Check that your IDS is properly"
" filled."
)
logged_fields.append(field)