Conversion of .mphtxt that comsol exports to mesh usable for fenics legacy

Dear All,

I am trying to use a COMSOL mesh exported in .mphtxt format with legacy FEniCS. However, I am not sure how to convert this format into one that can be used in my FEniCS code.

Currently, my workflow uses the XDMF format because I need MPI support for parallel computations.

Could anyone suggest the correct workflow to convert a COMSOL .mphtxt mesh into a format compatible with legacy FEniCS? Alternatively, is there a better mesh export format from COMSOL that works reliably with FEniCS and XDMF?

Thank you.

from dolfin import *
import os

## convert the mesh from .msh to .xml
os.system('dolfin-convert geo/BCS.msh geo/BCS.xml')



mesh = Mesh("geo/BCS.xml")
facet_f = MeshFunction("size_t", mesh, "geo/BCS_facet_region.xml")
region_f = MeshFunction("size_t", mesh, "geo/BCS_physical_region.xml")

##convert the xml mesh to xdmf
with XDMFFile("geo/mesh_BCS.xdmf") as f:
    f.write(mesh)

with XDMFFile("geo/facet_BCS.xdmf") as f:
    f.write(facet_f)

with XDMFFile("geo/region_BCS.xdmf") as f:
    f.write(region_f)

print("Converted successfully!")

Maybe you could try asking the developers at GitHub - nschloe/meshio: 🕸 input/output for many mesh formats · GitHub. I haven’t come across anyone in the FEniCS community using the .mphtxt format, so I think it may be unlikely that someone here’s already written a converter.

Thankyou @nate. I tried the .stl file from Comsol, used Gmsh to create a surface with tag 1 (include all in one) and a volume with tag 1, but there is a problem when I import the mesh in Fenics using the above code. The STL file is this. What does it look like and

,

I am getting the error with this one.

****************************************************************************
Expecting 39087 vertices
Found all vertices
Expecting 231505 cells
Found all cells
Traceback (most recent call last):
  File "/home/shah/miniconda3/envs/fenics-env/bin/dolfin-convert", line 132, in <module>
    main(sys.argv[1:])
  File "/home/shah/miniconda3/envs/fenics-env/bin/dolfin-convert", line 79, in main
    meshconvert.convert2xml(ifilename, ofilename, iformat=iformat)
  File "/home/shah/miniconda3/envs/fenics-env/lib/python3.10/site-packages/dolfin_utils/meshconvert/meshconvert.py", line 1301, in convert2xml
    convert(ifilename, XmlHandler(ofilename), iformat=iformat)
  File "/home/shah/miniconda3/envs/fenics-env/lib/python3.10/site-packages/dolfin_utils/meshconvert/meshconvert.py", line 1322, in convert
    gmsh2xml(ifilename, handler)
  File "/home/shah/miniconda3/envs/fenics-env/lib/python3.10/site-packages/dolfin_utils/meshconvert/meshconvert.py", line 494, in gmsh2xml
    index = nodes_as_facets[tuple(nodes)]
KeyError: (288, 289, 15868)
Traceback (most recent call last):
  File "/home/shah/phd/fenics/convert_xdmf.py", line 9, in <module>
    mesh = Mesh("geo2/BCS.xml")
RuntimeError: 

*** -------------------------------------------------------------------------
*** DOLFIN encountered an error. If you are not able to resolve this issue
*** using the information listed below, you can ask for help at
***
***     fenics-support@googlegroups.com
***
*** Remember to include the error message listed below and, if possible,
*** include a *minimal* running example to reproduce the error.
***
*** -------------------------------------------------------------------------
*** Error:   Unable to read data from XML file.
*** Reason:  Error while parsing XML with status "Start-end tags mismatch".
*** Where:   This error was encountered inside XMLFile.cpp.
*** Process: 0
*** 
*** DOLFIN version: 2019.1.0
*** Git changeset:  828b3965c3ebcd128da494c596c78b17c0a303da
*** --------------------------------------------------------------