forked from HYCOM/HYCOM-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hybgen.F90
2785 lines (2770 loc) · 105 KB
/
hybgen.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 hybgen(m,n, hybgen_raflag)
use mod_xc ! HYCOM communication interface
use mod_cb_arrays ! HYCOM saved arrays
use mod_pipe ! HYCOM debugging interface
!
! --- hycom version 1.0
implicit none
!
logical hybgen_raflag
integer m,n
!
! --- ---------------------
! --- hybrid grid generator
! --- ---------------------
!
logical, parameter :: lpipe_hybgen=.false. !for debugging
!
integer i,j,k
character text*12
!
103 format (i9,2i5,a/(33x,i3,2f8.3,f8.3,f9.3,f9.2))
!diag if (itest.gt.0 .and. jtest.gt.0) then
!diag write (lp,103) nstep,itest+i0,jtest+j0, &
!diag ' entering hybgen: temp saln dens thkns dpth', &
!diag (k,temp(itest,jtest,k,n),saln(itest,jtest,k,n), &
!diag th3d(itest,jtest,k,n)+thbase,dp(itest,jtest,k,n)*qonem, &
!diag p(itest,jtest,k+1)*qonem,k=1,kk)
!diag endif
!
call xctilr(dpmixl( 1-nbdy,1-nbdy, n),1, 1, 1,1, halo_ps)
!
!$OMP PARALLEL DO PRIVATE(j) &
!$OMP SHARED(m,n) &
!$OMP SCHEDULE(STATIC,jblk)
do j=1,jj
call hybgenaj(m,n, j)
enddo
!$OMP END PARALLEL DO
!
! --- vertical momentum flux across moving interfaces (the s-dot term in the
! --- momentum equation) - required to locally conserve momentum when hybgen
! --- moves vertical coordinates first, store old interface pressures in
! --- -pu-, -pv-
!
!$OMP PARALLEL DO PRIVATE(j,i,k) &
!$OMP SCHEDULE(STATIC,jblk)
do j=1,jj
do i=1,ii
if (SEA_U) then
pu(i,j,1)=0.0
pu(i,j,2)=dpu(i,j,1,n)
endif !iu
if (SEA_V) then
pv(i,j,1)=0.0
pv(i,j,2)=dpv(i,j,1,n)
endif !iv
enddo !i
do k=2,kk
do i=1,ii
if (SEA_U) then
pu(i,j,k+1)=pu(i,j,k)+dpu(i,j,k,n)
endif !iu
if (SEA_V) then
pv(i,j,k+1)=pv(i,j,k)+dpv(i,j,k,n)
endif !iv
enddo !i
enddo !k
enddo !j
!$OMP END PARALLEL DO
!
! --- update layer thickness at -u,v- points.
call dpudpv(dpu(1-nbdy,1-nbdy,1,n), &
dpv(1-nbdy,1-nbdy,1,n), &
p,depthu,depthv, 0,0)
!
if (lpipe .and. lpipe_hybgen) then
! --- compare two model runs.
! --- exit (if any) in next call to pipe_compare_all
!
call pipe_fatal_off
do k= 1,kk+1
write (text,'(a9,i3)') 'p k=',k
call pipe_compare(p( 1-nbdy,1-nbdy,k),ip,text)
write (text,'(a9,i3)') 'pu k=',k
call pipe_compare(pu(1-nbdy,1-nbdy,k),iu,text)
write (text,'(a9,i3)') 'pv k=',k
call pipe_compare(pv(1-nbdy,1-nbdy,k),iv,text)
enddo
do k= 1,kk
write (text,'(a9,i3)') 'dp k=',k
call pipe_compare(dp( 1-nbdy,1-nbdy,k,n),ip,text)
write (text,'(a9,i3)') 'dpu k=',k
call pipe_compare(dpu(1-nbdy,1-nbdy,k,n),iu,text)
write (text,'(a9,i3)') 'dpv k=',k
call pipe_compare(dpv(1-nbdy,1-nbdy,k,n),iv,text)
enddo
call pipe_fatal_on
endif
!
!$OMP PARALLEL DO PRIVATE(j) &
!$OMP SHARED(n) &
!$OMP SCHEDULE(STATIC,jblk)
do j=1,jj
call hybgenbj(n, j) !update velocity at time level t+1
enddo
!$OMP END PARALLEL DO
!
if (hybgen_raflag) then
!
! --- Apply Robert-Asselin update to layer thickness at time level t
!
!$OMP PARALLEL DO PRIVATE(j) &
!$OMP SHARED(m,n) &
!$OMP SCHEDULE(STATIC,jblk)
do j=1,jj
call hybgencj(m,n, j)
enddo
!$OMP END PARALLEL DO
!
!$OMP PARALLEL DO PRIVATE(j,i,k) &
!$OMP SCHEDULE(STATIC,jblk)
do j=1,jj
do i=1,ii
if (SEA_U) then
pu(i,j,1)=0.0
pu(i,j,2)=dpu(i,j,1,m)
endif !iu
if (SEA_V) then
pv(i,j,1)=0.0
pv(i,j,2)=dpv(i,j,1,m)
endif !iv
enddo !i
do k=2,kk
do i=1,ii
if (SEA_U) then
pu(i,j,k+1)=pu(i,j,k)+dpu(i,j,k,m)
endif !iu
if (SEA_V) then
pv(i,j,k+1)=pv(i,j,k)+dpv(i,j,k,m)
endif !iv
enddo !i
enddo !k
enddo !j
!$OMP END PARALLEL DO
!
call dpudpv(dpu(1-nbdy,1-nbdy,1,m), &
dpv(1-nbdy,1-nbdy,1,m), &
p,depthu,depthv, 0,0)
!
if (lpipe .and. lpipe_hybgen) then
! --- compare two model runs.
do k= 1,kk+1
write (text,'(a9,i3)') 'p k=',k
call pipe_compare(p( 1-nbdy,1-nbdy,k),ip,text)
write (text,'(a9,i3)') 'pu k=',k
call pipe_compare(pu(1-nbdy,1-nbdy,k),iu,text)
write (text,'(a9,i3)') 'pv k=',k
call pipe_compare(pv(1-nbdy,1-nbdy,k),iv,text)
enddo
do k= 1,kk
write (text,'(a9,i3)') 'dp k=',k
call pipe_compare(dp( 1-nbdy,1-nbdy,k,m),ip,text)
write (text,'(a9,i3)') 'dpu k=',k
call pipe_compare(dpu(1-nbdy,1-nbdy,k,m),iu,text)
write (text,'(a9,i3)') 'dpv k=',k
call pipe_compare(dpv(1-nbdy,1-nbdy,k,m),iv,text)
enddo
endif
!
!$OMP PARALLEL DO PRIVATE(j) &
!$OMP SHARED(m) &
!$OMP SCHEDULE(STATIC,jblk)
do j=1,jj
call hybgenbj(m, j) !update velocity at time level t
enddo
!$OMP END PARALLEL DO
endif !hybgen_raflag
!
return
end subroutine hybgen
subroutine hybgenaj(m,n,j )
use mod_xc ! HYCOM communication interface
use mod_cb_arrays ! HYCOM saved arrays
implicit none
!
integer m,n, j
!
! --- --------------------------------------------
! --- hybrid grid generator, single j-row (part A).
! --- --------------------------------------------
!
logical, parameter :: lunmix=.true. !unmix a too light deepest layer
logical, parameter :: lconserve=.false. !explicitly conserve each column
integer, parameter :: ndebug_tracer=0 !tracer to debug, usually 0 (off)
!
double precision asum( mxtrcr+4,3)
real offset(mxtrcr+4)
!
logical lcm(kdm) !use PCM for some layers?
real s1d(kdm,mxtrcr+4), & !original scalar fields
f1d(kdm,mxtrcr+4), & !final scalar fields
c1d(kdm,mxtrcr+4,3), & !interpolation coefficients
dpi( kdm), & !original layer thicknesses, >= dpthin
dprs(kdm), & !original layer thicknesses
pres(kdm+1), & !original layer interfaces
prsf(kdm+1), & !final layer interfaces
qhrlx( kdm+1), & !relaxation coefficient, from qhybrlx
dp0ij( kdm), & !minimum layer thickness
dp0cum(kdm+1) !minimum interface depth
real p_hat,p_hat0,p_hat2,p_hat3,hybrlx, &
delt,deltm,dels,delsm,q,qdep,qtr,qts,thkbop, &
zthk,dpthin
integer i,k,ka,kp,ktr,fixall,fixlay,nums1d
character*12 cinfo
!
double precision, parameter :: zp5=0.5 !for sign function
!
! --- c u s h i o n function (from Bleck & Benjamin, 1992):
! --- if delp >= qqmx*dp0 >> dp0, -cushn- returns -delp-
! --- if delp <= qqmn*dp0 << -dp0, -cushn- returns -dp0-
!
real qqmn,qqmx,cusha,cushb
parameter (qqmn=-4.0, qqmx=2.0) ! shifted range
! parameter (qqmn=-2.0, qqmx=4.0) ! traditional range
! parameter (qqmn=-4.0, qqmx=6.0) ! somewhat wider range
parameter (cusha=qqmn**2*(qqmx-1.0)/(qqmx-qqmn)**2)
parameter (cushb=1.0/qqmn)
!
real qq,cushn,delp,dp0
# include "stmt_fns.h"
qq( delp,dp0)=max(qqmn, min(qqmx, delp/dp0))
cushn(delp,dp0)=dp0* &
(1.0+cusha*(1.0-cushb*qq(delp,dp0))**2)* &
max(1.0, delp/(dp0*qqmx))
!
dpthin = 0.001*onemm
thkbop = thkbot*onem
hybrlx = 1.0/qhybrlx
!
if (mxlmy) then
nums1d = ntracr + 4
else
nums1d = ntracr + 2
endif
!
if (.not.isopcm) then
! --- lcm the same for all points
do k=1,nhybrd
lcm(k) = .false. !use same remapper for all layers
enddo !k
do k=nhybrd+1,kk
lcm(k) = .true. !purely isopycnal layers use PCM
enddo !k
endif
!
do i=1,ii
if (SEA_P) then
!
! --- terrain following starts at depth dpns and ends at depth dsns
qdep = max( 0.0, min( 1.0, &
(depths(i,j) - dsns)/ &
(dpns - dsns) ) )
!
if (qdep.lt.1.0) then
! --- terrain following, qhrlx=1 and ignore dp00
p(i,j, 1)=0.0
dp0cum(1)=0.0
qhrlx( 1)=1.0
dp0ij( 1)=qdep*dp0k(1) + (1.0-qdep)*ds0k(1)
!diag if (i.eq.itest .and. j.eq.jtest) then
!diag k=1
!diag write (lp,*) 'qdep = ',qdep
!diag write (lp,'(a/i6,1x,4f9.3/a)') &
!diag ' k dp0ij ds0k dp0k p', &
!diag k,dp0ij(k)*qonem,ds0k(1)*qonem,dp0k(1)*qonem, &
!diag p(i,j,k)*qonem, &
!diag ' k dp0ij p-cum p dp0cum'
!diag endif !debug
dp0cum(2)=dp0cum(1)+dp0ij(1)
qhrlx( 2)=1.0
p(i,j, 2)=p(i,j,1)+dp(i,j,1,n)
do k=2,kk
qhrlx( k+1)=1.0
dp0ij( k) =qdep*dp0k(k) + (1.0-qdep)*ds0k(k)
dp0cum(k+1)=dp0cum(k)+dp0ij(k)
p(i,j, k+1)=p(i,j,k)+dp(i,j,k,n)
!diag if (i.eq.itest .and. j.eq.jtest) then
!diag write (lp,'(i6,1x,4f9.3)') &
!diag k,dp0ij(k)*qonem,p(i,j,k)*qonem-dp0cum(k)*qonem, &
!diag p(i,j,k)*qonem,dp0cum(k)*qonem
!diag endif !debug
enddo !k
else
! --- not terrain following
p(i,j, 1)=0.0
dp0cum(1)=0.0
qhrlx( 1)=1.0 !no relaxation in top layer
dp0ij( 1)=dp0k(1)
!diag if (i.eq.itest .and. j.eq.jtest) then
!diag k=1
!diag write (lp,*) 'qdep = ',qdep
!diag write (lp,'(a/i6,1x,f9.3)') &
!diag ' k dp0ij dp0k q p-cum p dp0cum', &
!diag k,dp0ij(k)*qonem
!diag endif !debug
dp0cum(2)=dp0cum(1)+dp0ij(1)
qhrlx( 2)=1.0 !no relaxation in top layer
p(i,j, 2)=p(i,j,1)+dp(i,j,1,n)
do k=2,kk
! --- q is dp0k(k) when in surface fixed coordinates
! --- q is dp00i when much deeper than surface fixed coordinates
if (dp0k(k).le.dp00i) then
q = dp0k(k)
qts= 0.0 !0 at dp0k
else
q = max( dp00i, &
dp0k(k) * dp0k(k)/ &
max( dp0k( k), &
p(i,j,k)-dp0cum(k) ) )
qts= 1.0 - (q-dp00i)/(dp0k(k)-dp00i) !0 at dp0k, 1 at dp00i
endif
qhrlx( k+1)=1.0/(1.0 + qts*(hybrlx-1.0)) !1 at dp0k, qhybrlx at dp00i
dp0ij( k) =min( q, dp0k(k) )
dp0cum(k+1)=dp0cum(k)+dp0ij(k)
p(i,j, k+1)=p(i,j,k)+dp(i,j,k,n)
!diag if (i.eq.itest .and. j.eq.jtest) then
!diag write (lp,'(i6,1x,6f9.3)') &
!diag k,dp0ij(k)*qonem,dp0k(k)*qonem,q*qonem, &
!diag p(i,j,k)*qonem-dp0cum(k)*qonem, &
!diag p(i,j,k)*qonem,dp0cum(k)*qonem
!diag endif !debug
enddo !k
endif !qdep<1:else
!
! --- identify the current fixed coordinate layers
fixlay = 1 !layer 1 always fixed
do k= 2,nhybrd
if (dp0cum(k).ge.topiso(i,j)) then
exit !layers k to nhybrd might be isopycnal
endif
! --- top of layer is above topiso, i.e. always fixed coordinate layer
qhrlx(k+1) = 1.0 !no relaxation in fixed layers
fixlay = fixlay+1
enddo !k
!diag if (i.eq.itest .and. j.eq.jtest) then
!diag write(lp,'(a,i3)') &
!diag 'hybgen, always-fixed coordinate layers: 1 to ', &
!diag fixlay
!diag call flush(lp)
!diag endif !debug
!
fixall = fixlay
do k= fixall+1,nhybrd
!diag if (i.eq.itest .and. j.eq.jtest) then
!diag write (lp,'(i6,1x,2f9.3)') &
!diag k,p(i,j,k+1)*qonem,dp0cum(k+1)*qonem
!diag call flush(lp)
!diag endif !debug
if (p(i,j,k+1).gt.dp0cum(k+1)+0.1*dp0ij(k)) then
if (fixlay.gt.fixall) then
! --- should the previous layer remain fixed?
if (p(i,j,k).gt.dp0cum(k)) then
fixlay = fixlay-1
endif
endif
exit !layers k to nhybrd might be isopycnal
endif
! --- sometimes fixed coordinate layer
qhrlx(k) = 1.0 !no relaxation in fixed layers
fixlay = fixlay+1
enddo !k
!diag if (i.eq.itest .and. j.eq.jtest) then
!diag write(lp,'(a,i3)') &
!diag 'hybgen, fixed coordinate layers: 1 to ', &
!diag fixlay
!diag call flush(lp)
!diag endif !debug
!
!diag if (i.eq.itest .and. j.eq.jtest) then
!diag write (lp,'(a/(i6,1x,2f8.3,2f9.3,f9.3))') &
!diag 'hybgen: thkns minthk dpth mindpth hybrlx', &
!diag (k,dp(i,j,k,n)*qonem, dp0ij(k)*qonem, &
!diag p(i,j,k+1)*qonem,dp0cum(k+1)*qonem, &
!diag 1.0/qhrlx(k+1), &
!diag k=1,kk)
!diag endif !debug
!
! --- identify the deepest layer kp with significant thickness (> dpthin)
!
kp = 2 !minimum allowed value
do k=kk,3,-1
if (p(i,j,k+1)-p(i,j,k).ge.dpthin) then
kp=k
exit
endif
enddo !k
!
!diag if (i.eq.itest .and. j.eq.jtest) then
!diag write(lp,'(a,i3)') &
!diag 'hybgen, deepest inflated layer:',kp
!diag call flush(lp)
!diag endif !debug
!
k = kp !at least 2
ka = max(k-2,1) !k might be 2
!
if (k.gt.fixlay+1 .and. qdep.eq.1.0 .and. & !layer not fixed depth
p(i,j,k)-p(i,j,k-1).ge.dpthin .and. & !layer above not too thin
theta(i,j,k)-epsil.gt.th3d(i,j,k,n) .and. &
th3d(i,j,k-1,n) .gt.th3d(i,j,k,n) .and. &
th3d(i,j,ka, n) .gt.th3d(i,j,k,n) ) then
!
! --- water in the deepest inflated layer with significant thickness
! --- (kp) is too light, and it is lighter than the two layers above.
! ---
! --- this should only occur when relaxing or nudging layer thickness
! --- and is a bug (bad interaction with tsadvc) even in those cases
! ---
! --- entrain the entire layer into the one above
!--- note the double negative in T=T-q*(T-T'), equiv. to T=T+q*(T'-T)
q = (p(i,j,k+1)-p(i,j,k))/(p(i,j,k+1)-p(i,j,k-1))
if (hybflg.eq.0) then !T&S
temp(i,j,k-1,n)=temp(i,j,k-1,n)-q*(temp(i,j,k-1,n) - &
temp(i,j,k, n) )
saln(i,j,k-1,n)=saln(i,j,k-1,n)-q*(saln(i,j,k-1,n) - &
saln(i,j,k, n) )
th3d(i,j,k-1,n)=sig(temp(i,j,k-1,n),saln(i,j,k-1,n))-thbase
elseif (hybflg.eq.1) then !th&S
th3d(i,j,k-1,n)=th3d(i,j,k-1,n)-q*(th3d(i,j,k-1,n) - &
th3d(i,j,k, n) )
saln(i,j,k-1,n)=saln(i,j,k-1,n)-q*(saln(i,j,k-1,n) - &
saln(i,j,k, n) )
temp(i,j,k-1,n)=tofsig(th3d(i,j,k-1,n)+thbase,saln(i,j,k-1,n))
elseif (hybflg.eq.2) then !th&T
th3d(i,j,k-1,n)=th3d(i,j,k-1,n)-q*(th3d(i,j,k-1,n) - &
th3d(i,j,k, n) )
temp(i,j,k-1,n)=temp(i,j,k-1,n)-q*(temp(i,j,k-1,n) - &
temp(i,j,k, n) )
saln(i,j,k-1,n)=sofsig(th3d(i,j,k-1,n)+thbase,temp(i,j,k-1,n))
endif
if (ndebug_tracer.gt.0 .and. ndebug_tracer.le.ntracr .and. &
i.eq.itest .and. j.eq.jtest) then
ktr = ndebug_tracer
write(lp,'(a,i3,f6.3,f9.4)') &
'hybgen, 11(+):', &
k-1,0.0,tracer(i,j,k-1,n,ktr)
call flush(lp)
endif !debug_tracer
do ktr= 1,ntracr
tracer(i,j,k-1,n,ktr)=tracer(i,j,k-1,n,ktr)- &
q*(tracer(i,j,k-1,n,ktr) - &
tracer(i,j,k, n,ktr) )
enddo !ktr
if (ndebug_tracer.gt.0 .and. ndebug_tracer.le.ntracr .and. &
i.eq.itest .and. j.eq.jtest) then
ktr = ndebug_tracer
write(lp,'(a,i3,f6.3,f9.4)') &
'hybgen, 11(+):', &
k-1,q,tracer(i,j,k-1,n,ktr)
write(lp,'(a,i3,f6.3,f9.4)') &
'hybgen, 11(+):', &
k,q,tracer(i,j,k,n,ktr)
write(lp,'(a,i3)') &
'hybgen, deepest inflated layer:',kp
call flush(lp)
endif !debug_tracer
if (mxlmy) then
q2( i,j,k-1,n)=q2( i,j,k-1,n)- &
q*(q2( i,j,k-1,n)-q2( i,j,k,n))
q2l(i,j,k-1,n)=q2l(i,j,k-1,n)- &
q*(q2l(i,j,k-1,n)-q2l(i,j,k,n))
endif
! --- entrained the entire layer into the one above, so now kp=kp-1
p(i,j,k) = p(i,j,k+1)
kp = k-1
!diag if (i.eq.itest .and. j.eq.jtest) then
!diag write(lp,'(a,i3,f6.3,5f8.3)') &
!diag 'hybgen, 11(+):', &
!diag k-1,q,temp(i,j,k-1,n),saln(i,j,k-1,n), &
!diag th3d(i,j,k-1,n)+thbase,theta(i,j,k-1)+thbase
!diag write(lp,'(a,i3)') &
!diag 'hybgen, deepest inflated layer:',kp
!diag call flush(lp)
!diag endif !debug
elseif (k.gt.fixlay+1 .and. qdep.eq.1.0 .and. & !layer not fixed depth
p(i,j,k)-p(i,j,k-1).ge.dpthin .and. & !layer above not too thin
theta(i,j,k)-epsil.gt.th3d(i,j,k,n) .and. &
th3d(i,j,k-1,n) .gt.th3d(i,j,k,n) ) then
!
! --- water in the deepest inflated layer with significant thickness
! --- (kp) is too light, and it is lighter than the layer above.
! ---
! --- swap the entire layer with the one above.
!diag if (i.eq.itest .and. j.eq.jtest) then
!diag write(lp,'(a,i3,f8.5,5f10.5)') &
!diag 'hybgen, original:', &
!diag k-1,0.0,temp(i,j,k-1,n),saln(i,j,k-1,n), &
!diag th3d(i,j,k-1,n)+thbase,theta(i,j,k-1)+thbase
!diag write(lp,'(a,i3,f8.5,5f10.5)') &
!diag 'hybgen, original:', &
!diag k,0.0,temp(i,j,k, n),saln(i,j,k, n), &
!diag th3d(i,j,k, n)+thbase,theta(i,j,k )+thbase
!diag endif !debug
if (p(i,j,k+1)-p(i,j,k).le.p(i,j,k)-p(i,j,k-1)) then
! --- bottom layer is thinner, take entire bottom layer
!--- note the double negative in T=T-q*(T-T'), equiv. to T=T+q*(T'-T)
s1d(k-1,1) = temp(i,j,k-1,n)
s1d(k-1,2) = saln(i,j,k-1,n)
s1d(k-1,3) = th3d(i,j,k-1,n)
q = (p(i,j,k+1)-p(i,j,k))/(p(i,j,k)-p(i,j,k-1)) !<=1.0
if (hybflg.eq.0) then !T&S
temp(i,j,k-1,n)=temp(i,j,k-1,n)-q*(temp(i,j,k-1,n) - &
temp(i,j,k, n) )
saln(i,j,k-1,n)=saln(i,j,k-1,n)-q*(saln(i,j,k-1,n) - &
saln(i,j,k, n) )
th3d(i,j,k-1,n)=sig(temp(i,j,k-1,n),saln(i,j,k-1,n))-thbase
elseif (hybflg.eq.1) then !th&S
th3d(i,j,k-1,n)=th3d(i,j,k-1,n)-q*(th3d(i,j,k-1,n) - &
th3d(i,j,k, n) )
saln(i,j,k-1,n)=saln(i,j,k-1,n)-q*(saln(i,j,k-1,n) - &
saln(i,j,k, n) )
temp(i,j,k-1,n)=tofsig(th3d(i,j,k-1,n)+thbase, &
saln(i,j,k-1,n))
elseif (hybflg.eq.2) then !th&T
th3d(i,j,k-1,n)=th3d(i,j,k-1,n)-q*(th3d(i,j,k-1,n) - &
th3d(i,j,k, n) )
temp(i,j,k-1,n)=temp(i,j,k-1,n)-q*(temp(i,j,k-1,n) - &
temp(i,j,k, n) )
saln(i,j,k-1,n)=sofsig(th3d(i,j,k-1,n)+thbase, &
temp(i,j,k-1,n))
endif
temp(i,j,k,n) = s1d(k-1,1)
saln(i,j,k,n) = s1d(k-1,2)
th3d(i,j,k,n) = s1d(k-1,3)
do ktr= 1,ntracr
s1d(k-1,2+ktr) = tracer(i,j,k-1,n,ktr)
tracer(i,j,k-1,n,ktr) = tracer(i,j,k-1,n,ktr)- &
q*(tracer(i,j,k-1,n,ktr) - &
tracer(i,j,k, n,ktr) )
tracer(i,j,k, n,ktr) = s1d(k-1,2+ktr)
enddo !ktr
if (mxlmy) then
s1d(k-1,ntracr+3) = q2( i,j,k-1,n)
s1d(k-1,ntracr+4) = q2l(i,j,k-1,n)
q2( i,j,k-1,n) = q2( i,j,k-1,n)- &
q*(q2( i,j,k-1,n)-q2( i,j,k,n))
q2l(i,j,k-1,n) = q2l(i,j,k-1,n)- &
q*(q2l(i,j,k-1,n)-q2l(i,j,k,n))
q2( i,j,k, n) = s1d(k-1,ntracr+3)
q2l(i,j,k, n) = s1d(k-1,ntracr+4)
endif
else
! --- bottom layer is thicker, take entire layer above
s1d(k,1) = temp(i,j,k,n)
s1d(k,2) = saln(i,j,k,n)
s1d(k,3) = th3d(i,j,k,n)
q = (p(i,j,k)-p(i,j,k-1))/(p(i,j,k+1)-p(i,j,k)) !<1.0
if (hybflg.eq.0) then !T&S
temp(i,j,k,n)=temp(i,j,k,n)+q*(temp(i,j,k-1,n) - &
temp(i,j,k, n) )
saln(i,j,k,n)=saln(i,j,k,n)+q*(saln(i,j,k-1,n) - &
saln(i,j,k, n) )
th3d(i,j,k,n)=sig(temp(i,j,k,n),saln(i,j,k,n))-thbase
elseif (hybflg.eq.1) then !th&S
th3d(i,j,k,n)=th3d(i,j,k,n)+q*(th3d(i,j,k-1,n) - &
th3d(i,j,k, n) )
saln(i,j,k,n)=saln(i,j,k,n)+q*(saln(i,j,k-1,n) - &
saln(i,j,k, n) )
temp(i,j,k,n)=tofsig(th3d(i,j,k,n)+thbase,saln(i,j,k,n))
elseif (hybflg.eq.2) then !th&T
th3d(i,j,k,n)=th3d(i,j,k,n)+q*(th3d(i,j,k-1,n) - &
th3d(i,j,k, n) )
temp(i,j,k,n)=temp(i,j,k,n)+q*(temp(i,j,k-1,n) - &
temp(i,j,k, n) )
saln(i,j,k,n)=sofsig(th3d(i,j,k,n)+thbase,temp(i,j,k,n))
endif
temp(i,j,k-1,n) = s1d(k,1)
saln(i,j,k-1,n) = s1d(k,2)
th3d(i,j,k-1,n) = s1d(k,3)
do ktr= 1,ntracr
s1d(k,2+ktr) = tracer(i,j,k,n,ktr)
tracer(i,j,k, n,ktr) = tracer(i,j,k,n,ktr)+ &
q*(tracer(i,j,k-1,n,ktr) - &
tracer(i,j,k, n,ktr) )
tracer(i,j,k-1,n,ktr) = s1d(k,2+ktr)
enddo !ktr
if (mxlmy) then
s1d(k,ntracr+3) = q2( i,j,k,n)
s1d(k,ntracr+4) = q2l(i,j,k,n)
q2( i,j,k, n) = q2( i,j,k,n)- &
q*(q2( i,j,k-1,n)-q2( i,j,k,n))
q2l(i,j,k, n) = q2l(i,j,k,n)- &
q*(q2l(i,j,k-1,n)-q2l(i,j,k,n))
q2( i,j,k-1,n) = s1d(k,ntracr+3)
q2l(i,j,k-1,n) = s1d(k,ntracr+4)
endif
endif !bottom too light
!diag if (i.eq.itest .and. j.eq.jtest) then
!diag write(lp,'(a,i3,f8.5,5f10.5)') &
!diag 'hybgen, overturn:', &
!diag k-1,q,temp(i,j,k-1,n),saln(i,j,k-1,n), &
!diag th3d(i,j,k-1,n)+thbase,theta(i,j,k-1)+thbase
!diag write(lp,'(a,i3,f8.5,5f10.5)') &
!diag 'hybgen, overturn:', &
!diag k, q,temp(i,j,k, n),saln(i,j,k, n), &
!diag th3d(i,j,k, n)+thbase,theta(i,j,k )+thbase
!diag call flush(lp)
!diag endif !debug
endif
!
k = kp !at least 2
ka = max(k-2,1) !k might be 2
!
if (lunmix .and. & !usually .true.
k.gt.fixlay+1 .and. qdep.eq.1.0 .and. & !layer not fixed depth
p(i,j,k)-p(i,j,k-1).ge.dpthin .and. & !layer above not too thin
theta(i,j,k)-epsil.gt.th3d(i,j,k, n) .and. &
theta(i,j,k-1) .lt.th3d(i,j,k, n) .and. &
abs(theta(i,j,k-1)- th3d(i,j,k-1,n)).lt.hybiso .and. &
( th3d(i,j,k,n)- th3d(i,j,k-1,n)).gt. &
(theta(i,j,k) - theta(i,j,k-1) )*0.001 ) then
!
! --- water in the deepest inflated layer with significant thickness
! --- (kp) is too light, with the layer above near-isopycnal
! ---
! --- split layer into 2 sublayers, one near the desired density
! --- and one exactly matching the T&S properties of layer k-1.
! --- To prevent "runaway" T or S, the result satisfies either
! --- abs(T.k - T.k-1) <= abs(T.k-N - T.k-1) or
! --- abs(S.k - S.k-1) <= abs(S.k-N - S.k-1) where
! --- th3d.k-1 - th3d.k-N is at least theta(k-1) - theta(k-2)
! --- It is also limited to a 50% change in layer thickness.
!
!diag if (i.eq.itest .and. j.eq.jtest) then
!diag write(lp,'(a,i3)') &
!diag 'hybgen, deepest inflated layer too light (stable):',k
!diag call flush(lp)
!diag endif !debug
!
ka = 1
do ktr= k-2,2,-1
if ( th3d(i,j,k-1,n)- th3d(i,j,ktr,n).ge. &
theta(i,j,k-1) -theta(i,j,k-2) ) then
ka = ktr !usually k-2
exit
endif
enddo !ktr
!
delsm=abs(saln(i,j,ka, n)-saln(i,j,k-1,n))
dels =abs(saln(i,j,k-1,n)-saln(i,j,k, n))
deltm=abs(temp(i,j,ka, n)-temp(i,j,k-1,n))
delt =abs(temp(i,j,k-1,n)-temp(i,j,k, n))
! --- sanity check on deltm and delsm
q=min(temp(i,j,ka, n),temp(i,j,k-1,n),temp(i,j,k,n))
if (q.gt. 6.0) then
deltm=min( deltm, 6.0*(theta(i,j,k)-theta(i,j,k-1)) )
else !(q.le. 6.0)
deltm=min( deltm, 10.0*(theta(i,j,k)-theta(i,j,k-1)) )
endif
delsm=min( delsm, 1.3*(theta(i,j,k)-theta(i,j,k-1)) )
qts=0.0
if (delt.gt.epsil) then
qts=max(qts, (min(deltm, 2.0*delt)-delt)/delt) ! qts<=1.0
endif
if (dels.gt.epsil) then
qts=max(qts, (min(delsm, 2.0*dels)-dels)/dels) ! qts<=1.0
endif
q=(theta(i,j,k)-th3d(i,j,k, n))/ &
(theta(i,j,k)-th3d(i,j,k-1,n))
q=min(q,qts/(1.0+qts)) ! upper sublayer <= 50% of total
q=qhrlx(k)*q
! --- qhrlx is relaxation coefficient (inverse baroclinic time steps)
p_hat=q*(p(i,j,k+1)-p(i,j,k))
p(i,j,k)=p(i,j,k)+p_hat
if (hybflg.eq.0) then !T&S
temp(i,j,k,n)=temp(i,j,k,n)+(q/(1.0-q))*(temp(i,j,k,n) - &
temp(i,j,k-1,n) )
saln(i,j,k,n)=saln(i,j,k,n)+(q/(1.0-q))*(saln(i,j,k,n) - &
saln(i,j,k-1,n) )
th3d(i,j,k,n)=sig(temp(i,j,k,n),saln(i,j,k,n))-thbase
elseif (hybflg.eq.1) then !th&S
th3d(i,j,k,n)=th3d(i,j,k,n)+(q/(1.0-q))*(th3d(i,j,k,n) - &
th3d(i,j,k-1,n) )
saln(i,j,k,n)=saln(i,j,k,n)+(q/(1.0-q))*(saln(i,j,k,n) - &
saln(i,j,k-1,n) )
temp(i,j,k,n)=tofsig(th3d(i,j,k,n)+thbase,saln(i,j,k,n))
elseif (hybflg.eq.2) then !th&T
th3d(i,j,k,n)=th3d(i,j,k,n)+(q/(1.0-q))*(th3d(i,j,k,n) - &
th3d(i,j,k-1,n) )
temp(i,j,k,n)=temp(i,j,k,n)+(q/(1.0-q))*(temp(i,j,k,n) - &
temp(i,j,k-1,n) )
saln(i,j,k,n)=sofsig(th3d(i,j,k,n)+thbase,temp(i,j,k,n))
endif
if (ntracr.gt.0 .and. p_hat.ne.0.0) then
if (ndebug_tracer.gt.0 .and. ndebug_tracer.le.ntracr .and. &
i.eq.itest .and. j.eq.jtest) then
ktr = ndebug_tracer
write(lp,'(a,i3,f6.3,f9.4)') &
'hybgen, 10(+):', &
k-1,0.0,tracer(i,j,k-1,n,ktr)
call flush(lp)
endif !debug_tracer
! --- fraction of new upper layer from old lower layer
qtr=p_hat/max(p_hat,p(i,j,k)-p(i,j,k-1)) !between 0 and 1
do ktr= 1,ntracr
if (trcflg(ktr).eq.2) then !temperature tracer
tracer(i,j,k,n,ktr)=tracer(i,j,k,n,ktr)+ &
(q/(1.0-q))*(tracer(i,j,k, n,ktr)- &
tracer(i,j,k-1,n,ktr))
else !standard tracer - not split into two sub-layers
tracer(i,j,k-1,n,ktr)=tracer(i,j,k-1,n,ktr)+ &
qtr*(tracer(i,j,k, n,ktr)- &
tracer(i,j,k-1,n,ktr))
!diag if (i.eq.itest .and. j.eq.jtest) then
!diag write(lp,'(a,i4,i3,5e12.3)') &
!diag 'hybgen, 10(+):', &
!diag k,ktr,p_hat,p(i,j,k),p(i,j,k-1), &
!diag qtr,tracer(i,j,k-1,n,ktr)
!diag call flush(lp)
!diag endif !debug
endif !trcflg
enddo !ktr
if (ndebug_tracer.gt.0 .and. ndebug_tracer.le.ntracr .and. &
i.eq.itest .and. j.eq.jtest) then
ktr = ndebug_tracer
write(lp,'(a,i3,f6.3,f9.4)') &
'hybgen, 10(+):', &
k-1,qtr,tracer(i,j,k-1,n,ktr)
write(lp,'(a,i3,f6.3,f9.4)') &
'hybgen, 10(+):', &
k,qtr,tracer(i,j,k,n,ktr)
write(lp,'(a,i3)') &
'hybgen, deepest inflated layer:',kp
call flush(lp)
endif !debug_tracer
endif !tracers
if (mxlmy .and. p_hat.ne.0.0) then
! --- fraction of new upper layer from old lower layer
qtr=p_hat/max(p_hat,p(i,j,k)-p(i,j,k-1)) !between 0 and 1
q2( i,j,k-1,n)=q2( i,j,k-1,n)+ &
qtr*(q2( i,j,k,n)-q2( i,j,k-1,n))
q2l(i,j,k-1,n)=q2l(i,j,k-1,n)+ &
qtr*(q2l(i,j,k,n)-q2l(i,j,k-1,n))
!diag if (i.eq.itest .and. j.eq.jtest) then
!diag write(lp,'(a,i4,i3,6e12.3)') &
!diag 'hybgen, 10(+):', &
!diag k,0,p_hat,p(i,j,k)-p(i,j,k-1),p(i,j,k+1)-p(i,j,k), &
!diag qtr,q2(i,j,k-1,n),q2l(i,j,k-1,n)
!diag call flush(lp)
!diag endif !debug
endif
!diag if (i.eq.itest .and. j.eq.jtest) then
!diag write(lp,'(a,i3,f6.3,5f8.3)') &
!diag 'hybgen, 10(+):', &
!diag k,q,temp(i,j,k,n),saln(i,j,k,n), &
!diag th3d(i,j,k,n)+thbase,theta(i,j,k)+thbase
!diag call flush(lp)
!diag endif !debug
!diag if (i.eq.itest .and. j.eq.jtest) then
!diag write(lp,'(a,i3,f6.3,5f8.3)') &
!diag 'hybgen, 10(-):', &
!diag k,0.0,temp(i,j,k,n),saln(i,j,k,n), &
!diag th3d(i,j,k,n)+thbase,theta(i,j,k)+thbase
!diag call flush(lp)
!diag endif !debug
endif !too light
!
! --- massless or near-massless (thickness < dpthin) layers
!
do k=kp+1,kk
if (k.le.nhybrd) then
! --- fill thin and massless layers on sea floor with fluid from above
th3d(i,j,k,n)=th3d(i,j,k-1,n)
saln(i,j,k,n)=saln(i,j,k-1,n)
temp(i,j,k,n)=temp(i,j,k-1,n)
elseif (th3d(i,j,k,n).ne.theta(i,j,k)) then
if (hybflg.ne.2) then
! --- fill with saln from above
th3d(i,j,k,n)=max(theta(i,j,k), th3d(i,j,k-1,n))
saln(i,j,k,n)=saln(i,j,k-1,n)
temp(i,j,k,n)=tofsig(th3d(i,j,k,n)+thbase,saln(i,j,k,n))
saln(i,j,k,n)=sofsig(th3d(i,j,k,n)+thbase,temp(i,j,k,n))
else
! --- fill with temp from above
th3d(i,j,k,n)=max(theta(i,j,k), th3d(i,j,k-1,n))
temp(i,j,k,n)=temp(i,j,k-1,n)
saln(i,j,k,n)=sofsig(th3d(i,j,k,n)+thbase,temp(i,j,k,n))
endif
endif
do ktr= 1,ntracr
tracer(i,j,k,n,ktr)=tracer(i,j,k-1,n,ktr)
enddo !ktr
if (ndebug_tracer.gt.0 .and. ndebug_tracer.le.ntracr .and. &
i.eq.itest .and. j.eq.jtest) then
ktr = ndebug_tracer
write(lp,'(a,i3,f9.4)') &
'hybgen, massless:', &
k,tracer(i,j,k,n,ktr)
call flush(lp)
endif !debug_tracer
if (mxlmy) then
q2 (i,j,k,n)=q2( i,j,k-1,n)
q2l(i,j,k,n)=q2l(i,j,k-1,n)
endif
enddo !k
!
! --- store one-dimensional arrays of -temp-, -saln-, and -p- for the 'old'
! --- vertical grid before attempting to restore isopycnic conditions
pres(1)=p(i,j,1)
do k=1,kk
if (hybflg.eq.0) then !T&S
s1d(k,1) = temp(i,j,k,n)
s1d(k,2) = saln(i,j,k,n)
elseif (hybflg.eq.1) then !th&S
s1d(k,1) = th3d(i,j,k,n)
s1d(k,2) = saln(i,j,k,n)
elseif (hybflg.eq.2) then !th&T
s1d(k,1) = th3d(i,j,k,n)
s1d(k,2) = temp(i,j,k,n)
endif
do ktr= 1,ntracr
s1d(k,2+ktr) = tracer(i,j,k,n,ktr)
enddo !ktr
if (mxlmy) then
s1d(k,ntracr+3) = q2( i,j,k,n)
s1d(k,ntracr+4) = q2l(i,j,k,n)
endif
pres(k+1)=p(i,j,k+1)
dprs(k) =pres(k+1)-pres(k)
dpi( k) =max(dprs(k),dpthin)
!
if (isopcm) then
if (k.le.fixlay) then
lcm(k) = .false. !fixed layers are never PCM
else
! --- thin and isopycnal layers remapped with PCM.
lcm(k) = k.gt.nhybrd &
.or. dprs(k).le.dpthin &
.or. abs(th3d(i,j,k,n)-theta(i,j,k)).lt.hybiso
endif !k<=fixlay:else
endif !isopcm
enddo !k
!
! --- try to restore isopycnic conditions by moving layer interfaces
! --- qhrlx(k) are relaxation coefficients (inverse baroclinic time steps)
!
if (fixlay.ge.1) then
!
! --- maintain constant thickness, layer k = 1
k=1
p_hat=p(i,j,k)+dp0ij(k)
p(i,j,k+1)=p_hat
do k=2,kk
if (p(i,j,k+1).ge.p_hat) then
exit ! usually get here quickly
endif
p(i,j,k+1)=p_hat
enddo !k
endif
!
do k=2,nhybrd
!
!diag if (i.eq.itest .and. j.eq.jtest) then
!diag write(cinfo,'(a9,i2.2,1x)') ' do 88 k=',k
!diag write(lp,'(i9,2i5,a,a)') nstep,itest+i0,jtest+j0, &
!diag cinfo,': othkns odpth nthkns ndpth'
!diag do ka=1,kk
!diag if (pres(ka+1).eq.p(itest,jtest,ka+1) .and. &
!diag pres(ka ).eq.p(itest,jtest,ka ) ) then
!diag write(lp,'(i9,8x,a,a,i3,f10.3,f9.3)') &
!diag nstep,cinfo,':',ka, &
!diag (pres(ka+1)- &
!diag pres(ka) )*qonem, &
!diag pres(ka+1) *qonem
!diag else
!diag write(lp,'(i9,8x,a,a,i3,f10.3,f9.3,f10.3,f9.3)') &
!diag nstep,cinfo,':',ka, &
!diag (pres(ka+1)- &
!diag pres(ka) )*qonem, &
!diag pres(ka+1) *qonem, &
!diag (p(itest,jtest,ka+1)- &
!diag p(itest,jtest,ka) )*qonem, &
!diag p(itest,jtest,ka+1) *qonem
!diag endif
!diag enddo !ka
!diag call flush(lp)
!diag endif !debug
!
if (k.le.fixlay) then
!
! --- maintain constant thickness, k <= fixlay
if (k.lt.kk) then !p.kk+1 not changed
p(i,j,k+1)=min(dp0cum(k+1),p(i,j,kk+1))
if (k.eq.fixlay) then
! --- enforce interface order (may not be necessary).
do ka= k+2,kk
if (p(i,j,ka).ge.p(i,j,k+1)) then
exit ! usually get here quickly
else
p(i,j,ka) = p(i,j,k+1)
endif
enddo !ka
endif !k.eq.fixlay
endif !k.lt.kk
!
!diag if (i.eq.itest .and. j.eq.jtest) then
!diag write(lp,'(a,i3.2,f8.2)') 'hybgen, fixlay :', &
!diag k+1,p(i,j,k+1)*qonem
!diag call flush(lp)
!diag endif !debug
else
!
! --- do not maintain constant thickness, k > fixlay
!
if (th3d(i,j,k,n).gt.theta(i,j,k)+epsil .and. &
k.gt.fixlay+1) then
!
! --- water in layer k is too dense
! --- try to dilute with water from layer k-1
! --- do not move interface if k = fixlay + 1
!
if (th3d(i,j,k-1,n).ge.theta(i,j,k-1) .or. &
p(i,j,k).le.dp0cum(k)+onem .or. &
p(i,j,k+1)-p(i,j,k).le.p(i,j,k)-p(i,j,k-1)) then
!
! --- if layer k-1 is too light, thicken the thinner of the two,
! --- i.e. skip this layer if it is thicker.
!
!diag if (i.eq.itest .and. j.eq.jtest) then
!diag write(lp,'(a,3x,i2.2,1pe13.5)') &
!diag 'hybgen, too dense:',k,th3d(i,j,k,n)-theta(i,j,k)
!diag call flush(lp)
!diag endif !debug
!
if ((theta(i,j,k)-th3d(i,j,k-1,n)).le.epsil) then
! layer k-1 much too dense, take entire layer
p_hat=p(i,j,k-1)+dp0ij(k-1)
else
q=(theta(i,j,k)-th3d(i,j,k, n))/ &
(theta(i,j,k)-th3d(i,j,k-1,n)) ! -1 <= q < 0
p_hat0=p(i,j,k)+q*(p(i,j,k+1)-p(i,j,k)) ! <p(i,j,k)
if (k.eq.fixlay+2) then
! --- treat layer k-1 as fixed.
p_hat =p(i,j,k-1)+ max(p_hat0-p(i,j,k-1),dp0ij(k-1))
else
! --- maintain minimum thickess of layer k-1.
p_hat =p(i,j,k-1)+cushn(p_hat0-p(i,j,k-1),dp0ij(k-1))
endif !fixlay+2:else
end if
p_hat=min(p_hat,p(i,j,kk+1))
!
! --- if isopycnic conditions cannot be achieved because of a blocking
! --- layer in the interior ocean, move interface k-1 (and k-2 if
! --- necessary) upward
!
if (k.le.fixlay+2) then
! --- do nothing.
else if (p_hat.ge.p(i,j,k) .and. &
p(i,j,k-1).gt.dp0cum(k-1)+tenm .and. &
(p(i,j,kk+1)-p(i,j,k-1).lt.thkbop .or. &
p(i,j,k-1) -p(i,j,k-2).gt.qqmx*dp0ij(k-2))) then ! k.gt.2
if (k.eq.fixlay+3) then
! --- treat layer k-2 as fixed.
p_hat2=p(i,j,k-2)+ &
max(p(i,j,k-1)-p_hat+p_hat0-p(i,j,k-2), &
dp0ij(k-2))
else
! --- maintain minimum thickess of layer k-2.
p_hat2=p(i,j,k-2)+ &
cushn(p(i,j,k-1)-p_hat+p_hat0-p(i,j,k-2), &
dp0ij(k-2))
endif !fixlay+3:else
if (p_hat2.lt.p(i,j,k-1)-onemm) then
p(i,j,k-1)=(1.0-qhrlx(k-1))*p(i,j,k-1) + &
qhrlx(k-1) *max(p_hat2, &
2.0*p(i,j,k-1)-p_hat)
!diag if (i.eq.itest .and. j.eq.jtest) then
!diag write(lp,'(a,i3.2,f8.2)') 'hybgen, 1blocking :', &
!diag k-1,p(i,j,k-1)*qonem
!diag call flush(lp)
!diag endif !debug
p_hat=p(i,j,k-1)+cushn(p_hat0-p(i,j,k-1),dp0ij(k-1))
elseif (k.le.fixlay+3) then
! --- do nothing.
elseif (p(i,j,k-2).gt.dp0cum(k-2)+tenm .and. &
(p(i,j,kk+1)-p(i,j,k-2).lt.thkbop .or. &