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 @ 14283

Last change on this file since 14283 was 14283, checked in by agn, 3 years ago

correct various bugs in shear code

zd_cubic def in working code
correct bugs found by checking reproducibility
correct bugs found in email of 26 Nov 20
correct loop index limit in zdf_osm_osbl_state
Ensure salinity in ztsm_midu does not go to zero in zdf_osm_zmld_horizontal_gradients
Corrected bug Simon found in PE calcuklation

  • Property svn:keywords set to Id
File size: 159.7 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 zdfmxl         ! mixed layer depth
61   USE iom            ! I/O library
62   USE lib_mpp        ! MPP library
63   USE trd_oce        ! ocean trends definition
64   USE trdtra         ! tracers trends
65   !
66   USE in_out_manager ! I/O manager
67   USE lbclnk         ! ocean lateral boundary conditions (or mpp link)
68   USE prtctl         ! Print control
69   USE lib_fortran    ! Fortran utilities (allows no signed zero when 'key_nosignedzero' defined)
70
71   IMPLICIT NONE
72   PRIVATE
73
74   PUBLIC   zdf_osm       ! routine called by step.F90
75   PUBLIC   zdf_osm_init  ! routine called by nemogcm.F90
76   PUBLIC   osm_rst       ! routine called by step.F90
77   PUBLIC   tra_osm       ! routine called by step.F90
78   PUBLIC   trc_osm       ! routine called by trcstp.F90
79   PUBLIC   dyn_osm       ! routine called by step.F90
80
81   PUBLIC   ln_osm_mle    ! logical needed by tra_mle_init in tramle.F90
82
83   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:) ::   ghamu    !: non-local u-momentum flux
84   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:) ::   ghamv    !: non-local v-momentum flux
85   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:) ::   ghamt    !: non-local temperature flux (gamma/<ws>o)
86   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:) ::   ghams    !: non-local salinity flux (gamma/<ws>o)
87   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:) ::   etmean   !: averaging operator for avt
88   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   hbl      !: boundary layer depth
89   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   dh       ! depth of pycnocline
90   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   hml      ! ML depth
91   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   dstokes  !: penetration depth of the Stokes drift.
92
93   REAL(wp), ALLOCATABLE, SAVE, DIMENSION(:,:)           ::   r1_ft    ! inverse of the modified Coriolis parameter at t-pts
94   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   hmle     ! Depth of layer affexted by mixed layer eddies in Fox-Kemper parametrization
95   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   dbdx_mle ! zonal buoyancy gradient in ML
96   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   dbdy_mle ! meridional buoyancy gradient in ML
97   INTEGER,  PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   mld_prof ! level of base of MLE layer.
98
99   !                      !!** Namelist  namzdf_osm  **
100   LOGICAL  ::   ln_use_osm_la      ! Use namelist  rn_osm_la
101
102   LOGICAL  ::   ln_osm_mle           !: flag to activate the Mixed Layer Eddy (MLE) parameterisation
103
104   REAL(wp) ::   rn_osm_la          ! Turbulent Langmuir number
105   REAL(wp) ::   rn_osm_dstokes     ! Depth scale of Stokes drift
106   REAL(wp) ::   rn_zdfosm_adjust_sd = 1.0 ! factor to reduce Stokes drift by
107   REAL(wp) ::   rn_osm_hblfrac = 0.1! for nn_osm_wave = 3/4 specify fraction in top of hbl
108   LOGICAL  ::   ln_zdfosm_ice_shelter      ! flag to activate ice sheltering
109   REAL(wp) ::   rn_osm_hbl0 = 10._wp       ! Initial value of hbl for 1D runs
110   INTEGER  ::   nn_ave             ! = 0/1 flag for horizontal average on avt
111   INTEGER  ::   nn_osm_wave = 0    ! = 0/1/2 flag for getting stokes drift from La# / PM wind-waves/Inputs into sbcwave
112   INTEGER  ::   nn_osm_SD_reduce   ! = 0/1/2 flag for getting effective stokes drift from surface value
113   LOGICAL  ::   ln_dia_osm         ! Use namelist  rn_osm_la
114
115   LOGICAL  ::   ln_kpprimix  = .true.  ! Shear instability mixing
116   REAL(wp) ::   rn_riinfty   = 0.7     ! local Richardson Number limit for shear instability
117   REAL(wp) ::   rn_difri    =  0.005   ! maximum shear mixing at Rig = 0    (m2/s)
118   LOGICAL  ::   ln_convmix  = .true.   ! Convective instability mixing
119   REAL(wp) ::   rn_difconv = 1._wp     ! diffusivity when unstable below BL  (m2/s)
120
121! OSMOSIS mixed layer eddy parametrization constants
122   INTEGER  ::   nn_osm_mle             ! = 0/1 flag for horizontal average on avt
123   REAL(wp) ::   rn_osm_mle_ce           ! MLE coefficient
124   !                                        ! parameters used in nn_osm_mle = 0 case
125   REAL(wp) ::   rn_osm_mle_lf               ! typical scale of mixed layer front
126   REAL(wp) ::   rn_osm_mle_time             ! time scale for mixing momentum across the mixed layer
127   !                                        ! parameters used in nn_osm_mle = 1 case
128   REAL(wp) ::   rn_osm_mle_lat              ! reference latitude for a 5 km scale of ML front
129   LOGICAL  ::   ln_osm_hmle_limit           ! If true arbitrarily restrict hmle to rn_osm_hmle_limit*zmld
130   REAL(wp) ::   rn_osm_hmle_limit           ! If ln_osm_hmle_limit true arbitrarily restrict hmle to rn_osm_hmle_limit*zmld
131   REAL(wp) ::   rn_osm_mle_rho_c        ! Density criterion for definition of MLD used by FK
132   REAL(wp) ::   r5_21 = 5.e0 / 21.e0   ! factor used in mle streamfunction computation
133   REAL(wp) ::   rb_c                   ! ML buoyancy criteria = g rho_c /rau0 where rho_c is defined in zdfmld
134   REAL(wp) ::   rc_f                   ! MLE coefficient (= rn_ce / (5 km * fo) ) in nn_osm_mle=1 case
135   REAL(wp) ::   rn_osm_mle_thresh          ! Threshold buoyancy for deepening of MLE layer below OSBL base.
136   REAL(wp) ::   rn_osm_bl_thresh          ! Threshold buoyancy for deepening of OSBL base.
137   REAL(wp) ::   rn_osm_mle_tau             ! Adjustment timescale for MLE.
138
139
140   !                                    !!! ** General constants  **
141   REAL(wp) ::   epsln   = 1.0e-20_wp   ! a small positive number to ensure no div by zero
142   REAL(wp) ::   depth_tol = 1.0e-6_wp  ! a small-ish positive number to give a hbl slightly shallower than gdepw
143   REAL(wp) ::   pthird  = 1._wp/3._wp  ! 1/3
144   REAL(wp) ::   p2third = 2._wp/3._wp  ! 2/3
145
146   INTEGER :: idebug = 236
147   INTEGER :: jdebug = 228
148   !!----------------------------------------------------------------------
149   !! NEMO/OCE 4.0 , NEMO Consortium (2018)
150   !! $Id: zdfosm.F90 12317 2020-01-14 12:40:47Z agn $
151   !! Software governed by the CeCILL license (see ./LICENSE)
152   !!----------------------------------------------------------------------
153CONTAINS
154
155   INTEGER FUNCTION zdf_osm_alloc()
156      !!----------------------------------------------------------------------
157      !!                 ***  FUNCTION zdf_osm_alloc  ***
158      !!----------------------------------------------------------------------
159     ALLOCATE( ghamu(jpi,jpj,jpk), ghamv(jpi,jpj,jpk), ghamt(jpi,jpj,jpk),ghams(jpi,jpj,jpk), &
160          &       hbl(jpi,jpj), dh(jpi,jpj), hml(jpi,jpj), dstokes(jpi, jpj), &
161          &       etmean(jpi,jpj,jpk), STAT= zdf_osm_alloc )
162
163     ALLOCATE(  hmle(jpi,jpj), r1_ft(jpi,jpj), dbdx_mle(jpi,jpj), dbdy_mle(jpi,jpj), &
164          &       mld_prof(jpi,jpj), STAT= zdf_osm_alloc )
165
166!     ALLOCATE( ghamu(jpi,jpj,jpk), ghamv(jpi,jpj,jpk), ghamt(jpi,jpj,jpk),ghams(jpi,jpj,jpk), &    ! would ths be better ?
167!          &       hbl(jpi,jpj), dh(jpi,jpj), hml(jpi,jpj), dstokes(jpi, jpj), &
168!          &       etmean(jpi,jpj,jpk), STAT= zdf_osm_alloc )
169!     IF( zdf_osm_alloc /= 0 )   CALL ctl_warn('zdf_osm_alloc: failed to allocate zdf_osm arrays')
170!     IF ( ln_osm_mle ) THEN
171!        Allocate( hmle(jpi,jpj), r1_ft(jpi,jpj), STAT= zdf_osm_alloc )
172!        IF( zdf_osm_alloc /= 0 )   CALL ctl_warn('zdf_osm_alloc: failed to allocate zdf_osm mle arrays')
173!     ENDIF
174
175     IF( zdf_osm_alloc /= 0 )   CALL ctl_warn('zdf_osm_alloc: failed to allocate zdf_osm arrays')
176     CALL mpp_sum ( 'zdfosm', zdf_osm_alloc )
177   END FUNCTION zdf_osm_alloc
178
179
180   SUBROUTINE zdf_osm( kt, p_avm, p_avt )
181      !!----------------------------------------------------------------------
182      !!                   ***  ROUTINE zdf_osm  ***
183      !!
184      !! ** Purpose :   Compute the vertical eddy viscosity and diffusivity
185      !!      coefficients and non local mixing using the OSMOSIS scheme
186      !!
187      !! ** Method :   The boundary layer depth hosm is diagnosed at tracer points
188      !!      from profiles of buoyancy, and shear, and the surface forcing.
189      !!      Above hbl (sigma=-z/hbl <1) the mixing coefficients are computed from
190      !!
191      !!                      Kx =  hosm  Wx(sigma) G(sigma)
192      !!
193      !!             and the non local term ghamt = Cs / Ws(sigma) / hosm
194      !!      Below hosm  the coefficients are the sum of mixing due to internal waves
195      !!      shear instability and double diffusion.
196      !!
197      !!      -1- Compute the now interior vertical mixing coefficients at all depths.
198      !!      -2- Diagnose the boundary layer depth.
199      !!      -3- Compute the now boundary layer vertical mixing coefficients.
200      !!      -4- Compute the now vertical eddy vicosity and diffusivity.
201      !!      -5- Smoothing
202      !!
203      !!        N.B. The computation is done from jk=2 to jpkm1
204      !!             Surface value of avt are set once a time to zero
205      !!             in routine zdf_osm_init.
206      !!
207      !! ** Action  :   update the non-local terms ghamts
208      !!                update avt (before vertical eddy coef.)
209      !!
210      !! References : Large W.G., Mc Williams J.C. and Doney S.C.
211      !!         Reviews of Geophysics, 32, 4, November 1994
212      !!         Comments in the code refer to this paper, particularly
213      !!         the equation number. (LMD94, here after)
214      !!----------------------------------------------------------------------
215      INTEGER                   , INTENT(in   ) ::   kt            ! ocean time step
216      REAL(wp), DIMENSION(:,:,:), INTENT(inout) ::  p_avm, p_avt   ! momentum and tracer Kz (w-points)
217      !!
218      INTEGER ::   ji, jj, jk                   ! dummy loop indices
219
220      INTEGER ::   jl                   ! dummy loop indices
221
222      INTEGER ::   ikbot, jkmax, jkm1, jkp2     !
223
224      REAL(wp) ::   ztx, zty, zflageos, zstabl, zbuofdep,zucube      !
225      REAL(wp) ::   zbeta, zthermal                                  !
226      REAL(wp) ::   zehat, zeta, zhrib, zsig, zscale, zwst, zws, zwm ! Velocity scales
227      REAL(wp) ::   zwsun, zwmun, zcons, zconm, zwcons, zwconm      !
228
229      REAL(wp) ::   zsr, zbw, ze, zb, zd, zc, zaw, za, zb1, za1, zkw, zk0, zcomp , zrhd,zrhdr,zbvzed   ! In situ density
230      INTEGER  ::   jm                          ! dummy loop indices
231      REAL(wp) ::   zr1, zr2, zr3, zr4, zrhop   ! Compression terms
232      REAL(wp) ::   zflag, zrn2, zdep21, zdep32, zdep43
233      REAL(wp) ::   zesh2, zri, zfri            ! Interior richardson mixing
234      REAL(wp) ::   zdelta, zdelta2, zdzup, zdzdn, zdzh, zvath, zgat1, zdat1, zkm1m, zkm1t
235      REAL(wp) :: zt,zs,zu,zv,zrh               ! variables used in constructing averages
236! Scales
237      REAL(wp), DIMENSION(jpi,jpj) :: zrad0     ! Surface solar temperature flux (deg m/s)
238      REAL(wp), DIMENSION(jpi,jpj) :: zradh     ! Radiative flux at bl base (Buoyancy units)
239      REAL(wp), DIMENSION(jpi,jpj) :: zradav    ! Radiative flux, bl average (Buoyancy Units)
240      REAL(wp), DIMENSION(jpi,jpj) :: zustar    ! friction velocity
241      REAL(wp), DIMENSION(jpi,jpj) :: zwstrl    ! Langmuir velocity scale
242      REAL(wp), DIMENSION(jpi,jpj) :: zvstr     ! Velocity scale that ends to zustar for large Langmuir number.
243      REAL(wp), DIMENSION(jpi,jpj) :: zwstrc    ! Convective velocity scale
244      REAL(wp), DIMENSION(jpi,jpj) :: zuw0      ! Surface u-momentum flux
245      REAL(wp), DIMENSION(jpi,jpj) :: zvw0      ! Surface v-momentum flux
246      REAL(wp), DIMENSION(jpi,jpj) :: zwth0     ! Surface heat flux (Kinematic)
247      REAL(wp), DIMENSION(jpi,jpj) :: zws0      ! Surface freshwater flux
248      REAL(wp), DIMENSION(jpi,jpj) :: zwb0      ! Surface buoyancy flux
249      REAL(wp), DIMENSION(jpi,jpj) :: zwthav    ! Heat flux - bl average
250      REAL(wp), DIMENSION(jpi,jpj) :: zwsav     ! freshwater flux - bl average
251      REAL(wp), DIMENSION(jpi,jpj) :: zwbav     ! Buoyancy flux - bl average
252      REAL(wp), DIMENSION(jpi,jpj) :: zwb_ent   ! Buoyancy entrainment flux
253      REAL(wp), DIMENSION(jpi,jpj) :: zwb_min
254
255
256      REAL(wp), DIMENSION(jpi,jpj) :: zwb_fk_b  ! MLE buoyancy flux averaged over OSBL
257      REAL(wp), DIMENSION(jpi,jpj) :: zwb_fk    ! max MLE buoyancy flux
258      REAL(wp), DIMENSION(jpi,jpj) :: zdiff_mle ! extra MLE vertical diff
259      REAL(wp), DIMENSION(jpi,jpj) :: zvel_mle  ! velocity scale for dhdt with stable ML and FK
260
261      REAL(wp), DIMENSION(jpi,jpj) :: zustke    ! Surface Stokes drift
262      REAL(wp), DIMENSION(jpi,jpj) :: zla       ! Trubulent Langmuir number
263      REAL(wp), DIMENSION(jpi,jpj) :: zcos_wind ! Cos angle of surface stress
264      REAL(wp), DIMENSION(jpi,jpj) :: zsin_wind ! Sin angle of surface stress
265      REAL(wp), DIMENSION(jpi,jpj) :: zhol      ! Stability parameter for boundary layer
266      LOGICAL, DIMENSION(jpi,jpj)  :: lconv     ! unstable/stable bl
267      LOGICAL, DIMENSION(jpi,jpj)  :: lshear    ! Shear layers
268      LOGICAL, DIMENSION(jpi,jpj)  :: lpyc      ! OSBL pycnocline present
269      LOGICAL, DIMENSION(jpi,jpj)  :: lflux     ! surface flux extends below OSBL into MLE layer.
270      LOGICAL, DIMENSION(jpi,jpj)  :: lmle      ! MLE layer increases in hickness.
271
272      ! mixed-layer variables
273
274      INTEGER, DIMENSION(jpi,jpj) :: ibld ! level of boundary layer base
275      INTEGER, DIMENSION(jpi,jpj) :: imld ! level of mixed-layer depth (pycnocline top)
276      INTEGER, DIMENSION(jpi,jpj) :: jp_ext, jp_ext_mle ! offset for external level
277      INTEGER, DIMENSION(jpi, jpj) :: j_ddh ! Type of shear layer
278
279      REAL(wp) :: ztgrad,zsgrad,zbgrad ! Temporary variables used to calculate pycnocline gradients
280      REAL(wp) :: zugrad,zvgrad        ! temporary variables for calculating pycnocline shear
281
282      REAL(wp), DIMENSION(jpi,jpj) :: zhbl  ! bl depth - grid
283      REAL(wp), DIMENSION(jpi,jpj) :: zhml  ! ml depth - grid
284
285      REAL(wp), DIMENSION(jpi,jpj) :: zhmle ! MLE depth - grid
286      REAL(wp), DIMENSION(jpi,jpj) :: zmld  ! ML depth on grid
287
288      REAL(wp), DIMENSION(jpi,jpj) :: zdh   ! pycnocline depth - grid
289      REAL(wp), DIMENSION(jpi,jpj) :: zdhdt ! BL depth tendency
290      REAL(wp), DIMENSION(jpi,jpj) :: zddhdt                                    ! correction to dhdt due to internal structure.
291      REAL(wp), DIMENSION(jpi,jpj) :: zdtdz_bl_ext,zdsdz_bl_ext,zdbdz_bl_ext              ! external temperature/salinity and buoyancy gradients
292      REAL(wp), DIMENSION(jpi,jpj) :: zdtdz_mle_ext,zdsdz_mle_ext,zdbdz_mle_ext              ! external temperature/salinity and buoyancy gradients
293      REAL(wp), DIMENSION(jpi,jpj) :: zdtdx, zdtdy, zdsdx, zdsdy      ! horizontal gradients for Fox-Kemper parametrization.
294
295      REAL(wp), DIMENSION(jpi,jpj) :: zt_bl,zs_bl,zu_bl,zv_bl,zb_bl  ! averages over the depth of the blayer
296      REAL(wp), DIMENSION(jpi,jpj) :: zt_ml,zs_ml,zu_ml,zv_ml,zb_ml  ! averages over the depth of the mixed layer
297      REAL(wp), DIMENSION(jpi,jpj) :: zt_mle,zs_mle,zu_mle,zv_mle,zb_mle  ! averages over the depth of the MLE layer
298      REAL(wp), DIMENSION(jpi,jpj) :: zdt_bl,zds_bl,zdu_bl,zdv_bl,zdb_bl ! difference between blayer average and parameter at base of blayer
299      REAL(wp), DIMENSION(jpi,jpj) :: zdt_ml,zds_ml,zdu_ml,zdv_ml,zdb_ml ! difference between mixed layer average and parameter at base of blayer
300      REAL(wp), DIMENSION(jpi,jpj) :: zdt_mle,zds_mle,zdu_mle,zdv_mle,zdb_mle ! difference between MLE layer average and parameter at base of blayer
301!      REAL(wp), DIMENSION(jpi,jpj) :: zwth_ent,zws_ent ! heat and salinity fluxes at the top of the pycnocline
302      REAL(wp) :: zwth_ent,zws_ent ! heat and salinity fluxes at the top of the pycnocline
303      REAL(wp) :: zuw_bse,zvw_bse  ! momentum fluxes at the top of the pycnocline
304      REAL(wp), DIMENSION(jpi,jpj,jpk) :: zdtdz_pyc    ! parametrized gradient of temperature in pycnocline
305      REAL(wp), DIMENSION(jpi,jpj,jpk) :: zdsdz_pyc    ! parametrised gradient of salinity in pycnocline
306      REAL(wp), DIMENSION(jpi,jpj,jpk) :: zdbdz_pyc    ! parametrised gradient of buoyancy in the pycnocline
307      REAL(wp), DIMENSION(jpi,jpj,jpk) :: zdudz_pyc    ! u-shear across the pycnocline
308      REAL(wp), DIMENSION(jpi,jpj,jpk) :: zdvdz_pyc    ! v-shear across the pycnocline
309      REAL(wp), DIMENSION(jpi,jpj) :: zdbds_mle    ! Magnitude of horizontal buoyancy gradient.
310      ! Flux-gradient relationship variables
311      REAL(wp), DIMENSION(jpi, jpj) :: zshear, zri_i ! Shear production and interfacial richardon number.
312
313      REAL(wp) :: zl_c,zl_l,zl_eps  ! Used to calculate turbulence length scale.
314
315      REAL(wp) :: za_cubic, zb_cubic, zc_cubic, zd_cubic ! coefficients in cubic polynomial specifying diffusivity in pycnocline. 
316      REAL(wp), DIMENSION(jpi,jpj) :: zsc_wth_1,zsc_ws_1 ! Temporary scales used to calculate scalar non-gradient terms.
317      REAL(wp), DIMENSION(jpi,jpj) :: zsc_wth_pyc, zsc_ws_pyc ! Scales for pycnocline transport term/
318      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.
319      REAL(wp), DIMENSION(jpi,jpj) :: zhbl_t ! holds boundary layer depth updated by full timestep
320
321      ! For calculating Ri#-dependent mixing
322      REAL(wp), DIMENSION(jpi,jpj,jpk) :: z3du   ! u-shear^2
323      REAL(wp), DIMENSION(jpi,jpj,jpk) :: z3dv   ! v-shear^2
324      REAL(wp), DIMENSION(jpi,jpj,jpk) :: zrimix ! spatial form of ri#-induced diffusion
325
326      ! Temporary variables
327      INTEGER :: inhml
328      REAL(wp) :: znd,znd_d,zznd_ml,zznd_pyc,zznd_d ! temporary non-dimensional depths used in various routines
329      REAL(wp) :: ztemp, zari, zpert, zzdhdt, zdb   ! temporary variables
330      REAL(wp) :: zthick, zz0, zz1 ! temporary variables
331      REAL(wp) :: zvel_max, zhbl_s ! temporary variables
332      REAL(wp) :: zfac, ztmp       ! temporary variable
333      REAL(wp) :: zus_x, zus_y     ! temporary Stokes drift
334      REAL(wp), DIMENSION(jpi,jpj,jpk) :: zviscos ! viscosity
335      REAL(wp), DIMENSION(jpi,jpj,jpk) :: zdiffut ! t-diffusivity
336      REAL(wp), DIMENSION(jpi,jpj) :: zalpha_pyc
337      REAL(wp), DIMENSION(jpi,jpj) :: ztau_sc_u ! dissipation timescale at baes of WML.
338      REAL(wp) :: zdelta_pyc, zwt_pyc_sc_1, zws_pyc_sc_1, zzeta_pyc
339      REAL(wp) :: zbuoy_pyc_sc, zomega, zvw_max
340      INTEGER :: ibld_ext=0                          ! does not have to be zero for modified scheme
341      REAL(wp) :: zgamma_b_nd, zgamma_b, zdhoh, ztau
342      REAL(wp) :: zzeta_s = 0._wp
343      REAL(wp) :: zzeta_v = 0.46
344      REAL(wp) :: zabsstke
345      REAL(wp) :: zsqrtpi, z_two_thirds, zproportion, ztransp, zthickness
346      REAL(wp) :: z2k_times_thickness, zsqrt_depth, zexp_depth, zdstokes0, zf, zexperfc
347
348      ! For debugging
349      INTEGER :: ikt
350      !!--------------------------------------------------------------------
351      !
352      ibld(:,:)   = 0     ; imld(:,:)  = 0
353      zrad0(:,:)  = 0._wp ; zradh(:,:) = 0._wp ; zradav(:,:)    = 0._wp ; zustar(:,:)    = 0._wp
354      zwstrl(:,:) = 0._wp ; zvstr(:,:) = 0._wp ; zwstrc(:,:)    = 0._wp ; zuw0(:,:)      = 0._wp
355      zvw0(:,:)   = 0._wp ; zwth0(:,:) = 0._wp ; zws0(:,:)      = 0._wp ; zwb0(:,:)      = 0._wp
356      zwthav(:,:) = 0._wp ; zwsav(:,:) = 0._wp ; zwbav(:,:)     = 0._wp ; zwb_ent(:,:)   = 0._wp
357      zustke(:,:) = 0._wp ; zla(:,:)   = 0._wp ; zcos_wind(:,:) = 0._wp ; zsin_wind(:,:) = 0._wp
358      zhol(:,:)   = 0._wp
359      lconv(:,:)  = .FALSE.; lpyc(:,:) = .FALSE. ; lflux(:,:) = .FALSE. ;  lmle(:,:) = .FALSE.
360      ! mixed layer
361      ! no initialization of zhbl or zhml (or zdh?)
362      zhbl(:,:)    = 1._wp ; zhml(:,:)    = 1._wp ; zdh(:,:)      = 1._wp ; zdhdt(:,:)   = 0._wp
363      zt_bl(:,:)   = 0._wp ; zs_bl(:,:)   = 0._wp ; zu_bl(:,:)    = 0._wp
364      zv_bl(:,:)   = 0._wp ; zb_bl(:,:)  = 0._wp
365      zt_ml(:,:)   = 0._wp ; zs_ml(:,:)    = 0._wp ; zu_ml(:,:)   = 0._wp
366      zt_mle(:,:)   = 0._wp ; zs_mle(:,:)    = 0._wp ; zu_mle(:,:)   = 0._wp
367      zb_mle(:,:) = 0._wp
368      zv_ml(:,:)   = 0._wp ; zdt_bl(:,:)   = 0._wp ; zds_bl(:,:)  = 0._wp
369      zdu_bl(:,:)  = 0._wp ; zdv_bl(:,:)  = 0._wp ; zdb_bl(:,:)  = 0._wp
370      zdt_ml(:,:)  = 0._wp ; zds_ml(:,:)  = 0._wp ; zdu_ml(:,:)   = 0._wp ; zdv_ml(:,:)  = 0._wp
371      zdb_ml(:,:)  = 0._wp
372      zdt_mle(:,:)  = 0._wp ; zds_mle(:,:)  = 0._wp ; zdu_mle(:,:)   = 0._wp
373      zdv_mle(:,:)  = 0._wp ; zdb_mle(:,:)  = 0._wp
374      zwth_ent = 0._wp ; zws_ent = 0._wp
375      !
376      zdtdz_pyc(:,:,:) = 0._wp ; zdsdz_pyc(:,:,:) = 0._wp ; zdbdz_pyc(:,:,:) = 0._wp
377      zdudz_pyc(:,:,:) = 0._wp ; zdvdz_pyc(:,:,:) = 0._wp
378      !
379      zdtdz_bl_ext(:,:) = 0._wp ; zdsdz_bl_ext(:,:) = 0._wp ; zdbdz_bl_ext(:,:) = 0._wp
380
381      IF ( ln_osm_mle ) THEN  ! only initialise arrays if needed
382         zdtdx(:,:) = 0._wp ; zdtdy(:,:) = 0._wp ; zdsdx(:,:) = 0._wp
383         zdsdy(:,:) = 0._wp ; dbdx_mle(:,:) = 0._wp ; dbdy_mle(:,:) = 0._wp
384         zwb_fk(:,:) = 0._wp ; zvel_mle(:,:) = 0._wp; zdiff_mle(:,:) = 0._wp
385         zhmle(:,:) = 0._wp  ; zmld(:,:) = 0._wp
386      ENDIF
387      zwb_fk_b(:,:) = 0._wp   ! must be initialised even with ln_osm_mle=F as used in zdf_osm_calculate_dhdt
388
389      ! Flux-Gradient arrays.
390      zsc_wth_1(:,:)  = 0._wp ; zsc_ws_1(:,:)   = 0._wp ; zsc_uw_1(:,:)   = 0._wp
391      zsc_uw_2(:,:)   = 0._wp ; zsc_vw_1(:,:)   = 0._wp ; zsc_vw_2(:,:)   = 0._wp
392      zhbl_t(:,:)     = 0._wp ; zdhdt(:,:)      = 0._wp
393
394      zdiffut(:,:,:) = 0._wp ; zviscos(:,:,:) = 0._wp ; ghamt(:,:,:) = 0._wp
395      ghams(:,:,:)   = 0._wp ; ghamu(:,:,:)   = 0._wp ; ghamv(:,:,:) = 0._wp
396
397      zddhdt(:,:) = 0._wp
398      ! hbl = MAX(hbl,epsln)
399      !>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
400      ! Calculate boundary layer scales
401      !<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
402
403      ! Assume two-band radiation model for depth of OSBL
404     zz0 =       rn_abs       ! surface equi-partition in 2-bands
405     zz1 =  1. - rn_abs
406     DO jj = 2, jpjm1
407        DO ji = 2, jpim1
408           ! Surface downward irradiance (so always +ve)
409           zrad0(ji,jj) = qsr(ji,jj) * r1_rau0_rcp
410           ! Downwards irradiance at base of boundary layer
411           zradh(ji,jj) = zrad0(ji,jj) * ( zz0 * EXP( -hbl(ji,jj)/rn_si0 ) + zz1 * EXP( -hbl(ji,jj)/rn_si1) )
412           ! Downwards irradiance averaged over depth of the OSBL
413           zradav(ji,jj) = zrad0(ji,jj) * ( zz0 * ( 1.0 - EXP( -hbl(ji,jj)/rn_si0 ) )*rn_si0 &
414                 &                         + zz1 * ( 1.0 - EXP( -hbl(ji,jj)/rn_si1 ) )*rn_si1 ) / hbl(ji,jj)
415        END DO
416     END DO
417     ! Turbulent surface fluxes and fluxes averaged over depth of the OSBL
418     DO jj = 2, jpjm1
419        DO ji = 2, jpim1
420           zthermal = rab_n(ji,jj,1,jp_tem)
421           zbeta    = rab_n(ji,jj,1,jp_sal)
422           ! Upwards surface Temperature flux for non-local term
423           zwth0(ji,jj) = - qns(ji,jj) * r1_rau0_rcp * tmask(ji,jj,1)
424           ! Upwards surface salinity flux for non-local term
425           zws0(ji,jj) = - ( ( emp(ji,jj)-rnf(ji,jj) ) * tsn(ji,jj,1,jp_sal)  + sfx(ji,jj) ) * r1_rau0 * tmask(ji,jj,1)
426           ! Non radiative upwards surface buoyancy flux
427           zwb0(ji,jj) = grav * zthermal * zwth0(ji,jj) -  grav * zbeta * zws0(ji,jj)
428           ! turbulent heat flux averaged over depth of OSBL
429           zwthav(ji,jj) = 0.5 * zwth0(ji,jj) - ( 0.5*( zrad0(ji,jj) + zradh(ji,jj) ) - zradav(ji,jj) )
430           ! turbulent salinity flux averaged over depth of the OBSL
431           zwsav(ji,jj) = 0.5 * zws0(ji,jj)
432           ! turbulent buoyancy flux averaged over the depth of the OBSBL
433           zwbav(ji,jj) = grav  * zthermal * zwthav(ji,jj) - grav  * zbeta * zwsav(ji,jj)
434           ! Surface upward velocity fluxes
435           zuw0(ji,jj) = - 0.5 * (utau(ji-1,jj) + utau(ji,jj)) * r1_rau0 * tmask(ji,jj,1)
436           zvw0(ji,jj) = - 0.5 * (vtau(ji,jj-1) + vtau(ji,jj)) * r1_rau0 * tmask(ji,jj,1)
437           ! Friction velocity (zustar), at T-point : LMD94 eq. 2
438           zustar(ji,jj) = MAX( SQRT( SQRT( zuw0(ji,jj) * zuw0(ji,jj) + zvw0(ji,jj) * zvw0(ji,jj) ) ), 1.0e-8 )
439           zcos_wind(ji,jj) = -zuw0(ji,jj) / ( zustar(ji,jj) * zustar(ji,jj) )
440           zsin_wind(ji,jj) = -zvw0(ji,jj) / ( zustar(ji,jj) * zustar(ji,jj) )
441        END DO
442     END DO
443     ! Calculate Stokes drift in direction of wind (zustke) and Stokes penetration depth (dstokes)
444     SELECT CASE (nn_osm_wave)
445     ! Assume constant La#=0.3
446     CASE(0)
447        DO jj = 2, jpjm1
448           DO ji = 2, jpim1
449              zus_x = zcos_wind(ji,jj) * zustar(ji,jj) / 0.3**2
450              zus_y = zsin_wind(ji,jj) * zustar(ji,jj) / 0.3**2
451              ! Linearly
452              zustke(ji,jj) = MAX ( SQRT( zus_x*zus_x + zus_y*zus_y), 1.0e-8 )
453              dstokes(ji,jj) = rn_osm_dstokes
454           END DO
455        END DO
456     ! Assume Pierson-Moskovitz wind-wave spectrum
457     CASE(1)
458        DO jj = 2, jpjm1
459           DO ji = 2, jpim1
460              ! Use wind speed wndm included in sbc_oce module
461              zustke(ji,jj) =  MAX ( 0.016 * wndm(ji,jj), 1.0e-8 )
462              dstokes(ji,jj) = MAX ( 0.12 * wndm(ji,jj)**2 / grav, 5.e-1)
463           END DO
464        END DO
465     ! Use ECMWF wave fields as output from SBCWAVE
466     CASE(2)
467        zfac =  2.0_wp * rpi / 16.0_wp
468
469        DO jj = 2, jpjm1
470           DO ji = 2, jpim1
471              IF (hsw(ji,jj) > 1.e-4) THEN
472                 ! Use  wave fields
473                 zabsstke = SQRT(ut0sd(ji,jj)**2 + vt0sd(ji,jj)**2)
474                 zustke(ji,jj) = MAX ( ( zcos_wind(ji,jj) * ut0sd(ji,jj) + zsin_wind(ji,jj)  * vt0sd(ji,jj) ), 1.0e-8)
475                 dstokes(ji,jj) = MAX (zfac * hsw(ji,jj)*hsw(ji,jj) / ( MAX(zabsstke * wmp(ji,jj), 1.0e-7 ) ), 5.0e-1)
476              ELSE
477                 ! Assume masking issue (e.g. ice in ECMWF reanalysis but not in model run)
478                 ! .. so default to Pierson-Moskowitz
479                 zustke(ji,jj) = MAX ( 0.016 * wndm(ji,jj), 1.0e-8 )
480                 dstokes(ji,jj) = MAX ( 0.12 * wndm(ji,jj)**2 / grav, 5.e-1)
481              END IF
482            END DO
483        END DO
484     END SELECT
485
486     IF (ln_zdfosm_ice_shelter) THEN
487        ! Reduce both Stokes drift and its depth scale by ocean fraction to represent sheltering by ice
488        DO jj = 2, jpjm1
489           DO ji = 2, jpim1
490              zustke(ji,jj) = zustke(ji,jj) * (1.0_wp - fr_i(ji,jj))
491              dstokes(ji,jj) = dstokes(ji,jj) * (1.0_wp - fr_i(ji,jj))
492           END DO
493        END DO
494     END IF
495
496     SELECT CASE (nn_osm_SD_reduce)
497     ! Reduce surface Stokes drift by a constant factor or following Breivik (2016) + van  Roekel (2012) or Grant (2020).
498     CASE(0)
499        ! The Langmur number from the ECMWF model (or from PM)  appears to give La<0.3 for wind-driven seas.
500        !    The coefficient rn_zdfosm_adjust_sd = 0.8 gives La=0.3  in this situation.
501        ! It could represent the effects of the spread of wave directions
502        ! around the mean wind. The effect of this adjustment needs to be tested.
503        IF(nn_osm_wave > 0) THEN
504           zustke(2:jpim1,2:jpjm1) = rn_zdfosm_adjust_sd * zustke(2:jpim1,2:jpjm1)
505        END IF
506     CASE(1)
507        ! van  Roekel (2012): consider average SD over top 10% of boundary layer
508        ! assumes approximate depth profile of SD from Breivik (2016)
509        zsqrtpi = SQRT(rpi)
510        z_two_thirds = 2.0_wp / 3.0_wp
511
512        DO jj = 2, jpjm1
513           DO ji = 2, jpim1
514              zthickness = rn_osm_hblfrac*hbl(ji,jj)
515              z2k_times_thickness =  zthickness * 2.0_wp / MAX( ABS( 5.97_wp * dstokes(ji,jj) ), 0.0000001_wp )
516              zsqrt_depth = SQRT(z2k_times_thickness)
517              zexp_depth  = EXP(-z2k_times_thickness)
518              zustke(ji,jj) = zustke(ji,jj) * (1.0_wp - zexp_depth  &
519                   &              - z_two_thirds * ( zsqrtpi*zsqrt_depth*z2k_times_thickness * ERFC(zsqrt_depth) &
520                   &              + 1.0_wp - (1.0_wp + z2k_times_thickness)*zexp_depth ) ) / z2k_times_thickness
521
522           END DO
523        END DO
524     CASE(2)
525        ! Grant (2020): Match to exponential with same SD and d/dz(Sd) at depth 10% of boundary layer
526        ! assumes approximate depth profile of SD from Breivik (2016)
527        zsqrtpi = SQRT(rpi)
528
529        DO jj = 2, jpjm1
530           DO ji = 2, jpim1
531              zthickness = rn_osm_hblfrac*hbl(ji,jj)
532              z2k_times_thickness =  zthickness * 2.0_wp / MAX( ABS( 5.97_wp * dstokes(ji,jj) ), 0.0000001_wp )
533
534              IF(z2k_times_thickness < 50._wp) THEN
535                 zsqrt_depth = SQRT(z2k_times_thickness)
536                 zexperfc = zsqrtpi * zsqrt_depth * ERFC(zsqrt_depth) * EXP(z2k_times_thickness)
537              ELSE
538                 ! asymptotic expansion of sqrt(pi)*zsqrt_depth*EXP(z2k_times_thickness)*ERFC(zsqrt_depth) for large z2k_times_thickness
539                 ! See Abramowitz and Stegun, Eq. 7.1.23
540                 ! zexperfc = 1._wp - (1/2)/(z2k_times_thickness)  + (3/4)/(z2k_times_thickness**2) - (15/8)/(z2k_times_thickness**3)
541                 zexperfc = ((- 1.875_wp/z2k_times_thickness + 0.75_wp)/z2k_times_thickness - 0.5_wp)/z2k_times_thickness + 1.0_wp
542              END IF
543              zf = z2k_times_thickness*(1.0_wp/zexperfc - 1.0_wp)
544              dstokes(ji,jj) = 5.97 * zf * dstokes(ji,jj)
545              zustke(ji,jj) = zustke(ji,jj) * EXP(z2k_times_thickness * ( 1.0_wp / (2. * zf) - 1.0_wp )) * ( 1.0_wp - zexperfc)
546           END DO
547        END DO
548     END SELECT
549
550     ! Langmuir velocity scale (zwstrl), La # (zla)
551     ! mixed scale (zvstr), convective velocity scale (zwstrc)
552     DO jj = 2, jpjm1
553        DO ji = 2, jpim1
554           ! Langmuir velocity scale (zwstrl), at T-point
555           zwstrl(ji,jj) = ( zustar(ji,jj) * zustar(ji,jj) * zustke(ji,jj) )**pthird
556           zla(ji,jj) = MAX(MIN(SQRT ( zustar(ji,jj) / ( zwstrl(ji,jj) + epsln ) )**3, 4.0), 0.2)
557           IF(zla(ji,jj) > 0.45) dstokes(ji,jj) = MIN(dstokes(ji,jj), 0.5_wp*hbl(ji,jj))
558           ! Velocity scale that tends to zustar for large Langmuir numbers
559           zvstr(ji,jj) = ( zwstrl(ji,jj)**3  + &
560                & ( 1.0 - EXP( -0.5 * zla(ji,jj)**2 ) ) * zustar(ji,jj) * zustar(ji,jj) * zustar(ji,jj) )**pthird
561
562           ! limit maximum value of Langmuir number as approximate treatment for shear turbulence.
563           ! Note zustke and zwstrl are not amended.
564           !
565           ! get convective velocity (zwstrc), stabilty scale (zhol) and logical conection flag lconv
566           IF ( zwbav(ji,jj) > 0.0) THEN
567              zwstrc(ji,jj) = ( 2.0 * zwbav(ji,jj) * 0.9 * hbl(ji,jj) )**pthird
568              zhol(ji,jj) = -0.9 * hbl(ji,jj) * 2.0 * zwbav(ji,jj) / (zvstr(ji,jj)**3 + epsln )
569            ELSE
570              zhol(ji,jj) = -hbl(ji,jj) *  2.0 * zwbav(ji,jj)/ (zvstr(ji,jj)**3  + epsln )
571           ENDIF
572        END DO
573     END DO
574
575     !>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
576     ! Mixed-layer model - calculate averages over the boundary layer, and the change in the boundary layer depth
577     !<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
578     ! BL must be always 4 levels deep.
579     ! For calculation of lateral buoyancy gradients for FK in
580     ! zdf_osm_zmld_horizontal_gradients need halo values for ibld, so must
581     ! previously exist for hbl also.
582
583     ! agn 23/6/20: not clear all this is needed, as hbl checked after it is re-calculated anyway
584     ! ##########################################################################
585      hbl(:,:) = MAX(hbl(:,:), gdepw_n(:,:,4) )
586      ibld(:,:) = 4
587      DO jk = 5, jpkm1
588         DO jj = 1, jpj
589            DO ji = 1, jpi
590               IF ( hbl(ji,jj) >= gdepw_n(ji,jj,jk) ) THEN
591                  ibld(ji,jj) = MIN(mbkt(ji,jj), jk)
592               ENDIF
593            END DO
594         END DO
595      END DO
596     ! ##########################################################################
597
598      DO jj = 2, jpjm1
599         DO ji = 2, jpim1
600            zhbl(ji,jj) = gdepw_n(ji,jj,ibld(ji,jj))
601            imld(ji,jj) = MAX(3,ibld(ji,jj) - MAX( INT( dh(ji,jj) / e3t_n(ji, jj, ibld(ji,jj) )) , 1 ))
602            zhml(ji,jj) = gdepw_n(ji,jj,imld(ji,jj))
603            zdh(ji,jj) = zhbl(ji,jj) - zhml(ji,jj)
604         END DO
605      END DO
606      ! Averages over well-mixed and boundary layer
607      jp_ext(:,:) = 2
608      CALL zdf_osm_vertical_average(ibld, jp_ext, zt_bl, zs_bl, zb_bl, zu_bl, zv_bl, zdt_bl, zds_bl, zdb_bl, zdu_bl, zdv_bl)
609!      jp_ext(:,:) = ibld(:,:) - imld(:,:) + 1
610      CALL zdf_osm_vertical_average(ibld, ibld-imld+1, zt_ml, zs_ml, zb_ml, zu_ml, zv_ml, zdt_ml, zds_ml, zdb_ml, zdu_ml, zdv_ml)
611! Velocity components in frame aligned with surface stress.
612      CALL zdf_osm_velocity_rotation( zcos_wind, zsin_wind, zu_ml, zv_ml, zdu_ml, zdv_ml )
613      CALL zdf_osm_velocity_rotation( zcos_wind, zsin_wind, zu_bl, zv_bl, zdu_bl, zdv_bl )
614! Determine the state of the OSBL, stable/unstable, shear/no shear
615      CALL zdf_osm_osbl_state( lconv, lshear, j_ddh, zwb_ent, zwb_min, zshear, zri_i )
616
617      IF ( ln_osm_mle ) THEN
618! Fox-Kemper Scheme
619         mld_prof = 4
620         DO jk = 5, jpkm1
621           DO jj = 2, jpjm1
622             DO ji = 2, jpim1
623               IF ( hmle(ji,jj) >= gdepw_n(ji,jj,jk) ) mld_prof(ji,jj) = MIN(mbkt(ji,jj), jk)
624             END DO
625           END DO
626         END DO
627         jp_ext_mle(:,:) = 2
628        CALL zdf_osm_vertical_average(mld_prof, jp_ext_mle, zt_mle, zs_mle, zb_mle, zu_mle, zv_mle, zdt_mle, zds_mle, zdb_mle, zdu_mle, zdv_mle)
629
630         DO jj = 2, jpjm1
631           DO ji = 2, jpim1
632              zhmle(ji,jj) = gdepw_n(ji,jj,mld_prof(ji,jj))
633           END DO
634         END DO
635
636!! External gradient
637         CALL zdf_osm_external_gradients( ibld+2, zdtdz_bl_ext, zdsdz_bl_ext, zdbdz_bl_ext )
638         CALL zdf_osm_zmld_horizontal_gradients( zmld, zdtdx, zdtdy, zdsdx, zdsdy, dbdx_mle, dbdy_mle, zdbds_mle )
639         CALL zdf_osm_external_gradients( mld_prof, zdtdz_mle_ext, zdsdz_mle_ext, zdbdz_mle_ext )
640         CALL zdf_osm_osbl_state_fk( lpyc, lflux, lmle, zwb_fk )
641         CALL zdf_osm_mle_parameters( mld_prof, hmle, zhmle, zvel_mle, zdiff_mle )
642      ELSE    ! ln_osm_mle
643! FK not selected, Boundary Layer only.
644         lpyc(:,:) = .TRUE.
645         lflux(:,:) = .FALSE.
646         lmle(:,:) = .FALSE.
647         DO jj = 2, jpjm1
648           DO ji = 2, jpim1
649             IF ( lconv(ji,jj) .AND. zdb_bl(ji,jj) < rn_osm_bl_thresh ) lpyc(ji,jj) = .FALSE.
650           END DO
651         END DO
652      ENDIF   ! ln_osm_mle
653
654! Test if pycnocline well resolved
655      DO jj = 2, jpjm1
656        DO ji = 2,jpim1
657          IF (lconv(ji,jj) ) THEN
658             ztmp = 0.2 * zhbl(ji,jj) / e3w_n(ji,jj,ibld(ji,jj))
659             IF ( ztmp > 6 ) THEN
660      ! pycnocline well resolved
661               jp_ext(ji,jj) = 1
662             ELSE
663      ! pycnocline poorly resolved
664               jp_ext(ji,jj) = 0
665             ENDIF
666          ELSE
667      ! Stable conditions
668            jp_ext(ji,jj) = 0
669          ENDIF
670        END DO
671      END DO
672
673      CALL zdf_osm_vertical_average(ibld, jp_ext, zt_bl, zs_bl, zb_bl, zu_bl, zv_bl, zdt_bl, zds_bl, zdb_bl, zdu_bl, zdv_bl )
674!      jp_ext = ibld-imld+1
675      CALL zdf_osm_vertical_average(imld-1, ibld-imld+1, zt_ml, zs_ml, zb_ml, zu_ml, zv_ml, zdt_ml, zds_ml, zdb_ml, zdu_ml, zdv_ml)
676! Rate of change of hbl
677      CALL zdf_osm_calculate_dhdt( zdhdt, zddhdt )
678      DO jj = 2, jpjm1
679        DO ji = 2, jpim1
680          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
681               ! adjustment to represent limiting by ocean bottom
682          IF ( zhbl_t(ji,jj) >= gdepw_n(ji, jj, mbkt(ji,jj) + 1 ) ) THEN
683             zhbl_t(ji,jj) = MIN(zhbl_t(ji,jj), gdepw_n(ji,jj, mbkt(ji,jj) + 1) - depth_tol)! ht_n(:,:))
684             lpyc(ji,jj) = .FALSE.
685          ENDIF
686         END DO
687       END DO
688
689      imld(:,:) = ibld(:,:)           ! use imld to hold previous blayer index
690      ibld(:,:) = 4
691
692      DO jk = 4, jpkm1
693         DO jj = 2, jpjm1
694            DO ji = 2, jpim1
695               IF ( zhbl_t(ji,jj) >= gdepw_n(ji,jj,jk) ) THEN
696                  ibld(ji,jj) = jk
697               ENDIF
698            END DO
699         END DO
700      END DO
701
702!
703! Step through model levels taking account of buoyancy change to determine the effect on dhdt
704!
705      CALL zdf_osm_timestep_hbl( zdhdt )
706! is external level in bounds?
707
708      CALL zdf_osm_vertical_average( ibld, jp_ext, zt_bl, zs_bl, zb_bl, zu_bl, zv_bl, zdt_bl, zds_bl, zdb_bl, zdu_bl, zdv_bl )
709!
710!
711! Check to see if lpyc needs to be changed
712
713      CALL zdf_osm_pycnocline_thickness( dh, zdh )
714
715      DO jj = 2, jpjm1
716        DO ji = 2, jpim1
717          IF ( zdb_bl(ji,jj) < rn_osm_bl_thresh .or. ibld(ji,jj) + jp_ext(ji,jj) >= mbkt(ji,jj) .or. ibld(ji,jj)-imld(ji,jj) == 1 ) lpyc(ji,jj) = .FALSE. 
718        END DO
719      END DO       
720
721      dstokes(:,:) = MIN ( dstokes(:,:), hbl(:,:)/3. )  !  Limit delta for shallow boundary layers for calculating flux-gradient terms.
722!
723    ! Average over the depth of the mixed layer in the convective boundary layer
724!      jp_ext = ibld - imld +1
725      CALL zdf_osm_vertical_average( imld-1, ibld-imld+1, zt_ml, zs_ml, zb_ml, zu_ml, zv_ml, zdt_ml, zds_ml, zdb_ml, zdu_ml, zdv_ml )
726    ! rotate mean currents and changes onto wind align co-ordinates
727    !
728     CALL zdf_osm_velocity_rotation( zcos_wind, zsin_wind, zu_ml, zv_ml, zdu_ml, zdv_ml )
729     CALL zdf_osm_velocity_rotation( zcos_wind, zsin_wind, zu_bl, zv_bl, zdu_bl, zdv_bl )
730      !>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
731      !  Pycnocline gradients for scalars and velocity
732      !<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
733
734      CALL zdf_osm_external_gradients( ibld+2, zdtdz_bl_ext, zdsdz_bl_ext, zdbdz_bl_ext )
735      CALL zdf_osm_pycnocline_scalar_profiles( zdtdz_pyc, zdsdz_pyc, zdbdz_pyc, zalpha_pyc )
736      CALL zdf_osm_pycnocline_shear_profiles( zdudz_pyc, zdvdz_pyc )
737       !>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
738       ! Eddy viscosity/diffusivity and non-gradient terms in the flux-gradient relationship
739       !<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
740       CALL zdf_osm_diffusivity_viscosity( zdiffut, zviscos )
741
742       !
743       ! calculate non-gradient components of the flux-gradient relationships
744       !
745! Stokes term in scalar flux, flux-gradient relationship
746       WHERE ( lconv )
747          zsc_wth_1 = zwstrl**3 * zwth0 / ( zvstr**3 + 0.5 * zwstrc**3 + epsln)
748          !
749          zsc_ws_1 = zwstrl**3 * zws0 / ( zvstr**3 + 0.5 * zwstrc**3 + epsln )
750       ELSEWHERE
751          zsc_wth_1 = 2.0 * zwthav
752          !
753          zsc_ws_1 = 2.0 * zwsav
754       ENDWHERE
755
756
757       DO jj = 2, jpjm1
758          DO ji = 2, jpim1
759            IF ( lconv(ji,jj) ) THEN
760              DO jk = 2, imld(ji,jj)
761                 zznd_d = gdepw_n(ji,jj,jk) / dstokes(ji,jj)
762                 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)
763                 !
764                 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)
765              END DO ! end jk loop
766            ELSE     ! else for if (lconv)
767 ! Stable conditions
768               DO jk = 2, ibld(ji,jj)
769                  zznd_d=gdepw_n(ji,jj,jk) / dstokes(ji,jj)
770                  ghamt(ji,jj,jk) = ghamt(ji,jj,jk) + 1.5 * EXP ( -0.9 * zznd_d ) &
771                       &          *                 ( 1.0 - EXP ( -4.0 * zznd_d ) ) * zsc_wth_1(ji,jj)
772                  !
773                  ghams(ji,jj,jk) = ghams(ji,jj,jk) + 1.5 * EXP ( -0.9 * zznd_d ) &
774                       &          *                 ( 1.0 - EXP ( -4.0 * zznd_d ) ) *  zsc_ws_1(ji,jj)
775               END DO
776            ENDIF               ! endif for check on lconv
777
778          END DO  ! end of ji loop
779       END DO     ! end of jj loop
780
781! 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)
782       WHERE ( lconv )
783          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 )
784          zsc_uw_2 = ( zwstrl**3 + 0.5 * zwstrc**3 )**pthird * zustke / MIN( zla**(8.0/3.0) + epsln, 0.12 )
785          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 )
786       ELSEWHERE
787          zsc_uw_1 = zustar**2
788          zsc_vw_1 = ff_t * zhbl * zustke**3 * MIN( zla**(8.0/3.0), 0.12 ) / (zvstr**2 + epsln)
789       ENDWHERE
790       IF(ln_dia_osm) THEN
791          IF ( iom_use("ghamu_00") ) CALL iom_put( "ghamu_00", wmask*ghamu )
792          IF ( iom_use("ghamv_00") ) CALL iom_put( "ghamv_00", wmask*ghamv )
793       END IF
794       DO jj = 2, jpjm1
795          DO ji = 2, jpim1
796             IF ( lconv(ji,jj) ) THEN
797                DO jk = 2, imld(ji,jj)
798                   zznd_d = gdepw_n(ji,jj,jk) / dstokes(ji,jj)
799                   ghamu(ji,jj,jk) = ghamu(ji,jj,jk) +      ( -0.05 * EXP ( -0.4 * zznd_d )   * zsc_uw_1(ji,jj)   &
800                        &          +                        0.00125 * EXP (      - zznd_d )   * zsc_uw_2(ji,jj) ) &
801                        &          *                          ( 1.0 - EXP ( -2.0 * zznd_d ) )
802!
803                   ghamv(ji,jj,jk) = ghamv(ji,jj,jk) - 0.65 *  0.15 * EXP (      - zznd_d )                       &
804                        &          *                          ( 1.0 - EXP ( -2.0 * zznd_d ) ) * zsc_vw_1(ji,jj)
805                END DO   ! end jk loop
806             ELSE
807! Stable conditions
808                DO jk = 2, ibld(ji,jj) ! corrected to ibld
809                   zznd_d = gdepw_n(ji,jj,jk) / dstokes(ji,jj)
810                   ghamu(ji,jj,jk) = ghamu(ji,jj,jk) - 0.75 *   1.3 * EXP ( -0.5 * zznd_d )                       &
811                        &                                   * ( 1.0 - EXP ( -4.0 * zznd_d ) ) * zsc_uw_1(ji,jj)
812                   ghamv(ji,jj,jk) = ghamv(ji,jj,jk) + 0._wp
813                END DO   ! end jk loop
814             ENDIF
815          END DO  ! ji loop
816       END DO     ! jj loo
817
818! Buoyancy term in flux-gradient relationship [note : includes ROI ratio (X0.3) and pressure (X0.5)]
819
820       WHERE ( lconv )
821          zsc_wth_1 = zwbav * zwth0 * ( 1.0 + EXP ( 0.2 * zhol ) ) / ( zvstr**3 + 0.5 * zwstrc**3 + epsln )
822          zsc_ws_1  = zwbav * zws0  * ( 1.0 + EXP ( 0.2 * zhol ) ) / ( zvstr**3 + 0.5 * zwstrc**3 + epsln )
823       ELSEWHERE
824          zsc_wth_1 = 0._wp
825          zsc_ws_1 = 0._wp
826       ENDWHERE
827
828       DO jj = 2, jpjm1
829          DO ji = 2, jpim1
830             IF (lconv(ji,jj) ) THEN
831                DO jk = 2, imld(ji,jj)
832                   zznd_ml = gdepw_n(ji,jj,jk) / zhml(ji,jj)
833                   ! calculate turbulent length scale
834                   zl_c = 0.9 * ( 1.0 - EXP ( - 7.0 * ( zznd_ml - zznd_ml**3 / 3.0 ) ) )                                           &
835                        &     * ( 1.0 - EXP ( -15.0 * (     1.1 - zznd_ml          ) ) )
836                   zl_l = 2.0 * ( 1.0 - EXP ( - 2.0 * ( zznd_ml - zznd_ml**3 / 3.0 ) ) )                                           &
837                        &     * ( 1.0 - EXP ( - 5.0 * (     1.0 - zznd_ml          ) ) ) * ( 1.0 + dstokes(ji,jj) / zhml (ji,jj) )
838                   zl_eps = zl_l + ( zl_c - zl_l ) / ( 1.0 + EXP ( -3.0 * LOG10 ( - zhol(ji,jj) ) ) ) ** (3.0 / 2.0)
839                   ! non-gradient buoyancy terms
840                   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 )
841                   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 )
842                END DO
843               
844                IF ( lpyc(ji,jj) ) THEN
845                  ztau_sc_u(ji,jj) = zhml(ji,jj) / ( zvstr(ji,jj)**3 + zwstrc(ji,jj)**3 )**pthird
846                  ztau_sc_u(ji,jj) = ztau_sc_u(ji,jj) * ( 1.4 -0.4 / ( 1.0 + EXP( -3.5 * LOG10( -zhol(ji,jj) ) ) )**1.5 )
847                  zwth_ent =  -0.003 * ( 0.15 * zvstr(ji,jj)**3 + zwstrc(ji,jj)**3 )**pthird * ( 1.0 - zdh(ji,jj) /zhbl(ji,jj) ) * zdt_ml(ji,jj)                 
848                  zws_ent =  -0.003 * ( 0.15 * zvstr(ji,jj)**3 + zwstrc(ji,jj)**3 )**pthird * ( 1.0 - zdh(ji,jj) /zhbl(ji,jj) ) * zds_ml(ji,jj)
849! Cubic profile used for buoyancy term
850                  za_cubic = 0.755 * ztau_sc_u(ji,jj)
851                  zb_cubic = 0.25 * ztau_sc_u(ji,jj)
852                  DO jk = 2, ibld(ji,jj)
853                    zznd_pyc = -( gdepw_n(ji,jj,jk) - zhbl(ji,jj) ) / zdh(ji,jj)
854                    ghamt(ji,jj,jk) = ghamt(ji,jj,jk) - 0.045 * ( ( zwth_ent * zdbdz_pyc(ji,jj,jk) ) * ztau_sc_u(ji,jj)**2 ) * MAX( ( 1.75 * zznd_pyc -0.15 * zznd_pyc**2 - 0.2 * zznd_pyc**3 ), 0.0 )
855
856                    ghams(ji,jj,jk) = ghams(ji,jj,jk) - 0.045 * ( ( zws_ent * zdbdz_pyc(ji,jj,jk) ) * ztau_sc_u(ji,jj)**2 ) * MAX( ( 1.75 * zznd_pyc -0.15 * zznd_pyc**2 - 0.2 * zznd_pyc**3 ), 0.0 )
857                  END DO
858!
859                  zbuoy_pyc_sc = zalpha_pyc(ji,jj) * zdb_ml(ji,jj) / zdh(ji,jj) + zdbdz_bl_ext(ji,jj)
860                  zdelta_pyc = ( zvstr(ji,jj)**3 + zwstrc(ji,jj)**3 )**pthird / SQRT( MAX( zbuoy_pyc_sc, ( zvstr(ji,jj)**3 + zwstrc(ji,jj)**3 )**p2third / zdh(ji,jj)**2 ) )
861!
862                  zwt_pyc_sc_1 = 0.325 * ( zalpha_pyc(ji,jj) * zdt_ml(ji,jj) / zdh(ji,jj) + zdtdz_bl_ext(ji,jj) ) * zdelta_pyc**2 / zdh(ji,jj)
863!
864                  zws_pyc_sc_1 = 0.325 * ( zalpha_pyc(ji,jj) * zds_ml(ji,jj) / zdh(ji,jj) + zdsdz_bl_ext(ji,jj) ) * zdelta_pyc**2 / zdh(ji,jj)
865!
866                  zzeta_pyc = 0.15 - 0.175 / ( 1.0 + EXP( -3.5 * LOG10( -zhol(ji,jj) ) ) ) 
867                  DO jk = 2, ibld(ji,jj)
868                    zznd_pyc = -( gdepw_n(ji,jj,jk) - zhbl(ji,jj) ) / zdh(ji,jj)
869                    ghamt(ji,jj,jk) = ghamt(ji,jj,jk) + 0.05 * zwt_pyc_sc_1 * EXP( -0.25 * ( zznd_pyc / zzeta_pyc )**2 ) * zdh(ji,jj) / ( zvstr(ji,jj)**3 + zwstrc(ji,jj)**3 )**pthird
870!
871                    ghams(ji,jj,jk) = ghams(ji,jj,jk) + 0.05 * zws_pyc_sc_1 * EXP( -0.25 * ( zznd_pyc / zzeta_pyc )**2 ) * zdh(ji,jj) / ( zvstr(ji,jj)**3 + zwstrc(ji,jj)**3 )**pthird
872                  END DO
873               ENDIF ! End of pycnocline                 
874             ELSE ! lconv test - stable conditions
875                DO jk = 2, ibld(ji,jj)
876                   ghamt(ji,jj,jk) = ghamt(ji,jj,jk) + zsc_wth_1(ji,jj)
877                   ghams(ji,jj,jk) = ghams(ji,jj,jk) +  zsc_ws_1(ji,jj)
878                END DO
879             ENDIF
880          END DO   ! ji loop
881       END DO      ! jj loop
882
883       WHERE ( lconv )
884          zsc_uw_1 = -zwb0 * zustar**2 * zhml / ( zvstr**3 + 0.5 * zwstrc**3 + epsln )
885          zsc_uw_2 =  zwb0 * zustke    * zhml / ( zvstr**3 + 0.5 * zwstrc**3 + epsln )**(2.0/3.0)
886          zsc_vw_1 = 0._wp
887       ELSEWHERE
888         zsc_uw_1 = 0._wp
889         zsc_vw_1 = 0._wp
890       ENDWHERE
891
892       DO jj = 2, jpjm1
893          DO ji = 2, jpim1
894             IF ( lconv(ji,jj) ) THEN
895                DO jk = 2 , imld(ji,jj)
896                   zznd_d = gdepw_n(ji,jj,jk) / dstokes(ji,jj)
897                   ghamu(ji,jj,jk) = ghamu(ji,jj,jk) + 0.3 * 0.5 * ( zsc_uw_1(ji,jj) +   0.125 * EXP( -0.5 * zznd_d )     &
898                        &                                                            * (   1.0 - EXP( -0.5 * zznd_d ) )   &
899                        &                                          * zsc_uw_2(ji,jj)                                    )
900                   ghamv(ji,jj,jk) = ghamv(ji,jj,jk) + zsc_vw_1(ji,jj)
901                END DO  ! jk loop
902             ELSE
903             ! stable conditions
904                DO jk = 2, ibld(ji,jj)
905                   ghamu(ji,jj,jk) = ghamu(ji,jj,jk) + zsc_uw_1(ji,jj)
906                   ghamv(ji,jj,jk) = ghamv(ji,jj,jk) + zsc_vw_1(ji,jj)
907                END DO
908             ENDIF
909          END DO        ! ji loop
910       END DO           ! jj loop
911
912       DO jj = 2, jpjm1
913         DO ji = 2, jpim1
914           IF ( lpyc(ji,jj) ) THEN
915             IF ( j_ddh(ji,jj) == 0 ) THEN
916! Place holding code. Parametrization needs checking for these conditions.
917               zomega = ( 0.15 * zwstrl(ji,jj)**3 + zwstrc(ji,jj)**3 + 4.75 * ( zshear(ji,jj)* zhbl(ji,jj) ))**pthird
918               zuw_bse = -0.0035 * zomega * ( 1.0 - zdh(ji,jj) / zhbl(ji,jj) ) * zdu_ml(ji,jj)
919               zvw_bse = -0.0075 * zomega * ( 1.0 - zdh(ji,jj) / zhbl(ji,jj) ) * zdv_ml(ji,jj)
920             ELSE
921               zomega = ( 0.15 * zwstrl(ji,jj)**3 + zwstrc(ji,jj)**3 + 4.75 * ( zshear(ji,jj)* zhbl(ji,jj) ))**pthird
922               zuw_bse = -0.0035 * zomega * ( 1.0 - zdh(ji,jj) / zhbl(ji,jj) ) * zdu_ml(ji,jj)
923               zvw_bse = -0.0075 * zomega * ( 1.0 - zdh(ji,jj) / zhbl(ji,jj) ) * zdv_ml(ji,jj)
924             ENDIF
925             zd_cubic = zdh(ji,jj) / zhbl(ji,jj) * zuw0(ji,jj) - ( 2.0 + zdh(ji,jj) /zhml(ji,jj) ) * zuw_bse
926             zc_cubic = zuw_bse - zd_cubic
927! need ztau_sc_u to be available. Change to array.
928             DO jk = imld(ji,jj), ibld(ji,jj)
929                zznd_pyc = - ( gdepw_n(ji,jj,jk) - zhbl(ji,jj) ) / zdh(ji,jj)
930                ghamu(ji,jj,jk) = ghamu(ji,jj,jk) - 0.045 * ( ztau_sc_u(ji,jj)**2 ) * zuw_bse * &
931                     & ( zc_cubic * zznd_pyc**2 + zd_cubic * zznd_pyc**3 ) * ( 0.75 + 0.25 * zznd_pyc )**2 * zdbdz_pyc(ji,jj,jk)
932             END DO
933             zvw_max = 0.7 * ff_t(ji,jj) * ( zustke(ji,jj) * dstokes(ji,jj) + 0.75 * zustar(ji,jj) * zhml(ji,jj) )
934             zd_cubic = zvw_max * zdh(ji,jj) / zhml(ji,jj) - ( 2.0 + zdh(ji,jj) /zhml(ji,jj) ) * zvw_bse
935             zc_cubic = zvw_bse - zd_cubic
936             DO jk = imld(ji,jj), ibld(ji,jj)
937               zznd_pyc = -( gdepw_n(ji,jj,jk) -zhbl(ji,jj) ) / zdh(ji,jj)
938               ghamv(ji,jj,jk) = ghamv(ji,jj,jk) - 0.045 * ( ztau_sc_u(ji,jj)**2 ) * zvw_bse *  &
939                    & ( zc_cubic * zznd_pyc**2 + zd_cubic * zznd_pyc**3 ) * ( 0.75 + 0.25 * zznd_pyc )**2 * zdbdz_pyc(ji,jj,jk)
940             END DO
941           ENDIF  ! lpyc
942         END DO ! ji loop
943       END DO  ! jj loop
944
945       IF(ln_dia_osm) THEN
946          IF ( iom_use("ghamu_0") ) CALL iom_put( "ghamu_0", wmask*ghamu )
947          IF ( iom_use("zsc_uw_1_0") ) CALL iom_put( "zsc_uw_1_0", tmask(:,:,1)*zsc_uw_1 )
948       END IF
949! Transport term in flux-gradient relationship [note : includes ROI ratio (X0.3) ]
950
951       DO jj = 1, jpjm1
952          DO ji = 1, jpim1
953         
954            IF ( lconv(ji,jj) ) THEN
955              zsc_wth_1(ji,jj) = zwth0(ji,jj) / ( 1.0 - 0.56 * EXP( zhol(ji,jj) ) )
956              zsc_ws_1(ji,jj) = zws0(ji,jj) / (1.0 - 0.56 *EXP( zhol(ji,jj) ) )
957              IF ( lpyc(ji,jj) ) THEN
958! Pycnocline scales
959                 zsc_wth_pyc(ji,jj) = -0.2 * zwb0(ji,jj) * zdt_bl(ji,jj) / zdb_bl(ji,jj)
960                 zsc_ws_pyc(ji,jj) = -0.2 * zwb0(ji,jj) * zds_bl(ji,jj) / zdb_bl(ji,jj)
961               ENDIF
962            ELSE
963              zsc_wth_1(ji,jj) = 2.0 * zwthav(ji,jj)
964              zsc_ws_1(ji,jj) = zws0(ji,jj)
965            ENDIF
966          END DO
967        END DO
968
969       DO jj = 2, jpjm1
970          DO ji = 2, jpim1
971            IF ( lconv(ji,jj) ) THEN
972               DO jk = 2, imld(ji,jj)
973                  zznd_ml=gdepw_n(ji,jj,jk) / zhml(ji,jj)
974                  ghamt(ji,jj,jk) = ghamt(ji,jj,jk) + 0.3 * zsc_wth_1(ji,jj)                &
975                       &          * ( -2.0 + 2.75 * (       ( 1.0 + 0.6 * zznd_ml**4 )      &
976                       &                               - EXP(     - 6.0 * zznd_ml    ) ) )  &
977                       &          * ( 1.0 - EXP( - 15.0 * (         1.0 - zznd_ml    ) ) )
978                  !
979                  ghams(ji,jj,jk) = ghams(ji,jj,jk) + 0.3 * zsc_ws_1(ji,jj)  &
980                       &          * ( -2.0 + 2.75 * (       ( 1.0 + 0.6 * zznd_ml**4 )      &
981                       &                               - EXP(     - 6.0 * zznd_ml    ) ) )  &
982                       &          * ( 1.0 - EXP ( -15.0 * (         1.0 - zznd_ml    ) ) )
983               END DO
984!
985               IF ( lpyc(ji,jj) ) THEN
986! pycnocline
987                 DO jk = imld(ji,jj), ibld(ji,jj)
988                   zznd_pyc = - ( gdepw_n(ji,jj,jk) - zhbl(ji,jj) ) / zdh(ji,jj)
989                   ghamt(ji,jj,jk) = ghamt(ji,jj,jk) + 4.0 * zsc_wth_pyc(ji,jj) * ( 0.48 - EXP( -1.5 * ( zznd_pyc -0.3)**2 ) ) 
990                   ghams(ji,jj,jk) = ghams(ji,jj,jk) + 4.0 * zsc_ws_pyc(ji,jj) * ( 0.48 - EXP( -1.5 * ( zznd_pyc -0.3)**2 ) ) 
991                 END DO
992              ENDIF
993            ELSE
994               IF( zdhdt(ji,jj) > 0. ) THEN
995                 DO jk = 2, ibld(ji,jj)
996                    zznd_d = gdepw_n(ji,jj,jk) / dstokes(ji,jj)
997                    znd = gdepw_n(ji,jj,jk) / zhbl(ji,jj)
998                    ghamt(ji,jj,jk) = ghamt(ji,jj,jk) + 0.3 * ( -4.06 * EXP( -2.0 * zznd_d ) * (1.0 - EXP( -4.0 * zznd_d ) ) + &
999                 &  7.5 * EXP ( -10.0 * ( 0.95 - znd )**2 ) * ( 1.0 - znd ) ) * zsc_wth_1(ji,jj)
1000                    ghams(ji,jj,jk) = ghams(ji,jj,jk) + 0.3 * ( -4.06 * EXP( -2.0 * zznd_d ) * (1.0 - EXP( -4.0 * zznd_d ) ) + &
1001                  &  7.5 * EXP ( -10.0 * ( 0.95 - znd )**2 ) * ( 1.0 - znd ) ) * zsc_ws_1(ji,jj)
1002                 END DO
1003               ENDIF
1004            ENDIF
1005          ENDDO    ! ji loop
1006       END DO      ! jj loop
1007
1008       WHERE ( lconv )
1009          zsc_uw_1 = zustar**2
1010          zsc_vw_1 = ff_t * zustke * zhml
1011       ELSEWHERE
1012          zsc_uw_1 = zustar**2
1013          zsc_uw_2 = (2.25 - 3.0 * ( 1.0 - EXP( -1.25 * 2.0 ) ) ) * ( 1.0 - EXP( -4.0 * 2.0 ) ) * zsc_uw_1
1014          zsc_vw_1 = ff_t * zustke * zhbl
1015          zsc_vw_2 = -0.11 * SIN( 3.14159 * ( 2.0 + 0.4 ) ) * EXP(-( 1.5 + 2.0 )**2 ) * zsc_vw_1
1016       ENDWHERE
1017
1018       DO jj = 2, jpjm1
1019          DO ji = 2, jpim1
1020             IF ( lconv(ji,jj) ) THEN
1021               DO jk = 2, imld(ji,jj)
1022                  zznd_ml = gdepw_n(ji,jj,jk) / zhml(ji,jj)
1023                  zznd_d = gdepw_n(ji,jj,jk) / dstokes(ji,jj)
1024                  ghamu(ji,jj,jk) = ghamu(ji,jj,jk)&
1025                       & + 0.3 * ( -2.0 + 2.5 * ( 1.0 + 0.1 * zznd_ml**4 ) - EXP ( -8.0 * zznd_ml ) ) * zsc_uw_1(ji,jj)
1026                  !
1027                  ghamv(ji,jj,jk) = ghamv(ji,jj,jk)&
1028                       & + 0.3 * 0.1 * ( EXP( -zznd_d ) + EXP( -5.0 * ( 1.0 - zznd_ml ) ) ) * zsc_vw_1(ji,jj)
1029               END DO
1030             ELSE
1031               DO jk = 2, ibld(ji,jj)
1032                  znd = gdepw_n(ji,jj,jk) / zhbl(ji,jj)
1033                  zznd_d = gdepw_n(ji,jj,jk) / dstokes(ji,jj)
1034                  IF ( zznd_d <= 2.0 ) THEN
1035                     ghamu(ji,jj,jk) = ghamu(ji,jj,jk) + 0.5 * 0.3 &
1036                          &*  ( 2.25 - 3.0  * ( 1.0 - EXP( - 1.25 * zznd_d ) ) * ( 1.0 - EXP( -2.0 * zznd_d ) ) ) * zsc_uw_1(ji,jj)
1037                     !
1038                  ELSE
1039                     ghamu(ji,jj,jk) = ghamu(ji,jj,jk)&
1040                          & + 0.5 * 0.3 * ( 1.0 - EXP( -5.0 * ( 1.0 - znd ) ) ) * zsc_uw_2(ji,jj)
1041                     !
1042                  ENDIF
1043
1044                  ghamv(ji,jj,jk) = ghamv(ji,jj,jk)&
1045                       & + 0.3 * 0.15 * SIN( 3.14159 * ( 0.65 * zznd_d ) ) * EXP( -0.25 * zznd_d**2 ) * zsc_vw_1(ji,jj)
1046                  ghamv(ji,jj,jk) = ghamv(ji,jj,jk)&
1047                       & + 0.3 * 0.15 * EXP( -5.0 * ( 1.0 - znd ) ) * ( 1.0 - EXP( -20.0 * ( 1.0 - znd ) ) ) * zsc_vw_2(ji,jj)
1048               END DO
1049             ENDIF
1050          END DO
1051       END DO
1052
1053       IF(ln_dia_osm) THEN
1054          IF ( iom_use("ghamu_f") ) CALL iom_put( "ghamu_f", wmask*ghamu )
1055          IF ( iom_use("ghamv_f") ) CALL iom_put( "ghamv_f", wmask*ghamv )
1056          IF ( iom_use("zsc_uw_1_f") ) CALL iom_put( "zsc_uw_1_f", tmask(:,:,1)*zsc_uw_1 )
1057          IF ( iom_use("zsc_vw_1_f") ) CALL iom_put( "zsc_vw_1_f", tmask(:,:,1)*zsc_vw_1 )
1058          IF ( iom_use("zsc_uw_2_f") ) CALL iom_put( "zsc_uw_2_f", tmask(:,:,1)*zsc_uw_2 )
1059          IF ( iom_use("zsc_vw_2_f") ) CALL iom_put( "zsc_vw_2_f", tmask(:,:,1)*zsc_vw_2 )
1060       END IF
1061!
1062! Make surface forced velocity non-gradient terms go to zero at the base of the mixed layer.
1063
1064
1065 ! Make surface forced velocity non-gradient terms go to zero at the base of the boundary layer.
1066
1067      DO jj = 2, jpjm1
1068         DO ji = 2, jpim1
1069            IF ( .not. lconv(ji,jj) ) THEN
1070               DO jk = 2, ibld(ji,jj)
1071                  znd = -( gdepw_n(ji,jj,jk) - zhbl(ji,jj) ) / zhbl(ji,jj) !ALMG to think about
1072                  IF ( znd >= 0.0 ) THEN
1073                     ghamu(ji,jj,jk) = ghamu(ji,jj,jk) * ( 1.0 - EXP( -10.0 * znd**2 ) )
1074                     ghamv(ji,jj,jk) = ghamv(ji,jj,jk) * ( 1.0 - EXP( -10.0 * znd**2 ) )
1075                  ELSE
1076                     ghamu(ji,jj,jk) = 0._wp
1077                     ghamv(ji,jj,jk) = 0._wp
1078                  ENDIF
1079               END DO
1080            ENDIF
1081         END DO
1082      END DO
1083
1084      ! pynocline contributions
1085       DO jj = 2, jpjm1
1086          DO ji = 2, jpim1
1087            IF ( .not. lconv(ji,jj) ) THEN
1088             IF ( ibld(ji,jj) + jp_ext(ji,jj) < mbkt(ji,jj) ) THEN
1089                DO jk= 2, ibld(ji,jj)
1090                   ghamt(ji,jj,jk) = ghamt(ji,jj,jk) + zdiffut(ji,jj,jk) * zdtdz_pyc(ji,jj,jk)
1091                   ghams(ji,jj,jk) = ghams(ji,jj,jk) + zdiffut(ji,jj,jk) * zdsdz_pyc(ji,jj,jk)
1092                   ghamu(ji,jj,jk) = ghamu(ji,jj,jk) + zviscos(ji,jj,jk) * zdudz_pyc(ji,jj,jk)
1093                   ghamv(ji,jj,jk) = ghamv(ji,jj,jk) + zviscos(ji,jj,jk) * zdvdz_pyc(ji,jj,jk)
1094                END DO
1095             END IF
1096            END IF
1097          END DO
1098       END DO
1099      IF(ln_dia_osm) THEN
1100          IF ( iom_use("ghamu_b") ) CALL iom_put( "ghamu_b", wmask*ghamu )
1101          IF ( iom_use("ghamv_b") ) CALL iom_put( "ghamv_b", wmask*ghamv )
1102       END IF
1103
1104       DO jj=2, jpjm1
1105          DO ji = 2, jpim1
1106             ghamt(ji,jj,ibld(ji,jj)+ibld_ext) = 0._wp
1107             ghams(ji,jj,ibld(ji,jj)+ibld_ext) = 0._wp
1108             ghamu(ji,jj,ibld(ji,jj)+ibld_ext) = 0._wp
1109             ghamv(ji,jj,ibld(ji,jj)+ibld_ext) = 0._wp
1110          END DO       ! ji loop
1111       END DO          ! jj loop
1112
1113       IF(ln_dia_osm) THEN
1114          IF ( iom_use("ghamu_1") ) CALL iom_put( "ghamu_1", wmask*ghamu )
1115          IF ( iom_use("ghamv_1") ) CALL iom_put( "ghamv_1", wmask*ghamv )
1116          IF ( iom_use("zdudz_pyc") ) CALL iom_put( "zdudz_pyc", wmask*zdudz_pyc )
1117          IF ( iom_use("zdvdz_pyc") ) CALL iom_put( "zdvdz_pyc", wmask*zdvdz_pyc )
1118          IF ( iom_use("zviscos") ) CALL iom_put( "zviscos", wmask*zviscos )
1119       END IF
1120       !>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1121       ! Need to put in code for contributions that are applied explicitly to
1122       ! the prognostic variables
1123       !  1. Entrainment flux
1124       !
1125       !<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1126
1127
1128
1129       ! rotate non-gradient velocity terms back to model reference frame
1130
1131       DO jj = 2, jpjm1
1132          DO ji = 2, jpim1
1133             DO jk = 2, ibld(ji,jj)
1134                ztemp = ghamu(ji,jj,jk)
1135                ghamu(ji,jj,jk) = ghamu(ji,jj,jk) * zcos_wind(ji,jj) - ghamv(ji,jj,jk) * zsin_wind(ji,jj)
1136                ghamv(ji,jj,jk) = ghamv(ji,jj,jk) * zcos_wind(ji,jj) + ztemp * zsin_wind(ji,jj)
1137             END DO
1138          END DO
1139       END DO
1140
1141       IF(ln_dia_osm) THEN
1142          IF ( iom_use("zdtdz_pyc") ) CALL iom_put( "zdtdz_pyc", wmask*zdtdz_pyc )
1143          IF ( iom_use("zdsdz_pyc") ) CALL iom_put( "zdsdz_pyc", wmask*zdsdz_pyc )
1144          IF ( iom_use("zdbdz_pyc") ) CALL iom_put( "zdbdz_pyc", wmask*zdbdz_pyc )
1145       END IF
1146
1147! KPP-style Ri# mixing
1148       IF( ln_kpprimix) THEN
1149          DO jk = 2, jpkm1           !* Shear production at uw- and vw-points (energy conserving form)
1150             DO jj = 1, jpjm1
1151                DO ji = 1, jpim1   ! vector opt.
1152                   z3du(ji,jj,jk) = 0.5 * (  un(ji,jj,jk-1) -  un(ji  ,jj,jk) )   &
1153                        &                 * (  ub(ji,jj,jk-1) -  ub(ji  ,jj,jk) ) * wumask(ji,jj,jk) &
1154                        &                 / (  e3uw_n(ji,jj,jk) * e3uw_b(ji,jj,jk) )
1155                   z3dv(ji,jj,jk) = 0.5 * (  vn(ji,jj,jk-1) -  vn(ji,jj  ,jk) )   &
1156                        &                 * (  vb(ji,jj,jk-1) -  vb(ji,jj  ,jk) ) * wvmask(ji,jj,jk) &
1157                        &                 / (  e3vw_n(ji,jj,jk) * e3vw_b(ji,jj,jk) )
1158                END DO
1159             END DO
1160          END DO
1161      !
1162         DO jk = 2, jpkm1
1163            DO jj = 2, jpjm1
1164               DO ji = 2, jpim1   ! vector opt.
1165                  !                                          ! shear prod. at w-point weightened by mask
1166                  zesh2  =  ( z3du(ji-1,jj,jk) + z3du(ji,jj,jk) ) / MAX( 1._wp , umask(ji-1,jj,jk) + umask(ji,jj,jk) )   &
1167                     &    + ( z3dv(ji,jj-1,jk) + z3dv(ji,jj,jk) ) / MAX( 1._wp , vmask(ji,jj-1,jk) + vmask(ji,jj,jk) )
1168                  !                                          ! local Richardson number
1169                  zri   = MAX( rn2b(ji,jj,jk), 0._wp ) / MAX(zesh2, epsln)
1170                  zfri =  MIN( zri / rn_riinfty , 1.0_wp )
1171                  zfri  = ( 1.0_wp - zfri * zfri )
1172                  zrimix(ji,jj,jk)  =  zfri * zfri  * zfri * wmask(ji, jj, jk)
1173                END DO
1174             END DO
1175          END DO
1176
1177          DO jj = 2, jpjm1
1178             DO ji = 2, jpim1
1179                DO jk = ibld(ji,jj) + 1, jpkm1
1180                   zdiffut(ji,jj,jk) = zrimix(ji,jj,jk)*rn_difri
1181                   zviscos(ji,jj,jk) = zrimix(ji,jj,jk)*rn_difri
1182                END DO
1183             END DO
1184          END DO
1185
1186       END IF ! ln_kpprimix = .true.
1187
1188! KPP-style set diffusivity large if unstable below BL
1189       IF( ln_convmix) THEN
1190          DO jj = 2, jpjm1
1191             DO ji = 2, jpim1
1192                DO jk = ibld(ji,jj) + 1, jpkm1
1193                  IF(  MIN( rn2(ji,jj,jk), rn2b(ji,jj,jk) ) <= -1.e-12 ) zdiffut(ji,jj,jk) = rn_difconv
1194                END DO
1195             END DO
1196          END DO
1197       END IF ! ln_convmix = .true.
1198
1199
1200
1201       IF ( ln_osm_mle ) THEN  ! set up diffusivity and non-gradient mixing
1202          DO jj = 2 , jpjm1
1203             DO ji = 2, jpim1
1204                 IF ( lflux(ji,jj) ) THEN ! MLE mixing extends below boundary layer
1205                ! Calculate MLE flux contribution from surface fluxes
1206                   DO jk = 1, ibld(ji,jj)
1207                     znd = gdepw_n(ji,jj,jk) / MAX(zhbl(ji,jj),epsln)
1208                     ghamt(ji,jj,jk) = ghamt(ji,jj,jk) - zwth0(ji,jj) * ( 1.0 - znd )
1209                     ghams(ji,jj,jk) = ghams(ji,jj,jk) - zws0(ji,jj) * ( 1.0 - znd )
1210                    END DO
1211                    DO jk = 1, mld_prof(ji,jj)
1212                      znd = gdepw_n(ji,jj,jk) / MAX(zhmle(ji,jj),epsln)
1213                      ghamt(ji,jj,jk) = ghamt(ji,jj,jk) + zwth0(ji,jj) * ( 1.0 - znd )
1214                      ghams(ji,jj,jk) = ghams(ji,jj,jk) + zws0(ji,jj) * ( 1.0 -znd )
1215                    END DO
1216            ! Viscosity for MLEs
1217                    DO jk = 1, mld_prof(ji,jj)
1218                      znd = -gdepw_n(ji,jj,jk) / MAX(zhmle(ji,jj),epsln)
1219                      zdiffut(ji,jj,jk) = zdiffut(ji,jj,jk) + zdiff_mle(ji,jj) * ( 1.0 - ( 2.0 * znd + 1.0 )**2 ) * ( 1.0 + 5.0 / 21.0 * ( 2.0 * znd + 1.0 )** 2 )
1220                    END DO
1221                 ELSE
1222! Surface transports limited to OSBL.                 
1223            ! Viscosity for MLEs
1224                    DO jk = 1, mld_prof(ji,jj)
1225                      znd = -gdepw_n(ji,jj,jk) / MAX(zhmle(ji,jj),epsln)
1226                      zdiffut(ji,jj,jk) = zdiffut(ji,jj,jk) + zdiff_mle(ji,jj) * ( 1.0 - ( 2.0 * znd + 1.0 )**2 ) * ( 1.0 + 5.0 / 21.0 * ( 2.0 * znd + 1.0 )** 2 )
1227                    END DO
1228                 ENDIF
1229            END DO
1230          END DO
1231       ENDIF
1232
1233       IF(ln_dia_osm) THEN
1234          IF ( iom_use("zdtdz_pyc") ) CALL iom_put( "zdtdz_pyc", wmask*zdtdz_pyc )
1235          IF ( iom_use("zdsdz_pyc") ) CALL iom_put( "zdsdz_pyc", wmask*zdsdz_pyc )
1236          IF ( iom_use("zdbdz_pyc") ) CALL iom_put( "zdbdz_pyc", wmask*zdbdz_pyc )
1237       END IF
1238
1239
1240       ! Lateral boundary conditions on zvicos (sign unchanged), needed to caclulate viscosities on u and v grids
1241       !CALL lbc_lnk( zviscos(:,:,:), 'W', 1. )
1242
1243       ! GN 25/8: need to change tmask --> wmask
1244
1245     DO jk = 2, jpkm1
1246         DO jj = 2, jpjm1
1247             DO ji = 2, jpim1
1248                p_avt(ji,jj,jk) = MAX( zdiffut(ji,jj,jk), avtb(jk) ) * tmask(ji,jj,jk)
1249                p_avm(ji,jj,jk) = MAX( zviscos(ji,jj,jk), avmb(jk) ) * tmask(ji,jj,jk)
1250             END DO
1251         END DO
1252     END DO
1253      ! Lateral boundary conditions on ghamu and ghamv, currently on W-grid  (sign unchanged), needed to caclulate gham[uv] on u and v grids
1254     CALL lbc_lnk_multi( 'zdfosm', p_avt, 'W', 1. , p_avm, 'W', 1.,   &
1255      &                  ghamu, 'W', 1. , ghamv, 'W', 1. )
1256       DO jk = 2, jpkm1
1257           DO jj = 2, jpjm1
1258               DO ji = 2, jpim1
1259                  ghamu(ji,jj,jk) = ( ghamu(ji,jj,jk) + ghamu(ji+1,jj,jk) ) &
1260                     &  / MAX( 1., tmask(ji,jj,jk) + tmask (ji + 1,jj,jk) ) * umask(ji,jj,jk)
1261
1262                  ghamv(ji,jj,jk) = ( ghamv(ji,jj,jk) + ghamv(ji,jj+1,jk) ) &
1263                      &  / MAX( 1., tmask(ji,jj,jk) + tmask (ji,jj+1,jk) ) * vmask(ji,jj,jk)
1264
1265                  ghamt(ji,jj,jk) =  ghamt(ji,jj,jk) * tmask(ji,jj,jk)
1266                  ghams(ji,jj,jk) =  ghams(ji,jj,jk) * tmask(ji,jj,jk)
1267               END DO
1268           END DO
1269        END DO
1270        ! Lateral boundary conditions on final outputs for hbl,  on T-grid (sign unchanged)
1271        CALL lbc_lnk_multi( 'zdfosm', hbl, 'T', 1., dh, 'T', 1., hmle, 'T', 1. )
1272        ! Lateral boundary conditions on final outputs for gham[ts],  on W-grid  (sign unchanged)
1273        ! Lateral boundary conditions on final outputs for gham[uv],  on [UV]-grid  (sign unchanged)
1274        CALL lbc_lnk_multi( 'zdfosm', ghamt, 'W', 1. , ghams, 'W', 1.,   &
1275         &                  ghamu, 'U', -1. , ghamv, 'V', -1. )
1276
1277      IF(ln_dia_osm) THEN
1278         SELECT CASE (nn_osm_wave)
1279         ! Stokes drift set by assumimg onstant La#=0.3(=0)  or Pierson-Moskovitz spectrum (=1).
1280         CASE(0:1)
1281            IF ( iom_use("us_x") ) CALL iom_put( "us_x", tmask(:,:,1)*zustke*zcos_wind )   ! x surface Stokes drift
1282            IF ( iom_use("us_y") ) CALL iom_put( "us_y", tmask(:,:,1)*zustke*zsin_wind )  ! y surface Stokes drift
1283            IF ( iom_use("wind_wave_abs_power") ) CALL iom_put( "wind_wave_abs_power", 1000.*rau0*tmask(:,:,1)*zustar**2*zustke )
1284         ! Stokes drift read in from sbcwave  (=2).
1285         CASE(2:3)
1286            IF ( iom_use("us_x") ) CALL iom_put( "us_x", ut0sd*umask(:,:,1) )               ! x surface Stokes drift
1287            IF ( iom_use("us_y") ) CALL iom_put( "us_y", vt0sd*vmask(:,:,1) )               ! y surface Stokes drift
1288            IF ( iom_use("wmp") ) CALL iom_put( "wmp", wmp*tmask(:,:,1) )                   ! wave mean period
1289            IF ( iom_use("hsw") ) CALL iom_put( "hsw", hsw*tmask(:,:,1) )                   ! significant wave height
1290            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
1291            IF ( iom_use("hsw_NP") ) CALL iom_put( "hsw_NP", (0.22/grav)*wndm**2*tmask(:,:,1) )                   ! significant wave height from NP spectrum
1292            IF ( iom_use("wndm") ) CALL iom_put( "wndm", wndm*tmask(:,:,1) )                   ! U_10
1293            IF ( iom_use("wind_wave_abs_power") ) CALL iom_put( "wind_wave_abs_power", 1000.*rau0*tmask(:,:,1)*zustar**2* &
1294                 & SQRT(ut0sd**2 + vt0sd**2 ) )
1295         END SELECT
1296         IF ( iom_use("ghamt") ) CALL iom_put( "ghamt", tmask*ghamt )            ! <Tw_NL>
1297         IF ( iom_use("ghams") ) CALL iom_put( "ghams", tmask*ghams )            ! <Sw_NL>
1298         IF ( iom_use("ghamu") ) CALL iom_put( "ghamu", umask*ghamu )            ! <uw_NL>
1299         IF ( iom_use("ghamv") ) CALL iom_put( "ghamv", vmask*ghamv )            ! <vw_NL>
1300         IF ( iom_use("zwth0") ) CALL iom_put( "zwth0", tmask(:,:,1)*zwth0 )            ! <Tw_0>
1301         IF ( iom_use("zws0") ) CALL iom_put( "zws0", tmask(:,:,1)*zws0 )                ! <Sw_0>
1302         IF ( iom_use("hbl") ) CALL iom_put( "hbl", tmask(:,:,1)*hbl )                  ! boundary-layer depth
1303         IF ( iom_use("ibld") ) CALL iom_put( "ibld", tmask(:,:,1)*ibld )               ! boundary-layer max k
1304         IF ( iom_use("zdt_bl") ) CALL iom_put( "zdt_bl", tmask(:,:,1)*zdt_bl )           ! dt at ml base
1305         IF ( iom_use("zds_bl") ) CALL iom_put( "zds_bl", tmask(:,:,1)*zds_bl )           ! ds at ml base
1306         IF ( iom_use("zdb_bl") ) CALL iom_put( "zdb_bl", tmask(:,:,1)*zdb_bl )           ! db at ml base
1307         IF ( iom_use("zdu_bl") ) CALL iom_put( "zdu_bl", tmask(:,:,1)*zdu_bl )           ! du at ml base
1308         IF ( iom_use("zdv_bl") ) CALL iom_put( "zdv_bl", tmask(:,:,1)*zdv_bl )           ! dv at ml base
1309         IF ( iom_use("dh") ) CALL iom_put( "dh", tmask(:,:,1)*dh )               ! Initial boundary-layer depth
1310         IF ( iom_use("hml") ) CALL iom_put( "hml", tmask(:,:,1)*hml )               ! Initial boundary-layer depth
1311         IF ( iom_use("dstokes") ) CALL iom_put( "dstokes", tmask(:,:,1)*dstokes )      ! Stokes drift penetration depth
1312         IF ( iom_use("zustke") ) CALL iom_put( "zustke", tmask(:,:,1)*zustke )            ! Stokes drift magnitude at T-points
1313         IF ( iom_use("zwstrc") ) CALL iom_put( "zwstrc", tmask(:,:,1)*zwstrc )         ! convective velocity scale
1314         IF ( iom_use("zwstrl") ) CALL iom_put( "zwstrl", tmask(:,:,1)*zwstrl )         ! Langmuir velocity scale
1315         IF ( iom_use("zustar") ) CALL iom_put( "zustar", tmask(:,:,1)*zustar )         ! friction velocity scale
1316         IF ( iom_use("zvstr") ) CALL iom_put( "zvstr", tmask(:,:,1)*zvstr )         ! mixed velocity scale
1317         IF ( iom_use("zla") ) CALL iom_put( "zla", tmask(:,:,1)*zla )         ! langmuir #
1318         IF ( iom_use("wind_power") ) CALL iom_put( "wind_power", 1000.*rau0*tmask(:,:,1)*zustar**3 ) ! BL depth internal to zdf_osm routine
1319         IF ( iom_use("wind_wave_power") ) CALL iom_put( "wind_wave_power", 1000.*rau0*tmask(:,:,1)*zustar**2*zustke )
1320         IF ( iom_use("zhbl") ) CALL iom_put( "zhbl", tmask(:,:,1)*zhbl )               ! BL depth internal to zdf_osm routine
1321         IF ( iom_use("zhml") ) CALL iom_put( "zhml", tmask(:,:,1)*zhml )               ! ML depth internal to zdf_osm routine
1322         IF ( iom_use("imld") ) CALL iom_put( "imld", tmask(:,:,1)*imld )               ! index for ML depth internal to zdf_osm routine
1323         IF ( iom_use("zdh") ) CALL iom_put( "zdh", tmask(:,:,1)*zdh )                  ! pyc thicknessh internal to zdf_osm routine
1324         IF ( iom_use("zhol") ) CALL iom_put( "zhol", tmask(:,:,1)*zhol )               ! ML depth internal to zdf_osm routine
1325         IF ( iom_use("zwthav") ) CALL iom_put( "zwthav", tmask(:,:,1)*zwthav )         ! upward BL-avged turb temp flux
1326         IF ( iom_use("zwth_ent") ) CALL iom_put( "zwth_ent", tmask(:,:,1)*zwth_ent )   ! upward turb temp entrainment flux
1327         IF ( iom_use("zwb_ent") ) CALL iom_put( "zwb_ent", tmask(:,:,1)*zwb_ent )      ! upward turb buoyancy entrainment flux
1328         IF ( iom_use("zws_ent") ) CALL iom_put( "zws_ent", tmask(:,:,1)*zws_ent )      ! upward turb salinity entrainment flux
1329         IF ( iom_use("zt_ml") ) CALL iom_put( "zt_ml", tmask(:,:,1)*zt_ml )            ! average T in ML
1330
1331         IF ( iom_use("hmle") ) CALL iom_put( "hmle", tmask(:,:,1)*hmle )               ! FK layer depth
1332         IF ( iom_use("zmld") ) CALL iom_put( "zmld", tmask(:,:,1)*zmld )               ! FK target layer depth
1333         IF ( iom_use("zwb_fk") ) CALL iom_put( "zwb_fk", tmask(:,:,1)*zwb_fk )         ! FK b flux
1334         IF ( iom_use("zwb_fk_b") ) CALL iom_put( "zwb_fk_b", tmask(:,:,1)*zwb_fk_b )   ! FK b flux averaged over ML
1335         IF ( iom_use("mld_prof") ) CALL iom_put( "mld_prof", tmask(:,:,1)*mld_prof )! FK layer max k
1336         IF ( iom_use("zdtdx") ) CALL iom_put( "zdtdx", umask(:,:,1)*zdtdx )            ! FK dtdx at u-pt
1337         IF ( iom_use("zdtdy") ) CALL iom_put( "zdtdy", vmask(:,:,1)*zdtdy )            ! FK dtdy at v-pt
1338         IF ( iom_use("zdsdx") ) CALL iom_put( "zdsdx", umask(:,:,1)*zdsdx )            ! FK dtdx at u-pt
1339         IF ( iom_use("zdsdy") ) CALL iom_put( "zdsdy", vmask(:,:,1)*zdsdy )            ! FK dsdy at v-pt
1340         IF ( iom_use("dbdx_mle") ) CALL iom_put( "dbdx_mle", umask(:,:,1)*dbdx_mle )            ! FK dbdx at u-pt
1341         IF ( iom_use("dbdy_mle") ) CALL iom_put( "dbdy_mle", vmask(:,:,1)*dbdy_mle )            ! FK dbdy at v-pt
1342         IF ( iom_use("zdiff_mle") ) CALL iom_put( "zdiff_mle", tmask(:,:,1)*zdiff_mle )! FK diff in MLE at t-pt
1343         IF ( iom_use("zvel_mle") ) CALL iom_put( "zvel_mle", tmask(:,:,1)*zdiff_mle )! FK diff in MLE at t-pt
1344
1345      END IF
1346
1347CONTAINS
1348! subroutine code changed, needs syntax checking.
1349  SUBROUTINE zdf_osm_diffusivity_viscosity( zdiffut, zviscos )
1350
1351!!---------------------------------------------------------------------
1352     !!                   ***  ROUTINE zdf_osm_diffusivity_viscosity  ***
1353     !!
1354     !! ** Purpose : Determines the eddy diffusivity and eddy viscosity profiles in the mixed layer and the pycnocline.
1355     !!
1356     !! ** Method  :
1357     !!
1358     !! !!----------------------------------------------------------------------
1359     REAL(wp), DIMENSION(:,:,:) :: zdiffut
1360     REAL(wp), DIMENSION(:,:,:) :: zviscos
1361! local
1362
1363! Scales used to calculate eddy diffusivity and viscosity profiles
1364      REAL(wp), DIMENSION(jpi,jpj) :: zdifml_sc, zvisml_sc
1365      REAL(wp), DIMENSION(jpi,jpj) :: zdifpyc_n_sc, zdifpyc_s_sc, zdifpyc_shr
1366      REAL(wp), DIMENSION(jpi,jpj) :: zvispyc_n_sc, zvispyc_s_sc,zvispyc_shr
1367      REAL(wp), DIMENSION(jpi,jpj) :: zbeta_d_sc, zbeta_v_sc
1368!
1369      REAL(wp) :: zvel_sc_pyc, zvel_sc_ml, zstab_fac
1370     
1371      REAL(wp), PARAMETER :: rn_dif_ml = 0.8, rn_vis_ml = 0.375
1372      REAL(wp), PARAMETER :: rn_dif_pyc = 0.15, rn_vis_pyc = 0.142
1373      REAL(wp), PARAMETER :: rn_vispyc_shr = 0.15
1374     
1375      DO jj = 2, jpjm1
1376          DO ji = 2, jpim1
1377             IF ( lconv(ji,jj) ) THEN
1378             
1379               zvel_sc_pyc = ( 0.15 * zvstr(ji,jj)**3 + zwstrc(ji,jj)**3 + 4.25 * zshear(ji,jj) * zhbl(ji,jj) )**pthird
1380               zvel_sc_ml = ( zvstr(ji,jj)**3 + 0.5 * zwstrc(ji,jj)**3 )**pthird
1381               zstab_fac = ( zhml(ji,jj) / zvel_sc_ml * ( 1.4 - 0.4 / ( 1.0 + EXP(-3.5 * LOG10(-zhol(ji,jj) ) ) )**1.25 ) )**2
1382
1383               zdifml_sc(ji,jj) = rn_dif_ml * zhml(ji,jj) * zvel_sc_ml
1384               zvisml_sc(ji,jj) = rn_vis_ml * zdifml_sc(ji,jj)
1385
1386               IF ( lpyc(ji,jj) ) THEN
1387                 zdifpyc_n_sc(ji,jj) =  rn_dif_pyc * zvel_sc_ml * zdh(ji,jj)
1388
1389                 IF ( lshear(ji,jj) .and. j_ddh(ji,jj) == 1 ) THEN
1390                   zdifpyc_n_sc(ji,jj) = zdifpyc_n_sc(ji,jj) + rn_vispyc_shr * ( zshear(ji,jj) * zhbl(ji,jj) )**pthird * zhbl(ji,jj)
1391                 ENDIF
1392               
1393                 zdifpyc_s_sc(ji,jj) = zwb_ent(ji,jj) + 0.0025 * zvel_sc_pyc * ( zhbl(ji,jj) / zdh(ji,jj) - 1.0 ) * ( zb_ml(ji,jj) - zb_bl(ji,jj) )
1394                 zdifpyc_s_sc(ji,jj) = 0.09 * zdifpyc_s_sc(ji,jj) * zstab_fac
1395                 zdifpyc_s_sc(ji,jj) = MAX( zdifpyc_s_sc(ji,jj), -0.5 * zdifpyc_n_sc(ji,jj) )
1396                 
1397                 zvispyc_n_sc(ji,jj) = 0.09 * zvel_sc_pyc * ( 1.0 - zhbl(ji,jj) / zdh(ji,jj) )**2 * ( 0.005 * ( zu_ml(ji,jj)-zu_bl(ji,jj) )**2 + 0.0075 * ( zv_ml(ji,jj)-zv_bl(ji,jj) )**2 ) / zdh(ji,jj)
1398                 zvispyc_n_sc(ji,jj) = rn_vis_pyc * zvel_sc_ml * zdh(ji,jj) + zvispyc_n_sc(ji,jj) * zstab_fac
1399                 IF ( lshear(ji,jj) .and. j_ddh(ji,jj) == 1 ) THEN
1400                   zvispyc_n_sc(ji,jj) = zvispyc_n_sc(ji,jj) + rn_vispyc_shr * ( zshear(ji,jj) * zhbl(ji,jj ) )**pthird * zhbl(ji,jj)
1401                 ENDIF
1402               
1403                 zvispyc_s_sc(ji,jj) = 0.09 * ( zwb_min(ji,jj) + 0.0025 * zvel_sc_pyc * ( zhbl(ji,jj) / zdh(ji,jj) - 1.0 ) * ( zb_ml(ji,jj) - zb_bl(ji,jj) ) )
1404                 zvispyc_s_sc(ji,jj) = zvispyc_s_sc(ji,jj) * zstab_fac
1405                 zvispyc_s_sc(ji,jj) = MAX( zvispyc_s_sc(ji,jj), -0.5 * zvispyc_s_sc(ji,jj) )
1406
1407                 zbeta_d_sc(ji,jj) = 1.0 - ( ( zdifpyc_n_sc(ji,jj) + 1.4 * zdifpyc_s_sc(ji,jj) ) / ( zdifml_sc(ji,jj) + epsln ) )**p2third
1408                 zbeta_v_sc(ji,jj) = 1.0 -  2.0 * ( zvispyc_n_sc(ji,jj) + zvispyc_s_sc(ji,jj) ) / ( zvisml_sc(ji,jj) + epsln )
1409               ELSE
1410                 zbeta_d_sc(ji,jj) = 1.0
1411                 zbeta_v_sc(ji,jj) = 1.0
1412               ENDIF
1413             ELSE
1414               zdifml_sc(ji,jj) = zvstr(ji,jj) * zhbl(ji,jj) * MAX( EXP ( -( zhol(ji,jj) / 0.6_wp )**2 ), 0.2_wp)
1415               zvisml_sc(ji,jj) = zvstr(ji,jj) * zhbl(ji,jj) * MAX( EXP ( -( zhol(ji,jj) / 0.6_wp )**2 ), 0.2_wp)
1416             END IF
1417          END DO
1418       END DO
1419!
1420       DO jj = 2, jpjm1
1421          DO ji = 2, jpim1
1422             IF ( lconv(ji,jj) ) THEN
1423                DO jk = 2, imld(ji,jj)   ! mixed layer diffusivity
1424                    zznd_ml = gdepw_n(ji,jj,jk) / zhml(ji,jj)
1425                    !
1426                    zdiffut(ji,jj,jk) = zdifml_sc(ji,jj) * zznd_ml * ( 1.0 - zbeta_d_sc(ji,jj) * zznd_ml )**1.5
1427                    !
1428                    zviscos(ji,jj,jk) = zvisml_sc(ji,jj) * zznd_ml * ( 1.0 - zbeta_v_sc(ji,jj) * zznd_ml ) &
1429      &            *                                      ( 1.0 - 0.5 * zznd_ml**2 )
1430                END DO
1431! pycnocline
1432                IF ( lpyc(ji,jj) ) THEN
1433! Diffusivity profile in the pycnocline given by cubic polynomial.
1434                   za_cubic = 0.5
1435                   zb_cubic = -1.75 * zdifpyc_s_sc(ji,jj) / zdifpyc_n_sc(ji,jj)
1436                   zd_cubic = ( zdh(ji,jj) * zdifml_sc(ji,jj) / zhml(ji,jj) * SQRT( 1.0 - zbeta_d_sc(ji,jj) ) * ( 2.5 * zbeta_d_sc(ji,jj) - 1.0 ) &
1437                        & - 0.85 * zdifpyc_s_sc(ji,jj) ) / MAX(zdifpyc_n_sc(ji,jj), 1.e-8)
1438                   zd_cubic = zd_cubic - zb_cubic - 2.0 * ( 1.0 - za_cubic  - zb_cubic )
1439                   zc_cubic = 1.0 - za_cubic - zb_cubic - zd_cubic
1440                   DO jk = imld(ji,jj) , ibld(ji,jj)
1441                     zznd_pyc = -( gdepw_n(ji,jj,jk) - zhbl(ji,jj) ) / MAX(zdh(ji,jj), 1.e-6)
1442                         !
1443                     zdiffut(ji,jj,jk) = zdifpyc_n_sc(ji,jj) * ( za_cubic + zb_cubic * zznd_pyc + zc_cubic * zznd_pyc**2 +   zd_cubic * zznd_pyc**3 )
1444
1445                     zdiffut(ji,jj,jk) = zdiffut(ji,jj,jk) + zdifpyc_s_sc(ji,jj) * ( 1.75 * zznd_pyc - 0.15 * zznd_pyc**2 - 0.2 * zznd_pyc**3 )
1446                   END DO
1447 ! viscosity profiles.
1448                   za_cubic = 0.5
1449                   zb_cubic = -1.75 * zvispyc_s_sc(ji,jj) / zvispyc_n_sc(ji,jj)
1450                   zd_cubic = ( 0.5 * zvisml_sc(ji,jj) * zdh(ji,jj) / zhml(ji,jj) - 0.85 * zvispyc_s_sc(ji,jj)  )  / MAX(zvispyc_n_sc(ji,jj), 1.e-8)
1451                   zd_cubic = zd_cubic - zb_cubic - 2.0 * ( 1.0 - za_cubic - zb_cubic )
1452                   zc_cubic = 1.0 - za_cubic - zb_cubic - zd_cubic
1453                   DO jk = imld(ji,jj) , ibld(ji,jj)
1454                      zznd_pyc = -( gdepw_n(ji,jj,jk) - zhbl(ji,jj) ) / MAX(zdh(ji,jj), 1.e-6)
1455                       zviscos(ji,jj,jk) = zvispyc_n_sc(ji,jj) * ( za_cubic + zb_cubic * zznd_pyc + zc_cubic * zznd_pyc**2 + zd_cubic * zznd_pyc**3 )
1456                       zviscos(ji,jj,jk) = zviscos(ji,jj,jk) + zvispyc_s_sc(ji,jj) * ( 1.75 * zznd_pyc - 0.15 * zznd_pyc**2 -0.2 * zznd_pyc**3 )
1457                   END DO
1458                   IF ( zdhdt(ji,jj) > 0._wp ) THEN
1459                    zdiffut(ji,jj,ibld(ji,jj)+1) = MAX( 0.5 * zdhdt(ji,jj) * e3w_n(ji,jj,ibld(ji,jj)+1), 1.0e-6 )
1460                    zviscos(ji,jj,ibld(ji,jj)+1) = MAX( 0.5 * zdhdt(ji,jj) * e3w_n(ji,jj,ibld(ji,jj)+1), 1.0e-6 )
1461                   ELSE
1462                     zdiffut(ji,jj,ibld(ji,jj)) = 0._wp
1463                     zviscos(ji,jj,ibld(ji,jj)) = 0._wp
1464                   ENDIF
1465                ENDIF
1466             ELSE
1467             ! stable conditions
1468                DO jk = 2, ibld(ji,jj)
1469                   zznd_ml = gdepw_n(ji,jj,jk) / zhbl(ji,jj)
1470                   zdiffut(ji,jj,jk) = 0.75 * zdifml_sc(ji,jj) * zznd_ml * ( 1.0 - zznd_ml )**1.5
1471                   zviscos(ji,jj,jk) = 0.375 * zvisml_sc(ji,jj) * zznd_ml * (1.0 - zznd_ml) * ( 1.0 - zznd_ml**2 )
1472                END DO
1473
1474                IF ( zdhdt(ji,jj) > 0._wp ) THEN
1475                   zdiffut(ji,jj,ibld(ji,jj)) = MAX(zdhdt(ji,jj), 1.0e-6) * e3w_n(ji, jj, ibld(ji,jj))
1476                   zviscos(ji,jj,ibld(ji,jj)) = MAX(zdhdt(ji,jj), 1.0e-6) * e3w_n(ji, jj, ibld(ji,jj))
1477                ENDIF
1478             ENDIF   ! end if ( lconv )
1479             !
1480          END DO  ! end of ji loop
1481       END DO     ! end of jj loop
1482       
1483  END SUBROUTINE zdf_osm_diffusivity_viscosity
1484 
1485  SUBROUTINE zdf_osm_osbl_state( lconv, lshear, j_ddh, zwb_ent, zwb_min, zshear, zri_i )
1486
1487!!---------------------------------------------------------------------
1488     !!                   ***  ROUTINE zdf_osm_osbl_state  ***
1489     !!
1490     !! ** Purpose : Determines the state of the OSBL, stable/unstable, shear/ noshear. Also determines shear production, entrainment buoyancy flux and interfacial Richardson number
1491     !!
1492     !! ** Method  :
1493     !!
1494     !! !!----------------------------------------------------------------------
1495
1496     INTEGER, DIMENSION(jpi,jpj) :: j_ddh  ! j_ddh = 0, active shear layer; j_ddh=1, shear layer not active; j_ddh=2 shear production low.
1497     
1498     LOGICAL, DIMENSION(jpi,jpj) :: lconv, lshear
1499
1500     REAL(wp), DIMENSION(jpi,jpj) :: zwb_ent, zwb_min ! Buoyancy fluxes at base of well-mixed layer.
1501     REAL(wp), DIMENSION(jpi,jpj) :: zshear  ! production of TKE due to shear across the pycnocline
1502     REAL(wp), DIMENSION(jpi,jpj) :: zri_i  ! Interfacial Richardson Number
1503
1504! Local Variables
1505
1506     INTEGER :: jj, ji
1507     
1508     REAL(wp), DIMENSION(jpi,jpj) :: zekman
1509     REAL(wp) :: zri_p, zri_b   ! Richardson numbers
1510     REAL(wp) :: zshear_u, zshear_v, zwb_shr
1511     REAL(wp) :: zwcor, zrf_conv, zrf_shear, zrf_langmuir, zr_stokes
1512
1513     REAL, PARAMETER :: za_shr = 0.4, zb_shr = 6.5, za_wb_s = 0.1
1514     REAL, PARAMETER :: rn_ri_thres_a = 0.5, rn_ri_thresh_b = 0.59
1515     REAL, PARAMETER :: zalpha_c = 0.2, zalpha_lc = 0.04     
1516     REAL, PARAMETER :: zalpha_ls = 0.06, zalpha_s = 0.15
1517     REAL, PARAMETER :: rn_ri_p_thresh = 27.0
1518     REAL, PARAMETER :: zrot=0._wp  ! dummy rotation rate of surface stress.
1519     
1520! Determins stability and set flag lconv
1521     DO jj = 2, jpjm1
1522       DO ji = 2, jpim1
1523         IF ( zhol(ji,jj) < 0._wp ) THEN
1524            lconv(ji,jj) = .TRUE.
1525          ELSE
1526             lconv(ji,jj) = .FALSE.
1527          ENDIF
1528       END DO
1529     END DO       
1530 
1531     zekman(:,:) = EXP( - 4.0 * ABS( ff_t(:,:) ) * zhbl(:,:) / MAX(zustar(:,:), 1.e-8 ) )
1532     
1533     WHERE ( lconv )
1534       zri_i = zdb_ml * zhml**2 / MAX( ( zvstr**3 + 0.5 * zwstrc**3 )**p2third * zdh, 1.e-12 )
1535     END WHERE
1536
1537     zshear(:,:) = 0._wp
1538     j_ddh(:,:) = 1     
1539 
1540     DO jj = 2, jpjm1
1541       DO ji = 2, jpim1
1542         IF ( lconv(ji,jj) ) THEN
1543            IF ( zdb_bl(ji,jj) > 0._wp ) THEN
1544              zri_p = MAX (  SQRT( zdb_bl(ji,jj) * zdh(ji,jj) / MAX( zdu_bl(ji,jj)**2 + zdv_bl(ji,jj)**2, 1.e-8) )  *  ( zhbl(ji,jj) / zdh(ji,jj) ) * ( zvstr(ji,jj) / MAX( zustar(ji,jj), 1.e-6 ) )**2 &
1545                   & / MAX( zekman(ji,jj), 1.e-6 )  , 5._wp )
1546           
1547              zri_b = zdb_ml(ji,jj) * zdh(ji,jj) / MAX( zdu_ml(ji,jj)**2 + zdv_ml(ji,jj)**2, 1.e-8 )
1548                       
1549              zshear(ji,jj) = za_shr * zekman(ji,jj) * ( MAX( zustar(ji,jj)**2 * zdu_ml(ji,jj) / zhbl(ji,jj), 0._wp ) + zb_shr * MAX( -ff_t(ji,jj) * zustke(ji,jj) * dstokes(ji,jj) * zdv_ml(ji,jj) / zhbl(ji,jj), 0._wp ) )
1550!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1551! Test ensures j_ddh=0 is not selected. Change to zri_p<27 when  !
1552! full code available                                          !
1553!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1554              IF ( zri_p < -rn_ri_p_thresh .and. zshear(ji,jj) > 0._wp ) THEN
1555! Growing shear layer
1556                j_ddh(ji,jj) = 0
1557                lshear(ji,jj) = .TRUE.
1558              ELSE
1559                j_ddh(ji,jj) = 1
1560                IF ( zri_b <= 1.5 .and. zshear(ji,jj) > 0._wp ) THEN
1561! shear production large enough to determine layer charcteristics, but can't maintain a shear layer.
1562                  lshear(ji,jj) = .TRUE.
1563                ELSE
1564! Shear production may not be zero, but is small and doesn't determine characteristics of pycnocline.
1565                  zshear(ji,jj) = 0.5 * zshear(ji,jj)
1566                  lshear(ji,jj) = .FALSE.
1567                ENDIF
1568              ENDIF               
1569            ELSE                ! zdb_bl test, note zshear set to zero
1570              j_ddh(ji,jj) = 2
1571              lshear(ji,jj) = .FALSE.
1572            ENDIF
1573          ENDIF
1574       END DO
1575     END DO
1576 
1577! Calculate entrainment buoyancy flux due to surface fluxes.
1578
1579     DO jj = 2, jpjm1
1580       DO ji = 2, jpim1
1581         IF ( lconv(ji,jj) ) THEN
1582           zwcor = ABS(ff_t(ji,jj)) * zhbl(ji,jj) + epsln
1583           zrf_conv = TANH( ( zwstrc(ji,jj) / zwcor )**0.69 )
1584           zrf_shear = TANH( ( zustar(ji,jj) / zwcor )**0.69 )
1585           zrf_langmuir = TANH( ( zwstrl(ji,jj) / zwcor )**0.69 )
1586           IF (nn_osm_SD_reduce > 0 ) THEN
1587           ! Effective Stokes drift already reduced from surface value
1588              zr_stokes = 1.0_wp
1589           ELSE
1590            ! Effective Stokes drift only reduced by factor rn_zdfodm_adjust_sd,
1591             ! requires further reduction where BL is deep
1592              zr_stokes = 1.0 - EXP( -25.0 * dstokes(ji,jj) / hbl(ji,jj) &
1593            &                  * ( 1.0 + 4.0 * dstokes(ji,jj) / hbl(ji,jj) ) )
1594           END IF
1595           zwb_ent(ji,jj) = - 2.0 * 0.2 * zrf_conv * zwbav(ji,jj) &
1596                  &                  - 0.15 * zrf_shear * zustar(ji,jj)**3 /zhml(ji,jj) &
1597                  &         + zr_stokes * ( 0.15 * EXP( -1.5 * zla(ji,jj) ) * zrf_shear * zustar(ji,jj)**3 &
1598                  &                                         - zrf_langmuir * 0.03 * zwstrl(ji,jj)**3 ) / zhml(ji,jj)
1599             !
1600         ENDIF
1601       END DO ! ji loop
1602     END DO   ! jj loop
1603
1604     zwb_min(:,:) = 0._wp
1605
1606     DO jj = 2, jpjm1
1607       DO ji = 2, jpim1
1608         IF ( lshear(ji,jj) ) THEN
1609           IF ( lconv(ji,jj) ) THEN
1610! Unstable OSBL
1611              zwb_shr = -za_wb_s * zshear(ji,jj)
1612              IF ( j_ddh(ji,jj) == 0 ) THEN
1613
1614! Developing shear layer, additional shear production possible.
1615
1616                zshear_u = MAX( zustar(ji,jj)**2 * zdu_ml(ji,jj) /  zhbl(ji,jj), 0._wp )
1617                zshear(ji,jj) = zshear(ji,jj) + zshear_u * ( 1.0 - MIN( zri_p / rn_ri_p_thresh, 1.d0 ) )
1618                zshear(ji,jj) = MIN( zshear(ji,jj), zshear_u )
1619               
1620                zwb_shr = -za_wb_s * zshear(ji,jj)
1621               
1622              ENDIF               
1623              zwb_ent(ji,jj) = zwb_ent(ji,jj) + zwb_shr
1624              zwb_min(ji,jj) = zwb_ent(ji,jj) + zdh(ji,jj) / zhbl(ji,jj) * zwb0(ji,jj)
1625           ELSE    ! IF ( lconv ) THEN - ENDIF
1626! Stable OSBL  - shear production not coded for first attempt.           
1627           ENDIF  ! lconv
1628         ELSE  ! lshear
1629           IF ( lconv(ji,jj) ) THEN
1630! Unstable OSBL
1631              zwb_shr = -za_wb_s * zshear(ji,jj)
1632              zwb_ent(ji,jj) = zwb_ent(ji,jj) + zwb_shr
1633              zwb_min(ji,jj) = zwb_ent(ji,jj) + zdh(ji,jj) / zhbl(ji,jj) * zwb0(ji,jj)
1634           ENDIF  ! lconv
1635         ENDIF    ! lshear
1636       END DO   ! ji
1637     END DO     ! jj
1638   END SUBROUTINE zdf_osm_osbl_state
1639     
1640     
1641   SUBROUTINE zdf_osm_vertical_average( jnlev_av, jp_ext, zt, zs, zb, zu, zv, zdt, zds, zdb, zdu, zdv )
1642     !!---------------------------------------------------------------------
1643     !!                   ***  ROUTINE zdf_vertical_average  ***
1644     !!
1645     !! ** Purpose : Determines vertical averages from surface to jnlev.
1646     !!
1647     !! ** Method  : Averages are calculated from the surface to jnlev.
1648     !!              The external level used to calculate differences is ibld+ibld_ext
1649     !!
1650     !!----------------------------------------------------------------------
1651
1652        INTEGER, DIMENSION(jpi,jpj) :: jnlev_av  ! Number of levels to average over.
1653        INTEGER, DIMENSION(jpi,jpj) :: jp_ext
1654
1655        ! Alan: do we need zb?
1656        REAL(wp), DIMENSION(jpi,jpj) :: zt, zs, zb        ! Average temperature and salinity
1657        REAL(wp), DIMENSION(jpi,jpj) :: zu,zv         ! Average current components
1658        REAL(wp), DIMENSION(jpi,jpj) :: zdt, zds, zdb ! Difference between average and value at base of OSBL
1659        REAL(wp), DIMENSION(jpi,jpj) :: zdu, zdv      ! Difference for velocity components.
1660
1661        INTEGER :: jk, ji, jj, ibld_ext
1662        REAL(wp) :: zthick, zthermal, zbeta
1663
1664
1665        zt   = 0._wp
1666        zs   = 0._wp
1667        zu   = 0._wp
1668        zv   = 0._wp
1669        DO jj = 2, jpjm1                                 !  Vertical slab
1670         DO ji = 2, jpim1
1671            zthermal = rab_n(ji,jj,1,jp_tem) !ideally use ibld not 1??
1672            zbeta    = rab_n(ji,jj,1,jp_sal)
1673               ! average over depth of boundary layer
1674            zthick = epsln
1675            DO jk = 2, jnlev_av(ji,jj)
1676               zthick = zthick + e3t_n(ji,jj,jk)
1677               zt(ji,jj)   = zt(ji,jj)  + e3t_n(ji,jj,jk) * tsn(ji,jj,jk,jp_tem)
1678               zs(ji,jj)   = zs(ji,jj)  + e3t_n(ji,jj,jk) * tsn(ji,jj,jk,jp_sal)
1679               zu(ji,jj)   = zu(ji,jj)  + e3t_n(ji,jj,jk) &
1680                     &            * ( ub(ji,jj,jk) + ub(ji - 1,jj,jk) ) &
1681                     &            / MAX( 1. , umask(ji,jj,jk) + umask(ji - 1,jj,jk) )
1682               zv(ji,jj)   = zv(ji,jj)  + e3t_n(ji,jj,jk) &
1683                     &            * ( vb(ji,jj,jk) + vb(ji,jj - 1,jk) ) &
1684                     &            / MAX( 1. , vmask(ji,jj,jk) + vmask(ji,jj - 1,jk) )
1685            END DO
1686            zt(ji,jj) = zt(ji,jj) / zthick
1687            zs(ji,jj) = zs(ji,jj) / zthick
1688            zu(ji,jj) = zu(ji,jj) / zthick
1689            zv(ji,jj) = zv(ji,jj) / zthick
1690            zb(ji,jj) = grav * zthermal * zt(ji,jj) - grav * zbeta * zs(ji,jj)
1691            ibld_ext = jnlev_av(ji,jj) + jp_ext(ji,jj)
1692            IF ( ibld_ext < mbkt(ji,jj) ) THEN
1693              zdt(ji,jj) = zt(ji,jj) - tsn(ji,jj,ibld_ext,jp_tem)
1694              zds(ji,jj) = zs(ji,jj) - tsn(ji,jj,ibld_ext,jp_sal)
1695              zdu(ji,jj) = zu(ji,jj) - ( ub(ji,jj,ibld_ext) + ub(ji-1,jj,ibld_ext ) ) &
1696                     &    / MAX(1. , umask(ji,jj,ibld_ext ) + umask(ji-1,jj,ibld_ext ) )
1697              zdv(ji,jj) = zv(ji,jj) - ( vb(ji,jj,ibld_ext) + vb(ji,jj-1,ibld_ext ) ) &
1698                     &   / MAX(1. , vmask(ji,jj,ibld_ext ) + vmask(ji,jj-1,ibld_ext ) )
1699              zdb(ji,jj) = grav * zthermal * zdt(ji,jj) - grav * zbeta * zds(ji,jj)
1700            ELSE
1701              zdt(ji,jj) = 0._wp
1702              zds(ji,jj) = 0._wp
1703              zdu(ji,jj) = 0._wp
1704              zdv(ji,jj) = 0._wp
1705              zdb(ji,jj) = 0._wp
1706            ENDIF
1707         END DO
1708        END DO
1709   END SUBROUTINE zdf_osm_vertical_average
1710
1711   SUBROUTINE zdf_osm_velocity_rotation( zcos_w, zsin_w, zu, zv, zdu, zdv )
1712     !!---------------------------------------------------------------------
1713     !!                   ***  ROUTINE zdf_velocity_rotation  ***
1714     !!
1715     !! ** Purpose : Rotates frame of reference of averaged velocity components.
1716     !!
1717     !! ** Method  : The velocity components are rotated into frame specified by zcos_w and zsin_w
1718     !!
1719     !!----------------------------------------------------------------------
1720
1721        REAL(wp), DIMENSION(jpi,jpj) :: zcos_w, zsin_w       ! Cos and Sin of rotation angle
1722        REAL(wp), DIMENSION(jpi,jpj) :: zu, zv               ! Components of current
1723        REAL(wp), DIMENSION(jpi,jpj) :: zdu, zdv             ! Change in velocity components across pycnocline
1724
1725        INTEGER :: ji, jj
1726        REAL(wp) :: ztemp
1727
1728        DO jj = 2, jpjm1
1729           DO ji = 2, jpim1
1730              ztemp = zu(ji,jj)
1731              zu(ji,jj) = zu(ji,jj) * zcos_w(ji,jj) + zv(ji,jj) * zsin_w(ji,jj)
1732              zv(ji,jj) = zv(ji,jj) * zcos_w(ji,jj) - ztemp * zsin_w(ji,jj)
1733              ztemp = zdu(ji,jj)
1734              zdu(ji,jj) = zdu(ji,jj) * zcos_w(ji,jj) + zdv(ji,jj) * zsin_w(ji,jj)
1735              zdv(ji,jj) = zdv(ji,jj) * zsin_w(ji,jj) - ztemp * zsin_w(ji,jj)
1736           END DO
1737        END DO
1738    END SUBROUTINE zdf_osm_velocity_rotation
1739
1740    SUBROUTINE zdf_osm_osbl_state_fk( lpyc, lflux, lmle, zwb_fk )
1741     !!---------------------------------------------------------------------
1742     !!                   ***  ROUTINE zdf_osm_osbl_state_fk  ***
1743     !!
1744     !! ** Purpose : Determines the state of the OSBL and MLE layer. Info is returned in the logicals lpyc,lflux and lmle. Used with Fox-Kemper scheme.
1745     !!  lpyc :: determines whether pycnocline flux-grad relationship needs to be determined
1746     !!  lflux :: determines whether effects of surface flux extend below the base of the OSBL
1747     !!  lmle  :: determines whether the layer with MLE is increasing with time or if base is relaxing towards hbl.
1748     !!
1749     !! ** Method  :
1750     !!
1751     !!
1752     !!----------------------------------------------------------------------
1753     
1754! Outputs
1755      LOGICAL,  DIMENSION(jpi,jpj)  :: lpyc, lflux, lmle
1756      REAL(wp), DIMENSION(jpi,jpj)  :: zwb_fk
1757!
1758      REAL(wp), DIMENSION(jpi,jpj)  :: znd_param
1759      REAL(wp)                      :: zbuoy, ztmp, zpe_mle_layer
1760      REAL(wp)                      :: zpe_mle_ref, zwb_ent, zdbdz_mle_int
1761     
1762      znd_param(:,:) = 0._wp
1763
1764        DO jj = 2, jpjm1
1765          DO ji = 2, jpim1         
1766             ztmp =  r1_ft(ji,jj) *  MIN( 111.e3_wp , e1u(ji,jj) ) / rn_osm_mle_lf
1767             zwb_fk(ji,jj) = rn_osm_mle_ce * hmle(ji,jj) * hmle(ji,jj) * ztmp * zdbds_mle(ji,jj) * zdbds_mle(ji,jj)
1768          END DO
1769        END DO       
1770        DO jj = 2, jpjm1
1771          DO ji = 2, jpim1
1772                    !
1773            IF ( lconv(ji,jj) ) THEN
1774              IF ( zhmle(ji,jj) > 1.2 * zhbl(ji,jj) ) THEN
1775                zt_mle(ji,jj) = ( zt_mle(ji,jj) * zhmle(ji,jj) - zt_bl(ji,jj) * zhbl(ji,jj) ) / ( zhmle(ji,jj) - zhbl(ji,jj) )
1776                zs_mle(ji,jj) = ( zs_mle(ji,jj) * zhmle(ji,jj) - zs_bl(ji,jj) * zhbl(ji,jj) ) / ( zhmle(ji,jj) - zhbl(ji,jj) )
1777                zb_mle(ji,jj) = ( zb_mle(ji,jj) * zhmle(ji,jj) - zb_bl(ji,jj) * zhbl(ji,jj) ) / ( zhmle(ji,jj) - zhbl(ji,jj) )
1778                zdbdz_mle_int = ( zb_bl(ji,jj) - ( 2.0 * zb_mle(ji,jj) -zb_bl(ji,jj) ) ) / ( zhmle(ji,jj) - zhbl(ji,jj) )
1779! Calculate potential energies of actual profile and reference profile.
1780                zpe_mle_layer = 0._wp
1781                zpe_mle_ref = 0._wp
1782                zthermal = rab_n(ji,jj,1,jp_tem)
1783                zbeta    = rab_n(ji,jj,1,jp_sal)
1784
1785                DO jk = ibld(ji,jj), mld_prof(ji,jj)
1786                  zbuoy = grav * ( zthermal * tsn(ji,jj,jk,jp_tem) - zbeta * tsn(ji,jj,jk,jp_sal) )
1787                  zpe_mle_layer = zpe_mle_layer + zbuoy * gdepw_n(ji,jj,jk) * e3w_n(ji,jj,jk)
1788                  zpe_mle_ref = zpe_mle_ref + ( zb_bl(ji,jj) - zdbdz_mle_int * ( gdepw_n(ji,jj,jk) - zhbl(ji,jj) ) ) * gdepw_n(ji,jj,jk) * e3w_n(ji,jj,jk)
1789                END DO
1790! Non-dimensional parameter to diagnose the presence of thermocline
1791                   
1792                znd_param(ji,jj) = ( zpe_mle_layer - zpe_mle_ref ) * ABS( ff_t(ji,jj) ) / ( MAX( zwb_fk(ji,jj), 1.0e-10 ) * zhmle(ji,jj) )
1793              ENDIF
1794            ENDIF
1795          END DO
1796        END DO
1797
1798! Diagnosis
1799        DO jj = 2, jpjm1
1800           DO ji = 2, jpim1
1801             IF ( lconv(ji,jj) ) THEN
1802               zwb_ent = - 2.0 * 0.2 * zwbav(ji,jj) &
1803                  &                  - 0.15 * zustar(ji,jj)**3 /zhml(ji,jj) &
1804                  &         + ( 0.15 * EXP( -1.5 * zla(ji,jj) ) * zustar(ji,jj)**3 &
1805                  &         - 0.03 * zwstrl(ji,jj)**3 ) / zhml(ji,jj)
1806               IF ( -2.0 * zwb_fk(ji,jj) / zwb_ent > 0.5 ) THEN
1807                 IF ( zhmle(ji,jj) > 1.2 * zhbl(ji,jj) ) THEN
1808! MLE layer growing
1809                   IF ( znd_param (ji,jj) > 100. ) THEN
1810! Thermocline present
1811                     lflux(ji,jj) = .FALSE.
1812                     lmle(ji,jj) =.FALSE.
1813                   ELSE
1814! Thermocline not present
1815                     lflux(ji,jj) = .TRUE.
1816                     lmle(ji,jj) = .TRUE.
1817                   ENDIF  ! znd_param > 100
1818!
1819                   IF ( zdb_bl(ji,jj) < rn_osm_bl_thresh ) THEN
1820                     lpyc(ji,jj) = .FALSE.
1821                   ELSE
1822                      lpyc(ji,jj) = .TRUE.
1823                   ENDIF
1824                 ELSE
1825! MLE layer restricted to OSBL or just below.
1826                   IF ( zdb_bl(ji,jj) < rn_osm_bl_thresh ) THEN
1827! Weak stratification MLE layer can grow.
1828                     lpyc(ji,jj) = .FALSE.
1829                     lflux(ji,jj) = .TRUE.
1830                     lmle(ji,jj) = .TRUE.
1831                   ELSE
1832! Strong stratification
1833                     lpyc(ji,jj) = .TRUE.
1834                     lflux(ji,jj) = .FALSE.
1835                     lmle(ji,jj) = .FALSE.
1836                   ENDIF ! zdb_bl < rn_mle_thresh_bl and
1837                 ENDIF  ! zhmle > 1.2 zhbl
1838               ELSE
1839                 lpyc(ji,jj) = .TRUE.
1840                 lflux(ji,jj) = .FALSE.
1841                 lmle(ji,jj) = .FALSE.
1842                 IF ( zdb_bl(ji,jj) < rn_osm_bl_thresh ) lpyc(ji,jj) = .FALSE.
1843               ENDIF !  -2.0 * zwb_fk(ji,jj) / zwb_ent > 0.5
1844             ELSE
1845! Stable Boundary Layer
1846               lpyc(ji,jj) = .FALSE.
1847               lflux(ji,jj) = .FALSE.
1848               lmle(ji,jj) = .FALSE.
1849             ENDIF  ! lconv
1850           END DO
1851        END DO
1852    END SUBROUTINE zdf_osm_osbl_state_fk
1853
1854    SUBROUTINE zdf_osm_external_gradients(jbase, zdtdz, zdsdz, zdbdz )
1855     !!---------------------------------------------------------------------
1856     !!                   ***  ROUTINE zdf_osm_external_gradients  ***
1857     !!
1858     !! ** Purpose : Calculates the gradients below the OSBL
1859     !!
1860     !! ** Method  : Uses ibld and ibld_ext to determine levels to calculate the gradient.
1861     !!
1862     !!----------------------------------------------------------------------
1863
1864     INTEGER, DIMENSION(jpi,jpj)  :: jbase
1865     REAL(wp), DIMENSION(jpi,jpj) :: zdtdz, zdsdz, zdbdz   ! External gradients of temperature, salinity and buoyancy.
1866
1867     INTEGER :: jj, ji, jkb, jkb1
1868     REAL(wp) :: zthermal, zbeta
1869
1870
1871     DO jj = 2, jpjm1
1872        DO ji = 2, jpim1
1873           IF ( jbase(ji,jj)+1 < mbkt(ji,jj) ) THEN
1874              zthermal = rab_n(ji,jj,1,jp_tem) !ideally use ibld not 1??
1875              zbeta    = rab_n(ji,jj,1,jp_sal)
1876              jkb = jbase(ji,jj)
1877              jkb1 = MIN(jkb + 1, mbkt(ji,jj))
1878              zdtdz(ji,jj) = - ( tsn(ji,jj,jkb1,jp_tem) - tsn(ji,jj,jkb,jp_tem ) ) &
1879                   &   / e3t_n(ji,jj,ibld(ji,jj))
1880              zdsdz(ji,jj) = - ( tsn(ji,jj,jkb1,jp_sal) - tsn(ji,jj,jkb,jp_sal ) ) &
1881                   &   / e3t_n(ji,jj,ibld(ji,jj))
1882              zdbdz(ji,jj) = grav * zthermal * zdtdz(ji,jj) - grav * zbeta * zdsdz(ji,jj)
1883           ELSE
1884              zdtdz(ji,jj) = 0._wp
1885              zdsdz(ji,jj) = 0._wp
1886              zdbdz(ji,jj) = 0._wp
1887           END IF
1888        END DO
1889     END DO
1890    END SUBROUTINE zdf_osm_external_gradients
1891
1892    SUBROUTINE zdf_osm_pycnocline_scalar_profiles( zdtdz, zdsdz, zdbdz, zalpha )
1893
1894     REAL(wp), DIMENSION(jpi,jpj,jpk) :: zdtdz, zdsdz, zdbdz      ! gradients in the pycnocline
1895     REAL(wp), DIMENSION(jpi,jpj) :: zalpha
1896
1897     INTEGER :: jk, jj, ji
1898     REAL(wp) :: ztgrad, zsgrad, zbgrad
1899     REAL(wp) :: zgamma_b_nd, znd
1900     REAL(wp) :: zzeta_m, zzeta_en, zbuoy_pyc_sc
1901     REAL(wp), PARAMETER :: zgamma_b = 2.25, zzeta_sh = 0.15
1902
1903     DO jj = 2, jpjm1
1904        DO ji = 2, jpim1
1905           IF ( ibld(ji,jj) + jp_ext(ji,jj) < mbkt(ji,jj) ) THEN
1906              IF ( lconv(ji,jj) ) THEN  ! convective conditions
1907                IF ( lpyc(ji,jj) ) THEN
1908                   zzeta_m = 0.1 + 0.3 / ( 1.0 + EXP( -3.5 * LOG10( -zhol(ji,jj) ) ) )
1909                   zalpha(ji,jj) = 2.0 * ( 1.0 - ( 0.80 * zzeta_m + 0.5 * SQRT( 3.14159 / zgamma_b ) ) * zdbdz_bl_ext(ji,jj) * zdh(ji,jj) / zdb_ml(ji,jj) ) / ( 0.723 + SQRT( 3.14159 / zgamma_b ) )
1910                   zalpha(ji,jj) = MAX( zalpha(ji,jj), 0._wp )
1911
1912                   ztmp = 1._wp/MAX(zdh(ji,jj), epsln)
1913!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1914! Commented lines in this section are not needed in new code, once tested !
1915! can be removed                                                          !
1916!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1917!                   ztgrad = zalpha * zdt_ml(ji,jj) * ztmp + zdtdz_bl_ext(ji,jj)
1918!                   zsgrad = zalpha * zds_ml(ji,jj) * ztmp + zdsdz_bl_ext(ji,jj)
1919                   zbgrad = zalpha(ji,jj) * zdb_ml(ji,jj) * ztmp + zdbdz_bl_ext(ji,jj)
1920                   zgamma_b_nd = zdbdz_bl_ext(ji,jj) * zdh(ji,jj) / MAX(zdb_ml(ji,jj), epsln)
1921                   DO jk = 2, ibld(ji,jj)+ibld_ext
1922                     znd = -( gdepw_n(ji,jj,jk) - zhbl(ji,jj) ) * ztmp
1923                     IF ( znd <= zzeta_m ) THEN
1924!                        zdtdz(ji,jj,jk) = zdtdz_bl_ext(ji,jj) + zalpha * zdt_ml(ji,jj) * ztmp * &
1925!                &        EXP( -6.0 * ( znd -zzeta_m )**2 )
1926!                        zdsdz(ji,jj,jk) = zdsdz_bl_ext(ji,jj) + zalpha * zds_ml(ji,jj) * ztmp * &
1927!                                  & EXP( -6.0 * ( znd -zzeta_m )**2 )
1928                        zdbdz(ji,jj,jk) = zdbdz_bl_ext(ji,jj) + zalpha(ji,jj) * zdb_ml(ji,jj) * ztmp * &
1929                                  & EXP( -6.0 * ( znd -zzeta_m )**2 )
1930                     ELSE
1931!                         zdtdz(ji,jj,jk) =  ztgrad * EXP( -zgamma_b * ( znd - zzeta_m )**2 )
1932!                         zdsdz(ji,jj,jk) =  zsgrad * EXP( -zgamma_b * ( znd - zzeta_m )**2 )
1933                         zdbdz(ji,jj,jk) =  zbgrad * EXP( -zgamma_b * ( znd - zzeta_m )**2 )
1934                     ENDIF
1935                  END DO
1936               ENDIF ! if no pycnocline pycnocline gradients set to zero
1937              ELSE
1938                 ! stable conditions
1939                 ! if pycnocline profile only defined when depth steady of increasing.
1940                 IF ( zdhdt(ji,jj) > 0.0 ) THEN        ! Depth increasing, or steady.
1941                    IF ( zdb_bl(ji,jj) > 0._wp ) THEN
1942                       IF ( zhol(ji,jj) >= 0.5 ) THEN      ! Very stable - 'thick' pycnocline
1943                          ztmp = 1._wp/MAX(zhbl(ji,jj), epsln)
1944                          ztgrad = zdt_bl(ji,jj) * ztmp
1945                          zsgrad = zds_bl(ji,jj) * ztmp
1946                          zbgrad = zdb_bl(ji,jj) * ztmp
1947                          DO jk = 2, ibld(ji,jj)
1948                             znd = gdepw_n(ji,jj,jk) * ztmp
1949                             zdtdz(ji,jj,jk) =  ztgrad * EXP( -15.0 * ( znd - 0.9 )**2 )
1950                             zdbdz(ji,jj,jk) =  zbgrad * EXP( -15.0 * ( znd - 0.9 )**2 )
1951                             zdsdz(ji,jj,jk) =  zsgrad * EXP( -15.0 * ( znd - 0.9 )**2 )
1952                          END DO
1953                       ELSE                                   ! Slightly stable - 'thin' pycnoline - needed when stable layer begins to form.
1954                          ztmp = 1._wp/MAX(zdh(ji,jj), epsln)
1955                          ztgrad = zdt_bl(ji,jj) * ztmp
1956                          zsgrad = zds_bl(ji,jj) * ztmp
1957                          zbgrad = zdb_bl(ji,jj) * ztmp
1958                          DO jk = 2, ibld(ji,jj)
1959                             znd = -( gdepw_n(ji,jj,jk) - zhml(ji,jj) ) * ztmp
1960                             zdtdz(ji,jj,jk) =  ztgrad * EXP( -1.75 * ( znd + 0.75 )**2 )
1961                             zdbdz(ji,jj,jk) =  zbgrad * EXP( -1.75 * ( znd + 0.75 )**2 )
1962                             zdsdz(ji,jj,jk) =  zsgrad * EXP( -1.75 * ( znd + 0.75 )**2 )
1963                          END DO
1964                       ENDIF ! IF (zhol >=0.5)
1965                    ENDIF    ! IF (zdb_bl> 0.)
1966                 ENDIF       ! IF (zdhdt >= 0) zdhdt < 0 not considered since pycnocline profile is zero and profile arrays are intialized to zero
1967              ENDIF          ! IF (lconv)
1968           ENDIF      ! IF ( ibld(ji,jj) < mbkt(ji,jj) )
1969        END DO
1970     END DO
1971
1972    END SUBROUTINE zdf_osm_pycnocline_scalar_profiles
1973
1974    SUBROUTINE zdf_osm_pycnocline_shear_profiles( zdudz, zdvdz )
1975      !!---------------------------------------------------------------------
1976      !!                   ***  ROUTINE zdf_osm_pycnocline_shear_profiles  ***
1977      !!
1978      !! ** Purpose : Calculates velocity shear in the pycnocline
1979      !!
1980      !! ** Method  :
1981      !!
1982      !!----------------------------------------------------------------------
1983
1984      REAL(wp), DIMENSION(jpi,jpj,jpk) :: zdudz, zdvdz
1985
1986      INTEGER :: jk, jj, ji
1987      REAL(wp) :: zugrad, zvgrad, znd
1988      REAL(wp) :: zzeta_v = 0.45
1989      !
1990      DO jj = 2, jpjm1
1991         DO ji = 2, jpim1
1992            !
1993            IF ( ibld(ji,jj) + jp_ext(ji,jj) < mbkt(ji,jj) ) THEN
1994               IF ( lconv (ji,jj) ) THEN
1995                  ! Unstable conditions. Shouldn;t be needed with no pycnocline code.
1996!                  zugrad = 0.7 * zdu_ml(ji,jj) / zdh(ji,jj) + 0.3 * zustar(ji,jj)*zustar(ji,jj) / &
1997!                       &      ( ( ( zvstr(ji,jj)**3 + 0.5 * zwstrc(ji,jj)**3 )**pthird * zhml(ji,jj) ) * &
1998!                      &      MIN(zla(ji,jj)**(8.0/3.0) + epsln, 0.12 ))
1999                  !Alan is this right?
2000!                  zvgrad = ( 0.7 * zdv_ml(ji,jj) + &
2001!                       &    2.0 * ff_t(ji,jj) * zustke(ji,jj) * dstokes(ji,jj) / &
2002!                       &          ( ( zvstr(ji,jj)**3 + 0.5 * zwstrc(ji,jj)**3 )**pthird  + epsln ) &
2003!                       &      )/ (zdh(ji,jj)  + epsln )
2004!                  DO jk = 2, ibld(ji,jj) - 1 + ibld_ext
2005!                     znd = -( gdepw_n(ji,jj,jk) - zhbl(ji,jj) ) / (zdh(ji,jj) + epsln ) - zzeta_v
2006!                     IF ( znd <= 0.0 ) THEN
2007!                        zdudz(ji,jj,jk) = 1.25 * zugrad * EXP( 3.0 * znd )
2008!                        zdvdz(ji,jj,jk) = 1.25 * zvgrad * EXP( 3.0 * znd )
2009!                     ELSE
2010!                        zdudz(ji,jj,jk) = 1.25 * zugrad * EXP( -2.0 * znd )
2011!                        zdvdz(ji,jj,jk) = 1.25 * zvgrad * EXP( -2.0 * znd )
2012!                     ENDIF
2013!                  END DO
2014               ELSE
2015                  ! stable conditions
2016                  zugrad = 3.25 * zdu_bl(ji,jj) / zhbl(ji,jj)
2017                  zvgrad = 2.75 * zdv_bl(ji,jj) / zhbl(ji,jj)
2018                  DO jk = 2, ibld(ji,jj)
2019                     znd = gdepw_n(ji,jj,jk) / zhbl(ji,jj)
2020                     IF ( znd < 1.0 ) THEN
2021                        zdudz(ji,jj,jk) = zugrad * EXP( -40.0 * ( znd - 1.0 )**2 )
2022                     ELSE
2023                        zdudz(ji,jj,jk) = zugrad * EXP( -20.0 * ( znd - 1.0 )**2 )
2024                     ENDIF
2025                     zdvdz(ji,jj,jk) = zvgrad * EXP( -20.0 * ( znd - 0.85 )**2 )
2026                  END DO
2027               ENDIF
2028               !
2029            END IF      ! IF ( ibld(ji,jj) + ibld_ext < mbkt(ji,jj) )
2030         END DO
2031      END DO
2032    END SUBROUTINE zdf_osm_pycnocline_shear_profiles
2033
2034   SUBROUTINE zdf_osm_calculate_dhdt( zdhdt, zddhdt )
2035     !!---------------------------------------------------------------------
2036     !!                   ***  ROUTINE zdf_osm_calculate_dhdt  ***
2037     !!
2038     !! ** Purpose : Calculates the rate at which hbl changes.
2039     !!
2040     !! ** Method  :
2041     !!
2042     !!----------------------------------------------------------------------
2043
2044    REAL(wp), DIMENSION(jpi,jpj) :: zdhdt, zddhdt        ! Rate of change of hbl
2045
2046    INTEGER :: jj, ji
2047    REAL(wp) :: zgamma_b_nd, zgamma_dh_nd, zpert, zpsi
2048    REAL(wp) :: zvel_max!, zwb_min
2049    REAL(wp) :: zzeta_m = 0.3
2050    REAL(wp) :: zgamma_c = 2.0
2051    REAL(wp) :: zdhoh = 0.1
2052    REAL(wp) :: alpha_bc = 0.5
2053    REAL, PARAMETER :: a_ddh = 2.5, a_ddh_2 = 3.5 ! also in pycnocline_depth
2054 
2055  DO jj = 2, jpjm1
2056     DO ji = 2, jpim1
2057       
2058       IF ( lshear(ji,jj) ) THEN
2059          IF ( lconv(ji,jj) ) THEN    ! Convective
2060
2061             IF ( ln_osm_mle ) THEN
2062
2063                IF ( hmle(ji,jj) > hbl(ji,jj) ) THEN
2064          ! Fox-Kemper buoyancy flux average over OSBL
2065                   zwb_fk_b(ji,jj) = zwb_fk(ji,jj) *  &
2066                        (1.0 + hmle(ji,jj) / ( 6.0 * hbl(ji,jj) ) * (-1.0 + ( 1.0 - 2.0 * hbl(ji,jj) / hmle(ji,jj))**3) )
2067                ELSE
2068                   zwb_fk_b(ji,jj) = 0.5 * zwb_fk(ji,jj) * hmle(ji,jj) / hbl(ji,jj)
2069                ENDIF
2070                zvel_max = ( zwstrl(ji,jj)**3 + 0.5 * zwstrc(ji,jj)**3 )**p2third / hbl(ji,jj)
2071                IF ( ( zwb_ent(ji,jj) + 2.0 * zwb_fk_b(ji,jj) ) < 0.0 ) THEN
2072                   ! OSBL is deepening, entrainment > restratification
2073                   IF ( zdb_bl(ji,jj) > 0.0 .and. zdbdz_bl_ext(ji,jj) > 0.0 ) THEN
2074! *** Used for shear Needs to be changed to work stabily
2075!                zgamma_b_nd = zdbdz_bl_ext * dh / zdb_ml
2076!                zalpha_b = 6.7 * zgamma_b_nd / ( 1.0 + zgamma_b_nd )
2077!                zgamma_b = zgamma_b_nd / ( 0.12 * ( 1.25 + zgamma_b_nd ) )
2078!                za_1 = 1.0 / zgamma_b**2 - 0.017
2079!                za_2 = 1.0 / zgamma_b**3 - 0.0025
2080!                zpsi = zalpha_b * ( 1.0 + zgamma_b_nd ) * ( za_1 - 2.0 * za_2 * dh / hbl )
2081                      zpsi = 0._wp
2082                      zdhdt(ji,jj) = -( zwb_ent(ji,jj) + 2.0 * zwb_fk_b(ji,jj) ) / ( zvel_max + MAX(zdb_bl(ji,jj), 1.0e-15) )
2083                      zdhdt(ji,jj) = zdhdt(ji,jj)! - zpsi * ( -1.0 / zhml(ji,jj) + 2.4 * zdbdz_bl_ext(ji,jj) / zdb_ml(ji,jj) ) * zwb_min(ji,jj) * zdh(ji,jj) / zdb_bl(ji,jj)
2084                      IF ( j_ddh(ji,jj) == 1 ) THEN
2085                        IF ( ( zwstrc(ji,jj) / zvstr(ji,jj) )**3 <= 0.5 ) THEN
2086                           zari = MIN( 1.5 * zdb_bl(ji,jj) / ( zhbl(ji,jj) * ( MAX(zdbdz_bl_ext(ji,jj),0._wp) + zdb_bl(ji,jj)**2 / MAX(4.5 * zvstr(ji,jj)**2 , 1.e-12 )) ), 0.2d0 )
2087                        ELSE
2088                           zari = MIN( 1.5 * zdb_bl(ji,jj) / ( zhbl(ji,jj) * ( MAX(zdbdz_bl_ext(ji,jj),0._wp) + zdb_bl(ji,jj)**2 / MAX(4.5 * zwstrc(ji,jj)**2 , 1.e-12 )) ), 0.2d0 )
2089                        ENDIF
2090! Relaxation to dh_ref = zari * hbl
2091                        zddhdt(ji,jj) = -a_ddh_2 * ( 1.0 - zdh(ji,jj) / ( zari * zhbl(ji,jj) ) ) * zwb_ent(ji,jj) / zdb_bl(ji,jj)
2092                       
2093                      ELSE  ! j_ddh == 0
2094! Growing shear layer
2095                        zddhdt(ji,jj) = -a_ddh * ( 1.0 - zdh(ji,jj) / zhbl(ji,jj) ) * zwb_ent(ji,jj) / zdb_bl(ji,jj)
2096                      ENDIF ! j_ddh
2097                        zdhdt(ji,jj) = zdhdt(ji,jj) ! + zpsi * zddhdt(ji,jj)
2098                   ELSE    ! zdb_bl >0
2099                      zdhdt(ji,jj) = -( zwb_ent(ji,jj) + 2.0 * zwb_fk_b(ji,jj) ) /  MAX( zvel_max, 1.0e-15)
2100                   ENDIF
2101                ELSE   ! zwb_min + 2*zwb_fk_b < 0
2102                   ! OSBL shoaling due to restratification flux. This is the velocity defined in Fox-Kemper et al (2008)
2103                   zdhdt(ji,jj) = - zvel_mle(ji,jj)
2104
2105
2106                ENDIF
2107
2108             ELSE
2109                ! Fox-Kemper not used.
2110
2111                  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) / &
2112                  &        MAX((zvstr(ji,jj)**3 + 0.5 * zwstrc(ji,jj)**3 )**pthird, epsln)
2113                  zdhdt(ji,jj) = -zwb_ent(ji,jj) / ( zvel_max + MAX(zdb_bl(ji,jj), 1.0e-15) )
2114                ! added ajgn 23 July as temporay fix
2115
2116             ENDIF  ! ln_osm_mle
2117
2118            ELSE    ! lconv - Stable
2119                zdhdt(ji,jj) = ( 0.06 + 0.52 * zhol(ji,jj) / 2.0 ) * zvstr(ji,jj)**3 / hbl(ji,jj) + zwbav(ji,jj)
2120                IF ( zdhdt(ji,jj) < 0._wp ) THEN
2121                   ! For long timsteps factor in brackets slows the rapid collapse of the OSBL
2122                    zpert = 2.0 * ( 1.0 + 0.0 * 2.0 * zvstr(ji,jj) * rn_rdt / hbl(ji,jj) ) * zvstr(ji,jj)**2 / hbl(ji,jj)
2123                ELSE
2124                    zpert = MAX( 2.0 * zvstr(ji,jj)**2 / hbl(ji,jj), zdb_bl(ji,jj) )
2125                ENDIF
2126                zdhdt(ji,jj) = 2.0 * zdhdt(ji,jj) / MAX(zpert, epsln)
2127            ENDIF  ! lconv
2128       ELSE ! lshear
2129         IF ( lconv(ji,jj) ) THEN    ! Convective
2130
2131             IF ( ln_osm_mle ) THEN
2132
2133                IF ( hmle(ji,jj) > hbl(ji,jj) ) THEN
2134          ! Fox-Kemper buoyancy flux average over OSBL
2135                   zwb_fk_b(ji,jj) = zwb_fk(ji,jj) *  &
2136                        (1.0 + hmle(ji,jj) / ( 6.0 * hbl(ji,jj) ) * (-1.0 + ( 1.0 - 2.0 * hbl(ji,jj) / hmle(ji,jj))**3) )
2137                ELSE
2138                   zwb_fk_b(ji,jj) = 0.5 * zwb_fk(ji,jj) * hmle(ji,jj) / hbl(ji,jj)
2139                ENDIF
2140                zvel_max = ( zwstrl(ji,jj)**3 + 0.5 * zwstrc(ji,jj)**3 )**p2third / hbl(ji,jj)
2141                IF ( ( zwb_ent(ji,jj) + 2.0 * zwb_fk_b(ji,jj) ) < 0.0 ) THEN
2142                   ! OSBL is deepening, entrainment > restratification
2143                   IF ( zdb_bl(ji,jj) > 0.0 .and. zdbdz_bl_ext(ji,jj) > 0.0 ) THEN
2144                      zdhdt(ji,jj) = -( zwb_ent(ji,jj) + 2.0 * zwb_fk_b(ji,jj) ) / ( zvel_max + MAX(zdb_bl(ji,jj), 1.0e-15) )
2145                   ELSE
2146                      zdhdt(ji,jj) = -( zwb_ent(ji,jj) + 2.0 * zwb_fk_b(ji,jj) ) /  MAX( zvel_max, 1.0e-15)
2147                   ENDIF
2148                ELSE
2149                   ! OSBL shoaling due to restratification flux. This is the velocity defined in Fox-Kemper et al (2008)
2150                   zdhdt(ji,jj) = - zvel_mle(ji,jj)
2151
2152
2153                ENDIF
2154
2155             ELSE
2156                ! Fox-Kemper not used.
2157
2158                  zvel_max = -zwb_ent(ji,jj) / &
2159                  &        MAX((zvstr(ji,jj)**3 + 0.5 * zwstrc(ji,jj)**3 )**pthird, epsln)
2160                  zdhdt(ji,jj) = -zwb_ent(ji,jj) / ( zvel_max + MAX(zdb_bl(ji,jj), 1.0e-15) )
2161                ! added ajgn 23 July as temporay fix
2162
2163             ENDIF  ! ln_osm_mle
2164
2165            ELSE                        ! Stable
2166                zdhdt(ji,jj) = ( 0.06 + 0.52 * zhol(ji,jj) / 2.0 ) * zvstr(ji,jj)**3 / hbl(ji,jj) + zwbav(ji,jj)
2167                IF ( zdhdt(ji,jj) < 0._wp ) THEN
2168                   ! For long timsteps factor in brackets slows the rapid collapse of the OSBL
2169                    zpert = 2.0 * zvstr(ji,jj)**2 / hbl(ji,jj)
2170                ELSE
2171                    zpert = MAX( 2.0 * zvstr(ji,jj)**2 / hbl(ji,jj), zdb_bl(ji,jj) )
2172                ENDIF
2173                zdhdt(ji,jj) = 2.0 * zdhdt(ji,jj) / MAX(zpert, epsln)
2174            ENDIF  ! lconv
2175         ENDIF ! lshear
2176       END DO
2177     END DO
2178    END SUBROUTINE zdf_osm_calculate_dhdt
2179
2180    SUBROUTINE zdf_osm_timestep_hbl( zdhdt )
2181     !!---------------------------------------------------------------------
2182     !!                   ***  ROUTINE zdf_osm_timestep_hbl  ***
2183     !!
2184     !! ** Purpose : Increments hbl.
2185     !!
2186     !! ** Method  : If thechange in hbl exceeds one model level the change is
2187     !!              is calculated by moving down the grid, changing the buoyancy
2188     !!              jump. This is to ensure that the change in hbl does not
2189     !!              overshoot a stable layer.
2190     !!
2191     !!----------------------------------------------------------------------
2192
2193
2194    REAL(wp), DIMENSION(jpi,jpj) :: zdhdt   ! rates of change of hbl.
2195
2196    INTEGER :: jk, jj, ji, jm
2197    REAL(wp) :: zhbl_s, zvel_max, zdb
2198    REAL(wp) :: zthermal, zbeta
2199
2200     DO jj = 2, jpjm1
2201         DO ji = 2, jpim1
2202           IF ( ibld(ji,jj) - imld(ji,jj) > 1 ) THEN
2203!
2204! If boundary layer changes by more than one level, need to check for stable layers between initial and final depths.
2205!
2206              zhbl_s = hbl(ji,jj)
2207              jm = imld(ji,jj)
2208              zthermal = rab_n(ji,jj,1,jp_tem)
2209              zbeta = rab_n(ji,jj,1,jp_sal)
2210
2211
2212              IF ( lconv(ji,jj) ) THEN
2213!unstable
2214
2215                 IF( ln_osm_mle ) THEN
2216                    zvel_max = ( zwstrl(ji,jj)**3 + zwstrc(ji,jj)**3 )**p2third / hbl(ji,jj)
2217                 ELSE
2218
2219                    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) / &
2220                      &      ( zvstr(ji,jj)**3 + 0.5 * zwstrc(ji,jj)**3 )**pthird
2221
2222                 ENDIF
2223
2224                 DO jk = imld(ji,jj), ibld(ji,jj)
2225                    zdb = MAX( grav * ( zthermal * ( zt_bl(ji,jj) - tsn(ji,jj,jm,jp_tem) ) &
2226                         & - zbeta * ( zs_bl(ji,jj) - tsn(ji,jj,jm,jp_sal) ) ), &
2227                         &  0.0 ) + zvel_max
2228
2229
2230                    IF ( ln_osm_mle ) THEN
2231                       zhbl_s = zhbl_s + MIN( &
2232                        & rn_rdt * ( ( -zwb_ent(ji,jj) - 2.0 * zwb_fk_b(ji,jj) )/ zdb ) / FLOAT(ibld(ji,jj) - imld(ji,jj) ), &
2233                        & e3w_n(ji,jj,jm) )
2234                    ELSE
2235                      zhbl_s = zhbl_s + MIN( &
2236                        & rn_rdt * (  -zwb_ent(ji,jj) / zdb ) / FLOAT(ibld(ji,jj) - imld(ji,jj) ), &
2237                        & e3w_n(ji,jj,jm) )
2238                    ENDIF
2239
2240!                    zhbl_s = MIN(zhbl_s,  gdepw_n(ji,jj, mbkt(ji,jj) + 1) - depth_tol)
2241                    IF ( zhbl_s >= gdepw_n(ji,jj,mbkt(ji,jj) + 1) ) THEN
2242                      zhbl_s = MIN(zhbl_s,  gdepw_n(ji,jj, mbkt(ji,jj) + 1) - depth_tol)
2243                      lpyc(ji,jj) = .FALSE.
2244                    ENDIF
2245                    IF ( zhbl_s >= gdepw_n(ji,jj,jm+1) ) jm = jm + 1
2246                 END DO
2247                 hbl(ji,jj) = zhbl_s
2248                 ibld(ji,jj) = jm
2249             ELSE
2250! stable
2251                 DO jk = imld(ji,jj), ibld(ji,jj)
2252                    zdb = MAX( &
2253                         & grav * ( zthermal * ( zt_bl(ji,jj) - tsn(ji,jj,jm,jp_tem) )&
2254                         &           - zbeta * ( zs_bl(ji,jj) - tsn(ji,jj,jm,jp_sal) ) ),&
2255                         & 0.0 ) + &
2256             &       2.0 * zvstr(ji,jj)**2 / zhbl_s
2257
2258                    ! Alan is thuis right? I have simply changed hbli to hbl
2259                    zhol(ji,jj) = -zhbl_s / ( ( zvstr(ji,jj)**3 + epsln )/ zwbav(ji,jj) )
2260                    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) ) ) * &
2261               &                  zustar(ji,jj)**3 / zhbl_s ) * ( 0.725 + 0.225 * EXP( -7.5 * zhol(ji,jj) ) )
2262                    zdhdt(ji,jj) = zdhdt(ji,jj) + zwbav(ji,jj)
2263                    zhbl_s = zhbl_s + MIN( zdhdt(ji,jj) / zdb * rn_rdt / FLOAT( ibld(ji,jj) - imld(ji,jj) ), e3w_n(ji,jj,jm) )
2264
2265!                    zhbl_s = MIN(zhbl_s, gdepw_n(ji,jj, mbkt(ji,jj) + 1) - depth_tol)
2266                    IF ( zhbl_s >= mbkt(ji,jj) + 1 ) THEN
2267                      zhbl_s = MIN(zhbl_s,  gdepw_n(ji,jj, mbkt(ji,jj) + 1) - depth_tol)
2268                      lpyc(ji,jj) = .FALSE.
2269                    ENDIF
2270                    IF ( zhbl_s >= gdepw_n(ji,jj,jm) ) jm = jm + 1
2271                 END DO
2272             ENDIF   ! IF ( lconv )
2273             hbl(ji,jj) = MAX(zhbl_s, gdepw_n(ji,jj,4) )
2274             ibld(ji,jj) = MAX(jm, 4 )
2275           ELSE
2276! change zero or one model level.
2277             hbl(ji,jj) = MAX(zhbl_t(ji,jj), gdepw_n(ji,jj,4) )
2278           ENDIF
2279           zhbl(ji,jj) = gdepw_n(ji,jj,ibld(ji,jj))
2280         END DO
2281      END DO
2282
2283    END SUBROUTINE zdf_osm_timestep_hbl
2284
2285    SUBROUTINE zdf_osm_pycnocline_thickness( dh, zdh )
2286      !!---------------------------------------------------------------------
2287      !!                   ***  ROUTINE zdf_osm_pycnocline_thickness  ***
2288      !!
2289      !! ** Purpose : Calculates thickness of the pycnocline
2290      !!
2291      !! ** Method  : The thickness is calculated from a prognostic equation
2292      !!              that relaxes the pycnocine thickness to a diagnostic
2293      !!              value. The time change is calculated assuming the
2294      !!              thickness relaxes exponentially. This is done to deal
2295      !!              with large timesteps.
2296      !!
2297      !!----------------------------------------------------------------------
2298
2299      REAL(wp), DIMENSION(jpi,jpj) :: dh, zdh     ! pycnocline thickness.
2300       !
2301      INTEGER :: jj, ji
2302      INTEGER :: inhml
2303      REAL(wp) :: zari, ztau, zdh_ref
2304      REAL, PARAMETER :: a_ddh_2 = 3.5 ! also in pycnocline_depth
2305
2306    DO jj = 2, jpjm1
2307       DO ji = 2, jpim1
2308
2309         IF ( lshear(ji,jj) ) THEN
2310            IF ( lconv(ji,jj) ) THEN
2311              IF ( j_ddh(ji,jj) == 0 ) THEN
2312! ddhdt for pycnocline determined in osm_calculate_dhdt
2313                dh(ji,jj) = dh(ji,jj) + zddhdt(ji,jj) * rn_rdt
2314              ELSE
2315! Temporary (probably) Recalculate dh_ref to ensure dh doesn't go negative. Can't do this using zddhdt from calculate_dhdt
2316                IF ( ( zwstrc(ji,jj) / zvstr(ji,jj) )**3 <= 0.5 ) THEN
2317                  zari = MIN( 1.5 * zdb_bl(ji,jj) / ( zhbl(ji,jj) * ( MAX(zdbdz_bl_ext(ji,jj),0._wp) + zdb_bl(ji,jj)**2 / MAX(4.5 * zvstr(ji,jj)**2 , 1.e-12 )) ), 0.2d0 )
2318                ELSE
2319                  zari = MIN( 1.5 * zdb_bl(ji,jj) / ( zhbl(ji,jj) * ( MAX(zdbdz_bl_ext(ji,jj),0._wp) + zdb_bl(ji,jj)**2 / MAX(4.5 * zwstrc(ji,jj)**2 , 1.e-12 )) ), 0.2d0 )
2320                ENDIF
2321                ztau = MAX( zdb_bl(ji,jj) * ( zari * hbl(ji,jj) ) / ( a_ddh_2 * MAX(-zwb_ent(ji,jj), 1.e-12) ), 2.0 * rn_rdt )
2322                dh(ji,jj) = dh(ji,jj) * EXP( -rn_rdt / ztau ) + zari * zhbl(ji,jj) * ( 1.0 - EXP( -rn_rdt / ztau ) )
2323                IF ( dh(ji,jj) >= hbl(ji,jj) ) dh(ji,jj) = zari * zhbl(ji,jj)
2324              ENDIF
2325               
2326            ELSE ! lconv
2327! Initially shear only for entraining OSBL. Stable code will be needed if extended to stable OSBL
2328
2329               ztau = hbl(ji,jj) / MAX(zvstr(ji,jj), epsln)
2330               IF ( zdhdt(ji,jj) >= 0.0 ) THEN    ! probably shouldn't include wm here
2331                  ! boundary layer deepening
2332                  IF ( zdb_bl(ji,jj) > 0._wp ) THEN
2333                     ! pycnocline thickness set by stratification - use same relationship as for neutral conditions.
2334                     zari = MIN( 4.5 * ( zvstr(ji,jj)**2 ) &
2335                          & /  MAX(zdb_bl(ji,jj) * zhbl(ji,jj), epsln ) + 0.01  , 0.2 )
2336                     zdh_ref = MIN( zari, 0.2 ) * hbl(ji,jj)
2337                  ELSE
2338                     zdh_ref = 0.2 * hbl(ji,jj)
2339                  ENDIF
2340               ELSE     ! IF(dhdt < 0)
2341                  zdh_ref = 0.2 * hbl(ji,jj)
2342               ENDIF    ! IF (dhdt >= 0)
2343               dh(ji,jj) = dh(ji,jj) * EXP( -rn_rdt / ztau )+ zdh_ref * ( 1.0 - EXP( -rn_rdt / ztau ) )
2344               IF ( zdhdt(ji,jj) < 0._wp .and. dh(ji,jj) >= hbl(ji,jj) ) dh(ji,jj) = zdh_ref       ! can be a problem with dh>hbl for rapid collapse
2345               ! Alan: this hml is never defined or used -- do we need it?
2346            ENDIF
2347             
2348         ELSE   ! lshear 
2349! for lshear = .FALSE. calculate ddhdt here
2350
2351             IF ( lconv(ji,jj) ) THEN
2352
2353               IF( ln_osm_mle ) THEN
2354                  IF ( ( zwb_ent(ji,jj) + 2.0 * zwb_fk_b(ji,jj) ) < 0._wp ) THEN
2355                     ! OSBL is deepening. Note wb_fk_b is zero if ln_osm_mle=F
2356                     IF ( zdb_bl(ji,jj) > 0._wp .and. zdbdz_bl_ext(ji,jj) > 0._wp)THEN
2357                        IF ( ( zwstrc(ji,jj) / MAX(zvstr(ji,jj), epsln) )**3 <= 0.5 ) THEN  ! near neutral stability
2358                           zari = MIN( 1.5 * zdb_bl(ji,jj) / ( zhbl(ji,jj) * ( MAX(zdbdz_bl_ext(ji,jj),0._wp) + zdb_bl(ji,jj)**2 / MAX(4.5 * zvstr(ji,jj)**2 , 1.e-12 )) ), 0.2d0 )
2359                        ELSE                                                     ! unstable
2360                           zari = MIN( 1.5 * zdb_bl(ji,jj) / ( zhbl(ji,jj) * ( MAX(zdbdz_bl_ext(ji,jj),0._wp) + zdb_bl(ji,jj)**2 / MAX(4.5 * zwstrc(ji,jj)**2 , 1.e-12 )) ), 0.2d0 )
2361                        ENDIF
2362                        ztau = 0.2 * hbl(ji,jj) / MAX(epsln, (zvstr(ji,jj)**3 + 0.5 *zwstrc(ji,jj)**3)**pthird)
2363                        zdh_ref = zari * hbl(ji,jj)
2364                     ELSE
2365                        ztau = 0.2 * hbl(ji,jj) / MAX(epsln, (zvstr(ji,jj)**3 + 0.5 *zwstrc(ji,jj)**3)**pthird)
2366                        zdh_ref = 0.2 * hbl(ji,jj)
2367                     ENDIF
2368                  ELSE
2369                     ztau = 0.2 * hbl(ji,jj) /  MAX(epsln, (zvstr(ji,jj)**3 + 0.5 *zwstrc(ji,jj)**3)**pthird)
2370                     zdh_ref = 0.2 * hbl(ji,jj)
2371                  ENDIF
2372               ELSE ! ln_osm_mle
2373                  IF ( zdb_bl(ji,jj) > 0._wp .and. zdbdz_bl_ext(ji,jj) > 0._wp)THEN
2374                     IF ( ( zwstrc(ji,jj) / MAX(zvstr(ji,jj), epsln) )**3 <= 0.5 ) THEN  ! near neutral stability
2375                        zari = MIN( 1.5 * zdb_bl(ji,jj) / ( zhbl(ji,jj) * ( MAX(zdbdz_bl_ext(ji,jj),0._wp) + zdb_bl(ji,jj)**2 / MAX(4.5 * zvstr(ji,jj)**2 , 1.e-12 )) ), 0.2d0 )
2376                     ELSE                                                     ! unstable
2377                        zari = MIN( 1.5 * zdb_bl(ji,jj) / ( zhbl(ji,jj) * ( MAX(zdbdz_bl_ext(ji,jj),0._wp) + zdb_bl(ji,jj)**2 / MAX(4.5 * zwstrc(ji,jj)**2 , 1.e-12 )) ), 0.2d0 )
2378                     ENDIF
2379                     ztau = hbl(ji,jj) / MAX(epsln, (zvstr(ji,jj)**3 + 0.5 *zwstrc(ji,jj)**3)**pthird)
2380                     zdh_ref = zari * hbl(ji,jj)
2381                  ELSE
2382                     ztau = hbl(ji,jj) / MAX(epsln, (zvstr(ji,jj)**3 + 0.5 *zwstrc(ji,jj)**3)**pthird)
2383                     zdh_ref = 0.2 * hbl(ji,jj)
2384                  ENDIF
2385
2386               END IF  ! ln_osm_mle
2387
2388               dh(ji,jj) = dh(ji,jj) * EXP( -rn_rdt / ztau ) + zdh_ref * ( 1.0 - EXP( -rn_rdt / ztau ) )
2389!               IF ( zdhdt(ji,jj) < 0._wp .and. dh(ji,jj) >= hbl(ji,jj) ) dh(ji,jj) = zdh_ref
2390               IF ( dh(ji,jj) >= hbl(ji,jj) ) dh(ji,jj) = zdh_ref
2391               ! Alan: this hml is never defined or used
2392            ELSE   ! IF (lconv)
2393               ztau = hbl(ji,jj) / MAX(zvstr(ji,jj), epsln)
2394               IF ( zdhdt(ji,jj) >= 0.0 ) THEN    ! probably shouldn't include wm here
2395                  ! boundary layer deepening
2396                  IF ( zdb_bl(ji,jj) > 0._wp ) THEN
2397                     ! pycnocline thickness set by stratification - use same relationship as for neutral conditions.
2398                     zari = MIN( 4.5 * ( zvstr(ji,jj)**2 ) &
2399                          & /  MAX(zdb_bl(ji,jj) * zhbl(ji,jj), epsln ) + 0.01  , 0.2 )
2400                     zdh_ref = MIN( zari, 0.2 ) * hbl(ji,jj)
2401                  ELSE
2402                     zdh_ref = 0.2 * hbl(ji,jj)
2403                  ENDIF
2404               ELSE     ! IF(dhdt < 0)
2405                  zdh_ref = 0.2 * hbl(ji,jj)
2406               ENDIF    ! IF (dhdt >= 0)
2407               dh(ji,jj) = dh(ji,jj) * EXP( -rn_rdt / ztau )+ zdh_ref * ( 1.0 - EXP( -rn_rdt / ztau ) )
2408               IF ( zdhdt(ji,jj) < 0._wp .and. dh(ji,jj) >= hbl(ji,jj) ) dh(ji,jj) = zdh_ref       ! can be a problem with dh>hbl for rapid collapse
2409            ENDIF       ! IF (lconv)
2410         ENDIF  ! lshear
2411 
2412         hml(ji,jj) = hbl(ji,jj) - dh(ji,jj)
2413         inhml = MAX( INT( dh(ji,jj) / MAX(e3t_n(ji,jj,ibld(ji,jj)), 1.e-3) ) , 1 )
2414         imld(ji,jj) = MAX( ibld(ji,jj) - inhml, 3)
2415         zhml(ji,jj) = gdepw_n(ji,jj,imld(ji,jj))
2416         zdh(ji,jj) = zhbl(ji,jj) - zhml(ji,jj)
2417       END DO
2418     END DO
2419
2420    END SUBROUTINE zdf_osm_pycnocline_thickness
2421
2422
2423   SUBROUTINE zdf_osm_zmld_horizontal_gradients( zmld, zdtdx, zdtdy, zdsdx, zdsdy, dbdx_mle, dbdy_mle, zdbds_mle )
2424      !!----------------------------------------------------------------------
2425      !!                  ***  ROUTINE zdf_osm_horizontal_gradients  ***
2426      !!
2427      !! ** Purpose :   Calculates horizontal gradients of buoyancy for use with Fox-Kemper parametrization.
2428      !!
2429      !! ** Method  :
2430      !!
2431      !! References: Fox-Kemper et al., JPO, 38, 1145-1165, 2008
2432      !!             Fox-Kemper and Ferrari, JPO, 38, 1166-1179, 2008
2433
2434
2435      REAL(wp), DIMENSION(jpi,jpj)     :: dbdx_mle, dbdy_mle ! MLE horiz gradients at u & v points
2436      REAL(wp), DIMENSION(jpi,jpj)     :: zdbds_mle ! Magnitude of horizontal buoyancy gradient.
2437      REAL(wp), DIMENSION(jpi,jpj)     :: zmld ! ==  estimated FK BLD used for MLE horiz gradients  == !
2438      REAL(wp), DIMENSION(jpi,jpj)     :: zdtdx, zdtdy, zdsdx, zdsdy
2439
2440      INTEGER  ::   ji, jj, jk          ! dummy loop indices
2441      INTEGER  ::   ii, ij, ik, ikmax   ! local integers
2442      REAL(wp)                         :: zc
2443      REAL(wp)                         :: zN2_c           ! local buoyancy difference from 10m value
2444      REAL(wp), DIMENSION(jpi,jpj)     :: ztm, zsm, zLf_NH, zLf_MH
2445      REAL(wp), DIMENSION(jpi,jpj,jpts):: ztsm_midu, ztsm_midv, zabu, zabv
2446      REAL(wp), DIMENSION(jpi,jpj)     :: zmld_midu, zmld_midv
2447!!----------------------------------------------------------------------
2448      !
2449      !                                      !==  MLD used for MLE  ==!
2450
2451      mld_prof(:,:)  = nlb10               ! Initialization to the number of w ocean point
2452      zmld(:,:)  = 0._wp               ! here hmlp used as a dummy variable, integrating vertically N^2
2453      zN2_c = grav * rn_osm_mle_rho_c * r1_rau0   ! convert density criteria into N^2 criteria
2454      DO jk = nlb10, jpkm1
2455         DO jj = 1, jpj                ! Mixed layer level: w-level
2456            DO ji = 1, jpi
2457               ikt = mbkt(ji,jj)
2458               zmld(ji,jj) = zmld(ji,jj) + MAX( rn2b(ji,jj,jk) , 0._wp ) * e3w_n(ji,jj,jk)
2459               IF( zmld(ji,jj) < zN2_c )   mld_prof(ji,jj) = MIN( jk , ikt ) + 1   ! Mixed layer level
2460            END DO
2461         END DO
2462      END DO
2463      DO jj = 1, jpj
2464         DO ji = 1, jpi
2465            mld_prof(ji,jj) = MAX(mld_prof(ji,jj),ibld(ji,jj))
2466            zmld(ji,jj) = gdepw_n(ji,jj,mld_prof(ji,jj))
2467         END DO
2468      END DO
2469      ! ensure mld_prof .ge. ibld
2470      !
2471      ikmax = MIN( MAXVAL( mld_prof(:,:) ), jpkm1 )                  ! max level of the computation
2472      !
2473      ztm(:,:) = 0._wp
2474      zsm(:,:) = 0._wp
2475      DO jk = 1, ikmax                                 ! MLD and mean buoyancy and N2 over the mixed layer
2476         DO jj = 1, jpj
2477            DO ji = 1, jpi
2478               zc = e3t_n(ji,jj,jk) * REAL( MIN( MAX( 0, mld_prof(ji,jj)-jk ) , 1  )  )    ! zc being 0 outside the ML t-points
2479               ztm(ji,jj) = ztm(ji,jj) + zc * tsn(ji,jj,jk,jp_tem)
2480               zsm(ji,jj) = zsm(ji,jj) + zc * tsn(ji,jj,jk,jp_sal)
2481            END DO
2482         END DO
2483      END DO
2484      ! average temperature and salinity.
2485      ztm(:,:) = ztm(:,:) / MAX( e3t_n(:,:,1), zmld(:,:) )
2486      zsm(:,:) = zsm(:,:) / MAX( e3t_n(:,:,1), zmld(:,:) )
2487      ! calculate horizontal gradients at u & v points
2488
2489      DO jj = 2, jpjm1
2490         DO ji = 1, jpim1
2491            zdtdx(ji,jj) = ( ztm(ji+1,jj) - ztm( ji,jj) )  * umask(ji,jj,1) / e1u(ji,jj)
2492            zdsdx(ji,jj) = ( zsm(ji+1,jj) - zsm( ji,jj) )  * umask(ji,jj,1) / e1u(ji,jj)
2493            zmld_midu(ji,jj) = 0.25_wp * (zmld(ji+1,jj) + zmld( ji,jj))
2494            ztsm_midu(ji,jj,jp_tem) = 0.5_wp * ( ztm(ji+1,jj) + ztm( ji,jj) )
2495            ztsm_midu(ji,jj,jp_sal) = 0.5_wp * ( zsm(ji+1,jj) + zsm( ji,jj) )
2496         END DO
2497      END DO
2498
2499      DO jj = 1, jpjm1
2500         DO ji = 2, jpim1
2501            zdtdy(ji,jj) = ( ztm(ji,jj+1) - ztm( ji,jj) ) * vmask(ji,jj,1) / e1v(ji,jj)
2502            zdsdy(ji,jj) = ( zsm(ji,jj+1) - zsm( ji,jj) ) * vmask(ji,jj,1) / e1v(ji,jj)
2503            zmld_midv(ji,jj) = 0.25_wp * (zmld(ji,jj+1) + zmld( ji,jj))
2504            ztsm_midv(ji,jj,jp_tem) = 0.5_wp * ( ztm(ji,jj+1) + ztm( ji,jj) )
2505            ztsm_midv(ji,jj,jp_sal) = 0.5_wp * ( zsm(ji,jj+1) + zsm( ji,jj) )
2506         END DO
2507      END DO
2508
2509      ! ensure salinity > 0 in unset values so EOS doesn't give FP error with fpe0 on
2510      ztsm_midu(:,jpj,jp_sal) = 10.
2511      ztsm_midv(jpi,:,jp_sal) = 10.
2512
2513      CALL eos_rab(ztsm_midu, zmld_midu, zabu)
2514      CALL eos_rab(ztsm_midv, zmld_midv, zabv)
2515
2516      DO jj = 2, jpjm1
2517         DO ji = 1, jpim1
2518            dbdx_mle(ji,jj) = grav*(zdtdx(ji,jj)*zabu(ji,jj,jp_tem) - zdsdx(ji,jj)*zabu(ji,jj,jp_sal))
2519         END DO
2520      END DO
2521      DO jj = 1, jpjm1
2522         DO ji = 2, jpim1
2523            dbdy_mle(ji,jj) = grav*(zdtdy(ji,jj)*zabv(ji,jj,jp_tem) - zdsdy(ji,jj)*zabv(ji,jj,jp_sal))
2524         END DO
2525      END DO
2526
2527      DO jj = 2, jpjm1
2528        DO ji = 2, jpim1
2529           ztmp =  r1_ft(ji,jj) *  MIN( 111.e3_wp , e1u(ji,jj) ) / rn_osm_mle_lf
2530           zdbds_mle(ji,jj) = SQRT( 0.5_wp * ( dbdx_mle(ji,jj) * dbdx_mle(ji,jj) + dbdy_mle(ji,jj) * dbdy_mle(ji,jj) &
2531                & + dbdx_mle(ji-1,jj) * dbdx_mle(ji-1,jj) + dbdy_mle(ji,jj-1) * dbdy_mle(ji,jj-1) ) )
2532        END DO
2533      END DO
2534     
2535 END SUBROUTINE zdf_osm_zmld_horizontal_gradients
2536  SUBROUTINE zdf_osm_mle_parameters( mld_prof, hmle, zhmle, zvel_mle, zdiff_mle )
2537      !!----------------------------------------------------------------------
2538      !!                  ***  ROUTINE zdf_osm_mle_parameters  ***
2539      !!
2540      !! ** Purpose :   Timesteps the mixed layer eddy depth, hmle and calculates the mixed layer eddy fluxes for buoyancy, heat and salinity.
2541      !!
2542      !! ** Method  :
2543      !!
2544      !! References: Fox-Kemper et al., JPO, 38, 1145-1165, 2008
2545      !!             Fox-Kemper and Ferrari, JPO, 38, 1166-1179, 2008
2546
2547      INTEGER, DIMENSION(jpi,jpj)      :: mld_prof
2548      REAL(wp), DIMENSION(jpi,jpj)     :: hmle, zhmle, zwb_fk, zvel_mle, zdiff_mle
2549      INTEGER  ::   ji, jj, jk          ! dummy loop indices
2550      INTEGER  ::   ii, ij, ik, jkb, jkb1  ! local integers
2551      INTEGER , DIMENSION(jpi,jpj)     :: inml_mle
2552      REAL(wp) ::  ztmp, zdbdz, zdtdz, zdsdz, zthermal,zbeta, zbuoy, zdb_mle
2553
2554   ! Calculate vertical buoyancy, heat and salinity fluxes due to MLE.
2555
2556      DO jj = 2, jpjm1
2557        DO ji = 2, jpim1
2558          IF ( lconv(ji,jj) ) THEN
2559             ztmp =  r1_ft(ji,jj) *  MIN( 111.e3_wp , e1u(ji,jj) ) / rn_osm_mle_lf
2560      ! This velocity scale, defined in Fox-Kemper et al (2008), is needed for calculating dhdt.
2561             zvel_mle(ji,jj) = zdbds_mle(ji,jj) * ztmp * hmle(ji,jj) * tmask(ji,jj,1)
2562             zdiff_mle(ji,jj) = 5.e-4_wp * rn_osm_mle_ce * ztmp * zdbds_mle(ji,jj) * zhmle(ji,jj)**2
2563          ENDIF
2564        END DO
2565      END DO
2566   ! Timestep mixed layer eddy depth.
2567      DO jj = 2, jpjm1
2568        DO ji = 2, jpim1
2569           IF ( lmle(ji,jj) ) THEN  ! MLE layer growing.
2570! Buoyancy gradient at base of MLE layer.
2571              zthermal = rab_n(ji,jj,1,jp_tem)
2572              zbeta    = rab_n(ji,jj,1,jp_sal)
2573              jkb = mld_prof(ji,jj)
2574              jkb1 = MIN(jkb + 1, mbkt(ji,jj))
2575!             
2576              zbuoy = grav * ( zthermal * tsn(ji,jj,mld_prof(ji,jj)+2,jp_tem) - zbeta * tsn(ji,jj,mld_prof(ji,jj)+2,jp_sal) )
2577              zdb_mle = zb_bl(ji,jj) - zbuoy 
2578! Timestep hmle.
2579              hmle(ji,jj) = hmle(ji,jj) + zwb0(ji,jj) * rn_rdt / zdb_mle
2580           ELSE
2581              IF ( zhmle(ji,jj) > zhbl(ji,jj) ) THEN
2582                 hmle(ji,jj) = hmle(ji,jj) - ( hmle(ji,jj) - hbl(ji,jj) ) * rn_rdt / rn_osm_mle_tau
2583              ELSE
2584                 hmle(ji,jj) = hmle(ji,jj) - 10.0 * ( hmle(ji,jj) - hbl(ji,jj) ) * rn_rdt /rn_osm_mle_tau
2585              ENDIF
2586           ENDIF
2587           hmle(ji,jj) = MIN(hmle(ji,jj), ht_n(ji,jj))
2588          IF(ln_osm_hmle_limit) hmle(ji,jj) = MIN(hmle(ji,jj), MAX(rn_osm_hmle_limit,1.2*hbl(ji,jj)) )
2589        END DO
2590      END DO
2591
2592      mld_prof = 4
2593      DO jk = 5, jpkm1
2594        DO jj = 2, jpjm1
2595          DO ji = 2, jpim1
2596            IF ( hmle(ji,jj) >= gdepw_n(ji,jj,jk) ) mld_prof(ji,jj) = MIN(mbkt(ji,jj), jk)
2597          END DO
2598        END DO
2599      END DO
2600      DO jj = 2, jpjm1
2601         DO ji = 2, jpim1
2602            zhmle(ji,jj) = gdepw_n(ji,jj, mld_prof(ji,jj))
2603         END DO
2604       END DO
2605END SUBROUTINE zdf_osm_mle_parameters
2606
2607END SUBROUTINE zdf_osm
2608
2609
2610   SUBROUTINE zdf_osm_init
2611     !!----------------------------------------------------------------------
2612     !!                  ***  ROUTINE zdf_osm_init  ***
2613     !!
2614     !! ** Purpose :   Initialization of the vertical eddy diffivity and
2615     !!      viscosity when using a osm turbulent closure scheme
2616     !!
2617     !! ** Method  :   Read the namosm namelist and check the parameters
2618     !!      called at the first timestep (nit000)
2619     !!
2620     !! ** input   :   Namlist namosm
2621     !!----------------------------------------------------------------------
2622     INTEGER  ::   ios            ! local integer
2623     INTEGER  ::   ji, jj, jk     ! dummy loop indices
2624     REAL z1_t2
2625     !!
2626     NAMELIST/namzdf_osm/ ln_use_osm_la, rn_osm_la, rn_osm_dstokes, nn_ave &
2627          & ,nn_osm_wave, ln_dia_osm, rn_osm_hbl0, rn_zdfosm_adjust_sd &
2628          & ,ln_kpprimix, rn_riinfty, rn_difri, ln_convmix, rn_difconv, nn_osm_wave &
2629          & ,nn_osm_SD_reduce, ln_osm_mle, rn_osm_hblfrac, rn_osm_bl_thresh, ln_zdfosm_ice_shelter
2630! Namelist for Fox-Kemper parametrization.
2631      NAMELIST/namosm_mle/ nn_osm_mle, rn_osm_mle_ce, rn_osm_mle_lf, rn_osm_mle_time, rn_osm_mle_lat,&
2632           & rn_osm_mle_rho_c, rn_osm_mle_thresh, rn_osm_mle_tau, ln_osm_hmle_limit, rn_osm_hmle_limit
2633
2634     !!----------------------------------------------------------------------
2635     !
2636     REWIND( numnam_ref )              ! Namelist namzdf_osm in reference namelist : Osmosis ML model
2637     READ  ( numnam_ref, namzdf_osm, IOSTAT = ios, ERR = 901)
2638901  IF( ios /= 0 ) CALL ctl_nam ( ios , 'namzdf_osm in reference namelist' )
2639
2640     REWIND( numnam_cfg )              ! Namelist namzdf_tke in configuration namelist : Turbulent Kinetic Energy
2641     READ  ( numnam_cfg, namzdf_osm, IOSTAT = ios, ERR = 902 )
2642902  IF( ios >  0 ) CALL ctl_nam ( ios , 'namzdf_osm in configuration namelist' )
2643     IF(lwm) WRITE ( numond, namzdf_osm )
2644
2645     IF(lwp) THEN                    ! Control print
2646        WRITE(numout,*)
2647        WRITE(numout,*) 'zdf_osm_init : OSMOSIS Parameterisation'
2648        WRITE(numout,*) '~~~~~~~~~~~~'
2649        WRITE(numout,*) '   Namelist namzdf_osm : set osm mixing parameters'
2650        WRITE(numout,*) '     Use  rn_osm_la                                ln_use_osm_la = ', ln_use_osm_la
2651        WRITE(numout,*) '     Use  MLE in OBL, i.e. Fox-Kemper param        ln_osm_mle = ', ln_osm_mle
2652        WRITE(numout,*) '     Turbulent Langmuir number                     rn_osm_la   = ', rn_osm_la
2653        WRITE(numout,*) '     Stokes drift reduction factor                 rn_zdfosm_adjust_sd   = ', rn_zdfosm_adjust_sd
2654        WRITE(numout,*) '     Initial hbl for 1D runs                       rn_osm_hbl0   = ', rn_osm_hbl0
2655        WRITE(numout,*) '     Depth scale of Stokes drift                   rn_osm_dstokes = ', rn_osm_dstokes
2656        WRITE(numout,*) '     horizontal average flag                       nn_ave      = ', nn_ave
2657        WRITE(numout,*) '     Stokes drift                                  nn_osm_wave = ', nn_osm_wave
2658        SELECT CASE (nn_osm_wave)
2659        CASE(0)
2660           WRITE(numout,*) '     calculated assuming constant La#=0.3'
2661        CASE(1)
2662           WRITE(numout,*) '     calculated from Pierson Moskowitz wind-waves'
2663        CASE(2)
2664           WRITE(numout,*) '     calculated from ECMWF wave fields'
2665         END SELECT
2666        WRITE(numout,*) '     Stokes drift reduction                        nn_osm_SD_reduce', nn_osm_SD_reduce
2667        WRITE(numout,*) '     fraction of hbl to average SD over/fit'
2668        WRITE(numout,*) '     exponential with nn_osm_SD_reduce = 1 or 2    rn_osm_hblfrac =  ', rn_osm_hblfrac
2669        SELECT CASE (nn_osm_SD_reduce)
2670        CASE(0)
2671           WRITE(numout,*) '     No reduction'
2672        CASE(1)
2673           WRITE(numout,*) '     Average SD over upper rn_osm_hblfrac of BL'
2674        CASE(2)
2675           WRITE(numout,*) '     Fit exponential to slope rn_osm_hblfrac of BL'
2676        END SELECT
2677        WRITE(numout,*) '     reduce surface SD and depth scale under ice   ln_zdfosm_ice_shelter=', ln_zdfosm_ice_shelter
2678        WRITE(numout,*) '     Output osm diagnostics                       ln_dia_osm  = ',  ln_dia_osm
2679        WRITE(numout,*) '         Threshold used to define BL              rn_osm_bl_thresh  = ', rn_osm_bl_thresh, 'm^2/s'
2680        WRITE(numout,*) '     Use KPP-style shear instability mixing       ln_kpprimix = ', ln_kpprimix
2681        WRITE(numout,*) '     local Richardson Number limit for shear instability rn_riinfty = ', rn_riinfty
2682        WRITE(numout,*) '     maximum shear diffusivity at Rig = 0    (m2/s) rn_difri = ', rn_difri
2683        WRITE(numout,*) '     Use large mixing below BL when unstable       ln_convmix = ', ln_convmix
2684        WRITE(numout,*) '     diffusivity when unstable below BL     (m2/s) rn_difconv = ', rn_difconv
2685     ENDIF
2686
2687
2688     !                              ! Check wave coupling settings !
2689     !                         ! Further work needed - see ticket #2447 !
2690     IF( nn_osm_wave == 2 ) THEN
2691        IF (.NOT. ( ln_wave .AND. ln_sdw )) &
2692           & CALL ctl_stop( 'zdf_osm_init : ln_zdfosm and nn_osm_wave=2, ln_wave and ln_sdw must be true' )
2693     END IF
2694
2695     !                              ! allocate zdfosm arrays
2696     IF( zdf_osm_alloc() /= 0 )   CALL ctl_stop( 'STOP', 'zdf_osm_init : unable to allocate arrays' )
2697
2698
2699     IF( ln_osm_mle ) THEN
2700! Initialise Fox-Kemper parametrization
2701         REWIND( numnam_ref )              ! Namelist namosm_mle in reference namelist : Tracer advection scheme
2702         READ  ( numnam_ref, namosm_mle, IOSTAT = ios, ERR = 903)
2703903      IF( ios /= 0 )   CALL ctl_nam ( ios , 'namosm_mle in reference namelist')
2704
2705         REWIND( numnam_cfg )              ! Namelist namosm_mle in configuration namelist : Tracer advection scheme
2706         READ  ( numnam_cfg, namosm_mle, IOSTAT = ios, ERR = 904 )
2707904      IF( ios >  0 )   CALL ctl_nam ( ios , 'namosm_mle in configuration namelist')
2708         IF(lwm) WRITE ( numond, namosm_mle )
2709
2710         IF(lwp) THEN                     ! Namelist print
2711            WRITE(numout,*)
2712            WRITE(numout,*) 'zdf_osm_init : initialise mixed layer eddy (MLE)'
2713            WRITE(numout,*) '~~~~~~~~~~~~~'
2714            WRITE(numout,*) '   Namelist namosm_mle : '
2715            WRITE(numout,*) '         MLE type: =0 standard Fox-Kemper ; =1 new formulation        nn_osm_mle    = ', nn_osm_mle
2716            WRITE(numout,*) '         magnitude of the MLE (typical value: 0.06 to 0.08)           rn_osm_mle_ce    = ', rn_osm_mle_ce
2717            WRITE(numout,*) '         scale of ML front (ML radius of deformation) (nn_osm_mle=0)      rn_osm_mle_lf     = ', rn_osm_mle_lf, 'm'
2718            WRITE(numout,*) '         maximum time scale of MLE                    (nn_osm_mle=0)      rn_osm_mle_time   = ', rn_osm_mle_time, 's'
2719            WRITE(numout,*) '         reference latitude (degrees) of MLE coef.    (nn_osm_mle=1)      rn_osm_mle_lat    = ', rn_osm_mle_lat, 'deg'
2720            WRITE(numout,*) '         Density difference used to define ML for FK              rn_osm_mle_rho_c  = ', rn_osm_mle_rho_c
2721            WRITE(numout,*) '         Threshold used to define MLE for FK                      rn_osm_mle_thresh  = ', rn_osm_mle_thresh, 'm^2/s'
2722            WRITE(numout,*) '         Timescale for OSM-FK                                         rn_osm_mle_tau  = ', rn_osm_mle_tau, 's'
2723            WRITE(numout,*) '         switch to limit hmle                                      ln_osm_hmle_limit  = ', ln_osm_hmle_limit
2724            WRITE(numout,*) '         fraction of zmld to limit hmle to if ln_osm_hmle_limit =.T.  rn_osm_hmle_limit = ', rn_osm_hmle_limit
2725         ENDIF         !
2726     ENDIF
2727      !
2728      IF(lwp) THEN
2729         WRITE(numout,*)
2730         IF( ln_osm_mle ) THEN
2731            WRITE(numout,*) '   ==>>>   Mixed Layer Eddy induced transport added to OSMOSIS BL calculation'
2732            IF( nn_osm_mle == 0 )   WRITE(numout,*) '              Fox-Kemper et al 2010 formulation'
2733            IF( nn_osm_mle == 1 )   WRITE(numout,*) '              New formulation'
2734         ELSE
2735            WRITE(numout,*) '   ==>>>   Mixed Layer induced transport NOT added to OSMOSIS BL calculation'
2736         ENDIF
2737      ENDIF
2738      !
2739      IF( ln_osm_mle ) THEN                ! MLE initialisation
2740         !
2741         rb_c = grav * rn_osm_mle_rho_c /rau0        ! Mixed Layer buoyancy criteria
2742         IF(lwp) WRITE(numout,*)
2743         IF(lwp) WRITE(numout,*) '      ML buoyancy criteria = ', rb_c, ' m/s2 '
2744         IF(lwp) WRITE(numout,*) '      associated ML density criteria defined in zdfmxl = ', rn_osm_mle_rho_c, 'kg/m3'
2745         !
2746         IF( nn_osm_mle == 0 ) THEN           ! MLE array allocation & initialisation            !
2747!
2748         ELSEIF( nn_osm_mle == 1 ) THEN           ! MLE array allocation & initialisation
2749            rc_f = rn_osm_mle_ce/ (  5.e3_wp * 2._wp * omega * SIN( rad * rn_osm_mle_lat )  )
2750            !
2751         ENDIF
2752         !                                ! 1/(f^2+tau^2)^1/2 at t-point (needed in both nn_osm_mle case)
2753         z1_t2 = 2.e-5
2754         do jj=1,jpj
2755            do ji = 1,jpi
2756               r1_ft(ji,jj) = MIN(1./( ABS(ff_t(ji,jj)) + epsln ), ABS(ff_t(ji,jj))/z1_t2**2)
2757            end do
2758         end do
2759         ! z1_t2 = 1._wp / ( rn_osm_mle_time * rn_osm_mle_timeji,jj )
2760         ! r1_ft(:,:) = 1._wp / SQRT(  ff_t(:,:) * ff_t(:,:) + z1_t2  )
2761         !
2762      ENDIF
2763
2764     call osm_rst( nit000, 'READ' ) !* read or initialize hbl, dh, hmle
2765
2766
2767     IF( ln_zdfddm) THEN
2768        IF(lwp) THEN
2769           WRITE(numout,*)
2770           WRITE(numout,*) '    Double diffusion mixing on temperature and salinity '
2771           WRITE(numout,*) '    CAUTION : done in routine zdfosm, not in routine zdfddm '
2772        ENDIF
2773     ENDIF
2774
2775
2776     !set constants not in namelist
2777     !-----------------------------
2778
2779     IF(lwp) THEN
2780        WRITE(numout,*)
2781     ENDIF
2782
2783     IF (nn_osm_wave == 0) THEN
2784        dstokes(:,:) = rn_osm_dstokes
2785     END IF
2786
2787     ! Horizontal average : initialization of weighting arrays
2788     ! -------------------
2789
2790     SELECT CASE ( nn_ave )
2791
2792     CASE ( 0 )                ! no horizontal average
2793        IF(lwp) WRITE(numout,*) '          no horizontal average on avt'
2794        IF(lwp) WRITE(numout,*) '          only in very high horizontal resolution !'
2795        ! weighting mean arrays etmean
2796        !           ( 1  1 )
2797        ! avt = 1/4 ( 1  1 )
2798        !
2799        etmean(:,:,:) = 0.e0
2800
2801        DO jk = 1, jpkm1
2802           DO jj = 2, jpjm1
2803              DO ji = 2, jpim1   ! vector opt.
2804                 etmean(ji,jj,jk) = tmask(ji,jj,jk)                     &
2805                      &  / MAX( 1.,  umask(ji-1,jj  ,jk) + umask(ji,jj,jk)   &
2806                      &            + vmask(ji  ,jj-1,jk) + vmask(ji,jj,jk)  )
2807              END DO
2808           END DO
2809        END DO
2810
2811     CASE ( 1 )                ! horizontal average
2812        IF(lwp) WRITE(numout,*) '          horizontal average on avt'
2813        ! weighting mean arrays etmean
2814        !           ( 1/2  1  1/2 )
2815        ! avt = 1/8 ( 1    2  1   )
2816        !           ( 1/2  1  1/2 )
2817        etmean(:,:,:) = 0.e0
2818
2819        DO jk = 1, jpkm1
2820           DO jj = 2, jpjm1
2821              DO ji = 2, jpim1   ! vector opt.
2822                 etmean(ji,jj,jk) = tmask(ji, jj,jk)                           &
2823                      & / MAX( 1., 2.* tmask(ji,jj,jk)                           &
2824                      &      +.5 * ( tmask(ji-1,jj+1,jk) + tmask(ji-1,jj-1,jk)   &
2825                      &             +tmask(ji+1,jj+1,jk) + tmask(ji+1,jj-1,jk) ) &
2826                      &      +1. * ( tmask(ji-1,jj  ,jk) + tmask(ji  ,jj+1,jk)   &
2827                      &             +tmask(ji  ,jj-1,jk) + tmask(ji+1,jj  ,jk) ) )
2828              END DO
2829           END DO
2830        END DO
2831
2832     CASE DEFAULT
2833        WRITE(ctmp1,*) '          bad flag value for nn_ave = ', nn_ave
2834        CALL ctl_stop( ctmp1 )
2835
2836     END SELECT
2837
2838     ! Initialization of vertical eddy coef. to the background value
2839     ! -------------------------------------------------------------
2840     DO jk = 1, jpk
2841        avt (:,:,jk) = avtb(jk) * tmask(:,:,jk)
2842     END DO
2843
2844     ! zero the surface flux for non local term and osm mixed layer depth
2845     ! ------------------------------------------------------------------
2846     ghamt(:,:,:) = 0.
2847     ghams(:,:,:) = 0.
2848     ghamu(:,:,:) = 0.
2849     ghamv(:,:,:) = 0.
2850     !
2851     IF( lwxios ) THEN
2852        CALL iom_set_rstw_var_active('wn')
2853        CALL iom_set_rstw_var_active('hbl')
2854        CALL iom_set_rstw_var_active('dh')
2855        IF( ln_osm_mle ) THEN
2856            CALL iom_set_rstw_var_active('hmle')
2857        END IF
2858     ENDIF
2859   END SUBROUTINE zdf_osm_init
2860
2861
2862   SUBROUTINE osm_rst( kt, cdrw )
2863     !!---------------------------------------------------------------------
2864     !!                   ***  ROUTINE osm_rst  ***
2865     !!
2866     !! ** Purpose :   Read or write BL fields in restart file
2867     !!
2868     !! ** Method  :   use of IOM library. If the restart does not contain
2869     !!                required fields, they are recomputed from stratification
2870     !!----------------------------------------------------------------------
2871
2872     INTEGER, INTENT(in) :: kt
2873     CHARACTER(len=*), INTENT(in) ::   cdrw   ! "READ"/"WRITE" flag
2874
2875     INTEGER ::   id1, id2, id3   ! iom enquiry index
2876     INTEGER  ::   ji, jj, jk     ! dummy loop indices
2877     INTEGER  ::   iiki, ikt ! local integer
2878     REAL(wp) ::   zhbf           ! tempory scalars
2879     REAL(wp) ::   zN2_c           ! local scalar
2880     REAL(wp) ::   rho_c = 0.01_wp    !: density criterion for mixed layer depth
2881     INTEGER, DIMENSION(jpi,jpj) :: imld_rst ! level of mixed-layer depth (pycnocline top)
2882     !!----------------------------------------------------------------------
2883     !
2884     !!-----------------------------------------------------------------------------
2885     ! If READ/WRITE Flag is 'READ', try to get hbl from restart file. If successful then return
2886     !!-----------------------------------------------------------------------------
2887     IF( TRIM(cdrw) == 'READ'.AND. ln_rstart) THEN
2888        id1 = iom_varid( numror, 'wn'   , ldstop = .FALSE. )
2889        IF( id1 > 0 ) THEN                       ! 'wn' exists; read
2890           CALL iom_get( numror, jpdom_autoglo, 'wn', wn, ldxios = lrxios )
2891           WRITE(numout,*) ' ===>>>> :  wn read from restart file'
2892        ELSE
2893           wn(:,:,:) = 0._wp
2894           WRITE(numout,*) ' ===>>>> :  wn not in restart file, set to zero initially'
2895        END IF
2896
2897        id1 = iom_varid( numror, 'hbl'   , ldstop = .FALSE. )
2898        id2 = iom_varid( numror, 'dh'   , ldstop = .FALSE. )
2899        IF( id1 > 0 .AND. id2 > 0) THEN                       ! 'hbl' exists; read and return
2900           CALL iom_get( numror, jpdom_autoglo, 'hbl' , hbl , ldxios = lrxios )
2901           CALL iom_get( numror, jpdom_autoglo, 'dh', dh, ldxios = lrxios  )
2902           WRITE(numout,*) ' ===>>>> :  hbl & dh read from restart file'
2903           IF( ln_osm_mle ) THEN
2904              id3 = iom_varid( numror, 'hmle'   , ldstop = .FALSE. )
2905              IF( id3 > 0) THEN
2906                 CALL iom_get( numror, jpdom_autoglo, 'hmle' , hmle , ldxios = lrxios )
2907                 WRITE(numout,*) ' ===>>>> :  hmle read from restart file'
2908              ELSE
2909                 WRITE(numout,*) ' ===>>>> :  hmle not found, set to hbl'
2910                 hmle(:,:) = hbl(:,:)            ! Initialise MLE depth.
2911              END IF
2912           END IF
2913           RETURN
2914        ELSE                      ! 'hbl' & 'dh' not in restart file, recalculate
2915           WRITE(numout,*) ' ===>>>> : previous run without osmosis scheme, hbl computed from stratification'
2916        END IF
2917     END IF
2918
2919     !!-----------------------------------------------------------------------------
2920     ! If READ/WRITE Flag is 'WRITE', write hbl into the restart file, then return
2921     !!-----------------------------------------------------------------------------
2922     IF( TRIM(cdrw) == 'WRITE') THEN     !* Write hbli into the restart file, then return
2923        IF(lwp) WRITE(numout,*) '---- osm-rst ----'
2924         CALL iom_rstput( kt, nitrst, numrow, 'wn'     , wn,   ldxios = lwxios )
2925         CALL iom_rstput( kt, nitrst, numrow, 'hbl'    , hbl,  ldxios = lwxios )
2926         CALL iom_rstput( kt, nitrst, numrow, 'dh'     , dh,   ldxios = lwxios )
2927         IF( ln_osm_mle ) THEN
2928            CALL iom_rstput( kt, nitrst, numrow, 'hmle', hmle, ldxios = lwxios )
2929         END IF
2930        RETURN
2931     END IF
2932
2933     !!-----------------------------------------------------------------------------
2934     ! Getting hbl, no restart file with hbl, so calculate from surface stratification
2935     !!-----------------------------------------------------------------------------
2936     IF( lwp ) WRITE(numout,*) ' ===>>>> : calculating hbl computed from stratification'
2937     ! w-level of the mixing and mixed layers
2938     CALL eos_rab( tsn, rab_n )
2939     CALL bn2(tsn, rab_n, rn2)
2940     imld_rst(:,:)  = nlb10         ! Initialization to the number of w ocean point
2941     hbl(:,:)  = 0._wp              ! here hbl used as a dummy variable, integrating vertically N^2
2942     zN2_c = grav * rho_c * r1_rau0 ! convert density criteria into N^2 criteria
2943     !
2944     hbl(:,:)  = 0._wp              ! here hbl used as a dummy variable, integrating vertically N^2
2945     DO jk = 1, jpkm1
2946        DO jj = 1, jpj              ! Mixed layer level: w-level
2947           DO ji = 1, jpi
2948              ikt = mbkt(ji,jj)
2949              hbl(ji,jj) = hbl(ji,jj) + MAX( rn2(ji,jj,jk) , 0._wp ) * e3w_n(ji,jj,jk)
2950              IF( hbl(ji,jj) < zN2_c )   imld_rst(ji,jj) = MIN( jk , ikt ) + 1   ! Mixed layer level
2951           END DO
2952        END DO
2953     END DO
2954     !
2955     DO jj = 1, jpj
2956        DO ji = 1, jpi
2957           iiki = MAX(4,imld_rst(ji,jj))
2958           hbl (ji,jj) = gdepw_n(ji,jj,iiki  )    ! Turbocline depth
2959           dh (ji,jj) = e3t_n(ji,jj,iiki-1  )     ! Turbocline depth
2960        END DO
2961     END DO
2962
2963     WRITE(numout,*) ' ===>>>> : hbl computed from stratification'
2964
2965     IF( ln_osm_mle ) THEN
2966        hmle(:,:) = hbl(:,:)            ! Initialise MLE depth.
2967        WRITE(numout,*) ' ===>>>> : hmle set = to hbl'
2968     END IF
2969
2970     wn(:,:,:) = 0._wp
2971     WRITE(numout,*) ' ===>>>> :  wn not in restart file, set to zero initially'
2972   END SUBROUTINE osm_rst
2973
2974
2975   SUBROUTINE tra_osm( kt )
2976      !!----------------------------------------------------------------------
2977      !!                  ***  ROUTINE tra_osm  ***
2978      !!
2979      !! ** Purpose :   compute and add to the tracer trend the non-local tracer flux
2980      !!
2981      !! ** Method  :   ???
2982      !!----------------------------------------------------------------------
2983      REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::   ztrdt, ztrds   ! 3D workspace
2984      !!----------------------------------------------------------------------
2985      INTEGER, INTENT(in) :: kt
2986      INTEGER :: ji, jj, jk
2987      !
2988      IF( kt == nit000 ) THEN
2989         IF(lwp) WRITE(numout,*)
2990         IF(lwp) WRITE(numout,*) 'tra_osm : OSM non-local tracer fluxes'
2991         IF(lwp) WRITE(numout,*) '~~~~~~~   '
2992      ENDIF
2993
2994      IF( l_trdtra )   THEN                    !* Save ta and sa trends
2995         ALLOCATE( ztrdt(jpi,jpj,jpk) )   ;    ztrdt(:,:,:) = tsa(:,:,:,jp_tem)
2996         ALLOCATE( ztrds(jpi,jpj,jpk) )   ;    ztrds(:,:,:) = tsa(:,:,:,jp_sal)
2997      ENDIF
2998
2999      DO jk = 1, jpkm1
3000         DO jj = 2, jpjm1
3001            DO ji = 2, jpim1
3002               tsa(ji,jj,jk,jp_tem) =  tsa(ji,jj,jk,jp_tem)                      &
3003                  &                 - (  ghamt(ji,jj,jk  )  &
3004                  &                    - ghamt(ji,jj,jk+1) ) /e3t_n(ji,jj,jk)
3005               tsa(ji,jj,jk,jp_sal) =  tsa(ji,jj,jk,jp_sal)                      &
3006                  &                 - (  ghams(ji,jj,jk  )  &
3007                  &                    - ghams(ji,jj,jk+1) ) / e3t_n(ji,jj,jk)
3008            END DO
3009         END DO
3010      END DO
3011
3012      ! save the non-local tracer flux trends for diagnostics
3013      IF( l_trdtra )   THEN
3014         ztrdt(:,:,:) = tsa(:,:,:,jp_tem) - ztrdt(:,:,:)
3015         ztrds(:,:,:) = tsa(:,:,:,jp_sal) - ztrds(:,:,:)
3016
3017         CALL trd_tra( kt, 'TRA', jp_tem, jptra_osm, ztrdt )
3018         CALL trd_tra( kt, 'TRA', jp_sal, jptra_osm, ztrds )
3019         DEALLOCATE( ztrdt )      ;     DEALLOCATE( ztrds )
3020      ENDIF
3021
3022      IF(ln_ctl) THEN
3023         CALL prt_ctl( tab3d_1=tsa(:,:,:,jp_tem), clinfo1=' osm  - Ta: ', mask1=tmask,   &
3024         &             tab3d_2=tsa(:,:,:,jp_sal), clinfo2=       ' Sa: ', mask2=tmask, clinfo3='tra' )
3025      ENDIF
3026      !
3027   END SUBROUTINE tra_osm
3028
3029
3030   SUBROUTINE trc_osm( kt )          ! Dummy routine
3031      !!----------------------------------------------------------------------
3032      !!                  ***  ROUTINE trc_osm  ***
3033      !!
3034      !! ** Purpose :   compute and add to the passive tracer trend the non-local
3035      !!                 passive tracer flux
3036      !!
3037      !!
3038      !! ** Method  :   ???
3039      !!----------------------------------------------------------------------
3040      !
3041      !!----------------------------------------------------------------------
3042      INTEGER, INTENT(in) :: kt
3043      WRITE(*,*) 'trc_osm: Not written yet', kt
3044   END SUBROUTINE trc_osm
3045
3046
3047   SUBROUTINE dyn_osm( kt )
3048      !!----------------------------------------------------------------------
3049      !!                  ***  ROUTINE dyn_osm  ***
3050      !!
3051      !! ** Purpose :   compute and add to the velocity trend the non-local flux
3052      !! copied/modified from tra_osm
3053      !!
3054      !! ** Method  :   ???
3055      !!----------------------------------------------------------------------
3056      INTEGER, INTENT(in) ::   kt   !
3057      !
3058      INTEGER :: ji, jj, jk   ! dummy loop indices
3059      !!----------------------------------------------------------------------
3060      !
3061      IF( kt == nit000 ) THEN
3062         IF(lwp) WRITE(numout,*)
3063         IF(lwp) WRITE(numout,*) 'dyn_osm : OSM non-local velocity'
3064         IF(lwp) WRITE(numout,*) '~~~~~~~   '
3065      ENDIF
3066      !code saving tracer trends removed, replace with trdmxl_oce
3067
3068      DO jk = 1, jpkm1           ! add non-local u and v fluxes
3069         DO jj = 2, jpjm1
3070            DO ji = 2, jpim1
3071               ua(ji,jj,jk) =  ua(ji,jj,jk)                      &
3072                  &                 - (  ghamu(ji,jj,jk  )  &
3073                  &                    - ghamu(ji,jj,jk+1) ) / e3u_n(ji,jj,jk)
3074               va(ji,jj,jk) =  va(ji,jj,jk)                      &
3075                  &                 - (  ghamv(ji,jj,jk  )  &
3076                  &                    - ghamv(ji,jj,jk+1) ) / e3v_n(ji,jj,jk)
3077            END DO
3078         END DO
3079      END DO
3080      !
3081      ! code for saving tracer trends removed
3082      !
3083   END SUBROUTINE dyn_osm
3084
3085   !!======================================================================
3086
3087END MODULE zdfosm
Note: See TracBrowser for help on using the repository browser.