Skip to content

Commit 74f697f

Browse files
committed
move norm to array indexer
1 parent 9a816e3 commit 74f697f

2 files changed

Lines changed: 109 additions & 13 deletions

File tree

mesh/array_indexer.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,14 @@ def norm(self, n=0):
103103
"""
104104
c = len(self.shape)
105105
if c == 2:
106-
return self.g.norm(self)
106+
return np.sqrt(self.g.dx * self.g.dy *
107+
np.sum((self[self.g.ilo:self.g.ihi+1, self.g.jlo:self.g.jhi+1]**2).flat))
108+
107109
else:
108-
return self.g.norm(self[:, :, n])
110+
_tmp = self[:, :, n]
111+
return np.sqrt(self.g.dx * self.g.dy *
112+
np.sum((_tmp[self.g.ilo:self.g.ihi+1, self.g.jlo:self.g.jhi+1]**2).flat))
113+
109114

110115
def copy(self):
111116
"""make a copy of the array, defined on the same grid"""

mesh/patch.py

Lines changed: 102 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,6 @@ def scratch_array(self, nvar=1):
157157
_tmp = np.zeros((self.qx, self.qy, nvar), dtype=np.float64)
158158
return ai.ArrayIndexer(d=_tmp, grid=self)
159159

160-
def norm(self, d):
161-
"""
162-
find the norm of the quantity d defined on the same grid, in the
163-
domain's valid region
164-
"""
165-
return np.sqrt(self.dx * self.dy *
166-
np.sum((d[self.ilo:self.ihi+1, self.jlo:self.jhi+1]**2).flat))
167-
168160
def coarse_like(self, N):
169161
"""
170162
return a new grid object coarsened by a factor n, but with
@@ -245,9 +237,6 @@ def __init__(self, grid, dtype=np.float64):
245237
dtype : NumPy data type, optional
246238
The datatype of the data we wish to create (defaults to
247239
np.float64
248-
runtime_parameters : RuntimeParameters object, optional
249-
The runtime parameters that go along with this data
250-
251240
"""
252241

253242
self.grid = grid
@@ -659,6 +648,108 @@ def pretty_print(self, var, fmt=None):
659648
a.pretty_print(fmt=fmt)
660649

661650

651+
class FaceCenterData2d(CellCenterData2d):
652+
"""
653+
A class to define face-centered data that lives on a grid. Data
654+
can be face-centered in x or y. This is built in the same multistep
655+
process as a CellCenterData2d object"""
656+
657+
def __init__(self, grid, idir, dtype=np.float64):
658+
"""
659+
Initialize the FaceCenterData2d object
660+
661+
Parameters
662+
----------
663+
grid : Grid2d object
664+
The grid upon which the data will live
665+
idir : the direction in which we are face-centered (this will be
666+
1 for x or 2 for y)
667+
dtype : NumPy data type, optional
668+
The datatype of the data we wish to create (defaults to
669+
np.float64
670+
"""
671+
672+
super().__init__(grid, dtype=dtype)
673+
self.idir = idir
674+
675+
def add_derived(self, func):
676+
raise NotImplementedError("derived variables not yet supported for face-centered data")
677+
678+
679+
def create(self):
680+
"""Called after all the variables are registered and allocates the
681+
storage for the state data. For face-centered data, we have
682+
one more zone in the face-centered direction.
683+
684+
"""
685+
686+
if self.initialized == 1:
687+
msg.fail("ERROR: grid already initialized")
688+
689+
if self.idir == 1:
690+
_tmp = np.zeros((self.grid.qx+1, self.grid.qy, self.nvar),
691+
dtype=self.dtype)
692+
elif self.idir == 2:
693+
_tmp = np.zeros((self.grid.qx, self.grid.qy+1, self.nvar),
694+
dtype=self.dtype)
695+
696+
self.data = ai.ArrayIndexerFC(_tmp, idir=self.idir, grid=self.grid)
697+
698+
self.initialized = 1
699+
700+
def fill_BC(self, name):
701+
"""
702+
Fill the boundary conditions. This operates on a single state
703+
variable at a time, to allow for maximum flexibility.
704+
705+
We do periodic, reflect-even, reflect-odd, and outflow
706+
707+
Each variable name has a corresponding BC stored in the
708+
CellCenterData2d object -- we refer to this to figure out the
709+
action to take at each boundary.
710+
711+
Parameters
712+
----------
713+
name : str
714+
The name of the variable for which to fill the BCs.
715+
716+
"""
717+
718+
n = self.names.index(name)
719+
self.data.fill_ghost(n=n, bc=self.BCs[name])
720+
721+
if self.BCs[name].xlb in bnd.ext_bcs.keys() or \
722+
self.BCs[name].xrb in bnd.ext_bcs.keys() or \
723+
self.BCs[name].ylb in bnd.ext_bcs.keys() or \
724+
self.BCs[name].yrb in bnd.ext_bcs.keys():
725+
raise NotImplementedError("custom boundary conditions not supported for FaceCenterData2d")
726+
727+
def restrict(self, varname, N=2):
728+
raise NotImplementedError("restriction not implemented for FaceCenterData2d")
729+
730+
def prolong(self, varname):
731+
raise NotImplementedError("prolongation not implemented for FaceCenterData2d")
732+
733+
def write_data(self, f):
734+
"""
735+
write the data out to an hdf5 file -- here, f is an h5py
736+
File pbject
737+
738+
"""
739+
740+
# data
741+
gstate = f.create_group("face-centered-state")
742+
743+
for n in range(self.nvar):
744+
gvar = gstate.create_group(self.names[n])
745+
gvar.create_dataset("data",
746+
data=self.get_var_by_index(n).v())
747+
gvar.attrs["xlb"] = self.BCs[self.names[n]].xlb
748+
gvar.attrs["xrb"] = self.BCs[self.names[n]].xrb
749+
gvar.attrs["ylb"] = self.BCs[self.names[n]].ylb
750+
gvar.attrs["yrb"] = self.BCs[self.names[n]].yrb
751+
752+
662753
def cell_center_data_clone(old):
663754
"""
664755
Create a new CellCenterData2d object that is a copy of an existing

0 commit comments

Comments
 (0)