From 742ed80906c4d89563651c67db8cca63c414dbca Mon Sep 17 00:00:00 2001 From: Michael Hardman <29800382+mrhardman@users.noreply.github.com> Date: Mon, 6 Jul 2026 10:22:11 +0100 Subject: [PATCH 1/6] Attempt to add Hypnotoad like corners to gridue_to_bout.py converter. --- .../gridue_to_bout.py | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/boutdata/gridue_to_bout_converter/gridue_to_bout.py b/src/boutdata/gridue_to_bout_converter/gridue_to_bout.py index 737e9ad1c..968c7f57c 100644 --- a/src/boutdata/gridue_to_bout_converter/gridue_to_bout.py +++ b/src/boutdata/gridue_to_bout_converter/gridue_to_bout.py @@ -681,10 +681,35 @@ def Convert_grids( # Calculate curvature curl_bOverB_Rhat, curl_bOverB_Zhat, curl_bOverB_zetahat = calcRZCurvature(g) + # Ordering for corners + # (1) -- (3) + # | | + # | (0) | -> Radial, BOUT++ "x" + # | | + # (2) -- (4) + Rxy_corners = rm[:, :, 2].T + Zxy_corners = zm[:, :, 2].T + Rxy_lower_right_corners = rm[:, :, 4].T + Zxy_lower_right_corners = zm[:, :, 4].T + Rxy_upper_right_corners = rm[:, :, 3].T + Zxy_upper_right_corners = zm[:, :, 3].T + Rxy_upper_left_corners = rm[:, :, 1].T + Zxy_upper_left_corners = zm[:, :, 1].T + # Collect 2D variables for output grd = { "Rxy": Rxy, "Zxy": Zxy, + # corner data in Hypnotoad output variables + "Rxy_corners": Rxy_corners, + "Zxy_corners": Zxy_corners, + "Rxy_lower_right_corners": Rxy_lower_right_corners, + "Zxy_lower_right_corners": Zxy_lower_right_corners, + "Rxy_upper_right_corners": Rxy_upper_right_corners, + "Zxy_upper_right_corners": Zxy_upper_right_corners, + "Rxy_upper_left_corners": Rxy_upper_left_corners, + "Zxy_upper_left_corners": Zxy_upper_left_corners, + # end corner data "psixy": psixy, "dx": dx, "dy": np.full((nx, ny), dy), @@ -756,7 +781,12 @@ def Convert_grids( nx, ny = var.shape newvar = np.zeros((nx + 2, ny)) newvar[2:-2, :] = var[1:-1, :] - if name in ["Rxy", "Zxy", "psixy"]: + if name in ["Rxy", "Zxy", "psixy", + "Rxy_corners", "Zxy_corners", + "Rxy_lower_right_corners", "Zxy_lower_right_corners", + "Rxy_upper_right_corners", "Zxy_upper_right_corners", + "Rxy_upper_left_corners", "Zxy_upper_left_corners", + ]: # Linear extrapolation newvar[1, :] = 2.0 * newvar[2, :] - newvar[3, :] newvar[0, :] = 2.0 * newvar[1, :] - newvar[2, :] @@ -916,6 +946,7 @@ def Convert_grids( f.write("ny_inner", ny_inner) f.write("jyseps1_2", jyseps1_2) f.write("jyseps2_2", jyseps2_2) + f.write("y_boundary_guards", 0) f.write("rm", rm) f.write("zm", zm) f.write("topology", mesh_topology) From 5fb2672505feddc1404256ec0e0a773371d3f0f9 Mon Sep 17 00:00:00 2001 From: mrhardman Date: Mon, 6 Jul 2026 09:22:46 +0000 Subject: [PATCH 2/6] Apply ruff changes --- .../gridue_to_bout.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/boutdata/gridue_to_bout_converter/gridue_to_bout.py b/src/boutdata/gridue_to_bout_converter/gridue_to_bout.py index 968c7f57c..2ea2c22ae 100644 --- a/src/boutdata/gridue_to_bout_converter/gridue_to_bout.py +++ b/src/boutdata/gridue_to_bout_converter/gridue_to_bout.py @@ -781,12 +781,19 @@ def Convert_grids( nx, ny = var.shape newvar = np.zeros((nx + 2, ny)) newvar[2:-2, :] = var[1:-1, :] - if name in ["Rxy", "Zxy", "psixy", - "Rxy_corners", "Zxy_corners", - "Rxy_lower_right_corners", "Zxy_lower_right_corners", - "Rxy_upper_right_corners", "Zxy_upper_right_corners", - "Rxy_upper_left_corners", "Zxy_upper_left_corners", - ]: + if name in [ + "Rxy", + "Zxy", + "psixy", + "Rxy_corners", + "Zxy_corners", + "Rxy_lower_right_corners", + "Zxy_lower_right_corners", + "Rxy_upper_right_corners", + "Zxy_upper_right_corners", + "Rxy_upper_left_corners", + "Zxy_upper_left_corners", + ]: # Linear extrapolation newvar[1, :] = 2.0 * newvar[2, :] - newvar[3, :] newvar[0, :] = 2.0 * newvar[1, :] - newvar[2, :] From 426155082a391a6da7f9a605660ee28f5d67be3e Mon Sep 17 00:00:00 2001 From: Michael Hardman <29800382+mrhardman@users.noreply.github.com> Date: Thu, 9 Jul 2026 17:43:37 +0100 Subject: [PATCH 3/6] Change identification of corner points. --- .../gridue_to_bout_converter/gridue_to_bout.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/boutdata/gridue_to_bout_converter/gridue_to_bout.py b/src/boutdata/gridue_to_bout_converter/gridue_to_bout.py index 968c7f57c..7e58ec1d1 100644 --- a/src/boutdata/gridue_to_bout_converter/gridue_to_bout.py +++ b/src/boutdata/gridue_to_bout_converter/gridue_to_bout.py @@ -687,14 +687,14 @@ def Convert_grids( # | (0) | -> Radial, BOUT++ "x" # | | # (2) -- (4) - Rxy_corners = rm[:, :, 2].T - Zxy_corners = zm[:, :, 2].T - Rxy_lower_right_corners = rm[:, :, 4].T - Zxy_lower_right_corners = zm[:, :, 4].T + Rxy_corners = rm[:, :, 1].T + Zxy_corners = zm[:, :, 1].T + Rxy_lower_right_corners = rm[:, :, 2].T + Zxy_lower_right_corners = zm[:, :, 2].T Rxy_upper_right_corners = rm[:, :, 3].T Zxy_upper_right_corners = zm[:, :, 3].T - Rxy_upper_left_corners = rm[:, :, 1].T - Zxy_upper_left_corners = zm[:, :, 1].T + Rxy_upper_left_corners = rm[:, :, 4].T + Zxy_upper_left_corners = zm[:, :, 4].T # Collect 2D variables for output grd = { From cb3a81ce101fdd903e4fa143c0fcc53013da1807 Mon Sep 17 00:00:00 2001 From: Michael Hardman <29800382+mrhardman@users.noreply.github.com> Date: Tue, 14 Jul 2026 15:46:15 +0100 Subject: [PATCH 4/6] Only remove upper Y guard cells if topology is CDN or UDN. --- .../gridue_to_bout.py | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/boutdata/gridue_to_bout_converter/gridue_to_bout.py b/src/boutdata/gridue_to_bout_converter/gridue_to_bout.py index 8653510de..0185075a0 100644 --- a/src/boutdata/gridue_to_bout_converter/gridue_to_bout.py +++ b/src/boutdata/gridue_to_bout_converter/gridue_to_bout.py @@ -762,17 +762,19 @@ def Convert_grids( ixseps1 = g["iyseparatrix1"] + 2 # Lower X-point separatrix ixseps2 = min(g["iyseparatrix2"] + 2, nx) # Upper X-point separatrix # Double null -> Remove upper Y guard cells - ny_inner = g["ix_inner"] - for name in grd: - var = grd[name] - nx, ny = var.shape - newvar = np.zeros((nx, ny - 2)) - newvar[:, :ny_inner] = var[:, :ny_inner] - newvar[:, ny_inner:] = var[:, (ny_inner + 2) :] - grd[name] = newvar - g["ix_cut2"] = g["ix_cut2"] - 1 - g["ix_cut3"] = g["ix_cut3"] - 3 - g["ix_cut4"] = g["ix_cut4"] - 2 + # check if CDN or UDN + if mesh_topology[1:] == "DN": + ny_inner = g["ix_inner"] + for name in grd: + var = grd[name] + nx, ny = var.shape + newvar = np.zeros((nx, ny - 2)) + newvar[:, :ny_inner] = var[:, :ny_inner] + newvar[:, ny_inner:] = var[:, (ny_inner + 2) :] + grd[name] = newvar + g["ix_cut2"] = g["ix_cut2"] - 1 + g["ix_cut3"] = g["ix_cut3"] - 3 + g["ix_cut4"] = g["ix_cut4"] - 2 # Extrapolate X (radial) boundary cells # Removing one cell, adding two on each X boundary From a1cf9682aa5ab9a49382637cef68e13d43df5213 Mon Sep 17 00:00:00 2001 From: Michael Hardman <29800382+mrhardman@users.noreply.github.com> Date: Fri, 30 Oct 2026 15:29:56 +0000 Subject: [PATCH 5/6] Change identification of lower_right, upper_left, upper_right corners. --- .../gridue_to_bout_converter/gridue_to_bout.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/boutdata/gridue_to_bout_converter/gridue_to_bout.py b/src/boutdata/gridue_to_bout_converter/gridue_to_bout.py index 0185075a0..b5bcc1441 100644 --- a/src/boutdata/gridue_to_bout_converter/gridue_to_bout.py +++ b/src/boutdata/gridue_to_bout_converter/gridue_to_bout.py @@ -689,12 +689,12 @@ def Convert_grids( # (2) -- (4) Rxy_corners = rm[:, :, 1].T Zxy_corners = zm[:, :, 1].T - Rxy_lower_right_corners = rm[:, :, 2].T - Zxy_lower_right_corners = zm[:, :, 2].T - Rxy_upper_right_corners = rm[:, :, 3].T - Zxy_upper_right_corners = zm[:, :, 3].T - Rxy_upper_left_corners = rm[:, :, 4].T - Zxy_upper_left_corners = zm[:, :, 4].T + Rxy_lower_right_corners = rm[:, :, 3].T + Zxy_lower_right_corners = zm[:, :, 3].T + Rxy_upper_right_corners = rm[:, :, 4].T + Zxy_upper_right_corners = zm[:, :, 4].T + Rxy_upper_left_corners = rm[:, :, 2].T + Zxy_upper_left_corners = zm[:, :, 2].T # Collect 2D variables for output grd = { From e9b84777023f8d86e49bfaeafa8e5e2e5aed2fb3 Mon Sep 17 00:00:00 2001 From: mrhardman Date: Fri, 30 Oct 2026 17:19:21 +0000 Subject: [PATCH 6/6] Apply ruff changes --- .../gridue_to_bout_converter/README.md | 72 ++++++++++++------- 1 file changed, 45 insertions(+), 27 deletions(-) diff --git a/src/boutdata/gridue_to_bout_converter/README.md b/src/boutdata/gridue_to_bout_converter/README.md index 155ab5543..c072ed05e 100644 --- a/src/boutdata/gridue_to_bout_converter/README.md +++ b/src/boutdata/gridue_to_bout_converter/README.md @@ -5,44 +5,62 @@ It can be ran from the terminal. To run it directly after the creation of a gridue file on INGRID the following code needs to be added to ingrid.py: ```python - def ExportBOUTgrid(self, gridue_file, bout_grid_name: str = 'bout_from_in.grd.nc', plotting = True, verbose = True, ignore_checks = False): - """ - Export a BOUT grid file for the created grid. +def ExportBOUTgrid( + self, + gridue_file, + bout_grid_name: str = "bout_from_in.grd.nc", + plotting=True, + verbose=True, + ignore_checks=False, +): + """ + Export a BOUT grid file for the created grid. - Parameters - ---------- - gridue_file : str, mandatory - Name of gridue file to convert to BOUT grid. + Parameters + ---------- + gridue_file : str, mandatory + Name of gridue file to convert to BOUT grid. - bout_grid_name : str, optional - Name of BOUT grid file to save. + bout_grid_name : str, optional + Name of BOUT grid file to save. - plotting : bool, optional - If True, plot the gridue file before conversion. + plotting : bool, optional + If True, plot the gridue file before conversion. - verbose : bool, optional - If True, print verbose output during conversion. + verbose : bool, optional + If True, print verbose output during conversion. - ignore_checks : bool, optional - If True, ignore checks for gridue file format and structure. - - """ - bout_grid_name = gridue_file + "_" + bout_grid_name - Convert_grids(gridue_file,bout_grid_name,plotting, verbose, ignore_checks) + ignore_checks : bool, optional + If True, ignore checks for gridue file format and structure. + """ + bout_grid_name = gridue_file + "_" + bout_grid_name + Convert_grids(gridue_file, bout_grid_name, plotting, verbose, ignore_checks) ``` And modifying the `ExportGridue` function to: ```python - if type(self.CurrentTopology) in [SNL]: - if self.WriteGridueSNL(self.CurrentTopology.gridue_settings, fname): - print(f"# Successfully saved gridue file as '{fname}'") - self.ExportBOUTgrid(fname, 'bout_from_in.grd.nc', plotting = True, verbose = True, ignore_checks = False) - elif type(self.CurrentTopology) in [SF15, SF45, SF75, SF105, SF135, SF165, UDN, CDN]: - if self.WriteGridueDNL(self.CurrentTopology.gridue_settings, fname): - print(f"# Successfully saved gridue file as '{fname}'") - self.ExportBOUTgrid(fname, 'bout_from_in.grd.nc', plotting = True, verbose = True, ignore_checks = False) +if type(self.CurrentTopology) in [SNL]: + if self.WriteGridueSNL(self.CurrentTopology.gridue_settings, fname): + print(f"# Successfully saved gridue file as '{fname}'") + self.ExportBOUTgrid( + fname, + "bout_from_in.grd.nc", + plotting=True, + verbose=True, + ignore_checks=False, + ) +elif type(self.CurrentTopology) in [SF15, SF45, SF75, SF105, SF135, SF165, UDN, CDN]: + if self.WriteGridueDNL(self.CurrentTopology.gridue_settings, fname): + print(f"# Successfully saved gridue file as '{fname}'") + self.ExportBOUTgrid( + fname, + "bout_from_in.grd.nc", + plotting=True, + verbose=True, + ignore_checks=False, + ) ``` *Note:* The created file will have