Skip to content

Commit 0b42984

Browse files
committed
ids_toplevel import and geometry_directory variable name
1 parent 14ae765 commit 0b42984

5 files changed

Lines changed: 29 additions & 33 deletions

File tree

torax/_src/geometry/pydantic_model.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import inspect
2020
from typing import Annotated, Any, Literal, TypeAlias, TypeVar
2121
import pydantic
22+
23+
from imas.ids_toplevel import IDSToplevel
24+
2225
from torax._src.geometry import circular_geometry
2326
from torax._src.geometry import geometry
2427
from torax._src.geometry import geometry_provider
@@ -257,7 +260,7 @@ class IMASConfig(torax_pydantic.BaseModelFrozen):
257260
hires_factor: Only used when the initial condition ``psi`` is from plasma
258261
current. Sets up a higher resolution mesh with ``nrho_hires = nrho *
259262
hi_res_fac``, used for ``j`` to ``psi`` conversions.
260-
geometry_dir: Optionally overrides the default geometry directory.
263+
geometry_directory: Optionally overrides the default geometry directory.
261264
equilibrium_object: Either directly the equilbrium IDS containing the relevant data,
262265
or the path to the IMAS netCDF file containing the equilibrium.
263266
Ip_from_parameters: Toggles whether total plasma current is read from the
@@ -268,17 +271,17 @@ class IMASConfig(torax_pydantic.BaseModelFrozen):
268271
geometry_type: Annotated[Literal['imas'], TIME_INVARIANT] = 'imas'
269272
n_rho: Annotated[pydantic.PositiveInt, TIME_INVARIANT] = 25
270273
hires_factor: pydantic.PositiveInt = 4
271-
geometry_dir: Annotated[str | None, TIME_INVARIANT] = None
272-
equilibrium_object: str | Any = 'ITERhybrid_COCOS17_IDS_ddv4.nc'
273-
Ip_from_parameters: Annotated[bool, TIME_INVARIANT] = False
274+
geometry_directory: Annotated[str | None, TIME_INVARIANT] = None
275+
equilibrium_object: str | IDSToplevel = 'ITERhybrid_COCOS17_IDS_ddv4.nc'
276+
Ip_from_parameters: Annotated[bool, TIME_INVARIANT] = True
274277

275278
@pydantic.model_validator(mode='after')
276279
def _validate_model(self) -> typing_extensions.Self:
277280
if isinstance(self.equilibrium_object, str) and self.equilibrium_object[-3:] == '.h5':
278281
raise ValueError(
279-
"If you are using hdf5 backend, the path to the data must point the directory containing the equilibrium.h5 and master.h5 files. As the function concatenates the str for geometry_dir and equilibrium_object to give the path, your \
280-
equilibrium_object must be either the repository containing these files or an empty string '' (if your geometry_dir is already this specific repository).\n \
281-
In any case, make sure geometry_dir + equilibrium_object gives the path to this directory and not to the .h5 file."
282+
"If you are using hdf5 backend, the path to the data must point the directory containing the equilibrium.h5 and master.h5 files. As the function concatenates the str for geometry_directory and equilibrium_object to give the path, your \
283+
equilibrium_object must be either the repository containing these files or an empty string '' (if your geometry_directory is already this specific repository).\n \
284+
In any case, make sure geometry_directory + equilibrium_object gives the path to this directory and not to the .h5 file."
282285
)
283286
return self
284287

