📄 meshconvert.py
字号:
# Copyright (C) 2006 Anders Logg# Licensed under the GNU LGPL Version 2.1## Modified by Garth N. Wells (gmsh function)# Modified by Alexander H. Jarosch (gmsh fix)# Modified by Angelo Simone (Gmsh and Medit fix)# Modified by Andy R. Terrel (gmsh fix)# Modified by Magnus Vikstrom (metis and scotch function)# Modified by Bartosz Sawicki (diffpack function)# Modified by Gideon Simpson (Exodus II function)# Modified by Arve Knudsen (make into module, abaqus support)# Modified by Kent-Andre Mardal (Star-CD function)""" Module for converting various mesh formats."""import getoptimport sysfrom commands import getoutputimport reimport warningsimport os.pathdef format_from_suffix(suffix): "Return format for given suffix" if suffix == "xml": return "xml" elif suffix == "mesh": return "mesh" elif suffix == "gmsh": return "gmsh" elif suffix == "msh": return "gmsh" elif suffix == "gra": return "metis" elif suffix == "grf": return "scotch" elif suffix == "grid": return "diffpack" elif suffix == "inp": return "abaqus" elif suffix == "ncdf": return "NetCDF" elif suffix =="exo": return "ExodusII" elif suffix =="e": return "ExodusII" elif suffix == "vrt" or suffix == "cel": return "StarCD" else: _error("Sorry, unknown suffix %s." % suffix)def mesh2xml(ifilename, ofilename): """Convert between .mesh and .xml, parser implemented as a state machine: 0 = read 'Dimension' 1 = read dimension 2 = read 'Vertices' 3 = read number of vertices 4 = read next vertex 5 = read 'Triangles' or 'Tetrahedra' 6 = read number of cells 7 = read next cell 8 = done """ print "Converting from Medit format (.mesh) to DOLFIN XML format" # Open files ifile = open(ifilename, "r") ofile = open(ofilename, "w") # Scan file for cell type cell_type = None dim = 0 while 1: # Read next line line = ifile.readline() if not line: break # Remove newline if line[-1] == "\n": line = line[:-1] # Read dimension if line == "Dimension": line = ifile.readline() num_dims = int(line) if num_dims == 2: cell_type = "triangle" dim = 2 elif num_dims == 3: cell_type = "tetrahedron" dim = 3 break # Check that we got the cell type if cell_type == None: _error("Unable to find cell type.") # Step to beginning of file ifile.seek(0) # Write header write_header_mesh(ofile, cell_type, dim) # Current state state = 0 # Write data num_vertices_read = 0 num_cells_read = 0 while 1: # Read next line line = ifile.readline() if not line: break # Skip comments if line[0] == '#': continue # Remove newline if line[-1] == "\n": line = line[:-1] if state == 0: if line == "Dimension": state += 1 elif state == 1: num_dims = int(line) state +=1 elif state == 2: if line == "Vertices": state += 1 elif state == 3: num_vertices = int(line) write_header_vertices(ofile, num_vertices) state +=1 elif state == 4: if num_dims == 2: (x, y, tmp) = line.split() x = float(x) y = float(y) z = 0.0 elif num_dims == 3: (x, y, z, tmp) = line.split() x = float(x) y = float(y) z = float(z) write_vertex(ofile, num_vertices_read, x, y, z) num_vertices_read +=1 if num_vertices == num_vertices_read: write_footer_vertices(ofile) state += 1 elif state == 5: if line == "Triangles" and num_dims == 2: state += 1 if line == "Tetrahedra" and num_dims == 3: state += 1 elif state == 6: num_cells = int(line) write_header_cells(ofile, num_cells) state +=1 elif state == 7: if num_dims == 2: (n0, n1, n2, tmp) = line.split() n0 = int(n0) - 1 n1 = int(n1) - 1 n2 = int(n2) - 1 write_cell_triangle(ofile, num_cells_read, n0, n1, n2) elif num_dims == 3: (n0, n1, n2, n3, tmp) = line.split() n0 = int(n0) - 1 n1 = int(n1) - 1 n2 = int(n2) - 1 n3 = int(n3) - 1 write_cell_tetrahedron(ofile, num_cells_read, n0, n1, n2, n3) num_cells_read +=1 if num_cells == num_cells_read: write_footer_cells(ofile) state += 1 elif state == 8: break # Check that we got all data if state == 8: print "Conversion done" else: _error("Missing data, unable to convert") # Write footer write_footer_mesh(ofile) # Close files ifile.close() ofile.close()def gmsh2xml(ifilename, ofilename): """Convert between .gmsh v2.0 format (http://www.geuz.org/gmsh/) and .xml, parser implemented as a state machine: 0 = read 'MeshFormat' 1 = read mesh format data 2 = read 'EndMeshFormat' 3 = read 'Nodes' 4 = read number of vertices 5 = read vertices 6 = read 'EndNodes' 7 = read 'Elements' 8 = read number of cells 9 = read cells 10 = done """ print "Converting from Gmsh format (.msh, .gmsh) to DOLFIN XML format" # Open files ifile = open(ifilename, "r") ofile = open(ofilename, "w") # Scan file for cell type cell_type = None dim = 0 line = ifile.readline() while line: # Remove newline if line[-1] == "\n": line = line[:-1] # Read dimension if line.find("$Elements") == 0: line = ifile.readline() num_cells = int(line) num_cells_counted = 0 if num_cells == 0: _error("No cells found in gmsh file.") line = ifile.readline() # Now iterate through elements to find largest dimension. Gmsh # format might include elements of lower dimensions in the element list. # We also need to count number of elements of correct dimensions. # Also determine which vertices are not used. dim_2_count = 0 dim_3_count = 0 vertices_2_used = [] vertices_3_used = [] while line.find("$EndElements") == -1: element = line.split() elem_type = int(element[1]) num_tags = int(element[2]) if elem_type == 2: if dim < 2: cell_type = "triangle" dim = 2 node_num_list = [int(node) for node in element[3 + num_tags:]] vertices_2_used.extend(node_num_list) dim_2_count += 1 elif elem_type == 4: if dim < 3: cell_type = "tetrahedron" dim = 3 vertices_2_used = None node_num_list = [int(node) for node in element[3 + num_tags:]] vertices_3_used.extend(node_num_list) dim_3_count += 1 line = ifile.readline() else: # Read next line line = ifile.readline() # Check that we got the cell type and set num_cells_counted if cell_type == None: _error("Unable to find cell type.") if dim == 3: num_cells_counted = dim_3_count vertex_set = set(vertices_3_used) vertices_3_used = None elif dim == 2: num_cells_counted = dim_2_count vertex_set = set(vertices_2_used) vertices_2_used = None vertex_dict = {} for n,v in enumerate(vertex_set): vertex_dict[v] = n # Step to beginning of file ifile.seek(0) # Write header write_header_mesh(ofile, cell_type, dim) # Initialise node list (gmsh does not export all vertexes in order) nodelist = {} # Current state state = 0 # Write data num_vertices_read = 0 num_cells_read = 0 while state != 10: # Read next line line = ifile.readline() if not line: break # Skip comments if line[0] == '#': continue # Remove newline if line[-1] == "\n": line = line[:-1] if state == 0: if line == "$MeshFormat": state = 1 elif state == 1: (version, file_type, data_size) = line.split() state = 2 elif state == 2: if line == "$EndMeshFormat": state = 3 elif state == 3: if line == "$Nodes": state = 4 elif state == 4: #num_vertices = int(line) num_vertices = len(vertex_dict) write_header_vertices(ofile, num_vertices) state = 5 elif state == 5: (node_no, x, y, z) = line.split() if vertex_dict.has_key(int(node_no)): node_no = vertex_dict[int(node_no)] else: continue nodelist[int(node_no)] = num_vertices_read write_vertex(ofile, num_vertices_read, x, y, z) num_vertices_read +=1 if num_vertices == num_vertices_read: write_footer_vertices(ofile) state = 6 elif state == 6: if line == "$EndNodes": state = 7 elif state == 7: if line == "$Elements": state = 8 elif state == 8: write_header_cells(ofile, num_cells_counted) state = 9 elif state == 9: element = line.split() elem_type = int(element[1]) num_tags = int(element[2]) if elem_type == 2 and dim == 2: node_num_list = [vertex_dict[int(node)] for node in element[3 + num_tags:]] for node in node_num_list: if not node in nodelist: _error("Vertex %d of triangle %d not previously defined." % (node, num_cells_read)) n0 = nodelist[node_num_list[0]] n1 = nodelist[node_num_list[1]] n2 = nodelist[node_num_list[2]] write_cell_triangle(ofile, num_cells_read, n0, n1, n2) num_cells_read +=1 elif elem_type == 4 and dim == 3: node_num_list = [vertex_dict[int(node)] for node in element[3 + num_tags:]] for node in node_num_list: if not node in nodelist: _error("Vertex %d of tetrahedron %d not previously defined." % (node, num_cells_read)) n0 = nodelist[node_num_list[0]] n1 = nodelist[node_num_list[1]] n2 = nodelist[node_num_list[2]] n3 = nodelist[node_num_list[3]] write_cell_tetrahedron(ofile, num_cells_read, n0, n1, n2, n3) num_cells_read +=1 if num_cells_counted == num_cells_read: write_footer_cells(ofile) state = 10 elif state == 10: break # Check that we got all data if state == 10: print "Conversion done" else: _error("Missing data, unable to convert \n\ Did you use version 2.0 of the gmsh file format?") # Write footer write_footer_mesh(ofile) # Close files ifile.close() ofile.close()def xml_old2xml(ifilename, ofilename): "Convert from old DOLFIN XML format to new." print "Converting from old (pre DOLFIN 0.6.2) to new DOLFIN XML format..." # Open files ifile = open(ifilename, "r") ofile = open(ofilename, "w") # Scan file for cell type (assuming there is just one) cell_type = None dim = 0 while 1: # Read next line line = ifile.readline() if not line: break # Read dimension if "<triangle" in line: cell_type = "triangle" dim = 2 break elif "<tetrahedron" in line: cell_type = "tetrahedron" dim = 3 break # Step to beginning of file ifile.seek(0) # Read lines and make changes while 1: # Read next line line = ifile.readline() if not line: break
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -