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() xml_writer.write_header_graph(ofile, "directed") xml_writer.write_header_vertices(ofile, int(num_vertices)) for i in range(int(num_vertices)): line = ifile.readline() edges = line.split() xml_writer.write_graph_vertex(ofile, i, len(edges)) xml_writer.write_footer_vertices(ofile) xml_writer.write_header_edges(ofile, 2*int(num_edges)) # Step to beginning of file and skip header info ifile.seek(0) ifile.readline() for i in range(int(num_vertices)): print "vertex %g", i line = ifile.readline() edges = line.split() for e in edges: xml_writer.write_graph_edge(ofile, i, int(e)) xml_writer.write_footer_edges(ofile) xml_writer.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") xml_writer.write_header_graph(ofile, "undirected") xml_writer.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() xml_writer.write_graph_vertex(ofile, i, len(edges)-1) xml_writer.write_footer_vertices(ofile) xml_writer.write_header_edges(ofile, int(num_edges)) # 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)): xml_writer.write_graph_edge(ofile, i, int(edges[j])) xml_writer.write_footer_edges(ofile) xml_writer.write_footer_graph(ofile) # Close files ifile.close(); ofile.close();