-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgrid.f90
executable file
·1785 lines (1560 loc) · 55.5 KB
/
cgrid.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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
program cgrid_shallow_water
!C-grid shallow water model written by Peter Dueben based on F77 code of David Marshall
! USE rp_emulator !Use edited emulator for reduced precision to mimic bit flips (see difference_emulator.txt)
implicit none
!Grid parameters:
integer :: nx_f,ny_f,nx_c,ny_c,nx_i,ny_i,nt_f,nt_c
parameter(nx_f=181,ny_f=31) !Resolution fine grid
parameter(nx_c=61,ny_c=11) !Resolution coarse grid
parameter(nx_i=181,ny_i=31) !Resolution input file
parameter(nt_f=3,nt_c=3) !Time stepping fine/coarse grid
!General parameters:
integer :: i,j,k,l,n_c,n_f,nstop,itestcase, nnudge !Time intervals for nudging in timesteps
integer*8 :: k8,l8
integer :: nsec,nwrite,ninter
integer :: nxstep,nystep !Number of steps to jump for "on-screen" output
character*10 :: cinput
REAL*8 :: Lx,Ly !Domain size
REAL*8 :: slip,gp
real*8 :: time1, time2, time3, time_c, time_f,rdummy
logical :: lboundary ! .T. if with boundaries.
logical :: lrestart ! .T. if restart
REAL*8 :: random1, random2, random3
REAL*8 :: pfault
INTEGER :: ntime_f(0:nx_f,0:ny_f)
INTEGER*8 :: ntest1, ntest2
LOGICAL :: lerror, lbackup, ltopography
REAL*8 :: hr_c(0:nx_c,0:ny_c,0:1),ur_c(0:nx_c,0:ny_c,0:1),vr_c(0:nx_c,0:ny_c,0:1)
CHARACTER(len=70) :: output
!Parameters coarse grid:
REAL*8 :: h_c(0:nx_c,0:ny_c),u_c(0:nx_c,0:ny_c),v_c(0:nx_c,0:ny_c),taux_c(0:ny_c),tauy_c(0:nx_c) ! layer thickness (h), velocity components (u,v) and wind forcing
REAL*8 :: ht_c(0:nx_c,0:ny_c),ut_c(0:nx_c,0:ny_c),vt_c(0:nx_c,0:ny_c) !Fields to transform between the coarse and the fine model.
REAL*8 :: HC_c(0:nx_c,0:ny_c) !Height of the fluid column
REAL*8 :: dh_c(0:nx_c,0:ny_c,nt_c),du_c(0:nx_c,0:ny_c,nt_c),dv_c(0:nx_c,0:ny_c,nt_c) !Time increments for AB timestepping
REAL*8 :: ab_c(nt_c) !AB coefficients
REAL*8 :: dx_c,dy_c,dt_c,rdx_c,rdy_c
REAL*8 :: fu_c(0:ny_c),fv_c(0:ny_c) !Coriolis parameter at u and v grid-points respectively
REAL*8 :: b_c(0:nx_c,0:ny_c) ! Bernoulli potential and relative vorticity
REAL*8 :: nudge_c, nuu_c, nuh_c
!Parameters fine grid:
REAL*8 :: h_f(0:nx_f,0:ny_f),u_f(0:nx_f,0:ny_f),v_f(0:nx_f,0:ny_f),taux_f(0:ny_f),tauy_f(0:nx_f) ! layer thickness (h), velocity components (u,v) and wind forcing
REAL*8 :: ht_f(0:nx_f,0:ny_f),ut_f(0:nx_f,0:ny_f),vt_f(0:nx_f,0:ny_f) !Fields to transform between the coarse and the fine model.
REAL*8 :: HC_f(0:nx_f,0:ny_f) !Height of the fluid column
REAL*8 :: dh_f(0:nx_f,0:ny_f,nt_f),du_f(0:nx_f,0:ny_f,nt_f),dv_f(0:nx_f,0:ny_f,nt_f) !Time increments for AB timestepping
REAL*8 :: ab_f(nt_f) !AB coefficients
REAL*8 :: dx_f,dy_f,dt_f,rdx_f,rdy_f
REAL*8 :: fu_f(0:ny_f),fv_f(0:ny_f) !Coriolis parameter at u and v grid-points respectively
REAL*8 :: b_f(0:nx_f,0:ny_f)! Bernoulli potential and relative vorticity
REAL*8 :: nudge_f, nuu_f, nuh_f
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! Emulator
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
RPE_ACTIVE = .FALSE.
RPE_STOCHASTIC = .FALSE.
rpe_fault_rate = 0.0000001_8 !For simulated bit flips
lbackup = .TRUE.
ltopography = .FALSE. !Allows reproduction of coarse topography if hardware faults are simulated
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! INITIALIZATION
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
nstop = 100000 ! number of timesteps on coarse grid
nwrite= 1000 ! number of timesteps between file output
nxstep=4 !Advanced terminal output
nystep=nxstep*2
ninter = 1 !Ratio between coarse and fine timestep
lrestart = .FALSE.
cinput = '600000' ! Input number
nudge_c = 0.0_8 ! Strength of nudging
nudge_f = 0.0_8
nnudge = 1 ! Timestep to update transformation fields
itestcase = 5 ! Testcase: 1 = Gaussian hill (unstable), 2 = Stommel, 3 = Gaussian hill (stable), 4 = Random fields, 5 = Isolated mountain test
slip=1._8 ! free-slip (0.) or no-slip (1.)?
nsec=0 ! Running output number
! Initialise model fields:
CALL initialise(itestcase,lboundary,lrestart,Lx,Ly,gp,cinput,nx_i,ny_i,ninter,&
& nx_f,ny_f,nt_f,nuh_f,nuu_f,dt_f,HC_f,&
& dx_f,dy_f,rdx_f,rdy_f,ab_f,fu_f,fv_f,taux_f,tauy_f,h_f,dh_f,u_f,du_f,v_f,dv_f, &
& nx_c,ny_c,nt_c,nuh_c,nuu_c,dt_c,HC_c,&
& dx_c,dy_c,rdx_c,rdy_c,ab_c,fu_c,fv_c,taux_c,tauy_c,h_c,dh_c,u_c,du_c,v_c,dv_c)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! MAIN LOOP
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
time_c = 0.0_8
time_f = 0.0_8
ut_c = 0.0_8
vt_c = 0.0_8
ht_c = 0.0_8
ut_f = 0.0_8
vt_f = 0.0_8
ht_f = 0.0_8
ntime_f(:,:) = 1
ntest1 = 0
CALL CPU_TIME(time1)
!Initialise backup system:
IF(lbackup)THEN
CALL finetocoarse(nx_c,ny_c,hr_c(:,:,0),ur_c(:,:,0),vr_c(:,:,0),&
&nx_f,ny_f,h_f,u_f,v_f)
CALL finetocoarse(nx_c,ny_c,hr_c(:,:,1),ur_c(:,:,1),vr_c(:,:,1),&
&nx_f,ny_f,h_f,u_f,v_f)
END IF
DO n_c=1,nstop+1
!Output model fields:
RPE_ACTIVE = .FALSE.
IF(mod(n_c,nwrite)==1.or.nwrite==1) CALL output_fields(lboundary,nsec,nxstep,nystep,&
& u_c,du_c,v_c,dv_c,h_c,dh_c,dx_c,dy_c,nx_c,ny_c,nt_c,&
& u_f,du_f,v_f,dv_f,h_f,dh_f,dx_f,dy_f,nx_f,ny_f,nt_f)
RPE_ACTIVE = .TRUE.
!Perform model timestep:
CALL timeloop(n_c,lboundary,lrestart,gp,slip,&
& nx_f,ny_f,nt_f,nuh_f,nuu_f,dt_f,&
& rdx_f,rdy_f,ab_f,fu_f,fv_f,taux_f,tauy_f,&
& h_f,dh_f,u_f,du_f,v_f,dv_f,ut_f,vt_f,ht_f,HC_f,nudge_f)
!This will mimic a hardware fault and set large parts of the domain to NAN:
IF(n_c==9000.or.n_c==19000.or.n_c==49000.or.n_c==99000)THEN
k8 = HUGE(k8)
rdummy = TRANSFER(k8,rdummy)
k = (nx_f-1)/2 !The definition of k and l will define the part of the domain that is set to NAN
l = (ny_f-1)
DO i=1,k
DO j=1,l
h_f(i,j) = rdummy
u_f(i,j) = rdummy
v_f(i,j) = rdummy
dh_f(i,j,1) = rdummy
dh_f(i,j,2) = rdummy
dh_f(i,j,3) = rdummy
du_f(i,j,1) = rdummy
du_f(i,j,2) = rdummy
du_f(i,j,3) = rdummy
dv_f(i,j,1) = rdummy
dv_f(i,j,2) = rdummy
dv_f(i,j,3) = rdummy
END DO
END DO
END IF
!This will run the backup system:
IF(lbackup)THEN
call fault_secure(n_c,nx_c,ny_c,hr_c,ur_c,vr_c,HC_c,nx_f,ny_f,h_f,u_f,v_f,HC_f,ntime_f,ltopography)
u_c = ur_c(:,:,mod(n_c,2))
v_c = vr_c(:,:,mod(n_c,2))
h_c = hr_c(:,:,mod(n_c,2))
END IF
nsec =nsec + 1
END DO
CALL CPU_TIME(time2)
write(*,*) 'Time model run: ', time2-time1
end program cgrid_shallow_water
subroutine fault_secure(n_c,nx_c,ny_c,h_c,u_c,v_c,HC_c,&
&nx_f,ny_f,h_f,u_f,v_f,HC_f,ntime_f,ltopography)
!This is the backup system
USE rp_emulator
implicit none
INTEGER :: nx_c, ny_c, n_c
REAL*8 :: u_c(0:nx_c,0:ny_c,0:1), v_c(0:nx_c,0:ny_c,0:1), h_c(0:nx_c,0:ny_c,0:1), HC_c(0:nx_c,0:ny_c)
INTEGER :: nx_f, ny_f
REAL*8 :: u_f(0:nx_f,0:ny_f), v_f(0:nx_f,0:ny_f), h_f(0:nx_f,0:ny_f), HC_f(0:nx_f,0:ny_f)
INTEGER :: i,j,k,l
INTEGER :: ntime_f(0:nx_f,0:ny_f)
REAL*8 :: gf(3,3),gc(2,2),tc,uc
REAL*8 :: ut_f(0:nx_f,0:ny_f), vt_f(0:nx_f,0:ny_f), ht_f(0:nx_f,0:ny_f), HC2_f(0:nx_f,0:ny_f)
logical :: lerror
logical :: hl(0:nx_c,0:ny_c),ul(0:nx_c,0:ny_c),vl(0:nx_c,0:ny_c),ltopography
character(len=100) filename
!Flag that defines whether a hardware fault was found for a specific parameter
DO i=1,nx_c-1
Do j=1,ny_c-1
hl(i,j) = .TRUE.
ul(i,j) = .TRUE.
vl(i,j) = .TRUE.
END DO
END DO
!This will map the prognostic fields from the model to the backup grid:
CALL finetocoarse(nx_c,ny_c,h_c(:,:,mod(n_c,2)),u_c(:,:,mod(n_c,2)),v_c(:,:,mod(n_c,2)),&
&nx_f,ny_f,h_f,u_f,v_f)
!This will test whether the parameter on the backup grid has changed too much:
DO i=1,nx_c-1
Do j=1,ny_c-1
IF(abs(h_c(i,j,0)-h_c(i,j,1)).gt.0.05_8.or.h_c(i,j,mod(n_c,2)).ne.h_c(i,j,mod(n_c,2)))THEN !Put in flow depebent threshold here for h, second part tests for NAN
h_c(i,j,mod(n_c,2)) = h_c(i,j,mod(n_c+1,2))
hl(i,j) = .FALSE.
hl(i-1,j) = .FALSE.
hl(i,j-1) = .FALSE.
hl(i-1,j-1) = .FALSE.
IF(i==1)THEN
hl(nx_c-1,j) = .FALSE.
hl(nx_c-1,j-1) = .FALSE.
END IF
IF(j==1)THEN
hl(i,ny_c-1) = .FALSE.
hl(i-1,ny_c-1) = .FALSE.
END IF
IF(i==1.and.j==1)THEN
hl(nx_c-1,ny_c-1) = .FALSE.
END IF
END IF
IF(abs(u_c(i,j,0)-u_c(i,j,1)).gt.0.01_8.or.u_c(i,j,mod(n_c,2)).ne.u_c(i,j,mod(n_c,2)))THEN !Put in flow depebent threshold here for u, second part tests for NAN
u_c(i,j,mod(n_c,2)) = u_c(i,j,mod(n_c+1,2))
ul(i,j) = .FALSE.
ul(i-1,j) = .FALSE.
ul(i,j-1) = .FALSE.
ul(i-1,j-1) = .FALSE.
IF(i==1)THEN
ul(nx_c-1,j) = .FALSE.
ul(nx_c-1,j-1) = .FALSE.
END IF
IF(j==1)THEN
ul(i,ny_c-1) = .FALSE.
ul(i-1,ny_c-1) = .FALSE.
END IF
IF(i==1.and.j==1)THEN
ul(nx_c-1,ny_c-1) = .FALSE.
END IF
END IF
IF(abs(v_c(i,j,0)-v_c(i,j,1)).gt.0.01_8.or.v_c(i,j,mod(n_c,2)).ne.v_c(i,j,mod(n_c,2)))THEN !Put in flow depebent threshold here for v, second part tests for NAN
v_c(i,j,mod(n_c,2)) = v_c(i,j,mod(n_c+1,2))
vl(i,j) = .FALSE.
vl(i-1,j) = .FALSE.
vl(i,j-1) = .FALSE.
vl(i-1,j-1) = .FALSE.
IF(i==1)THEN
vl(nx_c-1,j) = .FALSE.
vl(nx_c-1,j-1) = .FALSE.
END IF
IF(j==1)THEN
vl(i,ny_c-1) = .FALSE.
vl(i-1,ny_c-1) = .FALSE.
END IF
IF(i==1.and.j==1)THEN
vl(nx_c-1,ny_c-1) = .FALSE.
END IF
END IF
END DO
END DO
!Care for periodicity:
do j=1,ny_c-1
u_c(nx_c,j,mod(n_c,2)) = u_c(1,j,mod(n_c,2))
v_c(nx_c,j,mod(n_c,2)) = v_c(1,j,mod(n_c,2))
h_c(nx_c,j,mod(n_c,2))= h_c(1,j,mod(n_c,2))
end do
do j=1,nx_c-1
u_c(j,ny_c,mod(n_c,2)) = u_c(j,1,mod(n_c,2))
v_c(j,ny_c,mod(n_c,2)) = v_c(j,1,mod(n_c,2))
h_c(j,ny_c,mod(n_c,2))=h_c(j,1,mod(n_c,2))
end do
u_c(nx_c,ny_c,mod(n_c,2))=u_c(1,1,mod(n_c,2))
v_c(nx_c,ny_c,mod(n_c,2))=v_c(1,1,mod(n_c,2))
h_c(nx_c,ny_c,mod(n_c,2))=h_c(1,1,mod(n_c,2))
!If hardware fault was detected, test whether the corresponding values on the model grid have resonable values:
DO i=1,nx_c-1
Do j=1,ny_c-1
IF(.not.hl(i,j))THEN
gc(1,1)=h_c(i,j,mod(n_c,2))
gc(2,1)=h_c(i+1,j,mod(n_c,2))
gc(1,2)=h_c(i,j+1,mod(n_c,2))
gc(2,2)=h_c(i+1,j+1,mod(n_c,2))
CALL map_fine_cell(gf,gc)
DO k=1,3
DO l=1,3
IF(abs(h_f((i-1)*3+k,(j-1)*3+l)).ge.8.0_8.or.h_f((i-1)*3+k,(j-1)*3+l).ne.h_f((i-1)*3+k,(j-1)*3+l)) h_f((i-1)*3+k,(j-1)*3+l) = gf(k,l) !Put in flow dependent limites for h here
END DO
END DO
END IF
IF(.not.ul(i,j))THEN
gc(1,1)=u_c(i,j,mod(n_c,2))
gc(2,1)=u_c(i+1,j,mod(n_c,2))
gc(1,2)=u_c(i,j+1,mod(n_c,2))
gc(2,2)=u_c(i+1,j+1,mod(n_c,2))
CALL map_fine_cell(gf,gc)
DO k=1,3
DO l=1,3
IF(u_f((i-1)*3+k-1,(j-1)*3+l).ne.u_f((i-1)*3+k-1,(j-1)*3+l).or.abs(u_f((i-1)*3+k-1,(j-1)*3+l)-10.0_8).ge.2.0_8) u_f((i-1)*3+k-1,(j-1)*3+l) = gf(k,l) !Put in flow dependent limites for u here
END DO
END DO
END IF
IF(.not.vl(i,j))THEN
gc(1,1)=v_c(i,j,mod(n_c,2))
gc(2,1)=v_c(i+1,j,mod(n_c,2))
gc(1,2)=v_c(i,j+1,mod(n_c,2))
gc(2,2)=v_c(i+1,j+1,mod(n_c,2))
CALL map_fine_cell(gf,gc)
DO k=1,3
DO l=1,3
IF(v_f((i-1)*3+k,(j-1)*3+l-1).ne.v_f((i-1)*3+k,(j-1)*3+l-1).or.abs(v_f((i-1)*3+k,(j-1)*3+l-1)).ge.1.0_8) v_f((i-1)*3+k,(j-1)*3+l-1) = gf(k,l) !Put in flow dependent limites for v here
END DO
END DO
END IF
END DO
END DO
!Take care for periodicity
DO j=1,ny_f-1
u_f(nx_f-1,j) = u_f(0,j)
END DO
DO i=1,nx_f-1
v_f(i,ny_f-1) = v_f(i,0)
END DO
!Change topography if the coarse value should be restored:
IF(ltopography)THEN
lerror = .FALSE.
LOOP1: DO i=1,nx_f-1
LOOP2: DO j=1,ny_f-1
IF(HC_f(i,j).ne.HC_f(i,j))lerror = .TRUE.
EXIT LOOP1
END DO LOOP2
END DO LOOP1
IF(lerror)THEN
CALL coarsetofine_h(nx_c,ny_c,HC_c,nx_f,ny_f,HC2_f)
DO i=1,nx_f-1
DO j=1,ny_f-1
IF(abs(HC_f(i,j)).gt.1000.0_8.or.HC_f(i,j).ne.HC_f(i,j))THEN
HC_f(i,j) = HC2_f(i,j)
END IF
END DO
END DO
END IF
END IF
return
end subroutine fault_secure
! --------------------------------------------------------------------------
subroutine timeloop(n,lboundary,lrestart,gp,slip,&
& nx,ny,nt,nuh,nuu,dt,&
& rdx,rdy,ab,fu,fv,taux,tauy,&
& h,dh,u,du,v,dv,ut,vt,ht,HC,nudge)
!Perform model timestep
USE rp_emulator
implicit none
INTEGER :: i,j,n,nx,ny,nt,k,l
logical :: lboundary, lrestart
REAL*8 :: gp,nuu,nuh,dt,rdx,rdy,slip
REAL*8 :: h(0:nx,0:ny),u(0:nx,0:ny),v(0:nx,0:ny),taux(0:ny),tauy(0:nx)
REAL*8 :: ht(0:nx,0:ny),ut(0:nx,0:ny),vt(0:nx,0:ny)
REAL*8 :: HC(0:nx,0:ny)
REAL*8 :: dh(0:nx,0:ny,nt),du(0:nx,0:ny,nt),dv(0:nx,0:ny,nt)
REAL*8 :: ab(nt)
REAL*8 :: fu(0:ny),fv(0:ny)
REAL*8 :: b(0:nx,0:ny),zeta(0:nx,0:ny)
REAL*8 :: nudge
REAL*8 :: td125,td25,td5,t1,t2
!Define constants are reals to make sure the emulator is working correctly
td125=0.125_8
td25 = 0.25_8
td5 = 0.5_8
t1 = 1.0_8
t2 = 2.0_8
!!!!!!!!!!!!!!!!!!!!!!!!!!!
!This is the grid:
!!!!!!!!!!!!!!!!!!!!!!!!!!!
!------------------------------------------------------------...
! h(1,ny-1) u(2,ny-1) h(2,ny-1) u(3,ny-1) h(3,ny-1) u(4,ny-1) ... u(nx-1,ny-1) h(nx-1,ny-1)
!
!--v(1,ny-1)---------------v(1,ny-1)---------------v(1,ny-1)-... v(nx-1,ny-1)
!
! h(1,ny-2) u(2,ny-2) h(2,ny-2) u(3,ny-2) h(3,ny-2) u(4,ny-2) ... u(nx-1,ny-2) h(nx-1,ny-2)
!.
!.
!.
!----------------------------------------------...
!
! h(1,2) u(2,2) h(2,2) u(3,2) h(3,2) u(4,2) ... u(nx-1,2) h(nx-1,2)
!
!--v(1,2))-----------v(2,2)------------v(3,2)--... v(nx-1,2)
!
! h(1,1) u(2,1) h(2,1) u(3,1) h(3,1) u(4,1) ... u(nx-1,1) h(nx-1,1)
!------------------------------------------------------
!Set condition for periodicity:
IF(.not.lboundary)THEN
do j=1,ny-1
u(0,j) = u(nx-1,j)
u(nx,j) = u(1,j)
v(0,j) = v(nx-1,j)
v(nx,j) = v(1,j)
h(0,j)=h(nx-1,j)
h(nx,j)=h(1,j)
end do
do j=1,nx-1
u(j,0) = u(j,ny-1)
u(j,ny) = u(j,1)
v(j,0) = v(j,ny-1)
v(j,ny) = v(j,1)
h(j,0)=h(j,ny-1)
h(j,ny)=h(j,1)
end do
u(0,0)=u(nx-1,ny-1)
v(0,0)=v(nx-1,ny-1)
h(0,0)=h(nx-1,ny-1)
u(nx,ny)=u(1,1)
v(nx,ny)=v(1,1)
h(nx,ny)=h(1,1)
u(0,ny)=u(nx-1,1)
v(0,ny)=v(nx-1,1)
h(0,ny)=h(nx-1,1)
u(nx,0)=u(1,ny-1)
v(nx,0)=v(1,ny-1)
h(nx,0)=h(1,ny-1)
END IF
! calculate Bernoulli potential
! 1/g*h+0.5(u^2+v^2)
do j=0,ny-1
do i=0,nx-1
b(i,j)=gp*h(i,j)+td125*&
& ((u(i,j)+u(i+1,j))**t2+(v(i,j)+v(i,j+1))**t2)
end do
end do
! calculate relative vorticity
! d_f v - d_y u
do j=1,ny
do i=1,nx
zeta(i,j)=(v(i,j)-v(i-1,j))*rdx-(u(i,j)-u(i,j-1))*rdy
end do
end do
! calculate forcing of u,v, and h
do j=1,ny-1
do i=2,nx-1
du(i,j,3)=du(i,j,2) !For Adams Bashforth
du(i,j,2)=du(i,j,1) !For Adams Bashforth
du(i,j,1)= nuu*(u(i+1,j)+u(i-1,j)-t2*u(i,j))*rdx**t2+nuu*(u(i,j+1)+u(i,j-1)-t2*u(i,j))*rdy**t2 & !\nu (dx dx u + dy dy u)
& +td25*(fu(j)+td5*(zeta(i,j)+zeta(i,j+1)))*(v(i-1,j)+v(i,j)+v(i-1,j+1)+v(i,j+1)) & ! +(f+zeta)v
& -(b(i,j)-b(i-1,j))*rdx + taux(j) + nudge*(ut(i,j)-u(i,j)) !-dx b = dx (g*h+0.5(u^2+v^2))
end do
end do
do j=2,ny-1
do i=1,nx-1
dv(i,j,3)=dv(i,j,2) !For Adams Bashforth
dv(i,j,2)=dv(i,j,1) !For Adams Bashforth
dv(i,j,1)= nuu*(v(i+1,j)+v(i-1,j)-t2*v(i,j))*rdx**t2+nuu*(v(i,j+1)+v(i,j-1)-t2*v(i,j))*rdy**t2 & !\nu (dx dx v + dy dy v)
& -td25*(fv(j)+td5*(zeta(i,j)+zeta(i+1,j)))*(u(i,j-1)+u(i,j)+u(i+1,j-1)+u(i+1,j)) & ! -(f+zeta)u
& - (b(i,j)-b(i,j-1))*rdy + tauy(i) + nudge*(vt(i,j)-v(i,j)) !-dx b = dx (g*h+0.5(u^2+v^2))
end do
end do
IF(.not.lboundary)THEN
i=1
do j=1,ny-1
du(i,j,3)=du(i,j,2) !For Adams Bashforth
du(i,j,2)=du(i,j,1) !For Adams Bashforth
du(i,j,1)=nuu*(u(i+1,j)+u(i-1,j)-t2*u(i,j))*rdx**t2+nuu*(u(i,j+1)+u(i,j-1)-t2*u(i,j))*rdy**t2 & !\nu (dx dx u + dy dy u)
& +td25*(fu(j)+td5*(zeta(i,j)+zeta(i,j+1)))*(v(i-1,j)+v(i,j)+v(i-1,j+1)+v(i,j+1)) & ! +(f+zeta)v
& -(b(i,j)-b(i-1,j))*rdx + taux(j) + nudge*(ut(i,j)-u(i,j)) !-dx b = dx (g*h+0.5(u^2+v^2))
end do
j=1
do i=1,nx-1
dv(i,j,3)=dv(i,j,2) !For Adams Bashforth
dv(i,j,2)=dv(i,j,1) !For Adams Bashforth
dv(i,j,1)=nuu*(v(i+1,j)+v(i-1,j)-t2*v(i,j))*rdx**t2+nuu*(v(i,j+1)+v(i,j-1)-t2*v(i,j))*rdy**t2 & !\nu (dx dx v + dy dy v)
& -td25*(fv(j)+td5*(zeta(i,j)+zeta(i+1,j)))*(u(i,j-1)+u(i,j)+u(i+1,j-1)+u(i+1,j)) & ! -(f+zeta)u
& - (b(i,j)-b(i,j-1))*rdy + tauy(i) + nudge*(vt(i,j)-v(i,j)) !-dx b = dx (g*h+0.5(u^2+v^2))
end do
END IF
do j=1,ny-1
do i=1,nx-1
dh(i,j,3)=dh(i,j,2)
dh(i,j,2)=dh(i,j,1)
dh(i,j,1)= & !nuh*(h(i+1,j)+h(i-1,j)-2.*h(i,j))/dx**2 +nuh*(h(i,j+1)+h(i,j-1)-2.*h(i,j))/dy**2 & !\nu_h (dx dx h + dy dy h)
& +(td5*(HC(i-1,j)+h(i-1,j)+HC(i,j)+h(i,j)))*u(i,j)*rdx &
& +(td5*(HC(i,j-1)+h(i,j-1)+HC(i,j)+h(i,j)))*v(i,j)*rdy &
& -(td5*(HC(i+1,j)+h(i+1,j)+HC(i,j)+h(i,j)))*u(i+1,j)*rdx &
& -(td5*(HC(i,j+1)+h(i,j+1)+HC(i,j)+h(i,j)))*v(i,j+1)*rdy + nudge*(ht(i,j)-h(i,j)) + nudge*(ht(i,j)-h(i,j))
end do
end do
! step forward for u,v, and h
do j=1,ny-1
do i=2,nx-1
IF(n.lt.3.and.(.not.lrestart))THEN
u(i,j)=u(i,j)+du(i,j,1)*dt
ELSE
u(i,j)=u(i,j)+ab(1)*du(i,j,1)+ab(2)*du(i,j,2)+ab(3)*du(i,j,3)
END IF
end do
end do
do j=2,ny-1
do i=1,nx-1
IF(n.lt.3.and.(.not.lrestart))THEN
v(i,j)=v(i,j)+dv(i,j,1)*dt
ELSE
v(i,j)=v(i,j)+ab(1)*dv(i,j,1)+ab(2)*dv(i,j,2)+ab(3)*dv(i,j,3)
END IF
end do
end do
IF(.not.lboundary)THEN
i=1
do j=1,ny-1
IF(n.lt.3.and.(.not.lrestart))THEN
u(i,j)=u(i,j)+du(i,j,1)*dt
ELSE
u(i,j)=u(i,j)+ab(1)*du(i,j,1)+ab(2)*du(i,j,2)+ab(3)*du(i,j,3)
END IF
end do
j=1
do i=1,nx-1
IF(n.lt.3.and.(.not.lrestart))THEN
v(i,j)=v(i,j)+dv(i,j,1)*dt
ELSE
v(i,j)=v(i,j)+ab(1)*dv(i,j,1)+ab(2)*dv(i,j,2)+ab(3)*dv(i,j,3)
END IF
end do
END IF
do j=1,ny-1
do i=1,nx-1
IF(n.lt.3.and.(.not.lrestart))THEN
h(i,j)=h(i,j)+dh(i,j,1)*dt
ELSE
h(i,j)=h(i,j)+ab(1)*dh(i,j,1)+ab(2)*dh(i,j,2)+ab(3)*dh(i,j,3)
END IF
end do
end do
! evaluate dummy array elements from boundary conditions
IF(lboundary)THEN
do j=1,ny-1
v(0,j)=(t1-t2*slip)*v(1,j)
v(nx,j)=(t1-t2*slip)*v(nx-1,j)
h(0,j)=h(1,j)
h(nx,j)=h(nx-1,j)
end do
do i=1,nx-1
u(i,0)=(t1-t2*slip)*u(i,1)
u(i,ny)=(t1-t2*slip)*u(i,ny-1)
h(i,0)=h(i,1)
h(i,ny)=h(i,ny-1)
end do
END IF
!Set condition for periodicity:
IF(.not.lboundary)THEN
do j=1,ny-1
u(0,j) = u(nx-1,j)
u(nx,j) = u(1,j)
v(0,j) = v(nx-1,j)
v(nx,j) = v(1,j)
h(0,j)=h(nx-1,j)
h(nx,j)=h(1,j)
end do
do j=1,nx-1
u(j,0) = u(j,ny-1)
u(j,ny) = u(j,1)
v(j,0) = v(j,ny-1)
v(j,ny) = v(j,1)
h(j,0)=h(j,ny-1)
h(j,ny)=h(j,1)
end do
u(0,0)=u(nx-1,ny-1)
v(0,0)=v(nx-1,ny-1)
h(0,0)=h(nx-1,ny-1)
u(nx,ny)=u(1,1)
v(nx,ny)=v(1,1)
h(nx,ny)=h(1,1)
u(0,ny)=u(nx-1,1)
v(0,ny)=v(nx-1,1)
h(0,ny)=h(nx-1,1)
u(nx,0)=u(1,ny-1)
v(nx,0)=v(1,ny-1)
h(nx,0)=h(1,ny-1)
END IF
end subroutine timeloop
subroutine finetocoarse(nx_c,ny_c,h_c,u_c,v_c,&
&nx_f,ny_f,h_f,u_f,v_f)
!Perform the mapping between the coarse and the fine grid
USE rp_emulator
implicit none
INTEGER :: nx_c, ny_c
REAL*8 :: u_c(0:nx_c,0:ny_c), v_c(0:nx_c,0:ny_c), h_c(0:nx_c,0:ny_c)
INTEGER :: nx_f, ny_f
REAL*8 :: u_f(0:nx_f,0:ny_f), v_f(0:nx_f,0:ny_f), h_f(0:nx_f,0:ny_f)
INTEGER :: i,j,k,l
REAL*8 :: t1,t6,t8,t9,t12
t1 = 1.0_8
t6 = 6.0_8
t8 = 8.0_8
t9 = 9.0_8
t12 = 12.0_8
!Height:
DO j=1,ny_c-1
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
END DO
DO j=2,ny_c-1
DO i=1,nx_c-1
v_c(i,j) = t1/t6*v_f((i-1)*3+1,(j-1)*3 )&
& + t1/t8*(v_f((i-1)*3+1,(j-1)*3-1)+v_f((i-1)*3 ,(j-1)*3 )+v_f((i-1)*3+2,(j-1)*3 )+v_f((i-1)*3+1,(j-1)*3+1))&
& +t1/t12*(v_f((i-1)*3 ,(j-1)*3-1)+v_f((i-1)*3+2,(j-1)*3-1)+v_f((i-1)*3 ,(j-1)*3+1)+v_f((i-1)*3+2,(j-1)*3+1))
END DO
END DO
return
end subroutine finetocoarse
subroutine coarsetofine(nx_c,ny_c,h_c,u_c,v_c,&
&nx_f,ny_f,h_f,u_f,v_f)
!Mapping between the coarse and the fine grid:
USE rp_emulator
implicit none
INTEGER :: nx_c, ny_c
REAL*8 :: u_c(0:nx_c,0:ny_c), v_c(0:nx_c,0:ny_c), h_c(0:nx_c,0:ny_c)
INTEGER :: nx_f, ny_f
REAL*8 :: u_f(0:nx_f,0:ny_f), v_f(0:nx_f,0:ny_f), h_f(0:nx_f,0:ny_f)
INTEGER :: i,j,k,l
REAL*8 :: gf(3,3),gc(2,2),tc,uc
!Height:
DO j=1,ny_c-2
DO i=1,nx_c-2
gc(1,1)=h_c(i,j)
gc(2,1)=h_c(i+1,j)
gc(1,2)=h_c(i,j+1)
gc(2,2)=h_c(i+1,j+1)
CALL map_fine_cell(gf,gc)
DO k=1,3
DO l=1,3
h_f((i-1)*3+1,(j-1)*3+1) = gf(1,1)
h_f((i-1)*3+2,(j-1)*3+1) = gf(2,1)
h_f((i-1)*3+3,(j-1)*3+1) = gf(3,1)
h_f((i-1)*3+1,(j-1)*3+2) = gf(1,2)
h_f((i-1)*3+2,(j-1)*3+2) = gf(2,2)
h_f((i-1)*3+3,(j-1)*3+2) = gf(3,2)
h_f((i-1)*3+1,(j-1)*3+3) = gf(1,3)
h_f((i-1)*3+2,(j-1)*3+3) = gf(2,3)
h_f((i-1)*3+3,(j-1)*3+3) = gf(3,3)
END DO
END DO
END DO
END DO
DO i=1,nx_c-2
gc(1,1)=h_c(i,ny_c-1)
gc(2,1)=h_c(i+1,ny_c-1)
gc(1,2)=h_c(i,1)
gc(2,2)=h_c(i+1,1)
CALL map_fine_cell(gf,gc)
DO k=1,3
DO l=1,3
h_f((i-1)*3+1,(ny_f-4)+1) = gf(1,1)
h_f((i-1)*3+2,(ny_f-4)+1) = gf(2,1)
h_f((i-1)*3+3,(ny_f-4)+1) = gf(3,1)
h_f((i-1)*3+1,(ny_f-4)+2) = gf(1,2)
h_f((i-1)*3+2,(ny_f-4)+2) = gf(2,2)
h_f((i-1)*3+3,(ny_f-4)+2) = gf(3,2)
h_f((i-1)*3+1,(ny_f-4)+3) = gf(1,3)
h_f((i-1)*3+2,(ny_f-4)+3) = gf(2,3)
h_f((i-1)*3+3,(ny_f-4)+3) = gf(3,3)
END DO
END DO
END DO
DO j=1,ny_c-2
gc(1,1)=h_c(nx_c-1,j)
gc(2,1)=h_c(1,j)
gc(1,2)=h_c(nx_c-1,j+1)
gc(2,2)=h_c(1,j+1)
CALL map_fine_cell(gf,gc)
DO k=1,3
DO l=1,3
h_f(nx_f-4+1,(j-1)*3+1) = gf(1,1)
h_f(nx_f-4+2,(j-1)*3+1) = gf(2,1)
h_f(nx_f-4+3,(j-1)*3+1) = gf(3,1)
h_f(nx_f-4+1,(j-1)*3+2) = gf(1,2)
h_f(nx_f-4+2,(j-1)*3+2) = gf(2,2)
h_f(nx_f-4+3,(j-1)*3+2) = gf(3,2)
h_f(nx_f-4+1,(j-1)*3+3) = gf(1,3)
h_f(nx_f-4+2,(j-1)*3+3) = gf(2,3)
h_f(nx_f-4+3,(j-1)*3+3) = gf(3,3)
END DO
END DO
END DO
gc(1,1)=h_c(nx_c-1,ny_c-1)
gc(2,1)=h_c(1,ny_c-1)
gc(1,2)=h_c(nx_c-1,1)
gc(2,2)=h_c(1,1)
CALL map_fine_cell(gf,gc)
DO k=1,3
DO l=1,3
h_f(nx_f-4+1,ny_f-4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O
END DO
DO j=1,ny_f-1
u_f(nx_f-1,j) = u_f(0,j)
END DO
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!Vertical velocity:
DO j=1,ny_c-2
DO i=1,nx_c-2
gc(1,1)=v_c(i,j)
gc(2,1)=v_c(i+1,j)
gc(1,2)=v_c(i,j+1)
gc(2,2)=v_c(i+1,j+1)
CALL map_fine_cell(gf,gc)
DO k=1,3
DO l=1,3
v_f((i-1)*3+1,(j-1)*3 ) = gf(1,1)
v_f((i-1)*3+2,(j-1)*3 ) = gf(2,1)
v_f((i-1)*3+3,(j-1)*3 ) = gf(3,1)
v_f((i-1)*3+1,(j-1)*3+1) = gf(1,2)
v_f((i-1)*3+2,(j-1)*3+1) = gf(2,2)
v_f((i-1)*3+3,(j-1)*3+1) = gf(3,2)
v_f((i-1)*3+1,(j-1)*3+2) = gf(1,3)
v_f((i-1)*3+2,(j-1)*3+2) = gf(2,3)
v_f((i-1)*3+3,(j-1)*3+2) = gf(3,3)
END DO
END DO
END DO
END DO
DO i=1,nx_c-2
gc(1,1)=v_c(i,ny_c-1)
gc(2,1)=v_c(i+1,ny_c-1)
gc(1,2)=v_c(i,1)
gc(2,2)=v_c(i+1,1)
CALL map_fine_cell(gf,gc)
DO k=1,3
DO l=1,3
v_f((i-1)*3+1,(ny_f-4) ) = gf(1,1)
v_f((i-1)*3+2,(ny_f-4) ) = gf(2,1)
v_f((i-1)*3+3,(ny_f-4) ) = gf(3,1)
v_f((i-1)*3+1,(ny_f-4)+1) = gf(1,2)
v_f((i-1)*3+2,(ny_f-4)+1) = gf(2,2)
v_f((i-1)*3+3,(ny_f-4)+1) = gf(3,2)
v_f((i-1)*3+1,(ny_f-4)+2) = gf(1,3)
v_f((i-1)*3+2,(ny_f-4)+2) = gf(2,3)
v_f((i-1)*3+3,(ny_f-4)+2) = gf(3,3)
END DO
END DO
END DO
DO j=1,ny_c-2
gc(1,1)=v_c(nx_c-1,j)
gc(2,1)=v_c(1,j)
gc(1,2)=v_c(nx_c-1,j+1)
gc(2,2)=v_c(1,j+1)
CALL map_fine_cell(gf,gc)
DO k=1,3
DO l=1,3
v_f(nx_f-4+1,(j-1)*3 ) = gf(1,1)
v_f(nx_f-4+2,(j-1)*3 ) = gf(2,1)
v_f(nx_f-4+3,(j-1)*3 ) = gf(3,1)
v_f(nx_f-4+1,(j-1)*3+1) = gf(1,2)
v_f(nx_f-4+2,(j-1)*3+1) = gf(2,2)
v_f(nx_f-4+3,(j-1)*3+1) = gf(3,2)
v_f(nx_f-4+1,(j-1)*3+2) = gf(1,3)
v_f(nx_f-4+2,(j-1)*3+2) = gf(2,3)
v_f(nx_f-4+3,(j-1)*3+2) = gf(3,3)
END DO
END DO
END DO
gc(1,1)=v_c(nx_c-1,ny_c-1)
gc(2,1)=v_c(1,ny_c-1)
gc(1,2)=v_c(nx_c-1,1)
gc(2,2)=v_c(1,1)
CALL map_fine_cell(gf,gc)
DO k=1,3
DO l=1,3
v_f(nx_f-4+1,ny_f-4 ) = gf(1,1)
v_f(nx_f-4+2,ny_f-4 ) = gf(2,1)
v_f(nx_f-4+3,ny_f-4 ) = gf(3,1)
v_f(nx_f-4+1,ny_f-4+1) = gf(1,2)
v_f(nx_f-4+2,ny_f-4+1) = gf(2,2)
v_f(nx_f-4+3,ny_f-4+1) = gf(3,2)
v_f(nx_f-4+1,ny_f-4+2) = gf(1,3)
v_f(nx_f-4+2,ny_f-4+2) = gf(2,3)
v_f(nx_f-4+3,ny_f-4+2) = gf(3,3)
END DO
END DO
DO i=1,nx_f-1
v_f(i,ny_f-1) = v_f(i,0)
END DO
return
end subroutine coarsetofine
subroutine map_fine_cell(gf,gc)
!Map individual cell
USE rp_emulator
implicit none
REAL*8 :: gf(3,3),gc(2,2),tc,uc
REAL*8 :: t1,t2,t3
t1 = 1.0_8
t2 = 2.0_8
t3 = 3.0_8
!!!!!!!!!!!!!!!!!!!!!!!!!!!
!This is the grid:
!!!!!!!!!!!!!!!!!!!!!!!!!!!
!--------------------------------------------------!
! gc(1,2) gc(2,2)
!
! gf(1,3) gf(2,3) gf(3,3)
!
! gf(1,2) gf(2,2) gf(3,2)
!
! gc(1,1)/gf(1,1) gf(2,1) gf(3,1) gc(2,1)
!--------------------------------------------------!
!See numerical recipes 3.6 p. 123
gf(1,1)=gc(1,1)