def test_nx_y_11(self): """Test nx, y with 1 division.""" ref_groups = groups_11 ref_centroids = centroids_11 mocmg.initialize() gmsh.initialize() rectangular_grid(bb_44, nx=[2], y=[[2.0]]) group_nums = gmsh.model.getPhysicalGroups() names = [gmsh.model.getPhysicalName(*grp) for grp in group_nums] ref_names = list(ref_groups.keys()) # Check correct names/entities for i, name in enumerate(ref_names): self.assertEqual(name, names[i]) index = names.index(name) group_ents = list( gmsh.model.getEntitiesForPhysicalGroup(*group_nums[index])) ref_group_ents = ref_groups[name] self.assertEqual(group_ents, ref_group_ents) # Check correct area/centroid for ent in gmsh.model.getEntities(2): tag = ent[1] mass = gmsh.model.occ.getMass(2, tag) self.assertAlmostEqual(4.0, mass, places=5, msg="2 width, 2 height, 4 area") x, y, z = gmsh.model.occ.getCenterOfMass(2, tag) centroid = (x, y, z) for i in range(3): self.assertAlmostEqual(centroid[i], ref_centroids[tag][i]) gmsh.clear() gmsh.finalize()
def test_nx_ny_21_with_material(self): """Test nx, ny with 2 levels of 1 division and a material.""" ref_groups = groups_21_with_material ref_centroids = centroids_21 mocmg.initialize() gmsh.initialize() rectangular_grid(bb_44, nx=[2, 2], ny=[2, 2], material="material_UO2") group_nums = gmsh.model.getPhysicalGroups() names = [gmsh.model.getPhysicalName(*grp) for grp in group_nums] ref_names = list(ref_groups.keys()) # Check correct names/entities for i, name in enumerate(ref_names): self.assertEqual(name, names[i]) index = names.index(name) group_ents = list( gmsh.model.getEntitiesForPhysicalGroup(*group_nums[index])) ref_group_ents = ref_groups[name] self.assertEqual(group_ents, ref_group_ents) # Check correct area/centroid for ent in gmsh.model.getEntities(2): tag = ent[1] mass = gmsh.model.occ.getMass(2, tag) self.assertAlmostEqual(1.0, mass, places=5, msg="1 width, 1 height, 1 area") x, y, z = gmsh.model.occ.getCenterOfMass(2, tag) centroid = (x, y, z) for i in range(3): self.assertAlmostEqual(centroid[i], ref_centroids[tag][i]) # Check materials gmsh.clear() gmsh.finalize()
def test_grid_quadratic_triangle(self): """Test the mesh class functions on a quadratic triangle mesh.""" ref_vertices = quadratic_triangle_vertices ref_cells = quadratic_triangle_cells ref_cell_sets = quadratic_triangle_cell_sets mocmg.initialize() mesh = mocmg.mesh.GridMesh(ref_vertices, ref_cells, ref_cell_sets, name="quad_tri_name") self.assertEqual(mesh.vertices, ref_vertices) self.assertEqual(mesh.cells, ref_cells) self.assertEqual(mesh.cell_sets, ref_cell_sets) # get_cells cell_set = mesh.get_cells("DISK") self.assertTrue( np.array_equal(cell_set, quadratic_triangle_cell_sets["DISK"])) # get_cell_area cell_area_ref = 0.261949 cell_area = mesh.get_cell_area(2) self.assertAlmostEqual(cell_area, cell_area_ref, 6) # get_set_area set_area_ref = 3.1391725 set_area = mesh.get_set_area("DISK") self.assertAlmostEqual(set_area, set_area_ref, 6) # name self.assertEqual(mesh.name, "quad_tri_name")
def test_2_rectangles_with_bad_overwrite(self): """Test a 2 rectangle case, overwriting a material that doesnt exist.""" with self.assertRaises(SystemExit): with captured_output() as (out, err): mocmg.initialize() gmsh.initialize() gmsh.model.occ.addRectangle(0.0, 0.0, 0.0, 2.0, 2.0) gmsh.model.occ.addRectangle(1.0, 1.0, 0.0, 2.0, 2.0) gmsh.model.occ.synchronize() gmsh.model.addPhysicalGroup(2, [1]) gmsh.model.addPhysicalGroup(2, [2]) gmsh.model.addPhysicalGroup(2, [1, 2]) gmsh.model.setPhysicalName(2, 1, "Material_1") gmsh.model.setPhysicalName(2, 2, "Material_2") gmsh.model.setPhysicalName(2, 3, "All") mocmg.model.group_preserving_fragment( [(2, 1)], [(2, 2)], overwrite_material="BAD_MAT" ) gmsh.clear() gmsh.finalize() out, err = out.getvalue().splitlines(), err.getvalue().splitlines() out = [line.split(None, 1)[1] for line in out] err = [line.split(None, 1)[1] for line in [err[0]]] # strip times self.assertEqual(out, ref_out) self.assertEqual(err, bad_overwrite)
def test_x_y_nonuniform_2(self): """Test x, y with nonuniform grid with 2 levels.""" ref_groups = groups_nu2 ref_centroids = centroids_nu2 ref_areas = areas_nu2 mocmg.initialize() gmsh.initialize() rectangular_grid(bb_12_4, x=[[0.0, 12.0], [2.0, 10.0]], y=[[2.0], [1.0, 3.0]]) group_nums = gmsh.model.getPhysicalGroups() names = [gmsh.model.getPhysicalName(*grp) for grp in group_nums] ref_names = list(ref_groups.keys()) # Check correct names/entities for i, name in enumerate(ref_names): self.assertEqual(name, names[i]) index = names.index(name) group_ents = list( gmsh.model.getEntitiesForPhysicalGroup(*group_nums[index])) ref_group_ents = ref_groups[name] self.assertEqual(group_ents, ref_group_ents) # Check correct area/centroid for ent in gmsh.model.getEntities(2): tag = ent[1] mass = gmsh.model.occ.getMass(2, tag) self.assertAlmostEqual(ref_areas[tag], mass, places=5) x, y, z = gmsh.model.occ.getCenterOfMass(2, tag) centroid = (x, y, z) for i in range(3): self.assertAlmostEqual(centroid[i], ref_centroids[tag][i]) gmsh.clear() gmsh.finalize()
def test_triangle_only(self): """Test reading abaqus file with triangle cells only and no cell sets.""" vertices_ref = { 1: np.array([1.0, 0.0, 0.0]), 2: np.array([0.62348980185873, 0.78183148246803, 0.0]), 3: np.array([-0.22252093395631, 0.97492791218182, 0.0]), 4: np.array([-0.90096886790242, 0.43388373911756, 0.0]), 5: np.array([-0.90096886790242, -0.43388373911756, 0.0]), 6: np.array([-0.22252093395631, -0.97492791218182, 0.0]), 7: np.array([0.62348980185873, -0.78183148246803, 0.0]), 8: np.array([1.5202888403297e-17, -7.7860210853066e-18, 0.0]), } cells_ref = { "triangle": { 1: np.array([4, 8, 3]), 2: np.array([5, 8, 4]), 3: np.array([3, 8, 2]), 4: np.array([6, 8, 5]), 5: np.array([2, 8, 1]), 6: np.array([7, 8, 6]), 7: np.array([1, 8, 7]), }, } out_ref = [ "INFO : mocmg.mesh.abaqus_IO - " + "Reading mesh data from tests/mesh/abaqus_files/triangle_only.inp" ] err_ref = [] with captured_output() as (out, err): mocmg.initialize() mesh = mocmg.mesh.read_abaqus_file( "tests/mesh/abaqus_files/triangle_only.inp") vertices = mesh.vertices cells = mesh.cells cell_sets = mesh.cell_sets # vertices for i in range(1, 9): for j in range(3): self.assertEqual(vertices[i][j], vertices_ref[i][j]) # cells self.assertEqual(len(cells), 1) self.assertEqual(list(cells.keys()), ["triangle"]) for i in range(1, 8): for j in range(3): self.assertEqual(cells["triangle"][i][j], cells_ref["triangle"][i][j]) # cell_sets self.assertEqual(cell_sets, {}) # message out, err = out.getvalue().splitlines(), err.getvalue().splitlines() # strip times out, err = [line.split(None, 1)[1] for line in out], [line.split(None, 1)[1] for line in err] self.assertEqual(out, out_ref) self.assertEqual(err, err_ref)
def test_2_pins_new_material(self): """Test overlaying grid on 2 pins with a new grid material.""" ref_groups = groups_2_pins_new_mat ref_centroids = centroids_2_pins with captured_output() as (out, err): mocmg.initialize() gmsh.initialize() gmsh.model.occ.addDisk(1.0, 1.0, 0.0, 0.5, 0.5) gmsh.model.occ.addDisk(3.0, 1.0, 0.0, 0.5, 0.5) gmsh.model.occ.synchronize() p = gmsh.model.addPhysicalGroup(2, [1]) gmsh.model.setPhysicalName(2, p, "MATERIAL_UO2") p = gmsh.model.addPhysicalGroup(2, [2]) gmsh.model.setPhysicalName(2, p, "MATERIAL_MOX") overlay_rectangular_grid(bb_42, nx=[2], ny=[1], material="MATERIAL_NEW") out, err = out.getvalue().splitlines(), err.getvalue().splitlines() out = [line.split(None, 1)[1] for line in out] self.assertEqual(out, reference_out) self.assertEqual(err, []) group_nums = gmsh.model.getPhysicalGroups() names = [gmsh.model.getPhysicalName(*grp) for grp in group_nums] ref_names = list(ref_groups.keys()) # Check correct names/entities for i, name in enumerate(ref_names): self.assertEqual(name, names[i]) index = names.index(name) group_ents = list( gmsh.model.getEntitiesForPhysicalGroup(*group_nums[index])) ref_group_ents = ref_groups[name] self.assertEqual(group_ents, ref_group_ents) # Check correct area/centroid for ent in gmsh.model.getEntities(2): tag = ent[1] mass = gmsh.model.occ.getMass(2, tag) if tag == 1 or tag == 2: self.assertAlmostEqual(0.785398, mass, places=5, msg="pi*0.5**2") x, y, z = gmsh.model.occ.getCenterOfMass(2, tag) centroid = (x, y, z) for i in range(3): self.assertAlmostEqual(centroid[i], ref_centroids[tag][i]) gmsh.clear() gmsh.finalize()
def test_get_entities_for_physical_group(self): """Test the get_entities_for_physical_group_name for a regular use case.""" mocmg.initialize() gmsh.initialize() tag = gmsh.model.occ.addDisk(0.0, 0.0, 0.0, 1.0, 1.0) gmsh.model.occ.synchronize() output_tag = gmsh.model.addPhysicalGroup(2, [tag]) gmsh.model.setPhysicalName(2, output_tag, "Test Physical Group Name") ents = mocmg.gmsh_utils.get_entities_for_physical_group_name( "Test Physical Group Name") self.assertEqual([1], ents) gmsh.clear() gmsh.finalize()
def test_disks_mixed_topology_no_cell_sets(self): """Test writing xdmf file for two disks with mixed topology and no cell sets.""" filename = "mixed_topology_disks" vertices = two_disks_tri6_quad8_vertices cells = two_disks_tri6_quad8_cells cells_h5_ref = two_disks_tri6_quad8_cells_h5_ref out_ref = [ "INFO : mocmg.mesh.xdmf_IO - Writing mesh data to XDMF file '" + filename + ".xdmf'." ] err_ref = [] with captured_output() as (out, err): mocmg.initialize() mesh = mocmg.mesh.Mesh(vertices, cells) mocmg.mesh.write_xdmf_file(filename + ".xdmf", mesh) # message out, err = out.getvalue().splitlines(), err.getvalue().splitlines() # strip times out, err = [line.split(None, 1)[1] for line in out], [line.split(None, 1)[1] for line in err] self.assertEqual(out, out_ref) self.assertEqual(err, err_ref) # Check xdmf ref_file = open("./tests/mesh/xdmf_files/" + filename + ".xdmf", "r") test_file = open(filename + ".xdmf", "r") ref_lines = ref_file.readlines() test_lines = test_file.readlines() ref_file.close() test_file.close() self.assertEqual(len(ref_lines), len(test_lines)) for i in range(len(ref_lines)): self.assertEqual(ref_lines[i], test_lines[i]) # Check h5 with h5py.File(filename + ".h5", "r") as f: vertices_h5 = np.array(f.get(filename + "/vertices")) cells_h5 = np.array(f.get(filename + "/cells")) # Vertices for i, coord in enumerate(vertices.values()): for j in range(len(coord)): self.assertEqual(vertices_h5[i][j], coord[j]) # Cells for i in range(len(cells_h5_ref)): self.assertEqual(cells_h5[i], cells_h5_ref[i]) os.remove(filename + ".xdmf") os.remove(filename + ".h5")
def test_on_mixed_topology(self): """Test the mesh class functions on a mixed topology mesh.""" ref_vertices = two_disks_tri6_quad8_vertices ref_cells = two_disks_tri6_quad8_cells mocmg.initialize() mesh = mocmg.mesh.Mesh(ref_vertices, ref_cells) self.assertEqual(mesh.vertices, ref_vertices) self.assertEqual(mesh.cells, ref_cells) # get_vertices_for_cells verts_from_cells = mesh.get_vertices_for_cells([1, 8]) verts_from_cells_ref = [[5, 31, 4, 32, 33, 11], [19, 20, 39, 41, 27, 42, 43, 44]] for i, vset in enumerate(verts_from_cells_ref): for j, v in enumerate(vset): self.assertEqual(v, verts_from_cells[i][j])
def test_3_rectangles_1_not_in_frag(self): """Test 2 rectangles in frag, one off to the side.""" ref_groups = groups_3_rectangles_1_not_in_frag ref_centroids = centroids_3_rectangles_1_not_in_frag ref_areas = areas_3_rectangles_1_not_in_frag # Setup with captured_output() as (out, err): mocmg.initialize() gmsh.initialize() gmsh.model.occ.addRectangle(0.0, 0.0, 0.0, 2.0, 2.0) gmsh.model.occ.addRectangle(1.0, 1.0, 0.0, 2.0, 2.0) gmsh.model.occ.addRectangle(4.0, 4.0, 0.0, 2.0, 2.0) gmsh.model.occ.synchronize() gmsh.model.addPhysicalGroup(2, [1]) gmsh.model.addPhysicalGroup(2, [2]) gmsh.model.addPhysicalGroup(2, [3]) gmsh.model.addPhysicalGroup(2, [1, 2, 3]) gmsh.model.setPhysicalName(2, 1, "Group 1") gmsh.model.setPhysicalName(2, 2, "Group 2") gmsh.model.setPhysicalName(2, 3, "Group 3") gmsh.model.setPhysicalName(2, 4, "All") mocmg.model.group_preserving_fragment([(2, 1)], [(2, 2)]) out, err = out.getvalue().splitlines(), err.getvalue().splitlines() out = [line.split(None, 1)[1] for line in out] err = [line.split(None, 1)[1] for line in err] # strip times self.assertEqual(out, ref_out) self.assertEqual(err, []) # Get info group_nums = gmsh.model.getPhysicalGroups() names = [gmsh.model.getPhysicalName(*grp) for grp in group_nums] ref_names = list(ref_groups.keys()) # Check correct group names/entities for i, name in enumerate(names): self.assertEqual(name, ref_names[i]) index = names.index(name) group_ents = list(gmsh.model.getEntitiesForPhysicalGroup(*group_nums[index])) ref_group_ents = ref_groups[name] self.assertEqual(group_ents, ref_group_ents) # Check correct area/centroid for ent in gmsh.model.getEntities(2): tag = ent[1] mass = gmsh.model.occ.getMass(2, tag) self.assertAlmostEqual(ref_areas[tag], mass, places=5) x, y, z = gmsh.model.occ.getCenterOfMass(2, tag) centroid = (x, y, z) for i in range(3): self.assertAlmostEqual(centroid[i], ref_centroids[tag][i]) # Clean up gmsh.clear() gmsh.finalize()
def test_grid_linear_triangle(self): """Test the mesh class functions on a linear triangle mesh.""" ref_vertices = linear_triangle_vertices ref_cells = linear_triangle_cells ref_cell_sets = linear_triangle_cell_sets mocmg.initialize() mesh = mocmg.mesh.GridMesh(ref_vertices, ref_cells, ref_cell_sets) self.assertEqual(mesh.vertices, ref_vertices) self.assertEqual(mesh.cells, ref_cells) self.assertEqual(mesh.cell_sets, ref_cell_sets) # get_cells cell_set = mesh.get_cells("DISK") self.assertTrue( np.array_equal(cell_set, linear_triangle_cell_sets["DISK"])) # Try w/ name that doesnt exist. with self.assertRaises(SystemExit): cell_set = mesh.get_cells("BAD NAME") # get_cell_area cell_area_ref = 0.210453 cell_area = mesh.get_cell_area(1) self.assertAlmostEqual(cell_area, cell_area_ref, 6) # Try will cell that doesnt exist with self.assertRaises(SystemExit): cell_area = mesh.get_cell_area(-1) # get_set_area set_area_ref = 2.828427 set_area = mesh.get_set_area("DISK") self.assertAlmostEqual(set_area, set_area_ref, 6) # get_vertices_for_cells verts_from_cells = mesh.get_vertices_for_cells([1, 2, 3]) verts_from_cells_ref = [[1, 16, 13], [9, 16, 1], [12, 15, 4]] for i, vset in enumerate(verts_from_cells_ref): for j, v in enumerate(vset): self.assertEqual(v, verts_from_cells[i][j]) # cell that doesnt exist with self.assertRaises(SystemExit): verts_from_cells = mesh.get_vertices_for_cells([1111111]) # get_vertices for cell set verts_from_cell_set_name = mesh.get_vertices("DISK") verts_from_cell_set_name_ref = [ 1, 3, 4, 7, 9, 10, 11, 12, 13, 14, 15, 16 ] for i, vert in enumerate(verts_from_cell_set_name_ref): self.assertEqual(vert, verts_from_cell_set_name[i]) # cell set that doesnt exist with self.assertRaises(SystemExit): verts_from_cell_set_name = mesh.get_vertices("NO_NAME")
def test_verbosity_info(self): """Test verbosity='info'.""" with captured_output() as (out, err): mocmg.initialize(verbosity="info", exit_on_error=False) log = logging.getLogger(__name__) _test_log_messages(log) out, err = out.getvalue().splitlines(), err.getvalue().splitlines() out, err = [line.split(None, 1)[1] for line in out ], [line.split(None, 1)[1] for line in err] # strip times self.assertEqual(out, reference_out) self.assertEqual(err, reference_err) # check log file f = open("mocmg.log", "r") lines = f.readlines() f.close() lines = [line.split(None, 1)[1].rstrip("\n") for line in lines] self.assertEqual(out + err, lines)
def test_double_init(self): """Test behavior if initialize is called twice.""" with captured_output() as (out, err): mocmg.initialize(exit_on_error=False) mocmg.initialize(exit_on_error=False) log = logging.getLogger(__name__) _test_log_messages(log) out, err = out.getvalue().splitlines(), err.getvalue().splitlines() out, err = [line.split(None, 1)[1] for line in out ], [line.split(None, 1)[1] for line in err] # strip times self.assertEqual(out, reference_out) self.assertEqual(err, reference_err) # check log file f = open("mocmg.log", "r") lines = f.readlines() f.close() lines = [line.split(None, 1)[1].rstrip("\n") for line in lines] self.assertEqual(reference_out + reference_err, lines)
def test_y_noniterable_type(self): """Test rect grid with non-iterable type elements.""" with self.assertRaises(SystemExit): with captured_output() as (out, err): mocmg.initialize() gmsh.initialize() rectangular_grid(bb_11, x=[[1]], y=[1.0]) gmsh.clear() gmsh.finalize() out, err = out.getvalue().splitlines(), err.getvalue().splitlines() out = [line.split(None, 1)[1] for line in out] err = [line.split(None, 1)[1] for line in [err[0]]] # strip times self.assertEqual(out, reference_out) self.assertEqual(err, y_nonlist_type) # check log file f = open("mocmg.log", "r") lines = f.readlines() f.close() lines = [line.split(None, 1)[1].rstrip("\n") for line in lines] self.assertEqual(lines, reference_out + y_nonlist_type)
def test_arg_len_mismatch_nxny(self): """Test rect grid with mismatched arg len.""" with self.assertRaises(SystemExit): with captured_output() as (out, err): mocmg.initialize() gmsh.initialize() rectangular_grid(bb_11, x=[2, 3], y=[1]) gmsh.clear() gmsh.finalize() out, err = out.getvalue().splitlines(), err.getvalue().splitlines() out = [line.split(None, 1)[1] for line in out] err = [line.split(None, 1)[1] for line in [err[0]]] # strip times self.assertEqual(out, reference_out) self.assertEqual(err, len_mismatch) # check log file f = open("mocmg.log", "r") lines = f.readlines() f.close() lines = [line.split(None, 1)[1].rstrip("\n") for line in lines] self.assertEqual(lines, reference_out + len_mismatch)
def test_thick_dz(self): """Test rect grid with large change in z direction.""" with self.assertRaises(SystemExit): with captured_output() as (out, err): mocmg.initialize() gmsh.initialize() rectangular_grid(bb_dz, x=[[0.5]], y=[[0.5]]) gmsh.clear() gmsh.finalize() out, err = out.getvalue().splitlines(), err.getvalue().splitlines() out = [line.split(None, 1)[1] for line in out] err = [line.split(None, 1)[1] for line in [err[0]]] # strip times self.assertEqual(out, reference_out) self.assertEqual(err, thick_dz) # check log file f = open("mocmg.log", "r") lines = f.readlines() f.close() lines = [line.split(None, 1)[1].rstrip("\n") for line in lines] self.assertEqual(lines, reference_out + thick_dz)
def test_ny0(self): """Test ny with 0 division.""" with self.assertRaises(SystemExit): with captured_output() as (out, err): mocmg.initialize() gmsh.initialize() rectangular_grid(bb_11, nx=[1, 1], ny=[0, 1]) gmsh.clear() gmsh.finalize() out, err = out.getvalue().splitlines(), err.getvalue().splitlines() out = [line.split(None, 1)[1] for line in out] err = [line.split(None, 1)[1] for line in [err[0]]] # strip times self.assertEqual(out, reference_out) self.assertEqual(err, ny_type) # check log file f = open("mocmg.log", "r") lines = f.readlines() f.close() lines = [line.split(None, 1)[1].rstrip("\n") for line in lines] self.assertEqual(lines, reference_out + ny_type)
def test_y_out_of_bb(self): """Test rect grid with a y-division outside the bb.""" with self.assertRaises(SystemExit): with captured_output() as (out, err): mocmg.initialize() gmsh.initialize() rectangular_grid(bb_11, x=[[1.0]], y=[[-1.0]]) gmsh.clear() gmsh.finalize() out, err = out.getvalue().splitlines(), err.getvalue().splitlines() out = [line.split(None, 1)[1] for line in out] err = [line.split(None, 1)[1] for line in [err[0]]] # strip times self.assertEqual(out, reference_out) self.assertEqual(err, out_of_bb) # check log file f = open("mocmg.log", "r") lines = f.readlines() f.close() lines = [line.split(None, 1)[1].rstrip("\n") for line in lines] self.assertEqual(lines, reference_out + out_of_bb)
def test_get_entities_for_physical_group_bad_name(self): """Test the get_entities_for_physical_group_name for a regular use case.""" with self.assertRaises(SystemExit): with captured_output() as (out, err): mocmg.initialize() gmsh.initialize() tag = gmsh.model.occ.addDisk(0.0, 0.0, 0.0, 1.0, 1.0) gmsh.model.occ.synchronize() output_tag = gmsh.model.addPhysicalGroup(2, [tag]) gmsh.model.setPhysicalName(2, output_tag, "Test Physical Group Name") mocmg.gmsh_utils.get_entities_for_physical_group_name( "Bad name") out, err = out.getvalue().splitlines(), err.getvalue().splitlines() out = [line.split(None, 1)[1] for line in out] err = [line.split(None, 1)[1] for line in [err[0]]] # strip times self.assertEqual(out, []) self.assertEqual(err, bad_name) gmsh.clear() gmsh.finalize()
def test_bad_bounding_box(self): """Test a bad bounding box that produces negative dx, dy, dz.""" with self.assertRaises(SystemExit): with captured_output() as (out, err): mocmg.initialize() gmsh.initialize() rectangular_grid([0, 0, 0, -1, -1, -1], x=[[0.5]], y=[[0.5]]) gmsh.clear() gmsh.finalize() out, err = out.getvalue().splitlines(), err.getvalue().splitlines() out = [line.split(None, 1)[1] for line in out] err = [line.split(None, 1)[1] for line in [err[0]]] # strip times self.assertEqual(out, reference_out) self.assertEqual(err, bad_bb) # check log file f = open("mocmg.log", "r") lines = f.readlines() f.close() lines = [line.split(None, 1)[1].rstrip("\n") for line in lines] self.assertEqual(lines, reference_out + bad_bb)
def test_with_bad_type(self): """Test writing a non-mesh.""" filename = "bad_type" out_ref = [] err_ref = [ "ERROR : mocmg.mesh.xdmf_IO - Invalid type given as input." ] with self.assertRaises(SystemExit): with captured_output() as (out, err): mocmg.initialize() mesh = [1] mocmg.mesh.write_xdmf_file(filename + ".xdmf", mesh) # message out, err = out.getvalue().splitlines(), err.getvalue().splitlines() # strip times out, err = [line.split(None, 1)[1] for line in out ], [line.split(None, 1)[1] for line in [err[0]]] self.assertEqual(out, out_ref) self.assertEqual(err, err_ref)
def test_element_type_error(self): """Test reading a file with an unsupported element type.""" out_ref = [ "INFO : mocmg.mesh.abaqus_IO - " + "Reading mesh data from tests/mesh/abaqus_files/element_error.inp" ] err_ref = [ "ERROR : mocmg.mesh.abaqus_IO - Unrecognized mesh element type: 'MADEUPTYPE'." ] with self.assertRaises(SystemExit): with captured_output() as (out, err): mocmg.initialize() mocmg.mesh.read_abaqus_file( "tests/mesh/abaqus_files/element_error.inp") out, err = out.getvalue().splitlines(), err.getvalue().splitlines() out, err = [line.split(None, 1)[1] for line in out ], [line.split(None, 1)[1] for line in [err[0]]] self.assertEqual(out, out_ref) self.assertEqual(err, err_ref)
def test_linear_quadrilateral(self): """Test the mesh class functions on a linear quadrilateral mesh.""" ref_vertices = linear_quadrilateral_vertices ref_cells = linear_quadrilateral_cells ref_cell_sets = linear_quadrilateral_cell_sets mocmg.initialize() mesh = mocmg.mesh.Mesh(ref_vertices, ref_cells, ref_cell_sets) self.assertEqual(mesh.vertices, ref_vertices) self.assertEqual(mesh.cells, ref_cells) self.assertEqual(mesh.cell_sets, ref_cell_sets) # get_cells cell_set = mesh.get_cells("DISK") self.assertTrue(np.array_equal(cell_set, linear_quadrilateral_cell_sets["DISK"])) # get_cell_area cell_area_ref = 0.0874078 cell_area = mesh.get_cell_area(1) self.assertAlmostEqual(cell_area, cell_area_ref, 6) # get_set_area set_area_ref = 3.0614675 set_area = mesh.get_set_area("DISK") self.assertAlmostEqual(set_area, set_area_ref, 6)
def test_quadratic_quadrilateral(self): """Test the mesh class functions on a quadratic quadrilateral mesh.""" ref_vertices = quadratic_quadrilateral_vertices ref_cells = quadratic_quadrilateral_cells ref_cell_sets = quadratic_quadrilateral_cell_sets mocmg.initialize() mesh = mocmg.mesh.Mesh(ref_vertices, ref_cells, ref_cell_sets) self.assertEqual(mesh.vertices, ref_vertices) self.assertEqual(mesh.cells, ref_cells) self.assertEqual(mesh.cell_sets, ref_cell_sets) # get_cells cell_set = mesh.get_cells("DISK") self.assertTrue(np.array_equal(cell_set, quadratic_quadrilateral_cell_sets["DISK"])) # get_cell_area cell_area_ref = 0.7847974 cell_area = mesh.get_cell_area(1) self.assertAlmostEqual(cell_area, cell_area_ref, 6) # get_set_area set_area_ref = 3.1391907 set_area = mesh.get_set_area("DISK") self.assertAlmostEqual(set_area, set_area_ref, 6)
def test_grid_get_cell_area(self): """Test the get_cell_area function.""" verts = { 1: np.array([0.0, 0.0, 0.0]), 2: np.array([1.0, 0.0, 0.0]), 3: np.array([1.0, 1.0, 0.0]), 4: np.array([0.0, 1.0, 0.0]), 5: np.array([0.5, 0.0, 0.0]), 6: np.array([1.25, 0.5, 0.0]), 7: np.array([0.5, 1.0, 0.0]), 8: np.array([0.25, 0.5, 0.0]), } cells = { "quad8": { 1: np.array([1, 2, 3, 4, 5, 6, 7, 8]), }, } mocmg.initialize() mesh = mocmg.mesh.GridMesh(verts, cells) cell_area = mesh.get_cell_area(1) self.assertAlmostEqual(cell_area, 1.0, 6)
def test_exit_on_error_true(self): """Test exit_on_error=True.""" with self.assertRaises(SystemExit): with captured_output() as (out, err): mocmg.initialize(exit_on_error=True) log = logging.getLogger(__name__) log.debug("Debug message") log.info("Info message") log.warning("Warning message") log.error("Error message") out, err = out.getvalue().splitlines(), err.getvalue().splitlines() out = [line.split(None, 1)[1] for line in out] err = [line.split(None, 1)[1] for line in err[0:2]] # strip times self.assertEqual(out, reference_out) self.assertEqual(err, reference_err[0:2]) # check log file f = open("mocmg.log", "r") lines = f.readlines() f.close() lines = [line.split(None, 1)[1].rstrip("\n") for line in lines] self.assertEqual(reference_out + reference_err[0:2], lines)
def test_require_condition_not_bool(self): """Test the "require" log level with not isinstance(condition, bool).""" with self.assertRaises(SystemExit): with captured_output() as (out, err): mocmg.initialize(exit_on_error=True) log = logging.getLogger(__name__) log.debug("Debug message") log.info("Info message") log.warning("Warning message") log.require("not a bool", "Condition met") out, err = out.getvalue().splitlines(), err.getvalue().splitlines() out = [line.split(None, 1)[1] for line in out] err = [line.split(None, 1)[1] for line in err[0:2]] # strip times self.assertEqual(out, reference_out) self.assertEqual(err, [reference_err[0], require_err[0]]) # check log file f = open("mocmg.log", "r") lines = f.readlines() f.close() lines = [line.split(None, 1)[1].rstrip("\n") for line in lines] self.assertEqual(reference_out + [reference_err[0], require_err[0]], lines)
def test_disks_mixed_topology_with_incomplete_materials(self): """Test writing xdmf file that only has materials for some cells.""" filename = "mixed_topology_disks_with_incomplete_materials" vertices = two_disks_tri6_quad8_vertices cells = two_disks_tri6_quad8_cells cell_sets = { "Material DISK1": np.array([1, 2, 3, 4, 5, 6, 7]), "Material DISK2": np.array([8, 9, 10, 11]), } out_ref = [ "INFO : mocmg.mesh.xdmf_IO - Generating global material ID map.", "INFO : mocmg.mesh.xdmf_IO - Material Name : Material ID", "INFO : mocmg.mesh.xdmf_IO - ==================================", "INFO : mocmg.mesh.xdmf_IO - MATERIAL_DISK1 : 0", "INFO : mocmg.mesh.xdmf_IO - MATERIAL_DISK2 : 1", "INFO : mocmg.mesh.xdmf_IO - Writing mesh data to XDMF file '" + filename + ".xdmf'.", ] err_ref = [ "ERROR : mocmg.mesh.xdmf_IO - Total number of cells (13) not " + "equal to number of cells with a material (11)." ] with self.assertRaises(SystemExit): with captured_output() as (out, err): mocmg.initialize() mesh = mocmg.mesh.Mesh(vertices, cells, cell_sets=cell_sets) mocmg.mesh.write_xdmf_file(filename + ".xdmf", mesh) # message out, err = out.getvalue().splitlines(), err.getvalue().splitlines() # strip times out = [line.split(None, 1)[1] for line in out] err = [line.split(None, 1)[1] for line in [err[0]]] self.assertEqual(out, out_ref) self.assertEqual(err, err_ref) os.remove(filename + ".h5")
def test_with_children(self): """Initialize and test a grid mesh with children.""" mocmg.initialize() mesh1 = mocmg.mesh.GridMesh(pin_1_vertices, pin_1_cells, name="pin1") self.assertEqual(mesh1.vertices, pin_1_vertices) self.assertEqual(mesh1.cells, pin_1_cells) mesh2 = mocmg.mesh.GridMesh(pin_2_vertices, pin_2_cells, name="pin2") self.assertEqual(mesh2.vertices, pin_2_vertices) self.assertEqual(mesh2.cells, pin_2_cells) # Init with normal data and children with self.assertRaises(SystemExit): mocmg.mesh.GridMesh(pin_1_vertices, pin_1_cells, children=[mesh1, mesh2]) # init with children both_pins_mesh = mocmg.mesh.GridMesh(children=[mesh1, mesh2], name="both_pins") # Check relationships ref_names = ["pin1", "pin2"] for i, mesh in enumerate(both_pins_mesh.children): name = mesh.name self.assertEqual(name, ref_names[i]) self.assertEqual("both_pins", mesh.parent.name)