-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestCABARET2D.f90
537 lines (328 loc) · 14.2 KB
/
testCABARET2D.f90
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
program TestCABARET2D
!Test program to solve the linear advection problem using CABARET
use NetCDFModule
implicit none
INCLUDE 'netcdf.inc'
double precision, parameter :: PI=3.14159265358979324D0
integer, parameter :: nX=128,nY=128
integer, parameter :: nT=500
double precision :: x(nX), y(nY), q(nX,nY,2)
double precision :: q_cellface_x(nX,nY,2), q_cellface_y(nX,nY,2)
double precision :: c(2)
double precision :: time(nT)
double precision :: deltaX, deltaY
double precision :: CFL
double precision :: deltaT
double precision :: std
character(len = 8), dimension(3) :: dimensionNames
integer :: iX,iY, iT
character(len=128) :: outputFileName
type(netcdfFile) :: outputNetCDFfile
double precision :: flux_left, flux_right
double precision :: flux_south, flux_north
double precision :: q_left_extrap, q_right_extrap
double precision :: q_north_extrap, q_south_extrap
double precision :: q_cellface_left, q_cellface_right
double precision :: q_cellface_south, q_cellface_north
double precision :: q0, q_half_step_left
double precision :: q_half_step_centre, q_half_step_south(nX)
double precision :: oneOnDeltaX, oneOnDeltaY
outputFileName = 'linearAdvection2D.nc'
c(1) = 1.0
c(2) = 1.0
deltaX = 1.0
deltaY = 1.0
oneOnDeltaX = 1.0/deltaX
oneOnDeltaY = 1.0/deltaY
CFL = 0.4
deltaT = (deltaX * CFL)/ c(1)
print *, deltaT
std = 4.0
x(1) = 0.0
y(1) = 0.0
do iX=2,nX
x(iX) = x(iX-1) + deltaX
enddo
do iY=2,nY
y(iY) = y(iY-1) + deltaY
enddo
time(1) = 0.0
do iT=2,nT
time(iT) = time(iT-1) + deltaT
enddo
!Set initial condition
do iY = 1,nY
q0 = exp(-(y(iY) - y(nY/2))**2 / (2.0*std*std) )
do iX = 1,nX
q(iX,iY,1) = q0*exp(-(x(iX) - x(nX/2))**2 / (2.0*std*std) )
enddo
enddo
!Time Stepping Loop
outputNetCDFfile = New(outputFileName)
call WriteDimToFile(outputNetCDFfile, "x", x, "double")
call WriteDimToFile(outputNetCDFfile, "y", y, "double")
call WriteDimToFile(outputNetCDFfile, "time", time, "double")
dimensionNames(1) = "x"
dimensionNames(2) = "y"
dimensionNames(3) = "time"
call CreateNewVariable(outputNetCDFfile, 'q', dimensionNames, "double")
print *, 'Writing initial output'
call WriteToFile2D_Dble_SingleRecord(outputNetCDFfile, q(:,:,1), 'q', 1)
print *, 'output written!'
!Initialise the variables at cell boundaries
call compute_value_cell_faces(q(:,:,1),c,q_cellface_x(:,:,1),q_cellface_y(:,:,1))
do iT=2,nT
do iY=1,nY
do iX=1,nX
!==========================================================!
!FTCS half-step
!==========================================================!
!Compute fluxes in the x direction
!Guess values of the state variable the cell faces to the left
!and right
!Use the values of the state variables to compute the fluxes
!through the left and right cell faces
flux_left = fluxes_at_cell_faces(q_cellface_x(:,iY,1), iX, c(1))
flux_right = fluxes_at_cell_faces(q_cellface_x(:,iY,1), iX+1, c(1))
!Use the values of the state variables to compute the fluxes
!through the south and north cell faces
flux_south = fluxes_at_cell_faces(q_cellface_y(iX,:,1), iY, c(2))
flux_north = fluxes_at_cell_faces(q_cellface_y(iX,:,1), iY+1,c(2))
!Take the half step at the centered point
q_half_step_centre = q(iX,iY,1) - 0.5*deltaT*( &
oneOnDeltaX * (flux_right-flux_left) + &
oneOnDeltaY * (flux_north-flux_south) )
if (1==iX) then
!If we are on the left boundary, we need to compute the forward
!time half step to the left, using the periodic bc's
!in order to extrapolate the state variable forward in time
!Guess values of the state variable the cell faces to the left
!and right
!Use the values of the state variables to compute the fluxes
!through the left and right cell faces
flux_left = fluxes_at_cell_faces(q_cellface_x(:,iY,1), iX-1, c(1))
flux_right = fluxes_at_cell_faces(q_cellface_x(:,iY,1), iX, c(1))
!Use the values of the state variables to compute the fluxes
!through the south and north cell faces
flux_south = fluxes_at_cell_faces(q_cellface_y(nX,:,1), iY, c(2))
flux_north = fluxes_at_cell_faces(q_cellface_y(nX,:,1), iY+1 ,c(2))
!Take the half step at the point to the left
q_half_step_left = q(nX,iY,1) - 0.5*deltaT*( &
oneOnDeltaX * (flux_right-flux_left) + &
oneOnDeltaY * (flux_north-flux_south) )
end if
if (1==iY) then
!If we are on the southern boundary, we need to compute the forward
!time half step to the north, using the periodic bc's
!in order to extrapolate the state variable forward in time
!Guess values of the state variable the cell faces to the left
!and right
flux_left = fluxes_at_cell_faces(q_cellface_x(:,nY,1), iX, c(1))
flux_right = fluxes_at_cell_faces(q_cellface_x(:,nY,1), iX+1, c(1))
!Use the values of the state variables to compute the fluxes
!through the south and north cell faces
flux_south = fluxes_at_cell_faces(q_cellface_y(iX,:,1), iY-1, c(2))
flux_north = fluxes_at_cell_faces(q_cellface_y(iX,:,1), iY ,c(2))
q_half_step_south(iX) = q(iX,nY,1) - 0.5*deltaT*( &
oneOnDeltaX * (flux_right-flux_left) + &
oneOnDeltaY * (flux_north-flux_south) )
end if
!Extrapolate from t=n+1/2 to t=n+1 in the x direction
!call compute_value_cell_faces(q(:,iY,1), iX-1, q_cellface_left,q_cellface_right)
!Step the values at the cell faces forward in time by
!linear extrapolation
if (1==iX) then
! !This step steps forward in time the cell face values at y_i,x_i
call extrapolate_forward(q_half_step_left, iX-1, q_cellface_x(:,iY,:))
call flux_limitor_x2(q_cellface_x, q_cellface_y, q_half_step_left, iX, iY, deltaX, deltaY, deltaT, c(2))
endif
if (1==iY) then
! !This step steps forward in time the cell face values at y_i,x_i
call extrapolate_forward(q_half_step_south(iX), iY-1, q_cellface_y(iX,:,:))
call flux_limitor_y2(q_cellface_y, q_cellface_x, q_half_step_south(iX), iX, iY, deltaX, deltaY, deltaT, c(1))
endif
!This step updates the cell face values at x_i+1
!call flux_limitor_x2(q_cellface_x, q_cellface_y, q_half_step_left, iX-1, iY, deltaX, deltaY, deltaT, c(2))
call extrapolate_forward(q_half_step_centre, iX, q_cellface_x(:,iY,:))
call extrapolate_forward(q_half_step_centre, iY, q_cellface_y(iX,:,:))
call flux_limitor_x2(q_cellface_x, q_cellface_y, q_half_step_centre, iX+1, iY, deltaX, deltaY, deltaT, c(2))
call flux_limitor_y2(q_cellface_y, q_cellface_x, q_half_step_centre, iX, iY+1, deltaX, deltaY, deltaT, c(1))
!
flux_left = fluxes_at_cell_faces(0.5*(q_cellface_x(:,iY,1)+q_cellface_x(:,iY,2)), iX, c(1))
flux_right = fluxes_at_cell_faces(0.5*(q_cellface_x(:,iY,1)+q_cellface_x(:,iY,2)), iX+1, c(1))
!Use the values of the state variables to compute the fluxes
!through the south and north cell faces
flux_south = fluxes_at_cell_faces(0.5*(q_cellface_y(iX,:,1)+q_cellface_y(iX,:,2)), iY, c(2))
flux_north = fluxes_at_cell_faces(0.5*(q_cellface_y(iX,:,1)+q_cellface_y(iX,:,2)), iY+1,c(2))
!Finally, take another BTCS step
q(iX,iY,2) = q(iX,iY,1) - deltaT*( &
oneOnDeltaX * (flux_right-flux_left) + &
oneOnDeltaY * (flux_north-flux_south) )
q_half_step_left = q_half_step_centre
enddo !END iX
q_half_step_south = q_half_step_centre
enddo !END iY
print *, 'time step: ', iT
call WriteToFile2D_Dble_SingleRecord(outputNetCDFfile, q(:,:,2), 'q', iT)
q(:,:,1) = q(:,:,2)
q_cellface_x(:,:,1) = q_cellface_x(:,:,2)
q_cellface_x(:,:,2) = 0.0
q_cellface_y(:,:,1) = q_cellface_y(:,:,2)
q_cellface_y(:,:,2) = 0.0
enddo !!END Time Stepping routine
call Delete(outputNetCDFfile)
contains
subroutine compute_value_cell_faces(q,c,q_cellface_x,q_cellface_y)
double precision, intent(in) :: q(:,:)
double precision, intent(in) :: c(2)
double precision, dimension (:,:), intent(out) :: q_cellface_x,q_cellface_y
integer :: iX, iY, nX, nY
nX = size(q,1)
nY = size(q,2)
do iY=1,nY
do iX=1,nX
if (0.0>c(1)) then
q_cellface_x(iX,iY) = q(iX,iY)
else
if (1==iX) then
q_cellface_x(iX,iY) = q(nX,iY)
else
q_cellface_x(iX,iY) = q(iX-1,iY)
endif !END 1==iX
endif !END 0>c(1)
if (0.0>c(2)) then
q_cellface_y(iX,iY) = q(iX,iY)
else
if (1==iY) then
q_cellface_y(iX,iY) = q(iX,nY)
else
q_cellface_y(iX,iY) = q(iX,iY-1)
end if !END 1==iY
end if !0>c(2)
enddo !END x-loop
enddo !END y-loop
end subroutine compute_value_cell_faces
function fluxes_at_cell_faces(q_at_cell_face, iX, c) result(flux)
double precision, dimension(:), intent(in) :: q_at_cell_face
integer, intent(in) :: iX
double precision, intent(in) :: c
double precision :: flux ! output
integer :: nX
nX = size(q_at_cell_face)
if (iX>nX) then
flux = c * q_at_cell_face(1)
else if (0==iX) then
flux = c * q_at_cell_face(nX)
else
flux = c * q_at_cell_face(iX)
endif
end function fluxes_at_cell_faces
subroutine flux_limitor_x2(q_cellface_x, q_cellface_y, q_half_step, iX, iY, deltaX, deltaY,deltaT, v)
double precision, intent(inout) :: q_cellface_x(:,:,:)
double precision, intent(in) :: q_cellface_y(:,:,:)
double precision, intent(in) :: q_half_step
integer, intent(in) :: iX, iY
double precision, intent(in) :: deltaX, deltaY, deltaT, v
double precision :: max_limiter
double precision :: min_limiter
integer :: nX, nY
double precision :: source_term
nX = size(q_cellface_x,1)
nY = size(q_cellface_x,2)
if (iY == 0) then
source_term = -1.0 * (v / deltaY) * ( q_cellface_y(iX,nY,1) - q_cellface_y(iX,nY-1,1) ) * deltaT
elseif (iY==1) then
source_term = -1.0 * (v / deltaY) * ( q_cellface_y(iX,1,1) - q_cellface_y(iX,nY,1) ) * deltaT
else
source_term = -1.0 * (v / deltaY) * ( q_cellface_y(iX,iY,1) - q_cellface_y(iX,iY-1,1) ) * deltaT
endif
if (0==iX) then
max_limiter = max(q_cellface_x(nX-1,iY,1),q_cellface_x(nX,iY,1), q_half_step) + source_term
min_limiter = min(q_cellface_x(nX-1,iY,1),q_cellface_x(nX,iY,1), q_half_step) + source_term
if (q_cellface_x(nX,iY,2) > max_limiter) then
q_cellface_x(nX,iY,2) = max_limiter
endif
if (q_cellface_x(nX,iY,2) < min_limiter) then
q_cellface_x(nX,iY,2) = min_limiter
endif
else if (1==iX) then
max_limiter = max(q_cellface_x(nX,iY,1),q_cellface_x(1,iY,1),q_half_step) + source_term
min_limiter = min(q_cellface_x(nX,iY,1),q_cellface_x(1,iY,1),q_half_step) + source_term
if (q_cellface_x(1,iY,2) > max_limiter) then
q_cellface_x(1,iY,2) = max_limiter
endif
if (q_cellface_x(1,iY,2) < min_limiter) then
q_cellface_x(1,iY,2) = min_limiter
endif
else
max_limiter = max(q_cellface_x(iX-1,iY,1), q_cellface_x(iX,iY,1), q_half_step) + source_term
min_limiter = min(q_cellface_x(iX-1,iY,1), q_cellface_x(iX,iY,1), q_half_step) + source_term
if (q_cellface_x(iX,iY,2) > max_limiter) then
q_cellface_x(iX,iY,2) = max_limiter
endif
if (q_cellface_x(iX,iY,2) < min_limiter) then
q_cellface_x(iX,iY,2) = min_limiter
endif
endif
end subroutine flux_limitor_x2
subroutine flux_limitor_y2(q_cellface_y, q_cellface_x, q_half_step, iX, iY, deltaX, deltaY,deltaT, u)
double precision, intent(inout) :: q_cellface_y(:,:,:)
double precision, intent(in) :: q_cellface_x(:,:,:)
double precision, intent(in) :: q_half_step
integer, intent(in) :: iX, iY
double precision, intent(in) :: deltaX, deltaY, deltaT, u
double precision :: max_limiter
double precision :: min_limiter
integer :: nX, nY
double precision :: source_term
nX = size(q_cellface_x,1)
nY = size(q_cellface_x,2)
if (iX == 0) then
source_term = - 2.0 * (u / deltaX) * (q_cellface_x(nX,iY,1) - q_cellface_x(nX-1,iY,1)) * deltaT
elseif (iX==1) then
source_term = - 2.0 * (u / deltaX) * (q_cellface_x(1,iY,1) - q_cellface_x(nX,iY,1)) * deltaT
else
source_term = - 2.0 * (u / deltaX) * (q_cellface_x(iX,iY,1) - q_cellface_x(iX-1,iY,1)) * deltaT
endif
if (0==iY) then
max_limiter = max(q_cellface_y(iX,nY-1,1),q_cellface_y(iX,nY,1), q_half_step) + source_term
min_limiter = min(q_cellface_y(iX,nY-1,1),q_cellface_y(iX,nY,1), q_half_step) + source_term
if (q_cellface_y(iX,nY,2) > max_limiter) then
q_cellface_y(iX,nY,2) = max_limiter
endif
if (q_cellface_y(iX,nY,2) < min_limiter) then
q_cellface_y(iX,nY,2) = min_limiter
endif
else if (1==iY) then
max_limiter = max(q_cellface_y(iX,1,1),q_cellface_x(iX,1,1),q_half_step) + source_term
min_limiter = min(q_cellface_y(iX,1,1),q_cellface_x(iX,1,1),q_half_step) + source_term
if (q_cellface_y(iX,1,2) > max_limiter) then
q_cellface_y(iX,1,2) = max_limiter
endif
if (q_cellface_y(iX,1,2) < min_limiter) then
q_cellface_y(iX,1,2) = min_limiter
endif
else
max_limiter = max(q_cellface_y(iX,iY-1,1),q_cellface_y(iX,iY,1),q_half_step) + source_term
min_limiter = min(q_cellface_y(iX,iY-1,1),q_cellface_y(iX,iY,1),q_half_step) + source_term
if (q_cellface_y(iX,iY,2) > max_limiter) then
q_cellface_y(iX,iY,2) = max_limiter
endif
if (q_cellface_y(iX,iY,2) < min_limiter) then
q_cellface_y(iX,iY,2) = min_limiter
endif
endif
end subroutine flux_limitor_y2
subroutine extrapolate_forward(q_half_step_centre, iX, q_cell_face)
double precision, intent(in) :: q_half_step_centre
integer, intent(in) :: iX
double precision, intent(inout) :: q_cell_face(:,:)
integer :: nX
nX = size(q_cell_face)
if (0==iX .or. nX==iX) then
q_cell_face(1,2) = (2.0 * q_half_step_centre) - q_cell_face(nX,1)
else
q_cell_face(iX+1,2) = (2.0 * q_half_step_centre) - q_cell_face(iX,1)
endif
end subroutine extrapolate_forward
end program TestCABARET2D