mirror of
https://github.com/wassname/simpeg.git
synced 2026-08-06 13:30:16 +08:00
Merge branch 'dev' into docs
This commit is contained in:
+32
-27
@@ -5,8 +5,8 @@ from scipy.sparse.linalg import dsolve
|
||||
|
||||
TOL = 1e-14
|
||||
|
||||
MAPS_TO_TEST_2D = ["CircleMap", "ComplexMap", "ExpMap", "IdentityMap", "Vertical1DMap", "Weighting", "FullMap"]
|
||||
MAPS_TO_TEST_3D = [ "ComplexMap", "ExpMap", "IdentityMap", "Vertical1DMap", "Weighting", "FullMap"]
|
||||
MAPS_TO_TEST_2D = ["CircleMap", "ComplexMap", "ExpMap", "IdentityMap", "SurjectVertical1D", "Weighting", "SurjectFull","FullMap","Vertical1DMap"]
|
||||
MAPS_TO_TEST_3D = [ "ComplexMap", "ExpMap", "IdentityMap", "SurjectVertical1D", "Weighting", "SurjectFull","FullMap","Vertical1DMap"]
|
||||
|
||||
class MapTests(unittest.TestCase):
|
||||
|
||||
@@ -52,7 +52,7 @@ class MapTests(unittest.TestCase):
|
||||
def test_mapMultiplication(self):
|
||||
M = Mesh.TensorMesh([2,3])
|
||||
expMap = Maps.ExpMap(M)
|
||||
vertMap = Maps.Vertical1DMap(M)
|
||||
vertMap = Maps.SurjectVertical1D(M)
|
||||
combo = expMap*vertMap
|
||||
m = np.arange(3.0)
|
||||
t_true = np.exp(np.r_[0,0,1,1,2,2.])
|
||||
@@ -83,22 +83,23 @@ class MapTests(unittest.TestCase):
|
||||
def test_activeCells(self):
|
||||
M = Mesh.TensorMesh([2,4],'0C')
|
||||
expMap = Maps.ExpMap(M)
|
||||
actMap = Maps.ActiveCells(M, M.vectorCCy <=0, 10, nC=M.nCy)
|
||||
vertMap = Maps.Vertical1DMap(M)
|
||||
combo = vertMap * actMap
|
||||
m = np.r_[1,2.]
|
||||
mod = Models.Model(m,combo)
|
||||
# import matplotlib.pyplot as plt
|
||||
# plt.colorbar(M.plotImage(mod.transform)[0])
|
||||
# plt.show()
|
||||
self.assertLess(np.linalg.norm(mod.transform - np.r_[1,1,2,2,10,10,10,10.]), TOL)
|
||||
self.assertLess((mod.transformDeriv - combo.deriv(m)).toarray().sum(), TOL)
|
||||
for actMap in [Maps.InjectActiveCells(M, M.vectorCCy <=0, 10, nC=M.nCy), Maps.ActiveCells(M, M.vectorCCy <=0, 10, nC=M.nCy)]:
|
||||
# actMap = Maps.InjectActiveCells(M, M.vectorCCy <=0, 10, nC=M.nCy)
|
||||
vertMap = Maps.SurjectVertical1D(M)
|
||||
combo = vertMap * actMap
|
||||
m = np.r_[1,2.]
|
||||
mod = Models.Model(m,combo)
|
||||
# import matplotlib.pyplot as plt
|
||||
# plt.colorbar(M.plotImage(mod.transform)[0])
|
||||
# plt.show()
|
||||
self.assertLess(np.linalg.norm(mod.transform - np.r_[1,1,2,2,10,10,10,10.]), TOL)
|
||||
self.assertLess((mod.transformDeriv - combo.deriv(m)).toarray().sum(), TOL)
|
||||
|
||||
def test_tripleMultiply(self):
|
||||
M = Mesh.TensorMesh([2,4],'0C')
|
||||
expMap = Maps.ExpMap(M)
|
||||
vertMap = Maps.Vertical1DMap(M)
|
||||
actMap = Maps.ActiveCells(M, M.vectorCCy <=0, 10, nC=M.nCy)
|
||||
vertMap = Maps.SurjectVertical1D(M)
|
||||
actMap = Maps.InjectActiveCells(M, M.vectorCCy <=0, 10, nC=M.nCy)
|
||||
m = np.r_[1,2.]
|
||||
t_true = np.exp(np.r_[1,1,2,2,10,10,10,10.])
|
||||
self.assertLess(np.linalg.norm((expMap * vertMap * actMap * m)-t_true,np.inf),TOL)
|
||||
@@ -115,29 +116,33 @@ class MapTests(unittest.TestCase):
|
||||
M2 = Mesh.TensorMesh([2,4])
|
||||
M3 = Mesh.TensorMesh([3,2,4])
|
||||
m = np.random.rand(M2.nC)
|
||||
m2to3 = Maps.Map2Dto3D(M3, normal='X')
|
||||
m = np.arange(m2to3.nP)
|
||||
self.assertTrue(m2to3.test())
|
||||
self.assertTrue(np.all(Utils.mkvc( (m2to3 * m).reshape(M3.vnC,order='F')[0,:,:] ) == m))
|
||||
|
||||
for m2to3 in [Maps.Surject2Dto3D(M3, normal='X'), Maps.Map2Dto3D(M3, normal='X')]:
|
||||
# m2to3 = Maps.Surject2Dto3D(M3, normal='X')
|
||||
m = np.arange(m2to3.nP)
|
||||
self.assertTrue(m2to3.test())
|
||||
self.assertTrue(np.all(Utils.mkvc( (m2to3 * m).reshape(M3.vnC,order='F')[0,:,:] ) == m))
|
||||
|
||||
|
||||
def test_map2Dto3D_y(self):
|
||||
M2 = Mesh.TensorMesh([3,4])
|
||||
M3 = Mesh.TensorMesh([3,2,4])
|
||||
m = np.random.rand(M2.nC)
|
||||
m2to3 = Maps.Map2Dto3D(M3, normal='Y')
|
||||
m = np.arange(m2to3.nP)
|
||||
self.assertTrue(m2to3.test())
|
||||
self.assertTrue(np.all(Utils.mkvc( (m2to3 * m).reshape(M3.vnC,order='F')[:,0,:] ) == m))
|
||||
for m2to3 in [Maps.Surject2Dto3D(M3, normal='Y'),Maps.Map2Dto3D(M3, normal='Y')]:
|
||||
# m2to3 = Maps.Surject2Dto3D(M3, normal='Y')
|
||||
m = np.arange(m2to3.nP)
|
||||
self.assertTrue(m2to3.test())
|
||||
self.assertTrue(np.all(Utils.mkvc( (m2to3 * m).reshape(M3.vnC,order='F')[:,0,:] ) == m))
|
||||
|
||||
def test_map2Dto3D_z(self):
|
||||
M2 = Mesh.TensorMesh([3,2])
|
||||
M3 = Mesh.TensorMesh([3,2,4])
|
||||
m = np.random.rand(M2.nC)
|
||||
m2to3 = Maps.Map2Dto3D(M3, normal='Z')
|
||||
m = np.arange(m2to3.nP)
|
||||
self.assertTrue(m2to3.test())
|
||||
self.assertTrue(np.all(Utils.mkvc( (m2to3 * m).reshape(M3.vnC,order='F')[:,:,0] ) == m))
|
||||
for m2to3 in [Maps.Surject2Dto3D(M3, normal='Z'),Maps.Map2Dto3D(M3, normal='Z')]:
|
||||
# m2to3 = Maps.Surject2Dto3D(M3, normal='Z')
|
||||
m = np.arange(m2to3.nP)
|
||||
self.assertTrue(m2to3.test())
|
||||
self.assertTrue(np.all(Utils.mkvc( (m2to3 * m).reshape(M3.vnC,order='F')[:,:,0] ) == m))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import os
|
||||
import glob
|
||||
import unittest
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_file_strings = glob.glob('test_*.py')
|
||||
module_strings = [str[0:len(str)-3] for str in test_file_strings]
|
||||
suites = [unittest.defaultTestLoader.loadTestsFromName(str) for str
|
||||
in module_strings]
|
||||
testSuite = unittest.TestSuite(suites)
|
||||
|
||||
unittest.TextTestRunner(verbosity=2).run(testSuite)
|
||||
@@ -0,0 +1,77 @@
|
||||
import unittest
|
||||
from SimPEG import *
|
||||
import SimPEG.DCIP as DC
|
||||
|
||||
|
||||
class DCProblemTests(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
|
||||
aSpacing=2.5
|
||||
nElecs=10
|
||||
|
||||
surveySize = nElecs*aSpacing - aSpacing
|
||||
cs = surveySize/nElecs/4
|
||||
|
||||
mesh = Mesh.TensorMesh([
|
||||
[(cs,10, -1.3),(cs,surveySize/cs),(cs,10, 1.3)],
|
||||
[(cs,3, -1.3),(cs,3,1.3)],
|
||||
# [(cs,5, -1.3),(cs,10)]
|
||||
],'CN')
|
||||
|
||||
srcList = DC.Utils.WennerSrcList(nElecs, aSpacing, in2D=True)
|
||||
survey = DC.SurveyDC(srcList)
|
||||
problem = DC.ProblemDC_CC(mesh)
|
||||
problem.pair(survey)
|
||||
|
||||
mSynth = np.ones(mesh.nC)
|
||||
survey.makeSyntheticData(mSynth)
|
||||
|
||||
# Now set up the problem to do some minimization
|
||||
dmis = DataMisfit.l2_DataMisfit(survey)
|
||||
reg = Regularization.Tikhonov(mesh)
|
||||
opt = Optimization.InexactGaussNewton(maxIterLS=20, maxIter=10, tolF=1e-6, tolX=1e-6, tolG=1e-6, maxIterCG=6)
|
||||
invProb = InvProblem.BaseInvProblem(dmis, reg, opt, beta=1e4)
|
||||
inv = Inversion.BaseInversion(invProb)
|
||||
|
||||
self.inv = inv
|
||||
self.reg = reg
|
||||
self.p = problem
|
||||
self.mesh = mesh
|
||||
self.m0 = mSynth
|
||||
self.survey = survey
|
||||
self.dmis = dmis
|
||||
|
||||
def test_misfit(self):
|
||||
derChk = lambda m: [self.survey.dpred(m), lambda mx: self.p.Jvec(self.m0, mx)]
|
||||
passed = Tests.checkDerivative(derChk, self.m0, plotIt=False)
|
||||
self.assertTrue(passed)
|
||||
|
||||
def test_adjoint(self):
|
||||
# Adjoint Test
|
||||
u = np.random.rand(self.mesh.nC*self.survey.nSrc)
|
||||
v = np.random.rand(self.mesh.nC)
|
||||
w = np.random.rand(self.survey.dobs.shape[0])
|
||||
wtJv = w.dot(self.p.Jvec(self.m0, v))
|
||||
vtJtw = v.dot(self.p.Jtvec(self.m0, w))
|
||||
passed = np.abs(wtJv - vtJtw) < 1e-10
|
||||
print 'Adjoint Test', np.abs(wtJv - vtJtw), passed
|
||||
self.assertTrue(passed)
|
||||
|
||||
def test_dataObj(self):
|
||||
derChk = lambda m: [self.dmis.eval(m), self.dmis.evalDeriv(m)]
|
||||
passed = Tests.checkDerivative(derChk, self.m0, plotIt=False)
|
||||
self.assertTrue(passed)
|
||||
|
||||
|
||||
def test_massMatrices(self):
|
||||
Gu = np.random.rand(self.mesh.nF)
|
||||
def derChk(m):
|
||||
self.p.curModel = m
|
||||
return [self.p.Msig * Gu, self.p.dMdsig(Gu)]
|
||||
passed = Tests.checkDerivative(derChk, self.m0, plotIt=False)
|
||||
self.assertTrue(passed)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -0,0 +1,65 @@
|
||||
import unittest
|
||||
import SimPEG.DCIP as DC
|
||||
from SimPEG import *
|
||||
|
||||
class IPforwardTests(unittest.TestCase):
|
||||
|
||||
def test_IPforward(self):
|
||||
|
||||
cs = 12.5
|
||||
nc = 200/cs+1
|
||||
hx = [(cs,7, -1.3),(cs,nc),(cs,7, 1.3)]
|
||||
hy = [(cs,7, -1.3),(cs,int(nc/2+1)),(cs,7, 1.3)]
|
||||
hz = [(cs,7, -1.3),(cs,int(nc/2+1))]
|
||||
mesh = Mesh.TensorMesh([hx, hy, hz], 'CCN')
|
||||
sighalf = 1e-2
|
||||
sigma = np.ones(mesh.nC)*sighalf
|
||||
p0 = np.r_[-50., 50., -50.]
|
||||
p1 = np.r_[ 50.,-50., -150.]
|
||||
blk_ind = Utils.ModelBuilder.getIndicesBlock(p0, p1, mesh.gridCC)
|
||||
sigma[blk_ind] = 1e-3
|
||||
eta = np.zeros_like(sigma)
|
||||
eta[blk_ind] = 0.1
|
||||
sigmaInf = sigma.copy()
|
||||
sigma0 = sigma*(1-eta)
|
||||
|
||||
nElecs = 11
|
||||
x_temp = np.linspace(-100, 100, nElecs)
|
||||
aSpacing = x_temp[1]-x_temp[0]
|
||||
y_temp = 0.
|
||||
xyz = Utils.ndgrid(x_temp, np.r_[y_temp], np.r_[0.])
|
||||
srcList = DC.Utils.WennerSrcList(nElecs,aSpacing)
|
||||
survey = DC.SurveyDC(srcList)
|
||||
|
||||
imap = Maps.IdentityMap(mesh)
|
||||
problem = DC.ProblemDC_CC(mesh, mapping=imap)
|
||||
|
||||
try:
|
||||
from pymatsolver import MumpsSolver
|
||||
solver = MumpsSolver
|
||||
except ImportError, e:
|
||||
solver = SolverLU
|
||||
|
||||
problem.Solver = solver
|
||||
problem.pair(survey)
|
||||
|
||||
phi0 = survey.dpred(sigma0)
|
||||
phiInf = survey.dpred(sigmaInf)
|
||||
|
||||
phiIP_true = phi0-phiInf
|
||||
|
||||
surveyIP = DC.SurveyIP(srcList)
|
||||
problemIP = DC.ProblemIP(mesh, sigma=sigma)
|
||||
problemIP.pair(surveyIP)
|
||||
|
||||
problemIP.Solver = solver
|
||||
|
||||
phiIP_approx = surveyIP.dpred(eta)
|
||||
|
||||
err = np.linalg.norm(phiIP_true-phiIP_approx) / np.linalg.norm(phiIP_true)
|
||||
|
||||
self.assertTrue(err < 0.02)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -0,0 +1,82 @@
|
||||
import unittest
|
||||
from SimPEG import *
|
||||
import SimPEG.DCIP as DC
|
||||
|
||||
class IPProblemTests(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
|
||||
cs = 12.5
|
||||
nc = 500/cs+1
|
||||
hx = [(cs,0, -1.3),(cs,nc),(cs,0, 1.3)]
|
||||
hy = [(cs,0, -1.3),(cs,int(nc/2+1)),(cs,0, 1.3)]
|
||||
hz = [(cs,0, -1.3),(cs,int(nc/2+1))]
|
||||
mesh = Mesh.TensorMesh([hx, hy, hz], 'CCN')
|
||||
sighalf = 1e-2
|
||||
sigma = np.ones(mesh.nC)*sighalf
|
||||
p0 = np.r_[-50., 50., -50.]
|
||||
p1 = np.r_[ 50.,-50., -150.]
|
||||
blk_ind = Utils.ModelBuilder.getIndicesBlock(p0, p1, mesh.gridCC)
|
||||
sigma[blk_ind] = 1e-3
|
||||
eta = np.zeros_like(sigma)
|
||||
eta[blk_ind] = 0.1
|
||||
|
||||
nElecs = 5
|
||||
x_temp = np.linspace(-250, 250, nElecs)
|
||||
aSpacing = x_temp[1]-x_temp[0]
|
||||
y_temp = 0.
|
||||
xyz = Utils.ndgrid(x_temp, np.r_[y_temp], np.r_[0.])
|
||||
srcList = DC.Utils.WennerSrcList(nElecs,aSpacing)
|
||||
survey = DC.SurveyIP(srcList)
|
||||
imap = Maps.IdentityMap(mesh)
|
||||
problem = DC.ProblemIP(mesh, sigma=sigma, mapping= imap)
|
||||
problem.pair(survey)
|
||||
|
||||
try:
|
||||
from pymatsolver import MumpsSolver
|
||||
problem.Solver = MumpsSolver
|
||||
except ImportError, e:
|
||||
problem.Solver = SolverLU
|
||||
|
||||
mSynth = eta
|
||||
survey.makeSyntheticData(mSynth)
|
||||
|
||||
# Now set up the problem to do some minimization
|
||||
dmis = DataMisfit.l2_DataMisfit(survey)
|
||||
reg = Regularization.Tikhonov(mesh)
|
||||
opt = Optimization.InexactGaussNewton(maxIterLS=20, maxIter=10, tolF=1e-6, tolX=1e-6, tolG=1e-6, maxIterCG=6)
|
||||
invProb = InvProblem.BaseInvProblem(dmis, reg, opt, beta=1e4)
|
||||
inv = Inversion.BaseInversion(invProb)
|
||||
|
||||
self.inv = inv
|
||||
self.reg = reg
|
||||
self.p = problem
|
||||
self.mesh = mesh
|
||||
self.m0 = mSynth
|
||||
self.survey = survey
|
||||
self.dmis = dmis
|
||||
|
||||
def test_misfit(self):
|
||||
derChk = lambda m: [self.survey.dpred(m), lambda mx: self.p.Jvec(self.m0, mx)]
|
||||
passed = Tests.checkDerivative(derChk, self.m0*0, plotIt=False)
|
||||
self.assertTrue(passed)
|
||||
|
||||
def test_adjoint(self):
|
||||
# Adjoint Test
|
||||
u = np.random.rand(self.mesh.nC*self.survey.nSrc)
|
||||
v = np.random.rand(self.mesh.nC)
|
||||
w = np.random.rand(self.survey.dobs.shape[0])
|
||||
wtJv = w.dot(self.p.Jvec(self.m0, v))
|
||||
vtJtw = v.dot(self.p.Jtvec(self.m0, w))
|
||||
passed = np.abs(wtJv - vtJtw) < 1e-10
|
||||
print 'Adjoint Test', np.abs(wtJv - vtJtw), passed
|
||||
self.assertTrue(passed)
|
||||
|
||||
def test_dataObj(self):
|
||||
derChk = lambda m: [self.dmis.eval(m), self.dmis.evalDeriv(m)]
|
||||
passed = Tests.checkDerivative(derChk, self.m0, plotIt=False)
|
||||
self.assertTrue(passed)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -18,8 +18,8 @@ class TDEM_bDerivTests(unittest.TestCase):
|
||||
mesh = Mesh.CylMesh([hx,1,hy], '00C')
|
||||
|
||||
active = mesh.vectorCCz<0.
|
||||
activeMap = Maps.ActiveCells(mesh, active, np.log(1e-8), nC=mesh.nCz)
|
||||
mapping = Maps.ExpMap(mesh) * Maps.Vertical1DMap(mesh) * activeMap
|
||||
activeMap = Maps.InjectActiveCells(mesh, active, np.log(1e-8), nC=mesh.nCz)
|
||||
mapping = Maps.ExpMap(mesh) * Maps.SurjectVertical1D(mesh) * activeMap
|
||||
|
||||
rxOffset = 40.
|
||||
rx = EM.TDEM.RxTDEM(np.array([[rxOffset, 0., 0.]]), np.logspace(-4,-3, 20), 'bz')
|
||||
@@ -204,8 +204,8 @@ class TDEM_bDerivTests(unittest.TestCase):
|
||||
d = Survey.Data(survey,v=d_vec)
|
||||
|
||||
# Check that d.T*Q*f = f.T*Q.T*d
|
||||
V1 = d_vec.dot(survey.projectFieldsDeriv(None, v=f).tovec())
|
||||
V2 = f.tovec().dot(survey.projectFieldsDeriv(None, v=d, adjoint=True).tovec())
|
||||
V1 = d_vec.dot(survey.evalDeriv(None, v=f).tovec())
|
||||
V2 = f.tovec().dot(survey.evalDeriv(None, v=d, adjoint=True).tovec())
|
||||
|
||||
self.assertTrue((V1-V2)/np.abs(V1) < tol)
|
||||
|
||||
|
||||
@@ -17,8 +17,8 @@ class TDEM_bDerivTests(unittest.TestCase):
|
||||
mesh = Mesh.CylMesh([hx,1,hy], '00C')
|
||||
|
||||
active = mesh.vectorCCz<0.
|
||||
activeMap = Maps.ActiveCells(mesh, active, np.log(1e-8), nC=mesh.nCz)
|
||||
mapping = Maps.ExpMap(mesh) * Maps.Vertical1DMap(mesh) * activeMap
|
||||
activeMap = Maps.InjectActiveCells(mesh, active, np.log(1e-8), nC=mesh.nCz)
|
||||
mapping = Maps.ExpMap(mesh) * Maps.SurjectVertical1D(mesh) * activeMap
|
||||
|
||||
rxOffset = 40.
|
||||
rx = EM.TDEM.RxTDEM(np.array([[rxOffset, 0., 0.]]), np.logspace(-4,-3, 20), 'bz')
|
||||
@@ -108,8 +108,8 @@ class TDEM_bDerivTests(unittest.TestCase):
|
||||
d = Survey.Data(survey,v=d_vec)
|
||||
|
||||
# Check that d.T*Q*f = f.T*Q.T*d
|
||||
V1 = d_vec.dot(survey.projectFieldsDeriv(None, v=f).tovec())
|
||||
V2 = np.sum((f.tovec())*(survey.projectFieldsDeriv(None, v=d, adjoint=True).tovec()))
|
||||
V1 = d_vec.dot(survey.evalDeriv(None, v=f).tovec())
|
||||
V2 = np.sum((f.tovec())*(survey.evalDeriv(None, v=d, adjoint=True).tovec()))
|
||||
|
||||
self.assertTrue((V1-V2)/np.abs(V1) < 1e-6)
|
||||
|
||||
|
||||
@@ -14,8 +14,8 @@ def getProb(meshType='CYL',rxTypes='bx,bz',nSrc=1):
|
||||
mesh = Mesh.CylMesh([hx,1,hy], '00C')
|
||||
|
||||
active = mesh.vectorCCz<0.
|
||||
activeMap = Maps.ActiveCells(mesh, active, np.log(1e-8), nC=mesh.nCz)
|
||||
mapping = Maps.ExpMap(mesh) * Maps.Vertical1DMap(mesh) * activeMap
|
||||
activeMap = Maps.InjectActiveCells(mesh, active, np.log(1e-8), nC=mesh.nCz)
|
||||
mapping = Maps.ExpMap(mesh) * Maps.SurjectVertical1D(mesh) * activeMap
|
||||
|
||||
rxOffset = 40.
|
||||
|
||||
|
||||
@@ -24,8 +24,8 @@ def halfSpaceProblemAnaDiff(meshType, sig_half=1e-2, rxOffset=50., bounds=[1e-5,
|
||||
mesh = Mesh.TensorMesh([hx,hy,hz], 'CCC')
|
||||
|
||||
active = mesh.vectorCCz<0.
|
||||
actMap = Maps.ActiveCells(mesh, active, np.log(1e-8), nC=mesh.nCz)
|
||||
mapping = Maps.ExpMap(mesh) * Maps.Vertical1DMap(mesh) * actMap
|
||||
actMap = Maps.InjectActiveCells(mesh, active, np.log(1e-8), nC=mesh.nCz)
|
||||
mapping = Maps.ExpMap(mesh) * Maps.SurjectVertical1D(mesh) * actMap
|
||||
|
||||
rx = EM.TDEM.RxTDEM(np.array([[rxOffset, 0., 0.]]), np.logspace(-5,-4, 21), 'bz')
|
||||
src = EM.TDEM.SrcTDEM_VMD_MVP([rx], loc=np.array([0., 0., 0.]))
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import os
|
||||
import glob
|
||||
import unittest
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_file_strings = glob.glob('test_*.py')
|
||||
module_strings = [str[0:len(str)-3] for str in test_file_strings]
|
||||
suites = [unittest.defaultTestLoader.loadTestsFromName(str) for str
|
||||
in module_strings]
|
||||
testSuite = unittest.TestSuite(suites)
|
||||
|
||||
unittest.TextTestRunner(verbosity=2).run(testSuite)
|
||||
@@ -0,0 +1,48 @@
|
||||
import unittest
|
||||
from SimPEG import *
|
||||
from SimPEG import MT
|
||||
|
||||
TOL = 1e-6
|
||||
|
||||
def appResPhs(freq,z):
|
||||
app_res = ((1./(8e-7*np.pi**2))/freq)*np.abs(z)**2
|
||||
app_phs = np.arctan2(-z.imag,z.real)*(180/np.pi)
|
||||
return app_res, app_phs
|
||||
|
||||
def appResNorm(sigmaHalf):
|
||||
nFreq = 26
|
||||
|
||||
m1d = Mesh.TensorMesh([[(100,5,1.5),(100.,10),(100,5,1.5)]], x0=['C'])
|
||||
sigma = np.zeros(m1d.nC) + sigmaHalf
|
||||
sigma[m1d.gridCC[:]>200] = 1e-8
|
||||
|
||||
# Calculate the analytic fields
|
||||
freqs = np.logspace(4,-4,nFreq)
|
||||
Z = []
|
||||
for freq in freqs:
|
||||
Ed, Eu, Hd, Hu = MT.Utils.getEHfields(m1d,sigma,freq,np.array([200]))
|
||||
Z.append((Ed + Eu)/(Hd + Hu))
|
||||
|
||||
Zarr = np.concatenate(Z)
|
||||
|
||||
app_r, app_p = appResPhs(freqs,Zarr)
|
||||
|
||||
return np.linalg.norm(np.abs(app_r - np.ones(nFreq)/sigmaHalf)) / np.log10(sigmaHalf)
|
||||
|
||||
|
||||
class TestAnalytics(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
def test_appRes2en1(self):self.assertLess(appResNorm(2e-1), TOL)
|
||||
def test_appRes2en2(self):self.assertLess(appResNorm(2e-2), TOL)
|
||||
def test_appRes2en3(self):self.assertLess(appResNorm(2e-3), TOL)
|
||||
def test_appRes2en4(self):self.assertLess(appResNorm(2e-4), TOL)
|
||||
def test_appRes2en5(self):self.assertLess(appResNorm(2e-5), TOL)
|
||||
def test_appRes2en6(self):self.assertLess(appResNorm(2e-6), TOL)
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -0,0 +1,162 @@
|
||||
import unittest
|
||||
import SimPEG as simpeg
|
||||
from SimPEG import MT
|
||||
from SimPEG.Utils import meshTensor
|
||||
import numpy as np
|
||||
# Define the tolerances
|
||||
TOLr = 5e-2
|
||||
TOLp = 5e-2
|
||||
|
||||
|
||||
def setupSurvey(sigmaHalf,tD=True):
|
||||
|
||||
# Frequency
|
||||
nFreq = 33
|
||||
freqs = np.logspace(3,-3,nFreq)
|
||||
# Make the mesh
|
||||
ct = 5
|
||||
air = meshTensor([(ct,25,1.3)])
|
||||
# coreT0 = meshTensor([(ct,15,1.2)])
|
||||
# coreT1 = np.kron(meshTensor([(coreT0[-1],15,1.3)]),np.ones((7,)))
|
||||
core = np.concatenate( ( np.kron(meshTensor([(ct,15,-1.2)]),np.ones((10,))) , meshTensor([(ct,20)]) ) )
|
||||
bot = meshTensor([(core[0],10,-1.3)])
|
||||
x0 = -np.array([np.sum(np.concatenate((core,bot)))])
|
||||
m1d = simpeg.Mesh.TensorMesh([np.concatenate((bot,core,air))], x0=x0)
|
||||
# Make the model
|
||||
sigma = np.zeros(m1d.nC) + sigmaHalf
|
||||
sigma[m1d.gridCC > 0 ] = 1e-8
|
||||
|
||||
rxList = []
|
||||
for rxType in ['z1dr','z1di']:
|
||||
rxList.append(MT.Rx(simpeg.mkvc(np.array([0.0]),2).T,rxType))
|
||||
# Source list
|
||||
srcList =[]
|
||||
if tD:
|
||||
for freq in freqs:
|
||||
srcList.append(MT.SrcMT.polxy_1DhomotD(rxList,freq))
|
||||
else:
|
||||
for freq in freqs:
|
||||
srcList.append(MT.SrcMT.polxy_1Dprimary(rxList,freq))
|
||||
|
||||
survey = MT.Survey(srcList)
|
||||
return survey, sigma, m1d
|
||||
|
||||
def getAppResPhs(MTdata):
|
||||
# Make impedance
|
||||
def appResPhs(freq,z):
|
||||
app_res = ((1./(8e-7*np.pi**2))/freq)*np.abs(z)**2
|
||||
app_phs = np.arctan2(z.imag,z.real)*(180/np.pi)
|
||||
return app_res, app_phs
|
||||
zList = []
|
||||
for src in MTdata.survey.srcList:
|
||||
zc = [src.freq]
|
||||
for rx in src.rxList:
|
||||
if 'i' in rx.rxType:
|
||||
m=1j
|
||||
else:
|
||||
m = 1
|
||||
zc.append(m*MTdata[src,rx])
|
||||
zList.append(zc)
|
||||
return [appResPhs(zList[i][0],np.sum(zList[i][1:3])) for i in np.arange(len(zList))]
|
||||
|
||||
def appRes_TotalFieldNorm(sigmaHalf):
|
||||
|
||||
# Make the survey
|
||||
survey, sigma, mesh = setupSurvey(sigmaHalf)
|
||||
problem = MT.Problem1D.eForm_TotalField(mesh)
|
||||
problem.pair(survey)
|
||||
|
||||
# Get the fields
|
||||
fields = problem.fields(sigma)
|
||||
|
||||
# Project the data
|
||||
data = survey.eval(fields)
|
||||
|
||||
# Calculate the app res and phs
|
||||
app_r = np.array(getAppResPhs(data))[:,0]
|
||||
|
||||
return np.linalg.norm(np.abs(app_r - np.ones(survey.nFreq)/sigmaHalf)*sigmaHalf)
|
||||
|
||||
def appPhs_TotalFieldNorm(sigmaHalf):
|
||||
|
||||
# Make the survey
|
||||
survey, sigma, mesh = setupSurvey(sigmaHalf)
|
||||
problem = MT.Problem1D.eForm_TotalField(mesh)
|
||||
problem.pair(survey)
|
||||
|
||||
# Get the fields
|
||||
fields = problem.fields(sigma)
|
||||
|
||||
# Project the data
|
||||
data = survey.eval(fields)
|
||||
|
||||
# Calculate the app phs
|
||||
app_p = np.array(getAppResPhs(data))[:,1]
|
||||
|
||||
return np.linalg.norm(np.abs(app_p - np.ones(survey.nFreq)*45)/ 45)
|
||||
|
||||
def appRes_psFieldNorm(sigmaHalf):
|
||||
|
||||
# Make the survey
|
||||
survey, sigma, mesh = setupSurvey(sigmaHalf,False)
|
||||
problem = MT.Problem1D.eForm_psField(mesh, sigmaPrimary = sigma)
|
||||
problem.pair(survey)
|
||||
|
||||
# Get the fields
|
||||
fields = problem.fields(sigma)
|
||||
|
||||
# Project the data
|
||||
data = survey.eval(fields)
|
||||
|
||||
# Calculate the app res and phs
|
||||
app_r = np.array(getAppResPhs(data))[:,0]
|
||||
|
||||
return np.linalg.norm(np.abs(app_r - np.ones(survey.nFreq)/sigmaHalf)*sigmaHalf)
|
||||
|
||||
def appPhs_psFieldNorm(sigmaHalf):
|
||||
|
||||
# Make the survey
|
||||
survey, sigma, mesh = setupSurvey(sigmaHalf,False)
|
||||
problem = MT.Problem1D.eForm_psField(mesh, sigmaPrimary = sigma)
|
||||
problem.pair(survey)
|
||||
|
||||
# Get the fields
|
||||
fields = problem.fields(sigma)
|
||||
|
||||
# Project the data
|
||||
data = survey.eval(fields)
|
||||
|
||||
# Calculate the app phs
|
||||
app_p = np.array(getAppResPhs(data))[:,1]
|
||||
|
||||
return np.linalg.norm(np.abs(app_p - np.ones(survey.nFreq)*45)/ 45)
|
||||
|
||||
class TestAnalytics(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
# Total Fields
|
||||
# def test_appRes2en1(self):self.assertLess(appRes_TotalFieldNorm(2e-1), TOLr)
|
||||
# def test_appPhs2en1(self):self.assertLess(appPhs_TotalFieldNorm(2e-1), TOLp)
|
||||
|
||||
# def test_appRes2en2(self):self.assertLess(appRes_TotalFieldNorm(2e-2), TOLr)
|
||||
# def test_appPhs2en2(self):self.assertLess(appPhs_TotalFieldNorm(2e-2), TOLp)
|
||||
|
||||
# def test_appRes2en3(self):self.assertLess(appRes_TotalFieldNorm(2e-3), TOLr)
|
||||
# def test_appPhs2en3(self):self.assertLess(appPhs_TotalFieldNorm(2e-3), TOLp)
|
||||
|
||||
# def test_appRes2en4(self):self.assertLess(appRes_TotalFieldNorm(2e-4), TOLr)
|
||||
# def test_appPhs2en4(self):self.assertLess(appPhs_TotalFieldNorm(2e-4), TOLp)
|
||||
|
||||
# def test_appRes2en5(self):self.assertLess(appRes_TotalFieldNorm(2e-5), TOLr)
|
||||
# def test_appPhs2en5(self):self.assertLess(appPhs_TotalFieldNorm(2e-5), TOLp)
|
||||
|
||||
# def test_appRes2en6(self):self.assertLess(appRes_TotalFieldNorm(2e-6), TOLr)
|
||||
# def test_appPhs2en6(self):self.assertLess(appPhs_TotalFieldNorm(2e-6), TOLp)
|
||||
|
||||
# Primary/secondary
|
||||
def test_appRes2en2_ps(self):self.assertLess(appRes_psFieldNorm(2e-2), TOLr)
|
||||
def test_appPhs2en2_ps(self):self.assertLess(appPhs_psFieldNorm(2e-2), TOLp)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -0,0 +1,135 @@
|
||||
import unittest
|
||||
import SimPEG as simpeg
|
||||
from SimPEG import MT
|
||||
from SimPEG.Utils import meshTensor
|
||||
import numpy as np
|
||||
# Define the tolerances
|
||||
TOLr = 5e-2
|
||||
TOLp = 5e-2
|
||||
|
||||
|
||||
def setupSurvey(sigmaHalf,tD=True):
|
||||
|
||||
# Frequency
|
||||
nFreq = 33
|
||||
freqs = np.logspace(3,-3,nFreq)
|
||||
# Make the mesh
|
||||
ct = 5
|
||||
air = meshTensor([(ct,25,1.3)])
|
||||
# coreT0 = meshTensor([(ct,15,1.2)])
|
||||
# coreT1 = np.kron(meshTensor([(coreT0[-1],15,1.3)]),np.ones((7,)))
|
||||
core = np.concatenate( ( np.kron(meshTensor([(ct,15,-1.2)]),np.ones((10,))) , meshTensor([(ct,20)]) ) )
|
||||
bot = meshTensor([(core[0],15,-1.3)])
|
||||
x0 = -np.array([np.sum(np.concatenate((core,bot)))])
|
||||
m1d = simpeg.Mesh.TensorMesh([np.concatenate((bot,core,air))], x0=x0)
|
||||
# Make the model
|
||||
sigma = np.zeros(m1d.nC) + sigmaHalf
|
||||
sigma[m1d.gridCC > 0 ] = 1e-8
|
||||
sigmaBack = sigma.copy()
|
||||
# Add structure
|
||||
shallow = (m1d.gridCC < -200) * (m1d.gridCC > -600)
|
||||
deep = (m1d.gridCC < -3000) * (m1d.gridCC > -5000)
|
||||
sigma[shallow] = 1
|
||||
sigma[deep] = 0.1
|
||||
|
||||
rxList = []
|
||||
for rxType in ['z1dr','z1di']:
|
||||
rxList.append(MT.Rx(simpeg.mkvc(np.array([0.0]),2).T,rxType))
|
||||
# Source list
|
||||
srcList =[]
|
||||
if tD:
|
||||
for freq in freqs:
|
||||
srcList.append(MT.SrcMT.polxy_1DhomotD(rxList,freq))
|
||||
else:
|
||||
for freq in freqs:
|
||||
srcList.append(MT.SrcMT.polxy_1Dprimary(rxList,freq))
|
||||
|
||||
survey = MT.Survey(srcList)
|
||||
return survey, sigma, m1d
|
||||
|
||||
def getAppResPhs(MTdata):
|
||||
# Make impedance
|
||||
def appResPhs(freq,z):
|
||||
app_res = ((1./(8e-7*np.pi**2))/freq)*np.abs(z)**2
|
||||
app_phs = np.arctan2(z.imag,z.real)*(180/np.pi)
|
||||
return app_res, app_phs
|
||||
zList = []
|
||||
for src in MTdata.survey.srcList:
|
||||
zc = [src.freq]
|
||||
for rx in src.rxList:
|
||||
if 'i' in rx.rxType:
|
||||
m=1j
|
||||
else:
|
||||
m = 1
|
||||
zc.append(m*MTdata[src,rx])
|
||||
zList.append(zc)
|
||||
return [appResPhs(zList[i][0],np.sum(zList[i][1:3])) for i in np.arange(len(zList))]
|
||||
|
||||
def calculateAnalyticSolution(srcList,mesh,model):
|
||||
surveyAna = MT.Survey(srcList)
|
||||
data1D = MT.Data(surveyAna)
|
||||
for src in surveyAna.srcList:
|
||||
elev = src.rxList[0].locs[0]
|
||||
anaEd, anaEu, anaHd, anaHu = MT.Utils.MT1Danalytic.getEHfields(mesh,model,src.freq,elev)
|
||||
anaE = anaEd+anaEu
|
||||
anaH = anaHd+anaHu
|
||||
# Scale the solution
|
||||
# anaE = (anaEtemp/anaEtemp[-1])#.conj()
|
||||
# anaH = (anaHtemp/anaEtemp[-1])#.conj()
|
||||
anaZ = anaE/anaH
|
||||
for rx in src.rxList:
|
||||
data1D[src,rx] = getattr(anaZ, rx.projComp)
|
||||
return data1D
|
||||
|
||||
def dataMis_AnalyticTotalDomain(sigmaHalf):
|
||||
|
||||
# Make the survey
|
||||
|
||||
# Total domain solution
|
||||
surveyTD, sigma, mesh = setupSurvey(sigmaHalf)
|
||||
problemTD = MT.Problem1D.eForm_TotalField(mesh)
|
||||
problemTD.pair(surveyTD)
|
||||
# Analytic data
|
||||
dataAnaObj = calculateAnalyticSolution(surveyTD.srcList,mesh,sigma)
|
||||
# dataTDObj = MT.DataMT.DataMT(surveyTD, surveyTD.dpred(sigma))
|
||||
dataTD = surveyTD.dpred(sigma)
|
||||
dataAna = simpeg.mkvc(dataAnaObj)
|
||||
return np.all((dataTD - dataAna)/dataAna < 2.)
|
||||
# surveyTD.dtrue = -simpeg.mkvc(dataAna,2)
|
||||
# surveyTD.dobs = -simpeg.mkvc(dataAna,2)
|
||||
# surveyTD.Wd = np.ones(surveyTD.dtrue.shape) #/(np.abs(surveyTD.dtrue)*0.01)
|
||||
# # Setup the data misfit
|
||||
# dmis = simpeg.DataMisfit.l2_DataMisfit(surveyTD)
|
||||
# dmis.Wd = surveyTD.Wd
|
||||
# return dmis.eval(sigma)
|
||||
|
||||
|
||||
def dataMis_AnalyticPrimarySecondary(sigmaHalf):
|
||||
|
||||
# Make the survey
|
||||
# Primary secondary
|
||||
surveyPS, sigmaPS, mesh = setupSurvey(sigmaHalf,tD=False)
|
||||
problemPS = MT.Problem1D.eForm_psField(mesh)
|
||||
problemPS.sigmaPrimary = sigmaPS
|
||||
problemPS.pair(surveyPS)
|
||||
# Analytic data
|
||||
dataAnaObj = calculateAnalyticSolution(surveyPS.srcList,mesh,sigmaPS)
|
||||
|
||||
dataPS = surveyPS.dpred(sigmaPS)
|
||||
dataAna = simpeg.mkvc(dataAnaObj)
|
||||
return np.all((dataPS - dataAna)/dataAna < 2.)
|
||||
|
||||
|
||||
|
||||
class TestNumericVsAnalytics(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
# Total Fields
|
||||
# def test_appRes2en2(self):self.assertTrue(dataMis_AnalyticTotalDomain(2e-2))
|
||||
|
||||
# Primary/secondary
|
||||
def test_appRes2en2_ps(self):self.assertTrue(dataMis_AnalyticPrimarySecondary(2e-2))
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -0,0 +1,271 @@
|
||||
# Test functions
|
||||
from glob import glob
|
||||
import numpy as np, sys, os, time, scipy, subprocess
|
||||
import SimPEG as simpeg
|
||||
import unittest
|
||||
from SimPEG import MT
|
||||
from SimPEG.Utils import meshTensor
|
||||
from scipy.constants import mu_0
|
||||
|
||||
TOLr = 5e-2
|
||||
TOL = 1e-4
|
||||
FLR = 1e-20 # "zero", so if residual below this --> pass regardless of order
|
||||
CONDUCTIVITY = 1e1
|
||||
MU = mu_0
|
||||
freq = [1e-1, 2e-1]
|
||||
addrandoms = True
|
||||
|
||||
|
||||
def getInputs():
|
||||
"""
|
||||
Function that returns Mesh, freqs, rx_loc, elev.
|
||||
"""
|
||||
# Make a mesh
|
||||
# M = simpeg.Mesh.TensorMesh([[(100,5,-1.5),(100.,10),(100,5,1.5)],[(100,5,-1.5),(100.,10),(100,5,1.5)],[(100,5,1.6),(100.,10),(100,3,2)]], x0=['C','C',-3529.5360])
|
||||
# M = simpeg.Mesh.TensorMesh([[(1000,6,-1.5),(1000.,6),(1000,6,1.5)],[(1000,6,-1.5),(1000.,2),(1000,6,1.5)],[(1000,6,-1.3),(1000.,6),(1000,6,1.3)]], x0=['C','C','C'])# Setup the model
|
||||
M = simpeg.Mesh.TensorMesh([[(1000,6,-1.5),(1000.,4),(1000,6,1.5)],[(1000,6,-1.5),(1000.,4),(1000,6,1.5)],[(500,8,-1.3),(500.,8),(500,8,1.3)]], x0=['C','C','C'])# Setup the model
|
||||
# Set the frequencies
|
||||
freqs = np.logspace(1,-3,5)
|
||||
elev = 0
|
||||
|
||||
## Setup the the survey object
|
||||
# Receiver locations
|
||||
rx_x, rx_y = np.meshgrid(np.arange(-1000,1001,500),np.arange(-1000,1001,500))
|
||||
rx_loc = np.hstack((simpeg.Utils.mkvc(rx_x,2),simpeg.Utils.mkvc(rx_y,2),elev+np.zeros((np.prod(rx_x.shape),1))))
|
||||
|
||||
return M, freqs, rx_loc, elev
|
||||
|
||||
def random(conds):
|
||||
''' Returns a halfspace model based on the inputs'''
|
||||
M, freqs, rx_loc, elev = getInputs()
|
||||
|
||||
# Backround
|
||||
sigBG = np.ones(M.nC)*conds
|
||||
# Add randomness to the model (10% of the value).
|
||||
sig = np.exp( np.log(sigBG) + np.random.randn(M.nC)*(conds)*1e-1 )
|
||||
|
||||
return (M, freqs, sig, sigBG, rx_loc)
|
||||
|
||||
def halfSpace(conds):
|
||||
''' Returns a halfspace model based on the inputs'''
|
||||
M, freqs, rx_loc, elev = getInputs()
|
||||
|
||||
# Model
|
||||
ccM = M.gridCC
|
||||
# conds = [1e-2]
|
||||
groundInd = ccM[:,2] < elev
|
||||
sig = np.zeros(M.nC) + 1e-8
|
||||
sig[groundInd] = conds
|
||||
# Set the background, not the same as the model
|
||||
sigBG = np.zeros(M.nC) + 1e-8
|
||||
sigBG[groundInd] = conds
|
||||
|
||||
return (M, freqs, sig, sigBG, rx_loc)
|
||||
|
||||
def blockInhalfSpace(conds):
|
||||
''' Returns a halfspace model based on the inputs'''
|
||||
M, freqs, rx_loc, elev = getInputs()
|
||||
|
||||
# Model
|
||||
ccM = M.gridCC
|
||||
# conds = [1e-2]
|
||||
groundInd = ccM[:,2] < elev
|
||||
sig = simpeg.Utils.ModelBuilder.defineBlock(M.gridCC,np.array([-1000,-1000,-1500]),np.array([1000,1000,-1000]),conds)
|
||||
sig[~groundInd] = 1e-8
|
||||
# Set the background, not the same as the model
|
||||
sigBG = np.zeros(M.nC) + 1e-8
|
||||
sigBG[groundInd] = conds[1]
|
||||
|
||||
return (M, freqs, sig, sigBG, rx_loc)
|
||||
|
||||
def twoLayer(conds):
|
||||
''' Returns a 2 layer model based on the conductivity values given'''
|
||||
M, freqs, rx_loc, elev = getInputs()
|
||||
|
||||
# Model
|
||||
ccM = M.gridCC
|
||||
groundInd = ccM[:,2] < elev
|
||||
botInd = ccM[:,2] < -3000
|
||||
sig = np.zeros(M.nC) + 1e-8
|
||||
sig[groundInd] = conds[1]
|
||||
sig[botInd] = conds[0]
|
||||
# Set the background, not the same as the model
|
||||
sigBG = np.zeros(M.nC) + 1e-8
|
||||
sigBG[groundInd] = conds[1]
|
||||
|
||||
|
||||
return (M, freqs, sig, sigBG, rx_loc)
|
||||
|
||||
|
||||
|
||||
def setupSimpegMTfwd_eForm_ps(inputSetup,comp='Imp',singleFreq=False,expMap=True):
|
||||
M,freqs,sig,sigBG,rx_loc = inputSetup
|
||||
# Make a receiver list
|
||||
rxList = []
|
||||
if comp == 'All':
|
||||
for rxType in ['zxxr','zxxi','zxyr','zxyi','zyxr','zyxi','zyyr','zyyi','tzxr','tzxi','tzyr','tzyi']:
|
||||
rxList.append(MT.Rx(rx_loc,rxType))
|
||||
elif comp == 'Imp':
|
||||
for rxType in ['zxxr','zxxi','zxyr','zxyi','zyxr','zyxi','zyyr','zyyi']:
|
||||
rxList.append(MT.Rx(rx_loc,rxType))
|
||||
elif comp == 'Tip':
|
||||
for rxType in ['tzxr','tzxi','tzyr','tzyi']:
|
||||
rxList.append(MT.Rx(rx_loc,rxType))
|
||||
else:
|
||||
rxList.append(MT.Rx(rx_loc,comp))
|
||||
# Source list
|
||||
srcList =[]
|
||||
|
||||
if singleFreq:
|
||||
srcList.append(MT.SrcMT.polxy_1Dprimary(rxList,singleFreq))
|
||||
else:
|
||||
for freq in freqs:
|
||||
srcList.append(MT.SrcMT.polxy_1Dprimary(rxList,freq))
|
||||
# Survey MT
|
||||
survey = MT.Survey(srcList)
|
||||
|
||||
## Setup the problem object
|
||||
sigma1d = M.r(sigBG,'CC','CC','M')[0,0,:]
|
||||
if expMap:
|
||||
problem = MT.Problem3D.eForm_ps(M,sigmaPrimary= np.log(sigma1d) )
|
||||
problem.mapping = simpeg.Maps.ExpMap(problem.mesh)
|
||||
problem.curModel = np.log(sig)
|
||||
else:
|
||||
problem = MT.Problem3D.eForm_ps(M,sigmaPrimary= sigma1d)
|
||||
problem.curModel = sig
|
||||
problem.pair(survey)
|
||||
problem.verbose = False
|
||||
try:
|
||||
from pymatsolver import MumpsSolver
|
||||
problem.Solver = MumpsSolver
|
||||
except:
|
||||
pass
|
||||
|
||||
return (survey, problem)
|
||||
|
||||
def getAppResPhs(MTdata):
|
||||
# Make impedance
|
||||
def appResPhs(freq,z):
|
||||
app_res = ((1./(8e-7*np.pi**2))/freq)*np.abs(z)**2
|
||||
app_phs = np.arctan2(z.imag,z.real)*(180/np.pi)
|
||||
return app_res, app_phs
|
||||
recData = MTdata.toRecArray('Complex')
|
||||
return appResPhs(recData['freq'],recData['zxy']), appResPhs(recData['freq'],recData['zyx'])
|
||||
|
||||
def JvecAdjointTest(inputSetup,comp='All',freq=False):
|
||||
(M, freqs, sig, sigBG, rx_loc) = inputSetup
|
||||
survey, problem = setupSimpegMTfwd_eForm_ps(inputSetup,comp='All',singleFreq=freq)
|
||||
print 'Adjoint test of eForm primary/secondary for {:s} comp at {:s}\n'.format(comp,str(survey.freqs))
|
||||
|
||||
m = sig
|
||||
u = problem.fields(m)
|
||||
|
||||
v = np.random.rand(survey.nD,)
|
||||
# print problem.PropMap.PropModel.nP
|
||||
w = np.random.rand(problem.mesh.nC,)
|
||||
|
||||
vJw = v.ravel().dot(problem.Jvec(m, w, u))
|
||||
wJtv = w.ravel().dot(problem.Jtvec(m, v, u))
|
||||
tol = np.max([TOL*(10**int(np.log10(np.abs(vJw)))),FLR])
|
||||
print ' vJw wJtv vJw - wJtv tol abs(vJw - wJtv) < tol'
|
||||
print vJw, wJtv, vJw - wJtv, tol, np.abs(vJw - wJtv) < tol
|
||||
return np.abs(vJw - wJtv) < tol
|
||||
|
||||
# Test the Jvec derivative
|
||||
def DerivJvecTest(inputSetup,comp='All',freq=False,expMap=True):
|
||||
(M, freqs, sig, sigBG, rx_loc) = inputSetup
|
||||
survey, problem = setupSimpegMTfwd_eForm_ps(inputSetup,comp=comp,singleFreq=freq,expMap=expMap)
|
||||
print 'Derivative test of Jvec for eForm primary/secondary for {:s} comp at {:s}\n'.format(comp,survey.freqs)
|
||||
# problem.mapping = simpeg.Maps.ExpMap(problem.mesh)
|
||||
# problem.sigmaPrimary = np.log(sigBG)
|
||||
x0 = np.log(sigBG)
|
||||
# cond = sig[0]
|
||||
# x0 = np.log(np.ones(problem.mesh.nC)*cond)
|
||||
# problem.sigmaPrimary = x0
|
||||
# if True:
|
||||
# x0 = x0 + np.random.randn(problem.mesh.nC)*cond*1e-1
|
||||
survey = problem.survey
|
||||
def fun(x):
|
||||
return survey.dpred(x), lambda x: problem.Jvec(x0, x)
|
||||
return simpeg.Tests.checkDerivative(fun, x0, num=3, plotIt=False, eps=FLR)
|
||||
|
||||
def DerivProjfieldsTest(inputSetup,comp='All',freq=False):
|
||||
|
||||
survey, problem = setupSimpegMTfwd_eForm_ps(inputSetup,comp,freq)
|
||||
print 'Derivative test of data projection for eFormulation primary/secondary\n\n'
|
||||
# problem.mapping = simpeg.Maps.ExpMap(problem.mesh)
|
||||
# Initate things for the derivs Test
|
||||
src = survey.srcList[0]
|
||||
rx = src.rxList[0]
|
||||
|
||||
u0x = np.random.randn(survey.mesh.nE)+np.random.randn(survey.mesh.nE)*1j
|
||||
u0y = np.random.randn(survey.mesh.nE)+np.random.randn(survey.mesh.nE)*1j
|
||||
u0 = np.vstack((simpeg.mkvc(u0x,2),simpeg.mkvc(u0y,2)))
|
||||
f0 = problem.fieldsPair(survey.mesh,survey)
|
||||
# u0 = np.hstack((simpeg.mkvc(u0_px,2),simpeg.mkvc(u0_py,2)))
|
||||
f0[src,'e_pxSolution'] = u0[:len(u0)/2]#u0x
|
||||
f0[src,'e_pySolution'] = u0[len(u0)/2::]#u0y
|
||||
|
||||
def fun(u):
|
||||
f = problem.fieldsPair(survey.mesh,survey)
|
||||
f[src,'e_pxSolution'] = u[:len(u)/2]
|
||||
f[src,'e_pySolution'] = u[len(u)/2::]
|
||||
return rx.eval(src,survey.mesh,f), lambda t: rx.evalDeriv(src,survey.mesh,f0,simpeg.mkvc(t,2))
|
||||
|
||||
return simpeg.Tests.checkDerivative(fun, u0, num=3, plotIt=False, eps=FLR)
|
||||
|
||||
def appResPhsHalfspace_eFrom_ps_Norm(sigmaHalf,appR=True,expMap=False):
|
||||
if appR:
|
||||
label = 'resistivity'
|
||||
else:
|
||||
label = 'phase'
|
||||
# Make the survey and the problem
|
||||
survey, problem = setupSimpegMTfwd_eForm_ps(halfSpace(sigmaHalf),expMap=expMap)
|
||||
print 'Apperent {:s} test of eFormulation primary/secondary at {:g}\n\n'.format(label,sigmaHalf)
|
||||
|
||||
data = problem.dataPair(survey,survey.dpred(problem.curModel))
|
||||
# Calculate the app phs
|
||||
app_rpxy, app_rpyx = np.array(getAppResPhs(data))
|
||||
if appR:
|
||||
return np.all(np.abs(app_rpxy[0,:] - 1./sigmaHalf) * sigmaHalf < .4)
|
||||
else:
|
||||
return np.all(np.abs(app_rpxy[1,:] + 135) / 135 < .4)
|
||||
|
||||
class TestAnalytics(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
# # Test apparent resistivity and phase
|
||||
def test_appRes1en2(self):self.assertTrue(appResPhsHalfspace_eFrom_ps_Norm(1e-2))
|
||||
def test_appPhs1en2(self):self.assertTrue(appResPhsHalfspace_eFrom_ps_Norm(1e-2,False))
|
||||
|
||||
def test_appRes1en3(self):self.assertTrue(appResPhsHalfspace_eFrom_ps_Norm(1e-3))
|
||||
def test_appPhs1en3(self):self.assertTrue(appResPhsHalfspace_eFrom_ps_Norm(1e-3,False))
|
||||
|
||||
# Do a derivative test
|
||||
def test_derivProj1(self):self.assertTrue(DerivProjfieldsTest(halfSpace(1e-2)))
|
||||
|
||||
# Do a derivative test of Jvec
|
||||
# def test_derivJvec_zxxr(self):self.assertTrue(DerivJvecTest(random(1e-2),'zxxr',.1))
|
||||
# def test_derivJvec_zxxi(self):self.assertTrue(DerivJvecTest(random(1e-2),'zxxi',.1))
|
||||
# def test_derivJvec_zxyr(self):self.assertTrue(DerivJvecTest(random(1e-2),'zxyr',.1))
|
||||
# def test_derivJvec_zxyi(self):self.assertTrue(DerivJvecTest(random(1e-2),'zxyi',.1))
|
||||
# def test_derivJvec_zyxr(self):self.assertTrue(DerivJvecTest(random(1e-2),'zyxr',.1))
|
||||
# def test_derivJvec_zyxi(self):self.assertTrue(DerivJvecTest(random(1e-2),'zyxi',.1))
|
||||
# def test_derivJvec_zyyr(self):self.assertTrue(DerivJvecTest(random(1e-2),'zyyr',.1))
|
||||
# def test_derivJvec_zyyi(self):self.assertTrue(DerivJvecTest(random(1e-2),'zyyi',.1))
|
||||
def test_derivJvec_All(self):self.assertTrue(DerivJvecTest(random(1e-2),'All',.1))
|
||||
|
||||
# Test the adjoint of Jvec and Jtvec
|
||||
# def test_JvecAdjoint_zxxr(self):self.assertTrue(JvecAdjointTest(random(1e-2),'zxxr',.1))
|
||||
# def test_JvecAdjoint_zxxi(self):self.assertTrue(JvecAdjointTest(random(1e-2),'zxxi',.1))
|
||||
# def test_JvecAdjoint_zxyr(self):self.assertTrue(JvecAdjointTest(random(1e-2),'zxyr',.1))
|
||||
# def test_JvecAdjoint_zxyi(self):self.assertTrue(JvecAdjointTest(random(1e-2),'zxyi',.1))
|
||||
# def test_JvecAdjoint_zyxr(self):self.assertTrue(JvecAdjointTest(random(1e-2),'zyxr',.1))
|
||||
# def test_JvecAdjoint_zyxi(self):self.assertTrue(JvecAdjointTest(random(1e-2),'zyxi',.1))
|
||||
# def test_JvecAdjoint_zyyr(self):self.assertTrue(JvecAdjointTest(random(1e-2),'zyyr',.1))
|
||||
# def test_JvecAdjoint_zyyi(self):self.assertTrue(JvecAdjointTest(random(1e-2),'zyyi',.1))
|
||||
def test_JvecAdjoint_All(self):self.assertTrue(JvecAdjointTest(random(1e-2),'All',.1))
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user