📄 dolfin-convert
字号:
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 # Modify line if "xmlns" in line: line = "<dolfin xmlns:dolfin=\"http://www.fenics.org/dolfin/\">\n" if "<mesh>" in line: line = " <mesh celltype=\"%s\" dim=\"%d\">\n" % (cell_type, dim) if dim == 2 and " z=\"0.0\"" in line: line = line.replace(" z=\"0.0\"", "") if " name=" in line: line = line.replace(" name=", " index=") if " name =" in line: line = line.replace(" name =", " index=") if "n0" in line: line = line.replace("n0", "v0") if "n1" in line: line = line.replace("n1", "v1") if "n2" in line: line = line.replace("n2", "v2") if "n3" in line: line = line.replace("n3", "v3") # Write line ofile.write(line) # Close files ifile.close(); ofile.close(); print "Conversion done"def metis_graph2graph_xml(ifilename, ofilename): "Convert from Metis graph format to DOLFIN Graph XML." print "Converting from Metis graph format to DOLFIN Graph XML." # Open files ifile = open(ifilename, "r") ofile = open(ofilename, "w") # Read number of vertices and edges line = ifile.readline() if not line: error("Empty file") (num_vertices, num_edges) = line.split() write_header_graph(ofile, "undirected") write_header_vertices(ofile, int(num_vertices)) for i in range(int(num_vertices)): line = ifile.readline() edges = line.split() write_graph_vertex(ofile, i, len(edges)) write_footer_vertices(ofile) write_header_edges(ofile, int(num_edges)) # Step to beginning of file and skip header info ifile.seek(0) ifile.readline() for i in range(int(num_vertices)): line = ifile.readline() edges = line.split() for e in edges: if i < int(e): write_graph_edge(ofile, i, int(e)) write_footer_edges(ofile) write_footer_graph(ofile) # Close files ifile.close(); ofile.close();def scotch_graph2graph_xml(ifilename, ofilename): "Convert from Scotch graph format to DOLFIN Graph XML." print "Converting from Scotch graph format to DOLFIN Graph XML." # Open files ifile = open(ifilename, "r") ofile = open(ofilename, "w") # Skip graph file version number ifile.readline() # Read number of vertices and edges line = ifile.readline() if not line: error("Empty file") (num_vertices, num_edges) = line.split() # Read start index and numeric flag # Start index is 0 or 1 (C/Fortran) # Numeric flag is 3 bits where bit 1 enables vertex labels # bit 2 enables edge weights and bit 3 enables vertex weights line = ifile.readline() (start_index, numeric_flag) = line.split() # Handling not implented if not numeric_flag == "000": error("Handling of scotch vertex labels, edge- and vertex weights not implemented") write_header_graph(ofile, "undirected") write_header_vertices(ofile, int(num_vertices)) # Read vertices and edges, first number gives number of edges from this vertex (not used) for i in range(int(num_vertices)): line = ifile.readline() edges = line.split() write_graph_vertex(ofile, i, len(edges)-1) write_footer_vertices(ofile) write_header_edges(ofile, int(num_edges)/2) # Step to beginning of file and skip header info ifile.seek(0) ifile.readline() ifile.readline() ifile.readline() for i in range(int(num_vertices)): line = ifile.readline() edges = line.split() for j in range(1, len(edges)): if i < int(edges[j]): write_graph_edge(ofile, i, int(edges[j])) write_footer_edges(ofile) write_footer_graph(ofile) # Close files ifile.close(); ofile.close();def diffpack2xml(ifilename, ofilename): "Convert from Diffpack tetrahedral grid format to DOLFIN XML." print diffpack2xml.__doc__ # Format strings for MeshFunction XML files meshfunction_header = """\<?xml version="1.0" encoding="UTF-8"?>\n<dolfin xmlns:dolfin="http://www.fenics.org/dolfin/"> <meshfunction type="uint" dim="%d" size="%d">\n""" meshfunction_entity = " <entity index=\"%d\" value=\"%d\"/>\n" meshfunction_footer = " </meshfunction>\n</dolfin>" # Open files ifile = open(ifilename, "r") ofile = open(ofilename, "w") ofile_mat = open(ofilename.split(".")[0]+"_mat.xml", "w") ofile_bi = open(ofilename.split(".")[0]+"_bi.xml", "w") # Read and analyze header while 1: line = ifile.readline() if not line: error("Empty file") if line[0] == "#": break if re.search(r"Number of elements", line): num_cells = int(re.match(r".*\s(\d+).*", line).group(1)) if re.search(r"Number of nodes", line): num_vertices = int(re.match(r".*\s(\d+).*", line).group(1)) write_header_mesh(ofile, "tetrahedron", 3) write_header_vertices(ofile, num_vertices) ofile_bi.write(meshfunction_header % (0, num_vertices)) ofile_mat.write(meshfunction_header % (3, num_cells)) # Read & write vertices # Note that only first boundary indicator is rewriten into XML for i in range(num_vertices): line = ifile.readline() m = re.match(r"^.*\(\s*(.*)\s*\).*\](.*)$", line) x = re.split("[\s,]+", m.group(1)) write_vertex(ofile, i, x[0], x[1], x[2]) tmp = m.group(2).split() if len(tmp) > 0: bi = int(tmp[0]) else: bi = 0 ofile_bi.write(meshfunction_entity % (i, bi)) write_footer_vertices(ofile) write_header_cells(ofile, num_cells) # Ignore comment lines while 1: line = ifile.readline() if not line: error("Empty file") if line[0] == "#": break # Read & write cells for i in range(int(num_cells)): line = ifile.readline() v = line.split(); if v[1] != "ElmT4n3D": error("Only tetrahedral elements (ElmT4n3D) are implemented.") write_cell_tetrahedron(ofile, i, int(v[3])-1, int(v[4])-1, int(v[5])-1, int(v[6])-1) ofile_mat.write(meshfunction_entity % (i, int(v[2]))) write_footer_cells(ofile) write_footer_mesh(ofile) ofile_bi.write(meshfunction_footer) ofile_mat.write(meshfunction_footer) # Close files ifile.close() ofile.close() ofile_mat.close() ofile_bi.close() # Write mesh headerdef write_header_mesh(ofile, cell_type, dim): ofile.write("""\<?xml version=\"1.0\" encoding=\"UTF-8\"?><dolfin xmlns:dolfin=\"http://www.fenics.org/dolfin/\"> <mesh celltype="%s" dim="%d">""" % (cell_type, dim))# Write graph headerdef write_header_graph(ofile, graph_type): ofile.write("""\<?xml version=\"1.0\" encoding=\"UTF-8\"?><dolfin xmlns:dolfin=\"http://www.fenics.org/dolfin/\"> <graph type="%s">""" % (graph_type))# Write mesh footerdef write_footer_mesh(ofile): ofile.write("""\ </mesh></dolfin>""")# Write graph footerdef write_footer_graph(ofile): ofile.write("""\ </graph></dolfin>""")def write_header_vertices(ofile, num_vertices): "Write vertices header" print "Expecting %d vertices" % num_vertices ofile.write(" <vertices size=\"%d\">\n" % num_vertices)def write_footer_vertices(ofile): "Write vertices footer" ofile.write(" </vertices>\n") print "Found all vertices"def write_header_edges(ofile, num_edges): "Write edges header" print "Expecting %d edges" % num_edges ofile.write(" <edges size=\"%d\">\n" % num_edges)def write_footer_edges(ofile): "Write edges footer" ofile.write(" </edges>\n") print "Found all edges"def write_vertex(ofile, vertex, x, y, z): "Write vertex" ofile.write(" <vertex index=\"%d\" x=\"%s\" y=\"%s\" z=\"%s\"/>\n" % \ (vertex, x, y, z))def write_graph_vertex(ofile, vertex, num_edges, weight = 1): "Write graph vertex" ofile.write(" <vertex index=\"%d\" num_edges=\"%d\" weight=\"%d\"/>\n" % \ (vertex, num_edges, weight))def write_graph_edge(ofile, v1, v2, weight = 1): "Write graph edge" ofile.write(" <edge v1=\"%d\" v2=\"%d\" weight=\"%d\"/>\n" % \ (v1, v2, weight))def write_header_cells(ofile, num_cells): "Write cells header" ofile.write(" <cells size=\"%d\">\n" % num_cells) print "Expecting %d cells" % num_cellsdef write_footer_cells(ofile): "Write cells footer" ofile.write(" </cells>\n") print "Found all cells"def write_cell_triangle(ofile, cell, n0, n1, n2): "Write cell (triangle)" ofile.write(" <triangle index=\"%d\" v0=\"%d\" v1=\"%d\" v2=\"%d\"/>\n" % \ (cell, n0, n1, n2))def write_cell_tetrahedron(ofile, cell, n0, n1, n2, n3): "Write cell (tetrahedron)" ofile.write(" <tetrahedron index=\"%d\" v0=\"%d\" v1=\"%d\" v2=\"%d\" v3=\"%d\"/>\n" % \ (cell, n0, n1, n2, n3))if __name__ == "__main__": main(sys.argv[1:])
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -