dolfin team mailing list archive
-
dolfin team
-
Mailing list archive
-
Message #03876
dolfin-convert: bug with conversion from gmsh?
Hi again,
I read the manual... so, here is the patch. To convert a Gmsh mesh one
must add an extra option to define the element type:
dolfin-convert -i gmsh -o xml -e <el_type> mesh.msh mesh.xml
where <el_type> is either "tria" for triangles or "tet" for tetrahedra.
Dolfin-convert will complain if one attempts to convert a Gmsh mesh
without the -e option. The patch has been tested with triangular meshes
but is should work fine with tetraheda as well.
angelo
diff -u --new-file --recursive dolfin-0.6.4/src/utils/convert/dolfin-convert dolfin-0.6.4-mod/src/utils/convert/dolfin-convert
--- dolfin-0.6.4/src/utils/convert/dolfin-convert 2006-12-01 11:43:25.000000000 +0100
+++ dolfin-0.6.4-mod/src/utils/convert/dolfin-convert 2006-12-02 05:10:39.000000000 +0100
@@ -5,11 +5,13 @@
#
# Modified by Garth N. Wells (gmsh function)
# Modified by Alexander H. Jarosch (gmsh fix)
+# Modified by Angelo Simone (gmsh fix)
#
# Script for converting between various data formats
import getopt
import sys
+import os
from commands import getoutput
def main(argv):
@@ -17,7 +19,7 @@
# Get command-line arguments
try:
- opts, args = getopt.getopt(argv, "hi:o:", ["help", "input=", "output="])
+ opts, args = getopt.getopt(argv, "hi:o:e:", ["help", "input=", "output=", "element="])
except getopt.GetoptError:
usage()
sys.exit(2)
@@ -25,6 +27,7 @@
# Get options
input_format = ""
output_format = ""
+ el_type = ""
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
@@ -33,6 +36,8 @@
input_format = arg
elif opt in ("-o", "--output"):
output_format = arg
+ elif opt in ("-e", "--element"):
+ el_type = arg
# Check that we got two filenames
if not len(args) == 2:
@@ -42,6 +47,8 @@
# Get filenames and suffixes
ifilename = args[0]
ofilename = args[1]
+ tfilename = "fooAS3934.tmp"
+
isuffix = ifilename.split(".")[-1]
osuffix = ofilename.split(".")[-1]
@@ -55,7 +62,7 @@
if input_format == "mesh" and output_format == "xml":
mesh2xml(ifilename, ofilename)
elif input_format == "gmsh" and output_format == "xml":
- gmsh2xml(ifilename, ofilename)
+ gmsh2xml(ifilename, ofilename, tfilename, el_type)
elif input_format == "xml-old" and output_format == "xml":
xml_old2xml(ifilename, ofilename)
else:
@@ -71,12 +78,14 @@
-h display this help text and exit
-i format specify input format
-o format specify output format
+ -e element specify element type
Alternatively, the following long options may be used:
--help same as -h
--input same as -i
--output same as -o
+ --element same as -e
Supported formats:
@@ -85,6 +94,11 @@
mesh - Medit, generated by tetgen with option -g
gmsh - Gmsh, version 2.0 file format
+Supported elements (active with gmsh mesh format)
+
+ tria - triangular element
+ tet - tetrahedral element
+
If --input or --output are not specified, the format will
be deduced from the suffix:
@@ -233,7 +247,7 @@
ifile.close()
ofile.close()
-def gmsh2xml(ifilename, ofilename):
+def gmsh2xml(ifilename, ofilename, tfilename, el_type):
"""Convert between .gmsh v2.0 format (http://www.geuz.org/gmsh/) and .xml,
parser implemented as a state machine:
@@ -251,14 +265,22 @@
"""
- print "Converting from Gmsh format (.msh, .gmsh) to DOLFIN XML format"
+ print "Converting from Gmsh format (.msh, .gmsh) to DOLFIN (>0.6.2) XML format"
+
+ if el_type == "tria":
+ print "Using triangles"
+ elif el_type == "tet":
+ print "Using Tetrahedra"
+ else:
+ usage()
+ error(" Element type not recognized or not specified")
# Open files
ifile = open(ifilename, "r")
- ofile = open(ofilename, "w")
+ ofile = open(tfilename, "w")
# Write header
- write_header(ofile)
+ write_headerN(ofile, el_type)
# Initialise node list (gmsh does not export all vertexes in order)
nodelist = {}
@@ -269,6 +291,7 @@
# Write data
num_vertices_read = 0
num_cells_read = 0
+ num_lines_written = 0
while 1:
@@ -306,7 +329,12 @@
x = float(x)
y = float(y)
z = float(z)
- write_vertex(ofile, num_vertices_read, x, y, z)
+
+ if el_type == "tria":
+ write_vertexTria(ofile, num_vertices_read, x, y)
+ elif el_type == "tet":
+ write_vertex(ofile, num_vertices_read, x, y, z)
+
num_vertices_read +=1
if num_vertices == num_vertices_read:
@@ -324,7 +352,7 @@
print "No cells found in gmsh file."
break
else:
- write_header_cells(ofile, num_cells)
+ write_header_cellsN(ofile)
state += 1
elif state == 9:
element = line.split()
@@ -338,7 +366,8 @@
n0 = nodelist[nn[0]]
n1 = nodelist[nn[1]]
n2 = nodelist[nn[2]]
- write_cell_triangle(ofile, num_cells_read, n0, n1, n2)
+ write_cell_triangle(ofile, num_lines_written, n0, n1, n2)
+ num_lines_written += 1
elif cell_type == 4:
nn = [int(node) for node in element[3 + num_tags:9]]
for node in nn:
@@ -348,12 +377,14 @@
n1 = nodelist[nn[1]]
n2 = nodelist[nn[2]]
n3 = nodelist[nn[3]]
- write_cell_tetrahedron(ofile, num_cells_read, n0, n1, n2, n3)
+ write_cell_tetrahedron(ofile, num_lines_written, n0, n1, n2, n3)
+ num_lines_written += 1
num_cells_read +=1
if num_cells == num_cells_read:
- write_footer_cells(ofile)
- state += 1
+ print "Expecting %d cells" % num_lines_written
+ write_footer_cells(ofile)
+ state += 1
elif state == 10:
break
@@ -370,6 +401,36 @@
# Close files
ifile.close()
ofile.close()
+
+ # -- Add cell number
+
+ # Open files
+ ifile = open(tfilename, "r")
+ ofile = open(ofilename, "w")
+
+ # Read lines and make changes
+ while 1:
+
+ # Read next line
+ line = ifile.readline()
+ if not line: break
+
+ # Modify line
+ if "<cells size=" in line:
+ line = line.replace('<cells size=>', '<cells size="%d">') % num_lines_written
+
+ # Write line
+ ofile.write(line)
+
+ ofile.write("\n")
+
+ # Close files
+ ifile.close();
+ ofile.close();
+
+ # Remove tmp file
+ os.system('rm -f fooAS3934.tmp')
+
def xml_old2xml(ifilename, ofilename):
"Convert from old DOLFIN XML format to new."
@@ -437,6 +498,23 @@
<mesh>
""")
+# Write mesh header
+def write_headerN(ofile, el_type):
+ if el_type == "tria":
+ ofile.write("""\
+<?xml version=\"1.0\" encoding=\"UTF-8\"?>
+
+<dolfin xmlns:dolfin=\"http://www.fenics.org/dolfin/\">
+ <mesh celltype="triangle" dim="2">
+""")
+ elif el_type == "tet":
+ ofile.write("""\
+<?xml version=\"1.0\" encoding=\"UTF-8\"?>
+
+<dolfin xmlns:dolfin=\"http://www.fenics.org/dolfin/\">
+ <mesh celltype="tetrahedron" dim="3">
+""")
+
# Write mesh footer
def write_footer(ofile):
ofile.write("""\
@@ -456,14 +534,23 @@
def write_vertex(ofile, vertex, x, y, z):
"Write vertex"
- ofile.write(" <vertex name=\"%d\" x=\"%g\" y=\"%g\" z=\"%g\"/>\n" % \
+ ofile.write(" <vertex index=\"%d\" x=\"%g\" y=\"%g\" z=\"%g\"/>\n" % \
(vertex, x, y, z))
+def write_vertexTria(ofile, vertex, x, y):
+ "Write vertex"
+ ofile.write(" <vertex index=\"%d\" x=\"%g\" y=\"%g\"/>\n" % \
+ (vertex, x, y))
+
def write_header_cells(ofile, num_cells):
"Write cells header"
ofile.write(" <cells size=\"%d\">\n" % num_cells)
print "Expecting %d cells" % num_cells
+def write_header_cellsN(ofile):
+ "Write cells header"
+ ofile.write(" <cells size=>\n")
+
def write_footer_cells(ofile):
"Write cells footer"
ofile.write(" </cells>\n")
@@ -471,14 +558,15 @@
def write_cell_triangle(ofile, cell, n0, n1, n2):
"Write cell (triangle)"
- ofile.write(" <triangle name=\"%d\" n0=\"%d\" n1=\"%d\" n2=\"%d\"/>\n" % \
+ 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 name=\"%d\" n0=\"%d\" n1=\"%d\" n2=\"%d\" n3=\"%d\"/>\n" % \
+ 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:])
Follow ups