-
Notifications
You must be signed in to change notification settings - Fork 7
/
fem_load_mesh.m
104 lines (87 loc) · 2.04 KB
/
fem_load_mesh.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
% Usage: [Coord, Elem, Sig] = load_mesh (FILENAME)
% Zeynep Akalin Acar, 2015
%
% Load a FEM mesh file from FILENAME.
% supports old-style and parallel mesh files
% with linear (8 node) and quadratic (20 node) elements.
function [Coord, Elem, Sig] = fem_load_mesh (fname)
fid = fopen(fname, 'r');
if fid == -1
error ('Failed to open %s\n', fname);
end
line = fgets(fid);
[nn, cnt] = sscanf(line, '%d');
if (cnt == 0 || nn < 0)
error('Invalid mesh file\n');
end
if (nn == 0)
line = fgets(fid);
[nn, cnt] = sscanf(line, '%d', 1);
if cnt == 0 || nn ~= 0
error ('Invalid mesh file\n');
end
line = fgets(fid);
[nn, cnt] = sscanf(line, '%d', 1);
if cnt == 0 || nn <= 0
error ('Invalid mesh file\n');
end
end
[C, cnt] = fscanf(fid, '%d %g %g %g', [4, nn]);
if cnt ~= nn*4
error ('Error reading nodes\n');
end
idx = 1:nn;
if idx ~= C(1,:)
error ('Error in node indices\n');
end
line = fgets(fid); % skip end of line
line = fgets(fid);
[tmp, cnt] = sscanf(line, '%d');
if cnt <= 0
error('Invalid mesh file\n');
end
ns = 20;
ne = tmp(1);
if (cnt == 2)
ns = tmp(2);
end
if (ns ~= 20 && ns ~= 8 && ns~= 4 && ns ~= 10)
error('Invalid nodes/element');
else
[E, cnt] = fscanf(fid, '%d', [ns + 1, ne]);
end
if cnt ~= ne * (ns+1)
error('Error reading elements\n');
end
idx = 1:ne;
if idx ~= E(1,:)
error('Error in element indices\n');
end
line = fgets(fid); % skip end of line
line = fgets(fid);
[tmp, cnt] = sscanf(line, '%d');
S = zeros(ne, 2);
if (cnt == 0)
warning('No conductivity information\n');
elseif (tmp == nn)
warning('Node conductivity not supported\n');
elseif (tmp == ne)
pos = ftell(fid);
[S, cnt] = fscanf(fid, '%d %g', [2, ne]);
if cnt ~= ne*2
fseek(fid, pos, 'bof');
[S, cnt] = fscanf(fid, '%d C%d', [2, ne]);
if cnt ~= ne*2
error('Failed to read sigma\n');
end
end
if idx ~= S(1,:)
error('Error in conductivity indices\n');
end
line = fgets(fid); % skip end of line
end
Coord = C(2:4,:)';
Elem = E(2:ns+1,:)';
Sig = S(2,:)';
fclose(fid);
end