mirror of
https://github.com/wassname/simpeg.git
synced 2026-08-11 11:26:01 +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):
|
class Model(np.ndarray):
|
||||||
|
|
||||||
def __new__(cls, input_array, mapping=None):
|
def __new__(cls, input_array, mapping=None):
|
||||||
# Input array is an already formed ndarray instance
|
assert isinstance(mapping, IdentityMap), 'mapping must be a SimPEG.Mapping'
|
||||||
# We first cast to be our class type
|
|
||||||
obj = np.asarray(input_array).view(cls)
|
obj = np.asarray(input_array).view(cls)
|
||||||
# add the new attribute to the created instance
|
obj._mapping = mapping
|
||||||
obj.mapping = mapping
|
if not obj.size == mapping.nP:
|
||||||
# Finally, we must return the newly created object:
|
raise Exception('Incorrect size for array.')
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
def __array_finalize__(self, obj):
|
def __array_finalize__(self, obj):
|
||||||
# see InfoArray.__array_finalize__ for comments
|
|
||||||
if obj is None: return
|
if obj is None: return
|
||||||
self.mapping = getattr(obj, 'mapping', None)
|
self._mapping = getattr(obj, '_mapping', None)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def mapping(self):
|
def mapping(self):
|
||||||
return self._mapping
|
return self._mapping
|
||||||
@mapping.setter
|
|
||||||
def mapping(self, value):
|
|
||||||
self._mapping = value
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def transform(self):
|
def transform(self):
|
||||||
@@ -36,6 +31,9 @@ class Model(np.ndarray):
|
|||||||
self._transformDeriv = self.mapping.transformDeriv(self.view(np.ndarray))
|
self._transformDeriv = self.mapping.transformDeriv(self.view(np.ndarray))
|
||||||
return self._transformDeriv
|
return self._transformDeriv
|
||||||
|
|
||||||
|
def test(self, **kwargs):
|
||||||
|
return self.mapping.test(self.view(np.ndarray),**kwargs)
|
||||||
|
|
||||||
|
|
||||||
class IdentityMap(object):
|
class IdentityMap(object):
|
||||||
"""
|
"""
|
||||||
@@ -98,8 +96,8 @@ class IdentityMap(object):
|
|||||||
print 'Testing the %s Class!' % self.__class__.__name__
|
print 'Testing the %s Class!' % self.__class__.__name__
|
||||||
if m is None:
|
if m is None:
|
||||||
m = self.example()
|
m = self.example()
|
||||||
if 'plotIt' not in kwargs:
|
if 'plotIt' not in kwargs:
|
||||||
kwargs['plotIt'] = False
|
kwargs['plotIt'] = False
|
||||||
return checkDerivative(lambda m : [self.transform(m), self.transformDeriv(m)], m, **kwargs)
|
return checkDerivative(lambda m : [self.transform(m), self.transformDeriv(m)], m, **kwargs)
|
||||||
|
|
||||||
def _assertMatchesPair(self, pair):
|
def _assertMatchesPair(self, pair):
|
||||||
@@ -451,25 +449,3 @@ class ComplexMap(IdentityMap):
|
|||||||
|
|
||||||
transformInverse = transformDeriv
|
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 TestUtils import checkDerivative
|
||||||
from scipy.sparse.linalg import dsolve
|
from scipy.sparse.linalg import dsolve
|
||||||
|
|
||||||
|
TOL = 1e-14
|
||||||
|
|
||||||
class MapTests(unittest.TestCase):
|
class MapTests(unittest.TestCase):
|
||||||
|
|
||||||
@@ -33,6 +34,50 @@ class MapTests(unittest.TestCase):
|
|||||||
maps = Maps.ComboMap(self.mesh2, combo)
|
maps = Maps.ComboMap(self.mesh2, combo)
|
||||||
self.assertTrue(maps.test())
|
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__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|||||||
Reference in New Issue
Block a user