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_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_assign_get_global_dofs(self): # # Assigning Dofs to entities within cell # # ===================================================================== # 1D # ===================================================================== mesh = Mesh1D(resolution=(1,)) cell = mesh.cells.get_child(0) etypes = ['DQ0', 'DQ1', 'DQ2', 'DQ3'] # # Expected Dofs # edofs = dict.fromkeys(etypes) edofs['DQ0'] = {'all': [0], 'pos': ([0], [2]), 'vertex': None, 'edge': None} edofs['DQ1'] = {'all': [0,1], 'pos': ([1], [3]), 'vertex': (1, [1]), 'edge': None} edofs['DQ2'] = {'all': [0,1,2], 'pos': ([2], [6]), 'vertex': (1, [1]), 'edge': [3]} edofs['DQ3'] = {'all': [0,1,2,3], 'pos': ([3], [6]), 'vertex': (1, [1]), 'edge': [3,4]} for etype in etypes: # # New Dofhandler # element = QuadFE(1,etype) dh = DofHandler(mesh, element) for key in edofs[etype].keys(): # # Assign all dofs # if key=='all': dh.assign_dofs(edofs[etype][key], cell) self.assertEqual(dh.get_cell_dofs(cell), edofs[etype][key]) # # Assign dofs at given positions # elif key=='pos': pos, dofs = edofs[etype]['pos'] dh.assign_dofs(dofs, cell, pos=pos) self.assertEqual(dh.get_cell_dofs(cell)[pos[0]], dofs[0]) # # Assign dofs to given vertex # elif key=='vertex': if edofs[etype]['vertex'] is not None: v_num, dofs = edofs[etype]['vertex'] vertex = cell.get_vertex(v_num) dh.assign_dofs(dofs, cell, vertex) self.assertEqual(dh.get_cell_dofs(cell, vertex), dofs) # # Assign dofs to interval # elif key=='edge': if edofs[etype][key] is not None: dofs = edofs[etype][key] dh.assign_dofs(dofs, cell, cell) self.assertEqual(dh.get_cell_dofs(cell, cell, interior=True), dofs) # # Clear dofs to reuse DofHandler # dh.clear_dofs() # ===================================================================== # 2D # ===================================================================== # New mesh mesh = Mesh2D(resolution=(1,1)) cell = mesh.cells.get_child(0) etypes = ['DQ0', 'Q1', 'Q2', 'Q3'] # # List of dofs to assign and check # edofs = dict.fromkeys(etypes) edofs['DQ0'] = {'all': [0], 'pos': ([0],[2]) , 'vertex': None, 'edge': None, 'cell': [0] } edofs['Q1'] = {'all': [0,1,2,3], 'pos': ([1],[2]), 'vertex': (1,[2]), 'edge': None, 'cell': None } edofs['Q2'] = {'all': [0,1,2,3,4,5,6,7,8], 'pos': ([5],[7]) , 'vertex': (3,[4]), 'edge': (2, [1]), 'cell': [1] } edofs['Q3'] = {'all': [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15], 'pos': ([11],[111]), 'vertex': (3,[1]), 'edge': (3,[2,3]), 'cell': [1,2,3,4]} for etype in etypes: # New element adn Dofhandler element = QuadFE(2, etype) dh = DofHandler(mesh, element) for key in edofs[etype].keys(): # # Assign all dofs # if key=='all': dh.assign_dofs(edofs[etype][key], cell) self.assertEqual(dh.get_cell_dofs(cell), edofs[etype][key]) # # Assign dofs to specific position # elif key=='pos': pos, dofs = edofs[etype][key] dh.assign_dofs(dofs, cell, pos=pos) self.assertEqual(dh.get_cell_dofs(cell)[pos[0]],dofs[0]) # # Assign dof to specific vertex # elif key=='vertex': if edofs[etype][key] is not None: v_num, dofs = edofs[etype][key] vertex = cell.get_vertex(v_num) dh.assign_dofs(dofs, cell, vertex) self.assertEqual(dh.get_cell_dofs(cell, vertex), dofs) # # Assign dofs to specific edge # elif key=='edge': if edofs[etype][key] is not None: e_num, dofs = edofs[etype][key] he = cell.get_half_edge(e_num) dh.assign_dofs(dofs, cell, he) self.assertEqual(dh.get_cell_dofs(cell, he, interior=True),dofs) # # Assign dofs to cell interior # elif key=='cell': if edofs[etype][key] is not None: dofs = edofs[etype][key] dh.assign_dofs(dofs, cell, cell) self.assertEqual(dh.get_cell_dofs(cell, cell, interior=True), dofs) # # Clear dofs to re-use DofHandler # dh.clear_dofs()