← Back to team overview

dolfin team mailing list archive

Re: dolfin-convert: bug with conversion from gmsh?

 

You could try generating some with tetgen.

When you submit the patch, please also include some (very small) test
meshes that you have run the code on. We can add these for future
reference.

Thanks
/Anders


On Tue, Dec 05, 2006 at 01:56:00AM +0100, angelo simone wrote:
> Exactly. Anyway, I got rid of the ugly hack...  ;-)   
> 
> Before sending a bundle, I would like to test the new version on 2d and 
> 3d Medit meshes, which I do not have. Does anyone have them? Thanks!
> 
> angelo
> 
> 
> Anders Logg wrote:
> > I think because the new mesh format requires celltype in the header of
> > mesh, so the mesh needs to be scanned before writing to find out the
> > celltype.
> >
> > Instead of doing this, the patch first generates an illegal xml file
> > missing the celltype attribute and then scans the generated file to
> > grep for the cell type and then fixes the header...
> >
> > It works but it's not a nice solution. Angelo, please submit an
> > updated patch that first scans the file for the cell type (preferably
> > not using grep).
> >
> > /Anders
> >
> > PS: Yes, I know you looked at xml_old2xml() that I wrote and I do use
> > grep, but it's still an ugly hack. :-)
> >
> >
> > On Mon, Dec 04, 2006 at 11:06:21AM +0100, Garth N. Wells wrote:
> >   
> >> Why are temporary files being created? Isn't updating to the new mesh 
> >> xml format just a case of changing the output formatting?
> >>
> >> Garth
> >>
> >> angelo simone wrote:
> >>     
> >>> hi,
> >>>
> >>> I have fixed dolfin-convert (patch attached). Now the output is in the 
> >>> current format for both Gmsh and Medit. I have tested dolfin-convert 
> >>> with triangular Gmsh meshes but is should work fine with all the 
> >>> remaining combinations as well.
> >>>
> >>> Regarding the addition to the user manual, maybe we should just add that 
> >>> on top of the steps described in the Gmsh mesh generation tutorial 
> >>> (http://www.geuz.org/gmsh/doc/gui_tutorial/gui_tutorial.pdf) one needs 
> >>> to create physical surfaces/volumes as well to generate a 
> >>> dolfin-friendly mesh -- yesterday I have used Gmsh for the first time... 
> >>> that's why I did that mistake. Anyway, dolfin-convert complains if the 
> >>> mesh is not in the correct format.
> >>>
> >>> 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 20:41:10.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 and Medit new format fix)
> >>>  #
> >>>  # Script for converting between various data formats
> >>>  
> >>>  import getopt
> >>>  import sys
> >>> +import os
> >>>  from commands import getoutput
> >>>  
> >>>  def main(argv):
> >>> @@ -42,6 +44,7 @@
> >>>      # Get filenames and suffixes
> >>>      ifilename = args[0]
> >>>      ofilename = args[1]
> >>> +    tfilename = ifilename+".tmp"
> >>>      isuffix = ifilename.split(".")[-1]
> >>>      osuffix = ofilename.split(".")[-1]
> >>>  
> >>> @@ -53,9 +56,9 @@
> >>>  
> >>>      # Choose conversion
> >>>      if input_format == "mesh" and output_format == "xml":
> >>> -        mesh2xml(ifilename, ofilename)
> >>> +        mesh2xml(ifilename, ofilename, tfilename)
> >>>      elif input_format == "gmsh" and output_format == "xml":
> >>> -        gmsh2xml(ifilename, ofilename)
> >>> +        gmsh2xml(ifilename, ofilename, tfilename)
> >>>      elif input_format == "xml-old" and output_format == "xml":
> >>>          xml_old2xml(ifilename, ofilename)
> >>>      else:
> >>> @@ -113,7 +116,7 @@
> >>>          print "*** %s" % line
> >>>      sys.exit(2)
> >>>  
> >>> -def mesh2xml(ifilename, ofilename):
> >>> +def mesh2xml(ifilename, ofilename, tfilename):
> >>>      """Convert between .mesh and .xml, parser implemented as a
> >>>      state machine:
> >>>      
> >>> @@ -133,7 +136,7 @@
> >>>  
> >>>      # Open files
> >>>      ifile = open(ifilename, "r")
> >>> -    ofile = open(ofilename, "w")
> >>> +    ofile = open(tfilename, "w")
> >>>  
> >>>      # Write header
> >>>      write_header(ofile)
> >>> @@ -233,7 +236,10 @@
> >>>      ifile.close()
> >>>      ofile.close()
> >>>  
> >>> -def gmsh2xml(ifilename, ofilename):
> >>> +    # Complete the conversion
> >>> +    complete_conversion(tfilename, ofilename)
> >>> +
> >>> +def gmsh2xml(ifilename, ofilename, tfilename):
> >>>      """Convert between .gmsh v2.0 format (http://www.geuz.org/gmsh/) and .xml, 
> >>>      parser implemented as a state machine:
> >>>      
> >>> @@ -255,7 +261,7 @@
> >>>  
> >>>      # Open files
> >>>      ifile = open(ifilename, "r")
> >>> -    ofile = open(ofilename, "w")
> >>> +    ofile = open(tfilename, "w")
> >>>  
> >>>      # Write header
> >>>      write_header(ofile)
> >>> @@ -349,6 +355,9 @@
> >>>                  n2 = nodelist[nn[2]]
> >>>                  n3 = nodelist[nn[3]]
> >>>                  write_cell_tetrahedron(ofile, num_cells_read, n0, n1, n2, n3)
> >>> +            else:
> >>> +                error("Unknown element type. Did you define a physical surface/volume? \n\
> >>> +If you didn't, gmsh exports edges as cells. \nSee http://www.geuz.org/gmsh/doc/texinfo/gmsh.html";)
> >>>  
> >>>              num_cells_read +=1 
> >>>              if num_cells == num_cells_read:
> >>> @@ -371,6 +380,9 @@
> >>>      ifile.close()
> >>>      ofile.close()
> >>>  
> >>> +    # Complete the conversion
> >>> +    complete_conversion(tfilename, ofilename)
> >>> +
> >>>  def xml_old2xml(ifilename, ofilename):
> >>>      "Convert from old DOLFIN XML format to new."
> >>>  
> >>> @@ -428,6 +440,52 @@
> >>>      ifile.close();
> >>>      ofile.close();
> >>>  
> >>> +def complete_conversion(tfilename, ofilename):
> >>> +    " -- Replace <mesh> with <mesh celltype=CELLTYPE dim=DIM>  and remove the third coordinate in 2D"
> >>> +
> >>> +    # Get dimension
> >>> +    tris = len(getoutput("grep triangle " + tfilename))
> >>> +    tets = len(getoutput("grep tetrahedron " + tfilename))
> >>> +    if tris > 0 and tets == 0:
> >>> +        dim = 2
> >>> +        celltype = "triangle"
> >>> +    elif tris == 0 and tets > 0:
> >>> +        dim = 3
> >>> +        celltype = "tetrahedron"
> >>> +    else:
> >>> +        error("Inconsistent mesh file, not exactly one type of cells.")
> >>> +
> >>> +    # 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 "<mesh>" in line:
> >>> +            line = "  <mesh celltype=\"%s\" dim=\"%d\">\n" % (celltype, dim)
> >>> +        if dim == 2 and " z=\"0.0\"" in line:
> >>> +            line = line.replace(" z=\"0.0\"", "")
> >>> +        if dim == 2 and " z=\"0\"" in line:
> >>> +            line = line.replace(" z=\"0\"", "")
> >>> +
> >>> +        # Write line
> >>> +        ofile.write(line)
> >>> +
> >>> +    ofile.write("\n")
> >>> +
> >>> +    # Close files
> >>> +    ifile.close();
> >>> +    ofile.close();
> >>> +
> >>> +    # Remove temporary file
> >>> +    os.remove(tfilename)
> >>> +
> >>>  # Write mesh header
> >>>  def write_header(ofile):
> >>>      ofile.write("""\
> >>> @@ -456,7 +514,7 @@
> >>>  
> >>>  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_header_cells(ofile, num_cells):
> >>> @@ -471,12 +529,12 @@
> >>>  
> >>>  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__":
> >>>
> >>>
> >>> ------------------------------------------------------------------------
> >>>
> >>> _
> 
> _______________________________________________
> DOLFIN-dev mailing list
> DOLFIN-dev@xxxxxxxxxx
> http://www.fenics.org/mailman/listinfo/dolfin-dev


Follow ups

References