From 7f5f30a7e6b668301bc528ec96ccad19e6ec2b3e Mon Sep 17 00:00:00 2001 From: John Omotani Date: Wed, 16 Oct 2024 16:21:28 +0100 Subject: [PATCH] In GUI, plot equilibrium even if there was an error in grid construction --- hypnotoad/cases/tokamak.py | 61 +++++++++++++++++++++++--------------- hypnotoad/gui/gui.py | 10 ++++++- 2 files changed, 46 insertions(+), 25 deletions(-) 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..79e3e3df 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()