mirror of
https://github.com/wassname/simpeg.git
synced 2026-07-25 13:30:06 +08:00
Separate tests into folders.
Build in a matrix?
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
if __name__ == '__main__':
|
||||
import os
|
||||
import glob
|
||||
import unittest
|
||||
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,380 @@
|
||||
import unittest
|
||||
from SimPEG import *
|
||||
|
||||
|
||||
class FieldsTest(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
mesh = Mesh.TensorMesh([np.ones(n)*5 for n in [10,11,12]],[0,0,-30])
|
||||
x = np.linspace(5,10,3)
|
||||
XYZ = Utils.ndgrid(x,x,np.r_[0.])
|
||||
srcLoc = np.r_[0,0,0.]
|
||||
rxList0 = Survey.BaseRx(XYZ, 'exi')
|
||||
Src0 = Survey.BaseSrc([rxList0], loc=srcLoc)
|
||||
rxList1 = Survey.BaseRx(XYZ, 'bxi')
|
||||
Src1 = Survey.BaseSrc([rxList1], loc=srcLoc)
|
||||
rxList2 = Survey.BaseRx(XYZ, 'bxi')
|
||||
Src2 = Survey.BaseSrc([rxList2], loc=srcLoc)
|
||||
rxList3 = Survey.BaseRx(XYZ, 'bxi')
|
||||
Src3 = Survey.BaseSrc([rxList3], loc=srcLoc)
|
||||
Src4 = Survey.BaseSrc([rxList0, rxList1, rxList2, rxList3], loc=srcLoc)
|
||||
srcList = [Src0,Src1,Src2,Src3,Src4]
|
||||
survey = Survey.BaseSurvey(srcList=srcList)
|
||||
self.D = Survey.Data(survey)
|
||||
self.F = Problem.Fields(mesh, survey, knownFields={'phi':'CC','e':'E','b':'F'}, dtype={"phi":float,"e":complex,"b":complex})
|
||||
self.Src0 = Src0
|
||||
self.Src1 = Src1
|
||||
self.mesh = mesh
|
||||
self.XYZ = XYZ
|
||||
|
||||
def test_contains(self):
|
||||
F = self.F
|
||||
nSrc = F.survey.nSrc
|
||||
self.assertTrue('b' not in F)
|
||||
self.assertTrue('e' not in F)
|
||||
e = np.random.rand(F.mesh.nE, nSrc)
|
||||
F[:, 'e'] = e
|
||||
self.assertTrue('b' not in F)
|
||||
self.assertTrue('e' in F)
|
||||
|
||||
def test_overlappingFields(self):
|
||||
self.assertRaises(AssertionError, Problem.Fields, self.F.mesh, self.F.survey,
|
||||
knownFields={'b':'F'},
|
||||
aliasFields={'b':['b',(lambda F, b, ind: b)]})
|
||||
|
||||
def test_SetGet(self):
|
||||
F = self.F
|
||||
nSrc = F.survey.nSrc
|
||||
e = np.random.rand(F.mesh.nE, nSrc) + np.random.rand(F.mesh.nE, nSrc)*1j
|
||||
F[:, 'e'] = e
|
||||
b = np.random.rand(F.mesh.nF, nSrc) + np.random.rand(F.mesh.nF, nSrc)*1j
|
||||
F[:, 'b'] = b
|
||||
|
||||
self.assertTrue(np.all(F[:, 'e'] == e))
|
||||
self.assertTrue(np.all(F[:, 'b'] == b))
|
||||
F[:] = {'b':b,'e':e}
|
||||
self.assertTrue(np.all(F[:, 'e'] == e))
|
||||
self.assertTrue(np.all(F[:, 'b'] == b))
|
||||
|
||||
for s in [0,0.0,np.r_[0],long(0)]:
|
||||
F[:, 'b'] = s
|
||||
self.assertTrue(np.all(F[:, 'b'] == b*0))
|
||||
|
||||
b = np.random.rand(F.mesh.nF,1)
|
||||
F[self.Src0, 'b'] = b
|
||||
self.assertTrue(np.all(F[self.Src0, 'b'] == b))
|
||||
|
||||
b = np.random.rand(F.mesh.nF,1)
|
||||
F[self.Src0, 'b'] = b
|
||||
self.assertTrue(np.all(F[self.Src0, 'b'] == b))
|
||||
|
||||
phi = np.random.rand(F.mesh.nC,2)
|
||||
F[[self.Src0,self.Src1], 'phi'] = phi
|
||||
self.assertTrue(np.all(F[[self.Src0,self.Src1], 'phi'] == phi))
|
||||
|
||||
fdict = F[:,:]
|
||||
self.assertTrue(type(fdict) is dict)
|
||||
self.assertTrue(sorted([k for k in fdict]) == ['b','e','phi'])
|
||||
|
||||
b = np.random.rand(F.mesh.nF, 2)
|
||||
F[[self.Src0, self.Src1],'b'] = b
|
||||
self.assertTrue(F[self.Src0]['b'].shape == (F.mesh.nF,1))
|
||||
self.assertTrue(F[self.Src0,'b'].shape == (F.mesh.nF,1))
|
||||
self.assertTrue(np.all(F[self.Src0,'b'] == Utils.mkvc(b[:,0],2)))
|
||||
self.assertTrue(np.all(F[self.Src1,'b'] == Utils.mkvc(b[:,1],2)))
|
||||
|
||||
def test_assertions(self):
|
||||
freq = [self.Src0, self.Src1]
|
||||
bWrongSize = np.random.rand(self.F.mesh.nE, self.F.survey.nSrc)
|
||||
def fun(): self.F[freq, 'b'] = bWrongSize
|
||||
self.assertRaises(ValueError, fun)
|
||||
def fun(): self.F[-999.]
|
||||
self.assertRaises(KeyError, fun)
|
||||
def fun(): self.F['notRight']
|
||||
self.assertRaises(KeyError, fun)
|
||||
def fun(): self.F[freq,'notThere']
|
||||
self.assertRaises(KeyError, fun)
|
||||
|
||||
|
||||
class FieldsTest_Alias(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
mesh = Mesh.TensorMesh([np.ones(n)*5 for n in [10,11,12]],[0,0,-30])
|
||||
x = np.linspace(5,10,3)
|
||||
XYZ = Utils.ndgrid(x,x,np.r_[0.])
|
||||
srcLoc = np.r_[0,0,0.]
|
||||
rxList0 = Survey.BaseRx(XYZ, 'exi')
|
||||
Src0 = Survey.BaseSrc([rxList0],loc=srcLoc)
|
||||
rxList1 = Survey.BaseRx(XYZ, 'bxi')
|
||||
Src1 = Survey.BaseSrc([rxList1],loc=srcLoc)
|
||||
rxList2 = Survey.BaseRx(XYZ, 'bxi')
|
||||
Src2 = Survey.BaseSrc([rxList2],loc=srcLoc)
|
||||
rxList3 = Survey.BaseRx(XYZ, 'bxi')
|
||||
Src3 = Survey.BaseSrc([rxList3],loc=srcLoc)
|
||||
Src4 = Survey.BaseSrc([rxList0, rxList1, rxList2, rxList3],loc=srcLoc)
|
||||
srcList = [Src0,Src1,Src2,Src3,Src4]
|
||||
survey = Survey.BaseSurvey(srcList=srcList)
|
||||
self.F = Problem.Fields(mesh, survey, knownFields={'e':'E'}, aliasFields={'b':['e','F',(lambda e, ind: self.F.mesh.edgeCurl * e)]})
|
||||
self.Src0 = Src0
|
||||
self.Src1 = Src1
|
||||
self.mesh = mesh
|
||||
self.XYZ = XYZ
|
||||
|
||||
def test_contains(self):
|
||||
F = self.F
|
||||
nSrc = F.survey.nSrc
|
||||
self.assertTrue('b' not in F)
|
||||
self.assertTrue('e' not in F)
|
||||
e = np.random.rand(F.mesh.nE, nSrc)
|
||||
F[:, 'e'] = e
|
||||
self.assertTrue('b' in F)
|
||||
self.assertTrue('e' in F)
|
||||
|
||||
def test_simpleAlias(self):
|
||||
F = self.F
|
||||
nSrc = F.survey.nSrc
|
||||
e = np.random.rand(F.mesh.nE, nSrc)
|
||||
F[:, 'e'] = e
|
||||
self.assertTrue(np.all(F[:, 'b'] == F.mesh.edgeCurl * e ))
|
||||
|
||||
e = np.random.rand(F.mesh.nE,1)
|
||||
F[self.Src0, 'e'] = e
|
||||
self.assertTrue(np.all(F[self.Src0, 'b'] == F.mesh.edgeCurl * e))
|
||||
|
||||
def f():
|
||||
F[self.Src0, 'b'] = F[self.Src0, 'b']
|
||||
self.assertRaises(KeyError, f) # can't set a alias attr.
|
||||
|
||||
def test_aliasFunction(self):
|
||||
def alias(e, ind):
|
||||
self.assertTrue(ind[0] is self.Src0)
|
||||
return self.F.mesh.edgeCurl * e
|
||||
F = Problem.Fields(self.F.mesh, self.F.survey, knownFields={'e':'E'}, aliasFields={'b':['e','F',alias]})
|
||||
e = np.random.rand(F.mesh.nE,1)
|
||||
F[self.Src0, 'e'] = e
|
||||
F[self.Src0, 'b']
|
||||
|
||||
|
||||
def alias(e, ind):
|
||||
self.assertTrue(type(ind) is list)
|
||||
self.assertTrue(ind[0] is self.Src0)
|
||||
self.assertTrue(ind[1] is self.Src1)
|
||||
return self.F.mesh.edgeCurl * e
|
||||
F = Problem.Fields(self.F.mesh, self.F.survey, knownFields={'e':'E'}, aliasFields={'b':['e','F',alias]})
|
||||
e = np.random.rand(F.mesh.nE,2)
|
||||
F[[self.Src0, self.Src1], 'e'] = e
|
||||
F[[self.Src0, self.Src1], 'b']
|
||||
|
||||
|
||||
class FieldsTest_Time(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
mesh = Mesh.TensorMesh([np.ones(n)*5 for n in [10,11,12]],[0,0,-30])
|
||||
x = np.linspace(5,10,3)
|
||||
XYZ = Utils.ndgrid(x,x,np.r_[0.])
|
||||
srcLoc = np.r_[0,0,0.]
|
||||
rxList0 = Survey.BaseRx(XYZ, 'exi')
|
||||
Src0 = Survey.BaseSrc([rxList0], loc=srcLoc)
|
||||
rxList1 = Survey.BaseRx(XYZ, 'bxi')
|
||||
Src1 = Survey.BaseSrc([rxList1], loc=srcLoc)
|
||||
rxList2 = Survey.BaseRx(XYZ, 'bxi')
|
||||
Src2 = Survey.BaseSrc([rxList2], loc=srcLoc)
|
||||
rxList3 = Survey.BaseRx(XYZ, 'bxi')
|
||||
Src3 = Survey.BaseSrc([rxList3], loc=srcLoc)
|
||||
Src4 = Survey.BaseSrc([rxList0, rxList1, rxList2, rxList3], loc=srcLoc)
|
||||
srcList = [Src0,Src1,Src2,Src3,Src4]
|
||||
survey = Survey.BaseSurvey(srcList=srcList)
|
||||
prob = Problem.BaseTimeProblem(mesh, timeSteps=[(10.,3), (20.,2)])
|
||||
survey.pair(prob)
|
||||
self.F = Problem.TimeFields(mesh, survey, knownFields={'phi':'CC','e':'E','b':'F'})
|
||||
self.Src0 = Src0
|
||||
self.Src1 = Src1
|
||||
self.mesh = mesh
|
||||
self.XYZ = XYZ
|
||||
|
||||
def test_contains(self):
|
||||
F = self.F
|
||||
nSrc = F.survey.nSrc
|
||||
nT = F.survey.prob.nT + 1
|
||||
self.assertTrue('b' not in F)
|
||||
self.assertTrue('e' not in F)
|
||||
self.assertTrue('phi' not in F)
|
||||
e = np.random.rand(F.mesh.nE, nSrc, nT)
|
||||
F[:, 'e', :] = e
|
||||
self.assertTrue('e' in F)
|
||||
self.assertTrue('b' not in F)
|
||||
self.assertTrue('phi' not in F)
|
||||
|
||||
def test_SetGet(self):
|
||||
F = self.F
|
||||
nSrc = F.survey.nSrc
|
||||
nT = F.survey.prob.nT + 1
|
||||
e = np.random.rand(F.mesh.nE, nSrc, nT)
|
||||
F[:, 'e'] = e
|
||||
b = np.random.rand(F.mesh.nF, nSrc, nT)
|
||||
F[:, 'b'] = b
|
||||
|
||||
self.assertTrue(np.all(F[:, 'e'] == e))
|
||||
self.assertTrue(np.all(F[:, 'b'] == b))
|
||||
F[:] = {'b':b,'e':e}
|
||||
self.assertTrue(np.all(F[:, 'e'] == e))
|
||||
self.assertTrue(np.all(F[:, 'b'] == b))
|
||||
|
||||
for s in [0,0.0,np.r_[0],long(0)]:
|
||||
F[:, 'b'] = s
|
||||
self.assertTrue(np.all(F[:, 'b'] == b*0))
|
||||
|
||||
b = np.random.rand(F.mesh.nF,1,nT)
|
||||
F[self.Src0, 'b'] = b
|
||||
self.assertTrue(np.all(F[self.Src0, 'b'] == b[:,0,:]))
|
||||
|
||||
b = np.random.rand(F.mesh.nF,1,nT)
|
||||
F[self.Src0, 'b', 0] = b[:,:,0]
|
||||
self.assertTrue(np.all(F[self.Src0, 'b', 0] == Utils.mkvc(b[:,0,0],2)))
|
||||
|
||||
phi = np.random.rand(F.mesh.nC,2,nT)
|
||||
F[[self.Src0,self.Src1], 'phi'] = phi
|
||||
self.assertTrue(np.all(F[[self.Src0,self.Src1], 'phi'] == phi))
|
||||
|
||||
fdict = F[:]
|
||||
self.assertTrue(type(fdict) is dict)
|
||||
self.assertTrue(sorted([k for k in fdict]) == ['b','e','phi'])
|
||||
|
||||
b = np.random.rand(F.mesh.nF, 2, nT)
|
||||
F[[self.Src0, self.Src1],'b'] = b
|
||||
self.assertTrue(F[self.Src0]['b'].shape == (F.mesh.nF,nT))
|
||||
self.assertTrue(F[self.Src0,'b'].shape == (F.mesh.nF,nT))
|
||||
self.assertTrue(np.all(F[self.Src0,'b'] == b[:,0,:]))
|
||||
self.assertTrue(np.all(F[self.Src1,'b'] == b[:,1,:]))
|
||||
self.assertTrue(np.all(F[self.Src0,'b',1] == Utils.mkvc(b[:,0,1],2)))
|
||||
self.assertTrue(np.all(F[self.Src1,'b',1] == Utils.mkvc(b[:,1,1],2)))
|
||||
self.assertTrue(np.all(F[self.Src0,'b',4] == Utils.mkvc(b[:,0,4],2)))
|
||||
self.assertTrue(np.all(F[self.Src1,'b',4] == Utils.mkvc(b[:,1,4],2)))
|
||||
|
||||
|
||||
b = np.random.rand(F.mesh.nF, 2, nT)
|
||||
F[[self.Src0, self.Src1],'b', 0] = b[:,:,0]
|
||||
|
||||
def test_assertions(self):
|
||||
freq = [self.Src0, self.Src1]
|
||||
bWrongSize = np.random.rand(self.F.mesh.nE, self.F.survey.nSrc)
|
||||
def fun(): self.F[freq, 'b'] = bWrongSize
|
||||
self.assertRaises(ValueError, fun)
|
||||
def fun(): self.F[-999.]
|
||||
self.assertRaises(KeyError, fun)
|
||||
def fun(): self.F['notRight']
|
||||
self.assertRaises(KeyError, fun)
|
||||
def fun(): self.F[freq,'notThere']
|
||||
self.assertRaises(KeyError, fun)
|
||||
|
||||
|
||||
class FieldsTest_Time_Aliased(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
mesh = Mesh.TensorMesh([np.ones(n)*5 for n in [10,11,12]],[0,0,-30])
|
||||
x = np.linspace(5,10,3)
|
||||
XYZ = Utils.ndgrid(x,x,np.r_[0.])
|
||||
srcLoc = np.r_[0,0,0.]
|
||||
rxList0 = Survey.BaseRx(XYZ, 'exi')
|
||||
Src0 = Survey.BaseSrc( [rxList0],loc=srcLoc)
|
||||
rxList1 = Survey.BaseRx(XYZ, 'bxi')
|
||||
Src1 = Survey.BaseSrc( [rxList1],loc=srcLoc)
|
||||
rxList2 = Survey.BaseRx(XYZ, 'bxi')
|
||||
Src2 = Survey.BaseSrc( [rxList2],loc=srcLoc)
|
||||
rxList3 = Survey.BaseRx(XYZ, 'bxi')
|
||||
Src3 = Survey.BaseSrc( [rxList3],loc=srcLoc)
|
||||
Src4 = Survey.BaseSrc( [rxList0, rxList1, rxList2, rxList3],loc=srcLoc)
|
||||
srcList = [Src0,Src1,Src2,Src3,Src4]
|
||||
survey = Survey.BaseSurvey(srcList=srcList)
|
||||
prob = Problem.BaseTimeProblem(mesh, timeSteps=[(10.,3), (20.,2)])
|
||||
survey.pair(prob)
|
||||
def alias(b, srcInd, timeInd):
|
||||
return self.F.mesh.edgeCurl.T * b + timeInd
|
||||
self.F = Problem.TimeFields(mesh, survey, knownFields={'b':'F'}, aliasFields={'e':['b','E',alias]})
|
||||
self.Src0 = Src0
|
||||
self.Src1 = Src1
|
||||
self.mesh = mesh
|
||||
self.XYZ = XYZ
|
||||
|
||||
def test_contains(self):
|
||||
F = self.F
|
||||
nSrc = F.survey.nSrc
|
||||
nT = F.survey.prob.nT + 1
|
||||
self.assertTrue('b' not in F)
|
||||
self.assertTrue('e' not in F)
|
||||
b = np.random.rand(F.mesh.nF, nSrc, nT)
|
||||
F[:, 'b', :] = b
|
||||
self.assertTrue('e' in F)
|
||||
self.assertTrue('b' in F)
|
||||
|
||||
|
||||
def test_simpleAlias(self):
|
||||
F = self.F
|
||||
nSrc = F.survey.nSrc
|
||||
nT = F.survey.prob.nT + 1
|
||||
b = np.random.rand(F.mesh.nF, nSrc, nT)
|
||||
F[:, 'b', :] = b
|
||||
self.assertTrue(np.all(F[:, 'e', 0] == F.mesh.edgeCurl.T * b[:,:,0] ))
|
||||
|
||||
e = range(nT)
|
||||
for i in range(nT):
|
||||
e[i] = F.mesh.edgeCurl.T*b[:,:,i] + i
|
||||
e[i] = e[i][:,:,np.newaxis]
|
||||
e = np.concatenate(e, axis=2)
|
||||
self.assertTrue(np.all(F[:, 'e', :] == e ))
|
||||
self.assertTrue(np.all(F[self.Src0, 'e', :] == e[:,0,:] ))
|
||||
self.assertTrue(np.all(F[self.Src1, 'e', :] == e[:,1,:] ))
|
||||
for t in range(nT):
|
||||
self.assertTrue(np.all(F[self.Src1, 'e', t] == Utils.mkvc(e[:,1,t],2) ))
|
||||
|
||||
b = np.random.rand(F.mesh.nF,nT)
|
||||
F[self.Src0, 'b',:] = b
|
||||
Cb = F.mesh.edgeCurl.T * b
|
||||
for i in range(Cb.shape[1]):
|
||||
Cb[:,i] += i
|
||||
self.assertTrue(np.all(F[self.Src0, 'e',:] == Cb))
|
||||
|
||||
def f():
|
||||
F[self.Src0, 'e'] = F[self.Src0, 'e']
|
||||
self.assertRaises(KeyError, f) # can't set a alias attr.
|
||||
|
||||
def test_aliasFunction(self):
|
||||
nT = self.F.survey.prob.nT + 1
|
||||
count = [0]
|
||||
def alias(e, srcInd, timeInd):
|
||||
count[0] += 1
|
||||
self.assertTrue(srcInd[0] is self.Src0)
|
||||
return self.F.mesh.edgeCurl * e
|
||||
F = Problem.TimeFields(self.F.mesh, self.F.survey, knownFields={'e':'E'}, aliasFields={'b':['e','F',alias]})
|
||||
e = np.random.rand(F.mesh.nE,1,nT)
|
||||
F[self.Src0, 'e', :] = e
|
||||
F[self.Src0, 'b', :]
|
||||
self.assertTrue(count[0] == nT) # ensure that this is called for every time separately.
|
||||
e = np.random.rand(F.mesh.nE,1,1)
|
||||
F[self.Src0, 'e', 1] = e
|
||||
count[0] = 0
|
||||
F[self.Src0, 'b', 1]
|
||||
self.assertTrue(count[0] == 1) # ensure that this is called only once.
|
||||
|
||||
|
||||
def alias(e, srcInd, timeInd):
|
||||
count[0] += 1
|
||||
self.assertTrue(type(srcInd) is list)
|
||||
self.assertTrue(srcInd[0] is self.Src0)
|
||||
self.assertTrue(srcInd[1] is self.Src1)
|
||||
return self.F.mesh.edgeCurl * e
|
||||
F = Problem.TimeFields(self.F.mesh, self.F.survey, knownFields={'e':'E'}, aliasFields={'b':['e','F',alias]})
|
||||
e = np.random.rand(F.mesh.nE,2, nT)
|
||||
F[[self.Src0, self.Src1], 'e', :] = e
|
||||
count[0] = 0
|
||||
F[[self.Src0, self.Src1], 'b', :]
|
||||
self.assertTrue(count[0] == nT) # ensure that this is called for every time separately.
|
||||
e = np.random.rand(F.mesh.nE,2, 1)
|
||||
F[[self.Src0, self.Src1], 'e', 1] = e
|
||||
count[0] = 0
|
||||
F[[self.Src0, self.Src1], 'b', 1]
|
||||
self.assertTrue(count[0] == 1) # ensure that this is called only once.
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -0,0 +1,193 @@
|
||||
import unittest
|
||||
from SimPEG import *
|
||||
from scipy.constants import mu_0
|
||||
|
||||
|
||||
class MyPropMap(Maps.PropMap):
|
||||
sigma = Maps.Property("Electrical Conductivity", defaultInvProp=True)
|
||||
mu = Maps.Property("Mu", defaultVal=mu_0)
|
||||
|
||||
class MyReciprocalPropMap(Maps.PropMap):
|
||||
sigma = Maps.Property("Electrical Conductivity", defaultInvProp=True, propertyLink=('rho', Maps.ReciprocalMap))
|
||||
rho = Maps.Property("Electrical Resistivity", propertyLink=('sigma', Maps.ReciprocalMap))
|
||||
mu = Maps.Property("Mu", defaultVal=mu_0, propertyLink=('mui', Maps.ReciprocalMap))
|
||||
mui = Maps.Property("Mu", defaultVal=1./mu_0, propertyLink=('mu', Maps.ReciprocalMap))
|
||||
|
||||
|
||||
class TestPropMaps(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def test_setup(self):
|
||||
expMap = Maps.ExpMap(Mesh.TensorMesh((3,)))
|
||||
assert expMap.nP == 3
|
||||
|
||||
PM1 = MyPropMap(expMap)
|
||||
PM2 = MyPropMap([('sigma', expMap)])
|
||||
PM3 = MyPropMap({'maps':[('sigma', expMap)], 'slices':{'sigma':slice(0,3)}})
|
||||
|
||||
for PM in [PM1,PM2,PM3]:
|
||||
assert PM.defaultInvProp == 'sigma'
|
||||
assert PM.sigmaMap is not None
|
||||
assert PM.sigmaMap is expMap
|
||||
assert PM.sigmaIndex == slice(0,3)
|
||||
assert getattr(PM, 'sigma', None) is None
|
||||
assert PM.muMap is None
|
||||
assert PM.muIndex is None
|
||||
|
||||
assert 'sigma' in PM
|
||||
assert 'mu' not in PM
|
||||
assert 'mui' not in PM
|
||||
|
||||
m = PM(np.r_[1.,2,3])
|
||||
|
||||
assert 'sigma' in m
|
||||
assert 'mu' not in m
|
||||
assert 'mui' not in m
|
||||
|
||||
assert m.mu == mu_0
|
||||
assert m.muModel is None
|
||||
assert m.muMap is None
|
||||
assert m.muDeriv is None
|
||||
|
||||
assert np.all(m.sigmaModel == np.r_[1.,2,3])
|
||||
assert m.sigmaMap is expMap
|
||||
assert np.all(m.sigma == np.exp(np.r_[1.,2,3]))
|
||||
assert m.sigmaDeriv is not None
|
||||
|
||||
assert m.nP == 3
|
||||
|
||||
def test_slices(self):
|
||||
expMap = Maps.ExpMap(Mesh.TensorMesh((3,)))
|
||||
PM = MyPropMap({'maps':[('sigma', expMap)], 'slices':{'sigma':[2,1,0]}})
|
||||
assert PM.sigmaIndex == [2,1,0]
|
||||
m = PM(np.r_[1.,2,3])
|
||||
assert np.all(m.sigmaModel == np.r_[3,2,1])
|
||||
assert np.all(m.sigma == np.exp(np.r_[3,2,1]))
|
||||
|
||||
def test_multiMap(self):
|
||||
m = Mesh.TensorMesh((3,))
|
||||
expMap = Maps.ExpMap(m)
|
||||
iMap = Maps.IdentityMap(m)
|
||||
PM = MyPropMap([('sigma', expMap), ('mu', iMap)])
|
||||
|
||||
pm = PM(np.r_[1.,2,3,4,5,6])
|
||||
|
||||
assert pm.nP == 6
|
||||
|
||||
assert 'sigma' in PM
|
||||
assert 'mu' in PM
|
||||
assert 'mui' not in PM
|
||||
|
||||
assert 'sigma' in pm
|
||||
assert 'mu' in pm
|
||||
assert 'mui' not in pm
|
||||
|
||||
assert np.all(pm.sigmaModel == [1.,2,3])
|
||||
assert np.all(pm.sigma == np.exp([1.,2,3]))
|
||||
assert np.all(pm.muModel == [4.,5,6])
|
||||
assert np.all(pm.mu == [4.,5,6])
|
||||
|
||||
|
||||
def test_multiMapCompressed(self):
|
||||
m = Mesh.TensorMesh((3,))
|
||||
expMap = Maps.ExpMap(m)
|
||||
iMap = Maps.IdentityMap(m)
|
||||
PM = MyPropMap({'maps':[('sigma', expMap), ('mu', iMap)],'slices':{'mu':[0,1,2]}})
|
||||
|
||||
pm = PM(np.r_[1,2.,3])
|
||||
|
||||
assert pm.nP == 3
|
||||
|
||||
assert 'sigma' in PM
|
||||
assert 'mu' in PM
|
||||
assert 'mui' not in PM
|
||||
|
||||
assert 'sigma' in pm
|
||||
assert 'mu' in pm
|
||||
assert 'mui' not in pm
|
||||
|
||||
assert np.all(pm.sigmaModel == [1,2,3])
|
||||
assert np.all(pm.sigma == np.exp([1,2,3]))
|
||||
assert np.all(pm.muModel == [1,2,3])
|
||||
assert np.all(pm.mu == [1,2,3])
|
||||
|
||||
def test_Projections(self):
|
||||
m = Mesh.TensorMesh((3,))
|
||||
iMap = Maps.IdentityMap(m)
|
||||
PM = MyReciprocalPropMap([('sigma', iMap)])
|
||||
v = np.r_[1,2.,3]
|
||||
pm = PM(v)
|
||||
|
||||
assert pm.sigmaProj is not None
|
||||
assert pm.rhoProj is None
|
||||
assert pm.muProj is None
|
||||
assert pm.muiProj is None
|
||||
|
||||
assert np.all(pm.sigmaProj * v == pm.sigmaModel)
|
||||
|
||||
def test_Links(self):
|
||||
m = Mesh.TensorMesh((3,))
|
||||
expMap = Maps.ExpMap(m)
|
||||
iMap = Maps.IdentityMap(m)
|
||||
PM = MyReciprocalPropMap([('sigma', iMap)])
|
||||
pm = PM(np.r_[1,2.,3])
|
||||
# print pm.sigma
|
||||
# print pm.sigmaMap
|
||||
assert np.all(pm.sigma == [1,2,3])
|
||||
assert np.all(pm.rho == 1./np.r_[1,2,3])
|
||||
assert pm.sigmaMap is iMap
|
||||
assert pm.rhoMap is None
|
||||
assert pm.sigmaDeriv is not None
|
||||
assert pm.rhoDeriv is not None
|
||||
|
||||
assert 'sigma' in PM
|
||||
assert 'rho' not in PM
|
||||
assert 'mu' not in PM
|
||||
assert 'mui' not in PM
|
||||
|
||||
|
||||
assert 'sigma' in pm
|
||||
assert 'rho' not in pm
|
||||
assert 'mu' not in pm
|
||||
assert 'mui' not in pm
|
||||
|
||||
assert pm.mu == mu_0
|
||||
assert pm.mui == 1.0/mu_0
|
||||
assert pm.muMap is None
|
||||
assert pm.muDeriv is None
|
||||
assert pm.muiMap is None
|
||||
assert pm.muiDeriv is None
|
||||
|
||||
PM = MyReciprocalPropMap([('rho', iMap)])
|
||||
pm = PM(np.r_[1,2.,3])
|
||||
# print pm.sigma
|
||||
# print pm.sigmaMap
|
||||
assert np.all(pm.sigma == 1./np.r_[1,2,3])
|
||||
assert np.all(pm.rho == [1,2,3])
|
||||
assert pm.sigmaMap is None
|
||||
assert pm.rhoMap is iMap
|
||||
assert pm.sigmaDeriv is not None
|
||||
assert pm.rhoDeriv is not None
|
||||
|
||||
assert 'sigma' not in PM
|
||||
assert 'rho' in PM
|
||||
assert 'mu' not in PM
|
||||
assert 'mui' not in PM
|
||||
|
||||
|
||||
assert 'sigma' not in pm
|
||||
assert 'rho' in pm
|
||||
assert 'mu' not in pm
|
||||
assert 'mui' not in pm
|
||||
|
||||
self.assertRaises(AssertionError, MyReciprocalPropMap, [('rho', iMap), ('sigma', iMap)])
|
||||
self.assertRaises(AssertionError, MyReciprocalPropMap, [('sigma', iMap), ('rho', iMap)])
|
||||
|
||||
MyReciprocalPropMap([('sigma', iMap), ('mu', iMap)]) # This should be fine
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
import unittest
|
||||
from SimPEG import *
|
||||
from SimPEG.Mesh import TensorMesh
|
||||
from SimPEG.Utils import sdiag
|
||||
import numpy as np
|
||||
import scipy.sparse as sparse
|
||||
|
||||
TOLD = 1e-10
|
||||
TOLI = 1e-3
|
||||
numRHS = 5
|
||||
|
||||
def dotest(MYSOLVER, multi=False, A=None, **solverOpts):
|
||||
if A is None:
|
||||
h1 = np.ones(10)*100.
|
||||
h2 = np.ones(10)*100.
|
||||
h3 = np.ones(10)*100.
|
||||
|
||||
h = [h1,h2,h3]
|
||||
|
||||
M = TensorMesh(h)
|
||||
|
||||
D = M.faceDiv
|
||||
G = -M.faceDiv.T
|
||||
Msig = M.getFaceInnerProduct()
|
||||
A = D*Msig*G
|
||||
A[-1,-1] *= 1/M.vol[-1] # remove the constant null space from the matrix
|
||||
else:
|
||||
M = Mesh.TensorMesh([A.shape[0]])
|
||||
|
||||
Ainv = MYSOLVER(A, **solverOpts)
|
||||
if multi:
|
||||
e = np.ones(M.nC)
|
||||
else:
|
||||
e = np.ones((M.nC, numRHS))
|
||||
rhs = A * e
|
||||
x = Ainv * rhs
|
||||
Ainv.clean()
|
||||
return np.linalg.norm(e-x,np.inf)
|
||||
|
||||
class TestSolver(unittest.TestCase):
|
||||
|
||||
def test_direct_spsolve_1(self): self.assertLess(dotest(Solver, False),TOLD)
|
||||
def test_direct_spsolve_M(self): self.assertLess(dotest(Solver, True),TOLD)
|
||||
|
||||
def test_direct_splu_1(self): self.assertLess(dotest(SolverLU, False),TOLD)
|
||||
def test_direct_splu_M(self): self.assertLess(dotest(SolverLU, True),TOLD)
|
||||
|
||||
def test_iterative_diag_1(self): self.assertLess(dotest(SolverDiag, False, A=Utils.sdiag(np.random.rand(10)+1.0)),TOLI)
|
||||
def test_iterative_diag_M(self): self.assertLess(dotest(SolverDiag, True, A=Utils.sdiag(np.random.rand(10)+1.0)),TOLI)
|
||||
|
||||
def test_iterative_cg_1(self): self.assertLess(dotest(SolverCG, False),TOLI)
|
||||
def test_iterative_cg_M(self): self.assertLess(dotest(SolverCG, True),TOLI)
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -0,0 +1,53 @@
|
||||
import unittest
|
||||
from SimPEG import *
|
||||
|
||||
class TestData(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
mesh = Mesh.TensorMesh([np.ones(n)*5 for n in [10,11,12]],[0,0,-30])
|
||||
x = np.linspace(5,10,3)
|
||||
XYZ = Utils.ndgrid(x,x,np.r_[0.])
|
||||
srcLoc = np.r_[0,0,0.]
|
||||
rxList0 = Survey.BaseRx(XYZ, 'exi')
|
||||
Src0 = Survey.BaseSrc([rxList0], loc=srcLoc)
|
||||
rxList1 = Survey.BaseRx(XYZ, 'bxi')
|
||||
Src1 = Survey.BaseSrc([rxList1], loc=srcLoc)
|
||||
rxList2 = Survey.BaseRx(XYZ, 'bxi')
|
||||
Src2 = Survey.BaseSrc([rxList2], loc=srcLoc)
|
||||
rxList3 = Survey.BaseRx(XYZ, 'bxi')
|
||||
Src3 = Survey.BaseSrc([rxList3], loc=srcLoc)
|
||||
Src4 = Survey.BaseSrc([rxList0, rxList1, rxList2, rxList3], loc=srcLoc)
|
||||
srcList = [Src0,Src1,Src2,Src3,Src4]
|
||||
survey = Survey.BaseSurvey(srcList=srcList)
|
||||
self.D = Survey.Data(survey)
|
||||
|
||||
def test_data(self):
|
||||
V = []
|
||||
for src in self.D.survey.srcList:
|
||||
for rx in src.rxList:
|
||||
v = np.random.rand(rx.nD)
|
||||
V += [v]
|
||||
self.D[src, rx] = v
|
||||
self.assertTrue(np.all(v == self.D[src, rx]))
|
||||
V = np.concatenate(V)
|
||||
self.assertTrue(np.all(V == Utils.mkvc(self.D)))
|
||||
|
||||
D2 = Survey.Data(self.D.survey, V)
|
||||
self.assertTrue(np.all(Utils.mkvc(D2) == Utils.mkvc(self.D)))
|
||||
|
||||
def test_uniqueSrcs(self):
|
||||
srcs = self.D.survey.srcList
|
||||
srcs += [srcs[0]]
|
||||
self.assertRaises(AssertionError, Survey.BaseSurvey, srcList=srcs)
|
||||
|
||||
def test_sourceIndex(self):
|
||||
survey = self.D.survey
|
||||
srcs = survey.srcList
|
||||
assert survey.getSourceIndex([srcs[1],srcs[0]]) == [1,0]
|
||||
assert survey.getSourceIndex([srcs[1],srcs[2],srcs[2]]) == [1,2,2]
|
||||
SrcNotThere = Survey.BaseSrc(srcs[0].rxList, loc=np.r_[0,0,0])
|
||||
self.assertRaises(KeyError, survey.getSourceIndex, [SrcNotThere])
|
||||
self.assertRaises(KeyError, survey.getSourceIndex, [srcs[1],srcs[2],SrcNotThere])
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -0,0 +1,144 @@
|
||||
import numpy as np
|
||||
import unittest
|
||||
from SimPEG import *
|
||||
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"]
|
||||
|
||||
class MapTests(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
|
||||
a = np.array([1, 1, 1])
|
||||
b = np.array([1, 2])
|
||||
self.mesh2 = Mesh.TensorMesh([a, b], x0=np.array([3, 5]))
|
||||
self.mesh3 = Mesh.TensorMesh([a, b, [3,4]], x0=np.array([3, 5, 2]))
|
||||
self.mesh22 = Mesh.TensorMesh([b, a], x0=np.array([3, 5]))
|
||||
|
||||
def test_transforms2D(self):
|
||||
for M in MAPS_TO_TEST_2D:
|
||||
maps = getattr(Maps, M)(self.mesh2)
|
||||
self.assertTrue(maps.test())
|
||||
|
||||
def test_transforms3D(self):
|
||||
for M in MAPS_TO_TEST_3D:
|
||||
maps = getattr(Maps, M)(self.mesh3)
|
||||
self.assertTrue(maps.test())
|
||||
|
||||
|
||||
def test_transforms_logMap_reciprocalMap(self):
|
||||
# Note that log/reciprocal maps can be kinda finicky, so we are being explicit about the random seed.
|
||||
v2 = np.r_[ 0.40077291, 0.14410044, 0.58452314, 0.96323738, 0.01198519, 0.79754415]
|
||||
dv2 = np.r_[ 0.80653921, 0.13132446, 0.4901117, 0.03358737, 0.65473762, 0.44252488]
|
||||
v3 = np.r_[ 0.96084865, 0.34385186, 0.39430044, 0.81671285, 0.65929109, 0.2235217, 0.87897526, 0.5784033, 0.96876393, 0.63535864, 0.84130763, 0.22123854]
|
||||
dv3 = np.r_[ 0.96827838, 0.26072111, 0.45090749, 0.10573893, 0.65276365, 0.15646586, 0.51679682, 0.23071984, 0.95106218, 0.14201845, 0.25093564, 0.3732866 ]
|
||||
maps = Maps.LogMap(self.mesh2)
|
||||
self.assertTrue(maps.test(v2, dx=dv2))
|
||||
maps = Maps.LogMap(self.mesh3)
|
||||
self.assertTrue(maps.test(v3, dx=dv3))
|
||||
|
||||
maps = Maps.ReciprocalMap(self.mesh2)
|
||||
self.assertTrue(maps.test(v2, dx=dv2))
|
||||
maps = Maps.ReciprocalMap(self.mesh3)
|
||||
self.assertTrue(maps.test(v3, dx=dv3))
|
||||
|
||||
def test_Mesh2MeshMap(self):
|
||||
maps = Maps.Mesh2Mesh([self.mesh22, self.mesh2])
|
||||
self.assertTrue(maps.test())
|
||||
|
||||
def test_mapMultiplication(self):
|
||||
M = Mesh.TensorMesh([2,3])
|
||||
expMap = Maps.ExpMap(M)
|
||||
vertMap = Maps.Vertical1DMap(M)
|
||||
combo = expMap*vertMap
|
||||
m = np.arange(3.0)
|
||||
t_true = np.exp(np.r_[0,0,1,1,2,2.])
|
||||
self.assertLess(np.linalg.norm((combo * m)-t_true,np.inf),TOL)
|
||||
self.assertLess(np.linalg.norm((expMap * vertMap * m)-t_true,np.inf),TOL)
|
||||
self.assertLess(np.linalg.norm(expMap * (vertMap * m)-t_true,np.inf),TOL)
|
||||
self.assertLess(np.linalg.norm((expMap * vertMap) * m-t_true,np.inf),TOL)
|
||||
#Try making a model
|
||||
mod = Models.Model(m, mapping=combo)
|
||||
# print mod.transform
|
||||
# import matplotlib.pyplot as plt
|
||||
# plt.colorbar(M.plotImage(mod.transform)[0])
|
||||
# plt.show()
|
||||
self.assertLess(np.linalg.norm(mod.transform-t_true,np.inf),TOL)
|
||||
|
||||
self.assertRaises(Exception,Models.Model,np.r_[1.0],mapping=combo)
|
||||
|
||||
self.assertRaises(ValueError, lambda: combo * (vertMap * expMap))
|
||||
self.assertRaises(ValueError, lambda: (combo * vertMap) * expMap)
|
||||
self.assertRaises(ValueError, lambda: vertMap * expMap)
|
||||
self.assertRaises(ValueError, lambda: expMap * np.ones(100))
|
||||
self.assertRaises(ValueError, lambda: expMap * np.ones((100.0,1)))
|
||||
self.assertRaises(ValueError, lambda: expMap * np.ones((100.0,5)))
|
||||
self.assertRaises(ValueError, lambda: combo * np.ones(100))
|
||||
self.assertRaises(ValueError, lambda: combo * np.ones((100.0,1)))
|
||||
self.assertRaises(ValueError, lambda: combo * np.ones((100.0,5)))
|
||||
|
||||
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)
|
||||
|
||||
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)
|
||||
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)
|
||||
self.assertLess(np.linalg.norm(((expMap * vertMap * actMap) * m)-t_true,np.inf),TOL)
|
||||
self.assertLess(np.linalg.norm((expMap * vertMap * (actMap * m))-t_true,np.inf),TOL)
|
||||
self.assertLess(np.linalg.norm((expMap * (vertMap * actMap) * m)-t_true,np.inf),TOL)
|
||||
self.assertLess(np.linalg.norm(((expMap * vertMap) * actMap * m)-t_true,np.inf),TOL)
|
||||
|
||||
self.assertRaises(ValueError, lambda: expMap * actMap * vertMap )
|
||||
self.assertRaises(ValueError, lambda: actMap * vertMap * expMap )
|
||||
|
||||
|
||||
def test_map2Dto3D_x(self):
|
||||
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))
|
||||
|
||||
|
||||
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))
|
||||
|
||||
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))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -0,0 +1,64 @@
|
||||
import unittest
|
||||
from SimPEG import Solver
|
||||
from SimPEG.Mesh import TensorMesh
|
||||
from SimPEG.Utils import sdiag
|
||||
import numpy as np
|
||||
import scipy.sparse as sp
|
||||
from SimPEG import Optimization
|
||||
from SimPEG.Tests import getQuadratic, Rosenbrock
|
||||
|
||||
TOL = 1e-2
|
||||
|
||||
class TestOptimizers(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.A = sp.identity(2).tocsr()
|
||||
self.b = np.array([-5,-5])
|
||||
|
||||
def test_GN_Rosenbrock(self):
|
||||
GN = Optimization.GaussNewton()
|
||||
xopt = GN.minimize(Rosenbrock,np.array([0,0]))
|
||||
x_true = np.array([1.,1.])
|
||||
print 'xopt: ', xopt
|
||||
print 'x_true: ', x_true
|
||||
self.assertTrue(np.linalg.norm(xopt-x_true,2) < TOL, True)
|
||||
|
||||
def test_GN_quadratic(self):
|
||||
GN = Optimization.GaussNewton()
|
||||
xopt = GN.minimize(getQuadratic(self.A,self.b),np.array([0,0]))
|
||||
x_true = np.array([5.,5.])
|
||||
print 'xopt: ', xopt
|
||||
print 'x_true: ', x_true
|
||||
self.assertTrue(np.linalg.norm(xopt-x_true,2) < TOL, True)
|
||||
|
||||
def test_ProjGradient_quadraticBounded(self):
|
||||
PG = Optimization.ProjectedGradient(debug=True)
|
||||
PG.lower, PG.upper = -2, 2
|
||||
xopt = PG.minimize(getQuadratic(self.A,self.b),np.array([0,0]))
|
||||
x_true = np.array([2.,2.])
|
||||
print 'xopt: ', xopt
|
||||
print 'x_true: ', x_true
|
||||
self.assertTrue(np.linalg.norm(xopt-x_true,2) < TOL, True)
|
||||
|
||||
def test_ProjGradient_quadratic1Bound(self):
|
||||
myB = np.array([-5,1])
|
||||
PG = Optimization.ProjectedGradient()
|
||||
PG.lower, PG.upper = -2, 2
|
||||
xopt = PG.minimize(getQuadratic(self.A,myB),np.array([0,0]))
|
||||
x_true = np.array([2.,-1.])
|
||||
print 'xopt: ', xopt
|
||||
print 'x_true: ', x_true
|
||||
self.assertTrue(np.linalg.norm(xopt-x_true,2) < TOL, True)
|
||||
|
||||
def test_NewtonRoot(self):
|
||||
fun = lambda x, return_g=True: np.sin(x) if not return_g else ( np.sin(x), sdiag( np.cos(x) ) )
|
||||
x = np.array([np.pi-0.3, np.pi+0.1, 0])
|
||||
xopt = Optimization.NewtonRoot(comments=False).root(fun,x)
|
||||
x_true = np.array([np.pi,np.pi,0])
|
||||
print 'Newton Root Finding'
|
||||
print 'xopt: ', xopt
|
||||
print 'x_true: ', x_true
|
||||
self.assertTrue(np.linalg.norm(xopt-x_true,2) < TOL, True)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -0,0 +1,25 @@
|
||||
import unittest
|
||||
from SimPEG import *
|
||||
|
||||
|
||||
class TestTimeProblem(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
mesh = Mesh.TensorMesh([10,10])
|
||||
self.prob = Problem.BaseTimeProblem(mesh)
|
||||
|
||||
def test_timeProblem_setTimeSteps(self):
|
||||
self.prob.timeSteps = [(1e-6, 3), 1e-5, (1e-4, 2)]
|
||||
trueTS = np.r_[1e-6,1e-6,1e-6,1e-5,1e-4,1e-4]
|
||||
self.assertTrue(np.all(trueTS == self.prob.timeSteps))
|
||||
|
||||
self.prob.timeSteps = trueTS
|
||||
self.assertTrue(np.all(trueTS == self.prob.timeSteps))
|
||||
|
||||
self.assertTrue(self.prob.nT == 6)
|
||||
|
||||
self.assertTrue(np.all(self.prob.times == np.r_[0,trueTS].cumsum()))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -0,0 +1,34 @@
|
||||
import numpy as np
|
||||
import unittest
|
||||
from SimPEG import *
|
||||
from scipy.sparse.linalg import dsolve
|
||||
import inspect
|
||||
|
||||
|
||||
class RegularizationTests(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.mesh2 = Mesh.TensorMesh([3, 2])
|
||||
|
||||
def test_regularization(self):
|
||||
for R in dir(Regularization):
|
||||
r = getattr(Regularization, R)
|
||||
if not inspect.isclass(r): continue
|
||||
if not issubclass(r, Regularization.BaseRegularization):
|
||||
continue
|
||||
# if 'Regularization' not in R: continue
|
||||
mapping = r.mapPair(self.mesh2)
|
||||
reg = r(self.mesh2, mapping=mapping)
|
||||
m = np.random.rand(mapping.nP)
|
||||
reg.mref = m[:]*np.mean(m)
|
||||
|
||||
print 'Check:', R
|
||||
passed = Tests.checkDerivative(lambda m : [reg.eval(m), reg.evalDeriv(m)], m, plotIt=False)
|
||||
self.assertTrue(passed)
|
||||
print 'Check 2 Deriv:', R
|
||||
passed = Tests.checkDerivative(lambda m : [reg.evalDeriv(m), reg.eval2Deriv(m)], m, plotIt=False)
|
||||
self.assertTrue(passed)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user