Skip to content

Commit

Permalink
Use attribute-dot-access for geqdsk files
Browse files Browse the repository at this point in the history
  • Loading branch information
ZedThree committed Sep 30, 2024
1 parent d9e134b commit 9bbf856
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 41 deletions.
35 changes: 9 additions & 26 deletions hypnotoad/cases/tokamak.py
Original file line number Diff line number Diff line change
Expand Up @@ -1716,51 +1716,34 @@ def read_geqdsk(
* ``psi_divide_twopi = bool`` - Divide poloidal flux, and so poloidal field, by 2pi
"""

if settings is None:
settings = {}

from freeqdsk.geqdsk import read as geq_read

data = geq_read(filehandle)

# Range of psi normalises psi derivatives
psi_bdry_gfile = data["sibdry"]
psi_axis_gfile = data["simagx"]

# 1D grid on which fpol is defined. Goes from normalised psi 0 to 1
psi1D = np.linspace(psi_axis_gfile, psi_bdry_gfile, data["nx"], endpoint=True)
psi1D = np.linspace(data.psi_axis, data.psi_boundary, data.nx, endpoint=True)

R1D = np.linspace(
data["rleft"], data["rleft"] + data["rdim"], data["nx"], endpoint=True
)
R1D = np.linspace(data.rleft, data.rleft + data.rdim, data.nx, endpoint=True)

Z1D = np.linspace(
data["zmid"] - 0.5 * data["zdim"],
data["zmid"] + 0.5 * data["zdim"],
data["ny"],
endpoint=True,
data.zmid - 0.5 * data.zdim, data.zmid + 0.5 * data.zdim, data.ny, endpoint=True
)

psi2D = data["psi"]

# Get the wall
wall = list(zip(data.rlim, data.zlim)) if data.nlim > 0 else None

pressure = data["pres"]
fpol = data["fpol"]

result = TokamakEquilibrium(
R1D,
Z1D,
psi2D,
data.psi,
psi1D,
fpol,
psi_bdry_gfile=psi_bdry_gfile,
psi_axis_gfile=psi_axis_gfile,
pressure=pressure,
fpol1D=data.fpol,
psi_bdry_gfile=data.psi_boundary,
psi_axis_gfile=data.psi_axis,
pressure=data.pressure,
wall=wall,
make_regions=make_regions,
settings=settings,
settings=settings or {},
nonorthogonal_settings=nonorthogonal_settings,
)

Expand Down
26 changes: 11 additions & 15 deletions hypnotoad/cases/torpex.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,41 +167,37 @@ def __init__(self, equilibOptions, meshOptions):
with open(equilibOptions["gfile"], "rt") as fh:
gfile = geq_read(fh)

R = numpy.linspace(
gfile["rleft"], gfile["rleft"] + gfile["rdim"], gfile["nx"]
)
R = numpy.linspace(gfile.rleft, gfile.rleft + gfile.rdim, gfile.nr)
Z = numpy.linspace(
gfile["zmid"] - 0.5 * gfile["zdim"],
gfile["zmid"] + 0.5 * gfile["zdim"],
gfile["ny"],
gfile.zmid - 0.5 * gfile.zdim, gfile.zmid + 0.5 * gfile.zdim, gfile.nz
)
self.Rmin = R[0]
self.Rmax = R[-1]
self.Zmin = Z[0]
self.Zmax = Z[-1]
psirz = gfile["psi"]
psirz = gfile.psi

# check sign of psirz is consistent with signs of psi_axis, psi_bndry and
# plasma current
psi_axis = gfile["simagx"]
R_axis = gfile["rmagx"]
Z_axis = gfile["zmagx"]
psi_bndry = gfile["sibdry"]
Ip = gfile["cpasma"]
psi_axis = gfile.psi_axis
R_axis = gfile.rmagx
Z_axis = gfile.zmagx
psi_bndry = gfile.psi_boundary

if psi_axis < psi_bndry:
# psi increases outward radially, so Bp is clockwise in the poloidal
# plane (outward in major radius at the top of the torus, inward at the
# bottom). This corresponds to plasma current in the anti-clockwise
# toroidal direction looking from above, so current should be positive
if Ip < 0.0:
if gfile.current < 0.0:
raise ValueError(
"direction of plasma current should be anti-clockwise to be "
"consistent with sign of grad(psi)"
)
else:
# psi decreases outward radially, so current should be in the opposite
# direction
if Ip > 0.0:
if gfile.current > 0.0:
raise ValueError(
"direction of plasma current should be clockwise to be "
"consistent with sign of grad(psi)"
Expand Down Expand Up @@ -247,7 +243,7 @@ def __init__(self, equilibOptions, meshOptions):
R, Z, psirz, self.user_options.psi_interpolation_method
)

self.Bt_axis = gfile["bcentr"]
self.Bt_axis = gfile.bcentr
elif "matfile" in equilibOptions:
# Loading directly from the TORPEX-provided matlab file should be slightly
# more accurate than going via a g-file because g-files don't save full
Expand Down

0 comments on commit 9bbf856

Please sign in to comment.