mirror of
https://github.com/wassname/simpeg.git
synced 2026-09-09 11:34:26 +08:00
Testing differential operators (Div, Grad, Curl)
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import numpy as np
|
||||
|
||||
import sys
|
||||
sys.path.append('../')
|
||||
from TensorMesh import TensorMesh
|
||||
from getDiffop import getCurlMatrix
|
||||
|
||||
|
||||
err=0.
|
||||
print '>> Test Curl operator'
|
||||
for i in range(4):
|
||||
icount=i+1
|
||||
nc = 2**icount
|
||||
# Define the mesh
|
||||
h1 = np.ones((1,nc))/nc
|
||||
h2 = np.ones((1,nc))/nc
|
||||
h3 = np.ones((1,nc))/nc
|
||||
h = [h1, h2, h3]
|
||||
x0 = np.zeros((3, 1))
|
||||
M = TensorMesh(h, x0)
|
||||
#n = M.plotGrid()
|
||||
|
||||
# Generate DIV matrix
|
||||
CURL = getCurlMatrix(h)
|
||||
#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))
|
||||
|
||||
Fx = sol(M.gridFx[:,2])
|
||||
Fy = sol(M.gridFy[:,0])
|
||||
Fz = sol(M.gridFz[:,1])
|
||||
curlE_anal = np.concatenate((Fx,Fy,Fz))
|
||||
|
||||
curlE = CURL*E
|
||||
err = np.linalg.norm((curlE-curlE_anal), np.inf)
|
||||
|
||||
if icount == 1:
|
||||
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
|
||||
+15
-12
@@ -5,16 +5,18 @@ sys.path.append('../')
|
||||
from TensorMesh import TensorMesh
|
||||
from getDIV import getDivMatrix, getarea, getvol
|
||||
|
||||
# Define the mesh
|
||||
|
||||
err=0.
|
||||
print '>> Test face Divergence operator'
|
||||
for i in range(4):
|
||||
icount=i+1;
|
||||
nc = 2*icount;
|
||||
h1 = np.pi/nc*np.ones((1,nc))
|
||||
h2 = np.pi/nc*np.ones((1,nc))
|
||||
h3 = np.pi/nc*np.ones((1,nc))
|
||||
icount=i+1
|
||||
nc = 2**icount
|
||||
# Define the mesh
|
||||
h1 = np.ones((1,nc))/nc
|
||||
h2 = np.ones((1,nc))/nc
|
||||
h3 = np.ones((1,nc))/nc
|
||||
h = [h1, h2, h3]
|
||||
x0 = -np.pi/2*np.ones((3, 1))
|
||||
x0 = np.zeros((3, 1))
|
||||
M = TensorMesh(h, x0)
|
||||
#n = M.plotGrid()
|
||||
|
||||
@@ -34,12 +36,13 @@ for i in range(4):
|
||||
|
||||
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.sqrt(vol), 2)
|
||||
err = np.linalg.norm((divF-divF_anal), np.inf)
|
||||
|
||||
if icount == 1:
|
||||
err1 = err
|
||||
print 'h | 2 norm | error ratio'
|
||||
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, err1/err)
|
||||
|
||||
print '%6.4f | %8.2e | %6.4f' % (h1[0,0], err, err_old/err)
|
||||
err_old = err
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
import numpy as np
|
||||
|
||||
import sys
|
||||
sys.path.append('../')
|
||||
from TensorMesh import TensorMesh
|
||||
from getDiffop import getGradMatrix
|
||||
|
||||
|
||||
err=0.
|
||||
print '>> Test nodal Gradient operator'
|
||||
for i in range(4):
|
||||
icount=i+1
|
||||
nc = 2**icount
|
||||
# Define the mesh
|
||||
h1 = np.ones((1,nc))/nc
|
||||
h2 = np.ones((1,nc))/nc
|
||||
h3 = np.ones((1,nc))/nc
|
||||
h = [h1, h2, h3]
|
||||
x0 = np.zeros((3, 1))
|
||||
M = TensorMesh(h, x0)
|
||||
#n = M.plotGrid()
|
||||
|
||||
# Generate DIV matrix
|
||||
GRAD = getGradMatrix(h)
|
||||
#Test function
|
||||
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
|
||||
|
||||
Ex = sol(M.gridEx[:,0])
|
||||
Ey = sol(M.gridEy[:,1])
|
||||
Ez = sol(M.gridEz[:,2])
|
||||
|
||||
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 '%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
|
||||
|
||||
Reference in New Issue
Block a user