Reading *.msh files (3D) in Matlab.

I have created this 3D geometry and the mesh in Gmsh, now I need to get the number of nodes, elements, faces from the mesh in Matlab. How can I do that?
I can send you the *.geo and the *.msh files.
Thanks in advance.
Sanwar

 Accepted Answer

If you're using gmsh, you can use its inbuilt exporter to export a .m file to read into MATLAB.
This loads in a structure array called msh.
Assuming a 3D linear tetrahedral mesh:
%Uses pre-created mesh from gmsh
%Run mesh file name without .m:
Single_coil
nodes = msh.POS';
elements = msh.TETS;
groupsID = elements(:,5);
%If you have multiple domains, this will contain domain IDs
% (assuming you haven't checked 'export all elements', else it will be a column of zeros.
elements = elements(:,1:4);
elements =elements';
geom2 = geometryFromMesh(model,nodes,elements,groupsID); %Import to PDE Toolbox
%If you have subdomains, this will identify their geometries even for a coherent mesh:
%In my example, my actual mesh is just a solid 3D box, however by giving the group IDs
% it is able to identify internal cells, faces etc
%% Plotting:
figure(1); pdeplot3D(model,'FaceAlpha',0.5) %Mesh Plot
figure(2); pdegplot(model,'CellLabels','on','FaceAlpha',0.5) %Geometry
figure(3); pdegplot(model,'FaceLabels','on','FaceAlpha',0.5) %Geometry
%% Finding specific elements/nodes:
%Given this setup, the labels in figures 2 and 3 will allow you to identify faces,
%cells or areas using the usual functions mentioned above.
C_e = findElements(model.Mesh,'region','cell',1); %Coil
C_n = findNodes(model.Mesh,'region','cell',1);

13 Comments

If you use quadratic or higher order meshes, the number of columns in msh.TETs will change, for a quadratic mesh:
elements = msh.TETS10; elements = elements(:,1:10); elements =elements';
Hi,
I want to simulate current flowing througn an object that has two cells (these two cells have different resistivity).
I have create the mesh from an STEP file in GMSH with two physical volumes, and I have sucessfully imported it into matlab as you explained.
BUT there is no current flowing between these two cells, do you know how I can fix it?
Thanks a a lot!
Fanf
I'd need some more details to be sure, but my initial guess would be to think carefully about the boundary conditions of the problem.
Hi
Thank you for replying in such a short time!!!
1 So first I have imported the mesh (a cylinder underneath a square) into matlab and use 'geometryfrommesh' to create model.
2 So I have 2 cells and 9 faces in total.
3 My BCs are like these: current in at Face 4 and current out at face 8. I also set the voltage to be 0 and face 8. And now I set the conductivity of two cells to be the same.
Q=-10; %current density =1A/500/100
applyBoundaryCondition(model,'mixed', ...
'Face',8, ...
'h',1,'r',0,'q',0,'g',Q);
applyBoundaryCondition(model,'neumann','Face',4,'q',0,'g',-Q);
specifyCoefficients(model,'m',0,...
'd',0,...
'c',10,...
'a',0,...
'f',0,'Cell',1);
specifyCoefficients(model,'m',0,...
'd',0,...
'c',10,...
'a',0,...
'f',0,'Cell',2);
4 The voltage result I got is shown below. So it seems like there is no current flowing through Cell 2.
5 Then I swap the BCs for face 4 and face 8, i got results like this. So basically Cell2 is not conducting at all.
Do you know what might go wrong?
Kazuki Ozawa
Kazuki Ozawa on 23 Dec 2021
Edited: Kazuki Ozawa on 23 Dec 2021
Hello! I faced same problem that several cells don't be conducted each other when I use a geometry from GMSH.
If you already solved it, please share me how to do it.
~Details~
In my case, after I make a geometry in GMSH, I do "Set order1","Set order2","Set order3" to recognize independent volumes. And then, *.m file is exported.
Next, after I run the *.m file from GMSH, I runned the same code following
nodes = msh.POS';
element = msh.TETS20;
grroupID = elements(:,5);
elements = elements(:,4);
elements = elements';
geom2 = geometryFromMesh(model,nodes,elements,groupID);
The code recognizes the several domains. However, they don't be conducted each other in the calculation.
I attached sample results about heat transfer in 2 cylinders next to each other.
I appreciate your answer.
Try adding the Coherence; command to your .geo file directly after importing/constructing your geometry in gosh that worked for me
Kazuki Ozawa
Kazuki Ozawa on 23 Dec 2021
Edited: Kazuki Ozawa on 23 Dec 2021
Thank you for your quick answer!
When I did coherence in gmsh, my geometry merged cell1 and cell2. Thus, I couldn't get 2 cells respectively from " *.m" file.
In your case, did your matlab recognize 2 cells respectively, after you did "Coherence" command in gmsh and export to *.m file.
I would like to make cell1 and cell2 seraparetely in order to define different material properties in matlab.
If your matlab could recognize sepate cells, what was your procedure in gmsh to export?
Many thanks
Matlab does indeed recognise separate cells.
For a GMSH output to work properly, I found you need to use coherence and make sure each cell is labelled. Export and save as a .m file making sure you do not check the export all cells button - this forces all cells to be exported in a merged fashion. Leaving this unchecked exports only cells which have been labelled in your .geo file.
Kazuki Ozawa
Kazuki Ozawa on 23 Dec 2021
Edited: Kazuki Ozawa on 23 Dec 2021
I appreciate you. And I am glad to hear that Matlab recognise separated cells.
I understand that the procedure of 1. Labelling the cells, 2. Do coherence 3. Meshing and 4. Export without cheking "save all elements" in GMSH.
Lastly, I appreciate you that if you could please provide information of the labelling (like some URL) since official manual doesn't mention how to label the volume very clearly?
Before I asked you, I tried to find the labelling because it looks quite important.
My geometry in GMSH is imported by 3D STEP file from solidworks.
I hope that the labelling function will work the geometry from STEP file as well.
Sincerely
Sibel Akyuz
Sibel Akyuz on 11 Oct 2022
Edited: Sibel Akyuz on 11 Oct 2022
lifesaver, thanks! I got this error "Subdomain ID must contain integers in the range from 1 to the number of subdomains in the mesh." and I fixed it by changing the volume numbers from 1 to 5 (5 is the number of volumes I had on the mesh).
Hi, my msh array has only POS element , no TETS elements. But there is an extra msh.TRIANGLES. Am I using gmsh by mistake?
Try ticking this option here. It will appear after you choose to save.
Hi, I run the code and it's error.
How should I do?
Thank you

Sign in to comment.

More Answers (2)

findElements and findNodes can be used to calculate the number of elements and nodes in a mesh respectievely.
You can find the documentation for both the functions in the following links

3 Comments

Thanks Sahithi. I tried these ways, but these are written for specific geometries (I think), and I couldn't apply them for the model I am working.
Moreover, I have written my own codes that can read the *.msh files.
@Sanwar Ahmad
Hi, I am stuck on the same problem as in your original question.
Can you share the codes to read .msh files?
Thanks.
@Harshit Ks, follow the link and download gmsh_size_read.m and any related .m files. You may have to change it based on the Gmsh version you are using, that's what I did.
Have fun.

Sign in to comment.

ALI
ALI on 1 May 2024
Edited: ALI on 1 May 2024
hi can i solve overset meshes in matlab please?
if you have any code please share

Tags

Asked:

on 1 Oct 2019

Edited:

ALI
on 1 May 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!