Skip to content

Commit

Permalink
Save R,Z locations for all 4 corners of each cell
Browse files Browse the repository at this point in the history
The corner positions can be useful for plotting, or coupling to other
codes. Saving all four corners separately means no special handling is
needed for branch cuts or boundaries, and there are no missing values
(e.g. upper corners at an upper boundary).
  • Loading branch information
johnomotani committed Feb 13, 2023
1 parent 8446529 commit da502ac
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 7 deletions.
9 changes: 9 additions & 0 deletions doc/grid-file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ Spatial positions
- Major radius and height of the lower-left corner of each grid cell. Not
needed by BOUT++, but may be useful for post-processing.

* - ``Rxy_lower_right_corners``, ``Zxy_lower_right_corners``,
``Rxy_upper_right_corners``, ``Zxy_upper_right_corners``,
``Rxy_upper_left_corners``, ``Zxy_upper_left_corners``

- Major radius and height of the other three corners of each grid cell.
Mostly redundant information with ``Rxy_corners`` and ``Zxy_corners``,
but may make handling branch cuts and upper/outer boundaries more
convenient. Not needed by BOUT++, but may be useful for post-processing.

Grid spacings
+++++++++++++

Expand Down
2 changes: 2 additions & 0 deletions doc/whats-new.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Release history

### New features

- Save R,Z locations for all four corners of each cell (#168)\
By [John Omotani](https://github.com/johnomotani)
- `PsiContour.plot()` and `FineContour.plot()` can be called with an `ax`
argument, and passing `psi` is optional (#163)\
By [Ben Dudson](https://github.com/bendudson)
Expand Down
29 changes: 26 additions & 3 deletions hypnotoad/core/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -3311,7 +3311,7 @@ def geometry(self):
# Call geometry() method of base class
super().geometry()

def addFromRegions(name):
def addFromRegions(name, *, all_corners=False):
# Collect a 2d field from the regions
self.fields_to_output.append(name)
f = MultiLocationArray(self.nx, self.ny)
Expand All @@ -3334,6 +3334,17 @@ def addFromRegions(name):
f.corners[self.region_indices[region.myID]] = f_region.corners[
:-1, :-1
]
if all_corners:
if f_region._corners_array is not None:
f.lower_right_corners[
self.region_indices[region.myID]
] = f_region.corners[1:, :-1]
f.upper_right_corners[
self.region_indices[region.myID]
] = f_region.corners[1:, 1:]
f.upper_left_corners[
self.region_indices[region.myID]
] = f_region.corners[:-1, 1:]

# Set 'bout_type' so it gets saved in the grid file
f.attributes["bout_type"] = "Field2D"
Expand Down Expand Up @@ -3369,8 +3380,8 @@ def addFromRegionsXArray(name):
# Set 'bout_type' so it gets saved in the grid file
f.attributes["bout_type"] = "ArrayX"

addFromRegions("Rxy")
addFromRegions("Zxy")
addFromRegions("Rxy", all_corners=True)
addFromRegions("Zxy", all_corners=True)
addFromRegions("psixy")
addFromRegions("dx")
addFromRegions("dy")
Expand Down Expand Up @@ -3436,6 +3447,18 @@ def writeCorners(self, name, array, f):
name + "_corners",
BoutArray(array.corners[:-1, :-1], attributes=array.attributes),
)
f.write(
name + "_lower_right_corners",
BoutArray(array.lower_right_corners[:-1, :-1], attributes=array.attributes),
)
f.write(
name + "_upper_right_corners",
BoutArray(array.upper_right_corners[:-1, :-1], attributes=array.attributes),
)
f.write(
name + "_upper_left_corners",
BoutArray(array.upper_left_corners[:-1, :-1], attributes=array.attributes),
)

def writeArrayXDirection(self, name, array, f):
f.write(name, BoutArray(array.centre[:, 0], attributes=array.attributes))
Expand Down
53 changes: 53 additions & 0 deletions hypnotoad/core/multilocationarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,22 @@ class MultiLocationArray(numpy.lib.mixins.NDArrayOperatorsMixin):
"""
Container for arrays representing points at different cell locations
Not all have to be filled.
Note the ``lower_right_corners``, ``upper_right_corners``, and
``upper_left_corners`` members are only intended to be used for the global arrays,
as within each region the ``corners`` member contains all the corners for every
cell. ``lower_right_corners``, ``upper_right_corners``, and ``upper_left_corners``
are therefore not set to zero in ``MultiLocationArray.zero()`` as they do not need
to be initialized.
"""

_centre_array = None
_xlow_array = None
_ylow_array = None
_corners_array = None
_lower_right_corners_array = None
_upper_right_corners_array = None
_upper_left_corners_array = None

def __init__(self, nx, ny):
self.nx = nx
Expand Down Expand Up @@ -67,6 +77,42 @@ def corners(self, value):
self._corners_array = numpy.zeros([self.nx + 1, self.ny + 1])
self._corners_array[...] = value

@property
def lower_right_corners(self):
if self._lower_right_corners_array is None:
self._lower_right_corners_array = numpy.zeros([self.nx + 1, self.ny + 1])
return self._lower_right_corners_array

@lower_right_corners.setter
def lower_right_corners(self, value):
if self._lower_right_corners_array is None:
self._lower_right_corners_array = numpy.zeros([self.nx + 1, self.ny + 1])
self._lower_right_corners_array[...] = value

@property
def upper_right_corners(self):
if self._upper_right_corners_array is None:
self._upper_right_corners_array = numpy.zeros([self.nx + 1, self.ny + 1])
return self._upper_right_corners_array

@upper_right_corners.setter
def upper_right_corners(self, value):
if self._upper_right_corners_array is None:
self._upper_right_corners_array = numpy.zeros([self.nx + 1, self.ny + 1])
self._upper_right_corners_array[...] = value

@property
def upper_left_corners(self):
if self._upper_left_corners_array is None:
self._upper_left_corners_array = numpy.zeros([self.nx + 1, self.ny + 1])
return self._upper_left_corners_array

@upper_left_corners.setter
def upper_left_corners(self, value):
if self._upper_left_corners_array is None:
self._upper_left_corners_array = numpy.zeros([self.nx + 1, self.ny + 1])
self._upper_left_corners_array[...] = value

def copy(self):
new_multilocationarray = MultiLocationArray(self.nx, self.ny)
if self.centre is not None:
Expand Down Expand Up @@ -219,6 +265,13 @@ def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):

def zero(self):
# Initialise all locations, set them to zero and return the result
#
# Note the ``lower_right_corners``, ``upper_right_corners``, and
# ``upper_left_corners`` members are only intended to be used for the global
# arrays, as within each region the ``corners`` member contains all the corners
# for every cell. ``lower_right_corners``, ``upper_right_corners``, and
# ``upper_left_corners`` are therefore not set to zero here as they do not need
# to be initialized.
self.centre = 0.0
self.xlow = 0.0
self.ylow = 0.0
Expand Down
Git LFS file not shown
Git LFS file not shown

0 comments on commit da502ac

Please sign in to comment.