-
Notifications
You must be signed in to change notification settings - Fork 13
/
run_simple_example.m
296 lines (246 loc) · 7.55 KB
/
run_simple_example.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
function run_simple_example()
%RUN_SIMPLE_EXAMPLE Test the toolbox with a 2d and 3d electrostatic simulation.
%
% This example is meant to systemically test/demonstrates the different functions.
% The example 'run_bridge_example' is meant to show-off the toolbox with a complex model.
%
% Please note that the plotted/computed data don't have any phyical meaning.
%
% See also RUN_EXTRACT_COMSOL, RUN_BRIDGE_EXAMPLE, EXTRACT_GEOM, EXTRACT_DATA, PLOT_GEOM, PLOT_DATA, INTERP_DATA, INTEGRATE_DATA.
% Thomas Guillod.
% 2015-2020 - BSD License.
close('all')
addpath('fem_mesh_utils')
%% load
data_tmp = load('model_simple/simple.mat');
data_2d = data_tmp.data_2d;
data_3d = data_tmp.data_3d;
%% extract and plot the geometry
% extract 2d
data_2d.edge = parse_data_2d(data_2d.edge);
data_2d.surface = parse_data_2d(data_2d.surface);
% extract 3d
data_3d.edge = parse_data_3d(data_3d.edge);
data_3d.surface = parse_data_3d(data_3d.surface);
data_3d.volume = parse_data_3d(data_3d.volume);
%% interpolate the data
% interpolation 2d
x = linspace(-0.15, +0.15, 25);
y = linspace(-0.15, +0.15, 25);
interp_2d(data_2d.surface, x, y);
% interpolation 2d
x = linspace(-0.15, +0.15, 25);
y = linspace(-0.15, +0.15, 25);
z = linspace(-0.15, +0.15, 25);
interp_3d(data_3d.volume, x, y, z);
%% integrate the data
% integral 2d
integrate_2d(data_2d)
% integral 3d
integrate_3d(data_3d)
end
function data = parse_data_2d(data_fem)
%PARSE_DATA_2D Parse a 2d geometry, the associated data, and plot them.
% data = PARSE_DATA_2D(data_fem)
% data_fem - geometry and data from the FEM solver (struct)
% data - parsed geometry and data (struct)
% parse geometry
data.geom = extract_geom(data_fem.geom_fem, true);
% parse data
data.E_x = extract_data(data.geom, data_fem.E_x, @mean);
data.E_y = extract_data(data.geom, data_fem.E_y, @mean);
data.E = extract_data(data.geom, data_fem.E, @mean);
% plot the geometry
figure()
plot_param = struct(...
'plot_arrow', true,...
'arrow_scale', 1.0,...
'arrow_color', 'r',...
'arrow_width', 1.0,...
'vertice_marker', 'x',...
'face_color', [0.8 0.8 1.0],...
'edge_color', 'k',...
'face_alpha', 0.5,...
'edge_width', 1.0...
);
plot_geom(data.geom, plot_param);
axis('equal')
grid('on')
xlabel('x [m]')
ylabel('y [m]')
title('Geometry')
% plot the data (scalar and vector)
figure()
plt_scalar = struct(...
'type', 'scalar',...
'data', sum(abs(data.E), 2),...
'face_alpha', 0.5,...
'edge_width', 1.0...
);
plot_data(data.geom, plt_scalar);
plt_vector = struct(...
'type', 'vector',...
'data_x', sum(abs(data.E_x), 2),...
'data_y', sum(abs(data.E_y), 2),...
'arrow_scale', 1.0,...
'arrow_color', 'r',...
'arrow_width', 1.0...
);
plot_data(data.geom, plt_vector);
axis('equal')
grid('on')
colorbar();
xlabel('x [m]')
ylabel('y [m]')
title('Electric Field [V/m]')
end
function data = parse_data_3d(data_fem)
%PARSE_DATA_3D Parse a 3d geometry, the associated data, and plot them.
% data = PARSE_DATA_3D(data_fem)
% data_fem - geometry and data from the FEM solver (struct)
% data - parsed geometry and data (struct)
% parse geometry
data.geom = extract_geom(data_fem.geom_fem, true);
% parse data
data.E_x = extract_data(data.geom, data_fem.E_x, @mean);
data.E_y = extract_data(data.geom, data_fem.E_y, @mean);
data.E_z = extract_data(data.geom, data_fem.E_z, @mean);
data.E = extract_data(data.geom, data_fem.E, @mean);
% plot the geometry
figure()
plot_param = struct(...
'plot_arrow', true,...
'arrow_scale', 1.0,...
'arrow_color', 'r',...
'arrow_width', 1.0,...
'vertice_marker', 'x',...
'face_color', [0.8 0.8 1.0],...
'edge_color', 'k',...
'face_alpha', 0.5,...
'edge_width', 1.0...
);
plot_geom(data.geom, plot_param);
axis('equal')
grid('on')
view([45,45])
xlabel('x [m]')
ylabel('y [m]')
zlabel('z [m]')
title('Geometry')
% plot the data (scalar and vector)
figure()
plt_scalar = struct(...
'type', 'scalar',...
'data', sum(abs(data.E), 2),...
'face_alpha', 0.5,...
'edge_width', 1.0...
);
plot_data(data.geom, plt_scalar);
plt_vector = struct(...
'type', 'vector',...
'data_x', sum(abs(data.E_x), 2),...
'data_y', sum(abs(data.E_y), 2),...
'data_z', sum(abs(data.E_z), 2),...
'arrow_scale', 1.0,...
'arrow_color', 'r',...
'arrow_width', 1.0...
);
plot_data(data.geom, plt_vector);
axis('equal')
grid('on')
view([45,45])
colorbar();
xlabel('x [m]')
ylabel('y [m]')
zlabel('z [m]')
title('Electric Field [V/m]')
end
function interp_2d(data, x, y)
%INTERP_2D Interpolate data in 2d and plot the result.
% INTERP_2D(data, x, y)
% data - parsed geometry and data (struct)
% x - x coordinates of the points (vector of float)
% y - y coordinates of the points (vector of float)
% create a grid
[x, y] = ndgrid(x, y);
x = x(:).';
y = y(:).';
% interpolate the data
pts = struct('x', x, 'y', y);
v_int = interp_data(data.geom, sum(data.E, 2), pts);
% plot the data
figure()
scatter(x, y, 5, v_int)
axis('equal')
grid('on')
colorbar();
xlabel('x [m]')
ylabel('y [m]')
title('Electric Field [V/m]')
end
function interp_3d(data, x, y, z)
%INTERP_3D Interpolate data in 3d and plot the result.
% INTERP_3D(data, x, y, z)
% data - parsed geometry and data (struct)
% x - x coordinates of the points (vector of float)
% y - y coordinates of the points (vector of float)
% z - z coordinates of the points (vector of float)
% create a grid
[x, y, z] = ndgrid(x, y, z);
x = x(:).';
y = y(:).';
z = z(:).';
% interpolate the data
pts = struct('x', x, 'y', y, 'z', z);
v_int = interp_data(data.geom, sum(data.E, 2), pts);
% plot the data
figure()
scatter3(x, y, z, 5, v_int)
axis('equal')
grid('on')
view([45,45])
colorbar();
xlabel('x [m]')
ylabel('y [m]')
zlabel('z [m]')
title('Electric Field [V/m]')
end
function integrate_2d(data)
%INTEGRATE_2D Integrate data on a 2d geometry.
% INTEGRATE_2D(data)
% data - parsed geometry and data (struct)
fprintf('2d\n')
int = struct('type', 'scalar', 'data', data.edge.E);
v = integrate_data(data.edge.geom, int);
fprintf(' edge / scalar = %s\n', mat2str(v, 3))
int = struct('type', 'tangential', 'data_x', data.edge.E_x, 'data_y', data.edge.E_y);
v = integrate_data(data.edge.geom, int);
fprintf(' edge / tangential = %s\n', mat2str(v, 3))
int = struct('type', 'normal', 'data_x', data.edge.E_x, 'data_y', data.edge.E_y);
v = integrate_data(data.edge.geom, int);
fprintf(' edge / normal = %s\n', mat2str(v, 3))
int = struct('type', 'scalar', 'data', data.surface.E);
v = integrate_data(data.surface.geom, int);
fprintf(' surface / scalar = %s\n', mat2str(v, 3))
end
function integrate_3d(data)
%INTEGRATE_3D Integrate data on a 3d geometry.
% INTEGRATE_3D(data)
% data - parsed geometry and data (struct)
fprintf('3d\n')
int = struct('type', 'scalar', 'data', data.edge.E);
v = integrate_data(data.edge.geom, int);
fprintf(' edge / scalar = %s\n', mat2str(v, 3))
int = struct('type', 'tangential', 'data_x', data.edge.E_x, 'data_y', data.edge.E_y, 'data_z', data.edge.E_z);
v = integrate_data(data.edge.geom, int);
fprintf(' edge / tangential = %s\n', mat2str(v, 3))
int = struct('type', 'scalar', 'data', data.surface.E);
v = integrate_data(data.surface.geom, int);
fprintf(' surface / scalar = %s\n', mat2str(v, 3))
int = struct('type', 'normal', 'data_x', data.surface.E_x, 'data_y', data.surface.E_y, 'data_z', data.surface.E_z);
v = integrate_data(data.surface.geom, int);
fprintf(' surface / normal = %s\n', mat2str(v, 3))
int = struct('type', 'scalar', 'data', data.volume.E);
v = integrate_data(data.volume.geom, int);
fprintf(' volume / scalar = %s\n', mat2str(v, 3))
end