Added tests for G.

This commit is contained in:
Dave Marchant
2014-02-12 11:36:49 -08:00
parent d4a316b764
commit ffb87aafa7
2 changed files with 55 additions and 11 deletions
+42
View File
@@ -39,6 +39,34 @@ class TDEM_bTests(unittest.TestCase):
diff = np.linalg.norm(bz_calc.flatten() - bz_ana.flatten())/np.linalg.norm(bz_ana.flatten())
self.assertTrue(diff<0.05)
class TDEM_bDerivTests(unittest.TestCase):
def setUp(self):
cs = 5.
ncx = 20
ncy = 6
npad = 20
hx = Utils.meshTensors(((0,cs), (ncx,cs), (npad,cs)))
hy = Utils.meshTensors(((npad,cs), (ncy,cs), (npad,cs)))
mesh = Mesh.Cyl1DMesh([hx,hy], -hy.sum()/2)
model = Model.Vertical1DModel(mesh)
opts = {'txLoc':0.,
'txType':'VMD_MVP',
'rxLoc':np.r_[150., 0.],
'rxType':'bz',
'timeCh':np.logspace(-4,-2,20),
}
self.dat = EM.TDEM.DataTDEM1D(**opts)
self.prb = EM.TDEM.ProblemTDEM_b(mesh, model)
self.prb.setTimes([1e-5, 5e-5, 2.5e-4], [10, 10, 10])
self.sigma = np.ones(mesh.nCz)*1e-8
self.sigma[mesh.vectorCCz<0] = 0.1
self.prb.pair(self.dat)
def test_AhVec(self):
"""
Test that fields and AhVec produce consistent results
@@ -55,7 +83,21 @@ class TDEM_bTests(unittest.TestCase):
self.assertTrue(np.linalg.norm(Ahu.get_e(i))/np.linalg.norm(u.get_e(i)) < 1.e-2)
def test_DerivG(self):
"""
Test the derivative of c with respect to sigma
"""
# Random model and perturbation
sigma = np.random.rand(self.prb.mesh.nCz)
f = self.prb.fields(sigma)
dm = np.random.rand(self.prb.mesh.nCz)
h = 1.
a = np.linalg.norm(self.prb.AhVec(sigma+h*dm, f).fieldVec() - self.prb.AhVec(sigma, f).fieldVec())
b = np.linalg.norm(self.prb.AhVec(sigma+h*dm, f).fieldVec() - self.prb.AhVec(sigma, f).fieldVec() - h*self.prb.G(sigma, dm, u=f).fieldVec())
# Assuming that the gradient is exact to machine precision
self.assertTrue(b<1e-16)
if __name__ == '__main__':