Skip to content

Commit

Permalink
In GUI, plot equilibrium even if there was an error in grid construction
Browse files Browse the repository at this point in the history
  • Loading branch information
johnomotani committed Oct 16, 2024
1 parent 6ee4d1c commit c287472
Show file tree
Hide file tree
Showing 3 changed files with 48 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

0 comments on commit c287472

Please sign in to comment.