-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadmsh.m
181 lines (130 loc) · 4.38 KB
/
readmsh.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
%read-in gmesh .msh mesh format. mesh should already be triangulated
%using gmesh.
%spits out mesh data and BC data for use in a NUDG code.
%note: you may need to tweak which BC flag. i.e. wall, neumann, dirichlet etc.
%filename = 'opeongonw.msh';
function [Nv,VX, VY, K, EToV,BCType,node,edge] = readmsh(filename)
fid = fopen(filename,'r');
%skip over meshformat header and nodes header
fgetl(fid);fgetl(fid);fgetl(fid);fgetl(fid);
numNodes = str2num(fgetl(fid));
Nv= numNodes;
for jj=1:numNodes
dump = str2num(fgetl(fid));
VX(jj) = dump(2);
VY(jj) = dump(3);
end
fgetl(fid); % '$EndNodes
fgetl(fid); % '$Elements''
numElements = str2num(fgetl(fid));
K = numElements;
node = [];
%read list of boundary nodes
readingnodes = true;
while readingnodes == true
dump = str2num(fgetl(fid));
if dump(2) ~= 15 %we hit an entry that's not a node.
readingnodes = false;
edge(1) = dump(end-1);
edge(2) = dump(end);
else %we're reading boundary nodes
mynode = [VX(dump(end)) VY(dump(end))];
node = [node; mynode];
end
end %done reading nodes
readingedges = true;
edges=0;
while readingedges == true
dump = str2num(fgetl(fid));
if dump(2) ~= 1 %we hit an entry that's not an edge
readingedges = false;
EToV(1) = dump(end-2);
EToV(2) = dump(end-1);
EToV(3) = dump(end);
else %we're reading edges
myedge(1) = dump(end-1);
myedge(2) = dump(end);
edge = [edge; myedge];
end
end
readingelements = true;
while readingelements == true
dumpstr = fgetl(fid);
if strcmp(dumpstr,'$EndElements') == 1 %end of element list
readingelements = false;
else %we're reading elements
dump = str2num(dumpstr);
myEToV(1) = dump(end-2);
myEToV(2) = dump(end-1);
myEToV(3) = dump(end);
EToV = [EToV; myEToV];
end
end
K = length(EToV);
%we've got all the info we need, so get out of dodge.
fclose(fid);
% figure(2);
% clf;
% hold on;
% for jj=1:length(edge)
% plot([VX(edge(jj,1)) VX(edge(jj,2))],[VY(edge(jj,1)) VY(edge(jj,2))]);
% end
% hold off;
% Reorder elements to ensure counter clockwise orientation
ax = VX(EToV(:,1)); ay = VY(EToV(:,1));
bx = VX(EToV(:,2)); by = VY(EToV(:,2));
cx = VX(EToV(:,3)); cy = VY(EToV(:,3));
D = (ax-cx).*(by-cy)-(bx-cx).*(ay-cy);
i = find(D<0);
EToV(i,:) = EToV(i,[1 3 2]);
%done reordering
% Build connectivity matrix
[EToE, EToF] = tiConnect2D(EToV);
Vert = [VX' VY'];
% [bdrynode,bdryedge] = bath2contour_derek(Opeongo);
%
% %find all boundary nodes (nodesOuter). Ain't nothin to it, but to do it.
% tol = 1e-6; %1m tolerance.
% %edgenum = findedge(node,Vert,edge,tol); %a mesh2d routine
% %edgenum = findedge(bdrynode,Vert,bdryedge,tol);
% edgenum = findedge(Vert,Vert,edge,tol);
% kk=1;
% for jj = 1:length(edgenum)
% %if edgenum of point jj is nonzero, then it lies on the boundary,
% %so put it in list of boundary pts.
% if edgenum(jj) ~= 0
% nodesOuter(kk) = jj;
% kk=kk+1;
% end
% end
%allocate BCType table.
BCType = 0*EToE;
%the 'node' data used by gmsh refers to its own spline data
%not the vertices of the finite element mesh.
%to get boundary vertices we need to do...
%figure(16);
%hold on;
%for j=1:length(edge)
% plot(VX(edge(j,:)),VY(edge(j,:)),'.-r');
%end
%drawnow;
%keyboard;
node=[VX' VY'];
%
%
% node = [VX(edge(:))' VY(edge(:))'];
% edge = [(1:length(node))' (2:length(node)+1)'];
% edge(end,2) = 1;
%i don't seem able to get the connectivity back this way
%bath = load('-ASCII', 'pinehurst_50m.dat');
%[node,edge] = bath2contour_spline(bath);
%keyboard;
%Insert the correct BC codes for boundaries
Wall=3;
%BCType = CorrectBCTable(EToV,BCType,nodesOuter,Wall,K);
BCType = CorrectBCTable_derek(EToV,VX,VY,node,edge,BCType,Wall);
%figure(1);
%hold on;
%plot(VX(nodesOuter),VY(nodesOuter),'.r');
%hold off;
end