def test_clear_dofs(self): # Define new dofhandler mesh = Mesh2D(resolution=(1,1)) cell = mesh.cells.get_child(0) element = QuadFE(2, 'Q2') dofhandler = DofHandler(mesh, element) # Fill dofs dofhandler.fill_dofs(cell) # Clear dofs dofhandler.clear_dofs() # Check that there are no dofs self.assertIsNone(dofhandler.get_cell_dofs(cell))
def test_share_dofs_with_children(self): # # 1D Test # mesh = Mesh1D(resolution=(1,)) mesh.cells.refine() for etype in ['DQ0','DQ1', 'DQ2', 'DQ3', 'Q1', 'Q2', 'Q3']: # New DofHandler element = QuadFE(1, etype) dh = DofHandler(mesh, element) # Fill in parent dofs and share with children cell = mesh.cells.get_child(0) dh.fill_dofs(cell) dh.share_dofs_with_children(cell) # Expected dofs for children left_child_dofs = {'DQ0': [None], 'DQ1': [0, None], 'DQ2': [0, 2, None], 'DQ3': [0, None, None, 2], 'Q1': [0, None], 'Q2': [0, 2, None], 'Q3': [0, None, None, 2]} right_child_dofs = {'DQ0': [None], 'DQ1': [None, 1], 'DQ2': [None, 1, None], 'DQ3': [None, 1, 3, None], 'Q1':[None, 1], 'Q2': [2, 1, None], 'Q3': [None, 1, 3, None]} left_child = cell.get_child(0) right_child = cell.get_child(1) # # Check whether shared dofs are as expected. # self.assertEqual(dh.get_cell_dofs(left_child), left_child_dofs[etype]) self.assertEqual(dh.get_cell_dofs(right_child), right_child_dofs[etype]) # # 2D Test # mesh = QuadMesh(resolution=(1,1)) mesh.cells.refine() for etype in ['DQ0','DQ1', 'DQ2', 'DQ3', 'Q1', 'Q2', 'Q3']: # New dofhandler element = QuadFE(2, etype) dh = DofHandler(mesh, element) # Fill in parent dofs and share with children cell = mesh.cells.get_child(0) dh.fill_dofs(cell) dh.share_dofs_with_children(cell) # Expected dofs for children child_dofs = {0: {'DQ0': [None], 'DQ1': [0, None, None, None], 'DQ2': [0, 4, 8, 7, None, None, None, None, None], 'DQ3': [0, None, None, None, None, 4, None, None, None, None, 11, None, None, None, None, 12], 'Q1': [0, None, None, None], 'Q2': [0, 4, 8, 7, None, None, None, None, None], 'Q3': [0, None, None, None, None, 4, None, None, None, None, 11, None, None, None, None, 12]}, 1: {'DQ0': [None], 'DQ1': [None, 1, None, None], 'DQ2': [None, 1, 5, None, None, None, None, None, None], 'DQ3': [None, 1, None, None, 5, None, None, 6, None, None, None, None, None, None, 13, None], 'Q1': [None, 1, None, None], 'Q2': [4, 1, 5, 8, None, None, None, None, None], 'Q3': [None, 1, None, None, 5, None, None, 6, None, None, None, None, None, None, 13, None]}, 2: {'DQ0': [None], 'DQ1': [None, None, 2, None], 'DQ2': [None, None, 2, 6, None, None, None, None, None], 'DQ3': [None, None, 2, None, None, None, 7, None, None, 8, None, None, 15, None, None, None], 'Q1': [None, None, 2, None], 'Q2': [8, 5, 2, 6, None, None, None, None, None], 'Q3': [None, None, 2, None, None, None, 7, None, None, 8, None, None, 15, None, None, None]}, 3: {'DQ0': [None], 'DQ1': [None, None, None, 3], 'DQ2': [None, None, None, 3, None, None, None, None, None], 'DQ3': [None, None, None, 3, None, None, None, None, 9, None, None, 10, None, 14, None, None], 'Q1': [None, None, None, 3], 'Q2': [7, 8, 6, 3, None, None, None, None, None], 'Q3': [None, None, None, 3, None, None, None, None, 9, None, None, 10, None, 14, None, None]}} for i in range(4): child = cell.get_child(i) # # Check whether shared dofs are as expected # self.assertEqual(dh.get_cell_dofs(child), child_dofs[i][etype])
def test_share_dofs_with_neighbors(self): # ===================================================================== # 1D # ===================================================================== # # Non-Periodic # # Define new mesh mesh = Mesh1D(resolution=(2,)) lcell = mesh.cells.get_child(0) rcell = mesh.cells.get_child(1) etypes = ['DQ0','DQ1', 'DQ2', 'DQ3', 'Q1', 'Q2', 'Q3'] # Specify expected dofs edofs = dict.fromkeys(etypes) edofs['DQ0'] = None edofs['DQ1'] = None edofs['DQ2'] = None edofs['DQ3'] = None edofs['Q1'] = [1, None] edofs['Q2'] = [1, None, None] edofs['Q3'] = [1, None, None, None] for etype in etypes: # New Dofhandler element = QuadFE(1,etype) dh = DofHandler(mesh, element) # Fill left dofs and share with right neighbor dh.fill_dofs(lcell) dh.share_dofs_with_neighbors(lcell, lcell.get_vertex(1)) self.assertEqual(dh.get_cell_dofs(rcell), edofs[etype]) dh.clear_dofs() # Fill left dofs and share with all neighbors dh.fill_dofs(lcell) dh.share_dofs_with_neighbors(lcell) self.assertEqual(dh.get_cell_dofs(rcell), edofs[etype]) # # Periodic # # Define new mesh mesh = Mesh1D(resolution=(2,), periodic=True) lcell = mesh.cells.get_child(0) rcell = mesh.cells.get_child(1) # Specify expected dofs edofs = dict.fromkeys(etypes) edofs['DQ0'] = {'right': None, 'all': None} edofs['DQ1'] = {'right': None, 'all': None} edofs['DQ2'] = {'right': None, 'all': None} edofs['DQ3'] = {'right': None, 'all': None} edofs['Q1'] = {'right': [1, None], 'all': [1, 0]} edofs['Q2'] = {'right': [1, None, None], 'all': [1, 0, None]} edofs['Q3'] = {'right': [1, None, None, None], 'all': [1, 0, None, None]} for etype in etypes: # New Dofhandler element = QuadFE(1,etype) dh = DofHandler(mesh, element) # Fill left dofs and share with right neighbor dh.fill_dofs(lcell) dh.share_dofs_with_neighbors(lcell, lcell.get_vertex(1)) self.assertEqual(dh.get_cell_dofs(rcell), edofs[etype]['right']) dh.clear_dofs() # Fill left dofs and share with all neighbors dh.fill_dofs(lcell) dh.share_dofs_with_neighbors(lcell) self.assertEqual(dh.get_cell_dofs(rcell), edofs[etype]['all']) # ===================================================================== # 2D # ===================================================================== # # Non-periodic # mesh = Mesh2D(resolution=(2,2)) c00 = mesh.cells.get_child(0) c10 = mesh.cells.get_child(1) c11 = mesh.cells.get_child(3) edofs = dict.fromkeys(etypes) edofs['DQ0'] = {'vertex': {c10: None, c11: None}, 'edge': {c10: None, c11: None}, 'all': {c10: None, c11: None}} edofs['DQ1'] = {'vertex': {c10: None, c11: None}, 'edge': {c10: None, c11: None}, 'all': {c10: None, c11: None}} edofs['DQ2'] = {'vertex': {c10: None, c11: None}, 'edge': {c10: None, c11: None}, 'all': {c10: None, c11: None}} edofs['DQ3'] = {'vertex': {c10: None, c11: None}, 'edge': {c10: None, c11: None}, 'all': {c10: None, c11: None}} edofs['Q1'] = {'vertex': {c10: [2], c11: [2]}, 'edge': {c10: None, c11: None}, 'all': {c10: [1,None, None, 2], c11: [2, None, None, None]}} edofs['Q2'] = {'vertex': {c10: [2], c11: [2]}, 'edge': {c10: [5], c11: None}, 'all': {c10: [1, None, None, 2, None, None, None, 5, None], c11: [2] + [None]*8}} edofs['Q3'] = {'vertex': {c10: [2], c11: [2]}, 'edge': {c10: [7,6], c11: None}, 'all': {c10: [1, None, None, 2, None, None, None, None, None, None, 7, 6, None, None, None, None], c11: [2] + [None]*15}} for etype in etypes: # New element element = QuadFE(2, etype) # New dofhandler dh = DofHandler(mesh, element) # *************************** # Share dofs accross vertex 2 # *************************** # Fill dh.fill_dofs(c00) # Share vertex = c00.get_vertex(2) dh.share_dofs_with_neighbors(c00, vertex) # Check self.assertEqual(dh.get_cell_dofs(c10, vertex), edofs[etype]['vertex'][c10]) self.assertEqual(dh.get_cell_dofs(c11, vertex), edofs[etype]['vertex'][c11]) # Clear dh.clear_dofs() # ***************************** # Share dofs across half_edge 1 # ***************************** # Fill dh.fill_dofs(c00) # Share edge = c00.get_half_edge(1) twin = edge.twin() dh.share_dofs_with_neighbors(c00, edge) # Check self.assertEqual(dh.get_cell_dofs(c10, twin, interior=True), edofs[etype]['edge'][c10]) self.assertEqual(dh.get_cell_dofs(c11, twin, interior=True), edofs[etype]['edge'][c11]) # Clear dh.clear_dofs() # ***************************** # Share dofs with all neighbors # ***************************** # Fill dh.fill_dofs(c00) # Share dh.share_dofs_with_neighbors(c00) # Check self.assertEqual(dh.get_cell_dofs(c10), edofs[etype]['all'][c10]) self.assertEqual(dh.get_cell_dofs(c11), edofs[etype]['all'][c11]) # # Periodic in both directions # mesh = Mesh2D(resolution=(2,2), periodic={0,1}) c00 = mesh.cells.get_child(0) c10 = mesh.cells.get_child(1) c11 = mesh.cells.get_child(3) edofs = dict.fromkeys(etypes) edofs['DQ0'] = {'vertex': {c10: None, c11: None}, 'edge': {c10: None, c11: None}, 'all': {c10: None, c11: None}} edofs['DQ1'] = {'vertex': {c10: None, c11: None}, 'edge': {c10: None, c11: None}, 'all': {c10: None, c11: None}} edofs['DQ2'] = {'vertex': {c10: None, c11: None}, 'edge': {c10: None, c11: None}, 'all': {c10: None, c11: None}} edofs['DQ3'] = {'vertex': {c10: None, c11: None}, 'edge': {c10: None, c11: None}, 'all': {c10: None, c11: None}} edofs['Q1'] = {'vertex': {c10: [0], c11: [0]}, 'edge': {c10: None, c11: None}, 'all': {c10: [1,0, 3, 2], c11: [2, 3, 0, 1]}} edofs['Q2'] = {'vertex': {c10: [0], c11: [0]}, 'edge': {c10: [7], c11: None}, 'all': {c10: [1, 0, 3, 2, None, 7, None, 5, None], c11: [2,3,0,1] + [None]*5}} edofs['Q3'] = {'vertex': {c10: [0], c11: [0]}, 'edge': {c10: [11,10], c11: None}, 'all': {c10: [1, 0, 3, 2, None, None, 11, 10, None, None, 7, 6, None, None, None, None], c11: [2,3,0,1] + [None]*12}} for etype in etypes: # New element element = QuadFE(2, etype) # New dofhandler dh = DofHandler(mesh, element) # *************************** # Share dofs accross vertex 0 # *************************** # Fill dh.fill_dofs(c00) # Share vertex = c00.get_vertex(0) self.assertTrue(vertex.is_periodic()) dh.share_dofs_with_neighbors(c00, vertex) # Check c10_vertex = vertex.get_periodic_pair(c10)[0] self.assertEqual(dh.get_cell_dofs(c10, c10_vertex), edofs[etype]['vertex'][c10]) c11_vertex = vertex.get_periodic_pair(c11)[0] self.assertEqual(dh.get_cell_dofs(c11, c11_vertex), edofs[etype]['vertex'][c11]) # Clear dh.clear_dofs() # ***************************** # Share dofs across half_edge 3 # ***************************** # Fill dh.fill_dofs(c00) # Share edge = c00.get_half_edge(3) twin = edge.twin() dh.share_dofs_with_neighbors(c00, edge) # Check self.assertEqual(dh.get_cell_dofs(c10, twin, interior=True), edofs[etype]['edge'][c10]) self.assertEqual(dh.get_cell_dofs(c11, twin, interior=True), edofs[etype]['edge'][c11]) # Clear dh.clear_dofs() # ***************************** # Share dofs with all neighbors # ***************************** # Fill dh.fill_dofs(c00) # Share dh.share_dofs_with_neighbors(c00) # Check self.assertEqual(dh.get_cell_dofs(c10), edofs[etype]['all'][c10]) self.assertEqual(dh.get_cell_dofs(c11), edofs[etype]['all'][c11])
def test_fill_dofs(self): etypes = ['DQ0', 'DQ1','DQ2', 'DQ3', 'Q1', 'Q2', 'Q3'] # ===================================================================== # 1D # ===================================================================== # # Non-periodic # mesh = Mesh1D(resolution=(1,)) expected_dofs = {'DQ0': [0], 'DQ1': [0,1], 'DQ2': [0,1,2], 'DQ3': [0,1,2,3], 'Q1': [0,1], 'Q2': [0,1,2], 'Q3': [0,1,2,3]} cell = mesh.cells.get_child(0) for etype in etypes: element = QuadFE(1, etype) dofhandler = DofHandler(mesh, element) dofhandler.fill_dofs(cell) dofs = dofhandler.get_cell_dofs(cell) self.assertEqual(expected_dofs[etype], dofs) # # Periodic # mesh = Mesh1D(resolution=(1,), periodic=True) expected_dofs = {'DQ0': [0], 'DQ1': [0,1], 'DQ2': [0,1,2], 'DQ3': [0,1,2,3], 'Q1': [0,0], 'Q2': [0,0,1], 'Q3': [0,0,1,2]} cell = mesh.cells.get_child(0) for etype in etypes: element = QuadFE(1, etype) dofhandler = DofHandler(mesh, element) dofhandler.fill_dofs(cell) dofs = dofhandler.get_cell_dofs(cell) self.assertEqual(dofs, expected_dofs[etype]) # ===================================================================== # 2D # ===================================================================== # # Non-periodic # mesh = Mesh2D(resolution=(1,1)) cell = mesh.cells.get_child(0) expected_dofs = {'DQ0': [0], 'DQ1': [0,1,2,3], 'DQ2': [0,1,2,3,4,5,6,7,8], 'DQ3': [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15], 'Q0': [0], 'Q1': [0,1,2,3], 'Q2': [0,1,2,3,4,5,6,7,8], 'Q3': [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]} for etype in etypes: element = QuadFE(2, etype) dofhandler = DofHandler(mesh, element) dofhandler.fill_dofs(cell) dofs = dofhandler.get_cell_dofs(cell) self.assertEqual(dofs, expected_dofs[etype]) # # Periodic x direction # mesh = Mesh2D(resolution=(1,1), periodic={0}) cell = mesh.cells.get_child(0) expected_dofs = {'DQ0': [0], 'DQ1': [0,1,2,3], 'DQ2': [0,1,2,3,4,5,6,7,8], 'DQ3': [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15], 'Q0': [0], 'Q1': [0,0,1,1], 'Q2': [0,0,1,1,2,3,4,3,5], 'Q3': [0,0,1,1,2,3,4,5,6,7,5,4,8,9,10,11]} for etype in etypes: element = QuadFE(2, etype) dofhandler = DofHandler(mesh, element) dofhandler.fill_dofs(cell) dofs = dofhandler.get_cell_dofs(cell) self.assertEqual(dofs, expected_dofs[etype]) # # Periodic in both directions # mesh = Mesh2D(resolution=(1,1), periodic={0,1}) cell = mesh.cells.get_child(0) expected_dofs = {'DQ0': [0], 'DQ1': [0,1,2,3], 'DQ2': [0,1,2,3,4,5,6,7,8], 'DQ3': [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15], 'Q0': [0], 'Q1': [0,0,0,0], 'Q2': [0,0,0,0,1,2,1,2,3], 'Q3': [0,0,0,0,1,2,3,4,2,1,4,3,5,6,7,8]} for etype in etypes: element = QuadFE(2, etype) dofhandler = DofHandler(mesh, element) dofhandler.fill_dofs(cell) dofs = dofhandler.get_cell_dofs(cell) self.assertEqual(dofs, expected_dofs[etype])