MATLAB Answers

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

110 views (last 30 days)
Sanwar Ahmad
Sanwar Ahmad on 1 Oct 2019
Commented: ADSW121365 on 6 Mar 2020
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

  0 Comments

Sign in to comment.

Answers (2)

Sahithi Kanumarlapudi
Sahithi Kanumarlapudi on 5 Nov 2019
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

  1 Comment

Sanwar Ahmad
Sanwar Ahmad on 6 Nov 2019
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.

Sign in to comment.


ADSW121365
ADSW121365 on 6 Mar 2020
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);

  1 Comment

ADSW121365
ADSW121365 on 6 Mar 2020
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';

Sign in to comment.

Tags


Translated by