-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRK.c
297 lines (167 loc) · 4.66 KB
/
RK.c
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "Global.h"
#include "math.h"
#include "par.h"
int CalculateRHS(){
int i, p;
FluxDriver(); // RIEMANN CALL
VolumeFlux(); // volume flux
for(i=1; i<IELEM-1; i++){ // khi :: 22/07/2012
for(p=0; p<IORDER; p++ ){
rRKInt[i][p][0] = 0. ;
rRKInt[i][p][0] = rRKInt[i][p][0] - (rFaceFlux[i][0]*Basis(p,1.0)- rFaceFlux[i-1][0]*Basis(p,-1.0));
rRKInt[i][p][0] = rRKInt[i][p][0] + rVolumeFlux[i][p][0];
rRKInt[i][p][0] = rRKInt[i][p][0]/Norms(p)*(2.0/iEl_Size);
//if(rRKInt[i][p][0] > 1000.0){printf("%f \n", rRKInt[i][p][0]);
//exit(EXIT_FAILURE);
//}
}
}
for(p=0; p<IORDER; p++ ){
rRKInt[IELEM-1][p][0]=rRKInt[1][p][0]; // checked khi
rRKInt[0][p][0]=rRKInt[IELEM-2][p][0]; // checked khi
}
}
int RungeKutta(){
double tol,rCurrent;
double rDT,midpoint;
int i,p,k,r,pp,t ;
float test, S, L0;
double uat1, uathold;;
tol = 0.00000001;
rCurrent = 0.0;
AdvecInit();
RKINT();
t=0;
rDT = 0.0;
while(rCurrent<ITIME){
// calculate the size of the time step
rDT = CFL*iEl_Size;
rDT = MIN(ITIME - rCurrent,rDT);
for(i=0; i<IELEM; i++){ // khi :: 22/07/2012
for(p=0; p<IORDER; p++ ){
rRKInt[i][p][0]=sol[i][p][0]; // copy solution to temp array for runge-kutta
}
}
for (r=0; r<IRK; r++){ // khi :: 22/07/2012
VolumeFlux(); // volume flux
FluxDriver(); // RIEMANN CALL
for(i=1; i<(IELEM-1); i++){ // khi :: 22/07/2012
//if(i==6)printf("%d %f \n",t, rFaceFlux[i-1][0]);
for(p=0; p<IORDER; p++ ){
rRHS[0] = 0. ;
rRHS[0] = rRHS[0] - (rFaceFlux[i][0]*Basis(p,1.0)- rFaceFlux[i-1][0]*Basis(p,-1.0));
//if(i==23)printf("%d: %f \n", p, rFaceFlux[i][0]);
rRHS[0] = rRHS[0] + rVolumeFlux[i][p][0];
//rRHS[0] = rRHS[0]/iEl_Size;
rRHS[0] = (2.0*rRHS[0])/(Norms(p)*iEl_Size);
rRKInt[i][p][0]= rRKAlpha[r]*sol[i][p][0]+rRKBeta[r]*rRKInt[i][p][0]+ rDT*rRKGamma[r]*rRHS[0];
//exit(EXIT_FAILURE);
}
}
for(p=0; p<IORDER; p++ ){
rRKInt[IELEM-1][p][0]=rRKInt[1][p][0]; // checked khi
rRKInt[0][p][0]=rRKInt[IELEM-2][p][0]; // checked khi
}
}
//printf("%d %f \n",i, rRKInt[5][0][0]);
//if(t==0)exit(EXIT_FAILURE);
for(i=0; i<IELEM; i++){ // khi :: 22/07/2012
for(p=0; p<IORDER; p++ ){
sol[i][p][0]=rRKInt[i][p][0]; // copy solution to temp array for runge-kutta
}
}
printf("%d: %f \n",t, rCurrent);
rCurrent = rCurrent + rDT;
t = t+1;
}
FILE *kpp = fopen("Final.dat","w");
midpoint = IEL_SIZE/2.0;
//L0= 0;
//S=0.;
for(i=1; i<(INODE-1); i++){
fprintf(kpp,"%f %f %f \n",midpoint,rRKInt[i][0][0] , InitCond(midpoint - ITIME));
midpoint = midpoint + IEL_SIZE;
S = ABS(rRKInt[i][0][0] - InitCond(midpoint - ITIME));
L0 = MAX(L0,S);
//printf("%f \n",L0);
}
fclose(kpp);
}
int RKTEST(){
int r;
float t,k1, k2, deltaT;
float y0, update,rhs;
t=1; // starting time
y0 =1; // initial conditions
deltaT = 0.025; //time step
while(t<1.2){
update = y0;
//printf("%f %f \n",t, y0);
k1 = tan(y0) + 1;
k2 = tan(y0+0.66666*deltaT*k1) + 1;
update = y0 + deltaT*(0.25*k1 + 0.75*k2);
y0= update;
t = t +deltaT;
}
}
int RK2(){
double tol,rCurrent;
double rDT,midpoint;
int i,p,k,r,pp,t ;
float test, S, L0;
double uat1, uathold;;
tol = 0.00000001;
rCurrent = 0.0;
AdvecInit();
t=0;
rDT = 0.0;
while(rCurrent<ITIME){
// calculate the size of the time step
rDT = CFL*iEl_Size;
rDT = MIN(ITIME - rCurrent,rDT);
//rRKInt=sol; // copy solution to temp array for runge-kutta
for(i=1; i<IELEM-1; i++){
for(p=0; p<IORDER; p++){
rRKInt[i][p][0] = sol[i][p][0];
}
}
// k1
CalculateRHS(); // this is k1
for(i=1; i<IELEM-1; i++){
for(p=0; p<IORDER; p++){
K1[i][p][0] = rRKInt[i][p][0];
rRKInt[i][p][0] = sol[i][p][0] + (2.0/3.0)*rDT*K1[i][p][0] ;
K2[i][p][0] = rRKInt[i][p][0];
}
}
CalculateRHS(); // this is k1
for(i=1; i<IELEM-1; i++){
for(p=0; p<IORDER; p++){
rRKInt[i][p][0] = sol[i][p][0] + rDT*(0.25*K1[i][p][0] + 0.75*K2[i][p][0]);
printf("%f \n", rRKInt[i][p][0]);
sol[i][p][0] = rRKInt[i][p][0]; // copy solution back
}
}
//update = y0 + deltaT*(0.25*k1 + 0.75*k2);
// apply BC
//printf("%d %f \n",i, rRKInt[5][0][0]);
//if(t==0)exit(EXIT_FAILURE);
rCurrent = rCurrent + rDT;
t = t+1;
}
FILE *kkpp = fopen("Final.dat","w");
midpoint = IEL_SIZE/2.0;
//L0= 0;
//S=0.;
for(i=1; i<(INODE-1); i++){
fprintf(kkpp,"%f %f %f \n",midpoint,rRKInt[i][0][0] , InitCond(midpoint - ITIME));
midpoint = midpoint + IEL_SIZE;
S = ABS(rRKInt[i][0][0] - InitCond(midpoint - ITIME));
L0 = MAX(L0,S);
//printf("%f \n",L0);
}
fclose(kkpp);
}