TensorMesh now inherits BaseMesh (worked with Luz!)

tests for tensorMesh and utils (e.g. ndgrid) are included and pass

Split the TensorMesh into Grid and View
This commit is contained in:
Rowan Cockett
2013-07-09 19:50:40 -07:00
parent 6c4910a157
commit 8e3e0e0faa
6 changed files with 439 additions and 265 deletions
+144
View File
@@ -0,0 +1,144 @@
import numpy as np
from utils import ndgrid
class TensorGrid(object):
"""
Define nodal, cell-centered and staggered tensor grids for 1, 2 and 3
dimensions.
This class is inherited by TensorMesh
"""
def __init__(self):
pass
def vectorNx():
doc = "Nodal grid vector (1D) in the x direction."
fget = lambda self: np.r_[0., self.hx.cumsum()] + self.x0[0]
return locals()
vectorNx = property(**vectorNx())
def vectorNy():
doc = "Nodal grid vector (1D) in the y direction."
fget = lambda self: None if self.dim < 2 else np.r_[0., self.hy.cumsum()] + self.x0[1]
return locals()
vectorNy = property(**vectorNy())
def vectorNz():
doc = "Nodal grid vector (1D) in the z direction."
fget = lambda self: None if self.dim < 3 else np.r_[0., self.hz.cumsum()] + self.x0[2]
return locals()
vectorNz = property(**vectorNz())
def vectorCCx():
doc = "Cell-centered grid vector (1D) in the x direction."
fget = lambda self: np.r_[0, self.hx[:-1].cumsum()] + self.hx*0.5 + self.x0[0]
return locals()
vectorCCx = property(**vectorCCx())
def vectorCCy():
doc = "Cell-centered grid vector (1D) in the y direction."
fget = lambda self: None if self.dim < 2 else np.r_[0, self.hy[:-1].cumsum()] + self.hy*0.5 + self.x0[1]
return locals()
vectorCCy = property(**vectorCCy())
def vectorCCz():
doc = "Cell-centered grid vector (1D) in the z direction."
fget = lambda self: None if self.dim < 3 else np.r_[0, self.hz[:-1].cumsum()] + self.hz*0.5 + self.x0[2]
return locals()
vectorCCz = property(**vectorCCz())
def gridCC():
doc = "Cell-centered grid."
def fget(self):
if self._gridCC is None:
self._gridCC = ndgrid([x for x in [self.vectorCCx, self.vectorCCy, self.vectorCCz] if not x is None])
return self._gridCC
return locals()
_gridCC = None # Store grid by default
gridCC = property(**gridCC())
def gridN():
doc = "Nodal grid."
def fget(self):
if self._gridN is None:
self._gridN = ndgrid([x for x in [self.vectorNx, self.vectorNy, self.vectorNz] if not x is None])
return self._gridN
return locals()
_gridN = None # Store grid by default
gridN = property(**gridN())
def gridFx():
doc = "Face staggered grid in the x direction."
def fget(self):
if self._gridFx is None:
self._gridFx = ndgrid([x for x in [self.vectorNx, self.vectorCCy, self.vectorCCz] if not x is None])
return self._gridFx
return locals()
_gridFx = None # Store grid by default
gridFx = property(**gridFx())
def gridFy():
doc = "Face staggered grid in the y direction."
def fget(self):
if self._gridFy is None:
self._gridFy = ndgrid([x for x in [self.vectorCCx, self.vectorNy, self.vectorCCz] if not x is None])
return self._gridFy
return locals()
_gridFy = None # Store grid by default
gridFy = property(**gridFy())
def gridFz():
doc = "Face staggered grid in the z direction."
def fget(self):
if self._gridFz is None:
self._gridFz = ndgrid([x for x in [self.vectorCCx, self.vectorCCy, self.vectorNz] if not x is None])
return self._gridFz
return locals()
_gridFz = None # Store grid by default
gridFz = property(**gridFz())
def gridEx():
doc = "Edge staggered grid in the x direction."
def fget(self):
if self._gridEx is None:
self._gridEx = ndgrid([x for x in [self.vectorCCx, self.vectorNy, self.vectorNz] if not x is None])
return self._gridEx
return locals()
_gridEx = None # Store grid by default
gridEx = property(**gridEx())
def gridEy():
doc = "Edge staggered grid in the y direction."
def fget(self):
if self._gridEy is None:
self._gridEy = ndgrid([x for x in [self.vectorNx, self.vectorCCy, self.vectorNz] if not x is None])
return self._gridEy
return locals()
_gridEy = None # Store grid by default
gridEy = property(**gridEy())
def gridEz():
doc = "Edge staggered grid in the z direction."
def fget(self):
if self._gridEz is None:
self._gridEz = ndgrid([x for x in [self.vectorNx, self.vectorNy, self.vectorCCz] if not x is None])
return self._gridEz
return locals()
_gridEz = None # Store grid by default
gridEz = property(**gridEz())
def getBoundaryIndex(self, gridType):
"""Needed for faces edges and cells"""
pass
def getCellNumbering(self):
pass
+69 -244
View File
@@ -1,257 +1,82 @@
#-------------------------------------------------------------------------------
# Packages
#-------------------------------------------------------------------------------
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from BaseMesh import BaseMesh
from TensorGrid import TensorGrid
from TensorView import TensorView
#-------------------------------------------------------------------------------
# Class definition
#-------------------------------------------------------------------------------
class TensorMesh:
"""
Define nodal, cell-centered and staggered tensor meshes for 1, 2 and 3
dimensions.
class TensorMesh(BaseMesh, TensorGrid, TensorView):
"""
# ---------------------- Properties --------------------------------------
h = None # Array Spacing or cell-sizes in each direction
x0 = None # Array Origin (x1,x2,x3)
dim = None # Int Dimension
n = None # Array Number of cells in each direction
nC = None # Int Total number of cells
nE = None # Int Total number of edges
nF = None # Int Total number of faces
# ----------------------- Methods ----------------------------------------
def __init__(self,h,x0):
"""Compute number of edges,faces and cell-centers """
# Assign values to properties
self.h = h
self.x0 = x0
#Compute derived properties
self.dim = np.size(x0)
#Compute the num of cells in each direction
self.n = np.zeros((self.dim,1))
for d in range(self.dim):
self.n[d] = np.size(h[d])
# Compute the number of cell-centers
self.nC = np.prod(self.n)
# Compute the number of edges (makes sense only for 3D)
# Equivalent to:
# nEdges = n[0] * (n[1]+1) * (n[2]+1)
# + (n[0]+1) * ny[1] * (nz[2]+1)
# + (n[0]+1) * (ny[1]+1)* (nz[2])
if self.dim == 3:
self.nE = np.prod(np.kron(np.ones((3,1)),self.n.T)+np.ones((3,3))-np.eye(3),1)
print self.nE
# Compute the number of faces (makes sense only for 2 and 3D)
# Equivalent to
# nFaces = (n[0]+1) * n[1] * n[2]
# + n[0] * (ny[1]+1) * nz[2]
# + n[0] * ny[1] * (nz[2]+1)
if self.dim >=2:
self.nF = np.prod(np.kron(np.ones((self.dim,1)),self.n.T)+np.eye(self.dim),1)
print self.nF
TensorMesh is a mesh class that deals with tensor product meshes.
def xin(self,i):
"""Construct the 1D nodal mesh from the ith-component of h. Return an array."""
return np.insert(np.cumsum(self.h[i-1]),0,0.0) + self.x0[i-1]
def xic(self,i):
"""Construct the 1D cell-centerd mesh from the ith-component of h. Return an array."""
return .5*( np.insert(np.cumsum(self.h[i-1][:,0:-1]),0,0.0) + np.cumsum(self.h[i-1]))
Any Mesh that has a constant width along the entire axis
such that it can defined by a single width vector, called 'h'.
e.g.
hx = np.array([1,1,1])
hy = np.array([1,2])
hz = np.array([1,1,1,1])
mesh = TensorMesh([hx, hy, hz])
"""
def __init__(self, h, x0=None):
super(TensorMesh, self).__init__(np.array([len(x) for x in h]), x0)
assert len(h) == len(x0), "Dimension mismatch. x0 != len(h)"
for i, h_i in enumerate(h):
assert type(h_i) == np.ndarray, ("h[%i] is not a numpy array." % i)
# Ensure h contains 1D vectors
self._h = [x.ravel() for x in h]
def h():
doc = "h is a list containing the cell widths of the tensor mesh in each dimension."
fget = lambda self: self._h
return locals()
h = property(**h())
def hx():
doc = "Width of cells in the x direction"
fget = lambda self: self._h[0]
return locals()
hx = property(**hx())
def hy():
doc = "Width of cells in the y direction"
fget = lambda self: None if self.dim < 2 else self._h[1]
return locals()
hy = property(**hy())
def hz():
doc = "Width of cells in the z direction"
fget = lambda self: None if self.dim < 3 else self._h[2]
return locals()
hz = property(**hz())
def getNodalGrid(self):
"""Construct nodal grid for 1, 2 and 3 dimensions"""
if self.dim==1:
return [self.xin(1)]
elif self.dim==2:
return self.ndgrid([self.xin(1),self.xin(2)])
elif self.dim==3:
return self.ndgrid([self.xin(1),self.xin(2),self.xin(3)])
def ndgrid(self, xin):
"""Form tensorial grid for 1, 2 and 3 dimensions. Return X1,X2,X3 arrays depending on the dimension"""
ei = lambda i : np.ones((np.size(xin[i-1]),1))
if self.dim==1:
return [xin]
elif self.dim==2:
X1 = np.kron(ei(2),xin[0]).reshape(-1,1)
X2 = np.kron(xin[1],ei(1).T).reshape(-1,1)
return X1,X2
elif self.dim==3:
X1 = np.kron(ei(3),np.kron(ei(2),xin[0])).reshape(-1,1)
X2 = np.kron(ei(3).T,np.kron(xin[1],ei(1).T)).reshape(-1,1)
X3 = np.kron(xin[2],np.kron(ei(2),ei(1))).T.reshape(-1,1)
return X1,X2,X3
def getCellCenteredGrid(self):
"""Construct cell-centered grid for 1, 2 and 3 dimensions."""
if self.dim==1:
return [self.xic(1)]
elif self.dim==2:
return self.ndgrid([self.xic(1),self.xic(2)])
elif self.dim==3:
return self.ndgrid([self.xic(1),self.xic(2),self.xic(3)])
def getFaceStgGrid(self,direction):
"""Construct the face staggered grids for 2 and 3 dimensions."""
if self.dim==1:
print 'Error: dimension must be larger than 1'
elif self.dim==2:
if direction == 1:
return self.ndgrid([self.xin(1),self.xic(2)])
elif direction == 2:
return self.ndgrid([self.xic(1),self.xin(2)])
else:
print 'Error: direction must be equal to 1 or 2'
elif self.dim==3:
if direction == 1:
return self.ndgrid([self.xin(1),self.xic(2),self.xic(3)])
elif direction == 2:
return self.ndgrid([self.xic(1),self.xin(2),self.xic(3)])
elif direction == 3:
return self.ndgrid([self.xic(1),self.xic(2),self.xin(3)])
else:
print 'Error: direction must be equal to 1, 2 or 3'
def getEdgeStgGrid(self,direction):
"""Construct the edge staggered grids for 3 dimension case."""
if self.dim != 3:
print 'Error: dimension must be equal to 3'
else:
if direction == 1:
return self.ndgrid([self.xic(1),self.xin(2),self.xin(3)])
elif direction == 2:
return self.ndgrid([self.xin(1),self.xic(2),self.xin(3)])
elif direction == 3:
return self.ndgrid([self.xin(1),self.xin(2),self.xic(3)])
else:
print 'Error: direction must be equal to 1, 2 or 3'
def plotImage(self,I):
if self.dim==1:
fig = plt.figure(1)
fig.clf()
ax=plt.subplot(111)
if np.size(I)==self.n[0]:
print 'cell-centered image'
xx = self.getCellCenteredGrid()
ax.plot(xx[0],I,'ro')
elif np.size(I)==self.n[0]+1:
print 'nodal image'
xx = self.getNodalGrid()
ax.plot(xx[0],I,'bs')
fig.show()
def plotGrid(self):
"""Plot the nodal, cell-centered and staggered grids for 1,2 and 3 dimensions."""
if self.dim == 1:
fig = plt.figure(1)
fig.clf()
ax = plt.subplot(111)
xn = self.getNodalGrid()
xc = self.getCellCenteredGrid()
print xn
ax.hold(True)
ax.plot(xn,np.ones(np.shape(xn)),'bs')
ax.plot(xc,np.ones(np.shape(xc)),'ro')
ax.plot(xn,np.ones(np.shape(xn)),'k--')
ax.grid(True)
ax.hold(False)
ax.set_xlabel('x1')
fig.show()
elif self.dim == 2:
fig = plt.figure(2)
fig.clf()
ax = plt.subplot(111)
xn = self.getNodalGrid()
xc = self.getCellCenteredGrid()
xs1 = self.getFaceStgGrid(1)
xs2 = self.getFaceStgGrid(2)
ax.hold(True)
ax.plot(xn[0],xn[1],'bs')
ax.plot(xc[0],xc[1],'ro')
ax.plot(xs1[0],xs1[1],'g>')
ax.plot(xs2[0],xs2[1],'g^')
ax.grid(True)
ax.hold(False)
ax.set_xlabel('x1')
ax.set_ylabel('x2')
fig.show()
elif self.dim == 3:
fig = plt.figure(3)
fig.clf()
ax = fig.add_subplot(111, projection='3d')
xn = self.getNodalGrid()
xc = self.getCellCenteredGrid()
xfs1 = self.getFaceStgGrid(1)
xfs2 = self.getFaceStgGrid(2)
xfs3 = self.getFaceStgGrid(3)
xes1 = self.getEdgeStgGrid(1)
xes2 = self.getEdgeStgGrid(2)
xes3 = self.getEdgeStgGrid(3)
ax.hold(True)
ax.plot(xn[0],xn[1],'bs',zs=xn[2])
ax.plot(xc[0],xc[1],'ro',zs=xc[2])
ax.plot(xfs1[0],xfs1[1],'g>',zs=xfs1[2])
ax.plot(xfs2[0],xfs2[1],'g<',zs=xfs2[2])
ax.plot(xfs3[0],xfs3[1],'g^',zs=xfs3[2])
ax.plot(xes1[0],xes1[1],'k>',zs=xes1[2])
ax.plot(xes2[0],xes2[1],'k<',zs=xes2[2])
ax.plot(xes3[0],xes3[1],'k^',zs=xes3[2])
ax.grid(True)
ax.hold(False)
ax.set_xlabel('x1')
ax.set_ylabel('x2')
ax.set_zlabel('x3')
fig.show()
if __name__ == '__main__':
print('Welcome to tensor mesh!')
testDim = 1
h1 = 0.3*np.ones((1,7))
h1[:,0] = 0.5
h1[:,-1] = 0.6
h2 = .5* np.ones((1,4))
h3 = .4* np.ones((1,6))
x0 = np.zeros((3,1))
h1 = 0.3*np.ones((1, 7))
h1[:, 0] = 0.5
h1[:, -1] = 0.6
h2 = .5 * np.ones((1, 4))
h3 = .4 * np.ones((1, 6))
x0 = np.zeros((3, 1))
if testDim == 1:
h = [h1]
x0 = x0[0]
elif testDim==2:
h = [h1,h2]
x0 = x0[0]
elif testDim == 2:
h = [h1, h2]
x0 = x0[0:2]
else:
h = [h1,h2,h3]
I = np.linspace(0,1,8)
M = TensorMesh(h,x0)
xn = M.plotGrid()
h = [h1, h2, h3]
I = np.linspace(0, 1, 8)
M = TensorMesh(h, x0)
xn = M.plotGrid()
+96
View File
@@ -0,0 +1,96 @@
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
class TensorView(object):
"""
Provides viewing functions for TensorMesh
This class is inherited by TensorMesh
"""
def __init__(self):
pass
def plotImage(self, I):
if self.dim == 1:
fig = plt.figure(1)
fig.clf()
ax = plt.subplot(111)
if np.size(I) == self.n[0]:
print 'cell-centered image'
xx = self.gridCC
ax.plot(xx[0], I, 'ro')
elif np.size(I) == self.n[0]+1:
print 'nodal image'
xx = self.gridN
ax.plot(xx[0], I, 'bs')
fig.show()
def plotGrid(self):
"""Plot the nodal, cell-centered and staggered grids for 1,2 and 3 dimensions."""
if self.dim == 1:
fig = plt.figure(1)
fig.clf()
ax = plt.subplot(111)
xn = self.gridN
xc = self.gridCC
print xn
ax.hold(True)
ax.plot(xn, np.ones(np.shape(xn)), 'bs')
ax.plot(xc, np.ones(np.shape(xc)), 'ro')
ax.plot(xn, np.ones(np.shape(xn)), 'k--')
ax.grid(True)
ax.hold(False)
ax.set_xlabel('x1')
fig.show()
elif self.dim == 2:
fig = plt.figure(2)
fig.clf()
ax = plt.subplot(111)
xn = self.gridN
xc = self.gridCC
xs1 = self.gridFx
xs2 = self.gridFy
ax.hold(True)
ax.plot(xn[:, 0], xn[:, 1], 'bs')
ax.plot(xc[:, 0], xc[:, 1], 'ro')
ax.plot(xs1[:, 0], xs1[:, 1], 'g>')
ax.plot(xs2[:, 0], xs2[:, 1], 'g^')
ax.grid(True)
ax.hold(False)
ax.set_xlabel('x1')
ax.set_ylabel('x2')
fig.show()
elif self.dim == 3:
fig = plt.figure(3)
fig.clf()
ax = fig.add_subplot(111, projection='3d')
xn = self.gridN
xc = self.gridCC
xfs1 = self.gridFx
xfs2 = self.gridFy
xfs3 = self.gridFz
xes1 = self.gridEx
xes2 = self.gridEy
xes3 = self.gridEz
ax.hold(True)
ax.plot(xn[:, 0], xn[:, 1], 'bs', zs=xn[:, 2])
ax.plot(xc[:, 0], xc[:, 1], 'ro', zs=xc[:, 2])
ax.plot(xfs1[:, 0], xfs1[:, 1], 'g>', zs=xfs1[:, 2])
ax.plot(xfs2[:, 0], xfs2[:, 1], 'g<', zs=xfs2[:, 2])
ax.plot(xfs3[:, 0], xfs3[:, 1], 'g^', zs=xfs3[:, 2])
ax.plot(xes1[:, 0], xes1[:, 1], 'k>', zs=xes1[:, 2])
ax.plot(xes2[:, 0], xes2[:, 1], 'k<', zs=xes2[:, 2])
ax.plot(xes3[:, 0], xes3[:, 1], 'k^', zs=xes3[:, 2])
ax.grid(True)
ax.hold(False)
ax.set_xlabel('x1')
ax.set_ylabel('x2')
ax.set_zlabel('x3')
fig.show()
+34
View File
@@ -0,0 +1,34 @@
import numpy as np
import unittest
import sys
sys.path.append('../')
from TensorMesh import TensorMesh
class TestSequenceFunctions(unittest.TestCase):
def setUp(self):
a = np.array([1, 1, 1])
b = np.array([1, 2])
x0 = np.array([3, 5])
self.mesh2 = TensorMesh([a, b], x0)
def test_vectorN_2D(self):
testNx = np.array([3, 4, 5, 6])
testNy = np.array([5, 6, 8])
xtest = np.all(self.mesh2.vectorNx == testNx)
ytest = np.all(self.mesh2.vectorNy == testNy)
self.assertTrue(xtest and ytest)
def test_vectorCC_2D(self):
testNx = np.array([3.5, 4.5, 5.5])
testNy = np.array([5.5, 7])
xtest = np.all(self.mesh2.vectorCCx == testNx)
ytest = np.all(self.mesh2.vectorCCy == testNy)
self.assertTrue(xtest and ytest)
if __name__ == '__main__':
unittest.main()
+53
View File
@@ -0,0 +1,53 @@
import numpy as np
import unittest
import sys
sys.path.append('../')
from utils import mkvc, ndgrid
class TestSequenceFunctions(unittest.TestCase):
def setUp(self):
self.a = np.array([1, 2, 3])
self.b = np.array([1, 2])
self.c = np.array([1, 2, 3, 4])
def test_mkvc1(self):
x = mkvc(self.a)
self.assertTrue(x.shape, (3,))
def test_mkvc2(self):
x = mkvc(self.a, 2)
self.assertTrue(x.shape, (3, 1))
def test_mkvc3(self):
x = mkvc(self.a, 3)
self.assertTrue(x.shape, (3, 1, 1))
def test_ndgrid_2D(self):
XY = ndgrid([self.a, self.b])
X1_test = np.array([1, 2, 3, 1, 2, 3])
X2_test = np.array([1, 1, 1, 2, 2, 2])
xtest = np.all(XY[:, 0] == X1_test)
ytest = np.all(XY[:, 1] == X2_test)
self.assertTrue(xtest and ytest)
def test_ndgrid_3D(self):
XYZ = ndgrid([self.a, self.b, self.c])
X1_test = np.array([1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3])
X2_test = np.array([1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2])
X3_test = np.array([1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4])
xtest = np.all(XYZ[:, 0] == X1_test)
ytest = np.all(XYZ[:, 1] == X2_test)
ztest = np.all(XYZ[:, 2] == X3_test)
self.assertTrue(xtest and ytest and ztest)
if __name__ == '__main__':
unittest.main()
+43 -21
View File
@@ -1,4 +1,5 @@
from numpy import *
import numpy as np
def diff(A, d):
@@ -43,35 +44,56 @@ def ave(A, d):
print('d must be 1,2 or 3')
def reshapeF(sp, d):
return reshape(sp, d, 'F')
def reshapeF(x, size):
return np.reshape(x, size, order='F')
def mkvc(A):
return reshape(A, [size(A), 1], 'F').flatten()
def mkvc(x, numDims=1):
"""Creates a vector with the number of dimension specified
e.g.:
a = np.array(1,2,3)
mkvc(a, 1).shape
> (3, )
mkvc(a, 2).shape
> (3, 1)
mkvc(a, 3).shape
> (3, 1, 1)
"""
assert type(x) == np.ndarray, "Vector must be a numpy array"
if numDims == 1:
return x.flatten(order='F')
elif numDims == 2:
return x.flatten(order='F')[:, np.newaxis]
elif numDims == 3:
return x.flatten(order='F')[:, np.newaxis, np.newaxis]
def ndgrid(x, y, z):
def ndgrid(xin):
"""Form tensorial grid for 1, 2 and 3 dimensions. Return X1,X2,X3 arrays depending on the dimension"""
n1 = size(x)
n2 = size(y)
n3 = size(z)
X = zeros([n1, n2, n3])
Y = zeros([n1, n2, n3])
Z = zeros([n1, n2, n3])
for i in range(0, n2):
for j in range(0, n3):
X[:, i, j] = x
if len(xin) == 1:
return xin
elif len(xin) == 2:
X2, X1 = [mkvc(x) for x in np.broadcast_arrays(mkvc(xin[1], 1), mkvc(xin[0], 2))]
return np.c_[X1, X2]
elif len(xin) == 3:
X3, X2, X1 = [mkvc(x) for x in np.broadcast_arrays(mkvc(xin[2], 1), mkvc(xin[1], 2), mkvc(xin[0], 3))]
return np.c_[X1, X2, X3]
for i in range(0, n1):
for j in range(0, n3):
Y[i, :, j] = y
for i in range(0, n1):
for j in range(0, n2):
Z[i, j, :] = z
def flattenF(x):
return np.flatten(x, order='F')
return (X, Y, Z)
def printF(x):
pass
def ind2sub(shape, ind):