New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
zdfosm.F90 in NEMO/branches/2019/dev_r11078_OSMOSIS_IMMERSE_Nurser_4.0/src/OCE/ZDF – NEMO

source: NEMO/branches/2019/dev_r11078_OSMOSIS_IMMERSE_Nurser_4.0/src/OCE/ZDF/zdfosm.F90 @ 13399

Last change on this file since 13399 was 13399, checked in by agn, 4 years ago

average utau and vtau onto T-points

  • Property svn:keywords set to Id
File size: 128.0 KB
Line 
1MODULE zdfosm
2   !!======================================================================
3   !!                       ***  MODULE  zdfosm  ***
4   !! Ocean physics:  vertical mixing coefficient compute from the OSMOSIS
5   !!                 turbulent closure parameterization
6   !!=====================================================================
7   !!  History : NEMO 4.0  ! A. Grant, G. Nurser
8   !! 15/03/2017  Changed calculation of pycnocline thickness in unstable conditions and stable conditions AG
9   !! 15/03/2017  Calculation of pycnocline gradients for stable conditions changed. Pycnocline gradients now depend on stability of the OSBL. A.G
10   !! 06/06/2017  (1) Checks on sign of buoyancy jump in calculation of  OSBL depth.  A.G.
11   !!             (2) Removed variable zbrad0, zbradh and zbradav since they are not used.
12   !!             (3) Approximate treatment for shear turbulence.
13   !!                        Minimum values for zustar and zustke.
14   !!                        Add velocity scale, zvstr, that tends to zustar for large Langmuir numbers.
15   !!                        Limit maximum value for Langmuir number.
16   !!                        Use zvstr in definition of stability parameter zhol.
17   !!             (4) Modified parametrization of entrainment flux, changing original coefficient 0.0485 for Langmuir contribution to 0.135 * zla
18   !!             (5) For stable boundary layer add factor that depends on length of timestep to 'slow' collapse and growth. Make sure buoyancy jump not negative.
19   !!             (6) For unstable conditions when growth is over multiple levels, limit change to maximum of one level per cycle through loop.
20   !!             (7) Change lower limits for loops that calculate OSBL averages from 1 to 2. Large gradients between levels 1 and 2 can cause problems.
21   !!             (8) Change upper limits from ibld-1 to ibld.
22   !!             (9) Calculation of pycnocline thickness in unstable conditions. Check added to ensure that buoyancy jump is positive before calculating Ri.
23   !!            (10) Thickness of interface layer at base of the stable OSBL set by Richardson number. Gives continuity in transition from unstable OSBL.
24   !!            (11) Checks that buoyancy jump is poitive when calculating pycnocline profiles.
25   !!            (12) Replace zwstrl with zvstr in calculation of eddy viscosity.
26   !! 27/09/2017 (13) Calculate Stokes drift and Stokes penetration depth from wave information
27   !!            (14) Buoyancy flux due to entrainment changed to include contribution from shear turbulence.
28   !! 28/09/2017 (15) Calculation of Stokes drift moved into separate do-loops to allow for different options for the determining the Stokes drift to be added.
29   !!            (16) Calculation of Stokes drift from windspeed for PM spectrum (for testing, commented out)
30   !!            (17) Modification to Langmuir velocity scale to include effects due to the Stokes penetration depth (for testing, commented out)
31   !! ??/??/2018 (18) Revision to code structure, selected using key_osmldpth1. Inline code moved into subroutines. Changes to physics made,
32   !!                  (a) Pycnocline temperature and salinity profies changed for unstable layers
33   !!                  (b) The stable OSBL depth parametrization changed.
34   !! 16/05/2019 (19) Fox-Kemper parametrization of restratification through mixed layer eddies added to revised code.
35   !! 23/05/19   (20) Old code where key_osmldpth1` is *not* set removed, together with the key key_osmldpth1
36   !!----------------------------------------------------------------------
37
38   !!----------------------------------------------------------------------
39   !!   'ln_zdfosm'                                             OSMOSIS scheme
40   !!----------------------------------------------------------------------
41   !!   zdf_osm       : update momentum and tracer Kz from osm scheme
42   !!   zdf_osm_init  : initialization, namelist read, and parameters control
43   !!   osm_rst       : read (or initialize) and write osmosis restart fields
44   !!   tra_osm       : compute and add to the T & S trend the non-local flux
45   !!   trc_osm       : compute and add to the passive tracer trend the non-local flux (TBD)
46   !!   dyn_osm       : compute and add to u & v trensd the non-local flux
47   !!
48   !! Subroutines in revised code.
49   !!----------------------------------------------------------------------
50   USE oce            ! ocean dynamics and active tracers
51                      ! uses wn from previous time step (which is now wb) to calculate hbl
52   USE dom_oce        ! ocean space and time domain
53   USE zdf_oce        ! ocean vertical physics
54   USE sbc_oce        ! surface boundary condition: ocean
55   USE sbcwave        ! surface wave parameters
56   USE phycst         ! physical constants
57   USE eosbn2         ! equation of state
58   USE traqsr         ! details of solar radiation absorption
59   USE zdfddm         ! double diffusion mixing (avs array)
60   USE iom            ! I/O library
61   USE lib_mpp        ! MPP library
62   USE trd_oce        ! ocean trends definition
63   USE trdtra         ! tracers trends
64   !
65   USE in_out_manager ! I/O manager
66   USE lbclnk         ! ocean lateral boundary conditions (or mpp link)
67   USE prtctl         ! Print control
68   USE lib_fortran    ! Fortran utilities (allows no signed zero when 'key_nosignedzero' defined)
69
70   IMPLICIT NONE
71   PRIVATE
72
73   PUBLIC   zdf_osm       ! routine called by step.F90
74   PUBLIC   zdf_osm_init  ! routine called by nemogcm.F90
75   PUBLIC   osm_rst       ! routine called by step.F90
76   PUBLIC   tra_osm       ! routine called by step.F90
77   PUBLIC   trc_osm       ! routine called by trcstp.F90
78   PUBLIC   dyn_osm       ! routine called by step.F90
79
80   PUBLIC   ln_osm_mle    ! logical needed by tra_mle_init in tramle.F90
81
82   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:) ::   ghamu    !: non-local u-momentum flux
83   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:) ::   ghamv    !: non-local v-momentum flux
84   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:) ::   ghamt    !: non-local temperature flux (gamma/<ws>o)
85   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:) ::   ghams    !: non-local salinity flux (gamma/<ws>o)
86   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:) ::   etmean   !: averaging operator for avt
87   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   hbl      !: boundary layer depth
88   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   dh       ! depth of pycnocline
89   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   hml      ! ML depth
90   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   dstokes  !: penetration depth of the Stokes drift.
91
92   REAL(wp), ALLOCATABLE, SAVE, DIMENSION(:,:)           ::   r1_ft    ! inverse of the modified Coriolis parameter at t-pts
93   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   hmle     ! Depth of layer affexted by mixed layer eddies in Fox-Kemper parametrization
94   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   dbdx_mle ! zonal buoyancy gradient in ML
95   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   dbdy_mle ! meridional buoyancy gradient in ML
96   INTEGER,  PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   mld_prof ! level of base of MLE layer.
97
98   !                      !!** Namelist  namzdf_osm  **
99   LOGICAL  ::   ln_use_osm_la      ! Use namelist  rn_osm_la
100
101   LOGICAL  ::   ln_osm_mle           !: flag to activate the Mixed Layer Eddy (MLE) parameterisation
102
103   REAL(wp) ::   rn_osm_la          ! Turbulent Langmuir number
104   REAL(wp) ::   rn_osm_dstokes     ! Depth scale of Stokes drift
105   REAL(wp) ::   rn_osm_hbl0 = 10._wp       ! Initial value of hbl for 1D runs
106   INTEGER  ::   nn_ave             ! = 0/1 flag for horizontal average on avt
107   INTEGER  ::   nn_osm_wave = 0    ! = 0/1/2 flag for getting stokes drift from La# / PM wind-waves/Inputs into sbcwave
108   LOGICAL  ::   ln_dia_osm         ! Use namelist  rn_osm_la
109
110
111   LOGICAL  ::   ln_kpprimix  = .true.  ! Shear instability mixing
112   REAL(wp) ::   rn_riinfty   = 0.7     ! local Richardson Number limit for shear instability
113   REAL(wp) ::   rn_difri    =  0.005   ! maximum shear mixing at Rig = 0    (m2/s)
114   LOGICAL  ::   ln_convmix  = .true.   ! Convective instability mixing
115   REAL(wp) ::   rn_difconv = 1._wp     ! diffusivity when unstable below BL  (m2/s)
116
117! OSMOSIS mixed layer eddy parametrization constants
118   INTEGER  ::   nn_osm_mle             ! = 0/1 flag for horizontal average on avt
119   REAL(wp) ::   rn_osm_mle_ce           ! MLE coefficient
120   !                                        ! parameters used in nn_osm_mle = 0 case
121   REAL(wp) ::   rn_osm_mle_lf               ! typical scale of mixed layer front
122   REAL(wp) ::   rn_osm_mle_time             ! time scale for mixing momentum across the mixed layer
123   !                                        ! parameters used in nn_osm_mle = 1 case
124   REAL(wp) ::   rn_osm_mle_lat              ! reference latitude for a 5 km scale of ML front
125   LOGICAL  ::   ln_osm_hmle_limit           ! If true arbitrarily restrict hmle to rn_osm_hmle_limit*zmld
126   REAL(wp) ::   rn_osm_hmle_limit           ! If ln_osm_hmle_limit true arbitrarily restrict hmle to rn_osm_hmle_limit*zmld
127   REAL(wp) ::   rn_osm_mle_rho_c        ! Density criterion for definition of MLD used by FK
128   REAL(wp) ::   r5_21 = 5.e0 / 21.e0   ! factor used in mle streamfunction computation
129   REAL(wp) ::   rb_c                   ! ML buoyancy criteria = g rho_c /rau0 where rho_c is defined in zdfmld
130   REAL(wp) ::   rc_f                   ! MLE coefficient (= rn_ce / (5 km * fo) ) in nn_osm_mle=1 case
131   REAL(wp) ::   rn_osm_mle_thresh          ! Threshold buoyancy for deepening of MLE layer below OSBL base.
132   REAL(wp) ::   rn_osm_mle_tau             ! Adjustment timescale for MLE.
133
134
135   !                                    !!! ** General constants  **
136   REAL(wp) ::   epsln   = 1.0e-20_wp   ! a small positive number to ensure no div by zero
137   REAL(wp) ::   depth_tol = 1.0e-6_wp  ! a small-ish positive number to give a hbl slightly shallower than gdepw
138   REAL(wp) ::   pthird  = 1._wp/3._wp  ! 1/3
139   REAL(wp) ::   p2third = 2._wp/3._wp  ! 2/3
140
141   INTEGER :: idebug = 236
142   INTEGER :: jdebug = 228
143   !!----------------------------------------------------------------------
144   !! NEMO/OCE 4.0 , NEMO Consortium (2018)
145   !! $Id: zdfosm.F90 12317 2020-01-14 12:40:47Z agn $
146   !! Software governed by the CeCILL license (see ./LICENSE)
147   !!----------------------------------------------------------------------
148CONTAINS
149
150   INTEGER FUNCTION zdf_osm_alloc()
151      !!----------------------------------------------------------------------
152      !!                 ***  FUNCTION zdf_osm_alloc  ***
153      !!----------------------------------------------------------------------
154     ALLOCATE( ghamu(jpi,jpj,jpk), ghamv(jpi,jpj,jpk), ghamt(jpi,jpj,jpk),ghams(jpi,jpj,jpk), &
155          &       hbl(jpi,jpj), dh(jpi,jpj), hml(jpi,jpj), dstokes(jpi, jpj), &
156          &       etmean(jpi,jpj,jpk), STAT= zdf_osm_alloc )
157
158     ALLOCATE(  hmle(jpi,jpj), r1_ft(jpi,jpj), dbdx_mle(jpi,jpj), dbdy_mle(jpi,jpj), &
159          &       mld_prof(jpi,jpj), STAT= zdf_osm_alloc )
160
161!     ALLOCATE( ghamu(jpi,jpj,jpk), ghamv(jpi,jpj,jpk), ghamt(jpi,jpj,jpk),ghams(jpi,jpj,jpk), &    ! would ths be better ?
162!          &       hbl(jpi,jpj), dh(jpi,jpj), hml(jpi,jpj), dstokes(jpi, jpj), &
163!          &       etmean(jpi,jpj,jpk), STAT= zdf_osm_alloc )
164!     IF( zdf_osm_alloc /= 0 )   CALL ctl_warn('zdf_osm_alloc: failed to allocate zdf_osm arrays')
165!     IF ( ln_osm_mle ) THEN
166!        Allocate( hmle(jpi,jpj), r1_ft(jpi,jpj), STAT= zdf_osm_alloc )
167!        IF( zdf_osm_alloc /= 0 )   CALL ctl_warn('zdf_osm_alloc: failed to allocate zdf_osm mle arrays')
168!     ENDIF
169
170     IF( zdf_osm_alloc /= 0 )   CALL ctl_warn('zdf_osm_alloc: failed to allocate zdf_osm arrays')
171     CALL mpp_sum ( 'zdfosm', zdf_osm_alloc )
172   END FUNCTION zdf_osm_alloc
173
174
175   SUBROUTINE zdf_osm( kt, p_avm, p_avt )
176      !!----------------------------------------------------------------------
177      !!                   ***  ROUTINE zdf_osm  ***
178      !!
179      !! ** Purpose :   Compute the vertical eddy viscosity and diffusivity
180      !!      coefficients and non local mixing using the OSMOSIS scheme
181      !!
182      !! ** Method :   The boundary layer depth hosm is diagnosed at tracer points
183      !!      from profiles of buoyancy, and shear, and the surface forcing.
184      !!      Above hbl (sigma=-z/hbl <1) the mixing coefficients are computed from
185      !!
186      !!                      Kx =  hosm  Wx(sigma) G(sigma)
187      !!
188      !!             and the non local term ghamt = Cs / Ws(sigma) / hosm
189      !!      Below hosm  the coefficients are the sum of mixing due to internal waves
190      !!      shear instability and double diffusion.
191      !!
192      !!      -1- Compute the now interior vertical mixing coefficients at all depths.
193      !!      -2- Diagnose the boundary layer depth.
194      !!      -3- Compute the now boundary layer vertical mixing coefficients.
195      !!      -4- Compute the now vertical eddy vicosity and diffusivity.
196      !!      -5- Smoothing
197      !!
198      !!        N.B. The computation is done from jk=2 to jpkm1
199      !!             Surface value of avt are set once a time to zero
200      !!             in routine zdf_osm_init.
201      !!
202      !! ** Action  :   update the non-local terms ghamts
203      !!                update avt (before vertical eddy coef.)
204      !!
205      !! References : Large W.G., Mc Williams J.C. and Doney S.C.
206      !!         Reviews of Geophysics, 32, 4, November 1994
207      !!         Comments in the code refer to this paper, particularly
208      !!         the equation number. (LMD94, here after)
209      !!----------------------------------------------------------------------
210      INTEGER                   , INTENT(in   ) ::   kt            ! ocean time step
211      REAL(wp), DIMENSION(:,:,:), INTENT(inout) ::  p_avm, p_avt   ! momentum and tracer Kz (w-points)
212      !!
213      INTEGER ::   ji, jj, jk                   ! dummy loop indices
214
215      INTEGER ::   jl                   ! dummy loop indices
216
217      INTEGER ::   ikbot, jkmax, jkm1, jkp2     !
218
219      REAL(wp) ::   ztx, zty, zflageos, zstabl, zbuofdep,zucube      !
220      REAL(wp) ::   zbeta, zthermal                                  !
221      REAL(wp) ::   zehat, zeta, zhrib, zsig, zscale, zwst, zws, zwm ! Velocity scales
222      REAL(wp) ::   zwsun, zwmun, zcons, zconm, zwcons, zwconm      !
223
224      REAL(wp) ::   zsr, zbw, ze, zb, zd, zc, zaw, za, zb1, za1, zkw, zk0, zcomp , zrhd,zrhdr,zbvzed   ! In situ density
225      INTEGER  ::   jm                          ! dummy loop indices
226      REAL(wp) ::   zr1, zr2, zr3, zr4, zrhop   ! Compression terms
227      REAL(wp) ::   zflag, zrn2, zdep21, zdep32, zdep43
228      REAL(wp) ::   zesh2, zri, zfri            ! Interior richardson mixing
229      REAL(wp) ::   zdelta, zdelta2, zdzup, zdzdn, zdzh, zvath, zgat1, zdat1, zkm1m, zkm1t
230      REAL(wp) :: zt,zs,zu,zv,zrh               ! variables used in constructing averages
231! Scales
232      REAL(wp), DIMENSION(jpi,jpj) :: zrad0     ! Surface solar temperature flux (deg m/s)
233      REAL(wp), DIMENSION(jpi,jpj) :: zradh     ! Radiative flux at bl base (Buoyancy units)
234      REAL(wp), DIMENSION(jpi,jpj) :: zradav    ! Radiative flux, bl average (Buoyancy Units)
235      REAL(wp), DIMENSION(jpi,jpj) :: zustar    ! friction velocity
236      REAL(wp), DIMENSION(jpi,jpj) :: zwstrl    ! Langmuir velocity scale
237      REAL(wp), DIMENSION(jpi,jpj) :: zvstr     ! Velocity scale that ends to zustar for large Langmuir number.
238      REAL(wp), DIMENSION(jpi,jpj) :: zwstrc    ! Convective velocity scale
239      REAL(wp), DIMENSION(jpi,jpj) :: zuw0      ! Surface u-momentum flux
240      REAL(wp), DIMENSION(jpi,jpj) :: zvw0      ! Surface v-momentum flux
241      REAL(wp), DIMENSION(jpi,jpj) :: zwth0     ! Surface heat flux (Kinematic)
242      REAL(wp), DIMENSION(jpi,jpj) :: zws0      ! Surface freshwater flux
243      REAL(wp), DIMENSION(jpi,jpj) :: zwb0      ! Surface buoyancy flux
244      REAL(wp), DIMENSION(jpi,jpj) :: zwthav    ! Heat flux - bl average
245      REAL(wp), DIMENSION(jpi,jpj) :: zwsav     ! freshwater flux - bl average
246      REAL(wp), DIMENSION(jpi,jpj) :: zwbav     ! Buoyancy flux - bl average
247      REAL(wp), DIMENSION(jpi,jpj) :: zwb_ent   ! Buoyancy entrainment flux
248
249
250      REAL(wp), DIMENSION(jpi,jpj) :: zwb_fk_b  ! MLE buoyancy flux averaged over OSBL
251      REAL(wp), DIMENSION(jpi,jpj) :: zwb_fk    ! max MLE buoyancy flux
252      REAL(wp), DIMENSION(jpi,jpj) :: zdiff_mle ! extra MLE vertical diff
253      REAL(wp), DIMENSION(jpi,jpj) :: zvel_mle  ! velocity scale for dhdt with stable ML and FK
254
255      REAL(wp), DIMENSION(jpi,jpj) :: zustke    ! Surface Stokes drift
256      REAL(wp), DIMENSION(jpi,jpj) :: zla       ! Trubulent Langmuir number
257      REAL(wp), DIMENSION(jpi,jpj) :: zcos_wind ! Cos angle of surface stress
258      REAL(wp), DIMENSION(jpi,jpj) :: zsin_wind ! Sin angle of surface stress
259      REAL(wp), DIMENSION(jpi,jpj) :: zhol      ! Stability parameter for boundary layer
260      LOGICAL, DIMENSION(jpi,jpj)  :: lconv     ! unstable/stable bl
261
262      ! mixed-layer variables
263
264      INTEGER, DIMENSION(jpi,jpj) :: ibld ! level of boundary layer base
265      INTEGER, DIMENSION(jpi,jpj) :: imld ! level of mixed-layer depth (pycnocline top)
266
267      REAL(wp) :: ztgrad,zsgrad,zbgrad ! Temporary variables used to calculate pycnocline gradients
268      REAL(wp) :: zugrad,zvgrad        ! temporary variables for calculating pycnocline shear
269
270      REAL(wp), DIMENSION(jpi,jpj) :: zhbl  ! bl depth - grid
271      REAL(wp), DIMENSION(jpi,jpj) :: zhml  ! ml depth - grid
272
273      REAL(wp), DIMENSION(jpi,jpj) :: zhmle ! MLE depth - grid
274      REAL(wp), DIMENSION(jpi,jpj) :: zmld  ! ML depth on grid
275
276      REAL(wp), DIMENSION(jpi,jpj) :: zdh   ! pycnocline depth - grid
277      REAL(wp), DIMENSION(jpi,jpj) :: zdhdt ! BL depth tendency
278      REAL(wp), DIMENSION(jpi,jpj) :: zdhdt_2                                    ! correction to dhdt due to internal structure.
279      REAL(wp), DIMENSION(jpi,jpj) :: zdtdz_ext,zdsdz_ext,zdbdz_ext              ! external temperature/salinity and buoyancy gradients
280
281      REAL(wp), DIMENSION(jpi,jpj) :: zdtdx, zdtdy, zdsdx, zdsdy      ! horizontal gradients for Fox-Kemper parametrization.
282
283      REAL(wp), DIMENSION(jpi,jpj) :: zt_bl,zs_bl,zu_bl,zv_bl,zrh_bl  ! averages over the depth of the blayer
284      REAL(wp), DIMENSION(jpi,jpj) :: zt_ml,zs_ml,zu_ml,zv_ml,zrh_ml  ! averages over the depth of the mixed layer
285      REAL(wp), DIMENSION(jpi,jpj) :: zdt_bl,zds_bl,zdu_bl,zdv_bl,zdrh_bl,zdb_bl ! difference between blayer average and parameter at base of blayer
286      REAL(wp), DIMENSION(jpi,jpj) :: zdt_ml,zds_ml,zdu_ml,zdv_ml,zdrh_ml,zdb_ml ! difference between mixed layer average and parameter at base of blayer
287      REAL(wp), DIMENSION(jpi,jpj) :: zwth_ent,zws_ent ! heat and salinity fluxes at the top of the pycnocline
288      REAL(wp), DIMENSION(jpi,jpj) :: zuw_bse,zvw_bse  ! momentum fluxes at the top of the pycnocline
289      REAL(wp), DIMENSION(jpi,jpj,jpk) :: zdtdz_pyc    ! parametrized gradient of temperature in pycnocline
290      REAL(wp), DIMENSION(jpi,jpj,jpk) :: zdsdz_pyc    ! parametrised gradient of salinity in pycnocline
291      REAL(wp), DIMENSION(jpi,jpj,jpk) :: zdbdz_pyc    ! parametrised gradient of buoyancy in the pycnocline
292      REAL(wp), DIMENSION(jpi,jpj,jpk) :: zdudz_pyc    ! u-shear across the pycnocline
293      REAL(wp), DIMENSION(jpi,jpj,jpk) :: zdvdz_pyc    ! v-shear across the pycnocline
294
295      ! Flux-gradient relationship variables
296
297      REAL(wp) :: zl_c,zl_l,zl_eps  ! Used to calculate turbulence length scale.
298
299      REAL(wp), DIMENSION(jpi,jpj) :: zdifml_sc,zvisml_sc,zdifpyc_sc,zvispyc_sc,zbeta_d_sc,zbeta_v_sc ! Scales for eddy diffusivity/viscosity
300      REAL(wp), DIMENSION(jpi,jpj) :: zsc_wth_1,zsc_ws_1 ! Temporary scales used to calculate scalar non-gradient terms.
301      REAL(wp), DIMENSION(jpi,jpj) :: zsc_uw_1,zsc_uw_2,zsc_vw_1,zsc_vw_2 ! Temporary scales for non-gradient momentum flux terms.
302      REAL(wp), DIMENSION(jpi,jpj) :: zhbl_t ! holds boundary layer depth updated by full timestep
303
304      ! For calculating Ri#-dependent mixing
305      REAL(wp), DIMENSION(jpi,jpj,jpk) :: z3du   ! u-shear^2
306      REAL(wp), DIMENSION(jpi,jpj,jpk) :: z3dv   ! v-shear^2
307      REAL(wp), DIMENSION(jpi,jpj,jpk) :: zrimix ! spatial form of ri#-induced diffusion
308
309      ! Temporary variables
310      INTEGER :: inhml
311      REAL(wp) :: znd,znd_d,zznd_ml,zznd_pyc,zznd_d ! temporary non-dimensional depths used in various routines
312      REAL(wp) :: ztemp, zari, zpert, zzdhdt, zdb   ! temporary variables
313      REAL(wp) :: zthick, zz0, zz1 ! temporary variables
314      REAL(wp) :: zvel_max, zhbl_s ! temporary variables
315      REAL(wp) :: zfac             ! temporary variable
316      REAL(wp) :: zus_x, zus_y     ! temporary Stokes drift
317      REAL(wp), DIMENSION(jpi,jpj,jpk) :: zviscos ! viscosity
318      REAL(wp), DIMENSION(jpi,jpj,jpk) :: zdiffut ! t-diffusivity
319
320      INTEGER :: ibld_ext=0                          ! does not have to be zero for modified scheme
321      REAL(wp) :: zwb_min, zgamma_b_nd, zgamma_b, zdhoh, ztau, zddhdt
322      REAL(wp) :: zzeta_s = 0._wp
323      REAL(wp) :: zzeta_v = 0.46
324      REAL(wp) :: zabsstke
325
326      ! For debugging
327      INTEGER :: ikt
328      !!--------------------------------------------------------------------
329      !
330      ibld(:,:)   = 0     ; imld(:,:)  = 0
331      zrad0(:,:)  = 0._wp ; zradh(:,:) = 0._wp ; zradav(:,:)    = 0._wp ; zustar(:,:)    = 0._wp
332      zwstrl(:,:) = 0._wp ; zvstr(:,:) = 0._wp ; zwstrc(:,:)    = 0._wp ; zuw0(:,:)      = 0._wp
333      zvw0(:,:)   = 0._wp ; zwth0(:,:) = 0._wp ; zws0(:,:)      = 0._wp ; zwb0(:,:)      = 0._wp
334      zwthav(:,:) = 0._wp ; zwsav(:,:) = 0._wp ; zwbav(:,:)     = 0._wp ; zwb_ent(:,:)   = 0._wp
335      zustke(:,:) = 0._wp ; zla(:,:)   = 0._wp ; zcos_wind(:,:) = 0._wp ; zsin_wind(:,:) = 0._wp
336      zhol(:,:)   = 0._wp
337      lconv(:,:)  = .FALSE.
338      ! mixed layer
339      ! no initialization of zhbl or zhml (or zdh?)
340      zhbl(:,:)    = 1._wp ; zhml(:,:)    = 1._wp ; zdh(:,:)      = 1._wp ; zdhdt(:,:)   = 0._wp
341      zt_bl(:,:)   = 0._wp ; zs_bl(:,:)   = 0._wp ; zu_bl(:,:)    = 0._wp ; zv_bl(:,:)   = 0._wp
342      zrh_bl(:,:)  = 0._wp ; zt_ml(:,:)   = 0._wp ; zs_ml(:,:)    = 0._wp ; zu_ml(:,:)   = 0._wp
343
344      zv_ml(:,:)   = 0._wp ; zrh_ml(:,:)  = 0._wp ; zdt_bl(:,:)   = 0._wp ; zds_bl(:,:)  = 0._wp
345      zdu_bl(:,:)  = 0._wp ; zdv_bl(:,:)  = 0._wp ; zdrh_bl(:,:)  = 0._wp ; zdb_bl(:,:)  = 0._wp
346      zdt_ml(:,:)  = 0._wp ; zds_ml(:,:)  = 0._wp ; zdu_ml(:,:)   = 0._wp ; zdv_ml(:,:)  = 0._wp
347      zdrh_ml(:,:) = 0._wp ; zdb_ml(:,:)  = 0._wp ; zwth_ent(:,:) = 0._wp ; zws_ent(:,:) = 0._wp
348      zuw_bse(:,:) = 0._wp ; zvw_bse(:,:) = 0._wp
349      !
350      zdtdz_pyc(:,:,:) = 0._wp ; zdsdz_pyc(:,:,:) = 0._wp ; zdbdz_pyc(:,:,:) = 0._wp
351      zdudz_pyc(:,:,:) = 0._wp ; zdvdz_pyc(:,:,:) = 0._wp
352      !
353      zdtdz_ext(:,:) = 0._wp ; zdsdz_ext(:,:) = 0._wp ; zdbdz_ext(:,:) = 0._wp
354
355      IF ( ln_osm_mle ) THEN  ! only initialise arrays if needed
356         zdtdx(:,:) = 0._wp ; zdtdy(:,:) = 0._wp ; zdsdx(:,:) = 0._wp
357         zdsdy(:,:) = 0._wp ; dbdx_mle(:,:) = 0._wp ; dbdy_mle(:,:) = 0._wp
358         zwb_fk(:,:) = 0._wp ; zvel_mle(:,:) = 0._wp; zdiff_mle(:,:) = 0._wp
359         zhmle(:,:) = 0._wp  ; zmld(:,:) = 0._wp
360      ENDIF
361      zwb_fk_b(:,:) = 0._wp   ! must be initialised even with ln_osm_mle=F as used in zdf_osm_calculate_dhdt
362
363      ! Flux-Gradient arrays.
364      zdifml_sc(:,:)  = 0._wp ; zvisml_sc(:,:)  = 0._wp ; zdifpyc_sc(:,:) = 0._wp
365      zvispyc_sc(:,:) = 0._wp ; zbeta_d_sc(:,:) = 0._wp ; zbeta_v_sc(:,:) = 0._wp
366      zsc_wth_1(:,:)  = 0._wp ; zsc_ws_1(:,:)   = 0._wp ; zsc_uw_1(:,:)   = 0._wp
367      zsc_uw_2(:,:)   = 0._wp ; zsc_vw_1(:,:)   = 0._wp ; zsc_vw_2(:,:)   = 0._wp
368      zhbl_t(:,:)     = 0._wp ; zdhdt(:,:)      = 0._wp
369
370      zdiffut(:,:,:) = 0._wp ; zviscos(:,:,:) = 0._wp ; ghamt(:,:,:) = 0._wp
371      ghams(:,:,:)   = 0._wp ; ghamu(:,:,:)   = 0._wp ; ghamv(:,:,:) = 0._wp
372
373      zdhdt_2(:,:) = 0._wp
374      ! hbl = MAX(hbl,epsln)
375      !>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
376      ! Calculate boundary layer scales
377      !<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
378
379      ! Assume two-band radiation model for depth of OSBL
380     zz0 =       rn_abs       ! surface equi-partition in 2-bands
381     zz1 =  1. - rn_abs
382     DO jj = 2, jpjm1
383        DO ji = 2, jpim1
384           ! Surface downward irradiance (so always +ve)
385           zrad0(ji,jj) = qsr(ji,jj) * r1_rau0_rcp
386           ! Downwards irradiance at base of boundary layer
387           zradh(ji,jj) = zrad0(ji,jj) * ( zz0 * EXP( -hbl(ji,jj)/rn_si0 ) + zz1 * EXP( -hbl(ji,jj)/rn_si1) )
388           ! Downwards irradiance averaged over depth of the OSBL
389           zradav(ji,jj) = zrad0(ji,jj) * ( zz0 * ( 1.0 - EXP( -hbl(ji,jj)/rn_si0 ) )*rn_si0 &
390                 &                         + zz1 * ( 1.0 - EXP( -hbl(ji,jj)/rn_si1 ) )*rn_si1 ) / hbl(ji,jj)
391        END DO
392     END DO
393     ! Turbulent surface fluxes and fluxes averaged over depth of the OSBL
394     DO jj = 2, jpjm1
395        DO ji = 2, jpim1
396           zthermal = rab_n(ji,jj,1,jp_tem)
397           zbeta    = rab_n(ji,jj,1,jp_sal)
398           ! Upwards surface Temperature flux for non-local term
399           zwth0(ji,jj) = - qns(ji,jj) * r1_rau0_rcp * tmask(ji,jj,1)
400           ! Upwards surface salinity flux for non-local term
401           zws0(ji,jj) = - ( ( emp(ji,jj)-rnf(ji,jj) ) * tsn(ji,jj,1,jp_sal)  + sfx(ji,jj) ) * r1_rau0 * tmask(ji,jj,1)
402           ! Non radiative upwards surface buoyancy flux
403           zwb0(ji,jj) = grav * zthermal * zwth0(ji,jj) -  grav * zbeta * zws0(ji,jj)
404           ! turbulent heat flux averaged over depth of OSBL
405           zwthav(ji,jj) = 0.5 * zwth0(ji,jj) - ( 0.5*( zrad0(ji,jj) + zradh(ji,jj) ) - zradav(ji,jj) )
406           ! turbulent salinity flux averaged over depth of the OBSL
407           zwsav(ji,jj) = 0.5 * zws0(ji,jj)
408           ! turbulent buoyancy flux averaged over the depth of the OBSBL
409           zwbav(ji,jj) = grav  * zthermal * zwthav(ji,jj) - grav  * zbeta * zwsav(ji,jj)
410           ! Surface upward velocity fluxes
411           zuw0(ji,jj) = - 0.5 * (utau(ji-1,jj) + utau(ji,jj)) * r1_rau0 * tmask(ji,jj,1)
412           zvw0(ji,jj) = - 0.5 * (vtau(ji,jj-1) + vtau(ji,jj)) * r1_rau0 * tmask(ji,jj,1)
413           ! Friction velocity (zustar), at T-point : LMD94 eq. 2
414           zustar(ji,jj) = MAX( SQRT( SQRT( zuw0(ji,jj) * zuw0(ji,jj) + zvw0(ji,jj) * zvw0(ji,jj) ) ), 1.0e-8 )
415           zcos_wind(ji,jj) = -zuw0(ji,jj) / ( zustar(ji,jj) * zustar(ji,jj) )
416           zsin_wind(ji,jj) = -zvw0(ji,jj) / ( zustar(ji,jj) * zustar(ji,jj) )
417        END DO
418     END DO
419     ! Calculate Stokes drift in direction of wind (zustke) and Stokes penetration depth (dstokes)
420     SELECT CASE (nn_osm_wave)
421     ! Assume constant La#=0.3
422     CASE(0)
423        DO jj = 2, jpjm1
424           DO ji = 2, jpim1
425              zus_x = zcos_wind(ji,jj) * zustar(ji,jj) / 0.3**2
426              zus_y = zsin_wind(ji,jj) * zustar(ji,jj) / 0.3**2
427              zustke(ji,jj) = MAX ( SQRT( zus_x*zus_x + zus_y*zus_y), 1.0e-8 )
428              ! dstokes(ji,jj) set to constant value rn_osm_dstokes from namelist in zdf_osm_init
429           END DO
430        END DO
431     ! Assume Pierson-Moskovitz wind-wave spectrum
432     CASE(1)
433        DO jj = 2, jpjm1
434           DO ji = 2, jpim1
435              ! Use wind speed wndm included in sbc_oce module
436              zustke(ji,jj) = MAX ( 0.016 * wndm(ji,jj), 1.0e-8 )
437              dstokes(ji,jj) = MAX( 0.12 * wndm(ji,jj)**2 / grav, 5.e-1)
438           END DO
439        END DO
440     ! Use ECMWF wave fields as output from SBCWAVE
441     CASE(2)
442        zfac =  2.0_wp * rpi / 16.0_wp
443        DO jj = 2, jpjm1
444           DO ji = 2, jpim1
445              ! The Langmur number from the ECMWF model appears to give La<0.3 for wind-driven seas.
446              !    The coefficient 0.8 gives La=0.3  in this situation.
447              ! It could represent the effects of the spread of wave directions
448              ! around the mean wind. The effect of this adjustment needs to be tested.
449              zabsstke = SQRT(ut0sd(ji,jj)**2 + vt0sd(ji,jj)**2)
450              zustke(ji,jj) = MAX (0.8 * ( zcos_wind(ji,jj) * ut0sd(ji,jj) + zsin_wind(ji,jj)  * vt0sd(ji,jj) ), 1.0e-8)
451              dstokes(ji,jj) = MAX(zfac * hsw(ji,jj)*hsw(ji,jj) / ( MAX(zabsstke*wmp(ji,jj), 1.0e-7 ) ), 5.0e-1) !rn_osm_dstokes !
452           END DO
453        END DO
454     END SELECT
455
456     ! Langmuir velocity scale (zwstrl), La # (zla)
457     ! mixed scale (zvstr), convective velocity scale (zwstrc)
458     DO jj = 2, jpjm1
459        DO ji = 2, jpim1
460           ! Langmuir velocity scale (zwstrl), at T-point
461           zwstrl(ji,jj) = ( zustar(ji,jj) * zustar(ji,jj) * zustke(ji,jj) )**pthird
462           zla(ji,jj) = MAX(MIN(SQRT ( zustar(ji,jj) / ( zwstrl(ji,jj) + epsln ) )**3, 4.0), 0.2)
463           IF(zla(ji,jj) > 0.45) dstokes(ji,jj) = MIN(dstokes(ji,jj), 0.5_wp*hbl(ji,jj))
464           ! Velocity scale that tends to zustar for large Langmuir numbers
465           zvstr(ji,jj) = ( zwstrl(ji,jj)**3  + &
466                & ( 1.0 - EXP( -0.5 * zla(ji,jj)**2 ) ) * zustar(ji,jj) * zustar(ji,jj) * zustar(ji,jj) )**pthird
467
468           ! limit maximum value of Langmuir number as approximate treatment for shear turbulence.
469           ! Note zustke and zwstrl are not amended.
470           !
471           ! get convective velocity (zwstrc), stabilty scale (zhol) and logical conection flag lconv
472           IF ( zwbav(ji,jj) > 0.0) THEN
473              zwstrc(ji,jj) = ( 2.0 * zwbav(ji,jj) * 0.9 * hbl(ji,jj) )**pthird
474              zhol(ji,jj) = -0.9 * hbl(ji,jj) * 2.0 * zwbav(ji,jj) / (zvstr(ji,jj)**3 + epsln )
475              lconv(ji,jj) = .TRUE.
476           ELSE
477              zhol(ji,jj) = -hbl(ji,jj) *  2.0 * zwbav(ji,jj)/ (zvstr(ji,jj)**3  + epsln )
478              lconv(ji,jj) = .FALSE.
479           ENDIF
480        END DO
481     END DO
482
483     !>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
484     ! Mixed-layer model - calculate averages over the boundary layer, and the change in the boundary layer depth
485     !<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
486     ! BL must be always 4 levels deep.
487     ! For calculation of lateral buoyancy gradients for FK in
488     ! zdf_osm_zmld_horizontal_gradients need halo values for ibld, so must
489     ! previously exist for hbl also.
490      hbl(:,:) = MAX(hbl(:,:), gdepw_n(:,:,4) )
491      ibld(:,:) = 4
492      DO jk = 5, jpkm1
493         DO jj = 1, jpj
494            DO ji = 1, jpi
495               IF ( hbl(ji,jj) >= gdepw_n(ji,jj,jk) ) THEN
496                  ibld(ji,jj) = MIN(mbkt(ji,jj), jk)
497               ENDIF
498            END DO
499         END DO
500      END DO
501
502      DO jj = 2, jpjm1
503         DO ji = 2, jpim1
504            zhbl(ji,jj) = gdepw_n(ji,jj,ibld(ji,jj))
505            imld(ji,jj) = MAX(3,ibld(ji,jj) - MAX( INT( dh(ji,jj) / e3t_n(ji, jj, ibld(ji,jj) )) , 1 ))
506            zhml(ji,jj) = gdepw_n(ji,jj,imld(ji,jj))
507         END DO
508      END DO
509      ! Averages over well-mixed and boundary layer
510      ! Alan: do we need zb_nl?, zb_ml?
511      CALL zdf_osm_vertical_average(ibld, zt_bl, zs_bl, zu_bl, zv_bl, zdt_bl, zds_bl, zdb_bl, zdu_bl, zdv_bl)
512      CALL zdf_osm_vertical_average(imld, zt_ml, zs_ml, zu_ml, zv_ml, zdt_ml, zds_ml, zdb_ml, zdu_ml, zdv_ml)
513! External gradient
514      CALL zdf_osm_external_gradients( zdtdz_ext, zdsdz_ext, zdbdz_ext )
515
516
517      IF ( ln_osm_mle ) THEN
518         CALL zdf_osm_zmld_horizontal_gradients( zmld, zdtdx, zdtdy, zdsdx, zdsdy, dbdx_mle, dbdy_mle )
519         CALL zdf_osm_mle_parameters( hmle, zwb_fk, zvel_mle, zdiff_mle )
520       ENDIF
521
522! Rate of change of hbl
523      CALL zdf_osm_calculate_dhdt( zdhdt, zdhdt_2 )
524      ! Calculate averages over depth of boundary layer
525         DO jj = 2, jpjm1
526            DO ji = 2, jpim1
527               zhbl_t(ji,jj) = hbl(ji,jj) + (zdhdt(ji,jj) - wn(ji,jj,ibld(ji,jj)))* rn_rdt ! certainly need wn here, so subtract it
528               ! adjustment to represent limiting by ocean bottom
529               zhbl_t(ji,jj) = MIN(zhbl_t(ji,jj), gdepw_n(ji,jj, mbkt(ji,jj) + 1) - depth_tol)! ht_n(:,:))
530            END DO
531         END DO
532
533      imld(:,:) = ibld(:,:)           ! use imld to hold previous blayer index
534      ibld(:,:) = 4
535
536      DO jk = 4, jpkm1
537         DO jj = 2, jpjm1
538            DO ji = 2, jpim1
539               IF ( zhbl_t(ji,jj) >= gdepw_n(ji,jj,jk) ) THEN
540                  ibld(ji,jj) = jk
541               ENDIF
542            END DO
543         END DO
544      END DO
545
546!
547! Step through model levels taking account of buoyancy change to determine the effect on dhdt
548!
549      CALL zdf_osm_timestep_hbl( zdhdt, zdhdt_2 )
550      ! Alan: do we need zb_ml?
551      CALL zdf_osm_vertical_average( ibld, zt_bl, zs_bl, zu_bl, zv_bl, zdt_bl, zds_bl, zdb_bl, zdu_bl, zdv_bl )
552!
553!
554      CALL zdf_osm_pycnocline_thickness( dh, zdh )
555      dstokes(:,:) = MIN ( dstokes(:,:), hbl(:,:)/3. )  !  Limit delta for shallow boundary layers for calculating flux-gradient terms.
556!
557    ! Average over the depth of the mixed layer in the convective boundary layer
558    ! Alan: do we need zb_ml?
559    CALL zdf_osm_vertical_average( imld, zt_ml, zs_ml, zu_ml, zv_ml, zdt_ml, zds_ml, zdb_ml, zdu_ml, zdv_ml )
560    ! rotate mean currents and changes onto wind align co-ordinates
561    !
562    CALL zdf_osm_velocity_rotation( zcos_wind, zsin_wind, zu_ml, zv_ml, zdu_ml, zdv_ml )
563    CALL zdf_osm_velocity_rotation( zcos_wind, zsin_wind, zu_bl, zv_bl, zdu_bl, zdv_bl )
564     zuw_bse = 0._wp
565     zvw_bse = 0._wp
566     zwth_ent = 0._wp
567     zws_ent = 0._wp
568     DO jj = 2, jpjm1
569        DO ji = 2, jpim1
570           IF ( ibld(ji,jj) < mbkt(ji,jj) ) THEN
571              IF ( lconv(ji,jj) ) THEN
572             zuw_bse(ji,jj) = -0.0075*((zvstr(ji,jj)**3+0.5*zwstrc(ji,jj)**3)**pthird*zdu_ml(ji,jj) + &
573                      &                    1.5*zustar(ji,jj)**2*(zhbl(ji,jj)-zhml(ji,jj)) )/ &
574                      &                     ( zhml(ji,jj)*MIN(zla(ji,jj)**(8./3.),1.) + epsln)
575            zvw_bse(ji,jj) = 0.01*(-(zvstr(ji,jj)**3+0.5*zwstrc(ji,jj)**3)**pthird*zdv_ml(ji,jj)+ &
576                      &                    2.0*ff_t(ji,jj)*zustke(ji,jj)*dstokes(ji,jj)*zla(ji,jj))
577                 IF ( zdb_ml(ji,jj) > 0._wp ) THEN
578                    zwth_ent(ji,jj) = zwb_ent(ji,jj) * zdt_ml(ji,jj) / (zdb_ml(ji,jj) + epsln)
579                    zws_ent(ji,jj) = zwb_ent(ji,jj) * zds_ml(ji,jj) / (zdb_ml(ji,jj) + epsln)
580                 ENDIF
581              ELSE
582                 zwth_ent(ji,jj) = -2.0 * zwthav(ji,jj) * ( (1.0 - 0.8) - ( 1.0 - 0.8)**(3.0/2.0) )
583                 zws_ent(ji,jj) = -2.0 * zwsav(ji,jj) * ( (1.0 - 0.8 ) - ( 1.0 - 0.8 )**(3.0/2.0) )
584              ENDIF
585           ENDIF
586        END DO
587     END DO
588
589      !>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
590      !  Pycnocline gradients for scalars and velocity
591      !<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
592
593      CALL zdf_osm_external_gradients( zdtdz_ext, zdsdz_ext, zdbdz_ext )
594      CALL zdf_osm_pycnocline_scalar_profiles( zdtdz_pyc, zdsdz_pyc, zdbdz_pyc )
595      CALL zdf_osm_pycnocline_shear_profiles( zdudz_pyc, zdvdz_pyc )
596       !>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
597       ! Eddy viscosity/diffusivity and non-gradient terms in the flux-gradient relationship
598       !<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
599
600       DO jj = 2, jpjm1
601          DO ji = 2, jpim1
602             IF ( lconv(ji,jj) ) THEN
603               zdifml_sc(ji,jj) = zhml(ji,jj) * ( zvstr(ji,jj)**3 + 0.5 * zwstrc(ji,jj)**3 )**pthird
604               zvisml_sc(ji,jj) = zdifml_sc(ji,jj)
605               zdifpyc_sc(ji,jj) = 0.165 * ( zvstr(ji,jj)**3 + 0.5 *zwstrc(ji,jj)**3 )**pthird * zdh(ji,jj)
606               zvispyc_sc(ji,jj) = 0.142 * ( zvstr(ji,jj)**3 + 0.5 * zwstrc(ji,jj)**3 )**pthird * zdh(ji,jj)
607               zbeta_d_sc(ji,jj) = 1.0 - (0.165 / 0.8 * zdh(ji,jj) / zhbl(ji,jj) )**p2third
608               zbeta_v_sc(ji,jj) = 1.0 -  2.0 * (0.142 /0.375) * zdh(ji,jj) / ( zhml(ji,jj) + epsln )
609             ELSE
610               zdifml_sc(ji,jj) = zvstr(ji,jj) * zhbl(ji,jj) * EXP ( -( zhol(ji,jj) / 0.6_wp )**2 )
611               zvisml_sc(ji,jj) = zvstr(ji,jj) * zhbl(ji,jj) * EXP ( -( zhol(ji,jj) / 0.6_wp )**2 )
612             END IF
613          END DO
614       END DO
615!
616       DO jj = 2, jpjm1
617          DO ji = 2, jpim1
618             IF ( lconv(ji,jj) ) THEN
619                DO jk = 2, imld(ji,jj)   ! mixed layer diffusivity
620                    zznd_ml = gdepw_n(ji,jj,jk) / zhml(ji,jj)
621                    !
622                    zdiffut(ji,jj,jk) = 0.8   * zdifml_sc(ji,jj) * zznd_ml * ( 1.0 - zbeta_d_sc(ji,jj) * zznd_ml    )**1.5
623                    !
624                    zviscos(ji,jj,jk) = 0.375 * zvisml_sc(ji,jj) * zznd_ml * ( 1.0 - zbeta_v_sc(ji,jj) * zznd_ml    ) &
625                         &            *                                      ( 1.0 -               0.5 * zznd_ml**2 )
626                END DO
627                ! pycnocline - if present linear profile
628                IF ( zdh(ji,jj) > 0._wp ) THEN
629                   zgamma_b = 6.0
630                   DO jk = imld(ji,jj)+1 , ibld(ji,jj)
631                       zznd_pyc = -( gdepw_n(ji,jj,jk) - zhml(ji,jj) ) / zdh(ji,jj)
632                       !
633                       zdiffut(ji,jj,jk) = zdifpyc_sc(ji,jj) * EXP( zgamma_b * zznd_pyc )
634                       !
635                       zviscos(ji,jj,jk) = zvispyc_sc(ji,jj) * EXP( zgamma_b * zznd_pyc )
636                   END DO
637                   IF ( ibld_ext == 0 ) THEN
638                       zdiffut(ji,jj,ibld(ji,jj)) = 0._wp
639                       zviscos(ji,jj,ibld(ji,jj)) = 0._wp
640                   ELSE
641                       zdiffut(ji,jj,ibld(ji,jj)) = zdhdt(ji,jj) * ( hbl(ji,jj) - gdepw_n(ji, jj, ibld(ji,jj)-1) )
642                       zviscos(ji,jj,ibld(ji,jj)) = zdhdt(ji,jj) * ( hbl(ji,jj) - gdepw_n(ji, jj, ibld(ji,jj)-1) )
643                   ENDIF
644                ENDIF
645                ! Temporary fix to ensure zdiffut is +ve; won't be necessary with wn taken out
646                zdiffut(ji,jj,ibld(ji,jj)) = MAX(zdhdt(ji,jj) * e3t_n(ji,jj,ibld(ji,jj)), 1.e-6)
647                zviscos(ji,jj,ibld(ji,jj)) = MAX(zdhdt(ji,jj) * e3t_n(ji,jj,ibld(ji,jj)), 1.e-5)
648                ! could be taken out, take account of entrainment represents as a diffusivity
649                ! should remove w from here, represents entrainment
650             ELSE
651             ! stable conditions
652                DO jk = 2, ibld(ji,jj)
653                   zznd_ml = gdepw_n(ji,jj,jk) / zhbl(ji,jj)
654                   zdiffut(ji,jj,jk) = 0.75 * zdifml_sc(ji,jj) * zznd_ml * ( 1.0 - zznd_ml )**1.5
655                   zviscos(ji,jj,jk) = 0.375 * zvisml_sc(ji,jj) * zznd_ml * (1.0 - zznd_ml) * ( 1.0 - zznd_ml**2 )
656                END DO
657
658                IF ( ibld_ext == 0 ) THEN
659                   zdiffut(ji,jj,ibld(ji,jj)) = 0._wp
660                   zviscos(ji,jj,ibld(ji,jj)) = 0._wp
661                ELSE
662                   zdiffut(ji,jj,ibld(ji,jj)) = MAX(zdhdt(ji,jj), 0._wp) * e3w_n(ji, jj, ibld(ji,jj))
663                   zviscos(ji,jj,ibld(ji,jj)) = MAX(zdhdt(ji,jj), 0._wp) * e3w_n(ji, jj, ibld(ji,jj))
664                ENDIF
665             ENDIF   ! end if ( lconv )
666             !
667          END DO  ! end of ji loop
668       END DO     ! end of jj loop
669
670       !
671       ! calculate non-gradient components of the flux-gradient relationships
672       !
673! Stokes term in scalar flux, flux-gradient relationship
674       WHERE ( lconv )
675          zsc_wth_1 = zwstrl**3 * zwth0 / ( zvstr**3 + 0.5 * zwstrc**3 + epsln)
676          !
677          zsc_ws_1 = zwstrl**3 * zws0 / ( zvstr**3 + 0.5 * zwstrc**3 + epsln )
678       ELSEWHERE
679          zsc_wth_1 = 2.0 * zwthav
680          !
681          zsc_ws_1 = 2.0 * zwsav
682       ENDWHERE
683
684
685       DO jj = 2, jpjm1
686          DO ji = 2, jpim1
687            IF ( lconv(ji,jj) ) THEN
688              DO jk = 2, imld(ji,jj)
689                 zznd_d = gdepw_n(ji,jj,jk) / dstokes(ji,jj)
690                 ghamt(ji,jj,jk) = ghamt(ji,jj,jk) + 1.35 * EXP ( -zznd_d ) * ( 1.0 - EXP ( -2.0 * zznd_d ) ) * zsc_wth_1(ji,jj)
691                 !
692                 ghams(ji,jj,jk) = ghams(ji,jj,jk) + 1.35 * EXP ( -zznd_d ) * ( 1.0 - EXP ( -2.0 * zznd_d ) ) *  zsc_ws_1(ji,jj)
693              END DO ! end jk loop
694            ELSE     ! else for if (lconv)
695 ! Stable conditions
696               DO jk = 2, ibld(ji,jj)
697                  zznd_d=gdepw_n(ji,jj,jk) / dstokes(ji,jj)
698                  ghamt(ji,jj,jk) = ghamt(ji,jj,jk) + 1.5 * EXP ( -0.9 * zznd_d ) &
699                       &          *                 ( 1.0 - EXP ( -4.0 * zznd_d ) ) * zsc_wth_1(ji,jj)
700                  !
701                  ghams(ji,jj,jk) = ghams(ji,jj,jk) + 1.5 * EXP ( -0.9 * zznd_d ) &
702                       &          *                 ( 1.0 - EXP ( -4.0 * zznd_d ) ) *  zsc_ws_1(ji,jj)
703               END DO
704            ENDIF               ! endif for check on lconv
705
706          END DO  ! end of ji loop
707       END DO     ! end of jj loop
708
709! Stokes term in flux-gradient relationship (note in zsc_uw_n don't use zvstr since term needs to go to zero as zwstrl goes to zero)
710       WHERE ( lconv )
711          zsc_uw_1 = ( zwstrl**3 + 0.5 * zwstrc**3 )**pthird * zustke / MAX( ( 1.0 - 1.0 * 6.5 * zla**(8.0/3.0) ), 0.2 )
712          zsc_uw_2 = ( zwstrl**3 + 0.5 * zwstrc**3 )**pthird * zustke / MIN( zla**(8.0/3.0) + epsln, 0.12 )
713          zsc_vw_1 = ff_t * zhml * zustke**3 * MIN( zla**(8.0/3.0), 0.12 ) / ( ( zvstr**3 + 0.5 * zwstrc**3 )**(2.0/3.0) + epsln )
714       ELSEWHERE
715          zsc_uw_1 = zustar**2
716          zsc_vw_1 = ff_t * zhbl * zustke**3 * MIN( zla**(8.0/3.0), 0.12 ) / (zvstr**2 + epsln)
717       ENDWHERE
718       IF(ln_dia_osm) THEN
719          IF ( iom_use("ghamu_00") ) CALL iom_put( "ghamu_00", wmask*ghamu )
720          IF ( iom_use("ghamv_00") ) CALL iom_put( "ghamv_00", wmask*ghamv )
721       END IF
722       DO jj = 2, jpjm1
723          DO ji = 2, jpim1
724             IF ( lconv(ji,jj) ) THEN
725                DO jk = 2, imld(ji,jj)
726                   zznd_d = gdepw_n(ji,jj,jk) / dstokes(ji,jj)
727                   ghamu(ji,jj,jk) = ghamu(ji,jj,jk) +      ( -0.05 * EXP ( -0.4 * zznd_d )   * zsc_uw_1(ji,jj)   &
728                        &          +                        0.00125 * EXP (      - zznd_d )   * zsc_uw_2(ji,jj) ) &
729                        &          *                          ( 1.0 - EXP ( -2.0 * zznd_d ) )
730!
731                   ghamv(ji,jj,jk) = ghamv(ji,jj,jk) - 0.65 *  0.15 * EXP (      - zznd_d )                       &
732                        &          *                          ( 1.0 - EXP ( -2.0 * zznd_d ) ) * zsc_vw_1(ji,jj)
733                END DO   ! end jk loop
734             ELSE
735! Stable conditions
736                DO jk = 2, ibld(ji,jj) ! corrected to ibld
737                   zznd_d = gdepw_n(ji,jj,jk) / dstokes(ji,jj)
738                   ghamu(ji,jj,jk) = ghamu(ji,jj,jk) - 0.75 *   1.3 * EXP ( -0.5 * zznd_d )                       &
739                        &                                   * ( 1.0 - EXP ( -4.0 * zznd_d ) ) * zsc_uw_1(ji,jj)
740                   ghamv(ji,jj,jk) = ghamv(ji,jj,jk) + 0._wp
741                END DO   ! end jk loop
742             ENDIF
743          END DO  ! ji loop
744       END DO     ! jj loo
745
746! Buoyancy term in flux-gradient relationship [note : includes ROI ratio (X0.3) and pressure (X0.5)]
747
748       WHERE ( lconv )
749          zsc_wth_1 = zwbav * zwth0 * ( 1.0 + EXP ( 0.2 * zhol ) ) / ( zvstr**3 + 0.5 * zwstrc**3 + epsln )
750          zsc_ws_1  = zwbav * zws0  * ( 1.0 + EXP ( 0.2 * zhol ) ) / ( zvstr**3 + 0.5 * zwstrc**3 + epsln )
751       ELSEWHERE
752          zsc_wth_1 = 0._wp
753          zsc_ws_1 = 0._wp
754       ENDWHERE
755
756       DO jj = 2, jpjm1
757          DO ji = 2, jpim1
758             IF (lconv(ji,jj) ) THEN
759                DO jk = 2, imld(ji,jj)
760                   zznd_ml = gdepw_n(ji,jj,jk) / zhml(ji,jj)
761                   ! calculate turbulent length scale
762                   zl_c = 0.9 * ( 1.0 - EXP ( - 7.0 * ( zznd_ml - zznd_ml**3 / 3.0 ) ) )                                           &
763                        &     * ( 1.0 - EXP ( -15.0 * (     1.1 - zznd_ml          ) ) )
764                   zl_l = 2.0 * ( 1.0 - EXP ( - 2.0 * ( zznd_ml - zznd_ml**3 / 3.0 ) ) )                                           &
765                        &     * ( 1.0 - EXP ( - 5.0 * (     1.0 - zznd_ml          ) ) ) * ( 1.0 + dstokes(ji,jj) / zhml (ji,jj) )
766                   zl_eps = zl_l + ( zl_c - zl_l ) / ( 1.0 + EXP ( -3.0 * LOG10 ( - zhol(ji,jj) ) ) ) ** (3.0/2.0)
767                   ! non-gradient buoyancy terms
768                   ghamt(ji,jj,jk) = ghamt(ji,jj,jk) + 0.3 * 0.5 * zsc_wth_1(ji,jj) * zl_eps * zhml(ji,jj) / ( 0.15 + zznd_ml )
769                   ghams(ji,jj,jk) = ghams(ji,jj,jk) + 0.3 * 0.5 *  zsc_ws_1(ji,jj) * zl_eps * zhml(ji,jj) / ( 0.15 + zznd_ml )
770                END DO
771             ELSE
772                DO jk = 2, ibld(ji,jj)
773                   ghamt(ji,jj,jk) = ghamt(ji,jj,jk) + zsc_wth_1(ji,jj)
774                   ghams(ji,jj,jk) = ghams(ji,jj,jk) +  zsc_ws_1(ji,jj)
775                END DO
776             ENDIF
777          END DO   ! ji loop
778       END DO      ! jj loop
779
780       WHERE ( lconv )
781          zsc_uw_1 = -zwb0 * zustar**2 * zhml / ( zvstr**3 + 0.5 * zwstrc**3 + epsln )
782          zsc_uw_2 =  zwb0 * zustke    * zhml / ( zvstr**3 + 0.5 * zwstrc**3 + epsln )**(2.0/3.0)
783          zsc_vw_1 = 0._wp
784       ELSEWHERE
785         zsc_uw_1 = 0._wp
786         zsc_vw_1 = 0._wp
787       ENDWHERE
788
789       DO jj = 2, jpjm1
790          DO ji = 2, jpim1
791             IF ( lconv(ji,jj) ) THEN
792                DO jk = 2 , imld(ji,jj)
793                   zznd_d = gdepw_n(ji,jj,jk) / dstokes(ji,jj)
794                   ghamu(ji,jj,jk) = ghamu(ji,jj,jk) + 0.3 * 0.5 * ( zsc_uw_1(ji,jj) +   0.125 * EXP( -0.5 * zznd_d )     &
795                        &                                                            * (   1.0 - EXP( -0.5 * zznd_d ) )   &
796                        &                                          * zsc_uw_2(ji,jj)                                    )
797                   ghamv(ji,jj,jk) = ghamv(ji,jj,jk) + zsc_vw_1(ji,jj)
798                END DO  ! jk loop
799             ELSE
800             ! stable conditions
801                DO jk = 2, ibld(ji,jj)
802                   ghamu(ji,jj,jk) = ghamu(ji,jj,jk) + zsc_uw_1(ji,jj)
803                   ghamv(ji,jj,jk) = ghamv(ji,jj,jk) + zsc_vw_1(ji,jj)
804                END DO
805             ENDIF
806          END DO        ! ji loop
807       END DO           ! jj loop
808
809       IF(ln_dia_osm) THEN
810          IF ( iom_use("ghamu_0") ) CALL iom_put( "ghamu_0", wmask*ghamu )
811          IF ( iom_use("zsc_uw_1_0") ) CALL iom_put( "zsc_uw_1_0", tmask(:,:,1)*zsc_uw_1 )
812       END IF
813! Transport term in flux-gradient relationship [note : includes ROI ratio (X0.3) ]
814
815       WHERE ( lconv )
816          zsc_wth_1 = zwth0
817          zsc_ws_1 = zws0
818       ELSEWHERE
819          zsc_wth_1 = 2.0 * zwthav
820          zsc_ws_1 = zws0
821       ENDWHERE
822
823       DO jj = 2, jpjm1
824          DO ji = 2, jpim1
825            IF ( lconv(ji,jj) ) THEN
826               DO jk = 2, imld(ji,jj)
827                  zznd_ml=gdepw_n(ji,jj,jk) / zhml(ji,jj)
828                  ghamt(ji,jj,jk) = ghamt(ji,jj,jk) + 0.3 * zsc_wth_1(ji,jj)                &
829                       &          * ( -2.0 + 2.75 * (       ( 1.0 + 0.6 * zznd_ml**4 )      &
830                       &                               - EXP(     - 6.0 * zznd_ml    ) ) )  &
831                       &          * ( 1.0 - EXP( - 15.0 * (         1.0 - zznd_ml    ) ) )
832                  !
833                  ghams(ji,jj,jk) = ghams(ji,jj,jk) + 0.3 * zsc_ws_1(ji,jj)  &
834                       &          * ( -2.0 + 2.75 * (       ( 1.0 + 0.6 * zznd_ml**4 )      &
835                       &                               - EXP(     - 6.0 * zznd_ml    ) ) )  &
836                       &          * ( 1.0 - EXP ( -15.0 * (         1.0 - zznd_ml    ) ) )
837               END DO
838            ELSE
839               DO jk = 2, ibld(ji,jj)
840                  zznd_d = gdepw_n(ji,jj,jk) / dstokes(ji,jj)
841                  znd = gdepw_n(ji,jj,jk) / zhbl(ji,jj)
842                  ghamt(ji,jj,jk) = ghamt(ji,jj,jk) + 0.3 * ( -4.06 * EXP( -2.0 * zznd_d ) * (1.0 - EXP( -4.0 * zznd_d ) ) + &
843               &  7.5 * EXP ( -10.0 * ( 0.95 - znd )**2 ) * ( 1.0 - znd ) ) * zsc_wth_1(ji,jj)
844                  ghams(ji,jj,jk) = ghams(ji,jj,jk) + 0.3 * ( -4.06 * EXP( -2.0 * zznd_d ) * (1.0 - EXP( -4.0 * zznd_d ) ) + &
845               &  7.5 * EXP ( -10.0 * ( 0.95 - znd )**2 ) * ( 1.0 - znd ) ) * zsc_ws_1(ji,jj)
846               END DO
847            ENDIF
848          ENDDO    ! ji loop
849       END DO      ! jj loop
850
851       WHERE ( lconv )
852          zsc_uw_1 = zustar**2
853          zsc_vw_1 = ff_t * zustke * zhml
854       ELSEWHERE
855          zsc_uw_1 = zustar**2
856          zsc_uw_2 = (2.25 - 3.0 * ( 1.0 - EXP( -1.25 * 2.0 ) ) ) * ( 1.0 - EXP( -4.0 * 2.0 ) ) * zsc_uw_1
857          zsc_vw_1 = ff_t * zustke * zhbl
858          zsc_vw_2 = -0.11 * SIN( 3.14159 * ( 2.0 + 0.4 ) ) * EXP(-( 1.5 + 2.0 )**2 ) * zsc_vw_1
859       ENDWHERE
860
861       DO jj = 2, jpjm1
862          DO ji = 2, jpim1
863             IF ( lconv(ji,jj) ) THEN
864               DO jk = 2, imld(ji,jj)
865                  zznd_ml = gdepw_n(ji,jj,jk) / zhml(ji,jj)
866                  zznd_d = gdepw_n(ji,jj,jk) / dstokes(ji,jj)
867                  ghamu(ji,jj,jk) = ghamu(ji,jj,jk)&
868                       & + 0.3 * ( -2.0 + 2.5 * ( 1.0 + 0.1 * zznd_ml**4 ) - EXP ( -8.0 * zznd_ml ) ) * zsc_uw_1(ji,jj)
869                  !
870                  ghamv(ji,jj,jk) = ghamv(ji,jj,jk)&
871                       & + 0.3 * 0.1 * ( EXP( -zznd_d ) + EXP( -5.0 * ( 1.0 - zznd_ml ) ) ) * zsc_vw_1(ji,jj)
872               END DO
873             ELSE
874               DO jk = 2, ibld(ji,jj)
875                  znd = gdepw_n(ji,jj,jk) / zhbl(ji,jj)
876                  zznd_d = gdepw_n(ji,jj,jk) / dstokes(ji,jj)
877                  IF ( zznd_d <= 2.0 ) THEN
878                     ghamu(ji,jj,jk) = ghamu(ji,jj,jk) + 0.5 * 0.3 &
879                          &*  ( 2.25 - 3.0  * ( 1.0 - EXP( - 1.25 * zznd_d ) ) * ( 1.0 - EXP( -2.0 * zznd_d ) ) ) * zsc_uw_1(ji,jj)
880                     !
881                  ELSE
882                     ghamu(ji,jj,jk) = ghamu(ji,jj,jk)&
883                          & + 0.5 * 0.3 * ( 1.0 - EXP( -5.0 * ( 1.0 - znd ) ) ) * zsc_uw_2(ji,jj)
884                     !
885                  ENDIF
886
887                  ghamv(ji,jj,jk) = ghamv(ji,jj,jk)&
888                       & + 0.3 * 0.15 * SIN( 3.14159 * ( 0.65 * zznd_d ) ) * EXP( -0.25 * zznd_d**2 ) * zsc_vw_1(ji,jj)
889                  ghamv(ji,jj,jk) = ghamv(ji,jj,jk)&
890                       & + 0.3 * 0.15 * EXP( -5.0 * ( 1.0 - znd ) ) * ( 1.0 - EXP( -20.0 * ( 1.0 - znd ) ) ) * zsc_vw_2(ji,jj)
891               END DO
892             ENDIF
893          END DO
894       END DO
895
896       IF(ln_dia_osm) THEN
897          IF ( iom_use("ghamu_f") ) CALL iom_put( "ghamu_f", wmask*ghamu )
898          IF ( iom_use("ghamv_f") ) CALL iom_put( "ghamv_f", wmask*ghamv )
899          IF ( iom_use("zsc_uw_1_f") ) CALL iom_put( "zsc_uw_1_f", tmask(:,:,1)*zsc_uw_1 )
900          IF ( iom_use("zsc_vw_1_f") ) CALL iom_put( "zsc_vw_1_f", tmask(:,:,1)*zsc_vw_1 )
901          IF ( iom_use("zsc_uw_2_f") ) CALL iom_put( "zsc_uw_2_f", tmask(:,:,1)*zsc_uw_2 )
902          IF ( iom_use("zsc_vw_2_f") ) CALL iom_put( "zsc_vw_2_f", tmask(:,:,1)*zsc_vw_2 )
903       END IF
904!
905! Make surface forced velocity non-gradient terms go to zero at the base of the mixed layer.
906
907      DO jj = 2, jpjm1
908         DO ji = 2, jpim1
909            IF ( lconv(ji,jj) ) THEN
910               DO jk = 2, ibld(ji,jj)
911                  znd = ( gdepw_n(ji,jj,jk) - zhml(ji,jj) ) / zhml(ji,jj) !ALMG to think about
912                  IF ( znd >= 0.0 ) THEN
913                     ghamu(ji,jj,jk) = ghamu(ji,jj,jk) * ( 1.0 - EXP( -30.0 * znd**2 ) )
914                     ghamv(ji,jj,jk) = ghamv(ji,jj,jk) * ( 1.0 - EXP( -30.0 * znd**2 ) )
915                  ELSE
916                     ghamu(ji,jj,jk) = 0._wp
917                     ghamv(ji,jj,jk) = 0._wp
918                  ENDIF
919               END DO
920            ELSE
921               DO jk = 2, ibld(ji,jj)
922                  znd = ( gdepw_n(ji,jj,jk) - zhml(ji,jj) ) / zhml(ji,jj) !ALMG to think about
923                  IF ( znd >= 0.0 ) THEN
924                     ghamu(ji,jj,jk) = ghamu(ji,jj,jk) * ( 1.0 - EXP( -10.0 * znd**2 ) )
925                     ghamv(ji,jj,jk) = ghamv(ji,jj,jk) * ( 1.0 - EXP( -10.0 * znd**2 ) )
926                  ELSE
927                     ghamu(ji,jj,jk) = 0._wp
928                     ghamv(ji,jj,jk) = 0._wp
929                  ENDIF
930               END DO
931            ENDIF
932         END DO
933      END DO
934
935       IF(ln_dia_osm) THEN
936          IF ( iom_use("ghamu_b") ) CALL iom_put( "ghamu_b", wmask*ghamu )
937          IF ( iom_use("ghamv_b") ) CALL iom_put( "ghamv_b", wmask*ghamv )
938       END IF
939      ! pynocline contributions
940       ! Temporary fix to avoid instabilities when zdb_bl becomes very very small
941       zsc_uw_1 = 0._wp ! 50.0 * zla**(8.0/3.0) * zustar**2 * zhbl / ( zdb_bl + epsln )
942       DO jj = 2, jpjm1
943          DO ji = 2, jpim1
944             IF ( ibld(ji,jj) + ibld_ext < mbkt(ji,jj) ) THEN
945                DO jk= 2, ibld(ji,jj)
946                   znd = gdepw_n(ji,jj,jk) / zhbl(ji,jj)
947                   ghamt(ji,jj,jk) = ghamt(ji,jj,jk) + zdiffut(ji,jj,jk) * zdtdz_pyc(ji,jj,jk)
948                   ghams(ji,jj,jk) = ghams(ji,jj,jk) + zdiffut(ji,jj,jk) * zdsdz_pyc(ji,jj,jk)
949                   ghamu(ji,jj,jk) = ghamu(ji,jj,jk) + zviscos(ji,jj,jk) * zdudz_pyc(ji,jj,jk)
950                   ghamu(ji,jj,jk) = ghamu(ji,jj,jk) + zsc_uw_1(ji,jj) * ( 1.0 - znd )**(7.0/4.0) * zdbdz_pyc(ji,jj,jk)
951                   ghamv(ji,jj,jk) = ghamv(ji,jj,jk) + zviscos(ji,jj,jk) * zdvdz_pyc(ji,jj,jk)
952                END DO
953             END IF
954          END DO
955       END DO
956
957! Entrainment contribution.
958
959       DO jj=2, jpjm1
960          DO ji = 2, jpim1
961            IF ( lconv(ji,jj) .AND. ibld(ji,jj) + ibld_ext < mbkt(ji,jj)) THEN
962               DO jk = 1, imld(ji,jj) - 1
963                  znd=gdepw_n(ji,jj,jk) / zhml(ji,jj)
964                  ! ghamt(ji,jj,jk) = ghamt(ji,jj,jk) + zwth_ent(ji,jj) * znd
965                  ! ghams(ji,jj,jk) = ghams(ji,jj,jk) + zws_ent(ji,jj) * znd
966                  ghamu(ji,jj,jk) = ghamu(ji,jj,jk) + zuw_bse(ji,jj) * znd
967                  ghamv(ji,jj,jk) = ghamv(ji,jj,jk) + zvw_bse(ji,jj) * znd
968               END DO
969               DO jk = imld(ji,jj), ibld(ji,jj)
970                  znd = -( gdepw_n(ji,jj,jk) - zhml(ji,jj) ) / zdh(ji,jj)
971                  ! ghamt(ji,jj,jk) = ghamt(ji,jj,jk) + zwth_ent(ji,jj) * ( 1.0 + znd )
972                  ! ghams(ji,jj,jk) = ghams(ji,jj,jk) + zws_ent(ji,jj) * ( 1.0 + znd )
973                  ghamu(ji,jj,jk) = ghamu(ji,jj,jk) + zuw_bse(ji,jj) * ( 1.0 + znd )
974                  ghamv(ji,jj,jk) = ghamv(ji,jj,jk) + zvw_bse(ji,jj) * ( 1.0 + znd )
975                END DO
976             ENDIF
977
978             ghamt(ji,jj,ibld(ji,jj)+ibld_ext) = 0._wp
979             ghams(ji,jj,ibld(ji,jj)+ibld_ext) = 0._wp
980             ghamu(ji,jj,ibld(ji,jj)+ibld_ext) = 0._wp
981             ghamv(ji,jj,ibld(ji,jj)+ibld_ext) = 0._wp
982          END DO       ! ji loop
983       END DO          ! jj loop
984
985       IF(ln_dia_osm) THEN
986          IF ( iom_use("ghamu_1") ) CALL iom_put( "ghamu_1", wmask*ghamu )
987          IF ( iom_use("ghamv_1") ) CALL iom_put( "ghamv_1", wmask*ghamv )
988          IF ( iom_use("zuw_bse") ) CALL iom_put( "zuw_bse", tmask(:,:,1)*zuw_bse )
989          IF ( iom_use("zvw_bse") ) CALL iom_put( "zvw_bse", tmask(:,:,1)*zvw_bse )
990          IF ( iom_use("zdudz_pyc") ) CALL iom_put( "zdudz_pyc", wmask*zdudz_pyc )
991          IF ( iom_use("zdvdz_pyc") ) CALL iom_put( "zdvdz_pyc", wmask*zdvdz_pyc )
992          IF ( iom_use("zviscos") ) CALL iom_put( "zviscos", wmask*zviscos )
993       END IF
994       !>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
995       ! Need to put in code for contributions that are applied explicitly to
996       ! the prognostic variables
997       !  1. Entrainment flux
998       !
999       !<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1000
1001
1002
1003       ! rotate non-gradient velocity terms back to model reference frame
1004
1005       DO jj = 2, jpjm1
1006          DO ji = 2, jpim1
1007             DO jk = 2, ibld(ji,jj)
1008                ztemp = ghamu(ji,jj,jk)
1009                ghamu(ji,jj,jk) = ghamu(ji,jj,jk) * zcos_wind(ji,jj) - ghamv(ji,jj,jk) * zsin_wind(ji,jj)
1010                ghamv(ji,jj,jk) = ghamv(ji,jj,jk) * zcos_wind(ji,jj) + ztemp * zsin_wind(ji,jj)
1011             END DO
1012          END DO
1013       END DO
1014
1015       IF(ln_dia_osm) THEN
1016          IF ( iom_use("zdtdz_pyc") ) CALL iom_put( "zdtdz_pyc", wmask*zdtdz_pyc )
1017          IF ( iom_use("zdsdz_pyc") ) CALL iom_put( "zdsdz_pyc", wmask*zdsdz_pyc )
1018          IF ( iom_use("zdbdz_pyc") ) CALL iom_put( "zdbdz_pyc", wmask*zdbdz_pyc )
1019       END IF
1020
1021! KPP-style Ri# mixing
1022       IF( ln_kpprimix) THEN
1023          DO jk = 2, jpkm1           !* Shear production at uw- and vw-points (energy conserving form)
1024             DO jj = 1, jpjm1
1025                DO ji = 1, jpim1   ! vector opt.
1026                   z3du(ji,jj,jk) = 0.5 * (  un(ji,jj,jk-1) -  un(ji  ,jj,jk) )   &
1027                        &                 * (  ub(ji,jj,jk-1) -  ub(ji  ,jj,jk) ) * wumask(ji,jj,jk) &
1028                        &                 / (  e3uw_n(ji,jj,jk) * e3uw_b(ji,jj,jk) )
1029                   z3dv(ji,jj,jk) = 0.5 * (  vn(ji,jj,jk-1) -  vn(ji,jj  ,jk) )   &
1030                        &                 * (  vb(ji,jj,jk-1) -  vb(ji,jj  ,jk) ) * wvmask(ji,jj,jk) &
1031                        &                 / (  e3vw_n(ji,jj,jk) * e3vw_b(ji,jj,jk) )
1032                END DO
1033             END DO
1034          END DO
1035      !
1036         DO jk = 2, jpkm1
1037            DO jj = 2, jpjm1
1038               DO ji = 2, jpim1   ! vector opt.
1039                  !                                          ! shear prod. at w-point weightened by mask
1040                  zesh2  =  ( z3du(ji-1,jj,jk) + z3du(ji,jj,jk) ) / MAX( 1._wp , umask(ji-1,jj,jk) + umask(ji,jj,jk) )   &
1041                     &    + ( z3dv(ji,jj-1,jk) + z3dv(ji,jj,jk) ) / MAX( 1._wp , vmask(ji,jj-1,jk) + vmask(ji,jj,jk) )
1042                  !                                          ! local Richardson number
1043                  zri   = MAX( rn2b(ji,jj,jk), 0._wp ) / MAX(zesh2, epsln)
1044                  zfri =  MIN( zri / rn_riinfty , 1.0_wp )
1045                  zfri  = ( 1.0_wp - zfri * zfri )
1046                  zrimix(ji,jj,jk)  =  zfri * zfri  * zfri * wmask(ji, jj, jk)
1047                END DO
1048             END DO
1049          END DO
1050
1051          DO jj = 2, jpjm1
1052             DO ji = 2, jpim1
1053                DO jk = ibld(ji,jj) + 1, jpkm1
1054                   zdiffut(ji,jj,jk) = zrimix(ji,jj,jk)*rn_difri
1055                   zviscos(ji,jj,jk) = zrimix(ji,jj,jk)*rn_difri
1056                END DO
1057             END DO
1058          END DO
1059
1060       END IF ! ln_kpprimix = .true.
1061
1062! KPP-style set diffusivity large if unstable below BL
1063       IF( ln_convmix) THEN
1064          DO jj = 2, jpjm1
1065             DO ji = 2, jpim1
1066                DO jk = ibld(ji,jj) + 1, jpkm1
1067                  IF(  MIN( rn2(ji,jj,jk), rn2b(ji,jj,jk) ) <= -1.e-12 ) zdiffut(ji,jj,jk) = rn_difconv
1068                END DO
1069             END DO
1070          END DO
1071       END IF ! ln_convmix = .true.
1072
1073
1074
1075       IF ( ln_osm_mle ) THEN  ! set up diffusivity and non-gradient mixing
1076          DO jj = 2 , jpjm1
1077             DO ji = 2, jpim1
1078                IF ( lconv(ji,jj) .AND. mld_prof(ji,jj) - ibld(ji,jj) > 1 ) THEN ! MLE mmixing extends below the OSBL.
1079            ! Calculate MLE flux profiles
1080                   ! DO jk = 1, mld_prof(ji,jj)
1081                   !    znd = - gdepw_n(ji,jj,jk) / MAX(zhmle(ji,jj),epsln)
1082                   !    ghamt(ji,jj,jk) = ghamt(ji,jj,jk) + &
1083                   !         & zwt_fk(ji,jj) * ( 1.0 - ( 2.0 * znd + 1.0 )**2 ) * ( 1.0 + r5_21 * ( 2.0 * znd + 1.0 )**2 )
1084                   !    ghams(ji,jj,jk) = ghams(ji,jj,jk) + &
1085                   !         & zws_fk(ji,jj) * ( 1.0 - ( 2.0 * znd + 1.0 )**2 ) * ( 1.0 + r5_21 * ( 2.0 * znd + 1.0 )**2 )
1086                   ! END DO
1087            ! Calculate MLE flux contribution from surface fluxes
1088                   DO jk = 1, ibld(ji,jj)
1089                     znd = gdepw_n(ji,jj,jk) / MAX(zhbl(ji,jj),epsln)
1090                     ghamt(ji,jj,jk) = ghamt(ji,jj,jk) - zwth0(ji,jj) * ( 1.0 - znd )
1091                     ghams(ji,jj,jk) = ghams(ji,jj,jk) - zws0(ji,jj) * ( 1.0 - znd )
1092                    END DO
1093                    DO jk = 1, mld_prof(ji,jj)
1094                      znd = gdepw_n(ji,jj,jk) / MAX(zhmle(ji,jj),epsln)
1095                      ghamt(ji,jj,jk) = ghamt(ji,jj,jk) + zwth0(ji,jj) * ( 1.0 - znd )
1096                      ghams(ji,jj,jk) = ghams(ji,jj,jk) + zws0(ji,jj) * ( 1.0 -znd )
1097                    END DO
1098            ! Viscosity for MLEs
1099                    DO jk = ibld(ji,jj), mld_prof(ji,jj)
1100                      zdiffut(ji,jj,jk) = MAX( zdiffut(ji,jj,jk), zdiff_mle(ji,jj) )
1101                    END DO
1102            ! Iterate to find approx vertical index for depth 1.1*zhmle(ji,jj)
1103                    jl = MIN(mld_prof(ji,jj) + 2, mbkt(ji,jj))
1104                    jl = MIN( MAX(INT( 0.1 * zhmle(ji,jj) / e3t_n(ji,jj,jl)), 2 ) + mld_prof(ji,jj), mbkt(ji,jj))
1105                    DO jk = mld_prof(ji,jj),  jl
1106                       zdiffut(ji,jj,jk) = MAX( zdiffut(ji,jj,jk), zdiff_mle(ji,jj) * &
1107                            & ( gdepw_n(ji,jj,jk) - gdepw_n(ji,jj,jl) ) / &
1108                            & ( gdepw_n(ji,jj,mld_prof(ji,jj)) - gdepw_n(ji,jj,jl) - epsln))
1109                    END DO
1110                ENDIF
1111            END DO
1112          END DO
1113       ENDIF
1114
1115       IF(ln_dia_osm) THEN
1116          IF ( iom_use("zdtdz_pyc") ) CALL iom_put( "zdtdz_pyc", wmask*zdtdz_pyc )
1117          IF ( iom_use("zdsdz_pyc") ) CALL iom_put( "zdsdz_pyc", wmask*zdsdz_pyc )
1118          IF ( iom_use("zdbdz_pyc") ) CALL iom_put( "zdbdz_pyc", wmask*zdbdz_pyc )
1119       END IF
1120
1121
1122       ! Lateral boundary conditions on zvicos (sign unchanged), needed to caclulate viscosities on u and v grids
1123       !CALL lbc_lnk( zviscos(:,:,:), 'W', 1. )
1124
1125       ! GN 25/8: need to change tmask --> wmask
1126
1127     DO jk = 2, jpkm1
1128         DO jj = 2, jpjm1
1129             DO ji = 2, jpim1
1130                p_avt(ji,jj,jk) = MAX( zdiffut(ji,jj,jk), avtb(jk) ) * tmask(ji,jj,jk)
1131                p_avm(ji,jj,jk) = MAX( zviscos(ji,jj,jk), avmb(jk) ) * tmask(ji,jj,jk)
1132             END DO
1133         END DO
1134     END DO
1135      ! Lateral boundary conditions on ghamu and ghamv, currently on W-grid  (sign unchanged), needed to caclulate gham[uv] on u and v grids
1136     CALL lbc_lnk_multi( 'zdfosm', p_avt, 'W', 1. , p_avm, 'W', 1.,   &
1137      &                  ghamu, 'W', 1. , ghamv, 'W', 1. )
1138       DO jk = 2, jpkm1
1139           DO jj = 2, jpjm1
1140               DO ji = 2, jpim1
1141                  ghamu(ji,jj,jk) = ( ghamu(ji,jj,jk) + ghamu(ji+1,jj,jk) ) &
1142                     &  / MAX( 1., tmask(ji,jj,jk) + tmask (ji + 1,jj,jk) ) * umask(ji,jj,jk)
1143
1144                  ghamv(ji,jj,jk) = ( ghamv(ji,jj,jk) + ghamv(ji,jj+1,jk) ) &
1145                      &  / MAX( 1., tmask(ji,jj,jk) + tmask (ji,jj+1,jk) ) * vmask(ji,jj,jk)
1146
1147                  ghamt(ji,jj,jk) =  ghamt(ji,jj,jk) * tmask(ji,jj,jk)
1148                  ghams(ji,jj,jk) =  ghams(ji,jj,jk) * tmask(ji,jj,jk)
1149               END DO
1150           END DO
1151        END DO
1152        ! Lateral boundary conditions on final outputs for hbl,  on T-grid (sign unchanged)
1153        CALL lbc_lnk_multi( 'zdfosm', hbl, 'T', 1., dh, 'T', 1., hmle, 'T', 1. )
1154        ! Lateral boundary conditions on final outputs for gham[ts],  on W-grid  (sign unchanged)
1155        ! Lateral boundary conditions on final outputs for gham[uv],  on [UV]-grid  (sign unchanged)
1156        CALL lbc_lnk_multi( 'zdfosm', ghamt, 'W', 1. , ghams, 'W', 1.,   &
1157         &                  ghamu, 'U', -1. , ghamv, 'V', -1. )
1158
1159      IF(ln_dia_osm) THEN
1160         SELECT CASE (nn_osm_wave)
1161         ! Stokes drift set by assumimg onstant La#=0.3(=0)  or Pierson-Moskovitz spectrum (=1).
1162         CASE(0:1)
1163            IF ( iom_use("us_x") ) CALL iom_put( "us_x", tmask(:,:,1)*zustke*zcos_wind )   ! x surface Stokes drift
1164            IF ( iom_use("us_y") ) CALL iom_put( "us_y", tmask(:,:,1)*zustke*zsin_wind )  ! y surface Stokes drift
1165            IF ( iom_use("wind_wave_abs_power") ) CALL iom_put( "wind_wave_abs_power", 1000.*rau0*tmask(:,:,1)*zustar**2*zustke )
1166         ! Stokes drift read in from sbcwave  (=2).
1167         CASE(2)
1168            IF ( iom_use("us_x") ) CALL iom_put( "us_x", ut0sd*umask(:,:,1) )               ! x surface Stokes drift
1169            IF ( iom_use("us_y") ) CALL iom_put( "us_y", vt0sd*vmask(:,:,1) )               ! y surface Stokes drift
1170            IF ( iom_use("wmp") ) CALL iom_put( "wmp", wmp*tmask(:,:,1) )                   ! wave mean period
1171            IF ( iom_use("hsw") ) CALL iom_put( "hsw", hsw*tmask(:,:,1) )                   ! significant wave height
1172            IF ( iom_use("wmp_NP") ) CALL iom_put( "wmp_NP", (2.*rpi*1.026/(0.877*grav) )*wndm*tmask(:,:,1) )                  ! wave mean period from NP spectrum
1173            IF ( iom_use("hsw_NP") ) CALL iom_put( "hsw_NP", (0.22/grav)*wndm**2*tmask(:,:,1) )                   ! significant wave height from NP spectrum
1174            IF ( iom_use("wndm") ) CALL iom_put( "wndm", wndm*tmask(:,:,1) )                   ! U_10
1175            IF ( iom_use("wind_wave_abs_power") ) CALL iom_put( "wind_wave_abs_power", 1000.*rau0*tmask(:,:,1)*zustar**2* &
1176                 & SQRT(ut0sd**2 + vt0sd**2 ) )
1177         END SELECT
1178         IF ( iom_use("ghamt") ) CALL iom_put( "ghamt", tmask*ghamt )            ! <Tw_NL>
1179         IF ( iom_use("ghams") ) CALL iom_put( "ghams", tmask*ghams )            ! <Sw_NL>
1180         IF ( iom_use("ghamu") ) CALL iom_put( "ghamu", umask*ghamu )            ! <uw_NL>
1181         IF ( iom_use("ghamv") ) CALL iom_put( "ghamv", vmask*ghamv )            ! <vw_NL>
1182         IF ( iom_use("zwth0") ) CALL iom_put( "zwth0", tmask(:,:,1)*zwth0 )            ! <Tw_0>
1183         IF ( iom_use("zws0") ) CALL iom_put( "zws0", tmask(:,:,1)*zws0 )                ! <Sw_0>
1184         IF ( iom_use("hbl") ) CALL iom_put( "hbl", tmask(:,:,1)*hbl )                  ! boundary-layer depth
1185         IF ( iom_use("ibld") ) CALL iom_put( "ibld", tmask(:,:,1)*ibld )               ! boundary-layer max k
1186         IF ( iom_use("zdt_bl") ) CALL iom_put( "zdt_bl", tmask(:,:,1)*zdt_bl )           ! dt at ml base
1187         IF ( iom_use("zds_bl") ) CALL iom_put( "zds_bl", tmask(:,:,1)*zds_bl )           ! ds at ml base
1188         IF ( iom_use("zdb_bl") ) CALL iom_put( "zdb_bl", tmask(:,:,1)*zdb_bl )           ! db at ml base
1189         IF ( iom_use("zdu_bl") ) CALL iom_put( "zdu_bl", tmask(:,:,1)*zdu_bl )           ! du at ml base
1190         IF ( iom_use("zdv_bl") ) CALL iom_put( "zdv_bl", tmask(:,:,1)*zdv_bl )           ! dv at ml base
1191         IF ( iom_use("dh") ) CALL iom_put( "dh", tmask(:,:,1)*dh )               ! Initial boundary-layer depth
1192         IF ( iom_use("hml") ) CALL iom_put( "hml", tmask(:,:,1)*hml )               ! Initial boundary-layer depth
1193         IF ( iom_use("dstokes") ) CALL iom_put( "dstokes", tmask(:,:,1)*dstokes )      ! Stokes drift penetration depth
1194         IF ( iom_use("zustke") ) CALL iom_put( "zustke", tmask(:,:,1)*zustke )            ! Stokes drift magnitude at T-points
1195         IF ( iom_use("zwstrc") ) CALL iom_put( "zwstrc", tmask(:,:,1)*zwstrc )         ! convective velocity scale
1196         IF ( iom_use("zwstrl") ) CALL iom_put( "zwstrl", tmask(:,:,1)*zwstrl )         ! Langmuir velocity scale
1197         IF ( iom_use("zustar") ) CALL iom_put( "zustar", tmask(:,:,1)*zustar )         ! friction velocity scale
1198         IF ( iom_use("zvstr") ) CALL iom_put( "zvstr", tmask(:,:,1)*zvstr )         ! mixed velocity scale
1199         IF ( iom_use("zla") ) CALL iom_put( "zla", tmask(:,:,1)*zla )         ! langmuir #
1200         IF ( iom_use("wind_power") ) CALL iom_put( "wind_power", 1000.*rau0*tmask(:,:,1)*zustar**3 ) ! BL depth internal to zdf_osm routine
1201         IF ( iom_use("wind_wave_power") ) CALL iom_put( "wind_wave_power", 1000.*rau0*tmask(:,:,1)*zustar**2*zustke )
1202         IF ( iom_use("zhbl") ) CALL iom_put( "zhbl", tmask(:,:,1)*zhbl )               ! BL depth internal to zdf_osm routine
1203         IF ( iom_use("zhml") ) CALL iom_put( "zhml", tmask(:,:,1)*zhml )               ! ML depth internal to zdf_osm routine
1204         IF ( iom_use("imld") ) CALL iom_put( "imld", tmask(:,:,1)*imld )               ! index for ML depth internal to zdf_osm routine
1205         IF ( iom_use("zdh") ) CALL iom_put( "zdh", tmask(:,:,1)*zdh )                  ! pyc thicknessh internal to zdf_osm routine
1206         IF ( iom_use("zhol") ) CALL iom_put( "zhol", tmask(:,:,1)*zhol )               ! ML depth internal to zdf_osm routine
1207         IF ( iom_use("zwthav") ) CALL iom_put( "zwthav", tmask(:,:,1)*zwthav )         ! upward BL-avged turb temp flux
1208         IF ( iom_use("zwth_ent") ) CALL iom_put( "zwth_ent", tmask(:,:,1)*zwth_ent )   ! upward turb temp entrainment flux
1209         IF ( iom_use("zwb_ent") ) CALL iom_put( "zwb_ent", tmask(:,:,1)*zwb_ent )      ! upward turb buoyancy entrainment flux
1210         IF ( iom_use("zws_ent") ) CALL iom_put( "zws_ent", tmask(:,:,1)*zws_ent )      ! upward turb salinity entrainment flux
1211         IF ( iom_use("zt_ml") ) CALL iom_put( "zt_ml", tmask(:,:,1)*zt_ml )            ! average T in ML
1212
1213         IF ( iom_use("hmle") ) CALL iom_put( "hmle", tmask(:,:,1)*hmle )               ! FK layer depth
1214         IF ( iom_use("zmld") ) CALL iom_put( "zmld", tmask(:,:,1)*zmld )               ! FK target layer depth
1215         IF ( iom_use("zwb_fk") ) CALL iom_put( "zwb_fk", tmask(:,:,1)*zwb_fk )         ! FK b flux
1216         IF ( iom_use("zwb_fk_b") ) CALL iom_put( "zwb_fk_b", tmask(:,:,1)*zwb_fk_b )   ! FK b flux averaged over ML
1217         IF ( iom_use("mld_prof") ) CALL iom_put( "mld_prof", tmask(:,:,1)*mld_prof )! FK layer max k
1218         IF ( iom_use("zdtdx") ) CALL iom_put( "zdtdx", umask(:,:,1)*zdtdx )            ! FK dtdx at u-pt
1219         IF ( iom_use("zdtdy") ) CALL iom_put( "zdtdy", vmask(:,:,1)*zdtdy )            ! FK dtdy at v-pt
1220         IF ( iom_use("zdsdx") ) CALL iom_put( "zdsdx", umask(:,:,1)*zdsdx )            ! FK dtdx at u-pt
1221         IF ( iom_use("zdsdy") ) CALL iom_put( "zdsdy", vmask(:,:,1)*zdsdy )            ! FK dsdy at v-pt
1222         IF ( iom_use("dbdx_mle") ) CALL iom_put( "dbdx_mle", umask(:,:,1)*dbdx_mle )            ! FK dbdx at u-pt
1223         IF ( iom_use("dbdy_mle") ) CALL iom_put( "dbdy_mle", vmask(:,:,1)*dbdy_mle )            ! FK dbdy at v-pt
1224         IF ( iom_use("zdiff_mle") ) CALL iom_put( "zdiff_mle", tmask(:,:,1)*zdiff_mle )! FK diff in MLE at t-pt
1225         IF ( iom_use("zvel_mle") ) CALL iom_put( "zvel_mle", tmask(:,:,1)*zdiff_mle )! FK diff in MLE at t-pt
1226
1227      END IF
1228
1229CONTAINS
1230
1231
1232   ! Alan: do we need zb?
1233   SUBROUTINE zdf_osm_vertical_average( jnlev_av, zt, zs, zu, zv, zdt, zds, zdb, zdu, zdv )
1234     !!---------------------------------------------------------------------
1235     !!                   ***  ROUTINE zdf_vertical_average  ***
1236     !!
1237     !! ** Purpose : Determines vertical averages from surface to jnlev.
1238     !!
1239     !! ** Method  : Averages are calculated from the surface to jnlev.
1240     !!              The external level used to calculate differences is ibld+ibld_ext
1241     !!
1242     !!----------------------------------------------------------------------
1243
1244        INTEGER, DIMENSION(jpi,jpj) :: jnlev_av  ! Number of levels to average over.
1245
1246        ! Alan: do we need zb?
1247        REAL(wp), DIMENSION(jpi,jpj) :: zt, zs        ! Average temperature and salinity
1248        REAL(wp), DIMENSION(jpi,jpj) :: zu,zv         ! Average current components
1249        REAL(wp), DIMENSION(jpi,jpj) :: zdt, zds, zdb ! Difference between average and value at base of OSBL
1250        REAL(wp), DIMENSION(jpi,jpj) :: zdu, zdv      ! Difference for velocity components.
1251
1252        INTEGER :: jk, ji, jj
1253        REAL(wp) :: zthick, zthermal, zbeta
1254
1255
1256        zt   = 0._wp
1257        zs   = 0._wp
1258        zu   = 0._wp
1259        zv   = 0._wp
1260        DO jj = 2, jpjm1                                 !  Vertical slab
1261         DO ji = 2, jpim1
1262            zthermal = rab_n(ji,jj,1,jp_tem) !ideally use ibld not 1??
1263            zbeta    = rab_n(ji,jj,1,jp_sal)
1264               ! average over depth of boundary layer
1265            zthick = epsln
1266            DO jk = 2, jnlev_av(ji,jj)
1267               zthick = zthick + e3t_n(ji,jj,jk)
1268               zt(ji,jj)   = zt(ji,jj)  + e3t_n(ji,jj,jk) * tsn(ji,jj,jk,jp_tem)
1269               zs(ji,jj)   = zs(ji,jj)  + e3t_n(ji,jj,jk) * tsn(ji,jj,jk,jp_sal)
1270               zu(ji,jj)   = zu(ji,jj)  + e3t_n(ji,jj,jk) &
1271                     &            * ( ub(ji,jj,jk) + ub(ji - 1,jj,jk) ) &
1272                     &            / MAX( 1. , umask(ji,jj,jk) + umask(ji - 1,jj,jk) )
1273               zv(ji,jj)   = zv(ji,jj)  + e3t_n(ji,jj,jk) &
1274                     &            * ( vb(ji,jj,jk) + vb(ji,jj - 1,jk) ) &
1275                     &            / MAX( 1. , vmask(ji,jj,jk) + vmask(ji,jj - 1,jk) )
1276            END DO
1277            zt(ji,jj) = zt(ji,jj) / zthick
1278            zs(ji,jj) = zs(ji,jj) / zthick
1279            zu(ji,jj) = zu(ji,jj) / zthick
1280            zv(ji,jj) = zv(ji,jj) / zthick
1281            ! Alan: do we need zb?
1282            zdt(ji,jj) = zt(ji,jj) - tsn(ji,jj,ibld(ji,jj)+ibld_ext,jp_tem)
1283            zds(ji,jj) = zs(ji,jj) - tsn(ji,jj,ibld(ji,jj)+ibld_ext,jp_sal)
1284            zdu(ji,jj) = zu(ji,jj) - ( ub(ji,jj,ibld(ji,jj)+ibld_ext) + ub(ji-1,jj,ibld(ji,jj)+ibld_ext ) ) &
1285                     &    / MAX(1. , umask(ji,jj,ibld(ji,jj)+ibld_ext ) + umask(ji-1,jj,ibld(ji,jj)+ibld_ext ) )
1286            zdv(ji,jj) = zv(ji,jj) - ( vb(ji,jj,ibld(ji,jj)+ibld_ext) + vb(ji,jj-1,ibld(ji,jj)+ibld_ext ) ) &
1287                     &   / MAX(1. , vmask(ji,jj,ibld(ji,jj)+ibld_ext ) + vmask(ji,jj-1,ibld(ji,jj)+ibld_ext ) )
1288            zdb(ji,jj) = grav * zthermal * zdt(ji,jj) - grav * zbeta * zds(ji,jj)
1289         END DO
1290        END DO
1291   END SUBROUTINE zdf_osm_vertical_average
1292
1293   SUBROUTINE zdf_osm_velocity_rotation( zcos_w, zsin_w, zu, zv, zdu, zdv )
1294     !!---------------------------------------------------------------------
1295     !!                   ***  ROUTINE zdf_velocity_rotation  ***
1296     !!
1297     !! ** Purpose : Rotates frame of reference of averaged velocity components.
1298     !!
1299     !! ** Method  : The velocity components are rotated into frame specified by zcos_w and zsin_w
1300     !!
1301     !!----------------------------------------------------------------------
1302
1303        REAL(wp), DIMENSION(jpi,jpj) :: zcos_w, zsin_w       ! Cos and Sin of rotation angle
1304        REAL(wp), DIMENSION(jpi,jpj) :: zu, zv               ! Components of current
1305        REAL(wp), DIMENSION(jpi,jpj) :: zdu, zdv             ! Change in velocity components across pycnocline
1306
1307        INTEGER :: ji, jj
1308        REAL(wp) :: ztemp
1309
1310        DO jj = 2, jpjm1
1311           DO ji = 2, jpim1
1312              ztemp = zu(ji,jj)
1313              zu(ji,jj) = zu(ji,jj) * zcos_w(ji,jj) + zv(ji,jj) * zsin_w(ji,jj)
1314              zv(ji,jj) = zv(ji,jj) * zcos_w(ji,jj) - ztemp * zsin_w(ji,jj)
1315              ztemp = zdu(ji,jj)
1316              zdu(ji,jj) = zdu(ji,jj) * zcos_w(ji,jj) + zdv(ji,jj) * zsin_w(ji,jj)
1317              zdv(ji,jj) = zdv(ji,jj) * zsin_w(ji,jj) - ztemp * zsin_w(ji,jj)
1318           END DO
1319        END DO
1320    END SUBROUTINE zdf_osm_velocity_rotation
1321
1322    SUBROUTINE zdf_osm_external_gradients( zdtdz, zdsdz, zdbdz )
1323     !!---------------------------------------------------------------------
1324     !!                   ***  ROUTINE zdf_osm_external_gradients  ***
1325     !!
1326     !! ** Purpose : Calculates the gradients below the OSBL
1327     !!
1328     !! ** Method  : Uses ibld and ibld_ext to determine levels to calculate the gradient.
1329     !!
1330     !!----------------------------------------------------------------------
1331
1332     REAL(wp), DIMENSION(jpi,jpj) :: zdtdz, zdsdz, zdbdz   ! External gradients of temperature, salinity and buoyancy.
1333
1334     INTEGER :: jj, ji, jkb, jkb1
1335     REAL(wp) :: zthermal, zbeta
1336
1337
1338     DO jj = 2, jpjm1
1339        DO ji = 2, jpim1
1340           IF ( ibld(ji,jj) + ibld_ext < mbkt(ji,jj) ) THEN
1341              zthermal = rab_n(ji,jj,1,jp_tem) !ideally use ibld not 1??
1342              zbeta    = rab_n(ji,jj,1,jp_sal)
1343              jkb = ibld(ji,jj) + ibld_ext
1344              jkb1 = MIN(jkb + 1, mbkt(ji,jj))
1345              zdtdz(ji,jj) = - ( tsn(ji,jj,jkb1,jp_tem) - tsn(ji,jj,jkb,jp_tem ) ) &
1346                   &   / e3t_n(ji,jj,ibld(ji,jj))
1347              zdsdz(ji,jj) = - ( tsn(ji,jj,jkb1,jp_sal) - tsn(ji,jj,jkb,jp_sal ) ) &
1348                   &   / e3t_n(ji,jj,ibld(ji,jj))
1349              zdbdz(ji,jj) = grav * zthermal * zdtdz(ji,jj) - grav * zbeta * zdsdz(ji,jj)
1350           END IF
1351        END DO
1352     END DO
1353    END SUBROUTINE zdf_osm_external_gradients
1354
1355    SUBROUTINE zdf_osm_pycnocline_scalar_profiles( zdtdz, zdsdz, zdbdz )
1356
1357     REAL(wp), DIMENSION(jpi,jpj,jpk) :: zdtdz, zdsdz, zdbdz      ! gradients in the pycnocline
1358
1359     INTEGER :: jk, jj, ji
1360     REAL(wp) :: ztgrad, zsgrad, zbgrad
1361     REAL(wp) :: zgamma_b_nd, zgamma_c, znd
1362     REAL(wp) :: zzeta_s=0.3, ztmp
1363
1364     DO jj = 2, jpjm1
1365        DO ji = 2, jpim1
1366           IF ( ibld(ji,jj) + ibld_ext < mbkt(ji,jj) ) THEN
1367              IF ( lconv(ji,jj) ) THEN  ! convective conditions
1368                    IF ( zdbdz_ext(ji,jj) > 0._wp .AND. &
1369                         & (zdhdt(ji,jj) > 0._wp .AND. ln_osm_mle .AND. zdb_bl(ji,jj) > rn_osm_mle_thresh &
1370                         &  .OR.  zdb_bl(ji,jj) > 0._wp)) THEN  ! zdhdt could be <0 due to FK, hence check zdhdt>0
1371                       ztmp = 1._wp/MAX(zdh(ji,jj), epsln)
1372                       ztgrad = 0.5 * zdt_ml(ji,jj) * ztmp + zdtdz_ext(ji,jj)
1373                       zsgrad = 0.5 * zds_ml(ji,jj) * ztmp + zdsdz_ext(ji,jj)
1374                       zbgrad = 0.5 * zdb_ml(ji,jj) * ztmp + zdbdz_ext(ji,jj)
1375                       zgamma_b_nd = zdbdz_ext(ji,jj) * zdh(ji,jj) / MAX(zdb_ml(ji,jj), epsln)
1376                       zgamma_c = ( 3.14159 / 4.0 ) * ( 0.5 + zgamma_b_nd ) /&
1377                            & ( 1.0 - 0.25 * SQRT( 3.14159 / 6.0 ) - 2.0 * zgamma_b_nd * zzeta_s )**2 ! check
1378                       DO jk = 2, ibld(ji,jj)+ibld_ext
1379                          znd = -( gdepw_n(ji,jj,jk) - zhbl(ji,jj) ) * ztmp
1380                          IF ( znd <= zzeta_s ) THEN
1381                             zdtdz(ji,jj,jk) = zdtdz_ext(ji,jj) + 0.5 * zdt_ml(ji,jj) * ztmp * &
1382                                  & EXP( -6.0 * ( znd -zzeta_s )**2 )
1383                             zdsdz(ji,jj,jk) = zdsdz_ext(ji,jj) + 0.5 * zds_ml(ji,jj) * ztmp * &
1384                                  & EXP( -6.0 * ( znd -zzeta_s )**2 )
1385                             zdbdz(ji,jj,jk) = zdbdz_ext(ji,jj) + 0.5 * zdb_ml(ji,jj) * ztmp * &
1386                                  & EXP( -6.0 * ( znd -zzeta_s )**2 )
1387                          ELSE
1388                             zdtdz(ji,jj,jk) =  ztgrad * EXP( -zgamma_c * ( znd - zzeta_s )**2 )
1389                             zdsdz(ji,jj,jk) =  zsgrad * EXP( -zgamma_c * ( znd - zzeta_s )**2 )
1390                             zdbdz(ji,jj,jk) =  zbgrad * EXP( -zgamma_c * ( znd - zzeta_s )**2 )
1391                          ENDIF
1392                       END DO
1393                    ENDIF ! If condition not satisfied, no pycnocline present. Gradients have been initialised to zero, so do nothing
1394              ELSE
1395                 ! stable conditions
1396                 ! if pycnocline profile only defined when depth steady of increasing.
1397                 IF ( zdhdt(ji,jj) >= 0.0 ) THEN        ! Depth increasing, or steady.
1398                    IF ( zdb_bl(ji,jj) > 0._wp ) THEN
1399                       IF ( zhol(ji,jj) >= 0.5 ) THEN      ! Very stable - 'thick' pycnocline
1400                          ztmp = 1._wp/MAX(zhbl(ji,jj), epsln)
1401                          ztgrad = zdt_bl(ji,jj) * ztmp
1402                          zsgrad = zds_bl(ji,jj) * ztmp
1403                          zbgrad = zdb_bl(ji,jj) * ztmp
1404                          DO jk = 2, ibld(ji,jj)
1405                             znd = gdepw_n(ji,jj,jk) * ztmp
1406                             zdtdz(ji,jj,jk) =  ztgrad * EXP( -15.0 * ( znd - 0.9 )**2 )
1407                             zdbdz(ji,jj,jk) =  zbgrad * EXP( -15.0 * ( znd - 0.9 )**2 )
1408                             zdsdz(ji,jj,jk) =  zsgrad * EXP( -15.0 * ( znd - 0.9 )**2 )
1409                          END DO
1410                       ELSE                                   ! Slightly stable - 'thin' pycnoline - needed when stable layer begins to form.
1411                          ztmp = 1._wp/MAX(zdh(ji,jj), epsln)
1412                          ztgrad = zdt_bl(ji,jj) * ztmp
1413                          zsgrad = zds_bl(ji,jj) * ztmp
1414                          zbgrad = zdb_bl(ji,jj) * ztmp
1415                          DO jk = 2, ibld(ji,jj)
1416                             znd = -( gdepw_n(ji,jj,jk) - zhml(ji,jj) ) * ztmp
1417                             zdtdz(ji,jj,jk) =  ztgrad * EXP( -1.75 * ( znd + 0.75 )**2 )
1418                             zdbdz(ji,jj,jk) =  zbgrad * EXP( -1.75 * ( znd + 0.75 )**2 )
1419                             zdsdz(ji,jj,jk) =  zsgrad * EXP( -1.75 * ( znd + 0.75 )**2 )
1420                          END DO
1421                       ENDIF ! IF (zhol >=0.5)
1422                    ENDIF    ! IF (zdb_bl> 0.)
1423                 ENDIF       ! IF (zdhdt >= 0) zdhdt < 0 not considered since pycnocline profile is zero and profile arrays are intialized to zero
1424              ENDIF          ! IF (lconv)
1425           END IF      ! IF ( ibld(ji,jj) + ibld_ext < mbkt(ji,jj) )
1426        END DO
1427     END DO
1428
1429    END SUBROUTINE zdf_osm_pycnocline_scalar_profiles
1430
1431    SUBROUTINE zdf_osm_pycnocline_shear_profiles( zdudz, zdvdz )
1432      !!---------------------------------------------------------------------
1433      !!                   ***  ROUTINE zdf_osm_pycnocline_shear_profiles  ***
1434      !!
1435      !! ** Purpose : Calculates velocity shear in the pycnocline
1436      !!
1437      !! ** Method  :
1438      !!
1439      !!----------------------------------------------------------------------
1440
1441      REAL(wp), DIMENSION(jpi,jpj,jpk) :: zdudz, zdvdz
1442
1443      INTEGER :: jk, jj, ji
1444      REAL(wp) :: zugrad, zvgrad, znd
1445      REAL(wp) :: zzeta_v = 0.45
1446      !
1447      DO jj = 2, jpjm1
1448         DO ji = 2, jpim1
1449            !
1450            IF ( ibld(ji,jj) + ibld_ext < mbkt(ji,jj) ) THEN
1451               IF ( lconv (ji,jj) ) THEN
1452                  ! Unstable conditions
1453                  zugrad = 0.7 * zdu_ml(ji,jj) / zdh(ji,jj) + 0.3 * zustar(ji,jj)*zustar(ji,jj) / &
1454                       &      ( ( ( zvstr(ji,jj)**3 + 0.5 * zwstrc(ji,jj)**3 )**pthird * zhml(ji,jj) ) * &
1455                       &      MIN(zla(ji,jj)**(8.0/3.0) + epsln, 0.12 ))
1456                  !Alan is this right?
1457                  zvgrad = ( 0.7 * zdv_ml(ji,jj) + &
1458                       &    2.0 * ff_t(ji,jj) * zustke(ji,jj) * dstokes(ji,jj) / &
1459                       &          ( ( zvstr(ji,jj)**3 + 0.5 * zwstrc(ji,jj)**3 )**pthird  + epsln ) &
1460                       &      )/ (zdh(ji,jj)  + epsln )
1461                  DO jk = 2, ibld(ji,jj) - 1 + ibld_ext
1462                     znd = -( gdepw_n(ji,jj,jk) - zhbl(ji,jj) ) / (zdh(ji,jj) + epsln ) - zzeta_v
1463                     IF ( znd <= 0.0 ) THEN
1464                        zdudz(ji,jj,jk) = 1.25 * zugrad * EXP( 3.0 * znd )
1465                        zdvdz(ji,jj,jk) = 1.25 * zvgrad * EXP( 3.0 * znd )
1466                     ELSE
1467                        zdudz(ji,jj,jk) = 1.25 * zugrad * EXP( -2.0 * znd )
1468                        zdvdz(ji,jj,jk) = 1.25 * zvgrad * EXP( -2.0 * znd )
1469                     ENDIF
1470                  END DO
1471               ELSE
1472                  ! stable conditions
1473                  zugrad = 3.25 * zdu_bl(ji,jj) / zhbl(ji,jj)
1474                  zvgrad = 2.75 * zdv_bl(ji,jj) / zhbl(ji,jj)
1475                  DO jk = 2, ibld(ji,jj)
1476                     znd = gdepw_n(ji,jj,jk) / zhbl(ji,jj)
1477                     IF ( znd < 1.0 ) THEN
1478                        zdudz(ji,jj,jk) = zugrad * EXP( -40.0 * ( znd - 1.0 )**2 )
1479                     ELSE
1480                        zdudz(ji,jj,jk) = zugrad * EXP( -20.0 * ( znd - 1.0 )**2 )
1481                     ENDIF
1482                     zdvdz(ji,jj,jk) = zvgrad * EXP( -20.0 * ( znd - 0.85 )**2 )
1483                  END DO
1484               ENDIF
1485               !
1486            END IF      ! IF ( ibld(ji,jj) + ibld_ext < mbkt(ji,jj) )
1487         END DO
1488      END DO
1489    END SUBROUTINE zdf_osm_pycnocline_shear_profiles
1490
1491    SUBROUTINE zdf_osm_calculate_dhdt( zdhdt, zdhdt_2 )
1492     !!---------------------------------------------------------------------
1493     !!                   ***  ROUTINE zdf_osm_calculate_dhdt  ***
1494     !!
1495     !! ** Purpose : Calculates the rate at which hbl changes.
1496     !!
1497     !! ** Method  :
1498     !!
1499     !!----------------------------------------------------------------------
1500
1501    REAL(wp), DIMENSION(jpi,jpj) :: zdhdt, zdhdt_2        ! Rate of change of hbl
1502
1503    INTEGER :: jj, ji
1504    REAL(wp) :: zgamma_b_nd, zgamma_dh_nd, zpert
1505    REAL(wp) :: zvel_max, zwb_min
1506    REAL(wp) :: zwcor, zrf_conv, zrf_shear, zrf_langmuir, zr_stokes
1507    REAL(wp) :: zzeta_m = 0.3
1508    REAL(wp) :: zgamma_c = 2.0
1509    REAL(wp) :: zdhoh = 0.1
1510    REAL(wp) :: alpha_bc = 0.5
1511
1512    DO jj = 2, jpjm1
1513       DO ji = 2, jpim1
1514          IF ( lconv(ji,jj) ) THEN    ! Convective
1515             ! Alan is this right?  Yes, it's a bit different from the previous relationship
1516             ! zwb_ent(ji,jj) = - 2.0 * 0.2 * zwbav(ji,jj) &
1517             !   &            - ( 0.15 * ( 1.0 - EXP( -1.5 * zla(ji,jj) ) ) * zustar(ji,jj)**3 + 0.03 * zwstrl(ji,jj)**3 ) / zhml(ji,jj)
1518             zwcor = ABS(ff_t(ji,jj)) * zhbl(ji,jj) + epsln
1519             zrf_conv = TANH( ( zwstrc(ji,jj) / zwcor )**0.69 )
1520             zrf_shear = TANH( ( zustar(ji,jj) / zwcor )**0.69 )
1521             zrf_langmuir = TANH( ( zwstrl(ji,jj) / zwcor )**0.69 )
1522             zr_stokes = 1.0 - EXP( -25.0 * dstokes(ji,jj) / hbl(ji,jj) &
1523                  &                  * ( 1.0 + 4.0 * dstokes(ji,jj) / hbl(ji,jj) ) )
1524
1525             zwb_ent(ji,jj) = - 2.0 * 0.2 * zrf_conv * zwbav(ji,jj) &
1526                  &                  - 0.15 * zrf_shear * zustar(ji,jj)**3 /zhml(ji,jj) &
1527                  &         + zr_stokes * ( 0.15 * EXP( -1.5 * zla(ji,jj) ) * zrf_shear * zustar(ji,jj)**3 &
1528                  &                                         - zrf_langmuir * 0.03 * zwstrl(ji,jj)**3 ) / zhml(ji,jj)
1529             !
1530             zwb_min = dh(ji,jj) * zwb0(ji,jj) / zhml(ji,jj) + zwb_ent(ji,jj)
1531
1532             IF ( ln_osm_mle ) THEN
1533                  !  zwb_fk_b(ji,jj) = zwb_fk(ji,jj) *  hmle(ji,jj) / ( 6.0 * hbl(ji,jj) ) * ( 6.0 * hbl(ji,jj) / hmle(ji,jj) - 1.0 + &
1534                ! &            ( 1.0 - 2.0 * hbl(ji,jj) / hmle(ji,jj))**3 )          ! Fox-Kemper buoyancy flux average over OSBL
1535                IF ( hmle(ji,jj) > hbl(ji,jj) ) THEN
1536                   zwb_fk_b(ji,jj) = zwb_fk(ji,jj) *  &
1537                        (1.0 + hmle(ji,jj) / ( 6.0 * hbl(ji,jj) ) * (-1.0 + ( 1.0 - 2.0 * hbl(ji,jj) / hmle(ji,jj))**3) )
1538                ELSE
1539                   zwb_fk_b(ji,jj) = 0.5 * zwb_fk(ji,jj) * hmle(ji,jj) / hbl(ji,jj)
1540                ENDIF
1541                zvel_max = ( zwstrl(ji,jj)**3 + 0.5 * zwstrc(ji,jj)**3 )**p2third / hbl(ji,jj)
1542                IF ( ( zwb_ent(ji,jj) + 2.0 * zwb_fk_b(ji,jj) ) < 0.0 ) THEN
1543                   ! OSBL is deepening, entrainment > restratification
1544                   IF ( zdb_bl(ji,jj) > 0.0 .and. zdbdz_ext(ji,jj) > 0.0 ) THEN
1545                      zdhdt(ji,jj) = -( zwb_ent(ji,jj) + 2.0 * zwb_fk_b(ji,jj) ) / ( zvel_max + MAX(zdb_bl(ji,jj), 1.0e-15) )
1546                   ELSE
1547                      zdhdt(ji,jj) = -( zwb_ent(ji,jj) + 2.0 * zwb_fk_b(ji,jj) ) /  MAX( zvel_max, 1.0e-15)
1548                   ENDIF
1549                ELSE
1550                   ! OSBL shoaling due to restratification flux. This is the velocity defined in Fox-Kemper et al (2008)
1551                   zdhdt(ji,jj) = - zvel_mle(ji,jj)
1552
1553
1554                ENDIF
1555
1556             ELSE
1557                ! Fox-Kemper not used.
1558
1559                  zvel_max = - ( 1.0 + 1.0 * ( zvstr(ji,jj)**3 + 0.5 * zwstrc(ji,jj)**3 )**pthird * rn_rdt / hbl(ji,jj) ) * zwb_ent(ji,jj) / &
1560                  &        MAX((zvstr(ji,jj)**3 + 0.5 * zwstrc(ji,jj)**3 )**pthird, epsln)
1561                  zdhdt(ji,jj) = -zwb_ent(ji,jj) / ( zvel_max + MAX(zdb_bl(ji,jj), 1.0e-15) )
1562                ! added ajgn 23 July as temporay fix
1563
1564             ENDIF
1565
1566             zdhdt_2(ji,jj) = 0._wp
1567
1568                ! commented out ajgn 23 July as temporay fix
1569!                 IF ( zdb_ml(ji,jj) > 0.0 .and. zdbdz_ext(ji,jj) > 0.0 ) THEN
1570! !additional term to account for thickness of pycnocline on dhdt. Small correction, so could get rid of this if necessary.
1571!                     zdh(ji,jj) = zhbl(ji,jj) - zhml(ji,jj)
1572!                     zgamma_b_nd = zdbdz_ext(ji,jj) * zhml(ji,jj) / zdb_ml(ji,jj)
1573!                     zgamma_dh_nd = zdbdz_ext(ji,jj) * zdh(ji,jj) / zdb_ml(ji,jj)
1574!                     zdhdt_2(ji,jj) = ( 1.0 - SQRT( 3.1415 / ( 4.0 * zgamma_c) ) * zdhoh ) * zdh(ji,jj) / zhml(ji,jj)
1575!                     zdhdt_2(ji,jj) = zdhdt_2(ji,jj) * ( zwb0(ji,jj) - (1.0 + zgamma_b_nd / alpha_bc ) * zwb_min )
1576!                     ! Alan no idea what this should be?
1577!                     zdhdt_2(ji,jj) = alpha_bc / ( 4.0 * zgamma_c ) * zdhdt_2(ji,jj) &
1578!                        &        + (alpha_bc + zgamma_dh_nd ) * ( 1.0 + SQRT( 3.1414 / ( 4.0 * zgamma_c ) ) * zdh(ji,jj) / zhbl(ji,jj) ) &
1579!                        &        * (1.0 / ( 4.0 * zgamma_c * alpha_bc ) ) * zwb_min * zdh(ji,jj) / zhbl(ji,jj)
1580!                     zdhdt_2(ji,jj) = zdhdt_2(ji,jj) / ( zvel_max + MAX( zdb_bl(ji,jj), 1.0e-15 ) )
1581!                     IF ( zdhdt_2(ji,jj) <= 0.2 * zdhdt(ji,jj) ) THEN
1582!                         zdhdt(ji,jj) = zdhdt(ji,jj) + zdhdt_2(ji,jj)
1583!                 ENDIF
1584            ELSE                        ! Stable
1585                zdhdt(ji,jj) = ( 0.06 + 0.52 * zhol(ji,jj) / 2.0 ) * zvstr(ji,jj)**3 / hbl(ji,jj) + zwbav(ji,jj)
1586                zdhdt_2(ji,jj) = 0._wp
1587                IF ( zdhdt(ji,jj) < 0._wp ) THEN
1588                   ! For long timsteps factor in brackets slows the rapid collapse of the OSBL
1589                    zpert = 2.0 * ( 1.0 + 0.0 * 2.0 * zvstr(ji,jj) * rn_rdt / hbl(ji,jj) ) * zvstr(ji,jj)**2 / hbl(ji,jj)
1590                ELSE
1591                    zpert = MAX( 2.0 * ( 1.0 + 0.0 * 2.0 * zvstr(ji,jj) * rn_rdt / hbl(ji,jj) ) * zvstr(ji,jj)**2 / hbl(ji,jj), zdb_bl(ji,jj) )
1592                ENDIF
1593                zdhdt(ji,jj) = 2.0 * zdhdt(ji,jj) / MAX(zpert, epsln)
1594            ENDIF
1595         END DO
1596      END DO
1597    END SUBROUTINE zdf_osm_calculate_dhdt
1598
1599    SUBROUTINE zdf_osm_timestep_hbl( zdhdt, zdhdt_2 )
1600     !!---------------------------------------------------------------------
1601     !!                   ***  ROUTINE zdf_osm_timestep_hbl  ***
1602     !!
1603     !! ** Purpose : Increments hbl.
1604     !!
1605     !! ** Method  : If thechange in hbl exceeds one model level the change is
1606     !!              is calculated by moving down the grid, changing the buoyancy
1607     !!              jump. This is to ensure that the change in hbl does not
1608     !!              overshoot a stable layer.
1609     !!
1610     !!----------------------------------------------------------------------
1611
1612
1613    REAL(wp), DIMENSION(jpi,jpj) :: zdhdt, zdhdt_2   ! rates of change of hbl.
1614
1615    INTEGER :: jk, jj, ji, jm
1616    REAL(wp) :: zhbl_s, zvel_max, zdb
1617    REAL(wp) :: zthermal, zbeta
1618
1619     DO jj = 2, jpjm1
1620         DO ji = 2, jpim1
1621           IF ( ibld(ji,jj) - imld(ji,jj) > 1 ) THEN
1622!
1623! If boundary layer changes by more than one level, need to check for stable layers between initial and final depths.
1624!
1625              zhbl_s = hbl(ji,jj)
1626              jm = imld(ji,jj)
1627              zthermal = rab_n(ji,jj,1,jp_tem)
1628              zbeta = rab_n(ji,jj,1,jp_sal)
1629
1630
1631              IF ( lconv(ji,jj) ) THEN
1632!unstable
1633
1634                 IF( ln_osm_mle ) THEN
1635                    zvel_max = ( zwstrl(ji,jj)**3 + zwstrc(ji,jj)**3 )**p2third / hbl(ji,jj)
1636                 ELSE
1637
1638                    zvel_max = -( 1.0 + 1.0 * ( zvstr(ji,jj)**3 + 0.5 * zwstrc(ji,jj)**3 )**pthird * rn_rdt / hbl(ji,jj) ) * zwb_ent(ji,jj) / &
1639                      &      ( zvstr(ji,jj)**3 + 0.5 * zwstrc(ji,jj)**3 )**pthird
1640
1641                 ENDIF
1642
1643                 DO jk = imld(ji,jj), ibld(ji,jj)
1644                    zdb = MAX( grav * ( zthermal * ( zt_bl(ji,jj) - tsn(ji,jj,jm,jp_tem) ) &
1645                         & - zbeta * ( zs_bl(ji,jj) - tsn(ji,jj,jm,jp_sal) ) ), &
1646                         &  0.0 ) + zvel_max
1647
1648
1649                    IF ( ln_osm_mle ) THEN
1650                       zhbl_s = zhbl_s + MIN( &
1651                        & rn_rdt * ( ( -zwb_ent(ji,jj) - 2.0 * zwb_fk_b(ji,jj) )/ zdb + zdhdt_2(ji,jj) ) / FLOAT(ibld(ji,jj) - imld(ji,jj) ), &
1652                        & e3w_n(ji,jj,jm) )
1653                    ELSE
1654                      zhbl_s = zhbl_s + MIN( &
1655                        & rn_rdt * (  -zwb_ent(ji,jj) / zdb + zdhdt_2(ji,jj) ) / FLOAT(ibld(ji,jj) - imld(ji,jj) ), &
1656                        & e3w_n(ji,jj,jm) )
1657                    ENDIF
1658
1659                    zhbl_s = MIN(zhbl_s,  gdepw_n(ji,jj, mbkt(ji,jj) + 1) - depth_tol)
1660
1661                    IF ( zhbl_s >= gdepw_n(ji,jj,jm+1) ) jm = jm + 1
1662                 END DO
1663                 hbl(ji,jj) = zhbl_s
1664                 ibld(ji,jj) = jm
1665             ELSE
1666! stable
1667                 DO jk = imld(ji,jj), ibld(ji,jj)
1668                    zdb = MAX( &
1669                         & grav * ( zthermal * ( zt_bl(ji,jj) - tsn(ji,jj,jm,jp_tem) )&
1670                         &           - zbeta * ( zs_bl(ji,jj) - tsn(ji,jj,jm,jp_sal) ) ),&
1671                         & 0.0 ) + &
1672             &       2.0 * zvstr(ji,jj)**2 / zhbl_s
1673
1674                    ! Alan is thuis right? I have simply changed hbli to hbl
1675                    zhol(ji,jj) = -zhbl_s / ( ( zvstr(ji,jj)**3 + epsln )/ zwbav(ji,jj) )
1676                    zdhdt(ji,jj) = -( zwbav(ji,jj) - 0.04 / 2.0 * zwstrl(ji,jj)**3 / zhbl_s - 0.15 / 2.0 * ( 1.0 - EXP( -1.5 * zla(ji,jj) ) ) * &
1677               &                  zustar(ji,jj)**3 / zhbl_s ) * ( 0.725 + 0.225 * EXP( -7.5 * zhol(ji,jj) ) )
1678                    zdhdt(ji,jj) = zdhdt(ji,jj) + zwbav(ji,jj)
1679                    zhbl_s = zhbl_s + MIN( zdhdt(ji,jj) / zdb * rn_rdt / FLOAT( ibld(ji,jj) - imld(ji,jj) ), e3w_n(ji,jj,jm) )
1680
1681                    zhbl_s = MIN(zhbl_s, gdepw_n(ji,jj, mbkt(ji,jj) + 1) - depth_tol)
1682                    IF ( zhbl_s >= gdepw_n(ji,jj,jm) ) jm = jm + 1
1683                 END DO
1684             ENDIF   ! IF ( lconv )
1685             hbl(ji,jj) = MAX(zhbl_s, gdepw_n(ji,jj,4) )
1686             ibld(ji,jj) = MAX(jm, 4 )
1687           ELSE
1688! change zero or one model level.
1689             hbl(ji,jj) = MAX(zhbl_t(ji,jj), gdepw_n(ji,jj,4) )
1690           ENDIF
1691           zhbl(ji,jj) = gdepw_n(ji,jj,ibld(ji,jj))
1692         END DO
1693      END DO
1694
1695    END SUBROUTINE zdf_osm_timestep_hbl
1696
1697    SUBROUTINE zdf_osm_pycnocline_thickness( dh, zdh )
1698      !!---------------------------------------------------------------------
1699      !!                   ***  ROUTINE zdf_osm_pycnocline_thickness  ***
1700      !!
1701      !! ** Purpose : Calculates thickness of the pycnocline
1702      !!
1703      !! ** Method  : The thickness is calculated from a prognostic equation
1704      !!              that relaxes the pycnocine thickness to a diagnostic
1705      !!              value. The time change is calculated assuming the
1706      !!              thickness relaxes exponentially. This is done to deal
1707      !!              with large timesteps.
1708      !!
1709      !!----------------------------------------------------------------------
1710
1711      REAL(wp), DIMENSION(jpi,jpj) :: dh, zdh     ! pycnocline thickness.
1712      !
1713      INTEGER :: jj, ji
1714      INTEGER :: inhml
1715      REAL(wp) :: zari, ztau, zddhdt
1716
1717
1718      DO jj = 2, jpjm1
1719         DO ji = 2, jpim1
1720            IF ( lconv(ji,jj) ) THEN
1721
1722               IF( ln_osm_mle ) THEN
1723                  IF ( ( zwb_ent(ji,jj) + zwb_fk_b(ji,jj) ) < 0._wp ) THEN
1724                     ! OSBL is deepening. Note wb_fk_b is zero if ln_osm_mle=F
1725                     IF ( zdb_bl(ji,jj) > 0._wp .and. zdbdz_ext(ji,jj) > 0._wp)THEN
1726                        IF ( ( zwstrc(ji,jj) / MAX(zvstr(ji,jj), epsln) )**3 <= 0.5 ) THEN  ! near neutral stability
1727                           zari = MIN( 1.5 * ( zdb_bl(ji,jj) / ( zdbdz_ext(ji,jj) * zhbl(ji,jj) ) ) / &
1728                                (1.0 + zdb_bl(ji,jj)**2 / MAX( 4.5 * zvstr(ji,jj)**2 * zdbdz_ext(ji,jj), epsln ) ), 0.2 )
1729                        ELSE                                                     ! unstable
1730                           zari = MIN( 1.5 * ( zdb_bl(ji,jj) / ( zdbdz_ext(ji,jj) * zhbl(ji,jj) ) ) / &
1731                                (1.0 + zdb_bl(ji,jj)**2 / ( 4.5 * zwstrc(ji,jj)**2 * zdbdz_ext(ji,jj) ) ), 0.2 )
1732                        ENDIF
1733                        ztau = 0.2 * hbl(ji,jj) / MAX(epsln, (zvstr(ji,jj)**3 + 0.5 *zwstrc(ji,jj)**3)**pthird)
1734                        zddhdt = zari * hbl(ji,jj)
1735                     ELSE
1736                        ztau = 0.2 * hbl(ji,jj) / MAX(epsln, (zvstr(ji,jj)**3 + 0.5 *zwstrc(ji,jj)**3)**pthird)
1737                        zddhdt = 0.2 * hbl(ji,jj)
1738                     ENDIF
1739                  ELSE
1740                     ztau = 0.2 * hbl(ji,jj) /  MAX(epsln, (zvstr(ji,jj)**3 + 0.5 *zwstrc(ji,jj)**3)**pthird)
1741                     zddhdt = 0.2 * hbl(ji,jj)
1742                  ENDIF
1743               ELSE ! ln_osm_mle
1744                  IF ( zdb_bl(ji,jj) > 0._wp .and. zdbdz_ext(ji,jj) > 0._wp)THEN
1745                     IF ( ( zwstrc(ji,jj) / MAX(zvstr(ji,jj), epsln) )**3 <= 0.5 ) THEN  ! near neutral stability
1746                        zari = MIN( 1.5 * ( zdb_bl(ji,jj) / MAX( zdbdz_ext(ji,jj) * zhbl(ji,jj), epsln ) ) / &
1747                             (1.0 + zdb_bl(ji,jj)**2 /  MAX(4.5 * zvstr(ji,jj)**2 * zdbdz_ext(ji,jj), epsln ) ), 0.2 )
1748                     ELSE                                                     ! unstable
1749                        zari = MIN( 1.5 * ( zdb_bl(ji,jj) / MAX( zdbdz_ext(ji,jj) * zhbl(ji,jj), epsln ) ) / &
1750                             (1.0 + zdb_bl(ji,jj)**2 / MAX(4.5 * zvstr(ji,jj)**2 * zdbdz_ext(ji,jj), epsln ) ), 0.2 )
1751                     ENDIF
1752                     ztau = hbl(ji,jj) / MAX(epsln, (zvstr(ji,jj)**3 + 0.5 *zwstrc(ji,jj)**3)**pthird)
1753                     zddhdt = zari * hbl(ji,jj)
1754                  ELSE
1755                     ztau = hbl(ji,jj) / MAX(epsln, (zvstr(ji,jj)**3 + 0.5 *zwstrc(ji,jj)**3)**pthird)
1756                     zddhdt = 0.2 * hbl(ji,jj)
1757                  ENDIF
1758
1759               END IF  ! ln_osm_mle
1760
1761               dh(ji,jj) = dh(ji,jj) * EXP( -rn_rdt / ztau ) + zddhdt * ( 1.0 - EXP( -rn_rdt / ztau ) )
1762               ! Alan: this hml is never defined or used
1763            ELSE   ! IF (lconv)
1764               ztau = hbl(ji,jj) / MAX(zvstr(ji,jj), epsln)
1765               IF ( zdhdt(ji,jj) >= 0.0 ) THEN    ! probably shouldn't include wm here
1766                  ! boundary layer deepening
1767                  IF ( zdb_bl(ji,jj) > 0._wp ) THEN
1768                     ! pycnocline thickness set by stratification - use same relationship as for neutral conditions.
1769                     zari = MIN( 4.5 * ( zvstr(ji,jj)**2 ) &
1770                          & /  MAX(zdb_bl(ji,jj) * zhbl(ji,jj), epsln ) + 0.01  , 0.2 )
1771                     zddhdt = MIN( zari, 0.2 ) * hbl(ji,jj)
1772                  ELSE
1773                     zddhdt = 0.2 * hbl(ji,jj)
1774                  ENDIF
1775               ELSE     ! IF(dhdt < 0)
1776                  zddhdt = 0.2 * hbl(ji,jj)
1777               ENDIF    ! IF (dhdt >= 0)
1778               dh(ji,jj) = dh(ji,jj) * EXP( -rn_rdt / ztau )+ zddhdt * ( 1.0 - EXP( -rn_rdt / ztau ) )
1779               IF ( zdhdt(ji,jj) < 0._wp .and. dh(ji,jj) >= hbl(ji,jj) ) dh(ji,jj) = zddhdt       ! can be a problem with dh>hbl for rapid collapse
1780               ! Alan: this hml is never defined or used -- do we need it?
1781            ENDIF       ! IF (lconv)
1782
1783            hml(ji,jj) = hbl(ji,jj) - dh(ji,jj)
1784            inhml = MAX( INT( dh(ji,jj) / e3t_n(ji,jj,ibld(ji,jj)) ) , 1 )
1785            imld(ji,jj) = MAX( ibld(ji,jj) - inhml, 3)
1786            zhml(ji,jj) = gdepw_n(ji,jj,imld(ji,jj))
1787            zdh(ji,jj) = zhbl(ji,jj) - zhml(ji,jj)
1788         END DO
1789      END DO
1790
1791    END SUBROUTINE zdf_osm_pycnocline_thickness
1792
1793
1794   SUBROUTINE zdf_osm_zmld_horizontal_gradients( zmld, zdtdx, zdtdy, zdsdx, zdsdy, dbdx_mle, dbdy_mle )
1795      !!----------------------------------------------------------------------
1796      !!                  ***  ROUTINE zdf_osm_horizontal_gradients  ***
1797      !!
1798      !! ** Purpose :   Calculates horizontal gradients of buoyancy for use with Fox-Kemper parametrization.
1799      !!
1800      !! ** Method  :
1801      !!
1802      !! References: Fox-Kemper et al., JPO, 38, 1145-1165, 2008
1803      !!             Fox-Kemper and Ferrari, JPO, 38, 1166-1179, 2008
1804
1805
1806      REAL(wp), DIMENSION(jpi,jpj)     :: dbdx_mle, dbdy_mle ! MLE horiz gradients at u & v points
1807      REAL(wp), DIMENSION(jpi,jpj)     :: zmld ! ==  estimated FK BLD used for MLE horiz gradients  == !
1808      REAL(wp), DIMENSION(jpi,jpj)     :: zdtdx, zdtdy, zdsdx, zdsdy
1809
1810      INTEGER  ::   ji, jj, jk          ! dummy loop indices
1811      INTEGER  ::   ii, ij, ik, ikmax   ! local integers
1812      REAL(wp)                         :: zc
1813      REAL(wp)                         :: zN2_c           ! local buoyancy difference from 10m value
1814      REAL(wp), DIMENSION(jpi,jpj)     :: ztm, zsm, zLf_NH, zLf_MH
1815      REAL(wp), DIMENSION(jpi,jpj,jpts):: ztsm_midu, ztsm_midv, zabu, zabv
1816      REAL(wp), DIMENSION(jpi,jpj)     :: zmld_midu, zmld_midv
1817!!----------------------------------------------------------------------
1818      !
1819      !                                      !==  MLD used for MLE  ==!
1820
1821      mld_prof(:,:)  = nlb10               ! Initialization to the number of w ocean point
1822      zmld(:,:)  = 0._wp               ! here hmlp used as a dummy variable, integrating vertically N^2
1823      zN2_c = grav * rn_osm_mle_rho_c * r1_rau0   ! convert density criteria into N^2 criteria
1824      DO jk = nlb10, jpkm1
1825         DO jj = 1, jpj                ! Mixed layer level: w-level
1826            DO ji = 1, jpi
1827               ikt = mbkt(ji,jj)
1828               zmld(ji,jj) = zmld(ji,jj) + MAX( rn2b(ji,jj,jk) , 0._wp ) * e3w_n(ji,jj,jk)
1829               IF( zmld(ji,jj) < zN2_c )   mld_prof(ji,jj) = MIN( jk , ikt ) + 1   ! Mixed layer level
1830            END DO
1831         END DO
1832      END DO
1833      DO jj = 1, jpj
1834         DO ji = 1, jpi
1835            mld_prof(ji,jj) = MAX(mld_prof(ji,jj),ibld(ji,jj))
1836            zmld(ji,jj) = gdepw_n(ji,jj,mld_prof(ji,jj))
1837         END DO
1838      END DO
1839      ! ensure mld_prof .ge. ibld
1840      !
1841      ikmax = MIN( MAXVAL( mld_prof(:,:) ), jpkm1 )                  ! max level of the computation
1842      !
1843      ztm(:,:) = 0._wp
1844      zsm(:,:) = 0._wp
1845      DO jk = 1, ikmax                                 ! MLD and mean buoyancy and N2 over the mixed layer
1846         DO jj = 1, jpj
1847            DO ji = 1, jpi
1848               zc = e3t_n(ji,jj,jk) * REAL( MIN( MAX( 0, mld_prof(ji,jj)-jk ) , 1  )  )    ! zc being 0 outside the ML t-points
1849               ztm(ji,jj) = ztm(ji,jj) + zc * tsn(ji,jj,jk,jp_tem)
1850               zsm(ji,jj) = zsm(ji,jj) + zc * tsn(ji,jj,jk,jp_sal)
1851            END DO
1852         END DO
1853      END DO
1854      ! average temperature and salinity.
1855      ztm(:,:) = ztm(:,:) / MAX( e3t_n(:,:,1), zmld(:,:) )
1856      zsm(:,:) = zsm(:,:) / MAX( e3t_n(:,:,1), zmld(:,:) )
1857      ! calculate horizontal gradients at u & v points
1858
1859      DO jj = 2, jpjm1
1860         DO ji = 1, jpim1
1861            zdtdx(ji,jj) = ( ztm(ji+1,jj) - ztm( ji,jj) )  * umask(ji,jj,1) / e1u(ji,jj)
1862            zdsdx(ji,jj) = ( zsm(ji+1,jj) - zsm( ji,jj) )  * umask(ji,jj,1) / e1u(ji,jj)
1863            zmld_midu(ji,jj) = 0.25_wp * (zmld(ji+1,jj) + zmld( ji,jj))
1864            ztsm_midu(ji,jj,jp_tem) = 0.5_wp * ( ztm(ji+1,jj) + ztm( ji,jj) )
1865            ztsm_midu(ji,jj,jp_sal) = 0.5_wp * ( zsm(ji+1,jj) + zsm( ji,jj) )
1866         END DO
1867      END DO
1868
1869      DO jj = 1, jpjm1
1870         DO ji = 2, jpim1
1871            zdtdy(ji,jj) = ( ztm(ji,jj+1) - ztm( ji,jj) ) * vmask(ji,jj,1) / e1v(ji,jj)
1872            zdsdy(ji,jj) = ( zsm(ji,jj+1) - zsm( ji,jj) ) * vmask(ji,jj,1) / e1v(ji,jj)
1873            zmld_midv(ji,jj) = 0.25_wp * (zmld(ji,jj+1) + zmld( ji,jj))
1874            ztsm_midv(ji,jj,jp_tem) = 0.5_wp * ( ztm(ji,jj+1) + ztm( ji,jj) )
1875            ztsm_midv(ji,jj,jp_sal) = 0.5_wp * ( zsm(ji,jj+1) + zsm( ji,jj) )
1876         END DO
1877      END DO
1878
1879      CALL eos_rab(ztsm_midu, zmld_midu, zabu)
1880      CALL eos_rab(ztsm_midv, zmld_midv, zabv)
1881
1882      DO jj = 2, jpjm1
1883         DO ji = 1, jpim1
1884            dbdx_mle(ji,jj) = grav*(zdtdx(ji,jj)*zabu(ji,jj,jp_tem) - zdsdx(ji,jj)*zabu(ji,jj,jp_sal))
1885         END DO
1886      END DO
1887      DO jj = 1, jpjm1
1888         DO ji = 2, jpim1
1889            dbdy_mle(ji,jj) = grav*(zdtdy(ji,jj)*zabv(ji,jj,jp_tem) - zdsdy(ji,jj)*zabv(ji,jj,jp_sal))
1890         END DO
1891      END DO
1892
1893 END SUBROUTINE zdf_osm_zmld_horizontal_gradients
1894  SUBROUTINE zdf_osm_mle_parameters( hmle, zwb_fk, zvel_mle, zdiff_mle )
1895      !!----------------------------------------------------------------------
1896      !!                  ***  ROUTINE zdf_osm_mle_parameters  ***
1897      !!
1898      !! ** Purpose :   Timesteps the mixed layer eddy depth, hmle and calculates the mixed layer eddy fluxes for buoyancy, heat and salinity.
1899      !!
1900      !! ** Method  :
1901      !!
1902      !! References: Fox-Kemper et al., JPO, 38, 1145-1165, 2008
1903      !!             Fox-Kemper and Ferrari, JPO, 38, 1166-1179, 2008
1904
1905      REAL(wp), DIMENSION(jpi,jpj)     :: hmle, zwb_fk, zvel_mle, zdiff_mle
1906      INTEGER  ::   ji, jj, jk          ! dummy loop indices
1907      INTEGER  ::   ii, ij, ik  ! local integers
1908      INTEGER , DIMENSION(jpi,jpj)     :: inml_mle
1909      REAL(wp) ::   zdb_mle, ztmp, zdbds_mle
1910
1911      mld_prof(:,:) = 4
1912      DO jk = 5, jpkm1
1913        DO jj = 2, jpjm1
1914          DO ji = 2, jpim1
1915            IF ( hmle(ji,jj) >= gdepw_n(ji,jj,jk) ) mld_prof(ji,jj) = MIN(mbkt(ji,jj), jk)
1916          END DO
1917        END DO
1918      END DO
1919      ! DO jj = 2, jpjm1
1920      !    DO ji = 1, jpim1
1921      !       zhmle(ji,jj) = gdepw_n(ji,jj,mld_prof(ji,jj))
1922      !    END DO
1923      !  END DO
1924   ! Timestep mixed layer eddy depth.
1925      DO jj = 2, jpjm1
1926        DO ji = 2, jpim1
1927          zdb_mle = grav * (rhop(ji,jj,mld_prof(ji,jj)) - rhop(ji,jj,ibld(ji,jj) )) * r1_rau0 ! check ALMG
1928          IF ( lconv(ji,jj) .and. ( zdb_bl(ji,jj) < rn_osm_mle_thresh .and. mld_prof(ji,jj) > ibld(ji,jj) .and. zdb_mle > 0.0 ) ) THEN
1929             hmle(ji,jj) = hmle(ji,jj) + zwb0(ji,jj) * rn_rdt / MAX( zdb_mle, rn_osm_mle_thresh )  ! MLE layer deepening through encroachment. Don't have a good maximum value for deepening, so use threshold buoyancy.
1930          ELSE
1931            ! MLE layer relaxes towards mixed layer depth on timescale tau_mle, or tau_mle/10
1932             ! IF ( hmle(ji,jj) > zmld(ji,jj) ) THEN
1933             !   hmle(ji,jj) = hmle(ji,jj) - ( hmle(ji,jj) - zmld(ji,jj) ) * rn_rdt / rn_osm_mle_tau
1934             ! ELSE
1935             !   hmle(ji,jj) = hmle(ji,jj) - 10.0 * ( hmle(ji,jj) - zmld(ji,jj) ) * rn_rdt / rn_osm_mle_tau ! fast relaxation if MLE layer shallower than MLD
1936             ! ENDIF
1937             IF ( hmle(ji,jj) > hbl(ji,jj) ) THEN
1938               hmle(ji,jj) = hmle(ji,jj) - ( hmle(ji,jj) - hbl(ji,jj) ) * rn_rdt / rn_osm_mle_tau
1939             ELSE
1940               hmle(ji,jj) = hmle(ji,jj) - 10.0 * ( hmle(ji,jj) - hbl(ji,jj) ) * rn_rdt / rn_osm_mle_tau ! fast relaxation if MLE layer shallower than MLD
1941             ENDIF
1942          ENDIF
1943          hmle(ji,jj) = MIN(hmle(ji,jj), ht_n(ji,jj))
1944          IF(ln_osm_hmle_limit) hmle(ji,jj) = MIN(hmle(ji,jj), rn_osm_hmle_limit*zmld(ji,jj))
1945        END DO
1946      END DO
1947
1948      mld_prof = 4
1949      DO jk = 5, jpkm1
1950        DO jj = 2, jpjm1
1951          DO ji = 2, jpim1
1952            IF ( hmle(ji,jj) >= gdepw_n(ji,jj,jk) ) mld_prof(ji,jj) = MIN(mbkt(ji,jj), jk)
1953          END DO
1954        END DO
1955      END DO
1956      DO jj = 2, jpjm1
1957         DO ji = 2, jpim1
1958            zhmle(ji,jj) = gdepw_n(ji,jj, mld_prof(ji,jj))
1959         END DO
1960       END DO
1961   ! Calculate vertical buoyancy, heat and salinity fluxes due to MLE.
1962
1963      DO jj = 2, jpjm1
1964        DO ji = 2, jpim1
1965          IF ( lconv(ji,jj) ) THEN
1966             ztmp =  r1_ft(ji,jj) *  MIN( 111.e3_wp , e1u(ji,jj) ) / rn_osm_mle_lf
1967             ! zwt_fk(ji,jj) = 0.5_wp * ztmp * ( dbdx_mle(ji,jj) * zdtdx(ji,jj) + dbdy_mle(ji,jj) * zdtdy(ji,jj) &
1968             !      & + dbdx_mle(ji-1,jj) * zdtdx(ji-1,jj) + dbdy_mle(ji,jj-1) * zdtdy(ji,jj-1) )
1969             ! zws_fk(ji,jj) = 0.5_wp * ztmp * ( dbdx_mle(ji,jj) * zdsdx(ji,jj) + dbdy_mle(ji,jj) * zdsdy(ji,jj) &
1970             !      & + dbdx_mle(ji-1,jj) * zdsdx(ji-1,jj) + dbdy_mle(ji,jj-1) * zdsdy(ji,jj-1) )
1971             zdbds_mle = SQRT( 0.5_wp * ( dbdx_mle(ji,jj) * dbdx_mle(ji,jj) + dbdy_mle(ji,jj) * dbdy_mle(ji,jj) &
1972                  & + dbdx_mle(ji-1,jj) * dbdx_mle(ji-1,jj) + dbdy_mle(ji,jj-1) * dbdy_mle(ji,jj-1) ) )
1973             zwb_fk(ji,jj) = rn_osm_mle_ce * hmle(ji,jj) * hmle(ji,jj) *ztmp * zdbds_mle * zdbds_mle
1974      ! This velocity scale, defined in Fox-Kemper et al (2008), is needed for calculating dhdt.
1975             zvel_mle(ji,jj) = zdbds_mle * ztmp * hmle(ji,jj) * tmask(ji,jj,1)
1976             zdiff_mle(ji,jj) = 1.e-4_wp * ztmp * zdbds_mle * zhmle(ji,jj)**3  / rn_osm_mle_lf
1977          ENDIF
1978        END DO
1979      END DO
1980END SUBROUTINE zdf_osm_mle_parameters
1981
1982END SUBROUTINE zdf_osm
1983
1984
1985   SUBROUTINE zdf_osm_init
1986     !!----------------------------------------------------------------------
1987     !!                  ***  ROUTINE zdf_osm_init  ***
1988     !!
1989     !! ** Purpose :   Initialization of the vertical eddy diffivity and
1990     !!      viscosity when using a osm turbulent closure scheme
1991     !!
1992     !! ** Method  :   Read the namosm namelist and check the parameters
1993     !!      called at the first timestep (nit000)
1994     !!
1995     !! ** input   :   Namlist namosm
1996     !!----------------------------------------------------------------------
1997     INTEGER  ::   ios            ! local integer
1998     INTEGER  ::   ji, jj, jk     ! dummy loop indices
1999     REAL z1_t2
2000     !!
2001     NAMELIST/namzdf_osm/ ln_use_osm_la, rn_osm_la, rn_osm_dstokes, nn_ave &
2002          & ,nn_osm_wave, ln_dia_osm, rn_osm_hbl0 &
2003          & ,ln_kpprimix, rn_riinfty, rn_difri, ln_convmix, rn_difconv, nn_osm_wave &
2004          & ,ln_osm_mle
2005! Namelist for Fox-Kemper parametrization.
2006      NAMELIST/namosm_mle/ nn_osm_mle, rn_osm_mle_ce, rn_osm_mle_lf, rn_osm_mle_time, rn_osm_mle_lat,&
2007           & rn_osm_mle_rho_c, rn_osm_mle_thresh, rn_osm_mle_tau, ln_osm_hmle_limit, rn_osm_hmle_limit
2008
2009     !!----------------------------------------------------------------------
2010     !
2011     REWIND( numnam_ref )              ! Namelist namzdf_osm in reference namelist : Osmosis ML model
2012     READ  ( numnam_ref, namzdf_osm, IOSTAT = ios, ERR = 901)
2013901  IF( ios /= 0 ) CALL ctl_nam ( ios , 'namzdf_osm in reference namelist' )
2014
2015     REWIND( numnam_cfg )              ! Namelist namzdf_tke in configuration namelist : Turbulent Kinetic Energy
2016     READ  ( numnam_cfg, namzdf_osm, IOSTAT = ios, ERR = 902 )
2017902  IF( ios >  0 ) CALL ctl_nam ( ios , 'namzdf_osm in configuration namelist' )
2018     IF(lwm) WRITE ( numond, namzdf_osm )
2019
2020     IF(lwp) THEN                    ! Control print
2021        WRITE(numout,*)
2022        WRITE(numout,*) 'zdf_osm_init : OSMOSIS Parameterisation'
2023        WRITE(numout,*) '~~~~~~~~~~~~'
2024        WRITE(numout,*) '   Namelist namzdf_osm : set osm mixing parameters'
2025        WRITE(numout,*) '     Use  rn_osm_la                                ln_use_osm_la = ', ln_use_osm_la
2026        WRITE(numout,*) '     Use  MLE in OBL, i.e. Fox-Kemper param        ln_osm_mle = ', ln_osm_mle
2027        WRITE(numout,*) '     Turbulent Langmuir number                     rn_osm_la   = ', rn_osm_la
2028        WRITE(numout,*) '     Initial hbl for 1D runs                       rn_osm_hbl0   = ', rn_osm_hbl0
2029        WRITE(numout,*) '     Depth scale of Stokes drift                   rn_osm_dstokes = ', rn_osm_dstokes
2030        WRITE(numout,*) '     horizontal average flag                       nn_ave      = ', nn_ave
2031        WRITE(numout,*) '     Stokes drift                                  nn_osm_wave = ', nn_osm_wave
2032        SELECT CASE (nn_osm_wave)
2033        CASE(0)
2034           WRITE(numout,*) '     calculated assuming constant La#=0.3'
2035        CASE(1)
2036           WRITE(numout,*) '     calculated from Pierson Moskowitz wind-waves'
2037        CASE(2)
2038           WRITE(numout,*) '     calculated from ECMWF wave fields'
2039        END SELECT
2040        WRITE(numout,*) '     Output osm diagnostics                       ln_dia_osm  = ',  ln_dia_osm
2041        WRITE(numout,*) '     Use KPP-style shear instability mixing       ln_kpprimix = ', ln_kpprimix
2042        WRITE(numout,*) '     local Richardson Number limit for shear instability rn_riinfty = ', rn_riinfty
2043        WRITE(numout,*) '     maximum shear diffusivity at Rig = 0    (m2/s) rn_difri = ', rn_difri
2044        WRITE(numout,*) '     Use large mixing below BL when unstable       ln_convmix = ', ln_convmix
2045        WRITE(numout,*) '     diffusivity when unstable below BL     (m2/s) rn_difconv = ', rn_difconv
2046     ENDIF
2047
2048     !                              ! allocate zdfosm arrays
2049     IF( zdf_osm_alloc() /= 0 )   CALL ctl_stop( 'STOP', 'zdf_osm_init : unable to allocate arrays' )
2050
2051
2052     IF( ln_osm_mle ) THEN
2053! Initialise Fox-Kemper parametrization
2054         REWIND( numnam_ref )              ! Namelist namosm_mle in reference namelist : Tracer advection scheme
2055         READ  ( numnam_ref, namosm_mle, IOSTAT = ios, ERR = 903)
2056903      IF( ios /= 0 )   CALL ctl_nam ( ios , 'namosm_mle in reference namelist')
2057
2058         REWIND( numnam_cfg )              ! Namelist namosm_mle in configuration namelist : Tracer advection scheme
2059         READ  ( numnam_cfg, namosm_mle, IOSTAT = ios, ERR = 904 )
2060904      IF( ios >  0 )   CALL ctl_nam ( ios , 'namosm_mle in configuration namelist')
2061         IF(lwm) WRITE ( numond, namosm_mle )
2062
2063         IF(lwp) THEN                     ! Namelist print
2064            WRITE(numout,*)
2065            WRITE(numout,*) 'zdf_osm_init : initialise mixed layer eddy (MLE)'
2066            WRITE(numout,*) '~~~~~~~~~~~~~'
2067            WRITE(numout,*) '   Namelist namosm_mle : '
2068            WRITE(numout,*) '         MLE type: =0 standard Fox-Kemper ; =1 new formulation        nn_osm_mle    = ', nn_osm_mle
2069            WRITE(numout,*) '         magnitude of the MLE (typical value: 0.06 to 0.08)           rn_osm_mle_ce    = ', rn_osm_mle_ce
2070            WRITE(numout,*) '         scale of ML front (ML radius of deformation) (nn_osm_mle=0)      rn_osm_mle_lf     = ', rn_osm_mle_lf, 'm'
2071            WRITE(numout,*) '         maximum time scale of MLE                    (nn_osm_mle=0)      rn_osm_mle_time   = ', rn_osm_mle_time, 's'
2072            WRITE(numout,*) '         reference latitude (degrees) of MLE coef.    (nn_osm_mle=1)      rn_osm_mle_lat    = ', rn_osm_mle_lat, 'deg'
2073            WRITE(numout,*) '         Density difference used to define ML for FK              rn_osm_mle_rho_c  = ', rn_osm_mle_rho_c
2074            WRITE(numout,*) '         Threshold used to define ML for FK                      rn_osm_mle_thresh  = ', rn_osm_mle_thresh, 'm^2/s'
2075            WRITE(numout,*) '         Timescale for OSM-FK                                         rn_osm_mle_tau  = ', rn_osm_mle_tau, 's'
2076            WRITE(numout,*) '         switch to limit hmle                                      ln_osm_hmle_limit  = ', ln_osm_hmle_limit
2077            WRITE(numout,*) '         fraction of zmld to limit hmle to if ln_osm_hmle_limit =.T.  rn_osm_hmle_limit = ', rn_osm_hmle_limit
2078         ENDIF         !
2079     ENDIF
2080      !
2081      IF(lwp) THEN
2082         WRITE(numout,*)
2083         IF( ln_osm_mle ) THEN
2084            WRITE(numout,*) '   ==>>>   Mixed Layer Eddy induced transport added to OSMOSIS BL calculation'
2085            IF( nn_osm_mle == 0 )   WRITE(numout,*) '              Fox-Kemper et al 2010 formulation'
2086            IF( nn_osm_mle == 1 )   WRITE(numout,*) '              New formulation'
2087         ELSE
2088            WRITE(numout,*) '   ==>>>   Mixed Layer induced transport NOT added to OSMOSIS BL calculation'
2089         ENDIF
2090      ENDIF
2091      !
2092      IF( ln_osm_mle ) THEN                ! MLE initialisation
2093         !
2094         rb_c = grav * rn_osm_mle_rho_c /rau0        ! Mixed Layer buoyancy criteria
2095         IF(lwp) WRITE(numout,*)
2096         IF(lwp) WRITE(numout,*) '      ML buoyancy criteria = ', rb_c, ' m/s2 '
2097         IF(lwp) WRITE(numout,*) '      associated ML density criteria defined in zdfmxl = ', rn_osm_mle_rho_c, 'kg/m3'
2098         !
2099         IF( nn_osm_mle == 0 ) THEN           ! MLE array allocation & initialisation            !
2100!
2101         ELSEIF( nn_osm_mle == 1 ) THEN           ! MLE array allocation & initialisation
2102            rc_f = rn_osm_mle_ce/ (  5.e3_wp * 2._wp * omega * SIN( rad * rn_osm_mle_lat )  )
2103            !
2104         ENDIF
2105         !                                ! 1/(f^2+tau^2)^1/2 at t-point (needed in both nn_osm_mle case)
2106         z1_t2 = 2.e-5
2107         do jj=1,jpj
2108            do ji = 1,jpi
2109               r1_ft(ji,jj) = MIN(1./( ABS(ff_t(ji,jj)) + epsln ), ABS(ff_t(ji,jj))/z1_t2**2)
2110            end do
2111         end do
2112         ! z1_t2 = 1._wp / ( rn_osm_mle_time * rn_osm_mle_timeji,jj )
2113         ! r1_ft(:,:) = 1._wp / SQRT(  ff_t(:,:) * ff_t(:,:) + z1_t2  )
2114         !
2115      ENDIF
2116
2117     call osm_rst( nit000, 'READ' ) !* read or initialize hbl, dh, hmle
2118
2119
2120     IF( ln_zdfddm) THEN
2121        IF(lwp) THEN
2122           WRITE(numout,*)
2123           WRITE(numout,*) '    Double diffusion mixing on temperature and salinity '
2124           WRITE(numout,*) '    CAUTION : done in routine zdfosm, not in routine zdfddm '
2125        ENDIF
2126     ENDIF
2127
2128
2129     !set constants not in namelist
2130     !-----------------------------
2131
2132     IF(lwp) THEN
2133        WRITE(numout,*)
2134     ENDIF
2135
2136     IF (nn_osm_wave == 0) THEN
2137        dstokes(:,:) = rn_osm_dstokes
2138     END IF
2139
2140     ! Horizontal average : initialization of weighting arrays
2141     ! -------------------
2142
2143     SELECT CASE ( nn_ave )
2144
2145     CASE ( 0 )                ! no horizontal average
2146        IF(lwp) WRITE(numout,*) '          no horizontal average on avt'
2147        IF(lwp) WRITE(numout,*) '          only in very high horizontal resolution !'
2148        ! weighting mean arrays etmean
2149        !           ( 1  1 )
2150        ! avt = 1/4 ( 1  1 )
2151        !
2152        etmean(:,:,:) = 0.e0
2153
2154        DO jk = 1, jpkm1
2155           DO jj = 2, jpjm1
2156              DO ji = 2, jpim1   ! vector opt.
2157                 etmean(ji,jj,jk) = tmask(ji,jj,jk)                     &
2158                      &  / MAX( 1.,  umask(ji-1,jj  ,jk) + umask(ji,jj,jk)   &
2159                      &            + vmask(ji  ,jj-1,jk) + vmask(ji,jj,jk)  )
2160              END DO
2161           END DO
2162        END DO
2163
2164     CASE ( 1 )                ! horizontal average
2165        IF(lwp) WRITE(numout,*) '          horizontal average on avt'
2166        ! weighting mean arrays etmean
2167        !           ( 1/2  1  1/2 )
2168        ! avt = 1/8 ( 1    2  1   )
2169        !           ( 1/2  1  1/2 )
2170        etmean(:,:,:) = 0.e0
2171
2172        DO jk = 1, jpkm1
2173           DO jj = 2, jpjm1
2174              DO ji = 2, jpim1   ! vector opt.
2175                 etmean(ji,jj,jk) = tmask(ji, jj,jk)                           &
2176                      & / MAX( 1., 2.* tmask(ji,jj,jk)                           &
2177                      &      +.5 * ( tmask(ji-1,jj+1,jk) + tmask(ji-1,jj-1,jk)   &
2178                      &             +tmask(ji+1,jj+1,jk) + tmask(ji+1,jj-1,jk) ) &
2179                      &      +1. * ( tmask(ji-1,jj  ,jk) + tmask(ji  ,jj+1,jk)   &
2180                      &             +tmask(ji  ,jj-1,jk) + tmask(ji+1,jj  ,jk) ) )
2181              END DO
2182           END DO
2183        END DO
2184
2185     CASE DEFAULT
2186        WRITE(ctmp1,*) '          bad flag value for nn_ave = ', nn_ave
2187        CALL ctl_stop( ctmp1 )
2188
2189     END SELECT
2190
2191     ! Initialization of vertical eddy coef. to the background value
2192     ! -------------------------------------------------------------
2193     DO jk = 1, jpk
2194        avt (:,:,jk) = avtb(jk) * tmask(:,:,jk)
2195     END DO
2196
2197     ! zero the surface flux for non local term and osm mixed layer depth
2198     ! ------------------------------------------------------------------
2199     ghamt(:,:,:) = 0.
2200     ghams(:,:,:) = 0.
2201     ghamu(:,:,:) = 0.
2202     ghamv(:,:,:) = 0.
2203     !
2204     IF( lwxios ) THEN
2205        CALL iom_set_rstw_var_active('wn')
2206        CALL iom_set_rstw_var_active('hbl')
2207        CALL iom_set_rstw_var_active('dh')
2208        IF( ln_osm_mle ) THEN
2209            CALL iom_set_rstw_var_active('hmle')
2210        END IF
2211     ENDIF
2212   END SUBROUTINE zdf_osm_init
2213
2214
2215   SUBROUTINE osm_rst( kt, cdrw )
2216     !!---------------------------------------------------------------------
2217     !!                   ***  ROUTINE osm_rst  ***
2218     !!
2219     !! ** Purpose :   Read or write BL fields in restart file
2220     !!
2221     !! ** Method  :   use of IOM library. If the restart does not contain
2222     !!                required fields, they are recomputed from stratification
2223     !!----------------------------------------------------------------------
2224
2225     INTEGER, INTENT(in) :: kt
2226     CHARACTER(len=*), INTENT(in) ::   cdrw   ! "READ"/"WRITE" flag
2227
2228     INTEGER ::   id1, id2, id3   ! iom enquiry index
2229     INTEGER  ::   ji, jj, jk     ! dummy loop indices
2230     INTEGER  ::   iiki, ikt ! local integer
2231     REAL(wp) ::   zhbf           ! tempory scalars
2232     REAL(wp) ::   zN2_c           ! local scalar
2233     REAL(wp) ::   rho_c = 0.01_wp    !: density criterion for mixed layer depth
2234     INTEGER, DIMENSION(jpi,jpj) :: imld_rst ! level of mixed-layer depth (pycnocline top)
2235     !!----------------------------------------------------------------------
2236     !
2237     !!-----------------------------------------------------------------------------
2238     ! If READ/WRITE Flag is 'READ', try to get hbl from restart file. If successful then return
2239     !!-----------------------------------------------------------------------------
2240     IF( TRIM(cdrw) == 'READ'.AND. ln_rstart) THEN
2241        id1 = iom_varid( numror, 'wn'   , ldstop = .FALSE. )
2242        IF( id1 > 0 ) THEN                       ! 'wn' exists; read
2243           CALL iom_get( numror, jpdom_autoglo, 'wn', wn, ldxios = lrxios )
2244           WRITE(numout,*) ' ===>>>> :  wn read from restart file'
2245        ELSE
2246           wn(:,:,:) = 0._wp
2247           WRITE(numout,*) ' ===>>>> :  wn not in restart file, set to zero initially'
2248        END IF
2249
2250        id1 = iom_varid( numror, 'hbl'   , ldstop = .FALSE. )
2251        id2 = iom_varid( numror, 'dh'   , ldstop = .FALSE. )
2252        IF( id1 > 0 .AND. id2 > 0) THEN                       ! 'hbl' exists; read and return
2253           CALL iom_get( numror, jpdom_autoglo, 'hbl' , hbl , ldxios = lrxios )
2254           CALL iom_get( numror, jpdom_autoglo, 'dh', dh, ldxios = lrxios  )
2255           WRITE(numout,*) ' ===>>>> :  hbl & dh read from restart file'
2256           IF( ln_osm_mle ) THEN
2257              id3 = iom_varid( numror, 'hmle'   , ldstop = .FALSE. )
2258              IF( id3 > 0) THEN
2259                 CALL iom_get( numror, jpdom_autoglo, 'hmle' , hmle , ldxios = lrxios )
2260                 WRITE(numout,*) ' ===>>>> :  hmle read from restart file'
2261              ELSE
2262                 WRITE(numout,*) ' ===>>>> :  hmle not found, set to hbl'
2263                 hmle(:,:) = hbl(:,:)            ! Initialise MLE depth.
2264              END IF
2265           END IF
2266           RETURN
2267        ELSE                      ! 'hbl' & 'dh' not in restart file, recalculate
2268           WRITE(numout,*) ' ===>>>> : previous run without osmosis scheme, hbl computed from stratification'
2269        END IF
2270     END IF
2271
2272     !!-----------------------------------------------------------------------------
2273     ! If READ/WRITE Flag is 'WRITE', write hbl into the restart file, then return
2274     !!-----------------------------------------------------------------------------
2275     IF( TRIM(cdrw) == 'WRITE') THEN     !* Write hbli into the restart file, then return
2276        IF(lwp) WRITE(numout,*) '---- osm-rst ----'
2277         CALL iom_rstput( kt, nitrst, numrow, 'wn'     , wn,   ldxios = lwxios )
2278         CALL iom_rstput( kt, nitrst, numrow, 'hbl'    , hbl,  ldxios = lwxios )
2279         CALL iom_rstput( kt, nitrst, numrow, 'dh'     , dh,   ldxios = lwxios )
2280         IF( ln_osm_mle ) THEN
2281            CALL iom_rstput( kt, nitrst, numrow, 'hmle', hmle, ldxios = lwxios )
2282         END IF
2283        RETURN
2284     END IF
2285
2286     !!-----------------------------------------------------------------------------
2287     ! Getting hbl, no restart file with hbl, so calculate from surface stratification
2288     !!-----------------------------------------------------------------------------
2289     IF( lwp ) WRITE(numout,*) ' ===>>>> : calculating hbl computed from stratification'
2290     ! w-level of the mixing and mixed layers
2291     CALL eos_rab( tsn, rab_n )
2292     CALL bn2(tsn, rab_n, rn2)
2293     imld_rst(:,:)  = nlb10         ! Initialization to the number of w ocean point
2294     hbl(:,:)  = 0._wp              ! here hbl used as a dummy variable, integrating vertically N^2
2295     zN2_c = grav * rho_c * r1_rau0 ! convert density criteria into N^2 criteria
2296     !
2297     hbl(:,:)  = 0._wp              ! here hbl used as a dummy variable, integrating vertically N^2
2298     DO jk = 1, jpkm1
2299        DO jj = 1, jpj              ! Mixed layer level: w-level
2300           DO ji = 1, jpi
2301              ikt = mbkt(ji,jj)
2302              hbl(ji,jj) = hbl(ji,jj) + MAX( rn2(ji,jj,jk) , 0._wp ) * e3w_n(ji,jj,jk)
2303              IF( hbl(ji,jj) < zN2_c )   imld_rst(ji,jj) = MIN( jk , ikt ) + 1   ! Mixed layer level
2304           END DO
2305        END DO
2306     END DO
2307     !
2308     DO jj = 1, jpj
2309        DO ji = 1, jpi
2310           iiki = MAX(4,imld_rst(ji,jj))
2311           hbl (ji,jj) = gdepw_n(ji,jj,iiki  )    ! Turbocline depth
2312           dh (ji,jj) = e3t_n(ji,jj,iiki-1  )     ! Turbocline depth
2313        END DO
2314     END DO
2315
2316     WRITE(numout,*) ' ===>>>> : hbl computed from stratification'
2317
2318     IF( ln_osm_mle ) THEN
2319        hmle(:,:) = hbl(:,:)            ! Initialise MLE depth.
2320        WRITE(numout,*) ' ===>>>> : hmle set = to hbl'
2321     END IF
2322
2323     wn(:,:,:) = 0._wp
2324     WRITE(numout,*) ' ===>>>> :  wn not in restart file, set to zero initially'
2325   END SUBROUTINE osm_rst
2326
2327
2328   SUBROUTINE tra_osm( kt )
2329      !!----------------------------------------------------------------------
2330      !!                  ***  ROUTINE tra_osm  ***
2331      !!
2332      !! ** Purpose :   compute and add to the tracer trend the non-local tracer flux
2333      !!
2334      !! ** Method  :   ???
2335      !!----------------------------------------------------------------------
2336      REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::   ztrdt, ztrds   ! 3D workspace
2337      !!----------------------------------------------------------------------
2338      INTEGER, INTENT(in) :: kt
2339      INTEGER :: ji, jj, jk
2340      !
2341      IF( kt == nit000 ) THEN
2342         IF(lwp) WRITE(numout,*)
2343         IF(lwp) WRITE(numout,*) 'tra_osm : OSM non-local tracer fluxes'
2344         IF(lwp) WRITE(numout,*) '~~~~~~~   '
2345      ENDIF
2346
2347      IF( l_trdtra )   THEN                    !* Save ta and sa trends
2348         ALLOCATE( ztrdt(jpi,jpj,jpk) )   ;    ztrdt(:,:,:) = tsa(:,:,:,jp_tem)
2349         ALLOCATE( ztrds(jpi,jpj,jpk) )   ;    ztrds(:,:,:) = tsa(:,:,:,jp_sal)
2350      ENDIF
2351
2352      DO jk = 1, jpkm1
2353         DO jj = 2, jpjm1
2354            DO ji = 2, jpim1
2355               tsa(ji,jj,jk,jp_tem) =  tsa(ji,jj,jk,jp_tem)                      &
2356                  &                 - (  ghamt(ji,jj,jk  )  &
2357                  &                    - ghamt(ji,jj,jk+1) ) /e3t_n(ji,jj,jk)
2358               tsa(ji,jj,jk,jp_sal) =  tsa(ji,jj,jk,jp_sal)                      &
2359                  &                 - (  ghams(ji,jj,jk  )  &
2360                  &                    - ghams(ji,jj,jk+1) ) / e3t_n(ji,jj,jk)
2361            END DO
2362         END DO
2363      END DO
2364
2365      ! save the non-local tracer flux trends for diagnostics
2366      IF( l_trdtra )   THEN
2367         ztrdt(:,:,:) = tsa(:,:,:,jp_tem) - ztrdt(:,:,:)
2368         ztrds(:,:,:) = tsa(:,:,:,jp_sal) - ztrds(:,:,:)
2369
2370         CALL trd_tra( kt, 'TRA', jp_tem, jptra_osm, ztrdt )
2371         CALL trd_tra( kt, 'TRA', jp_sal, jptra_osm, ztrds )
2372         DEALLOCATE( ztrdt )      ;     DEALLOCATE( ztrds )
2373      ENDIF
2374
2375      IF(ln_ctl) THEN
2376         CALL prt_ctl( tab3d_1=tsa(:,:,:,jp_tem), clinfo1=' osm  - Ta: ', mask1=tmask,   &
2377         &             tab3d_2=tsa(:,:,:,jp_sal), clinfo2=       ' Sa: ', mask2=tmask, clinfo3='tra' )
2378      ENDIF
2379      !
2380   END SUBROUTINE tra_osm
2381
2382
2383   SUBROUTINE trc_osm( kt )          ! Dummy routine
2384      !!----------------------------------------------------------------------
2385      !!                  ***  ROUTINE trc_osm  ***
2386      !!
2387      !! ** Purpose :   compute and add to the passive tracer trend the non-local
2388      !!                 passive tracer flux
2389      !!
2390      !!
2391      !! ** Method  :   ???
2392      !!----------------------------------------------------------------------
2393      !
2394      !!----------------------------------------------------------------------
2395      INTEGER, INTENT(in) :: kt
2396      WRITE(*,*) 'trc_osm: Not written yet', kt
2397   END SUBROUTINE trc_osm
2398
2399
2400   SUBROUTINE dyn_osm( kt )
2401      !!----------------------------------------------------------------------
2402      !!                  ***  ROUTINE dyn_osm  ***
2403      !!
2404      !! ** Purpose :   compute and add to the velocity trend the non-local flux
2405      !! copied/modified from tra_osm
2406      !!
2407      !! ** Method  :   ???
2408      !!----------------------------------------------------------------------
2409      INTEGER, INTENT(in) ::   kt   !
2410      !
2411      INTEGER :: ji, jj, jk   ! dummy loop indices
2412      !!----------------------------------------------------------------------
2413      !
2414      IF( kt == nit000 ) THEN
2415         IF(lwp) WRITE(numout,*)
2416         IF(lwp) WRITE(numout,*) 'dyn_osm : OSM non-local velocity'
2417         IF(lwp) WRITE(numout,*) '~~~~~~~   '
2418      ENDIF
2419      !code saving tracer trends removed, replace with trdmxl_oce
2420
2421      DO jk = 1, jpkm1           ! add non-local u and v fluxes
2422         DO jj = 2, jpjm1
2423            DO ji = 2, jpim1
2424               ua(ji,jj,jk) =  ua(ji,jj,jk)                      &
2425                  &                 - (  ghamu(ji,jj,jk  )  &
2426                  &                    - ghamu(ji,jj,jk+1) ) / e3u_n(ji,jj,jk)
2427               va(ji,jj,jk) =  va(ji,jj,jk)                      &
2428                  &                 - (  ghamv(ji,jj,jk  )  &
2429                  &                    - ghamv(ji,jj,jk+1) ) / e3v_n(ji,jj,jk)
2430            END DO
2431         END DO
2432      END DO
2433      !
2434      ! code for saving tracer trends removed
2435      !
2436   END SUBROUTINE dyn_osm
2437
2438   !!======================================================================
2439
2440END MODULE zdfosm
Note: See TracBrowser for help on using the repository browser.