-
Notifications
You must be signed in to change notification settings - Fork 237
/
gmredi_slope_psi.F
429 lines (374 loc) · 12.8 KB
/
gmredi_slope_psi.F
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
#include "GMREDI_OPTIONS.h"
#ifdef ALLOW_AUTODIFF
# include "AUTODIFF_OPTIONS.h"
#endif
CBOP
C !ROUTINE: GMREDI_SLOPE_PSI
C !INTERFACE:
SUBROUTINE GMREDI_SLOPE_PSI(
O taperX, taperY,
U SlopeX, SlopeY,
U dSigmaDrW, dSigmaDrS,
I LrhoW, LrhoS, rDepth, k,
I bi, bj, myThid )
C !DESCRIPTION: \bv
C *==========================================================*
C | SUBROUTINE GMREDI_SLOPE_PSI |
C | o Calculate slopes for use in GM advective form |
C *==========================================================*
C | On entry: |
C | dSigmaDrW,S contains the -d/dz Sigma if Z-coords |
C | but d/dp Sigma if P-coords |
C | SlopeX/Y contains X/Y gradients of sigma |
C | rDepth depth (> 0) in r-Unit from the surface |
C | On exit: |
C | dSigmaDrW,S contains the effective dSig/dz |
C | SlopeX/Y contains X/Y slopes |
C | taperFct contains tapering funct. value ; |
C | = 1 when using no tapering |
C *==========================================================*
C \ev
C !USES:
IMPLICIT NONE
C == Global variables ==
#include "SIZE.h"
#include "EEPARAMS.h"
#include "GMREDI.h"
#include "PARAMS.h"
#ifdef ALLOW_AUTODIFF_TAMC
# include "tamc.h"
#endif /* ALLOW_AUTODIFF_TAMC */
C !INPUT/OUTPUT PARAMETERS:
_RL taperX(1-OLx:sNx+OLx,1-OLy:sNy+OLy)
_RL taperY(1-OLx:sNx+OLx,1-OLy:sNy+OLy)
_RL SlopeX(1-OLx:sNx+OLx,1-OLy:sNy+OLy)
_RL SlopeY(1-OLx:sNx+OLx,1-OLy:sNy+OLy)
_RL dSigmaDrW(1-OLx:sNx+OLx,1-OLy:sNy+OLy)
_RL dSigmaDrS(1-OLx:sNx+OLx,1-OLy:sNy+OLy)
_RL LrhoW(1-OLx:sNx+OLx,1-OLy:sNy+OLy)
_RL LrhoS(1-OLx:sNx+OLx,1-OLy:sNy+OLy)
_RL rDepth
INTEGER k,bi,bj,myThid
#ifdef ALLOW_GMREDI
#ifdef GM_BOLUS_ADVEC
C !LOCAL VARIABLES:
_RL dSigmaDrLtd(1-OLx:sNx+OLx,1-OLy:sNy+OLy)
_RL f1, Smod, f2, Rnondim
_RL maxSlopeSqr
_RL slopeCutoff
_RL loc_maxSlope, loc_rMaxSlope
_RL fpi
PARAMETER( fpi = PI )
INTEGER i, j
#ifdef GMREDI_WITH_STABLE_ADJOINT
_RL slopeMaxSpec
#endif
#ifdef ALLOW_AUTODIFF_TAMC
C kkey :: tape key (depends on levels and tiles)
INTEGER kkey
#endif
CEOP
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----|
slopeCutoff = SQRT( GM_slopeSqCutoff )
C- Note regarding slopeCutoff: current code by-passes the reduction of taper
C fct when slope is larger than "slopeCutoff", and this is not very safe !
C However, with default GM_Small_Number (=1.E-20) and default value of
C GM_slopeSqCutoff (=1.E+48), this is unlikely to occur as it would require
C |sigmaX/Y| > 1.e+4
loc_maxSlope = GM_maxSlope*wUnit2rVel(k)
loc_rMaxSlope = GM_rMaxSlope*rVel2wUnit(k)
#ifdef ALLOW_AUTODIFF_TAMC
kkey = bi + (bj-1)*nSx + (ikey_dynamics-1)*nSx*nSy
kkey = k + (kkey-1)*Nr
#endif /* ALLOW_AUTODIFF_TAMC */
IF (GM_taper_scheme.EQ.'orig' .OR.
& GM_taper_scheme.EQ.'clipping') THEN
#ifdef GM_EXCLUDE_CLIPPING
STOP 'Need to compile without "#define GM_EXCLUDE_CLIPPING"'
#else /* GM_EXCLUDE_CLIPPING */
C- Original implementation in mitgcmuv
C (this turns out to be the same as Cox slope clipping)
C-- X-comp
#ifdef ALLOW_AUTODIFF
DO j=1-OLy,sNy+OLy
DO i=1-OLx+1,sNx+OLx
dSigmaDrLtd(i,j) = 0. _d 0
ENDDO
ENDDO
#endif /* ALLOW_AUTODIFF */
C- Cox 1987 "Slope clipping"
DO j=1-OLy,sNy+OLy
DO i=1-OLx+1,sNx+OLx
dSigmaDrLtd(i,j) = GM_Small_Number
& + ABS(SlopeX(i,j))*loc_rMaxSlope
ENDDO
ENDDO
#ifdef ALLOW_AUTODIFF_TAMC
CADJ STORE dSigmaDrLtd(:,:) = comlev1_bibj_k, key=kkey, byte=isbyte
CADJ STORE dSigmaDrW(:,:) = comlev1_bibj_k, key=kkey, byte=isbyte
#endif
DO j=1-OLy,sNy+OLy
DO i=1-OLx+1,sNx+OLx
IF (dSigmaDrW(i,j).LE.dSigmaDrLtd(i,j))
& dSigmaDrW(i,j) = dSigmaDrLtd(i,j)
ENDDO
ENDDO
#ifdef ALLOW_AUTODIFF_TAMC
CADJ STORE dSigmaDrW(:,:) = comlev1_bibj_k, key=kkey, byte=isbyte
#endif
DO j=1-OLy,sNy+OLy
DO i=1-OLx+1,sNx+OLx
SlopeX(i,j) = SlopeX(i,j)/dSigmaDrW(i,j)
taperX(i,j) = 1. _d 0
ENDDO
ENDDO
C-- Y-comp
#ifdef ALLOW_AUTODIFF
DO j=1-OLy+1,sNy+OLy
DO i=1-OLx,sNx+OLx
dSigmaDrLtd(i,j) = 0. _d 0
ENDDO
ENDDO
#endif /* ALLOW_AUTODIFF */
DO j=1-OLy+1,sNy+OLy
DO i=1-OLx,sNx+OLx
dSigmaDrLtd(i,j) = GM_Small_Number
& + ABS(SlopeY(i,j))*loc_rMaxSlope
ENDDO
ENDDO
#ifdef ALLOW_AUTODIFF_TAMC
CADJ STORE dSigmaDrLtd(:,:) = comlev1_bibj_k, key=kkey, byte=isbyte
CADJ STORE dSigmaDrS(:,:) = comlev1_bibj_k, key=kkey, byte=isbyte
#endif
DO j=1-OLy+1,sNy+OLy
DO i=1-OLx,sNx+OLx
IF (dSigmaDrS(i,j).LE.dSigmaDrLtd(i,j))
& dSigmaDrS(i,j) = dSigmaDrLtd(i,j)
ENDDO
ENDDO
#ifdef ALLOW_AUTODIFF_TAMC
CADJ STORE dSigmaDrS(:,:) = comlev1_bibj_k, key=kkey, byte=isbyte
#endif
DO j=1-OLy+1,sNy+OLy
DO i=1-OLx,sNx+OLx
SlopeY(i,j) = SlopeY(i,j)/dSigmaDrS(i,j)
taperY(i,j) = 1. _d 0
ENDDO
ENDDO
#endif /* GM_EXCLUDE_CLIPPING */
ELSEIF (GM_taper_scheme.EQ.'fm07') THEN
STOP 'GMREDI_SLOPE_PSI: AdvForm not yet implemented for fm07'
ELSE
#ifdef GM_EXCLUDE_TAPERING
STOP 'Need to compile without "#define GM_EXCLUDE_TAPERING"'
#else /* GM_EXCLUDE_TAPERING */
#ifdef ALLOW_AUTODIFF_TAMC
CADJ STORE slopeX(:,:) = comlev1_bibj_k, key=kkey, byte=isbyte
CADJ STORE dSigmaDrW(:,:) = comlev1_bibj_k, key=kkey, byte=isbyte
#endif
C- Compute the slope, no clipping, but avoid reverse slope in negatively
C stratified (dSigmaDr < 0) region :
DO j=1-OLy,sNy+OLy
DO i=1-OLx+1,sNx+OLx
IF (dSigmaDrW(i,j).LE.GM_Small_Number)
& dSigmaDrW(i,j) = GM_Small_Number
ENDDO
ENDDO
#ifdef ALLOW_AUTODIFF_TAMC
CADJ STORE dsigmaDrW(:,:) = comlev1_bibj_k, key=kkey, byte=isbyte
#endif
DO j=1-OLy,sNy+OLy
DO i=1-OLx+1,sNx+OLx
SlopeX(i,j) = SlopeX(i,j)/dSigmaDrW(i,j)
taperX(i,j) = 1. _d 0
ENDDO
ENDDO
#ifdef ALLOW_AUTODIFF_TAMC
CADJ STORE slopeX(:,:) = comlev1_bibj_k, key=kkey, byte=isbyte
#endif
IF (GM_taper_scheme.NE.'stableGmAdjTap') THEN
DO j=1-OLy,sNy+OLy
DO i=1-OLx+1,sNx+OLx
IF ( ABS(SlopeX(i,j)) .GE. slopeCutoff ) THEN
SlopeX(i,j) = SIGN(slopeCutoff,SlopeX(i,j))
taperX(i,j) = 0. _d 0
ENDIF
ENDDO
ENDDO
ENDIF
#ifdef ALLOW_AUTODIFF_TAMC
CADJ STORE slopeY(:,:) = comlev1_bibj_k, key=kkey, byte=isbyte
CADJ STORE dSigmaDrS(:,:) = comlev1_bibj_k, key=kkey, byte=isbyte
#endif
DO j=1-OLy+1,sNy+OLy
DO i=1-OLx,sNx+OLx
IF (dSigmaDrS(i,j).LE.GM_Small_Number)
& dSigmaDrS(i,j) = GM_Small_Number
ENDDO
ENDDO
#ifdef ALLOW_AUTODIFF_TAMC
CADJ STORE dsigmaDrS(:,:) = comlev1_bibj_k, key=kkey, byte=isbyte
#endif
DO j=1-OLy+1,sNy+OLy
DO i=1-OLx,sNx+OLx
SlopeY(i,j) = SlopeY(i,j)/dSigmaDrS(i,j)
taperY(i,j) = 1. _d 0
ENDDO
ENDDO
#ifdef ALLOW_AUTODIFF_TAMC
CADJ STORE slopeY(:,:) = comlev1_bibj_k, key=kkey, byte=isbyte
#endif
IF (GM_taper_scheme.NE.'stableGmAdjTap') THEN
DO j=1-OLy+1,sNy+OLy
DO i=1-OLx,sNx+OLx
IF ( ABS(SlopeY(i,j)) .GE. slopeCutoff ) THEN
SlopeY(i,j) = SIGN(slopeCutoff,SlopeY(i,j))
taperY(i,j) = 0. _d 0
ENDIF
ENDDO
ENDDO
ENDIF
C- Compute the tapering function for the GM+Redi tensor :
#ifdef ALLOW_AUTODIFF_TAMC
CADJ STORE slopeX(:,:) = comlev1_bibj_k, key=kkey, byte=isbyte
CADJ STORE slopeY(:,:) = comlev1_bibj_k, key=kkey, byte=isbyte
#endif
IF (GM_taper_scheme.EQ.'linear') THEN
C- Simplest adiabatic tapering = Smax/Slope (linear)
DO j=1-OLy,sNy+OLy
DO i=1-OLx+1,sNx+OLx
Smod = ABS(SlopeX(i,j))
IF ( Smod .GT. loc_maxSlope .AND.
& Smod .LT. slopeCutoff )
& taperX(i,j) = loc_maxSlope/(Smod+GM_Small_Number)
ENDDO
ENDDO
DO j=1-OLy+1,sNy+OLy
DO i=1-OLx,sNx+OLx
Smod = ABS(SlopeY(i,j))
IF ( Smod .GT. loc_maxSlope .AND.
& Smod .LT. slopeCutoff )
& taperY(i,j) = loc_maxSlope/(Smod+GM_Small_Number)
ENDDO
ENDDO
ELSEIF ( GM_taper_scheme.EQ.'gkw91' .OR.
& GM_taper_scheme.EQ.'ac02' ) THEN
C- Gerdes, Koberle and Willebrand, Clim. Dyn. 1991
maxSlopeSqr = loc_maxSlope*loc_maxSlope
DO j=1-OLy,sNy+OLy
DO i=1-OLx+1,sNx+OLx
Smod = ABS(SlopeX(i,j))
IF ( Smod .GT. loc_maxSlope .AND.
& Smod .LT. slopeCutoff )
& taperX(i,j) = maxSlopeSqr/
& ( SlopeX(i,j)*SlopeX(i,j) + GM_Small_Number )
ENDDO
ENDDO
DO j=1-OLy+1,sNy+OLy
DO i=1-OLx,sNx+OLx
Smod = ABS(SlopeY(i,j))
IF ( Smod .GT. loc_maxSlope .AND.
& Smod .LT. slopeCutoff )
& taperY(i,j) = maxSlopeSqr/
& ( SlopeY(i,j)*SlopeY(i,j) + GM_Small_Number )
ENDDO
ENDDO
ELSEIF (GM_taper_scheme.EQ.'dm95') THEN
C- Danabasoglu and McWilliams, J. Clim. 1995
DO j=1-OLy,sNy+OLy
DO i=1-OLx+1,sNx+OLx
Smod = ABS(SlopeX(i,j))*rVel2wUnit(k)
taperX(i,j) = op5*( oneRL + TANH( (GM_Scrit-Smod)/GM_Sd ))
ENDDO
ENDDO
DO j=1-OLy+1,sNy+OLy
DO i=1-OLx,sNx+OLx
Smod = ABS(SlopeY(i,j))*rVel2wUnit(k)
taperY(i,j) = op5*( oneRL + TANH( (GM_Scrit-Smod)/GM_Sd ))
ENDDO
ENDDO
ELSEIF (GM_taper_scheme.EQ.'ldd97') THEN
C- Large, Danabasoglu and Doney, JPO 1997
DO j=1-OLy,sNy+OLy
DO i=1-OLx+1,sNx+OLx
Smod = ABS(SlopeX(i,j))
IF ( Smod .LT. slopeCutoff ) THEN
f1 = op5*( oneRL
& + TANH( (GM_Scrit-Smod*rVel2wUnit(k))/GM_Sd ) )
IF ( Smod.NE.zeroRL ) THEN
Rnondim = rDepth/( LrhoW(i,j)*Smod )
ELSE
Rnondim = 1. _d 0
ENDIF
IF ( Rnondim.GE.oneRL ) THEN
f2 = 1. _d 0
ELSE
f2 = op5*( oneRL + SIN( fpi*(Rnondim-op5) ))
ENDIF
taperX(i,j) = f1*f2
ENDIF
ENDDO
ENDDO
DO j=1-OLy+1,sNy+OLy
DO i=1-OLx,sNx+OLx
Smod = ABS(SlopeY(i,j))
IF ( Smod .LT. slopeCutoff ) THEN
f1 = op5*( oneRL
& + TANH( (GM_Scrit-Smod*rVel2wUnit(k))/GM_Sd ) )
IF ( Smod.NE.zeroRL ) THEN
Rnondim = rDepth/( LrhoS(i,j)*Smod )
ELSE
Rnondim = 1. _d 0
ENDIF
IF ( Rnondim.GE.oneRL ) THEN
f2 = 1. _d 0
ELSE
f2 = op5*( oneRL + SIN( fpi*(Rnondim-op5) ))
ENDIF
taperY(i,j) = f1*f2
ENDIF
ENDDO
ENDDO
ELSEIF (GM_taper_scheme.EQ.'stableGmAdjTap') THEN
#ifndef GMREDI_WITH_STABLE_ADJOINT
STOP 'Need to compile wth "#define GMREDI_WITH_STABLE_ADJOINT"'
#else /* GMREDI_WITH_STABLE_ADJOINT */
c special choice for adjoint/optimization of parameters
c (~ strong clipping, reducing non linearity of psi=f(K))
slopeMaxSpec=1. _d -4
#ifdef ALLOW_AUTODIFF_TAMC
CADJ STORE slopeX(:,:) = comlev1_bibj_k, key=kkey, byte=isbyte
CADJ STORE slopeY(:,:) = comlev1_bibj_k, key=kkey, byte=isbyte
#endif
DO j=1-OLy,sNy+OLy
DO i=1-OLx+1,sNx+OLx
Smod = ABS(SlopeX(i,j))
IF ( Smod .GT. slopeMaxSpec ) THEN
SlopeX(i,j) = 5.*SlopeX(i,j)*slopeMaxSpec/Smod
ELSE
SlopeX(i,j) = 5.*SlopeX(i,j)
ENDIF
taperX(i,j) = 1.
ENDDO
ENDDO
DO j=1-OLy+1,sNy+OLy
DO i=1-OLx,sNx+OLx
Smod = ABS(SlopeY(i,j))
IF ( Smod .GT. slopeMaxSpec ) THEN
SlopeY(i,j) = 5.*SlopeY(i,j)*slopeMaxSpec/Smod
ELSE
SlopeY(i,j) = 5.*SlopeY(i,j)
ENDIF
taperY(i,j) = 1.
ENDDO
ENDDO
#endif /* GMREDI_WITH_STABLE_ADJOINT */
ELSEIF (GM_taper_scheme.NE.' ') THEN
STOP 'GMREDI_SLOPE_PSI: Bad GM_taper_scheme'
ENDIF
#endif /* GM_EXCLUDE_TAPERING */
ENDIF
#endif /* BOLUS_ADVEC */
#endif /* ALLOW_GMREDI */
RETURN
END