Skip to content

Commit

Permalink
merge region.py from master
Browse files Browse the repository at this point in the history
  • Loading branch information
qixia committed Nov 4, 2024
1 parent 6ff30c1 commit 98baac0
Showing 1 changed file with 31 additions and 38 deletions.
69 changes: 31 additions & 38 deletions xbout/region.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from copy import copy, deepcopy
from copy import deepcopy
import numpy as np
import xarray as xr
from .utils import _set_attrs_on_all_vars
Expand Down Expand Up @@ -72,30 +72,30 @@ def __init__(
if ds.metadata["keep_xboundaries"]:
xbndry = ds.metadata["MXG"]
if self.connection_inner_x is None:
self.nx -= xbndry
self.nx = self.nx - xbndry

# used to calculate x-coordinate of inner side (self.xinner)
xinner_ind += xbndry
xinner_ind = xinner_ind + xbndry

if self.connection_outer_x is None:
self.nx -= xbndry
self.nx = self.nx - xbndry

# used to calculate x-coordinate of outer side (self.xouter)
xouter_ind -= xbndry
xouter_ind = xouter_ind - xbndry

if ds.metadata["keep_yboundaries"]:
ybndry = ds.metadata["MYG"]
if self.connection_lower_y is None:
self.ny -= ybndry
self.ny = self.ny - ybndry

# used to calculate y-coordinate of lower side (self.ylower)
ylower_ind += ybndry
ylower_ind = ylower_ind + ybndry

if self.connection_upper_y is None:
self.ny -= ybndry
self.ny = self.ny - ybndry

# used to calculate y-coordinate of upper side (self.yupper)
yupper_ind -= ybndry
yupper_ind = yupper_ind - ybndry

# calculate start and end coordinates
#####################################
Expand Down Expand Up @@ -145,9 +145,6 @@ def __repr__(self):
result += f"\t{attr}\t{val}\n"
return result

def __eq__(self, other):
return vars(self) == vars(other)

def get_slices(self, mxg=0, myg=0):
"""
Return x- and y-dimension slices that select this region from the global
Expand All @@ -159,19 +156,19 @@ def get_slices(self, mxg=0, myg=0):
"""
xi = self.xinner_ind
if self.connection_inner_x is not None and xi is not None:
xi -= mxg
xi = xi - mxg

xo = self.xouter_ind
if self.connection_outer_x is not None and xo is not None:
xi += mxg
xo = xo + mxg

yl = self.ylower_ind
if self.connection_lower_y is not None and yl is not None:
yl -= myg
yl = yl - myg

yu = self.yupper_ind
if self.connection_upper_y is not None and yu is not None:
yu += myg
yu = yu + myg

return {self.xcoord: slice(xi, xo), self.ycoord: slice(yl, yu)}

Expand All @@ -189,10 +186,10 @@ def get_inner_guards_slices(self, *, mxg, myg=0):
"""
ylower = self.ylower_ind
if self.connection_lower_y is not None:
ylower -= myg
ylower = ylower - myg
yupper = self.yupper_ind
if self.connection_upper_y is not None:
yupper += myg
yupper = yupper + myg
return {
self.xcoord: slice(self.xinner_ind - mxg, self.xinner_ind),
self.ycoord: slice(ylower, yupper),
Expand All @@ -212,10 +209,10 @@ def get_outer_guards_slices(self, *, mxg, myg=0):
"""
ylower = self.ylower_ind
if self.connection_lower_y is not None:
ylower -= myg
ylower = ylower - myg
yupper = self.yupper_ind
if self.connection_upper_y is not None:
yupper += myg
yupper = yupper + myg
return {
self.xcoord: slice(self.xouter_ind, self.xouter_ind + mxg),
self.ycoord: slice(ylower, yupper),
Expand All @@ -235,10 +232,10 @@ def get_lower_guards_slices(self, *, myg, mxg=0):
"""
xinner = self.xinner_ind
if self.connection_inner_x is not None:
xinner -= mxg
xinner = xinner - mxg
xouter = self.xouter_ind
if self.connection_outer_x is not None:
xouter += mxg
xouter = xouter + mxg

ystart = self.ylower_ind - myg
ystop = self.ylower_ind
Expand Down Expand Up @@ -270,10 +267,10 @@ def get_upper_guards_slices(self, *, myg, mxg=0):
"""
xinner = self.xinner_ind
if self.connection_inner_x is not None:
xinner -= mxg
xinner = xinner - mxg
xouter = self.xouter_ind
if self.connection_outer_x is not None:
xouter += mxg
xouter = xouter + mxg

# wrap around to the beginning of the grid if necessary
ystart = self.yupper_ind
Expand Down Expand Up @@ -1189,15 +1186,15 @@ def _create_regions_toroidal(ds):
# Adjust for boundary cells
# keep_xboundaries is 1 if there are x-boundaries and 0 if there are not
if not ds.metadata["keep_xboundaries"]:
ixs1 -= mxg
ixs2 -= mxg
nx -= 2 * mxg
jys11 += ybndry
jys21 += ybndry
ny_inner += ybndry + ybndry_upper
jys12 += ybndry + 2 * ybndry_upper
jys22 += ybndry + 2 * ybndry_upper
ny += 2 * ybndry + 2 * ybndry_upper
ixs1 = ixs1 - mxg
ixs2 = ixs2 - mxg
nx = nx - 2 * mxg
jys11 = jys11 + ybndry
jys21 = jys21 + ybndry
ny_inner = ny_inner + ybndry + ybndry_upper
jys12 = jys12 + ybndry + 2 * ybndry_upper
jys22 = jys22 + ybndry + 2 * ybndry_upper
ny = ny + 2 * ybndry + 2 * ybndry_upper

# Note, include guard cells in the created regions, fill them later
if topology in topologies:
Expand All @@ -1220,6 +1217,7 @@ def _create_regions_toroidal(ds):
_check_connections(regions)

ds = _set_attrs_on_all_vars(ds, "regions", regions)
ds.metadata["topology"] = topology

return ds

Expand All @@ -1228,11 +1226,6 @@ def _create_single_region(ds, periodic_y=True):
nx = ds.metadata["nx"]
ny = ds.metadata["ny"]

mxg = ds.metadata["MXG"]
myg = ds.metadata["MYG"]
# keep_yboundaries is 1 if there are y-boundaries and 0 if there are not
ybndry = ds.metadata["keep_yboundaries"] * myg

connection = "all" if periodic_y else None

regions = {
Expand Down

0 comments on commit 98baac0

Please sign in to comment.