mesh2mesh interpolation a bit slow and memory heavy right now -> might not want to return the mat, just perform the interpolation

This commit is contained in:
Lindsey Heagy
2016-03-29 18:31:55 -07:00
parent d11f5c736b
commit 7aa50047ac
3 changed files with 351 additions and 35 deletions
+52
View File
@@ -1,4 +1,5 @@
import numpy as np
import scipy.sparse as sp
from SimPEG import Utils
@@ -594,3 +595,54 @@ class BaseRectangularMesh(BaseMesh):
return out
else:
return switchKernal(x)
def getInterpolationMatMesh2Mesh(self, mesh2, locType='CC'):
"""
Interpolates variables from the current mesh to a new mesh (mesh2)
:param Mesh mesh2: SimPEG mesh which we interpolate values to
:param string locType: location of variables 'CC', 'E', 'F', 'N'
:rtype: scipy.sparse.csr_matrix
:return P: interpolation matrix
"""
# Error Checking
if self._meshType == 'Cyl':
assert self.isSymmetric, "Currently, we do not support non-symmetric cyl meshes"
if mesh2._meshType == 'Cyl':
assert self._meshType == 'Cyl', "Interpolation from 3D mesh to Cyl mesh is not supported"
# if Cyl to cart call
if self._meshType == 'Cyl' and mesh2._meshType != 'Cyl':
return self.getInterpolationMatCartMesh(mesh2, locType)
# Scalars
if locType in ['CC', 'N', 'Fx', 'Fy', 'Fz', 'Ex', 'Ey', 'Ez']:
grid = getattr(mesh2, 'grid%s'%locType)
return self.getInterpolationMat(grid, locType)
# Vectors
else:
if self._meshType == 'Cyl':
if locType == 'F':
X = self.getInterpolationMatMesh2Mesh(mesh2, locType='Fx')
Z = self.getInterpolationMatMesh2Mesh(mesh2, locType='Fz')
return sp.block_diag([X, Z])
elif locType == 'E':
return self.getInterpolationMatMesh2Mesh(mesh2, locType='Ey')
if self.dim == 1:
return self.getInterpolationMatMesh2Mesh(mesh2, locType='%sx'%locType)
elif self.dim == 2:
X = self.getInterpolationMatMesh2Mesh(mesh2, locType='%sx'%locType)
Y = self.getInterpolationMatMesh2Mesh(mesh2, locType='%sy'%locType)
return sp.block_diag([X, Y])
elif self.dim == 3:
X = self.getInterpolationMatMesh2Mesh(mesh2, locType='%sx'%locType)
Y = self.getInterpolationMatMesh2Mesh(mesh2, locType='%sy'%locType)
Z = self.getInterpolationMatMesh2Mesh(mesh2, locType='%sz'%locType)
return sp.block_diag([X, Y, Z])
+44 -35
View File
@@ -82,14 +82,14 @@ class OrderTest(unittest.TestCase):
_meshType = meshTypes[0]
meshDimension = 3
def setupMesh(self, nc):
def makeMesh(self, nc, meshType=_meshType, meshDimension=meshDimension):
"""
For a given number of cells nc, generate a TensorMesh with uniform cells with edge length h=1/nc.
"""
if 'TensorMesh' in self._meshType:
if 'uniform' in self._meshType:
if 'TensorMesh' in meshType:
if 'uniform' in meshType:
h = [nc, nc, nc]
elif 'random' in self._meshType:
elif 'random' in meshType:
h1 = np.random.rand(nc)*nc*0.5 + nc*0.5
h2 = np.random.rand(nc)*nc*0.5 + nc*0.5
h3 = np.random.rand(nc)*nc*0.5 + nc*0.5
@@ -97,46 +97,46 @@ class OrderTest(unittest.TestCase):
else:
raise Exception('Unexpected meshType')
self.M = TensorMesh(h[:self.meshDimension])
max_h = max([np.max(hi) for hi in self.M.h])
return max_h
M = TensorMesh(h[:meshDimension])
max_h = max([np.max(hi) for hi in M.h])
return M, max_h
elif 'CylMesh' in self._meshType:
if 'uniform' in self._meshType:
elif 'CylMesh' in meshType:
if 'uniform' in meshType:
h = [nc, nc, nc]
else:
raise Exception('Unexpected meshType')
if self.meshDimension == 2:
self.M = CylMesh([h[0], 1, h[2]])
max_h = max([np.max(hi) for hi in [self.M.hx, self.M.hz]])
elif self.meshDimension == 3:
self.M = CylMesh(h)
max_h = max([np.max(hi) for hi in self.M.h])
return max_h
if meshDimension == 2:
M = CylMesh([h[0], 1, h[2]])
max_h = max([np.max(hi) for hi in [M.hx, M.hz]])
elif meshDimension == 3:
M = CylMesh(h)
max_h = max([np.max(hi) for hi in M.h])
return M, max_h
elif 'Curv' in self._meshType:
if 'uniform' in self._meshType:
elif 'Curv' in meshType:
if 'uniform' in meshType:
kwrd = 'rect'
elif 'rotate' in self._meshType:
elif 'rotate' in meshType:
kwrd = 'rotate'
else:
raise Exception('Unexpected meshType')
if self.meshDimension == 1:
if meshDimension == 1:
raise Exception('Lom not supported for 1D')
elif self.meshDimension == 2:
elif meshDimension == 2:
X, Y = Utils.exampleLrmGrid([nc, nc], kwrd)
self.M = CurvilinearMesh([X, Y])
elif self.meshDimension == 3:
M = CurvilinearMesh([X, Y])
elif meshDimension == 3:
X, Y, Z = Utils.exampleLrmGrid([nc, nc, nc], kwrd)
self.M = CurvilinearMesh([X, Y, Z])
return 1./nc
M = CurvilinearMesh([X, Y, Z])
return M, 1./nc
elif 'Tree' in self._meshType:
elif 'Tree' in meshType:
nc *= 2
if 'uniform' in self._meshType or 'notatree' in self._meshType:
if 'uniform' in meshType or 'notatree' in meshType:
h = [nc, nc, nc]
elif 'random' in self._meshType:
elif 'random' in meshType:
h1 = np.random.rand(nc)*nc*0.5 + nc*0.5
h2 = np.random.rand(nc)*nc*0.5 + nc*0.5
h3 = np.random.rand(nc)*nc*0.5 + nc*0.5
@@ -145,20 +145,29 @@ class OrderTest(unittest.TestCase):
raise Exception('Unexpected meshType')
levels = int(np.log(nc)/np.log(2))
self.M = Tree(h[:self.meshDimension], levels=levels)
M = Tree(h[:meshDimension], levels=levels)
def function(cell):
if 'notatree' in self._meshType:
if 'notatree' in meshType:
return levels - 1
r = cell.center - np.array([0.5]*len(cell.center))
dist = np.sqrt(r.dot(r))
if dist < 0.2:
return levels
return levels - 1
self.M.refine(function,balance=False)
self.M.number(balance=False)
# self.M.plotGrid(showIt=True)
max_h = max([np.max(hi) for hi in self.M.h])
return max_h
M.refine(function,balance=False)
M.number(balance=False)
# M.plotGrid(showIt=True)
max_h = max([np.max(hi) for hi in M.h])
return M, max_h
def setupMesh(self, nc):
"""
For a given number of cells nc, generate a TensorMesh with uniform cells with edge length h=1/nc.
"""
M, h = self.makeMesh(nc, meshType=self._meshType, meshDimension=self.meshDimension)
self.M = M
return h
def getError(self):
"""For given h, generate A[h], f and A(f) and return norm of error."""