Skip to content

Commit

Permalink
Merge pull request #186 from boutproject/plot-equilib-on-error
Browse files Browse the repository at this point in the history
In GUI, plot equilibrium even if there was an error in grid construction
  • Loading branch information
bendudson authored Nov 4, 2024
2 parents 6ee4d1c + b9ddece commit 7c6d49a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 25 deletions.
2 changes: 2 additions & 0 deletions doc/whats-new.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ Release history
===============

### Bug fixes
- When using the GUI, if there is an error in `TokamakEquilibrium` object
creation, still plot the equilibrium data (#186).


### New features
Expand Down
61 changes: 37 additions & 24 deletions hypnotoad/cases/tokamak.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,16 +523,22 @@ def __init__(

self.equilibOptions = {}

super().__init__(nonorthogonal_settings)

# Print the table of options
print(self.user_options.as_table(), flush=True)
if not self.user_options.orthogonal:
print(self.nonorthogonal_options.as_table(), flush=True)

if make_regions:
# Create self.regions
self.makeRegions()
try:
super().__init__(nonorthogonal_settings)

# Print the table of options
print(self.user_options.as_table(), flush=True)
if not self.user_options.orthogonal:
print(self.nonorthogonal_options.as_table(), flush=True)

if make_regions:
# Create self.regions
self.makeRegions()
except Exception:
# Some error occured, but still useful to return partially set up object,
# so, for example, the equilibrium data can be plotted
self.regions = {}
raise

def findLegs(self, xpoint, radius=0.01, step=0.01):
"""Find the divertor legs coming from a given X-point
Expand Down Expand Up @@ -1752,20 +1758,9 @@ def read_geqdsk(
pressure = data["pres"]
fpol = data["fpol"]

result = TokamakEquilibrium(
R1D,
Z1D,
psi2D,
psi1D,
fpol,
psi_bdry_gfile=psi_bdry_gfile,
psi_axis_gfile=psi_axis_gfile,
pressure=pressure,
wall=wall,
make_regions=make_regions,
settings=settings,
nonorthogonal_settings=nonorthogonal_settings,
)
# Call __new__() first in case there is an exception in __init__(), we can still
# return a partially-initialised TokamakEquilibrium object
result = TokamakEquilibrium.__new__(TokamakEquilibrium)

# Store geqdsk input as a string in the TokamakEquilibrium object so we can save it
# in BoutMesh.writeGridFile
Expand All @@ -1777,4 +1772,22 @@ def read_geqdsk(
if hasattr(filehandle, "name"):
result.geqdsk_filename = filehandle.name

try:
result.__init__(
R1D,
Z1D,
psi2D,
psi1D,
fpol,
psi_bdry_gfile=psi_bdry_gfile,
psi_axis_gfile=psi_axis_gfile,
pressure=pressure,
wall=wall,
make_regions=make_regions,
settings=settings,
nonorthogonal_settings=nonorthogonal_settings,
)
except Exception as e:
return result, e

return result
10 changes: 9 additions & 1 deletion hypnotoad/gui/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,9 +555,17 @@ def read_geqdsk(self):
settings=copy.deepcopy(self.options),
nonorthogonal_settings=copy.deepcopy(self.options),
)
try:
# If there was an error in tokamak.read_geqdsk(), it may return both
# the TokamakEquilibrium and an error, otherwise it would return
# just a TokamakEquilibrium.
self.eq, e = self.eq
self._popup_error_message(e)
except TypeError:
# No error, so self.eq is already the TokamakEquilibrium object.
pass
except (ValueError, RuntimeError, func_timeout.FunctionTimedOut) as e:
self._popup_error_message(e)
return

self.update_options_form()

Expand Down
12 changes: 12 additions & 0 deletions hypnotoad/scripts/hypnotoad_plot_equilibrium.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ def main():
try:
with open(args.equilibrium_file, "rt") as fh:
eq = tokamak.read_geqdsk(fh)
try:
# If there was an error in tokamak.read_geqdsk(), it may return both
# the TokamakEquilibrium and an error, otherwise it would return
# just a TokamakEquilibrium.
eq, e = eq
print(
f'Warning: got an error "{e}" while creating TokamakEquilibrium.',
"Continuing anyway",
)
except TypeError:
# No error, so self.eq is already the TokamakEquilibrium object.
pass
except ValueError:
# Maybe it was a disconnected double null? Need to tell hypnotoad
# nx_inter_sep>0 for disconnected case
Expand Down

0 comments on commit 7c6d49a

Please sign in to comment.