diff --git a/doc/whats-new.md b/doc/whats-new.md index 205f7dbf..1c65d9b9 100644 --- a/doc/whats-new.md +++ b/doc/whats-new.md @@ -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 diff --git a/hypnotoad/cases/tokamak.py b/hypnotoad/cases/tokamak.py index ea5c98b9..4a472c31 100644 --- a/hypnotoad/cases/tokamak.py +++ b/hypnotoad/cases/tokamak.py @@ -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 @@ -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 @@ -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 diff --git a/hypnotoad/gui/gui.py b/hypnotoad/gui/gui.py index 96baee09..721358b1 100644 --- a/hypnotoad/gui/gui.py +++ b/hypnotoad/gui/gui.py @@ -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() diff --git a/hypnotoad/scripts/hypnotoad_plot_equilibrium.py b/hypnotoad/scripts/hypnotoad_plot_equilibrium.py index 43164589..fa671a90 100755 --- a/hypnotoad/scripts/hypnotoad_plot_equilibrium.py +++ b/hypnotoad/scripts/hypnotoad_plot_equilibrium.py @@ -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