-
Notifications
You must be signed in to change notification settings - Fork 0
/
CFDVerification.m
360 lines (334 loc) · 12.7 KB
/
CFDVerification.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
%*************************************************************************%
% %
% (Grid) Convergence CFD Verification %
% %
%*************************************************************************%
%*************************************************************************%
% %
% Author: %
% Llorente-Lázaro, Víctor Javier :: ETSIAE - UPM %
% %
% Date: %
% February 18, 2022 %
% %
% Version 1.0: %
% - Constant refinement ratio %
% %
% References: %
% [1] Roache, P.J., Verification and Validation in Computational Science %
% and Engineering, Hermosa Publishers, Albuquerque, New Mexico (1998) %
% [2] Cadafalch, J., Pérez-Segarra, C.D., Cònsul, R., and Oliva, A. %
% (2002) Verification of Finite Volume Computation on Steady State %
% Fluid Flow and Heat Transfer. Journal of Fluid Engineering %
% 124(1):11-21 %
% [3] https://www.grc.nasa.gov/www/wind/valid/tutorial/spatconv.html %
% %
%*************************************************************************%
%*************************************************************************%
% %
% n: data set (>=3) %
% h = [h(1) ... h(n)]: geometric discretization parameter representative %
% of the grid spacing. %
% h(1) is the finer grid %
% h(n) is the coarser grid %
% phi = [phi(1) ... phi(n)]: numerical solution. %
% p = [p(1) ... p(n-2)]: order of accuracy of the numerical scheme. %
% r = [r(1) ... r(n-1)]: refinement ratio %
% GCI = [GCI(1) ... GCI(n-1)]: Grid Convergence Index (error estimation) %
% %
%*************************************************************************%
clear all
close all
clc
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Post-processing %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Case: examples or problem
problem = 'example 2';
% Define grid spacings and some observed quatity corresponging
% to each grid spacing
switch problem
case 'example 1'
% Solutions
phi(1) = 0.971023;
phi(2) = 0.970500;
phi(3) = 0.968540;
phi(4) = 0.961780;
% Maximum solutions
phi_max(1) = 1;
phi_max(2) = 1;
phi_max(3) = 1;
phi_max(4) = 1;
% Grids
h(1) = 0.5;
h(2) = 1;
h(3) = 2;
h(4) = 4;
case 'example 2'
% Solutions
phi(1) = 0.701487;
phi(2) = 0.701509;
phi(3) = 0.701598;
phi(4) = 0.701933;
phi(5) = 0.702980;
phi(6) = 0.703646;
% Maximum solutions
phi_max(1) = 1;
phi_max(2) = 1;
phi_max(3) = 1;
phi_max(4) = 1;
phi_max(5) = 1;
phi_max(6) = 1;
% Grids
h(1) = 3.125e-3;
h(2) = 6.25e-3;
h(3) = 0.0125;
h(4) = 0.025;
h(5) = 0.05;
h(6) = 0.1;
case 'example 3'
% Solutions
phi(1) = 0.715007185936;
phi(2) = 0.715007007122;
phi(3) = 0.715006709099;
phi(4) = 0.714961528778;
phi(5) = 0.714575171471;
% Maximum solutions
phi_max(1) = 1;
phi_max(2) = 1;
phi_max(3) = 1;
phi_max(4) = 1;
phi_max(5) = 1;
% Grids
h(1) = 6.25e-3;
h(2) = 0.0125;
h(3) = 0.025;
h(4) = 0.05;
h(5) = 0.1;
case 'problem'
otherwise
warning('No problem introduce')
end
% Dimensionality of the problem: one, two, three
dimension = 'two';
switch dimension
case 'one'
d = 1;
case 'two'
d = 2;
case 'three'
d = 3;
otherwise
warning('No dimension introduce')
end
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Collection of data %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Number of data sets
n = size(phi,2);
% Check data sets
if (n < 3)
warning('The number of data must be three or more')
return
end
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% h-refinement %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Refinement ratio
for i = 1:n-1
r(i) = h(i+1)/h(i);
end
% Small parameter
epsilon = 1.0e-5;
% Average refinement ratio
r_avg = mean(r);
% Is refinement ratio constant?
if (abs(r(1:n-1) - r_avg) > epsilon)
warning('Grids with a non-constant refinement ratio')
fprintf(r)
return
end
% h should be ordering from the finer grid to the coarser grid (r > 1)
if (r <= 1)
warning('Refinement ratio should be great than 1')
fprintf(r)
return
end
% Control volume of the grid
ConVol = h.^(1/d);
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Nodal Classification %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Normalization of the solution
for i = 1:n
norm_phi(i) = phi(i)/abs(phi_max(i));
end
% Difference in the normalized solution
for i = 1:n-1
D_norm_phi(i) = norm_phi(i) - norm_phi(i+1);
end
% Small parameter
epsilon = 1.0e-30;
% Class of node
% Richardson node: nodes where e = C0*h^p can be calculated. These
% Richardson nodes do not necessarily fulfill all the
% requirements for the generalized Richardson
% extrapolation. In fact, the solution may be outside
% the asymptotic range (h not small enough).
% Converged node: the condition of converged nodes is ill-defined because
% it can also be accomplished by inflection points in the
% solution where all three solutions cross through the
% same point, which are obviously not converged nodes.
% Oscillatory node: zig-zag solution
for i = 1:n-2
aux = D_norm_phi(i)*D_norm_phi(i+1);
if (aux > epsilon)
node{i} = 'Richardson';
elseif (abs(aux) < epsilon) && (aux > -epsilon)
node{i} = 'Converged';
else
node{i} = 'Oscillatory';
end
end
% Location of the Richardson node in the arrays
i_R = find(strcmp(node,'Richardson'));
% Location of the Converged node in the arrays
i_C = find(strcmp(node,'Converged'));
% Location of the Oscillatory node in the arrays
i_O = find(strcmp(node,'Oscillatory'));
% Location of the Richardson nodes and Converged nodes
i_RC = [i_R i_C];
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Calculation of the observed p %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Diference of the solution
for i = 1:n-1
D_phi(i) = phi(i+1) - phi(i);
end
% Local order of convergence
for i = 1:n-2
p(i) = log(D_phi(i+1)/D_phi(i))/log(r_avg);
end
% Global order of convergence at the Richardson nodes
p_avg = mean(p(i_R));
% Standard deviation of p from the global p: measure of how close the
% solutions are to the asymptotic range
p_std = std(p(i_R));
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Calculation of the observed GCI %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Factor of Safety
if (n >= 3)
Fs = 1.25;
else
Fs = 3;
end
% GCI computation
for i = 1:n-2
% Theoretical absolute error
e_abs = abs(D_phi(i));
% Theoretical relative error
e_rel = e_abs/abs(phi(i));
% Richardson correction
factor = 1/abs(r_avg^p(i) - 1);
% Grid Convergence Index
GCI(i) = Fs*e_rel*factor;
end
% At the converged nodes the GCI is assumed to be 0
GCI(i_C) = 0;
% Overall volume occupied by all the Richardson nodes and converged nodes
ConVol_total = sum(ConVol(i_RC));
% Global GCI
GCI_avg = dot(ConVol(i_RC),GCI(i_RC))/ConVol_total;
% Standard deviation of GCI
GCI_std = std(GCI(i_RC));
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Asymptotic range %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Checking for asymptotic range
for i = 1:n-3
ratio(i) = r_avg^p(i)*(GCI(i)/GCI(i+1));
end
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Plotting %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Observation vs grid size
subplot(2,2,1)
semilogx(h,phi,'-o')
xlabel('Grid size')
ylabel('Observation')
xlim([h(1) h(n)])
grid on
% Order of convergence vs grid size
subplot(2,2,2)
semilogx(h(1:n-2),p,'-o')
hold on
x = linspace(h(1),h(n),21);
y = p_avg*ones(size(x));
y_1 = (p_avg + p_std)*ones(size(x));
y_2 = (p_avg - p_std)*ones(size(x));
semilogx(x,y,'r-.',x,y_1,'k-.',x,y_2,'k-.')
xlabel('Grid size')
ylabel('Order of convergence')
xlim([h(1) h(n)])
grid on
% GCI vs grid size
subplot(2,2,3)
semilogx(h(1:n-2),GCI*100,'-o')
hold on
y = GCI_avg*100*ones(size(x));
y_1 = (GCI_avg + GCI_std)*100*ones(size(x));
y_2 = (GCI_avg - GCI_std)*100*ones(size(x));
semilogx(x,y,'r-.',x,y_1,'k-.',x,y_2,'k-.')
xlabel('Grid size')
ylabel('GCI(%)')
xlim([h(1) h(n)])
grid on
% Asymptotic range vs grid size
subplot(2,2,4)
semilogx(h(1:n-3),ratio,'-o')
hold on
y = 1*ones(size(x));
semilogx(x,y,'r-.');
xlabel('Grid size')
ylabel('Asymptotic ratio')
xlim([h(1) h(n)])
grid on
%
set(gcf,'color','w')
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Array dimension fitting %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% refinement ratio
r(n) = 0;
% node classification
node{n-1} = ' ';
node{n } = ' ';
% order of convergence
p(n-1:n) = 0;
% GCI index
GCI(n-1:n) = 0;
% asymptotic range
ratio(n-2:n) = 0;
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Print results %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fprintf('--- Performs CFD verification --- \n')
fprintf('\n')
fprintf('Number of data sets = %d\n',n)
fprintf('\n')
fprintf('Grid Refinement Solution Order of Class of GCI(%%) Asymptotic \n')
fprintf('size ratio convergence node ratio (~1) \n')
fprintf('-------- ---------- -------- ----------- ----------- -------- ---------- \n')
for i = 1:n
fprintf('%f %f %f %f %s %f %f\n',h(i),r(i),phi(i),p(i),node{i},GCI(i)*100,ratio(i))
end
fprintf('\n')
fprintf('Main results:\n')
fprintf('+ The global order of convergence is %f (std = %f)\n',p_avg,p_std)
fprintf('+ The global GCI is %f%% (std = %f%%)\n',GCI_avg*100,GCI_std*100)
fprintf('\n')
fprintf('--- End --- \n')
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% END PROGRAM %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%