torax/_src/geometry/standard_geometry.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ def calculate_area(x, z):
933933
def from_IMAS(
934934
cls,
935935
equilibrium_object: str | Any,
936-
geometry_dir: str | None,
936+
geometry_directory: str | None,
937937
Ip_from_parameters: bool,
938938
n_rho: int,
939939
hires_factor: int,
@@ -942,11 +942,11 @@ def from_IMAS(
942942
943943
Args:
944944
equilibrium_object: Either directly the equilbrium IDS containing the relevant data, or the name of the IMAS netCDF file containing the equilibrium.
945-
geometry_dir: Directory where to find the scenario file ontaining the parameters of the Data entry to read.
945+
geometry_directory: Directory where to find the scenario file ontaining the parameters of the Data entry to read.
946946
If None, then it defaults to another dir. See implementation.
947947
Ip_from_parameters: If True, the Ip is taken from the parameters and the
948-
values in the Geometry are resacled to match the new Ip.
949-
n_rho: Radial grid points (num cells)
948+
values in the Geometry are rescaled to match the new Ip.
949+
n_rho: Radial grid points (num cells).
950950
hires_factor: Grid refinement factor for poloidal flux <--> plasma current
951951
calculations.
952952
@@ -956,7 +956,7 @@ def from_IMAS(
956956
"""
957957
inputs = imas_equilibrium.geometry_from_IMAS(
958958
equilibrium_object=equilibrium_object,
959-
geometry_dir=geometry_dir,
959+
geometry_directory=geometry_directory,
960960
Ip_from_parameters=Ip_from_parameters,
961961
n_rho=n_rho,
962962
hires_factor=hires_factor,

torax/imas_tools/equilibrium.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def write_ids_equilibrium_into_config(
4949

5050
def geometry_from_IMAS(
5151
equilibrium_object: str | IDSToplevel,
52-
geometry_dir: str | None = None,
52+
geometry_directory: str | None = None,
5353
Ip_from_parameters: bool = False,
5454
n_rho: int = 25,
5555
hires_factor: int = 4,
@@ -59,7 +59,7 @@ def geometry_from_IMAS(
5959
equilibrium_object: Either directly the equilbrium IDS containing the
6060
relevant data, or the name of the IMAS netCDF file containing the
6161
equilibrium.
62-
geometry_dir: Directory where to find the equilibrium object.
62+
geometry_directory: Directory where to find the equilibrium object.
6363
If None, it defaults to another dir. See `load_geo_data`
6464
implementation.
6565
Ip_from_parameters: If True, the Ip is taken from the parameters and the
@@ -74,7 +74,7 @@ def geometry_from_IMAS(
7474
# If the equilibrium_object is the file name, load the ids from the netCDF.
7575
if isinstance(equilibrium_object, str):
7676
equilibrium = geometry_loader.load_geo_data(
77-
geometry_dir,
77+
geometry_directory,
7878
equilibrium_object,
7979
geometry_loader.GeometrySource.IMAS,
8080
)
@@ -132,6 +132,11 @@ def geometry_from_IMAS(
132132
# -> Ip_profile = integrate(y = spr * jtor, x= rhon, initial = 0.0)
133133
jtor = -1 * IMAS_data.profiles_1d.j_phi
134134
rhon = IMAS_data.profiles_1d.rho_tor_norm
135+
if len(rhon) == 0:
136+
if B_0 is None or len(IMAS_data.profiles_1d.phi) == 0:
137+
raise ValueError("rho_tor_norm not provided and cannot be calculated from given equilibrium IDS")
138+
rho_tor = np.sqrt(IMAS_data.profiles_1d.phi / (np.pi * B_0))
139+
rhon = rho_tor / rho_tor[-1]
135140
vpr = 4 * np.pi * Phi[-1] * rhon / (F * flux_surf_avg_1_over_R2)
136141
spr = vpr / (2 * np.pi * R_major)
137142
Ip_profile_unscaled = scipy.integrate.cumulative_trapezoid(y=spr * jtor, x=rhon, initial=0.0) # this Ip_profile by integration results in a discrepancy between this term and the total ip from IDS

torax/imas_tools/tests/equilibrium_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ def test_save_geometry_to_IMAS(
5656
atol = self.atol
5757
# Input equilibrium reading
5858
config_module = self._get_config_module(config_name)
59-
geometry_dir = "torax/data/third_party/geo"
59+
geometry_directory = "torax/data/third_party/geo"
6060
path = os.path.join(
61-
geometry_dir, config_module.CONFIG["geometry"]["equilibrium_object"]
61+
geometry_directory, config_module.CONFIG["geometry"]["equilibrium_object"]
6262
)
6363
equilibrium_in = imas_util.load_IMAS_data(path, "equilibrium")
6464
# Build TORAXSimState object and write output to equilibrium IDS.

torax/imas_tools/util.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,26 +48,14 @@ def save_netCDF(
4848
date_str = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
4949
file_name = "IDS_file_" + date_str
5050
filepath = os.path.join(directory_path, file_name) + ".nc"
51-
with imas.DBEntry(filepath, "w") as netcdf_entry:
52-
netcdf_entry.put(IDS)
51+
with imas.DBEntry(uri=filepath, mode="w") as netcdf_entry:
52+
netcdf_entry.put(ids=IDS)
5353

5454

5555
def load_IMAS_data(uri: str, ids_name: str) -> IDSToplevel:
5656
"""
5757
Loads a full IDS for a given uri or path_name and a given ids_name.
5858
"""
59-
db = imas.DBEntry(uri, "r")
60-
ids = db.get(ids_name)
59+
db = imas.DBEntry(uri=uri, mode="r")
60+
ids = db.get(ids_name=ids_name)
6161
return ids
62-
63-
# todo check if we can copy from geometry without weird dependency loops
64-
def face_to_cell(face):
65-
"""Infers cell values corresponding to a vector of face values.
66-
Args:
67-
face: An array containing face values.
68-
69-
Returns:
70-
cell: An array containing cell values.
71-
"""
72-
73-
return 0.5 * (face[:-1] + face[1:])

0 commit comments

Comments
 (0)