From bb0b9c1de4da6de5aee25edc6c00890f94ca68a3 Mon Sep 17 00:00:00 2001 From: rowanc1 Date: Thu, 3 Jul 2014 10:31:04 -0700 Subject: [PATCH] write test for invProp and invMat (failing) --- SimPEG/Mesh/InnerProducts.py | 21 ++++++++++++----- SimPEG/Tests/test_massMatricesDerivs.py | 31 ++++++++++++++++++++----- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/SimPEG/Mesh/InnerProducts.py b/SimPEG/Mesh/InnerProducts.py index f8bcee46..9bc092d3 100644 --- a/SimPEG/Mesh/InnerProducts.py +++ b/SimPEG/Mesh/InnerProducts.py @@ -109,10 +109,12 @@ class InnerProducts(object): return [V*proj(*locs[node][d-1]) for node in nodes] - def getFaceInnerProductDeriv(self, prop, doFast=True): + def getFaceInnerProductDeriv(self, prop, doFast=True, invProp=False, invMat=False): """ :param numpy.array prop: material property (tensor properties are possible) at each cell center (nC, (1, 3, or 6)) :param bool doFast: do a faster implementation if available. + :param bool invProp: inverts the material property + :param bool invMat: inverts the matrix :rtype: function :return: dMdmu(u), the derivative of the inner product matrix (u) @@ -122,23 +124,27 @@ class InnerProducts(object): :rtype: scipy.csr_matrix :return: dMdmu, the derivative of the inner product matrix for a certain u """ - return self._getInnerProductDeriv(prop, 'F', doFast=doFast) + return self._getInnerProductDeriv(prop, 'F', doFast=doFast, invProp=invProp, invMat=invMat) - def getEdgeInnerProductDeriv(self, prop, doFast=True): + def getEdgeInnerProductDeriv(self, prop, doFast=True, invProp=False, invMat=False): """ :param numpy.array prop: material property (tensor properties are possible) at each cell center (nC, (1, 3, or 6)) :param bool doFast: do a faster implementation if available. + :param bool invProp: inverts the material property + :param bool invMat: inverts the matrix :rtype: scipy.csr_matrix :return: dMdm, the derivative of the inner product matrix (nE, nC*nA) """ - return self._getInnerProductDeriv(prop, 'E', doFast=doFast) + return self._getInnerProductDeriv(prop, 'E', doFast=doFast, invProp=invProp, invMat=invMat) - def _getInnerProductDeriv(self, prop, projType, doFast=True): + def _getInnerProductDeriv(self, prop, projType, doFast=True, invProp=False, invMat=False): """ :param numpy.array prop: material property (tensor properties are possible) at each cell center (nC, (1, 3, or 6)) :param str projType: 'F' for faces 'E' for edges :param bool doFast: do a faster implementation if available. + :param bool invProp: inverts the material property + :param bool invMat: inverts the matrix :rtype: scipy.csr_matrix :return: dMdm, the derivative of the inner product matrix (nE, nC*nA) """ @@ -146,7 +152,10 @@ class InnerProducts(object): fast = None if hasattr(self, '_fastInnerProductDeriv') and doFast: - fast = self._fastInnerProductDeriv(projType, tensorType) + fast = self._fastInnerProductDeriv(projType, tensorType, invProp=invProp, invMat=invMat) + + if invProp or invMat: + raise NotImplementedError('inverting the property or the matrix is not yet implemented for this mesh/tensorType. You should write it!') if fast is not None: return fast diff --git a/SimPEG/Tests/test_massMatricesDerivs.py b/SimPEG/Tests/test_massMatricesDerivs.py index 21ac8c41..5eac4ca8 100644 --- a/SimPEG/Tests/test_massMatricesDerivs.py +++ b/SimPEG/Tests/test_massMatricesDerivs.py @@ -6,7 +6,7 @@ from TestUtils import checkDerivative class TestInnerProductsDerivs(unittest.TestCase): - def doTestFace(self, h, rep, fast, meshType): + def doTestFace(self, h, rep, fast, meshType, invProp=False, invMat=False): if meshType == 'LRM': hRect = Utils.exampleLrmGrid(h,'rotate') mesh = Mesh.LogicallyRectMesh(hRect) @@ -16,14 +16,14 @@ class TestInnerProductsDerivs(unittest.TestCase): mesh = Mesh.TensorMesh(h) v = np.random.rand(mesh.nF) sig = np.random.rand(1) if rep is 0 else np.random.rand(mesh.nC*rep) - Md = mesh.getFaceInnerProductDeriv(sig, doFast=fast) def fun(sig): - M = mesh.getFaceInnerProduct(sig) + M = mesh.getFaceInnerProduct(sig, invProp=invProp, invMat=invMat) + Md = mesh.getFaceInnerProductDeriv(sig, invProp=invProp, invMat=invMat, doFast=fast) return M*v, Md(v) print meshType, 'Face', h, rep, fast return checkDerivative(fun, sig, num=5, plotIt=False) - def doTestEdge(self, h, rep, fast, meshType): + def doTestEdge(self, h, rep, fast, meshType, invProp=False, invMat=False): if meshType == 'LRM': hRect = Utils.exampleLrmGrid(h,'rotate') mesh = Mesh.LogicallyRectMesh(hRect) @@ -33,9 +33,9 @@ class TestInnerProductsDerivs(unittest.TestCase): mesh = Mesh.TensorMesh(h) v = np.random.rand(mesh.nE) sig = np.random.rand(1) if rep is 0 else np.random.rand(mesh.nC*rep) - Md = mesh.getEdgeInnerProductDeriv(sig, doFast=fast) def fun(sig): - M = mesh.getEdgeInnerProduct(sig) + M = mesh.getEdgeInnerProduct(sig, invProp=invProp, invMat=invMat) + Md = mesh.getEdgeInnerProductDeriv(sig, invProp=invProp, invMat=invMat, doFast=fast) return M*v, Md(v) print meshType, 'Edge', h, rep, fast return checkDerivative(fun, sig, num=5, plotIt=False) @@ -118,6 +118,25 @@ class TestInnerProductsDerivs(unittest.TestCase): + # def test_FaceIP_1D_float_fast_harmonic(self): + # self.assertTrue(self.doTestFace([10],0, True, 'Tensor', invProp=True, invMat=True)) + # def test_FaceIP_2D_float_fast_harmonic(self): + # self.assertTrue(self.doTestFace([10, 4],0, True, 'Tensor', invProp=True, invMat=True)) + # def test_FaceIP_3D_float_fast_harmonic(self): + # self.assertTrue(self.doTestFace([10, 4, 5],0, True, 'Tensor', invProp=True, invMat=True)) + def test_FaceIP_1D_isotropic_fast_harmonic(self): + self.assertTrue(self.doTestFace([10],1, True, 'Tensor', invProp=True, invMat=True)) + def test_FaceIP_2D_isotropic_fast_harmonic(self): + self.assertTrue(self.doTestFace([10, 4],1, True, 'Tensor', invProp=True, invMat=True)) + def test_FaceIP_3D_isotropic_fast_harmonic(self): + self.assertTrue(self.doTestFace([10, 4, 5],1, True, 'Tensor', invProp=True, invMat=True)) + # def test_FaceIP_2D_anisotropic_fast_harmonic(self): + # self.assertTrue(self.doTestFace([10, 4],2, True, 'Tensor', invProp=True, invMat=True)) + # def test_FaceIP_3D_anisotropic_fast_harmonic(self): + # self.assertTrue(self.doTestFace([10, 4, 5],3, True, 'Tensor', invProp=True, invMat=True)) + + + def test_FaceIP_2D_float_LRM(self): self.assertTrue(self.doTestFace([10, 4],0, False, 'LRM')) def test_FaceIP_3D_float_LRM(self):