forked from HYCOM/HYCOM-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thermf.F90
2270 lines (2256 loc) · 88.1 KB
/
thermf.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
#if defined(ROW_LAND)
#define SEA_P .true.
#define SEA_U .true.
#define SEA_V .true.
#elif defined(ROW_ALLSEA)
#define SEA_P allip(j).or.ip(i,j).ne.0
#define SEA_U alliu(j).or.iu(i,j).ne.0
#define SEA_V alliv(j).or.iv(i,j).ne.0
#else
#define SEA_P ip(i,j).ne.0
#define SEA_U iu(i,j).ne.0
#define SEA_V iv(i,j).ne.0
#endif
subroutine thermf_oi(m,n)
use mod_xc ! HYCOM communication interface
use mod_cb_arrays ! HYCOM saved arrays
implicit none
!
integer m,n
!
! --- ----------------------------------------------------------
! --- thermal forcing - combine ocean and sea ice surface fluxes
! --- - complete surface salinity forcing
! --- - allow for ice shelves (no surface flux)
! --- ----------------------------------------------------------
!
logical, parameter :: ldebug_empbal=.false. !usually .false.
!
integer i,j,k
real emnp,dpemnp,dplay1,onetanew,onetaold, &
pbanew,pbaold,q,salt1n,salt1o
real*8 d1,d2,d3,d4,ssum(2),s1(2)
!
if (ishelf.ne.0) then !sea ice and an ice shelf
!$OMP PARALLEL DO PRIVATE(j,i)
do j=1,jj
do i=1,ii
if (SEA_P) then
if (ishlf(i,j).eq.1) then !standard ocean point
if ( cpl_swflx .and. cpl_lwmdnflx .and. cpl_lwmupflx &
.and. cpl_precip ) then
sstflx(i,j) = (1.0-covice(i,j))*sstflx(i,j) !relax over ocean
surflx(i,j) = surflx(i,j) + flxice(i,j) !ocn/ice frac. in coupler
salflx(i,j) = sflice(i,j) + & !ocn/ice frac. in coupler
sssflx(i,j) !relax evrywhere
wtrflx(i,j) = wtrflx(i,j) + wflice(i,j) + & !ocn/ice frac. in coupler
rivflx(i,j) !rivers evrywhere
else
sswflx(i,j) = (1.0-covice(i,j))*sswflx(i,j) + & !ocean
fswice(i,j) !ice cell average
surflx(i,j) = (1.0-covice(i,j))*surflx(i,j) + & !ocean
flxice(i,j) !ice cell average
sstflx(i,j) = (1.0-covice(i,j))*sstflx(i,j) !relax over ocean
salflx(i,j) = sflice(i,j) + & !ice cell average
sssflx(i,j) !relax everywhere
wtrflx(i,j) = (1.0-covice(i,j))*wtrflx(i,j) + & !ocean
wflice(i,j) + & !ice cell average
rivflx(i,j) !rivers everywhere
endif ! .not.cpl
else !under an ice shelf
sswflx(i,j) = 0.0
surflx(i,j) = 0.0
sstflx(i,j) = 0.0
salflx(i,j) = 0.0
wtrflx(i,j) = 0.0
endif !ishlf
util1(i,j) = surflx(i,j)*scp2(i,j)
util2(i,j) = wtrflx(i,j)*scp2(i,j)
endif !ip
enddo !i
enddo !j
!$OMP END PARALLEL DO
elseif (iceflg.ne.0) then
if ( cpl_swflx .and. cpl_lwmdnflx .and. cpl_lwmupflx &
.and. cpl_precip ) then
! --- allow for sea ice
!$OMP PARALLEL DO PRIVATE(j,i)
do j=1,jj
do i=1,ii
if (SEA_P) then
sstflx(i,j) = (1.0-covice(i,j))*sstflx(i,j) !relax over ocean
surflx(i,j) = surflx(i,j) + flxice(i,j) !ocn/ice frac. in coupler
salflx(i,j) = sflice(i,j) + & !ocn/ice frac. in coupler
sssflx(i,j) !relax evrywhere
wtrflx(i,j) = wtrflx(i,j) + wflice(i,j) + & !ocn/ice frac. in coupler
rivflx(i,j) !rivers evrywhere
util1(i,j) = surflx(i,j)*scp2(i,j)
util2(i,j) = wtrflx(i,j)*scp2(i,j)
endif
enddo !i
enddo
!$OMP END PARALLEL DO
else ! cpl_
!$OMP PARALLEL DO PRIVATE(j,i)
do j=1,jj
do i=1,ii
if (SEA_P) then
sswflx(i,j) = (1.0-covice(i,j))*sswflx(i,j) + &
fswice(i,j) !ice cell average
surflx(i,j) = (1.0-covice(i,j))*surflx(i,j) + &
flxice(i,j) !ice cell average
sstflx(i,j) = (1.0-covice(i,j))*sstflx(i,j) !relax over ocean
salflx(i,j) = sflice(i,j) + & !ice cell average
sssflx(i,j) !relax everywhere
wtrflx(i,j) = (1.0-covice(i,j))*wtrflx(i,j) + &
wflice(i,j) + & !ice cell average
rivflx(i,j) !rivers everywhere
util1(i,j) = surflx(i,j)*scp2(i,j)
util2(i,j) = wtrflx(i,j)*scp2(i,j)
endif
enddo !i
enddo !j
!$OMP END PARALLEL DO
endif ! .not. cpl
else !no sea ice
!$OMP PARALLEL DO PRIVATE(j,i)
do j=1,jj
do i=1,ii
if (SEA_P) then !sswflx, surflx and sstflx unchanged
salflx(i,j) = sssflx(i,j)
wtrflx(i,j) = wtrflx(i,j) + rivflx(i,j)
util1(i,j) = surflx(i,j)*scp2(i,j)
util2(i,j) = wtrflx(i,j)*scp2(i,j)
endif !ip
enddo !i
enddo !j
!$OMP END PARALLEL DO
endif !ishlf:iceflg
!
if (epmass .and. empbal.eq.1) then
!
! --- balance wtrflx to emptgt, via a region-wide offset:
!
call xcsum(d2, util2,ipa) !total wtrflx
d2 = d2/area !basin-wide average
if (d2.ne.emptgt) then !not balanced at emptgt
q = emptgt - d2
!$OMP PARALLEL DO PRIVATE(j,i) &
!$OMP SCHEDULE(STATIC,jblk)
do j=1,jj
do i=1,ii
if (SEA_P) then
wtrflx(i,j) = wtrflx(i,j) + q
util2(i,j) = wtrflx(i,j)*scp2(i,j)
endif
enddo !i
enddo !j
if (ldebug_empbal .and. &
mnproc.eq.1 .and. mod(nstep,100).le.1) then
write (lp,'(i9,a,1pe12.5)') &
nstep,' offset wtrflx by ',q
endif !master
endif !not already balanced
elseif (.not.epmass .and. empbal.eq.1) then
!
! --- balance wtrflx to emptgt, via a region-wide offset:
! --- virtual salt flux case, balance sss*wtrflx
!
do j=1,jj
do i=1,ii
if (SEA_P) then
util3(i,j) = wtrflx(i,j)*saln(i,j,1,n)*scp2(i,j)
util4(i,j) = saln(i,j,1,n)*scp2(i,j)
endif
enddo !i
enddo !j
call xcsum(d3, util3,ipa) !total sss*wtrflx
call xcsum(d4, util4,ipa) !total sss
d2 = d3/d4 !basin-wide weighted average
if (d2.ne.emptgt) then !not balanced at emptgt
q = emptgt - d2
!$OMP PARALLEL DO PRIVATE(j,i) &
!$OMP SCHEDULE(STATIC,jblk)
do j=1,jj
do i=1,ii
if (SEA_P) then
wtrflx(i,j) = wtrflx(i,j) + q
util2(i,j) = wtrflx(i,j)*scp2(i,j)
endif
enddo !i
enddo !j
if (ldebug_empbal .and. &
mnproc.eq.1 .and. mod(nstep,100).le.1) then
write (lp,'(i9,a,1pe12.5)') &
nstep,' offset wtrflx by ',q
endif !master
endif !not already balanced
elseif (epmass .and. empbal.eq.2) then
!
! --- balance wtrflx to emptgt, by scaling down +ve or -ve anomalies
!
call xcsum(d2, util2,ipa) !total
d2 = d2/area !basin-wide average
if (d2.ne.emptgt) then !not balanced at emptgt
do j=1,jj
do i=1,ii
if (SEA_P) then
util3(i,j) = max(wtrflx(i,j)-emptgt,0.0)*scp2(i,j)
endif
enddo !i
enddo !j
call xcsum(d3, util3,ipa) !positive anomaly only, >emptgt
d3 = d3/area !basin-wide average positive anomaly
d1 = d2-emptgt - d3 !basin-wide average negative anomaly
if (-d1.gt.d3) then
! --- scale down the negative values
q = -d3/d1 !<1
!$OMP PARALLEL DO PRIVATE(j,i) &
!$OMP SCHEDULE(STATIC,jblk)
do j=1,jj
do i=1,ii
if (SEA_P) then
if (wtrflx(i,j).lt.emptgt) then
wtrflx(i,j) = emptgt + q*(wtrflx(i,j)-emptgt)
util2(i,j) = wtrflx(i,j)*scp2(i,j)
endif
endif
enddo !i
enddo !j
if (ldebug_empbal .and. &
mnproc.eq.1 .and. mod(nstep,100).le.1) then
write (lp,'(i9,a,f8.5)') &
nstep,' scale -ve wtrflx anomaly by ',q
endif !master
else !-d1.lt.d3
! --- scale down the positive values
q = -d1/d3 !<1
!$OMP PARALLEL DO PRIVATE(j,i) &
!$OMP SCHEDULE(STATIC,jblk)
do j=1,jj
do i=1,ii
if (SEA_P) then
if (wtrflx(i,j).gt.emptgt) then
wtrflx(i,j) = emptgt + q*(wtrflx(i,j)-emptgt)
util2(i,j) = wtrflx(i,j)*scp2(i,j)
endif
endif
enddo !i
enddo !j
if (ldebug_empbal .and. &
mnproc.eq.1 .and. mod(nstep,100).le.1) then
write (lp,'(i9,a,f8.5)') &
nstep,' scale +ve wtrflx anomaly by ',q
endif !master
endif !reduce -ve:reduce +ve
endif !not already balanced
elseif (.not.epmass .and. empbal.eq.2) then
!
! --- balance wtrflx to emptgt, by scaling down +ve or -ve anomalies
! --- virtual salt flux case, balance sss*wtrflx
!
do j=1,jj
do i=1,ii
if (SEA_P) then
util2(i,j) = wtrflx(i,j)*saln(i,j,1,n)*scp2(i,j)
util3(i,j) = max(wtrflx(i,j)-emptgt,0.0)* &
saln(i,j,1,n)*scp2(i,j)
util4(i,j) = saln(i,j,1,n)*scp2(i,j)
endif
enddo !i
enddo !j
call xcsum(d2, util2,ipa) !total sss*wtrflx
call xcsum(d3, util3,ipa) !anom. sss*wtrflx, wtrflx >emptgt
call xcsum(d4, util4,ipa) !total sss
d2 = d2/d4 !basin-wide weighted average
d3 = d3/d4 !basin-wide weighted average positive anomaly
d1 = d2-emptgt - d3 !basin-wide weighted average negative anomaly
if (-d1.gt.d3) then
! --- scale down the negative values
q = -d3/d1 !<1
!$OMP PARALLEL DO PRIVATE(j,i) &
!$OMP SCHEDULE(STATIC,jblk)
do j=1,jj
do i=1,ii
if (SEA_P) then
if (wtrflx(i,j).lt.emptgt) then
wtrflx(i,j) = emptgt + q*(wtrflx(i,j)-emptgt)
endif
util2(i,j) = wtrflx(i,j)*scp2(i,j)
endif
enddo !i
enddo !j
if (ldebug_empbal .and. &
mnproc.eq.1 .and. mod(nstep,100).le.1) then
write (lp,'(i9,a,f8.5)') &
nstep,' scale -ve wtrflx anomaly by ',q
endif !master
else !-d1.lt.d3
! --- scale down the positive values
q = -d1/d3 !<1
!$OMP PARALLEL DO PRIVATE(j,i) &
!$OMP SCHEDULE(STATIC,jblk)
do j=1,jj
do i=1,ii
if (SEA_P) then
if (wtrflx(i,j).gt.emptgt) then
wtrflx(i,j) = emptgt + q*(wtrflx(i,j)-emptgt)
endif
util2(i,j) = wtrflx(i,j)*scp2(i,j)
endif
enddo !i
enddo !j
if (ldebug_empbal .and. &
mnproc.eq.1 .and. mod(nstep,100).le.1) then
write (lp,'(i9,a,f8.5)') &
nstep,' scale +ve wtrflx anomaly by ',q
endif !master
endif !reduce -ve:reduce +ve
endif !empbal
!!Alex New calculation of epmass, with E-P applied to the top layer
!!AJW Modified new epmass calculation to use actual dp rather than h
!!AJW updates pbavg, dp and S.1
!
if (epmass) then !requires btrlfr=.true., see blkdat.F
!$OMP PARALLEL DO PRIVATE(j,i,k,emnp,dpemnp,dplay1,onetanew, &
!$OMP onetaold,pbanew,pbaold,q)
do j=1,jj
do i=1,ii
if (SEA_P) then
! --- "dp" is dp', use actual dp = dp'/oneta, this is exact for
! --- btrmas=true and approximately correct for btrmas=false
! --- This only works if pbavg and dp have just been integrated from
! --- the same time, hence btrlfr=false is not allowed
!
emnp = -wtrflx(i,j)*svref !m/s
pbaold = pbavg(i,j,n)
pbanew = pbavg(i,j,n) - delt1*emnp*onem !emnp mass = rho0*vol
pbanew = max(pbanew, -pbot(i,j))
onetaold = max( oneta0, 1.0 + pbaold/pbot(i,j) )
onetanew = max( oneta0, 1.0 + pbanew/pbot(i,j) )
!
! if (i.eq.itest.and.j.eq.jtest) then
! s1(1) = dp(i,j,1,n)*onetaold*saln(i,j,1,n)
! ssum(1) = 0.0d0
! do k= 1,kk
! ssum(1) = ssum(1) + dp(i,j,k,n)*onetaold*saln(i,j,k,n)
! enddo !k
! endif !test
!
pbavg(i,j,n) = pbanew
oneta(i,j,n) = max(oneta0, 1.0 + pbavg(i,j,n)/pbot(i,j))
if (delt1.ne.baclin) then
! --- Robert-Asselin time filter correction
pbavg(i,j,m) = pbavg(i,j,m)+0.5*ra2fac*(pbanew-pbaold)
oneta(i,j,m) = max(oneta0,1.0 + pbavg(i,j,m)/pbot(i,j))
endif
! --- treat E-P as a layer above layer 1, merged into layer 1
! --- E-P salinity is 0 psu, E-P temperature is SST (T.1 is unchanged)
q = onetaold/onetanew
dplay1 = dp(i,j,1,n)*q
dpemnp = (pbanew - pbaold)/onetanew
salt1o = saln(i,j,1,n)*onetaold*dp(i,j,1,n)*qonem !diagnostic
saln(i,j,1,n) = saln(i,j,1,n) + &
(0.0-saln(i,j,1,n))* &
dpemnp/(dplay1 + dpemnp)
dp(i,j,1,n) = dplay1 + dpemnp
p(i,j,2 ) = dp(i,j,1,n)
salt1n = saln(i,j,1,n)*onetanew*dp(i,j,1,n)*qonem !diagnostic
do k= 2,kk
dp(i,j,k,n) = dp(i,j,k,n)*q
p(i,j,k+1) = dp(i,j,k,n) + p(i,j,k)
enddo !k
!
! if (i.eq.itest.and.j.eq.jtest) then
! s1(2) = dp(i,j,1,n)*onetanew*saln(i,j,1,n)
! ssum(2) = 0.0d0
! do k= 1,kk
! ssum(2) = ssum(2) + dp(i,j,k,n)*onetanew*saln(i,j,k,n)
! enddo !k
! --- normalize by onem, layer 1 is often 1 m thick
! s1(1) = s1(1)/onem
! s1(2) = s1(2)/onem
! write(lp,'(a,i9,1p3e16.8)')
! & 'epmass1:',nstep,s1(1),s1(2),s1(2)-s1(1)
! --- normalize by initial depth
! ssum(1) = ssum(1)/pbot(i,j)
! ssum(2) = ssum(2)/pbot(i,j)
! write(lp,'(a,i9,1p3e16.8)')
! & 'epmassd:',nstep,ssum(1),ssum(2),ssum(2)-ssum(1)
! write(lp,'(i9,a,3f12.6)')
! . nstep,',oneta =',onetaold,onetanew,onetaold/onetanew
! call flush(lp)
! endif !test
endif !ip
enddo !i
enddo !j
!$OMP END PARALLEL DO
endif !epmass
!
! --- regiona-wide statistics
!
call xcsum(d1, util1,ipa)
call xcsum(d2, util2,ipa)
watcum=watcum+d1
empcum=empcum+d2
!
!diag if (itest.gt.0 .and. jtest.gt.0) then
!diag write(lp,'(i9,2i5,a/19x,4f10.4)') &
!diag nstep,i0+itest,j0+jtest, &
!diag ' sswflx surflx sstflx wtrflx', &
!diag sswflx(itest,jtest), &
!diag surflx(itest,jtest), &
!diag sstflx(itest,jtest), &
!diag wtrflx(itest,jtest)
!diag endif !test
return
end subroutine thermf_oi
subroutine thermf(m,n, dtime)
use mod_xc ! HYCOM communication interface
use mod_cb_arrays ! HYCOM saved arrays
implicit none
!
integer m,n
real*8 dtime
!
! --- ---------------
! --- thermal forcing
! --- note: on exit flux is for ocean fraction of each grid cell
! --- ---------------
!
logical, parameter :: ldebug_sssbal=.false. !usually .false.
!
integer i,j,k,ktr,nm,l, iyear,iday,ihour
real day365,pwl,q,utotij,vtotij
real*8 t1mean,s1mean,tmean,smean,pmean,rmean, &
rareac,runsec,secpyr
real*8 d1,d2,d3,d4
!
real pwij(kk+1),trwij(kk,ntracr), &
prij(kk+1),trcij(kk,ntracr)
!
real*8 tmean0,smean0,rmean0
save tmean0,smean0,rmean0
!
double precision dtime_diurnl
save dtime_diurnl
data dtime_diurnl / -99.d0 /
!
# include "stmt_fns.h"
!
!$OMP PARALLEL DO PRIVATE(j,k,i,ktr) &
!$OMP SCHEDULE(STATIC,jblk)
do j=1,jj
do k=1,kk
do i=1,ii
if (SEA_P) then
p(i,j,k+1)=p(i,j,k)+dp(i,j,k,n)
endif !ip
enddo !i
enddo !k
enddo !j
!$OMP END PARALLEL DO
!
! --- ----------------------------
! --- thermal forcing at nestwalls
! --- ----------------------------
!
if (nestfq.ne.0.0 .and. delt1.ne.baclin) then !not on very 1st time step
!
!$OMP PARALLEL DO PRIVATE(j,i,k,pwl,q) &
!$OMP SCHEDULE(STATIC,jblk)
do j=1,jj
do i=1,ii
if (ip(i,j).eq.1 .and. rmunp(i,j).ne.0.0) then
! --- Newtonian relaxation with implict time step,
! --- result is positive if source and nest are positive
! --- Added by Remy Baraille, SHOM
k=1
saln(i,j,k,n)=( saln(i,j,k,n)+delt1*rmunp(i,j)* &
(snest(i,j,k,ln0)*wn0+snest(i,j,k,ln1)*wn1) )/ &
(1.0+delt1*rmunp(i,j))
temp(i,j,k,n)=( temp(i,j,k,n)+delt1*rmunp(i,j)* &
(tnest(i,j,k,ln0)*wn0+tnest(i,j,k,ln1)*wn1) )/ &
(1.0+delt1*rmunp(i,j))
th3d(i,j,k,n)=sig(temp(i,j,k,n),saln(i,j,k,n))-thbase
!
if (hybrid) then
do k=kk,2,-1
pwl=pnest(i,j,k,ln0)*wn0+pnest(i,j,k,ln1)*wn1
if (pwl.gt.p(i,j,kk+1)-tencm) then
pwl=p(i,j,kk+1)
endif
p(i,j,k)=min(p(i,j,k+1), &
( p(i,j,k)+delt1*rmunp(i,j)*pwl )/ &
(1.0+delt1*rmunp(i,j)))
dp(i,j,k,n)=p(i,j,k+1)-p(i,j,k)
!
if (pwl.lt.p(i,j,kk+1)) then
saln(i,j,k,n)=( saln(i,j,k,n)+delt1*rmunp(i,j)* &
(snest(i,j,k,ln0)*wn0+snest(i,j,k,ln1)*wn1) )/ &
(1.0+delt1*rmunp(i,j))
if (k.le.nhybrd) then
temp(i,j,k,n)=( temp(i,j,k,n)+delt1*rmunp(i,j)* &
(tnest(i,j,k,ln0)*wn0+tnest(i,j,k,ln1)*wn1) )/ &
(1.0+delt1*rmunp(i,j))
th3d(i,j,k,n)=sig(temp(i,j,k,n), &
saln(i,j,k,n))-thbase
else
th3d(i,j,k,n)= theta(i,j,k)
temp(i,j,k,n)=tofsig(theta(i,j,k)+thbase, &
saln(i,j,k,n))
endif
endif
enddo !k
dp(i,j,1,n)=p(i,j,2)-p(i,j,1)
else ! isopyc
do k=kk,2,-1
saln(i,j,k,n)=( saln(i,j,k,n)+delt1*rmunp(i,j)* &
(snest(i,j,k,ln0)*wn0+snest(i,j,k,ln1)*wn1) )/ &
(1.0+delt1*rmunp(i,j))
temp(i,j,k,n)=tofsig(th3d(i,j,k,n)+thbase,saln(i,j,k,n))
if (k.ge.3) then
pwl=pnest(i,j,k,ln0)*wn0+pnest(i,j,k,ln1)*wn1
pwl=max(p(i,j,2),pwl)
if (pwl.gt.p(i,j,kk+1)-tencm) then
pwl=p(i,j,kk+1)
endif
p(i,j,k)=min(p(i,j,k+1), &
( p(i,j,k)+delt1*rmunp(i,j)*pwl )/ &
(1.0+delt1*rmunp(i,j)))
endif
dp(i,j,k,n)=p(i,j,k+1)-p(i,j,k)
enddo !k
endif ! hybrid:isopyc
!
! --- minimal tracer support (non-negative in buffer zone).
do ktr= 1,ntracr
tracer(i,j,k,n,ktr)=max(tracer(i,j,k,n,ktr),0.0)
enddo
endif !ip.eq.1 .and. rmunp.ne.0.0
!
if (iu(i,j).eq.1) then
q =rmunvu(i,j)
if (q.ne.0.0) then
do k= 1,kk
pwl=u(i,j,k,n)
u(i,j,k,n)=( u(i,j,k,n)+delt1*q* &
(unest(i,j,k,ln0)*wn0+unest(i,j,k,ln1)*wn1) )/ &
(1.0+delt1*q)
enddo !k
endif !rmunvu.ne.0.0
endif !iu.eq.1
!
if (iv(i,j).eq.1) then
q =rmunvv(i,j)
if (q.ne.0.0) then
do k= 1,kk
pwl=v(i,j,k,n)
v(i,j,k,n)=( v(i,j,k,n)+delt1*q* &
(vnest(i,j,k,ln0)*wn0+vnest(i,j,k,ln1)*wn1) )/ &
(1.0+delt1*q)
enddo !k
endif !rmunvv.ne.0.0
endif !iv.eq.1
enddo !i
enddo !j
!$OMP END PARALLEL DO
!
endif ! nestfq.ne.0.0
!
! --- ----------------------------
! --- thermal forcing at sidewalls
! --- ----------------------------
!
if (relax .and. delt1.ne.baclin) then !not on very 1st time step
!
!$OMP PARALLEL DO PRIVATE(j,i,k,pwl) &
!$OMP SCHEDULE(STATIC,jblk)
do j=1,jj
do i=1,ii
if (SEA_P) then
if (rmu(i,j).ne.0.0) then
! --- Newtonian relaxation with implict time step,
! --- result is positive if source and wall are positive
k=1
saln(i,j,k,n)=( saln(i,j,k,n)+delt1*rmu(i,j)* &
( swall(i,j,k,lc0)*wc0+swall(i,j,k,lc1)*wc1 &
+swall(i,j,k,lc2)*wc2+swall(i,j,k,lc3)*wc3) )/ &
(1.0+delt1*rmu(i,j))
if (lwflag.eq.2 .or. sstflg.gt.2 .or. &
icmflg.eq.2 .or. ticegr.eq.0.0 ) then
! --- use seatmp, since it is the best available SST
if(cpl_seatmp) then
temp(i,j,k,n)=temp(i,j,k,n)+delt1*rmu(i,j)* &
imp_seatmp(i,j,1)/(1.0+delt1*rmu(i,j))
elseif (natm.eq.2) then
temp(i,j,k,n)=( temp(i,j,k,n)+delt1*rmu(i,j)* &
( seatmp(i,j,l0)*w0+seatmp(i,j,l1)*w1) )/ &
(1.0+delt1*rmu(i,j))
else
temp(i,j,k,n)=( temp(i,j,k,n)+delt1*rmu(i,j)* &
( seatmp(i,j,l0)*w0+seatmp(i,j,l1)*w1 &
+seatmp(i,j,l2)*w2+seatmp(i,j,l3)*w3) )/ &
(1.0+delt1*rmu(i,j))
endif
else
temp(i,j,k,n)=( temp(i,j,k,n)+delt1*rmu(i,j)* &
( twall(i,j,k,lc0)*wc0+twall(i,j,k,lc1)*wc1 &
+twall(i,j,k,lc2)*wc2+twall(i,j,k,lc3)*wc3) )/ &
(1.0+delt1*rmu(i,j))
endif
th3d(i,j,k,n)=sig(temp(i,j,k,n),saln(i,j,k,n))-thbase
!
if (hybrid) then
do k=kk,2,-1
pwl=pwall(i,j,k,lc0)*wc0+pwall(i,j,k,lc1)*wc1 &
+pwall(i,j,k,lc2)*wc2+pwall(i,j,k,lc3)*wc3
if (pwl.gt.p(i,j,kk+1)-tencm) then
pwl=p(i,j,kk+1)
endif
p(i,j,k)=min( p(i,j,k+1), &
( p(i,j,k)+delt1*rmu(i,j)*pwl )/ &
(1.0+delt1*rmu(i,j)) )
dp(i,j,k,n)=p(i,j,k+1)-p(i,j,k)
!
if (pwl.lt.p(i,j,kk+1)) then
saln(i,j,k,n)=( saln(i,j,k,n)+delt1*rmu(i,j)* &
( swall(i,j,k,lc0)*wc0+swall(i,j,k,lc1)*wc1 &
+swall(i,j,k,lc2)*wc2+swall(i,j,k,lc3)*wc3) )/ &
(1.0+delt1*rmu(i,j))
if (k.le.nhybrd) then
temp(i,j,k,n)=( temp(i,j,k,n)+delt1*rmu(i,j)* &
( twall(i,j,k,lc0)*wc0+twall(i,j,k,lc1)*wc1 &
+twall(i,j,k,lc2)*wc2+twall(i,j,k,lc3)*wc3) )/ &
(1.0+delt1*rmu(i,j))
th3d(i,j,k,n)=sig(temp(i,j,k,n),saln(i,j,k,n))-thbase
else
th3d(i,j,k,n)= theta(i,j,k)
temp(i,j,k,n)=tofsig(theta(i,j,k)+thbase, &
saln(i,j,k,n))
endif !hybrid:else
endif !pwl.lt.p(i,j,kk+1)
enddo !k
dp(i,j,1,n)=p(i,j,2)-p(i,j,1)
else ! isopyc
do k=kk,2,-1
saln(i,j,k,n)=( saln(i,j,k,n)+delt1*rmu(i,j)* &
( swall(i,j,k,lc0)*wc0+swall(i,j,k,lc1)*wc1 &
+swall(i,j,k,lc2)*wc2+swall(i,j,k,lc3)*wc3) )/ &
(1.0+delt1*rmu(i,j))
temp(i,j,k,n)=tofsig(th3d(i,j,k,n)+thbase,saln(i,j,k,n))
if (k.ge.3) then
pwl=pwall(i,j,k,lc0)*wc0+pwall(i,j,k,lc1)*wc1 &
+pwall(i,j,k,lc2)*wc2+pwall(i,j,k,lc3)*wc3
pwl=max(p(i,j,2),pwl)
if (pwl.gt.p(i,j,kk+1)-tencm) then
pwl=p(i,j,kk+1)
endif
p(i,j,k)=min(p(i,j,k+1), &
p(i,j,k)+delt1*rmu(i,j)*(pwl-p(i,j,k)))
endif !k.ge.3
dp(i,j,k,n)=p(i,j,k+1)-p(i,j,k)
enddo !k
endif !hybrid:isopyc
endif !rmu(i,j).ne.0.0
endif !ip
enddo !i
enddo !j
!$OMP END PARALLEL DO
!
endif ! relax = .true.
!
! --- ----------------------------
! --- tracer forcing at sidewalls
! --- ----------------------------
!
if (trcrlx .and. delt1.ne.baclin) then !not on very 1st time step
!
!$OMP PARALLEL DO PRIVATE(j,i,k,ktr,pwij,trwij,prij,trcij) &
!$OMP SCHEDULE(STATIC,jblk)
do j=1,jj
do i=1,ii
if (SEA_P) then
if (rmutra(i,j).ne.0.0) then !at least one mask is non-zero
prij(1)=0.0
do k=1,kk
prij(k+1) = prij(k)+dp(i,j,k,n)
pwij(k) = pwall(i,j,k,lc0)*wc0 &
+pwall(i,j,k,lc1)*wc1 &
+pwall(i,j,k,lc2)*wc2 &
+pwall(i,j,k,lc3)*wc3
do ktr= 1,ntracr
trwij(k,ktr) = trwall(i,j,k,lc0,ktr)*wc0 &
+trwall(i,j,k,lc1,ktr)*wc1 &
+trwall(i,j,k,lc2,ktr)*wc2 &
+trwall(i,j,k,lc3,ktr)*wc3
enddo !ktr
enddo !k
pwij(kk+1)=prij(kk+1)
! call plctrc(trwij,pwij,kk,ntracr,
! & trcij,prij,kk )
call plmtrc(trwij,pwij,kk,ntracr, &
trcij,prij,kk )
do ktr= 1,ntracr
if (rmutr(i,j,ktr).ne.0.0) then
do k=1,kk
tracer(i,j,k,n,ktr) = ( tracer(i,j,k,n,ktr)+ &
delt1*rmutr(i,j,ktr)*trcij(k,ktr) )/ &
(1.0+delt1*rmutr(i,j,ktr))
enddo !k
endif !rmutr.ktr.ne.0.0
enddo !ktr
endif !rmutra.ne.0.0
endif !ip
enddo !i
enddo !j
!$OMP END PARALLEL DO
!
endif ! trcrlx = .true.
!
! --- ---------------------------------------------------------
! --- Update dpu,dpv, and rebalance velocity, if dp has changed
! --- ---------------------------------------------------------
!
if ((nestfq.ne.0.0 .and. delt1.ne.baclin) .or. &
(relax .and. delt1.ne.baclin) ) then
call dpudpv(dpu(1-nbdy,1-nbdy,1,n), &
dpv(1-nbdy,1-nbdy,1,n), &
p,depthu,depthv, 0,0)
!
!$OMP PARALLEL DO PRIVATE(j,i,k,utotij,vtotij) &
!$OMP SCHEDULE(STATIC,jblk)
do j=1,jj
do i=1,ii
if (iu(i,j).eq.1 .and. &
max(rmunvu(i,j), &
rmu( i,j),rmu(i-1,j) ).ne.0.0) then
utotij = 0.0
do k=1,kk
utotij = utotij + u(i,j,k,n)*dpu(i,j,k,n)
enddo ! k
utotij=utotij/depthu(i,j)
do k=1,kk
u(i,j,k,n) = u(i,j,k,n) - utotij
enddo ! k
endif !rebalance u
!
if (iv(i,j).eq.1 .and. &
max(rmunvv(i,j), &
rmu( i,j),rmu(i,j-1) ).ne.0.0) then
vtotij = 0.0
do k=1,kk
vtotij = vtotij + v(i,j,k,n)*dpv(i,j,k,n)
enddo ! k
vtotij=vtotij/depthv(i,j)
do k=1,kk
v(i,j,k,n) = v(i,j,k,n) - vtotij
enddo ! k
endif !rebalance v
enddo !i
enddo !j
!$OMP END PARALLEL DO
endif !update dpu,dpv and rebalance u,v
!
! --- --------------------------------
! --- thermal forcing of ocean surface
! --- --------------------------------
!
!
if (thermo .or. sstflg.gt.0 .or. srelax) then
!
if (dswflg.eq.1 .and. dtime-dtime_diurnl.gt.1.0) then
! --- update diurnal factor table
call forday(dtime,yrflag, iyear,iday,ihour)
day365 = mod(iday+364,365)
call thermf_diurnal(diurnl, day365)
dtime_diurnl = dtime
!diag if (mnproc.eq.1) then
!diag write (lp,'(a)') 'diurnl updated'
!diag endif !1st tile
endif
!$OMP PARALLEL DO PRIVATE(j) &
!$OMP SHARED(m,n) &
!$OMP SCHEDULE(STATIC,jblk)
do j=1,jj
call thermfj(m,n,dtime, j)
enddo
!$OMP END PARALLEL DO
!
!diag if (itest.gt.0 .and. jtest.gt.0) then
!diag write(lp,'(i9,2i5,a/19x,4f10.4)') &
!diag nstep,i0+itest,j0+jtest, &
!diag ' sstflx ustar hekman surflx', &
!diag sstflx(itest,jtest), &
!diag ustar( itest,jtest), &
!diag hekman(itest,jtest), &
!diag surflx(itest,jtest)
!diag write(lp,'(i9,2i5,a/19x,4f10.4)') &
!diag nstep,i0+itest,j0+jtest, &
!diag ' sswflx wtrflx rivflx sssflx', &
!diag sswflx(itest,jtest), &
!diag wtrflx(itest,jtest), &
!diag rivflx(itest,jtest), &
!diag sssflx(itest,jtest)
!diag endif !test
!
! --- smooth surface fluxes?
!
if (flxsmo) then
call psmooth_ice(surflx, 0,0, ishlf, util1) !uses covice
call psmooth_ice(wtrflx, 0,0, ishlf, util1) !uses covice
endif
!
if (sssbal.eq.1) then
!
! --- balance sssflx, via a region-wide offset:
! --- util2 from thermfj holds sssflx*scp2
!
call xcsum(d2, util2,ipa) !total
if (d2.ne.0.0d0) then !not balanced
q = -d2/area
do j=1,jj
do i=1,ii
if (SEA_P) then
sssflx(i,j) = sssflx(i,j) + q
endif
enddo !i
enddo !j
if (ldebug_sssbal .and. &
mnproc.eq.1 .and. mod(nstep,100).le.1) then
write (lp,'(i9,a,1pe12.5)') &
nstep,' offset sssflx by ',q
endif !master
endif !not already balanced
elseif (sssbal.eq.2) then
!
! --- balance sssflx, by scaling up relaxation:
! --- util2 from thermfj holds sssflx*scp2
! --- util1 from thermfj holds max(sssflx*scp2), i.e. positive only
!
call xcsum(d2, util2,ipa) !total
if (d2.ne.0.0d0) then !not balanced
call xcsum(d1, util1,ipa) !positive only
d3 = d2 - d1 !negative only
if (d1.gt.-d3) then
! --- scale up the negative values
q = min(-d1/d3, 5.0 ) ! >1
!$OMP PARALLEL DO PRIVATE(j,k,l,i) &
!$OMP SCHEDULE(STATIC,jblk)
do j=1,jj
do i=1,ii
if (SEA_P) then
if (sssflx(i,j).lt.0.0) then
sssflx(i,j) = q*sssflx(i,j)
endif
endif
enddo !i
enddo !j
if (ldebug_sssbal .and. &
mnproc.eq.1 .and. mod(nstep,100).le.1) then
write (lp,'(i9,a,f6.3)') &
nstep,' scale -ve sssflx by ',q
endif !master
else !d1.lt.-d3
! --- scale up the positive values
q = min(-d3/d1, 5.0 ) ! >1
!$OMP PARALLEL DO PRIVATE(j,k,l,i) &
!$OMP SCHEDULE(STATIC,jblk)
do j=1,jj
do i=1,ii
if (SEA_P) then
if (sssflx(i,j).gt.0.0) then
sssflx(i,j) = q*sssflx(i,j)
endif
endif
enddo !i
enddo !j
if (ldebug_sssbal .and. &
mnproc.eq.1 .and. mod(nstep,100).le.1) then
write (lp,'(i9,a,f6.3)') &
nstep,' scale +ve sssflx by ',q
endif !master
endif !reduce +ve:reduce -ve
endif !not already balanced
endif !sssbal
!###
!
if (nstep.eq.nstep1+1 .or. diagno) then
if (nstep.eq.nstep1+1) then
nm=m
else
nm=n
endif
!$OMP PARALLEL DO PRIVATE(j,k,i) &
!$OMP SCHEDULE(STATIC,jblk)
do j=1,jj
do i=1,ii
if (SEA_P) then
util1(i,j)=temp(i,j,1,nm)*scp2(i,j)
util2(i,j)=saln(i,j,1,nm)*scp2(i,j)
endif !ip
enddo !i
enddo !j
!$OMP END PARALLEL DO
call xcsum(d1, util1,ipa)
call xcsum(d2, util2,ipa)
t1mean=d1
s1mean=d2
!
!$OMP PARALLEL DO PRIVATE(j,k,i,q) &
!$OMP SCHEDULE(STATIC,jblk)
do j=1,jj
k=1
do i=1,ii
if (SEA_P) then
! --- always use mass-conserving diagnostics
oneta(i,j,nm)= max(oneta0,1.0 + pbavg(i,j,nm)/pbot(i,j))
q=oneta(i,j,nm)*dp(i,j,k,nm)*scp2(i,j)
util1(i,j)=q
util2(i,j)=q*temp(i,j,k,nm)
util3(i,j)=q*saln(i,j,k,nm)
util4(i,j)=q*th3d(i,j,k,nm)
endif !ip
enddo !i
do k=2,kk
do i=1,ii
if (SEA_P) then
q=oneta(i,j,nm)*dp(i,j,k,nm)*scp2(i,j)
util1(i,j)=util1(i,j)+q
util2(i,j)=util2(i,j)+q*temp(i,j,k,nm)
util3(i,j)=util3(i,j)+q*saln(i,j,k,nm)
util4(i,j)=util4(i,j)+q*th3d(i,j,k,nm)
endif !ip
enddo !i
enddo !k
enddo !j
!$OMP END PARALLEL DO
call xcsum(d1, util1,ipa)
call xcsum(d2, util2,ipa)
call xcsum(d3, util3,ipa)
call xcsum(d4, util4,ipa)
pmean=d1
tmean=d2/pmean
smean=d3/pmean
rmean=d4/pmean
if (mnproc.eq.1) then
write (lp,'(i9,a,3f10.3)') &
nstep,' mean basin temp, saln, dens ', &
tmean,smean,rmean+thbase
endif !1st tile
if (nstep.eq.nstep1+1) then
!
! --- save initial basin means.
tmean0=tmean
smean0=smean
rmean0=rmean
else
!
! --- diagnostic printout of fluxes.
rareac=1.0/(area*(nstep-nstep1))
runsec= baclin*(nstep-nstep1)
if (yrflag.eq.0) then
secpyr=360.00d0*86400.0d0
elseif (yrflag.lt.3) then
secpyr=366.00d0*86400.0d0
elseif (yrflag.eq.3) then
secpyr=365.25d0*86400.0d0
elseif (yrflag.eq.4) then
secpyr=365.00d0*86400.0d0
endif
if (mnproc.eq.1) then
write (lp,'(i9,a,2f10.3)') &
nstep,' mean surface temp and saln ', &
t1mean/area,s1mean/area
write (lp,'(i9,a,2f10.3,a)') &
nstep,' energy residual (atmos,tot) ', &
watcum*rareac, &
(tmean-tmean0)*(spcifh*avgbot*rhoref)/runsec, &
' (W/m^2)'
write (lp,'(i9,a,2f10.3,a)') &
nstep,' e - p residual (atmos,tot) ', &
empcum*svref*rareac*100.0*secpyr, &
(smean-smean0)/(saln0*runsec)*avgbot*100.0*secpyr, &
' (cm/year)'
write (lp,'(i9,a,2f10.3)') &
nstep,' temp drift per century ', &
(watcum*rareac/(spcifh*avgbot*rhoref))*(secpyr*100.0d0), &
(tmean-tmean0)*(secpyr*100.0d0)/runsec
write (lp,'(i9,a,f10.3)') &
nstep,' saln drift per century ', &
(smean-smean0)*(secpyr*100.0d0)/runsec
write (lp,'(i9,a,9x,f10.3)') &
nstep,' dens drift per century ', &
(rmean-rmean0)*(secpyr*100.0d0)/runsec
endif !1st tile
call xcsync(flush_lp)
endif !master
endif !diagno
!c
endif ! thermo .or. sstflg.gt.0 .or. srelax
!
return
end subroutine thermf
!
subroutine thermfj(m,n,dtime, j)
use mod_xc ! HYCOM communication interface