-
Notifications
You must be signed in to change notification settings - Fork 3
/
decide_AB2_RK4.m
117 lines (99 loc) · 2.86 KB
/
decide_AB2_RK4.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
d = load_t_file('dt.t');
dx = 1.25/640;
tmax = d(end,1);
nt=size(d,1);
eps = d(end,2)/d(end,5);
dt = d(:,2);
cfl = d(:,3);
umax = dx*cfl./dt;
fprintf('tmax=%f nt=%i cfl_mean=%f eps=%e dt_mean=%e\n',tmax,nt,mean(cfl),eps,tmax/nt)
%% simulate what would happen if u does not change
%% same level
CFL_RK4 = 2.0;
CFL_AB2 = 0.2;
eps_new = eps;%/4;
dx_new = dx;%/2;
fprintf('Assuming CFL_RK4=%f CFL_AB2=%f\n',CFL_RK4,CFL_AB2)
t_rk4 = 0;
it_rk4 = 0;
while t_rk4 < tmax
u = interp1(d(:,1),umax,t_rk4,'nearest');
dt_rk4 = min( [ CFL_RK4*dx_new/u, 0.99*eps_new ] );
t_rk4 = t_rk4 + dt_rk4;
it_rk4 = it_rk4 + 1;
end
t_ab2 = 0;
it_ab2 = 0;
while t_ab2<tmax
u = interp1(d(:,1),umax,t_ab2,'nearest');
dt_ab2 = min( [ CFL_AB2*dx_new/u, 0.99*eps_new ] );
t_ab2 = t_ab2 + dt_ab2;
it_ab2 = it_ab2 + 1;
end
fprintf('---------SAME LEVEL--------------------\n');
fprintf('AB2: nt=%i nrhs=%i dt_mean=%e\n',it_ab2, it_ab2, tmax/it_ab2)
fprintf('RK4: nt=%i nrhs=%i dt_mean=%e\n',it_rk4, it_rk4*4, tmax/it_rk4)
if ( it_rk4*4 > it_ab2 )
fprintf('AB2 is faster (factor=%f)\n',it_rk4*4/it_ab2)
else
fprintf('RK4 is faster (factor=%f)\n',it_ab2/it_rk4/4)
end
%% one level up
CFL_RK4 = 2.0;
CFL_AB2 = 0.2;
eps_new = eps/4;
dx_new = dx/2;
t_rk4 = 0;
it_rk4 = 0;
while t_rk4 < tmax
u = interp1(d(:,1),umax,t_rk4,'nearest');
dt_rk4 = min( [ CFL_RK4*dx_new/u, 0.99*eps_new ] );
t_rk4 = t_rk4 + dt_rk4;
it_rk4 = it_rk4 + 1;
end
t_ab2 = 0;
it_ab2 = 0;
while t_ab2<tmax
u = interp1(d(:,1),umax,t_ab2,'nearest');
dt_ab2 = min( [ CFL_AB2*dx_new/u, 0.99*eps_new ] );
t_ab2 = t_ab2 + dt_ab2;
it_ab2 = it_ab2 + 1;
end
fprintf('---------ONE LEVEL UP--------------------\n');
fprintf('AB2: nt=%i nrhs=%i dt_mean=%e\n',it_ab2, it_ab2, tmax/it_ab2)
fprintf('RK4: nt=%i nrhs=%i dt_mean=%e\n',it_rk4, it_rk4*4, tmax/it_rk4)
if ( it_rk4*4 > it_ab2 )
fprintf('AB2 is faster (factor=%f)\n',it_rk4*4/it_ab2)
else
fprintf('RK4 is faster (factor=%f)\n',it_ab2/it_rk4/4)
end
%% two levels up
CFL_RK4 = 2.0;
CFL_AB2 = 0.2;
eps_new = eps/4/4;
dx_new = dx/2/2;
t_rk4 = 0;
it_rk4 = 0;
while t_rk4 < tmax
u = interp1(d(:,1),umax,t_rk4,'nearest');
dt_rk4 = min( [ CFL_RK4*dx_new/u, 0.99*eps_new ] );
t_rk4 = t_rk4 + dt_rk4;
it_rk4 = it_rk4 + 1;
end
t_ab2 = 0;
it_ab2 = 0;
while t_ab2<tmax
u = interp1(d(:,1),umax,t_ab2,'nearest');
dt_ab2 = min( [ CFL_AB2*dx_new/u, 0.99*eps_new ] );
t_ab2 = t_ab2 + dt_ab2;
it_ab2 = it_ab2 + 1;
end
fprintf('---------TWO LEVEL UP--------------------\n');
fprintf('AB2: nt=%i nrhs=%i dt_mean=%e\n',it_ab2, it_ab2, tmax/it_ab2)
fprintf('RK4: nt=%i nrhs=%i dt_mean=%e\n',it_rk4, it_rk4*4, tmax/it_rk4)
if ( it_rk4*4 > it_ab2 )
fprintf('AB2 is faster (factor=%f)\n',it_rk4*4/it_ab2)
else
fprintf('RK4 is faster (factor=%f)\n',it_ab2/it_rk4/4)
end
fprintf('-----------------------------------------\n');