Merged Seogi's code into the Tensor Mesh code base.

This is under the DiffOperators class, which can be inherited with BaseMesh to any Mesh Object.

Cell area and other dimension calculations are included in the TensorMesh class.

Wrote unit tests for cell vol, area, and edges.
This commit is contained in:
Rowan Cockett
2013-07-18 10:41:58 -07:00
parent 3680295e54
commit ef557beb84
8 changed files with 289 additions and 234 deletions
+9 -10
View File
@@ -3,7 +3,6 @@ import numpy as np
import sys
sys.path.append('../')
from TensorMesh import TensorMesh
from getDiffop import getCurlMatrix
err=0.
@@ -11,7 +10,7 @@ print '>> Test Curl operator'
for i in range(4):
icount=i+1
nc = 2**icount
# Define the mesh
# Define the mesh
h1 = np.ones((1,nc))/nc
h2 = np.ones((1,nc))/nc
h3 = np.ones((1,nc))/nc
@@ -21,28 +20,28 @@ for i in range(4):
#n = M.plotGrid()
# Generate DIV matrix
CURL = getCurlMatrix(h)
CURL = M.CURL
#Test function
fun = lambda x: np.cos(x) # i (cos(y)) + j (cos(z)) + k (cos(x))
sol = lambda x: np.sin(x) # i (sin(z)) + j (sin(x)) + k (sin(y))
Ex = fun(M.gridEx[:,1])
Ey = fun(M.gridEy[:,2])
Ez = fun(M.gridEz[:,0])
E = np.concatenate((Ex,Ey,Ez))
E = np.concatenate((Ex,Ey,Ez))
Fx = sol(M.gridFx[:,2])
Fy = sol(M.gridFy[:,0])
Fz = sol(M.gridFz[:,1])
curlE_anal = np.concatenate((Fx,Fy,Fz))
curlE_anal = np.concatenate((Fx,Fy,Fz))
curlE = CURL*E
curlE = CURL*E
err = np.linalg.norm((curlE-curlE_anal), np.inf)
if icount == 1:
print 'h | inf norm | error ratio'
print '---------------------------------------'
print 'h | inf norm | error ratio'
print '---------------------------------------'
print '%6.4f | %8.2e |'% (h1[0,0], err)
else:
print '%6.4f | %8.2e | %6.4f' % (h1[0,0], err, err_old/err)
err_old = err
err_old = err
+8 -11
View File
@@ -3,7 +3,6 @@ import numpy as np
import sys
sys.path.append('../')
from TensorMesh import TensorMesh
from getDIV import getDivMatrix, getarea, getvol
err=0.
@@ -11,7 +10,7 @@ print '>> Test face Divergence operator'
for i in range(4):
icount=i+1
nc = 2**icount
# Define the mesh
# Define the mesh
h1 = np.ones((1,nc))/nc
h2 = np.ones((1,nc))/nc
h3 = np.ones((1,nc))/nc
@@ -21,28 +20,26 @@ for i in range(4):
#n = M.plotGrid()
# Generate DIV matrix
DIV = getDivMatrix(h)
DIV = M.DIV
#Test function
fun = lambda x: np.sin(x)
Fx = fun(M.gridFx[:,0])
Fy = fun(M.gridFy[:,1])
Fz = fun(M.gridFz[:,2])
F = np.concatenate((Fx,Fy,Fz))
divF = DIV*F
sol = lambda x, y, z: (np.cos(x)+np.cos(y)+np.cos(z))
divF_anal = sol(M.gridCC[:,0], M.gridCC[:,1], M.gridCC[:,2])
area = getarea(h)
vol = getvol(h)
#err = np.linalg.norm((divF-divF_anal)*np.sqrt(vol), 2)
err = np.linalg.norm((divF-divF_anal), np.inf)
if icount == 1:
print 'h | inf norm | error ratio'
print '---------------------------------------'
print 'h | inf norm | error ratio'
print '---------------------------------------'
print '%6.4f | %8.2e |'% (h1[0,0], err)
else:
print '%6.4f | %8.2e | %6.4f' % (h1[0,0], err, err_old/err)
err_old = err
err_old = err
+10 -11
View File
@@ -3,15 +3,14 @@ import numpy as np
import sys
sys.path.append('../')
from TensorMesh import TensorMesh
from getDiffop import getGradMatrix
err=0.
print '>> Test nodal Gradient operator'
print '>> Test nodal Gradient operator'
for i in range(4):
icount=i+1
nc = 2**icount
# Define the mesh
# Define the mesh
h1 = np.ones((1,nc))/nc
h2 = np.ones((1,nc))/nc
h3 = np.ones((1,nc))/nc
@@ -21,11 +20,11 @@ for i in range(4):
#n = M.plotGrid()
# Generate DIV matrix
GRAD = getGradMatrix(h)
GRAD = M.GRAD
#Test function
fun = lambda x, y, z: (np.cos(x)+np.cos(y)+np.cos(z))
fun = lambda x, y, z: (np.cos(x)+np.cos(y)+np.cos(z))
sol = lambda x: -np.sin(x) # i (sin(x)) + j (sin(y)) + k (sin(z))
phi = fun(M.gridN[:,0], M.gridN[:,1], M.gridN[:,2])
gradE = GRAD*phi
@@ -33,14 +32,14 @@ for i in range(4):
Ey = sol(M.gridEy[:,1])
Ez = sol(M.gridEz[:,2])
gradE_anal = np.concatenate((Ex,Ey,Ez))
gradE_anal = np.concatenate((Ex,Ey,Ez))
err = np.linalg.norm((gradE-gradE_anal), np.inf)
if icount == 1:
print 'h | inf norm | error ratio'
print '---------------------------------------'
print 'h | inf norm | error ratio'
print '---------------------------------------'
print '%6.4f | %8.2e |'% (h1[0,0], err)
else:
print '%6.4f | %8.2e | %6.4f' % (h1[0,0], err, err_old/err)
err_old = err
err_old = err
+28 -2
View File
@@ -10,8 +10,9 @@ 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)
c = np.array([1, 4])
self.mesh2 = TensorMesh([a, b], np.array([3, 5]))
self.mesh3 = TensorMesh([a, b, c])
def test_vectorN_2D(self):
testNx = np.array([3, 4, 5, 6])
@@ -29,6 +30,31 @@ class TestSequenceFunctions(unittest.TestCase):
ytest = np.all(self.mesh2.vectorCCy == testNy)
self.assertTrue(xtest and ytest)
def test_area_3D(self):
test_area = np.array([1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 8, 8, 8, 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2])
t1 = np.all(self.mesh3.area == test_area)
self.assertTrue(t1)
def test_vol_3D(self):
test_vol = np.array([1, 1, 1, 2, 2, 2, 4, 4, 4, 8, 8, 8])
t1 = np.all(self.mesh3.vol == test_vol)
self.assertTrue(t1)
def test_vol_2D(self):
test_vol = np.array([1, 1, 1, 2, 2, 2])
t1 = np.all(self.mesh2.vol == test_vol)
self.assertTrue(t1)
def test_edge_3D(self):
test_edge = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4])
t1 = np.all(self.mesh3.edge == test_edge)
self.assertTrue(t1)
def test_edge_2D(self):
test_edge = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2])
t1 = np.all(self.mesh2.edge == test_edge)
self.assertTrue(t1)
if __name__ == '__main__':
unittest.main()