def setUp(self): a = np.array([1, 1, 1]) b = np.array([1, 2]) c = np.array([1, 4]) def gridIt(h): return [np.cumsum(np.r_[0, x]) for x in h] X, Y = ndgrid(gridIt([a, b]), vector=False) self.TM2 = TensorMesh([a, b]) self.Curv2 = CurvilinearMesh([X, Y]) X, Y, Z = ndgrid(gridIt([a, b, c]), vector=False) self.TM3 = TensorMesh([a, b, c]) self.Curv3 = CurvilinearMesh([X, Y, Z])
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
class BasicCurvTests(unittest.TestCase): def setUp(self): a = np.array([1, 1, 1]) b = np.array([1, 2]) c = np.array([1, 4]) def gridIt(h): return [np.cumsum(np.r_[0, x]) for x in h] X, Y = ndgrid(gridIt([a, b]), vector=False) self.TM2 = TensorMesh([a, b]) self.Curv2 = CurvilinearMesh([X, Y]) X, Y, Z = ndgrid(gridIt([a, b, c]), vector=False) self.TM3 = TensorMesh([a, b, c]) self.Curv3 = CurvilinearMesh([X, Y, Z]) def test_area_3D(self): test_area = np.array([1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 8, 8, 8, 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2]) self.assertTrue(np.all(self.Curv3.area == test_area)) def test_vol_3D(self): test_vol = np.array([1, 1, 1, 2, 2, 2, 4, 4, 4, 8, 8, 8]) np.testing.assert_almost_equal(self.Curv3.vol, test_vol) self.assertTrue(True) # Pass if you get past the assertion. def test_vol_2D(self): test_vol = np.array([1, 1, 1, 2, 2, 2]) t1 = np.all(self.Curv2.vol == test_vol) self.assertTrue(t1) def test_edge_3D(self): test_edge = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]) t1 = np.all(self.Curv3.edge == test_edge) self.assertTrue(t1) def test_edge_2D(self): test_edge = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2]) t1 = np.all(self.Curv2.edge == test_edge) self.assertTrue(t1) def test_tangents(self): T = self.Curv2.tangents self.assertTrue(np.all(self.Curv2.r(T, 'E', 'Ex', 'V')[0] == np.ones(self.Curv2.nEx))) self.assertTrue(np.all(self.Curv2.r(T, 'E', 'Ex', 'V')[1] == np.zeros(self.Curv2.nEx))) self.assertTrue(np.all(self.Curv2.r(T, 'E', 'Ey', 'V')[0] == np.zeros(self.Curv2.nEy))) self.assertTrue(np.all(self.Curv2.r(T, 'E', 'Ey', 'V')[1] == np.ones(self.Curv2.nEy))) T = self.Curv3.tangents self.assertTrue(np.all(self.Curv3.r(T, 'E', 'Ex', 'V')[0] == np.ones(self.Curv3.nEx))) self.assertTrue(np.all(self.Curv3.r(T, 'E', 'Ex', 'V')[1] == np.zeros(self.Curv3.nEx))) self.assertTrue(np.all(self.Curv3.r(T, 'E', 'Ex', 'V')[2] == np.zeros(self.Curv3.nEx))) self.assertTrue(np.all(self.Curv3.r(T, 'E', 'Ey', 'V')[0] == np.zeros(self.Curv3.nEy))) self.assertTrue(np.all(self.Curv3.r(T, 'E', 'Ey', 'V')[1] == np.ones(self.Curv3.nEy))) self.assertTrue(np.all(self.Curv3.r(T, 'E', 'Ey', 'V')[2] == np.zeros(self.Curv3.nEy))) self.assertTrue(np.all(self.Curv3.r(T, 'E', 'Ez', 'V')[0] == np.zeros(self.Curv3.nEz))) self.assertTrue(np.all(self.Curv3.r(T, 'E', 'Ez', 'V')[1] == np.zeros(self.Curv3.nEz))) self.assertTrue(np.all(self.Curv3.r(T, 'E', 'Ez', 'V')[2] == np.ones(self.Curv3.nEz))) def test_normals(self): N = self.Curv2.normals self.assertTrue(np.all(self.Curv2.r(N, 'F', 'Fx', 'V')[0] == np.ones(self.Curv2.nFx))) self.assertTrue(np.all(self.Curv2.r(N, 'F', 'Fx', 'V')[1] == np.zeros(self.Curv2.nFx))) self.assertTrue(np.all(self.Curv2.r(N, 'F', 'Fy', 'V')[0] == np.zeros(self.Curv2.nFy))) self.assertTrue(np.all(self.Curv2.r(N, 'F', 'Fy', 'V')[1] == np.ones(self.Curv2.nFy))) N = self.Curv3.normals self.assertTrue(np.all(self.Curv3.r(N, 'F', 'Fx', 'V')[0] == np.ones(self.Curv3.nFx))) self.assertTrue(np.all(self.Curv3.r(N, 'F', 'Fx', 'V')[1] == np.zeros(self.Curv3.nFx))) self.assertTrue(np.all(self.Curv3.r(N, 'F', 'Fx', 'V')[2] == np.zeros(self.Curv3.nFx))) self.assertTrue(np.all(self.Curv3.r(N, 'F', 'Fy', 'V')[0] == np.zeros(self.Curv3.nFy))) self.assertTrue(np.all(self.Curv3.r(N, 'F', 'Fy', 'V')[1] == np.ones(self.Curv3.nFy))) self.assertTrue(np.all(self.Curv3.r(N, 'F', 'Fy', 'V')[2] == np.zeros(self.Curv3.nFy))) self.assertTrue(np.all(self.Curv3.r(N, 'F', 'Fz', 'V')[0] == np.zeros(self.Curv3.nFz))) self.assertTrue(np.all(self.Curv3.r(N, 'F', 'Fz', 'V')[1] == np.zeros(self.Curv3.nFz))) self.assertTrue(np.all(self.Curv3.r(N, 'F', 'Fz', 'V')[2] == np.ones(self.Curv3.nFz))) def test_grid(self): self.assertTrue(np.all(self.Curv2.gridCC == self.TM2.gridCC)) self.assertTrue(np.all(self.Curv2.gridN == self.TM2.gridN)) self.assertTrue(np.all(self.Curv2.gridFx == self.TM2.gridFx)) self.assertTrue(np.all(self.Curv2.gridFy == self.TM2.gridFy)) self.assertTrue(np.all(self.Curv2.gridEx == self.TM2.gridEx)) self.assertTrue(np.all(self.Curv2.gridEy == self.TM2.gridEy)) self.assertTrue(np.all(self.Curv3.gridCC == self.TM3.gridCC)) self.assertTrue(np.all(self.Curv3.gridN == self.TM3.gridN)) self.assertTrue(np.all(self.Curv3.gridFx == self.TM3.gridFx)) self.assertTrue(np.all(self.Curv3.gridFy == self.TM3.gridFy)) self.assertTrue(np.all(self.Curv3.gridFz == self.TM3.gridFz)) self.assertTrue(np.all(self.Curv3.gridEx == self.TM3.gridEx)) self.assertTrue(np.all(self.Curv3.gridEy == self.TM3.gridEy)) self.assertTrue(np.all(self.Curv3.gridEz == self.TM3.gridEz))
def setupMesh(meshType, nC, nDim): """ For a given number of cells nc, generate a TensorMesh with uniform cells with edge length h=1/nc. """ if 'TensorMesh' in meshType: if 'uniform' in meshType: h = [nC, nC, nC] 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 h = [hi/np.sum(hi) for hi in [h1, h2, h3]] # normalize else: raise Exception('Unexpected meshType') mesh = TensorMesh(h[:nDim]) max_h = max([np.max(hi) for hi in mesh.h]) elif 'CylMesh' in meshType: if 'uniform' in meshType: h = [nC, nC, nC] 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 h = [hi/np.sum(hi) for hi in [h1, h2, h3]] # normalize h[1] = h[1]*2*np.pi else: raise Exception('Unexpected meshType') if nDim == 2: mesh = CylMesh([h[0], 1, h[2]]) max_h = max([np.max(hi) for hi in [mesh.hx, mesh.hz]]) elif nDim == 3: mesh = CylMesh(h) max_h = max([np.max(hi) for hi in mesh.h]) elif 'Curv' in meshType: if 'uniform' in meshType: kwrd = 'rect' elif 'rotate' in meshType: kwrd = 'rotate' else: raise Exception('Unexpected meshType') if nDim == 1: raise Exception('Lom not supported for 1D') elif nDim == 2: X, Y = utils.exampleLrmGrid([nC, nC], kwrd) mesh = CurvilinearMesh([X, Y]) elif nDim == 3: X, Y, Z = utils.exampleLrmGrid([nC, nC, nC], kwrd) mesh = CurvilinearMesh([X, Y, Z]) max_h = 1./nC elif 'Tree' in meshType: if Tree is None: raise Exception( "Tree Mesh not installed. Run 'python setup.py install'" ) nC *= 2 if 'uniform' in meshType or 'notatree' in meshType: h = [nC, nC, nC] 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 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)) mesh = Tree(h[:nDim], levels=levels) def function(cell): if 'notatree' in meshType: return levels - 1 r = cell.center - 0.5 dist = np.sqrt(r.dot(r)) if dist < 0.2: return levels return levels - 1 mesh.refine(function) # mesh.number() # mesh.plotGrid(showIt=True) max_h = max([np.max(hi) for hi in mesh.h]) return mesh, max_h