mirror of
https://github.com/wassname/simpeg.git
synced 2026-06-27 19:48:52 +08:00
Test Maps and Models
This commit is contained in:
+10
-34
@@ -4,25 +4,20 @@ from Tests import checkDerivative
|
||||
class Model(np.ndarray):
|
||||
|
||||
def __new__(cls, input_array, mapping=None):
|
||||
# Input array is an already formed ndarray instance
|
||||
# We first cast to be our class type
|
||||
assert isinstance(mapping, IdentityMap), 'mapping must be a SimPEG.Mapping'
|
||||
obj = np.asarray(input_array).view(cls)
|
||||
# add the new attribute to the created instance
|
||||
obj.mapping = mapping
|
||||
# Finally, we must return the newly created object:
|
||||
obj._mapping = mapping
|
||||
if not obj.size == mapping.nP:
|
||||
raise Exception('Incorrect size for array.')
|
||||
return obj
|
||||
|
||||
def __array_finalize__(self, obj):
|
||||
# see InfoArray.__array_finalize__ for comments
|
||||
if obj is None: return
|
||||
self.mapping = getattr(obj, 'mapping', None)
|
||||
self._mapping = getattr(obj, '_mapping', None)
|
||||
|
||||
@property
|
||||
def mapping(self):
|
||||
return self._mapping
|
||||
@mapping.setter
|
||||
def mapping(self, value):
|
||||
self._mapping = value
|
||||
|
||||
@property
|
||||
def transform(self):
|
||||
@@ -36,6 +31,9 @@ class Model(np.ndarray):
|
||||
self._transformDeriv = self.mapping.transformDeriv(self.view(np.ndarray))
|
||||
return self._transformDeriv
|
||||
|
||||
def test(self, **kwargs):
|
||||
return self.mapping.test(self.view(np.ndarray),**kwargs)
|
||||
|
||||
|
||||
class IdentityMap(object):
|
||||
"""
|
||||
@@ -98,8 +96,8 @@ class IdentityMap(object):
|
||||
print 'Testing the %s Class!' % self.__class__.__name__
|
||||
if m is None:
|
||||
m = self.example()
|
||||
if 'plotIt' not in kwargs:
|
||||
kwargs['plotIt'] = False
|
||||
if 'plotIt' not in kwargs:
|
||||
kwargs['plotIt'] = False
|
||||
return checkDerivative(lambda m : [self.transform(m), self.transformDeriv(m)], m, **kwargs)
|
||||
|
||||
def _assertMatchesPair(self, pair):
|
||||
@@ -451,25 +449,3 @@ class ComplexMap(IdentityMap):
|
||||
|
||||
transformInverse = transformDeriv
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
from SimPEG import *
|
||||
mesh = Mesh.TensorMesh([10,8])
|
||||
emap = ExpMap(mesh)
|
||||
vmap = Vertical1DMap(mesh)
|
||||
|
||||
combo = emap*vmap
|
||||
print combo
|
||||
print combo.maps
|
||||
|
||||
# combo = ComboMap(mesh, [ExpMap, Vertical1DMap])
|
||||
|
||||
# combo = ComboMap(mesh, [ExpMap, Vertical1DMap])
|
||||
# m = combo.example()
|
||||
# print m.shape
|
||||
# print combo.test(np.arange(8))
|
||||
mesh = Mesh.TensorMesh([10,8])
|
||||
mapping = ComplexMap(mesh)
|
||||
m = mapping.example()
|
||||
print m.shape
|
||||
# print mapping.test(m)
|
||||
|
||||
@@ -4,6 +4,7 @@ from SimPEG import *
|
||||
from TestUtils import checkDerivative
|
||||
from scipy.sparse.linalg import dsolve
|
||||
|
||||
TOL = 1e-14
|
||||
|
||||
class MapTests(unittest.TestCase):
|
||||
|
||||
@@ -33,6 +34,50 @@ class MapTests(unittest.TestCase):
|
||||
maps = Maps.ComboMap(self.mesh2, combo)
|
||||
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 = combo * m
|
||||
t_true = np.exp(np.r_[0,0,1,1,2,2.])
|
||||
self.assertLess(np.linalg.norm(t-t_true,np.inf),TOL)
|
||||
#Try making a model
|
||||
mod = Maps.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.assertTrue(mod.test(plotIt=False))
|
||||
|
||||
self.assertRaises(Exception,Maps.Model,np.r_[1.0],mapping=combo)
|
||||
|
||||
def test_activeCells(self):
|
||||
M = Mesh.TensorMesh([2,4],'0C')
|
||||
actMap = Maps.ActiveCells(M, M.vectorCCy <=0, 10, nC=M.nCy)
|
||||
vertMap = Maps.Vertical1DMap(M)
|
||||
mod = Maps.Model(np.r_[1,2.],vertMap * actMap)
|
||||
# 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.assertTrue(mod.test())
|
||||
|
||||
def test_activeCells(self):
|
||||
M = Mesh.TensorMesh([2,4],'0C')
|
||||
actMap = Maps.ActiveCells(M, M.vectorCCy <=0, 10, nC=M.nCy)
|
||||
vertMap = Maps.Vertical1DMap(M)
|
||||
mod = Maps.Model(np.r_[1,2.],vertMap * actMap)
|
||||
# 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.assertTrue(mod.test())
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user