def setupMesh(self, nc): """ 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: h = [nc, nc, nc] elif 'random' in self._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 h = [hi/np.sum(hi) for hi in [h1, h2, h3]] # normalize 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 elif 'CylMesh' in self._meshType: if 'uniform' in self._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 elif 'Curv' in self._meshType: if 'uniform' in self._meshType: kwrd = 'rect' elif 'rotate' in self._meshType: kwrd = 'rotate' else: raise Exception('Unexpected meshType') if self.meshDimension == 1: raise Exception('Lom not supported for 1D') elif self.meshDimension == 2: X, Y = Utils.exampleLrmGrid([nc, nc], kwrd) self.M = CurvilinearMesh([X, Y]) elif self.meshDimension == 3: X, Y, Z = Utils.exampleLrmGrid([nc, nc, nc], kwrd) self.M = CurvilinearMesh([X, Y, Z]) return 1./nc
def doTestFace(self, h, rep, fast, meshType, invProp=False, invMat=False): if meshType == 'Curv': hRect = Utils.exampleLrmGrid(h,'rotate') mesh = Mesh.CurvilinearMesh(hRect) elif meshType == 'Tree': mesh = Mesh.TreeMesh(h, levels=3) mesh.refine(lambda xc: 3) mesh.number(balance=False) elif meshType == 'Tensor': 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) def fun(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, ('harmonic' if invProp and invMat else 'standard')) return Tests.checkDerivative(fun, sig, num=5, plotIt=False)
def doTestFace(self, h, rep, fast, meshType, invProp=False, invMat=False): if meshType == 'Curv': hRect = Utils.exampleLrmGrid(h,'rotate') mesh = Mesh.CurvilinearMesh(hRect) elif meshType == 'Tree': mesh = Mesh.TreeMesh(h, levels=3) mesh.refine(lambda xc: 3) mesh.number(balance=False) elif meshType == 'Tensor': 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) def fun(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, ('harmonic' if invProp and invMat else 'standard') return Tests.checkDerivative(fun, sig, num=5, plotIt=False)
def setupMesh(self, nc): """ 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: h = [nc, nc, nc] elif 'random' in self._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 h = [hi / np.sum(hi) for hi in [h1, h2, h3]] # normalize 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 elif 'CylMesh' in self._meshType: if 'uniform' in self._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 elif 'Curv' in self._meshType: if 'uniform' in self._meshType: kwrd = 'rect' elif 'rotate' in self._meshType: kwrd = 'rotate' else: raise Exception('Unexpected meshType') if self.meshDimension == 1: raise Exception('Lom not supported for 1D') elif self.meshDimension == 2: X, Y = Utils.exampleLrmGrid([nc, nc], kwrd) self.M = CurvilinearMesh([X, Y]) elif self.meshDimension == 3: X, Y, Z = Utils.exampleLrmGrid([nc, nc, nc], kwrd) self.M = CurvilinearMesh([X, Y, Z]) return 1. / nc elif 'Tree' in self._meshType: nc *= 2 if 'uniform' in self._meshType or 'notatree' in self._meshType: h = [nc, nc, nc] elif 'random' in self._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 h = [hi / np.sum(hi) for hi in [h1, h2, h3]] # normalize else: raise Exception('Unexpected meshType') levels = int(np.log(nc) / np.log(2)) self.M = Tree(h[:self.meshDimension], levels=levels) def function(cell): if 'notatree' in self._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
def setupMesh(self, nc): """ 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: h = [nc, nc, nc] elif 'random' in self._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 h = [hi/np.sum(hi) for hi in [h1, h2, h3]] # normalize 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 elif 'CylMesh' in self._meshType: if 'uniform' in self._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 elif 'Curv' in self._meshType: if 'uniform' in self._meshType: kwrd = 'rect' elif 'rotate' in self._meshType: kwrd = 'rotate' else: raise Exception('Unexpected meshType') if self.meshDimension == 1: raise Exception('Lom not supported for 1D') elif self.meshDimension == 2: X, Y = Utils.exampleLrmGrid([nc, nc], kwrd) self.M = CurvilinearMesh([X, Y]) elif self.meshDimension == 3: X, Y, Z = Utils.exampleLrmGrid([nc, nc, nc], kwrd) self.M = CurvilinearMesh([X, Y, Z]) return 1./nc elif 'Tree' in self._meshType: nc *= 2 if 'uniform' in self._meshType or 'notatree' in self._meshType: h = [nc, nc, nc] elif 'random' in self._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 h = [hi/np.sum(hi) for hi in [h1, h2, h3]] # normalize else: raise Exception('Unexpected meshType') levels = int(np.log(nc)/np.log(2)) self.M = Tree(h[:self.meshDimension], levels=levels) def function(cell): if 'notatree' in self._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