# Importing Mesh from COMSOL

**URL:** <https://community.freefem.org/t/importing-mesh-from-comsol/2968>\
**Category:** General Discussion\
**Created:** [February 15, 2024, 7:42am UTC](https://community.freefem.org/t/importing-mesh-from-comsol/2968 "2024-02-15T07:42:22Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![MehranK1](https://avatars.discourse-cdn.com/v4/letter/m/e36b37/32.png) [@MehranK1](https://community.freefem.org/u/MehranK1)\
**Post date:** [February 15, 2024, 7:42am UTC](https://community.freefem.org/t/importing-mesh-from-comsol/2968/1 "2024-02-15T07:42:22Z")

</div>

Has anyone extracted a mesh from COMSOL and imported it into FreeFem?

I am exporting a 2d mesh from COMSOL. There are different formats that I can extract. I extract and tried all of them, but I cannot import it into FreeFem.

Does anyone know what format I should do and what procedure I should follow?

I appreciate your help.  
Best

---

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [February 15, 2024, 7:48am UTC](https://community.freefem.org/t/importing-mesh-from-comsol/2968/2 "2024-02-15T07:48:04Z")

</div>

What format did you try?

---

<div class="post-metadata">

**Author:** ![MehranK1](https://avatars.discourse-cdn.com/v4/letter/m/e36b37/32.png) [@MehranK1](https://community.freefem.org/u/MehranK1)\
**Post date:** [February 15, 2024, 7:51am UTC](https://community.freefem.org/t/importing-mesh-from-comsol/2968/3 "2024-02-15T07:51:52Z")

</div>

Thank you for your reply. I tried all the formats.  
mphbin, mphtxt: these two weren’t useful because freefem and also Gmsh cannot open them.

.nas: I could open this with Gmsh and save it as msh file. But I wasn’t able to open it with code.

.vtu: THis one was also not readable in freefem.

Could you please tell me what to do? Thank you.

---

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [February 15, 2024, 7:56am UTC](https://community.freefem.org/t/importing-mesh-from-comsol/2968/4 "2024-02-15T07:56:07Z")

</div>

If you can open with Gmsh and then export to `.mesh` or `.msh`, then that should be alright.

---

<div class="post-metadata">

**Author:** ![MehranK1](https://avatars.discourse-cdn.com/v4/letter/m/e36b37/32.png) [@MehranK1](https://community.freefem.org/u/MehranK1)\
**Post date:** [February 15, 2024, 8:00am UTC](https://community.freefem.org/t/importing-mesh-from-comsol/2968/5 "2024-02-15T08:00:11Z")

</div>

Thank you again.

I saved it .msh file, but it is not opening if freefem.

Here:  
load “gmsh”  
mesh Th=gmshload(“Mesh4.msh”);

this is the error:  
mshptg bug in number of points 1 \> 1 == max nb points  
Sorry Error build delaunay triangle error = 1  
current line = 3  
Exec error : Error mshptg8\_  
– number :1  
Exec error : Error mshptg8\_  
– number :1  
err code 8 , mpirank 0  
try getConsole C:\Users\ava\Desktop\My FreeFem\reading.edp  
save log in : ‘C:\Users\ava\Desktop\My FreeFem\reading.log’  
wait enter ?

Please let me know what I should do.

---

<div class="post-metadata">

**Author:** ![MehranK1](https://avatars.discourse-cdn.com/v4/letter/m/e36b37/32.png) [@MehranK1](https://community.freefem.org/u/MehranK1)\
**Post date:** [February 15, 2024, 8:11am UTC](https://community.freefem.org/t/importing-mesh-from-comsol/2968/6 "2024-02-15T08:11:23Z")

</div>

Can I give you my .nas or .msh file and you check it, please?

---

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [February 15, 2024, 11:28am UTC](https://community.freefem.org/t/importing-mesh-from-comsol/2968/7 "2024-02-15T11:28:41Z")

</div>

Sure, upload the `.msh`.

---

<div class="post-metadata">

**Author:** ![MehranK1](https://avatars.discourse-cdn.com/v4/letter/m/e36b37/32.png) [@MehranK1](https://community.freefem.org/u/MehranK1)\
**Post date:** [February 15, 2024, 1:33pm UTC](https://community.freefem.org/t/importing-mesh-from-comsol/2968/8 "2024-02-15T13:33:14Z")

</div>

Thank you.

I appreciate your help.  
[Mesh1.mesh](https://community.freefem.org/uploads/short-url/vZpWXW7ojF84QbyOCN7fZ0cGycr.mesh) (836.8 KB)

---

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [February 15, 2024, 2:49pm UTC](https://community.freefem.org/t/importing-mesh-from-comsol/2968/9 "2024-02-15T14:49:05Z")

</div>

Your mesh has no boundary elements, so it cannot be read by FreeFEM. You need to define the surface of the circle.

---

<div class="post-metadata">

**Author:** ![MehranK1](https://avatars.discourse-cdn.com/v4/letter/m/e36b37/32.png) [@MehranK1](https://community.freefem.org/u/MehranK1)\
**Post date:** [February 15, 2024, 2:51pm UTC](https://community.freefem.org/t/importing-mesh-from-comsol/2968/10 "2024-02-15T14:51:02Z")

</div>

Could you please help me how I can do that?

The file that I sent you is the result of gmsh from .nas of the COMSOL.

---

<div class="post-metadata">

**Author:** ![henkel](https://avatars.discourse-cdn.com/v4/letter/h/b77776/32.png) [@henkel](https://community.freefem.org/u/henkel)\
**Post date:** [February 15, 2024, 3:21pm UTC](https://community.freefem.org/t/importing-mesh-from-comsol/2968/11 "2024-02-15T15:21:53Z")

</div>

Some time ago I used [feconv](https://victorsndvg.github.io/FEconv/index.html) to convert a 3D mesh from mphtxt format to gmsh format. Check out the software. Maybe it also works for 2D meshes.

---

<div class="post-metadata">

**Author:** ![asdddas02](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/asdddas02/32/2930_2.png) [@asdddas02](https://community.freefem.org/u/asdddas02)\
**Post date:** [March 5, 2025, 4:12pm UTC](https://community.freefem.org/t/importing-mesh-from-comsol/2968/12 "2025-03-05T16:12:55Z")

</div>

Hello,  
I recently have tried feconv that @henkel suggested and it worked! Thanks to him!  
I wanted to import in freefem a mesh of a 2d square with a slit from COMSOL.

Firstly, you need to choose .mphtxt and check all the 4 first v’s on the export menu, that is:

- domain elements
- boundary elements
- vertex elements
- geometric entity information

But then all I had to do was to write in cmd (though installing feconv is a bit hard):

1. `cd <go to a place where feconv is>`
2. `feconv -ff <yourfile.mphtxt> <newfile.msh>`  
-ff is for when .msh file appears, you choose for freefem. You could also write -gm for gmsh.
3. And then you just read it in freefem:  
`mesh Th = readmesh("newfile.msh");`  
`plot(Th, wait=1);`

P.S. At least that’s what I had come to, through trying and failing.

P.S.S. If you need to deal with labels, I just printed them out with `cout << labels(Th)<< endl;` and it’s just numbers. I deformed the square by the numbers one by one until I finally discovered every sides’ labels.

P.S.S.S. If you read somewhere that for importing 2d-mesh you need to remove z-axis from your file because freefem can’t read because of it, don’t trust them. Freefem reads 2d meshes just fine with readmesh().
