edgeCurl is O(h) not O(h^2) ????

This commit is contained in:
Rowan Cockett
2015-11-09 19:51:46 -08:00
parent 94a79298bd
commit da7fbbb461
4 changed files with 397 additions and 96 deletions
+103
View File
@@ -0,0 +1,103 @@
import numpy as np
import unittest
from SimPEG.Tests import OrderTest
import matplotlib.pyplot as plt
#TODO: 'randomTensorMesh'
MESHTYPES = ['uniformTree'] #['randomTree', 'uniformTree']
call2 = lambda fun, xyz: fun(xyz[:, 0], xyz[:, 1])
call3 = lambda fun, xyz: fun(xyz[:, 0], xyz[:, 1], xyz[:, 2])
cart_row2 = lambda g, xfun, yfun: np.c_[call2(xfun, g), call2(yfun, g)]
cart_row3 = lambda g, xfun, yfun, zfun: np.c_[call3(xfun, g), call3(yfun, g), call3(zfun, g)]
cartF2 = lambda M, fx, fy: np.vstack((cart_row2(M.gridFx, fx, fy), cart_row2(M.gridFy, fx, fy)))
cartE2 = lambda M, ex, ey: np.vstack((cart_row2(M.gridEx, ex, ey), cart_row2(M.gridEy, ex, ey)))
cartF3 = lambda M, fx, fy, fz: np.vstack((cart_row3(M.gridFx, fx, fy, fz), cart_row3(M.gridFy, fx, fy, fz), cart_row3(M.gridFz, fx, fy, fz)))
cartE3 = lambda M, ex, ey, ez: np.vstack((cart_row3(M.gridEx, ex, ey, ez), cart_row3(M.gridEy, ex, ey, ez), cart_row3(M.gridEz, ex, ey, ez)))
class TestFaceDiv2D(OrderTest):
name = "Face Divergence 2D"
meshTypes = MESHTYPES
meshDimension = 2
meshSizes = [16, 32]
def getError(self):
#Test function
fx = lambda x, y: np.sin(2*np.pi*x)
fy = lambda x, y: np.sin(2*np.pi*y)
sol = lambda x, y: 2*np.pi*(np.cos(2*np.pi*x)+np.cos(2*np.pi*y))
Fc = cartF2(self.M, fx, fy)
F = self.M.projectFaceVector(Fc)
divF = self.M.faceDiv.dot(F)
divF_ana = call2(sol, self.M.gridCC)
err = np.linalg.norm((divF-divF_ana), np.inf)
# self.M.plotImage(divF-divF_ana, showIt=True)
return err
def test_order(self):
self.orderTest()
class TestFaceDiv3D(OrderTest):
name = "Face Divergence 3D"
meshTypes = MESHTYPES
meshSizes = [8, 16]
def getError(self):
#Test function
fx = lambda x, y, z: np.sin(2*np.pi*x)
fy = lambda x, y, z: np.sin(2*np.pi*y)
fz = lambda x, y, z: np.sin(2*np.pi*z)
sol = lambda x, y, z: (2*np.pi*np.cos(2*np.pi*x)+2*np.pi*np.cos(2*np.pi*y)+2*np.pi*np.cos(2*np.pi*z))
Fc = cartF3(self.M, fx, fy, fz)
F = self.M.projectFaceVector(Fc)
divF = self.M.faceDiv.dot(F)
divF_ana = call3(sol, self.M.gridCC)
return np.linalg.norm((divF-divF_ana), np.inf)
def test_order(self):
self.orderTest()
class TestCurl(OrderTest):
name = "Curl"
meshTypes = MESHTYPES
meshSizes = [4, 8, 16, 32]
def getError(self):
# fun: i (cos(y)) + j (cos(z)) + k (cos(x))
# sol: i (sin(z)) + j (sin(x)) + k (sin(y))
funX = lambda x, y, z: np.cos(2*np.pi*y)
funY = lambda x, y, z: np.cos(2*np.pi*z)
funZ = lambda x, y, z: np.cos(2*np.pi*x)
solX = lambda x, y, z: 2*np.pi*np.sin(2*np.pi*z)
solY = lambda x, y, z: 2*np.pi*np.sin(2*np.pi*x)
solZ = lambda x, y, z: 2*np.pi*np.sin(2*np.pi*y)
Ec = cartE3(self.M, funX, funY, funZ)
E = self.M.projectEdgeVector(Ec)
Fc = cartF3(self.M, solX, solY, solZ)
curlE_ana = self.M.projectFaceVector(Fc)
curlE = self.M.edgeCurl.dot(E)
err = np.linalg.norm((curlE - curlE_ana), np.inf)
return err
def test_order(self):
self.orderTest()
if __name__ == '__main__':
unittest.main()
+52 -9
View File
@@ -11,16 +11,21 @@ TOL = 1e-10
class TestSimpleQuadTree(unittest.TestCase):
def test_counts(self):
M = Tree([8,8])
nc = 8
h1 = np.random.rand(nc)*nc*0.5 + nc*0.5
h2 = np.random.rand(nc)*nc*0.5 + nc*0.5
h = [hi/np.sum(hi) for hi in [h1, h2]] # normalize
M = Tree(h)
M._refineCell([0,0,0])
M._refineCell([0,0,1])
M.number()
# M.plotGrid(showIt=True)
# assert sorted(M._cells) == [2, 34, 66, 99, 107, 115, 123, 129, 257, 386, 418, 450, 482]
assert M.nhFx == 2
assert M.nFx == 9
assert M.vol.sum() == 1.0
assert np.allclose(M.vol.sum(), 1.0)
assert np.allclose(np.r_[M._areaFxFull, M._areaFyFull], M._deflationMatrix('F') * M.area)
# def test_connectivity(self):
@@ -48,10 +53,7 @@ class TestSimpleQuadTree(unittest.TestCase):
# assert T._getNextCell([0,2,2], direction=1) == T._index([0,4,1])
# assert T._getNextCell([0,4,1], direction=1, positive=False) == [T._index([0,2,2]), [T._index([2,3,3]), T._index([3,3,3])]]
class TestOperatorsQuadTree(unittest.TestCase):
def test_counts(self):
def test_faceDiv(self):
hx, hy = np.r_[1.,2,3,4], np.r_[5.,6,7,8]
T = Tree([hx, hy], levels=2)
@@ -76,7 +78,31 @@ class TestOperatorsQuadTree(unittest.TestCase):
assert (M.faceDiv - T.permuteCC*T.faceDiv*T.permuteF.T).nnz == 0
class TestOperatorsOcTree(unittest.TestCase):
class TestOcTree(unittest.TestCase):
def test_counts(self):
nc = 8
h1 = np.random.rand(nc)*nc*0.5 + nc*0.5
h2 = np.random.rand(nc)*nc*0.5 + nc*0.5
h3 = np.random.rand(nc)*nc*0.5 + nc*0.5
h = [hi/np.sum(hi) for hi in [h1, h2, h3]] # normalize
M = Tree(h, levels=3)
M._refineCell([0,0,0,0])
M._refineCell([0,0,0,1])
M.number()
# M.plotGrid(showIt=True)
# assert M.nhFx == 2
# assert M.nFx == 9
assert np.allclose(M.vol.sum(), 1.0)
# assert np.allclose(M._areaFxFull, (M._deflationMatrix('F') * M.area)[:M.ntFx])
# assert np.allclose(M._areaFyFull, (M._deflationMatrix('F') * M.area)[M.ntFx:(M.ntFx+M.ntFy)])
# assert np.allclose(M._areaFzFull, (M._deflationMatrix('F') * M.area)[(M.ntFx+M.ntFy):])
# assert np.allclose(M._edgeExFull, (M._deflationMatrix('E') * M.edge)[:M.ntEx])
# assert np.allclose(M._edgeEyFull, (M._deflationMatrix('E') * M.edge)[M.ntEx:(M.ntEx+M.ntEy)])
# assert np.allclose(M._edgeEzFull, (M._deflationMatrix('E') * M.edge)[(M.ntEx+M.ntEy):])
def test_faceDiv(self):
@@ -136,6 +162,23 @@ class TestOperatorsOcTree(unittest.TestCase):
assert np.allclose(Mr.getFaceInnerProduct().todense(), (M.permuteF * M.getFaceInnerProduct() * M.permuteF.T).todense())
assert np.allclose(Mr.getEdgeInnerProduct().todense(), (M.permuteE * M.getEdgeInnerProduct() * M.permuteE.T).todense())
def test_VectorIdenties(self):
hx, hy, hz = [[(1,4)], [(1,4)], [(1,4)]]
M = Tree([hx, hy, hz], levels=2)
Mr = Mesh.TensorMesh([hx, hy, hz])
assert (M.faceDiv * M.edgeCurl).nnz == 0
assert (Mr.faceDiv * Mr.edgeCurl).nnz == 0
hx, hy, hz = np.r_[1.,2,3,4], np.r_[5.,6,7,8], np.r_[9.,10,11,12]
M = Tree([hx, hy, hz], levels=2)
Mr = Mesh.TensorMesh([hx, hy, hz])
assert np.max(np.abs((M.faceDiv * M.edgeCurl).todense().flatten())) < TOL
assert np.max(np.abs((Mr.faceDiv * Mr.edgeCurl).todense().flatten())) < TOL
if __name__ == '__main__':