Friday, November 18, 2011

Transfer your mesh from GMSH to Matlab

I believe examples are on of the best ways to learn, so I start from an example.

1- build your geometry in GMSH (you can easily do that by studying the first tutorial of the software)


2- Mesh the geometry by going to the Mesh tab and clicking on the 2D. This will build a triangular non structured mesh



3- Extract the mesh by going to File-Save Mesh, or you can use the hot key (Shift+Ctrl+S). This will give you a file with the same name as the project name and with the extension "MSH"
4- Open the file with Wordpad, Notepad or etc.

$MeshFormat
2.2 0 8
$EndMeshFormat
$Nodes
37
1 0 0 0
2 4 0 0
3 4 4 0
4 0 4 0
5 2 2 0
6 2.5 2 0
7 1.5 2 0
8 1.333333333330429 0 0
9 2.666666666663595 0 0
10 4 1.333333333330429 0
11 4 2.666666666663595 0
12 2.666666666669443 4 0
13 1.33333333333796 4 0
14 0 2.666666666669443 0
15 0 1.33333333333796 0
16 2.43301270189563 2.249999999994092 0
17 2.250000000005625 2.433012701888972 0
18 1.999999999999019 2.5 0
19 1.749999999993242 2.433012701888317 0
20 1.566987298104043 2.249999999993525 0
21 1.566987298103185 1.75000000000796 0
22 1.749999999991474 1.566987298112704 0
23 1.999999999998037 1.5 0
24 2.25000000000626 1.566987298111395 0
25 2.433012701896161 1.750000000006827 0
26 0.9761017850604321 2.346814881605136 0
27 3.02389821493962 1.65318511839525 0
28 2.334500907280026 3.134455102784824 0
29 1.665499092719324 0.8655448972157629 0
30 2.839249308207946 2.495807560436933 0
31 1.160750691791375 1.504192439564072 0
32 1.510100448723277 2.957936003268833 0
33 2.489899551276439 1.04206399673127 0
34 3.168083376431483 3.259385865977071 0
35 0.7639071134243337 3.194283510308682 0
36 3.236092886575931 0.8057164896913898 0
37 0.8319166235682255 0.7406141340235588 0
$EndNodes
$Elements
79
1 15 2 0 1 1
2 15 2 0 2 2
3 15 2 0 3 3
4 15 2 0 4 4
5 15 2 0 5 5
6 15 2 0 6 6
7 15 2 0 7 7
8 1 2 0 1 1 8
9 1 2 0 1 8 9
10 1 2 0 1 9 2
11 1 2 0 2 2 10
12 1 2 0 2 10 11
13 1 2 0 2 11 3
14 1 2 0 3 3 12
15 1 2 0 3 12 13
16 1 2 0 3 13 4
17 1 2 0 4 4 14
18 1 2 0 4 14 15
19 1 2 0 4 15 1
20 1 2 0 5 6 16
21 1 2 0 5 16 17
22 1 2 0 5 17 18
23 1 2 0 5 18 19
24 1 2 0 5 19 20
25 1 2 0 5 20 7
26 1 2 0 6 7 21
27 1 2 0 6 21 22
28 1 2 0 6 22 23
29 1 2 0 6 23 24
30 1 2 0 6 24 25
31 1 2 0 6 25 6
32 2 2 0 9 11 3 34
33 2 2 0 9 34 3 12
34 2 2 0 9 13 4 35
35 2 2 0 9 35 4 14
36 2 2 0 9 9 2 36
37 2 2 0 9 36 2 10
38 2 2 0 9 37 1 8
39 2 2 0 9 15 1 37
40 2 2 0 9 32 13 35
41 2 2 0 9 33 9 36
42 2 2 0 9 34 12 28
43 2 2 0 9 37 8 29
44 2 2 0 9 31 21 7
45 2 2 0 9 6 25 27
46 2 2 0 9 10 11 27
47 2 2 0 9 30 16 6
48 2 2 0 9 6 27 30
49 2 2 0 9 30 27 11
50 2 2 0 9 7 20 26
51 2 2 0 9 7 26 31
52 2 2 0 9 14 15 26
53 2 2 0 9 31 26 15
54 2 2 0 9 37 31 15
55 2 2 0 9 22 21 31
56 2 2 0 9 22 31 29
57 2 2 0 9 31 37 29
58 2 2 0 9 17 16 30
59 2 2 0 9 17 30 28
60 2 2 0 9 34 30 11
61 2 2 0 9 30 34 28
62 2 2 0 9 14 26 35
63 2 2 0 9 32 35 26
64 2 2 0 9 10 27 36
65 2 2 0 9 33 36 27
66 2 2 0 9 32 19 18
67 2 2 0 9 8 9 29
68 2 2 0 9 23 22 29
69 2 2 0 9 18 17 28
70 2 2 0 9 18 28 32
71 2 2 0 9 12 13 28
72 2 2 0 9 32 28 13
73 2 2 0 9 33 29 9
74 2 2 0 9 33 24 23
75 2 2 0 9 23 29 33
76 2 2 0 9 20 19 32
77 2 2 0 9 20 32 26
78 2 2 0 9 25 24 33
79 2 2 0 9 25 33 27
$EndElements

