Coverage for python/gsfit/database_readers/st40_astra_mdsplus/astra_passives_reader.py: 0%
42 statements
« prev ^ index » next coverage.py v7.15.0, created at 2026-07-07 13:12 +0000
« prev ^ index » next coverage.py v7.15.0, created at 2026-07-07 13:12 +0000
1"""Passives reader for ASTRA simulations using the fires.dat file.
3This module reads passive conductor geometry and properties from the fires.dat file
4which contains filament data in the following format:
5 ignore, r, z, d_r, d_z, resistivity, ignore, name
6"""
8import os
9from collections import defaultdict
10from typing import Any
12import numpy as np
13import numpy.typing as npt
16def _parse_fortran_float(value: str) -> float:
17 """
18 Parse Fortran-style floating point notation.
20 Converts strings like '.155222855D0' or '690D-9' to Python floats.
22 :param value: String containing Fortran-style float notation
23 :return: Python float value
24 """
25 # Replace Fortran 'D' notation with Python 'e' notation
26 value = value.replace("D", "e").replace("d", "e")
27 return float(value)
30def astra_passives_reader() -> dict[str, dict[str, Any]]:
31 """
32 Read passive conductor data from the fires.dat file.
34 The fires.dat file contains filament data with columns:
35 - Column 0: ignored
36 - Column 1: r (radial position in meters)
37 - Column 2: z (vertical position in meters)
38 - Column 3: d_r (radial width in meters)
39 - Column 4: d_z (vertical height in meters)
40 - Column 5: resistivity (in Ohm-meters, Fortran notation)
41 - Column 6: ignored
42 - Column 7: name (passive conductor name, e.g., IVC, OVC, GASBFLT)
44 :return: Dictionary mapping passive names to their properties:
45 - r: numpy array of radial positions
46 - z: numpy array of vertical positions
47 - d_r: numpy array of radial widths
48 - d_z: numpy array of vertical heights
49 - angle_1: numpy array of zeros (rectangular filaments)
50 - angle_2: numpy array of zeros (rectangular filaments)
51 - resistivity: mean resistivity of the passive
52 """
53 # Get the path to the fires.dat file (in the same directory as this module)
54 # Taken from:
55 # https://tokamak-devlin.tokamak.local/gitlab/physics/astra_te/-/blob/master/exp/equ/MCVC/fires.dat
56 fires_dat_path = os.path.join(os.path.dirname(__file__), "fires.dat")
58 # Read and parse the fires.dat file
59 filaments: dict[str, dict[str, list[float]]] = defaultdict(lambda: {"r": [], "z": [], "d_r": [], "d_z": [], "resistivity": []})
61 with open(fires_dat_path, "r") as f:
62 for line in f:
63 line = line.strip()
64 if not line:
65 continue
67 parts = line.split()
68 if len(parts) < 8:
69 continue
71 # Parse columns: ignore, r, z, d_r, d_z, resistivity, ignore, name
72 passive_r = _parse_fortran_float(parts[1])
73 passive_z = _parse_fortran_float(parts[2])
74 passive_d_r = _parse_fortran_float(parts[3])
75 passive_d_z = _parse_fortran_float(parts[4])
76 passive_resistivity = _parse_fortran_float(parts[5])
77 name = parts[7].strip()
79 filaments[name]["r"].append(passive_r)
80 filaments[name]["z"].append(passive_z)
81 filaments[name]["d_r"].append(passive_d_r)
82 filaments[name]["d_z"].append(passive_d_z)
83 filaments[name]["resistivity"].append(passive_resistivity)
85 # Convert to numpy arrays and compute derived quantities
86 passives_data: dict[str, dict[str, Any]] = {}
88 for passive_name, data in filaments.items():
89 r = np.array(data["r"]).astype(np.float64)
90 z = np.array(data["z"]).astype(np.float64)
91 d_r = np.array(data["d_r"]).astype(np.float64)
92 d_z = np.array(data["d_z"]).astype(np.float64)
93 n_filaments: int = r.size
95 # Use mean resistivity for the passive (all filaments in a passive have the same resistivity)
96 resistivity: float = float(np.mean(data["resistivity"]))
98 # Angles are zero for rectangular filaments (not provided in fires.dat)
99 angle_1 = np.zeros(n_filaments, dtype=np.float64)
100 angle_2 = np.zeros(n_filaments, dtype=np.float64)
102 passives_data[passive_name] = {
103 "r": r,
104 "z": z,
105 "d_r": d_r,
106 "d_z": d_z,
107 "angle_1": angle_1,
108 "angle_2": angle_2,
109 "resistivity": resistivity,
110 }
112 return passives_data