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

**URL:** <https://fenicsproject.discourse.group/t/conversion-of-mphtxt-that-comsol-exports-to-mesh-usable-for-fenics-legacy/19705>\
**Category:** mesh\
**Tags:** mesh\
**Created:** [May 15, 2026, 5:50pm UTC](https://fenicsproject.discourse.group/t/conversion-of-mphtxt-that-comsol-exports-to-mesh-usable-for-fenics-legacy/19705 "2026-05-15T17:50:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zubair\_Shah](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/zubair_shah/32/4258_2.png) [@Zubair\_Shah](https://fenicsproject.discourse.group/u/Zubair_Shah)\
**Post date:** [May 15, 2026, 5:50pm UTC](https://fenicsproject.discourse.group/t/conversion-of-mphtxt-that-comsol-exports-to-mesh-usable-for-fenics-legacy/19705/1 "2026-05-15T17:50:51Z")

</div>

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.

```auto
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!")

```

---

<div class="post-metadata">

**Author:** ![nate](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/nate/32/17_2.png) [@nate](https://fenicsproject.discourse.group/u/nate)\
**Post date:** [May 15, 2026, 9:39pm UTC](https://fenicsproject.discourse.group/t/conversion-of-mphtxt-that-comsol-exports-to-mesh-usable-for-fenics-legacy/19705/2 "2026-05-15T21:39:56Z")

</div>

Maybe you could try asking the developers at [GitHub - nschloe/meshio: 🕸 input/output for many mesh formats · GitHub](https://github.com/nschloe/meshio). 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.

---

<div class="post-metadata">

**Author:** ![Zubair\_Shah](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/zubair_shah/32/4258_2.png) [@Zubair\_Shah](https://fenicsproject.discourse.group/u/Zubair_Shah)\
**Post date:** [May 16, 2026, 8:27pm UTC](https://fenicsproject.discourse.group/t/conversion-of-mphtxt-that-comsol-exports-to-mesh-usable-for-fenics-legacy/19705/3 "2026-05-16T20:27:07Z")

</div>

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

,

 ![b_cut](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/9/9c4f68ff69582e123268a717aea939ac734fb1a5.jpeg)

I am getting the error with this one.

> ```auto
> ****************************************************************************
> 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
> *** --------------------------------------------------------------
> 
> ```