5- You can see the nodes from the line $Nodes to $EndNodes. The first number below $Nodes is the number of the nodes, The first number at the left column specifies the node number and the rest are the x,y and z position of the nodes (The nodes are highlighted in yellow). Copy the nodes into a separate "txt" file and name is as Nodes.txt.
6- The elements start from $Elements to $EndElements. In this example we are only interested in 2D elements, these elements start from the line in which the second column's value is 2. 32 2 2 0 9 11 3 34. Copy the elements into a separate "txt" file and name it as Elements.txt. The corresponding nodes of the element are the last three numbers, e.g. 32 2 2 0 9 11 3 3 4
7- Copy these two files into the working directory of your Matlab code.
8- Use the following code to transfer the data into Matlab:

----------------------------------------------------------
%----------- Description ---------
% Transfer Mesh from GMSH to Matlab
%---------------------------------
% Written by 
% Shah, 18 November 2011

clc
clear all
close all

load Elements.txt
load Nodes.txt

nrelm=size(Elements,1);
nnod=size(Nodes,1);

Edof=zeros(nrelm,4);
Ex=zeros(nrelm,3); Ey=Ex; Ez=Ex;
Edof(:,1)=sort(1:nrelm);
Elm(:,1)=sort(1:nrelm);

for i=1:nrelm
    Edof(i,2:7)=[2*Elements(i,6)-1 Elements(i,6)*2 2*Elements(i,7)-1 ...
        Elements(i,7)*2 2*Elements(i,8)-1 2*Elements(i,8)];  
    Elm(i,2:4)=Elements(i,6:8);  
    Ex(i,:)=[Nodes(Elm(i,2),2) Nodes(Elm(i,3),2) Nodes(Elm(i,4),2)];
    Ey(i,:)=[Nodes(Elm(i,2),3) Nodes(Elm(i,3),3) Nodes(Elm(i,4),3)];
    Ez(i,:)=[Nodes(Elm(i,2),4) Nodes(Elm(i,3),4) Nodes(Elm(i,4),4)];    
end

nrdof=max(max(Edof));


figure(1);
hold on;
for i=1:length(Edof(:,1));
    plot(Ex(i,[1:end 1]),Ey(i,[1:end 1]));
end
axis equal

------------------------------------------------------------------------------------------------

The output in Matlab looks like this:
This does not look a like good approximation of the geometry, a finer mesh would look like:






13 comments:

  1. This is very useful. Thanks!

    ReplyDelete
  2. What are u doing with the Edof matrix?

    ReplyDelete
  3. I use Edof as a connectivity matrix, the first column represents the element number and the rest of the columns represent degrees of freedom associated with the nodes of that element in an anti-clockwise fashion.

    ReplyDelete
    Replies
    1. hello.... i tried to do the above process and it works great. but in matlab, all the nodes are not visible. please could you tell me how can i make all the modes visible in matlab along with the node numbers. thank you

      Delete
    2. Hello, I also tried the above process, but for all my elements, the second column starts with a 1. I get an error message that says

      "Index exceeds matrix dimensions.

      Error in gmshtomatlab (line 24)
      Elements(i,7)*2 2*Elements(i,8)-1 2*Elements(i,8)];"

      What could I be doing wrong?

      Delete
    3. This comment has been removed by the author.

      Delete
  4. no need to do so, used an input file from Openbem (readgeom_GMSH) and does ,what you specified with a manual approach, automatically.
    Credits go to the programmers of Open_bem

    ReplyDelete
    Replies
    1. Hello, I also tried the above process, but for all my elements, the second column starts with a 1. I get an error message that says

      "Index exceeds matrix dimensions.

      Error in gmshtomatlab (line 24)
      Elements(i,7)*2 2*Elements(i,8)-1 2*Elements(i,8)];"

      Can I please ask for that input file from Openbem (readgeom_GMSH).

      Delete
  5. sorry guys good afternoon i write this post because i don't understand the number's meaning in output element's file, that is the difference between these two lines :66 2 2 0 9 32 19 18 and 6 15 2 0 6 6?thanks for the help!

    ReplyDelete
  6. hello.... i tried to do the above process and it works great. but in matlab, all the nodes are not visible. please could you tell me how can i make all the modes visible in matlab along with the node numbers. thank you

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. Hello,
    given that your code was very helpful, I would like to know how to be able to select boundary nodes after importing the mesh.
    Thanks in advance.

    ReplyDelete