Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 45 additions & 27 deletions src/boutdata/gridue_to_bout_converter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
64 changes: 52 additions & 12 deletions src/boutdata/gridue_to_bout_converter/gridue_to_bout.py
Original file line number Diff line number Diff line change
Expand Up @@ -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[:, :, 1].T
Zxy_corners = zm[:, :, 1].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 = {
"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),
Expand Down Expand Up @@ -737,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
Expand All @@ -756,7 +783,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"]:
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, :]
Expand Down Expand Up @@ -916,6 +955,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)
Expand Down
Loading