adjoint hooked up for b formulation

This commit is contained in:
Lindsey Heagy
2016-03-14 12:28:05 -07:00
parent fe91312917
commit c91815d14f
4 changed files with 181 additions and 90 deletions
+95 -47
View File
@@ -23,7 +23,7 @@ def setUp(rxcomp='bz'):
mapping = Maps.ExpMap(mesh) * Maps.SurjectVertical1D(mesh) * activeMap
rxOffset = 10.
rx = EM.TDEM.Rx(np.array([[rxOffset, 0., -1e-2]]), np.logspace(-4,-3, 20), rxcomp)
rx = EM.TDEM.Rx(np.array([[rxOffset, 0., -1e-2]]), np.logspace(-4,-3, 20), rxcomp) #,]
src = EM.TDEM.SurveyTDEM.MagDipole([rx], loc=np.array([0., 0., 0.]))
survey = EM.TDEM.Survey([src])
@@ -45,13 +45,14 @@ def setUp(rxcomp='bz'):
return prb, m, mesh
class TDEM_DerivTests(unittest.TestCase):
# ====== TEST A ========== #
def test_Deriv_Pieces(self):
def test_AderivTest(self):
prb, m0, mesh = setUp()
tInd = 0
tInd = 2
v = np.random.rand(mesh.nF)
@@ -64,36 +65,78 @@ class TDEM_DerivTests(unittest.TestCase):
return Av, ADeriv_dm
def A_adjointTest():
print '\n Testing A_adjoint'
m = np.random.rand(prb.mapping.nP)
v = np.random.rand(prb.mesh.nF)
u = np.random.rand(prb.mesh.nF)
prb.curModel = m0
tInd = 0 # not actually used
V1 = v.dot(prb.getAdiagDeriv(tInd, u, m))
V2 = m.dot(prb.getAdiagDeriv(tInd, u, v, adjoint=True))
passed = np.abs(V1-V2) < TOL * (np.abs(V1) + np.abs(V2))/2.
print 'AdjointTest', V1, V2, passed
self.assertTrue(passed)
print '\n Testing ADeriv'
Tests.checkDerivative(AderivTest, m0, plotIt=False, num=4, eps=1e-20)
A_adjointTest()
def test_A_adjointTest(self):
prb, m0, mesh = setUp()
tInd = 2
print '\n Testing A_adjoint'
m = np.random.rand(prb.mapping.nP)
v = np.random.rand(prb.mesh.nF)
u = np.random.rand(prb.mesh.nF)
prb.curModel = m0
tInd = 2 # not actually used
V1 = v.dot(prb.getAdiagDeriv(tInd, u, m))
V2 = m.dot(prb.getAdiagDeriv(tInd, u, v, adjoint=True))
passed = np.abs(V1-V2) < TOL * (np.abs(V1) + np.abs(V2))/2.
print 'AdjointTest', V1, V2, passed
self.assertTrue(passed)
# ====== TEST Fields Deriv Pieces ========== #
def test_eDeriv_m_adjoint(self):
prb, m0, mesh = setUp()
tInd = 0
v = np.random.rand(mesh.nF)
print '\n Testing eDeriv_m Adjoint'
prb, m0, mesh = setUp()
f = prb.fields(m0)
m = np.random.rand(prb.mapping.nP)
e = np.random.randn(prb.mesh.nE)
V1 = e.dot(f._eDeriv_m(1, prb.survey.srcList[0], m))
V2 = m.dot(f._eDeriv_m(1, prb.survey.srcList[0], e, adjoint=True))
tol = TOL * (np.abs(V1) + np.abs(V2)) / 2.
passed = np.abs(V1-V2) < tol
print ' ', V1, V2, np.abs(V1-V2), tol, passed
self.assertTrue(passed)
def test_eDeriv_u_adjoint(self):
print '\n Testing eDeriv_u Adjoint'
prb, m0, mesh = setUp()
f = prb.fields(m0)
b = np.random.rand(prb.mesh.nF)
e = np.random.randn(prb.mesh.nE)
V1 = e.dot(f._eDeriv_u(1, prb.survey.srcList[0], b))
V2 = b.dot(f._eDeriv_u(1, prb.survey.srcList[0], e, adjoint=True))
tol = TOL * (np.abs(V1) + np.abs(V2)) / 2.
passed = np.abs(V1-V2) < tol
print ' ', V1, V2, np.abs(V1-V2), tol, passed
self.assertTrue(passed)
# ====== TEST Jvec ========== #
def JvecTest(self, rxcomp):
prb, m, mesh = setUp(rxcomp)
derChk = lambda m: [prb.survey.dpred(m), lambda mx: prb.Jvec(m, mx)]
print '\n'
print 'test_Jvec_%s' %(rxcomp)
Tests.checkDerivative(derChk, m, plotIt=False, num=2, eps=1e-20)
if testDeriv:
def JvecTest(self, rxcomp):
prb, m, mesh = setUp(rxcomp)
derChk = lambda m: [prb.survey.dpred(m), lambda mx: prb.Jvec(m, mx)]
print '\n'
print 'test_Jvec_%s' %(rxcomp)
Tests.checkDerivative(derChk, m, plotIt=False, num=2, eps=1e-20)
def test_Jvec_b_bx(self):
self.JvecTest('bx')
@@ -103,33 +146,38 @@ class TDEM_DerivTests(unittest.TestCase):
def test_Jvec_b_ey(self):
self.JvecTest('ey')
else:
pass
# ====== TEST Jtvec ========== #
def adjointJvecVsJtvecTest(self, rxcomp='bz'):
print '\nAdjoint Testing Jvec, Jtvec %s' %(rxcomp)
prb, m0, mesh = setUp(rxcomp)
m = np.random.rand(prb.mapping.nP)
d = np.random.randn(prb.survey.nD)
V1 = d.dot(prb.Jvec(m0, m))
V2 = m.dot(prb.Jtvec(m0, d))
tol = TOL * (np.abs(V1) + np.abs(V2)) / 2.
passed = np.abs(V1-V2) < tol
print ' ', V1, V2, np.abs(V1-V2), tol, passed
self.assertTrue(passed)
if testAdjoint:
def test_Jvec_b_bx(self):
self.adjointJvecVsJtvecTest('bx')
def test_Jvec_b_bz(self):
self.adjointJvecVsJtvecTest('bz')
def JvecVsJtvecTest(self, rxcomp='bz'):
print '\nAdjoint Testing Jvec, Jtvec %s' %(rxcomp)
prb, m0, mesh = setUp(rxcomp)
m = np.random.rand(prb.mapping.nP)
d = np.random.randn(prb.survey.nD)
V1 = d.dot(prb.Jvec(m0, m))
V2 = m.dot(prb.Jtvec(m0, d))
tol = TOL * (np.abs(V1) + np.abs(V2)) / 2.
passed = np.abs(V1-V2) < tol
print ' ', V1, V2, np.abs(V1-V2), tol, passed
self.assertTrue(passed)
def test_Jvec_adjoint_b_bx(self):
self.JvecVsJtvecTest('bx')
def test_Jvec_adjoint_b_bz(self):
self.JvecVsJtvecTest('bz')
def test_Jvec_adjoint_b_ey(self):
self.JvecVsJtvecTest('ey')
def test_Jvec_b_ey(self):
self.adjointJvecVsJtvecTest('ey')
if __name__ == '__main__':
@@ -6,11 +6,9 @@ plotIt = False
testDeriv = True
testAdjoint = True
TOL = 1e-6
TOL = 1e-5
class TDEM_bDerivTests(unittest.TestCase):
def setUp(self):
def setUp(self, rxcomp='bz'):
cs = 5.
ncx = 20
@@ -25,46 +23,58 @@ class TDEM_bDerivTests(unittest.TestCase):
mapping = Maps.ExpMap(mesh) * Maps.SurjectVertical1D(mesh) * activeMap
rxOffset = 40.
rx = EM.TDEM.Rx(np.array([[rxOffset, 0., 0.]]), np.logspace(-4,-3, 20), 'bz')
rx = EM.TDEM.Rx(np.array([[rxOffset, 0., 0.]]), np.logspace(-4,-3, 20), rxcomp)
src = EM.TDEM.SurveyTDEM.MagDipole( [rx], loc=np.array([0., 0., 0.]))
rx2 = EM.TDEM.Rx(np.array([[rxOffset-10, 0., 0.]]), np.logspace(-5,-4, 25), 'bz')
rx2 = EM.TDEM.Rx(np.array([[rxOffset-10, 0., 0.]]), np.logspace(-5,-4, 25), rxcomp)
src2 = EM.TDEM.SurveyTDEM.MagDipole( [rx2], loc=np.array([0., 0., 0.]))
survey = EM.TDEM.Survey([src,src2])
self.prb = EM.TDEM.Problem_b(mesh, mapping=mapping)
# self.prb.timeSteps = [1e-5]
self.prb.timeSteps = [(1e-05, 10), (5e-05, 10), (2.5e-4, 10)]
# self.prb.timeSteps = [(1e-05, 100)]
prb = EM.TDEM.Problem_b(mesh, mapping=mapping)
# prb.timeSteps = [1e-5]
prb.timeSteps = [(1e-05, 10), (5e-05, 10), (2.5e-4, 10)]
# prb.timeSteps = [(1e-05, 100)]
try:
from pymatsolver import MumpsSolver
self.prb.Solver = MumpsSolver
prb.Solver = MumpsSolver
except ImportError, e:
self.prb.Solver = SolverLU
prb.Solver = SolverLU
self.m = np.log(1e-1)*np.ones(self.prb.mapping.nP) + 1e-2*np.random.randn(self.prb.mapping.nP)
m = np.log(1e-1)*np.ones(prb.mapping.nP) + 1e-2*np.random.randn(prb.mapping.nP)
prb.pair(survey)
return mesh, prb, m
class TDEM_bDerivTests(unittest.TestCase):
self.prb.pair(survey)
self.mesh = mesh
if testDeriv:
def test_Deriv_J(self):
def Deriv_J(self, rxcomp='bz'):
mesh, prb, m0 = setUp(rxcomp)
prb = self.prb
prb.timeSteps = [(1e-05, 10), (0.0001, 10), (0.001, 10)]
mesh = self.mesh
derChk = lambda m: [prb.survey.dpred(m), lambda mx: prb.Jvec(self.m, mx)]
derChk = lambda m: [prb.survey.dpred(m), lambda mx: prb.Jvec(m0, mx)]
print '\n'
print 'test_Deriv_J'
Tests.checkDerivative(derChk, self.m, plotIt=False, num=3, eps=1e-20)
print 'test_Deriv_J %s'%rxcomp
Tests.checkDerivative(derChk, m0, plotIt=False, num=3, eps=1e-20)
def test_Jvec_bx(self):
self.Deriv_J('bx')
def test_Jvec_bz(self):
self.Deriv_J('bz')
def test_Jvec_ey(self):
self.Deriv_J('ey')
if testAdjoint:
def test_adjointJvecVsJtvec(self):
mesh = self.mesh
prb = self.prb
m0 = self.m
def adjointJvecVsJtvec(self, rxcomp='bz'):
print ' \n Testing Adjoint %s' %rxcomp
mesh, prb, m0 = setUp(rxcomp)
m = np.random.rand(prb.mapping.nP)
d = np.random.rand(prb.survey.nD)
@@ -77,6 +87,15 @@ class TDEM_bDerivTests(unittest.TestCase):
print ' ', V1, V2, np.abs(V1-V2), tol, passed
self.assertTrue(passed)
def test_JvecVsJtvec_bx(self):
self.adjointJvecVsJtvec('bx')
def test_JvecVsJtvec_bz(self):
self.adjointJvecVsJtvec('bz')
def test_JvecVsJtvec_ey(self):
self.adjointJvecVsJtvec('ey')
if __name__ == '__main__':