Index: /NEMO/branches/2020/tickets_icb_1900/cfgs/ORCA2_ICE_PISCES/EXPREF/namelist_cfg
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/cfgs/ORCA2_ICE_PISCES/EXPREF/namelist_cfg	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/cfgs/ORCA2_ICE_PISCES/EXPREF/namelist_cfg	(revision 14016)
@@ -90,13 +90,4 @@
    !                       !    =2 annual global mean of e-p-r set to zero
    ln_wave     = .false.   !  Activate coupling with wave  (T => fill namsbc_wave)
-   ln_cdgw     = .false.   !  Neutral drag coefficient read from wave model (T => ln_wave=.true. & fill namsbc_wave)
-   ln_sdw      = .false.   !  Read 2D Surf Stokes Drift & Computation of 3D stokes drift (T => ln_wave=.true. & fill namsbc_wave) 
-   nn_sdrift   =  0        !  Parameterization for the calculation of 3D-Stokes drift from the surface Stokes drift
-      !                    !   = 0 Breivik 2015 parameterization: v_z=v_0*[exp(2*k*z)/(1-8*k*z)]
-      !                    !   = 1 Phillips:                      v_z=v_o*[exp(2*k*z)-beta*sqrt(-2*k*pi*z)*erfc(sqrt(-2*k*z))]
-      !                    !   = 2 Phillips as (1) but using the wave frequency from a wave model
-   ln_tauwoc   = .false.   !  Activate ocean stress modified by external wave induced stress (T => ln_wave=.true. & fill namsbc_wave)
-   ln_tauw     = .false.   !  Activate ocean stress components from wave model
-   ln_stcor    = .false.   !  Activate Stokes Coriolis term (T => ln_wave=.true. & ln_sdw=.true. & fill namsbc_wave)
 /
 !-----------------------------------------------------------------------
@@ -167,4 +158,27 @@
 &namsbc_wave   ! External fields from wave model                        (ln_wave=T)
 !-----------------------------------------------------------------------
+   ln_sdw      = .false.    !  get the 2D Surf Stokes Drift & Compute the 3D stokes drift
+   ln_stcor    = .false.    !  add Stokes Coriolis and tracer advection terms
+   ln_cdgw     = .false.    !  Neutral drag coefficient read from wave model
+   ln_tauoc    = .false.    !  ocean stress is modified by wave induced stress
+   ln_wave_test= .false.    !  Test case with constant wave fields
+!
+   ln_charn    = .false.     !  Charnock coefficient read from wave model (IFS only)
+   ln_taw      = .false.     !  ocean stress is modified by wave induced stress (coupled mode)
+   ln_phioc    = .false.     !  TKE flux from wave model
+   ln_bern_srfc= .false.     ! wave induced pressure. Bernoulli head J term
+   ln_breivikFV_2016 = .false. ! breivik 2016 vertical stokes profile
+   ln_vortex_force = .false.
+!
+   cn_dir      = './'      !  root directory for the waves data location
+   !___________!_________________________!___________________!___________!_____________!________!___________!__________________!__________!_______________!
+   !           !  file name              ! frequency (hours) ! variable  ! time interp.!  clim  ! 'yearly'/ ! weights filename ! rotation ! land/sea mask !
+   !           !                         !  (if <0  months)  !   name    !   (logical) !  (T/F) ! 'monthly' !                  ! pairing  !    filename   !
+   sn_cdg      =  'sdw_ecwaves_orca2'    ,        6.         , 'drag_coeff' ,  .true.  , .true. , 'yearly'  ,  ''              , ''       , ''
+   sn_usd      =  'sdw_ecwaves_orca2'    ,        6.         , 'u_sd2d'     ,  .true.  , .true. , 'yearly'  ,  ''              , ''       , ''
+   sn_vsd      =  'sdw_ecwaves_orca2'    ,        6.         , 'v_sd2d'     ,  .true.  , .true. , 'yearly'  ,  ''              , ''       , ''
+   sn_hsw      =  'sdw_ecwaves_orca2'    ,        6.         , 'hs'         ,  .true.  , .true. , 'yearly'  ,  ''              , ''       , ''
+   sn_wmp      =  'sdw_ecwaves_orca2'    ,        6.         , 'wmp'        ,  .true.  , .true. , 'yearly'  ,  ''              , ''       , ''
+   sn_wnum     =  'sdw_ecwaves_orca2'    ,        6.         , 'wave_num'   ,  .true.  , .true. , 'yearly'  ,  ''              , ''       , ''
 /
 !-----------------------------------------------------------------------
@@ -378,4 +392,5 @@
                                !        = 2 add a tke source just at the base of the ML
                                !        = 3 as = 1 applied on HF part of the stress           (ln_cpl=T)
+      ln_mxhsw    = .false.   !  surface mixing length scale = F(wave height)
 /
 !-----------------------------------------------------------------------
Index: /NEMO/branches/2020/tickets_icb_1900/cfgs/SHARED/field_def_nemo-ice.xml
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/cfgs/SHARED/field_def_nemo-ice.xml	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/cfgs/SHARED/field_def_nemo-ice.xml	(revision 14016)
@@ -51,4 +51,8 @@
           <field id="icehlid"      long_name="melt pond lid depth"                                     standard_name="sea_ice_meltpondlid_depth"                 unit="m" /> 
           <field id="icevlid"      long_name="melt pond lid volume"                                    standard_name="sea_ice_meltpondlid_volume"                unit="m" /> 
+          <field id="dvpn_mlt"     long_name="pond volume tendency due to surface melt"                standard_name="sea_ice_pondvolume_tendency_melt"          unit="kg/m2/s" />
+          <field id="dvpn_lid"     long_name="pond volume tendency due to exchanges with lid"          standard_name="sea_ice_pondvolume_tendency_lids"          unit="kg/m2/s" />
+          <field id="dvpn_rnf"     long_name="pond volume tendency due to runoff"                      standard_name="sea_ice_pondvolume_tendency_runoff"        unit="kg/m2/s" />
+          <field id="dvpn_drn"     long_name="pond volume tendency due to drainage"                    standard_name="sea_ice_pondvolume_tendency_drainage"      unit="kg/m2/s" />  
 	  
 	  <!-- heat -->
@@ -77,4 +81,5 @@
           <field id="sig1_pnorm"   long_name="P-normalized 1st principal stress component"                                                                       unit=""     />
           <field id="sig2_pnorm"   long_name="P-normalized 2nd principal stress component"                                                                       unit=""     />
+          <field id="icedlt"       long_name="delta"                                                   standard_name="delta"                                     unit=""     />
           <field id="normstr"      long_name="Average normal stress in sea ice"                        standard_name="average_normal_stress"                     unit="N/m"  />
           <field id="sheastr"      long_name="Maximum shear stress in sea ice"                         standard_name="maximum_shear_stress"                      unit="N/m"  />
@@ -82,4 +87,8 @@
           <field id="icediv"       long_name="Divergence of the sea-ice velocity field"                standard_name="divergence_of_sea_ice_velocity"            unit="s-1"  />
           <field id="iceshe"       long_name="Maximum shear of sea-ice velocity field"                 standard_name="maximum_shear_of_sea_ice_velocity"         unit="s-1"  />
+          <field id="aniso"        long_name="anisotropy of sea ice floe orientation (0.5 - 1)"        standard_name="anisotropy"                                unit=""     />
+          <field id="yield11"      long_name="yield surface tensor component 11"                       standard_name="yield11"                                   unit="N/m"  />
+          <field id="yield22"      long_name="yield surface tensor component 22"                       standard_name="yield22"                                   unit="N/m"  />
+          <field id="yield12"      long_name="yield surface tensor component 12"                       standard_name="yield12"                                   unit="N/m"  />
           <field id="beta_evp"     long_name="Relaxation parameter of ice rheology (beta)"             standard_name="relaxation_parameter_of_ice_rheology"      unit=""  />	 
  
@@ -297,8 +306,9 @@
           <field id="snwtemp_cat"  long_name="Snow temperature per category"                     unit="degC"    detect_missing_value="true" />
           <field id="icettop_cat"  long_name="Ice/snow surface temperature per category"         unit="degC"    detect_missing_value="true" />
-          <field id="iceapnd_cat"  long_name="Ice melt pond concentration per category"          unit=""        /> 
+          <field id="iceapnd_cat"  long_name="Ice melt pond grid fraction per category"          unit=""        /> 
+          <field id="icevpnd_cat"  long_name="Ice melt pond volume per grid area per category"   unit="m"       />  
           <field id="icehpnd_cat"  long_name="Ice melt pond thickness per category"              unit="m"       detect_missing_value="true" /> 
           <field id="icehlid_cat"  long_name="Ice melt pond lid thickness per category"          unit="m"       detect_missing_value="true" /> 
-          <field id="iceafpnd_cat" long_name="Ice melt pond fraction per category"               unit=""        /> 
+          <field id="iceafpnd_cat" long_name="Ice melt pond ice fraction per category"           unit=""        /> 
           <field id="iceaepnd_cat" long_name="Ice melt pond effective fraction per category"     unit=""        /> 
           <field id="icemask_cat"  long_name="Fraction of time step with sea ice (per category)" unit=""        />
@@ -405,4 +415,5 @@
 	  <field field_ref="sig1_pnorm"       name="sig1_pnorm"/>
 	  <field field_ref="sig2_pnorm"       name="sig2_pnorm"/>
+	  <field field_ref="icedlt"           name="sidelta" />
 	  
 	  <!-- heat fluxes -->
Index: /NEMO/branches/2020/tickets_icb_1900/cfgs/SHARED/field_def_nemo-oce.xml
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/cfgs/SHARED/field_def_nemo-oce.xml	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/cfgs/SHARED/field_def_nemo-oce.xml	(revision 14016)
@@ -234,4 +234,9 @@
         <field id="cfl_cw"       long_name="w-courant number"   unit="#" />
 
+        <!-- variables available with ln_zdfmfc=.true. -->
+         <field id="mf_Tp"       long_name="plume_temperature"      standard_name="plume_temperature"     unit="degC"   grid_ref="grid_T_3D" />
+         <field id="mf_Sp"       long_name="plume_salinity"         standard_name="plume_salinity"        unit="1e-3"   grid_ref="grid_T_3D" />
+         <field id="mf_mf"       long_name="mass flux"              standard_name="mf_mass_flux"          unit="m"      grid_ref="grid_T_3D" />
+
       </field_group> <!-- grid_T -->
 
@@ -649,4 +654,9 @@
 	<field id="avm_evd"      long_name="convective enhancement of vertical viscosity"   standard_name="ocean_vertical_momentum_diffusivity_due_to_convection"   unit="m2/s" />
 
+        <!-- mf_app and mf_wp: available with ln_zdfmfc -->
+         <field id="mf_app"      long_name="convective area"        standard_name="mf_convective_area"    unit="%"      grid_ref="grid_W_3D" />
+         <field id="mf_wp"       long_name="convective velocity"    standard_name="mf_convective_velo"    unit="m/s"    grid_ref="grid_W_3D" />
+
+
         <!-- avt_tide: available with ln_zdfiwm=T -->
         <field id="av_ratio"     long_name="S over T diffusivity ratio"                     standard_name="salinity_over_temperature_diffusivity_ratio"                     unit="1"    />
Index: /NEMO/branches/2020/tickets_icb_1900/cfgs/SHARED/namelist_ice_ref
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/cfgs/SHARED/namelist_ice_ref	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/cfgs/SHARED/namelist_ice_ref	(revision 14016)
@@ -24,5 +24,5 @@
    jpl              =   5             !  number of ice  categories
    nlay_i           =   2             !  number of ice  layers
-   nlay_s           =   1             !  number of snow layers (only 1 is working)
+   nlay_s           =   2             !  number of snow layers
    ln_virtual_itd   =   .false.       !  virtual ITD mono-category parameterization (jpl=1 only)
                                       !     i.e. enhanced thermal conductivity & virtual thin ice melting
@@ -62,4 +62,10 @@
       rn_lf_relax   =   1.e-5         !        relaxation time scale to reach static friction [s-1]
       rn_lf_tensile =   0.05          !        isotropic tensile strength [0-0.5??]
+
+   cn_dir           = './'      !  root directory for the grounded icebergs mask data location
+   !___________!________________!___________________!___________!_____________!________!___________!__________!__________!_______________!
+   !           !  file name     ! frequency (hours) ! variable  ! time interp.!  clim  ! 'yearly'/ ! weights  ! rotation ! land/sea mask !
+   !           !                !  (if <0  months)  !   name    !   (logical) !  (T/F) ! 'monthly' ! filename ! pairing  !    filename   !
+   sn_icbmsk       = 'NOT USED' ,       -12.        , 'icb_mask',   .false.   , .true. , 'yearly'  , ''       , ''       , ''
 /
 !------------------------------------------------------------------------------
@@ -92,4 +98,5 @@
 !------------------------------------------------------------------------------
    ln_rhg_EVP       = .true.          !  EVP rheology
+   ln_rhg_EAP       = .false.         !  EAP rheology
       ln_aEVP       = .true.          !     adaptive rheology (Kimmritz et al. 2016 & 2017)
       rn_creepl     =   2.0e-9        !     creep limit [1/s]
@@ -98,9 +105,13 @@
       rn_relast     =   0.333         !     ratio of elastic timescale to ice time step: Telast = dt_ice * rn_relast 
                                       !        advised value: 1/3 (nn_nevp=100) or 1/9 (nn_nevp=300)
-   nn_rhg_chkcvg    =   0             !  check convergence of rheology
+      nn_rhg_chkcvg =   0             !     check convergence of rheology
                                       !     = 0  no check
                                       !     = 1  check at the main time step (output xml: uice_cvg)
                                       !     = 2  check at both main and rheology time steps (additional output: ice_cvg.nc)
                                       !          this option 2 asks a lot of communications between cpu
+   ln_rhg_VP        = .false.         !  VP rheology
+      nn_vp_nout    = 10              !     number of outer iterations
+      nn_vp_ninn    = 1500            !     number of inner iterations
+      nn_vp_chkcvg  = 5               !     iteration step for convergence check
 /
 !------------------------------------------------------------------------------
@@ -195,7 +206,9 @@
 !------------------------------------------------------------------------------
    ln_pnd            = .true.         !  activate melt ponds or not
-      ln_pnd_LEV     = .true.         !  level ice melt ponds (from Flocco et al 2007,2010 & Holland et al 2012)
-         rn_apnd_min =   0.15         !     minimum ice fraction that contributes to melt pond. range: 0.0 -- 0.15 ??
-         rn_apnd_max =   0.85         !     maximum ice fraction that contributes to melt pond. range: 0.7 -- 0.85 ??
+      ln_pnd_TOPO    = .false.        !  topographic melt ponds
+      ln_pnd_LEV     = .true.         !  level ice melt ponds
+         rn_apnd_min =   0.15         !     minimum meltwater fraction contributing to pond growth (TOPO and LEV)
+         rn_apnd_max =   0.85         !     maximum meltwater fraction contributing to pond growth (TOPO and LEV)
+         rn_pnd_flush=   0.01         !     pond flushing efficiency (tuning parameter) (LEV)
       ln_pnd_CST     = .false.        !  constant  melt ponds
          rn_apnd     =   0.2          !     prescribed pond fraction, at Tsu=0 degC
@@ -261,6 +274,6 @@
    ln_icediachk     = .false.         !  check online heat, mass & salt budgets
       !                               !   rate of ice spuriously gained/lost at each time step => rn_icechk=1 <=> 1.e-6 m/hour
-      rn_icechk_cel =  100.           !     check at each gridcell          (1.e-4m/h)=> stops the code if violated (and writes a file)
-      rn_icechk_glo =  1.             !     check over the entire ice cover (1.e-6m/h)=> only prints warnings
+      rn_icechk_cel =  1.             !     check at each gridcell          (1.e-06m/h)=> stops the code if violated (and writes a file)
+      rn_icechk_glo =  1.e-04         !     check over the entire ice cover (1.e-10m/h)=> only prints warnings
    ln_icediahsb     = .false.         !  output the heat, mass & salt budgets (T) or not (F)
    ln_icectl        = .false.         !  ice points output for debug (T or F)
Index: /NEMO/branches/2020/tickets_icb_1900/cfgs/SHARED/namelist_ref
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/cfgs/SHARED/namelist_ref	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/cfgs/SHARED/namelist_ref	(revision 14016)
@@ -237,13 +237,4 @@
    ln_apr_dyn  = .false.   !  Patm gradient added in ocean & ice Eqs.   (T => fill namsbc_apr )
    ln_wave     = .false.   !  Activate coupling with wave  (T => fill namsbc_wave)
-   ln_cdgw     = .false.   !  Neutral drag coefficient read from wave model (T => ln_wave=.true. & fill namsbc_wave)
-   ln_sdw      = .false.   !  Read 2D Surf Stokes Drift & Computation of 3D stokes drift (T => ln_wave=.true. & fill namsbc_wave)
-   nn_sdrift   =  0        !  Parameterization for the calculation of 3D-Stokes drift from the surface Stokes drift
-      !                    !   = 0 Breivik 2015 parameterization: v_z=v_0*[exp(2*k*z)/(1-8*k*z)]
-      !                    !   = 1 Phillips:                      v_z=v_o*[exp(2*k*z)-beta*sqrt(-2*k*pi*z)*erfc(sqrt(-2*k*z))]
-      !                    !   = 2 Phillips as (1) but using the wave frequency from a wave model
-   ln_tauwoc   = .false.   !  Activate ocean stress modified by external wave induced stress (T => ln_wave=.true. & fill namsbc_wave)
-   ln_tauw     = .false.   !  Activate ocean stress components from wave model
-   ln_stcor    = .false.   !  Activate Stokes Coriolis term (T => ln_wave=.true. & ln_sdw=.true. & fill namsbc_wave)
    nn_lsm      = 0         !  =0 land/sea mask for input fields is not applied (keep empty land/sea mask filename field) ,
                            !  =1:n number of iterations of land/sea mask application for input fields (fill land/sea mask filename field)
@@ -376,20 +367,22 @@
    sn_rcv_cal    =   'coupled'              ,    'no'    ,     ''      ,         ''           ,   ''
    sn_rcv_co2    =   'coupled'              ,    'no'    ,     ''      ,         ''           ,   ''
-   sn_rcv_hsig   =   'none'                 ,    'no'    ,     ''      ,         ''           ,   ''
    sn_rcv_iceflx =   'none'                 ,    'no'    ,     ''      ,         ''           ,   ''
    sn_rcv_mslp   =   'none'                 ,    'no'    ,     ''      ,         ''           ,   ''
-   sn_rcv_phioc  =   'none'                 ,    'no'    ,     ''      ,         ''           ,   ''
-   sn_rcv_sdrfx  =   'none'                 ,    'no'    ,     ''      ,         ''           ,   ''
-   sn_rcv_sdrfy  =   'none'                 ,    'no'    ,     ''      ,         ''           ,   ''
-   sn_rcv_wper   =   'none'                 ,    'no'    ,     ''      ,         ''           ,   ''
-   sn_rcv_wnum   =   'none'                 ,    'no'    ,     ''      ,         ''           ,   ''
-   sn_rcv_wfreq  =   'none'                 ,    'no'    ,     ''      ,         ''           ,   ''
-   sn_rcv_wdrag  =   'none'                 ,    'no'    ,     ''      ,         ''           ,   ''
    sn_rcv_ts_ice =   'none'                 ,    'no'    ,     ''      ,         ''           ,   ''
    sn_rcv_isf    =   'none'                 ,    'no'    ,     ''      ,         ''           ,   ''
    sn_rcv_icb    =   'none'                 ,    'no'    ,     ''      ,         ''           ,   ''
-   sn_rcv_tauwoc =   'none'                 ,    'no'    ,     ''      ,         ''          ,   ''
-   sn_rcv_tauw   =   'none'                 ,    'no'    ,     ''      ,         ''          ,   ''
-   sn_rcv_wdrag  =   'none'                 ,    'no'    ,     ''      ,         ''          ,   ''
+   sn_rcv_hsig   =   'none'                 ,    'no'    ,     ''      '         ''           ,   'T'
+   sn_rcv_phioc  =   'none'                 ,    'no'    ,     ''      ,         ''           ,   'T'
+   sn_rcv_sdrfx  =   'none'                 ,    'no'    ,     ''      ,         ''           ,   'T'
+   sn_rcv_sdrfy  =   'none'                 ,    'no'    ,     ''      '         ''           ,   'T'
+   sn_rcv_wper   =   'none'                 ,    'no'    ,     ''      '         ''           ,   'T'
+   sn_rcv_wnum   =   'none'                 ,    'no'    ,     ''      '         ''           ,   'T'
+   sn_rcv_wstrf  =   'none'                 ,    'no'    ,     ''      '         ''           ,   'T'
+   sn_rcv_wdrag  =   'none'                 ,    'no'    ,     ''      '         ''           ,   'T'
+   sn_rcv_charn  =   'none'                 ,    'no'    ,     ''      ,         ''           ,   'T'
+   sn_rcv_taw    =   'none'                 ,    'no'    ,     ''      ,         ''           ,   'U,V'
+   sn_rcv_bhd    =   'none'                 ,    'no'    ,     ''      '         ''           ,   'T'
+   sn_rcv_tusd   =   'none'                 ,    'no'    ,     ''      '         ''           ,   'T'
+   sn_rcv_tvsd   =   'none'                 ,    'no'    ,     ''      '         ''           ,   'T'
 /
 !-----------------------------------------------------------------------
@@ -571,4 +564,18 @@
 &namsbc_wave   ! External fields from wave model                        (ln_wave=T)
 !-----------------------------------------------------------------------
+   ln_sdw      = .false.       !  get the 2D Surf Stokes Drift & Compute the 3D stokes drift
+   ln_stcor    = .false.       !  add Stokes Coriolis and tracer advection terms
+   ln_cdgw     = .false.       !  Neutral drag coefficient read from wave model
+   ln_tauoc    = .false.       !  ocean stress is modified by wave induced stress
+   ln_wave_test= .false.       !  Test case with constant wave fields
+!
+   ln_charn    = .false.       !  Charnock coefficient read from wave model (IFS only)
+   ln_taw      = .false.       !  ocean stress is modified by wave induced stress (coupled mode)
+   ln_phioc    = .false.       !  TKE flux from wave model
+   ln_bern_srfc= .false.       !  wave induced pressure. Bernoulli head J term
+   ln_breivikFV_2016 = .false. !  breivik 2016 vertical stokes profile
+   ln_vortex_force = .false.   !  Vortex Force term 
+   ln_stshear  = .false.       !  include stokes shear in EKE computation
+!
    cn_dir      = './'      !  root directory for the waves data location
    !___________!_________________________!___________________!___________!_____________!________!___________!__________________!__________!_______________!
@@ -580,9 +587,6 @@
    sn_hsw      =  'sdw_ecwaves_orca2'    ,        6.         , 'hs'         ,  .true.  , .true. , 'yearly'  ,  ''              , ''       , ''
    sn_wmp      =  'sdw_ecwaves_orca2'    ,        6.         , 'wmp'        ,  .true.  , .true. , 'yearly'  ,  ''              , ''       , ''
-   sn_wfr      =  'sdw_ecwaves_orca2'    ,        6.         , 'wfr'        ,  .true.  , .true. , 'yearly'  ,  ''              , ''       , ''
    sn_wnum     =  'sdw_ecwaves_orca2'    ,        6.         , 'wave_num'   ,  .true.  , .true. , 'yearly'  ,  ''              , ''       , ''
-   sn_tauwoc   =  'sdw_ecwaves_orca2'    ,        6.         , 'wave_stress',  .true.  , .true. , 'yearly'  ,  ''              , ''       , ''
-   sn_tauwx    =  'sdw_ecwaves_orca2'    ,        6.         , 'wave_stress',  .true.  , .true. , 'yearly'  ,  ''              , ''       , ''
-   sn_tauwy    =  'sdw_ecwaves_orca2'    ,        6.         , 'wave_stress',  .true.  , .true. , 'yearly'  ,  ''              , ''       , ''
+   sn_tauoc    =  'sdw_ecwaves_orca2'    ,        6.         , 'wave_stress',  .true.  , .true. , 'yearly'  ,  ''              , ''       , ''
 /
 !-----------------------------------------------------------------------
@@ -1114,4 +1118,5 @@
       nn_npc      =    1         ! frequency of application of npc
       nn_npcp     =  365         ! npc control print frequency
+   ln_zdfmfc   = .false.      !  Mass Flux Convection
    !
    ln_zdfddm   = .false.   ! double diffusive mixing
@@ -1164,4 +1169,5 @@
       rn_mxlice   = 10.       ! max constant ice thickness value when scaling under sea-ice ( nn_mxlice=1)
    rn_mxl0     =   0.04    !  surface  buoyancy lenght scale minimum value
+   ln_mxhsw    = .false.   !  surface mixing length scale = F(wave height)
    ln_lc       = .true.    !  Langmuir cell parameterisation (Axell 2002)
       rn_lc       =   0.15    !  coef. associated to Langmuir cells
@@ -1179,4 +1185,6 @@
    !                       !           = 2 weighted by 1-fr_i
    !                       !           = 3 weighted by 1-MIN(1,4*fr_i)   
+   nn_bc_surf   =     1    !  surface condition (0/1=Dir/Neum) ! Only applicable for wave coupling (ln_cplwave=1)
+   nn_bc_bot    =     1    !  bottom condition (0/1=Dir/Neum) ! Only applicable for wave coupling (ln_cplwave=1)
 /
 !-----------------------------------------------------------------------
@@ -1223,4 +1231,15 @@
       !                        !  = 1: Pierson Moskowitz wave spectrum
       !                        !  = 0: Constant La# = 0.3
+/
+!-----------------------------------------------------------------------
+&namzdf_mfc     !   Mass Flux Convection
+!-----------------------------------------------------------------------
+   ln_edmfuv  = .false.        ! Activate on velocity fields (Not available yet)
+   rn_cemf    =  1.            ! entrain/detrain coef. (<0 => cte; >0 % depending on dW/dz
+   rn_cwmf    = -0.            ! entrain/detrain coef. (<0 => cte; >0 % depending on dW/dz
+   rn_cent    = 2.e-5          ! entrain of convective area
+   rn_cdet    = 3.e-5          ! detrain of convective area 
+   rn_cap     = 0.9            ! Coef. for CAP estimation
+   App_max    = 0.1            ! Maximum convection area (% of the cell)
 /
 !-----------------------------------------------------------------------
Index: /NEMO/branches/2020/tickets_icb_1900/doc/namelists/namdyn_rhg
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/doc/namelists/namdyn_rhg	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/doc/namelists/namdyn_rhg	(revision 14016)
@@ -3,4 +3,5 @@
 !------------------------------------------------------------------------------
    ln_rhg_EVP       = .true.          !  EVP rheology
+   ln_rhg_EAP       = .false.          !  EAP rheology
       ln_aEVP       = .false.         !     adaptive rheology (Kimmritz et al. 2016 & 2017)
       rn_creepl     =   2.0e-9        !     creep limit [1/s]
Index: /NEMO/branches/2020/tickets_icb_1900/src/ICE/ice.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/ICE/ice.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/ICE/ice.F90	(revision 14016)
@@ -150,10 +150,18 @@
    !
    !                                     !!** ice-rheology namelist (namdyn_rhg) **
+   ! -- evp
+   LOGICAL , PUBLIC ::   ln_rhg_EVP       ! EVP rheology switch, used for rdgrft and rheology
+   LOGICAL , PUBLIC ::   ln_rhg_EAP       ! EAP rheology switch, used for rdgrft and rheology
    LOGICAL , PUBLIC ::   ln_aEVP          !: using adaptive EVP (T or F) 
-   REAL(wp), PUBLIC ::   rn_creepl        !: creep limit : has to be under 1.0e-9
+   REAL(wp), PUBLIC ::   rn_creepl        !: creep limit (has to be low enough, circa 10-9 m/s, depending on rheology)
    REAL(wp), PUBLIC ::   rn_ecc           !: eccentricity of the elliptical yield curve
    INTEGER , PUBLIC ::   nn_nevp          !: number of iterations for subcycling
    REAL(wp), PUBLIC ::   rn_relast        !: ratio => telast/rDt_ice (1/3 or 1/9 depending on nb of subcycling nevp) 
    INTEGER , PUBLIC ::   nn_rhg_chkcvg    !: check ice rheology convergence 
+   ! -- vp
+   LOGICAL , PUBLIC ::   ln_rhg_VP        !: VP rheology
+   INTEGER , PUBLIC ::   nn_vp_nout       !: Number of outer iterations
+   INTEGER , PUBLIC ::   nn_vp_ninn       !: Number of inner iterations (linear system solver)
+   INTEGER , PUBLIC ::   nn_vp_chkcvg     !: Number of iterations every each convergence is checked
    !
    !                                     !!** ice-advection namelist (namdyn_adv) **
@@ -208,7 +216,9 @@
    !                                     !!** ice-ponds namelist (namthd_pnd)
    LOGICAL , PUBLIC ::   ln_pnd           !: Melt ponds (T) or not (F)
-   LOGICAL , PUBLIC ::   ln_pnd_LEV       !: Melt ponds scheme from Holland et al (2012), Flocco et al (2007, 2010)
-   REAL(wp), PUBLIC ::   rn_apnd_min      !: Minimum ice fraction that contributes to melt ponds
-   REAL(wp), PUBLIC ::   rn_apnd_max      !: Maximum ice fraction that contributes to melt ponds
+   LOGICAL , PUBLIC ::   ln_pnd_TOPO      !: Topographic Melt ponds scheme (Flocco et al 2007, 2010)
+   LOGICAL , PUBLIC ::   ln_pnd_LEV       !: Simple melt pond scheme
+   REAL(wp), PUBLIC ::   rn_apnd_min      !: Minimum fraction of melt water contributing to ponds
+   REAL(wp), PUBLIC ::   rn_apnd_max      !: Maximum fraction of melt water contributing to ponds
+   REAL(wp), PUBLIC ::   rn_pnd_flush     !: Pond flushing efficiency (tuning parameter)
    LOGICAL , PUBLIC ::   ln_pnd_CST       !: Melt ponds scheme with constant fraction and depth
    REAL(wp), PUBLIC ::   rn_apnd          !: prescribed pond fraction (0<rn_apnd<1)
@@ -246,4 +256,6 @@
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   divu_i          !: Divergence of the velocity field             [s-1]
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   shear_i         !: Shear of the velocity field                  [s-1]
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   aniso_11, aniso_12   !: structure tensor elements
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   rdg_conv
    !
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   t_bo            !: Sea-Ice bottom temperature [Kelvin]     
@@ -341,4 +353,5 @@
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)     ::   om_i          !: mean ice age over all categories                        (s)
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)     ::   tau_icebfr    !: ice friction on ocean bottom (landfast param activated)
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)     ::   icb_mask      !: mask of grounded icebergs if landfast [0-1]
 
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:,:) ::   t_s           !: Snow temperatures     [K]
@@ -362,8 +375,13 @@
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)     ::   vt_il         !: total melt pond lid volume per gridcell area [m]
 
+   ! meltwater arrays to save for melt ponds (mv - could be grouped in a single meltwater volume array)
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:)   ::   dh_i_sum_2d   !: surface melt (2d arrays for ponds)       [m]
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:)   ::   dh_s_mlt_2d   !: snow surf melt (2d arrays for ponds)     [m]
+
    !!----------------------------------------------------------------------
    !! * Global variables at before time step
    !!----------------------------------------------------------------------
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:)   ::   v_s_b, v_i_b, h_s_b, h_i_b !: snow and ice volumes/thickness
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:)   ::   v_ip_b, v_il_b             !: ponds and lids volumes
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:)   ::   a_i_b, sv_i_b              !:
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:,:) ::   e_s_b                      !: snow heat content
@@ -392,4 +410,5 @@
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   diag_vsnw         !: snw volume variation   [m/s] 
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   diag_aice         !: ice conc.  variation   [s-1] 
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   diag_vpnd         !: pond volume variation  [m/s] 
    !
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   diag_adv_mass     !: advection of mass (kg/m2/s)
@@ -436,5 +455,6 @@
       ALLOCATE( u_oce    (jpi,jpj) , v_oce    (jpi,jpj) , ht_i_new  (jpi,jpj) , strength(jpi,jpj) ,  &
          &      stress1_i(jpi,jpj) , stress2_i(jpi,jpj) , stress12_i(jpi,jpj) ,                      &
-         &      delta_i  (jpi,jpj) , divu_i   (jpi,jpj) , shear_i   (jpi,jpj) , STAT=ierr(ii) )
+         &      delta_i  (jpi,jpj) , divu_i   (jpi,jpj) , shear_i   (jpi,jpj) ,                      &
+         &      aniso_11 (jpi,jpj) , aniso_12 (jpi,jpj) , rdg_conv  (jpi,jpj) , STAT=ierr(ii) )
 
       ii = ii + 1
@@ -468,5 +488,5 @@
          &      et_i (jpi,jpj) , et_s (jpi,jpj) , tm_i(jpi,jpj) , tm_s(jpi,jpj) ,  &
          &      sm_i (jpi,jpj) , tm_su(jpi,jpj) , hm_i(jpi,jpj) , hm_s(jpi,jpj) ,  &
-         &      om_i (jpi,jpj) , bvm_i(jpi,jpj) , tau_icebfr(jpi,jpj)            , STAT=ierr(ii) )
+         &      om_i (jpi,jpj) , bvm_i(jpi,jpj) , tau_icebfr(jpi,jpj), icb_mask(jpi,jpj), STAT=ierr(ii) )
 
       ii = ii + 1
@@ -478,5 +498,6 @@
       ii = ii + 1
       ALLOCATE( a_ip(jpi,jpj,jpl) , v_ip(jpi,jpj,jpl) , a_ip_frac(jpi,jpj,jpl) , h_ip(jpi,jpj,jpl),  &
-         &      v_il(jpi,jpj,jpl) , h_il(jpi,jpj,jpl) , a_ip_eff (jpi,jpj,jpl) , STAT = ierr(ii) )
+         &      v_il(jpi,jpj,jpl) , h_il(jpi,jpj,jpl) , a_ip_eff (jpi,jpj,jpl) ,                     &
+         &      dh_i_sum_2d(jpi,jpj,jpl) , dh_s_mlt_2d(jpi,jpj,jpl) , STAT = ierr(ii) )
 
       ii = ii + 1
@@ -486,4 +507,5 @@
       ii = ii + 1
       ALLOCATE( v_s_b (jpi,jpj,jpl) , v_i_b (jpi,jpj,jpl) , h_s_b(jpi,jpj,jpl)        , h_i_b(jpi,jpj,jpl),         &
+         &      v_ip_b(jpi,jpj,jpl) , v_il_b(jpi,jpj,jpl) ,                                                         &
          &      a_i_b (jpi,jpj,jpl) , sv_i_b(jpi,jpj,jpl) , e_i_b(jpi,jpj,nlay_i,jpl) , e_s_b(jpi,jpj,nlay_s,jpl) , &
          &      STAT=ierr(ii) )
@@ -500,5 +522,5 @@
       ALLOCATE( diag_trp_vi(jpi,jpj) , diag_trp_vs (jpi,jpj) , diag_trp_ei(jpi,jpj),                      & 
          &      diag_trp_es(jpi,jpj) , diag_trp_sv (jpi,jpj) , diag_heat  (jpi,jpj),                      &
-         &      diag_sice  (jpi,jpj) , diag_vice   (jpi,jpj) , diag_vsnw  (jpi,jpj), diag_aice(jpi,jpj),  &
+         &      diag_sice  (jpi,jpj) , diag_vice   (jpi,jpj) , diag_vsnw  (jpi,jpj), diag_aice(jpi,jpj), diag_vpnd(jpi,jpj),  &
          &      diag_adv_mass(jpi,jpj), diag_adv_salt(jpi,jpj), diag_adv_heat(jpi,jpj), STAT=ierr(ii) )
 
Index: /NEMO/branches/2020/tickets_icb_1900/src/ICE/icectl.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/ICE/icectl.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/ICE/icectl.F90	(revision 14016)
@@ -85,5 +85,6 @@
       !!
       REAL(wp) ::   zdiag_mass, zdiag_salt, zdiag_heat, &
-         &          zdiag_vmin, zdiag_amin, zdiag_amax, zdiag_eimin, zdiag_esmin, zdiag_smin
+         &          zdiag_vimin, zdiag_vsmin, zdiag_vpmin, zdiag_vlmin, zdiag_aimin, zdiag_aimax, &
+         &          zdiag_eimin, zdiag_esmin, zdiag_simin
       REAL(wp) ::   zvtrp, zetrp
       REAL(wp) ::   zarea
@@ -92,5 +93,5 @@
       IF( icount == 0 ) THEN
 
-         pdiag_v = glob_sum( 'icectl',   SUM( v_i * rhoi + v_s * rhos, dim=3 ) * e1e2t )
+         pdiag_v = glob_sum( 'icectl',   SUM( v_i * rhoi + v_s * rhos + ( v_ip + v_il ) * rhow, dim=3 ) * e1e2t )
          pdiag_s = glob_sum( 'icectl',   SUM( sv_i * rhoi            , dim=3 ) * e1e2t )
          pdiag_t = glob_sum( 'icectl', ( SUM( SUM( e_i, dim=4 ), dim=3 ) + SUM( SUM( e_s, dim=4 ), dim=3 ) ) * e1e2t )
@@ -112,5 +113,6 @@
 
          ! -- mass diag -- !
-         zdiag_mass = ( glob_sum( 'icectl', SUM( v_i * rhoi + v_s * rhos, dim=3 ) * e1e2t ) - pdiag_v ) * r1_Dt_ice       &
+         zdiag_mass = ( glob_sum( 'icectl', SUM( v_i * rhoi + v_s * rhos + ( v_ip + v_il ) * rhow, dim=3 ) * e1e2t )      &
+            &            - pdiag_v ) * r1_Dt_ice                                                                          &
             &         + glob_sum( 'icectl', ( wfx_bog + wfx_bom + wfx_sum + wfx_sni + wfx_opw + wfx_res + wfx_dyn +       &
             &                                 wfx_lam + wfx_pnd + wfx_snw_sni + wfx_snw_sum + wfx_snw_dyn + wfx_snw_sub + &
@@ -132,8 +134,11 @@
 
          ! -- min/max diag -- !
-         zdiag_amax  = glob_max( 'icectl', SUM( a_i, dim=3 ) )
-         zdiag_vmin  = glob_min( 'icectl', v_i )
-         zdiag_amin  = glob_min( 'icectl', a_i )
-         zdiag_smin  = glob_min( 'icectl', sv_i )
+         zdiag_aimax = glob_max( 'icectl', SUM( a_i, dim=3 ) )
+         zdiag_vimin = glob_min( 'icectl', v_i  )
+         zdiag_vsmin = glob_min( 'icectl', v_s  )
+         zdiag_vpmin = glob_min( 'icectl', v_ip )
+         zdiag_vlmin = glob_min( 'icectl', v_il )
+         zdiag_aimin = glob_min( 'icectl', a_i  )
+         zdiag_simin = glob_min( 'icectl', sv_i )
          zdiag_eimin = glob_min( 'icectl', SUM( e_i, dim=3 ) )
          zdiag_esmin = glob_min( 'icectl', SUM( e_s, dim=3 ) )
@@ -155,17 +160,20 @@
                &                   WRITE(numout,*)   cd_routine,' : violation heat cons. [J]  = ',zdiag_heat * rDt_ice
             ! check negative values
-            IF( zdiag_vmin  < 0. ) WRITE(numout,*)   cd_routine,' : violation v_i < 0         = ',zdiag_vmin
-            IF( zdiag_amin  < 0. ) WRITE(numout,*)   cd_routine,' : violation a_i < 0         = ',zdiag_amin
-            IF( zdiag_smin  < 0. ) WRITE(numout,*)   cd_routine,' : violation s_i < 0         = ',zdiag_smin
-            IF( zdiag_eimin < 0. ) WRITE(numout,*)   cd_routine,' : violation e_i < 0         = ',zdiag_eimin
-            IF( zdiag_esmin < 0. ) WRITE(numout,*)   cd_routine,' : violation e_s < 0         = ',zdiag_esmin
+            IF( zdiag_vimin < 0. ) WRITE(numout,*)   cd_routine,' : violation v_i  < 0        = ',zdiag_vimin
+            IF( zdiag_vsmin < 0. ) WRITE(numout,*)   cd_routine,' : violation v_s  < 0        = ',zdiag_vsmin
+            IF( zdiag_vpmin < 0. ) WRITE(numout,*)   cd_routine,' : violation v_ip < 0        = ',zdiag_vpmin
+            IF( zdiag_vlmin < 0. ) WRITE(numout,*)   cd_routine,' : violation v_il < 0        = ',zdiag_vlmin
+            IF( zdiag_aimin < 0. ) WRITE(numout,*)   cd_routine,' : violation a_i  < 0        = ',zdiag_aimin
+            IF( zdiag_simin < 0. ) WRITE(numout,*)   cd_routine,' : violation s_i  < 0        = ',zdiag_simin
+            IF( zdiag_eimin < 0. ) WRITE(numout,*)   cd_routine,' : violation e_i  < 0        = ',zdiag_eimin
+            IF( zdiag_esmin < 0. ) WRITE(numout,*)   cd_routine,' : violation e_s  < 0        = ',zdiag_esmin
             ! check maximum ice concentration
-            IF( zdiag_amax > MAX(rn_amax_n,rn_amax_s)+epsi10 .AND. cd_routine /= 'icedyn_adv' .AND. cd_routine /= 'icedyn_rdgrft' ) &
-               &                   WRITE(numout,*)   cd_routine,' : violation a_i > amax      = ',zdiag_amax
+            IF( zdiag_aimax>MAX(rn_amax_n,rn_amax_s)+epsi10 .AND. cd_routine /= 'icedyn_adv' .AND. cd_routine /= 'icedyn_rdgrft' ) &
+               &                   WRITE(numout,*)   cd_routine,' : violation a_i > amax      = ',zdiag_aimax
             ! check if advection scheme is conservative
             IF( ABS(zvtrp) > zchk_m * rn_icechk_glo * zarea .AND. cd_routine == 'icedyn_adv' ) &
-               &                   WRITE(numout,*)   cd_routine,' : violation adv scheme [kg] = ',zvtrp * rdt_ice
+               &                   WRITE(numout,*)   cd_routine,' : violation adv scheme [kg] = ',zvtrp * rDt_ice
             IF( ABS(zetrp) > zchk_t * rn_icechk_glo * zarea .AND. cd_routine == 'icedyn_adv' ) &
-               &                   WRITE(numout,*)   cd_routine,' : violation adv scheme [J]  = ',zetrp * rdt_ice
+               &                   WRITE(numout,*)   cd_routine,' : violation adv scheme [J]  = ',zetrp * rDt_ice
          ENDIF
          !
@@ -193,6 +201,6 @@
       ! water flux
       ! -- mass diag -- !
-      zdiag_mass = glob_sum( 'icectl', (  wfx_ice   + wfx_snw   + wfx_spr + wfx_sub &
-         &                              + diag_vice + diag_vsnw - diag_adv_mass ) * e1e2t )
+      zdiag_mass = glob_sum( 'icectl', (  wfx_ice   + wfx_snw   + wfx_spr   + wfx_sub + wfx_pnd &
+         &                              + diag_vice + diag_vsnw + diag_vpnd - diag_adv_mass ) * e1e2t )
 
       ! -- salt diag -- !
@@ -200,5 +208,5 @@
 
       ! -- heat diag -- !
-      zdiag_heat  = glob_sum( 'icectl', ( qt_oce_ai - qt_atm_oi + diag_heat - diag_adv_heat ) * e1e2t )
+      zdiag_heat = glob_sum( 'icectl', ( qt_oce_ai - qt_atm_oi + diag_heat - diag_adv_heat ) * e1e2t )
       ! equivalent to this:
       !!zdiag_heat = glob_sum( 'icectl', ( -diag_heat + hfx_sum + hfx_bom + hfx_bog + hfx_dif + hfx_opw + hfx_snw &
@@ -245,6 +253,6 @@
       IF( icount == 0 ) THEN
 
-         pdiag_v = SUM( v_i  * rhoi + v_s * rhos, dim=3 )
-         pdiag_s = SUM( sv_i * rhoi             , dim=3 )
+         pdiag_v = SUM( v_i  * rhoi + v_s * rhos + ( v_ip + v_il ) * rhow, dim=3 )
+         pdiag_s = SUM( sv_i * rhoi , dim=3 )
          pdiag_t = SUM( SUM( e_i, dim=4 ), dim=3 ) + SUM( SUM( e_s, dim=4 ), dim=3 )
 
@@ -261,5 +269,5 @@
 
          ! -- mass diag -- !
-         zdiag_mass =   ( SUM( v_i * rhoi + v_s * rhos, dim=3 ) - pdiag_v ) * r1_Dt_ice                             &
+         zdiag_mass =   ( SUM( v_i * rhoi + v_s * rhos + ( v_ip + v_il ) * rhow, dim=3 ) - pdiag_v ) * r1_Dt_ice    &
             &         + ( wfx_bog + wfx_bom + wfx_sum + wfx_sni + wfx_opw + wfx_res + wfx_dyn + wfx_lam + wfx_pnd + &
             &             wfx_snw_sni + wfx_snw_sum + wfx_snw_dyn + wfx_snw_sub + wfx_ice_sub + wfx_spr )           &
@@ -352,4 +360,10 @@
       CALL iom_rstput( 0, 0, inum, 'sneg_count', pdiag_smin(:,:) , ktype = jp_r8 )    ! 
       CALL iom_rstput( 0, 0, inum, 'eneg_count', pdiag_emin(:,:) , ktype = jp_r8 )    ! 
+      ! mean state
+      CALL iom_rstput( 0, 0, inum, 'icecon'    , SUM(a_i ,dim=3) , ktype = jp_r8 )    !
+      CALL iom_rstput( 0, 0, inum, 'icevol'    , SUM(v_i ,dim=3) , ktype = jp_r8 )    !
+      CALL iom_rstput( 0, 0, inum, 'snwvol'    , SUM(v_s ,dim=3) , ktype = jp_r8 )    !
+      CALL iom_rstput( 0, 0, inum, 'pndvol'    , SUM(v_ip,dim=3) , ktype = jp_r8 )    !
+      CALL iom_rstput( 0, 0, inum, 'lidvol'    , SUM(v_il,dim=3) , ktype = jp_r8 )    !
       
       CALL iom_close( inum )
@@ -776,9 +790,9 @@
       ! -- mass diag -- !
       zdiag_mass     = glob_sum( 'icectl', (  wfx_ice   + wfx_snw   + wfx_spr + wfx_sub &
-         &                                  + diag_vice + diag_vsnw - diag_adv_mass ) * e1e2t ) * rdt_ice
+         &                                  + diag_vice + diag_vsnw - diag_adv_mass ) * e1e2t ) * rDt_ice
       zdiag_adv_mass = glob_sum( 'icectl', diag_adv_mass * e1e2t ) * rDt_ice
 
       ! -- salt diag -- !
-      zdiag_salt     = glob_sum( 'icectl', ( sfx + diag_sice - diag_adv_salt ) * e1e2t ) * rdt_ice * 1.e-3
+      zdiag_salt     = glob_sum( 'icectl', ( sfx + diag_sice - diag_adv_salt ) * e1e2t ) * rDt_ice * 1.e-3
       zdiag_adv_salt = glob_sum( 'icectl', diag_adv_salt * e1e2t ) * rDt_ice * 1.e-3
 
Index: /NEMO/branches/2020/tickets_icb_1900/src/ICE/icedyn.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/ICE/icedyn.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/ICE/icedyn.F90	(revision 14016)
@@ -29,4 +29,5 @@
    USE lbclnk         ! lateral boundary conditions (or mpp links)
    USE timing         ! Timing
+   USE fldread        ! read input fields
 
    IMPLICIT NONE
@@ -51,4 +52,6 @@
    REAL(wp) ::   rn_vice          !    prescribed v-vel (case np_dynADV1D & np_dynADV2D)
    
+   TYPE(FLD), ALLOCATABLE, DIMENSION(:) ::   sf_icbmsk   ! structure of input grounded icebergs mask (file informations, fields read)
+
    !! * Substitutions
 #  include "do_loop_substitute.h90"
@@ -81,5 +84,5 @@
       !
       ! controls
-      IF( ln_timing )   CALL timing_start('icedyn')
+      IF( ln_timing )   CALL timing_start('ice_dyn')
       !
       IF( kt == nit000 .AND. lwp ) THEN
@@ -106,4 +109,8 @@
       END WHERE
       !
+      IF( ln_landfast_L16 ) THEN
+         CALL fld_read( kt, 1, sf_icbmsk )
+         icb_mask(:,:) = sf_icbmsk(1)%fnow(:,:,1)
+      ENDIF
       !
       SELECT CASE( nice_dyn )          !-- Set which dynamics is running
@@ -172,5 +179,5 @@
       !
       ! controls
-      IF( ln_timing )   CALL timing_stop ('icedyn')
+      IF( ln_timing )   CALL timing_stop ('ice_dyn')
       !
    END SUBROUTINE ice_dyn
@@ -216,9 +223,13 @@
       !! ** input   :   Namelist namdyn
       !!-------------------------------------------------------------------
-      INTEGER ::   ios, ioptio   ! Local integer output status for namelist read
+      INTEGER ::   ios, ioptio, ierror   ! Local integer output status for namelist read
+      !
+      CHARACTER(len=256) ::   cn_dir     ! Root directory for location of ice files
+      TYPE(FLD_N)        ::   sn_icbmsk  ! informations about the grounded icebergs field to be read
       !!
       NAMELIST/namdyn/ ln_dynALL, ln_dynRHGADV, ln_dynADV1D, ln_dynADV2D, rn_uice, rn_vice,  &
          &             rn_ishlat ,                                                           &
-         &             ln_landfast_L16, rn_lf_depfra, rn_lf_bfr, rn_lf_relax, rn_lf_tensile
+         &             ln_landfast_L16, rn_lf_depfra, rn_lf_bfr, rn_lf_relax, rn_lf_tensile, &
+         &             sn_icbmsk, cn_dir
       !!-------------------------------------------------------------------
       !
@@ -269,4 +280,21 @@
       IF( .NOT.ln_landfast_L16 )   tau_icebfr(:,:) = 0._wp
       !
+      !                                      !--- allocate and fill structure for grounded icebergs mask
+      IF( ln_landfast_L16 ) THEN
+         ALLOCATE( sf_icbmsk(1), STAT=ierror )
+         IF( ierror > 0 ) THEN
+            CALL ctl_stop( 'ice_dyn_init: unable to allocate sf_icbmsk structure' ) ; RETURN
+         ENDIF
+         !
+         CALL fld_fill( sf_icbmsk, (/ sn_icbmsk /), cn_dir, 'ice_dyn_init',   &
+            &                                               'landfast ice is a function of read grounded icebergs', 'icedyn' )
+         !
+         ALLOCATE( sf_icbmsk(1)%fnow(jpi,jpj,1) )
+         IF( sf_icbmsk(1)%ln_tint )   ALLOCATE( sf_icbmsk(1)%fdta(jpi,jpj,1,2) )
+         IF( TRIM(sf_icbmsk(1)%clrootname) == 'NOT USED' ) sf_icbmsk(1)%fnow(:,:,1) = 0._wp   ! not used field  (set to 0)         
+      ELSE
+         icb_mask(:,:) = 0._wp
+      ENDIF
+      !                                      !--- other init 
       CALL ice_dyn_rdgrft_init          ! set ice ridging/rafting parameters
       CALL ice_dyn_rhg_init             ! set ice rheology parameters
Index: /NEMO/branches/2020/tickets_icb_1900/src/ICE/icedyn_adv_pra.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/ICE/icedyn_adv_pra.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/ICE/icedyn_adv_pra.F90	(revision 14016)
@@ -156,5 +156,6 @@
 
          ! diagnostics
-         zdiag_adv_mass(:,:) =   SUM(  pv_i(:,:,:) , dim=3 ) * rhoi + SUM(  pv_s(:,:,:) , dim=3 ) * rhos
+         zdiag_adv_mass(:,:) =   SUM( pv_i (:,:,:) , dim=3 ) * rhoi + SUM( pv_s (:,:,:) , dim=3 ) * rhos &
+            &                  + SUM( pv_ip(:,:,:) , dim=3 ) * rhow + SUM( pv_il(:,:,:) , dim=3 ) * rhow
          zdiag_adv_salt(:,:) =   SUM( psv_i(:,:,:) , dim=3 ) * rhoi
          zdiag_adv_heat(:,:) = - SUM(SUM( pe_i(:,:,1:nlay_i,:) , dim=4 ), dim=3 ) &
@@ -178,5 +179,5 @@
                z0ei(:,:,jk,jl) = pe_i(:,:,jk,jl) * e1e2t(:,:) ! Ice  heat content
             END DO
-            IF ( ln_pnd_LEV ) THEN
+            IF ( ln_pnd_LEV .OR. ln_pnd_TOPO ) THEN
                z0ap(:,:,jl) = pa_ip(:,:,jl) * e1e2t(:,:)      ! Melt pond fraction
                z0vp(:,:,jl) = pv_ip(:,:,jl) * e1e2t(:,:)      ! Melt pond volume
@@ -214,5 +215,5 @@
             END DO
             !
-            IF ( ln_pnd_LEV ) THEN
+            IF ( ln_pnd_LEV .OR. ln_pnd_TOPO ) THEN
                CALL adv_x( zdt , zudy , 1._wp , zarea , z0ap , sxap , sxxap , syap , syyap , sxyap )    !--- melt pond fraction
                CALL adv_y( zdt , zvdx , 0._wp , zarea , z0ap , sxap , sxxap , syap , syyap , sxyap ) 
@@ -249,5 +250,5 @@
                   &                                 sxxe(:,:,jk,:), sye(:,:,jk,:), syye(:,:,jk,:), sxye(:,:,jk,:) )
             END DO
-            IF ( ln_pnd_LEV ) THEN
+            IF ( ln_pnd_LEV .OR. ln_pnd_TOPO ) THEN
                CALL adv_y( zdt , zvdx , 1._wp , zarea , z0ap , sxap , sxxap , syap , syyap , sxyap )    !--- melt pond fraction
                CALL adv_x( zdt , zudy , 0._wp , zarea , z0ap , sxap , sxxap , syap , syyap , sxyap )
@@ -278,5 +279,5 @@
          CALL lbc_lnk_multi( 'icedyn_adv_pra', z0ei  , 'T', 1._wp, sxe   , 'T', -1._wp, sye   , 'T', -1._wp  & ! ice enthalpy
             &                                , sxxe  , 'T', 1._wp, syye  , 'T',  1._wp, sxye  , 'T',  1._wp  )
-         IF ( ln_pnd_LEV ) THEN
+         IF ( ln_pnd_LEV .OR. ln_pnd_TOPO ) THEN
             CALL lbc_lnk_multi( 'icedyn_adv_pra', z0ap , 'T', 1._wp, sxap , 'T', -1._wp, syap , 'T', -1._wp  & ! melt pond fraction
                &                                , sxxap, 'T', 1._wp, syyap, 'T',  1._wp, sxyap, 'T',  1._wp  &
@@ -302,5 +303,5 @@
                pe_i(:,:,jk,jl) = z0ei(:,:,jk,jl) * r1_e1e2t(:,:) * tmask(:,:,1)
             END DO
-            IF ( ln_pnd_LEV ) THEN
+            IF ( ln_pnd_LEV .OR. ln_pnd_TOPO ) THEN
                pa_ip(:,:,jl) = z0ap(:,:,jl) * r1_e1e2t(:,:) * tmask(:,:,1)
                pv_ip(:,:,jl) = z0vp(:,:,jl) * r1_e1e2t(:,:) * tmask(:,:,1)
@@ -320,5 +321,6 @@
          !
          ! --- diagnostics --- !
-         diag_adv_mass(:,:) = diag_adv_mass(:,:) + (   SUM( pv_i(:,:,:) , dim=3 ) * rhoi + SUM( pv_s(:,:,:) , dim=3 ) * rhos &
+         diag_adv_mass(:,:) = diag_adv_mass(:,:) + (   SUM( pv_i (:,:,:) , dim=3 ) * rhoi + SUM( pv_s (:,:,:) , dim=3 ) * rhos &
+            &                                        + SUM( pv_ip(:,:,:) , dim=3 ) * rhow + SUM( pv_il(:,:,:) , dim=3 ) * rhow &
             &                                        - zdiag_adv_mass(:,:) ) * z1_dt
          diag_adv_salt(:,:) = diag_adv_salt(:,:) + (   SUM( psv_i(:,:,:) , dim=3 ) * rhoi &
@@ -769,5 +771,5 @@
                !                               ! -- check h_ip -- !
                ! if h_ip is larger than the surrounding 9 pts => reduce h_ip and increase a_ip
-               IF( ln_pnd_LEV .AND. pv_ip(ji,jj,jl) > 0._wp ) THEN
+               IF( ln_pnd_LEV .OR. ln_pnd_TOPO .AND. pv_ip(ji,jj,jl) > 0._wp ) THEN
                   zhip = pv_ip(ji,jj,jl) / MAX( epsi20, pa_ip(ji,jj,jl) )
                   IF( zhip > phip_max(ji,jj,jl) .AND. pa_ip(ji,jj,jl) < 0.15 ) THEN
@@ -1015,5 +1017,5 @@
             END DO
             !
-            IF( ln_pnd_LEV ) THEN                                    ! melt pond fraction
+            IF( ln_pnd_LEV .OR. ln_pnd_TOPO ) THEN                                    ! melt pond fraction
                IF( iom_varid( numrir, 'sxap', ldstop = .FALSE. ) > 0 ) THEN
                   CALL iom_get( numrir, jpdom_auto, 'sxap' , sxap , psgn = -1._wp )
@@ -1057,5 +1059,5 @@
             sxc0  = 0._wp   ;   syc0  = 0._wp   ;   sxxc0  = 0._wp   ;   syyc0  = 0._wp   ;   sxyc0  = 0._wp      ! snow layers heat content
             sxe   = 0._wp   ;   sye   = 0._wp   ;   sxxe   = 0._wp   ;   syye   = 0._wp   ;   sxye   = 0._wp      ! ice layers heat content
-            IF( ln_pnd_LEV ) THEN
+            IF( ln_pnd_LEV .OR. ln_pnd_TOPO ) THEN
                sxap = 0._wp ;   syap = 0._wp    ;   sxxap = 0._wp    ;   syyap = 0._wp    ;   sxyap = 0._wp       ! melt pond fraction
                sxvp = 0._wp ;   syvp = 0._wp    ;   sxxvp = 0._wp    ;   syyvp = 0._wp    ;   sxyvp = 0._wp       ! melt pond volume
@@ -1135,5 +1137,5 @@
          END DO
          !
-         IF( ln_pnd_LEV ) THEN                                       ! melt pond fraction
+         IF( ln_pnd_LEV .OR. ln_pnd_TOPO ) THEN                                       ! melt pond fraction
             CALL iom_rstput( iter, nitrst, numriw, 'sxap' , sxap  )
             CALL iom_rstput( iter, nitrst, numriw, 'syap' , syap  )
Index: /NEMO/branches/2020/tickets_icb_1900/src/ICE/icedyn_adv_umx.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/ICE/icedyn_adv_umx.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/ICE/icedyn_adv_umx.F90	(revision 14016)
@@ -182,5 +182,6 @@
 
          ! diagnostics
-         zdiag_adv_mass(:,:) =   SUM(  pv_i(:,:,:) , dim=3 ) * rhoi + SUM(  pv_s(:,:,:) , dim=3 ) * rhos
+         zdiag_adv_mass(:,:) =   SUM( pv_i (:,:,:) , dim=3 ) * rhoi + SUM( pv_s (:,:,:) , dim=3 ) * rhos &
+            &                  + SUM( pv_ip(:,:,:) , dim=3 ) * rhow + SUM( pv_il(:,:,:) , dim=3 ) * rhow
          zdiag_adv_salt(:,:) =   SUM( psv_i(:,:,:) , dim=3 ) * rhoi
          zdiag_adv_heat(:,:) = - SUM(SUM( pe_i(:,:,1:nlay_i,:) , dim=4 ), dim=3 ) &
@@ -338,5 +339,5 @@
          !
          !== melt ponds ==!
-         IF ( ln_pnd_LEV ) THEN
+         IF ( ln_pnd_LEV .OR. ln_pnd_TOPO ) THEN
             ! concentration
             zamsk = 1._wp
@@ -358,8 +359,8 @@
 
          ! --- Lateral boundary conditions --- !
-         IF    ( ln_pnd_LEV .AND. ln_pnd_lids ) THEN
+         IF    ( ( ln_pnd_LEV .OR. ln_pnd_TOPO ) .AND. ln_pnd_lids ) THEN
             CALL lbc_lnk_multi( 'icedyn_adv_umx', pa_i,'T',1._wp, pv_i,'T',1._wp, pv_s,'T',1._wp, psv_i,'T',1._wp, poa_i,'T',1._wp &
                &                                , pa_ip,'T',1._wp, pv_ip,'T',1._wp, pv_il,'T',1._wp )
-         ELSEIF( ln_pnd_LEV .AND. .NOT.ln_pnd_lids ) THEN
+         ELSEIF( ( ln_pnd_LEV .OR. ln_pnd_TOPO ) .AND. .NOT.ln_pnd_lids ) THEN
             CALL lbc_lnk_multi( 'icedyn_adv_umx', pa_i,'T',1._wp, pv_i,'T',1._wp, pv_s,'T',1._wp, psv_i,'T',1._wp, poa_i,'T',1._wp &
                &                                , pa_ip,'T',1._wp, pv_ip,'T',1._wp )
@@ -379,5 +380,6 @@
          !
          ! --- diagnostics --- !
-         diag_adv_mass(:,:) = diag_adv_mass(:,:) + (   SUM( pv_i(:,:,:) , dim=3 ) * rhoi + SUM( pv_s(:,:,:) , dim=3 ) * rhos &
+         diag_adv_mass(:,:) = diag_adv_mass(:,:) + (   SUM( pv_i (:,:,:) , dim=3 ) * rhoi + SUM( pv_s (:,:,:) , dim=3 ) * rhos &
+            &                                        + SUM( pv_ip(:,:,:) , dim=3 ) * rhow + SUM( pv_il(:,:,:) , dim=3 ) * rhow &
             &                                        - zdiag_adv_mass(:,:) ) * z1_dt
          diag_adv_salt(:,:) = diag_adv_salt(:,:) + (   SUM( psv_i(:,:,:) , dim=3 ) * rhoi &
@@ -1497,5 +1499,5 @@
                !                               ! -- check h_ip -- !
                ! if h_ip is larger than the surrounding 9 pts => reduce h_ip and increase a_ip
-               IF( ln_pnd_LEV .AND. pv_ip(ji,jj,jl) > 0._wp ) THEN
+               IF( ( ln_pnd_LEV .OR. ln_pnd_TOPO ) .AND. pv_ip(ji,jj,jl) > 0._wp ) THEN
                   zhip = pv_ip(ji,jj,jl) / MAX( epsi20, pa_ip(ji,jj,jl) )
                   IF( zhip > phip_max(ji,jj,jl) .AND. pa_ip(ji,jj,jl) < 0.15 ) THEN
Index: /NEMO/branches/2020/tickets_icb_1900/src/ICE/icedyn_rdgrft.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/ICE/icedyn_rdgrft.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/ICE/icedyn_rdgrft.F90	(revision 14016)
@@ -140,4 +140,5 @@
       INTEGER , DIMENSION(jpij) ::   iptidx        ! compute ridge/raft or not
       REAL(wp), DIMENSION(jpij) ::   zdivu, zdelt  ! 1D divu_i & delta_i
+      REAL(wp), DIMENSION(jpij) ::   zconv         ! 1D rdg_conv (if EAP rheology)
       !
       INTEGER, PARAMETER ::   jp_itermax = 20    
@@ -175,4 +176,5 @@
          ! just needed here
          CALL tab_2d_1d( npti, nptidx(1:npti), zdelt   (1:npti)      , delta_i )
+         CALL tab_2d_1d( npti, nptidx(1:npti), zconv   (1:npti)      , rdg_conv )
          ! needed here and in the iteration loop
          CALL tab_2d_1d( npti, nptidx(1:npti), zdivu   (1:npti)      , divu_i) ! zdivu is used as a work array here (no change in divu_i)
@@ -184,5 +186,7 @@
             ! closing_net = rate at which open water area is removed + ice area removed by ridging 
             !                                                        - ice area added in new ridges
-            closing_net(ji) = rn_csrdg * 0.5_wp * ( zdelt(ji) - ABS( zdivu(ji) ) ) - MIN( zdivu(ji), 0._wp )
+            IF( ln_rhg_EVP .OR. ln_rhg_VP ) & 
+               &               closing_net(ji) = rn_csrdg * 0.5_wp * ( zdelt(ji) - ABS( zdivu(ji) ) ) - MIN( zdivu(ji), 0._wp )
+            IF( ln_rhg_EAP )   closing_net(ji) = zconv(ji)
             !
             IF( zdivu(ji) < 0._wp )   closing_net(ji) = MAX( closing_net(ji), -zdivu(ji) )   ! make sure the closing rate is large enough
@@ -575,5 +579,5 @@
                oirft2(ji) = oa_i_2d(ji,jl1)   * afrft * hi_hrft 
 
-               IF ( ln_pnd_LEV ) THEN
+               IF ( ln_pnd_LEV .OR. ln_pnd_TOPO ) THEN
                   aprdg1     = a_ip_2d(ji,jl1) * afrdg
                   aprdg2(ji) = a_ip_2d(ji,jl1) * afrdg * hi_hrdg(ji,jl1)
@@ -612,5 +616,5 @@
                sv_i_2d(ji,jl1) = sv_i_2d(ji,jl1) - sirdg1    - sirft(ji)
                oa_i_2d(ji,jl1) = oa_i_2d(ji,jl1) - oirdg1    - oirft1
-               IF ( ln_pnd_LEV ) THEN
+               IF ( ln_pnd_LEV .OR. ln_pnd_TOPO ) THEN
                   a_ip_2d(ji,jl1) = a_ip_2d(ji,jl1) - aprdg1    - aprft1
                   v_ip_2d(ji,jl1) = v_ip_2d(ji,jl1) - vprdg(ji) - vprft(ji)
@@ -709,5 +713,5 @@
                   v_s_2d (ji,jl2) = v_s_2d (ji,jl2) + ( vsrdg (ji) * rn_fsnwrdg * fvol(ji)  +  &
                      &                                  vsrft (ji) * rn_fsnwrft * zswitch(ji) )
-                  IF ( ln_pnd_LEV ) THEN
+                  IF ( ln_pnd_LEV .OR. ln_pnd_TOPO ) THEN
                      v_ip_2d (ji,jl2) = v_ip_2d(ji,jl2) + (   vprdg (ji) * rn_fpndrdg * fvol   (ji)   &
                         &                                   + vprft (ji) * rn_fpndrft * zswitch(ji)   )
@@ -776,5 +780,6 @@
       !                              !--------------------------------------------------!
          strength(:,:) = rn_pstar * SUM( v_i(:,:,:), dim=3 ) * EXP( -rn_crhg * ( 1._wp - SUM( a_i(:,:,:), dim=3 ) ) )
-         ismooth = 1
+         ismooth = 1    ! original code
+!        ismooth = 0    ! try for EAP stability
          !                           !--------------------------------------------------!
       ELSE                           ! Zero strength                                    !
Index: /NEMO/branches/2020/tickets_icb_1900/src/ICE/icedyn_rhg.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/ICE/icedyn_rhg.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/ICE/icedyn_rhg.F90	(revision 14016)
@@ -17,4 +17,6 @@
    USE ice            ! sea-ice: variables
    USE icedyn_rhg_evp ! sea-ice: EVP rheology
+   USE icedyn_rhg_eap ! sea-ice: EAP rheology
+   USE icedyn_rhg_vp  ! sea-ice: VP  rheology
    USE icectl         ! sea-ice: control prints
    !
@@ -33,8 +35,7 @@
    !                                        ! associated indices:
    INTEGER, PARAMETER ::   np_rhgEVP = 1   ! EVP rheology
-!! INTEGER, PARAMETER ::   np_rhgEAP = 2   ! EAP rheology
+   INTEGER, PARAMETER ::   np_rhgEAP = 2   ! EAP rheology
+   INTEGER, PARAMETER ::   np_rhgVP  = 3   ! VP rheology
 
-   ! ** namelist (namrhg) **
-   LOGICAL ::   ln_rhg_EVP       ! EVP rheology
    !
    !!----------------------------------------------------------------------
@@ -77,9 +78,20 @@
          !                             !------------------------!
          CALL ice_dyn_rhg_evp( kt, Kmm, stress1_i, stress2_i, stress12_i, shear_i, divu_i, delta_i )
-         !         
+         !        
+         !                             !------------------------!
+      CASE( np_rhgVP  )                ! Viscous-Plastic        !
+         !                             !------------------------!
+         CALL ice_dyn_rhg_vp ( kt, shear_i, divu_i, delta_i )
+         !
+         !                             !----------------------------!
+      CASE( np_rhgEAP )                ! Elasto-Anisotropic-Plastic !
+         !                             !----------------------------!
+         CALL ice_dyn_rhg_eap( kt, Kmm, stress1_i, stress2_i, stress12_i, shear_i, divu_i, delta_i, aniso_11, aniso_12, rdg_conv )
       END SELECT
       !
-      IF( lrst_ice ) THEN                       !* write EVP fields in the restart file
-         IF( ln_rhg_EVP )   CALL rhg_evp_rst( 'WRITE', kt )
+      IF( lrst_ice ) THEN
+         IF( ln_rhg_EVP )   CALL rhg_evp_rst( 'WRITE', kt ) !* write EVP fields in the restart file
+         IF( ln_rhg_EAP )   CALL rhg_eap_rst( 'WRITE', kt ) !* write EAP fields in the restart file
+         ! MV note: no restart needed for VP as there is no time equation for stress tensor
       ENDIF
       !
@@ -108,5 +120,6 @@
       INTEGER ::   ios, ioptio   ! Local integer output status for namelist read
       !!
-      NAMELIST/namdyn_rhg/  ln_rhg_EVP, ln_aEVP, rn_creepl, rn_ecc , nn_nevp, rn_relast, nn_rhg_chkcvg
+      NAMELIST/namdyn_rhg/  ln_rhg_EVP, ln_aEVP, ln_rhg_EAP, rn_creepl, rn_ecc , nn_nevp, rn_relast, nn_rhg_chkcvg, &  !-- evp
+         &                  ln_rhg_VP, nn_vp_nout, nn_vp_ninn, nn_vp_chkcvg                                            !-- vp 
       !!-------------------------------------------------------------------
       !
@@ -124,13 +137,20 @@
          WRITE(numout,*) '      rheology EVP (icedyn_rhg_evp)                        ln_rhg_EVP    = ', ln_rhg_EVP
          WRITE(numout,*) '         use adaptive EVP (aEVP)                           ln_aEVP       = ', ln_aEVP
-         WRITE(numout,*) '         creep limit                                       rn_creepl     = ', rn_creepl
-         WRITE(numout,*) '         eccentricity of the elliptical yield curve        rn_ecc        = ', rn_ecc
+         WRITE(numout,*) '         creep limit                                       rn_creepl     = ', rn_creepl ! also used by vp
+         WRITE(numout,*) '         eccentricity of the elliptical yield curve        rn_ecc        = ', rn_ecc    ! also used by vp
          WRITE(numout,*) '         number of iterations for subcycling               nn_nevp       = ', nn_nevp
          WRITE(numout,*) '         ratio of elastic timescale over ice time step     rn_relast     = ', rn_relast
-         WRITE(numout,*) '      check convergence of rheology                        nn_rhg_chkcvg = ', nn_rhg_chkcvg
-         IF    ( nn_rhg_chkcvg == 0 ) THEN   ;   WRITE(numout,*) '         no check'
-         ELSEIF( nn_rhg_chkcvg == 1 ) THEN   ;   WRITE(numout,*) '         check cvg at the main time step'
-         ELSEIF( nn_rhg_chkcvg == 2 ) THEN   ;   WRITE(numout,*) '         check cvg at both main and rheology time steps'
+         WRITE(numout,*) '         check convergence of rheology                     nn_rhg_chkcvg = ', nn_rhg_chkcvg
+         WRITE(numout,*) '      rheology VP   (icedyn_rhg_VP)                        ln_rhg_VP     = ', ln_rhg_VP
+         WRITE(numout,*) '         number of outer iterations                        nn_vp_nout    = ', nn_vp_nout
+         WRITE(numout,*) '         number of inner iterations                        nn_vp_ninn    = ', nn_vp_ninn
+         WRITE(numout,*) '         iteration step for convergence check              nn_vp_chkcvg  = ', nn_vp_chkcvg
+         IF( ln_rhg_EVP ) THEN
+            IF    ( nn_rhg_chkcvg == 0 ) THEN   ;   WRITE(numout,*) '         no check cvg'
+            ELSEIF( nn_rhg_chkcvg == 1 ) THEN   ;   WRITE(numout,*) '         check cvg at the main time step'
+            ELSEIF( nn_rhg_chkcvg == 2 ) THEN   ;   WRITE(numout,*) '         check cvg at both main and rheology time steps'
+            ENDIF
          ENDIF
+         WRITE(numout,*) '      rheology EAP (icedyn_rhg_eap)                        ln_rhg_EAP = ', ln_rhg_EAP
       ENDIF
       !
@@ -138,8 +158,11 @@
       ioptio = 0 
       IF( ln_rhg_EVP ) THEN   ;   ioptio = ioptio + 1   ;   nice_rhg = np_rhgEVP    ;   ENDIF
-!!    IF( ln_rhg_EAP ) THEN   ;   ioptio = ioptio + 1   ;   nice_rhg = np_rhgEAP    ;   ENDIF
+      IF( ln_rhg_EAP ) THEN   ;   ioptio = ioptio + 1   ;   nice_rhg = np_rhgEAP    ;   ENDIF
+      IF( ln_rhg_VP  ) THEN   ;   ioptio = ioptio + 1   ;   nice_rhg = np_rhgVP     ;   ENDIF 
       IF( ioptio /= 1 )   CALL ctl_stop( 'ice_dyn_rhg_init: choose one and only one ice rheology' )
       !
       IF( ln_rhg_EVP  )   CALL rhg_evp_rst( 'READ' )  !* read or initialize all required files
+      IF( ln_rhg_EAP  )   CALL rhg_eap_rst( 'READ' )  !* read or initialize all required files
+      ! no restart for VP as there is no explicit time dependency in the equation
       !
    END SUBROUTINE ice_dyn_rhg_init
Index: /NEMO/branches/2020/tickets_icb_1900/src/ICE/icedyn_rhg_eap.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/ICE/icedyn_rhg_eap.F90	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/src/ICE/icedyn_rhg_eap.F90	(revision 14016)
@@ -0,0 +1,1977 @@
+MODULE icedyn_rhg_eap
+   !!======================================================================
+   !!                     ***  MODULE  icedyn_rhg_eap  ***
+   !!   Sea-Ice dynamics : rheology Elasto-Viscous-Plastic
+   !!======================================================================
+   !! History :   -   !  2007-03  (M.A. Morales Maqueda, S. Bouillon) Original code
+   !!            3.0  !  2008-03  (M. Vancoppenolle) adaptation to new model
+   !!             -   !  2008-11  (M. Vancoppenolle, S. Bouillon, Y. Aksenov) add surface tilt in ice rheolohy 
+   !!            3.3  !  2009-05  (G.Garric)    addition of the evp case
+   !!            3.4  !  2011-01  (A. Porter)   dynamical allocation 
+   !!            3.5  !  2012-08  (R. Benshila) AGRIF
+   !!            3.6  !  2016-06  (C. Rousset)  Rewriting + landfast ice + mEVP (Bouillon 2013)
+   !!            3.7  !  2017     (C. Rousset)  add aEVP (Kimmritz 2016-2017)
+   !!            4.0  !  2018     (many people) SI3 [aka Sea Ice cube]
+   !!                 !  2019     (S. Rynders, Y. Aksenov, C. Rousset)  change into eap rheology from
+   !!                                           CICE code (Tsamados, Heorton) 
+   !!----------------------------------------------------------------------
+#if defined key_si3
+   !!----------------------------------------------------------------------
+   !!   'key_si3'                                       SI3 sea-ice model
+   !!----------------------------------------------------------------------
+   !!   ice_dyn_rhg_eap : computes ice velocities from EVP rheology
+   !!   rhg_eap_rst     : read/write EVP fields in ice restart
+   !!----------------------------------------------------------------------
+   USE par_oce
+   USE phycst         ! Physical constant
+   USE dom_oce        ! Ocean domain
+   USE sbc_oce , ONLY : ln_ice_embd, nn_fsbc, ssh_m
+   USE sbc_ice , ONLY : utau_ice, vtau_ice, snwice_mass, snwice_mass_b
+   USE ice            ! sea-ice: ice variables
+   USE icevar         ! ice_var_sshdyn
+   USE icedyn_rdgrft  ! sea-ice: ice strength
+   USE bdy_oce , ONLY : ln_bdy 
+   USE bdyice 
+#if defined key_agrif
+   USE agrif_ice_interp
+#endif
+   !
+   USE in_out_manager ! I/O manager
+   USE iom            ! I/O manager library
+   USE lib_mpp        ! MPP library
+   USE lib_fortran    ! fortran utilities (glob_sum + no signed zero)
+   USE lbclnk         ! lateral boundary conditions (or mpp links)
+   USE prtctl         ! Print control
+
+   USE netcdf         ! NetCDF library for convergence test
+   IMPLICIT NONE
+   PRIVATE
+
+   PUBLIC   ice_dyn_rhg_eap   ! called by icedyn_rhg.F90
+   PUBLIC   rhg_eap_rst       ! called by icedyn_rhg.F90
+
+   REAL(wp), PARAMETER ::   pphi = 3.141592653589793_wp/12._wp    ! diamond shaped floe smaller angle (default phi = 30 deg)
+
+   ! look-up table for calculating structure tensor
+   INTEGER, PARAMETER ::   nx_yield = 41
+   INTEGER, PARAMETER ::   ny_yield = 41
+   INTEGER, PARAMETER ::   na_yield = 21
+
+   REAL(wp), DIMENSION(nx_yield, ny_yield, na_yield) ::   s11r, s12r, s22r, s11s, s12s, s22s
+
+   !! * Substitutions
+#  include "do_loop_substitute.h90"
+#  include "domzgr_substitute.h90"
+
+   !! for convergence tests
+   INTEGER ::   ncvgid   ! netcdf file id
+   INTEGER ::   nvarid   ! netcdf variable id
+   REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   zmsk00, zmsk15
+   !!----------------------------------------------------------------------
+   !! NEMO/ICE 4.0 , NEMO Consortium (2018)
+   !! $Id: icedyn_rhg_eap.F90 11536 2019-09-11 13:54:18Z smasson $
+   !! Software governed by the CeCILL license (see ./LICENSE)
+   !!----------------------------------------------------------------------
+CONTAINS
+
+   SUBROUTINE ice_dyn_rhg_eap( kt, Kmm, pstress1_i, pstress2_i, pstress12_i, pshear_i, pdivu_i, pdelta_i, paniso_11, paniso_12, prdg_conv )
+      !!-------------------------------------------------------------------
+      !!                 ***  SUBROUTINE ice_dyn_rhg_eap  ***
+      !!                             EAP-C-grid
+      !!
+      !! ** purpose : determines sea ice drift from wind stress, ice-ocean
+      !!  stress and sea-surface slope. Ice-ice interaction is described by 
+      !!  a non-linear elasto-anisotropic-plastic (EAP) law including shear 
+      !!  strength and a bulk rheology .	
+      !!
+      !!  The points in the C-grid look like this, dear reader
+      !!
+      !!                              (ji,jj)
+      !!                                 |
+      !!                                 |
+      !!                      (ji-1,jj)  |  (ji,jj)
+      !!                             ---------   
+      !!                            |         |
+      !!                            | (ji,jj) |------(ji,jj)
+      !!                            |         |
+      !!                             ---------   
+      !!                     (ji-1,jj-1)     (ji,jj-1)
+      !!
+      !! ** Inputs  : - wind forcing (stress), oceanic currents
+      !!                ice total volume (vt_i) per unit area
+      !!                snow total volume (vt_s) per unit area
+      !!
+      !! ** Action  : - compute u_ice, v_ice : the components of the 
+      !!                sea-ice velocity vector
+      !!              - compute delta_i, shear_i, divu_i, which are inputs
+      !!                of the ice thickness distribution
+      !!
+      !! ** Steps   : 0) compute mask at F point
+      !!              1) Compute ice snow mass, ice strength 
+      !!              2) Compute wind, oceanic stresses, mass terms and
+      !!                 coriolis terms of the momentum equation
+      !!              3) Solve the momentum equation (iterative procedure)
+      !!              4) Recompute delta, shear and divergence
+      !!                 (which are inputs of the ITD) & store stress
+      !!                 for the next time step
+      !!              5) Diagnostics including charge ellipse
+      !!
+      !! ** Notes   : There is the possibility to use aEVP from the nice work of Kimmritz et al. (2016 & 2017)
+      !!              by setting up ln_aEVP=T (i.e. changing alpha and beta parameters).
+      !!              This is an upgraded version of mEVP from Bouillon et al. 2013
+      !!              (i.e. more stable and better convergence)
+      !!
+      !! References : Hunke and Dukowicz, JPO97
+      !!              Bouillon et al., Ocean Modelling 2009
+      !!              Bouillon et al., Ocean Modelling 2013
+      !!              Kimmritz et al., Ocean Modelling 2016 & 2017
+      !!-------------------------------------------------------------------
+      INTEGER                 , INTENT(in   ) ::   kt                                    ! time step
+      INTEGER                 , INTENT(in   ) ::   Kmm                                   ! ocean time level index
+      REAL(wp), DIMENSION(:,:), INTENT(inout) ::   pstress1_i, pstress2_i, pstress12_i   !
+      REAL(wp), DIMENSION(:,:), INTENT(  out) ::   pshear_i  , pdivu_i   , pdelta_i      !
+      REAL(wp), DIMENSION(:,:), INTENT(inout) ::   paniso_11 , paniso_12                 ! structure tensor components
+      REAL(wp), DIMENSION(:,:), INTENT(inout) ::   prdg_conv                             ! for ridging
+      !!
+      INTEGER ::   ji, jj       ! dummy loop indices
+      INTEGER ::   jter         ! local integers
+      !
+      REAL(wp) ::   zrhoco                                              ! rau0 * rn_cio
+      REAL(wp) ::   zdtevp, z1_dtevp                                    ! time step for subcycling
+      REAL(wp) ::   ecc2, z1_ecc2                                       ! square of yield ellipse eccenticity
+      REAL(wp) ::   zalph1, z1_alph1, zalph2, z1_alph2                  ! alpha coef from Bouillon 2009 or Kimmritz 2017
+      REAl(wp) ::   zbetau, zbetav
+      REAL(wp) ::   zm1, zm2, zm3, zmassU, zmassV, zvU, zvV             ! ice/snow mass and volume
+      REAL(wp) ::   zds2, zdt, zdt2, zdiv, zdiv2, zdsT                  ! temporary scalars
+      REAL(wp) ::   zTauO, zTauB, zRHS, zvel                            ! temporary scalars
+      REAL(wp) ::   zkt                                                 ! isotropic tensile strength for landfast ice
+      REAL(wp) ::   zvCr                                                ! critical ice volume above which ice is landfast
+      !
+      REAL(wp) ::   zintb, zintn                                        ! dummy argument
+      REAL(wp) ::   zfac_x, zfac_y
+      REAL(wp) ::   zshear, zdum1, zdum2
+      REAL(wp) ::   zstressptmp, zstressmtmp, zstress12tmpF             ! anisotropic stress tensor components
+      REAL(wp) ::   zalphar, zalphas                                    ! for mechanical redistribution
+      REAL(wp) ::   zmresult11, zmresult12, z1dtevpkth, zp5kth, z1_dtevp_A  ! for structure tensor evolution
+      !
+      REAL(wp), DIMENSION(jpi,jpj) ::   zstress12tmp                    ! anisotropic stress tensor component for regridding
+      REAL(wp), DIMENSION(jpi,jpj) ::   zyield11, zyield22, zyield12    ! yield surface tensor for history
+      REAL(wp), DIMENSION(jpi,jpj) ::   zdelta, zp_delt                 ! delta and P/delta at T points
+      REAL(wp), DIMENSION(jpi,jpj) ::   zten_i                          ! tension
+      REAL(wp), DIMENSION(jpi,jpj) ::   zbeta                           ! beta coef from Kimmritz 2017
+      !
+      REAL(wp), DIMENSION(jpi,jpj) ::   zdt_m                           ! (dt / ice-snow_mass) on T points
+      REAL(wp), DIMENSION(jpi,jpj) ::   zaU  , zaV                      ! ice fraction on U/V points
+      REAL(wp), DIMENSION(jpi,jpj) ::   zmU_t, zmV_t                    ! (ice-snow_mass / dt) on U/V points
+      REAL(wp), DIMENSION(jpi,jpj) ::   zmf                             ! coriolis parameter at T points
+      REAL(wp), DIMENSION(jpi,jpj) ::   v_oceU, u_oceV, v_iceU, u_iceV  ! ocean/ice u/v component on V/U points                           
+      !
+      REAL(wp), DIMENSION(jpi,jpj) ::   zds                             ! shear
+      REAL(wp), DIMENSION(jpi,jpj) ::   zs1, zs2, zs12                  ! stress tensor components
+      REAL(wp), DIMENSION(jpi,jpj) ::   zsshdyn                         ! array used for the calculation of ice surface slope:
+      !                                                                 !    ocean surface (ssh_m) if ice is not embedded
+      !                                                                 !    ice bottom surface if ice is embedded   
+      REAL(wp), DIMENSION(jpi,jpj) ::   zfU  , zfV                      ! internal stresses
+      REAL(wp), DIMENSION(jpi,jpj) ::   zspgU, zspgV                    ! surface pressure gradient at U/V points
+      REAL(wp), DIMENSION(jpi,jpj) ::   zCorU, zCorV                    ! Coriolis stress array
+      REAL(wp), DIMENSION(jpi,jpj) ::   ztaux_ai, ztauy_ai              ! ice-atm. stress at U-V points
+      REAL(wp), DIMENSION(jpi,jpj) ::   ztaux_oi, ztauy_oi              ! ice-ocean stress at U-V points
+      REAL(wp), DIMENSION(jpi,jpj) ::   ztaux_bi, ztauy_bi              ! ice-OceanBottom stress at U-V points (landfast)
+      REAL(wp), DIMENSION(jpi,jpj) ::   ztaux_base, ztauy_base          ! ice-bottom stress at U-V points (landfast)
+      !
+      REAL(wp), DIMENSION(jpi,jpj) ::   zmsk01x, zmsk01y                ! dummy arrays
+      REAL(wp), DIMENSION(jpi,jpj) ::   zmsk00x, zmsk00y                ! mask for ice presence
+      REAL(wp), DIMENSION(jpi,jpj) ::   zfmask                          ! mask at F points for the ice
+
+      REAL(wp), PARAMETER          ::   zepsi  = 1.0e-20_wp             ! tolerance parameter
+      REAL(wp), PARAMETER          ::   zmmin  = 1._wp                  ! ice mass (kg/m2)  below which ice velocity becomes very small
+      REAL(wp), PARAMETER          ::   zamin  = 0.001_wp               ! ice concentration below which ice velocity becomes very small
+      !! --- check convergence
+      REAL(wp), DIMENSION(jpi,jpj) ::   zu_ice, zv_ice
+      !! --- diags
+      REAL(wp) ::   zsig1, zsig2, zsig12, zfac, z1_strength
+      REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   zsig_I, zsig_II, zsig1_p, zsig2_p         
+      !! --- SIMIP diags
+      REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   zdiag_xmtrp_ice ! X-component of ice mass transport (kg/s)
+      REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   zdiag_ymtrp_ice ! Y-component of ice mass transport (kg/s)
+      REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   zdiag_xmtrp_snw ! X-component of snow mass transport (kg/s)
+      REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   zdiag_ymtrp_snw ! Y-component of snow mass transport (kg/s)
+      REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   zdiag_xatrp     ! X-component of area transport (m2/s)
+      REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   zdiag_yatrp     ! Y-component of area transport (m2/s)      
+      !!-------------------------------------------------------------------
+
+      IF( kt == nit000 .AND. lwp )   WRITE(numout,*) '-- ice_dyn_rhg_eap: EAP sea-ice rheology'
+      !
+      ! for diagnostics and convergence tests
+      ALLOCATE( zmsk00(jpi,jpj), zmsk15(jpi,jpj) )
+      DO_2D( 1, 1, 1, 1 )
+         zmsk00(ji,jj) = MAX( 0._wp , SIGN( 1._wp , at_i(ji,jj) - epsi06  ) ) ! 1 if ice    , 0 if no ice
+         zmsk15(ji,jj) = MAX( 0._wp , SIGN( 1._wp , at_i(ji,jj) - 0.15_wp ) ) ! 1 if 15% ice, 0 if less
+      END_2D
+      !
+!!gm for Clem:  OPTIMIZATION:  I think zfmask can be computed one for all at the initialization....
+      !------------------------------------------------------------------------------!
+      ! 0) mask at F points for the ice
+      !------------------------------------------------------------------------------!
+      ! ocean/land mask
+      DO_2D( 1, 0, 1, 0 )
+         zfmask(ji,jj) = tmask(ji,jj,1) * tmask(ji+1,jj,1) * tmask(ji,jj+1,1) * tmask(ji+1,jj+1,1)
+      END_2D
+      CALL lbc_lnk( 'icedyn_rhg_eap', zfmask, 'F', 1._wp )
+
+      ! Lateral boundary conditions on velocity (modify zfmask)
+      DO_2D( 0, 0, 0, 0 )
+         IF( zfmask(ji,jj) == 0._wp ) THEN
+            zfmask(ji,jj) = rn_ishlat * MIN( 1._wp , MAX( umask(ji,jj,1), umask(ji,jj+1,1), &
+               &                                          vmask(ji,jj,1), vmask(ji+1,jj,1) ) )
+         ENDIF
+      END_2D
+      DO jj = 2, jpjm1
+         IF( zfmask(1,jj) == 0._wp ) THEN
+            zfmask(1  ,jj) = rn_ishlat * MIN( 1._wp , MAX( vmask(2,jj,1), umask(1,jj+1,1), umask(1,jj,1) ) )
+         ENDIF
+         IF( zfmask(jpi,jj) == 0._wp ) THEN
+            zfmask(jpi,jj) = rn_ishlat * MIN( 1._wp , MAX( umask(jpi,jj+1,1), vmask(jpim1,jj,1), umask(jpi,jj-1,1) ) )
+         ENDIF
+      END DO
+      DO ji = 2, jpim1
+         IF( zfmask(ji,1) == 0._wp ) THEN
+            zfmask(ji, 1 ) = rn_ishlat * MIN( 1._wp , MAX( vmask(ji+1,1,1), umask(ji,2,1), vmask(ji,1,1) ) )
+         ENDIF
+         IF( zfmask(ji,jpj) == 0._wp ) THEN
+            zfmask(ji,jpj) = rn_ishlat * MIN( 1._wp , MAX( vmask(ji+1,jpj,1), vmask(ji-1,jpj,1), umask(ji,jpjm1,1) ) )
+         ENDIF
+      END DO
+      CALL lbc_lnk( 'icedyn_rhg_eap', zfmask, 'F', 1.0_wp )
+
+      !------------------------------------------------------------------------------!
+      ! 1) define some variables and initialize arrays
+      !------------------------------------------------------------------------------!
+      zrhoco = rho0 * rn_cio 
+
+      ! ecc2: square of yield ellipse eccenticrity
+      ecc2    = rn_ecc * rn_ecc
+      z1_ecc2 = 1._wp / ecc2
+
+      ! alpha parameters (Bouillon 2009)
+      IF( .NOT. ln_aEVP ) THEN
+         zdtevp   = rDt_ice / REAL( nn_nevp )
+         zalph1 =   2._wp * rn_relast * REAL( nn_nevp )
+         zalph2 = zalph1 * z1_ecc2
+
+         z1_alph1 = 1._wp / ( zalph1 + 1._wp )
+         z1_alph2 = 1._wp / ( zalph2 + 1._wp )
+      ELSE
+         zdtevp   = rdt_ice
+         ! zalpha parameters set later on adaptatively
+      ENDIF
+      z1_dtevp = 1._wp / zdtevp
+         
+      ! Initialise stress tensor 
+      zs1 (:,:) = pstress1_i (:,:) 
+      zs2 (:,:) = pstress2_i (:,:)
+      zs12(:,:) = pstress12_i(:,:)
+
+      ! constants for structure tensor
+      z1_dtevp_A = z1_dtevp/10.0_wp
+      z1dtevpkth = 1._wp / (z1_dtevp_A + 0.00002_wp)
+      zp5kth = 0.5_wp * 0.00002_wp
+
+      ! Ice strength
+      CALL ice_strength
+
+      ! landfast param from Lemieux(2016): add isotropic tensile strength (following Konig Beatty and Holland, 2010)
+      IF( ln_landfast_L16 ) THEN   ;   zkt = rn_lf_tensile
+      ELSE                         ;   zkt = 0._wp
+      ENDIF
+      !
+      !------------------------------------------------------------------------------!
+      ! 2) Wind / ocean stress, mass terms, coriolis terms
+      !------------------------------------------------------------------------------!
+      ! sea surface height
+      !    embedded sea ice: compute representative ice top surface
+      !    non-embedded sea ice: use ocean surface for slope calculation
+      zsshdyn(:,:) = ice_var_sshdyn( ssh_m, snwice_mass, snwice_mass_b)
+
+      DO_2D( 0, 0, 0, 0 )
+
+         ! ice fraction at U-V points
+         zaU(ji,jj) = 0.5_wp * ( at_i(ji,jj) * e1e2t(ji,jj) + at_i(ji+1,jj) * e1e2t(ji+1,jj) ) * r1_e1e2u(ji,jj) * umask(ji,jj,1)
+         zaV(ji,jj) = 0.5_wp * ( at_i(ji,jj) * e1e2t(ji,jj) + at_i(ji,jj+1) * e1e2t(ji,jj+1) ) * r1_e1e2v(ji,jj) * vmask(ji,jj,1)
+
+         ! Ice/snow mass at U-V points
+         zm1 = ( rhos * vt_s(ji  ,jj  ) + rhoi * vt_i(ji  ,jj  ) )
+         zm2 = ( rhos * vt_s(ji+1,jj  ) + rhoi * vt_i(ji+1,jj  ) )
+         zm3 = ( rhos * vt_s(ji  ,jj+1) + rhoi * vt_i(ji  ,jj+1) )
+         zmassU = 0.5_wp * ( zm1 * e1e2t(ji,jj) + zm2 * e1e2t(ji+1,jj) ) * r1_e1e2u(ji,jj) * umask(ji,jj,1)
+         zmassV = 0.5_wp * ( zm1 * e1e2t(ji,jj) + zm3 * e1e2t(ji,jj+1) ) * r1_e1e2v(ji,jj) * vmask(ji,jj,1)
+
+         ! Ocean currents at U-V points
+         v_oceU(ji,jj)   = 0.25_wp * ( v_oce(ji,jj) + v_oce(ji,jj-1) + v_oce(ji+1,jj) + v_oce(ji+1,jj-1) ) * umask(ji,jj,1)
+         u_oceV(ji,jj)   = 0.25_wp * ( u_oce(ji,jj) + u_oce(ji-1,jj) + u_oce(ji,jj+1) + u_oce(ji-1,jj+1) ) * vmask(ji,jj,1)
+
+         ! Coriolis at T points (m*f)
+         zmf(ji,jj)      = zm1 * ff_t(ji,jj)
+
+         ! dt/m at T points (for alpha and beta coefficients)
+         zdt_m(ji,jj)    = zdtevp / MAX( zm1, zmmin )
+         
+         ! m/dt
+         zmU_t(ji,jj)    = zmassU * z1_dtevp
+         zmV_t(ji,jj)    = zmassV * z1_dtevp
+         
+         ! Drag ice-atm.
+         ztaux_ai(ji,jj) = zaU(ji,jj) * utau_ice(ji,jj)
+         ztauy_ai(ji,jj) = zaV(ji,jj) * vtau_ice(ji,jj)
+
+         ! Surface pressure gradient (- m*g*GRAD(ssh)) at U-V points
+         zspgU(ji,jj)    = - zmassU * grav * ( zsshdyn(ji+1,jj) - zsshdyn(ji,jj) ) * r1_e1u(ji,jj)
+         zspgV(ji,jj)    = - zmassV * grav * ( zsshdyn(ji,jj+1) - zsshdyn(ji,jj) ) * r1_e2v(ji,jj)
+
+         ! masks
+         zmsk00x(ji,jj) = 1._wp - MAX( 0._wp, SIGN( 1._wp, -zmassU ) )  ! 0 if no ice
+         zmsk00y(ji,jj) = 1._wp - MAX( 0._wp, SIGN( 1._wp, -zmassV ) )  ! 0 if no ice
+
+         ! switches
+         IF( zmassU <= zmmin .AND. zaU(ji,jj) <= zamin ) THEN   ;   zmsk01x(ji,jj) = 0._wp
+         ELSE                                                   ;   zmsk01x(ji,jj) = 1._wp   ;   ENDIF
+         IF( zmassV <= zmmin .AND. zaV(ji,jj) <= zamin ) THEN   ;   zmsk01y(ji,jj) = 0._wp
+         ELSE                                                   ;   zmsk01y(ji,jj) = 1._wp   ;   ENDIF
+
+      END_2D
+      CALL lbc_lnk_multi( 'icedyn_rhg_eap', zmf, 'T', 1.0_wp, zdt_m, 'T', 1.0_wp )
+      !
+      !                                  !== Landfast ice parameterization ==!
+      !
+      IF( ln_landfast_L16 ) THEN         !-- Lemieux 2016
+         DO_2D( 0, 0, 0, 0 )
+            ! ice thickness at U-V points
+            zvU = 0.5_wp * ( vt_i(ji,jj) * e1e2t(ji,jj) + vt_i(ji+1,jj) * e1e2t(ji+1,jj) ) * r1_e1e2u(ji,jj) * umask(ji,jj,1)
+            zvV = 0.5_wp * ( vt_i(ji,jj) * e1e2t(ji,jj) + vt_i(ji,jj+1) * e1e2t(ji,jj+1) ) * r1_e1e2v(ji,jj) * vmask(ji,jj,1)
+            ! ice-bottom stress at U points
+            zvCr = zaU(ji,jj) * rn_lf_depfra * hu(ji,jj,Kmm)
+            ztaux_base(ji,jj) = - rn_lf_bfr * MAX( 0._wp, zvU - zvCr ) * EXP( -rn_crhg * ( 1._wp - zaU(ji,jj) ) )
+            ! ice-bottom stress at V points
+            zvCr = zaV(ji,jj) * rn_lf_depfra * hv(ji,jj,Kmm)
+            ztauy_base(ji,jj) = - rn_lf_bfr * MAX( 0._wp, zvV - zvCr ) * EXP( -rn_crhg * ( 1._wp - zaV(ji,jj) ) )
+            ! ice_bottom stress at T points
+            zvCr = at_i(ji,jj) * rn_lf_depfra * ht(ji,jj)
+            tau_icebfr(ji,jj) = - rn_lf_bfr * MAX( 0._wp, vt_i(ji,jj) - zvCr ) * EXP( -rn_crhg * ( 1._wp - at_i(ji,jj) ) )
+         END_2D
+         CALL lbc_lnk( 'icedyn_rhg_eap', tau_icebfr(:,:), 'T', 1.0_wp )
+         !
+      ELSE                               !-- no landfast
+         DO_2D( 0, 0, 0, 0 )
+            ztaux_base(ji,jj) = 0._wp
+            ztauy_base(ji,jj) = 0._wp
+         END_2D
+      ENDIF
+
+      !------------------------------------------------------------------------------!
+      ! 3) Solution of the momentum equation, iterative procedure
+      !------------------------------------------------------------------------------!
+      !
+      !                                               ! ==================== !
+      DO jter = 1 , nn_nevp                           !    loop over jter    !
+         !                                            ! ==================== !        
+         l_full_nf_update = jter == nn_nevp   ! false: disable full North fold update (performances) for iter = 1 to nn_nevp-1
+         !
+         ! convergence test
+         IF( nn_rhg_chkcvg == 1 .OR. nn_rhg_chkcvg == 2  ) THEN
+            DO_2D( nn_hls, nn_hls, nn_hls, nn_hls )
+               zu_ice(ji,jj) = u_ice(ji,jj) * umask(ji,jj,1) ! velocity at previous time step
+               zv_ice(ji,jj) = v_ice(ji,jj) * vmask(ji,jj,1)
+            END_2D
+         ENDIF
+
+         ! --- divergence, tension & shear (Appendix B of Hunke & Dukowicz, 2002) --- !
+         DO_2D( 1, 0, 1, 0 )
+
+            ! shear at F points
+            zds(ji,jj) = ( ( u_ice(ji,jj+1) * r1_e1u(ji,jj+1) - u_ice(ji,jj) * r1_e1u(ji,jj) ) * e1f(ji,jj) * e1f(ji,jj)   &
+               &         + ( v_ice(ji+1,jj) * r1_e2v(ji+1,jj) - v_ice(ji,jj) * r1_e2v(ji,jj) ) * e2f(ji,jj) * e2f(ji,jj)   &
+               &         ) * r1_e1e2f(ji,jj) * zfmask(ji,jj)
+
+         END_2D
+
+         DO_2D( 0, 0, 0, 0 )
+
+            ! shear**2 at T points (doc eq. A16)
+            zds2 = ( zds(ji,jj  ) * zds(ji,jj  ) * e1e2f(ji,jj  ) + zds(ji-1,jj  ) * zds(ji-1,jj  ) * e1e2f(ji-1,jj  )  &
+               &   + zds(ji,jj-1) * zds(ji,jj-1) * e1e2f(ji,jj-1) + zds(ji-1,jj-1) * zds(ji-1,jj-1) * e1e2f(ji-1,jj-1)  &
+               &   ) * 0.25_wp * r1_e1e2t(ji,jj)
+           
+            ! divergence at T points
+            zdiv  = ( e2u(ji,jj) * u_ice(ji,jj) - e2u(ji-1,jj) * u_ice(ji-1,jj)   &
+               &    + e1v(ji,jj) * v_ice(ji,jj) - e1v(ji,jj-1) * v_ice(ji,jj-1)   &
+               &    ) * r1_e1e2t(ji,jj)
+            zdiv2 = zdiv * zdiv
+            
+            ! tension at T points
+            zdt  = ( ( u_ice(ji,jj) * r1_e2u(ji,jj) - u_ice(ji-1,jj) * r1_e2u(ji-1,jj) ) * e2t(ji,jj) * e2t(ji,jj)   &
+               &   - ( v_ice(ji,jj) * r1_e1v(ji,jj) - v_ice(ji,jj-1) * r1_e1v(ji,jj-1) ) * e1t(ji,jj) * e1t(ji,jj)   &
+               &   ) * r1_e1e2t(ji,jj)
+            zdt2 = zdt * zdt
+
+            ! delta at T points
+            zdelta(ji,jj) = SQRT( zdiv2 + ( zdt2 + zds2 ) * z1_ecc2 )  
+
+         END_2D
+         CALL lbc_lnk( 'icedyn_rhg_eap', zdelta, 'T', 1.0_wp )
+               
+         ! P/delta at T points
+         DO_2D( 1, 1, 1, 1 )
+            zp_delt(ji,jj) = strength(ji,jj) / ( zdelta(ji,jj) + rn_creepl )
+         END_2D
+
+         DO_2D( 0, 1, 0, 1 )   ! loop ends at jpi,jpj so that no lbc_lnk are needed for zs1 and zs2
+
+             ! shear at T points 
+            zdsT = ( zds(ji,jj  ) * e1e2f(ji,jj  ) + zds(ji-1,jj  ) * e1e2f(ji-1,jj  )  &
+               &   + zds(ji,jj-1) * e1e2f(ji,jj-1) + zds(ji-1,jj-1) * e1e2f(ji-1,jj-1)  &
+               &   ) * 0.25_wp * r1_e1e2t(ji,jj)
+            
+           ! divergence at T points (duplication to avoid communications)
+            zdiv  = ( e2u(ji,jj) * u_ice(ji,jj) - e2u(ji-1,jj) * u_ice(ji-1,jj)   &
+               &    + e1v(ji,jj) * v_ice(ji,jj) - e1v(ji,jj-1) * v_ice(ji,jj-1)   &
+               &    ) * r1_e1e2t(ji,jj)
+            
+            ! tension at T points (duplication to avoid communications)
+            zdt  = ( ( u_ice(ji,jj) * r1_e2u(ji,jj) - u_ice(ji-1,jj) * r1_e2u(ji-1,jj) ) * e2t(ji,jj) * e2t(ji,jj)   &
+               &   - ( v_ice(ji,jj) * r1_e1v(ji,jj) - v_ice(ji,jj-1) * r1_e1v(ji,jj-1) ) * e1t(ji,jj) * e1t(ji,jj)   &
+               &   ) * r1_e1e2t(ji,jj)
+
+            ! --- anisotropic stress calculation --- !
+            CALL update_stress_rdg (jter, nn_nevp, zdiv, zdt, zdsT, paniso_11(ji,jj), paniso_12(ji,jj), &
+                                    zstressptmp, zstressmtmp, zstress12tmp(ji,jj), strength(ji,jj), zalphar, zalphas)
+
+            ! structure tensor update
+               CALL calc_ffrac(zstressptmp, zstressmtmp, zstress12tmp(ji,jj), paniso_11(ji,jj), paniso_12(ji,jj), zmresult11,  zmresult12)
+
+               paniso_11(ji,jj) = (paniso_11(ji,jj)  + 0.5*2.e-5*zdtevp + zmresult11*zdtevp) / (1. + 2.e-5*zdtevp) ! implicit
+               paniso_12(ji,jj) = (paniso_12(ji,jj)                     + zmresult12*zdtevp) / (1. + 2.e-5*zdtevp) ! implicit
+
+            IF (jter == nn_nevp) THEN
+               zyield11(ji,jj) = 0.5_wp * (zstressptmp + zstressmtmp)
+               zyield22(ji,jj) = 0.5_wp * (zstressptmp - zstressmtmp)
+               zyield12(ji,jj) = zstress12tmp(ji,jj)
+               prdg_conv(ji,jj) = -min( zalphar, 0._wp)    
+            ENDIF
+
+            ! alpha for aEVP
+            !   gamma = 0.5*P/(delta+creepl) * (c*pi)**2/Area * dt/m
+            !   alpha = beta = sqrt(4*gamma)
+            IF( ln_aEVP ) THEN
+               zalph1   = MAX( 50._wp, rpi * SQRT( 0.5_wp * zp_delt(ji,jj) * r1_e1e2t(ji,jj) * zdt_m(ji,jj) ) )
+               z1_alph1 = 1._wp / ( zalph1 + 1._wp )
+               zalph2   = zalph1
+               z1_alph2 = z1_alph1
+               ! explicit:
+               ! z1_alph1 = 1._wp / zalph1
+               ! z1_alph2 = 1._wp / zalph1
+               ! zalph1 = zalph1 - 1._wp
+               ! zalph2 = zalph1
+            ENDIF
+
+            ! stress at T points (zkt/=0 if landfast)
+            zs1(ji,jj) = ( zs1(ji,jj) * zalph1 + zstressptmp ) * z1_alph1
+            zs2(ji,jj) = ( zs2(ji,jj) * zalph1 + zstressmtmp ) * z1_alph1
+         END_2D
+         CALL lbc_lnk_multi( 'icedyn_rhg_eap', zstress12tmp, 'T', 1.0_wp , paniso_11, 'T', 1.0_wp , paniso_12, 'T', 1.0_wp)
+
+        ! Save beta at T-points for further computations
+         IF( ln_aEVP ) THEN
+            DO_2D( 1, 1, 1, 1 )
+               zbeta(ji,jj) = MAX( 50._wp, rpi * SQRT( 0.5_wp * zp_delt(ji,jj) * r1_e1e2t(ji,jj) * zdt_m(ji,jj) ) )
+            END_2D
+         ENDIF
+
+         DO_2D( 1, 0, 1, 0 )
+            ! stress12tmp at F points 
+            zstress12tmpF = ( zstress12tmp(ji,jj+1) * e1e2t(ji,jj+1) + zstress12tmp(ji+1,jj+1) * e1e2t(ji+1,jj+1)  &
+               &            + zstress12tmp(ji,jj  ) * e1e2t(ji,jj  ) + zstress12tmp(ji+1,jj  ) * e1e2t(ji+1,jj  )  &
+               &            ) * 0.25_wp * r1_e1e2f(ji,jj)
+
+            ! alpha for aEVP
+            IF( ln_aEVP ) THEN
+               zalph2   = MAX( zbeta(ji,jj), zbeta(ji+1,jj), zbeta(ji,jj+1), zbeta(ji+1,jj+1) )
+               z1_alph2 = 1._wp / ( zalph2 + 1._wp )
+               ! explicit:
+               ! z1_alph2 = 1._wp / zalph2
+               ! zalph2 = zalph2 - 1._wp
+            ENDIF
+            
+            ! stress at F points (zkt/=0 if landfast)
+            zs12(ji,jj) = ( zs12(ji,jj) * zalph1 + zstress12tmpF ) * z1_alph1
+
+         END_2D
+         CALL lbc_lnk_multi( 'icedyn_rhg_eap', zs1, 'T', 1.0_wp, zs2, 'T', 1.0_wp, zs12, 'F', 1.0_wp )
+
+         ! --- Ice internal stresses (Appendix C of Hunke and Dukowicz, 2002) --- !
+         DO_2D( 0, 0, 0, 0 )
+            !                   !--- U points
+            zfU(ji,jj) = 0.5_wp * ( ( zs1(ji+1,jj) - zs1(ji,jj) ) * e2u(ji,jj)                                             &
+               &                  + ( zs2(ji+1,jj) * e2t(ji+1,jj) * e2t(ji+1,jj) - zs2(ji,jj) * e2t(ji,jj) * e2t(ji,jj)    &
+               &                    ) * r1_e2u(ji,jj)                                                                      &
+               &                  + ( zs12(ji,jj) * e1f(ji,jj) * e1f(ji,jj) - zs12(ji,jj-1) * e1f(ji,jj-1) * e1f(ji,jj-1)  &
+               &                    ) * 2._wp * r1_e1u(ji,jj)                                                              &
+               &                  ) * r1_e1e2u(ji,jj)
+            !
+            !                !--- V points
+            zfV(ji,jj) = 0.5_wp * ( ( zs1(ji,jj+1) - zs1(ji,jj) ) * e1v(ji,jj)                                             &
+               &                  - ( zs2(ji,jj+1) * e1t(ji,jj+1) * e1t(ji,jj+1) - zs2(ji,jj) * e1t(ji,jj) * e1t(ji,jj)    &
+               &                    ) * r1_e1v(ji,jj)                                                                      &
+               &                  + ( zs12(ji,jj) * e2f(ji,jj) * e2f(ji,jj) - zs12(ji-1,jj) * e2f(ji-1,jj) * e2f(ji-1,jj)  &
+               &                    ) * 2._wp * r1_e2v(ji,jj)                                                              &
+               &                  ) * r1_e1e2v(ji,jj)
+            !
+            !                !--- ice currents at U-V point
+            v_iceU(ji,jj) = 0.25_wp * ( v_ice(ji,jj) + v_ice(ji,jj-1) + v_ice(ji+1,jj) + v_ice(ji+1,jj-1) ) * umask(ji,jj,1)
+            u_iceV(ji,jj) = 0.25_wp * ( u_ice(ji,jj) + u_ice(ji-1,jj) + u_ice(ji,jj+1) + u_ice(ji-1,jj+1) ) * vmask(ji,jj,1)
+            !
+         END_2D
+         !
+         ! --- Computation of ice velocity --- !
+         !  Bouillon et al. 2013 (eq 47-48) => unstable unless alpha, beta vary as in Kimmritz 2016 & 2017
+         !  Bouillon et al. 2009 (eq 34-35) => stable
+         IF( MOD(jter,2) == 0 ) THEN ! even iterations
+            !
+            DO_2D( 0, 0, 0, 0 )
+               !                 !--- tau_io/(v_oce - v_ice)
+               zTauO = zaV(ji,jj) * zrhoco * SQRT( ( v_ice (ji,jj) - v_oce (ji,jj) ) * ( v_ice (ji,jj) - v_oce (ji,jj) )  &
+                  &                              + ( u_iceV(ji,jj) - u_oceV(ji,jj) ) * ( u_iceV(ji,jj) - u_oceV(ji,jj) ) )
+               !                 !--- Ocean-to-Ice stress
+               ztauy_oi(ji,jj) = zTauO * ( v_oce(ji,jj) - v_ice(ji,jj) )
+               !
+               !                 !--- tau_bottom/v_ice
+               zvel  = 5.e-05_wp + SQRT( v_ice(ji,jj) * v_ice(ji,jj) + u_iceV(ji,jj) * u_iceV(ji,jj) )
+               zTauB = ztauy_base(ji,jj) / zvel
+               !                 !--- OceanBottom-to-Ice stress
+               ztauy_bi(ji,jj) = zTauB * v_ice(ji,jj)
+               !
+               !                 !--- Coriolis at V-points (energy conserving formulation)
+               zCorV(ji,jj)  = - 0.25_wp * r1_e2v(ji,jj) *  &
+                  &    ( zmf(ji,jj  ) * ( e2u(ji,jj  ) * u_ice(ji,jj  ) + e2u(ji-1,jj  ) * u_ice(ji-1,jj  ) )  &
+                  &    + zmf(ji,jj+1) * ( e2u(ji,jj+1) * u_ice(ji,jj+1) + e2u(ji-1,jj+1) * u_ice(ji-1,jj+1) ) )
+               !
+               !                 !--- Sum of external forces (explicit solution) = F + tau_ia + Coriolis + spg + tau_io
+               zRHS = zfV(ji,jj) + ztauy_ai(ji,jj) + zCorV(ji,jj) + zspgV(ji,jj) + ztauy_oi(ji,jj)
+               !
+               !                 !--- landfast switch => 0 = static  friction : TauB > RHS & sign(TauB) /= sign(RHS)
+               !                                         1 = sliding friction : TauB < RHS
+               rswitch = 1._wp - MIN( 1._wp, ABS( SIGN( 1._wp, zRHS + ztauy_base(ji,jj) ) - SIGN( 1._wp, zRHS ) ) )
+               !
+               IF( ln_aEVP ) THEN !--- ice velocity using aEVP (Kimmritz et al 2016 & 2017)
+                  zbetav = MAX( zbeta(ji,jj), zbeta(ji,jj+1) )
+                  v_ice(ji,jj) = ( (          rswitch   * ( zmV_t(ji,jj) * ( zbetav * v_ice(ji,jj) + v_ice_b(ji,jj) )         & ! previous velocity
+                     &                                    + zRHS + zTauO * v_ice(ji,jj)                                       & ! F + tau_ia + Coriolis + spg + tau_io(only ocean part)
+                     &                                    ) / MAX( zepsi, zmV_t(ji,jj) * ( zbetav + 1._wp ) + zTauO - zTauB ) & ! m/dt + tau_io(only ice part) + landfast
+                     &            + ( 1._wp - rswitch ) * (  v_ice_b(ji,jj)                                                   & 
+                     &                                     + v_ice  (ji,jj) * MAX( 0._wp, zbetav - zdtevp * rn_lf_relax )     & ! static friction => slow decrease to v=0
+                     &                                    ) / ( zbetav + 1._wp )                                              &
+                     &             ) * zmsk01y(ji,jj) + v_oce(ji,jj) * 0.01_wp * ( 1._wp - zmsk01y(ji,jj) )                   & ! v_ice = v_oce/100 if mass < zmmin & conc < zamin
+                     &           )   * zmsk00y(ji,jj)
+               ELSE               !--- ice velocity using EVP implicit formulation (cf Madec doc & Bouillon 2009)
+                  v_ice(ji,jj) = ( (         rswitch   * ( zmV_t(ji,jj)  * v_ice(ji,jj)                                       & ! previous velocity
+                     &                                    + zRHS + zTauO * v_ice(ji,jj)                                       & ! F + tau_ia + Coriolis + spg + tau_io(only ocean part)
+                     &                                    ) / MAX( zepsi, zmV_t(ji,jj) + zTauO - zTauB )                      & ! m/dt + tau_io(only ice part) + landfast
+                     &            + ( 1._wp - rswitch ) *   v_ice(ji,jj) * MAX( 0._wp, 1._wp - zdtevp * rn_lf_relax )         & ! static friction => slow decrease to v=0
+                     &              ) * zmsk01y(ji,jj) + v_oce(ji,jj) * 0.01_wp * ( 1._wp - zmsk01y(ji,jj) )                  & ! v_ice = v_oce/100 if mass < zmmin & conc < zamin
+                     &            )   * zmsk00y(ji,jj)
+               ENDIF
+            END_2D
+            CALL lbc_lnk( 'icedyn_rhg_eap', v_ice, 'V', -1.0_wp )
+            !
+#if defined key_agrif
+!!          CALL agrif_interp_ice( 'V', jter, nn_nevp )
+            CALL agrif_interp_ice( 'V' )
+#endif
+            IF( ln_bdy )   CALL bdy_ice_dyn( 'V' )
+            !
+            DO_2D( 0, 0, 0, 0 )
+               !                 !--- tau_io/(u_oce - u_ice)
+               zTauO = zaU(ji,jj) * zrhoco * SQRT( ( u_ice (ji,jj) - u_oce (ji,jj) ) * ( u_ice (ji,jj) - u_oce (ji,jj) )  &
+                  &                              + ( v_iceU(ji,jj) - v_oceU(ji,jj) ) * ( v_iceU(ji,jj) - v_oceU(ji,jj) ) )
+               !                 !--- Ocean-to-Ice stress
+               ztaux_oi(ji,jj) = zTauO * ( u_oce(ji,jj) - u_ice(ji,jj) )
+               !
+               !                 !--- tau_bottom/u_ice
+               zvel  = 5.e-05_wp + SQRT( v_iceU(ji,jj) * v_iceU(ji,jj) + u_ice(ji,jj) * u_ice(ji,jj) )
+               zTauB = ztaux_base(ji,jj) / zvel
+               !                 !--- OceanBottom-to-Ice stress
+               ztaux_bi(ji,jj) = zTauB * u_ice(ji,jj)
+               !
+               !                 !--- Coriolis at U-points (energy conserving formulation)
+               zCorU(ji,jj)  =   0.25_wp * r1_e1u(ji,jj) *  &
+                  &    ( zmf(ji  ,jj) * ( e1v(ji  ,jj) * v_ice(ji  ,jj) + e1v(ji  ,jj-1) * v_ice(ji  ,jj-1) )  &
+                  &    + zmf(ji+1,jj) * ( e1v(ji+1,jj) * v_ice(ji+1,jj) + e1v(ji+1,jj-1) * v_ice(ji+1,jj-1) ) )
+               !
+               !                 !--- Sum of external forces (explicit solution) = F + tau_ia + Coriolis + spg + tau_io
+               zRHS = zfU(ji,jj) + ztaux_ai(ji,jj) + zCorU(ji,jj) + zspgU(ji,jj) + ztaux_oi(ji,jj)
+               !
+               !                 !--- landfast switch => 0 = static  friction : TauB > RHS & sign(TauB) /= sign(RHS)
+               !                                         1 = sliding friction : TauB < RHS
+               rswitch = 1._wp - MIN( 1._wp, ABS( SIGN( 1._wp, zRHS + ztaux_base(ji,jj) ) - SIGN( 1._wp, zRHS ) ) )
+               !
+               IF( ln_aEVP ) THEN !--- ice velocity using aEVP (Kimmritz et al 2016 & 2017)
+                  zbetau = MAX( zbeta(ji,jj), zbeta(ji+1,jj) )
+                  u_ice(ji,jj) = ( (          rswitch   * ( zmU_t(ji,jj) * ( zbetau * u_ice(ji,jj) + u_ice_b(ji,jj) )         & ! previous velocity
+                     &                                    + zRHS + zTauO * u_ice(ji,jj)                                       & ! F + tau_ia + Coriolis + spg + tau_io(only ocean part)
+                     &                                    ) / MAX( zepsi, zmU_t(ji,jj) * ( zbetau + 1._wp ) + zTauO - zTauB ) & ! m/dt + tau_io(only ice part) + landfast
+                     &            + ( 1._wp - rswitch ) * (  u_ice_b(ji,jj)                                                   &
+                     &                                     + u_ice  (ji,jj) * MAX( 0._wp, zbetau - zdtevp * rn_lf_relax )     & ! static friction => slow decrease to v=0
+                     &                                    ) / ( zbetau + 1._wp )                                              &
+                     &             ) * zmsk01x(ji,jj) + u_oce(ji,jj) * 0.01_wp * ( 1._wp - zmsk01x(ji,jj) )                   & ! v_ice = v_oce/100 if mass < zmmin & conc < zamin 
+                     &           )   * zmsk00x(ji,jj)
+               ELSE               !--- ice velocity using EVP implicit formulation (cf Madec doc & Bouillon 2009)
+                  u_ice(ji,jj) = ( (         rswitch   * ( zmU_t(ji,jj)  * u_ice(ji,jj)                                       & ! previous velocity
+                     &                                    + zRHS + zTauO * u_ice(ji,jj)                                       & ! F + tau_ia + Coriolis + spg + tau_io(only ocean part)
+                     &                                    ) / MAX( zepsi, zmU_t(ji,jj) + zTauO - zTauB )                      & ! m/dt + tau_io(only ice part) + landfast
+                     &            + ( 1._wp - rswitch ) *   u_ice(ji,jj) * MAX( 0._wp, 1._wp - zdtevp * rn_lf_relax )         & ! static friction => slow decrease to v=0
+                     &              ) * zmsk01x(ji,jj) + u_oce(ji,jj) * 0.01_wp * ( 1._wp - zmsk01x(ji,jj) )                  & ! v_ice = v_oce/100 if mass < zmmin & conc < zamin 
+                     &            )   * zmsk00x(ji,jj)
+               ENDIF
+            END_2D
+            CALL lbc_lnk( 'icedyn_rhg_eap', u_ice, 'U', -1.0_wp )
+            !
+#if defined key_agrif
+!!          CALL agrif_interp_ice( 'U', jter, nn_nevp )
+            CALL agrif_interp_ice( 'U' )
+#endif
+            IF( ln_bdy )   CALL bdy_ice_dyn( 'U' )
+            !
+         ELSE ! odd iterations
+            !
+            DO_2D( 0, 0, 0, 0 )
+               !                 !--- tau_io/(u_oce - u_ice)
+               zTauO = zaU(ji,jj) * zrhoco * SQRT( ( u_ice (ji,jj) - u_oce (ji,jj) ) * ( u_ice (ji,jj) - u_oce (ji,jj) )  &
+                  &                              + ( v_iceU(ji,jj) - v_oceU(ji,jj) ) * ( v_iceU(ji,jj) - v_oceU(ji,jj) ) )
+               !                 !--- Ocean-to-Ice stress
+               ztaux_oi(ji,jj) = zTauO * ( u_oce(ji,jj) - u_ice(ji,jj) )
+               !
+               !                 !--- tau_bottom/u_ice
+               zvel  = 5.e-05_wp + SQRT( v_iceU(ji,jj) * v_iceU(ji,jj) + u_ice(ji,jj) * u_ice(ji,jj) )
+               zTauB = ztaux_base(ji,jj) / zvel
+               !                 !--- OceanBottom-to-Ice stress
+               ztaux_bi(ji,jj) = zTauB * u_ice(ji,jj)
+               !
+               !                 !--- Coriolis at U-points (energy conserving formulation)
+               zCorU(ji,jj)  =   0.25_wp * r1_e1u(ji,jj) *  &
+                  &    ( zmf(ji  ,jj) * ( e1v(ji  ,jj) * v_ice(ji  ,jj) + e1v(ji  ,jj-1) * v_ice(ji  ,jj-1) )  &
+                  &    + zmf(ji+1,jj) * ( e1v(ji+1,jj) * v_ice(ji+1,jj) + e1v(ji+1,jj-1) * v_ice(ji+1,jj-1) ) )
+               !
+               !                 !--- Sum of external forces (explicit solution) = F + tau_ia + Coriolis + spg + tau_io
+               zRHS = zfU(ji,jj) + ztaux_ai(ji,jj) + zCorU(ji,jj) + zspgU(ji,jj) + ztaux_oi(ji,jj)
+               !
+               !                 !--- landfast switch => 0 = static  friction : TauB > RHS & sign(TauB) /= sign(RHS)
+               !                                         1 = sliding friction : TauB < RHS
+               rswitch = 1._wp - MIN( 1._wp, ABS( SIGN( 1._wp, zRHS + ztaux_base(ji,jj) ) - SIGN( 1._wp, zRHS ) ) )
+               !
+               IF( ln_aEVP ) THEN !--- ice velocity using aEVP (Kimmritz et al 2016 & 2017)
+                  zbetau = MAX( zbeta(ji,jj), zbeta(ji+1,jj) )
+                  u_ice(ji,jj) = ( (          rswitch   * ( zmU_t(ji,jj) * ( zbetau * u_ice(ji,jj) + u_ice_b(ji,jj) )         & ! previous velocity
+                     &                                    + zRHS + zTauO * u_ice(ji,jj)                                       & ! F + tau_ia + Coriolis + spg + tau_io(only ocean part)
+                     &                                    ) / MAX( zepsi, zmU_t(ji,jj) * ( zbetau + 1._wp ) + zTauO - zTauB ) & ! m/dt + tau_io(only ice part) + landfast
+                     &            + ( 1._wp - rswitch ) * (  u_ice_b(ji,jj)                                                   &
+                     &                                     + u_ice  (ji,jj) * MAX( 0._wp, zbetau - zdtevp * rn_lf_relax )     & ! static friction => slow decrease to v=0
+                     &                                    ) / ( zbetau + 1._wp )                                              &
+                     &             ) * zmsk01x(ji,jj) + u_oce(ji,jj) * 0.01_wp * ( 1._wp - zmsk01x(ji,jj) )                   & ! v_ice = v_oce/100 if mass < zmmin & conc < zamin 
+                     &           )   * zmsk00x(ji,jj)
+               ELSE               !--- ice velocity using EVP implicit formulation (cf Madec doc & Bouillon 2009)
+                  u_ice(ji,jj) = ( (         rswitch   * ( zmU_t(ji,jj)  * u_ice(ji,jj)                                       & ! previous velocity
+                     &                                    + zRHS + zTauO * u_ice(ji,jj)                                       & ! F + tau_ia + Coriolis + spg + tau_io(only ocean part)
+                     &                                    ) / MAX( zepsi, zmU_t(ji,jj) + zTauO - zTauB )                      & ! m/dt + tau_io(only ice part) + landfast
+                     &            + ( 1._wp - rswitch ) *   u_ice(ji,jj) * MAX( 0._wp, 1._wp - zdtevp * rn_lf_relax )         & ! static friction => slow decrease to v=0
+                     &              ) * zmsk01x(ji,jj) + u_oce(ji,jj) * 0.01_wp * ( 1._wp - zmsk01x(ji,jj) )                  & ! v_ice = v_oce/100 if mass < zmmin & conc < zamin
+                     &            )   * zmsk00x(ji,jj)
+               ENDIF
+            END_2D
+            CALL lbc_lnk( 'icedyn_rhg_eap', u_ice, 'U', -1.0_wp )
+            !
+#if defined key_agrif
+!!          CALL agrif_interp_ice( 'U', jter, nn_nevp )
+            CALL agrif_interp_ice( 'U' )
+#endif
+            IF( ln_bdy )   CALL bdy_ice_dyn( 'U' )
+            !
+            DO_2D( 0, 0, 0, 0 )
+               !                 !--- tau_io/(v_oce - v_ice)
+               zTauO = zaV(ji,jj) * zrhoco * SQRT( ( v_ice (ji,jj) - v_oce (ji,jj) ) * ( v_ice (ji,jj) - v_oce (ji,jj) )  &
+                  &                              + ( u_iceV(ji,jj) - u_oceV(ji,jj) ) * ( u_iceV(ji,jj) - u_oceV(ji,jj) ) )
+               !                 !--- Ocean-to-Ice stress
+               ztauy_oi(ji,jj) = zTauO * ( v_oce(ji,jj) - v_ice(ji,jj) )
+               !
+               !                 !--- tau_bottom/v_ice
+               zvel  = 5.e-05_wp + SQRT( v_ice(ji,jj) * v_ice(ji,jj) + u_iceV(ji,jj) * u_iceV(ji,jj) )
+               zTauB = ztauy_base(ji,jj) / zvel
+               !                 !--- OceanBottom-to-Ice stress
+               ztauy_bi(ji,jj) = zTauB * v_ice(ji,jj)
+               !
+               !                 !--- Coriolis at v-points (energy conserving formulation)
+               zCorV(ji,jj)  = - 0.25_wp * r1_e2v(ji,jj) *  &
+                  &    ( zmf(ji,jj  ) * ( e2u(ji,jj  ) * u_ice(ji,jj  ) + e2u(ji-1,jj  ) * u_ice(ji-1,jj  ) )  &
+                  &    + zmf(ji,jj+1) * ( e2u(ji,jj+1) * u_ice(ji,jj+1) + e2u(ji-1,jj+1) * u_ice(ji-1,jj+1) ) )
+               !
+               !                 !--- Sum of external forces (explicit solution) = F + tau_ia + Coriolis + spg + tau_io
+               zRHS = zfV(ji,jj) + ztauy_ai(ji,jj) + zCorV(ji,jj) + zspgV(ji,jj) + ztauy_oi(ji,jj)
+               !
+               !                 !--- landfast switch => 0 = static  friction : TauB > RHS & sign(TauB) /= sign(RHS)
+               !                                         1 = sliding friction : TauB < RHS
+               rswitch = 1._wp - MIN( 1._wp, ABS( SIGN( 1._wp, zRHS + ztauy_base(ji,jj) ) - SIGN( 1._wp, zRHS ) ) )
+               !
+               IF( ln_aEVP ) THEN !--- ice velocity using aEVP (Kimmritz et al 2016 & 2017)
+                  zbetav = MAX( zbeta(ji,jj), zbeta(ji,jj+1) )
+                  v_ice(ji,jj) = ( (          rswitch   * ( zmV_t(ji,jj) * ( zbetav * v_ice(ji,jj) + v_ice_b(ji,jj) )         & ! previous velocity
+                     &                                    + zRHS + zTauO * v_ice(ji,jj)                                       & ! F + tau_ia + Coriolis + spg + tau_io(only ocean part)
+                     &                                    ) / MAX( zepsi, zmV_t(ji,jj) * ( zbetav + 1._wp ) + zTauO - zTauB ) & ! m/dt + tau_io(only ice part) + landfast
+                     &            + ( 1._wp - rswitch ) * (  v_ice_b(ji,jj)                                                   &
+                     &                                     + v_ice  (ji,jj) * MAX( 0._wp, zbetav - zdtevp * rn_lf_relax )     & ! static friction => slow decrease to v=0
+                     &                                    ) / ( zbetav + 1._wp )                                              & 
+                     &             ) * zmsk01y(ji,jj) + v_oce(ji,jj) * 0.01_wp * ( 1._wp - zmsk01y(ji,jj) )                   & ! v_ice = v_oce/100 if mass < zmmin & conc < zamin
+                     &           )   * zmsk00y(ji,jj)
+               ELSE               !--- ice velocity using EVP implicit formulation (cf Madec doc & Bouillon 2009)
+                  v_ice(ji,jj) = ( (         rswitch   * ( zmV_t(ji,jj)  * v_ice(ji,jj)                                       & ! previous velocity
+                     &                                    + zRHS + zTauO * v_ice(ji,jj)                                       & ! F + tau_ia + Coriolis + spg + tau_io(only ocean part)
+                     &                                    ) / MAX( zepsi, zmV_t(ji,jj) + zTauO - zTauB )                      & ! m/dt + tau_io(only ice part) + landfast
+                     &            + ( 1._wp - rswitch ) *   v_ice(ji,jj) * MAX( 0._wp, 1._wp - zdtevp * rn_lf_relax )         & ! static friction => slow decrease to v=0
+                     &              ) * zmsk01y(ji,jj) + v_oce(ji,jj) * 0.01_wp * ( 1._wp - zmsk01y(ji,jj) )                  & ! v_ice = v_oce/100 if mass < zmmin & conc < zamin
+                     &            )   * zmsk00y(ji,jj)
+               ENDIF
+            END_2D
+            CALL lbc_lnk( 'icedyn_rhg_eap', v_ice, 'V', -1.0_wp )
+            !
+#if defined key_agrif
+!!          CALL agrif_interp_ice( 'V', jter, nn_nevp )
+            CALL agrif_interp_ice( 'V' )
+#endif
+            IF( ln_bdy )   CALL bdy_ice_dyn( 'V' )
+            !
+         ENDIF
+
+         ! convergence test
+         IF( nn_rhg_chkcvg == 2 )   CALL rhg_cvg( kt, jter, nn_nevp, u_ice, v_ice, zu_ice, zv_ice, jpi, jpj )
+         !
+         !                                                ! ==================== !
+      END DO                                              !  end loop over jter  !
+      !                                                   ! ==================== !
+      IF( ln_aEVP )   CALL iom_put( 'beta_evp' , zbeta )
+      !
+      CALL lbc_lnk( 'icedyn_rhg_eap', prdg_conv, 'T', 1.0_wp )  ! only need this in ridging module after rheology completed
+      !
+      !------------------------------------------------------------------------------!
+      ! 4) Recompute delta, shear and div (inputs for mechanical redistribution) 
+      !------------------------------------------------------------------------------!
+      DO_2D( 1, 0, 1, 0 )
+
+         ! shear at F points
+         zds(ji,jj) = ( ( u_ice(ji,jj+1) * r1_e1u(ji,jj+1) - u_ice(ji,jj) * r1_e1u(ji,jj) ) * e1f(ji,jj) * e1f(ji,jj)   &
+            &         + ( v_ice(ji+1,jj) * r1_e2v(ji+1,jj) - v_ice(ji,jj) * r1_e2v(ji,jj) ) * e2f(ji,jj) * e2f(ji,jj)   &
+            &         ) * r1_e1e2f(ji,jj) * zfmask(ji,jj)
+
+      END_2D
+      
+      DO_2D( 0, 0, 0, 0 )
+         
+         ! tension**2 at T points
+         zdt  = ( ( u_ice(ji,jj) * r1_e2u(ji,jj) - u_ice(ji-1,jj) * r1_e2u(ji-1,jj) ) * e2t(ji,jj) * e2t(ji,jj)   &
+            &   - ( v_ice(ji,jj) * r1_e1v(ji,jj) - v_ice(ji,jj-1) * r1_e1v(ji,jj-1) ) * e1t(ji,jj) * e1t(ji,jj)   &
+            &   ) * r1_e1e2t(ji,jj)
+         zdt2 = zdt * zdt
+         
+         zten_i(ji,jj) = zdt
+
+         ! shear**2 at T points (doc eq. A16)
+         zds2 = ( zds(ji,jj  ) * zds(ji,jj  ) * e1e2f(ji,jj  ) + zds(ji-1,jj  ) * zds(ji-1,jj  ) * e1e2f(ji-1,jj  )  &
+            &   + zds(ji,jj-1) * zds(ji,jj-1) * e1e2f(ji,jj-1) + zds(ji-1,jj-1) * zds(ji-1,jj-1) * e1e2f(ji-1,jj-1)  &
+            &   ) * 0.25_wp * r1_e1e2t(ji,jj)
+         
+         ! shear at T points
+         pshear_i(ji,jj) = SQRT( zdt2 + zds2 )
+
+         ! divergence at T points
+         pdivu_i(ji,jj) = ( e2u(ji,jj) * u_ice(ji,jj) - e2u(ji-1,jj) * u_ice(ji-1,jj)   &
+            &             + e1v(ji,jj) * v_ice(ji,jj) - e1v(ji,jj-1) * v_ice(ji,jj-1)   &
+            &             ) * r1_e1e2t(ji,jj)
+         
+         ! delta at T points
+         zfac            = SQRT( pdivu_i(ji,jj) * pdivu_i(ji,jj) + ( zdt2 + zds2 ) * z1_ecc2 ) ! delta  
+         rswitch         = 1._wp - MAX( 0._wp, SIGN( 1._wp, -zfac ) ) ! 0 if delta=0
+         pdelta_i(ji,jj) = zfac + rn_creepl * rswitch ! delta+creepl
+
+      END_2D
+      CALL lbc_lnk_multi( 'icedyn_rhg_eap', pshear_i, 'T', 1.0_wp, pdivu_i, 'T', 1.0_wp, pdelta_i, 'T', 1.0_wp, &
+         &                                    zten_i, 'T', 1.0_wp, zs1    , 'T', 1.0_wp, zs2     , 'T', 1.0_wp, &
+         &                                      zs12, 'F', 1.0_wp )
+      
+      ! --- Store the stress tensor for the next time step --- !
+      pstress1_i (:,:) = zs1 (:,:)
+      pstress2_i (:,:) = zs2 (:,:)
+      pstress12_i(:,:) = zs12(:,:)
+      !
+
+      !------------------------------------------------------------------------------!
+      ! 5) diagnostics
+      !------------------------------------------------------------------------------!
+      ! --- ice-ocean, ice-atm. & ice-oceanbottom(landfast) stresses --- !
+      IF(  iom_use('utau_oi') .OR. iom_use('vtau_oi') .OR. iom_use('utau_ai') .OR. iom_use('vtau_ai') .OR. &
+         & iom_use('utau_bi') .OR. iom_use('vtau_bi') ) THEN
+         !
+         CALL lbc_lnk_multi( 'icedyn_rhg_eap', ztaux_oi, 'U', -1.0_wp, ztauy_oi, 'V', -1.0_wp, ztaux_ai, 'U', -1.0_wp, &
+            &                                  ztauy_ai, 'V', -1.0_wp, ztaux_bi, 'U', -1.0_wp, ztauy_bi, 'V', -1.0_wp )
+         !
+         CALL iom_put( 'utau_oi' , ztaux_oi * zmsk00 )
+         CALL iom_put( 'vtau_oi' , ztauy_oi * zmsk00 )
+         CALL iom_put( 'utau_ai' , ztaux_ai * zmsk00 )
+         CALL iom_put( 'vtau_ai' , ztauy_ai * zmsk00 )
+         CALL iom_put( 'utau_bi' , ztaux_bi * zmsk00 )
+         CALL iom_put( 'vtau_bi' , ztauy_bi * zmsk00 )
+      ENDIF
+       
+      ! --- divergence, shear and strength --- !
+      IF( iom_use('icediv') )   CALL iom_put( 'icediv' , pdivu_i  * zmsk00 )   ! divergence
+      IF( iom_use('iceshe') )   CALL iom_put( 'iceshe' , pshear_i * zmsk00 )   ! shear
+      IF( iom_use('icedlt') )   CALL iom_put( 'icedlt' , pdelta_i * zmsk00 )   ! delta
+      IF( iom_use('icestr') )   CALL iom_put( 'icestr' , strength * zmsk00 )   ! strength
+
+      ! --- Stress tensor invariants (SIMIP diags) --- !
+      IF( iom_use('normstr') .OR. iom_use('sheastr') ) THEN
+         !
+         ALLOCATE( zsig_I(jpi,jpj) , zsig_II(jpi,jpj) )
+         !         
+         DO_2D( 1, 1, 1, 1 )
+         
+            ! Ice stresses
+            ! sigma1, sigma2, sigma12 are some useful recombination of the stresses (Hunke and Dukowicz MWR 2002, Bouillon et al., OM2013)
+            ! These are NOT stress tensor components, neither stress invariants, neither stress principal components
+            ! I know, this can be confusing...
+            zfac             =   strength(ji,jj) / ( pdelta_i(ji,jj) + rn_creepl ) 
+            zsig1            =   zfac * ( pdivu_i(ji,jj) - pdelta_i(ji,jj) )
+            zsig2            =   zfac * z1_ecc2 * zten_i(ji,jj)
+            zsig12           =   zfac * z1_ecc2 * pshear_i(ji,jj)
+            
+            ! Stress invariants (sigma_I, sigma_II, Coon 1974, Feltham 2008)
+            zsig_I (ji,jj)   =   zsig1 * 0.5_wp                                           ! 1st stress invariant, aka average normal stress, aka negative pressure
+            zsig_II(ji,jj)   =   SQRT ( MAX( 0._wp, zsig2 * zsig2 * 0.25_wp + zsig12 ) )  ! 2nd  ''       '', aka maximum shear stress
+            
+         END_2D
+         !
+         ! Stress tensor invariants (normal and shear stress N/m) - SIMIP diags - definitions following Coon (1974) and Feltham (2008)
+         IF( iom_use('normstr') )   CALL iom_put( 'normstr', zsig_I (:,:) * zmsk00(:,:) ) ! Normal stress
+         IF( iom_use('sheastr') )   CALL iom_put( 'sheastr', zsig_II(:,:) * zmsk00(:,:) ) ! Maximum shear stress
+         
+         DEALLOCATE ( zsig_I, zsig_II )
+         
+      ENDIF
+
+      ! --- Normalized stress tensor principal components --- !
+      ! This are used to plot the normalized yield curve, see Lemieux & Dupont, 2020
+      ! Recommendation 1 : we use ice strength, not replacement pressure
+      ! Recommendation 2 : need to use deformations at PREVIOUS iterate for viscosities
+      IF( iom_use('sig1_pnorm') .OR. iom_use('sig2_pnorm') ) THEN
+         !
+         ALLOCATE( zsig1_p(jpi,jpj) , zsig2_p(jpi,jpj) , zsig_I(jpi,jpj) , zsig_II(jpi,jpj) )         
+         !         
+         DO_2D( 1, 1, 1, 1 )
+         
+            ! Ice stresses computed with **viscosities** (delta, p/delta) at **previous** iterates 
+            !                        and **deformations** at current iterates
+            !                        following Lemieux & Dupont (2020)
+            zfac             =   zp_delt(ji,jj)
+            zsig1            =   zfac * ( pdivu_i(ji,jj) - ( zdelta(ji,jj) + rn_creepl ) )
+            zsig2            =   zfac * z1_ecc2 * zten_i(ji,jj)
+            zsig12           =   zfac * z1_ecc2 * pshear_i(ji,jj)
+            
+            ! Stress invariants (sigma_I, sigma_II, Coon 1974, Feltham 2008), T-point
+            zsig_I(ji,jj)    =   zsig1 * 0.5_wp                                           ! 1st stress invariant, aka average normal stress, aka negative pressure
+            zsig_II(ji,jj)   =   SQRT ( MAX( 0._wp, zsig2 * zsig2 * 0.25_wp + zsig12 ) )  ! 2nd  ''       '', aka maximum shear stress
+   
+            ! Normalized  principal stresses (used to display the ellipse)
+            z1_strength      =   1._wp / MAX( 1._wp, strength(ji,jj) )
+            zsig1_p(ji,jj)   =   ( zsig_I(ji,jj) + zsig_II(ji,jj) ) * z1_strength
+            zsig2_p(ji,jj)   =   ( zsig_I(ji,jj) - zsig_II(ji,jj) ) * z1_strength
+         END_2D
+         !
+         CALL iom_put( 'sig1_pnorm' , zsig1_p ) 
+         CALL iom_put( 'sig2_pnorm' , zsig2_p ) 
+      
+         DEALLOCATE( zsig1_p , zsig2_p , zsig_I, zsig_II )
+         
+      ENDIF
+
+      ! --- yieldcurve --- !
+      IF( iom_use('yield11') .OR. iom_use('yield12') .OR. iom_use('yield22')) THEN
+
+         CALL lbc_lnk_multi( 'icedyn_rhg_eap', zyield11, 'T', 1.0_wp, zyield22, 'T', 1.0_wp, zyield12, 'T', 1.0_wp )
+
+         CALL iom_put( 'yield11', zyield11 * zmsk00 )
+         CALL iom_put( 'yield22', zyield22 * zmsk00 )
+         CALL iom_put( 'yield12', zyield12 * zmsk00 )
+      ENDIF
+
+      ! --- anisotropy tensor --- !
+      IF( iom_use('aniso') ) THEN       
+         CALL lbc_lnk( 'icedyn_rhg_eap', paniso_11, 'T', 1.0_wp ) 
+         CALL iom_put( 'aniso' , paniso_11 * zmsk00 )
+      ENDIF
+      
+      ! --- SIMIP --- !
+      IF(  iom_use('dssh_dx') .OR. iom_use('dssh_dy') .OR. &
+         & iom_use('corstrx') .OR. iom_use('corstry') .OR. iom_use('intstrx') .OR. iom_use('intstry') ) THEN
+         !
+         CALL lbc_lnk_multi( 'icedyn_rhg_eap', zspgU, 'U', -1.0_wp, zspgV, 'V', -1.0_wp, &
+            &                                  zCorU, 'U', -1.0_wp, zCorV, 'V', -1.0_wp, &
+            &                                    zfU, 'U', -1.0_wp,   zfV, 'V', -1.0_wp )
+
+         CALL iom_put( 'dssh_dx' , zspgU * zmsk00 )   ! Sea-surface tilt term in force balance (x)
+         CALL iom_put( 'dssh_dy' , zspgV * zmsk00 )   ! Sea-surface tilt term in force balance (y)
+         CALL iom_put( 'corstrx' , zCorU * zmsk00 )   ! Coriolis force term in force balance (x)
+         CALL iom_put( 'corstry' , zCorV * zmsk00 )   ! Coriolis force term in force balance (y)
+         CALL iom_put( 'intstrx' , zfU   * zmsk00 )   ! Internal force term in force balance (x)
+         CALL iom_put( 'intstry' , zfV   * zmsk00 )   ! Internal force term in force balance (y)
+      ENDIF
+
+      IF(  iom_use('xmtrpice') .OR. iom_use('ymtrpice') .OR. &
+         & iom_use('xmtrpsnw') .OR. iom_use('ymtrpsnw') .OR. iom_use('xatrp') .OR. iom_use('yatrp') ) THEN
+         !
+         ALLOCATE( zdiag_xmtrp_ice(jpi,jpj) , zdiag_ymtrp_ice(jpi,jpj) , &
+            &      zdiag_xmtrp_snw(jpi,jpj) , zdiag_ymtrp_snw(jpi,jpj) , zdiag_xatrp(jpi,jpj) , zdiag_yatrp(jpi,jpj) )
+         !
+         DO_2D( 0, 0, 0, 0 )
+            ! 2D ice mass, snow mass, area transport arrays (X, Y)
+            zfac_x = 0.5 * u_ice(ji,jj) * e2u(ji,jj) * zmsk00(ji,jj)
+            zfac_y = 0.5 * v_ice(ji,jj) * e1v(ji,jj) * zmsk00(ji,jj)
+
+            zdiag_xmtrp_ice(ji,jj) = rhoi * zfac_x * ( vt_i(ji+1,jj) + vt_i(ji,jj) ) ! ice mass transport, X-component
+            zdiag_ymtrp_ice(ji,jj) = rhoi * zfac_y * ( vt_i(ji,jj+1) + vt_i(ji,jj) ) !        ''           Y-   ''
+
+            zdiag_xmtrp_snw(ji,jj) = rhos * zfac_x * ( vt_s(ji+1,jj) + vt_s(ji,jj) ) ! snow mass transport, X-component
+            zdiag_ymtrp_snw(ji,jj) = rhos * zfac_y * ( vt_s(ji,jj+1) + vt_s(ji,jj) ) !          ''          Y-   ''
+
+            zdiag_xatrp(ji,jj)     = zfac_x * ( at_i(ji+1,jj) + at_i(ji,jj) )        ! area transport,      X-component
+            zdiag_yatrp(ji,jj)     = zfac_y * ( at_i(ji,jj+1) + at_i(ji,jj) )        !        ''            Y-   ''
+
+         END_2D
+
+         CALL lbc_lnk_multi( 'icedyn_rhg_eap', zdiag_xmtrp_ice, 'U', -1.0_wp, zdiag_ymtrp_ice, 'V', -1.0_wp, &
+            &                                  zdiag_xmtrp_snw, 'U', -1.0_wp, zdiag_ymtrp_snw, 'V', -1.0_wp, &
+            &                                  zdiag_xatrp    , 'U', -1.0_wp, zdiag_yatrp    , 'V', -1.0_wp )
+
+         CALL iom_put( 'xmtrpice' , zdiag_xmtrp_ice )   ! X-component of sea-ice mass transport (kg/s)
+         CALL iom_put( 'ymtrpice' , zdiag_ymtrp_ice )   ! Y-component of sea-ice mass transport 
+         CALL iom_put( 'xmtrpsnw' , zdiag_xmtrp_snw )   ! X-component of snow mass transport (kg/s)
+         CALL iom_put( 'ymtrpsnw' , zdiag_ymtrp_snw )   ! Y-component of snow mass transport
+         CALL iom_put( 'xatrp'    , zdiag_xatrp     )   ! X-component of ice area transport
+         CALL iom_put( 'yatrp'    , zdiag_yatrp     )   ! Y-component of ice area transport
+
+         DEALLOCATE( zdiag_xmtrp_ice , zdiag_ymtrp_ice , &
+            &        zdiag_xmtrp_snw , zdiag_ymtrp_snw , zdiag_xatrp , zdiag_yatrp )
+
+      ENDIF
+      !
+      ! --- convergence tests --- !
+      IF( nn_rhg_chkcvg == 1 .OR. nn_rhg_chkcvg == 2 ) THEN
+         IF( iom_use('uice_cvg') ) THEN
+            IF( ln_aEVP ) THEN   ! output: beta * ( u(t=nn_nevp) - u(t=nn_nevp-1) )
+               CALL iom_put( 'uice_cvg', MAX( ABS( u_ice(:,:) - zu_ice(:,:) ) * zbeta(:,:) * umask(:,:,1) , &
+                  &                           ABS( v_ice(:,:) - zv_ice(:,:) ) * zbeta(:,:) * vmask(:,:,1) ) * zmsk15(:,:) )
+            ELSE                 ! output: nn_nevp * ( u(t=nn_nevp) - u(t=nn_nevp-1) )
+               CALL iom_put( 'uice_cvg', REAL( nn_nevp ) * MAX( ABS( u_ice(:,:) - zu_ice(:,:) ) * umask(:,:,1) , &
+                  &                                             ABS( v_ice(:,:) - zv_ice(:,:) ) * vmask(:,:,1) ) * zmsk15(:,:) )
+            ENDIF
+         ENDIF
+      ENDIF      
+      !
+      DEALLOCATE( zmsk00, zmsk15 )
+      !
+   END SUBROUTINE ice_dyn_rhg_eap
+
+   
+   SUBROUTINE rhg_cvg( kt, kiter, kitermax, pu, pv, pub, pvb, jpi, jpj )
+      !!----------------------------------------------------------------------
+      !!                    ***  ROUTINE rhg_cvg  ***
+      !!                     
+      !! ** Purpose :   check convergence of oce rheology
+      !!
+      !! ** Method  :   create a file ice_cvg.nc containing the convergence of ice velocity
+      !!                during the sub timestepping of rheology so as:
+      !!                  uice_cvg = MAX( u(t+1) - u(t) , v(t+1) - v(t) )
+      !!                This routine is called every sub-iteration, so it is cpu expensive
+      !!
+      !! ** Note    :   for the first sub-iteration, uice_cvg is set to 0 (too large otherwise)   
+      !!----------------------------------------------------------------------
+      INTEGER ,                 INTENT(in) ::   kt, kiter, kitermax       ! ocean time-step index
+      INTEGER ,                 INTENT(in) ::   jpi, jpj
+      REAL(wp), DIMENSION(:,:), INTENT(in) ::   pu, pv, pub, pvb          ! now and before velocities
+      !!
+      INTEGER           ::   it, idtime, istatus
+      INTEGER           ::   ji, jj          ! dummy loop indices
+      REAL(wp)          ::   zresm           ! local real 
+      CHARACTER(len=20) ::   clname
+      REAL(wp), DIMENSION(jpi,jpj) ::   zres           ! check convergence
+      !!----------------------------------------------------------------------
+
+      ! create file
+      IF( kt == nit000 .AND. kiter == 1 ) THEN
+         !
+         IF( lwp ) THEN
+            WRITE(numout,*)
+            WRITE(numout,*) 'rhg_cvg : ice rheology convergence control'
+            WRITE(numout,*) '~~~~~~~'
+         ENDIF
+         !
+         IF( lwm ) THEN
+            clname = 'ice_cvg.nc'
+            IF( .NOT. Agrif_Root() )   clname = TRIM(Agrif_CFixed())//"_"//TRIM(clname)
+            istatus = NF90_CREATE( TRIM(clname), NF90_CLOBBER, ncvgid )
+            istatus = NF90_DEF_DIM( ncvgid, 'time'  , NF90_UNLIMITED, idtime )
+            istatus = NF90_DEF_VAR( ncvgid, 'uice_cvg', NF90_DOUBLE   , (/ idtime /), nvarid )
+            istatus = NF90_ENDDEF(ncvgid)
+         ENDIF
+         !
+      ENDIF
+
+      ! time
+      it = ( kt - 1 ) * kitermax + kiter
+      
+      ! convergence
+      IF( kiter == 1 ) THEN ! remove the first iteration for calculations of convergence (always very large)
+         zresm = 0._wp
+      ELSE
+         DO_2D( 1, 1, 1, 1 )
+            zres(ji,jj) = MAX( ABS( pu(ji,jj) - pub(ji,jj) ) * umask(ji,jj,1), &
+               &               ABS( pv(ji,jj) - pvb(ji,jj) ) * vmask(ji,jj,1) ) * zmsk15(ji,jj)
+         END_2D
+         zresm = MAXVAL( zres )
+         CALL mpp_max( 'icedyn_rhg_evp', zresm )   ! max over the global domain
+      ENDIF
+
+      IF( lwm ) THEN
+         ! write variables
+         istatus = NF90_PUT_VAR( ncvgid, nvarid, (/zresm/), (/it/), (/1/) )
+         ! close file
+         IF( kt == nitend )   istatus = NF90_CLOSE(ncvgid)
+      ENDIF
+      
+   END SUBROUTINE rhg_cvg
+
+
+   SUBROUTINE update_stress_rdg( ksub, kndte, pdivu, ptension, pshear, pa11, pa12, &
+      &                          pstressp,  pstressm, pstress12, pstrength, palphar, palphas )
+      !!---------------------------------------------------------------------
+      !!                   ***  SUBROUTINE update_stress_rdg  ***
+      !!                     
+      !! ** Purpose :   Updates the stress depending on values of strain rate and structure
+      !!                tensor and for the last subcycle step it computes closing and sliding rate
+      !!---------------------------------------------------------------------
+      INTEGER,  INTENT(in   ) ::   ksub, kndte
+      REAL(wp), INTENT(in   ) ::   pstrength
+      REAL(wp), INTENT(in   ) ::   pdivu, ptension, pshear
+      REAL(wp), INTENT(in   ) ::   pa11, pa12
+      REAL(wp), INTENT(  out) ::   pstressp, pstressm, pstress12
+      REAL(wp), INTENT(  out) ::   palphar, palphas
+
+      INTEGER  ::   kx ,ky, ka
+
+      REAL(wp) ::   zstemp11r, zstemp12r, zstemp22r   
+      REAL(wp) ::   zstemp11s, zstemp12s, zstemp22s 
+      REAL(wp) ::   za22, zQd11Qd11, zQd11Qd12, zQd12Qd12 
+      REAL(wp) ::   zQ11Q11, zQ11Q12, zQ12Q12 
+      REAL(wp) ::   zdtemp11, zdtemp12, zdtemp22 
+      REAL(wp) ::   zrotstemp11r, zrotstemp12r, zrotstemp22r   
+      REAL(wp) ::   zrotstemp11s, zrotstemp12s, zrotstemp22s
+      REAL(wp) ::   zsig11, zsig12, zsig22 
+      REAL(wp) ::   zsgprm11, zsgprm12, zsgprm22 
+      REAL(wp) ::   zinvstressconviso 
+      REAL(wp) ::   zAngle_denom_gamma,  zAngle_denom_alpha 
+      REAL(wp) ::   zTany_1, zTany_2 
+      REAL(wp) ::   zx, zy, zdx, zdy, zda, zkxw, kyw, kaw
+      REAL(wp) ::   zinvdx, zinvdy, zinvda   
+      REAL(wp) ::   zdtemp1, zdtemp2, zatempprime, zinvsin
+
+      REAL(wp), PARAMETER ::   kfriction = 0.45_wp
+      !!---------------------------------------------------------------------
+      ! Factor to maintain the same stress as in EVP (see Section 3)
+      ! Can be set to 1 otherwise
+!      zinvstressconviso = 1._wp/(1._wp+kfriction*kfriction)
+      zinvstressconviso = 1._wp
+ 
+      zinvsin = 1._wp/sin(2._wp*pphi) * zinvstressconviso 
+      !now uses phi as set in higher code
+       
+      ! compute eigenvalues, eigenvectors and angles for structure tensor, strain
+      ! rates
+
+      ! 1) structure tensor
+      za22 = 1._wp-pa11
+      zQ11Q11 = 1._wp
+      zQ12Q12 = rsmall
+      zQ11Q12 = rsmall
+ 
+      ! gamma: angle between general coordiantes and principal axis of A 
+      ! here Tan2gamma = 2 a12 / (a11 - a22) 
+      IF((ABS(pa11 - za22) > rsmall).OR.(ABS(pa12) > rsmall)) THEN      
+         zAngle_denom_gamma = 1._wp/sqrt( ( pa11 - za22 )*( pa11 - za22) + &
+                              4._wp*pa12*pa12 )
+   
+         zQ11Q11 = 0.5_wp + ( pa11 - za22 )*0.5_wp*zAngle_denom_gamma   !Cos^2 
+         zQ12Q12 = 0.5_wp - ( pa11 - za22 )*0.5_wp*zAngle_denom_gamma   !Sin^2
+         zQ11Q12 = pa12*zAngle_denom_gamma                     !CosSin 
+      ENDIF
+ 
+      ! rotation Q*atemp*Q^T
+      zatempprime = zQ11Q11*pa11 + 2.0_wp*zQ11Q12*pa12 + zQ12Q12*za22 
+      
+      ! make first principal value the largest
+      zatempprime = max(zatempprime, 1.0_wp - zatempprime)
+ 
+      ! 2) strain rate
+      zdtemp11 = 0.5_wp*(pdivu + ptension)
+      zdtemp12 = pshear*0.5_wp
+      zdtemp22 = 0.5_wp*(pdivu - ptension)
+
+      ! here Tan2alpha = 2 dtemp12 / (dtemp11 - dtemp22) 
+
+      zQd11Qd11 = 1.0_wp
+      zQd12Qd12 = rsmall
+      zQd11Qd12 = rsmall
+
+      IF((ABS( zdtemp11 - zdtemp22) > rsmall).OR. (ABS(zdtemp12) > rsmall)) THEN
+
+         zAngle_denom_alpha = 1.0_wp/sqrt( ( zdtemp11 - zdtemp22 )* &
+                             ( zdtemp11 - zdtemp22 ) + 4.0_wp*zdtemp12*zdtemp12)
+
+         zQd11Qd11 = 0.5_wp + ( zdtemp11 - zdtemp22 )*0.5_wp*zAngle_denom_alpha !Cos^2 
+         zQd12Qd12 = 0.5_wp - ( zdtemp11 - zdtemp22 )*0.5_wp*zAngle_denom_alpha !Sin^2
+         zQd11Qd12 = zdtemp12*zAngle_denom_alpha !CosSin
+      ENDIF
+
+      zdtemp1 = zQd11Qd11*zdtemp11 + 2.0_wp*zQd11Qd12*zdtemp12 + zQd12Qd12*zdtemp22
+      zdtemp2 = zQd12Qd12*zdtemp11 - 2.0_wp*zQd11Qd12*zdtemp12 + zQd11Qd11*zdtemp22
+      ! In cos and sin values
+      zx = 0._wp
+      IF ((ABS(zdtemp1) > rsmall).OR.(ABS(zdtemp2) > rsmall)) THEN
+         zx = atan2(zdtemp2,zdtemp1)
+      ENDIF      
+               
+      ! to ensure the angle lies between pi/4 and 9 pi/4 
+      IF (zx < rpi*0.25_wp) zx = zx + rpi*2.0_wp
+      
+      ! y: angle between major principal axis of strain rate and structure
+      ! tensor
+      ! y = gamma - alpha 
+      ! Expressesed componently with
+      ! Tany = (Singamma*Cosgamma - Sinalpha*Cosgamma)/(Cos^2gamma - Sin^alpha)
+      
+      zTany_1 = zQ11Q12 - zQd11Qd12
+      zTany_2 = zQ11Q11 - zQd12Qd12
+      
+      zy = 0._wp
+      
+      IF ((ABS(zTany_1) > rsmall).OR.(ABS(zTany_2) > rsmall)) THEN
+         zy = atan2(zTany_1,zTany_2)
+      ENDIF
+      
+      ! to make sure y is between 0 and pi
+      IF (zy > rpi) zy = zy - rpi
+      IF (zy < 0)  zy = zy + rpi
+
+      ! 3) update anisotropic stress tensor 
+      zdx   = rpi/real(nx_yield-1,kind=wp)
+      zdy   = rpi/real(ny_yield-1,kind=wp)
+      zda   = 0.5_wp/real(na_yield-1,kind=wp)
+      zinvdx = 1._wp/zdx
+      zinvdy = 1._wp/zdy
+      zinvda = 1._wp/zda
+
+      ! % need 8 coords and 8 weights
+      ! % range in kx
+      kx  = int((zx-rpi*0.25_wp-rpi)*zinvdx) + 1
+      !!clem kx  = MAX( 1, MIN( nx_yield-1, INT((zx-rpi*0.25_wp-rpi)*zinvdx) + 1  ) )
+      zkxw = kx - (zx-rpi*0.25_wp-rpi)*zinvdx  
+      
+      ky  = int(zy*zinvdy) + 1
+      !!clem ky  = MAX( 1, MIN( ny_yield-1, INT(zy*zinvdy) + 1 ) )
+      kyw = ky - zy*zinvdy 
+      
+      ka  = int((zatempprime-0.5_wp)*zinvda) + 1
+      !!clem ka  = MAX( 1, MIN( na_yield-1, INT((zatempprime-0.5_wp)*zinvda) + 1 ) )
+      kaw = ka - (zatempprime-0.5_wp)*zinvda
+      
+      ! % Determine sigma_r(A1,Zeta,y) and sigma_s (see Section A1 of Tsamados 2013)
+!!$      zstemp11r =  zkxw  * kyw         * kaw         * s11r(kx  ,ky  ,ka  ) &
+!!$        & + (1._wp-zkxw) * kyw         * kaw         * s11r(kx+1,ky  ,ka  ) &
+!!$        & + zkxw         * (1._wp-kyw) * kaw         * s11r(kx  ,ky+1,ka  ) &
+!!$        & + zkxw         * kyw         * (1._wp-kaw) * s11r(kx  ,ky  ,ka+1) &
+!!$        & + (1._wp-zkxw) * (1._wp-kyw) * kaw         * s11r(kx+1,ky+1,ka  ) &
+!!$        & + (1._wp-zkxw) * kyw         * (1._wp-kaw) * s11r(kx+1,ky  ,ka+1) &
+!!$        & + zkxw         * (1._wp-kyw) * (1._wp-kaw) * s11r(kx  ,ky+1,ka+1) &
+!!$        & + (1._wp-zkxw) * (1._wp-kyw) * (1._wp-kaw) * s11r(kx+1,ky+1,ka+1)
+!!$      zstemp12r =  zkxw  * kyw         * kaw         * s12r(kx  ,ky  ,ka  ) &
+!!$        & + (1._wp-zkxw) * kyw         * kaw         * s12r(kx+1,ky  ,ka  ) &
+!!$        & + zkxw         * (1._wp-kyw) * kaw         * s12r(kx  ,ky+1,ka  ) &
+!!$        & + zkxw         * kyw         * (1._wp-kaw) * s12r(kx  ,ky  ,ka+1) &
+!!$        & + (1._wp-zkxw) * (1._wp-kyw) * kaw         * s12r(kx+1,ky+1,ka  ) &
+!!$        & + (1._wp-zkxw) * kyw         * (1._wp-kaw) * s12r(kx+1,ky  ,ka+1) &
+!!$        & + zkxw         * (1._wp-kyw) * (1._wp-kaw) * s12r(kx  ,ky+1,ka+1) &
+!!$        & + (1._wp-zkxw) * (1._wp-kyw) * (1._wp-kaw) * s12r(kx+1,ky+1,ka+1)
+!!$      zstemp22r =  zkxw  * kyw         * kaw         * s22r(kx  ,ky  ,ka  ) &
+!!$        & + (1._wp-zkxw) * kyw         * kaw         * s22r(kx+1,ky  ,ka  ) &
+!!$        & + zkxw         * (1._wp-kyw) * kaw         * s22r(kx  ,ky+1,ka  ) &
+!!$        & + zkxw         * kyw         * (1._wp-kaw) * s22r(kx  ,ky  ,ka+1) &
+!!$        & + (1._wp-zkxw) * (1._wp-kyw) * kaw         * s22r(kx+1,ky+1,ka  ) &
+!!$        & + (1._wp-zkxw) * kyw         * (1._wp-kaw) * s22r(kx+1,ky  ,ka+1) &
+!!$        & + zkxw         * (1._wp-kyw) * (1._wp-kaw) * s22r(kx  ,ky+1,ka+1) &
+!!$        & + (1._wp-zkxw) * (1._wp-kyw) * (1._wp-kaw) * s22r(kx+1,ky+1,ka+1)
+!!$      
+!!$      zstemp11s =  zkxw  * kyw         * kaw         * s11s(kx  ,ky  ,ka  ) &
+!!$        & + (1._wp-zkxw) * kyw         * kaw         * s11s(kx+1,ky  ,ka  ) &
+!!$        & + zkxw         * (1._wp-kyw) * kaw         * s11s(kx  ,ky+1,ka  ) &
+!!$        & + zkxw         * kyw         * (1._wp-kaw) * s11s(kx  ,ky  ,ka+1) &
+!!$        & + (1._wp-zkxw) * (1._wp-kyw) * kaw         * s11s(kx+1,ky+1,ka  ) &
+!!$        & + (1._wp-zkxw) * kyw         * (1._wp-kaw) * s11s(kx+1,ky  ,ka+1) &
+!!$        & + zkxw         * (1._wp-kyw) * (1._wp-kaw) * s11s(kx  ,ky+1,ka+1) &
+!!$        & + (1._wp-zkxw) * (1._wp-kyw) * (1._wp-kaw) * s11s(kx+1,ky+1,ka+1)
+!!$      zstemp12s =  zkxw  * kyw         * kaw         * s12s(kx  ,ky  ,ka  ) &
+!!$        & + (1._wp-zkxw) * kyw         * kaw         * s12s(kx+1,ky  ,ka  ) &
+!!$        & + zkxw         * (1._wp-kyw) * kaw         * s12s(kx  ,ky+1,ka  ) &
+!!$        & + zkxw         * kyw         * (1._wp-kaw) * s12s(kx  ,ky  ,ka+1) &
+!!$        & + (1._wp-zkxw) * (1._wp-kyw) * kaw         * s12s(kx+1,ky+1,ka  ) &
+!!$        & + (1._wp-zkxw) * kyw         * (1._wp-kaw) * s12s(kx+1,ky  ,ka+1) &
+!!$        & + zkxw         * (1._wp-kyw) * (1._wp-kaw) * s12s(kx  ,ky+1,ka+1) &
+!!$        & + (1._wp-zkxw) * (1._wp-kyw) * (1._wp-kaw) * s12s(kx+1,ky+1,ka+1)
+!!$      zstemp22s =  zkxw  * kyw         * kaw         * s22s(kx  ,ky  ,ka  ) &
+!!$        & + (1._wp-zkxw) * kyw         * kaw         * s22s(kx+1,ky  ,ka  ) &
+!!$        & + zkxw         * (1._wp-kyw) * kaw         * s22s(kx  ,ky+1,ka  ) &
+!!$        & + zkxw         * kyw         * (1._wp-kaw) * s22s(kx  ,ky  ,ka+1) &
+!!$        & + (1._wp-zkxw) * (1._wp-kyw) * kaw         * s22s(kx+1,ky+1,ka  ) &
+!!$        & + (1._wp-zkxw) * kyw         * (1._wp-kaw) * s22s(kx+1,ky  ,ka+1) &
+!!$        & + zkxw         * (1._wp-kyw) * (1._wp-kaw) * s22s(kx  ,ky+1,ka+1) &
+!!$        & + (1._wp-zkxw) * (1._wp-kyw) * (1._wp-kaw) * s22s(kx+1,ky+1,ka+1)
+
+      zstemp11r = s11r(kx,ky,ka)
+      zstemp12r = s12r(kx,ky,ka)
+      zstemp22r = s22r(kx,ky,ka)
+      zstemp11s = s11s(kx,ky,ka)
+      zstemp12s = s12s(kx,ky,ka)
+      zstemp22s = s22s(kx,ky,ka)
+      
+      
+      ! Calculate mean ice stress over a collection of floes (Equation 3 in
+      ! Tsamados 2013)
+
+      zsig11  = pstrength*(zstemp11r + kfriction*zstemp11s) * zinvsin
+      zsig12  = pstrength*(zstemp12r + kfriction*zstemp12s) * zinvsin
+      zsig22  = pstrength*(zstemp22r + kfriction*zstemp22s) * zinvsin
+
+      ! Back - rotation of the stress from principal axes into general coordinates
+
+      ! Update stress
+      zsgprm11 = zQ11Q11*zsig11 + zQ12Q12*zsig22 -      2._wp*zQ11Q12 *zsig12
+      zsgprm12 = zQ11Q12*zsig11 - zQ11Q12*zsig22 + (zQ11Q11 - zQ12Q12)*zsig12
+      zsgprm22 = zQ12Q12*zsig11 + zQ11Q11*zsig22 +      2._wp*zQ11Q12 *zsig12
+
+      pstressp  = zsgprm11 + zsgprm22
+      pstress12 = zsgprm12
+      pstressm  = zsgprm11 - zsgprm22
+
+      ! Compute ridging and sliding functions in general coordinates 
+      ! (Equation 11 in Tsamados 2013)
+      IF (ksub == kndte) THEN
+         zrotstemp11r = zQ11Q11*zstemp11r - 2._wp*zQ11Q12* zstemp12r &
+                      + zQ12Q12*zstemp22r
+         zrotstemp12r = zQ11Q11*zstemp12r +       zQ11Q12*(zstemp11r-zstemp22r) &
+                      - zQ12Q12*zstemp12r
+         zrotstemp22r = zQ12Q12*zstemp11r + 2._wp*zQ11Q12* zstemp12r & 
+                      + zQ11Q11*zstemp22r
+
+         zrotstemp11s = zQ11Q11*zstemp11s - 2._wp*zQ11Q12* zstemp12s &
+                      + zQ12Q12*zstemp22s
+         zrotstemp12s = zQ11Q11*zstemp12s +       zQ11Q12*(zstemp11s-zstemp22s) &
+                      - zQ12Q12*zstemp12s
+         zrotstemp22s = zQ12Q12*zstemp11s + 2._wp*zQ11Q12* zstemp12s & 
+                      + zQ11Q11*zstemp22s
+
+         palphar =  zrotstemp11r*zdtemp11 + 2._wp*zrotstemp12r*zdtemp12 &
+                  + zrotstemp22r*zdtemp22
+         palphas =  zrotstemp11s*zdtemp11 + 2._wp*zrotstemp12s*zdtemp12 &
+                  + zrotstemp22s*zdtemp22
+      ENDIF
+   END SUBROUTINE update_stress_rdg 
+
+!=======================================================================
+
+
+   SUBROUTINE calc_ffrac( pstressp, pstressm, pstress12, pa11, pa12, &
+      &                   pmresult11, pmresult12 )
+      !!---------------------------------------------------------------------
+      !!                   ***  ROUTINE calc_ffrac  ***
+      !!                     
+      !! ** Purpose :   Computes term in evolution equation for structure tensor
+      !!                which determines the ice floe re-orientation due to fracture
+      !! ** Method :    Eq. 7: Ffrac = -kf(A-S) or = 0 depending on sigma_1 and sigma_2
+      !!---------------------------------------------------------------------
+      REAL (wp), INTENT(in)  ::   pstressp, pstressm, pstress12, pa11, pa12
+      REAL (wp), INTENT(out) ::   pmresult11, pmresult12
+
+      ! local variables
+
+      REAL (wp) ::   zsigma11, zsigma12, zsigma22  ! stress tensor elements
+      REAL (wp) ::   zAngle_denom        ! angle with principal component axis
+      REAL (wp) ::   zsigma_1, zsigma_2   ! principal components of stress
+      REAL (wp) ::   zQ11, zQ12, zQ11Q11, zQ11Q12, zQ12Q12
+
+!!$      REAL (wp), PARAMETER ::   kfrac = 0.0001_wp   ! rate of fracture formation 
+      REAL (wp), PARAMETER ::   kfrac = 1.e-3_wp   ! rate of fracture formation 
+      REAL (wp), PARAMETER ::   threshold = 0.3_wp  ! critical confinement ratio 
+      !!---------------------------------------------------------------
+      !
+      zsigma11 = 0.5_wp*(pstressp+pstressm) 
+      zsigma12 = pstress12 
+      zsigma22 = 0.5_wp*(pstressp-pstressm) 
+
+      ! Here's the change - no longer calculate gamma,
+      ! use trig formulation, angle signs are kept correct, don't worry
+   
+      ! rotate tensor to get into sigma principal axis
+   
+      ! here Tan2gamma = 2 sig12 / (sig11 - sig12) 
+      ! add rsmall to the denominator to stop 1/0 errors, makes very little 
+      ! error to the calculated angles < rsmall
+
+      zQ11Q11 = 0.1_wp
+      zQ12Q12 = rsmall
+      zQ11Q12 = rsmall
+
+      IF((ABS( zsigma11 - zsigma22) > rsmall).OR.(ABS(zsigma12) > rsmall)) THEN
+
+         zAngle_denom = 1.0_wp/sqrt( ( zsigma11 - zsigma22 )*( zsigma11 - &
+                       zsigma22 ) + 4.0_wp*zsigma12*zsigma12)
+
+         zQ11Q11 = 0.5_wp + ( zsigma11 - zsigma22 )*0.5_wp*zAngle_denom   !Cos^2 
+         zQ12Q12 = 0.5_wp - ( zsigma11 - zsigma22 )*0.5_wp*zAngle_denom   !Sin^2
+         zQ11Q12 = zsigma12*zAngle_denom                      !CosSin 
+      ENDIF
+
+      zsigma_1 = zQ11Q11*zsigma11 + 2.0_wp*zQ11Q12*zsigma12 + zQ12Q12*zsigma22 ! S(1,1)
+      zsigma_2 = zQ12Q12*zsigma11 - 2.0_wp*zQ11Q12*zsigma12 + zQ11Q11*zsigma22 ! S(2,2)
+
+      ! Pure divergence
+      IF ((zsigma_1 >= 0.0_wp).AND.(zsigma_2 >= 0.0_wp))  THEN
+         pmresult11 = 0.0_wp
+         pmresult12 = 0.0_wp
+
+      ! Unconfined compression: cracking of blocks not along the axial splitting
+      ! direction
+      ! which leads to the loss of their shape, so we again model it through diffusion
+      ELSEIF ((zsigma_1 >= 0.0_wp).AND.(zsigma_2 < 0.0_wp))  THEN
+         pmresult11 = - kfrac * (pa11 - zQ12Q12)
+         pmresult12 = - kfrac * (pa12 + zQ11Q12)
+
+      ! Shear faulting
+      ELSEIF (zsigma_2 == 0.0_wp) THEN
+         pmresult11 = 0.0_wp
+         pmresult12 = 0.0_wp
+      ELSEIF ((zsigma_1 <= 0.0_wp).AND.(zsigma_1/zsigma_2 <= threshold)) THEN
+         pmresult11 = - kfrac * (pa11 - zQ12Q12)
+         pmresult12 = - kfrac * (pa12 + zQ11Q12)
+
+      ! Horizontal spalling
+      ELSE 
+         pmresult11 = 0.0_wp
+         pmresult12 = 0.0_wp
+      ENDIF
+
+   END SUBROUTINE calc_ffrac
+
+
+   SUBROUTINE rhg_eap_rst( cdrw, kt )
+      !!---------------------------------------------------------------------
+      !!                   ***  ROUTINE rhg_eap_rst  ***
+      !!                     
+      !! ** Purpose :   Read or write RHG file in restart file
+      !!      
+      !! ** Method  :   use of IOM library
+      !!----------------------------------------------------------------------
+      CHARACTER(len=*) , INTENT(in) ::   cdrw   ! "READ"/"WRITE" flag
+      INTEGER, OPTIONAL, INTENT(in) ::   kt     ! ice time-step
+      !
+      INTEGER  ::   iter                      ! local integer
+      INTEGER  ::   id1, id2, id3, id4, id5   ! local integers
+      INTEGER  ::   ix, iy, ip, iz, n, ia     ! local integers
+    
+      INTEGER, PARAMETER            ::    nz = 100
+      
+      REAL(wp) ::   ainit, xinit, yinit, pinit, zinit
+      REAL(wp) ::   da, dx, dy, dp, dz, a1
+
+      !!clem
+      REAL(wp) ::   zw1, zw2, zfac, ztemp
+      REAL(wp) ::   idx, idy, idz
+
+      REAL(wp), PARAMETER           ::   eps6 = 1.0e-6_wp
+      !!----------------------------------------------------------------------
+      !
+      IF( TRIM(cdrw) == 'READ' ) THEN        ! Read/initialize
+         !                                   ! ---------------
+         IF( ln_rstart ) THEN                   !* Read the restart file
+            !
+            id1 = iom_varid( numrir, 'stress1_i' , ldstop = .FALSE. )
+            id2 = iom_varid( numrir, 'stress2_i' , ldstop = .FALSE. )
+            id3 = iom_varid( numrir, 'stress12_i', ldstop = .FALSE. )
+            id4 = iom_varid( numrir, 'aniso_11'  , ldstop = .FALSE. )
+            id5 = iom_varid( numrir, 'aniso_12'  , ldstop = .FALSE. )
+            !
+            IF( MIN( id1, id2, id3, id4, id5 ) > 0 ) THEN      ! fields exist
+               CALL iom_get( numrir, jpdom_auto, 'stress1_i' , stress1_i , cd_type = 'T' )
+               CALL iom_get( numrir, jpdom_auto, 'stress2_i' , stress2_i , cd_type = 'T' )
+               CALL iom_get( numrir, jpdom_auto, 'stress12_i', stress12_i, cd_type = 'F' )
+               CALL iom_get( numrir, jpdom_auto, 'aniso_11'  , aniso_11  , cd_type = 'T' )
+               CALL iom_get( numrir, jpdom_auto, 'aniso_12'  , aniso_12  , cd_type = 'T' )
+            ELSE                                     ! start rheology from rest
+               IF(lwp) WRITE(numout,*)
+               IF(lwp) WRITE(numout,*) '   ==>>>   previous run without rheology, set stresses to 0'
+               stress1_i (:,:) = 0._wp
+               stress2_i (:,:) = 0._wp
+               stress12_i(:,:) = 0._wp
+               aniso_11  (:,:) = 0.5_wp
+               aniso_12  (:,:) = 0._wp
+            ENDIF
+         ELSE                                   !* Start from rest
+            IF(lwp) WRITE(numout,*)
+            IF(lwp) WRITE(numout,*) '   ==>>>   start from rest: set stresses to 0'
+            stress1_i (:,:) = 0._wp
+            stress2_i (:,:) = 0._wp
+            stress12_i(:,:) = 0._wp
+            aniso_11  (:,:) = 0.5_wp
+            aniso_12  (:,:) = 0._wp
+         ENDIF
+         !
+
+         da = 0.5_wp/real(na_yield-1,kind=wp)
+         ainit = 0.5_wp - da
+         dx = rpi/real(nx_yield-1,kind=wp)
+         xinit = rpi + 0.25_wp*rpi - dx
+         dz = rpi/real(nz,kind=wp)
+         zinit = -rpi*0.5_wp
+         dy = rpi/real(ny_yield-1,kind=wp)
+         yinit = -dy
+
+         s11r(:,:,:) = 0._wp
+         s12r(:,:,:) = 0._wp
+         s22r(:,:,:) = 0._wp
+         s11s(:,:,:) = 0._wp
+         s12s(:,:,:) = 0._wp
+         s22s(:,:,:) = 0._wp
+
+!!$         DO ia=1,na_yield
+!!$            DO ix=1,nx_yield
+!!$               DO iy=1,ny_yield
+!!$                  s11r(ix,iy,ia) = 0._wp
+!!$                  s12r(ix,iy,ia) = 0._wp
+!!$                  s22r(ix,iy,ia) = 0._wp
+!!$                  s11s(ix,iy,ia) = 0._wp
+!!$                  s12s(ix,iy,ia) = 0._wp
+!!$                  s22s(ix,iy,ia) = 0._wp
+!!$                  IF (ia <= na_yield-1) THEN
+!!$                     DO iz=1,nz
+!!$                        s11r(ix,iy,ia) = s11r(ix,iy,ia) + 1*w1(ainit+ia*da)* &
+!!$                           exp(-w2(ainit+ia*da)*(zinit+iz*dz)*(zinit+iz*dz))* &
+!!$                           s11kr(xinit+ix*dx,yinit+iy*dy,zinit+iz*dz)*dz/sin(2._wp*pphi)
+!!$                        s12r(ix,iy,ia) = s12r(ix,iy,ia) + 1*w1(ainit+ia*da)* &
+!!$                           exp(-w2(ainit+ia*da)*(zinit+iz*dz)*(zinit+iz*dz))* &
+!!$                           s12kr(xinit+ix*dx,yinit+iy*dy,zinit+iz*dz)*dz/sin(2._wp*pphi)
+!!$                        s22r(ix,iy,ia) = s22r(ix,iy,ia) + 1*w1(ainit+ia*da)* &
+!!$                           exp(-w2(ainit+ia*da)*(zinit+iz*dz)*(zinit+iz*dz))* &
+!!$                           s22kr(xinit+ix*dx,yinit+iy*dy,zinit+iz*dz)*dz/sin(2._wp*pphi) 
+!!$                        s11s(ix,iy,ia) = s11s(ix,iy,ia) + 1*w1(ainit+ia*da)* &
+!!$                           exp(-w2(ainit+ia*da)*(zinit+iz*dz)*(zinit+iz*dz))* &
+!!$                           s11ks(xinit+ix*dx,yinit+iy*dy,zinit+iz*dz)*dz/sin(2._wp*pphi)
+!!$                        s12s(ix,iy,ia) = s12s(ix,iy,ia) + 1*w1(ainit+ia*da)* &
+!!$                           exp(-w2(ainit+ia*da)*(zinit+iz*dz)*(zinit+iz*dz))* &
+!!$                           s12ks(xinit+ix*dx,yinit+iy*dy,zinit+iz*dz)*dz/sin(2._wp*pphi)
+!!$                        s22s(ix,iy,ia) = s22s(ix,iy,ia) + 1*w1(ainit+ia*da)* &
+!!$                           exp(-w2(ainit+ia*da)*(zinit+iz*dz)*(zinit+iz*dz))* &
+!!$                           s22ks(xinit+ix*dx,yinit+iy*dy,zinit+iz*dz)*dz/sin(2._wp*pphi)
+!!$                     ENDDO
+!!$                     IF (abs(s11r(ix,iy,ia)) < eps6) s11r(ix,iy,ia) = 0._wp
+!!$                     IF (abs(s12r(ix,iy,ia)) < eps6) s12r(ix,iy,ia) = 0._wp
+!!$                     IF (abs(s22r(ix,iy,ia)) < eps6) s22r(ix,iy,ia) = 0._wp
+!!$                     IF (abs(s11s(ix,iy,ia)) < eps6) s11s(ix,iy,ia) = 0._wp
+!!$                     IF (abs(s12s(ix,iy,ia)) < eps6) s12s(ix,iy,ia) = 0._wp
+!!$                     IF (abs(s22s(ix,iy,ia)) < eps6) s22s(ix,iy,ia) = 0._wp
+!!$                  ELSE
+!!$                     s11r(ix,iy,ia) = 0.5_wp*s11kr(xinit+ix*dx,yinit+iy*dy,0._wp)/sin(2._wp*pphi)
+!!$                     s12r(ix,iy,ia) = 0.5_wp*s12kr(xinit+ix*dx,yinit+iy*dy,0._wp)/sin(2._wp*pphi)
+!!$                     s22r(ix,iy,ia) = 0.5_wp*s22kr(xinit+ix*dx,yinit+iy*dy,0._wp)/sin(2._wp*pphi)
+!!$                     s11s(ix,iy,ia) = 0.5_wp*s11ks(xinit+ix*dx,yinit+iy*dy,0._wp)/sin(2._wp*pphi)
+!!$                     s12s(ix,iy,ia) = 0.5_wp*s12ks(xinit+ix*dx,yinit+iy*dy,0._wp)/sin(2._wp*pphi)
+!!$                     s22s(ix,iy,ia) = 0.5_wp*s22ks(xinit+ix*dx,yinit+iy*dy,0._wp)/sin(2._wp*pphi)
+!!$                     IF (abs(s11r(ix,iy,ia)) < eps6) s11r(ix,iy,ia) = 0._wp
+!!$                     IF (abs(s12r(ix,iy,ia)) < eps6) s12r(ix,iy,ia) = 0._wp
+!!$                     IF (abs(s22r(ix,iy,ia)) < eps6) s22r(ix,iy,ia) = 0._wp
+!!$                     IF (abs(s11s(ix,iy,ia)) < eps6) s11s(ix,iy,ia) = 0._wp
+!!$                     IF (abs(s12s(ix,iy,ia)) < eps6) s12s(ix,iy,ia) = 0._wp
+!!$                     IF (abs(s22s(ix,iy,ia)) < eps6) s22s(ix,iy,ia) = 0._wp
+!!$                  ENDIF
+!!$               ENDDO
+!!$            ENDDO
+!!$         ENDDO
+
+         !! faster but still very slow => to be improved         
+         zfac = dz/sin(2._wp*pphi)
+         DO ia = 1, na_yield-1
+            zw1 = w1(ainit+ia*da)
+            zw2 = w2(ainit+ia*da)
+            DO iz = 1, nz
+               idz = zinit+iz*dz
+               ztemp = zw1 * EXP(-zw2*(zinit+iz*dz)*(zinit+iz*dz))
+               DO iy = 1, ny_yield
+                  idy = yinit+iy*dy
+                  DO ix = 1, nx_yield
+                     idx = xinit+ix*dx
+                     s11r(ix,iy,ia) = s11r(ix,iy,ia) + ztemp * s11kr(idx,idy,idz)*zfac
+                     s12r(ix,iy,ia) = s12r(ix,iy,ia) + ztemp * s12kr(idx,idy,idz)*zfac
+                     s22r(ix,iy,ia) = s22r(ix,iy,ia) + ztemp * s22kr(idx,idy,idz)*zfac 
+                     s11s(ix,iy,ia) = s11s(ix,iy,ia) + ztemp * s11ks(idx,idy,idz)*zfac
+                     s12s(ix,iy,ia) = s12s(ix,iy,ia) + ztemp * s12ks(idx,idy,idz)*zfac
+                     s22s(ix,iy,ia) = s22s(ix,iy,ia) + ztemp * s22ks(idx,idy,idz)*zfac
+                  END DO
+               END DO
+            END DO
+         END DO
+
+         zfac = 1._wp/sin(2._wp*pphi)
+         ia = na_yield
+         DO iy = 1, ny_yield
+            idy = yinit+iy*dy
+            DO ix = 1, nx_yield
+               idx = xinit+ix*dx                  
+               s11r(ix,iy,ia) = 0.5_wp*s11kr(idx,idy,0._wp)*zfac
+               s12r(ix,iy,ia) = 0.5_wp*s12kr(idx,idy,0._wp)*zfac
+               s22r(ix,iy,ia) = 0.5_wp*s22kr(idx,idy,0._wp)*zfac
+               s11s(ix,iy,ia) = 0.5_wp*s11ks(idx,idy,0._wp)*zfac
+               s12s(ix,iy,ia) = 0.5_wp*s12ks(idx,idy,0._wp)*zfac
+               s22s(ix,iy,ia) = 0.5_wp*s22ks(idx,idy,0._wp)*zfac
+            ENDDO
+         ENDDO
+         WHERE (ABS(s11r(:,:,:)) < eps6) s11r(:,:,:) = 0._wp
+         WHERE (ABS(s12r(:,:,:)) < eps6) s12r(:,:,:) = 0._wp
+         WHERE (ABS(s22r(:,:,:)) < eps6) s22r(:,:,:) = 0._wp
+         WHERE (ABS(s11s(:,:,:)) < eps6) s11s(:,:,:) = 0._wp
+         WHERE (ABS(s12s(:,:,:)) < eps6) s12s(:,:,:) = 0._wp
+         WHERE (ABS(s22s(:,:,:)) < eps6) s22s(:,:,:) = 0._wp
+
+
+      ELSEIF( TRIM(cdrw) == 'WRITE' ) THEN   ! Create restart file
+         !                                   ! -------------------
+         IF(lwp) WRITE(numout,*) '---- rhg-rst ----'
+         iter = kt + nn_fsbc - 1             ! ice restarts are written at kt == nitrst - nn_fsbc + 1
+         !
+         CALL iom_rstput( iter, nitrst, numriw, 'stress1_i' , stress1_i  )
+         CALL iom_rstput( iter, nitrst, numriw, 'stress2_i' , stress2_i  )
+         CALL iom_rstput( iter, nitrst, numriw, 'stress12_i', stress12_i )
+         CALL iom_rstput( iter, nitrst, numriw, 'aniso_11'  , aniso_11   )
+         CALL iom_rstput( iter, nitrst, numriw, 'aniso_12'  , aniso_12   )
+         !
+      ENDIF
+      !
+   END SUBROUTINE rhg_eap_rst
+
+
+   FUNCTION w1(pa)
+      !!-------------------------------------------------------------------
+      !! Function : w1 (see Gaussian function psi in Tsamados et al 2013)
+      !!-------------------------------------------------------------------
+      REAL(wp), INTENT(in   ) ::   pa
+      REAL(wp) ::   w1
+      !!-------------------------------------------------------------------
+   
+      w1 = -   223.87569446_wp &
+       &   +  2361.21986630_wp*pa &
+       &   - 10606.56079975_wp*pa*pa &
+       &   + 26315.50025642_wp*pa*pa*pa &
+       &   - 38948.30444297_wp*pa*pa*pa*pa &
+       &   + 34397.72407466_wp*pa*pa*pa*pa*pa &
+       &   - 16789.98003081_wp*pa*pa*pa*pa*pa*pa &
+       &   +  3495.82839237_wp*pa*pa*pa*pa*pa*pa*pa
+   
+   END FUNCTION w1
+
+
+   FUNCTION w2(pa)
+      !!-------------------------------------------------------------------
+      !! Function : w2 (see Gaussian function psi in Tsamados et al 2013)
+      !!-------------------------------------------------------------------
+      REAL(wp), INTENT(in   ) ::   pa
+      REAL(wp) ::   w2
+      !!-------------------------------------------------------------------
+   
+      w2 = -    6670.68911883_wp &
+       &   +   70222.33061536_wp*pa &
+       &   -  314871.71525448_wp*pa*pa &
+       &   +  779570.02793492_wp*pa*pa*pa &
+       &   - 1151098.82436864_wp*pa*pa*pa*pa &
+       &   + 1013896.59464498_wp*pa*pa*pa*pa*pa &
+       &   -  493379.44906738_wp*pa*pa*pa*pa*pa*pa &
+       &   +  102356.55151800_wp*pa*pa*pa*pa*pa*pa*pa
+   
+   END FUNCTION w2
+
+   FUNCTION s11kr(px,py,pz) 
+      !!-------------------------------------------------------------------
+      !! Function : s11kr
+      !!-------------------------------------------------------------------
+      REAL(wp), INTENT(in   ) ::   px,py,pz
+   
+      REAL(wp) ::   s11kr, zpih
+   
+      REAL(wp) ::   zn1t2i11, zn1t2i12, zn1t2i21, zn1t2i22
+      REAL(wp) ::   zn2t1i11, zn2t1i12, zn2t1i21, zn2t1i22
+      REAL(wp) ::   zt1t2i11, zt1t2i12, zt1t2i21, zt1t2i22
+      REAL(wp) ::   zt2t1i11, zt2t1i12, zt2t1i21, zt2t1i22
+      REAL(wp) ::   zd11, zd12, zd22
+      REAL(wp) ::   zIIn1t2, zIIn2t1, zIIt1t2
+      REAL(wp) ::   zHen1t2, zHen2t1
+      !!-------------------------------------------------------------------
+   
+      zpih = 0.5_wp*rpi
+   
+      zn1t2i11 = cos(pz+zpih-pphi) * cos(pz+pphi)
+      zn1t2i12 = cos(pz+zpih-pphi) * sin(pz+pphi)
+      zn1t2i21 = sin(pz+zpih-pphi) * cos(pz+pphi)
+      zn1t2i22 = sin(pz+zpih-pphi) * sin(pz+pphi)
+      zn2t1i11 = cos(pz-zpih+pphi) * cos(pz-pphi)
+      zn2t1i12 = cos(pz-zpih+pphi) * sin(pz-pphi)
+      zn2t1i21 = sin(pz-zpih+pphi) * cos(pz-pphi)
+      zn2t1i22 = sin(pz-zpih+pphi) * sin(pz-pphi)
+      zt1t2i11 = cos(pz-pphi) * cos(pz+pphi)
+      zt1t2i12 = cos(pz-pphi) * sin(pz+pphi)
+      zt1t2i21 = sin(pz-pphi) * cos(pz+pphi)
+      zt1t2i22 = sin(pz-pphi) * sin(pz+pphi)
+      zt2t1i11 = cos(pz+pphi) * cos(pz-pphi)
+      zt2t1i12 = cos(pz+pphi) * sin(pz-pphi)
+      zt2t1i21 = sin(pz+pphi) * cos(pz-pphi)
+      zt2t1i22 = sin(pz+pphi) * sin(pz-pphi)
+   ! In expression of tensor d, with this formulatin d(x)=-d(x+pi)
+   ! Solution, when diagonalizing always check sgn(a11-a22) if > then keep x else
+   ! x=x-pi/2
+      zd11 = cos(py)*cos(py)*(cos(px)+sin(px)*tan(py)*tan(py))
+      zd12 = cos(py)*cos(py)*tan(py)*(-cos(px)+sin(px))
+      zd22 = cos(py)*cos(py)*(sin(px)+cos(px)*tan(py)*tan(py))
+      zIIn1t2 = zn1t2i11 * zd11 + (zn1t2i12 + zn1t2i21) * zd12 + zn1t2i22 * zd22
+      zIIn2t1 = zn2t1i11 * zd11 + (zn2t1i12 + zn2t1i21) * zd12 + zn2t1i22 * zd22
+      zIIt1t2 = zt1t2i11 * zd11 + (zt1t2i12 + zt1t2i21) * zd12 + zt1t2i22 * zd22
+   
+      IF (-zIIn1t2>=rsmall) THEN
+      zHen1t2 = 1._wp
+      ELSE
+      zHen1t2 = 0._wp
+      ENDIF
+   
+      IF (-zIIn2t1>=rsmall) THEN
+      zHen2t1 = 1._wp
+      ELSE
+      zHen2t1 = 0._wp
+      ENDIF
+   
+      s11kr = (- zHen1t2 * zn1t2i11 - zHen2t1 * zn2t1i11)
+
+   END FUNCTION s11kr
+
+   FUNCTION s12kr(px,py,pz)
+      !!-------------------------------------------------------------------
+      !! Function : s12kr
+      !!-------------------------------------------------------------------
+      REAL(wp), INTENT(in   ) ::   px,py,pz
+
+      REAL(wp) ::   s12kr, zs12r0, zs21r0, zpih
+
+      REAL(wp) ::   zn1t2i11, zn1t2i12, zn1t2i21, zn1t2i22
+      REAL(wp) ::   zn2t1i11, zn2t1i12, zn2t1i21, zn2t1i22
+      REAL(wp) ::   zt1t2i11, zt1t2i12, zt1t2i21, zt1t2i22
+      REAL(wp) ::   zt2t1i11, zt2t1i12, zt2t1i21, zt2t1i22
+      REAL(wp) ::   zd11, zd12, zd22
+      REAL(wp) ::   zIIn1t2, zIIn2t1, zIIt1t2
+      REAL(wp) ::   zHen1t2, zHen2t1
+      !!-------------------------------------------------------------------
+      zpih = 0.5_wp*rpi
+   
+      zn1t2i11 = cos(pz+zpih-pphi) * cos(pz+pphi)
+      zn1t2i12 = cos(pz+zpih-pphi) * sin(pz+pphi)
+      zn1t2i21 = sin(pz+zpih-pphi) * cos(pz+pphi)
+      zn1t2i22 = sin(pz+zpih-pphi) * sin(pz+pphi)
+      zn2t1i11 = cos(pz-zpih+pphi) * cos(pz-pphi)
+      zn2t1i12 = cos(pz-zpih+pphi) * sin(pz-pphi)
+      zn2t1i21 = sin(pz-zpih+pphi) * cos(pz-pphi)
+      zn2t1i22 = sin(pz-zpih+pphi) * sin(pz-pphi)
+      zt1t2i11 = cos(pz-pphi) * cos(pz+pphi)
+      zt1t2i12 = cos(pz-pphi) * sin(pz+pphi)
+      zt1t2i21 = sin(pz-pphi) * cos(pz+pphi)
+      zt1t2i22 = sin(pz-pphi) * sin(pz+pphi)
+      zt2t1i11 = cos(pz+pphi) * cos(pz-pphi)
+      zt2t1i12 = cos(pz+pphi) * sin(pz-pphi)
+      zt2t1i21 = sin(pz+pphi) * cos(pz-pphi)
+      zt2t1i22 = sin(pz+pphi) * sin(pz-pphi)
+      zd11 = cos(py)*cos(py)*(cos(px)+sin(px)*tan(py)*tan(py))
+      zd12 = cos(py)*cos(py)*tan(py)*(-cos(px)+sin(px))
+      zd22 = cos(py)*cos(py)*(sin(px)+cos(px)*tan(py)*tan(py))
+      zIIn1t2 = zn1t2i11 * zd11 + (zn1t2i12 + zn1t2i21) * zd12 + zn1t2i22 * zd22
+      zIIn2t1 = zn2t1i11 * zd11 + (zn2t1i12 + zn2t1i21) * zd12 + zn2t1i22 * zd22
+      zIIt1t2 = zt1t2i11 * zd11 + (zt1t2i12 + zt1t2i21) * zd12 + zt1t2i22 * zd22
+   
+      IF (-zIIn1t2>=rsmall) THEN
+      zHen1t2 = 1._wp
+      ELSE
+      zHen1t2 = 0._wp
+      ENDIF
+   
+      IF (-zIIn2t1>=rsmall) THEN
+      zHen2t1 = 1._wp
+      ELSE
+      zHen2t1 = 0._wp
+      ENDIF
+   
+      zs12r0 = (- zHen1t2 * zn1t2i12 - zHen2t1 * zn2t1i12)
+      zs21r0 = (- zHen1t2 * zn1t2i21 - zHen2t1 * zn2t1i21)
+      s12kr=0.5_wp*(zs12r0+zs21r0)
+   
+   END FUNCTION s12kr
+
+   FUNCTION s22kr(px,py,pz)
+      !!-------------------------------------------------------------------
+      !! Function : s22kr
+      !!-------------------------------------------------------------------
+      REAL(wp), INTENT(in   ) ::   px,py,pz
+
+      REAL(wp) ::   s22kr, zpih
+
+      REAL(wp) ::   zn1t2i11, zn1t2i12, zn1t2i21, zn1t2i22
+      REAL(wp) ::   zn2t1i11, zn2t1i12, zn2t1i21, zn2t1i22
+      REAL(wp) ::   zt1t2i11, zt1t2i12, zt1t2i21, zt1t2i22
+      REAL(wp) ::   zt2t1i11, zt2t1i12, zt2t1i21, zt2t1i22
+      REAL(wp) ::   zd11, zd12, zd22
+      REAL(wp) ::   zIIn1t2, zIIn2t1, zIIt1t2
+      REAL(wp) ::   zHen1t2, zHen2t1
+      !!-------------------------------------------------------------------
+      zpih = 0.5_wp*rpi
+
+      zn1t2i11 = cos(pz+zpih-pphi) * cos(pz+pphi)
+      zn1t2i12 = cos(pz+zpih-pphi) * sin(pz+pphi)
+      zn1t2i21 = sin(pz+zpih-pphi) * cos(pz+pphi)
+      zn1t2i22 = sin(pz+zpih-pphi) * sin(pz+pphi)
+      zn2t1i11 = cos(pz-zpih+pphi) * cos(pz-pphi)
+      zn2t1i12 = cos(pz-zpih+pphi) * sin(pz-pphi)
+      zn2t1i21 = sin(pz-zpih+pphi) * cos(pz-pphi)
+      zn2t1i22 = sin(pz-zpih+pphi) * sin(pz-pphi)
+      zt1t2i11 = cos(pz-pphi) * cos(pz+pphi)
+      zt1t2i12 = cos(pz-pphi) * sin(pz+pphi)
+      zt1t2i21 = sin(pz-pphi) * cos(pz+pphi)
+      zt1t2i22 = sin(pz-pphi) * sin(pz+pphi)
+      zt2t1i11 = cos(pz+pphi) * cos(pz-pphi)
+      zt2t1i12 = cos(pz+pphi) * sin(pz-pphi)
+      zt2t1i21 = sin(pz+pphi) * cos(pz-pphi)
+      zt2t1i22 = sin(pz+pphi) * sin(pz-pphi)
+      zd11 = cos(py)*cos(py)*(cos(px)+sin(px)*tan(py)*tan(py))
+      zd12 = cos(py)*cos(py)*tan(py)*(-cos(px)+sin(px))
+      zd22 = cos(py)*cos(py)*(sin(px)+cos(px)*tan(py)*tan(py))
+      zIIn1t2 = zn1t2i11 * zd11 + (zn1t2i12 + zn1t2i21) * zd12 + zn1t2i22 * zd22
+      zIIn2t1 = zn2t1i11 * zd11 + (zn2t1i12 + zn2t1i21) * zd12 + zn2t1i22 * zd22
+      zIIt1t2 = zt1t2i11 * zd11 + (zt1t2i12 + zt1t2i21) * zd12 + zt1t2i22 * zd22
+
+      IF (-zIIn1t2>=rsmall) THEN
+      zHen1t2 = 1._wp
+      ELSE
+      zHen1t2 = 0._wp
+      ENDIF
+
+      IF (-zIIn2t1>=rsmall) THEN
+      zHen2t1 = 1._wp
+      ELSE
+      zHen2t1 = 0._wp
+      ENDIF
+
+      s22kr = (- zHen1t2 * zn1t2i22 - zHen2t1 * zn2t1i22)
+
+   END FUNCTION s22kr
+
+   FUNCTION s11ks(px,py,pz)
+      !!-------------------------------------------------------------------
+      !! Function : s11ks
+      !!-------------------------------------------------------------------
+      REAL(wp), INTENT(in   ) ::   px,py,pz
+
+      REAL(wp) ::   s11ks, zpih
+
+      REAL(wp) ::   zn1t2i11, zn1t2i12, zn1t2i21, zn1t2i22
+      REAL(wp) ::   zn2t1i11, zn2t1i12, zn2t1i21, zn2t1i22
+      REAL(wp) ::   zt1t2i11, zt1t2i12, zt1t2i21, zt1t2i22
+      REAL(wp) ::   zt2t1i11, zt2t1i12, zt2t1i21, zt2t1i22
+      REAL(wp) ::   zd11, zd12, zd22
+      REAL(wp) ::   zIIn1t2, zIIn2t1, zIIt1t2
+      REAL(wp) ::   zHen1t2, zHen2t1
+      !!-------------------------------------------------------------------
+      zpih = 0.5_wp*rpi
+
+      zn1t2i11 = cos(pz+zpih-pphi) * cos(pz+pphi)
+      zn1t2i12 = cos(pz+zpih-pphi) * sin(pz+pphi)
+      zn1t2i21 = sin(pz+zpih-pphi) * cos(pz+pphi)
+      zn1t2i22 = sin(pz+zpih-pphi) * sin(pz+pphi)
+      zn2t1i11 = cos(pz-zpih+pphi) * cos(pz-pphi)
+      zn2t1i12 = cos(pz-zpih+pphi) * sin(pz-pphi)
+      zn2t1i21 = sin(pz-zpih+pphi) * cos(pz-pphi)
+      zn2t1i22 = sin(pz-zpih+pphi) * sin(pz-pphi)
+      zt1t2i11 = cos(pz-pphi) * cos(pz+pphi)
+      zt1t2i12 = cos(pz-pphi) * sin(pz+pphi)
+      zt1t2i21 = sin(pz-pphi) * cos(pz+pphi)
+      zt1t2i22 = sin(pz-pphi) * sin(pz+pphi)
+      zt2t1i11 = cos(pz+pphi) * cos(pz-pphi)
+      zt2t1i12 = cos(pz+pphi) * sin(pz-pphi)
+      zt2t1i21 = sin(pz+pphi) * cos(pz-pphi)
+      zt2t1i22 = sin(pz+pphi) * sin(pz-pphi)
+      zd11 = cos(py)*cos(py)*(cos(px)+sin(px)*tan(py)*tan(py))
+      zd12 = cos(py)*cos(py)*tan(py)*(-cos(px)+sin(px))
+      zd22 = cos(py)*cos(py)*(sin(px)+cos(px)*tan(py)*tan(py))
+      zIIn1t2 = zn1t2i11 * zd11 + (zn1t2i12 + zn1t2i21) * zd12 + zn1t2i22 * zd22
+      zIIn2t1 = zn2t1i11 * zd11 + (zn2t1i12 + zn2t1i21) * zd12 + zn2t1i22 * zd22
+      zIIt1t2 = zt1t2i11 * zd11 + (zt1t2i12 + zt1t2i21) * zd12 + zt1t2i22 * zd22
+
+      IF (-zIIn1t2>=rsmall) THEN
+      zHen1t2 = 1._wp
+      ELSE
+      zHen1t2 = 0._wp
+      ENDIF
+
+      IF (-zIIn2t1>=rsmall) THEN
+      zHen2t1 = 1._wp
+      ELSE
+      zHen2t1 = 0._wp
+      ENDIF
+
+      s11ks = sign(1._wp,zIIt1t2+rsmall)*(zHen1t2 * zt1t2i11 + zHen2t1 * zt2t1i11)
+
+   END FUNCTION s11ks
+
+   FUNCTION s12ks(px,py,pz)
+      !!-------------------------------------------------------------------
+      !! Function : s12ks
+      !!-------------------------------------------------------------------
+      REAL(wp), INTENT(in   ) ::   px,py,pz
+
+      REAL(wp) ::   s12ks, zs12s0, zs21s0, zpih
+
+      REAL(wp) ::   zn1t2i11, zn1t2i12, zn1t2i21, zn1t2i22
+      REAL(wp) ::   zn2t1i11, zn2t1i12, zn2t1i21, zn2t1i22
+      REAL(wp) ::   zt1t2i11, zt1t2i12, zt1t2i21, zt1t2i22
+      REAL(wp) ::   zt2t1i11, zt2t1i12, zt2t1i21, zt2t1i22
+      REAL(wp) ::   zd11, zd12, zd22
+      REAL(wp) ::   zIIn1t2, zIIn2t1, zIIt1t2
+      REAL(wp) ::   zHen1t2, zHen2t1
+      !!-------------------------------------------------------------------
+      zpih = 0.5_wp*rpi
+
+      zn1t2i11 = cos(pz+zpih-pphi) * cos(pz+pphi)
+      zn1t2i12 = cos(pz+zpih-pphi) * sin(pz+pphi)
+      zn1t2i21 = sin(pz+zpih-pphi) * cos(pz+pphi)
+      zn1t2i22 = sin(pz+zpih-pphi) * sin(pz+pphi)
+      zn2t1i11 = cos(pz-zpih+pphi) * cos(pz-pphi)
+      zn2t1i12 = cos(pz-zpih+pphi) * sin(pz-pphi)
+      zn2t1i21 = sin(pz-zpih+pphi) * cos(pz-pphi)
+      zn2t1i22 = sin(pz-zpih+pphi) * sin(pz-pphi)
+      zt1t2i11 = cos(pz-pphi) * cos(pz+pphi)
+      zt1t2i12 = cos(pz-pphi) * sin(pz+pphi)
+      zt1t2i21 = sin(pz-pphi) * cos(pz+pphi)
+      zt1t2i22 = sin(pz-pphi) * sin(pz+pphi)
+      zt2t1i11 = cos(pz+pphi) * cos(pz-pphi)
+      zt2t1i12 = cos(pz+pphi) * sin(pz-pphi)
+      zt2t1i21 = sin(pz+pphi) * cos(pz-pphi)
+      zt2t1i22 = sin(pz+pphi) * sin(pz-pphi)
+      zd11 = cos(py)*cos(py)*(cos(px)+sin(px)*tan(py)*tan(py))
+      zd12 = cos(py)*cos(py)*tan(py)*(-cos(px)+sin(px))
+      zd22 = cos(py)*cos(py)*(sin(px)+cos(px)*tan(py)*tan(py))
+      zIIn1t2 = zn1t2i11 * zd11 + (zn1t2i12 + zn1t2i21) * zd12 + zn1t2i22 * zd22
+      zIIn2t1 = zn2t1i11 * zd11 + (zn2t1i12 + zn2t1i21) * zd12 + zn2t1i22 * zd22
+      zIIt1t2 = zt1t2i11 * zd11 + (zt1t2i12 + zt1t2i21) * zd12 + zt1t2i22 * zd22
+
+      IF (-zIIn1t2>=rsmall) THEN
+      zHen1t2 = 1._wp
+      ELSE
+      zHen1t2 = 0._wp
+      ENDIF
+
+      IF (-zIIn2t1>=rsmall) THEN
+      zHen2t1 = 1._wp
+      ELSE
+      zHen2t1 = 0._wp
+      ENDIF
+
+      zs12s0 = sign(1._wp,zIIt1t2+rsmall)*(zHen1t2 * zt1t2i12 + zHen2t1 * zt2t1i12)
+      zs21s0 = sign(1._wp,zIIt1t2+rsmall)*(zHen1t2 * zt1t2i21 + zHen2t1 * zt2t1i21)
+      s12ks=0.5_wp*(zs12s0+zs21s0)
+
+   END FUNCTION s12ks
+
+   FUNCTION s22ks(px,py,pz) 
+      !!-------------------------------------------------------------------
+      !! Function : s22ks
+      !!-------------------------------------------------------------------
+      REAL(wp), INTENT(in   ) ::   px,py,pz
+
+      REAL(wp) ::   s22ks, zpih
+
+      REAL(wp) ::   zn1t2i11, zn1t2i12, zn1t2i21, zn1t2i22
+      REAL(wp) ::   zn2t1i11, zn2t1i12, zn2t1i21, zn2t1i22
+      REAL(wp) ::   zt1t2i11, zt1t2i12, zt1t2i21, zt1t2i22
+      REAL(wp) ::   zt2t1i11, zt2t1i12, zt2t1i21, zt2t1i22
+      REAL(wp) ::   zd11, zd12, zd22
+      REAL(wp) ::   zIIn1t2, zIIn2t1, zIIt1t2
+      REAL(wp) ::   zHen1t2, zHen2t1
+      !!-------------------------------------------------------------------
+      zpih = 0.5_wp*rpi
+
+      zn1t2i11 = cos(pz+zpih-pphi) * cos(pz+pphi)
+      zn1t2i12 = cos(pz+zpih-pphi) * sin(pz+pphi)
+      zn1t2i21 = sin(pz+zpih-pphi) * cos(pz+pphi)
+      zn1t2i22 = sin(pz+zpih-pphi) * sin(pz+pphi)
+      zn2t1i11 = cos(pz-zpih+pphi) * cos(pz-pphi)
+      zn2t1i12 = cos(pz-zpih+pphi) * sin(pz-pphi)
+      zn2t1i21 = sin(pz-zpih+pphi) * cos(pz-pphi)
+      zn2t1i22 = sin(pz-zpih+pphi) * sin(pz-pphi)
+      zt1t2i11 = cos(pz-pphi) * cos(pz+pphi)
+      zt1t2i12 = cos(pz-pphi) * sin(pz+pphi)
+      zt1t2i21 = sin(pz-pphi) * cos(pz+pphi)
+      zt1t2i22 = sin(pz-pphi) * sin(pz+pphi)
+      zt2t1i11 = cos(pz+pphi) * cos(pz-pphi)
+      zt2t1i12 = cos(pz+pphi) * sin(pz-pphi)
+      zt2t1i21 = sin(pz+pphi) * cos(pz-pphi)
+      zt2t1i22 = sin(pz+pphi) * sin(pz-pphi)
+      zd11 = cos(py)*cos(py)*(cos(px)+sin(px)*tan(py)*tan(py))
+      zd12 = cos(py)*cos(py)*tan(py)*(-cos(px)+sin(px))
+      zd22 = cos(py)*cos(py)*(sin(px)+cos(px)*tan(py)*tan(py))
+      zIIn1t2 = zn1t2i11 * zd11 + (zn1t2i12 + zn1t2i21) * zd12 + zn1t2i22 * zd22
+      zIIn2t1 = zn2t1i11 * zd11 + (zn2t1i12 + zn2t1i21) * zd12 + zn2t1i22 * zd22
+      zIIt1t2 = zt1t2i11 * zd11 + (zt1t2i12 + zt1t2i21) * zd12 + zt1t2i22 * zd22
+
+      IF (-zIIn1t2>=rsmall) THEN
+      zHen1t2 = 1._wp
+      ELSE
+      zHen1t2 = 0._wp
+      ENDIF
+
+      IF (-zIIn2t1>=rsmall) THEN
+      zHen2t1 = 1._wp
+      ELSE
+      zHen2t1 = 0._wp
+      ENDIF
+
+      s22ks = sign(1._wp,zIIt1t2+rsmall)*(zHen1t2 * zt1t2i22 + zHen2t1 * zt2t1i22)
+
+   END FUNCTION s22ks
+
+#else
+   !!----------------------------------------------------------------------
+   !!   Default option         Empty module           NO SI3 sea-ice model
+   !!----------------------------------------------------------------------
+#endif
+
+   !!==============================================================================
+END MODULE icedyn_rhg_eap
Index: /NEMO/branches/2020/tickets_icb_1900/src/ICE/icedyn_rhg_evp.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/ICE/icedyn_rhg_evp.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/ICE/icedyn_rhg_evp.F90	(revision 14016)
@@ -326,11 +326,11 @@
             zvV = 0.5_wp * ( vt_i(ji,jj) * e1e2t(ji,jj) + vt_i(ji,jj+1) * e1e2t(ji,jj+1) ) * r1_e1e2v(ji,jj) * vmask(ji,jj,1)
             ! ice-bottom stress at U points
-            zvCr = zaU(ji,jj) * rn_lf_depfra * hu(ji,jj,Kmm)
+            zvCr = zaU(ji,jj) * rn_lf_depfra * hu(ji,jj,Kmm) * ( 1._wp - icb_mask(ji,jj) ) ! if grounded icebergs are read: ocean depth = 0
             ztaux_base(ji,jj) = - rn_lf_bfr * MAX( 0._wp, zvU - zvCr ) * EXP( -rn_crhg * ( 1._wp - zaU(ji,jj) ) )
             ! ice-bottom stress at V points
-            zvCr = zaV(ji,jj) * rn_lf_depfra * hv(ji,jj,Kmm)
+            zvCr = zaV(ji,jj) * rn_lf_depfra * hv(ji,jj,Kmm) * ( 1._wp - icb_mask(ji,jj) ) ! if grounded icebergs are read: ocean depth = 0
             ztauy_base(ji,jj) = - rn_lf_bfr * MAX( 0._wp, zvV - zvCr ) * EXP( -rn_crhg * ( 1._wp - zaV(ji,jj) ) )
             ! ice_bottom stress at T points
-            zvCr = at_i(ji,jj) * rn_lf_depfra * ht(ji,jj)
+            zvCr = at_i(ji,jj) * rn_lf_depfra * ht(ji,jj) * ( 1._wp - icb_mask(ji,jj) )    ! if grounded icebergs are read: ocean depth = 0
             tau_icebfr(ji,jj) = - rn_lf_bfr * MAX( 0._wp, vt_i(ji,jj) - zvCr ) * EXP( -rn_crhg * ( 1._wp - at_i(ji,jj) ) )
          END_2D
Index: /NEMO/branches/2020/tickets_icb_1900/src/ICE/icedyn_rhg_vp.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/ICE/icedyn_rhg_vp.F90	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/src/ICE/icedyn_rhg_vp.F90	(revision 14016)
@@ -0,0 +1,1689 @@
+MODULE icedyn_rhg_vp
+   !!======================================================================
+   !!                     ***  MODULE  icedyn_rhg_vp  ***
+   !!   Sea-Ice dynamics : Viscous-plastic rheology with LSR technique
+   !!======================================================================
+   !!
+   !! History :   -   !  1997-20  (J. Zhang, M. Losch) Original code, implementation into mitGCM
+   !!            4.0  !  2020-09  (M. Vancoppenolle) Adaptation to SI3
+   !!
+   !!----------------------------------------------------------------------
+#if defined key_si3
+   !!----------------------------------------------------------------------
+   !!   'key_si3'                                       SI3 sea-ice model
+   !!----------------------------------------------------------------------
+   !!   ice_dyn_rhg_vp : computes ice velocities from VP rheolog with LSR solvery
+   !!----------------------------------------------------------------------
+   USE phycst         ! Physical constants
+   USE dom_oce        ! Ocean domain
+   USE sbc_oce , ONLY : ln_ice_embd, nn_fsbc, ssh_m
+   USE sbc_ice , ONLY : utau_ice, vtau_ice, snwice_mass, snwice_mass_b
+   USE ice            ! sea-ice: ice variables
+   USE icevar         ! ice_var_sshdyn
+   USE icedyn_rdgrft  ! sea-ice: ice strength
+   USE bdy_oce , ONLY : ln_bdy 
+   USE bdyice 
+#if defined key_agrif
+   USE agrif_ice_interp
+#endif
+   !
+   USE in_out_manager ! I/O manager
+   USE iom            ! I/O manager library
+   USE lib_mpp        ! MPP library
+   USE lib_fortran    ! fortran utilities (glob_sum + no signed zero)
+   USE lbclnk         ! lateral boundary conditions (or mpp links)
+   USE prtctl         ! Print control
+
+   USE netcdf         ! NetCDF library for convergence test
+   IMPLICIT NONE
+   PRIVATE
+
+   PUBLIC   ice_dyn_rhg_vp   ! called by icedyn_rhg.F90
+
+
+   LOGICAL  ::   lp_zebra_vp =.TRUE.      ! activate zebra (solve the linear system problem every odd j-band, then one every even one)
+   REAL(wp) ::   zrelaxu_vp=0.95     ! U-relaxation factor (MV: can probably be merged with V-factor once ok)
+   REAL(wp) ::   zrelaxv_vp=0.95     ! V-relaxation factor 
+   REAL(wp) ::   zuerr_max_vp=0.80   ! maximum velocity error, above which a forcing error is considered and solver is stopped
+   REAL(wp) ::   zuerr_min_vp=1.e-04   ! minimum velocity error, beyond which convergence is assumed
+
+   !! for convergence tests
+   INTEGER ::   ncvgid        ! netcdf file id
+   INTEGER ::   nvarid_ures
+   INTEGER ::   nvarid_vres
+   INTEGER ::   nvarid_velres
+   INTEGER ::   nvarid_udif
+   INTEGER ::   nvarid_vdif
+   INTEGER ::   nvarid_veldif
+   INTEGER ::   nvarid_mke
+   INTEGER ::   nvarid_ures_xy, nvarid_vres_xy
+
+   REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   zmsk00, zmsk15
+
+   !!----------------------------------------------------------------------
+   !! NEMO/ICE 4.0 , NEMO Consortium (2018)
+   !! $Id: icedyn_rhg_vp.F90 13279 2020-07-09 10:39:43Z clem $
+   !! Software governed by the CeCILL license (see ./LICENSE)
+   !!----------------------------------------------------------------------
+   
+CONTAINS
+
+   SUBROUTINE ice_dyn_rhg_vp( kt, pshear_i, pdivu_i, pdelta_i )
+      !!-------------------------------------------------------------------
+      !!
+      !!                 ***  SUBROUTINE ice_dyn_rhg_vp  ***
+      !!                             VP-LSR-C-grid
+      !!
+      !! ** Purpose : determines sea ice drift from wind stress, ice-ocean
+      !!  stress and sea-surface slope. Internal forces assume viscous-plastic rheology (Hibler, 1979)
+      !! 
+      !! ** Method
+      !!  
+      !!  The resolution algorithm  follows from Zhang and Hibler (1997) and Losch (2010)
+      !!  with elements from Lemieux and Tremblay (2008) and Lemieux and Tremblay (2009)
+      !!  
+      !!  The components of the momentum equations are arranged following the ideas of Zhang and Hibler (1997)
+      !!
+      !!  f1(u) = g1(v)
+      !!  f2(v) = g2(v)
+      !!
+      !!  The right-hand side (RHS) is explicit
+      !!  The left-hand side (LHS) is implicit
+      !!  Coriolis is part of explicit terms, whereas ice-ocean drag is implicit
+      !!
+      !!  Two iteration levels (outer and inner loops) are used to solve the equations
+      !!
+      !!  The outer loop (OL, typically 10 iterations) is there to deal with the (strong) non-linearities in the equation
+      !!
+      !!  The inner loop (IL, typically 1500 iterations) is there to solve the linear problem with a line-successive-relaxation algorithm
+      !!
+      !!  The velocity used in the non-linear terms uses a "modified euler time step" (not sure its the correct term), 
+      !!! with uk = ( uk-1 + uk-2 ) / 2.
+      !!  
+      !!  * Spatial discretization 
+      !!
+      !!  Assumes a C-grid
+      !!
+      !!  The points in the C-grid look like this, my darling
+      !!
+      !!                              (ji,jj)
+      !!                                 |
+      !!                                 |
+      !!                      (ji-1,jj)  |  (ji,jj)
+      !!                             ---------   
+      !!                            |         |
+      !!                            | (ji,jj) |------(ji,jj)
+      !!                            |         |
+      !!                             ---------   
+      !!                     (ji-1,jj-1)     (ji,jj-1)
+      !!
+      !! ** Inputs  : - wind forcing (stress), oceanic currents
+      !!                ice total volume (vt_i) per unit area
+      !!                snow total volume (vt_s) per unit area
+      !!
+      !! ** Action  : 
+      !!             
+      !! ** Steps   :
+      !!            
+      !! ** Notes   : 
+      !!             
+      !! References : Zhang and Hibler, JGR 1997; Losch et al., OM 2010., Lemieux et al., 2008, 2009, ...  
+      !!             
+      !!             
+      !!-------------------------------------------------------------------
+      !!
+      INTEGER                 , INTENT(in   ) ::   kt                                    ! time step
+      REAL(wp), DIMENSION(:,:), INTENT(  out) ::   pshear_i  , pdivu_i   , pdelta_i      !
+      !!
+      LOGICAL ::   ll_u_iterate, ll_v_iterate   ! continue iteration or not
+      !
+      INTEGER ::   ji, ji2, jj, jj2, jn          ! dummy loop indices
+      INTEGER ::   jter, i_out, i_inn  ! 
+      INTEGER ::   ji_min, jj_min      !
+      INTEGER ::   nn_zebra_vp         ! number of zebra steps
+
+      INTEGER ::   nn_nvp              ! total number of VP iterations (n_out_vp*n_inn_vp)      
+      !
+      REAL(wp) ::   zrhoco                                              ! rho0 * rn_cio
+      REAL(wp) ::   ecc2, z1_ecc2                                       ! square of yield ellipse eccenticity
+      REAL(wp) ::   zglob_area                                          ! global ice area for diagnostics
+      REAL(wp) ::   zkt                                                 ! isotropic tensile strength for landfast ice
+      REAL(wp) ::   zm1, zm2, zm3, zmassU, zmassV                       ! ice/snow mass and volume
+      REAL(wp) ::   zdeltat, zds2, zdt, zdt2, zdiv, zdiv2               ! temporary scalars
+      REAL(wp) ::   zp_deltastar_f                                      ! 
+      REAL(wp) ::   zu_cV, zv_cU                                        ! 
+      REAL(wp) ::   zfac, zfac1, zfac2, zfac3
+      REAL(wp) ::   zt12U, zt11U, zt22U, zt21U, zt122U, zt121U
+      REAL(wp) ::   zt12V, zt11V, zt22V, zt21V, zt122V, zt121V
+      REAL(wp) ::   zAA3, zw, ztau, zuerr_max, zverr_max
+      !
+      REAL(wp), DIMENSION(jpi,jpj) ::   zfmask                          ! mask at F points for the ice
+      REAL(wp), DIMENSION(jpi,jpj) ::   za_iU  , za_iV                      ! ice fraction on U/V points
+      REAL(wp), DIMENSION(jpi,jpj) ::   zmU_t, zmV_t                    ! Acceleration term contribution to RHS
+      REAL(wp), DIMENSION(jpi,jpj) ::   zmassU_t, zmassV_t              ! Mass per unit area divided by time step
+      !
+      REAL(wp), DIMENSION(jpi,jpj) ::   zdeltastar_t                    ! Delta* at T-points
+      REAL(wp), DIMENSION(jpi,jpj) ::   zten_i                          ! Tension
+      REAL(wp), DIMENSION(jpi,jpj) ::   zp_deltastar_t                  ! P/delta* at T points
+      REAL(wp), DIMENSION(jpi,jpj) ::   zzt, zet                        ! Viscosity pre-factors at T points
+      REAL(wp), DIMENSION(jpi,jpj) ::   zef                             ! Viscosity pre-factor at F point
+      !
+      REAL(wp), DIMENSION(jpi,jpj) ::   zmt                             ! Mass per unit area at t-point
+      REAL(wp), DIMENSION(jpi,jpj) ::   zmf                             ! Coriolis factor (m*f) at t-point 
+      REAL(wp), DIMENSION(jpi,jpj) ::   v_oceU, u_oceV, v_iceU, u_iceV  ! ocean/ice u/v component on V/U points
+      REAL(wp), DIMENSION(jpi,jpj) ::   zu_c, zv_c                      ! "current" ice velocity (m/s), average of previous two OL iterates
+      REAL(wp), DIMENSION(jpi,jpj) ::   zu_b, zv_b                      ! velocity at previous sub-iterate
+      REAL(wp), DIMENSION(jpi,jpj) ::   zuerr, zverr                    ! absolute U/Vvelocity difference between current and previous sub-iterates
+      !
+      REAL(wp), DIMENSION(jpi,jpj) ::   zds                             ! shear
+      REAL(wp), DIMENSION(jpi,jpj) ::   zsshdyn                         ! array used for the calculation of ice surface slope:
+      !                                                                 !    ocean surface (ssh_m) if ice is not embedded
+      !                                                                 !    ice bottom surface if ice is embedded   
+      REAL(wp), DIMENSION(jpi,jpj) ::   zCwU, zCwV                      ! ice-ocean drag pre-factor (rho*c*module(u))
+      REAL(wp), DIMENSION(jpi,jpj) ::   zspgU, zspgV                    ! surface pressure gradient at U/V points
+      REAL(wp), DIMENSION(jpi,jpj) ::   zCorU, zCorV                    ! Coriolis stress array
+      REAL(wp), DIMENSION(jpi,jpj) ::   ztaux_ai, ztauy_ai              ! ice-atm. stress at U-V points
+      REAL(wp), DIMENSION(jpi,jpj) ::   ztaux_oi_rhsu, ztauy_oi_rhsv    ! ice-ocean stress RHS contribution at U-V points
+      REAL(wp), DIMENSION(jpi,jpj) ::   zs1_rhsu, zs2_rhsu, zs12_rhsu   ! internal stress contributions to RHSU
+      REAL(wp), DIMENSION(jpi,jpj) ::   zs1_rhsv, zs2_rhsv, zs12_rhsv   ! internal stress contributions to RHSV
+      REAL(wp), DIMENSION(jpi,jpj) ::   zf_rhsu, zf_rhsv                ! U- and V- components of internal force RHS contributions
+      REAL(wp), DIMENSION(jpi,jpj) ::   zrhsu, zrhsv                    ! U and V RHS 
+      REAL(wp), DIMENSION(jpi,jpj) ::   zAU, zBU, zCU, zDU, zEU         ! Linear system coefficients, U equation
+      REAL(wp), DIMENSION(jpi,jpj) ::   zAV, zBV, zCV, zDV, zEV         ! Linear system coefficients, V equation
+      REAL(wp), DIMENSION(jpi,jpj) ::   zFU, zFU_prime, zBU_prime       ! Rearranged linear system coefficients, U equation
+      REAL(wp), DIMENSION(jpi,jpj) ::   zFV, zFV_prime, zBV_prime       ! Rearranged linear system coefficients, V equation
+      REAL(wp), DIMENSION(jpi,jpj) ::   zCU_prime, zCV_prime            ! Rearranged linear system coefficients, V equation
+!!!      REAL(wp), DIMENSION(jpi,jpj) ::   ztaux_bi, ztauy_bi              ! ice-OceanBottom stress at U-V points (landfast)
+!!!      REAL(wp), DIMENSION(jpi,jpj) ::   ztaux_base, ztauy_base          ! ice-bottom stress at U-V points (landfast)
+     !
+      REAL(wp), DIMENSION(jpi,jpj) ::   zmsk01x, zmsk01y                ! mask for lots of ice (1), little ice (0)
+      REAL(wp), DIMENSION(jpi,jpj) ::   zmsk00x, zmsk00y                ! mask for ice presence (1), no ice (0)
+      !
+      REAL(wp), PARAMETER          ::   zepsi  = 1.0e-20_wp             ! tolerance parameter
+      REAL(wp), PARAMETER          ::   zmmin  = 1._wp                  ! ice mass (kg/m2)  below which ice velocity becomes very small
+      REAL(wp), PARAMETER          ::   zamin  = 0.001_wp               ! ice concentration below which ice velocity becomes very small
+      !! --- diags
+      REAL(wp) ::   zsig1, zsig2, zsig12, zdelta, z1_strength, zfac_x, zfac_y
+      REAL(wp), DIMENSION(jpi,jpj) ::   zs1, zs2, zs12, zs12f           ! stress tensor components
+      REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   zsig_I, zsig_II, zsig1_p, zsig2_p
+      REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   ztaux_oi, ztauy_oi
+      REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   zdiag_xmtrp_ice, zdiag_ymtrp_ice ! X/Y-component of ice mass transport (kg/s, SIMIP)
+      REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   zdiag_xmtrp_snw, zdiag_ymtrp_snw ! X/Y-component of snow mass transport (kg/s, SIMIP)
+      REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   zdiag_xatrp, zdiag_yatrp         ! X/Y-component of area transport (m2/s, SIMIP)
+
+
+      CALL ctl_stop( 'STOP', 'icedyn_rhg_vp: stop because vp rheology is an ongoing work and should not be used' )
+      
+      !!----------------------------------------------------------------------------------------------------------------------
+      ! DEBUG put all forcing terms to zero
+         ! air-ice drag
+         utau_ice(:,:) = 0._wp
+         vtau_ice(:,:) = 0._wp
+         ! coriolis
+         ff_t(:,:) = 0._wp
+         ! ice-ocean drag
+         rn_cio = 0._wp
+         ! ssh 
+         ! done line 330 !!! dont forget to act there
+      ! END DEBUG
+
+      IF( kt == nit000 .AND. lwp )   WRITE(numout,*) '-- ice_dyn_rhg_vp: VP sea-ice rheology (LSR solver)'
+      IF( lwp )   WRITE(numout,*) '-- ice_dyn_rhg_vp: VP sea-ice rheology (LSR solver)'
+            
+      !------------------------------------------------------------------------------!
+      !
+      ! --- Initialization
+      !
+      !------------------------------------------------------------------------------!
+      
+      ! for diagnostics and convergence tests
+      ALLOCATE( zmsk00(jpi,jpj), zmsk15(jpi,jpj) )
+      DO jj = 1, jpj
+         DO ji = 1, jpi
+            zmsk00(ji,jj) = MAX( 0._wp , SIGN( 1._wp , at_i(ji,jj) - epsi06  ) ) ! 1 if ice    , 0 if no ice
+            zmsk15(ji,jj) = MAX( 0._wp , SIGN( 1._wp , at_i(ji,jj) - 0.15_wp ) ) ! 1 if 15% ice, 0 if less
+         END DO
+      END DO
+      
+      IF ( lp_zebra_vp ) THEN; nn_zebra_vp = 2
+                         ELSE; nn_zebra_vp = 1; ENDIF 
+
+      nn_nvp = nn_vp_nout * nn_vp_ninn ! maximum number of iterations
+
+      IF( lwp )   WRITE(numout,*) ' lp_zebra_vp : ', lp_zebra_vp
+      IF( lwp )   WRITE(numout,*) ' nn_zebra_vp : ', nn_zebra_vp
+      IF( lwp )   WRITE(numout,*) ' nn_nvp      : ', nn_nvp
+      
+      zrhoco = rho0 * rn_cio 
+
+      ! ecc2: square of yield ellipse eccentricity
+      ecc2    = rn_ecc * rn_ecc
+      z1_ecc2 = 1._wp / ecc2
+               
+      ! Initialise convergence checks
+      IF( nn_rhg_chkcvg /= 0 ) THEN
+      
+         ! ice area for global mean kinetic energy
+         zglob_area = glob_sum( 'ice_rhg_vp', at_i(:,:) * e1e2t(:,:) ) ! global ice area (km2)
+         
+      ENDIF
+
+      ! Landfast param from Lemieux(2016): add isotropic tensile strength (following Konig Beatty and Holland, 2010)
+      ! MV: Not working yet...
+      IF( ln_landfast_L16 ) THEN   ;   zkt = rn_lf_tensile
+      ELSE                         ;   zkt = 0._wp
+      ENDIF
+
+      zs1_rhsu(:,:) = 0._wp; zs2_rhsu(:,:) = 0._wp; zs1_rhsv(:,:) = 0._wp; zs2_rhsv(:,:) = 0._wp
+      zAU(:,:) = 0._wp; zBU(:,:) = 0._wp; zCU(:,:) = 0._wp; zDU(:,:) = 0._wp; zEU(:,:) = 0._wp;
+      zAV(:,:) = 0._wp; zBV(:,:) = 0._wp; zCV(:,:) = 0._wp; zDV(:,:) = 0._wp; zEV(:,:) = 0._wp;
+      zrhsu(:,:) = 0._wp; zrhsv(:,:) = 0._wp
+      zf_rhsu(:,:) = 0._wp; zf_rhsv(:,:) = 0._wp
+
+      !------------------------------------------------------------------------------!
+      !
+      ! --- Time-independent quantities
+      !
+      !------------------------------------------------------------------------------!
+      
+      CALL ice_strength ! strength at T points
+      
+      !------------------------------
+      ! -- F-mask       (code from EVP)
+      !------------------------------
+      ! MartinV: 
+      ! In EVP routine, zfmask is applied on shear at F-points, in order to enforce the lateral boundary condition (no-slip, ..., free-slip)
+      ! I am not sure the same recipe applies here
+      
+      ! - ocean/land mask
+      DO jj = 1, jpj - 1
+         DO ji = 1, jpi - 1
+            zfmask(ji,jj) = tmask(ji,jj,1) * tmask(ji+1,jj,1) * tmask(ji,jj+1,1) * tmask(ji+1,jj+1,1)
+         END DO
+      END DO
+
+      ! Lateral boundary conditions on velocity (modify zfmask)
+      ! Can be computed once for all, at first time step, for all rheologies
+      DO jj = 2, jpj - 1
+         DO ji = 2, jpi - 1   ! vector opt.
+            IF( zfmask(ji,jj) == 0._wp ) THEN
+               zfmask(ji,jj) = rn_ishlat * MIN( 1._wp , MAX( umask(ji,jj,1), umask(ji,jj+1,1), &
+                  &                                          vmask(ji,jj,1), vmask(ji+1,jj,1) ) )
+            ENDIF
+         END DO
+      END DO
+      DO jj = 2, jpj - 1
+         IF( zfmask(1,jj) == 0._wp ) THEN
+            zfmask(1  ,jj) = rn_ishlat * MIN( 1._wp , MAX( vmask(2,jj,1), umask(1,jj+1,1), umask(1,jj,1) ) )
+         ENDIF
+         IF( zfmask(jpi,jj) == 0._wp ) THEN
+            zfmask(jpi,jj) = rn_ishlat * MIN( 1._wp , MAX( umask(jpi,jj+1,1), vmask(jpi - 1,jj,1), umask(jpi,jj-1,1) ) )
+        ENDIF
+      END DO
+      DO ji = 2, jpi - 1
+         IF( zfmask(ji,1) == 0._wp ) THEN
+            zfmask(ji, 1 ) = rn_ishlat * MIN( 1._wp , MAX( vmask(ji+1,1,1), umask(ji,2,1), vmask(ji,1,1) ) )
+         ENDIF
+         IF( zfmask(ji,jpj) == 0._wp ) THEN
+            zfmask(ji,jpj) = rn_ishlat * MIN( 1._wp , MAX( vmask(ji+1,jpj,1), vmask(ji-1,jpj,1), umask(ji,jpj - 1,1) ) )
+         ENDIF
+      END DO
+      
+      CALL lbc_lnk( 'icedyn_rhg_vp', zfmask, 'F', 1._wp )
+      
+      !----------------------------------------------------------------------------------------------------------
+      ! -- Time-independent pre-factors for acceleration, ocean drag, coriolis, atmospheric drag, surface tilt
+      !----------------------------------------------------------------------------------------------------------
+      ! Compute all terms & factors independent of velocities, or only depending on velocities at previous time step      
+      
+      ! sea surface height
+      !    embedded sea ice: compute representative ice top surface
+      !    non-embedded sea ice: use ocean surface for slope calculation
+      zsshdyn(:,:) = ice_var_sshdyn( ssh_m, snwice_mass, snwice_mass_b)
+      zsshdyn(:,:) = 0._wp ! DEBUG CAREFUL !!!
+
+      zmt(:,:) = rhos * vt_s(:,:) + rhoi * vt_i(:,:)       ! Snow and ice mass at T-point
+      zmf(:,:) = zmt(:,:) * ff_t(:,:)                      ! Coriolis factor at T points (m*f)
+      
+      DO jj = 2, jpj - 1
+         DO ji = 2, jpi - 1
+
+            ! Ice fraction at U-V points
+            za_iU(ji,jj)    = 0.5_wp * ( at_i(ji,jj) * e1e2t(ji,jj) + at_i(ji+1,jj) * e1e2t(ji+1,jj) ) * r1_e1e2u(ji,jj) * umask(ji,jj,1)
+            za_iV(ji,jj)    = 0.5_wp * ( at_i(ji,jj) * e1e2t(ji,jj) + at_i(ji,jj+1) * e1e2t(ji,jj+1) ) * r1_e1e2v(ji,jj) * vmask(ji,jj,1)
+
+            ! Snow and ice mass at U-V points
+            zm1             = zmt(ji,jj)
+            zm2             = zmt(ji+1,jj)
+            zm3             = zmt(ji,jj+1)
+            
+            zmassU          = 0.5_wp * ( zm1 * e1e2t(ji,jj) + zm2 * e1e2t(ji+1,jj) ) * r1_e1e2u(ji,jj) * umask(ji,jj,1)
+            zmassV          = 0.5_wp * ( zm1 * e1e2t(ji,jj) + zm3 * e1e2t(ji,jj+1) ) * r1_e1e2v(ji,jj) * vmask(ji,jj,1)
+                          
+            ! Mass per unit area divided by time step
+            zmassU_t(ji,jj) = zmassU * r1_Dt_ice
+            zmassV_t(ji,jj) = zmassV * r1_Dt_ice
+
+            ! Acceleration term contribution to RHS (depends on velocity at previous time step)            
+            zmU_t(ji,jj)    = zmassU_t(ji,jj) * u_ice(ji,jj)
+            zmV_t(ji,jj)    = zmassV_t(ji,jj) * v_ice(ji,jj)
+            
+            ! Ocean currents at U-V points
+            v_oceU(ji,jj)   = 0.25_wp * ( v_oce(ji,jj) + v_oce(ji,jj-1) + v_oce(ji+1,jj) + v_oce(ji+1,jj-1) ) * umask(ji,jj,1)
+            u_oceV(ji,jj)   = 0.25_wp * ( u_oce(ji,jj) + u_oce(ji-1,jj) + u_oce(ji,jj+1) + u_oce(ji-1,jj+1) ) * vmask(ji,jj,1)
+            
+            ! Wind stress
+            ztaux_ai(ji,jj) = za_iU(ji,jj) * utau_ice(ji,jj)
+            ztauy_ai(ji,jj) = za_iV(ji,jj) * vtau_ice(ji,jj)
+
+            ! Force due to sea surface tilt(- m*g*GRAD(ssh))
+            zspgU(ji,jj)    = - zmassU * grav * ( zsshdyn(ji+1,jj) - zsshdyn(ji,jj) ) * r1_e1u(ji,jj)
+            zspgV(ji,jj)    = - zmassV * grav * ( zsshdyn(ji,jj+1) - zsshdyn(ji,jj) ) * r1_e2v(ji,jj)
+
+            ! Mask for ice presence (1) or absence (0)
+            zmsk00x(ji,jj)  = 1._wp - MAX( 0._wp, SIGN( 1._wp, -zmassU ) )  ! 0 if no ice
+            zmsk00y(ji,jj)  = 1._wp - MAX( 0._wp, SIGN( 1._wp, -zmassV ) )  ! 0 if no ice
+
+            ! Mask for lots of ice (1) or little ice (0)
+            IF ( zmassU <= zmmin .AND. za_iU(ji,jj) <= zamin ) THEN   ;   zmsk01x(ji,jj) = 0._wp
+            ELSE                                                      ;   zmsk01x(ji,jj) = 1._wp   ;   ENDIF
+            IF ( zmassV <= zmmin .AND. za_iV(ji,jj) <= zamin ) THEN   ;   zmsk01y(ji,jj) = 0._wp
+            ELSE                                                      ;   zmsk01y(ji,jj) = 1._wp   ;   ENDIF              
+
+! MV TEST DEBUG
+            IF ( ( zmt(ji,jj)   <= zmmin .OR. zmt(ji+1,jj)  <= zmmin )     .AND.  &
+               & ( at_i(ji,jj)  <= zamin .OR. at_i(ji+1,jj) <= zamin ) )    THEN   ;   zmsk01x(ji,jj) = 0._wp
+            ELSE                                                                   ;   zmsk01x(ji,jj) = 1._wp   ;   ENDIF
+
+            IF ( ( zmt(ji,jj)   <= zmmin .OR. zmt(ji,jj+1)  <= zmmin )     .AND.  &
+               & ( at_i(ji,jj)  <= zamin .OR. at_i(ji,jj+1) <= zamin ) )    THEN   ;   zmsk01y(ji,jj) = 0._wp
+            ELSE                                                                   ;   zmsk01y(ji,jj) = 1._wp   ;   ENDIF              
+! END MV TEST DEBUG
+
+         END DO
+      END DO   
+
+      CALL iom_put( 'zmsk00x'    , zmsk00x  )   ! MV DEBUG
+      CALL iom_put( 'zmsk00y'    , zmsk00y  )   ! MV DEBUG
+      CALL iom_put( 'zmsk01x'    , zmsk01x  )   ! MV DEBUG
+      CALL iom_put( 'zmsk01y'    , zmsk01y  )   ! MV DEBUG
+      CALL iom_put( 'ztaux_ai'   , ztaux_ai )   ! MV DEBUG
+      CALL iom_put( 'ztauy_ai'   , ztauy_ai )   ! MV DEBUG
+      CALL iom_put( 'zspgU'      , zspgU    )   ! MV DEBUG
+      CALL iom_put( 'zspgV'      , zspgV    )   ! MV DEBUG
+            
+      !------------------------------------------------------------------------------!
+      !
+      ! --- Start outer loop
+      !
+      !------------------------------------------------------------------------------!
+      
+      zu_c(:,:) = u_ice(:,:)
+      zv_c(:,:) = v_ice(:,:)
+      
+      jter = 0
+
+      DO i_out = 1, nn_vp_nout
+
+         IF( lwp )   WRITE(numout,*) ' outer loop  i_out : ', i_out
+               
+         ! Velocities used in the non linear terms are the average of the past two iterates
+         ! u_it = 0.5 * ( u_{it-1} + u_{it-2})
+         ! Also used in Hibler and Ackley (1983); Zhang and Hibler (1997); Lemieux and Tremblay (2009)
+         zu_c(:,:) = 0.5_wp * ( u_ice(:,:) + zu_c(:,:) )
+         zv_c(:,:) = 0.5_wp * ( v_ice(:,:) + zv_c(:,:) )
+         
+         !------------------------------------------------------------------------------!
+         !
+         ! --- Right-hand side (RHS) of the linear problem
+         !
+         !------------------------------------------------------------------------------!
+         ! In the outer loop, one needs to update all RHS terms
+         ! with explicit velocity dependencies (viscosities, coriolis, ocean stress)
+         ! as a function of uc
+         !
+      
+         !------------------------------------------
+         ! -- Strain rates, viscosities and P/Delta
+         !------------------------------------------
+
+         ! --- divergence, tension & shear (Appendix B of Hunke & Dukowicz, 2002) --- !
+         DO jj = 1, jpj - 1         ! loops start at 1 since there is no boundary condition (lbc_lnk) at i=1 and j=1 for F points
+            DO ji = 1, jpi - 1
+
+               ! shear at F points
+               zds(ji,jj) = ( ( zu_c(ji,jj+1) * r1_e1u(ji,jj+1) - zu_c(ji,jj) * r1_e1u(ji,jj) ) * e1f(ji,jj) * e1f(ji,jj)   &
+                  &         + ( zv_c(ji+1,jj) * r1_e2v(ji+1,jj) - zv_c(ji,jj) * r1_e2v(ji,jj) ) * e2f(ji,jj) * e2f(ji,jj)   &
+                  &         ) * r1_e1e2f(ji,jj) * zfmask(ji,jj)
+
+            END DO
+         END DO
+
+         CALL lbc_lnk( 'icedyn_rhg_vp', zds, 'F', 1. ) ! MV TEST could be un-necessary according to Gurvan
+         CALL iom_put( 'zds'        , zds      )   ! MV DEBUG
+
+         IF( lwp )   WRITE(numout,*) ' outer loop  1a i_out : ', i_out
+
+         !DO jj = 2, jpj - 1    ! loop to jpi,jpj to avoid making a communication for zs1,zs2,zs12
+         !   DO ji = 2, jpi - 1 ! 
+
+! MV DEBUG
+         DO jj = 2, jpj        ! loop to jpi,jpj to avoid making a communication for zs1,zs2,zs12
+            DO ji = 2, jpi     ! 
+! END MV DEBUG
+
+               ! shear**2 at T points (doc eq. A16)
+               zds2 = ( zds(ji,jj  ) * zds(ji,jj  ) * e1e2f(ji,jj  ) + zds(ji-1,jj  ) * zds(ji-1,jj  ) * e1e2f(ji-1,jj  )  &
+                  &   + zds(ji,jj-1) * zds(ji,jj-1) * e1e2f(ji,jj-1) + zds(ji-1,jj-1) * zds(ji-1,jj-1) * e1e2f(ji-1,jj-1)  &
+                  &   ) * 0.25_wp * r1_e1e2t(ji,jj)
+              
+               ! divergence at T points
+               zdiv  = ( e2u(ji,jj) * zu_c(ji,jj) - e2u(ji-1,jj) * zu_c(ji-1,jj)   &
+                  &    + e1v(ji,jj) * zv_c(ji,jj) - e1v(ji,jj-1) * zv_c(ji,jj-1)   &
+                  &    ) * r1_e1e2t(ji,jj)
+               zdiv2 = zdiv * zdiv
+               
+               ! tension at T points
+               zdt  = ( ( zu_c(ji,jj) * r1_e2u(ji,jj) - zu_c(ji-1,jj) * r1_e2u(ji-1,jj) ) * e2t(ji,jj) * e2t(ji,jj)   &
+                  &   - ( zv_c(ji,jj) * r1_e1v(ji,jj) - zv_c(ji,jj-1) * r1_e1v(ji,jj-1) ) * e1t(ji,jj) * e1t(ji,jj)   &
+                  &   ) * r1_e1e2t(ji,jj)
+               zdt2 = zdt * zdt
+               
+               ! delta at T points
+               zdeltat = SQRT( zdiv2 + ( zdt2 + zds2 ) * z1_ecc2 )  
+               
+               ! delta* at T points (following Lemieux and Dupont, GMD 2020)
+               zdeltastar_t(ji,jj) = zdeltat + rn_creepl
+              
+               ! P/delta at T-points
+               zp_deltastar_t(ji,jj) = strength(ji,jj) / zdeltastar_t(ji,jj)
+               
+               ! Temporary zzt and zet factors at T-points
+               zzt(ji,jj)     = zp_deltastar_t(ji,jj) * r1_e1e2t(ji,jj)
+               zet(ji,jj)     = zzt(ji,jj)     * z1_ecc2 
+                          
+            END DO
+         END DO
+         
+         CALL lbc_lnk_multi( 'icedyn_rhg_vp', zp_deltastar_t , 'T', 1. , zzt , 'T', 1., zet, 'T', 1. )
+
+         CALL iom_put( 'zzt'        , zzt      )   ! MV DEBUG
+         CALL iom_put( 'zet'        , zet      )   ! MV DEBUG
+         CALL iom_put( 'zp_deltastar_t', zp_deltastar_t ) ! MV DEBUG
+
+         IF( lwp )   WRITE(numout,*) ' outer loop  1b i_out : ', i_out
+
+         DO jj = 1, jpj - 1
+            DO ji = 1, jpi - 1
+               
+               ! P/delta* at F points
+               zp_deltastar_f = 0.25_wp * ( zp_deltastar_t(ji,jj) + zp_deltastar_t(ji+1,jj) + zp_deltastar_t(ji,jj+1) + zp_deltastar_t(ji+1,jj+1) )
+               
+               ! Temporary zef factor at F-point
+               zef(ji,jj)      = zp_deltastar_f * r1_e1e2f(ji,jj) * z1_ecc2 * zfmask(ji,jj)
+
+            END DO
+         END DO
+         
+         CALL lbc_lnk( 'icedyn_rhg_vp', zef, 'F', 1. )
+         CALL iom_put( 'zef'           , zef            ) ! MV DEBUG
+         IF( lwp )   WRITE(numout,*) ' outer loop  1c i_out : ', i_out
+
+         !---------------------------------------------------
+         ! -- Ocean-ice drag and Coriolis RHS contributions
+         !---------------------------------------------------
+
+         DO jj = 2, jpj - 1
+             DO ji = 2, jpi - 1
+                
+                !--- ice u-velocity @V points, v-velocity @U points (for non-linear drag computation)
+                zu_cV            = 0.25_wp * ( zu_c(ji,jj) + zu_c(ji-1,jj) + zu_c(ji,jj+1) + zu_c(ji-1,jj+1) ) * vmask(ji,jj,1)
+                zv_cU            = 0.25_wp * ( zv_c(ji,jj) + zv_c(ji,jj-1) + zv_c(ji+1,jj) + zv_c(ji+1,jj-1) ) * umask(ji,jj,1)
+                
+                !--- non-linear drag coefficients (need to be updated at each outer loop, see Lemieux and Tremblay JGR09, p.3, beginning of Section 3)
+                zCwU(ji,jj)          = za_iU(ji,jj) * zrhoco * SQRT( ( zu_c (ji,jj) - u_oce (ji,jj) ) * ( zu_c (ji,jj) - u_oce (ji,jj) )  &
+                  &                                                + ( zv_cU - v_oceU(ji,jj) ) * ( zv_cU - v_oceU(ji,jj) ) )
+                zCwV(ji,jj)          = za_iV(ji,jj) * zrhoco * SQRT( ( zv_c (ji,jj) - v_oce (ji,jj) ) * ( zv_c (ji,jj) - v_oce (ji,jj) )  &
+                  &                                                + ( zu_cV - u_oceV(ji,jj) ) * ( zu_cV - u_oceV(ji,jj) ) )
+                 
+                !--- Ocean-ice drag contributions to RHS 
+                ztaux_oi_rhsu(ji,jj) = zCwU(ji,jj) * u_oce(ji,jj)
+                ztauy_oi_rhsv(ji,jj) = zCwV(ji,jj) * v_oce(ji,jj)
+                
+                ! --- U-component of Coriolis Force (energy conserving formulation)
+                ! Note Lemieux et al 2008 recommend to do that implicitly, but I don't really see how this could be done
+                zCorU(ji,jj)         =   0.25_wp * r1_e1u(ji,jj) *  &
+                           &             ( zmf(ji  ,jj) * ( e1v(ji  ,jj) * zv_c(ji  ,jj) + e1v(ji  ,jj-1) * zv_c(ji  ,jj-1) )  &
+                           &             + zmf(ji+1,jj) * ( e1v(ji+1,jj) * zv_c(ji+1,jj) + e1v(ji+1,jj-1) * zv_c(ji+1,jj-1) ) )
+                           
+                ! --- V-component of Coriolis Force (energy conserving formulation)
+                zCorV(ji,jj)         = - 0.25_wp * r1_e2v(ji,jj) *  &
+                           &             ( zmf(ji,jj  ) * ( e2u(ji,jj  ) * zu_c(ji,jj  ) + e2u(ji-1,jj  ) * zu_c(ji-1,jj  ) )  &
+                           &             + zmf(ji,jj+1) * ( e2u(ji,jj+1) * zu_c(ji,jj+1) + e2u(ji-1,jj+1) * zu_c(ji-1,jj+1) ) )
+         
+             END DO
+         END DO
+
+         IF( lwp )   WRITE(numout,*) ' outer loop  1d i_out : ', i_out
+         
+         CALL lbc_lnk_multi( 'icedyn_rhg_vp', zCwU ,  'U', -1., zCwV, 'V', -1. )
+         CALL lbc_lnk_multi( 'icedyn_rhg_vp', zCorU,  'U', -1., zCorV, 'V', -1. )
+
+         CALL iom_put( 'zCwU'          , zCwU           ) ! MV DEBUG
+         CALL iom_put( 'zCwV'          , zCwV           ) ! MV DEBUG
+         CALL iom_put( 'zCorU'         , zCorU          ) ! MV DEBUG
+         CALL iom_put( 'zCorV'         , zCorV          ) ! MV DEBUG
+
+         IF( lwp )   WRITE(numout,*) ' outer loop  1f i_out : ', i_out
+         
+         ! a priori, Coriolis and drag terms only affect diagonal or independent term of the linear system, 
+         ! so there is no need for lbclnk on drag and coriolis
+
+         !-------------------------------------
+         ! -- Internal stress RHS contribution
+         !-------------------------------------
+
+         ! --- Stress contributions at T-points         
+         DO jj = 2, jpj    ! loop to jpi,jpj to avoid making a communication for zs1,zs2,zs12
+            DO ji = 2, jpi ! 
+            
+               ! sig1 contribution to RHS of U-equation at T-points
+               zs1_rhsu(ji,jj) =   zzt(ji,jj) * ( e1v(ji,jj)    * zv_c(ji,jj) - e1v(ji,jj-1)    * zv_c(ji,jj-1) - 1.0_wp )
+                                            
+               ! sig2 contribution to RHS of U-equation at T-points            
+               zs2_rhsu(ji,jj) = - zet(ji,jj) * ( r1_e1v(ji,jj) * zv_c(ji,jj) - r1_e1v(ji,jj-1) * zv_c(ji,jj-1) ) * e1t(ji,jj) * e1t(ji,jj) 
+
+               ! sig1 contribution to RHS of V-equation at T-points
+               zs1_rhsv(ji,jj) =   zzt(ji,jj) * ( e2u(ji,jj)    * zu_c(ji,jj) - e2u(ji-1,jj)    * zu_c(ji-1,jj) - 1.0_wp )
+
+               ! sig2 contribution to RHS of V-equation  at T-points
+               zs2_rhsv(ji,jj) =   zet(ji,jj) * ( r1_e2u(ji,jj) * zu_c(ji,jj) - r1_e2u(ji-1,jj) * zu_c(ji-1,jj) ) * e2t(ji,jj) * e2t(ji,jj)
+
+            END DO
+         END DO
+
+         CALL iom_put( 'zs1_rhsu'      , zs1_rhsu       ) ! MV DEBUG
+         CALL iom_put( 'zs2_rhsu'      , zs2_rhsu       ) ! MV DEBUG
+         CALL iom_put( 'zs1_rhsv'      , zs1_rhsv       ) ! MV DEBUG
+         CALL iom_put( 'zs2_rhsv'      , zs2_rhsv       ) ! MV DEBUG
+         
+         ! a priori, no lbclnk, because rhsu is only used in the inner domain
+         
+         ! --- Stress contributions at f-points         
+         ! MV NOTE: I applied zfmask on zds, by mimetism on EVP, but without deep understanding of what I was doing
+         ! My guess is that this is the way to enforce boundary conditions on strain rate tensor
+
+         IF( lwp )   WRITE(numout,*) ' outer loop 2 i_out : ', i_out
+
+         DO jj = 1, jpj - 1
+            DO ji = 1, jpi - 1
+               
+               ! sig12 contribution to RHS of U equation at F-points 
+               zs12_rhsu(ji,jj) = - zef(ji,jj)  * ( r1_e2v(ji+1,jj) * zv_c(ji+1,jj) - r1_e2v(ji,jj) * zv_c(ji,jj) ) * e2f(ji,jj) * e2f(ji,jj) * zfmask(ji,jj)
+               
+               ! sig12 contribution to RHS of V equation at F-points
+               zs12_rhsv(ji,jj) =   zef(ji,jj)  * ( r1_e1u(ji,jj+1) * zu_c(ji,jj+1) - r1_e1u(ji,jj) * zu_c(ji,jj) ) * e1f(ji,jj) * e1f(ji,jj) * zfmask(ji,jj)
+
+            END DO
+         END DO
+
+         CALL lbc_lnk( 'icedyn_rhg_vp', zs12_rhsu, 'F', 1. )
+         CALL lbc_lnk( 'icedyn_rhg_vp', zs12_rhsv, 'F', 1. )
+
+         CALL iom_put( 'zs12_rhsu'     , zs12_rhsu      ) ! MV DEBUG
+         CALL iom_put( 'zs12_rhsv'     , zs12_rhsv      ) ! MV DEBUG
+
+         ! a priori, no lbclnk, because rhsu are only used in the inner domain
+
+         ! --- Internal force contributions to RHS, taken as divergence of stresses (Appendix C of Hunke and Dukowicz, 2002)
+         ! OPT: merge with next loop and use intermediate scalars for zf_rhsu
+         
+         DO jj = 2, jpj - 1
+            DO ji = 2, jpi - 1               
+               ! --- U component of internal force contribution to RHS at U points
+               zf_rhsu(ji,jj) = 0.5_wp * r1_e1e2u(ji,jj) * & 
+                              (    e2u(ji,jj)    * ( zs1_rhsu(ji+1,jj) - zs1_rhsu(ji,jj) )                                                                 &
+                  &      +         r1_e2u(ji,jj) * ( e2t(ji+1,jj) * e2t(ji+1,jj) * zs2_rhsu(ji+1,jj) - e2t(ji,jj)   * e2t(ji,jj)   * zs2_rhsu(ji,jj) )     &
+                  &      + 2._wp * r1_e1u(ji,jj) * ( e1f(ji,jj)   * e1f(ji,jj)   * zs12_rhsu(ji,jj)  - e1f(ji,jj-1) * e1f(ji,jj-1) * zs12_rhsu(ji,jj-1) ) )
+                  
+               ! --- V component of internal force contribution to RHS at V points
+               zf_rhsv(ji,jj) = 0.5_wp * r1_e1e2v(ji,jj) * &
+                  &           (    e1v(ji,jj)    * ( zs1_rhsv(ji,jj+1) - zs1_rhsv(ji,jj) )                                                                 &
+                  &      +         r1_e1v(ji,jj) * ( e1t(ji,jj+1) * e1t(ji,jj+1) * zs2_rhsv(ji,jj+1) - e1t(ji,jj) * e1t(ji,jj)     * zs2_rhsv(ji,jj) )     &
+                  &      + 2._wp * r1_e2v(ji,jj) * ( e2f(ji,jj)   * e2f(ji,jj)   * zs12_rhsv(ji,jj)  - e2f(ji-1,jj) * e2f(ji-1,jj) * zs12_rhsv(ji-1,jj) ) )
+                  
+            END DO
+         END DO
+
+         CALL iom_put( 'zf_rhsu'       , zf_rhsu        ) ! MV DEBUG
+         CALL iom_put( 'zf_rhsv'       , zf_rhsv        ) ! MV DEBUG
+         
+         !---------------------------
+         ! -- Sum RHS contributions
+         !---------------------------
+         !
+         ! OPT: could use intermediate scalars to reduce memory access
+         DO jj = 2, jpj - 1
+            DO ji = 2, jpi - 1
+            
+               ! still miss ice ocean stress and acceleration contribution
+               zrhsu(ji,jj) = zmU_t(ji,jj) + ztaux_ai(ji,jj) + ztaux_oi_rhsu(ji,jj) + zspgU(ji,jj) + zCorU(ji,jj) + zf_rhsu(ji,jj)
+               zrhsv(ji,jj) = zmV_t(ji,jj) + ztauy_ai(ji,jj) + ztauy_oi_rhsv(ji,jj) + zspgV(ji,jj) + zCorV(ji,jj) + zf_rhsu(ji,jj)
+
+            END DO
+         END DO
+         
+         CALL lbc_lnk_multi( 'icedyn_rhg_vp', zrhsu, 'U', -1., zrhsv, 'V',  -1.)
+         CALL lbc_lnk_multi( 'icedyn_rhg_vp', zmU_t, 'U', -1., zmV_t, 'V',  -1.)
+         CALL lbc_lnk_multi( 'icedyn_rhg_vp', ztaux_oi_rhsu, 'U', -1., ztauy_oi_rhsv, 'V',  -1.)
+
+         CALL iom_put( 'zmU_t'         , zmU_t          ) ! MV DEBUG
+         CALL iom_put( 'zmV_t'         , zmV_t          ) ! MV DEBUG
+         CALL iom_put( 'ztaux_oi_rhsu' , ztaux_oi_rhsu  ) ! MV DEBUG
+         CALL iom_put( 'ztauy_oi_rhsv' , ztauy_oi_rhsv  ) ! MV DEBUG
+         CALL iom_put( 'zrhsu'         , zrhsu          ) ! MV DEBUG
+         CALL iom_put( 'zrhsv'         , zrhsv          ) ! MV DEBUG
+         
+         ! inner domain calculations -> no lbclnk
+
+         IF( lwp )   WRITE(numout,*) ' outer loop 4 i_out : ', i_out
+     
+         !---------------------------------------------------------------------------------------!
+         !
+         ! --- Linear system matrix
+         !
+         !---------------------------------------------------------------------------------------!
+      
+         ! Linear system matrix contains all implicit contributions 
+         ! 1) internal forces, 2) acceleration, 3) ice-ocean drag
+
+         ! The linear system equation is written as follows
+         ! AU * u_{i-1,j} + BU * u_{i,j}   + CU * u_{i+1,j}
+         !                = DU * u_{i,j-1} + EU * u_{i,j+1} + RHS 			(! my convention, not the same as ZH97 )         
+         
+         ! MV Note 1: martin losch applies boundary condition to BU in mitGCM - check whether it is necessary here ?
+         ! MV Note 2: "T" factor calculations can be optimized by putting things out of the loop 
+         !         only zzt and zet are iteration-dependent, other only depend on scale factors
+                  
+         DO ji = 2, jpi - 1 ! internal domain do loop
+            DO jj = 2, jpj - 1
+
+               !-------------------------------------
+               ! -- Internal forces LHS contribution
+               !-------------------------------------
+               !
+               ! --- U-component
+               !
+               ! "T" factors (intermediate results)
+               !
+               zfac       = 0.5_wp * r1_e1e2u(ji,jj)
+               zfac1      =         zfac * e2u(ji,jj)
+               zfac2      =         zfac * r1_e2u(ji,jj)
+               zfac3      = 2._wp * zfac * r1_e1u(ji,jj)
+
+               zt12U      = - zfac1 * zzt(ji+1,jj)
+               zt11U      =   zfac1 * zzt(ji,jj)
+         
+               zt22U      = - zfac2 * zet(ji+1,jj) * e2t(ji+1,jj) * e2t(ji+1,jj) * e2t(ji+1,jj) * e2t(ji+1,jj)
+               zt21U      =   zfac2 * zet(ji,jj)   * e2t(ji,jj)   * e2t(ji,jj)   * e2t(ji,jj)   * e2t(ji,jj)
+         
+               zt122U     = - zfac3 * zef(ji,jj)   * e1f(ji,jj)   * e1f(ji,jj)   * e1f(ji,jj)   * e1f(ji,jj)
+               zt121U     =   zfac3 * zef(ji,jj-1) * e1f(ji,jj-1) * e1f(ji,jj-1) * e1f(ji,jj-1) * e1f(ji,jj-1)
+               
+               !
+               ! Linear system coefficients
+               !
+               zAU(ji,jj) = - zt11U * e2u(ji-1,jj) - zt21U * r1_e2u(ji-1,jj)
+               zBU(ji,jj) = ( zt12U + zt11U ) * e2u(ji,jj) + ( zt22U + zt21U ) * r1_e2u(ji,jj) + ( zt122U + zt121U ) * r1_e1u(ji,jj)
+               zCU(ji,jj) = - zt12U * e2u(ji+1,jj) - zt22U * r1_e2u(ji+1,jj)
+         
+               zDU(ji,jj) =   zt121U * r1_e1u(ji,jj-1)
+               zEU(ji,jj) =   zt122U * r1_e1u(ji,jj+1)
+              
+               !
+               ! --- V-component
+               !
+               ! "T" factors (intermediate results)
+               !
+               zfac       = 0.5_wp * r1_e1e2v(ji,jj)
+               zfac1      =         zfac * e2v(ji,jj)
+               zfac2      =         zfac * r1_e1v(ji,jj)
+               zfac3      = 2._wp * zfac * r1_e2v(ji,jj)
+         
+               zt12V      = - zfac1 * zzt(ji,jj+1)
+               zt11V      =   zfac1 * zzt(ji,jj)
+         
+               zt22V      =   zfac2 * zet(ji,jj+1) * e1t(ji,jj+1) * e1t(ji,jj+1) * e1t(ji,jj+1) * e1t(ji,jj+1)
+               zt21V      = - zfac2 * zet(ji,jj)   * e1t(ji,jj)   * e1t(ji,jj)   * e1t(ji,jj)   * e1t(ji,jj)
+         
+               zt122V     =   zfac3 * zef(ji,jj)   * e2f(ji,jj)   * e2f(ji,jj)   * e2f(ji,jj)   * e2f(ji,jj)
+               zt121V     = - zfac3 * zef(ji-1,jj) * e2f(ji-1,jj) * e2f(ji-1,jj) * e2f(ji-1,jj) * e2f(ji-1,jj)
+         
+               !
+               ! Linear system coefficients
+               !
+               zAV(ji,jj) = - zt11V * e1v(ji,jj-1) + zt21V * r1_e1v(ji,jj-1)
+               zBV(ji,jj) =  ( zt12V + zt11V ) * e1v(ji,jj) - ( zt22V + zt21V ) * r1_e1v(ji,jj) - ( zt122V + zt121V ) * r1_e2v(ji,jj)
+               zCV(ji,jj) = - zt12V * e1v(ji,jj+1) + zt22V * r1_e1v(ji,jj+1)
+         
+               zDV(ji,jj) = - zt121V * r1_e2v(ji-1,jj) ! mistake is in the pdf notes not here
+               zEV(ji,jj) = - zt122V * r1_e2v(ji+1,jj)
+                  
+               !-----------------------------------------------------
+               ! -- Ocean-ice drag and acceleration LHS contribution
+               !-----------------------------------------------------
+               zBU(ji,jj) = zBU(ji,jj) + zCwU(ji,jj) + zmassU_t(ji,jj)
+               zBV(ji,jj) = ZBV(ji,jj) + zCwV(ji,jj) + zmassV_t(ji,jj)
+         
+            END DO
+         END DO
+
+         CALL lbc_lnk_multi( 'icedyn_rhg_vp', zAU  , 'U', 1., zAV  , 'V',  1. )
+         CALL lbc_lnk_multi( 'icedyn_rhg_vp', zBU  , 'U', 1., zBV  , 'V',  1. )
+         CALL lbc_lnk_multi( 'icedyn_rhg_vp', zCU  , 'U', 1., zCV  , 'V',  1. )
+         CALL lbc_lnk_multi( 'icedyn_rhg_vp', zDU  , 'U', 1., zDV  , 'V',  1. )
+         CALL lbc_lnk_multi( 'icedyn_rhg_vp', zEU  , 'U', 1., zEV  , 'V',  1. )
+               
+         CALL iom_put( 'zAU'           , zAU            ) ! MV DEBUG
+         CALL iom_put( 'zBU'           , zBU            ) ! MV DEBUG
+         CALL iom_put( 'zCU'           , zCU            ) ! MV DEBUG
+         CALL iom_put( 'zDU'           , zDU            ) ! MV DEBUG
+         CALL iom_put( 'zEU'           , zEU            ) ! MV DEBUG
+         CALL iom_put( 'zAV'           , zAV            ) ! MV DEBUG
+         CALL iom_put( 'zBV'           , zBV            ) ! MV DEBUG
+         CALL iom_put( 'zCV'           , zCV            ) ! MV DEBUG
+         CALL iom_put( 'zDV'           , zDV            ) ! MV DEBUG
+         CALL iom_put( 'zEV'           , zEV            ) ! MV DEBUG
+
+      !------------------------------------------------------------------------------!
+      !
+      ! --- Inner loop: solve linear system, check convergence
+      !
+      !------------------------------------------------------------------------------!
+               
+         ! Inner loop solves the linear problem .. requires 1500 iterations
+         ll_u_iterate = .TRUE.
+         ll_v_iterate = .TRUE.
+
+         DO i_inn = 1, nn_vp_ninn ! inner loop iterations
+
+            IF( lwp )   WRITE(numout,*) ' inner loop 1 i_inn : ', i_inn
+         
+            !--- mitgcm computes initial value of residual here...
+
+            jter             = jter + 1
+            ! l_full_nf_update = jter == nn_nvp   ! false: disable full North fold update (performances) for iter = 1 to nn_nevp-1
+
+            zu_b(:,:)      = u_ice(:,:) ! velocity at previous sub-iterate
+            zv_b(:,:)      = v_ice(:,:)
+
+!           zAU(:,:) = 0._wp; zBU(:,:) = 0._wp; zCU(:,:) = 0._wp; zDU(:,:) = 0._wp; zEU(:,:) = 0._wp
+!           zAV(:,:) = 0._wp; zBV(:,:) = 0._wp; zCV(:,:) = 0._wp; zDV(:,:) = 0._wp; zEV(:,:) = 0._wp
+
+            IF ( ll_u_iterate .OR. ll_v_iterate )   THEN
+
+                                           ! ---------------------------- !
+               IF ( ll_u_iterate ) THEN    ! --- Solve for u-velocity --- !
+                                           ! ---------------------------- !
+
+                  ! What follows could be subroutinized...
+      
+                  ! Thomas Algorithm  for tridiagonal solver
+                  ! A*u(i-1,j)+B*u(i,j)+C*u(i+1,j) = F
+
+                  zFU(:,:)       = 0._wp ; zFU_prime(:,:) = 0._wp ; zBU_prime(:,:) = 0._wp; zCU_prime(:,:) = 0._wp
+                  
+                  DO jn = 1, nn_zebra_vp ! "zebra" loop (! red-black-sor!!! )
+                  
+                     ! OPT: could be even better optimized with a true red-black SOR
+      
+                     IF ( jn == 1 ) THEN   ;   jj_min = 2 
+                     ELSE                  ;   jj_min = 3
+                     ENDIF
+
+                     IF ( lwp ) WRITE(numout,*) ' Into the U-zebra loop at step jn = ', jn, ', with jj_min = ', jj_min
+
+                     DO jj = jj_min, jpj - 1, nn_zebra_vp
+                     
+                        !------------------------
+                        ! Independent term (zFU)
+                        !------------------------
+                        DO ji = 2, jpi - 1    
+
+                           ! boundary condition substitution
+                           ! see Zhang and Hibler, 1997, Appendix B
+                           zAA3 = 0._wp
+                           IF ( ji == 2 )         zAA3 = zAA3 - zAU(ji,jj) * u_ice(ji-1,jj)
+                           IF ( ji == jpi - 1 )   zAA3 = zAA3 - zCU(ji,jj) * u_ice(ji+1,jj)
+
+                           ! right hand side
+                           zFU(ji,jj) = ( zrhsu(ji,jj) &                                      ! right-hand side terms
+                               &      +   zAA3         &                                      ! boundary condition translation
+                               &      +   zDU(ji,jj) * u_ice(ji,jj-1)   &                     ! internal force, j-1
+                               &      +   zEU(ji,jj) * u_ice(ji,jj+1) ) * umask(ji,jj,1)      ! internal force, j+1
+
+                        END DO
+
+                     END DO
+                     
+                     CALL lbc_lnk( 'icedyn_rhg_vp', zFU, 'U',  1. )
+                     
+                     !---------------
+                     ! Forward sweep
+                     !---------------   
+                     DO jj = jj_min, jpj - 1, nn_zebra_vp
+      
+                        DO ji = 3, jpi - 1
+
+                           zfac             = SIGN( 1._wp , zBU(ji-1,jj) ) * MAX( 0._wp , SIGN( 1._wp , ABS( zBU(ji-1,jj) ) - epsi20 ) )
+                           zw               = zfac * zAU(ji,jj) / MAX ( ABS( zBU(ji-1,jj) ) , epsi20 ) 
+                           zBU_prime(ji,jj) = zBU(ji,jj) - zw * zCU(ji-1,jj)
+                           zFU_prime(ji,jj) = zFU(ji,jj) - zw * zFU(ji-1,jj)
+
+                        END DO
+
+                     END DO
+
+                     CALL lbc_lnk_multi( 'icedyn_rhg_vp', zFU_prime, 'U',  1., zBU_prime, 'U', 1. )
+ 
+                     !-----------------------------
+                     ! Backward sweep & relaxation
+                     !-----------------------------
+
+                     DO jj = jj_min, jpj - 1, nn_zebra_vp
+                    
+                        ! --- Backward sweep 
+                        ! last row 
+                        zfac = SIGN( 1._wp , zBU_prime(jpi-1,jj) ) * MAX( 0._wp , SIGN( 1._wp , ABS( zBU_prime(jpi-1,jj) ) - epsi20 ) )
+                        u_ice(jpi-1,jj)    = zfac * zFU_prime(jpi-1,jj) / MAX( ABS ( zBU_prime(jpi-1,jj) ) , epsi20 ) & 
+                                           &            * umask(jpi-1,jj,1)
+                        DO ji = jpi-2 , 2, -1 ! all other rows    !  ---> original backward loop
+                           zfac = SIGN( 1._wp , zBU_prime(ji,jj) ) * MAX( 0._wp , SIGN( 1._wp , ABS( zBU_prime(ji,jj) ) - epsi20 ) )
+                           u_ice(ji,jj)    = zfac * ( zFU_prime(ji,jj) - zCU(ji,jj) * u_ice(ji+1,jj) ) * umask(ji,jj,1)   & 
+                                           &                  / MAX ( ABS ( zBU_prime(ji,jj) ) , epsi20 ) 
+                        END DO
+
+                        !--- Relaxation
+                        ! and velocity masking for little-ice and no-ice cases
+                        DO ji = 2, jpi - 1    
+                        
+                           u_ice(ji,jj) = zu_b(ji,jj) + zrelaxu_vp * ( u_ice(ji,jj) - zu_b(ji,jj) ) ! relaxation
+                           
+                           u_ice(ji,jj) =   zmsk00x(ji,jj)                                        &   ! masking
+                              &         * (           zmsk01x(ji,jj)   * u_ice(ji,jj)             &
+                              &           + ( 1._wp - zmsk01x(ji,jj) ) * u_oce(ji,jj) * 0.01_wp     ) * umask(ji,jj,1)
+                           
+                        END DO
+
+                     END DO ! jj
+                     
+                  END DO ! zebra loop
+
+               ENDIF !   ll_u_iterate
+
+               !                           ! ---------------------------- !
+               IF ( ll_v_iterate ) THEN    ! --- Solve for V-velocity --- !
+               !                           ! ---------------------------- !
+                                          
+                  ! MV OPT: what follows could be subroutinized...                  
+                  ! Thomas Algorithm  for tridiagonal solver
+                  ! A*v(i,j-1)+B*v(i,j)+C*v(i,j+1) = F
+                  ! It is intentional to have a ji then jj loop for V-velocity
+                  !!! ZH97 explain it is critical for convergence speed
+
+                  zFV(:,:)       = 0._wp ; zFV_prime(:,:) = 0._wp ; zBV_prime(:,:) = 0._wp; zCV_prime(:,:) = 0._wp
+
+                  DO jn = 1, nn_zebra_vp ! "zebra" loop
+      
+                     IF ( jn == 1 ) THEN   ;   ji_min = 2 
+                     ELSE                  ;   ji_min = 3
+                     ENDIF
+
+                     IF ( lwp ) WRITE(numout,*) ' Into the V-zebra loop at step jn = ', jn, ', with ji_min = ', ji_min
+         
+                     DO ji = ji_min, jpi - 1, nn_zebra_vp 
+                     
+                        !------------------------
+                        ! Independent term (zFV)
+                        !------------------------
+                        DO jj = 2, jpj - 1
+
+                           ! boundary condition substitution (check it is correctly applied !!!)
+                           ! see Zhang and Hibler, 1997, Appendix B
+                           zAA3 = 0._wp
+                           IF ( jj == 2 )       zAA3 = zAA3 - zAV(ji,jj) * v_ice(ji,jj-1)
+                           IF ( jj == jpj - 1 ) zAA3 = zAA3 - zCV(ji,jj) * v_ice(ji,jj+1)
+     
+                           ! right hand side
+                           zFV(ji,jj) = ( zrhsv(ji,jj) &                                   ! right-hand side terms
+                               &        + zAA3                                           & ! boundary condition translation
+                               &        + zDV(ji,jj) * v_ice(ji-1,jj)                    & ! internal force, j-1
+                               &        + zEV(ji,jj) * v_ice(ji+1,jj) ) * vmask(ji,jj,1)   ! internal force, j+1
+
+                        END DO
+                        
+                     END DO
+
+                     CALL lbc_lnk_multi( 'icedyn_rhg_vp', zFV, 'V',  1.)
+                     
+                     !---------------
+                     ! Forward sweep
+                     !---------------
+                     DO ji = ji_min, jpi - 1, nn_zebra_vp 
+                     
+                        DO jj = 3, jpj - 1
+
+                           zfac             = SIGN( 1._wp , zBV(ji,jj-1) ) * MAX( 0._wp , SIGN( 1._wp , ABS( zBV(ji,jj-1) ) - epsi20 ) )
+                           zw               = zfac * zAV(ji,jj) / MAX ( ABS( zBV(ji,jj-1) ) , epsi20 )
+                           zBV_prime(ji,jj) = zBV(ji,jj) - zw * zCV(ji,jj-1)
+                           zFV_prime(ji,jj) = zFV(ji,jj) - zw * zFV(ji,jj-1) 
+
+                        END DO
+
+                     END DO
+
+                     CALL lbc_lnk_multi( 'icedyn_rhg_vp', zFV_prime, 'V',  1., zBV_prime, 'V', 1. )
+                     
+                     !-----------------------------
+                     ! Backward sweep & relaxation
+                     !-----------------------------
+                     DO ji = ji_min, jpi - 1, nn_zebra_vp 
+                    
+                        ! --- Backward sweep 
+                        ! last row
+                        zfac = SIGN( 1._wp , zBV_prime(ji,jpj-1) ) * MAX( 0._wp , SIGN( 1._wp , ABS( zBV_prime(ji,jpj-1) ) - epsi20 ) )
+                        v_ice(ji,jpj-1)  = zfac * zFV_prime(ji,jpj-1) / MAX ( ABS(zBV_prime(ji,jpj-1) ) , epsi20 ) & 
+                                         &         * vmask(ji,jpj-1,1)  ! last row
+
+                        ! other rows
+                        DO jj = jpj-2, 2, -1 ! original back loop
+                           zfac = SIGN( 1._wp , zBV_prime(ji,jj) ) * MAX( 0._wp , SIGN( 1._wp , ABS( zBV_prime(ji,jj) ) - epsi20 ) )
+                           v_ice(ji,jj)   = zfac * ( zFV_prime(ji,jj) - zCV(ji,jj) * v_ice(ji,jj+1) ) * vmask(ji,jj,1) &
+                                          &  / MAX ( ABS( zBV_prime(ji,jj) ) , epsi20 )       
+                        END DO            
+                                                   
+                        ! --- Relaxation  & masking (should it be now or later)
+                        DO jj = 2, jpj - 1
+                        
+                            v_ice(ji,jj) = zv_b(ji,jj) + zrelaxv_vp * ( v_ice(ji,jj) - zv_b(ji,jj) )    ! relaxation
+                            
+                            v_ice(ji,jj) =   zmsk00y(ji,jj)                                        &      ! masking
+                              &         * (           zmsk01y(ji,jj)   * v_ice(ji,jj)              &
+                              &           + ( 1._wp - zmsk01y(ji,jj) ) * v_oce(ji,jj) * 0.01_wp    ) * vmask(ji,jj,1)
+
+                        END DO ! jj
+                        
+                     END DO ! ji
+                     
+                  END DO ! zebra loop
+                                    
+               ENDIF !   ll_v_iterate
+
+               CALL lbc_lnk_multi( 'icedyn_rhg_vp', u_ice, 'U', -1., v_ice, 'V', -1. )
+                              
+               !--------------------------------------------------------------------------------------
+               ! -- Check convergence based on maximum velocity difference, continue or stop the loop
+               !--------------------------------------------------------------------------------------
+
+               !------
+               ! on U
+               !------
+               ! MV OPT: if the number of iterations to convergence is really variable, and keep the convergence check
+               ! then we must optimize the use of the mpp_max, which is prohibitive                            
+               zuerr_max = 0._wp
+                               
+               IF ( ll_u_iterate .AND. MOD ( i_inn, nn_vp_chkcvg ) == 0 ) THEN
+
+                  ! - Maximum U-velocity difference               
+                  zuerr(:,:) = 0._wp
+                  DO jj = 2, jpj - 1
+                     DO ji = 2, jpi - 1
+                        zuerr(ji,jj) = ABS ( ( u_ice(ji,jj) - zu_b(ji,jj) ) ) * umask(ji,jj,1)
+                     END DO
+                  END DO
+                  zuerr_max = MAXVAL( zuerr )
+                  CALL mpp_max( 'icedyn_rhg_evp', zuerr_max )   ! max over the global domain - damned!
+                  
+                  ! - Stop if error is too large ("safeguard against bad forcing" of original Zhang routine)
+                  IF ( i_inn > 1 .AND. zuerr_max > zuerr_max_vp ) THEN
+                      IF ( lwp ) WRITE(numout,*) " VP rheology error was too large : ", zuerr_max, " in outer U-iteration ", i_out, " after ", i_inn, " iterations, we stopped "
+                      ll_u_iterate = .FALSE.
+                  ENDIF
+                  
+                  ! - Stop if error small enough
+                  IF ( zuerr_max < zuerr_min_vp ) THEN                                        
+                      IF ( lwp ) WRITE(numout,*) " VP rheology nicely done in outer U-iteration ", i_out, " after ", i_inn, " iterations, finished! "
+                      ll_u_iterate = .FALSE.
+                  ENDIF
+                                               
+               ENDIF ! ll_u_iterate
+
+               !------
+               ! on V
+               !------
+               zverr_max = 0._wp
+               
+               IF ( ll_v_iterate .AND. MOD ( i_inn, nn_vp_chkcvg ) == 0 ) THEN
+               
+                  ! - Maximum V-velocity difference
+                  zverr(:,:)   = 0._wp   
+                  DO jj = 2, jpj - 1
+                     DO ji = 2, jpi - 1
+                        zverr(ji,jj) = ABS ( ( v_ice(ji,jj) - zv_b(ji,jj) ) ) * vmask(ji,jj,1)
+                     END DO
+                  END DO
+                  
+                  zverr_max = MAXVAL( zverr )
+                  CALL mpp_max( 'icedyn_rhg_evp', zverr_max )   ! max over the global domain - damned!
+                  
+                  ! - Stop if error is too large ("safeguard against bad forcing" of original Zhang routine)
+                  IF ( i_inn > 1 .AND. zverr_max > zuerr_max_vp ) THEN
+                      IF ( lwp ) WRITE(numout,*) " VP rheology error was too large : ", zverr_max, " in outer V-iteration ", i_out, " after ", i_inn, " iterations, we stopped "
+                      ll_v_iterate = .FALSE.
+                  ENDIF
+                  
+                  ! - Stop if error small enough
+                  IF ( zverr_max < zuerr_min_vp ) THEN                                        
+                      IF ( lwp ) WRITE(numout,*) " VP rheology nicely done in outer V-iteration ", i_out, " after ", i_inn, " iterations, finished! "
+                      ll_v_iterate = .FALSE.
+                  ENDIF
+                  
+               ENDIF ! ll_v_iterate
+
+            ENDIF ! ---    end ll_u_iterate or ll_v_iterate
+               
+            !---------------------------------------------------------------------------------------
+            !
+            ! --- Calculate extra convergence diagnostics and save them
+            !
+            !---------------------------------------------------------------------------------------
+
+            IF( nn_rhg_chkcvg/=0 .AND. MOD ( i_inn - 1, nn_vp_chkcvg ) == 0 ) CALL rhg_cvg_vp( kt, jter, nn_nvp, u_ice, v_ice, zmt, zuerr_max, zverr_max, zglob_area, &
+                      &                         zrhsu, zAU, zBU, zCU, zDU, zEU, zrhsv, zAV, zBV, zCV, zDV, zEV )
+
+            IF ( lwp ) WRITE(numout,*) ' Done convergence tests '
+
+         END DO ! i_inn, end of inner loop
+
+      END DO ! End of outer loop (i_out) =============================================================================================
+
+      IF ( lwp ) WRITE(numout,*) ' We are out of outer loop '
+
+      CALL lbc_lnk_multi( 'icedyn_rhg_vp', zFU  , 'U',  1., zFV  , 'V',  1. )
+      CALL lbc_lnk_multi( 'icedyn_rhg_vp', zBU_prime  , 'U',  1., zBV_prime  , 'V',  1. )
+      CALL lbc_lnk_multi( 'icedyn_rhg_vp', zFU_prime  , 'U',  1., zFV_prime  , 'V',  1. )
+      CALL lbc_lnk_multi( 'icedyn_rhg_vp', zCU_prime  , 'U',  1., zCV_prime  , 'V',  1. )
+
+      CALL iom_put( 'zFU'           , zFU            ) ! MV DEBUG
+      CALL iom_put( 'zBU_prime'     , zBU_prime      ) ! MV DEBUG
+      CALL iom_put( 'zCU_prime'     , zCU_prime      ) ! MV DEBUG
+      CALL iom_put( 'zFU_prime'     , zFU_prime      ) ! MV DEBUG
+
+      CALL iom_put( 'zFV'           , zFV            ) ! MV DEBUG
+      CALL iom_put( 'zBV_prime'     , zBV_prime      ) ! MV DEBUG
+      CALL iom_put( 'zCV_prime'     , zCV_prime      ) ! MV DEBUG
+      CALL iom_put( 'zFV_prime'     , zFV_prime      ) ! MV DEBUG
+
+      CALL lbc_lnk_multi( 'icedyn_rhg_vp', u_ice, 'U', -1., v_ice, 'V', -1. )
+
+      IF ( lwp ) WRITE(numout,*) ' We are about to output uice_dbg '
+      IF( iom_use('uice_dbg' ) )   CALL iom_put( 'uice_dbg'   , u_ice    )                              ! ice velocity u after solver
+      IF( iom_use('vice_dbg' ) )   CALL iom_put( 'vice_dbg'   , v_ice    )                              ! ice velocity v after solver
+            
+      !------------------------------------------------------------------------------!
+      !
+      ! --- Convergence diagnostics 
+      !
+      !------------------------------------------------------------------------------!
+
+      IF( nn_rhg_chkcvg /= 0 ) THEN
+          
+         IF( iom_use('uice_cvg')  ) THEN
+            CALL iom_put( 'uice_cvg', MAX( ABS( u_ice(:,:) - zu_b(:,:) ) * umask(:,:,1) , &                ! ice velocity difference at last iteration
+                  &                        ABS( v_ice(:,:) - zv_b(:,:) ) * vmask(:,:,1) ) * zmsk15(:,:) )
+         ENDIF   
+        
+      ENDIF
+
+      ! MV DEBUG test - replace ice velocity by ocean current to give the model the means to go ahead
+      DO jj = 2, jpj - 1
+         DO ji = 2, jpi - 1   
+
+             u_ice(ji,jj) =   zmsk00x(ji,jj)                               & 
+      &         * (           zmsk01x(ji,jj)   * u_oce(ji,jj) * 0.01_wp    &
+                  + ( 1._wp - zmsk01x(ji,jj) ) * u_oce(ji,jj) * 0.01_wp    )
+
+             v_ice(ji,jj) =   zmsk00y(ji,jj)                               & 
+      &         * (           zmsk01y(ji,jj)   * v_oce(ji,jj) * 0.01_wp    &
+                  + ( 1._wp - zmsk01y(ji,jj) ) * v_oce(ji,jj) * 0.01_wp    )
+
+         END DO
+      END DO
+
+      CALL lbc_lnk_multi( 'icedyn_rhg_vp', u_ice, 'U', -1., v_ice, 'V', -1. )
+
+      IF ( lwp ) WRITE(numout,*) ' Velocity replaced '
+
+      ! END DEBUG
+
+      !------------------------------------------------------------------------------!
+      !
+      ! --- Recompute delta, shear and div (inputs for mechanical redistribution) 
+      !
+      !------------------------------------------------------------------------------!
+      !
+      ! MV OPT: subroutinize ?
+
+      DO jj = 1, jpj - 1
+         DO ji = 1, jpi - 1
+
+            ! shear at F points
+            zds(ji,jj) = ( ( u_ice(ji,jj+1) * r1_e1u(ji,jj+1) - u_ice(ji,jj) * r1_e1u(ji,jj) ) * e1f(ji,jj) * e1f(ji,jj)   &
+               &         + ( v_ice(ji+1,jj) * r1_e2v(ji+1,jj) - v_ice(ji,jj) * r1_e2v(ji,jj) ) * e2f(ji,jj) * e2f(ji,jj)   &
+               &         ) * r1_e1e2f(ji,jj) * zfmask(ji,jj)
+
+         END DO
+      END DO           
+      
+      DO jj = 2, jpj - 1
+         DO ji = 2, jpi - 1 ! 
+            
+            ! tension**2 at T points
+            zdt  = ( ( u_ice(ji,jj) * r1_e2u(ji,jj) - u_ice(ji-1,jj) * r1_e2u(ji-1,jj) ) * e2t(ji,jj) * e2t(ji,jj)   &
+               &   - ( v_ice(ji,jj) * r1_e1v(ji,jj) - v_ice(ji,jj-1) * r1_e1v(ji,jj-1) ) * e1t(ji,jj) * e1t(ji,jj)   &
+               &   ) * r1_e1e2t(ji,jj)
+            zdt2 = zdt * zdt
+            
+            zten_i(ji,jj) = zdt
+            
+            ! shear**2 at T points (doc eq. A16)
+            zds2 = ( zds(ji,jj  ) * zds(ji,jj  ) * e1e2f(ji,jj  ) + zds(ji-1,jj  ) * zds(ji-1,jj  ) * e1e2f(ji-1,jj  )  &
+               &   + zds(ji,jj-1) * zds(ji,jj-1) * e1e2f(ji,jj-1) + zds(ji-1,jj-1) * zds(ji-1,jj-1) * e1e2f(ji-1,jj-1)  &
+               &   ) * 0.25_wp * r1_e1e2t(ji,jj)
+            
+            ! shear at T points
+            pshear_i(ji,jj) = SQRT( zdt2 + zds2 )
+
+            ! divergence at T points
+            pdivu_i(ji,jj) = ( e2u(ji,jj) * u_ice(ji,jj) - e2u(ji-1,jj) * u_ice(ji-1,jj)   &
+               &             + e1v(ji,jj) * v_ice(ji,jj) - e1v(ji,jj-1) * v_ice(ji,jj-1)   &
+               &             ) * r1_e1e2t(ji,jj)
+            
+            ! delta at T points
+            zdelta         = SQRT( pdivu_i(ji,jj) * pdivu_i(ji,jj) + ( zdt2 + zds2 ) * z1_ecc2 )  
+            rswitch        = 1._wp - MAX( 0._wp, SIGN( 1._wp, -zdelta ) ) ! 0 if delta=0
+            
+            !pdelta_i(ji,jj) = zdelta + rn_creepl * rswitch
+            pdelta_i(ji,jj) = zdelta + rn_creepl
+
+         END DO
+      END DO
+
+      IF ( lwp ) WRITE(numout,*) ' Deformation recalculated '
+      
+      CALL lbc_lnk_multi( 'icedyn_rhg_vp', pshear_i, 'T', 1., pdivu_i, 'T', 1., pdelta_i, 'T', 1. )
+      
+      !------------------------------------------------------------------------------!
+      !
+      ! --- Diagnostics
+      !
+      !------------------------------------------------------------------------------!
+      !
+      ! MV OPT: subroutinize ?
+      !
+      !----------------------------------
+      ! --- Recompute stresses if needed 
+      !----------------------------------
+      !
+      ! ---- Sea ice stresses at T-points
+      IF ( iom_use('normstr') .OR. iom_use('sheastr') .OR. iom_use('intstrx') .OR. iom_use('intstry') ) THEN
+      
+         DO jj = 2, jpj - 1
+            DO ji = 2, jpi - 1
+               zp_deltastar_t(ji,jj)   =   strength(ji,jj) / pdelta_i(ji,jj) 
+               zfac                    =   zp_deltastar_t(ji,jj) 
+               zs1(ji,jj)              =   zfac * ( pdivu_i(ji,jj) - pdelta_i(ji,jj) )
+               zs2(ji,jj)              =   zfac * z1_ecc2 * zten_i(ji,jj)
+               zs12(ji,jj)             =   zfac * z1_ecc2 * pshear_i(ji,jj)
+            END DO
+         END DO
+
+         CALL lbc_lnk_multi( 'icedyn_rhg_vp', zs1, 'T', 1., zs2, 'T', 1., zs12, 'T', 1. )
+      
+      ENDIF
+      
+      ! ---- s12 at F-points      
+      IF ( iom_use('intstrx') .OR. iom_use('intstry') ) THEN
+
+         DO jj = 1, jpj - 1
+            DO ji = 1, jpi - 1
+            
+               ! P/delta* at F points
+               zp_deltastar_f = 0.25_wp * ( zp_deltastar_t(ji,jj) + zp_deltastar_t(ji+1,jj) + zp_deltastar_t(ji,jj+1) + zp_deltastar_t(ji+1,jj+1) )
+               
+               ! s12 at F-points 
+               zs12f(ji,jj) = zp_deltastar_f * z1_ecc2 * zds(ji,jj)
+               
+            END DO
+         END DO
+
+         CALL lbc_lnk( 'icedyn_rhg_vp', zs12f, 'F', 1. )
+         
+      ENDIF
+
+      IF ( lwp ) WRITE(numout,*) ' zs12f recalculated '
+
+      !
+      !-----------------------
+      ! --- Store diagnostics
+      !-----------------------
+      !
+      ! --- Ice-ocean, ice-atm. & ice-ocean bottom (landfast) stresses --- !
+      IF(  iom_use('utau_oi') .OR. iom_use('vtau_oi') .OR. iom_use('utau_ai') .OR. iom_use('vtau_ai') .OR. &
+         & iom_use('utau_bi') .OR. iom_use('vtau_bi') ) THEN
+
+         ALLOCATE( ztaux_oi(jpi,jpj) , ztauy_oi(jpi,jpj) )
+
+         !--- Recalculate oceanic stress at last inner iteration
+         DO jj = 2, jpj - 1
+            DO ji = 2, jpi - 1
+
+                !--- ice u-velocity @V points, v-velocity @U points (for non-linear drag computation)
+                zu_cV            = 0.25_wp * ( u_ice(ji,jj) + u_ice(ji-1,jj) + u_ice(ji,jj+1) + u_ice(ji-1,jj+1) ) * vmask(ji,jj,1)
+                zv_cU            = 0.25_wp * ( v_ice(ji,jj) + v_ice(ji,jj-1) + v_ice(ji+1,jj) + v_ice(ji+1,jj-1) ) * umask(ji,jj,1)
+                
+                !--- non-linear drag coefficients (need to be updated at each outer loop, see Lemieux and Tremblay JGR09, p.3, beginning of Section 3)
+                zCwU(ji,jj)          = za_iU(ji,jj) * zrhoco * SQRT( ( u_ice(ji,jj) - u_oce (ji,jj) ) * ( u_ice(ji,jj) - u_oce (ji,jj) )  &
+                  &                                                + ( zv_cU - v_oceU(ji,jj) ) * ( zv_cU - v_oceU(ji,jj) ) )
+                zCwV(ji,jj)          = za_iV(ji,jj) * zrhoco * SQRT( ( v_ice(ji,jj) - v_oce (ji,jj) ) * ( v_ice(ji,jj) - v_oce (ji,jj) )  &
+                  &                                                + ( zu_cV - u_oceV(ji,jj) ) * ( zu_cV - u_oceV(ji,jj) ) )
+                 
+                !--- Ocean-ice stress
+                ztaux_oi(ji,jj) = zCwU(ji,jj) * ( u_oce(ji,jj) - u_ice(ji,jj) )
+                ztauy_oi(ji,jj) = zCwV(ji,jj) * ( v_oce(ji,jj) - v_ice(ji,jj) )
+                
+            END DO
+         END DO
+         
+         !
+         CALL lbc_lnk_multi( 'icedyn_rhg_vp', ztaux_oi, 'U', -1., ztauy_oi, 'V', -1., ztaux_ai, 'U', -1., ztauy_ai, 'V', -1. ) !, &
+!            &                                 ztaux_bi, 'U', -1., ztauy_bi, 'V', -1. )
+         !
+         CALL iom_put( 'utau_oi' , ztaux_oi * zmsk00 )
+         CALL iom_put( 'vtau_oi' , ztauy_oi * zmsk00 )
+         CALL iom_put( 'utau_ai' , ztaux_ai * zmsk00 )
+         CALL iom_put( 'vtau_ai' , ztauy_ai * zmsk00 )
+!        CALL iom_put( 'utau_bi' , ztaux_bi * zmsk00 )
+!        CALL iom_put( 'vtau_bi' , ztauy_bi * zmsk00 )
+
+         DEALLOCATE( ztaux_oi , ztauy_oi )
+
+      ENDIF
+       
+      ! --- Divergence, shear and strength --- !
+      IF( iom_use('icediv') )   CALL iom_put( 'icediv' , pdivu_i  * zmsk00 )   ! divergence
+      IF( iom_use('iceshe') )   CALL iom_put( 'iceshe' , pshear_i * zmsk00 )   ! shear
+      IF( iom_use('icedlt') )   CALL iom_put( 'icedlt' , pdelta_i * zmsk00 )   ! delta
+      IF( iom_use('icestr') )   CALL iom_put( 'icestr' , strength * zmsk00 )   ! strength
+
+      IF ( lwp ) WRITE(numout,*) 'Some terms recalculated '
+
+      ! --- Stress tensor invariants (SIMIP diags) --- !
+      IF( iom_use('normstr') .OR. iom_use('sheastr') ) THEN
+         !
+         ! Stress tensor invariants (normal and shear stress N/m) - SIMIP diags.
+         ! Definitions following Coon (1974) and Feltham (2008)
+         !
+         ! sigma1, sigma2, sigma12 are useful (Hunke and Dukowicz MWR 2002, Bouillon et al., OM2013)
+         ! however these are NOT stress tensor components, neither stress invariants, nor stress principal components
+         !
+         ALLOCATE( zsig_I(jpi,jpj) , zsig_II(jpi,jpj) )
+         !         
+         DO jj = 2, jpj - 1
+            DO ji = 2, jpi - 1
+               ! Stress invariants
+               zsig_I(ji,jj)    =   zs1(ji,jj) * 0.5_wp                                        ! 1st invariant, aka average normal stress aka negative pressure
+               zsig_II(ji,jj)   =   SQRT ( zs2(ji,jj) * zs2(ji,jj) * 0.25_wp + zs12(ji,jj) )  ! 2nd invariant, aka maximum shear stress               
+            END DO
+         END DO
+
+         CALL lbc_lnk_multi( 'icedyn_rhg_vp', zsig_I, 'T', 1., zsig_II, 'T', 1.)
+         
+         IF( iom_use('normstr') )   CALL iom_put( 'normstr' ,   zsig_I(:,:)  * zmsk00(:,:) ) ! Normal stress
+         IF( iom_use('sheastr') )   CALL iom_put( 'sheastr' ,   zsig_II(:,:) * zmsk00(:,:) ) ! Maximum shear stress
+         
+         DEALLOCATE ( zsig_I, zsig_II )
+         
+      ENDIF
+
+      IF ( lwp ) WRITE(numout,*) 'SIMIP work done'
+
+      ! --- Normalized stress tensor principal components --- !
+      ! These are used to plot the normalized yield curve (Lemieux & Dupont, GMD 2020)
+      ! To plot the yield curve and evaluate physical convergence, they have two recommendations
+      ! Recommendation 1 : Use ice strength, not replacement pressure
+      ! Recommendation 2 : Need to use deformations at PREVIOUS iterate for viscosities (see p. 1765)
+      ! R2 means we need to recompute stresses
+
+      IF( iom_use('sig1_pnorm') .OR. iom_use('sig2_pnorm') ) THEN
+         !
+         ALLOCATE( zsig1_p(jpi,jpj) , zsig2_p(jpi,jpj) , zsig_I(jpi,jpj) , zsig_II(jpi,jpj) )
+         !         
+         DO jj = 2, jpj - 1
+            DO ji = 2, jpi - 1
+               ! Ice stresses computed with **viscosities** (delta, p/delta) at **previous** iterates 
+               !                        and **deformations** at current iterates
+               !                        following Lemieux & Dupont (2020)
+               zfac             =   zp_deltastar_t(ji,jj)
+               zsig1            =   zfac * ( pdivu_i(ji,jj) - zdeltastar_t(ji,jj) )
+               zsig1            = 0._wp !!! FUCKING DEBUG TEST !!!
+               zsig2            =   zfac * z1_ecc2 * zten_i(ji,jj)
+               zsig12           =   zfac * z1_ecc2 * pshear_i(ji,jj)
+               
+               ! Stress invariants (sigma_I, sigma_II, Coon 1974, Feltham 2008), T-point
+               zsig_I(ji,jj)    =   zsig1 * 0.5_wp                              ! 1st invariant
+               zsig_II(ji,jj)   =   SQRT ( zsig2 * zsig2 * 0.25_wp + zsig12 )   ! 2nd invariant
+
+               ! Normalized  principal stresses (used to display the ellipse)
+               z1_strength      =   1._wp / MAX ( 1._wp , strength(ji,jj) )
+               zsig1_p(ji,jj)   =   ( zsig_I(ji,jj) + zsig_II(ji,jj) ) * z1_strength
+               zsig2_p(ji,jj)   =   ( zsig_I(ji,jj) - zsig_II(ji,jj) ) * z1_strength
+            END DO
+         END DO
+         IF ( lwp ) WRITE(numout,*) 'Some shitty stress work done'
+         !
+         CALL lbc_lnk_multi( 'icedyn_rhg_vp', zsig1_p, 'T', 1., zsig2_p, 'T', 1.)
+         !      
+         IF ( lwp ) WRITE(numout,*) ' Beauaaaarflblbllll '
+         !
+         CALL iom_put( 'sig1_pnorm' , zsig1_p ) 
+         CALL iom_put( 'sig2_pnorm' , zsig2_p ) 
+
+         DEALLOCATE( zsig1_p , zsig2_p , zsig_I , zsig_II )
+
+         IF ( lwp ) WRITE(numout,*) ' So what ??? '
+         
+      ENDIF
+
+      ! --- SIMIP, terms of tendency for momentum equation  --- !
+      IF(  iom_use('dssh_dx') .OR. iom_use('dssh_dy') .OR. &
+         & iom_use('corstrx') .OR. iom_use('corstry') ) THEN
+
+         ! --- Recalculate Coriolis stress at last inner iteration
+         DO jj = 2, jpj - 1
+            DO ji = 2, jpi - 1
+                ! --- U-component 
+                zCorU(ji,jj)         =   0.25_wp * r1_e1u(ji,jj) *  &
+                           &             ( zmf(ji  ,jj) * ( e1v(ji  ,jj) * v_ice(ji  ,jj) + e1v(ji  ,jj-1) * v_ice(ji  ,jj-1) )  &
+                           &             + zmf(ji+1,jj) * ( e1v(ji+1,jj) * v_ice(ji+1,jj) + e1v(ji+1,jj-1) * v_ice(ji+1,jj-1) ) )
+                zCorV(ji,jj)         = - 0.25_wp * r1_e2v(ji,jj) *  &
+                           &             ( zmf(ji,jj  ) * ( e2u(ji,jj  ) * u_ice(ji,jj  ) + e2u(ji-1,jj  ) * u_ice(ji-1,jj  ) )  &
+                           &             + zmf(ji,jj+1) * ( e2u(ji,jj+1) * u_ice(ji,jj+1) + e2u(ji-1,jj+1) * u_ice(ji-1,jj+1) ) )
+            END DO
+         END DO
+         !
+         CALL lbc_lnk_multi( 'icedyn_rhg_vp', zspgU, 'U', -1., zspgV, 'V', -1., &
+            &                                 zCorU, 'U', -1., zCorV, 'V', -1. )
+         !
+         CALL iom_put( 'dssh_dx' , zspgU * zmsk00 )   ! Sea-surface tilt term in force balance (x)
+         CALL iom_put( 'dssh_dy' , zspgV * zmsk00 )   ! Sea-surface tilt term in force balance (y)
+         CALL iom_put( 'corstrx' , zCorU * zmsk00 )   ! Coriolis force term in force balance (x)
+         CALL iom_put( 'corstry' , zCorV * zmsk00 )   ! Coriolis force term in force balance (y)
+
+      ENDIF
+      
+      IF ( iom_use('intstrx') .OR. iom_use('intstry') ) THEN
+
+         ! Recalculate internal forces (divergence of stress tensor) at last inner iteration
+         DO jj = 2, jpj - 1
+            DO ji = 2, jpi - 1
+               zfU(ji,jj) = 0.5_wp * ( ( zs1(ji+1,jj) - zs1(ji,jj) ) * e2u(ji,jj)                                             &
+                  &                  + ( zs2(ji+1,jj) * e2t(ji+1,jj) * e2t(ji+1,jj) - zs2(ji,jj) * e2t(ji,jj) * e2t(ji,jj)    &
+                  &                    ) * r1_e2u(ji,jj)                                                                      &
+                  &                  + ( zs12f(ji,jj) * e1f(ji,jj) * e1f(ji,jj) - zs12f(ji,jj-1) * e1f(ji,jj-1) * e1f(ji,jj-1)  &
+                  &                    ) * 2._wp * r1_e1u(ji,jj)                                                              &
+                  &                  ) * r1_e1e2u(ji,jj)
+               zfV(ji,jj) = 0.5_wp * ( ( zs1(ji,jj+1) - zs1(ji,jj) ) * e1v(ji,jj)                                             &
+                  &                  - ( zs2(ji,jj+1) * e1t(ji,jj+1) * e1t(ji,jj+1) - zs2(ji,jj) * e1t(ji,jj) * e1t(ji,jj)    &
+                  &                    ) * r1_e1v(ji,jj)                                                                      &
+                  &                  + ( zs12f(ji,jj) * e2f(ji,jj) * e2f(ji,jj) - zs12f(ji-1,jj) * e2f(ji-1,jj) * e2f(ji-1,jj)  &
+                  &                    ) * 2._wp * r1_e2v(ji,jj)                                                              &
+                  &                  ) * r1_e1e2v(ji,jj)
+            END DO
+         END DO
+            
+         CALL lbc_lnk_multi( 'icedyn_rhg_vp', zfU, 'U', -1., zfV, 'V', -1. )
+         
+         CALL iom_put( 'intstrx' , zfU   * zmsk00 )   ! Internal force term in force balance (x)
+         CALL iom_put( 'intstry' , zfV   * zmsk00 )   ! Internal force term in force balance (y)
+         
+      ENDIF
+
+      ! --- Ice & snow mass and ice area transports
+      IF(  iom_use('xmtrpice') .OR. iom_use('ymtrpice') .OR. &
+         & iom_use('xmtrpsnw') .OR. iom_use('ymtrpsnw') .OR. iom_use('xatrp') .OR. iom_use('yatrp') ) THEN
+         !
+         ALLOCATE( zdiag_xmtrp_ice(jpi,jpj) , zdiag_ymtrp_ice(jpi,jpj) , &
+            &      zdiag_xmtrp_snw(jpi,jpj) , zdiag_ymtrp_snw(jpi,jpj) , zdiag_xatrp(jpi,jpj) , zdiag_yatrp(jpi,jpj) )
+         !
+         DO jj = 2, jpj - 1
+            DO ji = 2, jpi - 1
+               ! 2D ice mass, snow mass, area transport arrays (X, Y)
+               zfac_x = 0.5 * u_ice(ji,jj) * e2u(ji,jj) * zmsk00(ji,jj)
+               zfac_y = 0.5 * v_ice(ji,jj) * e1v(ji,jj) * zmsk00(ji,jj)
+
+               zdiag_xmtrp_ice(ji,jj) = rhoi * zfac_x * ( vt_i(ji+1,jj) + vt_i(ji,jj) ) ! ice mass transport, X-component
+               zdiag_ymtrp_ice(ji,jj) = rhoi * zfac_y * ( vt_i(ji,jj+1) + vt_i(ji,jj) ) !        ''           Y-   ''
+
+               zdiag_xmtrp_snw(ji,jj) = rhos * zfac_x * ( vt_s(ji+1,jj) + vt_s(ji,jj) ) ! snow mass transport, X-component
+               zdiag_ymtrp_snw(ji,jj) = rhos * zfac_y * ( vt_s(ji,jj+1) + vt_s(ji,jj) ) !          ''          Y-   ''
+
+               zdiag_xatrp(ji,jj)     = zfac_x * ( at_i(ji+1,jj) + at_i(ji,jj) )        ! area transport,      X-component
+               zdiag_yatrp(ji,jj)     = zfac_y * ( at_i(ji,jj+1) + at_i(ji,jj) )        !        ''            Y-   ''
+
+            END DO
+         END DO
+
+         CALL lbc_lnk_multi( 'icedyn_rhg_vp', zdiag_xmtrp_ice, 'U', -1., zdiag_ymtrp_ice, 'V', -1., &
+            &                                 zdiag_xmtrp_snw, 'U', -1., zdiag_ymtrp_snw, 'V', -1., &
+            &                                 zdiag_xatrp    , 'U', -1., zdiag_yatrp    , 'V', -1. )
+
+         CALL iom_put( 'xmtrpice' , zdiag_xmtrp_ice )   ! X-component of sea-ice mass transport (kg/s)
+         CALL iom_put( 'ymtrpice' , zdiag_ymtrp_ice )   ! Y-component of sea-ice mass transport 
+         CALL iom_put( 'xmtrpsnw' , zdiag_xmtrp_snw )   ! X-component of snow mass transport (kg/s)
+         CALL iom_put( 'ymtrpsnw' , zdiag_ymtrp_snw )   ! Y-component of snow mass transport
+         CALL iom_put( 'xatrp'    , zdiag_xatrp     )   ! X-component of ice area transport
+         CALL iom_put( 'yatrp'    , zdiag_yatrp     )   ! Y-component of ice area transport
+
+         DEALLOCATE( zdiag_xmtrp_ice , zdiag_ymtrp_ice , &
+            &        zdiag_xmtrp_snw , zdiag_ymtrp_snw , zdiag_xatrp , zdiag_yatrp )
+
+      ENDIF
+
+      DEALLOCATE( zmsk00, zmsk15 )
+
+   END SUBROUTINE ice_dyn_rhg_vp
+   
+   
+   
+   SUBROUTINE rhg_cvg_vp( kt, kiter, kitermax, pu, pv, pmt, puerr_max, pverr_max, pglob_area, &
+                  &       prhsu, pAU, pBU, pCU, pDU, pEU, prhsv, pAV, pBV, pCV, pDV, pEV )
+   
+      !!----------------------------------------------------------------------
+      !!                    ***  ROUTINE rhg_cvg_vp  ***
+      !!                     
+      !! ** Purpose :   check convergence of VP ice rheology
+      !!
+      !! ** Method  :   create a file ice_cvg.nc containing a few convergence diagnostics
+      !!                This routine is called every sub-iteration, so it is cpu expensive
+      !!
+      !!                Calculates / stores
+      !!                   - maximum absolute U-V difference (uice_cvg, u_dif, v_dif, m/s)
+      !!                   - residuals in U, V and UV-mean taken as square-root of area-weighted mean square residual (u_res, v_res, vel_res, N/m2)
+      !!                   - mean kinetic energy (mke_ice, J/m2)
+      !!
+      !! ** Note    :   for the first sub-iteration, uice_cvg is set to 0 (too large otherwise)   
+      !!----------------------------------------------------------------------
+      INTEGER ,                 INTENT(in) ::   kt, kiter, kitermax      ! ocean time-step index
+      REAL(wp), DIMENSION(:,:), INTENT(in) ::   pu, pv, pmt              ! now velocity and mass per unit area 
+      REAL(wp),                 INTENT(in) ::   puerr_max, pverr_max     ! absolute mean velocity difference
+      REAL(wp),                 INTENT(in) ::   pglob_area               ! global ice area
+      REAL(wp), DIMENSION(:,:), INTENT(in) ::   prhsu, pAU, pBU, pCU, pDU, pEU ! linear system coefficients 
+      REAL(wp), DIMENSION(:,:), INTENT(in) ::   prhsv, pAV, pBV, pCV, pDV, pEV
+      !!
+      INTEGER           ::   it, idtime, istatus, ix_dim, iy_dim
+      INTEGER           ::   ji, jj          ! dummy loop indices
+      REAL(wp)          ::   zveldif, zu_res_mean, zv_res_mean, zvelres, zmke, zu, zv ! local scalars
+      REAL(wp)          ::   z1_pglob_area
+      REAL(wp), DIMENSION(jpi,jpj) ::   zu_res, zv_res, zvel2                         ! local arrays
+                                                                             
+      CHARACTER(len=20) ::   clname
+      !!----------------------------------------------------------------------
+
+      IF( lwp ) THEN
+         WRITE(numout,*)
+         WRITE(numout,*) 'rhg_cvg_vp : ice rheology convergence control'
+         WRITE(numout,*) '~~~~~~~~~~~'
+         WRITE(numout,*) ' kiter    =  : ', kiter
+         WRITE(numout,*) ' kitermax =  : ', kitermax
+      ENDIF
+
+      ! create file
+      IF( kt == nit000 .AND. kiter == 1 ) THEN
+         !
+         IF( lwm ) THEN
+
+            clname = 'ice_cvg.nc'
+            IF( .NOT. Agrif_Root() )   clname = TRIM(Agrif_CFixed())//"_"//TRIM(clname)
+            istatus = NF90_CREATE( TRIM(clname), NF90_CLOBBER, ncvgid )
+
+            istatus = NF90_DEF_DIM( ncvgid, 'time'  , NF90_UNLIMITED, idtime )
+            istatus = NF90_DEF_DIM( ncvgid, 'x'     , jpi, ix_dim )
+            istatus = NF90_DEF_DIM( ncvgid, 'y'     , jpj, iy_dim )
+
+            ! i suggest vel_dif instead
+            istatus = NF90_DEF_VAR( ncvgid, 'u_res'  , NF90_DOUBLE  , (/ idtime /), nvarid_ures )
+            istatus = NF90_DEF_VAR( ncvgid, 'v_res'  , NF90_DOUBLE  , (/ idtime /), nvarid_vres )
+            istatus = NF90_DEF_VAR( ncvgid, 'vel_res', NF90_DOUBLE  , (/ idtime /), nvarid_velres )
+            istatus = NF90_DEF_VAR( ncvgid, 'u_dif'  , NF90_DOUBLE  , (/ idtime /), nvarid_udif )
+            istatus = NF90_DEF_VAR( ncvgid, 'v_dif'  , NF90_DOUBLE  , (/ idtime /), nvarid_vdif )
+            istatus = NF90_DEF_VAR( ncvgid, 'vel_dif', NF90_DOUBLE  , (/ idtime /), nvarid_veldif )
+            istatus = NF90_DEF_VAR( ncvgid, 'mke_ice', NF90_DOUBLE  , (/ idtime /), nvarid_mke )
+
+            istatus = NF90_DEF_VAR( ncvgid, 'u_res_xy', NF90_DOUBLE, (/ ix_dim, iy_dim /), nvarid_ures_xy)
+            istatus = NF90_DEF_VAR( ncvgid, 'v_res_xy', NF90_DOUBLE, (/ ix_dim, iy_dim /), nvarid_vres_xy)
+
+            istatus = NF90_ENDDEF(ncvgid)
+
+         ENDIF
+         !
+      ENDIF
+
+      IF ( lwp ) WRITE(numout,*) ' File created '
+
+      ! --- Max absolute velocity difference with previous iterate (zveldif)
+      zveldif = MAX( puerr_max, pverr_max ) ! velocity difference with previous iterate, should nearly be equivalent to evp code 
+                                            ! if puerrmask and pverrmax are masked at 15% (TEST)
+
+      ! ---  Mean residual and kinetic energy
+      IF ( kiter == 1 ) THEN
+
+         zu_res_mean = 0._wp
+         zv_res_mean = 0._wp
+         zvelres     = 0._wp
+         zmke        = 0._wp
+
+      ELSE
+
+      ! -- Mean residual (N/m^2), zu_res_mean
+      ! Here we take the residual of the linear system (N/m^2), 
+      ! We define it as in mitgcm: square-root of area-weighted mean square residual
+      ! Local residual r = Ax - B expresses to which extent the momentum balance is verified 
+      ! i.e., how close we are to a solution
+
+      IF ( lwp ) WRITE(numout,*) ' TEST 1 ' 
+
+      z1_pglob_area = 1._wp / pglob_area
+
+      zu_res(:,:) = 0._wp; zv_res(:,:) = 0._wp
+
+      DO jj = 2, jpj - 1
+         DO ji = 2, jpi - 1                                      
+            zu_res(ji,jj)  = ( prhsu(ji,jj) + pDU(ji,jj) * pu(ji,jj-1) + pEU(ji,jj) * pu(ji,jj+1)               &
+               &             - pAU(ji,jj) * pu(ji-1,jj) - pBU(ji,jj) * pu(ji,jj) - pCU(ji,jj) * pu(ji+1,jj) )
+                           
+            zv_res(ji,jj)  = ( prhsv(ji,jj) + pDV(ji,jj) * pv(ji-1,jj) + pEV(ji,jj) * pv(ji+1,jj)               &
+               &             - pAV(ji,jj) * pv(ji,jj-1) - pBV(ji,jj) * pv(ji,jj) - pCV(ji,jj) * pv(ji,jj+1) )
+
+            zu_res(ji,jj)  = SQRT( zu_res(ji,jj) * zu_res(ji,jj) ) * umask(ji,jj,1) * e1e2u(ji,jj) * z1_pglob_area
+            zv_res(ji,jj)  = SQRT( zv_res(ji,jj) * zv_res(ji,jj) ) * vmask(ji,jj,1) * e1e2v(ji,jj) * z1_pglob_area
+
+         END DO
+      END DO                  
+
+      IF ( lwp ) WRITE(numout,*) ' TEST 2 ' 
+      zu_res_mean = glob_sum( 'ice_rhg_vp', zu_res(:,:) )
+      zv_res_mean = glob_sum( 'ice_rhg_vp', zv_res(:,:) )
+      IF ( lwp ) WRITE(numout,*) ' TEST 3 ' 
+      zvelres     = 0.5_wp * ( zu_res_mean + zv_res_mean )
+
+      IF ( lwp ) WRITE(numout,*) ' TEST 4 ' 
+                                          
+      ! -- Global mean kinetic energy per unit area (J/m2)
+      zvel2(:,:) = 0._wp
+      DO jj = 2, jpj - 1
+         DO ji = 2, jpi - 1                   
+            zu     = 0.5_wp * ( pu(ji-1,jj) + pu(ji,jj) ) ! u-vel at T-point
+            zv     = 0.5_wp * ( pv(ji,jj-1) + pv(ji,jj) )
+            zvel2(ji,jj)  = zu*zu + zv*zv              ! square of ice velocity at T-point  
+         END DO
+      END DO
+       
+      IF ( lwp ) WRITE(numout,*) ' TEST 5 ' 
+
+      zmke = 0.5_wp * glob_sum( 'ice_rhg_vp', pmt(:,:) * e1e2t(:,:) * zvel2(:,:) ) / pglob_area
+
+      IF ( lwp ) WRITE(numout,*) ' TEST 6 '
+
+      ENDIF ! kiter
+                  
+         !                                                ! ==================== !
+
+      ! time
+      it = ( kt - 1 ) * kitermax + kiter
+
+
+      IF( lwm ) THEN
+         ! write variables
+         istatus = NF90_PUT_VAR( ncvgid, nvarid_ures, (/zu_res_mean/), (/it/), (/1/) ) ! U-residual of the linear system
+         istatus = NF90_PUT_VAR( ncvgid, nvarid_vres, (/zv_res_mean/), (/it/), (/1/) ) ! V-residual of the linear system
+         istatus = NF90_PUT_VAR( ncvgid, nvarid_velres, (/zvelres/), (/it/), (/1/) )   ! average of u- and v- residuals
+         istatus = NF90_PUT_VAR( ncvgid, nvarid_udif, (/puerr_max/), (/it/), (/1/) )   ! max U velocity difference, inner iterations
+         istatus = NF90_PUT_VAR( ncvgid, nvarid_vdif, (/pverr_max/), (/it/), (/1/) )   ! max V velocity difference, inner iterations
+         istatus = NF90_PUT_VAR( ncvgid, nvarid_veldif, (/zveldif/), (/it/), (/1/) )   ! max U or V velocity diff between subiterations
+         istatus = NF90_PUT_VAR( ncvgid, nvarid_mke, (/zmke/), (/it/), (/1/) )         ! mean kinetic energy
+
+         !
+         IF ( kiter == kitermax ) THEN
+            WRITE(numout,*) ' Should plot the spatially dependent residual '
+            istatus = NF90_PUT_VAR( ncvgid, nvarid_ures_xy, (/zu_res/) )          ! U-residual, spatially dependent
+            istatus = NF90_PUT_VAR( ncvgid, nvarid_vres_xy, (/zv_res/) )          ! V-residual, spatially dependent
+         ENDIF
+
+         ! close file
+         IF( kt == nitend )   istatus = NF90_CLOSE( ncvgid )
+      ENDIF
+      
+   END SUBROUTINE rhg_cvg_vp
+   
+
+   
+#else
+   !!----------------------------------------------------------------------
+   !!   Default option         Empty module           NO SI3 sea-ice model
+   !!----------------------------------------------------------------------
+#endif
+
+   !!==============================================================================
+END MODULE icedyn_rhg_vp
+
Index: /NEMO/branches/2020/tickets_icb_1900/src/ICE/iceistate.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/ICE/iceistate.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/ICE/iceistate.F90	(revision 14016)
@@ -426,5 +426,5 @@
       ! 4) Snow-ice mass (case ice is fully embedded)
       !----------------------------------------------
-      snwice_mass  (:,:) = tmask(:,:,1) * SUM( rhos * v_s(:,:,:) + rhoi * v_i(:,:,:), dim=3  )   ! snow+ice mass
+      snwice_mass  (:,:) = tmask(:,:,1) * SUM( rhos * v_s + rhoi * v_i + rhow * ( v_ip + v_il ), dim=3  )   ! snow+ice mass
       snwice_mass_b(:,:) = snwice_mass(:,:)
       !
Index: /NEMO/branches/2020/tickets_icb_1900/src/ICE/iceitd.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/ICE/iceitd.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/ICE/iceitd.F90	(revision 14016)
@@ -29,4 +29,5 @@
    USE lib_fortran    ! fortran utilities (glob_sum + no signed zero)
    USE prtctl         ! Print control
+   USE timing         ! Timing
 
    IMPLICIT NONE
@@ -87,4 +88,5 @@
       REAL(wp), DIMENSION(jpij,0:jpl) ::   zhbnew          ! new boundaries of ice categories
       !!------------------------------------------------------------------
+      IF( ln_timing )   CALL timing_start('iceitd_rem')
 
       IF( kt == nit000 .AND. lwp )   WRITE(numout,*) '-- ice_itd_rem: remapping ice thickness distribution' 
@@ -315,5 +317,5 @@
             IF ( a_i_1d(ji) > epsi10 .AND. h_i_1d(ji) < rn_himin ) THEN
                a_i_1d(ji) = a_i_1d(ji) * h_i_1d(ji) / rn_himin 
-               IF( ln_pnd_LEV )   a_ip_1d(ji) = a_ip_1d(ji) * h_i_1d(ji) / rn_himin
+               IF( ln_pnd_LEV .OR. ln_pnd_TOPO )   a_ip_1d(ji) = a_ip_1d(ji) * h_i_1d(ji) / rn_himin
                h_i_1d(ji) = rn_himin
             ENDIF
@@ -328,4 +330,5 @@
       IF( ln_icediachk )   CALL ice_cons_hsm(1, 'iceitd_rem', rdiag_v, rdiag_s, rdiag_t, rdiag_fv, rdiag_fs, rdiag_ft)
       IF( ln_icediachk )   CALL ice_cons2D  (1, 'iceitd_rem',  diag_v,  diag_s,  diag_t,  diag_fv,  diag_fs,  diag_ft)
+      IF( ln_timing    )   CALL timing_stop ('iceitd_rem')
       !
    END SUBROUTINE ice_itd_rem
@@ -486,15 +489,15 @@
                zaTsfn(ji,jl2)  = zaTsfn(ji,jl2) + ztrans
                !  
-               IF ( ln_pnd_LEV ) THEN
+               IF ( ln_pnd_LEV .OR. ln_pnd_TOPO ) THEN
                   ztrans          = a_ip_2d(ji,jl1) * zworka(ji)     ! Pond fraction
                   a_ip_2d(ji,jl1) = a_ip_2d(ji,jl1) - ztrans
                   a_ip_2d(ji,jl2) = a_ip_2d(ji,jl2) + ztrans
                   !                                              
-                  ztrans          = v_ip_2d(ji,jl1) * zworka(ji)     ! Pond volume (also proportional to da/a)
+                  ztrans          = v_ip_2d(ji,jl1) * zworkv(ji)     ! Pond volume
                   v_ip_2d(ji,jl1) = v_ip_2d(ji,jl1) - ztrans
                   v_ip_2d(ji,jl2) = v_ip_2d(ji,jl2) + ztrans
                   !
                   IF ( ln_pnd_lids ) THEN                            ! Pond lid volume
-                     ztrans          = v_il_2d(ji,jl1) * zworka(ji)
+                     ztrans          = v_il_2d(ji,jl1) * zworkv(ji)
                      v_il_2d(ji,jl1) = v_il_2d(ji,jl1) - ztrans
                      v_il_2d(ji,jl2) = v_il_2d(ji,jl2) + ztrans
@@ -606,4 +609,5 @@
       REAL(wp), DIMENSION(jpij,jpl-1) ::   zdaice, zdvice   ! ice area and volume transferred
       !!------------------------------------------------------------------
+      IF( ln_timing )   CALL timing_start('iceitd_reb')
       !
       IF( kt == nit000 .AND. lwp )   WRITE(numout,*) '-- ice_itd_reb: rebining ice thickness distribution' 
@@ -635,13 +639,10 @@
                jdonor(ji,jl)  = jl 
                ! how much of a_i you send in cat sup is somewhat arbitrary
-               !!clem: these do not work properly after a restart (I do not know why) => not sure it is still true
-               !!          zdaice(ji,jl)  = a_i_1d(ji) * ( h_i_1d(ji) - hi_max(jl) + epsi10 ) / h_i_1d(ji)  
-               !!          zdvice(ji,jl)  = v_i_1d(ji) - ( a_i_1d(ji) - zdaice(ji,jl) ) * ( hi_max(jl) - epsi10 )
-               !!clem: these do not work properly after a restart (I do not know why) => not sure it is still true
-               !!          zdaice(ji,jl)  = a_i_1d(ji)
-               !!          zdvice(ji,jl)  = v_i_1d(ji)
-               !!clem: these are from UCL and work ok
-               zdaice(ji,jl)  = a_i_1d(ji) * 0.5_wp
-               zdvice(ji,jl)  = v_i_1d(ji) - zdaice(ji,jl) * ( hi_max(jl) + hi_max(jl-1) ) * 0.5_wp
+               ! these are from CICE => transfer everything
+               !!zdaice(ji,jl)  = a_i_1d(ji)
+               !!zdvice(ji,jl)  = v_i_1d(ji)
+               ! these are from LLN => transfer only half of the category
+               zdaice(ji,jl)  =                       0.5_wp  * a_i_1d(ji)
+               zdvice(ji,jl)  = v_i_1d(ji) - (1._wp - 0.5_wp) * a_i_1d(ji) * hi_mean(jl)
             END DO
             !
@@ -686,4 +687,5 @@
       IF( ln_icediachk )   CALL ice_cons_hsm(1, 'iceitd_reb', rdiag_v, rdiag_s, rdiag_t, rdiag_fv, rdiag_fs, rdiag_ft)
       IF( ln_icediachk )   CALL ice_cons2D  (1, 'iceitd_reb',  diag_v,  diag_s,  diag_t,  diag_fv,  diag_fs,  diag_ft)
+      IF( ln_timing    )   CALL timing_stop ('iceitd_reb')
       !
    END SUBROUTINE ice_itd_reb
Index: /NEMO/branches/2020/tickets_icb_1900/src/ICE/icesbc.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/ICE/icesbc.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/ICE/icesbc.F90	(revision 14016)
@@ -62,5 +62,5 @@
       !!-------------------------------------------------------------------
       !
-      IF( ln_timing )   CALL timing_start('ice_sbc')
+      IF( ln_timing )   CALL timing_start('icesbc')
       !
       IF( kt == nit000 .AND. lwp ) THEN
@@ -89,5 +89,5 @@
       ENDIF
       !
-      IF( ln_timing )   CALL timing_stop('ice_sbc')
+      IF( ln_timing )   CALL timing_stop('icesbc')
       !
    END SUBROUTINE ice_sbc_tau
@@ -122,5 +122,5 @@
       !!--------------------------------------------------------------------
       !
-      IF( ln_timing )   CALL timing_start('ice_sbc_flx')
+      IF( ln_timing )   CALL timing_start('icesbc')
 
       IF( kt == nit000 .AND. lwp ) THEN
@@ -176,5 +176,5 @@
       ENDIF
       !
-      IF( ln_timing )   CALL timing_stop('ice_sbc_flx')
+      IF( ln_timing )   CALL timing_stop('icesbc')
       !
    END SUBROUTINE ice_sbc_flx
Index: /NEMO/branches/2020/tickets_icb_1900/src/ICE/icestp.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/ICE/icestp.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/ICE/icestp.F90	(revision 14016)
@@ -121,5 +121,5 @@
       !!----------------------------------------------------------------------
       !
-      IF( ln_timing )   CALL timing_start('ice_stp')
+      IF( ln_timing )   CALL timing_start('icestp')
       !
       !                                      !-----------------------!
@@ -215,5 +215,5 @@
 !!gm   remark, the ocean-ice stress is not saved in ice diag call above .....  find a solution!!!
       !
-      IF( ln_timing )   CALL timing_stop('ice_stp')
+      IF( ln_timing )   CALL timing_stop('icestp')
       !
    END SUBROUTINE ice_stp
@@ -373,4 +373,6 @@
       v_i_b (:,:,:)   = v_i (:,:,:)     ! ice volume
       v_s_b (:,:,:)   = v_s (:,:,:)     ! snow volume
+      v_ip_b(:,:,:)   = v_ip(:,:,:)     ! pond volume
+      v_il_b(:,:,:)   = v_il(:,:,:)     ! pond lid volume
       sv_i_b(:,:,:)   = sv_i(:,:,:)     ! salt content
       e_s_b (:,:,:,:) = e_s (:,:,:,:)   ! snow thermal energy
@@ -432,4 +434,5 @@
          diag_heat(ji,jj) = 0._wp ;   diag_sice(ji,jj) = 0._wp
          diag_vice(ji,jj) = 0._wp ;   diag_vsnw(ji,jj) = 0._wp
+         diag_aice(ji,jj) = 0._wp ;   diag_vpnd(ji,jj) = 0._wp
 
          tau_icebfr (ji,jj) = 0._wp   ! landfast ice param only (clem: important to keep the init here)
@@ -457,4 +460,7 @@
             qcn_ice    (ji,jj,jl) = 0._wp   ! conductive flux (ln_cndflx=T & ln_cndemule=T)
             qtr_ice_bot(ji,jj,jl) = 0._wp   ! part of solar radiation transmitted through the ice needed at least for outputs
+            ! Melt pond surface melt diagnostics (mv - more efficient: grouped into one water volume flux)
+            dh_i_sum_2d(ji,jj,jl) = 0._wp
+            dh_s_mlt_2d(ji,jj,jl) = 0._wp
          END_2D
       ENDDO
@@ -485,4 +491,6 @@
          diag_vsnw(:,:) = diag_vsnw(:,:) &
             &             + SUM(     v_s (:,:,:)          - v_s_b (:,:,:)                  , dim=3 ) * r1_Dt_ice * rhos
+         diag_vpnd(:,:) = diag_vpnd(:,:) &
+            &             + SUM(     v_ip + v_il          - v_ip_b - v_il_b                , dim=3 ) * r1_Dt_ice * rhow
          !
          IF( kn == 2 )    CALL iom_put ( 'hfxdhc' , diag_heat )   ! output of heat trend
Index: /NEMO/branches/2020/tickets_icb_1900/src/ICE/icethd.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/ICE/icethd.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/ICE/icethd.F90	(revision 14016)
@@ -166,4 +166,12 @@
          qsb_ice_bot(ji,jj) = rswitch * MIN( qsb_ice_bot(ji,jj), - zqfr_neg * r1_Dt_ice / MAX( at_i(ji,jj), epsi10 ) )
 
+         ! If conditions are always supercooled (such as at the mouth of ice-shelves), then ice grows continuously
+         ! ==> stop ice formation by artificially setting up the turbulent fluxes to 0 when volume > 20m (arbitrary)
+         IF( ( t_bo(ji,jj) - ( sst_m(ji,jj) + rt0 ) ) > 0._wp .AND. vt_i(ji,jj) >= 20._wp ) THEN
+            zqfr               = 0._wp
+            zqfr_pos           = 0._wp
+            qsb_ice_bot(ji,jj) = 0._wp
+         ENDIF
+         !
          ! --- Energy Budget of the leads (qlead, J.m-2) --- !
          !     qlead is the energy received from the atm. in the leads.
@@ -239,5 +247,4 @@
             IF( ln_icedH ) THEN                                     ! --- Growing/Melting --- !
                               CALL ice_thd_dh                           ! Ice-Snow thickness   
-                              CALL ice_thd_pnd                          ! Melt ponds formation
                               CALL ice_thd_ent( e_i_1d(1:npti,:) )      ! Ice enthalpy remapping
             ENDIF
@@ -260,4 +267,7 @@
       IF( ln_icediachk )   CALL ice_cons2D  (1, 'icethd',  diag_v,  diag_s,  diag_t,  diag_fv,  diag_fs,  diag_ft)
       !                   
+      IF ( ln_pnd .AND. ln_icedH ) &
+         &                    CALL ice_thd_pnd                      ! --- Melt ponds 
+      !
       IF( jpl > 1  )          CALL ice_itd_rem( kt )                ! --- Transport ice between thickness categories --- !
       !
@@ -266,5 +276,5 @@
                               CALL ice_cor( kt , 2 )                ! --- Corrections --- !
       !
-      oa_i(:,:,:) = oa_i(:,:,:) + a_i(:,:,:) * rdt_ice              ! ice natural aging incrementation     
+      oa_i(:,:,:) = oa_i(:,:,:) + a_i(:,:,:) * rDt_ice              ! ice natural aging incrementation     
       !
       ! convergence tests
@@ -377,7 +387,4 @@
             CALL tab_2d_1d( npti, nptidx(1:npti), sz_i_1d(1:npti,jk), sz_i(:,:,jk,kl)  )
          END DO
-         CALL tab_2d_1d( npti, nptidx(1:npti), a_ip_1d     (1:npti), a_ip     (:,:,kl) )
-         CALL tab_2d_1d( npti, nptidx(1:npti), h_ip_1d     (1:npti), h_ip     (:,:,kl) )
-         CALL tab_2d_1d( npti, nptidx(1:npti), h_il_1d     (1:npti), h_il     (:,:,kl) )
          !
          CALL tab_2d_1d( npti, nptidx(1:npti), qprec_ice_1d  (1:npti), qprec_ice            )
@@ -409,5 +416,4 @@
          CALL tab_2d_1d( npti, nptidx(1:npti), wfx_spr_1d (1:npti), wfx_spr          )
          CALL tab_2d_1d( npti, nptidx(1:npti), wfx_lam_1d (1:npti), wfx_lam          )
-         CALL tab_2d_1d( npti, nptidx(1:npti), wfx_pnd_1d (1:npti), wfx_pnd          )
          !
          CALL tab_2d_1d( npti, nptidx(1:npti), sfx_bog_1d (1:npti), sfx_bog          )
@@ -464,6 +470,4 @@
          v_s_1d (1:npti) = h_s_1d (1:npti) * a_i_1d (1:npti)
          sv_i_1d(1:npti) = s_i_1d (1:npti) * v_i_1d (1:npti)
-         v_ip_1d(1:npti) = h_ip_1d(1:npti) * a_ip_1d(1:npti)
-         v_il_1d(1:npti) = h_il_1d(1:npti) * a_ip_1d(1:npti)
          oa_i_1d(1:npti) = o_i_1d (1:npti) * a_i_1d (1:npti)
          
@@ -483,7 +487,4 @@
             CALL tab_1d_2d( npti, nptidx(1:npti), sz_i_1d(1:npti,jk), sz_i(:,:,jk,kl)  )
          END DO
-         CALL tab_1d_2d( npti, nptidx(1:npti), a_ip_1d     (1:npti), a_ip     (:,:,kl) )
-         CALL tab_1d_2d( npti, nptidx(1:npti), h_ip_1d     (1:npti), h_ip     (:,:,kl) )
-         CALL tab_1d_2d( npti, nptidx(1:npti), h_il_1d     (1:npti), h_il     (:,:,kl) )
          !
          CALL tab_1d_2d( npti, nptidx(1:npti), wfx_snw_sni_1d(1:npti), wfx_snw_sni )
@@ -501,5 +502,4 @@
          CALL tab_1d_2d( npti, nptidx(1:npti), wfx_spr_1d (1:npti), wfx_spr        )
          CALL tab_1d_2d( npti, nptidx(1:npti), wfx_lam_1d (1:npti), wfx_lam        )
-         CALL tab_1d_2d( npti, nptidx(1:npti), wfx_pnd_1d (1:npti), wfx_pnd        )
          !
          CALL tab_1d_2d( npti, nptidx(1:npti), sfx_bog_1d (1:npti), sfx_bog        )
@@ -529,4 +529,7 @@
          CALL tab_1d_2d( npti, nptidx(1:npti), cnd_ice_1d(1:npti), cnd_ice(:,:,kl) )
          CALL tab_1d_2d( npti, nptidx(1:npti), t1_ice_1d (1:npti), t1_ice (:,:,kl) )
+         ! Melt ponds
+         CALL tab_1d_2d( npti, nptidx(1:npti), dh_i_sum  (1:npti) , dh_i_sum_2d(:,:,kl) )
+         CALL tab_1d_2d( npti, nptidx(1:npti), dh_s_mlt  (1:npti) , dh_s_mlt_2d(:,:,kl) )
          ! SIMIP diagnostics         
          CALL tab_1d_2d( npti, nptidx(1:npti), t_si_1d       (1:npti), t_si       (:,:,kl) )
@@ -537,6 +540,4 @@
          CALL tab_1d_2d( npti, nptidx(1:npti), v_s_1d (1:npti), v_s (:,:,kl) )
          CALL tab_1d_2d( npti, nptidx(1:npti), sv_i_1d(1:npti), sv_i(:,:,kl) )
-         CALL tab_1d_2d( npti, nptidx(1:npti), v_ip_1d(1:npti), v_ip(:,:,kl) )
-         CALL tab_1d_2d( npti, nptidx(1:npti), v_il_1d(1:npti), v_il(:,:,kl) )
          CALL tab_1d_2d( npti, nptidx(1:npti), oa_i_1d(1:npti), oa_i(:,:,kl) )
          ! check convergence of heat diffusion scheme
Index: /NEMO/branches/2020/tickets_icb_1900/src/ICE/icethd_dh.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/ICE/icethd_dh.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/ICE/icethd_dh.F90	(revision 14016)
@@ -55,4 +55,7 @@
       !!                - Snow ice formation
       !!
+      !! ** Note     :  h=max(0,h+dh) are often used to ensure positivity of h.
+      !!                very small negative values can occur otherwise (e.g. -1.e-20)
+      !!
       !! References : Bitz and Lipscomb, 1999, J. Geophys. Res.
       !!              Fichefet T. and M. Maqueda 1997, J. Geophys. Res., 102(C6), 12609-12646   
@@ -79,5 +82,4 @@
       REAL(wp) ::   zfmdt        ! exchange mass flux x time step (J/m2), >0 towards the ocean
 
-      REAL(wp), DIMENSION(jpij) ::   zqprec      ! energy of fallen snow                       (J.m-3)
       REAL(wp), DIMENSION(jpij) ::   zq_top      ! heat for surface ablation                   (J.m-2)
       REAL(wp), DIMENSION(jpij) ::   zq_bot      ! heat for bottom ablation                    (J.m-2)
@@ -85,15 +87,11 @@
       REAL(wp), DIMENSION(jpij) ::   zf_tt       ! Heat budget to determine melting or freezing(W.m-2)
       REAL(wp), DIMENSION(jpij) ::   zevap_rema  ! remaining mass flux from sublimation        (kg.m-2)
-
-      REAL(wp), DIMENSION(jpij) ::   zdh_s_mel   ! snow melt 
-      REAL(wp), DIMENSION(jpij) ::   zdh_s_pre   ! snow precipitation 
-      REAL(wp), DIMENSION(jpij) ::   zdh_s_sub   ! snow sublimation
-
-      REAL(wp), DIMENSION(jpij,nlay_s) ::   zh_s      ! snw layer thickness
-      REAL(wp), DIMENSION(jpij,nlay_i) ::   zh_i      ! ice layer thickness
-      REAL(wp), DIMENSION(jpij,nlay_i) ::   zdeltah
-      INTEGER , DIMENSION(jpij,nlay_i) ::   icount    ! number of layers vanished by melting 
-
+      REAL(wp), DIMENSION(jpij) ::   zdeltah     
       REAL(wp), DIMENSION(jpij) ::   zsnw        ! distribution of snow after wind blowing
+
+      INTEGER , DIMENSION(jpij,nlay_i)     ::   icount    ! number of layers vanishing by melting 
+      REAL(wp), DIMENSION(jpij,0:nlay_i+1) ::   zh_i      ! ice layer thickness (m)
+      REAL(wp), DIMENSION(jpij,0:nlay_s  ) ::   zh_s      ! snw layer thickness (m)
+      REAL(wp), DIMENSION(jpij,0:nlay_s  ) ::   ze_s      ! snw layer enthalpy (J.m-3)
 
       REAL(wp) ::   zswitch_sal
@@ -108,11 +106,23 @@
       END SELECT
 
-      ! initialize layer thicknesses and enthalpies
+      ! initialize ice layer thicknesses and enthalpies
+      eh_i_old(1:npti,0:nlay_i+1) = 0._wp
       h_i_old (1:npti,0:nlay_i+1) = 0._wp
-      eh_i_old(1:npti,0:nlay_i+1) = 0._wp
+      zh_i    (1:npti,0:nlay_i+1) = 0._wp
       DO jk = 1, nlay_i
          DO ji = 1, npti
+            eh_i_old(ji,jk) = h_i_1d(ji) * r1_nlay_i * e_i_1d(ji,jk)
             h_i_old (ji,jk) = h_i_1d(ji) * r1_nlay_i
-            eh_i_old(ji,jk) = e_i_1d(ji,jk) * h_i_old(ji,jk)
+            zh_i    (ji,jk) = h_i_1d(ji) * r1_nlay_i
+         END DO
+      END DO
+      !
+      ! initialize snw layer thicknesses and enthalpies
+      zh_s(1:npti,0) = 0._wp
+      ze_s(1:npti,0) = 0._wp
+      DO jk = 1, nlay_s
+         DO ji = 1, npti
+            zh_s(ji,jk) = h_s_1d(ji) * r1_nlay_s
+            ze_s(ji,jk) = e_s_1d(ji,jk)
          END DO
       END DO
@@ -141,18 +151,4 @@
          zf_tt(ji)         = qcn_ice_bot_1d(ji) + qsb_ice_bot_1d(ji) + fhld_1d(ji) + qtr_ice_bot_1d(ji) * frq_m_1d(ji) 
          zq_bot(ji)        = MAX( 0._wp, zf_tt(ji) * rDt_ice )
-      END DO
-
-      ! Ice and snow layer thicknesses
-      !-------------------------------
-      DO jk = 1, nlay_i
-         DO ji = 1, npti
-            zh_i(ji,jk) = h_i_1d(ji) * r1_nlay_i
-         END DO
-      END DO
-
-      DO jk = 1, nlay_s
-         DO ji = 1, npti
-            zh_s(ji,jk) = h_s_1d(ji) * r1_nlay_s
-         END DO
       END DO
 
@@ -167,12 +163,11 @@
          DO ji = 1, npti
             IF( t_s_1d(ji,jk) > rt0 ) THEN
-               hfx_res_1d    (ji) = hfx_res_1d    (ji) + e_s_1d(ji,jk) * zh_s(ji,jk) * a_i_1d(ji) * r1_Dt_ice   ! heat flux to the ocean [W.m-2], < 0
-               wfx_snw_sum_1d(ji) = wfx_snw_sum_1d(ji) + rhos          * zh_s(ji,jk) * a_i_1d(ji) * r1_Dt_ice   ! mass flux
+               hfx_res_1d    (ji) = hfx_res_1d    (ji) - ze_s(ji,jk) * zh_s(ji,jk) * a_i_1d(ji) * r1_Dt_ice   ! heat flux to the ocean [W.m-2], < 0
+               wfx_snw_sum_1d(ji) = wfx_snw_sum_1d(ji) + rhos        * zh_s(ji,jk) * a_i_1d(ji) * r1_Dt_ice   ! mass flux
                ! updates
-               dh_s_mlt(ji)    = dh_s_mlt(ji) - zh_s(ji,jk)
-               h_s_1d  (ji)    = h_s_1d(ji) - zh_s(ji,jk)
+               dh_s_mlt(ji)    =             dh_s_mlt(ji) - zh_s(ji,jk)
+               h_s_1d  (ji)    = MAX( 0._wp, h_s_1d  (ji) - zh_s(ji,jk) )
                zh_s    (ji,jk) = 0._wp
-               e_s_1d  (ji,jk) = 0._wp
-               t_s_1d  (ji,jk) = rt0
+               ze_s    (ji,jk) = 0._wp
             END IF
          END DO
@@ -181,69 +176,40 @@
       ! Snow precipitation
       !-------------------
-      CALL ice_var_snwblow( 1.0_wp - at_i_1d(1:npti), zsnw(1:npti) )   ! snow distribution over ice after wind blowing
-
-      zdeltah(1:npti,:) = 0._wp
+      CALL ice_var_snwblow( 1._wp - at_i_1d(1:npti), zsnw(1:npti) )   ! snow distribution over ice after wind blowing
+
       DO ji = 1, npti
          IF( sprecip_1d(ji) > 0._wp ) THEN
+            zh_s(ji,0) = zsnw(ji) * sprecip_1d(ji) * rDt_ice * r1_rhos / at_i_1d(ji)   ! thickness of precip
+            ze_s(ji,0) = MAX( 0._wp, - qprec_ice_1d(ji) )                              ! enthalpy of the precip (>0, J.m-3)
             !
-            ! --- precipitation ---
-            zdh_s_pre (ji) = zsnw(ji) * sprecip_1d(ji) * rDt_ice * r1_rhos / at_i_1d(ji)   ! thickness change
-            zqprec    (ji) = - qprec_ice_1d(ji)                                             ! enthalpy of the precip (>0, J.m-3)
+            hfx_spr_1d(ji) = hfx_spr_1d(ji) + ze_s(ji,0) * zh_s(ji,0) * a_i_1d(ji) * r1_Dt_ice   ! heat flux from snow precip (>0, W.m-2)
+            wfx_spr_1d(ji) = wfx_spr_1d(ji) - rhos       * zh_s(ji,0) * a_i_1d(ji) * r1_Dt_ice   ! mass flux, <0
             !
-            hfx_spr_1d(ji) = hfx_spr_1d(ji) + zdh_s_pre(ji) * a_i_1d(ji) * zqprec(ji)    * r1_Dt_ice   ! heat flux from snow precip (>0, W.m-2)
-            wfx_spr_1d(ji) = wfx_spr_1d(ji) - rhos          * a_i_1d(ji) * zdh_s_pre(ji) * r1_Dt_ice   ! mass flux, <0
-            
-            ! --- melt of falling snow ---
-            rswitch              = MAX( 0._wp , SIGN( 1._wp , zqprec(ji) - epsi20 ) )
-            zdeltah       (ji,1) = - rswitch * zq_top(ji) / MAX( zqprec(ji) , epsi20 )   ! thickness change
-            zdeltah       (ji,1) = MAX( - zdh_s_pre(ji), zdeltah(ji,1) )                 ! bound melting 
-            hfx_snw_1d    (ji)   = hfx_snw_1d    (ji) - zdeltah(ji,1) * a_i_1d(ji) * zqprec(ji)    * r1_Dt_ice   ! heat used to melt snow (W.m-2, >0)
-            wfx_snw_sum_1d(ji)   = wfx_snw_sum_1d(ji) - rhos          * a_i_1d(ji) * zdeltah(ji,1) * r1_Dt_ice   ! snow melting only = water into the ocean (then without snow precip), >0
-            
-            ! updates available heat + precipitations after melting
-            dh_s_mlt (ji) = dh_s_mlt(ji) + zdeltah(ji,1)
-            zq_top   (ji) = MAX( 0._wp , zq_top (ji) + zdeltah(ji,1) * zqprec(ji) )      
-            zdh_s_pre(ji) = zdh_s_pre(ji) + zdeltah(ji,1)
-            
             ! update thickness
-            h_s_1d(ji) = MAX( 0._wp , h_s_1d(ji) + zdh_s_pre(ji) )
-            !
-         ELSE
-            !
-            zdh_s_pre(ji) = 0._wp
-            zqprec   (ji) = 0._wp
-            !
+            h_s_1d(ji) = h_s_1d(ji) + zh_s(ji,0)
          ENDIF
-      END DO
-
-      ! recalculate snow layers
-      DO jk = 1, nlay_s
-         DO ji = 1, npti
-            zh_s(ji,jk) = h_s_1d(ji) * r1_nlay_s
-         END DO
       END DO
 
       ! Snow melting
       ! ------------
-      ! If heat still available (zq_top > 0), then melt more snow
-      zdeltah(1:npti,:) = 0._wp
-      zdh_s_mel(1:npti) = 0._wp
-      DO jk = 1, nlay_s
+      ! If heat still available (zq_top > 0)
+      ! then all snw precip has been melted and we need to melt more snow
+      DO jk = 0, nlay_s
          DO ji = 1, npti
             IF( zh_s(ji,jk) > 0._wp .AND. zq_top(ji) > 0._wp ) THEN
                !
-               rswitch          = MAX( 0._wp, SIGN( 1._wp, e_s_1d(ji,jk) - epsi20 ) )
-               zdeltah  (ji,jk) = - rswitch * zq_top(ji) / MAX( e_s_1d(ji,jk), epsi20 )   ! thickness change
-               zdeltah  (ji,jk) = MAX( zdeltah(ji,jk) , - zh_s(ji,jk) )                   ! bound melting
-               zdh_s_mel(ji)    = zdh_s_mel(ji) + zdeltah(ji,jk)
-               
-               hfx_snw_1d(ji)     = hfx_snw_1d(ji)     - zdeltah(ji,jk) * a_i_1d(ji) * e_s_1d (ji,jk) * r1_Dt_ice   ! heat used to melt snow(W.m-2, >0)
-               wfx_snw_sum_1d(ji) = wfx_snw_sum_1d(ji) - rhos           * a_i_1d(ji) * zdeltah(ji,jk) * r1_Dt_ice   ! snow melting only = water into the ocean (then without snow precip)
+               rswitch = MAX( 0._wp , SIGN( 1._wp , ze_s(ji,jk) - epsi20 ) )
+               zdum    = - rswitch * zq_top(ji) / MAX( ze_s(ji,jk), epsi20 )   ! thickness change
+               zdum    = MAX( zdum , - zh_s(ji,jk) )                           ! bound melting
+               
+               hfx_snw_1d    (ji) = hfx_snw_1d    (ji) - ze_s(ji,jk) * zdum * a_i_1d(ji) * r1_Dt_ice   ! heat used to melt snow(W.m-2, >0)
+               wfx_snw_sum_1d(ji) = wfx_snw_sum_1d(ji) - rhos        * zdum * a_i_1d(ji) * r1_Dt_ice   ! snow melting only = water into the ocean
                
                ! updates available heat + thickness
-               dh_s_mlt(ji)    = dh_s_mlt(ji) + zdeltah(ji,jk)
-               zq_top  (ji)    = MAX( 0._wp , zq_top(ji) + zdeltah(ji,jk) * e_s_1d(ji,jk) )
-               h_s_1d  (ji)    = MAX( 0._wp , h_s_1d(ji) + zdeltah(ji,jk) )
-               zh_s    (ji,jk) = MAX( 0._wp , zh_s(ji,jk) + zdeltah(ji,jk) )
+               dh_s_mlt(ji)    =              dh_s_mlt(ji)    + zdum
+               zq_top  (ji)    = MAX( 0._wp , zq_top  (ji)    + zdum * ze_s(ji,jk) )
+               h_s_1d  (ji)    = MAX( 0._wp , h_s_1d  (ji)    + zdum )
+               zh_s    (ji,jk) = MAX( 0._wp , zh_s    (ji,jk) + zdum )
+!!$               IF( zh_s(ji,jk) == 0._wp )   ze_s(ji,jk) = 0._wp
                !
             ENDIF
@@ -255,48 +221,31 @@
       ! qla_ice is always >=0 (upwards), heat goes to the atmosphere, therefore snow sublimates
       !    comment: not counted in mass/heat exchange in iceupdate.F90 since this is an exchange with atm. (not ocean)
-      zdeltah(1:npti,:) = 0._wp
+      zdeltah   (1:npti) = 0._wp ! total snow thickness that sublimates, < 0
+      zevap_rema(1:npti) = 0._wp
       DO ji = 1, npti
          IF( evap_ice_1d(ji) > 0._wp ) THEN
+            zdeltah   (ji) = MAX( - evap_ice_1d(ji) * r1_rhos * rDt_ice, - h_s_1d(ji) )   ! amount of snw that sublimates, < 0            
+            zevap_rema(ji) = MAX( 0._wp, evap_ice_1d(ji) * rDt_ice + zdeltah(ji) * rhos ) ! remaining evap in kg.m-2 (used for ice sublimation later on)
+         ENDIF
+      END DO
+      
+      DO jk = 0, nlay_s
+         DO ji = 1, npti
+            zdum = MAX( -zh_s(ji,jk), zdeltah(ji) ) ! snow layer thickness that sublimates, < 0
             !
-            zdh_s_sub (ji)   = MAX( - h_s_1d(ji) , - evap_ice_1d(ji) * r1_rhos * rDt_ice )
-            zevap_rema(ji)   = evap_ice_1d(ji) * rDt_ice + zdh_s_sub(ji) * rhos   ! remaining evap in kg.m-2 (used for ice melting later on)
-            zdeltah   (ji,1) = MAX( zdh_s_sub(ji), - zdh_s_pre(ji) )
-            
-            hfx_sub_1d    (ji) = hfx_sub_1d(ji) + &   ! Heat flux by sublimation [W.m-2], < 0 (sublimate snow that had fallen, then pre-existing snow)
-               &                 ( zdeltah(ji,1) * zqprec(ji) + ( zdh_s_sub(ji) - zdeltah(ji,1) ) * e_s_1d(ji,1) )  &
-               &                 * a_i_1d(ji) * r1_Dt_ice
-            wfx_snw_sub_1d(ji) = wfx_snw_sub_1d(ji) - rhos * a_i_1d(ji) * zdh_s_sub(ji) * r1_Dt_ice   ! Mass flux by sublimation
-            
-            ! new snow thickness
-            h_s_1d(ji)    =  MAX( 0._wp , h_s_1d(ji) + zdh_s_sub(ji) )
-            ! update precipitations after sublimation and correct sublimation
-            zdh_s_pre(ji) = zdh_s_pre(ji) + zdeltah(ji,1)
-            zdh_s_sub(ji) = zdh_s_sub(ji) - zdeltah(ji,1)
-            !
-         ELSE
-            !
-            zdh_s_sub (ji) = 0._wp
-            zevap_rema(ji) = 0._wp
-            !
-         ENDIF
-      END DO
-      
-      ! --- Update snow diags --- !
-      DO ji = 1, npti
-         dh_s_tot(ji) = zdh_s_mel(ji) + zdh_s_pre(ji) + zdh_s_sub(ji)
-      END DO
-
-      ! Update temperature, energy
-      !---------------------------
-      ! new temp and enthalpy of the snow (remaining snow precip + remaining pre-existing snow)
-      DO jk = 1, nlay_s
-         DO ji = 1,npti
-            rswitch       = MAX( 0._wp , SIGN( 1._wp, h_s_1d(ji) - epsi20 ) )
-            e_s_1d(ji,jk) = rswitch / MAX( h_s_1d(ji), epsi20 ) *            &
-              &             ( ( zdh_s_pre(ji)              ) * zqprec(ji) +  &
-              &               ( h_s_1d(ji) - zdh_s_pre(ji) ) * rhos * ( rcpi * ( rt0 - t_s_1d(ji,jk) ) + rLfus ) )
-         END DO
-      END DO
-      
+            hfx_sub_1d    (ji) = hfx_sub_1d    (ji) + ze_s(ji,jk) * zdum * a_i_1d(ji) * r1_Dt_ice  ! Heat flux of snw that sublimates [W.m-2], < 0
+            wfx_snw_sub_1d(ji) = wfx_snw_sub_1d(ji) - rhos        * zdum * a_i_1d(ji) * r1_Dt_ice  ! Mass flux by sublimation
+
+            ! update thickness
+            h_s_1d(ji)    = MAX( 0._wp , h_s_1d(ji)    + zdum )
+            zh_s  (ji,jk) = MAX( 0._wp , zh_s  (ji,jk) + zdum )
+!!$            IF( zh_s(ji,jk) == 0._wp )   ze_s(ji,jk) = 0._wp
+
+            ! update sublimation left
+            zdeltah(ji) = MIN( zdeltah(ji) - zdum, 0._wp )
+         END DO
+      END DO
+
+      !      
       !                       ! ============ !
       !                       !     Ice      !
@@ -305,5 +254,4 @@
       ! Surface ice melting 
       !--------------------
-      zdeltah(1:npti,:) = 0._wp ! important
       DO jk = 1, nlay_i
          DO ji = 1, npti
@@ -313,20 +261,17 @@
 
                zEi            = - e_i_1d(ji,jk) * r1_rhoi             ! Specific enthalpy of layer k [J/kg, <0]       
-               zdE            = 0._wp                                 ! Specific enthalpy difference (J/kg, <0)
-                                                                      ! set up at 0 since no energy is needed to melt water...(it is already melted)
-               zdeltah(ji,jk) = MIN( 0._wp , - zh_i(ji,jk) )          ! internal melting occurs when the internal temperature is above freezing     
-                                                                      ! this should normally not happen, but sometimes, heat diffusion leads to this
-               zfmdt          = - zdeltah(ji,jk) * rhoi               ! Mass flux x time step > 0
-                         
-               dh_i_itm(ji)   = dh_i_itm(ji) + zdeltah(ji,jk)         ! Cumulate internal melting
-               
-               zfmdt          = - rhoi * zdeltah(ji,jk)               ! Recompute mass flux [kg/m2, >0]
-
-               hfx_res_1d(ji) = hfx_res_1d(ji) + zfmdt * a_i_1d(ji) * zEi * r1_Dt_ice                           ! Heat flux to the ocean [W.m-2], <0
-               !                                                                                                  ice enthalpy zEi is "sent" to the ocean
-               sfx_res_1d(ji) = sfx_res_1d(ji) - rhoi * a_i_1d(ji) * zdeltah(ji,jk) * s_i_1d(ji) * r1_Dt_ice    ! Salt flux
-               !                                                                                                  using s_i_1d and not sz_i_1d(jk) is ok
-               wfx_res_1d(ji) = wfx_res_1d(ji) - rhoi * a_i_1d(ji) * zdeltah(ji,jk) * r1_Dt_ice                 ! Mass flux
-
+               zdE            =   0._wp                               ! Specific enthalpy difference (J/kg, <0)
+               !                                                          set up at 0 since no energy is needed to melt water...(it is already melted)
+               zdum           = MIN( 0._wp , - zh_i(ji,jk) )          ! internal melting occurs when the internal temperature is above freezing     
+               !                                                          this should normally not happen, but sometimes, heat diffusion leads to this
+               zfmdt          = - zdum * rhoi                         ! Recompute mass flux [kg/m2, >0]
+               !
+               dh_i_itm(ji)   = dh_i_itm(ji) + zdum                   ! Cumulate internal melting
+               !
+               hfx_res_1d(ji) = hfx_res_1d(ji) + zEi  * zfmdt             * a_i_1d(ji) * r1_Dt_ice    ! Heat flux to the ocean [W.m-2], <0
+               !                                                                                          ice enthalpy zEi is "sent" to the ocean
+               wfx_res_1d(ji) = wfx_res_1d(ji) - rhoi * zdum              * a_i_1d(ji) * r1_Dt_ice    ! Mass flux
+               sfx_res_1d(ji) = sfx_res_1d(ji) - rhoi * zdum * s_i_1d(ji) * a_i_1d(ji) * r1_Dt_ice    ! Salt flux
+               !                                                                                          using s_i_1d and not sz_i_1d(jk) is ok
             ELSE                                        !-- Surface melting
                
@@ -337,60 +282,60 @@
                zfmdt          = - zq_top(ji) / zdE                    ! Mass flux to the ocean [kg/m2, >0]
                
-               zdeltah(ji,jk) = - zfmdt * r1_rhoi                     ! Melt of layer jk [m, <0]
-               
-               zdeltah(ji,jk) = MIN( 0._wp , MAX( zdeltah(ji,jk) , - zh_i(ji,jk) ) )    ! Melt of layer jk cannot exceed the layer thickness [m, <0]
-               
-               zq_top(ji)      = MAX( 0._wp , zq_top(ji) - zdeltah(ji,jk) * rhoi * zdE ) ! update available heat
-               
-               dh_i_sum(ji)   = dh_i_sum(ji) + zdeltah(ji,jk)         ! Cumulate surface melt
-               
-               zfmdt          = - rhoi * zdeltah(ji,jk)               ! Recompute mass flux [kg/m2, >0]
+               zdum           = - zfmdt * r1_rhoi                     ! Melt of layer jk [m, <0]
+               
+               zdum           = MIN( 0._wp , MAX( zdum , - zh_i(ji,jk) ) )    ! Melt of layer jk cannot exceed the layer thickness [m, <0]
+
+               zq_top(ji)     = MAX( 0._wp , zq_top(ji) - zdum * rhoi * zdE ) ! update available heat
+               
+               dh_i_sum(ji)   = dh_i_sum(ji) + zdum                   ! Cumulate surface melt
+               
+               zfmdt          = - rhoi * zdum                         ! Recompute mass flux [kg/m2, >0]
                
                zQm            = zfmdt * zEw                           ! Energy of the melt water sent to the ocean [J/m2, <0]
                
-               sfx_sum_1d(ji) = sfx_sum_1d(ji) - rhoi * a_i_1d(ji) * zdeltah(ji,jk) * s_i_1d(ji) * r1_Dt_ice    ! Salt flux >0
-               !                                                                                                  using s_i_1d and not sz_i_1d(jk) is ok)
-               hfx_thd_1d(ji) = hfx_thd_1d(ji) + zfmdt * a_i_1d(ji) * zEw * r1_Dt_ice                           ! Heat flux [W.m-2], < 0
-               hfx_sum_1d(ji) = hfx_sum_1d(ji) - zfmdt * a_i_1d(ji) * zdE * r1_Dt_ice                           ! Heat flux used in this process [W.m-2], > 0  
-               ! 
-               wfx_sum_1d(ji) = wfx_sum_1d(ji) - rhoi * a_i_1d(ji) * zdeltah(ji,jk) * r1_Dt_ice                 ! Mass flux
-               
+               hfx_thd_1d(ji) = hfx_thd_1d(ji) + zEw  * zfmdt             * a_i_1d(ji) * r1_Dt_ice    ! Heat flux [W.m-2], < 0
+               hfx_sum_1d(ji) = hfx_sum_1d(ji) - zdE  * zfmdt             * a_i_1d(ji) * r1_Dt_ice    ! Heat flux used in this process [W.m-2], > 0  
+               wfx_sum_1d(ji) = wfx_sum_1d(ji) - rhoi * zdum              * a_i_1d(ji) * r1_Dt_ice    ! Mass flux
+               sfx_sum_1d(ji) = sfx_sum_1d(ji) - rhoi * zdum * s_i_1d(ji) * a_i_1d(ji) * r1_Dt_ice    ! Salt flux >0
+               !                                                                                          using s_i_1d and not sz_i_1d(jk) is ok) 
             END IF
-            
+            ! update thickness
+            zh_i(ji,jk) = MAX( 0._wp, zh_i(ji,jk) + zdum )
+            h_i_1d(ji)  = MAX( 0._wp, h_i_1d(ji)  + zdum )
+            !
+            ! update heat content (J.m-2) and layer thickness
+            eh_i_old(ji,jk) = eh_i_old(ji,jk) + zdum * e_i_1d(ji,jk)
+            h_i_old (ji,jk) = h_i_old (ji,jk) + zdum
+            !
+            !
             ! Ice sublimation
             ! ---------------
-            zdum            = MAX( - ( zh_i(ji,jk) + zdeltah(ji,jk) ) , - zevap_rema(ji) * r1_rhoi )
-            zdeltah (ji,jk) = zdeltah (ji,jk) + zdum
-            dh_i_sub(ji)    = dh_i_sub(ji)    + zdum
-            
-            sfx_sub_1d(ji)     = sfx_sub_1d(ji) - rhoi * a_i_1d(ji) * zdum * s_i_1d(ji) * r1_Dt_ice  ! Salt flux >0
-            !                                                                                          clem: flux is sent to the ocean for simplicity
-            !                                                                                                but salt should remain in the ice except
-            !                                                                                                if all ice is melted. => must be corrected
-            hfx_sub_1d(ji)     = hfx_sub_1d(ji) + zdum * e_i_1d(ji,jk) * a_i_1d(ji) * r1_Dt_ice      ! Heat flux [W.m-2], < 0
-
-            wfx_ice_sub_1d(ji) = wfx_ice_sub_1d(ji) - rhoi * a_i_1d(ji) * zdum * r1_Dt_ice           ! Mass flux > 0
-
-            ! update remaining mass flux
-            zevap_rema(ji)  = zevap_rema(ji) + zdum * rhoi
-            
+            zdum               = MAX( - zh_i(ji,jk) , - zevap_rema(ji) * r1_rhoi )
+            !
+            hfx_sub_1d(ji)     = hfx_sub_1d(ji)     + e_i_1d(ji,jk) * zdum              * a_i_1d(ji) * r1_Dt_ice ! Heat flux [W.m-2], < 0
+            wfx_ice_sub_1d(ji) = wfx_ice_sub_1d(ji) - rhoi          * zdum              * a_i_1d(ji) * r1_Dt_ice ! Mass flux > 0
+            sfx_sub_1d(ji)     = sfx_sub_1d(ji)     - rhoi          * zdum * s_i_1d(ji) * a_i_1d(ji) * r1_Dt_ice ! Salt flux >0
+            !                                                                                                      clem: flux is sent to the ocean for simplicity
+            !                                                                                                            but salt should remain in the ice except
+            !                                                                                                            if all ice is melted. => must be corrected
+            ! update remaining mass flux and thickness
+            zevap_rema(ji) = zevap_rema(ji) + zdum * rhoi            
+            zh_i(ji,jk)    = MAX( 0._wp, zh_i(ji,jk) + zdum )
+            h_i_1d(ji)     = MAX( 0._wp, h_i_1d(ji)  + zdum )
+            dh_i_sub(ji)   = dh_i_sub(ji) + zdum
+
+            ! update heat content (J.m-2) and layer thickness
+            eh_i_old(ji,jk) = eh_i_old(ji,jk) + zdum * e_i_1d(ji,jk)
+            h_i_old (ji,jk) = h_i_old (ji,jk) + zdum
+
             ! record which layers have disappeared (for bottom melting) 
             !    => icount=0 : no layer has vanished
             !    => icount=5 : 5 layers have vanished
-            rswitch       = MAX( 0._wp , SIGN( 1._wp , - ( zh_i(ji,jk) + zdeltah(ji,jk) ) ) ) 
+            rswitch       = MAX( 0._wp , SIGN( 1._wp , - zh_i(ji,jk) ) ) 
             icount(ji,jk) = NINT( rswitch )
-            zh_i(ji,jk)   = MAX( 0._wp , zh_i(ji,jk) + zdeltah(ji,jk) )
                         
-            ! update heat content (J.m-2) and layer thickness
-            eh_i_old(ji,jk) = eh_i_old(ji,jk) + zdeltah(ji,jk) * e_i_1d(ji,jk)
-            h_i_old (ji,jk) = h_i_old (ji,jk) + zdeltah(ji,jk)
-         END DO
-      END DO
-      
-      ! update ice thickness
-      DO ji = 1, npti
-         h_i_1d(ji) =  MAX( 0._wp , h_i_1d(ji) + dh_i_sum(ji) + dh_i_itm(ji) + dh_i_sub(ji) )
-      END DO
-
+         END DO
+      END DO
+      
       ! remaining "potential" evap is sent to ocean
       DO ji = 1, npti
@@ -430,29 +375,31 @@
                   &          + zswi2  * 0.26 / ( 0.26 + 0.74 * EXP ( - 724300.0 * zgrr ) )  , 0.5 )
 
-               s_i_new(ji)   = zswitch_sal * zfracs * sss_1d(ji) + ( 1. - zswitch_sal ) * s_i_1d(ji)  ! New ice salinity
-
-               ztmelts       = - rTmlt * s_i_new(ji)                                                  ! New ice melting point (C)
-
-               zt_i_new      = zswitch_sal * t_bo_1d(ji) + ( 1. - zswitch_sal) * t_i_1d(ji, nlay_i)
-               
-               zEi           = rcpi * ( zt_i_new - (ztmelts+rt0) ) &                                  ! Specific enthalpy of forming ice (J/kg, <0)
-                  &            - rLfus * ( 1.0 - ztmelts / ( MIN( zt_i_new - rt0, -epsi10 ) ) ) + rcp  * ztmelts
-
-               zEw           = rcp  * ( t_bo_1d(ji) - rt0 )                                           ! Specific enthalpy of seawater (J/kg, < 0)
-
-               zdE           = zEi - zEw                                                              ! Specific enthalpy difference (J/kg, <0)
-
-               dh_i_bog(ji)  = rDt_ice * MAX( 0._wp , zf_tt(ji) / ( zdE * rhoi ) )
+               s_i_new(ji)    = zswitch_sal * zfracs * sss_1d(ji) + ( 1. - zswitch_sal ) * s_i_1d(ji)  ! New ice salinity
+
+               ztmelts        = - rTmlt * s_i_new(ji)                                                  ! New ice melting point (C)
+
+               zt_i_new       = zswitch_sal * t_bo_1d(ji) + ( 1. - zswitch_sal) * t_i_1d(ji, nlay_i)
+               
+               zEi            = rcpi * ( zt_i_new - (ztmelts+rt0) ) &                                  ! Specific enthalpy of forming ice (J/kg, <0)
+                  &             - rLfus * ( 1.0 - ztmelts / ( MIN( zt_i_new - rt0, -epsi10 ) ) ) + rcp * ztmelts
+
+               zEw            = rcp  * ( t_bo_1d(ji) - rt0 )                                           ! Specific enthalpy of seawater (J/kg, < 0)
+
+               zdE            = zEi - zEw                                                              ! Specific enthalpy difference (J/kg, <0)
+
+               dh_i_bog(ji)   = rDt_ice * MAX( 0._wp , zf_tt(ji) / ( zdE * rhoi ) )
                
             END DO
             ! Contribution to Energy and Salt Fluxes                                    
-            zfmdt          = - rhoi * dh_i_bog(ji)                                                   ! Mass flux x time step (kg/m2, < 0)
+            zfmdt = - rhoi * dh_i_bog(ji)                                                              ! Mass flux x time step (kg/m2, < 0)
             
-            hfx_thd_1d(ji) = hfx_thd_1d(ji) + zfmdt * a_i_1d(ji) * zEw * r1_Dt_ice                           ! Heat flux to the ocean [W.m-2], >0
-            hfx_bog_1d(ji) = hfx_bog_1d(ji) - zfmdt * a_i_1d(ji) * zdE * r1_Dt_ice                           ! Heat flux used in this process [W.m-2], <0
-            
-            sfx_bog_1d(ji) = sfx_bog_1d(ji) - rhoi * a_i_1d(ji) * dh_i_bog(ji) * s_i_new(ji) * r1_Dt_ice     ! Salt flux, <0
-
-            wfx_bog_1d(ji) = wfx_bog_1d(ji) - rhoi * a_i_1d(ji) * dh_i_bog(ji) * r1_Dt_ice                   ! Mass flux, <0
+            hfx_thd_1d(ji) = hfx_thd_1d(ji) + zEw  * zfmdt                      * a_i_1d(ji) * r1_Dt_ice   ! Heat flux to the ocean [W.m-2], >0
+            hfx_bog_1d(ji) = hfx_bog_1d(ji) - zdE  * zfmdt                      * a_i_1d(ji) * r1_Dt_ice   ! Heat flux used in this process [W.m-2], <0          
+            wfx_bog_1d(ji) = wfx_bog_1d(ji) - rhoi * dh_i_bog(ji)               * a_i_1d(ji) * r1_Dt_ice   ! Mass flux, <0
+            sfx_bog_1d(ji) = sfx_bog_1d(ji) - rhoi * dh_i_bog(ji) * s_i_new(ji) * a_i_1d(ji) * r1_Dt_ice   ! Salt flux, <0
+
+            ! update thickness
+            zh_i(ji,nlay_i+1) = zh_i(ji,nlay_i+1) + dh_i_bog(ji)
+            h_i_1d(ji)        = h_i_1d(ji)        + dh_i_bog(ji)
 
             ! update heat content (J.m-2) and layer thickness
@@ -466,5 +413,4 @@
       ! Ice Basal melt
       !---------------
-      zdeltah(1:npti,:) = 0._wp ! important
       DO jk = nlay_i, 1, -1
          DO ji = 1, npti
@@ -475,99 +421,104 @@
                IF( t_i_1d(ji,jk) >= (ztmelts+rt0) ) THEN   !-- Internal melting
 
-                  zEi               = - e_i_1d(ji,jk) * r1_rhoi     ! Specific enthalpy of melting ice (J/kg, <0)
-                  zdE               = 0._wp                         ! Specific enthalpy difference   (J/kg, <0)
-                                                                    !    set up at 0 since no energy is needed to melt water...(it is already melted)
-                  zdeltah   (ji,jk) = MIN( 0._wp , - zh_i(ji,jk) )  ! internal melting occurs when the internal temperature is above freezing     
-                                                                    ! this should normally not happen, but sometimes, heat diffusion leads to this
-
-                  dh_i_itm (ji)     = dh_i_itm(ji) + zdeltah(ji,jk)
-
-                  zfmdt             = - zdeltah(ji,jk) * rhoi      ! Mass flux x time step > 0
-
-                  hfx_res_1d(ji) = hfx_res_1d(ji) + zfmdt * a_i_1d(ji) * zEi * r1_Dt_ice                           ! Heat flux to the ocean [W.m-2], <0
-                  !                                                                                                  ice enthalpy zEi is "sent" to the ocean
-                  sfx_res_1d(ji) = sfx_res_1d(ji) - rhoi * a_i_1d(ji) * zdeltah(ji,jk) * s_i_1d(ji) * r1_Dt_ice    ! Salt flux
-                  !                                                                                                  using s_i_1d and not sz_i_1d(jk) is ok
-                  wfx_res_1d(ji) = wfx_res_1d(ji) - rhoi * a_i_1d(ji) * zdeltah(ji,jk) * r1_Dt_ice                 ! Mass flux
-
-                  ! update heat content (J.m-2) and layer thickness
-                  eh_i_old(ji,jk) = eh_i_old(ji,jk) + zdeltah(ji,jk) * e_i_1d(ji,jk)
-                  h_i_old (ji,jk) = h_i_old (ji,jk) + zdeltah(ji,jk)
-
+                  zEi            = - e_i_1d(ji,jk) * r1_rhoi     ! Specific enthalpy of melting ice (J/kg, <0)
+                  zdE            = 0._wp                         ! Specific enthalpy difference   (J/kg, <0)
+                  !                                                  set up at 0 since no energy is needed to melt water...(it is already melted)
+                  zdum           = MIN( 0._wp , - zh_i(ji,jk) )  ! internal melting occurs when the internal temperature is above freezing     
+                  !                                                  this should normally not happen, but sometimes, heat diffusion leads to this
+                  dh_i_itm (ji)  = dh_i_itm(ji) + zdum
+                  !
+                  zfmdt          = - zdum * rhoi                 ! Mass flux x time step > 0
+                  !
+                  hfx_res_1d(ji) = hfx_res_1d(ji) + zEi  * zfmdt             * a_i_1d(ji) * r1_Dt_ice   ! Heat flux to the ocean [W.m-2], <0
+                  !                                                                                         ice enthalpy zEi is "sent" to the ocean
+                  wfx_res_1d(ji) = wfx_res_1d(ji) - rhoi * zdum              * a_i_1d(ji) * r1_Dt_ice   ! Mass flux
+                  sfx_res_1d(ji) = sfx_res_1d(ji) - rhoi * zdum * s_i_1d(ji) * a_i_1d(ji) * r1_Dt_ice   ! Salt flux
+                  !                                                                                         using s_i_1d and not sz_i_1d(jk) is ok
                ELSE                                        !-- Basal melting
 
-                  zEi             = - e_i_1d(ji,jk) * r1_rhoi                                 ! Specific enthalpy of melting ice (J/kg, <0)
-                  zEw             = rcp * ztmelts                                             ! Specific enthalpy of meltwater (J/kg, <0)
-                  zdE             = zEi - zEw                                                 ! Specific enthalpy difference   (J/kg, <0)
-
-                  zfmdt           = - zq_bot(ji) / zdE                                        ! Mass flux x time step (kg/m2, >0)
-
-                  zdeltah(ji,jk)  = - zfmdt * r1_rhoi                                         ! Gross thickness change
-
-                  zdeltah(ji,jk)  = MIN( 0._wp , MAX( zdeltah(ji,jk), - zh_i(ji,jk) ) )       ! bound thickness change
+                  zEi            = - e_i_1d(ji,jk) * r1_rhoi                       ! Specific enthalpy of melting ice (J/kg, <0)
+                  zEw            = rcp * ztmelts                                   ! Specific enthalpy of meltwater (J/kg, <0)
+                  zdE            = zEi - zEw                                       ! Specific enthalpy difference   (J/kg, <0)
+
+                  zfmdt          = - zq_bot(ji) / zdE                              ! Mass flux x time step (kg/m2, >0)
+
+                  zdum           = - zfmdt * r1_rhoi                               ! Gross thickness change
+
+                  zdum           = MIN( 0._wp , MAX( zdum, - zh_i(ji,jk) ) )       ! bound thickness change
                   
-                  zq_bot(ji)      = MAX( 0._wp , zq_bot(ji) - zdeltah(ji,jk) * rhoi * zdE )   ! update available heat. MAX is necessary for roundup errors
-
-                  dh_i_bom(ji)    = dh_i_bom(ji) + zdeltah(ji,jk)                             ! Update basal melt
-
-                  zfmdt           = - zdeltah(ji,jk) * rhoi                                   ! Mass flux x time step > 0
-
-                  zQm             = zfmdt * zEw                                               ! Heat exchanged with ocean
-
-                  hfx_thd_1d(ji)  = hfx_thd_1d(ji) + zfmdt * a_i_1d(ji) * zEw * r1_Dt_ice                           ! Heat flux to the ocean [W.m-2], <0  
-                  hfx_bom_1d(ji)  = hfx_bom_1d(ji) - zfmdt * a_i_1d(ji) * zdE * r1_Dt_ice                           ! Heat used in this process [W.m-2], >0  
-
-                  sfx_bom_1d(ji)  = sfx_bom_1d(ji) - rhoi *  a_i_1d(ji) * zdeltah(ji,jk) * s_i_1d(ji) * r1_Dt_ice   ! Salt flux
-                  !                                                                                                   using s_i_1d and not sz_i_1d(jk) is ok
-                  
-                  wfx_bom_1d(ji)  = wfx_bom_1d(ji) - rhoi * a_i_1d(ji) * zdeltah(ji,jk) * r1_Dt_ice                 ! Mass flux
-
-                  ! update heat content (J.m-2) and layer thickness
-                  eh_i_old(ji,jk) = eh_i_old(ji,jk) + zdeltah(ji,jk) * e_i_1d(ji,jk)
-                  h_i_old (ji,jk) = h_i_old (ji,jk) + zdeltah(ji,jk)
+                  zq_bot(ji)     = MAX( 0._wp , zq_bot(ji) - zdum * rhoi * zdE )   ! update available heat. MAX is necessary for roundup errors
+
+                  dh_i_bom(ji)   = dh_i_bom(ji) + zdum                             ! Update basal melt
+
+                  zfmdt          = - zdum * rhoi                                   ! Mass flux x time step > 0
+
+                  zQm            = zfmdt * zEw                                     ! Heat exchanged with ocean
+
+                  hfx_thd_1d(ji) = hfx_thd_1d(ji) + zEw  * zfmdt             * a_i_1d(ji) * r1_Dt_ice   ! Heat flux to the ocean [W.m-2], <0  
+                  hfx_bom_1d(ji) = hfx_bom_1d(ji) - zdE  * zfmdt             * a_i_1d(ji) * r1_Dt_ice   ! Heat used in this process [W.m-2], >0
+                  wfx_bom_1d(ji) = wfx_bom_1d(ji) - rhoi * zdum              * a_i_1d(ji) * r1_Dt_ice   ! Mass flux
+                  sfx_bom_1d(ji) = sfx_bom_1d(ji) - rhoi * zdum * s_i_1d(ji) * a_i_1d(ji) * r1_Dt_ice   ! Salt flux
+                  !                                                                                         using s_i_1d and not sz_i_1d(jk) is ok
                ENDIF
-           
+               ! update thickness
+               zh_i(ji,jk) = MAX( 0._wp, zh_i(ji,jk) + zdum )
+               h_i_1d(ji)  = MAX( 0._wp, h_i_1d(ji)  + zdum )
+               !
+               ! update heat content (J.m-2) and layer thickness
+               eh_i_old(ji,jk) = eh_i_old(ji,jk) + zdum * e_i_1d(ji,jk)
+               h_i_old (ji,jk) = h_i_old (ji,jk) + zdum
             ENDIF
          END DO
       END DO
 
-      ! Update temperature, energy
-      ! --------------------------
-      DO ji = 1, npti
-         h_i_1d(ji) = MAX( 0._wp , h_i_1d(ji) + dh_i_bog(ji) + dh_i_bom(ji) )
-      END DO  
-
-      ! If heat still available then melt more snow
-      !-------------------------------------------
-      zdeltah(1:npti,:) = 0._wp ! important
-      DO ji = 1, npti
-         zq_rema (ji)   = zq_top(ji) + zq_bot(ji) 
-         rswitch        = 1._wp - MAX( 0._wp, SIGN( 1._wp, - h_s_1d(ji) ) )   ! =1 if snow
-         rswitch        = rswitch * MAX( 0._wp, SIGN( 1._wp, e_s_1d(ji,1) - epsi20 ) )
-         zdeltah (ji,1) = - rswitch * zq_rema(ji) / MAX( e_s_1d(ji,1), epsi20 )
-         zdeltah (ji,1) = MIN( 0._wp , MAX( zdeltah(ji,1) , - h_s_1d(ji) ) ) ! bound melting
-         dh_s_tot(ji)   = dh_s_tot(ji) + zdeltah(ji,1)
-         h_s_1d  (ji)   = h_s_1d  (ji) + zdeltah(ji,1)
-        
-         zq_rema(ji)        = zq_rema(ji) + zdeltah(ji,1) * e_s_1d(ji,1)                               ! update available heat (J.m-2)
-         hfx_snw_1d(ji)     = hfx_snw_1d(ji) - zdeltah(ji,1) * a_i_1d(ji) * e_s_1d(ji,1) * r1_Dt_ice   ! Heat used to melt snow, W.m-2 (>0)
-         wfx_snw_sum_1d(ji) = wfx_snw_sum_1d(ji) - rhos * a_i_1d(ji) * zdeltah(ji,1) * r1_Dt_ice       ! Mass flux
-         dh_s_mlt(ji)       = dh_s_mlt(ji) + zdeltah(ji,1)
-         !    
-         ! Remaining heat flux (W.m-2) is sent to the ocean heat budget
-         !!hfx_res_1d(ji) = hfx_res_1d(ji) + ( zq_rema(ji) * a_i_1d(ji) ) * r1_Dt_ice
-
-         IF( ln_icectl .AND. zq_rema(ji) < 0. .AND. lwp ) WRITE(numout,*) 'ALERTE zq_rema <0 = ', zq_rema(ji)
-      END DO
-
-      !
+      ! Remove snow if ice has melted entirely
+      ! --------------------------------------
+      DO jk = 0, nlay_s
+         DO ji = 1,npti
+            IF( h_i_1d(ji) == 0._wp ) THEN
+               ! mass & energy loss to the ocean
+               hfx_res_1d(ji) = hfx_res_1d(ji) - ze_s(ji,jk) * zh_s(ji,jk) * a_i_1d(ji) * r1_Dt_ice  ! heat flux to the ocean [W.m-2], < 0
+               wfx_res_1d(ji) = wfx_res_1d(ji) + rhos        * zh_s(ji,jk) * a_i_1d(ji) * r1_Dt_ice  ! mass flux
+
+               ! update thickness and energy
+               h_s_1d(ji)    = 0._wp
+               ze_s  (ji,jk) = 0._wp
+               zh_s  (ji,jk) = 0._wp
+            ENDIF
+         END DO
+      END DO
+      
+      ! Snow load on ice
+      ! -----------------
+      ! When snow load exceeds Archimede's limit and sst is positive,
+      ! snow-ice formation (next bloc) can lead to negative ice enthalpy.
+      ! Therefore we consider here that this excess of snow falls into the ocean
+      zdeltah(1:npti) = h_s_1d(1:npti) + h_i_1d(1:npti) * (rhoi-rho0) * r1_rhos
+      DO jk = 0, nlay_s
+         DO ji = 1, npti
+            IF( zdeltah(ji) > 0._wp .AND. sst_1d(ji) > 0._wp ) THEN
+               ! snow layer thickness that falls into the ocean
+               zdum = MIN( zdeltah(ji) , zh_s(ji,jk) )
+               ! mass & energy loss to the ocean
+               hfx_res_1d(ji) = hfx_res_1d(ji) - ze_s(ji,jk) * zdum * a_i_1d(ji) * r1_Dt_ice  ! heat flux to the ocean [W.m-2], < 0
+               wfx_res_1d(ji) = wfx_res_1d(ji) + rhos        * zdum * a_i_1d(ji) * r1_Dt_ice  ! mass flux
+               ! update thickness and energy
+               h_s_1d(ji)    = MAX( 0._wp, h_s_1d(ji)  - zdum )
+               zh_s  (ji,jk) = MAX( 0._wp, zh_s(ji,jk) - zdum )
+               ! update snow thickness that still has to fall
+               zdeltah(ji)   = MAX( 0._wp, zdeltah(ji) - zdum )
+            ENDIF
+         END DO
+      END DO
+      
       ! Snow-Ice formation
       ! ------------------
-      ! When snow load excesses Archimede's limit, snow-ice interface goes down under sea-level, 
-      ! flooding of seawater transforms snow into ice dh_snowice is positive for the ice
+      ! When snow load exceeds Archimede's limit, snow-ice interface goes down under sea-level, 
+      ! flooding of seawater transforms snow into ice. Thickness that is transformed is dh_snowice (positive for the ice)
       z1_rho = 1._wp / ( rhos+rho0-rhoi )
+      zdeltah(1:npti) = 0._wp
       DO ji = 1, npti
          !
-         dh_snowice(ji) = MAX(  0._wp , ( rhos * h_s_1d(ji) + (rhoi-rho0) * h_i_1d(ji) ) * z1_rho )
+         dh_snowice(ji) = MAX( 0._wp , ( rhos * h_s_1d(ji) + (rhoi-rho0) * h_i_1d(ji) ) * z1_rho )
 
          h_i_1d(ji)    = h_i_1d(ji) + dh_snowice(ji)
@@ -579,56 +530,163 @@
          zQm            = zfmdt * zEw 
          
-         hfx_thd_1d(ji) = hfx_thd_1d(ji) + zfmdt * a_i_1d(ji) * zEw * r1_Dt_ice ! Heat flux
-
-         sfx_sni_1d(ji) = sfx_sni_1d(ji) + sss_1d(ji) * a_i_1d(ji) * zfmdt * r1_Dt_ice ! Salt flux
+         hfx_thd_1d(ji) = hfx_thd_1d(ji) + zEw        * zfmdt * a_i_1d(ji) * r1_Dt_ice ! Heat flux
+         sfx_sni_1d(ji) = sfx_sni_1d(ji) + sss_1d(ji) * zfmdt * a_i_1d(ji) * r1_Dt_ice ! Salt flux
 
          ! Case constant salinity in time: virtual salt flux to keep salinity constant
          IF( nn_icesal /= 2 )  THEN
-            sfx_bri_1d(ji) = sfx_bri_1d(ji) - sss_1d (ji) * a_i_1d(ji) * zfmdt                  * r1_Dt_ice  & ! put back sss_m     into the ocean
-               &                            - s_i_1d(ji)  * a_i_1d(ji) * dh_snowice(ji) * rhoi * r1_Dt_ice     ! and get  rn_icesal from the ocean 
+            sfx_bri_1d(ji) = sfx_bri_1d(ji) - sss_1d(ji) * zfmdt                 * a_i_1d(ji) * r1_Dt_ice  &  ! put back sss_m     into the ocean
+               &                            - s_i_1d(ji) * dh_snowice(ji) * rhoi * a_i_1d(ji) * r1_Dt_ice     ! and get  rn_icesal from the ocean 
          ENDIF
 
          ! Mass flux: All snow is thrown in the ocean, and seawater is taken to replace the volume
-         wfx_sni_1d(ji)     = wfx_sni_1d(ji)     - a_i_1d(ji) * dh_snowice(ji) * rhoi * r1_Dt_ice
-         wfx_snw_sni_1d(ji) = wfx_snw_sni_1d(ji) + a_i_1d(ji) * dh_snowice(ji) * rhos * r1_Dt_ice
+         wfx_sni_1d    (ji) = wfx_sni_1d    (ji) - dh_snowice(ji) * rhoi * a_i_1d(ji) * r1_Dt_ice
+         wfx_snw_sni_1d(ji) = wfx_snw_sni_1d(ji) + dh_snowice(ji) * rhos * a_i_1d(ji) * r1_Dt_ice
+
+         ! update thickness
+         zh_i(ji,0)  = zh_i(ji,0) + dh_snowice(ji)
+         zdeltah(ji) =              dh_snowice(ji)
 
          ! update heat content (J.m-2) and layer thickness
-         eh_i_old(ji,0) = eh_i_old(ji,0) + dh_snowice(ji) * e_s_1d(ji,1) + zfmdt * zEw
          h_i_old (ji,0) = h_i_old (ji,0) + dh_snowice(ji)
-         
-      END DO
-
-      !
-      ! Update temperature, energy
-      ! --------------------------
-      DO ji = 1, npti
-         rswitch     = 1._wp - MAX( 0._wp , SIGN( 1._wp , - h_i_1d(ji) ) ) 
-         t_su_1d(ji) = rswitch * t_su_1d(ji) + ( 1._wp - rswitch ) * rt0
-      END DO
-
+         eh_i_old(ji,0) = eh_i_old(ji,0) + zfmdt * zEw           ! 1st part (sea water enthalpy)
+
+      END DO
+      !
+      DO jk = nlay_s, 0, -1   ! flooding of snow starts from the base
+         DO ji = 1, npti
+            zdum           = MIN( zdeltah(ji), zh_s(ji,jk) )     ! amount of snw that floods, > 0
+            zh_s(ji,jk)    = MAX( 0._wp, zh_s(ji,jk) - zdum )    ! remove some snow thickness
+            eh_i_old(ji,0) = eh_i_old(ji,0) + zdum * ze_s(ji,jk) ! 2nd part (snow enthalpy)
+            ! update dh_snowice
+            zdeltah(ji)    = MAX( 0._wp, zdeltah(ji) - zdum )
+         END DO
+      END DO
+      !
+      !
+!!$      ! --- Update snow diags --- !
+!!$      !!clem: this is wrong. dh_s_tot is not used anyway
+!!$      DO ji = 1, npti
+!!$         dh_s_tot(ji) = dh_s_tot(ji) + dh_s_mlt(ji) + zdeltah(ji) + zdh_s_sub(ji) - dh_snowice(ji)
+!!$      END DO
+      !
+      !
+      ! Remapping of snw enthalpy on a regular grid
+      !--------------------------------------------
+      CALL snw_ent( zh_s, ze_s, e_s_1d )
+      
+      ! recalculate t_s_1d from e_s_1d
       DO jk = 1, nlay_s
          DO ji = 1,npti
-            ! where there is no ice or no snow
-            rswitch = ( 1._wp - MAX( 0._wp, SIGN( 1._wp, - h_s_1d(ji) ) ) ) * ( 1._wp - MAX( 0._wp, SIGN(1._wp, - h_i_1d(ji) ) ) )
-            ! mass & energy loss to the ocean
-            hfx_res_1d(ji) = hfx_res_1d(ji) + ( 1._wp - rswitch ) * &
-               &                              ( e_s_1d(ji,jk) * h_s_1d(ji) * r1_nlay_s * a_i_1d(ji) * r1_Dt_ice )  ! heat flux to the ocean [W.m-2], < 0
-            wfx_res_1d(ji) = wfx_res_1d(ji) + ( 1._wp - rswitch ) * &
-               &                              ( rhos          * h_s_1d(ji) * r1_nlay_s * a_i_1d(ji) * r1_Dt_ice )  ! mass flux
-            ! update energy (mass is updated in the next loop)
-            e_s_1d(ji,jk) = rswitch * e_s_1d(ji,jk)
-            ! recalculate t_s_1d from e_s_1d
-            t_s_1d(ji,jk) = rt0 + rswitch * ( - e_s_1d(ji,jk) * r1_rhos * r1_rcpi + rLfus * r1_rcpi )
-         END DO
-      END DO
+            IF( h_s_1d(ji) > 0._wp ) THEN
+               t_s_1d(ji,jk) = rt0 + ( - e_s_1d(ji,jk) * r1_rhos * r1_rcpi + rLfus * r1_rcpi )
+            ELSE
+               t_s_1d(ji,jk) = rt0
+            ENDIF
+         END DO
+      END DO
+
+      ! Note: remapping of ice enthalpy is done in icethd.F90
 
       ! --- ensure that a_i = 0 & h_s = 0 where h_i = 0 ---
       WHERE( h_i_1d(1:npti) == 0._wp )   
-         a_i_1d(1:npti) = 0._wp
-         h_s_1d(1:npti) = 0._wp
+         a_i_1d (1:npti) = 0._wp
+         h_s_1d (1:npti) = 0._wp
+         t_su_1d(1:npti) = rt0
       END WHERE
-      !
+      
    END SUBROUTINE ice_thd_dh
 
+   SUBROUTINE snw_ent( ph_old, pe_old, pe_new )
+      !!-------------------------------------------------------------------
+      !!               ***   ROUTINE snw_ent  ***
+      !!
+      !! ** Purpose :
+      !!           This routine computes new vertical grids in the snow, 
+      !!           and consistently redistributes temperatures. 
+      !!           Redistribution is made so as to ensure to energy conservation
+      !!
+      !!
+      !! ** Method  : linear conservative remapping
+      !!           
+      !! ** Steps : 1) cumulative integrals of old enthalpies/thicknesses
+      !!            2) linear remapping on the new layers
+      !!
+      !! ------------ cum0(0)                        ------------- cum1(0)
+      !!                                    NEW      -------------
+      !! ------------ cum0(1)               ==>      -------------
+      !!     ...                                     -------------
+      !! ------------                                -------------
+      !! ------------ cum0(nlay_s+1)                 ------------- cum1(nlay_s)
+      !!
+      !!
+      !! References : Bitz & Lipscomb, JGR 99; Vancoppenolle et al., GRL, 2005
+      !!-------------------------------------------------------------------
+      REAL(wp), DIMENSION(jpij,0:nlay_s), INTENT(in   ) ::   ph_old             ! old thicknesses (m)
+      REAL(wp), DIMENSION(jpij,0:nlay_s), INTENT(in   ) ::   pe_old             ! old enthlapies (J.m-3)
+      REAL(wp), DIMENSION(jpij,1:nlay_s), INTENT(inout) ::   pe_new             ! new enthlapies (J.m-3, remapped)
+      !
+      INTEGER  :: ji         !  dummy loop indices
+      INTEGER  :: jk0, jk1   !  old/new layer indices
+      !
+      REAL(wp), DIMENSION(jpij,0:nlay_s+1) ::   zeh_cum0, zh_cum0   ! old cumulative enthlapies and layers interfaces
+      REAL(wp), DIMENSION(jpij,0:nlay_s)   ::   zeh_cum1, zh_cum1   ! new cumulative enthlapies and layers interfaces
+      REAL(wp), DIMENSION(jpij)            ::   zhnew               ! new layers thicknesses
+      !!-------------------------------------------------------------------
+
+      !--------------------------------------------------------------------------
+      !  1) Cumulative integral of old enthalpy * thickness and layers interfaces
+      !--------------------------------------------------------------------------
+      zeh_cum0(1:npti,0) = 0._wp 
+      zh_cum0 (1:npti,0) = 0._wp
+      DO jk0 = 1, nlay_s+1
+         DO ji = 1, npti
+            zeh_cum0(ji,jk0) = zeh_cum0(ji,jk0-1) + pe_old(ji,jk0-1) * ph_old(ji,jk0-1)
+            zh_cum0 (ji,jk0) = zh_cum0 (ji,jk0-1) + ph_old(ji,jk0-1)
+         END DO
+      END DO
+
+      !------------------------------------
+      !  2) Interpolation on the new layers
+      !------------------------------------
+      ! new layer thickesses
+      DO ji = 1, npti
+         zhnew(ji) = SUM( ph_old(ji,0:nlay_s) ) * r1_nlay_s  
+      END DO
+
+      ! new layers interfaces
+      zh_cum1(1:npti,0) = 0._wp
+      DO jk1 = 1, nlay_s
+         DO ji = 1, npti
+            zh_cum1(ji,jk1) = zh_cum1(ji,jk1-1) + zhnew(ji)
+         END DO
+      END DO
+
+      zeh_cum1(1:npti,0:nlay_s) = 0._wp 
+      ! new cumulative q*h => linear interpolation
+      DO jk0 = 1, nlay_s+1
+         DO jk1 = 1, nlay_s-1
+            DO ji = 1, npti
+               IF( zh_cum1(ji,jk1) <= zh_cum0(ji,jk0) .AND. zh_cum1(ji,jk1) > zh_cum0(ji,jk0-1) ) THEN
+                  zeh_cum1(ji,jk1) = ( zeh_cum0(ji,jk0-1) * ( zh_cum0(ji,jk0) - zh_cum1(ji,jk1  ) ) +  &
+                     &                 zeh_cum0(ji,jk0  ) * ( zh_cum1(ji,jk1) - zh_cum0(ji,jk0-1) ) )  &
+                     &             / ( zh_cum0(ji,jk0) - zh_cum0(ji,jk0-1) )
+               ENDIF
+            END DO
+         END DO
+      END DO
+      ! to ensure that total heat content is strictly conserved, set:
+      zeh_cum1(1:npti,nlay_s) = zeh_cum0(1:npti,nlay_s+1) 
+
+      ! new enthalpies
+      DO jk1 = 1, nlay_s
+         DO ji = 1, npti
+            rswitch      = MAX( 0._wp , SIGN( 1._wp , zhnew(ji) - epsi20 ) ) 
+            pe_new(ji,jk1) = rswitch * ( zeh_cum1(ji,jk1) - zeh_cum1(ji,jk1-1) ) / MAX( zhnew(ji), epsi20 )
+         END DO
+      END DO
+      
+   END SUBROUTINE snw_ent
+
+   
 #else
    !!----------------------------------------------------------------------
Index: /NEMO/branches/2020/tickets_icb_1900/src/ICE/icethd_pnd.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/ICE/icethd_pnd.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/ICE/icethd_pnd.F90	(revision 14016)
@@ -20,6 +20,8 @@
    USE ice1D          ! sea-ice: thermodynamics variables
    USE icetab         ! sea-ice: 1D <==> 2D transformation
+   USE sbc_ice        ! surface energy budget
    !
    USE in_out_manager ! I/O manager
+   USE iom            ! I/O manager library
    USE lib_mpp        ! MPP library
    USE lib_fortran    ! fortran utilities (glob_sum + no signed zero)
@@ -34,8 +36,32 @@
    INTEGER ::              nice_pnd    ! choice of the type of pond scheme
    !                                   ! associated indices:
-   INTEGER, PARAMETER ::   np_pndNO  = 0   ! No pond scheme
-   INTEGER, PARAMETER ::   np_pndCST = 1   ! Constant ice pond scheme
-   INTEGER, PARAMETER ::   np_pndLEV = 2   ! Level ice pond scheme
-
+   INTEGER, PARAMETER ::   np_pndNO   = 0   ! No pond scheme
+   INTEGER, PARAMETER ::   np_pndCST  = 1   ! Constant ice pond scheme
+   INTEGER, PARAMETER ::   np_pndLEV  = 2   ! Level ice pond scheme
+   INTEGER, PARAMETER ::   np_pndTOPO = 3   ! Level ice pond scheme
+
+   !-------------------------------------------------------------------------- 
+   ! Diagnostics for pond volume per area
+   !
+   ! dV/dt = mlt + drn + lid + rnf
+   ! mlt   = input from surface melting
+   ! drn   = drainage through brine network
+   ! lid   = lid growth & melt
+   ! rnf   = runoff (water directly removed out of surface melting + overflow)
+   !
+   ! In topo mode, the pond water lost because it is in the snow is not included in the budget
+   ! In level mode, all terms are incorporated
+   !
+   REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   diag_dvpn_mlt       ! meltwater pond volume input      [kg/m2/s]
+   REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   diag_dvpn_drn       ! pond volume lost by drainage     [-]
+   REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   diag_dvpn_lid       ! exchange with lid / refreezing   [-]
+   REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   diag_dvpn_rnf       ! meltwater pond lost to runoff    [-]      
+   REAL(wp), ALLOCATABLE, DIMENSION(:)   ::   diag_dvpn_mlt_1d    ! meltwater pond volume input      [-]
+   REAL(wp), ALLOCATABLE, DIMENSION(:)   ::   diag_dvpn_drn_1d    ! pond volume lost by drainage     [-]
+   REAL(wp), ALLOCATABLE, DIMENSION(:)   ::   diag_dvpn_lid_1d    ! exchange with lid / refreezing   [-]
+   REAL(wp), ALLOCATABLE, DIMENSION(:)   ::   diag_dvpn_rnf_1d    ! meltwater pond lost to runoff    [-]
+
+   !! * Substitutions
+#  include "do_loop_substitute.h90"
    !!----------------------------------------------------------------------
    !! NEMO/ICE 4.0 , NEMO Consortium (2018)
@@ -46,19 +72,81 @@
 
    SUBROUTINE ice_thd_pnd
+
       !!-------------------------------------------------------------------
       !!               ***  ROUTINE ice_thd_pnd   ***
       !!               
       !! ** Purpose :   change melt pond fraction and thickness
-      !!                
+      !!
+      !! ** Note    : Melt ponds affect only radiative transfer for now
+      !!              No heat, no salt.
+      !!              The current diagnostics lacks a contribution from drainage
       !!-------------------------------------------------------------------
+      INTEGER ::   ji, jj, jl        ! loop indices
+      !!-------------------------------------------------------------------
+      
+      ALLOCATE( diag_dvpn_mlt(jpi,jpj), diag_dvpn_lid(jpi,jpj), diag_dvpn_drn(jpi,jpj), diag_dvpn_rnf(jpi,jpj) )
+      ALLOCATE( diag_dvpn_mlt_1d(jpij), diag_dvpn_lid_1d(jpij), diag_dvpn_drn_1d(jpij), diag_dvpn_rnf_1d(jpij) )
       !
-      SELECT CASE ( nice_pnd )
+      diag_dvpn_mlt (:,:) = 0._wp   ;   diag_dvpn_drn (:,:) = 0._wp
+      diag_dvpn_lid (:,:) = 0._wp   ;   diag_dvpn_rnf (:,:) = 0._wp
+      diag_dvpn_mlt_1d(:) = 0._wp   ;   diag_dvpn_drn_1d(:) = 0._wp
+      diag_dvpn_lid_1d(:) = 0._wp   ;   diag_dvpn_rnf_1d(:) = 0._wp
+
+      !-------------------------------------
+      !  Remove ponds where ice has vanished
+      !-------------------------------------
+      at_i(:,:) = SUM( a_i, dim=3 )
       !
-      CASE (np_pndCST)   ;   CALL pnd_CST    !==  Constant melt ponds  ==!
-         !
-      CASE (np_pndLEV)   ;   CALL pnd_LEV    !==  Level ice melt ponds  ==!
-         !
-      END SELECT
+      DO jl = 1, jpl
+         DO_2D( 1, 1, 1, 1 )
+            IF( v_i(ji,jj,jl) < epsi10 .OR. at_i(ji,jj) < epsi10 ) THEN
+               wfx_pnd  (ji,jj)    = wfx_pnd(ji,jj) + ( v_ip(ji,jj,jl) + v_il(ji,jj,jl) ) * rhow * r1_Dt_ice
+               a_ip     (ji,jj,jl) = 0._wp
+               v_ip     (ji,jj,jl) = 0._wp
+               v_il     (ji,jj,jl) = 0._wp
+               h_ip     (ji,jj,jl) = 0._wp
+               h_il     (ji,jj,jl) = 0._wp
+               a_ip_frac(ji,jj,jl) = 0._wp
+            ENDIF
+         END_2D
+      END DO
+      
+      !------------------------------
+      !  Identify grid cells with ice
+      !------------------------------
+      npti = 0   ;   nptidx(:) = 0
+      DO_2D( 1, 1, 1, 1 )
+         IF( at_i(ji,jj) >= epsi10 ) THEN
+            npti = npti + 1
+            nptidx( npti ) = (jj - 1) * jpi + ji
+         ENDIF
+      END_2D
+
+      !------------------------------------
+      !  Select melt pond scheme to be used
+      !------------------------------------
+      IF( npti > 0 ) THEN
+         SELECT CASE ( nice_pnd )
+            !
+         CASE (np_pndCST)   ;   CALL pnd_CST                              !==  Constant melt ponds  ==!
+            !
+         CASE (np_pndLEV)   ;   CALL pnd_LEV                              !==  Level ice melt ponds  ==!
+            !
+         CASE (np_pndTOPO)  ;   CALL pnd_TOPO                             !==  Topographic melt ponds  ==!
+            !
+         END SELECT
+      ENDIF
+
+      !------------------------------------
+      !  Diagnostics
+      !------------------------------------
+      CALL iom_put( 'dvpn_mlt', diag_dvpn_mlt ) ! input from melting
+      CALL iom_put( 'dvpn_lid', diag_dvpn_lid ) ! exchanges with lid
+      CALL iom_put( 'dvpn_drn', diag_dvpn_drn ) ! vertical drainage
+      CALL iom_put( 'dvpn_rnf', diag_dvpn_rnf ) ! runoff + overflow
       !
+      DEALLOCATE( diag_dvpn_mlt   , diag_dvpn_lid   , diag_dvpn_drn   , diag_dvpn_rnf    )
+      DEALLOCATE( diag_dvpn_mlt_1d, diag_dvpn_lid_1d, diag_dvpn_drn_1d, diag_dvpn_rnf_1d )
+      
    END SUBROUTINE ice_thd_pnd 
 
@@ -80,18 +168,45 @@
       !! ** References : Bush, G.W., and Trump, D.J. (2017)
       !!-------------------------------------------------------------------
-      INTEGER  ::   ji        ! loop indices
+      INTEGER  ::   ji, jl    ! loop indices
+      REAL(wp) ::   zdv_pnd   ! Amount of water going into the ponds & lids
       !!-------------------------------------------------------------------
-      DO ji = 1, npti
-         !
-         IF( a_i_1d(ji) > 0._wp .AND. t_su_1d(ji) >= rt0 ) THEN
-            h_ip_1d(ji)      = rn_hpnd    
-            a_ip_1d(ji)      = rn_apnd * a_i_1d(ji)
-            h_il_1d(ji)      = 0._wp    ! no pond lids whatsoever
-         ELSE
-            h_ip_1d(ji)      = 0._wp    
-            a_ip_1d(ji)      = 0._wp
-            h_il_1d(ji)      = 0._wp
-         ENDIF
-         !
+      DO jl = 1, jpl
+         
+         CALL tab_2d_1d( npti, nptidx(1:npti), a_i_1d    (1:npti), a_i    (:,:,jl) )
+         CALL tab_2d_1d( npti, nptidx(1:npti), t_su_1d   (1:npti), t_su   (:,:,jl) )
+         CALL tab_2d_1d( npti, nptidx(1:npti), a_ip_1d   (1:npti), a_ip   (:,:,jl) )
+         CALL tab_2d_1d( npti, nptidx(1:npti), h_ip_1d   (1:npti), h_ip   (:,:,jl) )
+         CALL tab_2d_1d( npti, nptidx(1:npti), h_il_1d   (1:npti), h_il   (:,:,jl) )
+         CALL tab_2d_1d( npti, nptidx(1:npti), wfx_pnd_1d(1:npti), wfx_pnd(:,:)    )
+
+         DO ji = 1, npti
+            !
+            zdv_pnd = ( h_ip_1d(ji) + h_il_1d(ji) ) * a_ip_1d(ji)
+            !
+            IF( a_i_1d(ji) >= 0.01_wp .AND. t_su_1d(ji) >= rt0 ) THEN
+               h_ip_1d(ji)      = rn_hpnd    
+               a_ip_1d(ji)      = rn_apnd * a_i_1d(ji)
+               h_il_1d(ji)      = 0._wp    ! no pond lids whatsoever
+            ELSE
+               h_ip_1d(ji)      = 0._wp    
+               a_ip_1d(ji)      = 0._wp
+               h_il_1d(ji)      = 0._wp
+            ENDIF
+            !
+            v_ip_1d(ji) = h_ip_1d(ji) * a_ip_1d(ji)
+            v_il_1d(ji) = h_il_1d(ji) * a_ip_1d(ji)
+            !
+            zdv_pnd = ( h_ip_1d(ji) + h_il_1d(ji) ) * a_ip_1d(ji) - zdv_pnd
+            wfx_pnd_1d(ji) = wfx_pnd_1d(ji) - zdv_pnd * rhow * r1_Dt_ice
+            !
+         END DO
+
+         CALL tab_1d_2d( npti, nptidx(1:npti), a_ip_1d   (1:npti), a_ip   (:,:,jl) )
+         CALL tab_1d_2d( npti, nptidx(1:npti), h_ip_1d   (1:npti), h_ip   (:,:,jl) )
+         CALL tab_1d_2d( npti, nptidx(1:npti), h_il_1d   (1:npti), h_il   (:,:,jl) )
+         CALL tab_1d_2d( npti, nptidx(1:npti), v_ip_1d   (1:npti), v_ip   (:,:,jl) )
+         CALL tab_1d_2d( npti, nptidx(1:npti), v_il_1d   (1:npti), v_il   (:,:,jl) )
+         CALL tab_1d_2d( npti, nptidx(1:npti), wfx_pnd_1d(1:npti), wfx_pnd(:,:)    )
+
       END DO
       !
@@ -132,9 +247,10 @@
       !!                    if no lids:   Vp = Vp * exp(0.01*MAX(Tp-Tsu,0)/Tp)                      --- from Holland et al 2012 ---
       !!
-      !!              - Flushing:         w = -perm/visc * rho_oce * grav * Hp / Hi                 --- from Flocco et al 2007 ---
-      !!                                     perm = permability of sea-ice
+      !!              - Flushing:         w = -perm/visc * rho_oce * grav * Hp / Hi * flush         --- from Flocco et al 2007 ---
+      !!                                     perm = permability of sea-ice                              + correction from Hunke et al 2012 (flush)
       !!                                     visc = water viscosity
       !!                                     Hp   = height of top of the pond above sea-level
       !!                                     Hi   = ice thickness thru which there is flushing
+      !!                                     flush= correction otherwise flushing is excessive
       !!
       !!              - Corrections:      remove melt ponds when lid thickness is 10 times the pond thickness
@@ -143,12 +259,13 @@
       !!                                  a_ip/a_i = a_ip_frac = h_ip / zaspect
       !!
-      !! ** Tunable parameters : ln_pnd_lids, rn_apnd_max, rn_apnd_min
+      !! ** Tunable parameters : rn_apnd_max, rn_apnd_min, rn_pnd_flush
       !! 
-      !! ** Note       :   mostly stolen from CICE
+      !! ** Note       :   Mostly stolen from CICE but not only. These are between level-ice ponds and CESM ponds. 
       !!
       !! ** References :   Flocco and Feltham (JGR, 2007)
       !!                   Flocco et al       (JGR, 2010)
       !!                   Holland et al      (J. Clim, 2012)
-      !!-------------------------------------------------------------------
+      !!                   Hunke et al        (OM 2012)
+      !!-------------------------------------------------------------------  
       REAL(wp), DIMENSION(nlay_i) ::   ztmp           ! temporary array
       !!
@@ -157,147 +274,1064 @@
       REAL(wp), PARAMETER ::   zvisc   =  1.79e-3_wp  ! water viscosity
       !!
-      REAL(wp) ::   zfr_mlt, zdv_mlt                  ! fraction and volume of available meltwater retained for melt ponding
+      REAL(wp) ::   zfr_mlt, zdv_mlt, zdv_avail       ! fraction and volume of available meltwater retained for melt ponding
       REAL(wp) ::   zdv_frz, zdv_flush                ! Amount of melt pond that freezes, flushes
+      REAL(wp) ::   zdv_pnd                           ! Amount of water going into the ponds & lids
       REAL(wp) ::   zhp                               ! heigh of top of pond lid wrt ssh
       REAL(wp) ::   zv_ip_max                         ! max pond volume allowed
       REAL(wp) ::   zdT                               ! zTp-t_su
-      REAL(wp) ::   zsbr                              ! Brine salinity
+      REAL(wp) ::   zsbr, ztmelts                     ! Brine salinity
       REAL(wp) ::   zperm                             ! permeability of sea ice
       REAL(wp) ::   zfac, zdum                        ! temporary arrays
       REAL(wp) ::   z1_rhow, z1_aspect, z1_Tp         ! inverse
       !!
-      INTEGER  ::   ji, jk                            ! loop indices
+      INTEGER  ::   ji, jk, jl                        ! loop indices
       !!-------------------------------------------------------------------
       z1_rhow   = 1._wp / rhow 
       z1_aspect = 1._wp / zaspect
       z1_Tp     = 1._wp / zTp 
-
-      DO ji = 1, npti
-         !                                                            !----------------------------------------------------!
-         IF( h_i_1d(ji) < rn_himin .OR. a_i_1d(ji) < epsi10 ) THEN    ! Case ice thickness < rn_himin or tiny ice fraction !
-            !                                                         !----------------------------------------------------!
-            !--- Remove ponds on thin ice or tiny ice fractions
-            a_ip_1d(ji)      = 0._wp
-            h_ip_1d(ji)      = 0._wp
-            h_il_1d(ji)      = 0._wp
-            !                                                         !--------------------------------!
-         ELSE                                                         ! Case ice thickness >= rn_himin !
-            !                                                         !--------------------------------!
-            v_ip_1d(ji) = h_ip_1d(ji) * a_ip_1d(ji)   ! retrieve volume from thickness
+      
+      CALL tab_2d_1d( npti, nptidx(1:npti), at_i_1d          (1:npti), at_i          )
+      CALL tab_2d_1d( npti, nptidx(1:npti), wfx_pnd_1d       (1:npti), wfx_pnd       )
+      
+      CALL tab_2d_1d( npti, nptidx(1:npti), diag_dvpn_mlt_1d (1:npti), diag_dvpn_mlt )
+      CALL tab_2d_1d( npti, nptidx(1:npti), diag_dvpn_drn_1d (1:npti), diag_dvpn_drn )
+      CALL tab_2d_1d( npti, nptidx(1:npti), diag_dvpn_lid_1d (1:npti), diag_dvpn_lid )
+      CALL tab_2d_1d( npti, nptidx(1:npti), diag_dvpn_rnf_1d (1:npti), diag_dvpn_rnf )
+
+      DO jl = 1, jpl
+
+         CALL tab_2d_1d( npti, nptidx(1:npti), a_i_1d  (1:npti), a_i (:,:,jl) )
+         CALL tab_2d_1d( npti, nptidx(1:npti), h_i_1d  (1:npti), h_i (:,:,jl) )
+         CALL tab_2d_1d( npti, nptidx(1:npti), t_su_1d (1:npti), t_su(:,:,jl) )
+         CALL tab_2d_1d( npti, nptidx(1:npti), a_ip_1d (1:npti), a_ip(:,:,jl) )
+         CALL tab_2d_1d( npti, nptidx(1:npti), h_ip_1d (1:npti), h_ip(:,:,jl) )
+         CALL tab_2d_1d( npti, nptidx(1:npti), h_il_1d (1:npti), h_il(:,:,jl) )
+
+         CALL tab_2d_1d( npti, nptidx(1:npti), dh_i_sum(1:npti), dh_i_sum_2d(:,:,jl) )
+         CALL tab_2d_1d( npti, nptidx(1:npti), dh_s_mlt(1:npti), dh_s_mlt_2d(:,:,jl) )
+
+         DO jk = 1, nlay_i
+            CALL tab_2d_1d( npti, nptidx(1:npti), sz_i_1d(1:npti,jk), sz_i(:,:,jk,jl) )
+            CALL tab_2d_1d( npti, nptidx(1:npti), t_i_1d (1:npti,jk), t_i (:,:,jk,jl) )
+         END DO
+         
+         !-----------------------
+         ! Melt pond calculations
+         !-----------------------
+         DO ji = 1, npti
+            !
+            zdv_pnd = ( h_ip_1d(ji) + h_il_1d(ji) ) * a_ip_1d(ji)
+            !                                                            !----------------------------------------------------!
+            IF( h_i_1d(ji) < rn_himin .OR. a_i_1d(ji) < 0.01_wp ) THEN   ! Case ice thickness < rn_himin or tiny ice fraction !
+               !                                                         !----------------------------------------------------!
+               !--- Remove ponds on thin ice or tiny ice fractions
+               a_ip_1d(ji) = 0._wp
+               h_ip_1d(ji) = 0._wp
+               h_il_1d(ji) = 0._wp
+               !                                                         !--------------------------------!
+            ELSE                                                         ! Case ice thickness >= rn_himin !
+               !                                                         !--------------------------------!
+               v_ip_1d(ji) = h_ip_1d(ji) * a_ip_1d(ji)   ! retrieve volume from thickness
+               v_il_1d(ji) = h_il_1d(ji) * a_ip_1d(ji)
+               !
+               !------------------!
+               ! case ice melting !
+               !------------------!
+               !
+               !--- available meltwater for melt ponding (zdv_avail) ---!
+               zdv_avail = -( dh_i_sum(ji)*rhoi + dh_s_mlt(ji)*rhos ) * z1_rhow * a_i_1d(ji) ! > 0
+               zfr_mlt   = rn_apnd_min + ( rn_apnd_max - rn_apnd_min ) * at_i_1d(ji) !  = ( 1 - r ) = fraction of melt water that is not flushed
+               zdv_mlt   = MAX( 0._wp, zfr_mlt * zdv_avail ) ! max for roundoff errors? 
+               !
+               !--- overflow ---!
+               !
+               ! area driven overflow
+               !    If pond area exceeds zfr_mlt * a_i_1d(ji) then reduce the pond volume
+               !       a_ip_max = zfr_mlt * a_i
+               !       => from zaspect = h_ip / (a_ip / a_i), set v_ip_max as: 
+               zv_ip_max = zfr_mlt**2 * a_i_1d(ji) * zaspect
+               zdv_mlt   = MAX( 0._wp, MIN( zdv_mlt, zv_ip_max - v_ip_1d(ji) ) )
+
+               ! depth driven overflow
+               !    If pond depth exceeds half the ice thickness then reduce the pond volume
+               !       h_ip_max = 0.5 * h_i
+               !       => from zaspect = h_ip / (a_ip / a_i), set v_ip_max as: 
+               zv_ip_max = z1_aspect * a_i_1d(ji) * 0.25 * h_i_1d(ji) * h_i_1d(ji)
+               zdv_mlt   = MAX( 0._wp, MIN( zdv_mlt, zv_ip_max - v_ip_1d(ji) ) )
+
+               !--- Pond growing ---!
+               v_ip_1d(ji) = v_ip_1d(ji) + zdv_mlt
+               !
+               !--- Lid melting ---!
+               IF( ln_pnd_lids )   v_il_1d(ji) = MAX( 0._wp, v_il_1d(ji) - zdv_mlt ) ! must be bounded by 0
+               !
+               !-------------------!
+               ! case ice freezing ! i.e. t_su_1d(ji) < (zTp+rt0)
+               !-------------------!
+               !
+               zdT = MAX( zTp+rt0 - t_su_1d(ji), 0._wp )
+               !
+               !--- Pond contraction (due to refreezing) ---!
+               IF( ln_pnd_lids ) THEN
+                  !
+                  !--- Lid growing and subsequent pond shrinking ---! 
+                  zdv_frz = - 0.5_wp * MAX( 0._wp, -v_il_1d(ji) + & ! Flocco 2010 (eq. 5) solved implicitly as aH**2 + bH + c = 0
+                     &                    SQRT( v_il_1d(ji)**2 + a_ip_1d(ji)**2 * 4._wp * rcnd_i * zdT * rDt_ice / (rLfus * rhow) ) ) ! max for roundoff errors
+
+                  ! Lid growing
+                  v_il_1d(ji) = MAX( 0._wp, v_il_1d(ji) - zdv_frz )
+
+                  ! Pond shrinking
+                  v_ip_1d(ji) = MAX( 0._wp, v_ip_1d(ji) + zdv_frz )
+
+               ELSE
+                  zdv_frz = v_ip_1d(ji) * ( EXP( 0.01_wp * zdT * z1_Tp ) - 1._wp )  ! Holland 2012 (eq. 6) 
+                  ! Pond shrinking
+                  v_ip_1d(ji) = MAX( 0._wp, v_ip_1d(ji) + zdv_frz )
+               ENDIF
+               !
+               !--- Set new pond area and depth ---! assuming linear relation between h_ip and a_ip_frac
+               ! v_ip     = h_ip * a_ip
+               ! a_ip/a_i = a_ip_frac = h_ip / zaspect (cf Holland 2012, fitting SHEBA so that knowing v_ip we can distribute it to a_ip and h_ip)
+               a_ip_1d(ji) = MIN( a_i_1d(ji), SQRT( v_ip_1d(ji) * z1_aspect * a_i_1d(ji) ) ) ! make sure a_ip < a_i
+               h_ip_1d(ji) = zaspect * a_ip_1d(ji) / a_i_1d(ji)
+               !
+
+               !------------------------------------------------!            
+               ! Pond drainage through brine network (flushing) !
+               !------------------------------------------------!
+               ! height of top of the pond above sea-level
+               zhp = ( h_i_1d(ji) * ( rho0 - rhoi ) + h_ip_1d(ji) * ( rho0 - rhow * a_ip_1d(ji) / a_i_1d(ji) ) ) * r1_rho0
+
+               ! Calculate the permeability of the ice (Assur 1958, see Flocco 2010)
+               DO jk = 1, nlay_i
+                  ! MV Assur is inconsistent with SI3
+                  !!zsbr = - 1.2_wp                                  &
+                  !!   &   - 21.8_wp    * ( t_i_1d(ji,jk) - rt0 )    &
+                  !!   &   - 0.919_wp   * ( t_i_1d(ji,jk) - rt0 )**2 &
+                  !!   &   - 0.0178_wp  * ( t_i_1d(ji,jk) - rt0 )**3
+                  !!ztmp(jk) = sz_i_1d(ji,jk) / zsbr
+                  ! MV linear expression more consistent & simpler: zsbr = - ( t_i_1d(ji,jk) - rt0 ) / rTmlt
+                  ztmelts  = -rTmlt * sz_i_1d(ji,jk)
+                  ztmp(jk) = ztmelts / MIN( ztmelts, t_i_1d(ji,jk) - rt0 )
+               END DO
+               zperm = MAX( 0._wp, 3.e-08_wp * MINVAL(ztmp)**3 )
+
+               ! Do the drainage using Darcy's law
+               zdv_flush   = -zperm * rho0 * grav * zhp * rDt_ice / (zvisc * h_i_1d(ji)) * a_ip_1d(ji) * rn_pnd_flush ! zflush comes from Hunke et al. (2012)
+               zdv_flush   = MAX( zdv_flush, -v_ip_1d(ji) ) ! < 0 
+               v_ip_1d(ji) = v_ip_1d(ji) + zdv_flush
+
+               !--- Set new pond area and depth ---! assuming linear relation between h_ip and a_ip_frac
+               a_ip_1d(ji) = MIN( a_i_1d(ji), SQRT( v_ip_1d(ji) * z1_aspect * a_i_1d(ji) ) ) ! make sure a_ip < a_i
+               h_ip_1d(ji) = zaspect * a_ip_1d(ji) / a_i_1d(ji)
+
+               !--- Corrections and lid thickness ---!
+               IF( ln_pnd_lids ) THEN
+                  !--- retrieve lid thickness from volume ---!
+                  IF( a_ip_1d(ji) > 0.01_wp ) THEN   ;   h_il_1d(ji) = v_il_1d(ji) / a_ip_1d(ji)
+                  ELSE                               ;   h_il_1d(ji) = 0._wp
+                  ENDIF
+                  !--- remove ponds if lids are much larger than ponds ---!
+                  IF ( h_il_1d(ji) > h_ip_1d(ji) * 10._wp ) THEN
+                     a_ip_1d(ji) = 0._wp
+                     h_ip_1d(ji) = 0._wp
+                     h_il_1d(ji) = 0._wp
+                  ENDIF
+               ENDIF
+
+               ! diagnostics: dvpnd = mlt+rnf+lid+drn
+               diag_dvpn_mlt_1d(ji) = diag_dvpn_mlt_1d(ji) + rhow *   zdv_avail             * r1_Dt_ice   ! > 0, surface melt input
+               diag_dvpn_rnf_1d(ji) = diag_dvpn_rnf_1d(ji) + rhow * ( zdv_mlt - zdv_avail ) * r1_Dt_ice   ! < 0, runoff
+               diag_dvpn_lid_1d(ji) = diag_dvpn_lid_1d(ji) + rhow *   zdv_frz               * r1_Dt_ice   ! < 0, shrinking
+               diag_dvpn_drn_1d(ji) = diag_dvpn_drn_1d(ji) + rhow *   zdv_flush             * r1_Dt_ice   ! < 0, drainage
+               !
+            ENDIF
+            !
+            v_ip_1d(ji) = h_ip_1d(ji) * a_ip_1d(ji)
             v_il_1d(ji) = h_il_1d(ji) * a_ip_1d(ji)
             !
-            !------------------!
-            ! case ice melting !
-            !------------------!
+            zdv_pnd = ( h_ip_1d(ji) + h_il_1d(ji) ) * a_ip_1d(ji) - zdv_pnd
+            wfx_pnd_1d(ji) = wfx_pnd_1d(ji) - zdv_pnd * rhow * r1_Dt_ice
             !
-            !--- available meltwater for melt ponding ---!
-            zdum    = -( dh_i_sum(ji)*rhoi + dh_s_mlt(ji)*rhos ) * z1_rhow * a_i_1d(ji)
-            zfr_mlt = rn_apnd_min + ( rn_apnd_max - rn_apnd_min ) * at_i_1d(ji) !  = ( 1 - r ) = fraction of melt water that is not flushed
-            zdv_mlt = MAX( 0._wp, zfr_mlt * zdum ) ! max for roundoff errors? 
-            !
-            !--- overflow ---!
-            ! If pond area exceeds zfr_mlt * a_i_1d(ji) then reduce the pond volume
-            !    a_ip_max = zfr_mlt * a_i
-            !    => from zaspect = h_ip / (a_ip / a_i), set v_ip_max as: 
-            zv_ip_max = zfr_mlt**2 * a_i_1d(ji) * zaspect
-            zdv_mlt = MAX( 0._wp, MIN( zdv_mlt, zv_ip_max - v_ip_1d(ji) ) )
-
-            ! If pond depth exceeds half the ice thickness then reduce the pond volume
-            !    h_ip_max = 0.5 * h_i
-            !    => from zaspect = h_ip / (a_ip / a_i), set v_ip_max as: 
-            zv_ip_max = z1_aspect * a_i_1d(ji) * 0.25 * h_i_1d(ji) * h_i_1d(ji)
-            zdv_mlt = MAX( 0._wp, MIN( zdv_mlt, zv_ip_max - v_ip_1d(ji) ) )
-            
-            !--- Pond growing ---!
-            v_ip_1d(ji) = v_ip_1d(ji) + zdv_mlt
-            !
-            !--- Lid melting ---!
-            IF( ln_pnd_lids )   v_il_1d(ji) = MAX( 0._wp, v_il_1d(ji) - zdv_mlt ) ! must be bounded by 0
-            !
-            !--- mass flux ---!
-            IF( zdv_mlt > 0._wp ) THEN
-               zfac = zdv_mlt * rhow * r1_Dt_ice                        ! melt pond mass flux < 0 [kg.m-2.s-1]
-               wfx_pnd_1d(ji) = wfx_pnd_1d(ji) - zfac
-               !
-               zdum = zfac / ( wfx_snw_sum_1d(ji) + wfx_sum_1d(ji) )    ! adjust ice/snow melting flux > 0 to balance melt pond flux
-               wfx_snw_sum_1d(ji) = wfx_snw_sum_1d(ji) * (1._wp + zdum)
-               wfx_sum_1d(ji)     = wfx_sum_1d(ji)     * (1._wp + zdum)
-            ENDIF
-
-            !-------------------!
-            ! case ice freezing ! i.e. t_su_1d(ji) < (zTp+rt0)
-            !-------------------!
-            !
-            zdT = MAX( zTp+rt0 - t_su_1d(ji), 0._wp )
-            !
-            !--- Pond contraction (due to refreezing) ---!
-            IF( ln_pnd_lids ) THEN
-               !
-               !--- Lid growing and subsequent pond shrinking ---! 
-               zdv_frz = 0.5_wp * MAX( 0._wp, -v_il_1d(ji) + & ! Flocco 2010 (eq. 5) solved implicitly as aH**2 + bH + c = 0
-                  &                    SQRT( v_il_1d(ji)**2 + a_ip_1d(ji)**2 * 4._wp * rcnd_i * zdT * rdt_ice / (rLfus * rhow) ) ) ! max for roundoff errors
-               
-               ! Lid growing
-               v_il_1d(ji) = MAX( 0._wp, v_il_1d(ji) + zdv_frz )
-               
-               ! Pond shrinking
-               v_ip_1d(ji) = MAX( 0._wp, v_ip_1d(ji) - zdv_frz )
-
-            ELSE
-               ! Pond shrinking
-               v_ip_1d(ji) = v_ip_1d(ji) * EXP( 0.01_wp * zdT * z1_Tp ) ! Holland 2012 (eq. 6)
-            ENDIF
-            !
-            !--- Set new pond area and depth ---! assuming linear relation between h_ip and a_ip_frac
-            ! v_ip     = h_ip * a_ip
-            ! a_ip/a_i = a_ip_frac = h_ip / zaspect (cf Holland 2012, fitting SHEBA so that knowing v_ip we can distribute it to a_ip and h_ip)
-            a_ip_1d(ji)      = MIN( a_i_1d(ji), SQRT( v_ip_1d(ji) * z1_aspect * a_i_1d(ji) ) ) ! make sure a_ip < a_i
-            h_ip_1d(ji)      = zaspect * a_ip_1d(ji) / a_i_1d(ji)
-
-            !---------------!            
-            ! Pond flushing !
-            !---------------!
-            ! height of top of the pond above sea-level
-            zhp = ( h_i_1d(ji) * ( rho0 - rhoi ) + h_ip_1d(ji) * ( rho0 - rhow * a_ip_1d(ji) / a_i_1d(ji) ) ) * r1_rho0
-            
-            ! Calculate the permeability of the ice (Assur 1958, see Flocco 2010)
-            DO jk = 1, nlay_i
-               zsbr = - 1.2_wp                                  &
-                  &   - 21.8_wp    * ( t_i_1d(ji,jk) - rt0 )    &
-                  &   - 0.919_wp   * ( t_i_1d(ji,jk) - rt0 )**2 &
-                  &   - 0.0178_wp  * ( t_i_1d(ji,jk) - rt0 )**3
-               ztmp(jk) = sz_i_1d(ji,jk) / zsbr
-            END DO
-            zperm = MAX( 0._wp, 3.e-08_wp * MINVAL(ztmp)**3 )
-            
-            ! Do the drainage using Darcy's law
-            zdv_flush   = -zperm * rho0 * grav * zhp * rdt_ice / (zvisc * h_i_1d(ji)) * a_ip_1d(ji)
-            zdv_flush   = MAX( zdv_flush, -v_ip_1d(ji) )
-            v_ip_1d(ji) = v_ip_1d(ji) + zdv_flush
-            
-            !--- Set new pond area and depth ---! assuming linear relation between h_ip and a_ip_frac
-            a_ip_1d(ji)      = MIN( a_i_1d(ji), SQRT( v_ip_1d(ji) * z1_aspect * a_i_1d(ji) ) ) ! make sure a_ip < a_i
-            h_ip_1d(ji)      = zaspect * a_ip_1d(ji) / a_i_1d(ji)
-
-            !--- Corrections and lid thickness ---!
-            IF( ln_pnd_lids ) THEN
-               !--- retrieve lid thickness from volume ---!
-               IF( a_ip_1d(ji) > epsi10 ) THEN   ;   h_il_1d(ji) = v_il_1d(ji) / a_ip_1d(ji)
-               ELSE                              ;   h_il_1d(ji) = 0._wp
-               ENDIF
-               !--- remove ponds if lids are much larger than ponds ---!
-               IF ( h_il_1d(ji) > h_ip_1d(ji) * 10._wp ) THEN
-                  a_ip_1d(ji)      = 0._wp
-                  h_ip_1d(ji)      = 0._wp
-                  h_il_1d(ji)      = 0._wp
-               ENDIF
-            ENDIF
-            !
-         ENDIF
-         
+         END DO
+
+         !--------------------------------------------------------------------
+         ! Retrieve 2D arrays
+         !--------------------------------------------------------------------
+         CALL tab_1d_2d( npti, nptidx(1:npti), a_ip_1d(1:npti), a_ip(:,:,jl) )
+         CALL tab_1d_2d( npti, nptidx(1:npti), h_ip_1d(1:npti), h_ip(:,:,jl) )
+         CALL tab_1d_2d( npti, nptidx(1:npti), h_il_1d(1:npti), h_il(:,:,jl) )
+         CALL tab_1d_2d( npti, nptidx(1:npti), v_ip_1d(1:npti), v_ip(:,:,jl) )
+         CALL tab_1d_2d( npti, nptidx(1:npti), v_il_1d(1:npti), v_il(:,:,jl) )
+         !
       END DO
       !
+      CALL tab_1d_2d( npti, nptidx(1:npti), wfx_pnd_1d(1:npti), wfx_pnd )
+      !
+      CALL tab_1d_2d( npti, nptidx(1:npti), diag_dvpn_mlt_1d (1:npti), diag_dvpn_mlt        )
+      CALL tab_1d_2d( npti, nptidx(1:npti), diag_dvpn_drn_1d (1:npti), diag_dvpn_drn        )
+      CALL tab_1d_2d( npti, nptidx(1:npti), diag_dvpn_lid_1d (1:npti), diag_dvpn_lid        )
+      CALL tab_1d_2d( npti, nptidx(1:npti), diag_dvpn_rnf_1d (1:npti), diag_dvpn_rnf        )
+      !
    END SUBROUTINE pnd_LEV
 
+
+
+   SUBROUTINE pnd_TOPO    
+                                         
+      !!-------------------------------------------------------------------
+      !!                ***  ROUTINE pnd_TOPO  ***
+      !!
+      !! ** Purpose :   Compute melt pond evolution based on the ice
+      !!                topography inferred from the ice thickness distribution
+      !!
+      !! ** Method  :   This code is initially based on Flocco and Feltham
+      !!                (2007) and Flocco et al. (2010). 
+      !!
+      !!                - Calculate available pond water base on surface meltwater
+      !!                - Redistribute water as a function of topography, drain water
+      !!                - Exchange water with the lid
+      !!
+      !! ** Tunable parameters :
+      !!
+      !! ** Note :
+      !!
+      !! ** References
+      !!
+      !!    Flocco, D. and D. L. Feltham, 2007.  A continuum model of melt pond
+      !!    evolution on Arctic sea ice.  J. Geophys. Res. 112, C08016, doi:
+      !!    10.1029/2006JC003836.
+      !!
+      !!    Flocco, D., D. L. Feltham and A. K. Turner, 2010.  Incorporation of
+      !!    a physically based melt pond scheme into the sea ice component of a
+      !!    climate model.  J. Geophys. Res. 115, C08012,
+      !!    doi: 10.1029/2009JC005568.
+      !!
+      !!-------------------------------------------------------------------
+      REAL(wp), PARAMETER :: &   ! shared parameters for topographic melt ponds
+         zTd     = 0.15_wp       , & ! temperature difference for freeze-up (C)
+         zvp_min = 1.e-4_wp          ! minimum pond volume (m)
+
+
+      ! local variables
+      REAL(wp) :: &
+         zdHui,   &      ! change in thickness of ice lid (m)
+         zomega,  &      ! conduction
+         zdTice,  &      ! temperature difference across ice lid (C)
+         zdvice,  &      ! change in ice volume (m)
+         zTavg,   &      ! mean surface temperature across categories (C)
+         zfsurf,  &      ! net heat flux, excluding conduction and transmitted radiation (W/m2)
+         zTp,     &      ! pond freezing temperature (C)
+         zrhoi_L, &      ! volumetric latent heat of sea ice (J/m^3)
+         zfr_mlt, &      ! fraction and volume of available meltwater retained for melt ponding
+         z1_rhow, &      ! inverse water density
+         zv_pnd  , &     ! volume of meltwater contributing to ponds
+         zv_mlt          ! total amount of meltwater produced
+
+      REAL(wp), DIMENSION(jpi,jpj) ::   zvolp, &     !! total melt pond water available before redistribution and drainage
+                                        zvolp_res    !! remaining melt pond water available after drainage
+                                        
+      REAL(wp), DIMENSION(jpi,jpj,jpl) ::   z1_a_i
+
+      INTEGER  ::   ji, jj, jk, jl                    ! loop indices
+
+      INTEGER  ::   i_test
+
+      ! Note
+      ! equivalent for CICE translation
+      ! a_ip      -> apond
+      ! a_ip_frac -> apnd
+
+      CALL ctl_stop( 'STOP', 'icethd_pnd : topographic melt ponds are still an ongoing work' )
+      
+      !---------------------------------------------------------------
+      ! Initialise
+      !---------------------------------------------------------------
+
+      ! Parameters & constants (move to parameters)
+      zrhoi_L   = rhoi * rLfus      ! volumetric latent heat (J/m^3)
+      zTp       = rt0 - 0.15_wp          ! pond freezing point, slightly below 0C (ponds are bid saline)
+      z1_rhow   = 1._wp / rhow 
+
+      ! Set required ice variables (hard-coded here for now)
+!      zfpond(:,:) = 0._wp          ! contributing freshwater flux (?) 
+      
+      at_i (:,:) = SUM( a_i (:,:,:), dim=3 ) ! ice fraction
+      vt_i (:,:) = SUM( v_i (:,:,:), dim=3 ) ! volume per grid area
+      vt_ip(:,:) = SUM( v_ip(:,:,:), dim=3 ) ! pond volume per grid area
+      vt_il(:,:) = SUM( v_il(:,:,:), dim=3 ) ! lid volume per grid area
+      
+      ! thickness
+      WHERE( a_i(:,:,:) > epsi20 )   ;   z1_a_i(:,:,:) = 1._wp / a_i(:,:,:)
+      ELSEWHERE                      ;   z1_a_i(:,:,:) = 0._wp
+      END WHERE
+      h_i(:,:,:) = v_i (:,:,:) * z1_a_i(:,:,:)
+      
+      !---------------------------------------------------------------
+      ! Change 2D to 1D
+      !---------------------------------------------------------------
+      ! MV 
+      ! a less computing-intensive version would have 2D-1D passage here
+      ! use what we have in iceitd.F90 (incremental remapping)
+
+      !--------------------------------------------------------------
+      ! Collect total available pond water volume
+      !--------------------------------------------------------------
+      ! Assuming that meltwater (+rain in principle) runsoff the surface
+      ! Holland et al (2012) suggest that the fraction of runoff decreases with total ice fraction
+      ! I cite her words, they are very talkative
+      ! "grid cells with very little ice cover (and hence more open water area) 
+      ! have a higher runoff fraction to rep- resent the greater proximity of ice to open water."
+      ! "This results in the same runoff fraction r for each ice category within a grid cell"
+      
+      zvolp(:,:) = 0._wp
+
+      DO jl = 1, jpl
+         DO_2D( 1, 1, 1, 1 )
+                 
+               IF ( a_i(ji,jj,jl) > epsi10 ) THEN
+            
+                  !--- Available and contributing meltwater for melt ponding ---!
+                  zv_mlt  = - ( dh_i_sum_2d(ji,jj,jl) * rhoi + dh_s_mlt_2d(ji,jj,jl) * rhos ) &        ! available volume of surface melt water per grid area
+                     &    * z1_rhow * a_i(ji,jj,jl)
+                      ! MV -> could move this directly in ice_thd_dh and get an array (ji,jj,jl) for surface melt water volume per grid area
+                  zfr_mlt = rn_apnd_min + ( rn_apnd_max - rn_apnd_min ) * at_i(ji,jj)                  ! fraction of surface meltwater going to ponds
+                  zv_pnd  = zfr_mlt * zv_mlt                                                           ! contributing meltwater volume for category jl
+
+                  diag_dvpn_mlt(ji,jj) = diag_dvpn_mlt(ji,jj) + zv_mlt * r1_Dt_ice                     ! diags
+                  diag_dvpn_rnf(ji,jj) = diag_dvpn_rnf(ji,jj) + ( 1. - zfr_mlt ) * zv_mlt * r1_Dt_ice   
+
+                  !--- Create possible new ponds
+                  ! if pond does not exist, create new pond over full ice area
+                  !IF ( a_ip_frac(ji,jj,jl) < epsi10 ) THEN
+                  IF ( a_ip(ji,jj,jl) < epsi10 ) THEN
+                     a_ip(ji,jj,jl)       = a_i(ji,jj,jl)
+                     a_ip_frac(ji,jj,jl)  = 1.0_wp    ! pond fraction of sea ice (apnd for CICE)
+                  ENDIF
+                  
+                  !--- Deepen existing ponds with no change in pond fraction, before redistribution and drainage
+                  v_ip(ji,jj,jl) = v_ip(ji,jj,jl) +  zv_pnd                                            ! use pond water to increase thickness
+                  h_ip(ji,jj,jl) = v_ip(ji,jj,jl) / a_ip(ji,jj,jl)
+                  
+                  !--- Total available pond water volume (pre-existing + newly produced)j
+                  zvolp(ji,jj)   = zvolp(ji,jj)   + v_ip(ji,jj,jl) 
+!                 zfpond(ji,jj) = zfpond(ji,jj) + zpond * a_ip_frac(ji,jj,jl) ! useless for now
+                   
+               ENDIF ! a_i
+
+         END_2D
+      END DO ! ji
+                  
+      !--------------------------------------------------------------
+      ! Redistribute and drain water from ponds
+      !--------------------------------------------------------------   
+      CALL ice_thd_pnd_area( zvolp, zvolp_res )
+                                   
+      !--------------------------------------------------------------
+      ! Melt pond lid growth and melt
+      !--------------------------------------------------------------   
+      
+      IF( ln_pnd_lids ) THEN
+
+         DO_2D( 1, 1, 1, 1 )
+
+            IF ( at_i(ji,jj) > 0.01 .AND. hm_i(ji,jj) > rn_himin .AND. vt_ip(ji,jj) > zvp_min * at_i(ji,jj) ) THEN
+                  
+               !--------------------------
+               ! Pond lid growth and melt
+               !--------------------------
+               ! Mean surface temperature
+               zTavg = 0._wp
+               DO jl = 1, jpl
+                  zTavg = zTavg + t_su(ji,jj,jl)*a_i(ji,jj,jl)
+               END DO
+               zTavg = zTavg / a_i(ji,jj,jl) !!! could get a division by zero here
+         
+               DO jl = 1, jpl-1
+            
+                  IF ( v_il(ji,jj,jl) > epsi10 ) THEN
+               
+                     !----------------------------------------------------------------
+                     ! Lid melting: floating upper ice layer melts in whole or part
+                     !----------------------------------------------------------------
+                     ! Use Tsfc for each category
+                     IF ( t_su(ji,jj,jl) > zTp ) THEN
+
+                        zdvice = MIN( dh_i_sum_2d(ji,jj,jl)*a_ip(ji,jj,jl), v_il(ji,jj,jl) )
+                        
+                        IF ( zdvice > epsi10 ) THEN
+                        
+                           v_il (ji,jj,jl) = v_il (ji,jj,jl)   - zdvice
+                           v_ip(ji,jj,jl)  = v_ip(ji,jj,jl)    + zdvice ! MV: not sure i understand dh_i_sum seems counted twice - 
+                                                                        ! as it is already counted in surface melt
+!                          zvolp(ji,jj)     = zvolp(ji,jj)     + zdvice ! pointless to calculate total volume (done in icevar)
+!                          zfpond(ji,jj)    = fpond(ji,jj)     + zdvice ! pointless to follow fw budget (ponds have no fw)
+                     
+                           IF ( v_il(ji,jj,jl) < epsi10 .AND. v_ip(ji,jj,jl) > epsi10) THEN
+                           ! ice lid melted and category is pond covered
+                              v_ip(ji,jj,jl)  = v_ip(ji,jj,jl)  + v_il(ji,jj,jl) 
+!                             zfpond(ji,jj)    = zfpond (ji,jj)    + v_il(ji,jj,jl) 
+                              v_il(ji,jj,jl)   = 0._wp
+                           ENDIF
+                           h_ip(ji,jj,jl) = v_ip(ji,jj,jl) / a_ip(ji,jj,jl) !!! could get a division by zero here
+                           
+                           diag_dvpn_lid(ji,jj) = diag_dvpn_lid(ji,jj) + zdvice   ! diag
+                           
+                        ENDIF
+                        
+                     !----------------------------------------------------------------
+                     ! Freeze pre-existing lid 
+                     !----------------------------------------------------------------
+
+                     ELSE IF ( v_ip(ji,jj,jl) > epsi10 ) THEN ! Tsfcn(i,j,n) <= Tp
+
+                        ! differential growth of base of surface floating ice layer
+                        zdTice = MAX( - t_su(ji,jj,jl) - zTd , 0._wp ) ! > 0   
+                        zomega = rcnd_i * zdTice / zrhoi_L
+                        zdHui  = SQRT( 2._wp * zomega * rDt_ice + ( v_il(ji,jj,jl) / a_i(ji,jj,jl) )**2 ) &
+                               - v_il(ji,jj,jl) / a_i(ji,jj,jl)
+                        zdvice = min( zdHui*a_ip(ji,jj,jl) , v_ip(ji,jj,jl) )
+                  
+                        IF ( zdvice > epsi10 ) THEN
+                           v_il (ji,jj,jl)  = v_il(ji,jj,jl)   + zdvice
+                           v_ip(ji,jj,jl)   = v_ip(ji,jj,jl)   - zdvice
+!                          zvolp(ji,jj)    = zvolp(ji,jj)     - zdvice
+!                          zfpond(ji,jj)   = zfpond(ji,jj)    - zdvice
+                           h_ip(ji,jj,jl)   = v_ip(ji,jj,jl) / a_ip(ji,jj,jl)
+                           
+                           diag_dvpn_lid(ji,jj) = diag_dvpn_lid(ji,jj) - zdvice    ! diag
+                           
+                        ENDIF
+                  
+                     ENDIF ! Tsfcn(i,j,n)
+
+                  !----------------------------------------------------------------
+                  ! Freeze new lids
+                  !----------------------------------------------------------------
+                  !  upper ice layer begins to form
+                  ! note: albedo does not change
+
+                  ELSE ! v_il < epsi10
+                    
+                     ! thickness of newly formed ice
+                     ! the surface temperature of a meltpond is the same as that
+                     ! of the ice underneath (0C), and the thermodynamic surface 
+                     ! flux is the same
+                     
+                     !!! we need net surface energy flux, excluding conduction
+                     !!! fsurf is summed over categories in CICE
+                     !!! we have the category-dependent flux, let us use it ?
+                     zfsurf = qns_ice(ji,jj,jl) + qsr_ice(ji,jj,jl)                     
+                     zdHui  = MAX ( -zfsurf * rDt_ice/zrhoi_L , 0._wp )
+                     zdvice = MIN ( zdHui * a_ip(ji,jj,jl) , v_ip(ji,jj,jl) )
+                     IF ( zdvice > epsi10 ) THEN
+                        v_il (ji,jj,jl)  = v_il(ji,jj,jl)   + zdvice
+                        v_ip(ji,jj,jl)   = v_ip(ji,jj,jl)   - zdvice
+                        
+                        diag_dvpn_lid(ji,jj) = diag_dvpn_lid(ji,jj) - zdvice      ! diag
+!                       zvolp(ji,jj)     = zvolp(ji,jj)     - zdvice
+!                       zfpond(ji,jj)    = zfpond(ji,jj)    - zdvice
+                        h_ip(ji,jj,jl)   = v_ip(ji,jj,jl) / a_ip(ji,jj,jl) ! MV - in principle, this is useless as h_ip is computed in icevar
+                     ENDIF
+               
+                  ENDIF  ! v_il
+            
+               END DO ! jl
+
+            ELSE  ! remove ponds on thin ice
+
+               v_ip(ji,jj,:) = 0._wp
+               v_il(ji,jj,:) = 0._wp
+!              zfpond(ji,jj) = zfpond(ji,jj)- zvolp(ji,jj)
+!		            zvolp(ji,jj)    = 0._wp         
+
+            ENDIF
+
+         END_2D
+
+      ENDIF ! ln_pnd_lids
+
+      !---------------------------------------------------------------
+      ! Clean-up variables (probably duplicates what icevar would do)
+      !---------------------------------------------------------------
+      ! MV comment
+      ! In the ideal world, the lines above should update only v_ip, a_ip, v_il
+      ! icevar should recompute all other variables (if needed at all)
+
+      DO jl = 1, jpl
+
+         DO_2D( 1, 1, 1, 1 )
+
+!              ! zap lids on small ponds
+!              IF ( a_i(ji,jj,jl) > epsi10 .AND. v_ip(ji,jj,jl) < epsi10 &
+!                                          .AND. v_il(ji,jj,jl) > epsi10) THEN
+!                 v_il(ji,jj,jl) = 0._wp ! probably uselesss now since we get zap_small
+!              ENDIF
+      
+               ! recalculate equivalent pond variables
+               IF ( a_ip(ji,jj,jl) > epsi10) THEN
+                  h_ip(ji,jj,jl)      = v_ip(ji,jj,jl) / a_i(ji,jj,jl)
+                  a_ip_frac(ji,jj,jl) = a_ip(ji,jj,jl) / a_i(ji,jj,jl) ! MV in principle, useless as computed in icevar
+                  h_il(ji,jj,jl) = v_il(ji,jj,jl) / a_ip(ji,jj,jl) ! MV in principle, useless as computed in icevar
+               ENDIF
+!                 h_ip(ji,jj,jl)      = 0._wp ! MV in principle, useless as computed in icevar
+!                 h_il(ji,jj,jl)      = 0._wp ! MV in principle, useless as omputed in icevar
+!              ENDIF
+               
+         END_2D
+
+      END DO   ! jl
+
+
+   END SUBROUTINE pnd_TOPO
+
+   
+   SUBROUTINE ice_thd_pnd_area( zvolp , zdvolp )
+
+       !!-------------------------------------------------------------------
+       !!                ***  ROUTINE ice_thd_pnd_area ***
+       !!
+       !! ** Purpose : Given the total volume of available pond water, 
+       !!              redistribute and drain water
+       !!
+       !! ** Method
+       !!
+       !-----------|
+       !           |
+       !           |-----------|
+       !___________|___________|______________________________________sea-level
+       !           |           |
+       !           |           |---^--------|
+       !           |           |   |        |
+       !           |           |   |        |-----------|              |-------
+       !           |           |   | alfan  |           |              |
+       !           |           |   |        |           |--------------|
+       !           |           |   |        |           |              |
+       !---------------------------v-------------------------------------------
+       !           |           |   ^        |           |              |
+       !           |           |   |        |           |--------------|
+       !           |           |   | betan  |           |              |
+       !           |           |   |        |-----------|              |-------
+       !           |           |   |        |
+       !           |           |---v------- |
+       !           |           |
+       !           |-----------|
+       !           |
+       !-----------|
+       !
+       !!
+       !!------------------------------------------------------------------
+       
+       REAL (wp), DIMENSION(jpi,jpj), INTENT(INOUT) :: &
+          zvolp,                                       &  ! total available pond water
+          zdvolp                                          ! remaining meltwater after redistribution
+
+       INTEGER ::  &
+          ns,      &
+          m_index, &
+          permflag
+
+       REAL (wp), DIMENSION(jpl) :: &
+          hicen, &
+          hsnon, &
+          asnon, &
+          alfan, &
+          betan, &
+          cum_max_vol, &
+          reduced_aicen
+
+       REAL (wp), DIMENSION(0:jpl) :: &
+          cum_max_vol_tmp
+
+       REAL (wp) :: &
+          hpond, &
+          drain, &
+          floe_weight, &
+          pressure_head, &
+          hsl_rel, &
+          deltah, &
+          perm, &
+          msno
+
+       REAL (wp), parameter :: &
+          viscosity = 1.79e-3_wp     ! kinematic water viscosity in kg/m/s
+
+      REAL(wp), PARAMETER :: &   ! shared parameters for topographic melt ponds
+         zvp_min = 1.e-4_wp          ! minimum pond volume (m)
+
+      INTEGER  ::   ji, jj, jk, jl                    ! loop indices
+
+       a_ip(:,:,:) = 0._wp
+       h_ip(:,:,:) = 0._wp
+ 
+       DO_2D( 1, 1, 1, 1 )
+ 
+             IF ( at_i(ji,jj) > 0.01 .AND. hm_i(ji,jj) > rn_himin .AND. zvolp(ji,jj) > zvp_min * at_i(ji,jj) ) THEN
+ 
+        !-------------------------------------------------------------------
+        ! initialize
+        !-------------------------------------------------------------------
+ 
+        DO jl = 1, jpl
+ 
+           !----------------------------------------
+           ! compute the effective snow fraction
+           !----------------------------------------
+ 
+           IF (a_i(ji,jj,jl) < epsi10)  THEN
+              hicen(jl) =  0._wp
+              hsnon(jl) =  0._wp
+              reduced_aicen(jl) = 0._wp
+              asnon(jl) = 0._wp         !js: in CICE 5.1.2: make sense as the compiler may not initiate the variables
+           ELSE
+              hicen(jl) = v_i(ji,jj,jl) / a_i(ji,jj,jl)
+              hsnon(jl) = v_s(ji,jj,jl) / a_i(ji,jj,jl)
+              reduced_aicen(jl) = 1._wp ! n=jpl
+ 
+              !js: initial code in NEMO_DEV
+              !IF (n < jpl) reduced_aicen(jl) = aicen(jl) &
+              !                     * (-0.024_wp*hicen(jl) + 0.832_wp)
+ 
+              !js: from CICE 5.1.2: this limit reduced_aicen to 0.2 when hicen is too large
+              IF (jl < jpl) reduced_aicen(jl) = a_i(ji,jj,jl) & 
+                                   * max(0.2_wp,(-0.024_wp*hicen(jl) + 0.832_wp))
+ 
+              asnon(jl) = reduced_aicen(jl)  ! effective snow fraction (empirical)
+              ! MV should check whether this makes sense to have the same effective snow fraction in here
+              ! OLI: it probably doesn't
+           END IF
+ 
+  ! This choice for alfa and beta ignores hydrostatic equilibium of categories.
+  ! Hydrostatic equilibium of the entire ITD is accounted for below, assuming
+  ! a surface topography implied by alfa=0.6 and beta=0.4, and rigidity across all
+  ! categories.  alfa and beta partition the ITD - they are areas not thicknesses!
+  ! Multiplying by hicen, alfan and betan (below) are thus volumes per unit area.
+  ! Here, alfa = 60% of the ice area (and since hice is constant in a category,
+  ! alfan = 60% of the ice volume) in each category lies above the reference line,
+  ! and 40% below. Note: p6 is an arbitrary choice, but alfa+beta=1 is required.
+ 
+  ! MV:
+  ! Note that this choice is not in the original FF07 paper and has been adopted in CICE
+  ! No reason why is explained in the doc, but I guess there is a reason. I'll try to investigate, maybe
+ 
+  ! Where does that choice come from ? => OLI : Coz' Chuck Norris said so...
+ 
+           alfan(jl) = 0.6 * hicen(jl)
+           betan(jl) = 0.4 * hicen(jl)
+ 
+           cum_max_vol(jl)     = 0._wp
+           cum_max_vol_tmp(jl) = 0._wp
+ 
+        END DO ! jpl
+ 
+        cum_max_vol_tmp(0) = 0._wp
+        drain = 0._wp
+        zdvolp(ji,jj) = 0._wp
+ 
+        !----------------------------------------------------------
+        ! Drain overflow water, update pond fraction and volume
+        !----------------------------------------------------------
+ 
+        !--------------------------------------------------------------------------
+        ! the maximum amount of water that can be contained up to each ice category
+        !--------------------------------------------------------------------------
+        ! If melt ponds are too deep to be sustainable given the ITD (OVERFLOW)
+        ! Then the excess volume cum_max_vol(jl) drains out of the system
+        ! It should be added to wfx_pnd_out
+ 
+        DO jl = 1, jpl-1 ! last category can not hold any volume
+ 
+           IF (alfan(jl+1) >= alfan(jl) .AND. alfan(jl+1) > 0._wp ) THEN
+ 
+              ! total volume in level including snow
+              cum_max_vol_tmp(jl) = cum_max_vol_tmp(jl-1) + &
+                 (alfan(jl+1) - alfan(jl)) * sum(reduced_aicen(1:jl))
+ 
+              ! subtract snow solid volumes from lower categories in current level
+              DO ns = 1, jl
+                 cum_max_vol_tmp(jl) = cum_max_vol_tmp(jl) &
+                    - rhos/rhow * &     ! free air fraction that can be filled by water
+                      asnon(ns)  * &    ! effective areal fraction of snow in that category
+                      max(min(hsnon(ns)+alfan(ns)-alfan(jl), alfan(jl+1)-alfan(jl)), 0._wp)
+              END DO
+ 
+           ELSE ! assume higher categories unoccupied
+              cum_max_vol_tmp(jl) = cum_max_vol_tmp(jl-1)
+           END IF
+           !IF (cum_max_vol_tmp(jl) < z0) THEN
+           !   CALL abort_ice('negative melt pond volume')
+           !END IF
+        END DO
+        cum_max_vol_tmp(jpl) = cum_max_vol_tmp(jpl-1)  ! last category holds no volume
+        cum_max_vol  (1:jpl) = cum_max_vol_tmp(1:jpl)
+ 
+        !----------------------------------------------------------------
+        ! is there more meltwater than can be held in the floe?
+        !----------------------------------------------------------------
+        IF (zvolp(ji,jj) >= cum_max_vol(jpl)) THEN
+           drain = zvolp(ji,jj) - cum_max_vol(jpl) + epsi10
+           zvolp(ji,jj) = zvolp(ji,jj) - drain ! update meltwater volume available
+ 
+           diag_dvpn_rnf(ji,jj) = - drain      ! diag - overflow counted in the runoff part (arbitrary choice)
+           
+           zdvolp(ji,jj) = drain         ! this is the drained water
+           IF (zvolp(ji,jj) < epsi10) THEN
+              zdvolp(ji,jj) = zdvolp(ji,jj) + zvolp(ji,jj)
+              zvolp(ji,jj) = 0._wp
+           END IF
+        END IF
+ 
+        ! height and area corresponding to the remaining volume
+        ! routine leaves zvolp unchanged
+        CALL ice_thd_pnd_depth(reduced_aicen, asnon, hsnon, alfan, zvolp(ji,jj), cum_max_vol, hpond, m_index)
+ 
+        DO jl = 1, m_index
+           !h_ip(jl) = hpond - alfan(jl) + alfan(1) ! here oui choulde update
+           !                                         !  volume instead, no ?
+           h_ip(ji,jj,jl) = max((hpond - alfan(jl) + alfan(1)), 0._wp)      !js: from CICE 5.1.2
+           a_ip(ji,jj,jl) = reduced_aicen(jl)
+           ! in practise, pond fraction depends on the empirical snow fraction
+           ! so in turn on ice thickness
+        END DO
+        !zapond = sum(a_ip(1:m_index))     !js: from CICE 5.1.2; not in Icepack1.1.0-6-gac6195d
+ 
+        !------------------------------------------------------------------------
+        ! Drainage through brine network (permeability)
+        !------------------------------------------------------------------------
+        !!! drainage due to ice permeability - Darcy's law
+ 
+        ! sea water level
+        msno = 0._wp 
+        DO jl = 1 , jpl
+          msno = msno + v_s(ji,jj,jl) * rhos
+        END DO
+        floe_weight = ( msno + rhoi*vt_i(ji,jj) + rho0*zvolp(ji,jj) ) / at_i(ji,jj)
+        hsl_rel = floe_weight / rho0 &
+                - ( ( sum(betan(:)*a_i(ji,jj,:)) / at_i(ji,jj) ) + alfan(1) )
+ 
+        deltah = hpond - hsl_rel
+        pressure_head = grav * rho0 * max(deltah, 0._wp)
+ 
+        ! drain if ice is permeable
+        permflag = 0
+ 
+        IF (pressure_head > 0._wp) THEN
+           DO jl = 1, jpl-1
+              IF ( hicen(jl) /= 0._wp ) THEN
+ 
+              !IF (hicen(jl) > 0._wp) THEN           !js: from CICE 5.1.2
+ 
+                 perm = 0._wp ! MV ugly dummy patch
+                 CALL ice_thd_pnd_perm(t_i(ji,jj,:,jl),  sz_i(ji,jj,:,jl), perm) ! bof 
+                 IF (perm > 0._wp) permflag = 1
+ 
+                 drain = perm*a_ip(ji,jj,jl)*pressure_head*rDt_ice / &
+                                          (viscosity*hicen(jl))
+                 zdvolp(ji,jj) = zdvolp(ji,jj) + min(drain, zvolp(ji,jj))
+                 zvolp(ji,jj) = max(zvolp(ji,jj) - drain, 0._wp)
+ 
+                 diag_dvpn_drn(ji,jj) = - drain ! diag (could be better coded)
+                 
+                 IF (zvolp(ji,jj) < epsi10) THEN
+                    zdvolp(ji,jj) = zdvolp(ji,jj) + zvolp(ji,jj)
+                    zvolp(ji,jj) = 0._wp
+                 END IF
+             END IF
+          END DO
+ 
+           ! adjust melt pond dimensions
+           IF (permflag > 0) THEN
+              ! recompute pond depth
+             CALL ice_thd_pnd_depth(reduced_aicen, asnon, hsnon, alfan, zvolp(ji,jj), cum_max_vol, hpond, m_index)
+              DO jl = 1, m_index
+                 h_ip(ji,jj,jl) = hpond - alfan(jl) + alfan(1)
+                 a_ip(ji,jj,jl) = reduced_aicen(jl)
+              END DO
+              !zapond = sum(a_ip(1:m_index))       !js: from CICE 5.1.2; not in Icepack1.1.0-6-gac6195d
+           END IF
+        END IF ! pressure_head
+ 
+        !-------------------------------
+        ! remove water from the snow
+        !-------------------------------
+        !------------------------------------------------------------------------
+        ! total melt pond volume in category does not include snow volume
+        ! snow in melt ponds is not melted
+        !------------------------------------------------------------------------
+        
+        ! MV here, it seems that we remove some meltwater from the ponds, but I can't really tell
+        ! how much, so I did not diagnose it
+        ! so if there is a problem here, nobody is going to see it...
+        
+ 
+        ! Calculate pond volume for lower categories
+        DO jl = 1,m_index-1
+           v_ip(ji,jj,jl) = a_ip(ji,jj,jl) * h_ip(ji,jj,jl) & ! what is not in the snow
+                          - (rhos/rhow) * asnon(jl) * min(hsnon(jl), h_ip(ji,jj,jl))
+        END DO
+ 
+        ! Calculate pond volume for highest category = remaining pond volume
+ 
+        ! The following is completely unclear to Martin at least
+        ! Could we redefine properly and recode in a more readable way ?
+ 
+        ! m_index = last category with melt pond
+ 
+        IF (m_index == 1) v_ip(ji,jj,m_index) = zvolp(ji,jj) ! volume of mw in 1st category is the total volume of melt water
+ 
+        IF (m_index > 1) THEN
+          IF (zvolp(ji,jj) > sum( v_ip(ji,jj,1:m_index-1))) THEN
+             v_ip(ji,jj,m_index) = zvolp(ji,jj) - sum(v_ip(ji,jj,1:m_index-1))
+          ELSE
+             v_ip(ji,jj,m_index) = 0._wp 
+             h_ip(ji,jj,m_index) = 0._wp
+             a_ip(ji,jj,m_index) = 0._wp
+             ! If remaining pond volume is negative reduce pond volume of
+             ! lower category
+             IF ( zvolp(ji,jj) + epsi10 < SUM(v_ip(ji,jj,1:m_index-1))) &
+              v_ip(ji,jj,m_index-1) = v_ip(ji,jj,m_index-1) - sum(v_ip(ji,jj,1:m_index-1)) + zvolp(ji,jj)
+          END IF
+        END IF
+ 
+        DO jl = 1,m_index
+           IF (a_ip(ji,jj,jl) > epsi10) THEN
+               h_ip(ji,jj,jl) = v_ip(ji,jj,jl) / a_ip(ji,jj,jl)
+           ELSE
+              zdvolp(ji,jj) = zdvolp(ji,jj) + v_ip(ji,jj,jl)
+              h_ip(ji,jj,jl) = 0._wp 
+              v_ip(ji,jj,jl)  = 0._wp
+              a_ip(ji,jj,jl) = 0._wp
+           END IF
+        END DO
+        DO jl = m_index+1, jpl
+           h_ip(ji,jj,jl) = 0._wp 
+           a_ip(ji,jj,jl) = 0._wp 
+           v_ip(ji,jj,jl) = 0._wp 
+        END DO
+        
+           ENDIF
+
+     END_2D
+
+    END SUBROUTINE ice_thd_pnd_area
+
+
+    SUBROUTINE ice_thd_pnd_depth(aicen, asnon, hsnon, alfan, zvolp, cum_max_vol, hpond, m_index)
+       !!-------------------------------------------------------------------
+       !!                ***  ROUTINE ice_thd_pnd_depth  ***
+       !!
+       !! ** Purpose :   Compute melt pond depth
+       !!-------------------------------------------------------------------
+
+       REAL (wp), DIMENSION(jpl), INTENT(IN) :: &
+          aicen, &
+          asnon, &
+          hsnon, &
+          alfan, &
+          cum_max_vol
+
+       REAL (wp), INTENT(IN) :: &
+          zvolp
+
+       REAL (wp), INTENT(OUT) :: &
+          hpond
+
+       INTEGER, INTENT(OUT) :: &
+          m_index
+
+       INTEGER :: n, ns
+
+       REAL (wp), DIMENSION(0:jpl+1) :: &
+          hitl, &
+          aicetl
+
+       REAL (wp) :: &
+          rem_vol, &
+          area, &
+          vol, &
+          tmp, &
+          z0   = 0.0_wp
+
+       !----------------------------------------------------------------
+       ! hpond is zero if zvolp is zero - have we fully drained?
+       !----------------------------------------------------------------
+
+       IF (zvolp < epsi10) THEN
+        hpond = z0
+        m_index = 0
+       ELSE
+
+        !----------------------------------------------------------------
+        ! Calculate the category where water fills up to
+        !----------------------------------------------------------------
+
+        !----------|
+        !          |
+        !          |
+        !          |----------|                                     -- --
+        !__________|__________|_________________________________________ ^
+        !          |          |             rem_vol     ^                | Semi-filled
+        !          |          |----------|-- -- -- - ---|-- ---- -- -- --v layer
+        !          |          |          |              |
+        !          |          |          |              |hpond
+        !          |          |          |----------|   |     |-------
+        !          |          |          |          |   |     |
+        !          |          |          |          |---v-----|
+        !          |          | m_index  |          |         |
+        !-------------------------------------------------------------
+
+        m_index = 0  ! 1:m_index categories have water in them
+        DO n = 1, jpl
+           IF (zvolp <= cum_max_vol(n)) THEN
+              m_index = n
+              IF (n == 1) THEN
+                 rem_vol = zvolp
+              ELSE
+                 rem_vol = zvolp - cum_max_vol(n-1)
+              END IF
+              exit ! to break out of the loop
+           END IF
+        END DO
+        m_index = min(jpl-1, m_index)
+
+        !----------------------------------------------------------------
+        ! semi-filled layer may have m_index different snow in it
+        !----------------------------------------------------------------
+
+        !-----------------------------------------------------------  ^
+        !                                                             |  alfan(m_index+1)
+        !                                                             |
+        !hitl(3)-->                             |----------|          |
+        !hitl(2)-->                |------------| * * * * *|          |
+        !hitl(1)-->     |----------|* * * * * * |* * * * * |          |
+        !hitl(0)-->-------------------------------------------------  |  ^
+        !                various snow from lower categories          |  |alfa(m_index)
+
+        ! hitl - heights of the snow layers from thinner and current categories
+        ! aicetl - area of each snow depth in this layer
+
+        hitl(:) = z0
+        aicetl(:) = z0
+        DO n = 1, m_index
+           hitl(n)   = max(min(hsnon(n) + alfan(n) - alfan(m_index), &
+                                  alfan(m_index+1) - alfan(m_index)), z0)
+           aicetl(n) = asnon(n)
+
+           aicetl(0) = aicetl(0) + (aicen(n) - asnon(n))
+        END DO
+
+        hitl(m_index+1) = alfan(m_index+1) - alfan(m_index)
+        aicetl(m_index+1) = z0
+
+        !----------------------------------------------------------------
+        ! reorder array according to hitl
+        ! snow heights not necessarily in height order
+        !----------------------------------------------------------------
+
+        DO ns = 1, m_index+1
+           DO n = 0, m_index - ns + 1
+              IF (hitl(n) > hitl(n+1)) THEN ! swap order
+                 tmp = hitl(n)
+                 hitl(n) = hitl(n+1)
+                 hitl(n+1) = tmp
+                 tmp = aicetl(n)
+                 aicetl(n) = aicetl(n+1)
+                 aicetl(n+1) = tmp
+              END IF
+           END DO
+        END DO
+
+        !----------------------------------------------------------------
+        ! divide semi-filled layer into set of sublayers each vertically homogenous
+        !----------------------------------------------------------------
+
+        !hitl(3)----------------------------------------------------------------
+        !                                                       | * * * * * * * *
+        !                                                       |* * * * * * * * *
+        !hitl(2)----------------------------------------------------------------
+        !                                    | * * * * * * * *  | * * * * * * * *
+        !                                    |* * * * * * * * * |* * * * * * * * *
+        !hitl(1)----------------------------------------------------------------
+        !                 | * * * * * * * *  | * * * * * * * *  | * * * * * * * *
+        !                 |* * * * * * * * * |* * * * * * * * * |* * * * * * * * *
+        !hitl(0)----------------------------------------------------------------
+        !    aicetl(0)         aicetl(1)           aicetl(2)          aicetl(3)
+
+        ! move up over layers incrementing volume
+        DO n = 1, m_index+1
+
+           area = sum(aicetl(:)) - &                 ! total area of sub-layer
+                (rhos/rho0) * sum(aicetl(n:jpl+1)) ! area of sub-layer occupied by snow
+
+           vol = (hitl(n) - hitl(n-1)) * area      ! thickness of sub-layer times area
+
+           IF (vol >= rem_vol) THEN  ! have reached the sub-layer with the depth within
+              hpond = rem_vol / area + hitl(n-1) + alfan(m_index) - alfan(1)
+
+              exit
+           ELSE  ! still in sub-layer below the sub-layer with the depth
+              rem_vol = rem_vol - vol
+           END IF
+
+        END DO
+
+       END IF
+
+    END SUBROUTINE ice_thd_pnd_depth
+
+
+    SUBROUTINE ice_thd_pnd_perm(ticen, salin, perm)
+       !!-------------------------------------------------------------------
+       !!                ***  ROUTINE ice_thd_pnd_perm ***
+       !!
+       !! ** Purpose :   Determine the liquid fraction of brine in the ice
+       !!                and its permeability
+       !!-------------------------------------------------------------------
+
+       REAL (wp), DIMENSION(nlay_i), INTENT(IN) :: &
+          ticen,  &     ! internal ice temperature (K)
+          salin         ! salinity (ppt)     !js: ppt according to cice
+
+       REAL (wp), INTENT(OUT) :: &
+          perm      ! permeability
+
+       REAL (wp) ::   &
+          Sbr       ! brine salinity
+
+       REAL (wp), DIMENSION(nlay_i) ::   &
+          Tin, &    ! ice temperature
+          phi       ! liquid fraction
+
+       INTEGER :: k
+
+       !-----------------------------------------------------------------
+       ! Compute ice temperatures from enthalpies using quadratic formula
+       !-----------------------------------------------------------------
+
+       DO k = 1,nlay_i
+          Tin(k) = ticen(k) - rt0   !js: from K to degC
+       END DO
+
+       !-----------------------------------------------------------------
+       ! brine salinity and liquid fraction
+       !-----------------------------------------------------------------
+
+       DO k = 1, nlay_i
+       
+          Sbr    = - Tin(k) / rTmlt ! Consistent expression with SI3 (linear liquidus)
+          ! Best expression to date is that one (Vancoppenolle et al JGR 2019)
+          ! Sbr  = - 18.7 * Tin(k) - 0.519 * Tin(k)**2 - 0.00535 * Tin(k) **3
+          phi(k) = salin(k) / Sbr
+          
+       END DO
+
+       !-----------------------------------------------------------------
+       ! permeability
+       !-----------------------------------------------------------------
+
+       perm = 3.0e-08_wp * (minval(phi))**3 ! Golden et al. (2007)
+
+   END SUBROUTINE ice_thd_pnd_perm
 
    SUBROUTINE ice_thd_pnd_init 
@@ -315,6 +1349,7 @@
       INTEGER  ::   ios, ioptio   ! Local integer
       !!
-      NAMELIST/namthd_pnd/  ln_pnd, ln_pnd_LEV , rn_apnd_min, rn_apnd_max, &
+      NAMELIST/namthd_pnd/  ln_pnd, ln_pnd_LEV , rn_apnd_min, rn_apnd_max, rn_pnd_flush, &
          &                          ln_pnd_CST , rn_apnd, rn_hpnd,         &
+         &                          ln_pnd_TOPO,                           &
          &                          ln_pnd_lids, ln_pnd_alb
       !!-------------------------------------------------------------------
@@ -332,7 +1367,9 @@
          WRITE(numout,*) '   Namelist namicethd_pnd:'
          WRITE(numout,*) '      Melt ponds activated or not                                 ln_pnd       = ', ln_pnd
+         WRITE(numout,*) '         Topographic melt pond scheme                             ln_pnd_TOPO  = ', ln_pnd_TOPO
          WRITE(numout,*) '         Level ice melt pond scheme                               ln_pnd_LEV   = ', ln_pnd_LEV
          WRITE(numout,*) '            Minimum ice fraction that contributes to melt ponds   rn_apnd_min  = ', rn_apnd_min
          WRITE(numout,*) '            Maximum ice fraction that contributes to melt ponds   rn_apnd_max  = ', rn_apnd_max
+         WRITE(numout,*) '            Pond flushing efficiency                              rn_pnd_flush = ', rn_pnd_flush
          WRITE(numout,*) '         Constant ice melt pond scheme                            ln_pnd_CST   = ', ln_pnd_CST
          WRITE(numout,*) '            Prescribed pond fraction                              rn_apnd      = ', rn_apnd
@@ -347,6 +1384,7 @@
       IF( ln_pnd_CST  ) THEN   ;   ioptio = ioptio + 1   ;   nice_pnd = np_pndCST    ;   ENDIF
       IF( ln_pnd_LEV  ) THEN   ;   ioptio = ioptio + 1   ;   nice_pnd = np_pndLEV    ;   ENDIF
+      IF( ln_pnd_TOPO ) THEN   ;   ioptio = ioptio + 1   ;   nice_pnd = np_pndTOPO   ;   ENDIF
       IF( ioptio /= 1 )   &
-         & CALL ctl_stop( 'ice_thd_pnd_init: choose either none (ln_pnd=F) or only one pond scheme (ln_pnd_LEV or ln_pnd_CST)' )
+         & CALL ctl_stop( 'ice_thd_pnd_init: choose either none (ln_pnd=F) or only one pond scheme (ln_pnd_LEV, ln_pnd_CST or ln_pnd_TOPO)' )
       !
       SELECT CASE( nice_pnd )
Index: /NEMO/branches/2020/tickets_icb_1900/src/ICE/icethd_zdf_bl99.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/ICE/icethd_zdf_bl99.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/ICE/icethd_zdf_bl99.F90	(revision 14016)
@@ -109,29 +109,29 @@
       REAL(wp), DIMENSION(jpij) ::   zdqns_ice_b  ! derivative of the surface flux function
       !
-      REAL(wp), DIMENSION(jpij       )     ::   ztsuold     ! Old surface temperature in the ice
-      REAL(wp), DIMENSION(jpij,nlay_i)     ::   ztiold      ! Old temperature in the ice
-      REAL(wp), DIMENSION(jpij,nlay_s)     ::   ztsold      ! Old temperature in the snow
-      REAL(wp), DIMENSION(jpij,nlay_i)     ::   ztib        ! Temporary temperature in the ice to check the convergence
-      REAL(wp), DIMENSION(jpij,nlay_s)     ::   ztsb        ! Temporary temperature in the snow to check the convergence
-      REAL(wp), DIMENSION(jpij,0:nlay_i)   ::   ztcond_i    ! Ice thermal conductivity
-      REAL(wp), DIMENSION(jpij,0:nlay_i)   ::   ztcond_i_cp ! copy
-      REAL(wp), DIMENSION(jpij,0:nlay_i)   ::   zradtr_i    ! Radiation transmitted through the ice
-      REAL(wp), DIMENSION(jpij,0:nlay_i)   ::   zradab_i    ! Radiation absorbed in the ice
-      REAL(wp), DIMENSION(jpij,0:nlay_i)   ::   zkappa_i    ! Kappa factor in the ice
-      REAL(wp), DIMENSION(jpij,0:nlay_i)   ::   zeta_i      ! Eta factor in the ice
-      REAL(wp), DIMENSION(jpij,0:nlay_s)   ::   zradtr_s    ! Radiation transmited through the snow
-      REAL(wp), DIMENSION(jpij,0:nlay_s)   ::   zradab_s    ! Radiation absorbed in the snow
-      REAL(wp), DIMENSION(jpij,0:nlay_s)   ::   zkappa_s    ! Kappa factor in the snow
-      REAL(wp), DIMENSION(jpij,0:nlay_s)   ::   zeta_s      ! Eta factor in the snow
-      REAL(wp), DIMENSION(jpij)            ::   zkappa_comb ! Combined snow and ice surface conductivity
-      REAL(wp), DIMENSION(jpij,nlay_i+3)   ::   zindterm    ! 'Ind'ependent term
-      REAL(wp), DIMENSION(jpij,nlay_i+3)   ::   zindtbis    ! Temporary 'ind'ependent term
-      REAL(wp), DIMENSION(jpij,nlay_i+3)   ::   zdiagbis    ! Temporary 'dia'gonal term
-      REAL(wp), DIMENSION(jpij,nlay_i+3,3) ::   ztrid       ! Tridiagonal system terms
-      REAL(wp), DIMENSION(jpij)            ::   zq_ini      ! diag errors on heat
-      REAL(wp), DIMENSION(jpij)            ::   zghe        ! G(he), th. conduct enhancement factor, mono-cat
-      REAL(wp), DIMENSION(jpij)            ::   za_s_fra    ! ice fraction covered by snow 
-      REAL(wp), DIMENSION(jpij)            ::   isnow       ! snow presence (1) or not (0) 
-      REAL(wp), DIMENSION(jpij)            ::   isnow_comb  ! snow presence for met-office 
+      REAL(wp), DIMENSION(jpij       )   ::   ztsuold     ! Old surface temperature in the ice
+      REAL(wp), DIMENSION(jpij,nlay_i)   ::   ztiold      ! Old temperature in the ice
+      REAL(wp), DIMENSION(jpij,nlay_s)   ::   ztsold      ! Old temperature in the snow
+      REAL(wp), DIMENSION(jpij,nlay_i)   ::   ztib        ! Temporary temperature in the ice to check the convergence
+      REAL(wp), DIMENSION(jpij,nlay_s)   ::   ztsb        ! Temporary temperature in the snow to check the convergence
+      REAL(wp), DIMENSION(jpij,0:nlay_i) ::   ztcond_i    ! Ice thermal conductivity
+      REAL(wp), DIMENSION(jpij,0:nlay_i) ::   ztcond_i_cp ! copy
+      REAL(wp), DIMENSION(jpij,0:nlay_i) ::   zradtr_i    ! Radiation transmitted through the ice
+      REAL(wp), DIMENSION(jpij,0:nlay_i) ::   zradab_i    ! Radiation absorbed in the ice
+      REAL(wp), DIMENSION(jpij,0:nlay_i) ::   zkappa_i    ! Kappa factor in the ice
+      REAL(wp), DIMENSION(jpij,0:nlay_i) ::   zeta_i      ! Eta factor in the ice
+      REAL(wp), DIMENSION(jpij,0:nlay_s) ::   zradtr_s    ! Radiation transmited through the snow
+      REAL(wp), DIMENSION(jpij,0:nlay_s) ::   zradab_s    ! Radiation absorbed in the snow
+      REAL(wp), DIMENSION(jpij,0:nlay_s) ::   zkappa_s    ! Kappa factor in the snow
+      REAL(wp), DIMENSION(jpij,0:nlay_s) ::   zeta_s      ! Eta factor in the snow
+      REAL(wp), DIMENSION(jpij)          ::   zkappa_comb ! Combined snow and ice surface conductivity
+      REAL(wp), DIMENSION(jpij)          ::   zq_ini      ! diag errors on heat
+      REAL(wp), DIMENSION(jpij)          ::   zghe        ! G(he), th. conduct enhancement factor, mono-cat
+      REAL(wp), DIMENSION(jpij)          ::   za_s_fra    ! ice fraction covered by snow 
+      REAL(wp), DIMENSION(jpij)          ::   isnow       ! snow presence (1) or not (0) 
+      REAL(wp), DIMENSION(jpij)          ::   isnow_comb  ! snow presence for met-office 
+      REAL(wp), DIMENSION(jpij,nlay_i+nlay_s+1)   ::   zindterm    ! 'Ind'ependent term
+      REAL(wp), DIMENSION(jpij,nlay_i+nlay_s+1)   ::   zindtbis    ! Temporary 'ind'ependent term
+      REAL(wp), DIMENSION(jpij,nlay_i+nlay_s+1)   ::   zdiagbis    ! Temporary 'dia'gonal term
+      REAL(wp), DIMENSION(jpij,nlay_i+nlay_s+1,3) ::   ztrid       ! Tridiagonal system terms
       !
       ! Mono-category
@@ -533,14 +533,24 @@
             ! Solve the tridiagonal system with Gauss elimination method.
             ! Thomas algorithm, from Computational fluid Dynamics, J.D. ANDERSON, McGraw-Hill 1984
-            jm_maxt = 0
-            jm_mint = nlay_i+5
-            DO ji = 1, npti
-               jm_mint = MIN(jm_min(ji),jm_mint)
-               jm_maxt = MAX(jm_max(ji),jm_maxt)
-            END DO
-
-            DO jk = jm_mint+1, jm_maxt
-               DO ji = 1, npti
-                  jm = MIN(MAX(jm_min(ji)+1,jk),jm_max(ji))
+!!$            jm_maxt = 0
+!!$            jm_mint = nlay_i+5
+!!$            DO ji = 1, npti
+!!$               jm_mint = MIN(jm_min(ji),jm_mint)
+!!$               jm_maxt = MAX(jm_max(ji),jm_maxt)
+!!$            END DO
+!!$            !!clem SNWLAY => check why LIM1D does not get this loop. Is nlay_i+5 correct?
+!!$            
+!!$            DO jk = jm_mint+1, jm_maxt
+!!$               DO ji = 1, npti
+!!$                  jm = MIN(MAX(jm_min(ji)+1,jk),jm_max(ji))
+!!$                  zdiagbis(ji,jm) = ztrid   (ji,jm,2) - ztrid(ji,jm,1) * ztrid   (ji,jm-1,3) / zdiagbis(ji,jm-1)
+!!$                  zindtbis(ji,jm) = zindterm(ji,jm  ) - ztrid(ji,jm,1) * zindtbis(ji,jm-1  ) / zdiagbis(ji,jm-1)
+!!$               END DO
+!!$            END DO
+            ! clem: maybe one should find a way to reverse this loop for mpi performance
+            DO ji = 1, npti
+               jm_mint = jm_min(ji)
+               jm_maxt = jm_max(ji)
+               DO jm = jm_mint+1, jm_maxt
                   zdiagbis(ji,jm) = ztrid   (ji,jm,2) - ztrid(ji,jm,1) * ztrid   (ji,jm-1,3) / zdiagbis(ji,jm-1)
                   zindtbis(ji,jm) = zindterm(ji,jm  ) - ztrid(ji,jm,1) * zindtbis(ji,jm-1  ) / zdiagbis(ji,jm-1)
@@ -564,21 +574,30 @@
             END DO
 
+            ! snow temperatures      
             DO ji = 1, npti
                ! Variables used after iterations
                ! Value must be frozen after convergence for MPP independance reason
-               IF ( .NOT. l_T_converged(ji) ) THEN
-                  ! snow temperatures      
-                  IF( h_s_1d(ji) > 0._wp ) THEN
-                     t_s_1d(ji,nlay_s) = ( zindtbis(ji,nlay_s+1) - ztrid(ji,nlay_s+1,3) * t_i_1d(ji,1) ) / zdiagbis(ji,nlay_s+1)
-                  ENDIF
-                  ! surface temperature
+               IF ( .NOT. l_T_converged(ji) .AND. h_s_1d(ji) > 0._wp ) &
+                  &   t_s_1d(ji,nlay_s) = ( zindtbis(ji,nlay_s+1) - ztrid(ji,nlay_s+1,3) * t_i_1d(ji,1) ) / zdiagbis(ji,nlay_s+1)
+            END DO
+            !!clem SNWLAY
+            DO jm = nlay_s, 2, -1
+               DO ji = 1, npti
+                  jk = jm - 1
+                  IF ( .NOT. l_T_converged(ji) .AND. h_s_1d(ji) > 0._wp ) &
+                     &   t_s_1d(ji,jk) = ( zindtbis(ji,jm) - ztrid(ji,jm,3) * t_s_1d(ji,jk+1) ) / zdiagbis(ji,jm)
+               END DO
+            END DO
+            
+            ! surface temperature
+            DO ji = 1, npti
+               IF( .NOT. l_T_converged(ji) ) THEN
                   ztsub(ji) = t_su_1d(ji)
                   IF( t_su_1d(ji) < rt0 ) THEN
-                     t_su_1d(ji) = (  zindtbis(ji,jm_min(ji)) - ztrid(ji,jm_min(ji),3) *  &
-                        &           ( isnow(ji) * t_s_1d(ji,1) + ( 1._wp - isnow(ji) ) *  t_i_1d(ji,1) ) ) / zdiagbis(ji,jm_min(ji))
+                     t_su_1d(ji) = ( zindtbis(ji,jm_min(ji)) - ztrid(ji,jm_min(ji),3) *  &
+                        &          ( isnow(ji) * t_s_1d(ji,1) + ( 1._wp - isnow(ji) ) * t_i_1d(ji,1) ) ) / zdiagbis(ji,jm_min(ji))
                   ENDIF
                ENDIF
             END DO
-            !clem: in order to have several layers of snow, there is a missing loop here for t_s_1d(1:nlay_s-1)
             !
             !--------------------------------------------------------------
@@ -727,19 +746,28 @@
             ! Solve the tridiagonal system with Gauss elimination method.
             ! Thomas algorithm, from Computational fluid Dynamics, J.D. ANDERSON, McGraw-Hill 1984
-            jm_maxt = 0
-            jm_mint = nlay_i+5
-            DO ji = 1, npti
-               jm_mint = MIN(jm_min(ji),jm_mint)
-               jm_maxt = MAX(jm_max(ji),jm_maxt)
-            END DO
-            
-            DO jk = jm_mint+1, jm_maxt
-               DO ji = 1, npti
-                  jm = MIN(MAX(jm_min(ji)+1,jk),jm_max(ji))
+!!$            jm_maxt = 0
+!!$            jm_mint = nlay_i+5
+!!$            DO ji = 1, npti
+!!$               jm_mint = MIN(jm_min(ji),jm_mint)
+!!$               jm_maxt = MAX(jm_max(ji),jm_maxt)
+!!$            END DO
+!!$            
+!!$            DO jk = jm_mint+1, jm_maxt
+!!$               DO ji = 1, npti
+!!$                  jm = MIN(MAX(jm_min(ji)+1,jk),jm_max(ji))
+!!$                  zdiagbis(ji,jm) = ztrid   (ji,jm,2) - ztrid(ji,jm,1) * ztrid   (ji,jm-1,3) / zdiagbis(ji,jm-1)
+!!$                  zindtbis(ji,jm) = zindterm(ji,jm)   - ztrid(ji,jm,1) * zindtbis(ji,jm-1)   / zdiagbis(ji,jm-1)
+!!$               END DO
+!!$            END DO
+            ! clem: maybe one should find a way to reverse this loop for mpi performance
+            DO ji = 1, npti
+               jm_mint = jm_min(ji)
+               jm_maxt = jm_max(ji)
+               DO jm = jm_mint+1, jm_maxt
                   zdiagbis(ji,jm) = ztrid   (ji,jm,2) - ztrid(ji,jm,1) * ztrid   (ji,jm-1,3) / zdiagbis(ji,jm-1)
-                  zindtbis(ji,jm) = zindterm(ji,jm)   - ztrid(ji,jm,1) * zindtbis(ji,jm-1)   / zdiagbis(ji,jm-1)
+                  zindtbis(ji,jm) = zindterm(ji,jm  ) - ztrid(ji,jm,1) * zindtbis(ji,jm-1  ) / zdiagbis(ji,jm-1)
                END DO
             END DO
-            
+
             ! ice temperatures
             DO ji = 1, npti
@@ -761,13 +789,17 @@
             ! snow temperatures      
             DO ji = 1, npti
-               ! Variable used after iterations
+               ! Variables used after iterations
                ! Value must be frozen after convergence for MPP independance reason
-               IF ( .NOT. l_T_converged(ji) ) THEN
-                  IF( h_s_1d(ji) > 0._wp ) THEN
-                     t_s_1d(ji,nlay_s) = ( zindtbis(ji,nlay_s+1) - ztrid(ji,nlay_s+1,3) * t_i_1d(ji,1) ) / zdiagbis(ji,nlay_s+1)
-                  ENDIF
-               ENDIF
-            END DO
-            !clem: in order to have several layers of snow, there is a missing loop here for t_s_1d(1:nlay_s-1)
+               IF ( .NOT. l_T_converged(ji) .AND. h_s_1d(ji) > 0._wp ) &
+                  &   t_s_1d(ji,nlay_s) = ( zindtbis(ji,nlay_s+1) - ztrid(ji,nlay_s+1,3) * t_i_1d(ji,1) ) / zdiagbis(ji,nlay_s+1)
+            END DO
+            !!clem SNWLAY
+            DO jm = nlay_s, 2, -1
+               DO ji = 1, npti
+                  jk = jm - 1
+                  IF ( .NOT. l_T_converged(ji) .AND. h_s_1d(ji) > 0._wp ) &
+                     &   t_s_1d(ji,jk) = ( zindtbis(ji,jm) - ztrid(ji,jm,3) * t_s_1d(ji,jk+1) ) / zdiagbis(ji,jm)
+               END DO
+            END DO
             !
             !--------------------------------------------------------------
@@ -923,6 +955,6 @@
          !--- Snow-ice interfacial temperature (diagnostic SIMIP)
          IF( h_s_1d(ji) >= zhs_ssl ) THEN
-            t_si_1d(ji) = (   rn_cnd_s       * h_i_1d(ji) * r1_nlay_i * t_s_1d(ji,1)   &
-               &            + ztcond_i(ji,1) * h_s_1d(ji) * r1_nlay_s * t_i_1d(ji,1) ) &
+            t_si_1d(ji) = (   rn_cnd_s       * h_i_1d(ji) * r1_nlay_i * t_s_1d(ji,nlay_s)   &
+               &            + ztcond_i(ji,1) * h_s_1d(ji) * r1_nlay_s * t_i_1d(ji,1)      ) &
                &          / ( rn_cnd_s       * h_i_1d(ji) * r1_nlay_i &
                &            + ztcond_i(ji,1) * h_s_1d(ji) * r1_nlay_s )
Index: /NEMO/branches/2020/tickets_icb_1900/src/ICE/iceupdate.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/ICE/iceupdate.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/ICE/iceupdate.F90	(revision 14016)
@@ -94,5 +94,5 @@
       REAL(wp), DIMENSION(jpi,jpj) ::   z2d                  ! 2D workspace
       !!---------------------------------------------------------------------
-      IF( ln_timing )   CALL timing_start('ice_update')
+      IF( ln_timing )   CALL timing_start('iceupdate')
 
       IF( kt == nit000 .AND. lwp ) THEN
@@ -154,5 +154,5 @@
          ! ice-ocean  mass flux
          wfx_ice(ji,jj) = wfx_bog(ji,jj) + wfx_bom(ji,jj) + wfx_sum(ji,jj) + wfx_sni(ji,jj)   &
-            &           + wfx_opw(ji,jj) + wfx_dyn(ji,jj) + wfx_res(ji,jj) + wfx_lam(ji,jj) + wfx_pnd(ji,jj)
+            &           + wfx_opw(ji,jj) + wfx_dyn(ji,jj) + wfx_res(ji,jj) + wfx_lam(ji,jj)
          
          ! snw-ocean mass flux
@@ -160,6 +160,6 @@
          
          ! total mass flux at the ocean/ice interface
-         fmmflx(ji,jj) =                - wfx_ice(ji,jj) - wfx_snw(ji,jj) - wfx_err_sub(ji,jj)   ! ice-ocean mass flux saved at least for biogeochemical model
-         emp   (ji,jj) = emp_oce(ji,jj) - wfx_ice(ji,jj) - wfx_snw(ji,jj) - wfx_err_sub(ji,jj)   ! atm-ocean + ice-ocean mass flux
+         fmmflx(ji,jj) =                - wfx_ice(ji,jj) - wfx_snw(ji,jj) - wfx_pnd(ji,jj) - wfx_err_sub(ji,jj)   ! ice-ocean mass flux saved at least for biogeochemical model
+         emp   (ji,jj) = emp_oce(ji,jj) - wfx_ice(ji,jj) - wfx_snw(ji,jj) - wfx_pnd(ji,jj) - wfx_err_sub(ji,jj)   ! atm-ocean + ice-ocean mass flux
 
          ! Salt flux at the ocean surface      
@@ -172,5 +172,5 @@
          snwice_mass_b(ji,jj) = snwice_mass(ji,jj)       ! save mass from the previous ice time step
          !                                               ! new mass per unit area
-         snwice_mass  (ji,jj) = tmask(ji,jj,1) * ( rhos * vt_s(ji,jj) + rhoi * vt_i(ji,jj)  ) 
+         snwice_mass  (ji,jj) = tmask(ji,jj,1) * ( rhos * vt_s(ji,jj) + rhoi * vt_i(ji,jj) + rhow * (vt_ip(ji,jj) + vt_il(ji,jj)) ) 
          !                                               ! time evolution of snow+ice mass
          snwice_fmass (ji,jj) = ( snwice_mass(ji,jj) - snwice_mass_b(ji,jj) ) * r1_Dt_ice
@@ -286,5 +286,5 @@
       IF( ln_icectl         )   CALL ice_prt       (kt, iiceprt, jiceprt, 3, 'Final state ice_update') ! prints
       IF( sn_cfctl%l_prtctl )   CALL ice_prt3D     ('iceupdate')                                       ! prints
-      IF( ln_timing         )   CALL timing_stop   ('ice_update')                                      ! timing
+      IF( ln_timing         )   CALL timing_stop   ('iceupdate')                                       ! timing
       !
    END SUBROUTINE ice_update_flx
@@ -324,5 +324,5 @@
       REAL(wp) ::   zflagi                          !   -      -
       !!---------------------------------------------------------------------
-      IF( ln_timing )   CALL timing_start('ice_update_tau')
+      IF( ln_timing )   CALL timing_start('ice_update')
 
       IF( kt == nit000 .AND. lwp ) THEN
@@ -376,5 +376,5 @@
       CALL lbc_lnk_multi( 'iceupdate', utau, 'U', -1.0_wp, vtau, 'V', -1.0_wp )   ! lateral boundary condition
       !
-      IF( ln_timing )   CALL timing_stop('ice_update_tau')
+      IF( ln_timing )   CALL timing_stop('ice_update')
       !  
    END SUBROUTINE ice_update_tau
Index: /NEMO/branches/2020/tickets_icb_1900/src/ICE/icevar.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/ICE/icevar.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/ICE/icevar.F90	(revision 14016)
@@ -236,5 +236,5 @@
       z1_zhmax =  1._wp / hi_max(jpl)               
       WHERE( h_i(:,:,jpl) > zhmax )   ! bound h_i by hi_max (i.e. 99 m) with associated update of ice area
-         h_i  (:,:,jpl) = zhmax
+         h_i   (:,:,jpl) = zhmax
          a_i   (:,:,jpl) = v_i(:,:,jpl) * z1_zhmax 
          z1_a_i(:,:,jpl) = zhmax * z1_v_i(:,:,jpl)
@@ -252,5 +252,5 @@
       ELSEWHERE( h_il(:,:,:) >= zhl_max )  ;   a_ip_eff(:,:,:) = 0._wp                  ! lid is very thick. Cover all the pond up with ice and snow
       ELSEWHERE                            ;   a_ip_eff(:,:,:) = a_ip_frac(:,:,:) * &   ! lid is in between. Expose part of the pond
-         &                                                       ( h_il(:,:,:) - zhl_min ) / ( zhl_max - zhl_min )
+         &                                                       ( zhl_max - h_il(:,:,:) ) / ( zhl_max - zhl_min )
       END WHERE
       !
@@ -534,7 +534,8 @@
          DO_2D( 1, 1, 1, 1 )
             ! update exchanges with ocean
-            sfx_res(ji,jj)  = sfx_res(ji,jj) + (1._wp - zswitch(ji,jj) ) * sv_i(ji,jj,jl)   * rhoi * r1_Dt_ice
-            wfx_res(ji,jj)  = wfx_res(ji,jj) + (1._wp - zswitch(ji,jj) ) * v_i (ji,jj,jl)   * rhoi * r1_Dt_ice
-            wfx_res(ji,jj)  = wfx_res(ji,jj) + (1._wp - zswitch(ji,jj) ) * v_s (ji,jj,jl)   * rhos * r1_Dt_ice
+            sfx_res(ji,jj)  = sfx_res(ji,jj) + ( 1._wp - zswitch(ji,jj) ) * sv_i(ji,jj,jl)   * rhoi * r1_Dt_ice
+            wfx_res(ji,jj)  = wfx_res(ji,jj) + ( 1._wp - zswitch(ji,jj) ) * v_i (ji,jj,jl)   * rhoi * r1_Dt_ice
+            wfx_res(ji,jj)  = wfx_res(ji,jj) + ( 1._wp - zswitch(ji,jj) ) * v_s (ji,jj,jl)   * rhos * r1_Dt_ice
+            wfx_pnd(ji,jj)  = wfx_pnd(ji,jj) + ( 1._wp - zswitch(ji,jj) ) * ( v_ip(ji,jj,jl)+v_il(ji,jj,jl) ) * rhow * r1_Dt_ice
             !
             a_i  (ji,jj,jl) = a_i (ji,jj,jl) * zswitch(ji,jj)
@@ -551,4 +552,6 @@
             v_ip (ji,jj,jl) = v_ip (ji,jj,jl) * zswitch(ji,jj)
             v_il (ji,jj,jl) = v_il (ji,jj,jl) * zswitch(ji,jj)
+            h_ip (ji,jj,jl) = h_ip (ji,jj,jl) * zswitch(ji,jj)
+            h_il (ji,jj,jl) = h_il (ji,jj,jl) * zswitch(ji,jj)
             !
          END_2D
@@ -635,4 +638,12 @@
                psv_i  (ji,jj,jl) = 0._wp
             ENDIF
+            IF( pv_ip(ji,jj,jl) < 0._wp .OR. pv_il(ji,jj,jl) < 0._wp .OR. pa_ip(ji,jj,jl) <= 0._wp ) THEN
+               wfx_pnd(ji,jj)    = wfx_pnd(ji,jj) + pv_il(ji,jj,jl) * rhow * z1_dt
+               pv_il  (ji,jj,jl) = 0._wp
+            ENDIF
+            IF( pv_ip(ji,jj,jl) < 0._wp .OR. pa_ip(ji,jj,jl) <= 0._wp ) THEN
+               wfx_pnd(ji,jj)    = wfx_pnd(ji,jj) + pv_ip(ji,jj,jl) * rhow * z1_dt
+               pv_ip  (ji,jj,jl) = 0._wp
+            ENDIF
          END_2D
          !
@@ -643,6 +654,4 @@
       WHERE( pa_i  (:,:,:) < 0._wp )   pa_i  (:,:,:) = 0._wp
       WHERE( pa_ip (:,:,:) < 0._wp )   pa_ip (:,:,:) = 0._wp
-      WHERE( pv_ip (:,:,:) < 0._wp )   pv_ip (:,:,:) = 0._wp ! in theory one should change wfx_pnd(-) and wfx_sum(+)
-      WHERE( pv_il (:,:,:) < 0._wp )   pv_il (:,:,:) = 0._wp !    but it does not change conservation, so keep it this way is ok
       !
    END SUBROUTINE ice_var_zapneg
@@ -675,5 +684,5 @@
       WHERE( pe_i (1:npti,:,:) < 0._wp )   pe_i (1:npti,:,:) = 0._wp   !  e_i must be >= 0
       WHERE( pe_s (1:npti,:,:) < 0._wp )   pe_s (1:npti,:,:) = 0._wp   !  e_s must be >= 0
-      IF( ln_pnd_LEV ) THEN
+      IF( ln_pnd_LEV .OR. ln_pnd_TOPO ) THEN
          WHERE( pa_ip(1:npti,:) < 0._wp )    pa_ip(1:npti,:)   = 0._wp   ! a_ip must be >= 0
          WHERE( pv_ip(1:npti,:) < 0._wp )    pv_ip(1:npti,:)   = 0._wp   ! v_ip must be >= 0
Index: /NEMO/branches/2020/tickets_icb_1900/src/ICE/icewri.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/ICE/icewri.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/ICE/icewri.F90	(revision 14016)
@@ -160,7 +160,8 @@
       IF( iom_use('icebrv_cat'  ) )   CALL iom_put( 'icebrv_cat'  ,   bv_i * 100.  * zmsk00l + zmiss_val * ( 1._wp - zmsk00l ) ) ! brine volume
       IF( iom_use('iceapnd_cat' ) )   CALL iom_put( 'iceapnd_cat' ,   a_ip         * zmsk00l                                   ) ! melt pond frac for categories
+      IF( iom_use('icevpnd_cat' ) )   CALL iom_put( 'icevpnd_cat' ,   v_ip         * zmsk00l                                   ) ! melt pond volume for categories
       IF( iom_use('icehpnd_cat' ) )   CALL iom_put( 'icehpnd_cat' ,   h_ip         * zmsk00l + zmiss_val * ( 1._wp - zmsk00l ) ) ! melt pond thickness for categories
       IF( iom_use('icehlid_cat' ) )   CALL iom_put( 'icehlid_cat' ,   h_il         * zmsk00l + zmiss_val * ( 1._wp - zmsk00l ) ) ! melt pond lid thickness for categories
-      IF( iom_use('iceafpnd_cat') )   CALL iom_put( 'iceafpnd_cat',   a_ip_frac    * zmsk00l                                   ) ! melt pond frac for categories
+      IF( iom_use('iceafpnd_cat') )   CALL iom_put( 'iceafpnd_cat',   a_ip_frac    * zmsk00l                                   ) ! melt pond frac per ice area for categories
       IF( iom_use('iceaepnd_cat') )   CALL iom_put( 'iceaepnd_cat',   a_ip_eff     * zmsk00l                                   ) ! melt pond effective frac for categories
       IF( iom_use('icealb_cat'  ) )   CALL iom_put( 'icealb_cat'  ,   alb_ice      * zmsk00l + zmiss_val * ( 1._wp - zmsk00l ) ) ! ice albedo for categories
Index: /NEMO/branches/2020/tickets_icb_1900/src/OCE/C1D/step_c1d.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/OCE/C1D/step_c1d.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/OCE/C1D/step_c1d.F90	(revision 14016)
@@ -104,4 +104,5 @@
       IF( ln_tradmp )   CALL tra_dmp( kstp, Nbb, Nnn, ts, Nrhs  )  ! internal damping trends- tracers
       IF(.NOT.ln_linssh)CALL tra_adv( kstp, Nbb, Nnn, ts, Nrhs  )  ! horizontal & vertical advection
+      IF( ln_zdfmfc  )  CALL tra_mfc( kstp, Nbb     , ts, Nrhs  )  ! Mass Flux Convection
       IF( ln_zdfosm  )  CALL tra_osm( kstp, Nnn     , ts, Nrhs  )  ! OSMOSIS non-local tracer fluxes
                         CALL tra_zdf( kstp, Nbb, Nnn, Nrhs, ts, Naa   )         ! vertical mixing
@@ -122,6 +123,6 @@
                         CALL dyn_atf    ( kstp, Nbb, Nnn, Naa , uu, vv, e3t, e3u, e3v )  ! time filtering of "now" fields
       IF(.NOT.ln_linssh)CALL ssh_atf    ( kstp, Nbb, Nnn, Naa , ssh )                    ! time filtering of "now" sea surface height
-      IF( kstp == nit000 .AND. ln_linssh) THEN
-         ssh(:,:,Naa) = ssh(:,:,Nnn)  ! init ssh after in ln_linssh case
+      IF( kstp == nit000 .AND. ln_linssh) THEN 
+         ssh(:,:,Naa) = ssh(:,:,Nnn)  ! init ssh after in ln_linssh case 
       ENDIF
       !
Index: /NEMO/branches/2020/tickets_icb_1900/src/OCE/DYN/dynspg.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/OCE/DYN/dynspg.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/OCE/DYN/dynspg.F90	(revision 14016)
@@ -6,4 +6,6 @@
    !! History :  1.0  ! 2005-12  (C. Talandier, G. Madec, V. Garnier)  Original code
    !!            3.2  ! 2009-07  (R. Benshila)  Suppression of rigid-lid option
+   !!            4.2  ! 2020-12  (G. Madec, E. Clementi) add Bernoulli Head for
+   !!                            wave coupling
    !!----------------------------------------------------------------------
 
@@ -19,4 +21,5 @@
    USE sbc_ice , ONLY : snwice_mass, snwice_mass_b
    USE sbcapr         ! surface boundary condition: atmospheric pressure
+   USE sbcwave,  ONLY : bhd_wave
    USE dynspg_exp     ! surface pressure gradient     (dyn_spg_exp routine)
    USE dynspg_ts      ! surface pressure gradient     (dyn_spg_ts  routine)
@@ -143,4 +146,11 @@
          ENDIF
          !
+         IF( ln_wave .and. ln_bern_srfc ) THEN          !== Add J terms: depth-independent Bernoulli head
+            DO_2D( 0, 0, 0, 0 )
+               spgu(ji,jj) = spgu(ji,jj) + ( bhd_wave(ji+1,jj) - bhd_wave(ji,jj) ) / e1u(ji,jj)   !++ bhd_wave from wave model in m2/s2 [BHD parameters in WW3]
+               spgv(ji,jj) = spgv(ji,jj) + ( bhd_wave(ji,jj+1) - bhd_wave(ji,jj) ) / e2v(ji,jj)
+            END_2D
+         ENDIF
+         !
          DO_3D( 0, 0, 0, 0, 1, jpkm1 )       !== Add all terms to the general trend
             puu(ji,jj,jk,Krhs) = puu(ji,jj,jk,Krhs) + spgu(ji,jj)
Index: /NEMO/branches/2020/tickets_icb_1900/src/OCE/DYN/dynvor.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/OCE/DYN/dynvor.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/OCE/DYN/dynvor.F90	(revision 14016)
@@ -21,4 +21,5 @@
    !!             -   ! 2018-03  (G. Madec)  add two new schemes (ln_dynvor_enT and ln_dynvor_eet)
    !!             -   ! 2018-04  (G. Madec)  add pre-computed gradient for metric term calculation
+   !!            4.2  ! 2020-12  (G. Madec, E. Clementi) add vortex force trends (ln_vortex_force=T)
    !!----------------------------------------------------------------------
 
@@ -37,5 +38,5 @@
    USE trddyn         ! trend manager: dynamics
    USE sbcwave        ! Surface Waves (add Stokes-Coriolis force)
-   USE sbc_oce , ONLY : ln_stcor    ! use Stoke-Coriolis force
+   USE sbc_oce,  ONLY : ln_stcor, ln_vortex_force   ! use Stoke-Coriolis force
    !
    USE lbclnk         ! ocean lateral boundary conditions (or mpp link)
@@ -121,17 +122,12 @@
          ALLOCATE( ztrdu(jpi,jpj,jpk), ztrdv(jpi,jpj,jpk) )
          !
-         ztrdu(:,:,:) = puu(:,:,:,Krhs)            !* planetary vorticity trend (including Stokes-Coriolis force)
+         ztrdu(:,:,:) = puu(:,:,:,Krhs)            !* planetary vorticity trend
          ztrdv(:,:,:) = pvv(:,:,:,Krhs)
          SELECT CASE( nvor_scheme )
          CASE( np_ENS )           ;   CALL vor_ens( kt, Kmm, ncor, puu(:,:,:,Kmm) , pvv(:,:,:,Kmm) , puu(:,:,:,Krhs), pvv(:,:,:,Krhs) )   ! enstrophy conserving scheme
-            IF( ln_stcor )            CALL vor_ens( kt, Kmm, ncor, usd, vsd, puu(:,:,:,Krhs), pvv(:,:,:,Krhs) )   ! add the Stokes-Coriolis trend
          CASE( np_ENE, np_MIX )   ;   CALL vor_ene( kt, Kmm, ncor, puu(:,:,:,Kmm) , pvv(:,:,:,Kmm) , puu(:,:,:,Krhs), pvv(:,:,:,Krhs) )   ! energy conserving scheme
-            IF( ln_stcor )            CALL vor_ene( kt, Kmm, ncor, usd, vsd, puu(:,:,:,Krhs), pvv(:,:,:,Krhs) )   ! add the Stokes-Coriolis trend
          CASE( np_ENT )           ;   CALL vor_enT( kt, Kmm, ncor, puu(:,:,:,Kmm) , pvv(:,:,:,Kmm) , puu(:,:,:,Krhs), pvv(:,:,:,Krhs) )   ! energy conserving scheme (T-pts)
-            IF( ln_stcor )            CALL vor_enT( kt, Kmm, ncor, usd, vsd, puu(:,:,:,Krhs), pvv(:,:,:,Krhs) )   ! add the Stokes-Coriolis trend
          CASE( np_EET )           ;   CALL vor_eeT( kt, Kmm, ncor, puu(:,:,:,Kmm) , pvv(:,:,:,Kmm) , puu(:,:,:,Krhs), pvv(:,:,:,Krhs) )   ! energy conserving scheme (een with e3t)
-            IF( ln_stcor )            CALL vor_eeT( kt, Kmm, ncor, usd, vsd, puu(:,:,:,Krhs), pvv(:,:,:,Krhs) )   ! add the Stokes-Coriolis trend
          CASE( np_EEN )           ;   CALL vor_een( kt, Kmm, ncor, puu(:,:,:,Kmm) , pvv(:,:,:,Kmm) , puu(:,:,:,Krhs), pvv(:,:,:,Krhs) )   ! energy & enstrophy scheme
-            IF( ln_stcor )            CALL vor_een( kt, Kmm, ncor, usd, vsd, puu(:,:,:,Krhs), pvv(:,:,:,Krhs) )   ! add the Stokes-Coriolis trend
          END SELECT
          ztrdu(:,:,:) = puu(:,:,:,Krhs) - ztrdu(:,:,:)
@@ -161,21 +157,43 @@
          CASE( np_ENT )                        !* energy conserving scheme  (T-pts)
                              CALL vor_enT( kt, Kmm, ntot, puu(:,:,:,Kmm) , pvv(:,:,:,Kmm) , puu(:,:,:,Krhs), pvv(:,:,:,Krhs) )   ! total vorticity trend
-            IF( ln_stcor )   CALL vor_enT( kt, Kmm, ncor, usd, vsd, puu(:,:,:,Krhs), pvv(:,:,:,Krhs) )   ! add the Stokes-Coriolis trend
+            IF( ln_stcor .AND. .NOT. ln_vortex_force )  THEN
+                             CALL vor_enT( kt, Kmm, ncor, usd, vsd, puu(:,:,:,Krhs), pvv(:,:,:,Krhs) )   ! add the Stokes-Coriolis trend 
+            ELSE IF( ln_stcor .AND. ln_vortex_force )   THEN
+                             CALL vor_enT( kt, Kmm, ntot, usd, vsd, puu(:,:,:,Krhs), pvv(:,:,:,Krhs) )   ! add the Stokes-Coriolis trend and vortex force
+            ENDIF
          CASE( np_EET )                        !* energy conserving scheme (een scheme using e3t)
                              CALL vor_eeT( kt, Kmm, ntot, puu(:,:,:,Kmm) , pvv(:,:,:,Kmm) , puu(:,:,:,Krhs), pvv(:,:,:,Krhs) )   ! total vorticity trend
-            IF( ln_stcor )   CALL vor_eeT( kt, Kmm, ncor, usd, vsd, puu(:,:,:,Krhs), pvv(:,:,:,Krhs) )   ! add the Stokes-Coriolis trend
+            IF( ln_stcor .AND. .NOT. ln_vortex_force )  THEN
+                             CALL vor_eeT( kt, Kmm, ncor, usd, vsd, puu(:,:,:,Krhs), pvv(:,:,:,Krhs) )   ! add the Stokes-Coriolis trend
+            ELSE IF( ln_stcor .AND. ln_vortex_force )   THEN
+                             CALL vor_eeT( kt, Kmm, ntot, usd, vsd, puu(:,:,:,Krhs), pvv(:,:,:,Krhs) )   ! add the Stokes-Coriolis trend and vortex force
+            ENDIF
          CASE( np_ENE )                        !* energy conserving scheme
                              CALL vor_ene( kt, Kmm, ntot, puu(:,:,:,Kmm) , pvv(:,:,:,Kmm) , puu(:,:,:,Krhs), pvv(:,:,:,Krhs) )   ! total vorticity trend
-            IF( ln_stcor )   CALL vor_ene( kt, Kmm, ncor, usd, vsd, puu(:,:,:,Krhs), pvv(:,:,:,Krhs) )   ! add the Stokes-Coriolis trend
+            IF( ln_stcor .AND. .NOT. ln_vortex_force )  THEN
+                             CALL vor_ene( kt, Kmm, ncor, usd, vsd, puu(:,:,:,Krhs), pvv(:,:,:,Krhs) )   ! add the Stokes-Coriolis trend
+            ELSE IF( ln_stcor .AND. ln_vortex_force )   THEN
+                             CALL vor_ene( kt, Kmm, ntot, usd, vsd, puu(:,:,:,Krhs), pvv(:,:,:,Krhs) )   ! add the Stokes-Coriolis trend and vortex force
+            ENDIF
          CASE( np_ENS )                        !* enstrophy conserving scheme
                              CALL vor_ens( kt, Kmm, ntot, puu(:,:,:,Kmm) , pvv(:,:,:,Kmm) , puu(:,:,:,Krhs), pvv(:,:,:,Krhs) )  ! total vorticity trend
-            IF( ln_stcor )   CALL vor_ens( kt, Kmm, ncor, usd, vsd, puu(:,:,:,Krhs), pvv(:,:,:,Krhs) )  ! add the Stokes-Coriolis trend
+
+            IF( ln_stcor .AND. .NOT. ln_vortex_force )  THEN
+                             CALL vor_ens( kt, Kmm, ncor, usd, vsd, puu(:,:,:,Krhs), pvv(:,:,:,Krhs) )  ! add the Stokes-Coriolis trend
+            ELSE IF( ln_stcor .AND. ln_vortex_force )   THEN
+                             CALL vor_ens( kt, Kmm, ntot, usd, vsd, puu(:,:,:,Krhs), pvv(:,:,:,Krhs) )  ! add the Stokes-Coriolis trend and vortex force
+            ENDIF
          CASE( np_MIX )                        !* mixed ene-ens scheme
                              CALL vor_ens( kt, Kmm, nrvm, puu(:,:,:,Kmm) , pvv(:,:,:,Kmm) , puu(:,:,:,Krhs), pvv(:,:,:,Krhs) )   ! relative vorticity or metric trend (ens)
                              CALL vor_ene( kt, Kmm, ncor, puu(:,:,:,Kmm) , pvv(:,:,:,Kmm) , puu(:,:,:,Krhs), pvv(:,:,:,Krhs) )   ! planetary vorticity trend (ene)
-            IF( ln_stcor )   CALL vor_ene( kt, Kmm, ncor, usd, vsd, puu(:,:,:,Krhs), pvv(:,:,:,Krhs) )   ! add the Stokes-Coriolis trend
+            IF( ln_stcor )        CALL vor_ene( kt, Kmm, ncor, usd, vsd, puu(:,:,:,Krhs), pvv(:,:,:,Krhs) )        ! add the Stokes-Coriolis trend
+            IF( ln_vortex_force ) CALL vor_ens( kt, Kmm, nrvm, usd, vsd, puu(:,:,:,Krhs), pvv(:,:,:,Krhs) )   ! add vortex force
          CASE( np_EEN )                        !* energy and enstrophy conserving scheme
                              CALL vor_een( kt, Kmm, ntot, puu(:,:,:,Kmm) , pvv(:,:,:,Kmm) , puu(:,:,:,Krhs), pvv(:,:,:,Krhs) )   ! total vorticity trend
-            IF( ln_stcor )   CALL vor_een( kt, Kmm, ncor, usd, vsd, puu(:,:,:,Krhs), pvv(:,:,:,Krhs) )   ! add the Stokes-Coriolis trend
+            IF( ln_stcor .AND. .NOT. ln_vortex_force )  THEN
+                             CALL vor_een( kt, Kmm, ncor, usd, vsd, puu(:,:,:,Krhs), pvv(:,:,:,Krhs) )   ! add the Stokes-Coriolis trend
+            ELSE IF( ln_stcor .AND. ln_vortex_force )   THEN
+                             CALL vor_een( kt, Kmm, ntot, usd, vsd, puu(:,:,:,Krhs), pvv(:,:,:,Krhs) )   ! add the Stokes-Coriolis trend and vortex force
+            ENDIF
          END SELECT
          !
Index: /NEMO/branches/2020/tickets_icb_1900/src/OCE/DYN/dynzad.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/OCE/DYN/dynzad.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/OCE/DYN/dynzad.F90	(revision 14016)
@@ -16,4 +16,5 @@
    USE trd_oce        ! trends: ocean variables
    USE trddyn         ! trend manager: dynamics
+   USE sbcwave, ONLY: wsd   ! Surface Waves (add vertical Stokes-drift)
    !
    USE in_out_manager ! I/O manager
@@ -79,5 +80,9 @@
       DO jk = 2, jpkm1                ! Vertical momentum advection at level w and u- and v- vertical
          DO_2D( 0, 1, 0, 1 )              ! vertical fluxes
+          IF( ln_vortex_force ) THEN
+            zww(ji,jj) = 0.25_wp * e1e2t(ji,jj) * ( ww(ji,jj,jk) + wsd(ji,jj,jk) )
+          ELSE
             zww(ji,jj) = 0.25_wp * e1e2t(ji,jj) * ww(ji,jj,jk)
+          ENDIF
          END_2D
          DO_2D( 0, 0, 0, 0 )              ! vertical momentum advection at w-point
Index: /NEMO/branches/2020/tickets_icb_1900/src/OCE/SBC/cpl_oasis3.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/OCE/SBC/cpl_oasis3.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/OCE/SBC/cpl_oasis3.F90	(revision 14016)
@@ -66,5 +66,5 @@
    INTEGER                    ::   nsnd         ! total number of fields sent 
    INTEGER                    ::   ncplmodel    ! Maximum number of models to/from which NEMO is potentialy sending/receiving data
-   INTEGER, PUBLIC, PARAMETER ::   nmaxfld=60   ! Maximum number of coupling fields
+   INTEGER, PUBLIC, PARAMETER ::   nmaxfld=62   ! Maximum number of coupling fields
    INTEGER, PUBLIC, PARAMETER ::   nmaxcat=5    ! Maximum number of coupling fields
    INTEGER, PUBLIC, PARAMETER ::   nmaxcpl=5    ! Maximum number of coupling fields
Index: /NEMO/branches/2020/tickets_icb_1900/src/OCE/SBC/sbc_oce.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/OCE/SBC/sbc_oce.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/OCE/SBC/sbc_oce.F90	(revision 14016)
@@ -12,4 +12,5 @@
    !!            4.0  ! 2016-06  (L. Brodeau) new unified bulk routine (based on AeroBulk)
    !!            4.0  ! 2019-03  (F. Lemarié, G. Samson) add compatibility with ABL mode
+   !!            4.2  ! 2020-12  (G. Madec, E. Clementi) modified wave parameters in namelist
    !!----------------------------------------------------------------------
 
@@ -36,4 +37,5 @@
    LOGICAL , PUBLIC ::   ln_blk         !: bulk formulation
    LOGICAL , PUBLIC ::   ln_abl         !: Atmospheric boundary layer model
+   LOGICAL , PUBLIC ::   ln_wave        !: wave in the system (forced or coupled)
 #if defined key_oasis3
    LOGICAL , PUBLIC ::   lk_oasis = .TRUE.  !: OASIS used
@@ -56,13 +58,4 @@
    !                                             !:  = 1 global mean of e-p-r set to zero at each nn_fsbc time step
    !                                             !:  = 2 annual global mean of e-p-r set to zero
-   LOGICAL , PUBLIC ::   ln_wave        !: true if some coupling with wave model
-   LOGICAL , PUBLIC ::   ln_cdgw        !: true if neutral drag coefficient from wave model
-   LOGICAL , PUBLIC ::   ln_sdw         !: true if 3d stokes drift from wave model
-   LOGICAL , PUBLIC ::   ln_tauwoc       !: true if normalized stress from wave is used
-   LOGICAL , PUBLIC ::   ln_tauw        !: true if ocean stress components from wave is used
-   LOGICAL , PUBLIC ::   ln_stcor       !: true if Stokes-Coriolis term is used
-   !
-   INTEGER , PUBLIC ::   nn_sdrift      ! type of parameterization to calculate vertical Stokes drift
-   !
    LOGICAL , PUBLIC ::   ln_icebergs    !: Icebergs
    !
@@ -71,5 +64,19 @@
    !                                   !!* namsbc_cpl namelist *
    INTEGER , PUBLIC ::   nn_cats_cpl    !: Number of sea ice categories over which the coupling is carried out
-
+   !
+   !                                   !!* namsbc_wave namelist *
+   LOGICAL , PUBLIC ::   ln_sdw         !: =T 3d stokes drift from wave model
+   LOGICAL , PUBLIC ::   ln_stcor       !: =T if Stokes-Coriolis and tracer advection terms are used
+   LOGICAL , PUBLIC ::   ln_cdgw        !: =T neutral drag coefficient from wave model
+   LOGICAL , PUBLIC ::   ln_tauoc       !: =T if normalized stress from wave is used
+   LOGICAL , PUBLIC ::   ln_wave_test   !: =T wave test case (constant Stokes drift)
+   LOGICAL , PUBLIC ::   ln_charn       !: =T Chranock coefficient from wave model
+   LOGICAL , PUBLIC ::   ln_taw         !: =T wind stress corrected by wave intake
+   LOGICAL , PUBLIC ::   ln_phioc       !: =T TKE surface BC from wave model 
+   LOGICAL , PUBLIC ::   ln_bern_srfc   !: Bernoulli head, waves' inuced pressure
+   LOGICAL , PUBLIC ::   ln_breivikFV_2016 !: Breivik 2016 profile
+   LOGICAL , PUBLIC ::   ln_vortex_force !: vortex force activation
+   LOGICAL , PUBLIC ::   ln_stshear     !: Stoked Drift shear contribution in zdftke
+   !
    !!----------------------------------------------------------------------
    !!           switch definition (improve readability)
@@ -81,18 +88,5 @@
    INTEGER , PUBLIC, PARAMETER ::   jp_purecpl = 5        !: Pure ocean-atmosphere Coupled formulation
    INTEGER , PUBLIC, PARAMETER ::   jp_none    = 6        !: for OPA when doing coupling via SAS module
-
-   !!----------------------------------------------------------------------
-   !!           Stokes drift parametrization definition
-   !!----------------------------------------------------------------------
-   INTEGER , PUBLIC, PARAMETER ::   jp_breivik_2014 = 0     !: Breivik  2014: v_z=v_0*[exp(2*k*z)/(1-8*k*z)]
-   INTEGER , PUBLIC, PARAMETER ::   jp_li_2017      = 1     !: Li et al 2017: Stokes drift based on Phillips spectrum (Breivik 2016)
-   !  with depth averaged profile
-   INTEGER , PUBLIC, PARAMETER ::   jp_peakfr       = 2     !: Li et al 2017: using the peak wave number read from wave model instead
-   !  of the inverse depth scale
-   LOGICAL , PUBLIC            ::   ll_st_bv2014  = .FALSE. !  logical indicator, .true. if Breivik 2014 parameterisation is active.
-   LOGICAL , PUBLIC            ::   ll_st_li2017  = .FALSE. !  logical indicator, .true. if Li 2017 parameterisation is active.
-   LOGICAL , PUBLIC            ::   ll_st_bv_li   = .FALSE. !  logical indicator, .true. if either Breivik or Li parameterisation is active.
-   LOGICAL , PUBLIC            ::   ll_st_peakfr  = .FALSE. !  logical indicator, .true. if using Li 2017 with peak wave number
-
+   !
    !!----------------------------------------------------------------------
    !!           component definition
Index: /NEMO/branches/2020/tickets_icb_1900/src/OCE/SBC/sbcblk.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/OCE/SBC/sbcblk.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/OCE/SBC/sbcblk.F90	(revision 14016)
@@ -314,23 +314,4 @@
          ENDIF
       END DO
-      !
-      IF( ln_wave ) THEN
-         !Activated wave module but neither drag nor stokes drift activated
-         IF( .NOT.(ln_cdgw .OR. ln_sdw .OR. ln_tauwoc .OR. ln_stcor ) )   THEN
-            CALL ctl_stop( 'STOP',  'Ask for wave coupling but ln_cdgw=F, ln_sdw=F, ln_tauwoc=F, ln_stcor=F' )
-            !drag coefficient read from wave model definable only with mfs bulk formulae and core
-         ELSEIF(ln_cdgw .AND. .NOT. ln_NCAR )       THEN
-            CALL ctl_stop( 'drag coefficient read from wave model definable only with NCAR and CORE bulk formulae')
-         ELSEIF(ln_stcor .AND. .NOT. ln_sdw)                             THEN
-            CALL ctl_stop( 'Stokes-Coriolis term calculated only if activated Stokes Drift ln_sdw=T')
-         ENDIF
-      ELSE
-         IF( ln_cdgw .OR. ln_sdw .OR. ln_tauwoc .OR. ln_stcor )                &
-            &   CALL ctl_stop( 'Not Activated Wave Module (ln_wave=F) but asked coupling ',    &
-            &                  'with drag coefficient (ln_cdgw =T) '  ,                        &
-            &                  'or Stokes Drift (ln_sdw=T) ' ,                                 &
-            &                  'or ocean stress modification due to waves (ln_tauwoc=T) ',      &
-            &                  'or Stokes-Coriolis term (ln_stcori=T)'  )
-      ENDIF
       !
       IF( ln_abl ) THEN       ! ABL: read 3D fields for wind, temperature, humidity and pressure gradient
Index: /NEMO/branches/2020/tickets_icb_1900/src/OCE/SBC/sbcblk_algo_ecmwf.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/OCE/SBC/sbcblk_algo_ecmwf.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/OCE/SBC/sbcblk_algo_ecmwf.F90	(revision 14016)
@@ -17,4 +17,5 @@
    !!----------------------------------------------------------------------
    !! History :  4.0  !  2016-02  (L.Brodeau)   Original code
+   !!            4.2  !  2020-12  (G. Madec, E. Clementi) Charnock coeff from wave model
    !!----------------------------------------------------------------------
 
@@ -31,5 +32,5 @@
    USE in_out_manager  ! I/O manager
    USE prtctl          ! Print control
-   USE sbcwave, ONLY   :  cdn_wave ! wave module
+   USE sbcwave, ONLY   : charn ! wave module
 #if defined key_si3 || defined key_cice
    USE sbc_ice         ! Surface boundary condition: ice fields
@@ -233,5 +234,10 @@
       u_star = 0.035_wp*U_blk*ztmp1/ztmp0       ! (u* = 0.035*Un10)
 
-      z0     = charn0*u_star*u_star/grav + 0.11_wp*znu_a/u_star
+      IF (ln_charn)  THEN          !  Charnock value if wave coupling
+         z0     = charn*u_star*u_star/grav + 0.11_wp*znu_a/u_star
+      ELSE
+         z0     = charn0*u_star*u_star/grav + 0.11_wp*znu_a/u_star
+      ENDIF
+
       z0     = MIN( MAX(ABS(z0), 1.E-9) , 1._wp )                      ! (prevents FPE from stupid values from masked region later on)
 
@@ -302,5 +308,9 @@
          ztmp2  = u_star*u_star
          ztmp1  = znu_a/u_star
-         z0     = MIN( ABS( alpha_M*ztmp1 + charn0*ztmp2/grav ) , 0.001_wp)
+         IF (ln_charn) THEN     ! Charnock value if wave coupling
+            z0  = MIN( ABS( alpha_M*ztmp1 + charn*ztmp2/grav ) , 0.001_wp)         
+         ELSE
+            z0     = MIN( ABS( alpha_M*ztmp1 + charn0*ztmp2/grav ) , 0.001_wp)
+         ENDIF
          z0t    = MIN( ABS( alpha_H*ztmp1                     ) , 0.001_wp)   ! eq.3.26, Chap.3, p.34, IFS doc - Cy31r1
          z0q    = MIN( ABS( alpha_Q*ztmp1                     ) , 0.001_wp)
Index: /NEMO/branches/2020/tickets_icb_1900/src/OCE/SBC/sbccpl.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/OCE/SBC/sbccpl.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/OCE/SBC/sbccpl.F90	(revision 14016)
@@ -8,4 +8,5 @@
    !!            3.1  ! 2009_02  (G. Madec, S. Masson, E. Maisonave, A. Caubel) generic coupled interface
    !!            3.4  ! 2011_11  (C. Harris) more flexibility + multi-category fields
+   !!            4.2  ! 2020-12  (G. Madec, E. Clementi)  wave coupling updates
    !!----------------------------------------------------------------------
 
@@ -106,20 +107,26 @@
    INTEGER, PARAMETER ::   jpr_fraqsr = 42   ! fraction of solar net radiation absorbed in the first ocean level
    INTEGER, PARAMETER ::   jpr_mslp   = 43   ! mean sea level pressure 
-   INTEGER, PARAMETER ::   jpr_hsig   = 44   ! Hsig 
-   INTEGER, PARAMETER ::   jpr_phioc  = 45   ! Wave=>ocean energy flux 
-   INTEGER, PARAMETER ::   jpr_sdrftx = 46   ! Stokes drift on grid 1 
-   INTEGER, PARAMETER ::   jpr_sdrfty = 47   ! Stokes drift on grid 2 
+   !**  surface wave coupling  **
+   INTEGER, PARAMETER ::   jpr_hsig   = 44   ! Hsig
+   INTEGER, PARAMETER ::   jpr_phioc  = 45   ! Wave=>ocean energy flux
+   INTEGER, PARAMETER ::   jpr_sdrftx = 46   ! Stokes drift on grid 1
+   INTEGER, PARAMETER ::   jpr_sdrfty = 47   ! Stokes drift on grid 2
    INTEGER, PARAMETER ::   jpr_wper   = 48   ! Mean wave period
    INTEGER, PARAMETER ::   jpr_wnum   = 49   ! Mean wavenumber
-   INTEGER, PARAMETER ::   jpr_tauwoc = 50   ! Stress fraction adsorbed by waves
+   INTEGER, PARAMETER ::   jpr_wstrf  = 50   ! Stress fraction adsorbed by waves
    INTEGER, PARAMETER ::   jpr_wdrag  = 51   ! Neutral surface drag coefficient
-   INTEGER, PARAMETER ::   jpr_isf    = 52
-   INTEGER, PARAMETER ::   jpr_icb    = 53
-   INTEGER, PARAMETER ::   jpr_wfreq  = 54   ! Wave peak frequency
-   INTEGER, PARAMETER ::   jpr_tauwx  = 55   ! x component of the ocean stress from waves
-   INTEGER, PARAMETER ::   jpr_tauwy  = 56   ! y component of the ocean stress from waves
-   INTEGER, PARAMETER ::   jpr_ts_ice = 57   ! Sea ice surface temp
-
-   INTEGER, PARAMETER ::   jprcv      = 57   ! total number of fields received  
+   INTEGER, PARAMETER ::   jpr_charn  = 52   ! Chranock coefficient
+   INTEGER, PARAMETER ::   jpr_twox   = 53   ! wave to ocean momentum flux
+   INTEGER, PARAMETER ::   jpr_twoy   = 54   ! wave to ocean momentum flux
+   INTEGER, PARAMETER ::   jpr_tawx   = 55   ! net wave-supported stress
+   INTEGER, PARAMETER ::   jpr_tawy   = 56   ! net wave-supported stress
+   INTEGER, PARAMETER ::   jpr_bhd    = 57   ! Bernoulli head. waves' induced surface pressure
+   INTEGER, PARAMETER ::   jpr_tusd   = 58   ! zonal stokes transport
+   INTEGER, PARAMETER ::   jpr_tvsd   = 59   ! meridional stokes tranmport
+   INTEGER, PARAMETER ::   jpr_isf    = 60
+   INTEGER, PARAMETER ::   jpr_icb    = 61
+   INTEGER, PARAMETER ::   jpr_ts_ice = 62   ! Sea ice surface temp
+
+   INTEGER, PARAMETER ::   jprcv      = 62   ! total number of fields received  
 
    INTEGER, PARAMETER ::   jps_fice   =  1   ! ice fraction sent to the atmosphere
@@ -184,12 +191,12 @@
       &             sn_snd_thick1, sn_snd_cond, sn_snd_mpnd , sn_snd_sstfrz, sn_snd_ttilyr
    !                                   ! Received from the atmosphere
-   TYPE(FLD_C) ::   sn_rcv_w10m, sn_rcv_taumod, sn_rcv_tau, sn_rcv_tauw, sn_rcv_dqnsdt, sn_rcv_qsr,  &
+   TYPE(FLD_C) ::   sn_rcv_w10m, sn_rcv_taumod, sn_rcv_tau, sn_rcv_dqnsdt, sn_rcv_qsr,  &
       &             sn_rcv_qns , sn_rcv_emp   , sn_rcv_rnf, sn_rcv_ts_ice
    TYPE(FLD_C) ::   sn_rcv_cal, sn_rcv_iceflx, sn_rcv_co2, sn_rcv_mslp, sn_rcv_icb, sn_rcv_isf
-   ! Send to waves 
+   !                                   ! Send to waves 
    TYPE(FLD_C) ::   sn_snd_ifrac, sn_snd_crtw, sn_snd_wlev 
-   ! Received from waves 
-   TYPE(FLD_C) ::   sn_rcv_hsig, sn_rcv_phioc, sn_rcv_sdrfx, sn_rcv_sdrfy, sn_rcv_wper, sn_rcv_wnum, sn_rcv_tauwoc, &
-                    sn_rcv_wdrag, sn_rcv_wfreq
+   !                                   ! Received from waves 
+   TYPE(FLD_C) ::   sn_rcv_hsig, sn_rcv_phioc, sn_rcv_sdrfx, sn_rcv_sdrfy, sn_rcv_wper, sn_rcv_wnum, &
+      &             sn_rcv_wstrf, sn_rcv_wdrag, sn_rcv_charn, sn_rcv_taw, sn_rcv_bhd, sn_rcv_tusd, sn_rcv_tvsd
    !                                   ! Other namelist parameters
    INTEGER     ::   nn_cplmodel           ! Maximum number of models to/from which NEMO is potentialy sending/receiving data
@@ -274,9 +281,9 @@
          &                  sn_snd_ifrac , sn_snd_crtw  , sn_snd_wlev , sn_rcv_hsig  , sn_rcv_phioc ,  & 
          &                  sn_rcv_w10m  , sn_rcv_taumod, sn_rcv_tau  , sn_rcv_dqnsdt, sn_rcv_qsr   ,  & 
-         &                  sn_rcv_sdrfx , sn_rcv_sdrfy , sn_rcv_wper , sn_rcv_wnum  , sn_rcv_tauwoc,  &
-         &                  sn_rcv_wdrag , sn_rcv_qns   , sn_rcv_emp  , sn_rcv_rnf   , sn_rcv_cal   ,  &
-         &                  sn_rcv_iceflx, sn_rcv_co2   , sn_rcv_mslp ,                                &
-         &                  sn_rcv_icb   , sn_rcv_isf   , sn_rcv_wfreq, sn_rcv_tauw  ,                 &
-         &                  sn_rcv_ts_ice
+         &                  sn_rcv_sdrfx , sn_rcv_sdrfy , sn_rcv_wper , sn_rcv_wnum  , sn_rcv_wstrf ,  &
+         &                  sn_rcv_charn , sn_rcv_taw   , sn_rcv_bhd  , sn_rcv_tusd  , sn_rcv_tvsd,    &
+         &                  sn_rcv_wdrag , sn_rcv_qns   , sn_rcv_emp  , sn_rcv_rnf   , sn_rcv_cal  ,   &
+         &                  sn_rcv_iceflx, sn_rcv_co2   , sn_rcv_icb  , sn_rcv_isf   , sn_rcv_ts_ice 
+
       !!---------------------------------------------------------------------
       !
@@ -319,4 +326,6 @@
          WRITE(numout,*)'      sea ice heat fluxes             = ', TRIM(sn_rcv_iceflx%cldes), ' (', TRIM(sn_rcv_iceflx%clcat), ')'
          WRITE(numout,*)'      atm co2                         = ', TRIM(sn_rcv_co2%cldes   ), ' (', TRIM(sn_rcv_co2%clcat   ), ')'
+         WRITE(numout,*)'      Sea ice surface skin temperature= ', TRIM(sn_rcv_ts_ice%cldes), ' (', TRIM(sn_rcv_ts_ice%clcat), ')'
+         WRITE(numout,*)'      surface waves:'
          WRITE(numout,*)'      significant wave heigth         = ', TRIM(sn_rcv_hsig%cldes  ), ' (', TRIM(sn_rcv_hsig%clcat  ), ')' 
          WRITE(numout,*)'      wave to oce energy flux         = ', TRIM(sn_rcv_phioc%cldes ), ' (', TRIM(sn_rcv_phioc%clcat ), ')' 
@@ -325,9 +334,7 @@
          WRITE(numout,*)'      Mean wave period                = ', TRIM(sn_rcv_wper%cldes  ), ' (', TRIM(sn_rcv_wper%clcat  ), ')' 
          WRITE(numout,*)'      Mean wave number                = ', TRIM(sn_rcv_wnum%cldes  ), ' (', TRIM(sn_rcv_wnum%clcat  ), ')' 
-         WRITE(numout,*)'      Wave peak frequency             = ', TRIM(sn_rcv_wfreq%cldes ), ' (', TRIM(sn_rcv_wfreq%clcat ), ')'
-         WRITE(numout,*)'      Stress frac adsorbed by waves   = ', TRIM(sn_rcv_tauwoc%cldes), ' (', TRIM(sn_rcv_tauwoc%clcat ), ')' 
-         WRITE(numout,*)'      Stress components by waves      = ', TRIM(sn_rcv_tauw%cldes  ), ' (', TRIM(sn_rcv_tauw%clcat  ), ')'
+         WRITE(numout,*)'      Stress frac adsorbed by waves   = ', TRIM(sn_rcv_wstrf%cldes ), ' (', TRIM(sn_rcv_wstrf%clcat ), ')'
          WRITE(numout,*)'      Neutral surf drag coefficient   = ', TRIM(sn_rcv_wdrag%cldes ), ' (', TRIM(sn_rcv_wdrag%clcat ), ')' 
-         WRITE(numout,*)'      Sea ice surface skin temperature= ', TRIM(sn_rcv_ts_ice%cldes), ' (', TRIM(sn_rcv_ts_ice%clcat), ')' 
+         WRITE(numout,*)'      Charnock coefficient            = ', TRIM(sn_rcv_charn%cldes ), ' (', TRIM(sn_rcv_charn%clcat ), ')'
          WRITE(numout,*)'  sent fields (multiple ice categories)'
          WRITE(numout,*)'      surface temperature             = ', TRIM(sn_snd_temp%cldes  ), ' (', TRIM(sn_snd_temp%clcat  ), ')'
@@ -351,5 +358,24 @@
          WRITE(numout,*)'                      - mesh          = ', sn_snd_crtw%clvgrd 
       ENDIF
-
+      IF( lwp .AND. ln_wave) THEN                        ! control print
+      WRITE(numout,*)'      surface waves:'
+         WRITE(numout,*)'      Significant wave heigth         = ', TRIM(sn_rcv_hsig%cldes  ), ' (', TRIM(sn_rcv_hsig%clcat  ), ')'
+         WRITE(numout,*)'      Wave to oce energy flux         = ', TRIM(sn_rcv_phioc%cldes ), ' (', TRIM(sn_rcv_phioc%clcat ), ')'
+         WRITE(numout,*)'      Surface Stokes drift grid u     = ', TRIM(sn_rcv_sdrfx%cldes ), ' (', TRIM(sn_rcv_sdrfx%clcat ), ')'
+         WRITE(numout,*)'      Surface Stokes drift grid v     = ', TRIM(sn_rcv_sdrfy%cldes ), ' (', TRIM(sn_rcv_sdrfy%clcat ), ')'
+         WRITE(numout,*)'      Mean wave period                = ', TRIM(sn_rcv_wper%cldes  ), ' (', TRIM(sn_rcv_wper%clcat  ), ')'
+         WRITE(numout,*)'      Mean wave number                = ', TRIM(sn_rcv_wnum%cldes  ), ' (', TRIM(sn_rcv_wnum%clcat  ), ')'
+         WRITE(numout,*)'      Stress frac adsorbed by waves   = ', TRIM(sn_rcv_wstrf%cldes ), ' (', TRIM(sn_rcv_wstrf%clcat ), ')'
+         WRITE(numout,*)'      Neutral surf drag coefficient   = ', TRIM(sn_rcv_wdrag%cldes ), ' (', TRIM(sn_rcv_wdrag%clcat ), ')'
+         WRITE(numout,*)'      Charnock coefficient            = ', TRIM(sn_rcv_charn%cldes ), ' (', TRIM(sn_rcv_charn%clcat ), ')'
+         WRITE(numout,*)' Transport associated to Stokes drift grid u = ', TRIM(sn_rcv_tusd%cldes ), ' (', TRIM(sn_rcv_tusd%clcat ), ')'
+         WRITE(numout,*)' Transport associated to Stokes drift grid v = ', TRIM(sn_rcv_tvsd%cldes ), ' (', TRIM(sn_rcv_tvsd%clcat ), ')'
+         WRITE(numout,*)'      Bernouilli pressure head        = ', TRIM(sn_rcv_bhd%cldes   ), ' (', TRIM(sn_rcv_bhd%clcat  ), ')'
+         WRITE(numout,*)'Wave to ocean momentum flux and Net wave-supported stress = ', TRIM(sn_rcv_taw%cldes ), ' (', TRIM(sn_rcv_taw%clcat ), ')'
+         WRITE(numout,*)'      Surface current to waves        = ', TRIM(sn_snd_crtw%cldes  ), ' (', TRIM(sn_snd_crtw%clcat  ), ')'
+         WRITE(numout,*)'                      - referential   = ', sn_snd_crtw%clvref
+         WRITE(numout,*)'                      - orientation   = ', sn_snd_crtw%clvor
+         WRITE(numout,*)'                      - mesh          = ', sn_snd_crtw%clvgrd
+      ENDIF
       !                                   ! allocate sbccpl arrays
       IF( sbc_cpl_alloc() /= 0 )   CALL ctl_stop( 'STOP', 'sbc_cpl_alloc : unable to allocate arrays' )
@@ -629,9 +655,4 @@
          cpl_wper = .TRUE.
       ENDIF
-      srcv(jpr_wfreq)%clname = 'O_WFreq'     ! wave peak frequency 
-      IF( TRIM(sn_rcv_wfreq%cldes ) == 'coupled' )  THEN
-         srcv(jpr_wfreq)%laction = .TRUE.
-         cpl_wfreq = .TRUE.
-      ENDIF
       srcv(jpr_wnum)%clname = 'O_WNum'       ! mean wave number
       IF( TRIM(sn_rcv_wnum%cldes ) == 'coupled' )  THEN
@@ -639,15 +660,8 @@
          cpl_wnum = .TRUE.
       ENDIF
-      srcv(jpr_tauwoc)%clname = 'O_TauOce'   ! stress fraction adsorbed by the wave
-      IF( TRIM(sn_rcv_tauwoc%cldes ) == 'coupled' )  THEN
-         srcv(jpr_tauwoc)%laction = .TRUE.
-         cpl_tauwoc = .TRUE.
-      ENDIF
-      srcv(jpr_tauwx)%clname = 'O_Tauwx'      ! ocean stress from wave in the x direction
-      srcv(jpr_tauwy)%clname = 'O_Tauwy'      ! ocean stress from wave in the y direction
-      IF( TRIM(sn_rcv_tauw%cldes ) == 'coupled' )  THEN
-         srcv(jpr_tauwx)%laction = .TRUE.
-         srcv(jpr_tauwy)%laction = .TRUE.
-         cpl_tauw = .TRUE.
+      srcv(jpr_wstrf)%clname = 'O_WStrf'     ! stress fraction adsorbed by the wave
+      IF( TRIM(sn_rcv_wstrf%cldes ) == 'coupled' )  THEN
+         srcv(jpr_wstrf)%laction = .TRUE.
+         cpl_wstrf = .TRUE.
       ENDIF
       srcv(jpr_wdrag)%clname = 'O_WDrag'     ! neutral surface drag coefficient
@@ -656,7 +670,36 @@
          cpl_wdrag = .TRUE.
       ENDIF
-      IF( srcv(jpr_tauwoc)%laction .AND. srcv(jpr_tauwx)%laction .AND. srcv(jpr_tauwy)%laction ) &
-            CALL ctl_stop( 'More than one method for modifying the ocean stress has been selected ', &
-                                     '(sn_rcv_tauwoc=coupled and sn_rcv_tauw=coupled)' )
+      srcv(jpr_charn)%clname = 'O_Charn'     ! Chranock coefficient
+      IF( TRIM(sn_rcv_charn%cldes ) == 'coupled' )  THEN
+         srcv(jpr_charn)%laction = .TRUE.
+         cpl_charn = .TRUE.
+      ENDIF
+      srcv(jpr_bhd)%clname = 'O_Bhd'     ! Bernoulli head. waves' induced surface pressure
+      IF( TRIM(sn_rcv_bhd%cldes ) == 'coupled' )  THEN
+         srcv(jpr_bhd)%laction = .TRUE.
+         cpl_bhd = .TRUE.
+      ENDIF
+      srcv(jpr_tusd)%clname = 'O_Tusd'     ! zonal stokes transport
+      IF( TRIM(sn_rcv_tusd%cldes ) == 'coupled' )  THEN
+         srcv(jpr_tusd)%laction = .TRUE.
+         cpl_tusd = .TRUE.
+      ENDIF
+      srcv(jpr_tvsd)%clname = 'O_Tvsd'     ! meridional stokes tranmport
+      IF( TRIM(sn_rcv_tvsd%cldes ) == 'coupled' )  THEN
+         srcv(jpr_tvsd)%laction = .TRUE.
+         cpl_tvsd = .TRUE.
+      ENDIF
+
+      srcv(jpr_twox)%clname = 'O_Twox'     ! wave to ocean momentum flux in the u direction
+      srcv(jpr_twoy)%clname = 'O_Twoy'     ! wave to ocean momentum flux in the v direction
+      srcv(jpr_tawx)%clname = 'O_Tawx'     ! Net wave-supported stress in the u direction
+      srcv(jpr_tawy)%clname = 'O_Tawy'     ! Net wave-supported stress in the v direction
+      IF( TRIM(sn_rcv_taw%cldes ) == 'coupled' )  THEN
+         srcv(jpr_twox)%laction = .TRUE.
+         srcv(jpr_twoy)%laction = .TRUE.
+         srcv(jpr_tawx)%laction = .TRUE.
+         srcv(jpr_tawy)%laction = .TRUE.
+         cpl_taw = .TRUE.
+      ENDIF
       !
       !                                                      ! ------------------------------- !
@@ -1058,5 +1101,4 @@
       !   initialisation of the coupler  !
       ! ================================ !
-
       CALL cpl_define(jprcv, jpsnd, nn_cplmodel)
       
@@ -1071,4 +1113,5 @@
       ENDIF
       xcplmask(:,:,0) = 1. - SUM( xcplmask(:,:,1:nn_cplmodel), dim = 3 )
+      !
       !
    END SUBROUTINE sbc_cpl_init
@@ -1146,4 +1189,8 @@
          IF( ncpl_qsr_freq /= 0) ncpl_qsr_freq = 86400 / ncpl_qsr_freq ! used by top
          
+         IF ( ln_wave .AND. nn_components == 0 ) THEN
+            ncpl_qsr_freq = 1;
+            WRITE(numout,*) 'ncpl_qsr_freq is set to 1 when coupling NEMO with wave (without SAS) '
+         ENDIF
       ENDIF
       !
@@ -1320,9 +1367,4 @@
          IF( srcv(jpr_hsig)%laction ) hsw(:,:) = frcv(jpr_hsig)%z3(:,:,1)
       ! 
-      !                                                      ! ========================= !  
-      !                                                      !    Wave peak frequency    ! 
-      !                                                      ! ========================= !  
-         IF( srcv(jpr_wfreq)%laction ) wfreq(:,:) = frcv(jpr_wfreq)%z3(:,:,1)
-      !
       !                                                      ! ========================= ! 
       !                                                      !    Vertical mixing Qiao   !
@@ -1331,6 +1373,6 @@
 
          ! Calculate the 3D Stokes drift both in coupled and not fully uncoupled mode
-         IF( srcv(jpr_sdrftx)%laction .OR. srcv(jpr_sdrfty)%laction .OR. srcv(jpr_wper)%laction &
-                                      .OR. srcv(jpr_hsig)%laction   .OR. srcv(jpr_wfreq)%laction) THEN
+         IF( srcv(jpr_sdrftx)%laction .OR. srcv(jpr_sdrfty)%laction .OR. &
+             srcv(jpr_wper)%laction .OR. srcv(jpr_hsig)%laction )   THEN
             CALL sbc_stokes( Kmm )
          ENDIF
@@ -1339,19 +1381,48 @@
       !                                                      ! Stress adsorbed by waves  !
       !                                                      ! ========================= ! 
-      IF( srcv(jpr_tauwoc)%laction .AND. ln_tauwoc ) tauoc_wave(:,:) = frcv(jpr_tauwoc)%z3(:,:,1)
-
-      !                                                      ! ========================= !  
-      !                                                      ! Stress component by waves ! 
-      !                                                      ! ========================= !  
-      IF( srcv(jpr_tauwx)%laction .AND. srcv(jpr_tauwy)%laction .AND. ln_tauw ) THEN
-         tauw_x(:,:) = frcv(jpr_tauwx)%z3(:,:,1)
-         tauw_y(:,:) = frcv(jpr_tauwy)%z3(:,:,1)
-      ENDIF
-
+      IF( srcv(jpr_wstrf)%laction .AND. ln_tauoc )  tauoc_wave(:,:) = frcv(jpr_wstrf)%z3(:,:,1)
+      !
       !                                                      ! ========================= ! 
       !                                                      !   Wave drag coefficient   !
       !                                                      ! ========================= ! 
       IF( srcv(jpr_wdrag)%laction .AND. ln_cdgw )   cdn_wave(:,:) = frcv(jpr_wdrag)%z3(:,:,1)
-
+      !
+      !                                                      ! ========================= !
+      !                                                      !   Chranock coefficient    !
+      !                                                      ! ========================= !
+      IF( srcv(jpr_charn)%laction .AND. ln_charn )  charn(:,:) = frcv(jpr_charn)%z3(:,:,1)
+      !
+      !                                                      ! ========================= !
+      !                                                      ! net wave-supported stress !
+      !                                                      ! ========================= !
+      IF( srcv(jpr_tawx)%laction .AND. ln_taw )     tawx(:,:) = frcv(jpr_tawx)%z3(:,:,1)
+      IF( srcv(jpr_tawy)%laction .AND. ln_taw )     tawy(:,:) = frcv(jpr_tawy)%z3(:,:,1)
+      !
+      !                                                      ! ========================= !
+      !                                                      !wave to ocean momentum flux!
+      !                                                      ! ========================= !
+      IF( srcv(jpr_twox)%laction .AND. ln_taw )     twox(:,:) = frcv(jpr_twox)%z3(:,:,1)
+      IF( srcv(jpr_twoy)%laction .AND. ln_taw )     twoy(:,:) = frcv(jpr_twoy)%z3(:,:,1)
+      !                                                      
+      !                                                      ! ========================= !
+      !                                                      !    wave TKE flux at sfc   !
+      !                                                      ! ========================= !
+      IF( srcv(jpr_phioc)%laction .AND. ln_phioc )     phioc(:,:) = frcv(jpr_phioc)%z3(:,:,1)
+      !
+      !                                                      ! ========================= !
+      !                                                      !      Bernoulli head       !
+      !                                                      ! ========================= !
+      IF( srcv(jpr_bhd)%laction .AND. ln_bern_srfc )   bhd_wave(:,:) = frcv(jpr_bhd)%z3(:,:,1)
+      !
+      !                                                      ! ========================= !
+      !                                                      !   Stokes transport u dir  !
+      !                                                      ! ========================= !
+      IF( srcv(jpr_tusd)%laction .AND. ln_breivikFV_2016 )    tusd(:,:) = frcv(jpr_tusd)%z3(:,:,1)
+      !
+      !                                                      ! ========================= !
+      !                                                      !   Stokes transport v dir  !
+      !                                                      ! ========================= !
+      IF( srcv(jpr_tvsd)%laction .AND. ln_breivikFV_2016 )     tvsd(:,:) = frcv(jpr_tvsd)%z3(:,:,1)
+      !
       !  Fields received by SAS when OASIS coupling
       !  (arrays no more filled at sbcssm stage)
Index: /NEMO/branches/2020/tickets_icb_1900/src/OCE/SBC/sbcmod.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/OCE/SBC/sbcmod.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/OCE/SBC/sbcmod.F90	(revision 14016)
@@ -16,4 +16,5 @@
    !!            4.0  ! 2016-06  (L. Brodeau) new general bulk formulation
    !!            4.0  ! 2019-03  (F. Lemarié & G. Samson)  add ABL compatibility (ln_abl=TRUE)
+   !!            4.2  ! 2020-12  (G. Madec, E. Clementi) modified wave forcing and coupling  
    !!----------------------------------------------------------------------
 
@@ -54,4 +55,5 @@
    USE usrdef_sbc     ! user defined: surface boundary condition
    USE closea         ! closed sea
+   USE lbclnk         ! ocean lateral boundary conditions (or mpp link)
    !
    USE prtctl         ! Print control                    (prt_ctl routine)
@@ -70,5 +72,6 @@
 
    INTEGER ::   nsbc   ! type of surface boundary condition (deduced from namsbc informations)
-
+   !! * Substitutions
+#  include "do_loop_substitute.h90"
    !!----------------------------------------------------------------------
    !! NEMO/OCE 4.0 , NEMO Consortium (2018)
@@ -99,7 +102,6 @@
          &             nn_ice   , ln_ice_embd,                                       &
          &             ln_traqsr, ln_dm2dc ,                                         &
-         &             ln_rnf   , nn_fwb   , ln_ssr   , ln_apr_dyn,                  &
-         &             ln_wave  , ln_cdgw  , ln_sdw   , ln_tauwoc , ln_stcor  ,      &
-         &             ln_tauw  , nn_lsm, nn_sdrift
+         &             ln_rnf   , nn_fwb     , ln_ssr   , ln_apr_dyn,                &
+         &             ln_wave  , nn_lsm
       !!----------------------------------------------------------------------
       !
@@ -133,4 +135,5 @@
          WRITE(numout,*) '         bulk         formulation                   ln_blk        = ', ln_blk
          WRITE(numout,*) '         ABL          formulation                   ln_abl        = ', ln_abl
+         WRITE(numout,*) '         Surface wave (forced or coupled)           ln_wave       = ', ln_wave
          WRITE(numout,*) '      Type of coupling (Ocean/Ice/Atmosphere) : '
          WRITE(numout,*) '         ocean-atmosphere coupled formulation       ln_cpl        = ', ln_cpl
@@ -150,32 +153,5 @@
          WRITE(numout,*) '         runoff / runoff mouths                     ln_rnf        = ', ln_rnf
          WRITE(numout,*) '         nb of iterations if land-sea-mask applied  nn_lsm        = ', nn_lsm
-         WRITE(numout,*) '         surface wave                               ln_wave       = ', ln_wave
-         WRITE(numout,*) '               Stokes drift corr. to vert. velocity ln_sdw        = ', ln_sdw
-         WRITE(numout,*) '                  vertical parametrization          nn_sdrift     = ', nn_sdrift
-         WRITE(numout,*) '               wave modified ocean stress           ln_tauwoc     = ', ln_tauwoc
-         WRITE(numout,*) '               wave modified ocean stress component ln_tauw       = ', ln_tauw
-         WRITE(numout,*) '               Stokes coriolis term                 ln_stcor      = ', ln_stcor
-         WRITE(numout,*) '               neutral drag coefficient (CORE,NCAR) ln_cdgw       = ', ln_cdgw
-      ENDIF
-      !
-      IF( .NOT.ln_wave ) THEN
-         ln_sdw = .false. ; ln_cdgw = .false. ; ln_tauwoc = .false. ; ln_tauw = .false. ; ln_stcor = .false.
-      ENDIF 
-      IF( ln_sdw ) THEN
-         IF( .NOT.(nn_sdrift==jp_breivik_2014 .OR. nn_sdrift==jp_li_2017 .OR. nn_sdrift==jp_peakfr) ) &
-            CALL ctl_stop( 'The chosen nn_sdrift for Stokes drift vertical velocity must be 0, 1, or 2' )
-      ENDIF
-      ll_st_bv2014  = ( nn_sdrift==jp_breivik_2014 )
-      ll_st_li2017  = ( nn_sdrift==jp_li_2017 )
-      ll_st_bv_li   = ( ll_st_bv2014 .OR. ll_st_li2017 )
-      ll_st_peakfr  = ( nn_sdrift==jp_peakfr )
-      IF( ln_tauwoc .AND. ln_tauw ) &
-         CALL ctl_stop( 'More than one method for modifying the ocean stress has been selected ', &
-                                  '(ln_tauwoc=.true. and ln_tauw=.true.)' )
-      IF( ln_tauwoc ) &
-         CALL ctl_warn( 'You are subtracting the wave stress to the ocean (ln_tauwoc=.true.)' )
-      IF( ln_tauw ) &
-         CALL ctl_warn( 'The wave modified ocean stress components are used (ln_tauw=.true.) ', &
-                              'This will override any other specification of the ocean stress' )
+      ENDIF
       !
       IF( .NOT.ln_usr ) THEN     ! the model calendar needs some specificities (except in user defined case)
@@ -357,5 +333,22 @@
       IF( nn_ice == 3 )   CALL cice_sbc_init( nsbc, Kbb, Kmm )   ! CICE initialization
       !
-      IF( ln_wave     )   CALL sbc_wave_init                     ! surface wave initialisation
+      IF( ln_wave     ) THEN
+                          CALL sbc_wave_init                     ! surface wave initialisation
+      ELSE
+                          IF(lwp) WRITE(numout,*)
+                          IF(lwp) WRITE(numout,*) '   No surface waves : all wave related logical set to false'
+                          ln_sdw       = .false.
+                          ln_stcor     = .false.
+                          ln_cdgw      = .false.
+                          ln_tauoc     = .false.
+                          ln_wave_test = .false.
+                          ln_charn     = .false.
+                          ln_taw       = .false.
+                          ln_phioc     = .false.
+                          ln_bern_srfc = .false.
+                          ln_breivikFV_2016 = .false.
+                          ln_vortex_force = .false.
+                          ln_stshear  = .false.
+      ENDIF
       !
    END SUBROUTINE sbc_init
@@ -380,4 +373,5 @@
       INTEGER, INTENT(in) ::   kt   ! ocean time step
       INTEGER, INTENT(in) ::   Kbb, Kmm   ! ocean time level indices
+      INTEGER  ::   jj, ji          ! dummy loop argument
       !
       LOGICAL ::   ll_sas, ll_opa   ! local logical
@@ -412,9 +406,8 @@
       !
       IF( .NOT.ll_sas )   CALL sbc_ssm ( kt, Kbb, Kmm )  ! mean ocean sea surface variables (sst_m, sss_m, ssu_m, ssv_m)
-      IF( ln_wave     )   CALL sbc_wave( kt, Kmm )       ! surface waves
-
       !
       !                                            !==  sbc formulation  ==!
       !                                                   
+      !
       SELECT CASE( nsbc )                                ! Compute ocean surface boundary condition
       !                                                  ! (i.e. utau,vtau, qns, qsr, emp, sfx)
@@ -423,4 +416,9 @@
       CASE( jp_blk     )
          IF( ll_sas    )       CALL sbc_cpl_rcv   ( kt, nn_fsbc, nn_ice, Kbb, Kmm )   ! OPA-SAS coupling: SAS receiving fields from OPA
+!!!!!!!!!!! ATTENTION:ln_wave is not only used for oasis coupling !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+         IF( ln_wave )   THEN
+             IF ( lk_oasis )  CALL sbc_cpl_rcv ( kt, nn_fsbc, nn_ice, Kbb, Kmm )   ! OPA-wave coupling
+             CALL sbc_wave ( kt, Kmm )
+         ENDIF
                                CALL sbc_blk       ( kt )                    ! bulk formulation for the ocean
                                !
@@ -436,5 +434,33 @@
       IF( ln_mixcpl )          CALL sbc_cpl_rcv   ( kt, nn_fsbc, nn_ice, Kbb, Kmm )  ! forced-coupled mixed formulation after forcing
       !
-      IF ( ln_wave .AND. (ln_tauwoc .OR. ln_tauw) ) CALL sbc_wstress( )              ! Wind stress provided by waves 
+      IF( ln_wave .AND. ln_tauoc )  THEN            ! Wave stress reduction
+         DO_2D( 0, 0, 0, 0)
+            utau(ji,jj) = utau(ji,jj) * ( tauoc_wave(ji,jj) + tauoc_wave(ji-1,jj) ) * 0.5_wp
+            vtau(ji,jj) = vtau(ji,jj) * ( tauoc_wave(ji,jj) + tauoc_wave(ji,jj-1) ) * 0.5_wp
+         END_2D
+         !
+         CALL lbc_lnk( 'sbcwave', utau, 'U', -1. )
+         CALL lbc_lnk( 'sbcwave', vtau, 'V', -1. )
+         !
+         taum(:,:) = taum(:,:)*tauoc_wave(:,:)
+         !
+         IF( kt == nit000 )   CALL ctl_warn( 'sbc: You are subtracting the wave stress to the ocean.',   &
+            &                                'If not requested select ln_tauoc=.false.' )
+         !
+      ELSEIF( ln_wave .AND. ln_taw ) THEN                  ! Wave stress reduction
+         utau(:,:) = utau(:,:) - tawx(:,:) + twox(:,:)
+         vtau(:,:) = vtau(:,:) - tawy(:,:) + twoy(:,:)
+         CALL lbc_lnk( 'sbcwave', utau, 'U', -1. )
+         CALL lbc_lnk( 'sbcwave', vtau, 'V', -1. )
+         !
+         DO_2D( 0, 0, 0, 0)
+             taum(ji,jj) = sqrt((.5*(utau(ji-1,jj)+utau(ji,jj)))**2 + (.5*(vtau(ji,jj-1)+vtau(ji,jj)))**2)
+         END_2D
+         !
+         IF( kt == nit000 )   CALL ctl_warn( 'sbc: You are subtracting the wave stress to the ocean.',   &
+            &                                'If not requested select ln_taw=.false.' )
+         !
+      ENDIF
+      CALL lbc_lnk( 'sbcmod', taum(:,:), 'T', 1. )
       !
       !                                            !==  Misc. Options  ==!
Index: /NEMO/branches/2020/tickets_icb_1900/src/OCE/SBC/sbcwave.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/OCE/SBC/sbcwave.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/OCE/SBC/sbcwave.F90	(revision 14016)
@@ -9,15 +9,17 @@
    !!             -   !  2016-12  (G. Madec, E. Clementi) update Stoke drift computation
    !!                                                    + add sbc_wave_ini routine
+   !!            4.2  !  2020-12  (G. Madec, E. Clementi) updates, new Stoke drift computation 
+   !!                                                    according to Couvelard et al.,2019
    !!----------------------------------------------------------------------
 
    !!----------------------------------------------------------------------
    !!   sbc_stokes    : calculate 3D Stokes-drift velocities
-   !!   sbc_wave      : wave data from wave model in netcdf files 
+   !!   sbc_wave      : wave data from wave model: forced (netcdf files) or coupled mode
    !!   sbc_wave_init : initialisation fo surface waves 
    !!----------------------------------------------------------------------
-   USE phycst         ! physical constants 
+   USE phycst         ! physical constants
    USE oce            ! ocean variables
-   USE sbc_oce	       ! Surface boundary condition: ocean fields
-   USE zdf_oce,  ONLY : ln_zdfswm
+   USE dom_oce        ! ocean domain variables
+   USE sbc_oce        ! Surface boundary condition: ocean fields
    USE bdy_oce        ! open boundary condition variables
    USE domvvl         ! domain: variable volume layers
@@ -26,5 +28,5 @@
    USE in_out_manager ! I/O manager
    USE lib_mpp        ! distribued memory computing library
-   USE fldread	       ! read input fields
+   USE fldread        ! read input fields
 
    IMPLICIT NONE
@@ -32,19 +34,21 @@
 
    PUBLIC   sbc_stokes      ! routine called in sbccpl
-   PUBLIC   sbc_wstress     ! routine called in sbcmod 
    PUBLIC   sbc_wave        ! routine called in sbcmod
    PUBLIC   sbc_wave_init   ! routine called in sbcmod
    
    ! Variables checking if the wave parameters are coupled (if not, they are read from file)
-   LOGICAL, PUBLIC ::   cpl_hsig   = .FALSE.
-   LOGICAL, PUBLIC ::   cpl_phioc  = .FALSE.
-   LOGICAL, PUBLIC ::   cpl_sdrftx = .FALSE.
-   LOGICAL, PUBLIC ::   cpl_sdrfty = .FALSE.
-   LOGICAL, PUBLIC ::   cpl_wper   = .FALSE.
-   LOGICAL, PUBLIC ::   cpl_wfreq  = .FALSE.
-   LOGICAL, PUBLIC ::   cpl_wnum   = .FALSE.
-   LOGICAL, PUBLIC ::   cpl_tauwoc = .FALSE.
-   LOGICAL, PUBLIC ::   cpl_tauw   = .FALSE.
-   LOGICAL, PUBLIC ::   cpl_wdrag  = .FALSE.
+   LOGICAL, PUBLIC ::   cpl_hsig          = .FALSE.
+   LOGICAL, PUBLIC ::   cpl_phioc         = .FALSE.
+   LOGICAL, PUBLIC ::   cpl_sdrftx        = .FALSE.
+   LOGICAL, PUBLIC ::   cpl_sdrfty        = .FALSE.
+   LOGICAL, PUBLIC ::   cpl_wper          = .FALSE.
+   LOGICAL, PUBLIC ::   cpl_wnum          = .FALSE.
+   LOGICAL, PUBLIC ::   cpl_wstrf         = .FALSE.
+   LOGICAL, PUBLIC ::   cpl_wdrag         = .FALSE.
+   LOGICAL, PUBLIC ::   cpl_charn         = .FALSE.
+   LOGICAL, PUBLIC ::   cpl_taw           = .FALSE.
+   LOGICAL, PUBLIC ::   cpl_bhd           = .FALSE.
+   LOGICAL, PUBLIC ::   cpl_tusd          = .FALSE.
+   LOGICAL, PUBLIC ::   cpl_tvsd          = .FALSE.
 
    INTEGER ::   jpfld    ! number of files to read for stokes drift
@@ -53,22 +57,32 @@
    INTEGER ::   jp_hsw   ! index of significant wave hight      (m)      at T-point
    INTEGER ::   jp_wmp   ! index of mean wave period            (s)      at T-point
-   INTEGER ::   jp_wfr   ! index of wave peak frequency         (1/s)    at T-point
 
    TYPE(FLD), ALLOCATABLE, DIMENSION(:) ::   sf_cd      ! structure of input fields (file informations, fields read) Drag Coefficient
    TYPE(FLD), ALLOCATABLE, DIMENSION(:) ::   sf_sd      ! structure of input fields (file informations, fields read) Stokes Drift
    TYPE(FLD), ALLOCATABLE, DIMENSION(:) ::   sf_wn      ! structure of input fields (file informations, fields read) wave number for Qiao
-   TYPE(FLD), ALLOCATABLE, DIMENSION(:) ::   sf_tauwoc  ! structure of input fields (file informations, fields read) normalized wave stress into the ocean
-   TYPE(FLD), ALLOCATABLE, DIMENSION(:) ::   sf_tauw    ! structure of input fields (file informations, fields read) ocean stress components from wave model
-
-   REAL(wp), PUBLIC, ALLOCATABLE, DIMENSION(:,:)   ::   cdn_wave            !:
-   REAL(wp), PUBLIC, ALLOCATABLE, DIMENSION(:,:)   ::   hsw, wmp, wnum      !: 
-   REAL(wp), PUBLIC, ALLOCATABLE, DIMENSION(:,:)   ::   wfreq               !: 
-   REAL(wp), PUBLIC, ALLOCATABLE, DIMENSION(:,:)   ::   tauoc_wave          !:  
-   REAL(wp), PUBLIC, ALLOCATABLE, DIMENSION(:,:)   ::   tauw_x, tauw_y      !:  
-   REAL(wp), PUBLIC, ALLOCATABLE, DIMENSION(:,:)   ::   tsd2d               !: 
-   REAL(wp), PUBLIC, ALLOCATABLE, DIMENSION(:,:)   ::   div_sd              !: barotropic stokes drift divergence
-   REAL(wp), PUBLIC, ALLOCATABLE, DIMENSION(:,:)   ::   ut0sd, vt0sd        !: surface Stokes drift velocities at t-point
-   REAL(wp), PUBLIC, ALLOCATABLE, DIMENSION(:,:,:) ::   usd  , vsd  , wsd   !: Stokes drift velocities at u-, v- & w-points, resp.
-
+   TYPE(FLD), ALLOCATABLE, DIMENSION(:) ::   sf_tauoc   ! structure of input fields (file informations, fields read) normalized wave stress into the ocean
+
+   REAL(wp), PUBLIC, ALLOCATABLE, DIMENSION(:,:)   ::   cdn_wave        !: Neutral drag coefficient at t-point
+   REAL(wp), PUBLIC, ALLOCATABLE, DIMENSION(:,:)   ::   hsw             !: Significant Wave Height at t-point
+   REAL(wp), PUBLIC, ALLOCATABLE, DIMENSION(:,:)   ::   wmp             !: Wave Mean Period at t-point
+   REAL(wp), PUBLIC, ALLOCATABLE, DIMENSION(:,:)   ::   wnum            !: Wave Number at t-point
+   REAL(wp), PUBLIC, ALLOCATABLE, DIMENSION(:,:)   ::   tauoc_wave      !: stress reduction factor  at t-point
+   REAL(wp), PUBLIC, ALLOCATABLE, DIMENSION(:,:)   ::   tsd2d           !: Surface Stokes Drift module at t-point
+   REAL(wp), PUBLIC, ALLOCATABLE, DIMENSION(:,:)   ::   div_sd          !: barotropic stokes drift divergence
+   REAL(wp), PUBLIC, ALLOCATABLE, DIMENSION(:,:)   ::   ut0sd, vt0sd    !: surface Stokes drift velocities at t-point
+   REAL(wp), PUBLIC, ALLOCATABLE, DIMENSION(:,:,:) ::   usd, vsd, wsd   !: Stokes drift velocities at u-, v- & w-points, resp.u
+!
+   REAL(wp), PUBLIC, ALLOCATABLE, DIMENSION(:,:)   ::   charn           !: charnock coefficient at t-point
+   REAL(wp), PUBLIC, ALLOCATABLE, DIMENSION(:,:)   ::   tawx            !: Net wave-supported stress, u
+   REAL(wp), PUBLIC, ALLOCATABLE, DIMENSION(:,:)   ::   tawy            !: Net wave-supported stress, v
+   REAL(wp), PUBLIC, ALLOCATABLE, DIMENSION(:,:)   ::   twox            !: wave-ocean momentum flux, u
+   REAL(wp), PUBLIC, ALLOCATABLE, DIMENSION(:,:)   ::   twoy            !: wave-ocean momentum flux, v
+   REAL(wp), PUBLIC, ALLOCATABLE, DIMENSION(:,:)   ::   tauoc_wavex     !: stress reduction factor  at, u component
+   REAL(wp), PUBLIC, ALLOCATABLE, DIMENSION(:,:)   ::   tauoc_wavey     !: stress reduction factor  at, v component
+   REAL(wp), PUBLIC, ALLOCATABLE, DIMENSION(:,:)   ::   phioc           !: tke flux from wave model
+   REAL(wp), PUBLIC, ALLOCATABLE, DIMENSION(:,:)   ::   KZN2            !: Kz*N2
+   REAL(wp), PUBLIC, ALLOCATABLE, DIMENSION(:,:)   ::   bhd_wave        !: Bernoulli head. wave induce pression
+   REAL(wp), PUBLIC, ALLOCATABLE, DIMENSION(:,:)   ::   tusd, tvsd      !: Stokes drift transport
+   REAL(wp), PUBLIC, ALLOCATABLE, DIMENSION(:,:,:) ::   ZMX             !: Kz*N2
    !! * Substitutions
 #  include "do_loop_substitute.h90"
@@ -88,28 +102,79 @@
       !!                2014 (DOI: 10.1175/JPO-D-14-0020.1)
       !!
-      !! ** Method  : - Calculate Stokes transport speed 
-      !!              - Calculate horizontal divergence 
-      !!              - Integrate the horizontal divergenze from the bottom 
-      !! ** action  
+      !! ** Method  : - Calculate the horizontal Stokes drift velocity (Breivik et al. 2014)
+      !!              - Calculate its horizontal divergence
+      !!              - Calculate the vertical Stokes drift velocity
+      !!              - Calculate the barotropic Stokes drift divergence
+      !!
+      !! ** action  : - tsd2d         : module of the surface Stokes drift velocity
+      !!              - usd, vsd, wsd : 3 components of the Stokes drift velocity
+      !!              - div_sd        : barotropic Stokes drift divergence
       !!---------------------------------------------------------------------
       INTEGER, INTENT(in) :: Kmm ! ocean time level index
       INTEGER  ::   jj, ji, jk   ! dummy loop argument
       INTEGER  ::   ik           ! local integer 
-      REAL(wp) ::  ztransp, zfac, zsp0
-      REAL(wp) ::  zdepth, zsqrt_depth,  zexp_depth, z_two_thirds, zsqrtpi !sqrt of pi
-      REAL(wp) ::  zbot_u, zbot_v, zkb_u, zkb_v, zke3_u, zke3_v, zda_u, zda_v
-      REAL(wp) ::  zstokes_psi_u_bot, zstokes_psi_v_bot
-      REAL(wp) ::  zdep_u, zdep_v, zkh_u, zkh_v
-      REAL(wp), DIMENSION(:,:)  , ALLOCATABLE ::   zk_t, zk_u, zk_v, zu0_sd, zv0_sd     ! 2D workspace
-      REAL(wp), DIMENSION(:,:)  , ALLOCATABLE ::   zstokes_psi_u_top, zstokes_psi_v_top ! 2D workspace
-      REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::   ze3divh                              ! 3D workspace
-      !!---------------------------------------------------------------------
-      !
-      ALLOCATE( ze3divh(jpi,jpj,jpkm1) )   ! jpkm1 -> avoid lbc_lnk on jpk that is not defined
+      REAL(wp) ::  ztransp, zfac, ztemp, zsp0, zsqrt, zbreiv16_w
+      REAL(wp) ::  zdep_u, zdep_v, zkh_u, zkh_v, zda_u, zda_v, sdtrp
+      REAL(wp), DIMENSION(:,:)  , ALLOCATABLE ::   zk_t, zk_u, zk_v, zu0_sd, zv0_sd ! 2D workspace
+      REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::   ze3divh, zInt_w                  ! 3D workspace
+      !!---------------------------------------------------------------------
+      !
+      ALLOCATE( ze3divh(jpi,jpj,jpkm1) ) ! jpkm1 -> avoid lbc_lnk on jpk that is not defined
+      ALLOCATE( zInt_w(jpi,jpj,jpk) )
       ALLOCATE( zk_t(jpi,jpj), zk_u(jpi,jpj), zk_v(jpi,jpj), zu0_sd(jpi,jpj), zv0_sd(jpi,jpj) )
+      zk_t    (:,:) = 0._wp
+      zk_u    (:,:) = 0._wp
+      zk_v    (:,:) = 0._wp
+      zu0_sd  (:,:) = 0._wp
+      zv0_sd  (:,:) = 0._wp
+      ze3divh (:,:,:) = 0._wp
+
       !
       ! select parameterization for the calculation of vertical Stokes drift
       ! exp. wave number at t-point
-      IF( ll_st_bv_li ) THEN   ! (Eq. (19) in Breivik et al. (2014) )
+      IF( ln_breivikFV_2016 ) THEN
+      ! Assumptions :  ut0sd and vt0sd are surface Stokes drift at T-points
+      !                sdtrp is the norm of Stokes transport
+      !
+         zfac = 0.166666666667_wp
+         DO_2D( 1, 1, 1, 1 ) ! In the deep-water limit we have ke = ||ust0||/( 6 * ||transport|| )
+            zsp0          = SQRT( ut0sd(ji,jj)*ut0sd(ji,jj) + vt0sd(ji,jj)*vt0sd(ji,jj) ) !<-- norm of Surface Stokes drift
+            tsd2d(ji,jj)  = zsp0
+            IF( cpl_tusd .AND. cpl_tvsd ) THEN  !stokes transport is provided in coupled mode
+               sdtrp      = SQRT( tusd(ji,jj)*tusd(ji,jj) + tvsd(ji,jj)*tvsd(ji,jj) )  !<-- norm of Surface Stokes drift transport
+            ELSE 
+               ! Stokes drift transport estimated from Hs and Tmean 
+               sdtrp      = 2.0_wp * rpi / 16.0_wp *                             &
+                   &        hsw(ji,jj)*hsw(ji,jj) / MAX( wmp(ji,jj), 0.0000001_wp )
+            ENDIF
+            zk_t (ji,jj)  = zfac * zsp0 / MAX ( sdtrp, 0.0000001_wp ) !<-- ke = ||ust0||/( 6 * ||transport|| )
+         END_2D
+      !# define zInt_w ze3divh
+         DO_3D( 1, 1, 1, 1, 1, jpk ) ! Compute the primitive of Breivik 2016 function at W-points
+            zfac             = - 2._wp * zk_t (ji,jj) * gdepw(ji,jj,jk,Kmm)  !<-- zfac should be negative definite
+            ztemp            = EXP ( zfac )
+            zsqrt            = SQRT( -zfac )
+            zbreiv16_w       = ztemp - SQRT(rpi)*zsqrt*ERFC(zsqrt) !Eq. 16 Breivik 2016
+            zInt_w(ji,jj,jk) = ztemp - 4._wp * zk_t (ji,jj) * gdepw(ji,jj,jk,Kmm) * zbreiv16_w
+         END_3D
+!
+         DO jk = 1, jpkm1
+            zfac = 0.166666666667_wp
+            DO_2D( 1, 1, 1, 1 ) !++ Compute the FV Breivik 2016 function at T-points
+               zsp0          = zfac / MAX(zk_t (ji,jj),0.0000001_wp)
+               ztemp         = zInt_w(ji,jj,jk) - zInt_w(ji,jj,jk+1)
+               zu0_sd(ji,jj) = ut0sd(ji,jj) * zsp0 * ztemp * tmask(ji,jj,jk)
+               zv0_sd(ji,jj) = vt0sd(ji,jj) * zsp0 * ztemp * tmask(ji,jj,jk)
+            END_2D
+            DO_2D( 1, 0, 1, 0 ) ! ++ Interpolate at U/V points
+               zfac          =  1.0_wp / e3u(ji  ,jj,jk,Kmm)
+               usd(ji,jj,jk) =  0.5_wp * zfac * ( zu0_sd(ji,jj)+zu0_sd(ji+1,jj) ) * umask(ji,jj,jk)
+               zfac          =  1.0_wp / e3v(ji  ,jj,jk,Kmm)
+               vsd(ji,jj,jk) =  0.5_wp * zfac * ( zv0_sd(ji,jj)+zv0_sd(ji,jj+1) ) * vmask(ji,jj,jk)
+            END_2D
+         ENDDO
+      !# undef zInt_w
+      !
+      ELSE
          zfac = 2.0_wp * rpi / 16.0_wp
          DO_2D( 1, 1, 1, 1 )
@@ -128,23 +193,11 @@
             zv0_sd(ji,jj) = 0.5_wp * ( vt0sd(ji,jj) + vt0sd(ji,jj+1) )
          END_2D
-      ELSE IF( ll_st_peakfr ) THEN    ! peak wave number calculated from the peak frequency received by the wave model
-         DO_2D( 1, 1, 1, 1 )
-            zk_t(ji,jj) = ( 2.0_wp * rpi * wfreq(ji,jj) ) * ( 2.0_wp * rpi * wfreq(ji,jj) ) / grav
-         END_2D
-         DO_2D( 1, 0, 1, 0 )
-            zk_u(ji,jj) = 0.5_wp * ( zk_t(ji,jj) + zk_t(ji+1,jj) )
-            zk_v(ji,jj) = 0.5_wp * ( zk_t(ji,jj) + zk_t(ji,jj+1) )
-            !
-            zu0_sd(ji,jj) = 0.5_wp * ( ut0sd(ji,jj) + ut0sd(ji+1,jj) )
-            zv0_sd(ji,jj) = 0.5_wp * ( vt0sd(ji,jj) + vt0sd(ji,jj+1) )
-         END_2D
-      ENDIF
-      !
+
       !                       !==  horizontal Stokes Drift 3D velocity  ==!
-      IF( ll_st_bv2014 ) THEN
+
          DO_3D( 0, 0, 0, 0, 1, jpkm1 )
             zdep_u = 0.5_wp * ( gdept(ji,jj,jk,Kmm) + gdept(ji+1,jj,jk,Kmm) )
             zdep_v = 0.5_wp * ( gdept(ji,jj,jk,Kmm) + gdept(ji,jj+1,jk,Kmm) )
-            !                          
+            !
             zkh_u = zk_u(ji,jj) * zdep_u     ! k * depth
             zkh_v = zk_v(ji,jj) * zdep_v
@@ -156,45 +209,4 @@
             vsd(ji,jj,jk) = zda_v * zv0_sd(ji,jj) * vmask(ji,jj,jk)
          END_3D
-      ELSE IF( ll_st_li2017 .OR. ll_st_peakfr ) THEN
-         ALLOCATE( zstokes_psi_u_top(jpi,jpj), zstokes_psi_v_top(jpi,jpj) )
-         DO_2D( 1, 0, 1, 0 )
-            zstokes_psi_u_top(ji,jj) = 0._wp
-            zstokes_psi_v_top(ji,jj) = 0._wp
-         END_2D
-         zsqrtpi = SQRT(rpi)
-         z_two_thirds = 2.0_wp / 3.0_wp
-         DO_3D( 0, 0, 0, 0, 1, jpkm1 )       ! exp. wave number & Stokes drift velocity at u- & v-points
-            zbot_u = ( gdepw(ji,jj,jk+1,Kmm) + gdepw(ji+1,jj,jk+1,Kmm) )  ! 2 * bottom depth
-            zbot_v = ( gdepw(ji,jj,jk+1,Kmm) + gdepw(ji,jj+1,jk+1,Kmm) )  ! 2 * bottom depth
-            zkb_u  = zk_u(ji,jj) * zbot_u                             ! 2 * k * bottom depth
-            zkb_v  = zk_v(ji,jj) * zbot_v                             ! 2 * k * bottom depth
-            !
-            zke3_u = MAX(1.e-8_wp, 2.0_wp * zk_u(ji,jj) * e3u(ji,jj,jk,Kmm))     ! 2k * thickness
-            zke3_v = MAX(1.e-8_wp, 2.0_wp * zk_v(ji,jj) * e3v(ji,jj,jk,Kmm))     ! 2k * thickness
-
-            ! Depth attenuation .... do u component first..
-            zdepth      = zkb_u
-            zsqrt_depth = SQRT(zdepth)
-            zexp_depth  = EXP(-zdepth)
-            zstokes_psi_u_bot = 1.0_wp - zexp_depth  &
-                 &              - z_two_thirds * ( zsqrtpi*zsqrt_depth*zdepth*ERFC(zsqrt_depth) &
-                 &              + 1.0_wp - (1.0_wp + zdepth)*zexp_depth )
-            zda_u                    = ( zstokes_psi_u_bot - zstokes_psi_u_top(ji,jj) ) / zke3_u
-            zstokes_psi_u_top(ji,jj) =   zstokes_psi_u_bot
-
-            !         ... and then v component
-            zdepth      =zkb_v
-            zsqrt_depth = SQRT(zdepth)
-            zexp_depth  = EXP(-zdepth)
-            zstokes_psi_v_bot = 1.0_wp - zexp_depth  &
-                 &              - z_two_thirds * ( zsqrtpi*zsqrt_depth*zdepth*ERFC(zsqrt_depth) &
-                 &              + 1.0_wp - (1.0_wp + zdepth)*zexp_depth )
-            zda_v                    = ( zstokes_psi_v_bot - zstokes_psi_v_top(ji,jj) ) / zke3_v
-            zstokes_psi_v_top(ji,jj) =   zstokes_psi_v_bot
-            !
-            usd(ji,jj,jk) = zda_u * zu0_sd(ji,jj) * umask(ji,jj,jk)
-            vsd(ji,jj,jk) = zda_v * zv0_sd(ji,jj) * vmask(ji,jj,jk)
-         END_3D
-         DEALLOCATE( zstokes_psi_u_top, zstokes_psi_v_top )
       ENDIF
 
@@ -235,63 +247,28 @@
       CALL iom_put( "vstokes",  vsd  )
       CALL iom_put( "wstokes",  wsd  )
-      !
-      DEALLOCATE( ze3divh )
+!      !
+      DEALLOCATE( ze3divh, zInt_w )
       DEALLOCATE( zk_t, zk_u, zk_v, zu0_sd, zv0_sd )
       !
    END SUBROUTINE sbc_stokes
-
-
-   SUBROUTINE sbc_wstress( )
-      !!---------------------------------------------------------------------
-      !!                     ***  ROUTINE sbc_wstress  ***
-      !!
-      !! ** Purpose :   Updates the ocean momentum modified by waves
-      !!
-      !! ** Method  : - Calculate u,v components of stress depending on stress
-      !!                model 
-      !!              - Calculate the stress module
-      !!              - The wind module is not modified by waves 
-      !! ** action  
-      !!---------------------------------------------------------------------
-      INTEGER  ::   jj, ji   ! dummy loop argument
-      !
-      IF( ln_tauwoc ) THEN
-         utau(:,:) = utau(:,:)*tauoc_wave(:,:)
-         vtau(:,:) = vtau(:,:)*tauoc_wave(:,:)
-         taum(:,:) = taum(:,:)*tauoc_wave(:,:)
-      ENDIF
-      !
-      IF( ln_tauw ) THEN
-         DO_2D( 1, 0, 1, 0 )
-            ! Stress components at u- & v-points
-            utau(ji,jj) = 0.5_wp * ( tauw_x(ji,jj) + tauw_x(ji+1,jj) )
-            vtau(ji,jj) = 0.5_wp * ( tauw_y(ji,jj) + tauw_y(ji,jj+1) )
-            !
-            ! Stress module at t points
-            taum(ji,jj) = SQRT( tauw_x(ji,jj)*tauw_x(ji,jj) + tauw_y(ji,jj)*tauw_y(ji,jj) )
-         END_2D
-         CALL lbc_lnk_multi( 'sbcwave', utau(:,:), 'U', -1.0_wp , vtau(:,:), 'V', -1.0_wp , taum(:,:) , 'T', -1.0_wp )
-      ENDIF
-      !
-   END SUBROUTINE sbc_wstress
-
-
+!
+!
    SUBROUTINE sbc_wave( kt, Kmm )
       !!---------------------------------------------------------------------
       !!                     ***  ROUTINE sbc_wave  ***
       !!
-      !! ** Purpose :   read wave parameters from wave model  in netcdf files.
-      !!
-      !! ** Method  : - Read namelist namsbc_wave
-      !!              - Read Cd_n10 fields in netcdf files 
-      !!              - Read stokes drift 2d in netcdf files 
-      !!              - Read wave number in netcdf files 
-      !!              - Compute 3d stokes drift using Breivik et al.,2014
-      !!                formulation
-      !! ** action  
+      !! ** Purpose :   read wave parameters from wave model in netcdf files
+      !!                or from a coupled wave mdoel
+      !!
       !!---------------------------------------------------------------------
       INTEGER, INTENT(in   ) ::   kt   ! ocean time step
       INTEGER, INTENT(in   ) ::   Kmm  ! ocean time index
       !!---------------------------------------------------------------------
+      !
+      IF( kt == nit000 .AND. lwp ) THEN
+         WRITE(numout,*)
+         WRITE(numout,*) 'sbc_wave : update the read waves fields'
+         WRITE(numout,*) '~~~~~~~~ '
+      ENDIF
       !
       IF( ln_cdgw .AND. .NOT. cpl_wdrag ) THEN     !==  Neutral drag coefficient  ==!
@@ -300,40 +277,44 @@
       ENDIF
 
-      IF( ln_tauwoc .AND. .NOT. cpl_tauwoc ) THEN  !==  Wave induced stress  ==!
-         CALL fld_read( kt, nn_fsbc, sf_tauwoc )         ! read wave norm stress from external forcing
-         tauoc_wave(:,:) = sf_tauwoc(1)%fnow(:,:,1) * tmask(:,:,1)
-      ENDIF
-
-      IF( ln_tauw .AND. .NOT. cpl_tauw ) THEN      !==  Wave induced stress  ==!
-         CALL fld_read( kt, nn_fsbc, sf_tauw )           ! read ocean stress components from external forcing (T grid)
-         tauw_x(:,:) = sf_tauw(1)%fnow(:,:,1) * tmask(:,:,1)
-         tauw_y(:,:) = sf_tauw(2)%fnow(:,:,1) * tmask(:,:,1)
-      ENDIF
-
-      IF( ln_sdw )  THEN                           !==  Computation of the 3d Stokes Drift  ==! 
+      IF( ln_tauoc .AND. .NOT. cpl_wstrf ) THEN    !==  Wave induced stress  ==!
+         CALL fld_read( kt, nn_fsbc, sf_tauoc )          ! read stress reduction factor due to wave from external forcing
+         tauoc_wave(:,:) = sf_tauoc(1)%fnow(:,:,1) * tmask(:,:,1)
+      ELSEIF ( ln_taw .AND. cpl_taw ) THEN
+         IF (kt < 1) THEN ! The first fields gave by OASIS have very high erroneous values ....
+            twox(:,:)=0._wp
+            twoy(:,:)=0._wp
+            tawx(:,:)=0._wp
+            tawy(:,:)=0._wp
+            tauoc_wavex(:,:) = 1._wp
+            tauoc_wavey(:,:) = 1._wp
+         ELSE
+            tauoc_wavex(:,:) = abs(twox(:,:)/tawx(:,:))
+            tauoc_wavey(:,:) = abs(twoy(:,:)/tawy(:,:))
+         ENDIF
+      ENDIF
+
+      IF ( ln_phioc .and. cpl_phioc .and.  kt == nit000 ) THEN
+         WRITE(numout,*)
+         WRITE(numout,*) 'sbc_wave : PHIOC from wave model'
+         WRITE(numout,*) '~~~~~~~~ '
+      ENDIF
+
+      IF( ln_sdw .AND. .NOT. cpl_sdrftx)  THEN       !==  Computation of the 3d Stokes Drift  ==! 
          !
          IF( jpfld > 0 ) THEN                            ! Read from file only if the field is not coupled
             CALL fld_read( kt, nn_fsbc, sf_sd )          ! read wave parameters from external forcing
+            !                                            ! NB: test case mode, not read as jpfld=0
             IF( jp_hsw > 0 )   hsw  (:,:) = sf_sd(jp_hsw)%fnow(:,:,1) * tmask(:,:,1)  ! significant wave height
             IF( jp_wmp > 0 )   wmp  (:,:) = sf_sd(jp_wmp)%fnow(:,:,1) * tmask(:,:,1)  ! wave mean period
-            IF( jp_wfr > 0 )   wfreq(:,:) = sf_sd(jp_wfr)%fnow(:,:,1) * tmask(:,:,1)  ! Peak wave frequency
             IF( jp_usd > 0 )   ut0sd(:,:) = sf_sd(jp_usd)%fnow(:,:,1) * tmask(:,:,1)  ! 2D zonal Stokes Drift at T point
             IF( jp_vsd > 0 )   vt0sd(:,:) = sf_sd(jp_vsd)%fnow(:,:,1) * tmask(:,:,1)  ! 2D meridional Stokes Drift at T point
          ENDIF
          !
-         ! Read also wave number if needed, so that it is available in coupling routines
-         IF( ln_zdfswm .AND. .NOT.cpl_wnum ) THEN
-            CALL fld_read( kt, nn_fsbc, sf_wn )          ! read wave parameters from external forcing
-            wnum(:,:) = sf_wn(1)%fnow(:,:,1) * tmask(:,:,1)
-         ENDIF
-           
-         ! Calculate only if required fields have been read
-         ! In coupled wave model-NEMO case the call is done after coupling
+         IF( jpfld == 4 .OR. ln_wave_test )   &
+            &      CALL sbc_stokes( Kmm )                 ! Calculate only if all required fields are read
+            !                                            ! or in wave test case
+         !  !                                            ! In coupled case the call is done after (in sbc_cpl)
+      ENDIF
          !
-         IF( ( ll_st_bv_li   .AND. jp_hsw>0 .AND. jp_wmp>0 .AND. jp_usd>0 .AND. jp_vsd>0 ) .OR. &
-           & ( ll_st_peakfr  .AND. jp_wfr>0 .AND. jp_usd>0 .AND. jp_vsd>0                ) ) CALL sbc_stokes( Kmm )
-         !
-      ENDIF
-      !
    END SUBROUTINE sbc_wave
 
@@ -343,12 +324,9 @@
       !!                     ***  ROUTINE sbc_wave_init  ***
       !!
-      !! ** Purpose :   read wave parameters from wave model  in netcdf files.
+      !! ** Purpose :   Initialisation fo surface waves
       !!
       !! ** Method  : - Read namelist namsbc_wave
-      !!              - Read Cd_n10 fields in netcdf files 
-      !!              - Read stokes drift 2d in netcdf files 
-      !!              - Read wave number in netcdf files 
-      !!              - Compute 3d stokes drift using Breivik et al.,2014
-      !!                formulation
+      !!              - create the structure used to read required wave fields
+      !!                (its size depends on namelist options)
       !! ** action  
       !!---------------------------------------------------------------------
@@ -357,129 +335,185 @@
       !!
       CHARACTER(len=100)     ::  cn_dir                            ! Root directory for location of drag coefficient files
-      TYPE(FLD_N), ALLOCATABLE, DIMENSION(:) ::   slf_i, slf_j     ! array of namelist informations on the fields to read
+      TYPE(FLD_N), ALLOCATABLE, DIMENSION(:) ::   slf_i            ! array of namelist informations on the fields to read
       TYPE(FLD_N)            ::  sn_cdg, sn_usd, sn_vsd,  &
-                             &   sn_hsw, sn_wmp, sn_wfr, sn_wnum, &
-                             &   sn_tauwoc, sn_tauwx, sn_tauwy     ! informations about the fields to be read
-      !
-      NAMELIST/namsbc_wave/  sn_cdg, cn_dir, sn_usd, sn_vsd, sn_hsw, sn_wmp, sn_wfr, &
-                             sn_wnum, sn_tauwoc, sn_tauwx, sn_tauwy
-      !!---------------------------------------------------------------------
+                             &   sn_hsw, sn_wmp, sn_wnum, sn_tauoc    ! informations about the fields to be read
+      !
+      NAMELIST/namsbc_wave/ cn_dir, sn_cdg, sn_usd, sn_vsd, sn_hsw, sn_wmp, sn_wnum, sn_tauoc,   &
+         &                  ln_cdgw, ln_sdw, ln_tauoc, ln_stcor, ln_charn, ln_taw, ln_phioc,     &
+         &                  ln_wave_test, ln_bern_srfc, ln_breivikFV_2016, ln_vortex_force, ln_stshear
+      !!---------------------------------------------------------------------
+      IF(lwp) THEN
+         WRITE(numout,*)
+         WRITE(numout,*) 'sbc_wave_init : surface waves in the system'
+         WRITE(numout,*) '~~~~~~~~~~~~~ '
+      ENDIF
       !
       READ  ( numnam_ref, namsbc_wave, IOSTAT = ios, ERR = 901)
-901   IF( ios /= 0 )   CALL ctl_nam ( ios , 'namsbc_wave in reference namelist' )
-         
+901   IF( ios /= 0 )   CALL ctl_nam ( ios , 'namsbc_wave in reference namelist')
+
       READ  ( numnam_cfg, namsbc_wave, IOSTAT = ios, ERR = 902 )
 902   IF( ios >  0 )   CALL ctl_nam ( ios , 'namsbc_wave in configuration namelist' )
       IF(lwm) WRITE ( numond, namsbc_wave )
       !
-      IF( ln_cdgw ) THEN
-         IF( .NOT. cpl_wdrag ) THEN
-            ALLOCATE( sf_cd(1), STAT=ierror )               !* allocate and fill sf_wave with sn_cdg
-            IF( ierror > 0 )   CALL ctl_stop( 'STOP', 'sbc_wave_init: unable to allocate sf_wave structure' )
+      IF(lwp) THEN
+         WRITE(numout,*) '   Namelist namsbc_wave'
+         WRITE(numout,*) '      Stokes drift                                  ln_sdw = ', ln_sdw
+         WRITE(numout,*) '      Breivik 2016                       ln_breivikFV_2016 = ', ln_breivikFV_2016
+         WRITE(numout,*) '      Stokes Coriolis & tracer advection terms    ln_stcor = ', ln_stcor
+         WRITE(numout,*) '      Vortex Force                         ln_vortex_force = ', ln_vortex_force
+         WRITE(numout,*) '      Bernouilli Head Pressure                ln_bern_srfc = ', ln_bern_srfc
+         WRITE(numout,*) '      wave modified ocean stress                  ln_tauoc = ', ln_tauoc
+         WRITE(numout,*) '      neutral drag coefficient (CORE bulk only)    ln_cdgw = ', ln_cdgw
+         WRITE(numout,*) '      charnock coefficient                        ln_charn = ', ln_charn
+         WRITE(numout,*) '      Stress modificated by wave                    ln_taw = ', ln_taw
+         WRITE(numout,*) '      TKE flux from wave                          ln_phioc = ', ln_phioc
+         WRITE(numout,*) '      Surface shear with Stokes drift           ln_stshear = ', ln_stshear
+         WRITE(numout,*) '      Test with constant wave fields          ln_wave_test = ', ln_wave_test
+      ENDIF
+
+      !                                ! option check
+      IF( .NOT.( ln_cdgw .OR. ln_sdw .OR. ln_tauoc .OR. ln_stcor .OR. ln_charn) )   &
+         &     CALL ctl_warn( 'Ask for wave coupling but ln_cdgw=F, ln_sdw=F, ln_tauoc=F, ln_stcor=F')
+      IF( ln_cdgw .AND. ln_blk )   &
+         &     CALL ctl_stop( 'drag coefficient read from wave model NOT available yet with aerobulk package')
+      IF( ln_stcor .AND. .NOT.ln_sdw )   &
+         &     CALL ctl_stop( 'Stokes-Coriolis term calculated only if activated Stokes Drift ln_sdw=T')
+
+      !                             !==  Allocate wave arrays  ==!
+      ALLOCATE( ut0sd (jpi,jpj)    , vt0sd (jpi,jpj) )
+      ALLOCATE( hsw   (jpi,jpj)    , wmp   (jpi,jpj) )
+      ALLOCATE( wnum  (jpi,jpj) )
+      ALLOCATE( tsd2d (jpi,jpj)    , div_sd(jpi,jpj)    , bhd_wave(jpi,jpj)     )
+      ALLOCATE( usd   (jpi,jpj,jpk), vsd   (jpi,jpj,jpk), wsd     (jpi,jpj,jpk) )
+      ALLOCATE( tusd  (jpi,jpj)    , tvsd  (jpi,jpj)    , ZMX     (jpi,jpj,jpk) )
+      usd   (:,:,:) = 0._wp
+      vsd   (:,:,:) = 0._wp
+      wsd   (:,:,:) = 0._wp
+      hsw     (:,:) = 0._wp
+      wmp     (:,:) = 0._wp
+      ut0sd   (:,:) = 0._wp
+      vt0sd   (:,:) = 0._wp
+      tusd    (:,:) = 0._wp
+      tvsd    (:,:) = 0._wp
+      bhd_wave(:,:) = 0._wp
+      ZMX   (:,:,:) = 0._wp
+!
+      IF( ln_wave_test ) THEN       !==  Wave TEST case  ==!   set uniform waves fields
+         jpfld    = 0                   ! No field read
+         ln_cdgw  = .FALSE.             ! No neutral wave drag input
+         ln_tauoc = .FALSE.             ! No wave induced drag reduction factor
+         ut0sd(:,:) = 0.13_wp * tmask(:,:,1)   ! m/s
+         vt0sd(:,:) = 0.00_wp                  ! m/s
+         hsw  (:,:) = 2.80_wp                  ! meters
+         wmp  (:,:) = 8.00_wp                  ! seconds
+         !
+      ELSE                          !==  create the structure associated with fields to be read  ==!
+         IF( ln_cdgw ) THEN                       ! wave drag
+            IF( .NOT. cpl_wdrag ) THEN
+               ALLOCATE( sf_cd(1), STAT=ierror )               !* allocate and fill sf_wave with sn_cdg
+               IF( ierror > 0 )   CALL ctl_stop( 'STOP', 'sbc_wave_init: unable to allocate sf_wave structure' )
+               !
+                                      ALLOCATE( sf_cd(1)%fnow(jpi,jpj,1)   )
+               IF( sn_cdg%ln_tint )   ALLOCATE( sf_cd(1)%fdta(jpi,jpj,1,2) )
+               CALL fld_fill( sf_cd, (/ sn_cdg /), cn_dir, 'sbc_wave_init', 'Wave module ', 'namsbc_wave' )
+            ENDIF
+            ALLOCATE( cdn_wave(jpi,jpj) )
+            cdn_wave(:,:) = 0._wp
+         ENDIF
+         IF( ln_charn ) THEN                     ! wave drag
+            IF( .NOT. cpl_charn ) THEN
+               CALL ctl_stop( 'STOP', 'Charnock based wind stress can be used in coupled mode only' )
+            ENDIF
+            ALLOCATE( charn(jpi,jpj) )
+            charn(:,:) = 0._wp
+         ENDIF
+         IF( ln_taw ) THEN                     ! wind stress
+            IF( .NOT. cpl_taw ) THEN
+               CALL ctl_stop( 'STOP', 'wind stress from wave model can be used in coupled mode only, use ln_cdgw instead' )
+            ENDIF
+            ALLOCATE( tawx(jpi,jpj) )
+            ALLOCATE( tawy(jpi,jpj) )
+            ALLOCATE( twox(jpi,jpj) )
+            ALLOCATE( twoy(jpi,jpj) )
+            ALLOCATE( tauoc_wavex(jpi,jpj) )
+            ALLOCATE( tauoc_wavey(jpi,jpj) )
+            tawx(:,:) = 0._wp
+            tawy(:,:) = 0._wp
+            twox(:,:) = 0._wp
+            twoy(:,:) = 0._wp
+            tauoc_wavex(:,:) = 1._wp
+            tauoc_wavey(:,:) = 1._wp
+         ENDIF
+
+         IF( ln_phioc ) THEN                     ! TKE flux
+            IF( .NOT. cpl_phioc ) THEN
+                CALL ctl_stop( 'STOP', 'phioc can be used in coupled mode only' )
+            ENDIF
+            ALLOCATE( phioc(jpi,jpj) )
+            phioc(:,:) = 0._wp
+         ENDIF
+
+         IF( ln_tauoc ) THEN                    ! normalized wave stress into the ocean
+            IF( .NOT. cpl_wstrf ) THEN
+               ALLOCATE( sf_tauoc(1), STAT=ierror )           !* allocate and fill sf_wave with sn_tauoc
+               IF( ierror > 0 )   CALL ctl_stop( 'STOP', 'sbc_wave_init: unable to allocate sf_tauoc structure' )
+               !
+                                       ALLOCATE( sf_tauoc(1)%fnow(jpi,jpj,1)   )
+               IF( sn_tauoc%ln_tint )  ALLOCATE( sf_tauoc(1)%fdta(jpi,jpj,1,2) )
+               CALL fld_fill( sf_tauoc, (/ sn_tauoc /), cn_dir, 'sbc_wave_init', 'Wave module', 'namsbc_wave' )
+            ENDIF
+            ALLOCATE( tauoc_wave(jpi,jpj) )
+            tauoc_wave(:,:) = 0._wp
+         ENDIF
+
+         IF( ln_sdw ) THEN                      ! Stokes drift
+            ! 1. Find out how many fields have to be read from file if not coupled
+            jpfld=0
+            jp_usd=0   ;   jp_vsd=0   ;   jp_hsw=0   ;   jp_wmp=0
+            IF( .NOT. cpl_sdrftx ) THEN
+               jpfld  = jpfld + 1
+               jp_usd = jpfld
+            ENDIF
+            IF( .NOT. cpl_sdrfty ) THEN
+               jpfld  = jpfld + 1
+               jp_vsd = jpfld
+            ENDIF
+            IF( .NOT. cpl_hsig ) THEN
+               jpfld  = jpfld + 1
+               jp_hsw = jpfld
+            ENDIF
+            IF( .NOT. cpl_wper ) THEN
+               jpfld  = jpfld + 1
+               jp_wmp = jpfld
+            ENDIF
+            ! 2. Read from file only the non-coupled fields 
+            IF( jpfld > 0 ) THEN
+               ALLOCATE( slf_i(jpfld) )
+               IF( jp_usd > 0 )   slf_i(jp_usd) = sn_usd
+               IF( jp_vsd > 0 )   slf_i(jp_vsd) = sn_vsd
+               IF( jp_hsw > 0 )   slf_i(jp_hsw) = sn_hsw
+               IF( jp_wmp > 0 )   slf_i(jp_wmp) = sn_wmp
+               ALLOCATE( sf_sd(jpfld), STAT=ierror )   !* allocate and fill sf_sd with stokes drift
+               IF( ierror > 0 )   CALL ctl_stop( 'STOP', 'sbc_wave_init: unable to allocate sf_wave structure' )
+               !
+               DO ifpr= 1, jpfld
+                  ALLOCATE( sf_sd(ifpr)%fnow(jpi,jpj,1) )
+                  IF( slf_i(ifpr)%ln_tint )   ALLOCATE( sf_sd(ifpr)%fdta(jpi,jpj,1,2) )
+               END DO
+               !
+               CALL fld_fill( sf_sd, slf_i, cn_dir, 'sbc_wave_init', 'Wave module ', 'namsbc_wave' )
+            ENDIF
             !
-                                   ALLOCATE( sf_cd(1)%fnow(jpi,jpj,1)   )
-            IF( sn_cdg%ln_tint )   ALLOCATE( sf_cd(1)%fdta(jpi,jpj,1,2) )
-            CALL fld_fill( sf_cd, (/ sn_cdg /), cn_dir, 'sbc_wave_init', 'Wave module ', 'namsbc_wave' )
-         ENDIF
-         ALLOCATE( cdn_wave(jpi,jpj) )
-      ENDIF
-
-      IF( ln_tauwoc ) THEN
-         IF( .NOT. cpl_tauwoc ) THEN
-            ALLOCATE( sf_tauwoc(1), STAT=ierror )           !* allocate and fill sf_wave with sn_tauwoc
-            IF( ierror > 0 )   CALL ctl_stop( 'STOP', 'sbc_wave_init: unable to allocate sf_wave structure' )
+            ! 3. Wave number (only needed for Qiao parametrisation, ln_zdfqiao=T)
+            IF( .NOT. cpl_wnum ) THEN
+               ALLOCATE( sf_wn(1), STAT=ierror )           !* allocate and fill sf_wave with sn_wnum
+               IF( ierror > 0 )   CALL ctl_stop( 'STOP', 'sbc_wave_init: unable to allocate sf_wn structure' )
+                                      ALLOCATE( sf_wn(1)%fnow(jpi,jpj,1)   )
+               IF( sn_wnum%ln_tint )  ALLOCATE( sf_wn(1)%fdta(jpi,jpj,1,2) )
+               CALL fld_fill( sf_wn, (/ sn_wnum /), cn_dir, 'sbc_wave', 'Wave module', 'namsbc_wave' )
+            ENDIF
             !
-                                     ALLOCATE( sf_tauwoc(1)%fnow(jpi,jpj,1)   )
-            IF( sn_tauwoc%ln_tint )  ALLOCATE( sf_tauwoc(1)%fdta(jpi,jpj,1,2) )
-            CALL fld_fill( sf_tauwoc, (/ sn_tauwoc /), cn_dir, 'sbc_wave_init', 'Wave module', 'namsbc_wave' )
-         ENDIF
-         ALLOCATE( tauoc_wave(jpi,jpj) )
-      ENDIF
-
-      IF( ln_tauw ) THEN
-         IF( .NOT. cpl_tauw ) THEN
-            ALLOCATE( sf_tauw(2), STAT=ierror )           !* allocate and fill sf_wave with sn_tauwx/y
-            IF( ierror > 0 )   CALL ctl_stop( 'STOP', 'sbc_wave_init: unable to allocate sf_tauw structure' )
-            !
-            ALLOCATE( slf_j(2) )
-            slf_j(1) = sn_tauwx
-            slf_j(2) = sn_tauwy
-                                    ALLOCATE( sf_tauw(1)%fnow(jpi,jpj,1)   )
-                                    ALLOCATE( sf_tauw(2)%fnow(jpi,jpj,1)   )
-            IF( slf_j(1)%ln_tint )  ALLOCATE( sf_tauw(1)%fdta(jpi,jpj,1,2) )
-            IF( slf_j(2)%ln_tint )  ALLOCATE( sf_tauw(2)%fdta(jpi,jpj,1,2) )
-            CALL fld_fill( sf_tauw, (/ slf_j /), cn_dir, 'sbc_wave_init', 'read wave input', 'namsbc_wave' )
-         ENDIF
-         ALLOCATE( tauw_x(jpi,jpj) )
-         ALLOCATE( tauw_y(jpi,jpj) )
-      ENDIF
-
-      IF( ln_sdw ) THEN   ! Find out how many fields have to be read from file if not coupled
-         jpfld=0
-         jp_usd=0   ;   jp_vsd=0   ;   jp_hsw=0   ;   jp_wmp=0   ;   jp_wfr=0
-         IF( .NOT. cpl_sdrftx ) THEN
-            jpfld  = jpfld + 1
-            jp_usd = jpfld
-         ENDIF
-         IF( .NOT. cpl_sdrfty ) THEN
-            jpfld  = jpfld + 1
-            jp_vsd = jpfld
-         ENDIF
-         IF( .NOT. cpl_hsig  .AND. ll_st_bv_li  ) THEN
-            jpfld  = jpfld + 1
-            jp_hsw = jpfld
-         ENDIF
-         IF( .NOT. cpl_wper  .AND. ll_st_bv_li  ) THEN
-            jpfld  = jpfld + 1
-            jp_wmp = jpfld
-         ENDIF
-         IF( .NOT. cpl_wfreq .AND. ll_st_peakfr ) THEN
-            jpfld  = jpfld + 1
-            jp_wfr = jpfld
-         ENDIF
-
-         ! Read from file only the non-coupled fields 
-         IF( jpfld > 0 ) THEN
-            ALLOCATE( slf_i(jpfld) )
-            IF( jp_usd > 0 )   slf_i(jp_usd) = sn_usd
-            IF( jp_vsd > 0 )   slf_i(jp_vsd) = sn_vsd
-            IF( jp_hsw > 0 )   slf_i(jp_hsw) = sn_hsw
-            IF( jp_wmp > 0 )   slf_i(jp_wmp) = sn_wmp
-            IF( jp_wfr > 0 )   slf_i(jp_wfr) = sn_wfr
-
-            ALLOCATE( sf_sd(jpfld), STAT=ierror )   !* allocate and fill sf_sd with stokes drift
-            IF( ierror > 0 )   CALL ctl_stop( 'STOP', 'sbc_wave_init: unable to allocate sf_wave structure' )
-            !
-            DO ifpr= 1, jpfld
-               ALLOCATE( sf_sd(ifpr)%fnow(jpi,jpj,1) )
-               IF( slf_i(ifpr)%ln_tint )   ALLOCATE( sf_sd(ifpr)%fdta(jpi,jpj,1,2) )
-            END DO
-            !
-            CALL fld_fill( sf_sd, slf_i, cn_dir, 'sbc_wave_init', 'Wave module ', 'namsbc_wave' )
-         ENDIF
-         ALLOCATE( usd  (jpi,jpj,jpk), vsd  (jpi,jpj,jpk), wsd(jpi,jpj,jpk) )
-         ALLOCATE( hsw  (jpi,jpj)    , wmp  (jpi,jpj)     )
-         ALLOCATE( wfreq(jpi,jpj) )
-         ALLOCATE( ut0sd(jpi,jpj)    , vt0sd(jpi,jpj)     )
-         ALLOCATE( div_sd(jpi,jpj) )
-         ALLOCATE( tsd2d (jpi,jpj) )
-
-         ut0sd(:,:) = 0._wp
-         vt0sd(:,:) = 0._wp
-         hsw(:,:) = 0._wp
-         wmp(:,:) = 0._wp
-
-         usd(:,:,:) = 0._wp
-         vsd(:,:,:) = 0._wp
-         wsd(:,:,:) = 0._wp
-         ! Wave number needed only if ln_zdfswm=T
-         IF( .NOT. cpl_wnum ) THEN
-            ALLOCATE( sf_wn(1), STAT=ierror )           !* allocate and fill sf_wave with sn_wnum
-            IF( ierror > 0 )   CALL ctl_stop( 'STOP', 'sbc_wave_init: unable toallocate sf_wave structure' )
-                                   ALLOCATE( sf_wn(1)%fnow(jpi,jpj,1)   )
-            IF( sn_wnum%ln_tint )  ALLOCATE( sf_wn(1)%fdta(jpi,jpj,1,2) )
-            CALL fld_fill( sf_wn, (/ sn_wnum /), cn_dir, 'sbc_wave', 'Wave module', 'namsbc_wave' )
-         ENDIF
-         ALLOCATE( wnum(jpi,jpj) )
+         ENDIF
+         !
       ENDIF
       !
Index: /NEMO/branches/2020/tickets_icb_1900/src/OCE/TRA/eosbn2.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/OCE/TRA/eosbn2.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/OCE/TRA/eosbn2.F90	(revision 14016)
@@ -56,5 +56,5 @@
    !                  !! * Interface
    INTERFACE eos
-      MODULE PROCEDURE eos_insitu, eos_insitu_pot, eos_insitu_2d
+      MODULE PROCEDURE eos_insitu, eos_insitu_pot, eos_insitu_2d, eos_insitu_pot_2d
    END INTERFACE
    !
@@ -576,4 +576,77 @@
 
 
+   SUBROUTINE eos_insitu_pot_2d( pts, prhop )
+      !!----------------------------------------------------------------------
+      !!                  ***  ROUTINE eos_insitu_pot  ***
+      !!
+      !! ** Purpose :   Compute the in situ density (ratio rho/rho0) and the
+      !!      potential volumic mass (Kg/m3) from potential temperature and
+      !!      salinity fields using an equation of state selected in the
+      !!     namelist.
+      !!
+      !! ** Action  :
+      !!              - prhop, the potential volumic mass (Kg/m3)
+      !!
+      !!----------------------------------------------------------------------
+      REAL(wp), DIMENSION(jpi,jpj,jpts), INTENT(in   ) ::   pts    ! 1 : potential temperature  [Celsius]
+      !                                                                ! 2 : salinity               [psu]
+      REAL(wp), DIMENSION(jpi,jpj     ), INTENT(  out) ::   prhop  ! potential density (surface referenced)
+      !
+      INTEGER  ::   ji, jj, jk, jsmp             ! dummy loop indices
+      INTEGER  ::   jdof
+      REAL(wp) ::   zt , zh , zstemp, zs , ztm   ! local scalars
+      REAL(wp) ::   zn , zn0, zn1, zn2, zn3      !   -      -
+      REAL(wp), DIMENSION(:), ALLOCATABLE :: zn0_sto, zn_sto, zsign    ! local vectors
+      !!----------------------------------------------------------------------
+      !
+      IF( ln_timing )   CALL timing_start('eos-pot')
+      !
+      SELECT CASE ( neos )
+      !
+      CASE( np_teos10, np_eos80 )                !==  polynomial TEOS-10 / EOS-80 ==!
+         !
+            DO_2D( 1, 1, 1, 1 )
+               !
+               zt  = pts (ji,jj,jp_tem) * r1_T0                           ! temperature
+               zs  = SQRT( ABS( pts(ji,jj,jp_sal) + rdeltaS ) * r1_S0 )   ! square root salinity
+               ztm = tmask(ji,jj,1)                                         ! tmask
+               !
+               zn0 = (((((EOS060*zt   &
+                  &   + EOS150*zs+EOS050)*zt   &
+                  &   + (EOS240*zs+EOS140)*zs+EOS040)*zt   &
+                  &   + ((EOS330*zs+EOS230)*zs+EOS130)*zs+EOS030)*zt   &
+                  &   + (((EOS420*zs+EOS320)*zs+EOS220)*zs+EOS120)*zs+EOS020)*zt   &
+                  &   + ((((EOS510*zs+EOS410)*zs+EOS310)*zs+EOS210)*zs+EOS110)*zs+EOS010)*zt   &
+                  &   + (((((EOS600*zs+EOS500)*zs+EOS400)*zs+EOS300)*zs+EOS200)*zs+EOS100)*zs+EOS000
+                  !
+               !
+               prhop(ji,jj) = zn0 * ztm                           ! potential density referenced at the surface
+               !
+            END_2D
+
+      CASE( np_seos )                !==  simplified EOS  ==!
+         !
+         DO_2D( 1, 1, 1, 1 )
+            zt  = pts  (ji,jj,jp_tem) - 10._wp
+            zs  = pts  (ji,jj,jp_sal) - 35._wp
+            ztm = tmask(ji,jj,1)
+            !                                                     ! potential density referenced at the surface
+            zn =  - rn_a0 * ( 1._wp + 0.5_wp*rn_lambda1*zt ) * zt   &
+               &  + rn_b0 * ( 1._wp - 0.5_wp*rn_lambda2*zs ) * zs   &
+               &  - rn_nu * zt * zs
+            prhop(ji,jj) = ( rho0 + zn ) * ztm
+            !
+         END_2D
+         !
+      END SELECT
+      IF(sn_cfctl%l_prtctl)   CALL prt_ctl( tab2d_1=prhop, clinfo1=' pot: ', kdim=1 )
+      !
+      IF(sn_cfctl%l_prtctl)   CALL prt_ctl( tab2d_1=prhop, clinfo1=' eos-pot: ' )
+      !
+      IF( ln_timing )   CALL timing_stop('eos-pot')
+      !
+   END SUBROUTINE eos_insitu_pot_2d
+
+
    SUBROUTINE rab_3d( pts, pab, Kmm )
       !!
Index: /NEMO/branches/2020/tickets_icb_1900/src/OCE/TRA/trazdf.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/OCE/TRA/trazdf.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/OCE/TRA/trazdf.F90	(revision 14016)
@@ -17,4 +17,5 @@
    USE phycst         ! physical constant
    USE zdf_oce        ! ocean vertical physics variables
+   USE zdfmfc         ! Mass FLux Convection 
    USE sbc_oce        ! surface boundary condition: ocean
    USE ldftra         ! lateral diffusion: eddy diffusivity
@@ -198,4 +199,9 @@
             ENDIF
             !
+            ! Modification of diagonal to add MF scheme
+            IF ( ln_zdfmfc ) THEN
+               CALL diag_mfc( zwi, zwd, zws, p2dt, Kaa )
+            END IF
+            !
             !! Matrix inversion from the first level
             !!----------------------------------------------------------------------
@@ -226,7 +232,12 @@
          ENDIF 
          !         
+         ! Modification of rhs to add MF scheme
+         IF ( ln_zdfmfc ) THEN
+            CALL rhs_mfc( pt(:,:,:,jn,Krhs), jn )
+         END IF
+         !
          DO_2D( 0, 0, 0, 0 )         !* 2nd recurrence:    Zk = Yk - Ik / Tk-1  Zk-1
             pt(ji,jj,1,jn,Kaa) =        e3t(ji,jj,1,Kbb) * pt(ji,jj,1,jn,Kbb)    &
-               &               + p2dt * e3t(ji,jj,1,Kmm) * pt(ji,jj,1,jn,Krhs)
+               &               + p2dt * e3t(ji,jj,1,Kmm) * pt(ji,jj,1,jn,Krhs) 
          END_2D
          DO_3D( 0, 0, 0, 0, 2, jpkm1 )
Index: /NEMO/branches/2020/tickets_icb_1900/src/OCE/ZDF/zdf_oce.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/OCE/ZDF/zdf_oce.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/OCE/ZDF/zdf_oce.F90	(revision 14016)
@@ -40,4 +40,5 @@
    LOGICAL , PUBLIC ::   ln_zdfswm   !: surface  wave-induced mixing flag
    LOGICAL , PUBLIC ::   ln_zdfiwm   !: internal wave-induced mixing flag
+   LOGICAL , PUBLIC ::   ln_zdfmfc   !: convection: eddy diffusivity Mass Flux Convection
    !                             ! coefficients 
    REAL(wp), PUBLIC ::   rn_avm0     !: vertical eddy viscosity (m2/s)
Index: /NEMO/branches/2020/tickets_icb_1900/src/OCE/ZDF/zdfmfc.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/OCE/ZDF/zdfmfc.F90	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/src/OCE/ZDF/zdfmfc.F90	(revision 14016)
@@ -0,0 +1,475 @@
+ MODULE zdfmfc
+   !!======================================================================
+   !!                       ***  MODULE  zdfmfc  ***
+   !! Ocean physics: Mass-Flux scheme parameterization of Convection:
+   !!                Non-local transport for the convective ocean boundary
+   !!                layer. Subgrid-scale large eddies are represented by a
+   !!                mass-flux contribution (ln_zdfmfc = .TRUE.)
+   !!======================================================================
+   !! History : NEMO  !
+   !!            3.6  !  2016-06  (H. Giordani, R. Bourdallé-Badie)  Original code
+   !!            4.2  !  2020-12  (H. Giordani, R. Bourdallé-Badie)  adapt to NEM04.2
+   !!----------------------------------------------------------------------
+   !!----------------------------------------------------------------------
+   !!   tra_mfc       : Compute the Mass Flux and trends of T/S
+   !!   diag_mfc      : Modify diagonal of trazdf Matrix
+   !!   rhs_mfc       : Modify RHS of trazdf Matrix
+   !!   zdf_mfc_init  : initialization, namelist read, and parameters control
+   !!----------------------------------------------------------------------
+   !
+   USE oce            ! ocean dynamics and active tracers
+   USE dom_oce        ! ocean space and time domain
+   USE domvvl         ! ocean space and time domain : variable volume layer
+   USE domzgr
+   USE zdf_oce        ! ocean vertical physics
+   USE sbc_oce        ! surface boundary condition: ocean
+   USE phycst         ! physical constants
+   USE eosbn2         ! equation of state (eos routine)
+   USE zdfmxl         ! mixed layer
+   USE lbclnk         ! ocean lateral boundary conditions (or mpp link)
+   USE lib_mpp        ! MPP manager
+   USE prtctl         ! Print control
+   USE in_out_manager ! I/O manager
+   USE iom            ! I/O manager library
+   USE timing         ! Timing
+   USE lib_fortran    ! Fortran utilities (allows no signed zero when 'key_nosignedzero' defined) 
+
+   IMPLICIT NONE
+   PRIVATE
+
+   PUBLIC   tra_mfc         ! routine called in step module
+   PUBLIC   diag_mfc        ! routine called in trazdf module
+   PUBLIC   rhs_mfc         ! routine called in trazdf module
+   PUBLIC   zdf_mfc_init    ! routine called in nemo module
+   !
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:)   ::  edmfa, edmfb, edmfc   !: diagonal term of the matrix.
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:,:) ::  edmftra               !: y term for matrix inversion
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:)   ::  edmfm               !: y term for matrix inversion
+   !
+   !! ** Namelist  namzdf_edmf  **
+   REAL(wp) ::   rn_cemf           ! entrain of T/S
+   REAL(wp) ::   rn_cwmf           ! detrain of T/S
+   REAL(wp) ::   rn_cent           ! entrain of the convective mass flux
+   REAL(wp) ::   rn_cdet           ! detrain of the convective mass flux
+   REAL(wp) ::   rn_cap            ! Factor of computation for convective area (negative => area constant)
+   REAL(wp) ::   App_max           ! Maximum of the convective area
+   LOGICAL, PUBLIC, SAVE  ::   ln_edmfuv         !: EDMF flag for velocity  !
+   !
+   !! * Substitutions
+#  include "do_loop_substitute.h90"
+#  include "domzgr_substitute.h90"
+   !!----------------------------------------------------------------------
+   !! NEMO/OCE 4.2 , NEMO Consortium (2018)
+   !! $Id: zdfmfc.F90 13783 2020-20-02 15:30:22Z rbourdal $
+   !! Software governed by the CeCILL license (see ./LICENSE)
+   !!----------------------------------------------------------------------
+CONTAINS
+
+   INTEGER FUNCTION zdf_mfc_alloc()
+      !!----------------------------------------------------------------------
+      !!                ***  FUNCTION zdf_edmf_alloc  ***
+      !!----------------------------------------------------------------------
+      ALLOCATE( edmfa(jpi,jpj,jpk) , edmfb(jpi,jpj,jpk) , edmfc(jpi,jpj,jpk)      &
+         &      , edmftra(jpi,jpj,jpk,2), edmfm(jpi,jpj,jpk) ,  STAT= zdf_mfc_alloc )
+         !
+      IF( lk_mpp             )   CALL mpp_sum ( 'zdfmfc', zdf_mfc_alloc )
+      IF( zdf_mfc_alloc /= 0 )   CALL ctl_warn('zdf_mfc_alloc: failed to allocate arrays')
+   END FUNCTION zdf_mfc_alloc
+
+
+   SUBROUTINE tra_mfc( kt, Kmm, pts, Krhs )
+      !!----------------------------------------------------------------------
+      !!                   ***  ROUTINE zdf_mfc  ***
+      !!
+      !! ** Purpose :      Compute a mass flux, depending on surface flux, over
+      !!            the instable part of the water column.
+      !!
+      !! ** Method  :     Compute surface instability and mix tracers until stable level
+      !!           
+      !!
+      !! ** Action  :      Compute convection plume and (ta,sa)-trends for trazdf (EDMF scheme)
+      !!
+      !! References :      
+      !!                   Giordani, Bourdallé-Badie and Madec JAMES 2020
+      !!----------------------------------------------------------------------
+      !!----------------------------------------------------------------------
+      INTEGER                                  , INTENT(in)    :: Kmm, Krhs ! time level indices
+      REAL(wp), DIMENSION(jpi,jpj,jpk,jpts,jpt), INTENT(inout) :: pts       ! active tracers and RHS of tracer equation
+      REAL(wp), DIMENSION(jpi,jpj,jpk,2) ::   ztsp         ! T/S of the plume
+      REAL(wp), DIMENSION(jpi,jpj,jpk,2) ::   ztse         ! T/S at W point
+      REAL(wp), DIMENSION(jpi,jpj,jpk) :: zrwp          !
+      REAL(wp), DIMENSION(jpi,jpj,jpk) :: zrwp2         !
+      REAL(wp), DIMENSION(jpi,jpj,jpk) :: zapp          !
+      REAL(wp), DIMENSION(jpi,jpj,jpk) :: zedmf         !
+      REAL(wp), DIMENSION(jpi,jpj,jpk) :: zepsT, zepsW  !
+      !
+      REAL(wp), DIMENSION(jpi,jpj) :: zustar, zustar2   !
+      REAL(wp), DIMENSION(jpi,jpj) :: zuws, zvws, zsws, zfnet          !
+      REAL(wp), DIMENSION(jpi,jpj) :: zfbuo, zrautbm1, zrautb, zraupl
+      REAL(wp), DIMENSION(jpi,jpj) :: zwpsurf            !
+      REAL(wp), DIMENSION(jpi,jpj) :: zop0 , zsp0 !
+      REAL(wp), DIMENSION(jpi,jpj) :: zrwp_0, zrwp2_0  !
+      REAL(wp), DIMENSION(jpi,jpj) :: zapp0           !
+      REAL(wp), DIMENSION(jpi,jpj) :: zphp, zph, zphpm1, zphm1, zNHydro
+      REAL(wp), DIMENSION(jpi,jpj) :: zhcmo          !
+      !
+      REAL(wp), DIMENSION(jpi,jpj,jpk)   ::   zn2    ! N^2
+      REAL(wp), DIMENSION(jpi,jpj,2  ) ::   zab, zabm1, zabp ! alpha and beta
+     
+      REAL(wp), PARAMETER :: zepsilon = 1.e-30                 ! local small value
+
+      REAL(wp) :: zrho, zrhop
+      REAL(wp) :: zcnh, znum, zden, zcoef1, zcoef2
+      REAL(wp) :: zca, zcb, zcd, zrw, zxl, zcdet, zctre
+      REAL(wp) :: zaw, zbw, zxw
+      REAL(wp) :: alpha
+     !
+      INTEGER, INTENT(in   )    ::   kt   ! ocean time-step index      !
+      !
+      INTEGER  ::   ji, jj, jk  ! dummy  loop arguments   
+      !
+      !------------------------------------------------------------------
+      ! Initialisation of coefficients
+      !------------------------------------------------------------------
+      zca          = 1._wp
+      zcb          = 1._wp
+      zcd          = 1._wp
+
+      !------------------------------------------------------------------
+      ! Surface boundary condition
+      !------------------------------------------------------------------
+      ! surface Stress
+      !--------------------
+      zuws(:,:) = utau(:,:) * r1_rho0 
+      zvws(:,:) = vtau(:,:) * r1_rho0 
+      zustar2(:,:) = SQRT(zuws(:,:)*zuws(:,:)+zvws(:,:)*zvws(:,:))
+      zustar(:,:)  = SQRT(zustar2(:,:))
+
+      ! Heat Flux
+      !--------------------
+      zfnet(:,:) = qns(:,:) + qsr(:,:)
+      zfnet(:,:) = zfnet(:,:) / (rho0 * rcp)
+
+      ! Water Flux
+      !---------------------
+      zsws(:,:) = emp(:,:)
+
+      !-------------------------------------------
+      ! Initialisation of prognostic variables
+      !-------------------------------------------
+      zrwp (:,:,:) =  0._wp ; zrwp2(:,:,:) =  0._wp ; zedmf(:,:,:) =  0._wp
+      zph  (:,:)   =  0._wp ; zphm1(:,:)   =  0._wp ; zphpm1(:,:)  =  0._wp
+      ztsp(:,:,:,:)=  0._wp
+
+      ! Tracers inside plume (ztsp) and environment (ztse)
+      ztsp(:,:,1,jp_tem) = pts(:,:,1,jp_tem,Kmm) * tmask(:,:,1)
+      ztsp(:,:,1,jp_sal) = pts(:,:,1,jp_sal,Kmm) * tmask(:,:,1)
+      ztse(:,:,1,jp_tem) = pts(:,:,1,jp_tem,Kmm) * tmask(:,:,1)
+      ztse(:,:,1,jp_sal) = pts(:,:,1,jp_sal,Kmm) * tmask(:,:,1)
+
+      CALL eos( ztse(:,:,1,:) ,  zrautb(:,:) )
+      CALL eos( ztsp(:,:,1,:) ,  zraupl(:,:) )
+
+      !-------------------------------------------
+      ! Boundary Condition of Mass Flux (plume velo.; convective area, entrain/detrain)
+      !-------------------------------------------
+      zhcmo(:,:) = e3t(:,:,1,Kmm)
+      zfbuo(:,:)   = 0._wp
+      WHERE ( ABS(zrautb(:,:)) > 1.e-20 ) zfbuo(:,:)   =   &
+         &      grav * ( 2.e-4_wp *zfnet(:,:) - 7.6E-4_wp*pts(:,:,1,jp_sal,Kmm)*zsws(:,:)/zrautb(:,:)) * zhcmo(:,:)
+
+      zedmf(:,:,1) = -0.065_wp*(ABS(zfbuo(:,:)))**(1._wp/3._wp)*SIGN(1.,zfbuo(:,:))
+      zedmf(:,:,1) = MAX(0., zedmf(:,:,1))
+
+      zwpsurf(:,:) = 2._wp/3._wp*zustar(:,:) + 2._wp/3._wp*ABS(zfbuo(:,:))**(1._wp/3._wp)
+      zwpsurf(:,:) = MAX(1.e-5_wp,zwpsurf(:,:))
+      zwpsurf(:,:) = MIN(1.,zwpsurf(:,:))
+
+      zapp(:,:,:)  = App_max
+      WHERE(zwpsurf .NE. 0.) zapp(:,:,1)   = MIN(MAX(0.,zedmf(:,:,1)/zwpsurf(:,:)), App_max)
+
+      zedmf(:,:,1) = 0._wp 
+      zrwp (:,:,1) = 0._wp 
+      zrwp2(:,:,1) = 0._wp
+      zepsT(:,:,:) = 0.001_wp
+      zepsW(:,:,:) = 0.001_wp
+
+
+      !--------------------------------------------------------------
+      ! Compute plume properties 
+      ! In the same loop on vert. levels computation of:
+      !    - Vertical velocity: zWp
+      !    - Convective Area: zAp
+      !    - Tracers properties inside the plume (if necessary): ztp
+      !---------------------------------------------------------------
+
+      DO jk= 2, jpk
+
+         ! Compute the buoyancy acceleration on T-points at jk-1
+         zrautbm1(:,:) = zrautb(:,:)
+         CALL eos( pts (:,:,jk  ,:,Kmm) ,  zrautb(:,:)   )
+         CALL eos( ztsp(:,:,jk-1,:    ) ,  zraupl(:,:)   )
+
+         zphm1(:,:)  = zphm1(:,:)  + grav * zrautbm1(:,:) * e3t(:,:,jk-1, Kmm)
+         zphpm1(:,:) = zphpm1(:,:) + grav * zraupl(:,:)   * e3t(:,:,jk-1, Kmm)
+         zph(:,:)    = zphm1(:,:)  + grav * zrautb(:,:)   * e3t(:,:,jk  , Kmm)
+         zph(:,:)    = MAX( zph(:,:), zepsilon)
+
+         WHERE(zrautbm1 .NE. 0.) zfbuo(:,:)  =  grav * (zraupl(:,:) - zrautbm1(:,:)) / zrautbm1(:,:)
+
+         DO_2D( 0, 0, 0, 0 )
+
+            ! Compute Environment of Plume. Interpolation T/S (before time step) on W-points
+            zrw              =  (gdept(ji,jj,jk,Kmm) - gdepw(ji,jj,jk,Kmm)) &
+               &              / (gdept(ji,jj,jk,Kmm) - gdept(ji,jj,jk-1,Kmm))
+            ztse(ji,jj,jk,:) = (pts(ji,jj,jk,:,Kmm) * zrw + pts(ji,jj,jk-1,:,Kmm)*(1._wp - zrw) )*tmask(ji,jj,jk)
+
+            !---------------------------------------------------------------
+            ! Compute the vertical velocity on W-points
+            !---------------------------------------------------------------
+
+            ! Non-hydrostatic pressure terms in the wp2 equation
+            zcnh = 0.2_wp 
+            znum = 0.5_wp  + zcnh - &
+                   (zcnh*grav*zraupl(ji,jj)/zph(ji,jj)+zcb*zepsW(ji,jj,jk-1)) &
+                   *e3t(ji,jj,jk-1,Kmm)*0.5_wp   
+            zden = 0.5_wp + zcnh + &
+                   (zcnh*grav*zraupl(ji,jj)/zph(ji,jj)+zcb*zepsW(ji,jj,jk-1)) &
+                   *e3t(ji,jj,jk-1,Kmm)*0.5_wp   
+
+            zcoef1 = zca*e3t(ji,jj,jk-1,Kmm) / zden
+            zcoef2 = znum/zden
+
+            ! compute wp2 
+            zrwp2(ji,jj,jk) = zcoef1*zfbuo(ji,jj) &
+                            + zcoef2*zrwp2(ji,jj,jk-1)
+            zrwp2(ji,jj,jk) = MAX ( zrwp2(ji,jj,jk)*wmask(ji,jj,jk) , 0.)
+            zrwp (ji,jj,jk) = SQRT( zrwp2(ji,jj,jk) )
+
+            !----------------------------------------------------------------------------------
+            ! Compute convective area on W-point
+            ! Compute vertical profil of the convective area with mass conservation hypothesis
+            ! If rn_cap negative => constant value on the water column.
+            !----------------------------------------------------------------------------------
+            IF( rn_cap .GT. 0. ) THEN
+
+               zxw = MAX(zrwp(ji,jj,jk-1), zrwp(ji,jj,jk) )
+               IF( zxw > 0. ) THEN
+
+                  zxl = (zrwp(ji,jj,jk-1)-zrwp(ji,jj,jk))/(e3t(ji,jj,jk-1,Kmm)*zxw)
+                  IF (zxl .LT. 0._wp) THEN
+                     zctre  = -1.*rn_cap*zxl 
+                     zcdet  =  0._wp
+                  ELSE
+                     zctre  =  0._wp
+                     zcdet  =  rn_cap*zxl 
+                  END IF
+                     zapp(ji,jj,jk) = zapp(ji,jj,jk-1)*     &
+                     &                (1._wp + (zxl + zctre - zcdet )*e3t(ji,jj,jk-1,Kmm))
+                  ELSE
+                     zapp(ji,jj,jk) = App_max
+                  END IF
+                  zapp(ji,jj,jk) = MIN( MAX(zapp(ji,jj,jk),0.), App_max)
+               ELSE
+                  zapp(ji,jj,jk) = -1. * rn_cap
+               END IF
+
+            ! Compute Mass Flux on W-point
+            zedmf(ji,jj,jk)   = -zapp(ji,jj,jk) * zrwp(ji,jj,jk)* wmask(ji,jj,jk)
+
+            ! Compute Entrainment coefficient
+            IF(rn_cemf .GT. 0.) THEN
+               zxw = 0.5_wp*(zrwp(ji,jj,jk-1)+ zrwp(ji,jj,jk) )
+               zepsT(ji,jj,jk)  =  0.01_wp
+               IF( zxw > 0.  ) THEN
+                  zepsT(ji,jj,jk)  =  zepsT(ji,jj,jk) +                       &
+                                   &  ABS( zrwp(ji,jj,jk-1)-zrwp(ji,jj,jk) )  &
+                                   &  / ( e3t(ji,jj,jk-1,Kmm) * zxw )
+                  zepsT(ji,jj,jk)  = zepsT(ji,jj,jk) * rn_cemf * wmask(ji,jj,jk)
+               ENDIF
+            ELSE
+               zepsT(ji,jj,jk)  = -rn_cemf
+            ENDIF
+
+            ! Compute the detrend coef for velocity (on W-point and not T-points, bug ???)
+            IF(rn_cwmf .GT. 0.) THEN
+               zepsW(ji,jj,jk)  =  rn_cwmf * zepsT(ji,jj,jk)
+            ELSE
+               zepsW(ji,jj,jk)  = -rn_cwmf
+            ENDIF
+
+            !---------------------------------------------------------------
+            ! Compute the plume properties on T-points
+            !---------------------------------------------------------------
+            IF(zrwp (ji,jj,jk) .LT. 1.e-12_wp .AND. zrwp (ji,jj,jk-1) .LT. 1.e-12_wp) THEN
+               ztsp(ji,jj,jk-1,jp_tem) = pts(ji,jj,jk-1,jp_tem,Kmm)
+               ztsp(ji,jj,jk-1,jp_sal) = pts(ji,jj,jk-1,jp_sal,Kmm)
+            ENDIF
+
+            zcoef1 =  (1._wp-zepsT(ji,jj,jk)*(1._wp-zrw)*e3w(ji,jj,jk,Kmm)*wmask(ji,jj,jk ) ) &
+            &       / (1._wp+zepsT(ji,jj,jk)*zrw*e3w(ji,jj,jk,Kmm)*wmask(ji,jj,jk) )
+            !
+            zcoef2 =  zepsT(ji,jj,jk)*e3w(ji,jj,jk,Kmm)*wmask(ji,jj,jk)                       &
+            &       / (1._wp+zepsT(ji,jj,jk)*zrw*e3w(ji,jj,jk,Kmm)*wmask(ji,jj,jk))
+            !
+            ztsp(ji,jj,jk,jp_tem) = (zcoef1 * ztsp(ji,jj,jk-1,jp_tem) +  &
+            &                        zcoef2 * ztse(ji,jj,jk  ,jp_tem) )*tmask(ji,jj,jk)
+            ztsp(ji,jj,jk,jp_sal) = (zcoef1 * ztsp(ji,jj,jk-1,jp_sal) +  &
+            &                        zcoef2 * ztse(ji,jj,jk  ,jp_sal) )*tmask(ji,jj,jk)
+
+         END_2D 
+      END DO ! end of loop on jpk
+
+      ! Compute Mass Flux on T-point
+      DO jk=1,jpk-1
+         edmfm(:,:,jk) = (zedmf(:,:,jk+1)  + zedmf(:,:,jk) )*0.5_wp
+      END DO
+      edmfm(:,:,jpk) = zedmf(:,:,jpk) 
+
+      ! Save variable (on T point)
+      CALL iom_put( "mf_Tp" , ztsp(:,:,:,jp_tem) )  ! Save plume temperature
+      CALL iom_put( "mf_Sp" , ztsp(:,:,:,jp_sal) )  ! Save plume salinity
+      CALL iom_put( "mf_mf" , edmfm(:,:,:)       )  ! Save Mass Flux
+      ! Save variable (on W point)
+      CALL iom_put( "mf_wp" , zrwp (:,:,:)       )  ! Save convective velocity in the plume
+      CALL iom_put( "mf_app", zapp (:,:,:)       )  ! Save convective area
+
+      !=================================================================================
+      !  Computation of a tridiagonal matrix and right hand side terms of the linear system
+      !=================================================================================
+      edmfa(:,:,:)     = 0._wp
+      edmfb(:,:,:)     = 0._wp
+      edmfc(:,:,:)     = 0._wp
+      edmftra(:,:,:,:) = 0._wp
+
+      !---------------------------------------------------------------
+      ! Diagonal terms 
+      !---------------------------------------------------------------
+      DO jk=1,jpk-1
+         edmfa(:,:,jk) =  0._wp
+         edmfb(:,:,jk) = -edmfm(:,:,jk  ) / e3w(:,:,jk+1,Kmm)
+         edmfc(:,:,jk) =  edmfm(:,:,jk+1) / e3w(:,:,jk+1,Kmm)
+      END DO
+      edmfa(:,:,jpk)   = -edmfm(:,:,jpk-1) / e3w(:,:,jpk,Kmm)
+      edmfb(:,:,jpk)   =  edmfm(:,:,jpk  ) / e3w(:,:,jpk,Kmm)
+      edmfc(:,:,jpk)   =  0._wp
+
+      !---------------------------------------------------------------
+      ! right hand side term for Temperature
+      !---------------------------------------------------------------
+      DO jk=1,jpk-1
+        edmftra(:,:,jk,1) = - edmfm(:,:,jk  ) * ztsp(:,:,jk  ,jp_tem) / e3w(:,:,jk+1,Kmm) &
+                          & + edmfm(:,:,jk+1) * ztsp(:,:,jk+1,jp_tem) / e3w(:,:,jk+1,Kmm)
+      END DO
+      edmftra(:,:,jpk,1) = - edmfm(:,:,jpk-1) * ztsp(:,:,jpk-1,jp_tem) / e3w(:,:,jpk,Kmm) &
+                         & + edmfm(:,:,jpk  ) * ztsp(:,:,jpk  ,jp_tem) / e3w(:,:,jpk,Kmm)
+                        
+      !---------------------------------------------------------------
+      ! Right hand side term for Salinity
+      !---------------------------------------------------------------
+      DO jk=1,jpk-1
+         edmftra(:,:,jk,2) =  - edmfm(:,:,jk  ) * ztsp(:,:,jk  ,jp_sal) / e3w(:,:,jk+1,Kmm) &
+                           &  + edmfm(:,:,jk+1) * ztsp(:,:,jk+1,jp_sal) / e3w(:,:,jk+1,Kmm)
+      END DO
+      edmftra(:,:,jpk,2) = - edmfm(:,:,jpk-1) * ztsp(:,:,jpk-1,jp_sal) / e3w(:,:,jpk,Kmm) &
+                         & + edmfm(:,:,jpk  ) * ztsp(:,:,jpk  ,jp_sal) / e3w(:,:,jpk,Kmm)
+      !
+      !
+      CALL lbc_lnk_multi( 'zdfmfc', edmfm,'T',1., edmfa,'T',1., edmfb,'T',1., edmfc,'T',1., edmftra(:,:,:,jp_tem),'T',1., edmftra(:,:,:,jp_sal),'T',1.)
+      !
+   END SUBROUTINE tra_mfc
+
+   
+   SUBROUTINE diag_mfc( zdiagi, zdiagd, zdiags, p2dt, Kaa )
+
+      REAL(wp), DIMENSION(jpi,jpj,jpk), INTENT(inout) ::  zdiagi, zdiagd, zdiags  ! inout: tridaig. terms 
+      REAL(wp)                        , INTENT(in   ) ::   p2dt                   ! tracer time-step
+      INTEGER                         , INTENT(in   ) ::   Kaa                    ! ocean time level indices
+
+      INTEGER  ::   ji, jj, jk  ! dummy  loop arguments   
+
+         DO_3D( 0, 0, 0, 0, 1, jpkm1 )
+            zdiagi(ji,jj,jk) = zdiagi(ji,jj,jk) + e3t(ji,jj,jk,Kaa) * p2dt *edmfa(ji,jj,jk)
+            zdiags(ji,jj,jk) = zdiags(ji,jj,jk) + e3t(ji,jj,jk,Kaa) * p2dt *edmfc(ji,jj,jk)
+            zdiagd(ji,jj,jk) = zdiagd(ji,jj,jk) + e3t(ji,jj,jk,Kaa) * p2dt *edmfb(ji,jj,jk)
+         END_3D
+
+   END SUBROUTINE diag_mfc
+
+   SUBROUTINE rhs_mfc( zrhs, jjn )
+
+      REAL(wp), DIMENSION(jpi,jpj,jpk), INTENT(inout) ::   zrhs                   ! inout: rhs trend 
+      INTEGER                         , INTENT(in   ) ::   jjn                    ! tracer indices
+
+      INTEGER  ::   ji, jj, jk  ! dummy  loop arguments   
+
+      DO_3D( 0, 0, 0, 0, 1, jpkm1 )
+         zrhs(ji,jj,jk) = zrhs(ji,jj,jk) + edmftra(ji,jj,jk,jjn)
+      END_3D
+
+   END SUBROUTINE rhs_mfc
+
+
+
+   SUBROUTINE zdf_mfc_init
+      !!----------------------------------------------------------------------
+      !!                  ***  ROUTINE zdf_mfc_init  ***
+      !!                    
+      !! ** Purpose :   Initialization of the vertical eddy diffivity and
+      !!      mass flux
+      !!
+      !! ** Method  :   Read the namzdf_mfc namelist and check the parameters
+      !!      called at the first timestep (nit000)
+      !!
+      !! ** input   :   Namlist namzdf_mfc
+      !!
+      !! ** Action  :   Increase by 1 the nstop flag is setting problem encounter
+      !!
+      !!----------------------------------------------------------------------
+      !
+      INTEGER ::   jk    ! dummy loop indices
+      INTEGER ::   ios   ! Local integer output status for namelist read
+      REAL(wp)::   zcr   ! local scalar
+      !!
+      NAMELIST/namzdf_mfc/ ln_edmfuv, rn_cemf, rn_cwmf, rn_cent, rn_cdet, rn_cap, App_max
+      !!----------------------------------------------------------
+      !
+      !
+!      REWIND( numnam_ref )              ! Namelist namzdf_mfc in reference namelist : Vertical eddy diffivity mass flux
+      READ  ( numnam_ref, namzdf_mfc, IOSTAT = ios, ERR = 901)
+901   IF( ios /= 0 ) CALL ctl_nam ( ios , 'namzdf_edmf in reference namelist' )
+
+!      REWIND( numnam_cfg )              ! Namelist namzdf_mfc in configuration namelist : Vertical eddy diffivity mass flux
+      READ  ( numnam_cfg, namzdf_mfc, IOSTAT = ios, ERR = 902 )
+902   IF( ios /= 0 ) CALL ctl_nam ( ios , 'namzdf_edmf in configuration namelist' )
+      IF(lwm) WRITE ( numond, namzdf_mfc )
+
+      IF(lwp) THEN                     !* Control print
+         WRITE(numout,*)
+         WRITE(numout,*) 'zdf_mfc_init'
+         WRITE(numout,*) '~~~~~~~~~~~~~'
+         WRITE(numout,*) '   Namelist namzdf_mfc : set eddy diffusivity Mass Flux Convection'
+         WRITE(numout,*) '   Apply mass flux on velocities (Not yet avail.)     ln_edmfuv = ', ln_edmfuv
+         WRITE(numout,*) '   Coeff for entrain/detrain T/S of plume (Neg => cte) rn_cemf  = ', rn_cemf
+         WRITE(numout,*) '   Coeff for entrain/detrain Wp of plume  (Neg => cte) rn_cwmf  = ', rn_cwmf
+         WRITE(numout,*) '   Coeff for entrain/detrain area of plume             rn_cap   = ', rn_cap
+         WRITE(numout,*) '   Coeff for entrain area of plume                     rn_cent  = ', rn_cent
+         WRITE(numout,*) '   Coeff for detrain area of plume                     rn_cdet  = ', rn_cdet
+         WRITE(numout,*) '   Max convective area                                 App_max  = ', App_max
+       ENDIF
+                                     !* allocate edmf arrays
+      IF( zdf_mfc_alloc() /= 0 )   CALL ctl_stop( 'STOP', 'zdf_edmf_init : unable to allocate arrays' )
+      edmfa(:,:,:)     = 0._wp
+      edmfb(:,:,:)     = 0._wp
+      edmfc(:,:,:)     = 0._wp
+      edmftra(:,:,:,:) = 0._wp
+      !
+   END SUBROUTINE zdf_mfc_init
+
+   !!======================================================================
+ 
+   !!======================================================================
+END MODULE zdfmfc
+
+
+
Index: /NEMO/branches/2020/tickets_icb_1900/src/OCE/ZDF/zdfphy.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/OCE/ZDF/zdfphy.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/OCE/ZDF/zdfphy.F90	(revision 14016)
@@ -21,4 +21,5 @@
    USE zdfddm         ! vertical physics: double diffusion mixing      
    USE zdfevd         ! vertical physics: convection via enhanced vertical diffusion  
+   USE zdfmfc         ! vertical physics: Mass Flux Convection 
    USE zdfiwm         ! vertical physics: internal wave-induced mixing  
    USE zdfswm         ! vertical physics: surface  wave-induced mixing
@@ -78,4 +79,5 @@
       NAMELIST/namzdf/ ln_zdfcst, ln_zdfric, ln_zdftke, ln_zdfgls,   &     ! type of closure scheme
          &             ln_zdfosm,                                    &     ! type of closure scheme
+         &             ln_zdfmfc,                                    &     ! convection : mass flux
          &             ln_zdfevd, nn_evdm, rn_evd ,                  &     ! convection : evd
          &             ln_zdfnpc, nn_npc , nn_npcp,                  &     ! convection : npc
@@ -112,4 +114,5 @@
          WRITE(numout,*) '         OSMOSIS-OBL closure (OSM)               ln_zdfosm = ', ln_zdfosm
          WRITE(numout,*) '      convection: '
+         WRITE(numout,*) '         convection mass flux (mfc)              ln_zdfmfc = ', ln_zdfmfc
          WRITE(numout,*) '         enhanced vertical diffusion             ln_zdfevd = ', ln_zdfevd
          WRITE(numout,*) '            applied on momentum (=1/0)             nn_evdm = ', nn_evdm
@@ -172,10 +175,15 @@
       IF( ln_zdfnpc .AND. ln_zdfevd )   CALL ctl_stop( 'zdf_phy_init: chose between ln_zdfnpc and ln_zdfevd' )
       IF( ln_zdfosm .AND. ln_zdfevd )   CALL ctl_stop( 'zdf_phy_init: chose between ln_zdfosm and ln_zdfevd' )
+      IF( ln_zdfmfc .AND. ln_zdfevd )   CALL ctl_stop( 'zdf_phy_init: chose between ln_zdfmfc and ln_zdfevd' )
+      IF( ln_zdfmfc .AND. ln_zdfnpc )   CALL ctl_stop( 'zdf_phy_init: chose between ln_zdfmfc and ln_zdfnpc' )
+      IF( ln_zdfmfc .AND. ln_zdfosm )   CALL ctl_stop( 'zdf_phy_init: chose between ln_zdfmfc and ln_zdfosm' )
       IF( lk_top    .AND. ln_zdfnpc )   CALL ctl_stop( 'zdf_phy_init: npc scheme is not working with key_top' )
       IF( lk_top    .AND. ln_zdfosm )   CALL ctl_stop( 'zdf_phy_init: osmosis scheme is not working with key_top' )
+      IF( lk_top    .AND. ln_zdfmfc )   CALL ctl_stop( 'zdf_phy_init: Mass Flux scheme is not working with key_top' )
       IF(lwp) THEN
          WRITE(numout,*)
          IF    ( ln_zdfnpc ) THEN  ;   WRITE(numout,*) '   ==>>>   convection: use non penetrative convective scheme'
          ELSEIF( ln_zdfevd ) THEN  ;   WRITE(numout,*) '   ==>>>   convection: use enhanced vertical diffusion scheme'
+         ELSEIF( ln_zdfmfc ) THEN  ;   WRITE(numout,*) '   ==>>>   convection: use Mass Flux scheme'
          ELSE                      ;   WRITE(numout,*) '   ==>>>   convection: no specific scheme used'
          ENDIF
@@ -205,5 +213,7 @@
       ELSE                   ;   l_zdfsh2 = .TRUE.
       ENDIF
-
+      !                          !== Mass Flux Convectiive algorithm  ==!
+      IF( ln_zdfmfc )   CALL zdf_mfc_init       ! Convection computed with eddy diffusivity mass flux
+      !
       !                          !== gravity wave-driven mixing  ==!
       IF( ln_zdfiwm )   CALL zdf_iwm_init       ! internal wave-driven mixing
Index: /NEMO/branches/2020/tickets_icb_1900/src/OCE/ZDF/zdfsh2.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/OCE/ZDF/zdfsh2.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/OCE/ZDF/zdfsh2.F90	(revision 14016)
@@ -6,4 +6,6 @@
    !! History :   -   !  2014-10  (A. Barthelemy, G. Madec)  original code
    !!   NEMO     4.0  !  2017-04  (G. Madec)  remove u-,v-pts avm
+   !!   NEMO     4.2  !  2020-12  (G. Madec, E. Clementi) add Stokes Drift Shear
+   !                  !           for wave coupling
    !!----------------------------------------------------------------------
 
@@ -13,4 +15,6 @@
    USE oce
    USE dom_oce        ! domain: ocean
+   USE sbcwave        ! Surface Waves (add Stokes shear)
+   USE sbc_oce , ONLY: ln_stshear  !Stoked Drift shear contribution
    !
    USE in_out_manager ! I/O manager
@@ -21,5 +25,5 @@
 
    PUBLIC   zdf_sh2        ! called by zdftke, zdfglf, and zdfric
-   
+
    !! * Substitutions
 #  include "do_loop_substitute.h90"
@@ -59,22 +63,37 @@
       !!--------------------------------------------------------------------
       !
-      DO jk = 2, jpkm1
-         DO_2D( 1, 0, 1, 0 )     !* 2 x shear production at uw- and vw-points (energy conserving form)
-            zsh2u(ji,jj) = ( p_avm(ji+1,jj,jk) + p_avm(ji,jj,jk) ) &
-               &         * (   uu(ji,jj,jk-1,Kmm) -   uu(ji,jj,jk,Kmm) ) &
-               &         * (   uu(ji,jj,jk-1,Kbb) -   uu(ji,jj,jk,Kbb) ) & 
-               &         / ( e3uw(ji,jj,jk  ,Kmm) * e3uw(ji,jj,jk,Kbb) ) &
-               &         * wumask(ji,jj,jk)
-            zsh2v(ji,jj) = ( p_avm(ji,jj+1,jk) + p_avm(ji,jj,jk) ) &
-               &         * (   vv(ji,jj,jk-1,Kmm) -   vv(ji,jj,jk,Kmm) ) &
-               &         * (   vv(ji,jj,jk-1,Kbb) -   vv(ji,jj,jk,Kbb) ) &
-               &         / ( e3vw(ji,jj,jk  ,Kmm) * e3vw(ji,jj,jk,Kbb) ) &
-               &         * wvmask(ji,jj,jk)
-         END_2D
+      DO jk = 2, jpkm1                 !* Shear production at uw- and vw-points (energy conserving form)
+         IF ( cpl_sdrftx .AND. ln_stshear )  THEN       ! Surface Stokes Drift available  ===>>>  shear + stokes drift contibution
+            DO_2D( 1, 0, 1, 0 )
+               zsh2u(ji,jj) = ( p_avm(ji+1,jj,jk) + p_avm(ji,jj,jk) )        &
+                  &         * ( uu (ji,jj,jk-1,Kmm) -   uu (ji,jj,jk,Kmm)    &
+                  &           + usd(ji,jj,jk-1) -   usd(ji,jj,jk) )  &
+                  &         * ( uu (ji,jj,jk-1,Kbb) -   uu (ji,jj,jk,Kbb) )  &
+                  &         / ( e3uw(ji,jj,jk,Kmm) * e3uw(ji,jj,jk,Kbb) ) * wumask(ji,jj,jk)
+               zsh2v(ji,jj) = ( p_avm(ji,jj+1,jk) + p_avm(ji,jj,jk) )         &
+                  &         * ( vv (ji,jj,jk-1,Kmm) -   vv (ji,jj,jk,Kmm)     &
+                  &           + vsd(ji,jj,jk-1) -   vsd(ji,jj,jk) )   &
+                  &         * ( vv (ji,jj,jk-1,Kbb) -   vv (ji,jj,jk,Kbb) )   &
+                  &/ ( e3vw(ji,jj,jk,Kmm) * e3vw(ji,jj,jk,Kbb) ) * wvmask(ji,jj,jk)
+            END_2D
+         ELSE
+            DO_2D( 1, 0, 1, 0 )     !* 2 x shear production at uw- and vw-points (energy conserving form)
+               zsh2u(ji,jj) = ( p_avm(ji+1,jj,jk) + p_avm(ji,jj,jk) ) &
+                  &         * (   uu(ji,jj,jk-1,Kmm) -   uu(ji,jj,jk,Kmm) ) &
+                  &         * (   uu(ji,jj,jk-1,Kbb) -   uu(ji,jj,jk,Kbb) ) & 
+                  &         / ( e3uw(ji,jj,jk  ,Kmm) * e3uw(ji,jj,jk,Kbb) ) &
+                  &         * wumask(ji,jj,jk)
+               zsh2v(ji,jj) = ( p_avm(ji,jj+1,jk) + p_avm(ji,jj,jk) ) &
+                  &         * (   vv(ji,jj,jk-1,Kmm) -   vv(ji,jj,jk,Kmm) ) &
+                  &         * (   vv(ji,jj,jk-1,Kbb) -   vv(ji,jj,jk,Kbb) ) &
+                  &         / ( e3vw(ji,jj,jk  ,Kmm) * e3vw(ji,jj,jk,Kbb) ) &
+                  &         * wvmask(ji,jj,jk)
+            END_2D
+         ENDIF
          DO_2D( 0, 0, 0, 0 )     !* shear production at w-point ! coast mask: =2 at the coast ; =1 otherwise (NB: wmask useless as zsh2 are masked)
             p_sh2(ji,jj,jk) = 0.25 * (   ( zsh2u(ji-1,jj) + zsh2u(ji,jj) ) * ( 2. - umask(ji-1,jj,jk) * umask(ji,jj,jk) )   &
                &                       + ( zsh2v(ji,jj-1) + zsh2v(ji,jj) ) * ( 2. - vmask(ji,jj-1,jk) * vmask(ji,jj,jk) )   )
          END_2D
-      END DO 
+      END DO
       !
    END SUBROUTINE zdf_sh2
Index: /NEMO/branches/2020/tickets_icb_1900/src/OCE/ZDF/zdftke.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/OCE/ZDF/zdftke.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/OCE/ZDF/zdftke.F90	(revision 14016)
@@ -29,4 +29,6 @@
    !!            4.0  !  2017-04  (G. Madec)  remove CPP ddm key & avm at t-point only 
    !!             -   !  2017-05  (G. Madec)  add top/bottom friction as boundary condition
+   !!            4.2  !  2020-12  (G. Madec, E. Clementi) add wave coupling
+   !                  !           following Couvelard et al., 2019
    !!----------------------------------------------------------------------
 
@@ -58,4 +60,5 @@
    USE prtctl         ! Print control
    USE lib_fortran    ! Fortran utilities (allows no signed zero when 'key_nosignedzero' defined)  
+   USE sbcwave        ! Surface boundary waves
 
    IMPLICIT NONE
@@ -68,4 +71,5 @@
    !                      !!** Namelist  namzdf_tke  **
    LOGICAL  ::   ln_mxl0   ! mixing length scale surface value as function of wind stress or not
+   LOGICAL  ::   ln_mxhsw  ! mixing length scale surface value as a fonction of wave height
    INTEGER  ::   nn_mxlice ! type of scaling under sea-ice (=0/1/2/3)
    REAL(wp) ::   rn_mxlice ! ice thickness value when scaling under sea-ice
@@ -81,4 +85,6 @@
    INTEGER  ::   nn_etau   ! type of depth penetration of surface tke (=0/1/2/3)
    INTEGER  ::      nn_htau   ! type of tke profile of penetration (=0/1)
+   INTEGER  ::   nn_bc_surf! surface condition (0/1=Dir/Neum) ! Only applicable for wave coupling
+   INTEGER  ::   nn_bc_bot ! surface condition (0/1=Dir/Neum) ! Only applicable for wave coupling
    REAL(wp) ::      rn_efr    ! fraction of TKE surface value which penetrates in the ocean
    LOGICAL  ::   ln_lc     ! Langmuir cells (LC) as a source term of TKE or not
@@ -209,6 +215,7 @@
       REAL(wp) ::   zus   , zwlc  , zind       !   -      -
       REAL(wp) ::   zzd_up, zzd_lw             !   -      -
+      REAL(wp) ::   ztaui, ztauj, z1_norm
       INTEGER , DIMENSION(jpi,jpj)     ::   imlc
-      REAL(wp), DIMENSION(jpi,jpj)     ::   zice_fra, zhlc, zus3
+      REAL(wp), DIMENSION(jpi,jpj)     ::   zice_fra, zhlc, zus3, zWlc2
       REAL(wp), DIMENSION(jpi,jpj,jpk) ::   zpelc, zdiag, zd_up, zd_lw
       !!--------------------------------------------------------------------
@@ -219,4 +226,6 @@
       zfact2  = 1.5_wp * rn_Dt * rn_ediss
       zfact3  = 0.5_wp         * rn_ediss
+      !
+      zpelc(:,:,:) = 0._wp ! need to be initialised in case ln_lc is not used
       !
       ! ice fraction considered for attenuation of langmuir & wave breaking
@@ -232,10 +241,9 @@
       !                     !<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
       !
-      DO_2D( 0, 0, 0, 0 )         ! en(1)   = rn_ebb taum / rau0  (min value rn_emin0)
-!! clem: this should be the right formulation but it makes the model unstable unless drags are calculated implicitly
-!!       one way around would be to increase zbbirau 
-!!          en(ji,jj,1) = MAX( rn_emin0, ( ( 1._wp - fr_i(ji,jj) ) * zbbrau + &
-!!             &                                     fr_i(ji,jj)   * zbbirau ) * taum(ji,jj) ) * tmask(ji,jj,1)
+      DO_2D( 0, 0, 0, 0 )
          en(ji,jj,1) = MAX( rn_emin0, zbbrau * taum(ji,jj) ) * tmask(ji,jj,1)
+         zdiag(ji,jj,1) = 1._wp/en(ji,jj,1)
+         zd_lw(ji,jj,1) = 1._wp  
+         zd_up(ji,jj,1) = 0._wp
       END_2D
       !
@@ -274,8 +282,45 @@
       !
       !                     !<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
-      IF( ln_lc ) THEN      !  Langmuir circulation source term added to tke   !   (Axell JGR 2002)
+      IF( ln_lc ) THEN      !  Langmuir circulation source term added to tke (Axell JGR 2002)
          !                  !>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
          !
-         !                        !* total energy produce by LC : cumulative sum over jk
+         !                       !* Langmuir velocity scale
+         !
+         IF ( cpl_sdrftx )  THEN       ! Surface Stokes Drift available
+            !                                ! Craik-Leibovich velocity scale Wlc = ( u* u_s )^1/2    with u* = (taum/rho0)^1/2
+            !                                ! associated kinetic energy : 1/2 (Wlc)^2 = u* u_s
+            !                                ! more precisely, it is the dot product that must be used :
+            !                                !     1/2  (W_lc)^2 = MAX( u* u_s + v* v_s , 0 )   only the positive part
+!!gm  ! PS: currently we don't have neither the 2 stress components at t-point !nor the angle between u* and u_s
+!!gm  ! so we will overestimate the LC velocity....   !!gm I will do the work if !LC have an effect !
+            DO_2D( 0, 0, 0, 0 )
+!!XC                  zWlc2(ji,jj) = 0.5_wp * SQRT( taum(ji,jj) * r1_rho0 * ( ut0sd(ji,jj)**2 +vt0sd(ji,jj)**2 )  )
+                  zWlc2(ji,jj) = 0.5_wp *  ( ut0sd(ji,jj)**2 +vt0sd(ji,jj)**2 )
+            END_2D
+!
+!  Projection of Stokes drift in the wind stress direction
+!
+            DO_2D( 0, 0, 0, 0 )
+                  ztaui   = 0.5_wp * ( utau(ji,jj) + utau(ji-1,jj) )
+                  ztauj   = 0.5_wp * ( vtau(ji,jj) + vtau(ji,jj-1) )
+                  z1_norm = 1._wp / MAX( SQRT(ztaui*ztaui+ztauj*ztauj), 1.e-12 ) * tmask(ji,jj,1)
+                  zWlc2(ji,jj) = 0.5_wp * z1_norm * ( MAX( ut0sd(ji,jj)*ztaui + vt0sd(ji,jj)*ztauj, 0._wp ) )**2
+            END_2D
+         CALL lbc_lnk      ( 'zdftke', zWlc2, 'T', 1. )
+!
+         ELSE                          ! Surface Stokes drift deduced from surface stress
+            !                                ! Wlc = u_s   with u_s = 0.016*U_10m, the surface stokes drift  (Axell 2002, Eq.44)
+            !                                ! using |tau| = rho_air Cd |U_10m|^2 , it comes:
+            !                                ! Wlc = 0.016 * [|tau|/(rho_air Cdrag) ]^1/2   and thus:
+            !                                ! 1/2 Wlc^2 = 0.5 * 0.016 * 0.016 |tau| /( rho_air Cdrag )
+            zcof = 0.5 * 0.016 * 0.016 / ( zrhoa * zcdrag )      ! to convert stress in 10m wind using a constant drag
+            DO_2D( 1, 1, 1, 1 )
+               zWlc2(ji,jj) = zcof * taum(ji,jj)
+            END_2D
+            !
+         ENDIF
+         !
+         !                       !* Depth of the LC circulation  (Axell 2002, Eq.47)
+         !                             !- LHS of Eq.47
          zpelc(:,:,1) =  MAX( rn2b(:,:,1), 0._wp ) * gdepw(:,:,1,Kmm) * e3w(:,:,1,Kmm)
          DO jk = 2, jpk
@@ -283,10 +328,9 @@
                &        MAX( rn2b(:,:,jk), 0._wp ) * gdepw(:,:,jk,Kmm) * e3w(:,:,jk,Kmm)
          END DO
-         !                        !* finite Langmuir Circulation depth
-         zcof = 0.5 * 0.016 * 0.016 / ( zrhoa * zcdrag )
+         !
+         !                             !- compare LHS to RHS of Eq.47
          imlc(:,:) = mbkt(:,:) + 1       ! Initialization to the number of w ocean point (=2 over land)
-         DO_3DS( 1, 1, 1, 1, jpkm1, 2, -1 )   ! Last w-level at which zpelc>=0.5*us*us 
-            zus = zcof * taum(ji,jj)          !      with us=0.016*wind(starting from jpk-1)
-            IF( zpelc(ji,jj,jk) > zus )   imlc(ji,jj) = jk
+         DO_3DS( 1, 1, 1, 1, jpkm1, 2, -1 )
+            IF( zpelc(ji,jj,jk) > zWlc2(ji,jj) )   imlc(ji,jj) = jk
          END_3D
          !                               ! finite LC depth
@@ -294,7 +338,8 @@
             zhlc(ji,jj) = gdepw(ji,jj,imlc(ji,jj),Kmm)
          END_2D
+         !
          zcof = 0.016 / SQRT( zrhoa * zcdrag )
          DO_2D( 0, 0, 0, 0 )
-            zus  = zcof * SQRT( taum(ji,jj) )           ! Stokes drift
+            zus = SQRT( 2. * zWlc2(ji,jj) )             ! Stokes drift
             zus3(ji,jj) = MAX( 0._wp, 1._wp - zice_fra(ji,jj) ) * zus * zus * zus * tmask(ji,jj,1) ! zus > 0. ok
          END_2D
@@ -351,12 +396,43 @@
             &                                ) * wmask(ji,jj,jk)
       END_3D
+      !
+      !                     !<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+      !                     !  Surface boundary condition on tke if
+      !                     !  coupling with waves
+      !                     !<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+      !
+      IF ( cpl_phioc .and. ln_phioc )  THEN
+         SELECT CASE (nn_bc_surf) ! Boundary Condition using surface TKE flux from waves 
+
+         CASE ( 0 ) ! Dirichlet BC
+            DO_2D( 0, 0, 0, 0 )    ! en(1)   = rn_ebb taum / rho0  (min value rn_emin0)
+               IF ( phioc(ji,jj) < 0 )  phioc(ji,jj) = 0._wp
+               en(ji,jj,1) = MAX( rn_emin0, .5 * ( 15.8 * phioc(ji,jj) / rho0 )**(2./3.) )  * tmask(ji,jj,1)
+               zdiag(ji,jj,1) = 1._wp/en(ji,jj,1)  ! choose to keep coherence with former estimation of
+            END_2D
+
+         CASE ( 1 ) ! Neumann BC
+            DO_2D( 0, 0, 0, 0 )
+               IF ( phioc(ji,jj) < 0 )  phioc(ji,jj) = 0._wp
+               en(ji,jj,2)    = en(ji,jj,2) + ( rn_Dt * phioc(ji,jj) / rho0 ) /e3w(ji,jj,2,Kmm)
+               en(ji,jj,1)    = en(ji,jj,2) + (2 * e3t(ji,jj,1,Kmm) * phioc(ji,jj)/rho0) / ( p_avm(ji,jj,1) + p_avm(ji,jj,2) )
+               zdiag(ji,jj,2) = zdiag(ji,jj,2) + zd_lw(ji,jj,2)
+               zdiag(ji,jj,1) = 1._wp
+               zd_lw(ji,jj,2) = 0._wp
+            END_2D
+
+         END SELECT
+
+      ENDIF
+      !
       !                          !* Matrix inversion from level 2 (tke prescribed at level 1)
-      DO_3D( 0, 0, 0, 0, 3, jpkm1 )                ! First recurrence : Dk = Dk - Lk * Uk-1 / Dk-1
+      DO_3D( 0, 0, 0, 0, 2, jpkm1 )                ! First recurrence : Dk = Dk - Lk * Uk-1 / Dk-1
          zdiag(ji,jj,jk) = zdiag(ji,jj,jk) - zd_lw(ji,jj,jk) * zd_up(ji,jj,jk-1) / zdiag(ji,jj,jk-1)
       END_3D
-      DO_2D( 0, 0, 0, 0 )                          ! Second recurrence : Lk = RHSk - Lk / Dk-1 * Lk-1
-         zd_lw(ji,jj,2) = en(ji,jj,2) - zd_lw(ji,jj,2) * en(ji,jj,1)    ! Surface boudary conditions on tke
-      END_2D
-      DO_3D( 0, 0, 0, 0, 3, jpkm1 )
+!XC : commented to allow for neumann boundary condition
+!      DO_2D( 0, 0, 0, 0 )
+!         zd_lw(ji,jj,2) = en(ji,jj,2) - zd_lw(ji,jj,2) * en(ji,jj,1)    ! Surface boudary conditions on tke
+!      END_2D
+      DO_3D( 0, 0, 0, 0, 2, jpkm1 )
          zd_lw(ji,jj,jk) = en(ji,jj,jk) - zd_lw(ji,jj,jk) / zdiag(ji,jj,jk-1) *zd_lw(ji,jj,jk-1)
       END_3D
@@ -460,56 +536,63 @@
       zmxlm(:,:,:)  = rmxl_min    
       zmxld(:,:,:)  = rmxl_min
+      !
+      IF(ln_sdw .AND. ln_mxhsw) THEN
+         zmxlm(:,:,1)= vkarmn * MAX ( 1.6 * hsw(:,:) , 0.02 )        ! surface mixing length = F(wave height)
+         ! from terray et al 1999 and mellor and blumberg 2004 it should be 0.85 and not 1.6
+         zcoef       = vkarmn * ( (rn_ediff*rn_ediss)**0.25 ) / rn_ediff
+         zmxlm(:,:,1)= zcoef * MAX ( 1.6 * hsw(:,:) , 0.02 )        ! surface mixing length = F(wave height)
+      ELSE
       ! 
-     IF( ln_mxl0 ) THEN            ! surface mixing length = F(stress) : l=vkarmn*2.e5*taum/(rho0*g)
-         !
-         zraug = vkarmn * 2.e5_wp / ( rho0 * grav )
+         IF( ln_mxl0 ) THEN            ! surface mixing length = F(stress) : l=vkarmn*2.e5*taum/(rho0*g)
+         !
+            zraug = vkarmn * 2.e5_wp / ( rho0 * grav )
 #if ! defined key_si3 && ! defined key_cice
-         DO_2D( 0, 0, 0, 0 )                  ! No sea-ice
-            zmxlm(ji,jj,1) =  zraug * taum(ji,jj) * tmask(ji,jj,1)
-         END_2D
+            DO_2D( 0, 0, 0, 0 )                  ! No sea-ice
+               zmxlm(ji,jj,1) =  zraug * taum(ji,jj) * tmask(ji,jj,1)
+            END_2D
 #else
-         SELECT CASE( nn_mxlice )             ! Type of scaling under sea-ice
-         !
-         CASE( 0 )                      ! No scaling under sea-ice
+            SELECT CASE( nn_mxlice )             ! Type of scaling under sea-ice
+            !
+            CASE( 0 )                      ! No scaling under sea-ice
+               DO_2D( 0, 0, 0, 0 )
+                  zmxlm(ji,jj,1) = zraug * taum(ji,jj) * tmask(ji,jj,1)
+               END_2D
+               !
+            CASE( 1 )                      ! scaling with constant sea-ice thickness
+               DO_2D( 0, 0, 0, 0 )
+                  zmxlm(ji,jj,1) =  ( ( 1._wp - fr_i(ji,jj) ) * zraug * taum(ji,jj) + &
+                     &                          fr_i(ji,jj)   * rn_mxlice           ) * tmask(ji,jj,1)
+               END_2D
+               !
+            CASE( 2 )                      ! scaling with mean sea-ice thickness
+               DO_2D( 0, 0, 0, 0 )
+#if defined key_si3
+                  zmxlm(ji,jj,1) = ( ( 1._wp - fr_i(ji,jj) ) * zraug * taum(ji,jj) + &
+                     &                         fr_i(ji,jj)   * hm_i(ji,jj) * 2._wp ) * tmask(ji,jj,1)
+#elif defined key_cice
+                  zmaxice = MAXVAL( h_i(ji,jj,:) )
+                  zmxlm(ji,jj,1) = ( ( 1._wp - fr_i(ji,jj) ) * zraug * taum(ji,jj) + &
+                     &                         fr_i(ji,jj)   * zmaxice             ) * tmask(ji,jj,1)
+#endif
+               END_2D
+               !
+            CASE( 3 )                      ! scaling with max sea-ice thickness
+               DO_2D( 0, 0, 0, 0 )
+                  zmaxice = MAXVAL( h_i(ji,jj,:) )
+                  zmxlm(ji,jj,1) = ( ( 1._wp - fr_i(ji,jj) ) * zraug * taum(ji,jj) + &
+                     &                         fr_i(ji,jj)   * zmaxice             ) * tmask(ji,jj,1)
+               END_2D
+               !
+            END SELECT
+#endif
+            !
             DO_2D( 0, 0, 0, 0 )
-               zmxlm(ji,jj,1) = zraug * taum(ji,jj) * tmask(ji,jj,1)
+               zmxlm(ji,jj,1) = MAX( rn_mxl0, zmxlm(ji,jj,1) )
             END_2D
             !
-         CASE( 1 )                      ! scaling with constant sea-ice thickness
-            DO_2D( 0, 0, 0, 0 )
-               zmxlm(ji,jj,1) =  ( ( 1._wp - fr_i(ji,jj) ) * zraug * taum(ji,jj) + &
-                  &                          fr_i(ji,jj)   * rn_mxlice           ) * tmask(ji,jj,1)
-            END_2D
-            !
-         CASE( 2 )                      ! scaling with mean sea-ice thickness
-            DO_2D( 0, 0, 0, 0 )
-#if defined key_si3
-               zmxlm(ji,jj,1) = ( ( 1._wp - fr_i(ji,jj) ) * zraug * taum(ji,jj) + &
-                  &                         fr_i(ji,jj)   * hm_i(ji,jj) * 2._wp ) * tmask(ji,jj,1)
-#elif defined key_cice
-               zmaxice = MAXVAL( h_i(ji,jj,:) )
-               zmxlm(ji,jj,1) = ( ( 1._wp - fr_i(ji,jj) ) * zraug * taum(ji,jj) + &
-                  &                         fr_i(ji,jj)   * zmaxice             ) * tmask(ji,jj,1)
-#endif
-            END_2D
-            !
-         CASE( 3 )                      ! scaling with max sea-ice thickness
-            DO_2D( 0, 0, 0, 0 )
-               zmaxice = MAXVAL( h_i(ji,jj,:) )
-               zmxlm(ji,jj,1) = ( ( 1._wp - fr_i(ji,jj) ) * zraug * taum(ji,jj) + &
-                  &                         fr_i(ji,jj)   * zmaxice             ) * tmask(ji,jj,1)
-            END_2D
-            !
-         END SELECT
-#endif
-         !
-         DO_2D( 0, 0, 0, 0 )
-            zmxlm(ji,jj,1) = MAX( rn_mxl0, zmxlm(ji,jj,1) )
-         END_2D
-         !
-      ELSE
-         zmxlm(:,:,1) = rn_mxl0
-      ENDIF
-
+         ELSE
+            zmxlm(:,:,1) = rn_mxl0
+         ENDIF
+      ENDIF
       !
       DO_3D( 0, 0, 0, 0, 2, jpkm1 )
@@ -624,5 +707,6 @@
          &                 rn_mxl0 , nn_mxlice, rn_mxlice,             &
          &                 nn_pdl  , ln_lc    , rn_lc    ,             &
-         &                 nn_etau , nn_htau  , rn_efr   , nn_eice  
+         &                 nn_etau , nn_htau  , rn_efr   , nn_eice  ,  &   
+         &                 nn_bc_surf, nn_bc_bot, ln_mxhsw
       !!----------------------------------------------------------------------
       !
@@ -666,4 +750,10 @@
          WRITE(numout,*) '      Langmuir cells parametrization              ln_lc     = ', ln_lc
          WRITE(numout,*) '         coef to compute vertical velocity of LC     rn_lc  = ', rn_lc
+         IF ( cpl_phioc .and. ln_phioc )  THEN
+            SELECT CASE( nn_bc_surf)             ! Type of scaling under sea-ice
+            CASE( 0 )   ;   WRITE(numout,*) '  nn_bc_surf=0 ==>>> DIRICHLET SBC using surface TKE flux from waves'
+            CASE( 1 )   ;   WRITE(numout,*) '  nn_bc_surf=1 ==>>> NEUMANN SBC using surface TKE flux from waves'
+            END SELECT
+         ENDIF
          WRITE(numout,*) '      test param. to add tke induced by wind      nn_etau   = ', nn_etau
          WRITE(numout,*) '          type of tke penetration profile            nn_htau   = ', nn_htau
Index: /NEMO/branches/2020/tickets_icb_1900/src/OCE/step.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/OCE/step.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/OCE/step.F90	(revision 14016)
@@ -296,4 +296,5 @@
 
                             CALL tra_adv    ( kstp, Nbb, Nnn, ts, Nrhs )  ! hor. + vert. advection	==> RHS
+         IF( ln_zdfmfc  )   CALL tra_mfc    ( kstp, Nbb,      ts, Nrhs )  ! Mass Flux Convection 
          IF( ln_zdfosm  )   CALL tra_osm    ( kstp,      Nnn, ts, Nrhs )  ! OSMOSIS non-local tracer fluxes ==> RHS
          IF( lrst_oce .AND. ln_zdfosm ) &
Index: /NEMO/branches/2020/tickets_icb_1900/src/OCE/step_oce.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/src/OCE/step_oce.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/src/OCE/step_oce.F90	(revision 14016)
@@ -70,4 +70,5 @@
    USE zdfphy          ! vertical physics manager      (zdf_phy_init routine)
    USE zdfosm  , ONLY : osm_rst, dyn_osm, tra_osm      ! OSMOSIS routines used in step.F90
+   USE zdfmfc          ! Mass FLux Convection routine used in step.F90
 
    USE diu_layers      ! diurnal SST bulk and coolskin routines
Index: /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/EXP00/axis_def_nemo.xml
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/EXP00/axis_def_nemo.xml	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/EXP00/axis_def_nemo.xml	(revision 14016)
@@ -0,0 +1,1 @@
+link ../../../cfgs/SHARED/axis_def_nemo.xml
Index: /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/EXP00/context_nemo.xml
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/EXP00/context_nemo.xml	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/EXP00/context_nemo.xml	(revision 14016)
@@ -0,0 +1,25 @@
+<!--
+ ============================================================================================== 
+    NEMO context
+============================================================================================== 
+-->
+<context id="nemo">
+    <!-- $id$ -->
+
+<!-- Fields definition -->
+    <field_definition src="./field_def_nemo-oce.xml"/>   <!--  NEMO ocean dynamics                     -->
+
+<!-- Files definition -->
+    <file_definition src="./file_def_nemo-oce.xml"/>     <!--  NEMO ocean dynamics                     -->
+
+<!-- Axis definition -->
+    <axis_definition src="./axis_def_nemo.xml"/>
+ 
+<!-- Domain definition -->
+    <domain_definition src="./domain_def_nemo.xml"/>
+
+<!-- Grids definition -->
+    <grid_definition   src="./grid_def_nemo.xml"/>
+  
+
+</context>
Index: /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/EXP00/domain_def_nemo.xml
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/EXP00/domain_def_nemo.xml	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/EXP00/domain_def_nemo.xml	(revision 14016)
@@ -0,0 +1,1 @@
+link ../../../cfgs/SHARED/domain_def_nemo.xml
Index: /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/EXP00/field_def_nemo-oce.xml
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/EXP00/field_def_nemo-oce.xml	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/EXP00/field_def_nemo-oce.xml	(revision 14016)
@@ -0,0 +1,1 @@
+link ../../../cfgs/SHARED/field_def_nemo-oce.xml
Index: /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/EXP00/file_def_nemo-oce.xml
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/EXP00/file_def_nemo-oce.xml	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/EXP00/file_def_nemo-oce.xml	(revision 14016)
@@ -0,0 +1,104 @@
+<?xml version="1.0"?>
+
+    <!-- 
+============================================================================================================
+=                                           output files definition                                        =
+=                                            Define your own files                                         =
+=                                         put the variables you want...                                    =
+============================================================================================================
+    -->
+    
+    <file_definition type="one_file" name="@expname@_@freq@_@startdate@_@enddate@" sync_freq="1h" min_digits="4">
+    
+      <file_group id="1ts" output_freq="1ts"  output_level="10" enabled=".TRUE."/> <!-- 1 time step files -->
+
+      <file_group id="1h" output_freq="1h"  output_level="10" enabled=".TRUE."/> <!-- 1h files -->
+
+      <file_group id="2h" output_freq="2h"  output_level="10" enabled=".TRUE."/> <!-- 2h files -->
+
+      <file_group id="3h" output_freq="3h"  output_level="10" enabled=".TRUE."/> <!-- 3h files -->     
+
+      <file_group id="4h" output_freq="4h"  output_level="10" enabled=".TRUE."/> <!-- 4h files -->
+
+      <file_group id="6h" output_freq="6h"  output_level="10" enabled=".TRUE."> <!-- 6h files -->     
+        <file id="file61" name_suffix="_grid_T" description="ocean T grid variables" >
+          <field field_ref="e3t"                           />
+          <field field_ref="toce"         name="votemper"     operation="instant" freq_op="1d" > @toce_e3t / @e3t </field>
+          <field field_ref="soce"         name="vosaline"     operation="instant" freq_op="1d" > @soce_e3t / @e3t </field>
+          <field field_ref="mldkz5"                       />
+          <field field_ref="mldr10_1"                     />
+          <field field_ref="empmr"        name="wfo"      />
+          <field field_ref="qsr_oce"      name="qsr_oce"  />
+          <field field_ref="qns_oce"      name="qns_oce"  />
+          <field field_ref="qt_oce"       name="qt_oce"   />
+          <field field_ref="saltflx"      name="sfx"      />
+          <field field_ref="taum"         name="taum"     />
+          <field field_ref="wspd"         name="windsp"   />
+          <field field_ref="precip"       name="precip"   />
+        </file>
+      </file_group>
+
+
+      <file_group id="12h" output_freq="12h"  output_level="10" enabled=".TRUE."> <!-- 1d files -->
+
+        <file id="file1" name_suffix="_grid_T" description="ocean T grid variables" >
+          <field field_ref="e3t"                           />
+          <field field_ref="toce"         name="votemper"     operation="instant" freq_op="1d" > @toce_e3t / @e3t </field>
+          <field field_ref="soce"         name="vosaline"     operation="instant" freq_op="1d" > @soce_e3t / @e3t </field>
+          <field field_ref="mldkz5"                       />
+          <field field_ref="mldr10_1"                     />
+          <field field_ref="empmr"        name="wfo"      />
+          <field field_ref="qsr_oce"      name="qsr_oce"  />
+          <field field_ref="qns_oce"      name="qns_oce"  />
+          <field field_ref="qt_oce"       name="qt_oce"   />
+          <field field_ref="saltflx"      name="sfx"      />
+          <field field_ref="taum"         name="taum"     />
+          <field field_ref="wspd"         name="windsp"   />
+          <field field_ref="precip"       name="precip"   />
+        </file>
+
+       <file id="file2" name_suffix="_grid_U" description="ocean U grid variables" >
+          <field field_ref="uoce"         name="uo"       operation="instant" freq_op="1d" > @uoce_e3u / @e3u </field>
+          <field field_ref="utau"         name="tauuo"    />
+        </file>
+
+       <file id="file3" name_suffix="_grid_V" description="ocean V grid variables" >
+          <field field_ref="voce"         name="vo"       operation="instant" freq_op="1d" > @voce_e3v / @e3v </field>
+          <field field_ref="vtau"         name="tauvo"    />
+        </file>
+
+        <file id="file4" name_suffix="_grid_W" description="ocean W grid variables" >
+          <field field_ref="e3w" />
+          <field field_ref="avt"          name="difvho"   />
+        </file>
+
+      </file_group>
+
+      
+      <file_group id="1d" output_freq="1d"  output_level="10" enabled=".TRUE."> <!-- 1d files -->
+        <file id="file60" name_suffix="_grid_T" description="ocean T grid variables" >
+          <field field_ref="e3t"                           />
+          <field field_ref="toce"         name="votemper"     operation="instant" freq_op="1d" > @toce_e3t / @e3t </field>
+          <field field_ref="soce"         name="vosaline"     operation="instant" freq_op="1d" > @soce_e3t / @e3t </field>
+          <field field_ref="mldkz5"                       />
+          <field field_ref="mldr10_1"                     />
+          <field field_ref="empmr"        name="wfo"      />
+          <field field_ref="qsr_oce"      name="qsr_oce"  />
+          <field field_ref="qns_oce"      name="qns_oce"  />
+          <field field_ref="qt_oce"       name="qt_oce"   />
+          <field field_ref="saltflx"      name="sfx"      />
+          <field field_ref="taum"         name="taum"     />
+          <field field_ref="wspd"         name="windsp"   />
+          <field field_ref="precip"       name="precip"   />
+        </file>
+      </file_group>
+
+      <file_group id="3d" output_freq="3d"  output_level="10" enabled=".TRUE."/> <!-- 3d files -->
+      <file_group id="5d" output_freq="5d"  output_level="10" enabled=".TRUE."/> <!-- 5d files -->   
+      <file_group id="1m" output_freq="1mo" output_level="10" enabled=".TRUE."/> <!-- real monthly files -->
+      <file_group id="2y"  output_freq="2y" output_level="10" enabled=".TRUE."/> <!-- real 2y files -->
+      <file_group id="5y"  output_freq="5y" output_level="10" enabled=".TRUE."/> <!-- real 5y files -->
+      <file_group id="10y" output_freq="10y" output_level="10" enabled=".TRUE."/> <!-- real 10y files -->
+
+   </file_definition>
+    
Index: /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/EXP00/file_def_nemo-oce.xml_ori
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/EXP00/file_def_nemo-oce.xml_ori	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/EXP00/file_def_nemo-oce.xml_ori	(revision 14016)
@@ -0,0 +1,141 @@
+<?xml version="1.0"?>
+
+    <!-- 
+============================================================================================================
+=                                           output files definition                                        =
+=                                            Define your own files                                         =
+=                                         put the variables you want...                                    =
+============================================================================================================
+    -->
+    
+    <file_definition type="one_file" name="@expname@_@freq@_@startdate@_@enddate@" sync_freq="10d" min_digits="4">
+    
+      <file_group id="1ts" output_freq="1ts"  output_level="10" enabled=".TRUE."> <!-- 1 time step files -->
+      </file_group>
+
+
+      <file_group id="1h" output_freq="1h"  output_level="10" enabled=".TRUE."> <!-- 1h files -->
+      </file_group>
+
+      <file_group id="2h" output_freq="2h"  output_level="10" enabled=".TRUE."/> <!-- 2h files -->
+
+      <file_group id="3h" output_freq="3h"  output_level="10" enabled=".TRUE."/> <!-- 3h files -->     
+
+      <file_group id="4h" output_freq="4h"  output_level="10" enabled=".TRUE."/> <!-- 4h files -->
+
+      <file_group id="6h" output_freq="6h"  output_level="10" enabled=".TRUE."/> <!-- 6h files -->  
+
+   
+      <file_group id="12h" output_freq="12h"  output_level="10" enabled=".TRUE."> <!-- 6h files -->  
+
+        <file id="file105" name_suffix="_grid_T" description="ocean T grid variables" >
+          <field field_ref="e3t"                           />
+          <field field_ref="toce"         name="votemper"     operation="instant" freq_op="12h" > @toce_e3t / @e3t </field>
+          <field field_ref="soce"         name="vosaline"     operation="instant" freq_op="12h" > @soce_e3t / @e3t </field>
+          <field field_ref="mldkz5"                       />
+          <field field_ref="mldr10_1"                     />
+          <field field_ref="mlddzt"                     />
+          <field field_ref="mldr0_1"                     />
+          <field field_ref="tinv"                     />
+          <field field_ref="empmr"        name="wfo"      />
+          <field field_ref="qsr_oce"      name="qsr_oce"  />
+          <field field_ref="qns_oce"      name="qns_oce"  />
+          <field field_ref="qt_oce"       name="qt_oce"   />
+          <field field_ref="saltflx"      name="sfx"      />
+          <field field_ref="taum"         name="taum"     />
+          <field field_ref="wspd"         name="windsp"   />
+          <field field_ref="precip"       name="precip"   />
+          <field field_ref="mf_Tp"         />
+          <field field_ref="mf_Sp"         />
+          <field field_ref="mf_epsT"         />
+          <field field_ref="mf_epsW"         />
+          <field field_ref="mf_edmf" />
+          <field field_ref="mf_FT" />
+          <field field_ref="mf_FS" />
+        </file>
+
+       <file id="file106" name_suffix="_grid_U" description="ocean U grid variables" >
+          <field field_ref="uoce"         name="uo"       operation="instant" freq_op="12h" > @uoce_e3u / @e3u </field>
+          <field field_ref="utau"         name="tauuo"    />
+        </file>
+
+       <file id="file107" name_suffix="_grid_V" description="ocean V grid variables" >
+          <field field_ref="voce"         name="vo"       operation="instant" freq_op="12h" > @voce_e3v / @e3v </field>
+          <field field_ref="vtau"         name="tauvo"    />
+        </file>
+
+        <file id="file108" name_suffix="_grid_W" description="ocean W grid variables" >
+          <field field_ref="e3w" />
+          <field field_ref="woce"         name="wo"       />
+          <field field_ref="avt"          name="difvho"   />
+          <field field_ref="mf_wp" />
+          <field field_ref="mf_wp0" />
+          <field field_ref="mf_app" />
+          <field field_ref="KZ_FT" />
+          <field field_ref="KZ_FS" />
+        </file>
+
+      </file_group>
+
+   
+      <file_group id="1d" output_freq="1d"  output_level="10" enabled=".TRUE."> <!-- 1d files -->
+
+        <file id="file5" name_suffix="_grid_T" description="ocean T grid variables" >
+          <field field_ref="e3t"                           />
+          <field field_ref="toce"         name="votemper"     operation="instant" freq_op="1d" > @toce_e3t / @e3t </field>
+          <field field_ref="soce"         name="vosaline"     operation="instant" freq_op="1d" > @soce_e3t / @e3t </field>
+          <field field_ref="mldkz5"                       />
+          <field field_ref="mldr10_1"                     />
+          <field field_ref="mlddzt"                     />
+          <field field_ref="mldr0_1"                     />
+          <field field_ref="tinv"                     />
+          <field field_ref="empmr"        name="wfo"      />
+          <field field_ref="qsr_oce"      name="qsr_oce"  />
+          <field field_ref="qns_oce"      name="qns_oce"  />
+          <field field_ref="qt_oce"       name="qt_oce"   />
+          <field field_ref="saltflx"      name="sfx"      />
+          <field field_ref="taum"         name="taum"     />
+          <field field_ref="wspd"         name="windsp"   />
+          <field field_ref="precip"       name="precip"   />
+          <field field_ref="mf_Tp"         />
+          <field field_ref="mf_Sp"         />
+          <field field_ref="mf_epsT"         />
+          <field field_ref="mf_epsW"         />
+          <field field_ref="mf_edmf" />
+          <field field_ref="mf_FT" />
+          <field field_ref="mf_FS" />
+        </file>
+
+       <file id="file6" name_suffix="_grid_U" description="ocean U grid variables" >
+          <field field_ref="uoce"         name="uo"       operation="instant" freq_op="1d" > @uoce_e3u / @e3u </field>
+          <field field_ref="utau"         name="tauuo"    />
+        </file>
+
+       <file id="file7" name_suffix="_grid_V" description="ocean V grid variables" >
+          <field field_ref="voce"         name="vo"       operation="instant" freq_op="1d" > @voce_e3v / @e3v </field>
+          <field field_ref="vtau"         name="tauvo"    />
+        </file>
+
+        <file id="file8" name_suffix="_grid_W" description="ocean W grid variables" >
+          <field field_ref="e3w" />
+          <field field_ref="woce"         name="wo"       />
+          <field field_ref="avt"          name="difvho"   />
+          <field field_ref="mf_wp" />
+          <field field_ref="mf_wp0" />
+          <field field_ref="mf_app" />
+          <field field_ref="KZ_FT" />
+          <field field_ref="KZ_FS" />
+        </file>
+
+      </file_group>
+
+      
+      <file_group id="3d" output_freq="3d"  output_level="10" enabled=".TRUE."/> <!-- 3d files -->
+      <file_group id="5d" output_freq="5d"  output_level="10" enabled=".TRUE."/> <!-- 5d files -->   
+      <file_group id="1m" output_freq="1mo" output_level="10" enabled=".TRUE."/> <!-- real monthly files -->
+      <file_group id="2y"  output_freq="2y" output_level="10" enabled=".TRUE."/> <!-- real 2y files -->
+      <file_group id="5y"  output_freq="5y" output_level="10" enabled=".TRUE."/> <!-- real 5y files -->
+      <file_group id="10y" output_freq="10y" output_level="10" enabled=".TRUE."/> <!-- real 10y files -->
+
+   </file_definition>
+    
Index: /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/EXP00/grid_def_nemo.xml
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/EXP00/grid_def_nemo.xml	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/EXP00/grid_def_nemo.xml	(revision 14016)
@@ -0,0 +1,1 @@
+link ../../../cfgs/SHARED/grid_def_nemo.xml
Index: /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/EXP00/iodef.xml
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/EXP00/iodef.xml	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/EXP00/iodef.xml	(revision 14016)
@@ -0,0 +1,26 @@
+<?xml version="1.0"?>
+<simulation>
+
+<!-- ============================================================================================ -->
+<!-- XIOS context                                                                                 -->
+<!-- ============================================================================================ -->
+
+  <context id="xios" >
+
+      <variable_definition>
+
+          <variable id="info_level"                type="int">10</variable>
+          <variable id="using_server"              type="bool">true</variable>
+          <variable id="using_oasis"               type="bool">false</variable>
+          <variable id="oasis_codes_id"            type="string" >oceanx</variable>
+
+      </variable_definition>
+  </context>
+
+<!-- ============================================================================================ -->
+<!-- NEMO  CONTEXT add and suppress the components you need                                       -->
+<!-- ============================================================================================ -->
+
+  <context id="nemo" src="./context_nemo.xml"/>       <!--  NEMO       -->
+
+</simulation>
Index: /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/EXP00/namelist_cfg
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/EXP00/namelist_cfg	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/EXP00/namelist_cfg	(revision 14016)
@@ -0,0 +1,506 @@
+!!>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+!! NEMO/OCE :   Reference namelist_ref                                !!
+!!>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+!! NEMO/OCE  :  1 - Domain & run manager (namrun, namcfg, namdom, namtsd, namcrs, namc1d, namc1d_uvd)
+!! namelists    2 - Surface boundary (namsbc, namsbc_flx, namsbc_blk, namsbc_cpl,
+!!                                    namsbc_sas, namtra_qsr, namsbc_rnf,
+!!                                    namsbc_isf, namsbc_iscpl, namsbc_apr, 
+!!                                    namsbc_ssr, namsbc_wave, namberg)
+!!              3 - lateral boundary (namlbc, namagrif, nambdy, nambdy_tide)
+!!              4 - top/bot boundary (namdrg, namdrg_top, namdrg_bot, nambbc, nambbl)
+!!              5 - Tracer           (nameos, namtra_adv, namtra_ldf, namtra_eiv, namtra_dmp)
+!!              6 - dynamics         (namdyn_adv, namdyn_vor, namdyn_hpg, namdyn_spg, namdyn_ldf)
+!!              7 - Vertical physics (namzdf, namzdf_ric, namzdf_tke, namzdf_gls, namzdf_iwm)
+!!              8 - diagnostics      (namnc4, namtrd, namspr, namflo, namhsb)
+!!              9 - Obs & Assim      (namobs, nam_asminc)
+!!             10 - miscellaneous    (nammpp, namctl, namsto)
+!!>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+
+!!======================================================================
+!!              ***  Domain & Run management namelists  ***           !!
+!!                                                                    !!
+!!   namrun       parameters of the run
+!!   namdom       space and time domain
+!!   namcfg       parameters of the configuration                       (default: user defined GYRE)
+!!   namwad       Wetting and drying                                    (default: OFF)
+!!   namtsd       data: temperature & salinity                          (default: OFF)
+!!   namcrs       coarsened grid (for outputs and/or TOP)               (ln_crs =T)
+!!   namc1d       1D configuration options                              ("key_c1d")
+!!   namc1d_dyndmp 1D newtonian damping applied on currents             ("key_c1d")
+!!   namc1d_uvd   1D data (currents)                                    ("key_c1d")
+!!======================================================================
+!
+!-----------------------------------------------------------------------
+&namusr_def    !   C1D user defined namelist
+!-----------------------------------------------------------------------
+   rn_bathy    =  2380.   ! depth in meters
+/
+!-----------------------------------------------------------------------
+&namrun        !   parameters of the run
+!-----------------------------------------------------------------------
+   cn_exp      = "C1D_ASICS"!  experience name
+   nn_it000    =       1   !  first time step
+   nn_itend    =   17040   !  last  time step (std 5475)
+   nn_date0    = 20130115  !  date at nit_0000 (format yyyymmdd) used if ln_rstart=F or (ln_rstart=T and nn_rstctl=0 or 1)
+   nn_stock    =   17040   !  frequency of creation of a restart file (modulo referenced to 1)
+   nn_write    =   17040   !  frequency of write in the output file   (modulo referenced to nn_it000)
+/
+!-----------------------------------------------------------------------
+&namdom        !   time and space domain
+!-----------------------------------------------------------------------
+   ln_linssh   = .true.   !  =T  linear free surface  ==>>  model level are fixed in time
+   !
+   rn_Dt      =  360.     !  time step for the dynamics and tracer
+/
+!-----------------------------------------------------------------------
+&namcfg        !   parameters of the configuration                      (default: use namusr_def in namelist_cfg)
+!-----------------------------------------------------------------------
+/
+!-----------------------------------------------------------------------
+&namtsd        !    Temperature & Salinity Data  (init/dmp)             (default: OFF)
+!-----------------------------------------------------------------------
+   !                       ! =T  read T-S fields for:
+   ln_tsd_init = .true.          !  ocean initialisation
+   
+   cn_dir      = './'      !  root directory for the T-S data location
+   !___________!_________________________!___________________!___________!_____________!________!___________!__________________!__________!_______________!
+   !           !  file name              ! frequency (hours) ! variable  ! time interp.!  clim  ! 'yearly'/ ! weights filename ! rotation ! land/sea mask !
+   !           !                         !  (if <0  months)  !   name    !   (logical) !  (T/F) ! 'monthly' !                  ! pairing  !    filename   !
+   sn_tem      = 'init_ASICS'       ,         24.       ,'votemper',  .false.     , .true.,  'daily'  ,    ''            ,    ''    ,    ''
+   sn_sal      = 'init_ASICS'       ,         24.       ,'vosaline',  .false.     , .true.,  'daily'  ,    ''            ,    ''    ,    ''
+/
+!-----------------------------------------------------------------------
+&namwad        !   Wetting and Drying (WaD)                             (default: OFF)
+!-----------------------------------------------------------------------
+/
+!-----------------------------------------------------------------------
+&namcrs        !   coarsened grid (for outputs and/or TOP)              (ln_crs =T)
+!-----------------------------------------------------------------------
+/
+!-----------------------------------------------------------------------
+&namc1d        !   1D configuration options                             ("key_c1d" default: PAPA station)
+!-----------------------------------------------------------------------
+   rn_lat1d    =      30   !  Column latitude
+   rn_lon1d    =      15   !  Column longitude
+   ln_c1d_locpt =  .true.  ! Localization of 1D config in a grid (T) or independant point (F)
+/
+!-----------------------------------------------------------------------
+&namc1d_dyndmp !   U & V newtonian damping                              ("key_c1d" default: OFF)
+!-----------------------------------------------------------------------
+   ln_dyndmp   =  .false.  !  add a damping term (T) or not (F)
+/
+!-----------------------------------------------------------------------
+&namc1d_uvd    !   data: U & V currents                                 ("key_c1d" default: OFF)
+!-----------------------------------------------------------------------
+   !                       !  =T read U-V fields for:
+   ln_uvd_init   = .false.       !  ocean initialisation
+   ln_uvd_dyndmp = .false.       !  U-V restoring
+
+   cn_dir      = './'      !  root directory for the U-V data location
+   !___________!_________________________!___________________!___________!_____________!________!___________!__________________!__________!_______________!
+   !           !  file name              ! frequency (hours) ! variable  ! time interp.!  clim  ! 'yearly'/ ! weights filename ! rotation ! land/sea mask !
+   !           !                         !  (if <0  months)  !   name    !   (logical) !  (T/F) ! 'monthly' !                  ! pairing  !    filename   !
+   sn_ucur     = 'ucurrent'              ,         -1.       ,'u_current',   .false.   , .true. , 'monthly' ,  ''              ,  'Ume'   , ''
+   sn_vcur     = 'vcurrent'              ,         -1.       ,'v_current',   .false.   , .true. , 'monthly' ,  ''              ,  'Vme'   , ''
+/
+
+!!======================================================================
+!!            ***  Surface Boundary Condition namelists  ***          !!
+!!                                                                    !!
+!!   namsbc          surface boundary condition manager                 (default: NO selection)
+!!   namsbc_flx      flux               formulation                     (ln_flx     =T)
+!!   namsbc_blk      Bulk formulae formulation                          (ln_blk     =T)
+!!   namsbc_cpl      CouPLed            formulation                     ("key_oasis3" )
+!!   namsbc_sas      Stand-Alone Surface module                         (SAS_SRC  only)
+!!   namsbc_iif      Ice-IF: use observed ice cover                     (nn_ice = 1   )
+!!   namtra_qsr      penetrative solar radiation                        (ln_traqsr  =T)
+!!   namsbc_ssr      sea surface restoring term (for T and/or S)        (ln_ssr     =T)
+!!   namsbc_rnf      river runoffs                                      (ln_rnf     =T)
+!!   namsbc_apr      Atmospheric Pressure                               (ln_apr_dyn =T)
+!!   namsbc_isf      ice shelf melting/freezing                         (ln_isfcav  =T : read (ln_read_cfg=T) or set or usr_def_zgr )
+!!   namsbc_iscpl    coupling option between land ice model and ocean   (ln_isfcav  =T)
+!!   namsbc_wave     external fields from wave model                    (ln_wave    =T)
+!!   namberg         iceberg floats                                     (ln_icebergs=T)
+!!======================================================================
+!
+!-----------------------------------------------------------------------
+&namsbc        !   Surface Boundary Condition manager                   (default: NO selection)
+!-----------------------------------------------------------------------
+   nn_fsbc     = 1         !  frequency of SBC module call
+      !                    !  (control sea-ice & iceberg model call)
+                     ! Type of air-sea fluxes 
+   ln_flx      = .true.    !  flux formulation                          (T => fill namsbc_flx )
+      !              ! Type of coupling (Ocean/Ice/Atmosphere) :
+                     ! Misc. options of sbc : 
+   ln_traqsr   = .true.    !  Light penetration in the ocean            (T => fill namtra_qsr)
+/
+!-----------------------------------------------------------------------
+&namsbc_flx    !   surface boundary condition : flux formulation        (ln_flx =T)
+!-----------------------------------------------------------------------
+   sn_utau     = 'forc_ASICS' ,        1         , 'TAUX2'    , .false.      , .false., 'yearly'  , ''       , ''       , ''
+   sn_vtau     = 'forc_ASICS' ,        1         , 'TAUY2'    , .false.      , .false., 'yearly'  , ''       , ''       , ''
+   sn_qtot     = 'forc_ASICS' ,        1         , 'FNET2'    , .false.      , .false., 'yearly'  , ''       , ''       , ''
+   sn_qsr      = 'forc_ASICS' ,        1         , 'FSOL2'    , .false.      , .false., 'yearly'  , ''       , ''       , ''
+   sn_emp      = 'forc_ASICS' ,        1         , 'EMP2'     , .false.      , .false., 'yearly'  , ''       , ''       , ''
+   !
+   cn_dir      = './'      !  root directory for the location of the flux files
+/
+!-----------------------------------------------------------------------
+&namsbc_blk    !   namsbc_blk  generic Bulk formula                     (ln_blk =T)
+!-----------------------------------------------------------------------
+   !                    !  bulk algorithm :
+/
+!-----------------------------------------------------------------------
+&namsbc_cpl    !   coupled ocean/atmosphere model                       ("key_oasis3")
+!-----------------------------------------------------------------------
+/
+!-----------------------------------------------------------------------
+&namsbc_sas    !   Stand-Alone Surface module: ocean data               (SAS_SRC  only)
+!-----------------------------------------------------------------------
+/
+!-----------------------------------------------------------------------
+&namsbc_iif    !   Ice-IF : use observed ice cover                      (nn_ice = 1)
+!-----------------------------------------------------------------------
+/
+!-----------------------------------------------------------------------
+&namtra_qsr    !   penetrative solar radiation                          (ln_traqsr =T)
+!-----------------------------------------------------------------------
+   !                       !  type of penetration                        (default: NO selection)
+   ln_qsr_rgb  = .true.      !  RGB light penetration (Red-Green-Blue)
+   
+   cn_dir      = './'      !  root directory for the chlorophyl data location
+   !___________!_________________________!___________________!___________!_____________!________!___________!__________________!__________!_______________!
+   !           !  file name              ! frequency (hours) ! variable  ! time interp.!  clim  ! 'yearly'/ ! weights filename ! rotation ! land/sea mask !
+   !           !                         !  (if <0  months)  !   name    !   (logical) !  (T/F) ! 'monthly' !                  ! pairing  !    filename   !
+
+   sn_chl      ='chlorophyll',         -1.       , 'CHLA'    ,   .true.     , .true. , 'yearly'  ,        ''       ,    ''    , ''
+/
+!-----------------------------------------------------------------------
+&namsbc_ssr    !   surface boundary condition : sea surface restoring   (ln_ssr =T)
+!-----------------------------------------------------------------------
+/
+!-----------------------------------------------------------------------
+&namsbc_rnf    !   runoffs                                              (ln_rnf =T)
+!-----------------------------------------------------------------------
+/
+!-----------------------------------------------------------------------
+&namsbc_apr    !   Atmospheric pressure used as ocean forcing           (ln_apr_dyn =T)
+!-----------------------------------------------------------------------
+/
+!-----------------------------------------------------------------------
+&namsbc_isf    !  Top boundary layer (ISF)                              (ln_isfcav =T : read (ln_read_cfg=T) 
+!-----------------------------------------------------------------------             or set or usr_def_zgr )
+/
+!-----------------------------------------------------------------------
+&namsbc_iscpl  !   land ice / ocean coupling option                     (ln_isfcav =T : read (ln_read_cfg=T) 
+!-----------------------------------------------------------------------             or set or usr_def_zgr )
+/
+!-----------------------------------------------------------------------
+&namsbc_wave   ! External fields from wave model                        (ln_wave=T)
+!-----------------------------------------------------------------------
+/
+!-----------------------------------------------------------------------
+&namberg       !   iceberg parameters                                   (default: OFF)
+!-----------------------------------------------------------------------
+/
+
+!!======================================================================
+!!               ***  Lateral boundary condition  ***                 !!
+!!                                                                    !!
+!!   namlbc        lateral momentum boundary condition                  (default: NO selection)
+!!   namagrif      agrif nested grid   (read by child model only)       ("key_agrif")
+!!   nam_tide      Tidal forcing                                        (default: OFF)
+!!   nambdy        Unstructured open boundaries                         (default: OFF)
+!!   nambdy_dta    Unstructured open boundaries - external data         (see  nambdy)
+!!   nambdy_tide   tidal forcing at open boundaries                     (default: OFF)
+!!======================================================================
+!
+!-----------------------------------------------------------------------
+&namlbc        !   lateral momentum boundary condition                  (default: NO selection)
+!-----------------------------------------------------------------------
+   rn_shlat    =  0.   !  shlat = 0  !  0 < shlat < 2  !  shlat = 2  !  2 < shlat
+/
+!-----------------------------------------------------------------------
+&namagrif      !  AGRIF zoom                                            ("key_agrif")
+!-----------------------------------------------------------------------
+/
+!-----------------------------------------------------------------------
+&nam_tide      !   tide parameters                                      (default: OFF)
+!-----------------------------------------------------------------------
+/
+!-----------------------------------------------------------------------
+&nambdy        !  unstructured open boundaries                          (default: OFF)
+!-----------------------------------------------------------------------
+/
+!-----------------------------------------------------------------------
+&nambdy_dta    !  open boundaries - external data                       (see nam_bdy)
+!-----------------------------------------------------------------------
+/
+!-----------------------------------------------------------------------
+&nambdy_tide   !  tidal forcing at open boundaries                      (default: OFF)
+!-----------------------------------------------------------------------
+/
+
+!!======================================================================
+!!                ***  Top/Bottom boundary condition  ***             !!
+!!                                                                    !!
+!!   namdrg        top/bottom drag coefficient                          (default: NO selection)
+!!   namdrg_top    top    friction                                      (ln_drg_OFF=F & ln_isfcav=T)
+!!   namdrg_bot    bottom friction                                      (ln_drg_OFF=F)
+!!   nambbc        bottom temperature boundary condition                (default: OFF)
+!!   nambbl        bottom boundary layer scheme                         (default: OFF)
+!!======================================================================
+!
+!-----------------------------------------------------------------------
+&namdrg        !   top/bottom drag coefficient                          (default: NO selection)
+!-----------------------------------------------------------------------
+   ln_non_lin  = .true.   !  non-linear  drag: Cd = Cd0 |U|
+   ln_drgimp   = .false.  !  implicit top/bottom friction flag
+/
+!-----------------------------------------------------------------------
+&namdrg_top    !   TOP friction                                         (ln_drg_OFF =F & ln_isfcav=T)
+!-----------------------------------------------------------------------
+/
+!-----------------------------------------------------------------------
+&namdrg_bot    !   BOTTOM friction                                      (ln_drg_OFF =F)
+!-----------------------------------------------------------------------
+/
+!-----------------------------------------------------------------------
+&nambbc        !   bottom temperature boundary condition                (default: OFF)
+!-----------------------------------------------------------------------
+/
+!-----------------------------------------------------------------------
+&nambbl        !   bottom boundary layer scheme                         (default: OFF)
+!-----------------------------------------------------------------------
+/
+
+!!======================================================================
+!!                        Tracer (T-S) namelists                      !!
+!!                                                                    !!
+!!   nameos        equation of state                                    (default: NO selection)
+!!   namtra_adv    advection scheme                                     (default: NO selection)
+!!   namtra_ldf    lateral diffusion scheme                             (default: NO selection)
+!!   namtra_mle    mixed layer eddy param. (Fox-Kemper param.)          (default: OFF)
+!!   namtra_eiv    eddy induced velocity param.                         (default: OFF)
+!!   namtra_dmp    T & S newtonian damping                              (default: OFF)
+!!======================================================================
+!
+!-----------------------------------------------------------------------
+&nameos        !   ocean Equation Of Seawater                           (default: NO selection)
+!-----------------------------------------------------------------------
+   ln_eos80    = .true.         !  = Use EOS80
+/
+!-----------------------------------------------------------------------
+&namtra_adv    !   advection scheme for tracer                          (default: NO selection)
+!-----------------------------------------------------------------------
+   ln_traadv_OFF = .true. !  No tracer advection
+/
+!-----------------------------------------------------------------------
+&namtra_ldf    !   lateral diffusion scheme for tracers                 (default: NO selection)
+!-----------------------------------------------------------------------
+   !                       !  Operator type:
+   ln_traldf_OFF   = .true.   !  No explicit diffusion
+/
+!-----------------------------------------------------------------------
+&namtra_mle    !   mixed layer eddy parametrisation (Fox-Kemper)       (default: OFF)
+!-----------------------------------------------------------------------
+/
+!-----------------------------------------------------------------------
+&namtra_eiv    !   eddy induced velocity param.                         (default: OFF)
+!-----------------------------------------------------------------------
+/
+!-----------------------------------------------------------------------
+&namtra_dmp    !   tracer: T & S newtonian damping                      (default: OFF)
+!-----------------------------------------------------------------------
+/
+
+!!======================================================================
+!!                      ***  Dynamics namelists  ***                  !!
+!!                                                                    !!
+!!   nam_vvl       vertical coordinate options                          (default: z-star)
+!!   namdyn_adv    formulation of the momentum advection                (default: NO selection)
+!!   namdyn_vor    advection scheme                                     (default: NO selection)
+!!   namdyn_hpg    hydrostatic pressure gradient                        (default: NO selection)
+!!   namdyn_spg    surface pressure gradient                            (default: NO selection)
+!!   namdyn_ldf    lateral diffusion scheme                             (default: NO selection)
+!!   namdta_dyn    offline TOP: dynamics read in files                  (OFF_SRC only)
+!!======================================================================
+!
+!-----------------------------------------------------------------------
+&nam_vvl       !   vertical coordinate options                          (default: z-star)
+!-----------------------------------------------------------------------
+/
+!-----------------------------------------------------------------------
+&namdyn_adv    !   formulation of the momentum advection                (default: NO selection)
+!-----------------------------------------------------------------------
+   ln_dynadv_OFF = .true. !  linear dynamics (no momentum advection)
+/
+!-----------------------------------------------------------------------
+&namdyn_vor    !   Vorticity / Coriolis scheme                          (default: NO selection)
+!-----------------------------------------------------------------------
+   ln_dynvor_ene = .true. !  energy    conserving scheme
+/
+!-----------------------------------------------------------------------
+&namdyn_hpg    !   Hydrostatic pressure gradient option                 (default: NO selection)
+!-----------------------------------------------------------------------
+   ln_hpg_sco  = .true.   !  s-coordinate (standard jacobian formulation)
+/
+!-----------------------------------------------------------------------
+&namdyn_spg    !   surface pressure gradient                            (default: NO selection)
+!-----------------------------------------------------------------------
+    ln_dynspg_ts   = .false.   ! split-explicit free surface
+       ln_bt_fw      = .false.     ! Forward integration of barotropic Eqs.
+       ln_bt_av      = .true.     ! Time filtering of barotropic variables
+/
+!-----------------------------------------------------------------------
+&namdyn_ldf    !   lateral diffusion on momentum                        (default: NO selection)
+!-----------------------------------------------------------------------
+   !                       !  Type of the operator :
+   ln_dynldf_OFF = .true.     !  No operator (i.e. no explicit diffusion)
+/
+!-----------------------------------------------------------------------
+&namdta_dyn    !   offline ocean input files                            (OFF_SRC only)
+!-----------------------------------------------------------------------
+/
+
+!!======================================================================
+!!                     vertical physics namelists                     !!
+!!                                                                    !!
+!!    namzdf        vertical physics manager                            (default: NO selection)
+!!    namzdf_ric    richardson number vertical mixing                   (ln_zdfric=T)
+!!    namzdf_tke    TKE vertical mixing                                 (ln_zdftke=T)
+!!    namzdf_gls    GLS vertical mixing                                 (ln_zdfgls=T)
+!!    namzdf_osm    OSM vertical diffusion                              (ln_zdfosm=T)
+!!    namzdf_iwm    tidal mixing parameterization                       (ln_zdfiwm=T)
+!!======================================================================
+!
+!-----------------------------------------------------------------------
+&namzdf        !   vertical physics manager                             (default: NO selection)
+!-----------------------------------------------------------------------
+   !                       ! type of vertical closure (required)
+   ln_zdftke   = .true.       !  Turbulent Kinetic Energy closure       (T =>   fill namzdf_tke)
+   ln_zdfmfc   = .true.       !  Mass Flux Convection
+   ln_zdfiwm   = .true.       ! internal wave-induced mixing            (T =>   fill namzdf_iwm)
+/
+!-----------------------------------------------------------------------
+&namzdf_ric    !   richardson number dependent vertical diffusion       (ln_zdfric =T)
+!-----------------------------------------------------------------------
+/
+!-----------------------------------------------------------------------
+&namzdf_tke    !   turbulent eddy kinetic dependent vertical diffusion  (ln_zdftke =T)
+!-----------------------------------------------------------------------
+/
+!-----------------------------------------------------------------------
+&namzdf_gls    !   GLS vertical diffusion                               (ln_zdfgls =T)
+!-----------------------------------------------------------------------
+/
+!-----------------------------------------------------------------------
+&namzdf_osm    !   OSM vertical diffusion                               (ln_zdfosm =T)
+!-----------------------------------------------------------------------
+/
+!-----------------------------------------------------------------------
+&namzdf_mfc     !   Mass Flux Convection
+!-----------------------------------------------------------------------
+   ln_edmfuv  = .false.        ! Activate on velocity fields (Not available yet)
+   rn_cemf    =  1.            ! entrain/detrain coef. (<0 => cte; >0 % depending on dW/dz
+   rn_cwmf    = -0.            ! entrain/detrain coef. (<0 => cte; >0 % depending on dW/dz
+   rn_cent    = 2.e-5          ! entrain of convective area
+   rn_cdet    = 3.e-5          ! detrain of convective area
+   rn_cap     = 0.9            ! Coef. for CAP estimation
+   App_max    = 0.1            ! Maximum convection area (% of the cell)
+/
+!-----------------------------------------------------------------------
+&namzdf_iwm    !    internal wave-driven mixing parameterization        (ln_zdfiwm =T)
+!-----------------------------------------------------------------------
+   nn_zpyc     = 1         !  pycnocline-intensified dissipation scales as N (=1) or N^2 (=2)
+   ln_mevar    = .true.    !  variable (T) or constant (F) mixing efficiency
+   ln_tsdiff   = .true.    !  account for differential T/S mixing (T) or not (F)
+
+   cn_dir      = './'      !  root directory for the iwm data location                                                                  
+   !___________!_________________________!___________________!___________!_____________!________!___________!__________________!__________!_______________!
+   !           !  file name              ! frequency (hours) ! variable  ! time interp.!  clim  ! 'yearly'/ ! weights filename ! rotation ! land/sea mask !
+   !           !                         !  (if <0  months)  !   name    !   (logical) !  (T/F) ! 'monthly' !                  ! pairing  !    filename   !
+   sn_mpb      = 'forcing_iwm_ASICSstation' , -12.           , 'mixing_power_bot' , .false.  , .true. , 'yearly' , '' , ''  , ''
+   sn_mpp      = 'forcing_iwm_ASICSstation' , -12.           , 'mixing_power_pyc' , .false.  , .true. , 'yearly' , '' , ''  , ''
+   sn_mpc      = 'forcing_iwm_ASICSstation' , -12.           , 'mixing_power_cri' , .false.  , .true. , 'yearly' , '' , ''  , ''
+   sn_dsb      = 'forcing_iwm_ASICSstation' , -12.           , 'decay_scale_bot'  , .false.  , .true. , 'yearly' , '' , ''  , ''
+   sn_dsc      = 'forcing_iwm_ASICSstation' , -12.           , 'decay_scale_cri'  , .false.  , .true. , 'yearly' , '' , ''  , ''
+/
+!!======================================================================
+!!                  ***  Diagnostics namelists  ***                   !!
+!!                                                                    !!
+!!   namtrd       dynamics and/or tracer trends                         (default: OFF)
+!!   namhsb       Heat and salt budgets                                 (default: OFF)
+!!   namdiu       Cool skin and warm layer models                       (default: OFF)
+!!   namdiu       Cool skin and warm layer models                       (default: OFF)
+!!   namflo       float parameters                                      (default: OFF)
+!!   nam_diadct   transports through some sections                      (default: OFF)
+!!   nam_dia25h   25h Mean Output                                       (default: OFF)
+!!   namnc4       netcdf4 chunking and compression settings             ("key_netcdf4")
+!!======================================================================
+!
+!-----------------------------------------------------------------------
+&namtrd        !   trend diagnostics                                    (default: OFF)
+!-----------------------------------------------------------------------
+/
+!-----------------------------------------------------------------------
+&namhsb        !  Heat and salt budgets                                 (default: OFF)
+!-----------------------------------------------------------------------
+/
+!-----------------------------------------------------------------------
+&namdiu        !   Cool skin and warm layer models                      (default: OFF)
+!-----------------------------------------------------------------------
+/
+!-----------------------------------------------------------------------
+&namflo        !   float parameters                                     (default: OFF)
+!-----------------------------------------------------------------------
+/
+!-----------------------------------------------------------------------
+&nam_diadct    ! transports through some sections                       (default: OFF)
+!-----------------------------------------------------------------------
+/
+!-----------------------------------------------------------------------
+&nam_dia25h    !  25h Mean Output                                       (default: OFF)
+!-----------------------------------------------------------------------
+/
+!-----------------------------------------------------------------------
+&namnc4        !   netcdf4 chunking and compression settings            ("key_netcdf4")
+!-----------------------------------------------------------------------
+/
+
+!!======================================================================
+!!               ***  Observation & Assimilation  ***                 !!
+!!                                                                    !!
+!!   namobs       observation and model comparison                      (default: OFF)
+!!   nam_asminc   assimilation increments                               ('key_asminc')
+!!======================================================================
+!
+!-----------------------------------------------------------------------
+&namobs        !  observation usage switch                              (default: OFF)
+!-----------------------------------------------------------------------
+/
+!-----------------------------------------------------------------------
+&nam_asminc    !   assimilation increments                              ('key_asminc')
+!-----------------------------------------------------------------------
+/
+
+!!======================================================================
+!!                  ***  Miscellaneous namelists  ***                 !!
+!!                                                                    !!
+!!   nammpp            Massively Parallel Processing                    ("key_mpp_mpi")
+!!   namctl            Control prints                                   (default: OFF)
+!!   namsto            Stochastic parametrization of EOS                (default: OFF)
+!!======================================================================
+!
+!-----------------------------------------------------------------------
+&nammpp        !   Massively Parallel Processing                        ("key_mpp_mpi")
+!-----------------------------------------------------------------------
+/
+!-----------------------------------------------------------------------
+&namctl        !   Control prints                                       (default: OFF)
+!-----------------------------------------------------------------------
+/
+!-----------------------------------------------------------------------
+&namsto        ! Stochastic parametrization of EOS                      (default: OFF)
+!-----------------------------------------------------------------------
+/
Index: /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/EXP00/namelist_ref
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/EXP00/namelist_ref	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/EXP00/namelist_ref	(revision 14016)
@@ -0,0 +1,1 @@
+link ../../../cfgs/SHARED/namelist_ref
Index: /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/MY_SRC/usrdef_hgr.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/MY_SRC/usrdef_hgr.F90	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/MY_SRC/usrdef_hgr.F90	(revision 14016)
@@ -0,0 +1,108 @@
+MODULE usrdef_hgr
+   !!======================================================================
+   !!                     ***  MODULE usrdef_hgr   ***
+   !!
+   !!                     ===  C1D_ASICS configuration  ===
+   !!
+   !! User defined :   mesh and Coriolis parameter of a user configuration
+   !!======================================================================
+   !! History :  4.0 ! 2016-03  (S. Flavoni) 
+   !!----------------------------------------------------------------------
+
+   !!----------------------------------------------------------------------
+   !!   usr_def_hgr   : initialize the horizontal mesh 
+   !!----------------------------------------------------------------------
+   USE dom_oce  , ONLY: nimpp, njmpp        ! ocean space and time domain
+   USE c1d      ,  ONLY: rn_lon1d, rn_lat1d ! ocean lon/lat define by namelist
+   USE par_oce        ! ocean space and time domain
+   USE phycst         ! physical constants
+   USE usrdef_nam     !
+   !
+   USE in_out_manager ! I/O manager
+   USE lib_mpp        ! MPP library
+   
+   IMPLICIT NONE
+   PRIVATE
+
+   PUBLIC   usr_def_hgr   ! called in domhgr.F90
+
+   !!----------------------------------------------------------------------
+   !! NEMO/OCE 4.0 , NEMO Consortium (2018)
+   !! $Id: usrdef_hgr.F90 10072 2018-08-28 15:21:50Z nicolasmartin $
+   !! Software governed by the CeCILL license (see ./LICENSE)
+   !!----------------------------------------------------------------------
+CONTAINS
+
+   SUBROUTINE usr_def_hgr( plamt , plamu , plamv  , plamf  ,   &   ! geographic position (required)
+      &                    pphit , pphiu , pphiv  , pphif  ,   &   !
+      &                    kff   , pff_f , pff_t  ,            &   ! Coriolis parameter  (if domain not on the sphere)
+      &                    pe1t  , pe1u  , pe1v   , pe1f   ,   &   ! scale factors       (required)
+      &                    pe2t  , pe2u  , pe2v   , pe2f   ,   &   !
+      &                    ke1e2u_v      , pe1e2u , pe1e2v     )   ! u- & v-surfaces (if gridsize reduction is used in strait(s))
+      !!----------------------------------------------------------------------
+      !!                  ***  ROUTINE usr_def_hgr  ***
+      !!
+      !! ** Purpose :   user defined mesh and Coriolis parameter
+      !!
+      !! ** Method  :   set all intent(out) argument to a proper value
+      !!
+      !!                Here C1D_ASICS configuration :
+      !!          Rectangular 3x3 domain 
+      !!          - Located at 10E-42N
+      !!          - a constant horizontal resolution  
+      !!
+      !! ** Action  : - define longitude & latitude of t-, u-, v- and f-points (in degrees) 
+      !!              - define coriolis parameter at f-point if the domain in not on the sphere
+      !!              - define i- & j-scale factors at t-, u-, v- and f-points (in meters)
+      !!              - define u- & v-surfaces (if gridsize reduction is used in some straits) (in m2)
+      !!----------------------------------------------------------------------
+      REAL(wp), DIMENSION(:,:), INTENT(out) ::   plamt, plamu, plamv, plamf   ! longitude outputs                     [degrees]
+      REAL(wp), DIMENSION(:,:), INTENT(out) ::   pphit, pphiu, pphiv, pphif   ! latitude outputs                      [degrees]
+      INTEGER                 , INTENT(out) ::   kff                          ! =1 Coriolis parameter computed here, =0 otherwise
+      REAL(wp), DIMENSION(:,:), INTENT(out) ::   pff_f, pff_t                 ! Coriolis factor at f-point                [1/s]
+      REAL(wp), DIMENSION(:,:), INTENT(out) ::   pe1t, pe1u, pe1v, pe1f       ! i-scale factors                             [m]
+      REAL(wp), DIMENSION(:,:), INTENT(out) ::   pe2t, pe2u, pe2v, pe2f       ! j-scale factors                             [m]
+      INTEGER                 , INTENT(out) ::   ke1e2u_v                     ! =1 u- & v-surfaces computed here, =0 otherwise 
+      REAL(wp), DIMENSION(:,:), INTENT(out) ::   pe1e2u, pe1e2v               ! u- & v-surfaces (if reduction in strait)   [m2]
+      !
+      INTEGER  ::   ji, jj               ! dummy loop indices
+      REAL(wp) ::   zlam1, zlam0, zcos_alpha, zim1 , zjm1 , ze1  , ze1deg, zf0   ! local scalars
+      REAL(wp) ::   zphi1, zphi0, zsin_alpha, zim05, zjm05, zbeta, znorme, zfact !   -      -
+      !!-------------------------------------------------------------------------------
+      !
+      IF(lwp) WRITE(numout,*)
+      IF(lwp) WRITE(numout,*) 'usr_def_hgr : C1D configuration'
+      IF(lwp) WRITE(numout,*) '~~~~~~~~~~~   uniform grid spacing WITH Coriolis force'
+      !
+      !                       !==  grid point position  ==!   (Special case of 1D config: all lon/lat are the same)
+      !                       ! longitude
+      plamt(:,:) = rn_lon1d
+      plamu(:,:) = rn_lon1d 
+      plamv(:,:) = rn_lon1d
+      plamf(:,:) = rn_lon1d
+      !                       ! latitude
+      pphit(:,:) = rn_lat1d
+      pphiu(:,:) = rn_lat1d
+      pphiv(:,:) = rn_lat1d
+      pphif(:,:) = rn_lat1d
+      !                       !== Horizontal scale factors ==! (in meters)
+      !                     
+      !                                         ! constant grid spacing
+      pe1t(:,:) = 100.  ;   pe2t(:,:) = 100.
+      pe1u(:,:) = 100.  ;   pe2u(:,:) = 100.
+      pe1v(:,:) = 100.  ;   pe2v(:,:) = 100.
+      pe1f(:,:) = 100.  ;   pe2f(:,:) = 100.
+      !
+      !                                         ! NO reduction of grid size in some straits 
+      ke1e2u_v = 0                              !    ==>> u_ & v_surfaces will be computed in dom_ghr routine
+      pe1e2u(:,:) = 0._wp                       !    CAUTION: set to zero to avoid error with some compilers that
+      pe1e2v(:,:) = 0._wp                       !             require an initialization of INTENT(out) arguments
+      !
+      !
+      !                       !==  Coriolis parameter  ==!
+      kff = 0                 !  indicate to compute Coriolis parameter afterward
+      !
+   END SUBROUTINE usr_def_hgr
+
+   !!======================================================================
+END MODULE usrdef_hgr
Index: /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/MY_SRC/usrdef_nam.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/MY_SRC/usrdef_nam.F90	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/MY_SRC/usrdef_nam.F90	(revision 14016)
@@ -0,0 +1,99 @@
+MODULE usrdef_nam
+   !!======================================================================
+   !!                     ***  MODULE usrdef_nam   ***
+   !!
+   !!                     ===  C1D_ASICS configuration  ===
+   !!
+   !! User defined : set the domain characteristics of a user configuration
+   !!======================================================================
+   !! History :  4.0  ! 2016-03  (S. Flavoni, G. Madec)  Original code
+   !!----------------------------------------------------------------------
+
+   !!----------------------------------------------------------------------
+   !!   usr_def_nam   : read user defined namelist and set global domain size
+   !!   usr_def_hgr   : initialize the horizontal mesh 
+   !!----------------------------------------------------------------------
+   USE dom_oce  , ONLY: nimpp, njmpp             ! ocean space and time domain
+   USE dom_oce  , ONLY: ln_zco, ln_zps, ln_sco   ! flag of type of coordinate
+   USE par_oce        ! ocean space and time domain
+   USE phycst         ! physical constants
+   !
+   USE in_out_manager ! I/O manager
+   USE lib_mpp        ! MPP library
+   
+   IMPLICIT NONE
+   PRIVATE
+
+   PUBLIC   usr_def_nam   ! called in nemogcm.F90 module
+
+   !                              !!* namusr_def namelist *!!
+   LOGICAL, PUBLIC ::   ln_bench   ! =T benchmark test with gyre: the gridsize is constant (no need to adjust timestep or viscosity)
+   INTEGER, PUBLIC ::   nn_GYRE    ! 1/nn_GYRE = the resolution chosen in degrees and thus defining the horizontal domain size
+   REAL(wp), PUBLIC::   rn_bathy   ! Depth in meters for 1D configuration
+
+   !!----------------------------------------------------------------------
+   !! NEMO/OCE 4.0 , NEMO Consortium (2018)
+   !! $Id: usrdef_nam.F90 12377 2020-02-12 14:39:06Z acc $
+   !! Software governed by the CeCILL license (see ./LICENSE)
+   !!----------------------------------------------------------------------
+CONTAINS
+
+   SUBROUTINE usr_def_nam( cd_cfg, kk_cfg, kpi, kpj, kpk, kperio )
+      !!----------------------------------------------------------------------
+      !!                     ***  ROUTINE dom_nam  ***
+      !!                    
+      !! ** Purpose :   read user defined namelist and define the domain size
+      !!
+      !! ** Method  :   read in namusr_def containing all the user specific namelist parameter
+      !!
+      !!                Here C1D configuration
+      !!
+      !! ** input   : - namusr_def namelist found in namelist_cfg
+      !!----------------------------------------------------------------------
+      CHARACTER(len=*)              , INTENT(out) ::   cd_cfg          ! configuration name
+      INTEGER                       , INTENT(out) ::   kk_cfg          ! configuration resolution
+      INTEGER                       , INTENT(out) ::   kpi, kpj, kpk   ! global domain sizes 
+      INTEGER                       , INTENT(out) ::   kperio          ! lateral global domain b.c. 
+      !
+      INTEGER ::   ios   ! Local integer
+      !!
+      NAMELIST/namusr_def/ rn_bathy
+      !!----------------------------------------------------------------------
+      !
+      READ  ( numnam_cfg, namusr_def, IOSTAT = ios, ERR = 902 )
+902   IF( ios /= 0 )   CALL ctl_nam ( ios , 'namusr_def in configuration namelist' )
+      !
+      IF(lwm)   WRITE( numond, namusr_def )
+      !
+      cd_cfg = 'C1D'               ! name & resolution (not used)
+      kk_cfg = 0
+
+      ! Global Domain size:  C1D domain is 3 x 3 grid-points x 75 or vertical levels
+      kpi = 3
+      kpj = 3
+      kpk = 75 
+      !                             ! Set the lateral boundary condition of the global domain
+      kperio =  7                   ! C1D configuration : 3x3 basin with cyclic Est-West and Norht-South condition
+      !
+      !                             ! control print
+      IF(lwp) THEN
+         WRITE(numout,*) '   '
+         WRITE(numout,*) 'usr_def_nam  : read the user defined namelist (namusr_def) in namelist_cfg'
+         WRITE(numout,*) '~~~~~~~~~~~ '
+         WRITE(numout,*) '   Namelist namusr_def : C1 case'
+         WRITE(numout,*) '      type of vertical coordinate : '
+         WRITE(numout,*) '         z-coordinate flag                     ln_zco = ', ln_zco
+         WRITE(numout,*) '         z-partial-step coordinate flag        ln_zps = ', ln_zps
+         WRITE(numout,*) '         s-coordinate flag                     ln_sco = ', ln_sco
+         WRITE(numout,*) '      C1D domain = 3 x 3 x75 grid-points                '
+         WRITE(numout,*) '         resulting global domain size :        jpiglo = ', kpi
+         WRITE(numout,*) '                                               jpjglo = ', kpj
+         WRITE(numout,*) '                                               jpkglo = ', kpk
+         WRITE(numout,*) '   Lateral boundary condition of the global domain'
+         WRITE(numout,*) '      C1D : closed basin                       jperio = ', kperio
+      ENDIF
+      !
+   END SUBROUTINE usr_def_nam
+
+   !!======================================================================
+END MODULE usrdef_nam
Index: /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/MY_SRC/usrdef_zgr.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/MY_SRC/usrdef_zgr.F90	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/MY_SRC/usrdef_zgr.F90	(revision 14016)
@@ -0,0 +1,184 @@
+MODULE usrdef_zgr
+   !!======================================================================
+   !!                       ***  MODULE  usrdef_zgr  ***
+   !!
+   !!                       ===  C1D_ASICS configuration  ===
+   !!
+   !! User defined : vertical coordinate system of a user configuration
+   !!======================================================================
+   !! History :  4.0  ! 2016-06  (R. Bourdalle-Badie)  Original code
+   !!----------------------------------------------------------------------
+
+   !!----------------------------------------------------------------------
+   !!   usr_def_zgr   : user defined vertical coordinate system
+   !!      zgr_z      : reference 1D z-coordinate 
+   !!      zgr_top_bot: ocean top and bottom level indices
+   !!      zgr_zco    : 3D verticl coordinate in pure z-coordinate case
+   !!---------------------------------------------------------------------
+   USE oce            ! ocean variables
+   USE dom_oce        ! ocean domain
+   USE depth_e3       ! depth <=> e3
+   USE usrdef_nam     ! User defined : namelist variables
+   !
+   USE in_out_manager ! I/O manager
+   USE lbclnk         ! ocean lateral boundary conditions (or mpp link)
+   USE lib_mpp        ! distributed memory computing library
+
+   IMPLICIT NONE
+   PRIVATE
+
+   PUBLIC   usr_def_zgr        ! called by domzgr.F90
+
+   !! * Substitutions
+#  include "do_loop_substitute.h90"
+   !!----------------------------------------------------------------------
+   !! NEMO/OCE 4.0 , NEMO Consortium (2018)
+   !! $Id: usrdef_zgr.F90 13295 2020-07-10 18:24:21Z acc $
+   !! Software governed by the CeCILL license (see ./LICENSE)
+   !!----------------------------------------------------------------------
+CONTAINS             
+
+   SUBROUTINE usr_def_zgr( ld_zco  , ld_zps  , ld_sco  , ld_isfcav,    &   ! type of vertical coordinate
+      &                    pdept_1d, pdepw_1d, pe3t_1d , pe3w_1d  ,    &   ! 1D reference vertical coordinate
+      &                    pdept , pdepw ,                             &   ! 3D t & w-points depth
+      &                    pe3t  , pe3u  , pe3v   , pe3f ,             &   ! vertical scale factors
+      &                    pe3w  , pe3uw , pe3vw         ,             &   !     -      -      -
+      &                    k_top  , k_bot    )                             ! top & bottom ocean level
+      !!---------------------------------------------------------------------
+      !!              ***  ROUTINE usr_def_zgr  ***
+      !!
+      !! ** Purpose :   User defined the vertical coordinates
+      !!
+      !!----------------------------------------------------------------------
+      LOGICAL                   , INTENT(out) ::   ld_zco, ld_zps, ld_sco      ! vertical coordinate flags
+      LOGICAL                   , INTENT(out) ::   ld_isfcav                   ! under iceshelf cavity flag
+      REAL(wp), DIMENSION(:)    , INTENT(out) ::   pdept_1d, pdepw_1d          ! 1D grid-point depth     [m]
+      REAL(wp), DIMENSION(:)    , INTENT(out) ::   pe3t_1d , pe3w_1d           ! 1D grid-point depth     [m]
+      REAL(wp), DIMENSION(:,:,:), INTENT(out) ::   pdept, pdepw                ! grid-point depth        [m]
+      REAL(wp), DIMENSION(:,:,:), INTENT(out) ::   pe3t , pe3u , pe3v , pe3f   ! vertical scale factors  [m]
+      REAL(wp), DIMENSION(:,:,:), INTENT(out) ::   pe3w , pe3uw, pe3vw         ! i-scale factors 
+      INTEGER , DIMENSION(:,:)  , INTENT(out) ::   k_top, k_bot                ! first & last ocean level
+      !
+      INTEGER  ::   ji, jj, jk        ! dummy indices
+      INTEGER  ::   ik                ! local integers
+      REAL(wp) ::   zfact, z1_jpkm1   ! local scalar
+      REAL(wp) ::   ze3min            ! local scalar
+      REAL(wp) ::   zt, zw            ! local scalars
+      REAL(wp) ::   zsur, za0, za1, zkth, zacr        ! Values for the Madec & Imbard (1996) function
+      REAL(wp) ::   za2, zkth2, zacr2                 ! Values for optional double tanh function set from parameters
+      REAL(wp), DIMENSION(jpi,jpj) ::   zht, zhu, z2d ! 2D workspace
+
+      !!----------------------------------------------------------------------
+      !
+      IF(lwp) WRITE(numout,*)
+      IF(lwp) WRITE(numout,*) 'usr_def_zgr : C1D configuration (zps-coordinate closed box ocean without cavities)'
+      IF(lwp) WRITE(numout,*) '~~~~~~~~~~~'
+      !
+      ! type of vertical coordinate
+      ! ---------------------------
+      ld_zco    = .FALSE.         ! C1D case:  z-coordinate without ocean cavities
+      ld_zps    = .TRUE.
+      ld_sco    = .FALSE.
+      ld_isfcav = .FALSE.
+      !
+      ! Build the vertical coordinate system
+      ! ------------------------------------
+      !
+      ! Set parameters of z(k) function
+      ! -------------------------------
+      zsur =   -3958.95137127683
+      za0  =    103.953009600000
+      za1  =    2.41595126900000
+      zkth =    15.3510137000000
+      zacr =    7.00000000000000
+      za2  =    100.760928500000
+      zkth2=    48.0298937200000
+      zacr2=    13.0000000000000
+      !
+      IF(lwp) THEN            ! Parameter print
+         WRITE(numout,*)
+         WRITE(numout,*) '     zgr_z75L   : Reference vertical z-coordinates '
+         WRITE(numout,*) '     ~~~~~~~'
+         WRITE(numout,*) '       C1D case : L75 function with the following coefficients :'
+         WRITE(numout,*) '                 zsur = ', zsur
+         WRITE(numout,*) '                 za0  = ', za0
+         WRITE(numout,*) '                 za1  = ', za1
+         WRITE(numout,*) '                 zkth = ', zkth
+         WRITE(numout,*) '                 zacr = ', zacr
+         WRITE(numout,*) '                 za2  = ', za2
+         WRITE(numout,*) '                 zkth2= ', zkth2
+         WRITE(numout,*) '                 zacr2= ', zacr2
+      ENDIF
+
+      !                       !==  UNmasked meter bathymetry  ==!
+      !
+      zht(:,:) = rn_bathy
+      !
+      DO jk = 1, jpk          ! depth at T and W-points
+         zw = REAL( jk , wp )
+         zt = REAL( jk , wp ) + 0.5_wp
+         pdepw_1d(jk) = ( zsur + za0 * zw + za1 * zacr * LOG ( COSH( (zw-zkth ) / zacr  ) )    &
+                  &                    + za2 * zacr2* LOG ( COSH( (zw-zkth2) / zacr2 ) )  )
+         pdept_1d(jk) = ( zsur + za0 * zt + za1 * zacr * LOG ( COSH( (zt-zkth ) / zacr  ) )    &
+                  &                    + za2 * zacr2* LOG ( COSH( (zt-zkth2) / zacr2 ) )  )
+      END DO
+      !
+      !                       ! e3t and e3w from depth
+      CALL depth_to_e3( pdept_1d, pdepw_1d, pe3t_1d, pe3w_1d )
+      !
+      !                       ! recompute depths from SUM(e3)  <== needed
+      CALL e3_to_depth( pe3t_1d, pe3w_1d, pdept_1d, pdepw_1d )
+      !
+      IF(lwp) THEN                        ! control print
+         WRITE(numout,*)
+         WRITE(numout,*) '              Reference 1D z-coordinate depth and scale factors:'
+         WRITE(numout, "(9x,' level  gdept_1d  gdepw_1d  e3t_1d   e3w_1d  ')" )
+         WRITE(numout, "(10x, i4, 4f9.2)" ) ( jk, pdept_1d(jk), pdepw_1d(jk), pe3t_1d(jk), pe3w_1d(jk), jk = 1, jpk )
+      ENDIF
+      !
+      !                       !==  top masked level bathymetry  ==!  (all coordinates)
+      !
+      k_top(:,:) = 1 
+      !                                   !* bottom ocean compute from the depth of grid-points
+      k_bot(:,:) = jpkm1
+      DO jk = jpkm1, 1, -1
+        ze3min = 0.1_wp * pe3t_1d (jk)
+         WHERE( zht(:,:) < pdepw_1d(jk) + ze3min )   k_bot(:,:) = jk-1
+      END DO
+      !
+      !                                !* vertical coordinate system
+      DO jk = 1, jpk                      ! initialization to the reference z-coordinate
+         pdept(:,:,jk) = pdept_1d(jk)
+         pdepw(:,:,jk) = pdepw_1d(jk)
+         pe3t (:,:,jk) = pe3t_1d (jk)
+         pe3u (:,:,jk) = pe3t_1d (jk)
+         pe3v (:,:,jk) = pe3t_1d (jk)
+         pe3f (:,:,jk) = pe3t_1d (jk)
+         pe3w (:,:,jk) = pe3w_1d (jk)
+         pe3uw(:,:,jk) = pe3w_1d (jk)
+         pe3vw(:,:,jk) = pe3w_1d (jk)
+      END DO
+      ! bottom scale factors and depth at T- and W-points
+      DO_2D( 1, 1, 1, 1 )
+         ik = k_bot(ji,jj)
+         pdepw(ji,jj,ik+1) = MIN( zht(ji,jj) , pdepw_1d(ik+1) )
+         pe3t (ji,jj,ik  ) = pdepw(ji,jj,ik+1) - pdepw(ji,jj,ik)
+         pe3t (ji,jj,ik+1) = pe3t (ji,jj,ik  ) 
+         !
+         pdept(ji,jj,ik  ) = pdepw(ji,jj,ik  ) + pe3t (ji,jj,ik  ) * 0.5_wp
+         pdept(ji,jj,ik+1) = pdepw(ji,jj,ik+1) + pe3t (ji,jj,ik+1) * 0.5_wp
+         pe3w (ji,jj,ik+1) = pdept(ji,jj,ik+1) - pdept(ji,jj,ik)              ! = pe3t (ji,jj,ik  )
+      END_2D        
+      !                                   ! bottom scale factors and depth at  U-, V-, UW and VW-points
+      !                                   ! usually Computed as the minimum of neighbooring scale factors
+      pe3u (:,:,:) = pe3t(:,:,:)          ! HERE C1D configuration : 
+      pe3v (:,:,:) = pe3t(:,:,:)          !    e3 increases with k-index 
+      pe3f (:,:,:) = pe3t(:,:,:)          !    so e3 minimum of (i,i+1) points is (i) point
+      pe3uw(:,:,:) = pe3w(:,:,:)          !    in j-direction e3v=e3t and e3f=e3v
+      pe3vw(:,:,:) = pe3w(:,:,:)          !    ==>>  no need of lbc_lnk calls
+      !      
+      !
+   END SUBROUTINE usr_def_zgr
+
+   !!======================================================================
+END MODULE usrdef_zgr
Index: /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/cpp_C1D_ASICS.fcm
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/cpp_C1D_ASICS.fcm	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/C1D_ASICS/cpp_C1D_ASICS.fcm	(revision 14016)
@@ -0,0 +1,1 @@
+ bld::tool::fppkeys   key_c1d key_mpp_mpi key_iomput
Index: /NEMO/branches/2020/tickets_icb_1900/tests/ICE_ADV2D/MY_SRC/usrdef_sbc.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/ICE_ADV2D/MY_SRC/usrdef_sbc.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/ICE_ADV2D/MY_SRC/usrdef_sbc.F90	(revision 14016)
@@ -18,5 +18,5 @@
    USE sbc_ice         ! Surface boundary condition: ice fields
    USE phycst          ! physical constants
-   USE ice, ONLY       : at_i_b, a_i_b
+   USE ice, ONLY       : jpl, at_i_b, a_i_b
    USE icethd_dh       ! for CALL ice_thd_snwblow
    !
Index: /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/README
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/README	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/README	(revision 14016)
@@ -0,0 +1,54 @@
+-------
+purpose
+-------
+This demonstration case allows for examining the sea ice rheology under idealised
+forcing.  The set-up follows the paper "Stress and deformation characteristics of sea
+ice in a high-resolution, anisotropic sea ice model" by Heorton et al, Phil. Trans,
+R. Soc. 2018.
+
+-----------
+Experiments
+-----------
+A range of experiments can be configured:
+
+- The suggested experiment is to examine the different shear patterns formed
+  by different rheologies. The default set-up reproduces the leftmost panels
+  of figure 3a (ln_rhg_EAP true in namelist_ice_cfg) and 3b (ln_rhg_EVP true in
+  namelist_ice_cfg). Increase the last time step in namelist_cfg appropriately
+  to generate the other panels.
+
+- The resolution of the box can be changed analogous to figure 2 of the paper
+  (change rn_dx and rn_dy in namelist_cfg, default is 2000m)
+
+- The ratio of the windstress component can be changed to cause different modes 
+  of failure (Rwind in usrdef_sbc.F90, default is -0.8) 
+
+It takes about 10 minutes to run a simulation on 16 processors
+
+----------
+How to run
+----------
+
+a) Compile and run the model once to get mesh_mask_xxxx.nc files.
+   E.g., starting in the root NEMO directory:
+
+ ./makenemo -a ICE_RHEO -r ICE_RHEO_TEST -m X64_MOBILIS -j 4
+ cd tests/ICE_RHEO_TEST/EXP00
+ mpirun -np 1 ./nemo
+
+c) Create the initial condition file for sea-ice (initice.nc) by running
+   this python script:
+
+ python ../../ICE_RHEO/EXPREF/make_INITICE.py
+
+d) Run the model a second time
+
+ mpirun -np 16 ./nemo
+
+---------------
+What to look at
+---------------
+EVP rheology has less defined shear strain maxima (linear kinematic
+features). If the model is run longer one can see that the LKFs appearing
+with EVP rheology intersect at a different angle than with EAP rheology
+
Index: /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/context_nemo.xml
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/context_nemo.xml	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/context_nemo.xml	(revision 14016)
@@ -0,0 +1,47 @@
+<!--
+ ============================================================================================== 
+    NEMO context
+============================================================================================== 
+-->
+<context id="nemo">
+<!-- $id$ -->
+    <variable_definition>
+    <!-- Year of time origin for NetCDF files; defaults to 1800 -->
+       <variable id="ref_year" type="int"   > 1800 </variable>
+       <variable id="rau0"     type="float" > 1026.0 </variable>
+       <variable id="cpocean"  type="float" > 3991.86795711963 </variable>
+       <variable id="convSpsu" type="float" > 0.99530670233846  </variable>
+       <variable id="rhoic"    type="float" > 917.0 </variable>
+       <variable id="rhosn"    type="float" > 330.0 </variable>
+       <variable id="missval"  type="float" > 1.e20 </variable>
+    </variable_definition>
+<!-- Fields definition -->
+    <field_definition src="./field_def_nemo-oce.xml"/>    <!--  NEMO ocean dynamics     -->
+    <field_definition src="./field_def_nemo-ice.xml"/>    <!--  NEMO sea-ice model      -->
+
+<!-- Files definition -->
+    <file_definition src="./file_def_nemo-oce.xml"/>     <!--  NEMO ocean dynamics      -->
+    <file_definition src="./file_def_nemo-ice.xml"/>     <!--  NEMO sea-ice model       -->
+    <!-- 
+============================================================================================================
+= grid definition = = DO NOT CHANGE =
+============================================================================================================
+    -->
+    
+    <axis_definition>
+      <axis id="deptht"  long_name="Vertical T levels"  unit="m" positive="down" />
+      <axis id="depthu"  long_name="Vertical U levels"  unit="m" positive="down" />
+      <axis id="depthv"  long_name="Vertical V levels"  unit="m" positive="down" />
+      <axis id="depthw"  long_name="Vertical W levels"  unit="m" positive="down" />
+      <axis id="nfloat"  long_name="Float number"       unit="-"                 />
+      <axis id="icbcla"  long_name="Iceberg class"      unit="1"                 />
+      <axis id="ncatice" long_name="Ice category"       unit="1"                 />
+      <axis id="iax_20C" long_name="20 degC isotherm"   unit="degC"              />
+      <axis id="iax_28C" long_name="28 degC isotherm"   unit="degC"              />
+    </axis_definition>
+ 
+    <domain_definition src="./domain_def_nemo.xml"/>
+  
+    <grid_definition src="./grid_def_nemo.xml"/>
+
+</context>
Index: /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/domain_def_nemo.xml
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/domain_def_nemo.xml	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/domain_def_nemo.xml	(revision 14016)
@@ -0,0 +1,1 @@
+link ../../../cfgs/SHARED/domain_def_nemo.xml
Index: /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/field_def_nemo-ice.xml
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/field_def_nemo-ice.xml	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/field_def_nemo-ice.xml	(revision 14016)
@@ -0,0 +1,1 @@
+link ../../../cfgs/SHARED/field_def_nemo-ice.xml
Index: /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/field_def_nemo-oce.xml
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/field_def_nemo-oce.xml	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/field_def_nemo-oce.xml	(revision 14016)
@@ -0,0 +1,1 @@
+link ../../../cfgs/SHARED/field_def_nemo-oce.xml
Index: /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/file_def_nemo-ice.xml
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/file_def_nemo-ice.xml	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/file_def_nemo-ice.xml	(revision 14016)
@@ -0,0 +1,58 @@
+<?xml version="1.0"?>
+    <!-- $id$ -->
+    
+ <!--
+============================================================================================================
+=                                           output files definition                                        =
+============================================================================================================
+  -->
+    
+    <file_definition type="one_file" name="@expname@_@freq@_@startdate@_@enddate@" sync_freq="1d" min_digits="4">
+    
+    
+      <file_group id="5ts" output_freq="5ts"  output_level="10" enabled=".TRUE."/>  <!-- 1 day files -->   	
+      <file_group id="1ts" output_freq="1ts"  output_level="10" enabled=".TRUE."/> <!-- 1 time step files -->
+
+      <file_group id="1h" output_freq="1h"  output_level="10" enabled=".TRUE."> <!-- 1h files -->
+	<file id="file1" name_suffix="_icemod" description="ice variables" enabled=".true." >
+
+        <!-- ice mask -->
+        <field field_ref="icemask"          name="simsk"   />
+
+        <!-- general -->
+        <field field_ref="snwvolu"          name="snvolu" />
+        <field field_ref="icethic"          name="sithic" />
+        <field field_ref="iceconc"          name="siconc" />
+
+        <!-- momentum -->
+        <field field_ref="uice"             name="sivelu"  />
+        <field field_ref="vice"             name="sivelv"  />
+        <field field_ref="icevel"           name="sivelo"  />
+        <field field_ref="utau_ai"          name="utau_ai" />
+        <field field_ref="vtau_ai"          name="vtau_ai" />
+
+        <!-- rheology -->
+        <field field_ref="icediv"           name="sidive"  />
+        <field field_ref="iceshe"           name="sishea"  />
+        <field field_ref="icestr"           name="sistre"  />
+        <field field_ref="normstr"          name="normstr" />
+        <field field_ref="sheastr"          name="sheastr" />
+        <field field_ref="aniso"            name="aniso"   />
+
+        </file>
+      </file_group>
+
+      <file_group id="2h" output_freq="2h"  output_level="10" enabled=".TRUE."/> <!-- 2h files -->
+      <file_group id="3h" output_freq="3h"  output_level="10" enabled=".TRUE."/> <!-- 3h files -->     
+      <file_group id="4h" output_freq="4h"  output_level="10" enabled=".TRUE."/> <!-- 4h files -->
+      <file_group id="6h" output_freq="6h"  output_level="10" enabled=".TRUE."/> <!-- 6h files -->        
+      <file_group id="5d" output_freq="5d"  output_level="10" enabled=".TRUE."/>  <!-- 5d files -->   	
+      <file_group id="2m" output_freq="2mo" output_level="10" enabled=".TRUE."/> <!-- real 2m files -->
+      <file_group id="3m" output_freq="3mo" output_level="10" enabled=".TRUE."/> <!-- real 3m files -->
+      <file_group id="4m" output_freq="4mo" output_level="10" enabled=".TRUE."/> <!-- real 4m files -->
+      <file_group id="6m" output_freq="6mo" output_level="10" enabled=".TRUE."/> <!-- real 6m files -->
+      <file_group id="2y"  output_freq="2y" output_level="10" enabled=".TRUE."/> <!-- real 2y files -->
+      <file_group id="5y"  output_freq="5y" output_level="10" enabled=".TRUE."/> <!-- real 5y files -->
+      <file_group id="10y" output_freq="10y" output_level="10" enabled=".TRUE."/> <!-- real 10y files -->
+
+   </file_definition>
Index: /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/file_def_nemo-oce.xml
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/file_def_nemo-oce.xml	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/file_def_nemo-oce.xml	(revision 14016)
@@ -0,0 +1,44 @@
+<?xml version="1.0"?>
+
+<!--
+============================================================================================================
+=                                           output files definition                                        =
+=                                            Define your own filesfor ocean dynamics context                                         =
+=                                         put the variables you want...                                    =
+============================================================================================================
+    -->
+   
+    <file_definition type="one_file" name="@expname@_@freq@_@startdate@_@enddate@" sync_freq="1d" min_digits="4">
+    
+
+      <file_group id="1ts" output_freq="1ts"  output_level="10" enabled=".TRUE."/> <!-- 1 time step files -->
+      <file_group id="1h" output_freq="1h"  output_level="10" enabled=".TRUE."> <!-- 1h files -->
+        <file id="file2" name_suffix="_grid_U" description="ocean U grid variables" >
+          <field field_ref="uoce"         name="vozocrtx"  />
+          <field field_ref="utau"         name="sozotaux"  />
+        </file>
+
+        <file id="file3" name_suffix="_grid_V" description="ocean V grid variables" >
+          <field field_ref="voce"         name="vomecrty"  />
+          <field field_ref="vtau"         name="sometauy"  />
+        </file>
+      </file_group>
+
+      <file_group id="2h" output_freq="2h"  output_level="10" enabled=".TRUE."/> <!-- 2h files -->
+      <file_group id="3h" output_freq="3h"  output_level="10" enabled=".TRUE."/> <!-- 3h files -->     
+      <file_group id="4h" output_freq="4h"  output_level="10" enabled=".TRUE."/> <!-- 4h files -->
+      <file_group id="6h" output_freq="6h"  output_level="10" enabled=".TRUE."/> <!-- 6h files -->     
+      <file_group id="5d" output_freq="5d"  output_level="10" enabled=".TRUE."/>  <!-- 5d files -->   
+      <file_group id="2m" output_freq="2mo" output_level="10" enabled=".TRUE."/> <!-- real 2m files -->
+      <file_group id="3m" output_freq="3mo" output_level="10" enabled=".TRUE."/> <!-- real 3m files -->
+      <file_group id="4m" output_freq="4mo" output_level="10" enabled=".TRUE."/> <!-- real 4m files -->
+      <file_group id="6m" output_freq="6mo" output_level="10" enabled=".TRUE."/> <!-- real 6m files -->
+      <file_group id="2y"  output_freq="2y" output_level="10" enabled=".TRUE."/> <!-- real 2y files -->
+      <file_group id="5y"  output_freq="5y" output_level="10" enabled=".TRUE."/> <!-- real 5y files -->
+      <file_group id="10y" output_freq="10y" output_level="10" enabled=".TRUE."/> <!-- real 10y files -->
+
+   </file_definition>
+    
+
+
+
Index: /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/grid_def_nemo.xml
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/grid_def_nemo.xml	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/grid_def_nemo.xml	(revision 14016)
@@ -0,0 +1,1 @@
+link ../../../cfgs/SHARED/grid_def_nemo.xml
Index: /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/iodef.xml
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/iodef.xml	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/iodef.xml	(revision 14016)
@@ -0,0 +1,30 @@
+<?xml version="1.0"?>
+<simulation> 
+
+<!-- ============================================================================================ -->
+<!-- XIOS context                                                                                 -->
+<!-- ============================================================================================ -->
+
+  <context id="xios" >
+
+      <variable_definition>
+
+	  <!--
+          <variable id="optimal_buffer_size"       type="string">memory</variable>
+          <variable id="buffer_size_factor"        type="double">1.0</variable>
+	  -->
+	  <variable id="info_level"                type="int">10</variable>
+	  <variable id="using_server"              type="bool">false</variable>
+	  <variable id="using_oasis"               type="bool">false</variable>
+	  <variable id="oasis_codes_id"            type="string" >oceanx</variable>
+	
+      </variable_definition>
+  </context>
+
+<!-- ============================================================================================ -->
+<!-- NEMO  CONTEXT add and suppress the components you need                                       -->
+<!-- ============================================================================================ -->
+
+  <context id="nemo" src="./context_nemo.xml"/>       <!--  NEMO       -->
+
+</simulation>
Index: /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/make_INITICE.py
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/make_INITICE.py	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/make_INITICE.py	(revision 14016)
@@ -0,0 +1,91 @@
+#!/usr/bin/python
+
+import os,sys
+from netCDF4 import Dataset as netcdf
+import numpy as np
+
+resname=''
+
+# input file
+fcoord='mesh_mask.nc'
+
+# output file
+fflx='initice.nc'
+
+print '   creating init ice file  ' +fflx
+
+# Reading coordinates file
+nccoord=netcdf(fcoord,'r')
+nav_lon=nccoord.variables['glamt'][0,:,:]
+nav_lat=nccoord.variables['gphit'][0,:,:]
+time_counter=1
+LON1= nav_lon.shape[1]
+LAT1= nav_lon.shape[0]
+print 'nav_lon.shape[1]' ,nav_lon.shape[1]
+print 'LON1 ', LON1
+print 'LAT1 ', LAT1
+
+# Creating INITICE netcdf file
+nc=netcdf(fflx,'w')
+nc.createDimension('y',LAT1)
+nc.createDimension('x',LON1)
+nc.createDimension('time_counter',None)    # Setting dimension size to 0 or None makes it unlimited. 
+
+cdflon=nc.createVariable('nav_lon','f',('y','x'))
+cdflat=nc.createVariable('nav_lat','f',('y','x'))
+cdftimecounter=nc.createVariable('time_counter','f',('time_counter'))
+
+# ati : Fraction of open waters in sea ice - units %
+# hti : Sea ice thickness - units m
+# hts : Snow thickness - units m
+# smi : Sea ice salinity:
+# tmi : Sea ice internal temperature - units K
+# tsu : Sea ice surface temperature - units K
+#
+# Take constant values from namelist &namiceini of NEMO
+rn_hti_ini=2.0
+rn_hts_ini=0.1            #  initial real snow thickness (m)
+rn_ati_ini=0.99           #  initial ice concentration   (-)
+rn_smi_ini=6.3            #  initial ice salinity     (g/kg)
+rn_tmi_ini=270.           #  initial ice/snw temperature (K)
+rn_tsu_ini=270.           #  initial sea ice temperature (K)
+#
+cdfati=nc.createVariable('ati','f',('time_counter','y','x'))
+cdfati.units='Percentage'
+cdfati.long_name='Sea ice concentration'
+cdfhti=nc.createVariable('hti','f',('time_counter','y','x'))
+cdfhti.long_name='Sea ice thickness'
+cdfhti.units='m'
+cdfhts=nc.createVariable('hts','f',('time_counter','y','x'))
+cdfhts.long_name='Snow thickness'
+cdfhts.units='m'
+cdfsmi=nc.createVariable('smi','f',('time_counter','y','x'))
+cdfsmi.long_name='Sea ice salinity'
+cdfsmi.units='pss'
+cdftmi=nc.createVariable('tmi','f',('time_counter','y','x'))
+cdftmi.long_name='Sea ice internal temperature'
+cdftmi.units='Kelvin'
+cdftsu=nc.createVariable('tsu','f',('time_counter','y','x'))
+cdftsu.long_name='Sea ice surface temperature'
+cdftsu.units='Kelvin'
+
+cdflon[:,:]=nav_lon[:,:]
+cdflat[:,:]=nav_lat[:,:]
+cdftimecounter[0]=1
+
+cdfati[:,:,:]=rn_ati_ini
+cdfhts[:,:,:]=rn_hts_ini
+cdfsmi[:,:,:]=rn_smi_ini
+cdftmi[:,:,:]=rn_tmi_ini
+cdftsu[:,:,:]=rn_tsu_ini
+
+# --- add noise in initial thickness to help nucleation of deformation ---
+#for y in np.arange(1,1000,1) :
+#    for x in np.arange(1,1000,1) :
+#        cdfhti[:,y,x] = rn_hti_ini+0.02*rn_hti_ini*np.random.uniform(-1,1)
+cdfhti[:,:,:]=rn_hti_ini
+
+nc.close()
+nccoord.close()
+
+#sys.exit()
Index: /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/namelist_cfg
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/namelist_cfg	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/namelist_cfg	(revision 14016)
@@ -0,0 +1,263 @@
+!!>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+!! NEMO/OCE  Configuration namelist : overwrite default values defined in SHARED/namelist_ref
+!!>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+!!               ICE_RHEO configuration                              !!
+!!======================================================================
+!!              ***  Domain & Run management namelists  ***           !!
+!!                                                                    !!
+!!   namrun       parameters of the run
+!!   namdom       space and time domain
+!!   namcfg       parameters of the configuration                       (default: user defined GYRE)
+!!   namwad       Wetting and drying                                    (default: OFF)
+!!   namtsd       data: temperature & salinity                          (default: OFF)
+!!   namcrs       coarsened grid (for outputs and/or TOP)               (ln_crs =T)
+!!   namc1d       1D configuration options                              ("key_c1d")
+!!   namc1d_dyndmp 1D newtonian damping applied on currents             ("key_c1d")
+!!   namc1d_uvd   1D data (currents)                                    ("key_c1d")
+!!======================================================================
+!
+!-----------------------------------------------------------------------
+&namusr_def    !   ICE_RHEO user defined namelist
+!-----------------------------------------------------------------------
+   rn_dx       =    2000.   ! horizontal resolution in meters
+   rn_dy       =    2000.   ! horizontal resolution in meters
+   ln_corio    =    .false. ! set coriolis=0 (false) or not (true)
+      rn_ppgphi0  =  90.    ! Reference latitude [degrees]
+/
+!-----------------------------------------------------------------------
+&namrun        !   parameters of the run
+!-----------------------------------------------------------------------
+   nn_no       =       0      !  job number (no more used...)
+   cn_exp      =  "ICE_RHEO"  !  experience name
+   nn_it000    =       1      !  first time step
+   nn_itend    =     720     !  last  time step (std 5475)
+   nn_istate   =       1      !  output the initial state (1) or not (0)
+/
+!-----------------------------------------------------------------------
+&namdom        !   time and space domain
+!-----------------------------------------------------------------------
+   ln_linssh   = .true.    !  =T  linear free surface  ==>>  model level are fixed in time
+   !
+   rn_Dt      = 30.       !  time step for the dynamics and tracer
+   !
+   ln_meshmask = .true.    !  =T  create a mesh file
+/
+!-----------------------------------------------------------------------
+&namcfg        !   parameters of the configuration                      (default: use namusr_def in namelist_cfg)
+!-----------------------------------------------------------------------
+   ln_read_cfg = .false.    !  (=T) read the domain configuration file
+      !                     !  (=F) user defined configuration  ==>>>  see usrdef(_...) modules
+/
+!!======================================================================
+!!            ***  Surface Boundary Condition namelists  ***          !!
+!!                                                                    !!
+!!   namsbc          surface boundary condition manager                 (default: NO selection)
+!!   namsbc_flx      flux               formulation                     (ln_flx     =T)
+!!   namsbc_blk      Bulk formulae formulation                          (ln_blk     =T)
+!!   namsbc_cpl      CouPLed            formulation                     ("key_oasis3" )
+!!   namsbc_sas      Stand-Alone Surface module                         (SAS_SRC  only)
+!!   namsbc_iif      Ice-IF: use observed ice cover                     (nn_ice = 1   )
+!!   namtra_qsr      penetrative solar radiation                        (ln_traqsr  =T)
+!!   namsbc_ssr      sea surface restoring term (for T and/or S)        (ln_ssr     =T)
+!!   namsbc_rnf      river runoffs                                      (ln_rnf     =T)
+!!   namsbc_apr      Atmospheric Pressure                               (ln_apr_dyn =T)
+!!   namsbc_wave     external fields from wave model                    (ln_wave    =T)
+!!   namberg         iceberg floats                                     (ln_icebergs=T)
+!!======================================================================
+!
+!-----------------------------------------------------------------------
+&namsbc        !   Surface Boundary Condition manager                   (default: NO selection)
+!-----------------------------------------------------------------------
+   ln_usr      = .true.   !  user defined formulation                  (T => check usrdef_sbc)
+   ln_flx      = .false.   !  flux formulation                          (T => fill namsbc_flx )
+   ln_blk      = .false.    !  Bulk formulation                          (T => fill namsbc_blk )
+   nn_fsbc     =  1        !  frequency of surface boundary condition computation
+   nn_ice      =  2        !  sea-ice model
+/
+!-----------------------------------------------------------------------
+&namsbc_blk    !   namsbc_blk  generic Bulk formula          (ln_blk =T)
+!-----------------------------------------------------------------------
+   !                    !  bulk algorithm :
+   ln_NCAR      = .true.    ! "NCAR"      algorithm   (Large and Yeager 2008)
+   ln_COARE_3p0 = .false.   ! "COARE 3.0" algorithm   (Fairall et al. 2003)
+   ln_COARE_3p6 = .false.   ! "COARE 3.6" algorithm   (Edson et al. 2013)
+   ln_ECMWF     = .false.   ! "ECMWF"     algorithm   (IFS cycle 45r1)
+      !
+      rn_zqt      = 10.       !  Air temperature & humidity reference height (m)
+      rn_zu       = 10.       !  Wind vector reference height (m)
+      ln_Cd_L12   = .false.   !  air-ice drags = F(ice conc.) (Lupkes et al. 2012)
+      ln_Cd_L15   = .false.   !  air-ice drags = F(ice conc.) (Lupkes et al. 2015)
+      rn_pfac     = 1.        !  multiplicative factor for precipitation (total & snow)
+      rn_efac     = 1.        !  multiplicative factor for evaporation (0. or 1.)
+
+   cn_dir      = './'      !  root directory for the bulk data location
+   !___________!_________________________!___________________!___________!_____________!________!___________!______________________________________!__________!_______________!
+   !           !  file name              ! frequency (hours) ! variable  ! time interp.!  clim  ! 'yearly'/ !       weights filename               ! rotation ! land/sea mask !
+   !           !                         !  (if <0  months)  !   name    !   (logical) !  (T/F) ! 'monthly' !                                      ! pairing  !    filename   !
+   sn_wndi     = 'u_10.15JUNE2009_fill'       ,    3.        , 'U_10_MOD',   .true.    , .true. , 'yearly'  , ''                                   , 'Uwnd'   , ''
+   sn_wndj     = 'v_10.15JUNE2009_fill'       ,    3.        , 'V_10_MOD',   .true.    , .true. , 'yearly'  , ''                                   , 'Vwnd'   , ''
+   sn_qsr      = ''                           ,    0.        , 'SWDN_MOD',   .false.   , .true. , 'yearly'  , ''                                   , ''       , ''
+   sn_qlw      = ''                           ,    0.        , 'LWDN_MOD',   .false.   , .true. , 'yearly'  , ''                                   , ''       , ''
+   sn_tair     = ''                           ,    0.        , 'T_10_MOD',   .false.   , .true. , 'yearly'  , ''                                   , ''       , ''
+   sn_humi     = ''                           ,    0.        , 'Q_10_MOD',   .false.   , .true. , 'yearly'  , ''                                   , ''       , ''
+   sn_prec     = ''                           ,    0.        , 'PRC_MOD1',   .false.   , .true. , 'yearly'  , ''                                   , ''       , ''
+   sn_snow     = ''                           ,    0.        , 'SNOW'    ,   .false.   , .true. , 'yearly'  , ''                                   , ''       , ''
+   sn_slp      = ''                           ,    0.        , 'SLP'     ,   .false.   , .true. , 'yearly'  , ''                                   , ''       , ''
+
+/
+!-----------------------------------------------------------------------
+&namsbc_sas    !   Stand-Alone Surface module: ocean data               (SAS_SRC  only)
+!-----------------------------------------------------------------------
+   l_sasread   = .false.  ! =T Read in file ;  =F set all to 0. (see sbcssm.F90)
+/
+!!======================================================================
+!!               ***  Lateral boundary condition  ***                 !!
+!!                                                                    !!
+!!   namlbc        lateral momentum boundary condition                  (default: NO selection)
+!!   namagrif      agrif nested grid   (read by child model only)       ("key_agrif")
+!!   nam_tide      Tidal forcing                                        (default: OFF)
+!!   nambdy        Unstructured open boundaries                         (default: OFF)
+!!   nambdy_dta    Unstructured open boundaries - external data         (see  nambdy)
+!!   nambdy_tide   tidal forcing at open boundaries                     (default: OFF)
+!!======================================================================
+!
+!-----------------------------------------------------------------------
+&namlbc        !   lateral momentum boundary condition                  (default: NO selection)
+!-----------------------------------------------------------------------
+   rn_shlat    =    0.     !  free slip
+/
+!-----------------------------------------------------------------------
+&namagrif      !  AGRIF zoom                                            ("key_agrif")
+!-----------------------------------------------------------------------
+/
+!!======================================================================
+!!                ***  Top/Bottom boundary condition  ***             !!
+!!                                                                    !!
+!!   namdrg        top/bottom drag coefficient                          (default: NO selection)
+!!   namdrg_top    top    friction                                      (ln_drg_OFF=F & ln_isfcav=T)
+!!   namdrg_bot    bottom friction                                      (ln_drg_OFF=F)
+!!   nambbc        bottom temperature boundary condition                (default: OFF)
+!!   nambbl        bottom boundary layer scheme                         (default: OFF)
+!!======================================================================
+!
+!-----------------------------------------------------------------------
+&namdrg        !   top/bottom drag coefficient                          (default: NO selection)
+!-----------------------------------------------------------------------
+   ln_drg_OFF  = .false.   !  free-slip       : Cd = 0                  (F => fill namdrg_bot
+/
+!!======================================================================
+!!                        Tracer (T-S) namelists                      !!
+!!                                                                    !!
+!!   nameos        equation of state                                    (default: NO selection)
+!!   namtra_adv    advection scheme                                     (default: NO selection)
+!!   namtra_ldf    lateral diffusion scheme                             (default: NO selection)
+!!   namtra_mle    mixed layer eddy param. (Fox-Kemper param.)          (default: OFF)
+!!   namtra_eiv    eddy induced velocity param.                         (default: OFF)
+!!   namtra_dmp    T & S newtonian damping                              (default: OFF)
+!!======================================================================
+!
+!-----------------------------------------------------------------------
+&nameos        !   ocean Equation Of Seawater                           (default: NO selection)
+!-----------------------------------------------------------------------
+   ln_teos10   = .true.          !  = Use TEOS-10
+/
+!-----------------------------------------------------------------------
+&namtra_adv    !   advection scheme for tracer                          (default: NO selection)
+!-----------------------------------------------------------------------
+   ln_traadv_fct =  .true.    !  FCT scheme
+      nn_fct_h   =  4               !  =2/4, horizontal 2nd / 4th order 
+      nn_fct_v   =  2               !  =2/4, vertical   2nd / COMPACT 4th order 
+/
+!----------------------------------------------------------------------------------
+&namtra_ldf    !   lateral diffusion scheme for tracers                 (default: NO selection)
+!----------------------------------------------------------------------------------
+   ln_traldf_OFF    =  .true.   !    laplacian operator
+/
+!!======================================================================
+!!                      ***  Dynamics namelists  ***                  !!
+!!                                                                    !!
+!!   nam_vvl       vertical coordinate options                          (default: z-star)
+!!   namdyn_adv    formulation of the momentum advection                (default: NO selection)
+!!   namdyn_vor    advection scheme                                     (default: NO selection)
+!!   namdyn_hpg    hydrostatic pressure gradient                        (default: NO selection)
+!!   namdyn_spg    surface pressure gradient                            (default: NO selection)
+!!   namdyn_ldf    lateral diffusion scheme                             (default: NO selection)
+!!   namdta_dyn    offline TOP: dynamics read in files                  (OFF_SRC only)
+!!======================================================================
+!
+!-----------------------------------------------------------------------
+&namdyn_adv    !   formulation of the momentum advection                (default: NO selection)
+!-----------------------------------------------------------------------
+   ln_dynadv_OFF = .true. !  linear dynamics (no momentum advection)
+/
+!-----------------------------------------------------------------------
+&namdyn_hpg    !   Hydrostatic pressure gradient option                 (default: NO selection)
+!-----------------------------------------------------------------------
+   ln_hpg_zco  = .true.   !  z-coordinate - full steps
+/
+!-----------------------------------------------------------------------
+&namdyn_spg    !   surface pressure gradient                            (default: NO selection)
+!-----------------------------------------------------------------------
+   ln_dynspg_ts  = .true.  !  split-explicit free surface
+/
+!-----------------------------------------------------------------------
+&namdyn_ldf    !   lateral diffusion on momentum                        (default: NO selection)
+!-----------------------------------------------------------------------
+   ln_dynldf_OFF = .true.     !  No operator (i.e. no explicit diffusion)
+/
+!!======================================================================
+!!                     vertical physics namelists                     !!
+!!                                                                    !!
+!!    namzdf        vertical physics manager                            (default: NO selection)
+!!    namzdf_ric    richardson number vertical mixing                   (ln_zdfric=T)
+!!    namzdf_tke    TKE vertical mixing                                 (ln_zdftke=T)
+!!    namzdf_gls    GLS vertical mixing                                 (ln_zdfgls=T)
+!!    namzdf_osm    OSM vertical diffusion                              (ln_zdfosm=T)
+!!    namzdf_iwm    tidal mixing parameterization                       (ln_zdfiwm=T)
+!!======================================================================
+!
+!-----------------------------------------------------------------------
+&namzdf        !   vertical physics manager                             (default: NO selection)
+!-----------------------------------------------------------------------
+   ln_zdfcst   = .true.       !  constant mixing
+/
+!!======================================================================
+!!                  ***  Diagnostics namelists  ***                   !!
+!!                                                                    !!
+!!   namtrd       dynamics and/or tracer trends                         (default: OFF)
+!!   namhsb       Heat and salt budgets                                 (default: OFF)
+!!   namdiu       Cool skin and warm layer models                       (default: OFF)
+!!   namdiu       Cool skin and warm layer models                       (default: OFF)
+!!   namflo       float parameters                                      (default: OFF)
+!!   nam_diadct   transports through some sections                      (default: OFF)
+!!   nam_dia25h   25h Mean Output                                       (default: OFF)
+!!   namnc4       netcdf4 chunking and compression settings             ("key_netcdf4")
+!!======================================================================
+!
+!!======================================================================
+!!               ***  Observation & Assimilation  ***                 !!
+!!                                                                    !!
+!!   namobs       observation and model comparison                      (default: OFF)
+!!   nam_asminc   assimilation increments                               ('key_asminc')
+!!======================================================================
+!
+!!======================================================================
+!!                  ***  Miscellaneous namelists  ***                 !!
+!!                                                                    !!
+!!   nammpp            Massively Parallel Processing                    ("key_mpp_mpi")
+!!   namctl            Control prints                                   (default: OFF)
+!!   namsto            Stochastic parametrization of EOS                (default: OFF)
+!!======================================================================
+!
+!-----------------------------------------------------------------------
+&nammpp        !   Massively Parallel Processing                        ("key_mpp_mpi")
+!-----------------------------------------------------------------------
+/
+!-----------------------------------------------------------------------
+&namctl        !   Control prints                                       (default: OFF)
+!-----------------------------------------------------------------------
+/
+!-----------------------------------------------------------------------
+&namsto        ! Stochastic parametrization of EOS                      (default: OFF)
+!-----------------------------------------------------------------------
+/
Index: /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/namelist_ice_cfg
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/namelist_ice_cfg	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/namelist_ice_cfg	(revision 14016)
@@ -0,0 +1,131 @@
+!!>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+!! SI3 configuration namelist: Overwrites SHARED/namelist_ice_ref
+!!              1 - Generic parameters                 (nampar)
+!!              2 - Ice thickness discretization       (namitd)
+!!              3 - Ice dynamics                       (namdyn)
+!!              4 - Ice ridging/rafting                (namdyn_rdgrft)
+!!              5 - Ice rheology                       (namdyn_rhg)
+!!              6 - Ice advection                      (namdyn_adv)
+!!              7 - Ice surface boundary conditions    (namsbc)
+!!              8 - Ice thermodynamics                 (namthd)
+!!              9 - Ice heat diffusion                 (namthd_zdf)
+!!             10 - Ice lateral melting                (namthd_da)
+!!             11 - Ice growth in open water           (namthd_do)
+!!             12 - Ice salinity                       (namthd_sal)
+!!             13 - Ice melt ponds                     (namthd_pnd)
+!!             14 - Ice initialization                 (namini)
+!!             15 - Ice/snow albedos                   (namalb)
+!!             16 - Ice diagnostics                    (namdia)
+!!>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+!
+!------------------------------------------------------------------------------
+&nampar         !   Generic parameters
+!------------------------------------------------------------------------------
+   jpl              =   1             !  number of ice  categories
+   ln_icedyn        = .true.          !  ice dynamics (T) or not (F)
+   ln_icethd        = .false.         !  ice thermo   (T) or not (F)
+/
+!------------------------------------------------------------------------------
+&namitd         !   Ice discretization
+!------------------------------------------------------------------------------
+/
+!------------------------------------------------------------------------------
+&namdyn         !   Ice dynamics
+!------------------------------------------------------------------------------
+   ln_dynALL        = .true.          !  dyn.: full ice dynamics               (rheology + advection + ridging/rafting + correction)
+   ln_dynRHGADV     = .false.         !  dyn.: no ridge/raft & no corrections  (rheology + advection)
+   ln_dynADV1D      = .false.         !  dyn.: only advection 1D                  (Schar & Smolarkiewicz 1996 test case)
+   ln_dynADV2D      = .false.         !  dyn.: only advection 2D w prescribed vel.(rn_uvice + advection)
+/
+!------------------------------------------------------------------------------
+&namdyn_rdgrft  !   Ice ridging/rafting
+!------------------------------------------------------------------------------
+   ln_str_H79       = .true.          !  ice strength param.: Hibler_79   => P = pstar*<h>*exp(-c_rhg*A)
+      rn_pstar      =   2700          !     ice strength thickness parameter [N/m2]
+      rn_crhg       =   20.0          !     ice strength conc. parameter (-)
+/
+!------------------------------------------------------------------------------
+&namdyn_rhg     !   Ice rheology
+!------------------------------------------------------------------------------
+   ln_rhg_EVP       = .false.         !  EVP rheology
+      ln_aEVP       = .false.         !     adaptive rheology (Kimmritz et al. 2016 & 2017)
+      rn_creepl     =   2.0e-9        !     creep limit [1/s]
+      rn_ecc        =   2.0           !     eccentricity of the elliptical yield curve          
+      nn_nevp       =   200           !     number of EVP subcycles                             
+      rn_relast     =   0.333         !     ratio of elastic timescale to ice time step: Telast = dt_ice * rn_relast 
+                                      !        advised value: 1/3 (rn_nevp=120) or 1/9 (rn_nevp=300)
+   ln_rhg_EAP       = .true.          !  EAP rheology
+   nn_rhg_chkcvg    =   0             !  check convergence of rheology
+                                      !     = 0  no check
+                                      !     = 1  check at the main time step (output xml: uice_cvg)
+                                      !     = 2  check at both main and rheology time steps (additional output: ice_cvg.nc)
+                                      !          this option 2 asks a lot of communications between cpu
+/
+!------------------------------------------------------------------------------
+&namdyn_adv     !   Ice advection
+!------------------------------------------------------------------------------
+   ln_adv_Pra       = .true.          !  Advection scheme (Prather)
+   ln_adv_UMx       = .false.         !  Advection scheme (Ultimate-Macho)
+      nn_UMx        =   5             !     order of the scheme for UMx (1-5 ; 20=centered 2nd order)
+/
+!------------------------------------------------------------------------------
+&namsbc         !   Ice surface boundary conditions
+!------------------------------------------------------------------------------
+/
+!------------------------------------------------------------------------------
+&namthd         !   Ice thermodynamics
+!------------------------------------------------------------------------------
+/
+!------------------------------------------------------------------------------
+&namthd_zdf     !   Ice heat diffusion
+!------------------------------------------------------------------------------
+/
+!------------------------------------------------------------------------------
+&namthd_da      !   Ice lateral melting
+!------------------------------------------------------------------------------
+/
+!------------------------------------------------------------------------------
+&namthd_do      !   Ice growth in open water
+!------------------------------------------------------------------------------
+/
+!------------------------------------------------------------------------------
+&namthd_sal     !   Ice salinity
+!------------------------------------------------------------------------------
+/
+!------------------------------------------------------------------------------
+&namthd_pnd     !   Melt ponds
+!------------------------------------------------------------------------------
+/
+!------------------------------------------------------------------------------
+&namini         !   Ice initialization
+!------------------------------------------------------------------------------
+   ln_iceini        = .true.          !  activate ice initialization (T) or not (F)
+   nn_iceini_file   =   1             !     0 = Initialise sea ice based on SSTs
+                                      !     1 = Initialise sea ice from single category netcdf file
+                                      !     2 = Initialise sea ice from multi category restart file
+   rn_thres_sst     =   2.0           !  max temp. above Tfreeze with initial ice = (sst - tfreeze)
+   rn_hti_ini_n     =   3.0           !  initial ice thickness       (m), North
+   rn_hts_ini_n     =   0.1           !  initial snow thickness      (m), North
+   rn_ati_ini_n     =   0.99          !  initial ice concentration   (-), North
+   rn_smi_ini_n     =   6.3           !  initial ice salinity     (g/kg), North
+   rn_tmi_ini_n     = 270.            !  initial ice temperature     (K), North
+   rn_tsu_ini_n     = 270.            !  initial surface temperature (K), North
+   rn_tms_ini_n     = 270.            !  initial snw temperature     (K), North
+   rn_apd_ini_n     =   0.0           !  initial pond fraction       (-), North
+   rn_hpd_ini_n     =   0.0           !  initial pond depth          (m), North
+   sn_hti = 'initice'                 , -12. ,'hti'   ,  .false.  , .true., 'yearly'  , '' , '', ''
+   sn_hts = 'initice'                 , -12. ,'hts'   ,  .false.  , .true., 'yearly'  , '' , '', ''
+   sn_ati = 'initice'                 , -12. ,'ati'   ,  .false.  , .true., 'yearly'  , '' , '', ''
+   sn_tsu = 'initice'                 , -12. ,'tsu'   ,  .false.  , .true., 'yearly'  , '' , '', ''
+   sn_tmi = 'initice'                 , -12. ,'tmi'   ,  .false.  , .true., 'yearly'  , '' , '', ''
+   sn_smi = 'initice'                 , -12. ,'smi'   ,  .false.  , .true., 'yearly'  , '' , '', ''
+   cn_dir='./'
+/
+!------------------------------------------------------------------------------
+&namalb         !   albedo parameters
+!------------------------------------------------------------------------------
+/
+!------------------------------------------------------------------------------
+&namdia         !   Diagnostics
+!------------------------------------------------------------------------------
+/
Index: /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/namelist_ice_ref
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/namelist_ice_ref	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/namelist_ice_ref	(revision 14016)
@@ -0,0 +1,1 @@
+link ../../../cfgs/SHARED/namelist_ice_ref
Index: /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/namelist_ref
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/namelist_ref	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/EXPREF/namelist_ref	(revision 14016)
@@ -0,0 +1,1 @@
+link ../../../cfgs/SHARED/namelist_ref
Index: /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/MY_SRC/icedyn_rhg_eap.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/MY_SRC/icedyn_rhg_eap.F90	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/MY_SRC/icedyn_rhg_eap.F90	(revision 14016)
@@ -0,0 +1,2002 @@
+MODULE icedyn_rhg_eap
+   !!======================================================================
+   !!                     ***  MODULE  icedyn_rhg_eap  ***
+   !!   Sea-Ice dynamics : rheology Elasto-Viscous-Plastic
+   !!======================================================================
+   !! History :   -   !  2007-03  (M.A. Morales Maqueda, S. Bouillon) Original code
+   !!            3.0  !  2008-03  (M. Vancoppenolle) adaptation to new model
+   !!             -   !  2008-11  (M. Vancoppenolle, S. Bouillon, Y. Aksenov) add surface tilt in ice rheolohy 
+   !!            3.3  !  2009-05  (G.Garric)    addition of the evp case
+   !!            3.4  !  2011-01  (A. Porter)   dynamical allocation 
+   !!            3.5  !  2012-08  (R. Benshila) AGRIF
+   !!            3.6  !  2016-06  (C. Rousset)  Rewriting + landfast ice + mEVP (Bouillon 2013)
+   !!            3.7  !  2017     (C. Rousset)  add aEVP (Kimmritz 2016-2017)
+   !!            4.0  !  2018     (many people) SI3 [aka Sea Ice cube]
+   !!                 !  2019     (S. Rynders, Y. Aksenov, C. Rousset)  change into eap rheology from
+   !!                                           CICE code (Tsamados, Heorton) 
+   !!----------------------------------------------------------------------
+#if defined key_si3
+   !!----------------------------------------------------------------------
+   !!   'key_si3'                                       SI3 sea-ice model
+   !!----------------------------------------------------------------------
+   !!   ice_dyn_rhg_eap : computes ice velocities from EVP rheology
+   !!   rhg_eap_rst     : read/write EVP fields in ice restart
+   !!----------------------------------------------------------------------
+   USE phycst         ! Physical constant
+   USE dom_oce        ! Ocean domain
+   USE sbc_oce , ONLY : ln_ice_embd, nn_fsbc, ssh_m
+   USE sbc_ice , ONLY : utau_ice, vtau_ice, snwice_mass, snwice_mass_b
+   USE ice            ! sea-ice: ice variables
+   USE icevar         ! ice_var_sshdyn
+   USE icedyn_rdgrft  ! sea-ice: ice strength
+   USE bdy_oce , ONLY : ln_bdy 
+   USE bdyice 
+#if defined key_agrif
+   USE agrif_ice_interp
+#endif
+   !
+   USE in_out_manager ! I/O manager
+   USE iom            ! I/O manager library
+   USE lib_mpp        ! MPP library
+   USE lib_fortran    ! fortran utilities (glob_sum + no signed zero)
+   USE lbclnk         ! lateral boundary conditions (or mpp links)
+   USE prtctl         ! Print control
+
+   USE netcdf         ! NetCDF library for convergence test
+   IMPLICIT NONE
+   PRIVATE
+
+   PUBLIC   ice_dyn_rhg_eap   ! called by icedyn_rhg.F90
+   PUBLIC   rhg_eap_rst       ! called by icedyn_rhg.F90
+
+   REAL(wp), PARAMETER ::   pphi = 3.141592653589793_wp/12._wp    ! diamond shaped floe smaller angle (default phi = 30 deg)
+
+   ! look-up table for calculating structure tensor
+   INTEGER, PARAMETER ::   nx_yield = 41
+   INTEGER, PARAMETER ::   ny_yield = 41
+   INTEGER, PARAMETER ::   na_yield = 21
+
+   REAL(wp), DIMENSION(nx_yield, ny_yield, na_yield) ::   s11r, s12r, s22r, s11s, s12s, s22s
+
+   !! * Substitutions
+#  include "do_loop_substitute.h90"
+#  include "domzgr_substitute.h90"
+
+   !! for convergence tests
+   INTEGER ::   ncvgid   ! netcdf file id
+   INTEGER ::   nvarid   ! netcdf variable id
+   REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   zmsk00, zmsk15
+   !!----------------------------------------------------------------------
+   !! NEMO/ICE 4.0 , NEMO Consortium (2018)
+   !! $Id: icedyn_rhg_eap.F90 11536 2019-09-11 13:54:18Z smasson $
+   !! Software governed by the CeCILL license (see ./LICENSE)
+   !!----------------------------------------------------------------------
+CONTAINS
+
+   SUBROUTINE ice_dyn_rhg_eap( kt, Kmm, pstress1_i, pstress2_i, pstress12_i, pshear_i, pdivu_i, pdelta_i, paniso_11, paniso_12, prdg_conv )
+      !!-------------------------------------------------------------------
+      !!                 ***  SUBROUTINE ice_dyn_rhg_eap  ***
+      !!                             EAP-C-grid
+      !!
+      !! ** purpose : determines sea ice drift from wind stress, ice-ocean
+      !!  stress and sea-surface slope. Ice-ice interaction is described by 
+      !!  a non-linear elasto-anisotropic-plastic (EAP) law including shear 
+      !!  strength and a bulk rheology .	
+      !!
+      !!  The points in the C-grid look like this, dear reader
+      !!
+      !!                              (ji,jj)
+      !!                                 |
+      !!                                 |
+      !!                      (ji-1,jj)  |  (ji,jj)
+      !!                             ---------   
+      !!                            |         |
+      !!                            | (ji,jj) |------(ji,jj)
+      !!                            |         |
+      !!                             ---------   
+      !!                     (ji-1,jj-1)     (ji,jj-1)
+      !!
+      !! ** Inputs  : - wind forcing (stress), oceanic currents
+      !!                ice total volume (vt_i) per unit area
+      !!                snow total volume (vt_s) per unit area
+      !!
+      !! ** Action  : - compute u_ice, v_ice : the components of the 
+      !!                sea-ice velocity vector
+      !!              - compute delta_i, shear_i, divu_i, which are inputs
+      !!                of the ice thickness distribution
+      !!
+      !! ** Steps   : 0) compute mask at F point
+      !!              1) Compute ice snow mass, ice strength 
+      !!              2) Compute wind, oceanic stresses, mass terms and
+      !!                 coriolis terms of the momentum equation
+      !!              3) Solve the momentum equation (iterative procedure)
+      !!              4) Recompute delta, shear and divergence
+      !!                 (which are inputs of the ITD) & store stress
+      !!                 for the next time step
+      !!              5) Diagnostics including charge ellipse
+      !!
+      !! ** Notes   : There is the possibility to use aEVP from the nice work of Kimmritz et al. (2016 & 2017)
+      !!              by setting up ln_aEVP=T (i.e. changing alpha and beta parameters).
+      !!              This is an upgraded version of mEVP from Bouillon et al. 2013
+      !!              (i.e. more stable and better convergence)
+      !!
+      !! References : Hunke and Dukowicz, JPO97
+      !!              Bouillon et al., Ocean Modelling 2009
+      !!              Bouillon et al., Ocean Modelling 2013
+      !!              Kimmritz et al., Ocean Modelling 2016 & 2017
+      !!-------------------------------------------------------------------
+      INTEGER                 , INTENT(in   ) ::   kt                                    ! time step
+      INTEGER                 , INTENT(in   ) ::   Kmm                                   ! ocean time level index
+      REAL(wp), DIMENSION(:,:), INTENT(inout) ::   pstress1_i, pstress2_i, pstress12_i   !
+      REAL(wp), DIMENSION(:,:), INTENT(  out) ::   pshear_i  , pdivu_i   , pdelta_i      !
+      REAL(wp), DIMENSION(:,:), INTENT(inout) ::   paniso_11 , paniso_12                 ! structure tensor components
+      REAL(wp), DIMENSION(:,:), INTENT(inout) ::   prdg_conv                             ! for ridging
+      !!
+      INTEGER ::   ji, jj       ! dummy loop indices
+      INTEGER ::   jter         ! local integers
+      !
+      REAL(wp) ::   zrhoco                                              ! rau0 * rn_cio
+      REAL(wp) ::   zdtevp, z1_dtevp                                    ! time step for subcycling
+      REAL(wp) ::   ecc2, z1_ecc2                                       ! square of yield ellipse eccenticity
+      REAL(wp) ::   zalph1, z1_alph1, zalph2, z1_alph2                  ! alpha coef from Bouillon 2009 or Kimmritz 2017
+      REAl(wp) ::   zbetau, zbetav
+      REAL(wp) ::   zm1, zm2, zm3, zmassU, zmassV, zvU, zvV             ! ice/snow mass and volume
+      REAL(wp) ::   zds2, zdt, zdt2, zdiv, zdiv2, zdsT                  ! temporary scalars
+      REAL(wp) ::   zTauO, zTauB, zRHS, zvel                            ! temporary scalars
+      REAL(wp) ::   zkt                                                 ! isotropic tensile strength for landfast ice
+      REAL(wp) ::   zvCr                                                ! critical ice volume above which ice is landfast
+      !
+      REAL(wp) ::   zintb, zintn                                        ! dummy argument
+      REAL(wp) ::   zfac_x, zfac_y
+      REAL(wp) ::   zshear, zdum1, zdum2
+      REAL(wp) ::   zstressptmp, zstressmtmp, zstress12tmpF             ! anisotropic stress tensor components
+      REAL(wp) ::   zalphar, zalphas                                    ! for mechanical redistribution
+      REAL(wp) ::   zmresult11, zmresult12, z1dtevpkth, zp5kth, z1_dtevp_A  ! for structure tensor evolution
+      REAL(wp) ::   zinvw                                                ! for test case
+
+      !
+      REAL(wp), DIMENSION(jpi,jpj) ::   zstress12tmp                    ! anisotropic stress tensor component for regridding
+      REAL(wp), DIMENSION(jpi,jpj) ::   zyield11, zyield22, zyield12    ! yield surface tensor for history
+      REAL(wp), DIMENSION(jpi,jpj) ::   zdelta, zp_delt                 ! delta and P/delta at T points
+      REAL(wp), DIMENSION(jpi,jpj) ::   zten_i                          ! tension
+      REAL(wp), DIMENSION(jpi,jpj) ::   zbeta                           ! beta coef from Kimmritz 2017
+      !
+      REAL(wp), DIMENSION(jpi,jpj) ::   zdt_m                           ! (dt / ice-snow_mass) on T points
+      REAL(wp), DIMENSION(jpi,jpj) ::   zaU  , zaV                      ! ice fraction on U/V points
+      REAL(wp), DIMENSION(jpi,jpj) ::   zmU_t, zmV_t                    ! (ice-snow_mass / dt) on U/V points
+      REAL(wp), DIMENSION(jpi,jpj) ::   zmf                             ! coriolis parameter at T points
+      REAL(wp), DIMENSION(jpi,jpj) ::   v_oceU, u_oceV, v_iceU, u_iceV  ! ocean/ice u/v component on V/U points                           
+      !
+      REAL(wp), DIMENSION(jpi,jpj) ::   zds                             ! shear
+      REAL(wp), DIMENSION(jpi,jpj) ::   zs1, zs2, zs12                  ! stress tensor components
+      REAL(wp), DIMENSION(jpi,jpj) ::   zsshdyn                         ! array used for the calculation of ice surface slope:
+      !                                                                 !    ocean surface (ssh_m) if ice is not embedded
+      !                                                                 !    ice bottom surface if ice is embedded   
+      REAL(wp), DIMENSION(jpi,jpj) ::   zfU  , zfV                      ! internal stresses
+      REAL(wp), DIMENSION(jpi,jpj) ::   zspgU, zspgV                    ! surface pressure gradient at U/V points
+      REAL(wp), DIMENSION(jpi,jpj) ::   zCorU, zCorV                    ! Coriolis stress array
+      REAL(wp), DIMENSION(jpi,jpj) ::   ztaux_ai, ztauy_ai              ! ice-atm. stress at U-V points
+      REAL(wp), DIMENSION(jpi,jpj) ::   ztaux_oi, ztauy_oi              ! ice-ocean stress at U-V points
+      REAL(wp), DIMENSION(jpi,jpj) ::   ztaux_bi, ztauy_bi              ! ice-OceanBottom stress at U-V points (landfast)
+      REAL(wp), DIMENSION(jpi,jpj) ::   ztaux_base, ztauy_base          ! ice-bottom stress at U-V points (landfast)
+      !
+      REAL(wp), DIMENSION(jpi,jpj) ::   zmsk01x, zmsk01y                ! dummy arrays
+      REAL(wp), DIMENSION(jpi,jpj) ::   zmsk00x, zmsk00y                ! mask for ice presence
+      REAL(wp), DIMENSION(jpi,jpj) ::   zfmask                          ! mask at F points for the ice
+
+      REAL(wp), PARAMETER          ::   zepsi  = 1.0e-20_wp             ! tolerance parameter
+      REAL(wp), PARAMETER          ::   zmmin  = 1._wp                  ! ice mass (kg/m2)  below which ice velocity becomes very small
+      REAL(wp), PARAMETER          ::   zamin  = 0.001_wp               ! ice concentration below which ice velocity becomes very small
+      !! --- check convergence
+      REAL(wp), DIMENSION(jpi,jpj) ::   zu_ice, zv_ice
+      !! --- diags
+      REAL(wp) ::   zsig1, zsig2, zsig12, zfac, z1_strength
+      REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   zsig_I, zsig_II, zsig1_p, zsig2_p         
+      !! --- SIMIP diags
+      REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   zdiag_xmtrp_ice ! X-component of ice mass transport (kg/s)
+      REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   zdiag_ymtrp_ice ! Y-component of ice mass transport (kg/s)
+      REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   zdiag_xmtrp_snw ! X-component of snow mass transport (kg/s)
+      REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   zdiag_ymtrp_snw ! Y-component of snow mass transport (kg/s)
+      REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   zdiag_xatrp     ! X-component of area transport (m2/s)
+      REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   zdiag_yatrp     ! Y-component of area transport (m2/s)      
+      !!-------------------------------------------------------------------
+
+      IF( kt == nit000 .AND. lwp )   WRITE(numout,*) '-- ice_dyn_rhg_eap: EAP sea-ice rheology'
+      !
+      ! for diagnostics and convergence tests
+      ALLOCATE( zmsk00(jpi,jpj), zmsk15(jpi,jpj) )
+      DO_2D( 1, 1, 1, 1 )
+         zmsk00(ji,jj) = MAX( 0._wp , SIGN( 1._wp , at_i(ji,jj) - epsi06  ) ) ! 1 if ice    , 0 if no ice
+         zmsk15(ji,jj) = MAX( 0._wp , SIGN( 1._wp , at_i(ji,jj) - 0.15_wp ) ) ! 1 if 15% ice, 0 if less
+      END_2D
+      !
+!!gm for Clem:  OPTIMIZATION:  I think zfmask can be computed one for all at the initialization....
+      !------------------------------------------------------------------------------!
+      ! 0) mask at F points for the ice
+      !------------------------------------------------------------------------------!
+      ! ocean/land mask
+      DO_2D( 1, 0, 1, 0 )
+         zfmask(ji,jj) = tmask(ji,jj,1) * tmask(ji+1,jj,1) * tmask(ji,jj+1,1) * tmask(ji+1,jj+1,1)
+      END_2D
+      CALL lbc_lnk( 'icedyn_rhg_eap', zfmask, 'F', 1._wp )
+
+      ! Lateral boundary conditions on velocity (modify zfmask)
+      DO_2D( 0, 0, 0, 0 )
+         IF( zfmask(ji,jj) == 0._wp ) THEN
+            zfmask(ji,jj) = rn_ishlat * MIN( 1._wp , MAX( umask(ji,jj,1), umask(ji,jj+1,1), &
+               &                                          vmask(ji,jj,1), vmask(ji+1,jj,1) ) )
+         ENDIF
+      END_2D
+      DO jj = 2, jpjm1
+         IF( zfmask(1,jj) == 0._wp ) THEN
+            zfmask(1  ,jj) = rn_ishlat * MIN( 1._wp , MAX( vmask(2,jj,1), umask(1,jj+1,1), umask(1,jj,1) ) )
+         ENDIF
+         IF( zfmask(jpi,jj) == 0._wp ) THEN
+            zfmask(jpi,jj) = rn_ishlat * MIN( 1._wp , MAX( umask(jpi,jj+1,1), vmask(jpim1,jj,1), umask(jpi,jj-1,1) ) )
+         ENDIF
+      END DO
+      DO ji = 2, jpim1
+         IF( zfmask(ji,1) == 0._wp ) THEN
+            zfmask(ji, 1 ) = rn_ishlat * MIN( 1._wp , MAX( vmask(ji+1,1,1), umask(ji,2,1), vmask(ji,1,1) ) )
+         ENDIF
+         IF( zfmask(ji,jpj) == 0._wp ) THEN
+            zfmask(ji,jpj) = rn_ishlat * MIN( 1._wp , MAX( vmask(ji+1,jpj,1), vmask(ji-1,jpj,1), umask(ji,jpjm1,1) ) )
+         ENDIF
+      END DO
+      CALL lbc_lnk( 'icedyn_rhg_eap', zfmask, 'F', 1.0_wp )
+
+      !------------------------------------------------------------------------------!
+      ! 1) define some variables and initialize arrays
+      !------------------------------------------------------------------------------!
+      zrhoco = rho0 * rn_cio 
+!extra code for test case boundary conditions
+      zinvw=1._wp/(zrhoco*0.5_wp)
+
+      ! ecc2: square of yield ellipse eccenticrity
+      ecc2    = rn_ecc * rn_ecc
+      z1_ecc2 = 1._wp / ecc2
+
+      ! alpha parameters (Bouillon 2009)
+      IF( .NOT. ln_aEVP ) THEN
+         zdtevp   = rDt_ice / REAL( nn_nevp )
+         zalph1 =   2._wp * rn_relast * REAL( nn_nevp )
+         zalph2 = zalph1 * z1_ecc2
+
+         z1_alph1 = 1._wp / ( zalph1 + 1._wp )
+         z1_alph2 = 1._wp / ( zalph2 + 1._wp )
+      ELSE
+         zdtevp   = rdt_ice
+         ! zalpha parameters set later on adaptatively
+      ENDIF
+      z1_dtevp = 1._wp / zdtevp
+         
+      ! Initialise stress tensor 
+      zs1 (:,:) = pstress1_i (:,:) 
+      zs2 (:,:) = pstress2_i (:,:)
+      zs12(:,:) = pstress12_i(:,:)
+
+      ! constants for structure tensor
+      z1_dtevp_A = z1_dtevp/10.0_wp
+      z1dtevpkth = 1._wp / (z1_dtevp_A + 0.00002_wp)
+      zp5kth = 0.5_wp * 0.00002_wp
+
+      ! Ice strength
+      CALL ice_strength
+
+      ! landfast param from Lemieux(2016): add isotropic tensile strength (following Konig Beatty and Holland, 2010)
+      IF( ln_landfast_L16 ) THEN   ;   zkt = rn_lf_tensile
+      ELSE                         ;   zkt = 0._wp
+      ENDIF
+      !
+      !------------------------------------------------------------------------------!
+      ! 2) Wind / ocean stress, mass terms, coriolis terms
+      !------------------------------------------------------------------------------!
+      ! sea surface height
+      !    embedded sea ice: compute representative ice top surface
+      !    non-embedded sea ice: use ocean surface for slope calculation
+      zsshdyn(:,:) = ice_var_sshdyn( ssh_m, snwice_mass, snwice_mass_b)
+
+      DO_2D( 0, 0, 0, 0 )
+
+         ! ice fraction at U-V points
+         zaU(ji,jj) = 0.5_wp * ( at_i(ji,jj) * e1e2t(ji,jj) + at_i(ji+1,jj) * e1e2t(ji+1,jj) ) * r1_e1e2u(ji,jj) * umask(ji,jj,1)
+         zaV(ji,jj) = 0.5_wp * ( at_i(ji,jj) * e1e2t(ji,jj) + at_i(ji,jj+1) * e1e2t(ji,jj+1) ) * r1_e1e2v(ji,jj) * vmask(ji,jj,1)
+
+         ! Ice/snow mass at U-V points
+         zm1 = ( rhos * vt_s(ji  ,jj  ) + rhoi * vt_i(ji  ,jj  ) )
+         zm2 = ( rhos * vt_s(ji+1,jj  ) + rhoi * vt_i(ji+1,jj  ) )
+         zm3 = ( rhos * vt_s(ji  ,jj+1) + rhoi * vt_i(ji  ,jj+1) )
+         zmassU = 0.5_wp * ( zm1 * e1e2t(ji,jj) + zm2 * e1e2t(ji+1,jj) ) * r1_e1e2u(ji,jj) * umask(ji,jj,1)
+         zmassV = 0.5_wp * ( zm1 * e1e2t(ji,jj) + zm3 * e1e2t(ji,jj+1) ) * r1_e1e2v(ji,jj) * vmask(ji,jj,1)
+
+         ! Ocean currents at U-V points
+         v_oceU(ji,jj)   = 0.25_wp * ( v_oce(ji,jj) + v_oce(ji,jj-1) + v_oce(ji+1,jj) + v_oce(ji+1,jj-1) ) * umask(ji,jj,1)
+         u_oceV(ji,jj)   = 0.25_wp * ( u_oce(ji,jj) + u_oce(ji-1,jj) + u_oce(ji,jj+1) + u_oce(ji-1,jj+1) ) * vmask(ji,jj,1)
+
+         ! Coriolis at T points (m*f)
+         zmf(ji,jj)      = zm1 * ff_t(ji,jj)
+
+         ! dt/m at T points (for alpha and beta coefficients)
+         zdt_m(ji,jj)    = zdtevp / MAX( zm1, zmmin )
+         
+         ! m/dt
+         zmU_t(ji,jj)    = zmassU * z1_dtevp
+         zmV_t(ji,jj)    = zmassV * z1_dtevp
+         
+         ! Drag ice-atm.
+         ztaux_ai(ji,jj) = zaU(ji,jj) * utau_ice(ji,jj)
+         ztauy_ai(ji,jj) = zaV(ji,jj) * vtau_ice(ji,jj)
+
+         ! Surface pressure gradient (- m*g*GRAD(ssh)) at U-V points
+         zspgU(ji,jj)    = - zmassU * grav * ( zsshdyn(ji+1,jj) - zsshdyn(ji,jj) ) * r1_e1u(ji,jj)
+         zspgV(ji,jj)    = - zmassV * grav * ( zsshdyn(ji,jj+1) - zsshdyn(ji,jj) ) * r1_e2v(ji,jj)
+
+         ! masks
+         zmsk00x(ji,jj) = 1._wp - MAX( 0._wp, SIGN( 1._wp, -zmassU ) )  ! 0 if no ice
+         zmsk00y(ji,jj) = 1._wp - MAX( 0._wp, SIGN( 1._wp, -zmassV ) )  ! 0 if no ice
+
+         ! switches
+         IF( zmassU <= zmmin .AND. zaU(ji,jj) <= zamin ) THEN   ;   zmsk01x(ji,jj) = 0._wp
+         ELSE                                                   ;   zmsk01x(ji,jj) = 1._wp   ;   ENDIF
+         IF( zmassV <= zmmin .AND. zaV(ji,jj) <= zamin ) THEN   ;   zmsk01y(ji,jj) = 0._wp
+         ELSE                                                   ;   zmsk01y(ji,jj) = 1._wp   ;   ENDIF
+
+      END_2D
+      CALL lbc_lnk_multi( 'icedyn_rhg_eap', zmf, 'T', 1.0_wp, zdt_m, 'T', 1.0_wp )
+      !
+      !                                  !== Landfast ice parameterization ==!
+      !
+      IF( ln_landfast_L16 ) THEN         !-- Lemieux 2016
+         DO_2D( 0, 0, 0, 0 )
+            ! ice thickness at U-V points
+            zvU = 0.5_wp * ( vt_i(ji,jj) * e1e2t(ji,jj) + vt_i(ji+1,jj) * e1e2t(ji+1,jj) ) * r1_e1e2u(ji,jj) * umask(ji,jj,1)
+            zvV = 0.5_wp * ( vt_i(ji,jj) * e1e2t(ji,jj) + vt_i(ji,jj+1) * e1e2t(ji,jj+1) ) * r1_e1e2v(ji,jj) * vmask(ji,jj,1)
+            ! ice-bottom stress at U points
+            zvCr = zaU(ji,jj) * rn_lf_depfra * hu(ji,jj,Kmm)
+            ztaux_base(ji,jj) = - rn_lf_bfr * MAX( 0._wp, zvU - zvCr ) * EXP( -rn_crhg * ( 1._wp - zaU(ji,jj) ) )
+            ! ice-bottom stress at V points
+            zvCr = zaV(ji,jj) * rn_lf_depfra * hv(ji,jj,Kmm)
+            ztauy_base(ji,jj) = - rn_lf_bfr * MAX( 0._wp, zvV - zvCr ) * EXP( -rn_crhg * ( 1._wp - zaV(ji,jj) ) )
+            ! ice_bottom stress at T points
+            zvCr = at_i(ji,jj) * rn_lf_depfra * ht(ji,jj)
+            tau_icebfr(ji,jj) = - rn_lf_bfr * MAX( 0._wp, vt_i(ji,jj) - zvCr ) * EXP( -rn_crhg * ( 1._wp - at_i(ji,jj) ) )
+         END_2D
+         CALL lbc_lnk( 'icedyn_rhg_eap', tau_icebfr(:,:), 'T', 1.0_wp )
+         !
+      ELSE                               !-- no landfast
+         DO_2D( 0, 0, 0, 0 )
+            ztaux_base(ji,jj) = 0._wp
+            ztauy_base(ji,jj) = 0._wp
+         END_2D
+      ENDIF
+
+      !------------------------------------------------------------------------------!
+      ! 3) Solution of the momentum equation, iterative procedure
+      !------------------------------------------------------------------------------!
+      !
+      !                                               ! ==================== !
+      DO jter = 1 , nn_nevp                           !    loop over jter    !
+         !                                            ! ==================== !        
+         l_full_nf_update = jter == nn_nevp   ! false: disable full North fold update (performances) for iter = 1 to nn_nevp-1
+         !
+         ! convergence test
+         IF( nn_rhg_chkcvg == 1 .OR. nn_rhg_chkcvg == 2  ) THEN
+            DO_2D( nn_hls, nn_hls, nn_hls, nn_hls )
+               zu_ice(ji,jj) = u_ice(ji,jj) * umask(ji,jj,1) ! velocity at previous time step
+               zv_ice(ji,jj) = v_ice(ji,jj) * vmask(ji,jj,1)
+            END_2D
+         ENDIF
+
+         ! --- divergence, tension & shear (Appendix B of Hunke & Dukowicz, 2002) --- !
+         DO_2D( 1, 0, 1, 0 )
+
+            ! shear at F points
+            zds(ji,jj) = ( ( u_ice(ji,jj+1) * r1_e1u(ji,jj+1) - u_ice(ji,jj) * r1_e1u(ji,jj) ) * e1f(ji,jj) * e1f(ji,jj)   &
+               &         + ( v_ice(ji+1,jj) * r1_e2v(ji+1,jj) - v_ice(ji,jj) * r1_e2v(ji,jj) ) * e2f(ji,jj) * e2f(ji,jj)   &
+               &         ) * r1_e1e2f(ji,jj) * zfmask(ji,jj)
+
+         END_2D
+
+         DO_2D( 0, 0, 0, 0 )
+
+            ! shear**2 at T points (doc eq. A16)
+            zds2 = ( zds(ji,jj  ) * zds(ji,jj  ) * e1e2f(ji,jj  ) + zds(ji-1,jj  ) * zds(ji-1,jj  ) * e1e2f(ji-1,jj  )  &
+               &   + zds(ji,jj-1) * zds(ji,jj-1) * e1e2f(ji,jj-1) + zds(ji-1,jj-1) * zds(ji-1,jj-1) * e1e2f(ji-1,jj-1)  &
+               &   ) * 0.25_wp * r1_e1e2t(ji,jj)
+           
+            ! divergence at T points
+            zdiv  = ( e2u(ji,jj) * u_ice(ji,jj) - e2u(ji-1,jj) * u_ice(ji-1,jj)   &
+               &    + e1v(ji,jj) * v_ice(ji,jj) - e1v(ji,jj-1) * v_ice(ji,jj-1)   &
+               &    ) * r1_e1e2t(ji,jj)
+            zdiv2 = zdiv * zdiv
+            
+            ! tension at T points
+            zdt  = ( ( u_ice(ji,jj) * r1_e2u(ji,jj) - u_ice(ji-1,jj) * r1_e2u(ji-1,jj) ) * e2t(ji,jj) * e2t(ji,jj)   &
+               &   - ( v_ice(ji,jj) * r1_e1v(ji,jj) - v_ice(ji,jj-1) * r1_e1v(ji,jj-1) ) * e1t(ji,jj) * e1t(ji,jj)   &
+               &   ) * r1_e1e2t(ji,jj)
+            zdt2 = zdt * zdt
+
+            ! delta at T points
+            zdelta(ji,jj) = SQRT( zdiv2 + ( zdt2 + zds2 ) * z1_ecc2 )  
+
+         END_2D
+         CALL lbc_lnk( 'icedyn_rhg_eap', zdelta, 'T', 1.0_wp )
+               
+         ! P/delta at T points
+         DO_2D( 1, 1, 1, 1 )
+            zp_delt(ji,jj) = strength(ji,jj) / ( zdelta(ji,jj) + rn_creepl )
+         END_2D
+
+         DO_2D( 0, 1, 0, 1 )   ! loop ends at jpi,jpj so that no lbc_lnk are needed for zs1 and zs2
+
+             ! shear at T points 
+            zdsT = ( zds(ji,jj  ) * e1e2f(ji,jj  ) + zds(ji-1,jj  ) * e1e2f(ji-1,jj  )  &
+               &   + zds(ji,jj-1) * e1e2f(ji,jj-1) + zds(ji-1,jj-1) * e1e2f(ji-1,jj-1)  &
+               &   ) * 0.25_wp * r1_e1e2t(ji,jj)
+            
+           ! divergence at T points (duplication to avoid communications)
+            zdiv  = ( e2u(ji,jj) * u_ice(ji,jj) - e2u(ji-1,jj) * u_ice(ji-1,jj)   &
+               &    + e1v(ji,jj) * v_ice(ji,jj) - e1v(ji,jj-1) * v_ice(ji,jj-1)   &
+               &    ) * r1_e1e2t(ji,jj)
+            
+            ! tension at T points (duplication to avoid communications)
+            zdt  = ( ( u_ice(ji,jj) * r1_e2u(ji,jj) - u_ice(ji-1,jj) * r1_e2u(ji-1,jj) ) * e2t(ji,jj) * e2t(ji,jj)   &
+               &   - ( v_ice(ji,jj) * r1_e1v(ji,jj) - v_ice(ji,jj-1) * r1_e1v(ji,jj-1) ) * e1t(ji,jj) * e1t(ji,jj)   &
+               &   ) * r1_e1e2t(ji,jj)
+
+            ! --- anisotropic stress calculation --- !
+            CALL update_stress_rdg (jter, nn_nevp, zdiv, zdt, zdsT, paniso_11(ji,jj), paniso_12(ji,jj), &
+                                    zstressptmp, zstressmtmp, zstress12tmp(ji,jj), strength(ji,jj), zalphar, zalphas)
+
+            ! structure tensor update
+               CALL calc_ffrac(zstressptmp, zstressmtmp, zstress12tmp(ji,jj), paniso_11(ji,jj), paniso_12(ji,jj), zmresult11,  zmresult12)
+
+               paniso_11(ji,jj) = (paniso_11(ji,jj)  + 0.5*2.e-5*zdtevp + zmresult11*zdtevp) / (1. + 2.e-5*zdtevp) ! implicit
+               paniso_12(ji,jj) = (paniso_12(ji,jj)                     + zmresult12*zdtevp) / (1. + 2.e-5*zdtevp) ! implicit
+
+            IF (jter == nn_nevp) THEN
+               zyield11(ji,jj) = 0.5_wp * (zstressptmp + zstressmtmp)
+               zyield22(ji,jj) = 0.5_wp * (zstressptmp - zstressmtmp)
+               zyield12(ji,jj) = zstress12tmp(ji,jj)
+               prdg_conv(ji,jj) = -min( zalphar, 0._wp)    
+            ENDIF
+
+            ! alpha for aEVP
+            !   gamma = 0.5*P/(delta+creepl) * (c*pi)**2/Area * dt/m
+            !   alpha = beta = sqrt(4*gamma)
+            IF( ln_aEVP ) THEN
+               zalph1   = MAX( 50._wp, rpi * SQRT( 0.5_wp * zp_delt(ji,jj) * r1_e1e2t(ji,jj) * zdt_m(ji,jj) ) )
+               z1_alph1 = 1._wp / ( zalph1 + 1._wp )
+               zalph2   = zalph1
+               z1_alph2 = z1_alph1
+               ! explicit:
+               ! z1_alph1 = 1._wp / zalph1
+               ! z1_alph2 = 1._wp / zalph1
+               ! zalph1 = zalph1 - 1._wp
+               ! zalph2 = zalph1
+            ENDIF
+
+            ! stress at T points (zkt/=0 if landfast)
+            zs1(ji,jj) = ( zs1(ji,jj) * zalph1 + zstressptmp ) * z1_alph1
+            zs2(ji,jj) = ( zs2(ji,jj) * zalph1 + zstressmtmp ) * z1_alph1
+         END_2D
+         CALL lbc_lnk_multi( 'icedyn_rhg_eap', zstress12tmp, 'T', 1.0_wp , paniso_11, 'T', 1.0_wp , paniso_12, 'T', 1.0_wp)
+
+        ! Save beta at T-points for further computations
+         IF( ln_aEVP ) THEN
+            DO_2D( 1, 1, 1, 1 )
+               zbeta(ji,jj) = MAX( 50._wp, rpi * SQRT( 0.5_wp * zp_delt(ji,jj) * r1_e1e2t(ji,jj) * zdt_m(ji,jj) ) )
+            END_2D
+         ENDIF
+
+         DO_2D( 1, 0, 1, 0 )
+            ! stress12tmp at F points 
+            zstress12tmpF = ( zstress12tmp(ji,jj+1) * e1e2t(ji,jj+1) + zstress12tmp(ji+1,jj+1) * e1e2t(ji+1,jj+1)  &
+               &            + zstress12tmp(ji,jj  ) * e1e2t(ji,jj  ) + zstress12tmp(ji+1,jj  ) * e1e2t(ji+1,jj  )  &
+               &            ) * 0.25_wp * r1_e1e2f(ji,jj)
+
+            ! alpha for aEVP
+            IF( ln_aEVP ) THEN
+               zalph2   = MAX( zbeta(ji,jj), zbeta(ji+1,jj), zbeta(ji,jj+1), zbeta(ji+1,jj+1) )
+               z1_alph2 = 1._wp / ( zalph2 + 1._wp )
+               ! explicit:
+               ! z1_alph2 = 1._wp / zalph2
+               ! zalph2 = zalph2 - 1._wp
+            ENDIF
+            
+            ! stress at F points (zkt/=0 if landfast)
+            zs12(ji,jj) = ( zs12(ji,jj) * zalph1 + zstress12tmpF ) * z1_alph1
+
+         END_2D
+         CALL lbc_lnk_multi( 'icedyn_rhg_eap', zs1, 'T', 1.0_wp, zs2, 'T', 1.0_wp, zs12, 'F', 1.0_wp )
+
+         ! --- Ice internal stresses (Appendix C of Hunke and Dukowicz, 2002) --- !
+         DO_2D( 0, 0, 0, 0 )
+            !                   !--- U points
+            zfU(ji,jj) = 0.5_wp * ( ( zs1(ji+1,jj) - zs1(ji,jj) ) * e2u(ji,jj)                                             &
+               &                  + ( zs2(ji+1,jj) * e2t(ji+1,jj) * e2t(ji+1,jj) - zs2(ji,jj) * e2t(ji,jj) * e2t(ji,jj)    &
+               &                    ) * r1_e2u(ji,jj)                                                                      &
+               &                  + ( zs12(ji,jj) * e1f(ji,jj) * e1f(ji,jj) - zs12(ji,jj-1) * e1f(ji,jj-1) * e1f(ji,jj-1)  &
+               &                    ) * 2._wp * r1_e1u(ji,jj)                                                              &
+               &                  ) * r1_e1e2u(ji,jj)
+            !
+            !                !--- V points
+            zfV(ji,jj) = 0.5_wp * ( ( zs1(ji,jj+1) - zs1(ji,jj) ) * e1v(ji,jj)                                             &
+               &                  - ( zs2(ji,jj+1) * e1t(ji,jj+1) * e1t(ji,jj+1) - zs2(ji,jj) * e1t(ji,jj) * e1t(ji,jj)    &
+               &                    ) * r1_e1v(ji,jj)                                                                      &
+               &                  + ( zs12(ji,jj) * e2f(ji,jj) * e2f(ji,jj) - zs12(ji-1,jj) * e2f(ji-1,jj) * e2f(ji-1,jj)  &
+               &                    ) * 2._wp * r1_e2v(ji,jj)                                                              &
+               &                  ) * r1_e1e2v(ji,jj)
+            !
+            !                !--- ice currents at U-V point
+            v_iceU(ji,jj) = 0.25_wp * ( v_ice(ji,jj) + v_ice(ji,jj-1) + v_ice(ji+1,jj) + v_ice(ji+1,jj-1) ) * umask(ji,jj,1)
+            u_iceV(ji,jj) = 0.25_wp * ( u_ice(ji,jj) + u_ice(ji-1,jj) + u_ice(ji,jj+1) + u_ice(ji-1,jj+1) ) * vmask(ji,jj,1)
+            !
+         END_2D
+         !
+         ! --- Computation of ice velocity --- !
+         !  Bouillon et al. 2013 (eq 47-48) => unstable unless alpha, beta vary as in Kimmritz 2016 & 2017
+         !  Bouillon et al. 2009 (eq 34-35) => stable
+         IF( MOD(jter,2) == 0 ) THEN ! even iterations
+            !
+            DO_2D( 0, 0, 0, 0 )
+               !                 !--- tau_io/(v_oce - v_ice)
+               zTauO = zaV(ji,jj) * zrhoco * SQRT( ( v_ice (ji,jj) - v_oce (ji,jj) ) * ( v_ice (ji,jj) - v_oce (ji,jj) )  &
+                  &                              + ( u_iceV(ji,jj) - u_oceV(ji,jj) ) * ( u_iceV(ji,jj) - u_oceV(ji,jj) ) )
+               !                 !--- Ocean-to-Ice stress
+               ztauy_oi(ji,jj) = zTauO * ( v_oce(ji,jj) - v_ice(ji,jj) )
+               !
+               !                 !--- tau_bottom/v_ice
+               zvel  = 5.e-05_wp + SQRT( v_ice(ji,jj) * v_ice(ji,jj) + u_iceV(ji,jj) * u_iceV(ji,jj) )
+               zTauB = ztauy_base(ji,jj) / zvel
+               !                 !--- OceanBottom-to-Ice stress
+               ztauy_bi(ji,jj) = zTauB * v_ice(ji,jj)
+               !
+               !                 !--- Coriolis at V-points (energy conserving formulation)
+               zCorV(ji,jj)  = - 0.25_wp * r1_e2v(ji,jj) *  &
+                  &    ( zmf(ji,jj  ) * ( e2u(ji,jj  ) * u_ice(ji,jj  ) + e2u(ji-1,jj  ) * u_ice(ji-1,jj  ) )  &
+                  &    + zmf(ji,jj+1) * ( e2u(ji,jj+1) * u_ice(ji,jj+1) + e2u(ji-1,jj+1) * u_ice(ji-1,jj+1) ) )
+               !
+               !                 !--- Sum of external forces (explicit solution) = F + tau_ia + Coriolis + spg + tau_io
+               zRHS = zfV(ji,jj) + ztauy_ai(ji,jj) + zCorV(ji,jj) + zspgV(ji,jj) + ztauy_oi(ji,jj)
+               !
+               !                 !--- landfast switch => 0 = static  friction : TauB > RHS & sign(TauB) /= sign(RHS)
+               !                                         1 = sliding friction : TauB < RHS
+               rswitch = 1._wp - MIN( 1._wp, ABS( SIGN( 1._wp, zRHS + ztauy_base(ji,jj) ) - SIGN( 1._wp, zRHS ) ) )
+               !
+               IF( ln_aEVP ) THEN !--- ice velocity using aEVP (Kimmritz et al 2016 & 2017)
+                  zbetav = MAX( zbeta(ji,jj), zbeta(ji,jj+1) )
+                  v_ice(ji,jj) = ( (          rswitch   * ( zmV_t(ji,jj) * ( zbetav * v_ice(ji,jj) + v_ice_b(ji,jj) )         & ! previous velocity
+                     &                                    + zRHS + zTauO * v_ice(ji,jj)                                       & ! F + tau_ia + Coriolis + spg + tau_io(only ocean part)
+                     &                                    ) / MAX( zepsi, zmV_t(ji,jj) * ( zbetav + 1._wp ) + zTauO - zTauB ) & ! m/dt + tau_io(only ice part) + landfast
+                     &            + ( 1._wp - rswitch ) * (  v_ice_b(ji,jj)                                                   & 
+                     &                                     + v_ice  (ji,jj) * MAX( 0._wp, zbetav - zdtevp * rn_lf_relax )     & ! static friction => slow decrease to v=0
+                     &                                    ) / ( zbetav + 1._wp )                                              &
+                     &             ) * zmsk01y(ji,jj) + v_oce(ji,jj) * 0.01_wp * ( 1._wp - zmsk01y(ji,jj) )                   & ! v_ice = v_oce/100 if mass < zmmin & conc < zamin
+                     &           )   * zmsk00y(ji,jj)
+               ELSE               !--- ice velocity using EVP implicit formulation (cf Madec doc & Bouillon 2009)
+                  v_ice(ji,jj) = ( (         rswitch   * ( zmV_t(ji,jj)  * v_ice(ji,jj)                                       & ! previous velocity
+                     &                                    + zRHS + zTauO * v_ice(ji,jj)                                       & ! F + tau_ia + Coriolis + spg + tau_io(only ocean part)
+                     &                                    ) / MAX( zepsi, zmV_t(ji,jj) + zTauO - zTauB )                      & ! m/dt + tau_io(only ice part) + landfast
+                     &            + ( 1._wp - rswitch ) *   v_ice(ji,jj) * MAX( 0._wp, 1._wp - zdtevp * rn_lf_relax )         & ! static friction => slow decrease to v=0
+                     &              ) * zmsk01y(ji,jj) + v_oce(ji,jj) * 0.01_wp * ( 1._wp - zmsk01y(ji,jj) )                  & ! v_ice = v_oce/100 if mass < zmmin & conc < zamin
+                     &            )   * zmsk00y(ji,jj)
+               ENDIF
+!extra code for test case boundary conditions
+               IF (mjg(jj)<25 .or. mjg(jj)>975 .or. mig(ji)<25 .or. mig(ji)>975) THEN
+                  v_ice(ji,jj) = zinvw*(ztauy_ai(ji,jj) + zCorV(ji,jj) + zspgV(ji,jj) + ztauy_oi(ji,jj))
+               END IF
+
+            END_2D
+            CALL lbc_lnk( 'icedyn_rhg_eap', v_ice, 'V', -1.0_wp )
+            !
+#if defined key_agrif
+!!          CALL agrif_interp_ice( 'V', jter, nn_nevp )
+            CALL agrif_interp_ice( 'V' )
+#endif
+            IF( ln_bdy )   CALL bdy_ice_dyn( 'V' )
+            !
+            DO_2D( 0, 0, 0, 0 )
+               !                 !--- tau_io/(u_oce - u_ice)
+               zTauO = zaU(ji,jj) * zrhoco * SQRT( ( u_ice (ji,jj) - u_oce (ji,jj) ) * ( u_ice (ji,jj) - u_oce (ji,jj) )  &
+                  &                              + ( v_iceU(ji,jj) - v_oceU(ji,jj) ) * ( v_iceU(ji,jj) - v_oceU(ji,jj) ) )
+               !                 !--- Ocean-to-Ice stress
+               ztaux_oi(ji,jj) = zTauO * ( u_oce(ji,jj) - u_ice(ji,jj) )
+               !
+               !                 !--- tau_bottom/u_ice
+               zvel  = 5.e-05_wp + SQRT( v_iceU(ji,jj) * v_iceU(ji,jj) + u_ice(ji,jj) * u_ice(ji,jj) )
+               zTauB = ztaux_base(ji,jj) / zvel
+               !                 !--- OceanBottom-to-Ice stress
+               ztaux_bi(ji,jj) = zTauB * u_ice(ji,jj)
+               !
+               !                 !--- Coriolis at U-points (energy conserving formulation)
+               zCorU(ji,jj)  =   0.25_wp * r1_e1u(ji,jj) *  &
+                  &    ( zmf(ji  ,jj) * ( e1v(ji  ,jj) * v_ice(ji  ,jj) + e1v(ji  ,jj-1) * v_ice(ji  ,jj-1) )  &
+                  &    + zmf(ji+1,jj) * ( e1v(ji+1,jj) * v_ice(ji+1,jj) + e1v(ji+1,jj-1) * v_ice(ji+1,jj-1) ) )
+               !
+               !                 !--- Sum of external forces (explicit solution) = F + tau_ia + Coriolis + spg + tau_io
+               zRHS = zfU(ji,jj) + ztaux_ai(ji,jj) + zCorU(ji,jj) + zspgU(ji,jj) + ztaux_oi(ji,jj)
+               !
+               !                 !--- landfast switch => 0 = static  friction : TauB > RHS & sign(TauB) /= sign(RHS)
+               !                                         1 = sliding friction : TauB < RHS
+               rswitch = 1._wp - MIN( 1._wp, ABS( SIGN( 1._wp, zRHS + ztaux_base(ji,jj) ) - SIGN( 1._wp, zRHS ) ) )
+               !
+               IF( ln_aEVP ) THEN !--- ice velocity using aEVP (Kimmritz et al 2016 & 2017)
+                  zbetau = MAX( zbeta(ji,jj), zbeta(ji+1,jj) )
+                  u_ice(ji,jj) = ( (          rswitch   * ( zmU_t(ji,jj) * ( zbetau * u_ice(ji,jj) + u_ice_b(ji,jj) )         & ! previous velocity
+                     &                                    + zRHS + zTauO * u_ice(ji,jj)                                       & ! F + tau_ia + Coriolis + spg + tau_io(only ocean part)
+                     &                                    ) / MAX( zepsi, zmU_t(ji,jj) * ( zbetau + 1._wp ) + zTauO - zTauB ) & ! m/dt + tau_io(only ice part) + landfast
+                     &            + ( 1._wp - rswitch ) * (  u_ice_b(ji,jj)                                                   &
+                     &                                     + u_ice  (ji,jj) * MAX( 0._wp, zbetau - zdtevp * rn_lf_relax )     & ! static friction => slow decrease to v=0
+                     &                                    ) / ( zbetau + 1._wp )                                              &
+                     &             ) * zmsk01x(ji,jj) + u_oce(ji,jj) * 0.01_wp * ( 1._wp - zmsk01x(ji,jj) )                   & ! v_ice = v_oce/100 if mass < zmmin & conc < zamin 
+                     &           )   * zmsk00x(ji,jj)
+               ELSE               !--- ice velocity using EVP implicit formulation (cf Madec doc & Bouillon 2009)
+                  u_ice(ji,jj) = ( (         rswitch   * ( zmU_t(ji,jj)  * u_ice(ji,jj)                                       & ! previous velocity
+                     &                                    + zRHS + zTauO * u_ice(ji,jj)                                       & ! F + tau_ia + Coriolis + spg + tau_io(only ocean part)
+                     &                                    ) / MAX( zepsi, zmU_t(ji,jj) + zTauO - zTauB )                      & ! m/dt + tau_io(only ice part) + landfast
+                     &            + ( 1._wp - rswitch ) *   u_ice(ji,jj) * MAX( 0._wp, 1._wp - zdtevp * rn_lf_relax )         & ! static friction => slow decrease to v=0
+                     &              ) * zmsk01x(ji,jj) + u_oce(ji,jj) * 0.01_wp * ( 1._wp - zmsk01x(ji,jj) )                  & ! v_ice = v_oce/100 if mass < zmmin & conc < zamin 
+                     &            )   * zmsk00x(ji,jj)
+               ENDIF
+!extra code for test case boundary conditions
+               IF (mjg(jj)<25 .or. mjg(jj)>975 .or. mig(ji)<25 .or. mig(ji)>975) THEN
+                  u_ice(ji,jj) = zinvw*(ztaux_ai(ji,jj) + zCorU(ji,jj) + zspgU(ji,jj) + ztaux_oi(ji,jj))
+               END IF
+
+            END_2D
+            CALL lbc_lnk( 'icedyn_rhg_eap', u_ice, 'U', -1.0_wp )
+            !
+#if defined key_agrif
+!!          CALL agrif_interp_ice( 'U', jter, nn_nevp )
+            CALL agrif_interp_ice( 'U' )
+#endif
+            IF( ln_bdy )   CALL bdy_ice_dyn( 'U' )
+            !
+         ELSE ! odd iterations
+            !
+            DO_2D( 0, 0, 0, 0 )
+               !                 !--- tau_io/(u_oce - u_ice)
+               zTauO = zaU(ji,jj) * zrhoco * SQRT( ( u_ice (ji,jj) - u_oce (ji,jj) ) * ( u_ice (ji,jj) - u_oce (ji,jj) )  &
+                  &                              + ( v_iceU(ji,jj) - v_oceU(ji,jj) ) * ( v_iceU(ji,jj) - v_oceU(ji,jj) ) )
+               !                 !--- Ocean-to-Ice stress
+               ztaux_oi(ji,jj) = zTauO * ( u_oce(ji,jj) - u_ice(ji,jj) )
+               !
+               !                 !--- tau_bottom/u_ice
+               zvel  = 5.e-05_wp + SQRT( v_iceU(ji,jj) * v_iceU(ji,jj) + u_ice(ji,jj) * u_ice(ji,jj) )
+               zTauB = ztaux_base(ji,jj) / zvel
+               !                 !--- OceanBottom-to-Ice stress
+               ztaux_bi(ji,jj) = zTauB * u_ice(ji,jj)
+               !
+               !                 !--- Coriolis at U-points (energy conserving formulation)
+               zCorU(ji,jj)  =   0.25_wp * r1_e1u(ji,jj) *  &
+                  &    ( zmf(ji  ,jj) * ( e1v(ji  ,jj) * v_ice(ji  ,jj) + e1v(ji  ,jj-1) * v_ice(ji  ,jj-1) )  &
+                  &    + zmf(ji+1,jj) * ( e1v(ji+1,jj) * v_ice(ji+1,jj) + e1v(ji+1,jj-1) * v_ice(ji+1,jj-1) ) )
+               !
+               !                 !--- Sum of external forces (explicit solution) = F + tau_ia + Coriolis + spg + tau_io
+               zRHS = zfU(ji,jj) + ztaux_ai(ji,jj) + zCorU(ji,jj) + zspgU(ji,jj) + ztaux_oi(ji,jj)
+               !
+               !                 !--- landfast switch => 0 = static  friction : TauB > RHS & sign(TauB) /= sign(RHS)
+               !                                         1 = sliding friction : TauB < RHS
+               rswitch = 1._wp - MIN( 1._wp, ABS( SIGN( 1._wp, zRHS + ztaux_base(ji,jj) ) - SIGN( 1._wp, zRHS ) ) )
+               !
+               IF( ln_aEVP ) THEN !--- ice velocity using aEVP (Kimmritz et al 2016 & 2017)
+                  zbetau = MAX( zbeta(ji,jj), zbeta(ji+1,jj) )
+                  u_ice(ji,jj) = ( (          rswitch   * ( zmU_t(ji,jj) * ( zbetau * u_ice(ji,jj) + u_ice_b(ji,jj) )         & ! previous velocity
+                     &                                    + zRHS + zTauO * u_ice(ji,jj)                                       & ! F + tau_ia + Coriolis + spg + tau_io(only ocean part)
+                     &                                    ) / MAX( zepsi, zmU_t(ji,jj) * ( zbetau + 1._wp ) + zTauO - zTauB ) & ! m/dt + tau_io(only ice part) + landfast
+                     &            + ( 1._wp - rswitch ) * (  u_ice_b(ji,jj)                                                   &
+                     &                                     + u_ice  (ji,jj) * MAX( 0._wp, zbetau - zdtevp * rn_lf_relax )     & ! static friction => slow decrease to v=0
+                     &                                    ) / ( zbetau + 1._wp )                                              &
+                     &             ) * zmsk01x(ji,jj) + u_oce(ji,jj) * 0.01_wp * ( 1._wp - zmsk01x(ji,jj) )                   & ! v_ice = v_oce/100 if mass < zmmin & conc < zamin 
+                     &           )   * zmsk00x(ji,jj)
+               ELSE               !--- ice velocity using EVP implicit formulation (cf Madec doc & Bouillon 2009)
+                  u_ice(ji,jj) = ( (         rswitch   * ( zmU_t(ji,jj)  * u_ice(ji,jj)                                       & ! previous velocity
+                     &                                    + zRHS + zTauO * u_ice(ji,jj)                                       & ! F + tau_ia + Coriolis + spg + tau_io(only ocean part)
+                     &                                    ) / MAX( zepsi, zmU_t(ji,jj) + zTauO - zTauB )                      & ! m/dt + tau_io(only ice part) + landfast
+                     &            + ( 1._wp - rswitch ) *   u_ice(ji,jj) * MAX( 0._wp, 1._wp - zdtevp * rn_lf_relax )         & ! static friction => slow decrease to v=0
+                     &              ) * zmsk01x(ji,jj) + u_oce(ji,jj) * 0.01_wp * ( 1._wp - zmsk01x(ji,jj) )                  & ! v_ice = v_oce/100 if mass < zmmin & conc < zamin
+                     &            )   * zmsk00x(ji,jj)
+               ENDIF
+!extra code for test case boundary conditions
+               IF (mjg(jj)<25 .or. mjg(jj)>975 .or. mig(ji)<25 .or. mig(ji)>975) THEN
+                  u_ice(ji,jj) = zinvw*(ztaux_ai(ji,jj) + zCorU(ji,jj) + zspgU(ji,jj) + ztaux_oi(ji,jj))
+               END IF
+            END_2D
+            CALL lbc_lnk( 'icedyn_rhg_eap', u_ice, 'U', -1.0_wp )
+            !
+#if defined key_agrif
+!!          CALL agrif_interp_ice( 'U', jter, nn_nevp )
+            CALL agrif_interp_ice( 'U' )
+#endif
+            IF( ln_bdy )   CALL bdy_ice_dyn( 'U' )
+            !
+            DO_2D( 0, 0, 0, 0 )
+               !                 !--- tau_io/(v_oce - v_ice)
+               zTauO = zaV(ji,jj) * zrhoco * SQRT( ( v_ice (ji,jj) - v_oce (ji,jj) ) * ( v_ice (ji,jj) - v_oce (ji,jj) )  &
+                  &                              + ( u_iceV(ji,jj) - u_oceV(ji,jj) ) * ( u_iceV(ji,jj) - u_oceV(ji,jj) ) )
+               !                 !--- Ocean-to-Ice stress
+               ztauy_oi(ji,jj) = zTauO * ( v_oce(ji,jj) - v_ice(ji,jj) )
+               !
+               !                 !--- tau_bottom/v_ice
+               zvel  = 5.e-05_wp + SQRT( v_ice(ji,jj) * v_ice(ji,jj) + u_iceV(ji,jj) * u_iceV(ji,jj) )
+               zTauB = ztauy_base(ji,jj) / zvel
+               !                 !--- OceanBottom-to-Ice stress
+               ztauy_bi(ji,jj) = zTauB * v_ice(ji,jj)
+               !
+               !                 !--- Coriolis at v-points (energy conserving formulation)
+               zCorV(ji,jj)  = - 0.25_wp * r1_e2v(ji,jj) *  &
+                  &    ( zmf(ji,jj  ) * ( e2u(ji,jj  ) * u_ice(ji,jj  ) + e2u(ji-1,jj  ) * u_ice(ji-1,jj  ) )  &
+                  &    + zmf(ji,jj+1) * ( e2u(ji,jj+1) * u_ice(ji,jj+1) + e2u(ji-1,jj+1) * u_ice(ji-1,jj+1) ) )
+               !
+               !                 !--- Sum of external forces (explicit solution) = F + tau_ia + Coriolis + spg + tau_io
+               zRHS = zfV(ji,jj) + ztauy_ai(ji,jj) + zCorV(ji,jj) + zspgV(ji,jj) + ztauy_oi(ji,jj)
+               !
+               !                 !--- landfast switch => 0 = static  friction : TauB > RHS & sign(TauB) /= sign(RHS)
+               !                                         1 = sliding friction : TauB < RHS
+               rswitch = 1._wp - MIN( 1._wp, ABS( SIGN( 1._wp, zRHS + ztauy_base(ji,jj) ) - SIGN( 1._wp, zRHS ) ) )
+               !
+               IF( ln_aEVP ) THEN !--- ice velocity using aEVP (Kimmritz et al 2016 & 2017)
+                  zbetav = MAX( zbeta(ji,jj), zbeta(ji,jj+1) )
+                  v_ice(ji,jj) = ( (          rswitch   * ( zmV_t(ji,jj) * ( zbetav * v_ice(ji,jj) + v_ice_b(ji,jj) )         & ! previous velocity
+                     &                                    + zRHS + zTauO * v_ice(ji,jj)                                       & ! F + tau_ia + Coriolis + spg + tau_io(only ocean part)
+                     &                                    ) / MAX( zepsi, zmV_t(ji,jj) * ( zbetav + 1._wp ) + zTauO - zTauB ) & ! m/dt + tau_io(only ice part) + landfast
+                     &            + ( 1._wp - rswitch ) * (  v_ice_b(ji,jj)                                                   &
+                     &                                     + v_ice  (ji,jj) * MAX( 0._wp, zbetav - zdtevp * rn_lf_relax )     & ! static friction => slow decrease to v=0
+                     &                                    ) / ( zbetav + 1._wp )                                              & 
+                     &             ) * zmsk01y(ji,jj) + v_oce(ji,jj) * 0.01_wp * ( 1._wp - zmsk01y(ji,jj) )                   & ! v_ice = v_oce/100 if mass < zmmin & conc < zamin
+                     &           )   * zmsk00y(ji,jj)
+               ELSE               !--- ice velocity using EVP implicit formulation (cf Madec doc & Bouillon 2009)
+                  v_ice(ji,jj) = ( (         rswitch   * ( zmV_t(ji,jj)  * v_ice(ji,jj)                                       & ! previous velocity
+                     &                                    + zRHS + zTauO * v_ice(ji,jj)                                       & ! F + tau_ia + Coriolis + spg + tau_io(only ocean part)
+                     &                                    ) / MAX( zepsi, zmV_t(ji,jj) + zTauO - zTauB )                      & ! m/dt + tau_io(only ice part) + landfast
+                     &            + ( 1._wp - rswitch ) *   v_ice(ji,jj) * MAX( 0._wp, 1._wp - zdtevp * rn_lf_relax )         & ! static friction => slow decrease to v=0
+                     &              ) * zmsk01y(ji,jj) + v_oce(ji,jj) * 0.01_wp * ( 1._wp - zmsk01y(ji,jj) )                  & ! v_ice = v_oce/100 if mass < zmmin & conc < zamin
+                     &            )   * zmsk00y(ji,jj)
+               ENDIF
+!extra code for test case boundary conditions
+               IF (mjg(jj)<25 .or. mjg(jj)>975 .or. mig(ji)<25 .or. mig(ji)>975) THEN
+                  v_ice(ji,jj) = zinvw*(ztauy_ai(ji,jj) + zCorV(ji,jj) + zspgV(ji,jj) + ztauy_oi(ji,jj))
+               END IF
+            END_2D
+            CALL lbc_lnk( 'icedyn_rhg_eap', v_ice, 'V', -1.0_wp )
+            !
+#if defined key_agrif
+!!          CALL agrif_interp_ice( 'V', jter, nn_nevp )
+            CALL agrif_interp_ice( 'V' )
+#endif
+            IF( ln_bdy )   CALL bdy_ice_dyn( 'V' )
+            !
+         ENDIF
+
+         ! convergence test
+         IF( nn_rhg_chkcvg == 2 )   CALL rhg_cvg( kt, jter, nn_nevp, u_ice, v_ice, zu_ice, zv_ice )
+         !
+         !                                                ! ==================== !
+      END DO                                              !  end loop over jter  !
+      !                                                   ! ==================== !
+      IF( ln_aEVP )   CALL iom_put( 'beta_evp' , zbeta )
+      !
+      CALL lbc_lnk( 'icedyn_rhg_eap', prdg_conv, 'T', 1.0_wp )  ! only need this in ridging module after rheology completed
+      !
+      !------------------------------------------------------------------------------!
+      ! 4) Recompute delta, shear and div (inputs for mechanical redistribution) 
+      !------------------------------------------------------------------------------!
+      DO_2D( 1, 0, 1, 0 )
+
+         ! shear at F points
+         zds(ji,jj) = ( ( u_ice(ji,jj+1) * r1_e1u(ji,jj+1) - u_ice(ji,jj) * r1_e1u(ji,jj) ) * e1f(ji,jj) * e1f(ji,jj)   &
+            &         + ( v_ice(ji+1,jj) * r1_e2v(ji+1,jj) - v_ice(ji,jj) * r1_e2v(ji,jj) ) * e2f(ji,jj) * e2f(ji,jj)   &
+            &         ) * r1_e1e2f(ji,jj) * zfmask(ji,jj)
+
+      END_2D
+      
+      DO_2D( 0, 0, 0, 0 )
+         
+         ! tension**2 at T points
+         zdt  = ( ( u_ice(ji,jj) * r1_e2u(ji,jj) - u_ice(ji-1,jj) * r1_e2u(ji-1,jj) ) * e2t(ji,jj) * e2t(ji,jj)   &
+            &   - ( v_ice(ji,jj) * r1_e1v(ji,jj) - v_ice(ji,jj-1) * r1_e1v(ji,jj-1) ) * e1t(ji,jj) * e1t(ji,jj)   &
+            &   ) * r1_e1e2t(ji,jj)
+         zdt2 = zdt * zdt
+         
+         zten_i(ji,jj) = zdt
+
+         ! shear**2 at T points (doc eq. A16)
+         zds2 = ( zds(ji,jj  ) * zds(ji,jj  ) * e1e2f(ji,jj  ) + zds(ji-1,jj  ) * zds(ji-1,jj  ) * e1e2f(ji-1,jj  )  &
+            &   + zds(ji,jj-1) * zds(ji,jj-1) * e1e2f(ji,jj-1) + zds(ji-1,jj-1) * zds(ji-1,jj-1) * e1e2f(ji-1,jj-1)  &
+            &   ) * 0.25_wp * r1_e1e2t(ji,jj)
+         
+         ! shear at T points
+         pshear_i(ji,jj) = SQRT( zdt2 + zds2 )
+
+         ! divergence at T points
+         pdivu_i(ji,jj) = ( e2u(ji,jj) * u_ice(ji,jj) - e2u(ji-1,jj) * u_ice(ji-1,jj)   &
+            &             + e1v(ji,jj) * v_ice(ji,jj) - e1v(ji,jj-1) * v_ice(ji,jj-1)   &
+            &             ) * r1_e1e2t(ji,jj)
+         
+         ! delta at T points
+         zfac            = SQRT( pdivu_i(ji,jj) * pdivu_i(ji,jj) + ( zdt2 + zds2 ) * z1_ecc2 ) ! delta  
+         rswitch         = 1._wp - MAX( 0._wp, SIGN( 1._wp, -zfac ) ) ! 0 if delta=0
+         pdelta_i(ji,jj) = zfac + rn_creepl * rswitch ! delta+creepl
+
+      END_2D
+      CALL lbc_lnk_multi( 'icedyn_rhg_eap', pshear_i, 'T', 1.0_wp, pdivu_i, 'T', 1.0_wp, pdelta_i, 'T', 1.0_wp, &
+         &                                    zten_i, 'T', 1.0_wp, zs1    , 'T', 1.0_wp, zs2     , 'T', 1.0_wp, &
+         &                                      zs12, 'F', 1.0_wp )
+      
+      ! --- Store the stress tensor for the next time step --- !
+      pstress1_i (:,:) = zs1 (:,:)
+      pstress2_i (:,:) = zs2 (:,:)
+      pstress12_i(:,:) = zs12(:,:)
+      !
+
+      !------------------------------------------------------------------------------!
+      ! 5) diagnostics
+      !------------------------------------------------------------------------------!
+      ! --- ice-ocean, ice-atm. & ice-oceanbottom(landfast) stresses --- !
+      IF(  iom_use('utau_oi') .OR. iom_use('vtau_oi') .OR. iom_use('utau_ai') .OR. iom_use('vtau_ai') .OR. &
+         & iom_use('utau_bi') .OR. iom_use('vtau_bi') ) THEN
+         !
+         CALL lbc_lnk_multi( 'icedyn_rhg_eap', ztaux_oi, 'U', -1.0_wp, ztauy_oi, 'V', -1.0_wp, ztaux_ai, 'U', -1.0_wp, &
+            &                                  ztauy_ai, 'V', -1.0_wp, ztaux_bi, 'U', -1.0_wp, ztauy_bi, 'V', -1.0_wp )
+         !
+         CALL iom_put( 'utau_oi' , ztaux_oi * zmsk00 )
+         CALL iom_put( 'vtau_oi' , ztauy_oi * zmsk00 )
+         CALL iom_put( 'utau_ai' , ztaux_ai * zmsk00 )
+         CALL iom_put( 'vtau_ai' , ztauy_ai * zmsk00 )
+         CALL iom_put( 'utau_bi' , ztaux_bi * zmsk00 )
+         CALL iom_put( 'vtau_bi' , ztauy_bi * zmsk00 )
+      ENDIF
+       
+      ! --- divergence, shear and strength --- !
+      IF( iom_use('icediv') )   CALL iom_put( 'icediv' , pdivu_i  * zmsk00 )   ! divergence
+      IF( iom_use('iceshe') )   CALL iom_put( 'iceshe' , pshear_i * zmsk00 )   ! shear
+      IF( iom_use('icedlt') )   CALL iom_put( 'icedlt' , pdelta_i * zmsk00 )   ! delta
+      IF( iom_use('icestr') )   CALL iom_put( 'icestr' , strength * zmsk00 )   ! strength
+
+      ! --- Stress tensor invariants (SIMIP diags) --- !
+      IF( iom_use('normstr') .OR. iom_use('sheastr') ) THEN
+         !
+         ALLOCATE( zsig_I(jpi,jpj) , zsig_II(jpi,jpj) )
+         !         
+         DO_2D( 1, 1, 1, 1 )
+         
+            ! Ice stresses
+            ! sigma1, sigma2, sigma12 are some useful recombination of the stresses (Hunke and Dukowicz MWR 2002, Bouillon et al., OM2013)
+            ! These are NOT stress tensor components, neither stress invariants, neither stress principal components
+            ! I know, this can be confusing...
+            zfac             =   strength(ji,jj) / ( pdelta_i(ji,jj) + rn_creepl ) 
+            zsig1            =   zfac * ( pdivu_i(ji,jj) - pdelta_i(ji,jj) )
+            zsig2            =   zfac * z1_ecc2 * zten_i(ji,jj)
+            zsig12           =   zfac * z1_ecc2 * pshear_i(ji,jj)
+            
+            ! Stress invariants (sigma_I, sigma_II, Coon 1974, Feltham 2008)
+            zsig_I (ji,jj)   =   zsig1 * 0.5_wp                                           ! 1st stress invariant, aka average normal stress, aka negative pressure
+            zsig_II(ji,jj)   =   SQRT ( MAX( 0._wp, zsig2 * zsig2 * 0.25_wp + zsig12 ) )  ! 2nd  ''       '', aka maximum shear stress
+            
+         END_2D
+         !
+         ! Stress tensor invariants (normal and shear stress N/m) - SIMIP diags - definitions following Coon (1974) and Feltham (2008)
+         IF( iom_use('normstr') )   CALL iom_put( 'normstr', zsig_I (:,:) * zmsk00(:,:) ) ! Normal stress
+         IF( iom_use('sheastr') )   CALL iom_put( 'sheastr', zsig_II(:,:) * zmsk00(:,:) ) ! Maximum shear stress
+         
+         DEALLOCATE ( zsig_I, zsig_II )
+         
+      ENDIF
+
+      ! --- Normalized stress tensor principal components --- !
+      ! This are used to plot the normalized yield curve, see Lemieux & Dupont, 2020
+      ! Recommendation 1 : we use ice strength, not replacement pressure
+      ! Recommendation 2 : need to use deformations at PREVIOUS iterate for viscosities
+      IF( iom_use('sig1_pnorm') .OR. iom_use('sig2_pnorm') ) THEN
+         !
+         ALLOCATE( zsig1_p(jpi,jpj) , zsig2_p(jpi,jpj) , zsig_I(jpi,jpj) , zsig_II(jpi,jpj) )         
+         !         
+         DO_2D( 1, 1, 1, 1 )
+         
+            ! Ice stresses computed with **viscosities** (delta, p/delta) at **previous** iterates 
+            !                        and **deformations** at current iterates
+            !                        following Lemieux & Dupont (2020)
+            zfac             =   zp_delt(ji,jj)
+            zsig1            =   zfac * ( pdivu_i(ji,jj) - ( zdelta(ji,jj) + rn_creepl ) )
+            zsig2            =   zfac * z1_ecc2 * zten_i(ji,jj)
+            zsig12           =   zfac * z1_ecc2 * pshear_i(ji,jj)
+            
+            ! Stress invariants (sigma_I, sigma_II, Coon 1974, Feltham 2008), T-point
+            zsig_I(ji,jj)    =   zsig1 * 0.5_wp                                           ! 1st stress invariant, aka average normal stress, aka negative pressure
+            zsig_II(ji,jj)   =   SQRT ( MAX( 0._wp, zsig2 * zsig2 * 0.25_wp + zsig12 ) )  ! 2nd  ''       '', aka maximum shear stress
+   
+            ! Normalized  principal stresses (used to display the ellipse)
+            z1_strength      =   1._wp / MAX( 1._wp, strength(ji,jj) )
+            zsig1_p(ji,jj)   =   ( zsig_I(ji,jj) + zsig_II(ji,jj) ) * z1_strength
+            zsig2_p(ji,jj)   =   ( zsig_I(ji,jj) - zsig_II(ji,jj) ) * z1_strength
+         END_2D
+         !
+         CALL iom_put( 'sig1_pnorm' , zsig1_p ) 
+         CALL iom_put( 'sig2_pnorm' , zsig2_p ) 
+      
+         DEALLOCATE( zsig1_p , zsig2_p , zsig_I, zsig_II )
+         
+      ENDIF
+
+      ! --- yieldcurve --- !
+      IF( iom_use('yield11') .OR. iom_use('yield12') .OR. iom_use('yield22')) THEN
+
+         CALL lbc_lnk_multi( 'icedyn_rhg_eap', zyield11, 'T', 1.0_wp, zyield22, 'T', 1.0_wp, zyield12, 'T', 1.0_wp )
+
+         CALL iom_put( 'yield11', zyield11 * zmsk00 )
+         CALL iom_put( 'yield22', zyield22 * zmsk00 )
+         CALL iom_put( 'yield12', zyield12 * zmsk00 )
+      ENDIF
+
+      ! --- anisotropy tensor --- !
+      IF( iom_use('aniso') ) THEN       
+         CALL lbc_lnk( 'icedyn_rhg_eap', paniso_11, 'T', 1.0_wp ) 
+         CALL iom_put( 'aniso' , paniso_11 * zmsk00 )
+      ENDIF
+      
+      ! --- SIMIP --- !
+      IF(  iom_use('dssh_dx') .OR. iom_use('dssh_dy') .OR. &
+         & iom_use('corstrx') .OR. iom_use('corstry') .OR. iom_use('intstrx') .OR. iom_use('intstry') ) THEN
+         !
+         CALL lbc_lnk_multi( 'icedyn_rhg_eap', zspgU, 'U', -1.0_wp, zspgV, 'V', -1.0_wp, &
+            &                                  zCorU, 'U', -1.0_wp, zCorV, 'V', -1.0_wp, &
+            &                                    zfU, 'U', -1.0_wp,   zfV, 'V', -1.0_wp )
+
+         CALL iom_put( 'dssh_dx' , zspgU * zmsk00 )   ! Sea-surface tilt term in force balance (x)
+         CALL iom_put( 'dssh_dy' , zspgV * zmsk00 )   ! Sea-surface tilt term in force balance (y)
+         CALL iom_put( 'corstrx' , zCorU * zmsk00 )   ! Coriolis force term in force balance (x)
+         CALL iom_put( 'corstry' , zCorV * zmsk00 )   ! Coriolis force term in force balance (y)
+         CALL iom_put( 'intstrx' , zfU   * zmsk00 )   ! Internal force term in force balance (x)
+         CALL iom_put( 'intstry' , zfV   * zmsk00 )   ! Internal force term in force balance (y)
+      ENDIF
+
+      IF(  iom_use('xmtrpice') .OR. iom_use('ymtrpice') .OR. &
+         & iom_use('xmtrpsnw') .OR. iom_use('ymtrpsnw') .OR. iom_use('xatrp') .OR. iom_use('yatrp') ) THEN
+         !
+         ALLOCATE( zdiag_xmtrp_ice(jpi,jpj) , zdiag_ymtrp_ice(jpi,jpj) , &
+            &      zdiag_xmtrp_snw(jpi,jpj) , zdiag_ymtrp_snw(jpi,jpj) , zdiag_xatrp(jpi,jpj) , zdiag_yatrp(jpi,jpj) )
+         !
+         DO_2D( 0, 0, 0, 0 )
+            ! 2D ice mass, snow mass, area transport arrays (X, Y)
+            zfac_x = 0.5 * u_ice(ji,jj) * e2u(ji,jj) * zmsk00(ji,jj)
+            zfac_y = 0.5 * v_ice(ji,jj) * e1v(ji,jj) * zmsk00(ji,jj)
+
+            zdiag_xmtrp_ice(ji,jj) = rhoi * zfac_x * ( vt_i(ji+1,jj) + vt_i(ji,jj) ) ! ice mass transport, X-component
+            zdiag_ymtrp_ice(ji,jj) = rhoi * zfac_y * ( vt_i(ji,jj+1) + vt_i(ji,jj) ) !        ''           Y-   ''
+
+            zdiag_xmtrp_snw(ji,jj) = rhos * zfac_x * ( vt_s(ji+1,jj) + vt_s(ji,jj) ) ! snow mass transport, X-component
+            zdiag_ymtrp_snw(ji,jj) = rhos * zfac_y * ( vt_s(ji,jj+1) + vt_s(ji,jj) ) !          ''          Y-   ''
+
+            zdiag_xatrp(ji,jj)     = zfac_x * ( at_i(ji+1,jj) + at_i(ji,jj) )        ! area transport,      X-component
+            zdiag_yatrp(ji,jj)     = zfac_y * ( at_i(ji,jj+1) + at_i(ji,jj) )        !        ''            Y-   ''
+
+         END_2D
+
+         CALL lbc_lnk_multi( 'icedyn_rhg_eap', zdiag_xmtrp_ice, 'U', -1.0_wp, zdiag_ymtrp_ice, 'V', -1.0_wp, &
+            &                                  zdiag_xmtrp_snw, 'U', -1.0_wp, zdiag_ymtrp_snw, 'V', -1.0_wp, &
+            &                                  zdiag_xatrp    , 'U', -1.0_wp, zdiag_yatrp    , 'V', -1.0_wp )
+
+         CALL iom_put( 'xmtrpice' , zdiag_xmtrp_ice )   ! X-component of sea-ice mass transport (kg/s)
+         CALL iom_put( 'ymtrpice' , zdiag_ymtrp_ice )   ! Y-component of sea-ice mass transport 
+         CALL iom_put( 'xmtrpsnw' , zdiag_xmtrp_snw )   ! X-component of snow mass transport (kg/s)
+         CALL iom_put( 'ymtrpsnw' , zdiag_ymtrp_snw )   ! Y-component of snow mass transport
+         CALL iom_put( 'xatrp'    , zdiag_xatrp     )   ! X-component of ice area transport
+         CALL iom_put( 'yatrp'    , zdiag_yatrp     )   ! Y-component of ice area transport
+
+         DEALLOCATE( zdiag_xmtrp_ice , zdiag_ymtrp_ice , &
+            &        zdiag_xmtrp_snw , zdiag_ymtrp_snw , zdiag_xatrp , zdiag_yatrp )
+
+      ENDIF
+      !
+      ! --- convergence tests --- !
+      IF( nn_rhg_chkcvg == 1 .OR. nn_rhg_chkcvg == 2 ) THEN
+         IF( iom_use('uice_cvg') ) THEN
+            IF( ln_aEVP ) THEN   ! output: beta * ( u(t=nn_nevp) - u(t=nn_nevp-1) )
+               CALL iom_put( 'uice_cvg', MAX( ABS( u_ice(:,:) - zu_ice(:,:) ) * zbeta(:,:) * umask(:,:,1) , &
+                  &                           ABS( v_ice(:,:) - zv_ice(:,:) ) * zbeta(:,:) * vmask(:,:,1) ) * zmsk15(:,:) )
+            ELSE                 ! output: nn_nevp * ( u(t=nn_nevp) - u(t=nn_nevp-1) )
+               CALL iom_put( 'uice_cvg', REAL( nn_nevp ) * MAX( ABS( u_ice(:,:) - zu_ice(:,:) ) * umask(:,:,1) , &
+                  &                                             ABS( v_ice(:,:) - zv_ice(:,:) ) * vmask(:,:,1) ) * zmsk15(:,:) )
+            ENDIF
+         ENDIF
+      ENDIF      
+      !
+      DEALLOCATE( zmsk00, zmsk15 )
+      !
+   END SUBROUTINE ice_dyn_rhg_eap
+
+   
+   SUBROUTINE rhg_cvg( kt, kiter, kitermax, pu, pv, pub, pvb )
+      !!----------------------------------------------------------------------
+      !!                    ***  ROUTINE rhg_cvg  ***
+      !!                     
+      !! ** Purpose :   check convergence of oce rheology
+      !!
+      !! ** Method  :   create a file ice_cvg.nc containing the convergence of ice velocity
+      !!                during the sub timestepping of rheology so as:
+      !!                  uice_cvg = MAX( u(t+1) - u(t) , v(t+1) - v(t) )
+      !!                This routine is called every sub-iteration, so it is cpu expensive
+      !!
+      !! ** Note    :   for the first sub-iteration, uice_cvg is set to 0 (too large otherwise)   
+      !!----------------------------------------------------------------------
+      INTEGER ,                 INTENT(in) ::   kt, kiter, kitermax       ! ocean time-step index
+      REAL(wp), DIMENSION(:,:), INTENT(in) ::   pu, pv, pub, pvb          ! now and before velocities
+      !!
+      INTEGER           ::   it, idtime, istatus
+      INTEGER           ::   ji, jj          ! dummy loop indices
+      REAL(wp)          ::   zresm           ! local real 
+      CHARACTER(len=20) ::   clname
+      REAL(wp), DIMENSION(jpi,jpj) ::   zres           ! check convergence
+      !!----------------------------------------------------------------------
+
+      ! create file
+      IF( kt == nit000 .AND. kiter == 1 ) THEN
+         !
+         IF( lwp ) THEN
+            WRITE(numout,*)
+            WRITE(numout,*) 'rhg_cvg : ice rheology convergence control'
+            WRITE(numout,*) '~~~~~~~'
+         ENDIF
+         !
+         IF( lwm ) THEN
+            clname = 'ice_cvg.nc'
+            IF( .NOT. Agrif_Root() )   clname = TRIM(Agrif_CFixed())//"_"//TRIM(clname)
+            istatus = NF90_CREATE( TRIM(clname), NF90_CLOBBER, ncvgid )
+            istatus = NF90_DEF_DIM( ncvgid, 'time'  , NF90_UNLIMITED, idtime )
+            istatus = NF90_DEF_VAR( ncvgid, 'uice_cvg', NF90_DOUBLE   , (/ idtime /), nvarid )
+            istatus = NF90_ENDDEF(ncvgid)
+         ENDIF
+         !
+      ENDIF
+
+      ! time
+      it = ( kt - 1 ) * kitermax + kiter
+      
+      ! convergence
+      IF( kiter == 1 ) THEN ! remove the first iteration for calculations of convergence (always very large)
+         zresm = 0._wp
+      ELSE
+         DO_2D( 1, 1, 1, 1 )
+            zres(ji,jj) = MAX( ABS( pu(ji,jj) - pub(ji,jj) ) * umask(ji,jj,1), &
+               &               ABS( pv(ji,jj) - pvb(ji,jj) ) * vmask(ji,jj,1) ) * zmsk15(ji,jj)
+         END_2D
+
+         ! cut of the boundary of the box (forced velocities)
+         IF (mjg(jj)<=30 .or. mjg(jj)>970 .or. mig(ji)<=30 .or. mig(ji)>970) THEN
+            zres(ji,jj) = 0._wp
+         END IF
+         zresm = MAXVAL( zres )
+         CALL mpp_max( 'icedyn_rhg_evp', zresm )   ! max over the global domain
+      ENDIF
+
+      IF( lwm ) THEN
+         ! write variables
+         istatus = NF90_PUT_VAR( ncvgid, nvarid, (/zresm/), (/it/), (/1/) )
+         ! close file
+         IF( kt == nitend )   istatus = NF90_CLOSE(ncvgid)
+      ENDIF
+      
+   END SUBROUTINE rhg_cvg
+
+
+   SUBROUTINE update_stress_rdg( ksub, kndte, pdivu, ptension, pshear, pa11, pa12, &
+      &                          pstressp,  pstressm, pstress12, pstrength, palphar, palphas )
+      !!---------------------------------------------------------------------
+      !!                   ***  SUBROUTINE update_stress_rdg  ***
+      !!                     
+      !! ** Purpose :   Updates the stress depending on values of strain rate and structure
+      !!                tensor and for the last subcycle step it computes closing and sliding rate
+      !!---------------------------------------------------------------------
+      INTEGER,  INTENT(in   ) ::   ksub, kndte
+      REAL(wp), INTENT(in   ) ::   pstrength
+      REAL(wp), INTENT(in   ) ::   pdivu, ptension, pshear
+      REAL(wp), INTENT(in   ) ::   pa11, pa12
+      REAL(wp), INTENT(  out) ::   pstressp, pstressm, pstress12
+      REAL(wp), INTENT(  out) ::   palphar, palphas
+
+      INTEGER  ::   kx ,ky, ka
+
+      REAL(wp) ::   zstemp11r, zstemp12r, zstemp22r   
+      REAL(wp) ::   zstemp11s, zstemp12s, zstemp22s 
+      REAL(wp) ::   za22, zQd11Qd11, zQd11Qd12, zQd12Qd12 
+      REAL(wp) ::   zQ11Q11, zQ11Q12, zQ12Q12 
+      REAL(wp) ::   zdtemp11, zdtemp12, zdtemp22 
+      REAL(wp) ::   zrotstemp11r, zrotstemp12r, zrotstemp22r   
+      REAL(wp) ::   zrotstemp11s, zrotstemp12s, zrotstemp22s
+      REAL(wp) ::   zsig11, zsig12, zsig22 
+      REAL(wp) ::   zsgprm11, zsgprm12, zsgprm22 
+      REAL(wp) ::   zinvstressconviso 
+      REAL(wp) ::   zAngle_denom_gamma,  zAngle_denom_alpha 
+      REAL(wp) ::   zTany_1, zTany_2 
+      REAL(wp) ::   zx, zy, zdx, zdy, zda, zkxw, kyw, kaw
+      REAL(wp) ::   zinvdx, zinvdy, zinvda   
+      REAL(wp) ::   zdtemp1, zdtemp2, zatempprime, zinvsin
+
+      REAL(wp), PARAMETER ::   kfriction = 0.45_wp
+      !!---------------------------------------------------------------------
+      ! Factor to maintain the same stress as in EVP (see Section 3)
+      ! Can be set to 1 otherwise
+!      zinvstressconviso = 1._wp/(1._wp+kfriction*kfriction)
+      zinvstressconviso = 1._wp
+ 
+      zinvsin = 1._wp/sin(2._wp*pphi) * zinvstressconviso 
+      !now uses phi as set in higher code
+       
+      ! compute eigenvalues, eigenvectors and angles for structure tensor, strain
+      ! rates
+
+      ! 1) structure tensor
+      za22 = 1._wp-pa11
+      zQ11Q11 = 1._wp
+      zQ12Q12 = rsmall
+      zQ11Q12 = rsmall
+ 
+      ! gamma: angle between general coordiantes and principal axis of A 
+      ! here Tan2gamma = 2 a12 / (a11 - a22) 
+      IF((ABS(pa11 - za22) > rsmall).OR.(ABS(pa12) > rsmall)) THEN      
+         zAngle_denom_gamma = 1._wp/sqrt( ( pa11 - za22 )*( pa11 - za22) + &
+                              4._wp*pa12*pa12 )
+   
+         zQ11Q11 = 0.5_wp + ( pa11 - za22 )*0.5_wp*zAngle_denom_gamma   !Cos^2 
+         zQ12Q12 = 0.5_wp - ( pa11 - za22 )*0.5_wp*zAngle_denom_gamma   !Sin^2
+         zQ11Q12 = pa12*zAngle_denom_gamma                     !CosSin 
+      ENDIF
+ 
+      ! rotation Q*atemp*Q^T
+      zatempprime = zQ11Q11*pa11 + 2.0_wp*zQ11Q12*pa12 + zQ12Q12*za22 
+      
+      ! make first principal value the largest
+      zatempprime = max(zatempprime, 1.0_wp - zatempprime)
+ 
+      ! 2) strain rate
+      zdtemp11 = 0.5_wp*(pdivu + ptension)
+      zdtemp12 = pshear*0.5_wp
+      zdtemp22 = 0.5_wp*(pdivu - ptension)
+
+      ! here Tan2alpha = 2 dtemp12 / (dtemp11 - dtemp22) 
+
+      zQd11Qd11 = 1.0_wp
+      zQd12Qd12 = rsmall
+      zQd11Qd12 = rsmall
+
+      IF((ABS( zdtemp11 - zdtemp22) > rsmall).OR. (ABS(zdtemp12) > rsmall)) THEN
+
+         zAngle_denom_alpha = 1.0_wp/sqrt( ( zdtemp11 - zdtemp22 )* &
+                             ( zdtemp11 - zdtemp22 ) + 4.0_wp*zdtemp12*zdtemp12)
+
+         zQd11Qd11 = 0.5_wp + ( zdtemp11 - zdtemp22 )*0.5_wp*zAngle_denom_alpha !Cos^2 
+         zQd12Qd12 = 0.5_wp - ( zdtemp11 - zdtemp22 )*0.5_wp*zAngle_denom_alpha !Sin^2
+         zQd11Qd12 = zdtemp12*zAngle_denom_alpha !CosSin
+      ENDIF
+
+      zdtemp1 = zQd11Qd11*zdtemp11 + 2.0_wp*zQd11Qd12*zdtemp12 + zQd12Qd12*zdtemp22
+      zdtemp2 = zQd12Qd12*zdtemp11 - 2.0_wp*zQd11Qd12*zdtemp12 + zQd11Qd11*zdtemp22
+      ! In cos and sin values
+      zx = 0._wp
+      IF ((ABS(zdtemp1) > rsmall).OR.(ABS(zdtemp2) > rsmall)) THEN
+         zx = atan2(zdtemp2,zdtemp1)
+      ENDIF      
+               
+      ! to ensure the angle lies between pi/4 and 9 pi/4 
+      IF (zx < rpi*0.25_wp) zx = zx + rpi*2.0_wp
+      
+      ! y: angle between major principal axis of strain rate and structure
+      ! tensor
+      ! y = gamma - alpha 
+      ! Expressesed componently with
+      ! Tany = (Singamma*Cosgamma - Sinalpha*Cosgamma)/(Cos^2gamma - Sin^alpha)
+      
+      zTany_1 = zQ11Q12 - zQd11Qd12
+      zTany_2 = zQ11Q11 - zQd12Qd12
+      
+      zy = 0._wp
+      
+      IF ((ABS(zTany_1) > rsmall).OR.(ABS(zTany_2) > rsmall)) THEN
+         zy = atan2(zTany_1,zTany_2)
+      ENDIF
+      
+      ! to make sure y is between 0 and pi
+      IF (zy > rpi) zy = zy - rpi
+      IF (zy < 0)  zy = zy + rpi
+
+      ! 3) update anisotropic stress tensor 
+      zdx   = rpi/real(nx_yield-1,kind=wp)
+      zdy   = rpi/real(ny_yield-1,kind=wp)
+      zda   = 0.5_wp/real(na_yield-1,kind=wp)
+      zinvdx = 1._wp/zdx
+      zinvdy = 1._wp/zdy
+      zinvda = 1._wp/zda
+
+      ! % need 8 coords and 8 weights
+      ! % range in kx
+      kx  = int((zx-rpi*0.25_wp-rpi)*zinvdx) + 1
+      !!clem kx  = MAX( 1, MIN( nx_yield-1, INT((zx-rpi*0.25_wp-rpi)*zinvdx) + 1  ) )
+      zkxw = kx - (zx-rpi*0.25_wp-rpi)*zinvdx  
+      
+      ky  = int(zy*zinvdy) + 1
+      !!clem ky  = MAX( 1, MIN( ny_yield-1, INT(zy*zinvdy) + 1 ) )
+      kyw = ky - zy*zinvdy 
+      
+      ka  = int((zatempprime-0.5_wp)*zinvda) + 1
+      !!clem ka  = MAX( 1, MIN( na_yield-1, INT((zatempprime-0.5_wp)*zinvda) + 1 ) )
+      kaw = ka - (zatempprime-0.5_wp)*zinvda
+      
+      ! % Determine sigma_r(A1,Zeta,y) and sigma_s (see Section A1 of Tsamados 2013)
+!!$      zstemp11r =  zkxw  * kyw         * kaw         * s11r(kx  ,ky  ,ka  ) &
+!!$        & + (1._wp-zkxw) * kyw         * kaw         * s11r(kx+1,ky  ,ka  ) &
+!!$        & + zkxw         * (1._wp-kyw) * kaw         * s11r(kx  ,ky+1,ka  ) &
+!!$        & + zkxw         * kyw         * (1._wp-kaw) * s11r(kx  ,ky  ,ka+1) &
+!!$        & + (1._wp-zkxw) * (1._wp-kyw) * kaw         * s11r(kx+1,ky+1,ka  ) &
+!!$        & + (1._wp-zkxw) * kyw         * (1._wp-kaw) * s11r(kx+1,ky  ,ka+1) &
+!!$        & + zkxw         * (1._wp-kyw) * (1._wp-kaw) * s11r(kx  ,ky+1,ka+1) &
+!!$        & + (1._wp-zkxw) * (1._wp-kyw) * (1._wp-kaw) * s11r(kx+1,ky+1,ka+1)
+!!$      zstemp12r =  zkxw  * kyw         * kaw         * s12r(kx  ,ky  ,ka  ) &
+!!$        & + (1._wp-zkxw) * kyw         * kaw         * s12r(kx+1,ky  ,ka  ) &
+!!$        & + zkxw         * (1._wp-kyw) * kaw         * s12r(kx  ,ky+1,ka  ) &
+!!$        & + zkxw         * kyw         * (1._wp-kaw) * s12r(kx  ,ky  ,ka+1) &
+!!$        & + (1._wp-zkxw) * (1._wp-kyw) * kaw         * s12r(kx+1,ky+1,ka  ) &
+!!$        & + (1._wp-zkxw) * kyw         * (1._wp-kaw) * s12r(kx+1,ky  ,ka+1) &
+!!$        & + zkxw         * (1._wp-kyw) * (1._wp-kaw) * s12r(kx  ,ky+1,ka+1) &
+!!$        & + (1._wp-zkxw) * (1._wp-kyw) * (1._wp-kaw) * s12r(kx+1,ky+1,ka+1)
+!!$      zstemp22r =  zkxw  * kyw         * kaw         * s22r(kx  ,ky  ,ka  ) &
+!!$        & + (1._wp-zkxw) * kyw         * kaw         * s22r(kx+1,ky  ,ka  ) &
+!!$        & + zkxw         * (1._wp-kyw) * kaw         * s22r(kx  ,ky+1,ka  ) &
+!!$        & + zkxw         * kyw         * (1._wp-kaw) * s22r(kx  ,ky  ,ka+1) &
+!!$        & + (1._wp-zkxw) * (1._wp-kyw) * kaw         * s22r(kx+1,ky+1,ka  ) &
+!!$        & + (1._wp-zkxw) * kyw         * (1._wp-kaw) * s22r(kx+1,ky  ,ka+1) &
+!!$        & + zkxw         * (1._wp-kyw) * (1._wp-kaw) * s22r(kx  ,ky+1,ka+1) &
+!!$        & + (1._wp-zkxw) * (1._wp-kyw) * (1._wp-kaw) * s22r(kx+1,ky+1,ka+1)
+!!$      
+!!$      zstemp11s =  zkxw  * kyw         * kaw         * s11s(kx  ,ky  ,ka  ) &
+!!$        & + (1._wp-zkxw) * kyw         * kaw         * s11s(kx+1,ky  ,ka  ) &
+!!$        & + zkxw         * (1._wp-kyw) * kaw         * s11s(kx  ,ky+1,ka  ) &
+!!$        & + zkxw         * kyw         * (1._wp-kaw) * s11s(kx  ,ky  ,ka+1) &
+!!$        & + (1._wp-zkxw) * (1._wp-kyw) * kaw         * s11s(kx+1,ky+1,ka  ) &
+!!$        & + (1._wp-zkxw) * kyw         * (1._wp-kaw) * s11s(kx+1,ky  ,ka+1) &
+!!$        & + zkxw         * (1._wp-kyw) * (1._wp-kaw) * s11s(kx  ,ky+1,ka+1) &
+!!$        & + (1._wp-zkxw) * (1._wp-kyw) * (1._wp-kaw) * s11s(kx+1,ky+1,ka+1)
+!!$      zstemp12s =  zkxw  * kyw         * kaw         * s12s(kx  ,ky  ,ka  ) &
+!!$        & + (1._wp-zkxw) * kyw         * kaw         * s12s(kx+1,ky  ,ka  ) &
+!!$        & + zkxw         * (1._wp-kyw) * kaw         * s12s(kx  ,ky+1,ka  ) &
+!!$        & + zkxw         * kyw         * (1._wp-kaw) * s12s(kx  ,ky  ,ka+1) &
+!!$        & + (1._wp-zkxw) * (1._wp-kyw) * kaw         * s12s(kx+1,ky+1,ka  ) &
+!!$        & + (1._wp-zkxw) * kyw         * (1._wp-kaw) * s12s(kx+1,ky  ,ka+1) &
+!!$        & + zkxw         * (1._wp-kyw) * (1._wp-kaw) * s12s(kx  ,ky+1,ka+1) &
+!!$        & + (1._wp-zkxw) * (1._wp-kyw) * (1._wp-kaw) * s12s(kx+1,ky+1,ka+1)
+!!$      zstemp22s =  zkxw  * kyw         * kaw         * s22s(kx  ,ky  ,ka  ) &
+!!$        & + (1._wp-zkxw) * kyw         * kaw         * s22s(kx+1,ky  ,ka  ) &
+!!$        & + zkxw         * (1._wp-kyw) * kaw         * s22s(kx  ,ky+1,ka  ) &
+!!$        & + zkxw         * kyw         * (1._wp-kaw) * s22s(kx  ,ky  ,ka+1) &
+!!$        & + (1._wp-zkxw) * (1._wp-kyw) * kaw         * s22s(kx+1,ky+1,ka  ) &
+!!$        & + (1._wp-zkxw) * kyw         * (1._wp-kaw) * s22s(kx+1,ky  ,ka+1) &
+!!$        & + zkxw         * (1._wp-kyw) * (1._wp-kaw) * s22s(kx  ,ky+1,ka+1) &
+!!$        & + (1._wp-zkxw) * (1._wp-kyw) * (1._wp-kaw) * s22s(kx+1,ky+1,ka+1)
+
+      zstemp11r = s11r(kx,ky,ka)
+      zstemp12r = s12r(kx,ky,ka)
+      zstemp22r = s22r(kx,ky,ka)
+      zstemp11s = s11s(kx,ky,ka)
+      zstemp12s = s12s(kx,ky,ka)
+      zstemp22s = s22s(kx,ky,ka)
+      
+      
+      ! Calculate mean ice stress over a collection of floes (Equation 3 in
+      ! Tsamados 2013)
+
+      zsig11  = pstrength*(zstemp11r + kfriction*zstemp11s) * zinvsin
+      zsig12  = pstrength*(zstemp12r + kfriction*zstemp12s) * zinvsin
+      zsig22  = pstrength*(zstemp22r + kfriction*zstemp22s) * zinvsin
+
+      ! Back - rotation of the stress from principal axes into general coordinates
+
+      ! Update stress
+      zsgprm11 = zQ11Q11*zsig11 + zQ12Q12*zsig22 -      2._wp*zQ11Q12 *zsig12
+      zsgprm12 = zQ11Q12*zsig11 - zQ11Q12*zsig22 + (zQ11Q11 - zQ12Q12)*zsig12
+      zsgprm22 = zQ12Q12*zsig11 + zQ11Q11*zsig22 +      2._wp*zQ11Q12 *zsig12
+
+      pstressp  = zsgprm11 + zsgprm22
+      pstress12 = zsgprm12
+      pstressm  = zsgprm11 - zsgprm22
+
+      ! Compute ridging and sliding functions in general coordinates 
+      ! (Equation 11 in Tsamados 2013)
+      IF (ksub == kndte) THEN
+         zrotstemp11r = zQ11Q11*zstemp11r - 2._wp*zQ11Q12* zstemp12r &
+                      + zQ12Q12*zstemp22r
+         zrotstemp12r = zQ11Q11*zstemp12r +       zQ11Q12*(zstemp11r-zstemp22r) &
+                      - zQ12Q12*zstemp12r
+         zrotstemp22r = zQ12Q12*zstemp11r + 2._wp*zQ11Q12* zstemp12r & 
+                      + zQ11Q11*zstemp22r
+
+         zrotstemp11s = zQ11Q11*zstemp11s - 2._wp*zQ11Q12* zstemp12s &
+                      + zQ12Q12*zstemp22s
+         zrotstemp12s = zQ11Q11*zstemp12s +       zQ11Q12*(zstemp11s-zstemp22s) &
+                      - zQ12Q12*zstemp12s
+         zrotstemp22s = zQ12Q12*zstemp11s + 2._wp*zQ11Q12* zstemp12s & 
+                      + zQ11Q11*zstemp22s
+
+         palphar =  zrotstemp11r*zdtemp11 + 2._wp*zrotstemp12r*zdtemp12 &
+                  + zrotstemp22r*zdtemp22
+         palphas =  zrotstemp11s*zdtemp11 + 2._wp*zrotstemp12s*zdtemp12 &
+                  + zrotstemp22s*zdtemp22
+      ENDIF
+   END SUBROUTINE update_stress_rdg 
+
+!=======================================================================
+
+
+   SUBROUTINE calc_ffrac( pstressp, pstressm, pstress12, pa11, pa12, &
+      &                   pmresult11, pmresult12 )
+      !!---------------------------------------------------------------------
+      !!                   ***  ROUTINE calc_ffrac  ***
+      !!                     
+      !! ** Purpose :   Computes term in evolution equation for structure tensor
+      !!                which determines the ice floe re-orientation due to fracture
+      !! ** Method :    Eq. 7: Ffrac = -kf(A-S) or = 0 depending on sigma_1 and sigma_2
+      !!---------------------------------------------------------------------
+      REAL (wp), INTENT(in)  ::   pstressp, pstressm, pstress12, pa11, pa12
+      REAL (wp), INTENT(out) ::   pmresult11, pmresult12
+
+      ! local variables
+
+      REAL (wp) ::   zsigma11, zsigma12, zsigma22  ! stress tensor elements
+      REAL (wp) ::   zAngle_denom        ! angle with principal component axis
+      REAL (wp) ::   zsigma_1, zsigma_2   ! principal components of stress
+      REAL (wp) ::   zQ11, zQ12, zQ11Q11, zQ11Q12, zQ12Q12
+
+!!$      REAL (wp), PARAMETER ::   kfrac = 0.0001_wp   ! rate of fracture formation 
+      REAL (wp), PARAMETER ::   kfrac = 1.e-3_wp   ! rate of fracture formation 
+      REAL (wp), PARAMETER ::   threshold = 0.3_wp  ! critical confinement ratio 
+      !!---------------------------------------------------------------
+      !
+      zsigma11 = 0.5_wp*(pstressp+pstressm) 
+      zsigma12 = pstress12 
+      zsigma22 = 0.5_wp*(pstressp-pstressm) 
+
+      ! Here's the change - no longer calculate gamma,
+      ! use trig formulation, angle signs are kept correct, don't worry
+   
+      ! rotate tensor to get into sigma principal axis
+   
+      ! here Tan2gamma = 2 sig12 / (sig11 - sig12) 
+      ! add rsmall to the denominator to stop 1/0 errors, makes very little 
+      ! error to the calculated angles < rsmall
+
+      zQ11Q11 = 0.1_wp
+      zQ12Q12 = rsmall
+      zQ11Q12 = rsmall
+
+      IF((ABS( zsigma11 - zsigma22) > rsmall).OR.(ABS(zsigma12) > rsmall)) THEN
+
+         zAngle_denom = 1.0_wp/sqrt( ( zsigma11 - zsigma22 )*( zsigma11 - &
+                       zsigma22 ) + 4.0_wp*zsigma12*zsigma12)
+
+         zQ11Q11 = 0.5_wp + ( zsigma11 - zsigma22 )*0.5_wp*zAngle_denom   !Cos^2 
+         zQ12Q12 = 0.5_wp - ( zsigma11 - zsigma22 )*0.5_wp*zAngle_denom   !Sin^2
+         zQ11Q12 = zsigma12*zAngle_denom                      !CosSin 
+      ENDIF
+
+      zsigma_1 = zQ11Q11*zsigma11 + 2.0_wp*zQ11Q12*zsigma12 + zQ12Q12*zsigma22 ! S(1,1)
+      zsigma_2 = zQ12Q12*zsigma11 - 2.0_wp*zQ11Q12*zsigma12 + zQ11Q11*zsigma22 ! S(2,2)
+
+      ! Pure divergence
+      IF ((zsigma_1 >= 0.0_wp).AND.(zsigma_2 >= 0.0_wp))  THEN
+         pmresult11 = 0.0_wp
+         pmresult12 = 0.0_wp
+
+      ! Unconfined compression: cracking of blocks not along the axial splitting
+      ! direction
+      ! which leads to the loss of their shape, so we again model it through diffusion
+      ELSEIF ((zsigma_1 >= 0.0_wp).AND.(zsigma_2 < 0.0_wp))  THEN
+         pmresult11 = - kfrac * (pa11 - zQ12Q12)
+         pmresult12 = - kfrac * (pa12 + zQ11Q12)
+
+      ! Shear faulting
+      ELSEIF (zsigma_2 == 0.0_wp) THEN
+         pmresult11 = 0.0_wp
+         pmresult12 = 0.0_wp
+      ELSEIF ((zsigma_1 <= 0.0_wp).AND.(zsigma_1/zsigma_2 <= threshold)) THEN
+         pmresult11 = - kfrac * (pa11 - zQ12Q12)
+         pmresult12 = - kfrac * (pa12 + zQ11Q12)
+
+      ! Horizontal spalling
+      ELSE 
+         pmresult11 = 0.0_wp
+         pmresult12 = 0.0_wp
+      ENDIF
+
+   END SUBROUTINE calc_ffrac
+
+
+   SUBROUTINE rhg_eap_rst( cdrw, kt )
+      !!---------------------------------------------------------------------
+      !!                   ***  ROUTINE rhg_eap_rst  ***
+      !!                     
+      !! ** Purpose :   Read or write RHG file in restart file
+      !!      
+      !! ** Method  :   use of IOM library
+      !!----------------------------------------------------------------------
+      CHARACTER(len=*) , INTENT(in) ::   cdrw   ! "READ"/"WRITE" flag
+      INTEGER, OPTIONAL, INTENT(in) ::   kt     ! ice time-step
+      !
+      INTEGER  ::   iter                      ! local integer
+      INTEGER  ::   id1, id2, id3, id4, id5   ! local integers
+      INTEGER  ::   ix, iy, ip, iz, n, ia     ! local integers
+    
+      INTEGER, PARAMETER            ::    nz = 100
+      
+      REAL(wp) ::   ainit, xinit, yinit, pinit, zinit
+      REAL(wp) ::   da, dx, dy, dp, dz, a1
+
+      !!clem
+      REAL(wp) ::   zw1, zw2, zfac, ztemp
+      REAL(wp) ::   idx, idy, idz
+
+      REAL(wp), PARAMETER           ::   eps6 = 1.0e-6_wp
+      !!----------------------------------------------------------------------
+      !
+      IF( TRIM(cdrw) == 'READ' ) THEN        ! Read/initialize
+         !                                   ! ---------------
+         IF( ln_rstart ) THEN                   !* Read the restart file
+            !
+            id1 = iom_varid( numrir, 'stress1_i' , ldstop = .FALSE. )
+            id2 = iom_varid( numrir, 'stress2_i' , ldstop = .FALSE. )
+            id3 = iom_varid( numrir, 'stress12_i', ldstop = .FALSE. )
+            id4 = iom_varid( numrir, 'aniso_11'  , ldstop = .FALSE. )
+            id5 = iom_varid( numrir, 'aniso_12'  , ldstop = .FALSE. )
+            !
+            IF( MIN( id1, id2, id3, id4, id5 ) > 0 ) THEN      ! fields exist
+               CALL iom_get( numrir, jpdom_auto, 'stress1_i' , stress1_i , cd_type = 'T' )
+               CALL iom_get( numrir, jpdom_auto, 'stress2_i' , stress2_i , cd_type = 'T' )
+               CALL iom_get( numrir, jpdom_auto, 'stress12_i', stress12_i, cd_type = 'F' )
+               CALL iom_get( numrir, jpdom_auto, 'aniso_11'  , aniso_11  , cd_type = 'T' )
+               CALL iom_get( numrir, jpdom_auto, 'aniso_12'  , aniso_12  , cd_type = 'T' )
+            ELSE                                     ! start rheology from rest
+               IF(lwp) WRITE(numout,*)
+               IF(lwp) WRITE(numout,*) '   ==>>>   previous run without rheology, set stresses to 0'
+               stress1_i (:,:) = 0._wp
+               stress2_i (:,:) = 0._wp
+               stress12_i(:,:) = 0._wp
+               aniso_11  (:,:) = 0.5_wp
+               aniso_12  (:,:) = 0._wp
+            ENDIF
+         ELSE                                   !* Start from rest
+            IF(lwp) WRITE(numout,*)
+            IF(lwp) WRITE(numout,*) '   ==>>>   start from rest: set stresses to 0'
+            stress1_i (:,:) = 0._wp
+            stress2_i (:,:) = 0._wp
+            stress12_i(:,:) = 0._wp
+            aniso_11  (:,:) = 0.5_wp
+            aniso_12  (:,:) = 0._wp
+         ENDIF
+         !
+
+         da = 0.5_wp/real(na_yield-1,kind=wp)
+         ainit = 0.5_wp - da
+         dx = rpi/real(nx_yield-1,kind=wp)
+         xinit = rpi + 0.25_wp*rpi - dx
+         dz = rpi/real(nz,kind=wp)
+         zinit = -rpi*0.5_wp
+         dy = rpi/real(ny_yield-1,kind=wp)
+         yinit = -dy
+
+         s11r(:,:,:) = 0._wp
+         s12r(:,:,:) = 0._wp
+         s22r(:,:,:) = 0._wp
+         s11s(:,:,:) = 0._wp
+         s12s(:,:,:) = 0._wp
+         s22s(:,:,:) = 0._wp
+
+!!$         DO ia=1,na_yield
+!!$            DO ix=1,nx_yield
+!!$               DO iy=1,ny_yield
+!!$                  s11r(ix,iy,ia) = 0._wp
+!!$                  s12r(ix,iy,ia) = 0._wp
+!!$                  s22r(ix,iy,ia) = 0._wp
+!!$                  s11s(ix,iy,ia) = 0._wp
+!!$                  s12s(ix,iy,ia) = 0._wp
+!!$                  s22s(ix,iy,ia) = 0._wp
+!!$                  IF (ia <= na_yield-1) THEN
+!!$                     DO iz=1,nz
+!!$                        s11r(ix,iy,ia) = s11r(ix,iy,ia) + 1*w1(ainit+ia*da)* &
+!!$                           exp(-w2(ainit+ia*da)*(zinit+iz*dz)*(zinit+iz*dz))* &
+!!$                           s11kr(xinit+ix*dx,yinit+iy*dy,zinit+iz*dz)*dz/sin(2._wp*pphi)
+!!$                        s12r(ix,iy,ia) = s12r(ix,iy,ia) + 1*w1(ainit+ia*da)* &
+!!$                           exp(-w2(ainit+ia*da)*(zinit+iz*dz)*(zinit+iz*dz))* &
+!!$                           s12kr(xinit+ix*dx,yinit+iy*dy,zinit+iz*dz)*dz/sin(2._wp*pphi)
+!!$                        s22r(ix,iy,ia) = s22r(ix,iy,ia) + 1*w1(ainit+ia*da)* &
+!!$                           exp(-w2(ainit+ia*da)*(zinit+iz*dz)*(zinit+iz*dz))* &
+!!$                           s22kr(xinit+ix*dx,yinit+iy*dy,zinit+iz*dz)*dz/sin(2._wp*pphi) 
+!!$                        s11s(ix,iy,ia) = s11s(ix,iy,ia) + 1*w1(ainit+ia*da)* &
+!!$                           exp(-w2(ainit+ia*da)*(zinit+iz*dz)*(zinit+iz*dz))* &
+!!$                           s11ks(xinit+ix*dx,yinit+iy*dy,zinit+iz*dz)*dz/sin(2._wp*pphi)
+!!$                        s12s(ix,iy,ia) = s12s(ix,iy,ia) + 1*w1(ainit+ia*da)* &
+!!$                           exp(-w2(ainit+ia*da)*(zinit+iz*dz)*(zinit+iz*dz))* &
+!!$                           s12ks(xinit+ix*dx,yinit+iy*dy,zinit+iz*dz)*dz/sin(2._wp*pphi)
+!!$                        s22s(ix,iy,ia) = s22s(ix,iy,ia) + 1*w1(ainit+ia*da)* &
+!!$                           exp(-w2(ainit+ia*da)*(zinit+iz*dz)*(zinit+iz*dz))* &
+!!$                           s22ks(xinit+ix*dx,yinit+iy*dy,zinit+iz*dz)*dz/sin(2._wp*pphi)
+!!$                     ENDDO
+!!$                     IF (abs(s11r(ix,iy,ia)) < eps6) s11r(ix,iy,ia) = 0._wp
+!!$                     IF (abs(s12r(ix,iy,ia)) < eps6) s12r(ix,iy,ia) = 0._wp
+!!$                     IF (abs(s22r(ix,iy,ia)) < eps6) s22r(ix,iy,ia) = 0._wp
+!!$                     IF (abs(s11s(ix,iy,ia)) < eps6) s11s(ix,iy,ia) = 0._wp
+!!$                     IF (abs(s12s(ix,iy,ia)) < eps6) s12s(ix,iy,ia) = 0._wp
+!!$                     IF (abs(s22s(ix,iy,ia)) < eps6) s22s(ix,iy,ia) = 0._wp
+!!$                  ELSE
+!!$                     s11r(ix,iy,ia) = 0.5_wp*s11kr(xinit+ix*dx,yinit+iy*dy,0._wp)/sin(2._wp*pphi)
+!!$                     s12r(ix,iy,ia) = 0.5_wp*s12kr(xinit+ix*dx,yinit+iy*dy,0._wp)/sin(2._wp*pphi)
+!!$                     s22r(ix,iy,ia) = 0.5_wp*s22kr(xinit+ix*dx,yinit+iy*dy,0._wp)/sin(2._wp*pphi)
+!!$                     s11s(ix,iy,ia) = 0.5_wp*s11ks(xinit+ix*dx,yinit+iy*dy,0._wp)/sin(2._wp*pphi)
+!!$                     s12s(ix,iy,ia) = 0.5_wp*s12ks(xinit+ix*dx,yinit+iy*dy,0._wp)/sin(2._wp*pphi)
+!!$                     s22s(ix,iy,ia) = 0.5_wp*s22ks(xinit+ix*dx,yinit+iy*dy,0._wp)/sin(2._wp*pphi)
+!!$                     IF (abs(s11r(ix,iy,ia)) < eps6) s11r(ix,iy,ia) = 0._wp
+!!$                     IF (abs(s12r(ix,iy,ia)) < eps6) s12r(ix,iy,ia) = 0._wp
+!!$                     IF (abs(s22r(ix,iy,ia)) < eps6) s22r(ix,iy,ia) = 0._wp
+!!$                     IF (abs(s11s(ix,iy,ia)) < eps6) s11s(ix,iy,ia) = 0._wp
+!!$                     IF (abs(s12s(ix,iy,ia)) < eps6) s12s(ix,iy,ia) = 0._wp
+!!$                     IF (abs(s22s(ix,iy,ia)) < eps6) s22s(ix,iy,ia) = 0._wp
+!!$                  ENDIF
+!!$               ENDDO
+!!$            ENDDO
+!!$         ENDDO
+
+         !! faster but still very slow => to be improved         
+         zfac = dz/sin(2._wp*pphi)
+         DO ia = 1, na_yield-1
+            zw1 = w1(ainit+ia*da)
+            zw2 = w2(ainit+ia*da)
+            DO iz = 1, nz
+               idz = zinit+iz*dz
+               ztemp = zw1 * EXP(-zw2*(zinit+iz*dz)*(zinit+iz*dz))
+               DO iy = 1, ny_yield
+                  idy = yinit+iy*dy
+                  DO ix = 1, nx_yield
+                     idx = xinit+ix*dx
+                     s11r(ix,iy,ia) = s11r(ix,iy,ia) + ztemp * s11kr(idx,idy,idz)*zfac
+                     s12r(ix,iy,ia) = s12r(ix,iy,ia) + ztemp * s12kr(idx,idy,idz)*zfac
+                     s22r(ix,iy,ia) = s22r(ix,iy,ia) + ztemp * s22kr(idx,idy,idz)*zfac 
+                     s11s(ix,iy,ia) = s11s(ix,iy,ia) + ztemp * s11ks(idx,idy,idz)*zfac
+                     s12s(ix,iy,ia) = s12s(ix,iy,ia) + ztemp * s12ks(idx,idy,idz)*zfac
+                     s22s(ix,iy,ia) = s22s(ix,iy,ia) + ztemp * s22ks(idx,idy,idz)*zfac
+                  END DO
+               END DO
+            END DO
+         END DO
+
+         zfac = 1._wp/sin(2._wp*pphi)
+         ia = na_yield
+         DO iy = 1, ny_yield
+            idy = yinit+iy*dy
+            DO ix = 1, nx_yield
+               idx = xinit+ix*dx                  
+               s11r(ix,iy,ia) = 0.5_wp*s11kr(idx,idy,0._wp)*zfac
+               s12r(ix,iy,ia) = 0.5_wp*s12kr(idx,idy,0._wp)*zfac
+               s22r(ix,iy,ia) = 0.5_wp*s22kr(idx,idy,0._wp)*zfac
+               s11s(ix,iy,ia) = 0.5_wp*s11ks(idx,idy,0._wp)*zfac
+               s12s(ix,iy,ia) = 0.5_wp*s12ks(idx,idy,0._wp)*zfac
+               s22s(ix,iy,ia) = 0.5_wp*s22ks(idx,idy,0._wp)*zfac
+            ENDDO
+         ENDDO
+         WHERE (ABS(s11r(:,:,:)) < eps6) s11r(:,:,:) = 0._wp
+         WHERE (ABS(s12r(:,:,:)) < eps6) s12r(:,:,:) = 0._wp
+         WHERE (ABS(s22r(:,:,:)) < eps6) s22r(:,:,:) = 0._wp
+         WHERE (ABS(s11s(:,:,:)) < eps6) s11s(:,:,:) = 0._wp
+         WHERE (ABS(s12s(:,:,:)) < eps6) s12s(:,:,:) = 0._wp
+         WHERE (ABS(s22s(:,:,:)) < eps6) s22s(:,:,:) = 0._wp
+
+
+      ELSEIF( TRIM(cdrw) == 'WRITE' ) THEN   ! Create restart file
+         !                                   ! -------------------
+         IF(lwp) WRITE(numout,*) '---- rhg-rst ----'
+         iter = kt + nn_fsbc - 1             ! ice restarts are written at kt == nitrst - nn_fsbc + 1
+         !
+         CALL iom_rstput( iter, nitrst, numriw, 'stress1_i' , stress1_i  )
+         CALL iom_rstput( iter, nitrst, numriw, 'stress2_i' , stress2_i  )
+         CALL iom_rstput( iter, nitrst, numriw, 'stress12_i', stress12_i )
+         CALL iom_rstput( iter, nitrst, numriw, 'aniso_11'  , aniso_11   )
+         CALL iom_rstput( iter, nitrst, numriw, 'aniso_12'  , aniso_12   )
+         !
+      ENDIF
+      !
+   END SUBROUTINE rhg_eap_rst
+
+
+   FUNCTION w1(pa)
+      !!-------------------------------------------------------------------
+      !! Function : w1 (see Gaussian function psi in Tsamados et al 2013)
+      !!-------------------------------------------------------------------
+      REAL(wp), INTENT(in   ) ::   pa
+      REAL(wp) ::   w1
+      !!-------------------------------------------------------------------
+   
+      w1 = -   223.87569446_wp &
+       &   +  2361.21986630_wp*pa &
+       &   - 10606.56079975_wp*pa*pa &
+       &   + 26315.50025642_wp*pa*pa*pa &
+       &   - 38948.30444297_wp*pa*pa*pa*pa &
+       &   + 34397.72407466_wp*pa*pa*pa*pa*pa &
+       &   - 16789.98003081_wp*pa*pa*pa*pa*pa*pa &
+       &   +  3495.82839237_wp*pa*pa*pa*pa*pa*pa*pa
+   
+   END FUNCTION w1
+
+
+   FUNCTION w2(pa)
+      !!-------------------------------------------------------------------
+      !! Function : w2 (see Gaussian function psi in Tsamados et al 2013)
+      !!-------------------------------------------------------------------
+      REAL(wp), INTENT(in   ) ::   pa
+      REAL(wp) ::   w2
+      !!-------------------------------------------------------------------
+   
+      w2 = -    6670.68911883_wp &
+       &   +   70222.33061536_wp*pa &
+       &   -  314871.71525448_wp*pa*pa &
+       &   +  779570.02793492_wp*pa*pa*pa &
+       &   - 1151098.82436864_wp*pa*pa*pa*pa &
+       &   + 1013896.59464498_wp*pa*pa*pa*pa*pa &
+       &   -  493379.44906738_wp*pa*pa*pa*pa*pa*pa &
+       &   +  102356.55151800_wp*pa*pa*pa*pa*pa*pa*pa
+   
+   END FUNCTION w2
+
+   FUNCTION s11kr(px,py,pz) 
+      !!-------------------------------------------------------------------
+      !! Function : s11kr
+      !!-------------------------------------------------------------------
+      REAL(wp), INTENT(in   ) ::   px,py,pz
+   
+      REAL(wp) ::   s11kr, zpih
+   
+      REAL(wp) ::   zn1t2i11, zn1t2i12, zn1t2i21, zn1t2i22
+      REAL(wp) ::   zn2t1i11, zn2t1i12, zn2t1i21, zn2t1i22
+      REAL(wp) ::   zt1t2i11, zt1t2i12, zt1t2i21, zt1t2i22
+      REAL(wp) ::   zt2t1i11, zt2t1i12, zt2t1i21, zt2t1i22
+      REAL(wp) ::   zd11, zd12, zd22
+      REAL(wp) ::   zIIn1t2, zIIn2t1, zIIt1t2
+      REAL(wp) ::   zHen1t2, zHen2t1
+      !!-------------------------------------------------------------------
+   
+      zpih = 0.5_wp*rpi
+   
+      zn1t2i11 = cos(pz+zpih-pphi) * cos(pz+pphi)
+      zn1t2i12 = cos(pz+zpih-pphi) * sin(pz+pphi)
+      zn1t2i21 = sin(pz+zpih-pphi) * cos(pz+pphi)
+      zn1t2i22 = sin(pz+zpih-pphi) * sin(pz+pphi)
+      zn2t1i11 = cos(pz-zpih+pphi) * cos(pz-pphi)
+      zn2t1i12 = cos(pz-zpih+pphi) * sin(pz-pphi)
+      zn2t1i21 = sin(pz-zpih+pphi) * cos(pz-pphi)
+      zn2t1i22 = sin(pz-zpih+pphi) * sin(pz-pphi)
+      zt1t2i11 = cos(pz-pphi) * cos(pz+pphi)
+      zt1t2i12 = cos(pz-pphi) * sin(pz+pphi)
+      zt1t2i21 = sin(pz-pphi) * cos(pz+pphi)
+      zt1t2i22 = sin(pz-pphi) * sin(pz+pphi)
+      zt2t1i11 = cos(pz+pphi) * cos(pz-pphi)
+      zt2t1i12 = cos(pz+pphi) * sin(pz-pphi)
+      zt2t1i21 = sin(pz+pphi) * cos(pz-pphi)
+      zt2t1i22 = sin(pz+pphi) * sin(pz-pphi)
+   ! In expression of tensor d, with this formulatin d(x)=-d(x+pi)
+   ! Solution, when diagonalizing always check sgn(a11-a22) if > then keep x else
+   ! x=x-pi/2
+      zd11 = cos(py)*cos(py)*(cos(px)+sin(px)*tan(py)*tan(py))
+      zd12 = cos(py)*cos(py)*tan(py)*(-cos(px)+sin(px))
+      zd22 = cos(py)*cos(py)*(sin(px)+cos(px)*tan(py)*tan(py))
+      zIIn1t2 = zn1t2i11 * zd11 + (zn1t2i12 + zn1t2i21) * zd12 + zn1t2i22 * zd22
+      zIIn2t1 = zn2t1i11 * zd11 + (zn2t1i12 + zn2t1i21) * zd12 + zn2t1i22 * zd22
+      zIIt1t2 = zt1t2i11 * zd11 + (zt1t2i12 + zt1t2i21) * zd12 + zt1t2i22 * zd22
+   
+      IF (-zIIn1t2>=rsmall) THEN
+      zHen1t2 = 1._wp
+      ELSE
+      zHen1t2 = 0._wp
+      ENDIF
+   
+      IF (-zIIn2t1>=rsmall) THEN
+      zHen2t1 = 1._wp
+      ELSE
+      zHen2t1 = 0._wp
+      ENDIF
+   
+      s11kr = (- zHen1t2 * zn1t2i11 - zHen2t1 * zn2t1i11)
+
+   END FUNCTION s11kr
+
+   FUNCTION s12kr(px,py,pz)
+      !!-------------------------------------------------------------------
+      !! Function : s12kr
+      !!-------------------------------------------------------------------
+      REAL(wp), INTENT(in   ) ::   px,py,pz
+
+      REAL(wp) ::   s12kr, zs12r0, zs21r0, zpih
+
+      REAL(wp) ::   zn1t2i11, zn1t2i12, zn1t2i21, zn1t2i22
+      REAL(wp) ::   zn2t1i11, zn2t1i12, zn2t1i21, zn2t1i22
+      REAL(wp) ::   zt1t2i11, zt1t2i12, zt1t2i21, zt1t2i22
+      REAL(wp) ::   zt2t1i11, zt2t1i12, zt2t1i21, zt2t1i22
+      REAL(wp) ::   zd11, zd12, zd22
+      REAL(wp) ::   zIIn1t2, zIIn2t1, zIIt1t2
+      REAL(wp) ::   zHen1t2, zHen2t1
+      !!-------------------------------------------------------------------
+      zpih = 0.5_wp*rpi
+   
+      zn1t2i11 = cos(pz+zpih-pphi) * cos(pz+pphi)
+      zn1t2i12 = cos(pz+zpih-pphi) * sin(pz+pphi)
+      zn1t2i21 = sin(pz+zpih-pphi) * cos(pz+pphi)
+      zn1t2i22 = sin(pz+zpih-pphi) * sin(pz+pphi)
+      zn2t1i11 = cos(pz-zpih+pphi) * cos(pz-pphi)
+      zn2t1i12 = cos(pz-zpih+pphi) * sin(pz-pphi)
+      zn2t1i21 = sin(pz-zpih+pphi) * cos(pz-pphi)
+      zn2t1i22 = sin(pz-zpih+pphi) * sin(pz-pphi)
+      zt1t2i11 = cos(pz-pphi) * cos(pz+pphi)
+      zt1t2i12 = cos(pz-pphi) * sin(pz+pphi)
+      zt1t2i21 = sin(pz-pphi) * cos(pz+pphi)
+      zt1t2i22 = sin(pz-pphi) * sin(pz+pphi)
+      zt2t1i11 = cos(pz+pphi) * cos(pz-pphi)
+      zt2t1i12 = cos(pz+pphi) * sin(pz-pphi)
+      zt2t1i21 = sin(pz+pphi) * cos(pz-pphi)
+      zt2t1i22 = sin(pz+pphi) * sin(pz-pphi)
+      zd11 = cos(py)*cos(py)*(cos(px)+sin(px)*tan(py)*tan(py))
+      zd12 = cos(py)*cos(py)*tan(py)*(-cos(px)+sin(px))
+      zd22 = cos(py)*cos(py)*(sin(px)+cos(px)*tan(py)*tan(py))
+      zIIn1t2 = zn1t2i11 * zd11 + (zn1t2i12 + zn1t2i21) * zd12 + zn1t2i22 * zd22
+      zIIn2t1 = zn2t1i11 * zd11 + (zn2t1i12 + zn2t1i21) * zd12 + zn2t1i22 * zd22
+      zIIt1t2 = zt1t2i11 * zd11 + (zt1t2i12 + zt1t2i21) * zd12 + zt1t2i22 * zd22
+   
+      IF (-zIIn1t2>=rsmall) THEN
+      zHen1t2 = 1._wp
+      ELSE
+      zHen1t2 = 0._wp
+      ENDIF
+   
+      IF (-zIIn2t1>=rsmall) THEN
+      zHen2t1 = 1._wp
+      ELSE
+      zHen2t1 = 0._wp
+      ENDIF
+   
+      zs12r0 = (- zHen1t2 * zn1t2i12 - zHen2t1 * zn2t1i12)
+      zs21r0 = (- zHen1t2 * zn1t2i21 - zHen2t1 * zn2t1i21)
+      s12kr=0.5_wp*(zs12r0+zs21r0)
+   
+   END FUNCTION s12kr
+
+   FUNCTION s22kr(px,py,pz)
+      !!-------------------------------------------------------------------
+      !! Function : s22kr
+      !!-------------------------------------------------------------------
+      REAL(wp), INTENT(in   ) ::   px,py,pz
+
+      REAL(wp) ::   s22kr, zpih
+
+      REAL(wp) ::   zn1t2i11, zn1t2i12, zn1t2i21, zn1t2i22
+      REAL(wp) ::   zn2t1i11, zn2t1i12, zn2t1i21, zn2t1i22
+      REAL(wp) ::   zt1t2i11, zt1t2i12, zt1t2i21, zt1t2i22
+      REAL(wp) ::   zt2t1i11, zt2t1i12, zt2t1i21, zt2t1i22
+      REAL(wp) ::   zd11, zd12, zd22
+      REAL(wp) ::   zIIn1t2, zIIn2t1, zIIt1t2
+      REAL(wp) ::   zHen1t2, zHen2t1
+      !!-------------------------------------------------------------------
+      zpih = 0.5_wp*rpi
+
+      zn1t2i11 = cos(pz+zpih-pphi) * cos(pz+pphi)
+      zn1t2i12 = cos(pz+zpih-pphi) * sin(pz+pphi)
+      zn1t2i21 = sin(pz+zpih-pphi) * cos(pz+pphi)
+      zn1t2i22 = sin(pz+zpih-pphi) * sin(pz+pphi)
+      zn2t1i11 = cos(pz-zpih+pphi) * cos(pz-pphi)
+      zn2t1i12 = cos(pz-zpih+pphi) * sin(pz-pphi)
+      zn2t1i21 = sin(pz-zpih+pphi) * cos(pz-pphi)
+      zn2t1i22 = sin(pz-zpih+pphi) * sin(pz-pphi)
+      zt1t2i11 = cos(pz-pphi) * cos(pz+pphi)
+      zt1t2i12 = cos(pz-pphi) * sin(pz+pphi)
+      zt1t2i21 = sin(pz-pphi) * cos(pz+pphi)
+      zt1t2i22 = sin(pz-pphi) * sin(pz+pphi)
+      zt2t1i11 = cos(pz+pphi) * cos(pz-pphi)
+      zt2t1i12 = cos(pz+pphi) * sin(pz-pphi)
+      zt2t1i21 = sin(pz+pphi) * cos(pz-pphi)
+      zt2t1i22 = sin(pz+pphi) * sin(pz-pphi)
+      zd11 = cos(py)*cos(py)*(cos(px)+sin(px)*tan(py)*tan(py))
+      zd12 = cos(py)*cos(py)*tan(py)*(-cos(px)+sin(px))
+      zd22 = cos(py)*cos(py)*(sin(px)+cos(px)*tan(py)*tan(py))
+      zIIn1t2 = zn1t2i11 * zd11 + (zn1t2i12 + zn1t2i21) * zd12 + zn1t2i22 * zd22
+      zIIn2t1 = zn2t1i11 * zd11 + (zn2t1i12 + zn2t1i21) * zd12 + zn2t1i22 * zd22
+      zIIt1t2 = zt1t2i11 * zd11 + (zt1t2i12 + zt1t2i21) * zd12 + zt1t2i22 * zd22
+
+      IF (-zIIn1t2>=rsmall) THEN
+      zHen1t2 = 1._wp
+      ELSE
+      zHen1t2 = 0._wp
+      ENDIF
+
+      IF (-zIIn2t1>=rsmall) THEN
+      zHen2t1 = 1._wp
+      ELSE
+      zHen2t1 = 0._wp
+      ENDIF
+
+      s22kr = (- zHen1t2 * zn1t2i22 - zHen2t1 * zn2t1i22)
+
+   END FUNCTION s22kr
+
+   FUNCTION s11ks(px,py,pz)
+      !!-------------------------------------------------------------------
+      !! Function : s11ks
+      !!-------------------------------------------------------------------
+      REAL(wp), INTENT(in   ) ::   px,py,pz
+
+      REAL(wp) ::   s11ks, zpih
+
+      REAL(wp) ::   zn1t2i11, zn1t2i12, zn1t2i21, zn1t2i22
+      REAL(wp) ::   zn2t1i11, zn2t1i12, zn2t1i21, zn2t1i22
+      REAL(wp) ::   zt1t2i11, zt1t2i12, zt1t2i21, zt1t2i22
+      REAL(wp) ::   zt2t1i11, zt2t1i12, zt2t1i21, zt2t1i22
+      REAL(wp) ::   zd11, zd12, zd22
+      REAL(wp) ::   zIIn1t2, zIIn2t1, zIIt1t2
+      REAL(wp) ::   zHen1t2, zHen2t1
+      !!-------------------------------------------------------------------
+      zpih = 0.5_wp*rpi
+
+      zn1t2i11 = cos(pz+zpih-pphi) * cos(pz+pphi)
+      zn1t2i12 = cos(pz+zpih-pphi) * sin(pz+pphi)
+      zn1t2i21 = sin(pz+zpih-pphi) * cos(pz+pphi)
+      zn1t2i22 = sin(pz+zpih-pphi) * sin(pz+pphi)
+      zn2t1i11 = cos(pz-zpih+pphi) * cos(pz-pphi)
+      zn2t1i12 = cos(pz-zpih+pphi) * sin(pz-pphi)
+      zn2t1i21 = sin(pz-zpih+pphi) * cos(pz-pphi)
+      zn2t1i22 = sin(pz-zpih+pphi) * sin(pz-pphi)
+      zt1t2i11 = cos(pz-pphi) * cos(pz+pphi)
+      zt1t2i12 = cos(pz-pphi) * sin(pz+pphi)
+      zt1t2i21 = sin(pz-pphi) * cos(pz+pphi)
+      zt1t2i22 = sin(pz-pphi) * sin(pz+pphi)
+      zt2t1i11 = cos(pz+pphi) * cos(pz-pphi)
+      zt2t1i12 = cos(pz+pphi) * sin(pz-pphi)
+      zt2t1i21 = sin(pz+pphi) * cos(pz-pphi)
+      zt2t1i22 = sin(pz+pphi) * sin(pz-pphi)
+      zd11 = cos(py)*cos(py)*(cos(px)+sin(px)*tan(py)*tan(py))
+      zd12 = cos(py)*cos(py)*tan(py)*(-cos(px)+sin(px))
+      zd22 = cos(py)*cos(py)*(sin(px)+cos(px)*tan(py)*tan(py))
+      zIIn1t2 = zn1t2i11 * zd11 + (zn1t2i12 + zn1t2i21) * zd12 + zn1t2i22 * zd22
+      zIIn2t1 = zn2t1i11 * zd11 + (zn2t1i12 + zn2t1i21) * zd12 + zn2t1i22 * zd22
+      zIIt1t2 = zt1t2i11 * zd11 + (zt1t2i12 + zt1t2i21) * zd12 + zt1t2i22 * zd22
+
+      IF (-zIIn1t2>=rsmall) THEN
+      zHen1t2 = 1._wp
+      ELSE
+      zHen1t2 = 0._wp
+      ENDIF
+
+      IF (-zIIn2t1>=rsmall) THEN
+      zHen2t1 = 1._wp
+      ELSE
+      zHen2t1 = 0._wp
+      ENDIF
+
+      s11ks = sign(1._wp,zIIt1t2+rsmall)*(zHen1t2 * zt1t2i11 + zHen2t1 * zt2t1i11)
+
+   END FUNCTION s11ks
+
+   FUNCTION s12ks(px,py,pz)
+      !!-------------------------------------------------------------------
+      !! Function : s12ks
+      !!-------------------------------------------------------------------
+      REAL(wp), INTENT(in   ) ::   px,py,pz
+
+      REAL(wp) ::   s12ks, zs12s0, zs21s0, zpih
+
+      REAL(wp) ::   zn1t2i11, zn1t2i12, zn1t2i21, zn1t2i22
+      REAL(wp) ::   zn2t1i11, zn2t1i12, zn2t1i21, zn2t1i22
+      REAL(wp) ::   zt1t2i11, zt1t2i12, zt1t2i21, zt1t2i22
+      REAL(wp) ::   zt2t1i11, zt2t1i12, zt2t1i21, zt2t1i22
+      REAL(wp) ::   zd11, zd12, zd22
+      REAL(wp) ::   zIIn1t2, zIIn2t1, zIIt1t2
+      REAL(wp) ::   zHen1t2, zHen2t1
+      !!-------------------------------------------------------------------
+      zpih = 0.5_wp*rpi
+
+      zn1t2i11 = cos(pz+zpih-pphi) * cos(pz+pphi)
+      zn1t2i12 = cos(pz+zpih-pphi) * sin(pz+pphi)
+      zn1t2i21 = sin(pz+zpih-pphi) * cos(pz+pphi)
+      zn1t2i22 = sin(pz+zpih-pphi) * sin(pz+pphi)
+      zn2t1i11 = cos(pz-zpih+pphi) * cos(pz-pphi)
+      zn2t1i12 = cos(pz-zpih+pphi) * sin(pz-pphi)
+      zn2t1i21 = sin(pz-zpih+pphi) * cos(pz-pphi)
+      zn2t1i22 = sin(pz-zpih+pphi) * sin(pz-pphi)
+      zt1t2i11 = cos(pz-pphi) * cos(pz+pphi)
+      zt1t2i12 = cos(pz-pphi) * sin(pz+pphi)
+      zt1t2i21 = sin(pz-pphi) * cos(pz+pphi)
+      zt1t2i22 = sin(pz-pphi) * sin(pz+pphi)
+      zt2t1i11 = cos(pz+pphi) * cos(pz-pphi)
+      zt2t1i12 = cos(pz+pphi) * sin(pz-pphi)
+      zt2t1i21 = sin(pz+pphi) * cos(pz-pphi)
+      zt2t1i22 = sin(pz+pphi) * sin(pz-pphi)
+      zd11 = cos(py)*cos(py)*(cos(px)+sin(px)*tan(py)*tan(py))
+      zd12 = cos(py)*cos(py)*tan(py)*(-cos(px)+sin(px))
+      zd22 = cos(py)*cos(py)*(sin(px)+cos(px)*tan(py)*tan(py))
+      zIIn1t2 = zn1t2i11 * zd11 + (zn1t2i12 + zn1t2i21) * zd12 + zn1t2i22 * zd22
+      zIIn2t1 = zn2t1i11 * zd11 + (zn2t1i12 + zn2t1i21) * zd12 + zn2t1i22 * zd22
+      zIIt1t2 = zt1t2i11 * zd11 + (zt1t2i12 + zt1t2i21) * zd12 + zt1t2i22 * zd22
+
+      IF (-zIIn1t2>=rsmall) THEN
+      zHen1t2 = 1._wp
+      ELSE
+      zHen1t2 = 0._wp
+      ENDIF
+
+      IF (-zIIn2t1>=rsmall) THEN
+      zHen2t1 = 1._wp
+      ELSE
+      zHen2t1 = 0._wp
+      ENDIF
+
+      zs12s0 = sign(1._wp,zIIt1t2+rsmall)*(zHen1t2 * zt1t2i12 + zHen2t1 * zt2t1i12)
+      zs21s0 = sign(1._wp,zIIt1t2+rsmall)*(zHen1t2 * zt1t2i21 + zHen2t1 * zt2t1i21)
+      s12ks=0.5_wp*(zs12s0+zs21s0)
+
+   END FUNCTION s12ks
+
+   FUNCTION s22ks(px,py,pz) 
+      !!-------------------------------------------------------------------
+      !! Function : s22ks
+      !!-------------------------------------------------------------------
+      REAL(wp), INTENT(in   ) ::   px,py,pz
+
+      REAL(wp) ::   s22ks, zpih
+
+      REAL(wp) ::   zn1t2i11, zn1t2i12, zn1t2i21, zn1t2i22
+      REAL(wp) ::   zn2t1i11, zn2t1i12, zn2t1i21, zn2t1i22
+      REAL(wp) ::   zt1t2i11, zt1t2i12, zt1t2i21, zt1t2i22
+      REAL(wp) ::   zt2t1i11, zt2t1i12, zt2t1i21, zt2t1i22
+      REAL(wp) ::   zd11, zd12, zd22
+      REAL(wp) ::   zIIn1t2, zIIn2t1, zIIt1t2
+      REAL(wp) ::   zHen1t2, zHen2t1
+      !!-------------------------------------------------------------------
+      zpih = 0.5_wp*rpi
+
+      zn1t2i11 = cos(pz+zpih-pphi) * cos(pz+pphi)
+      zn1t2i12 = cos(pz+zpih-pphi) * sin(pz+pphi)
+      zn1t2i21 = sin(pz+zpih-pphi) * cos(pz+pphi)
+      zn1t2i22 = sin(pz+zpih-pphi) * sin(pz+pphi)
+      zn2t1i11 = cos(pz-zpih+pphi) * cos(pz-pphi)
+      zn2t1i12 = cos(pz-zpih+pphi) * sin(pz-pphi)
+      zn2t1i21 = sin(pz-zpih+pphi) * cos(pz-pphi)
+      zn2t1i22 = sin(pz-zpih+pphi) * sin(pz-pphi)
+      zt1t2i11 = cos(pz-pphi) * cos(pz+pphi)
+      zt1t2i12 = cos(pz-pphi) * sin(pz+pphi)
+      zt1t2i21 = sin(pz-pphi) * cos(pz+pphi)
+      zt1t2i22 = sin(pz-pphi) * sin(pz+pphi)
+      zt2t1i11 = cos(pz+pphi) * cos(pz-pphi)
+      zt2t1i12 = cos(pz+pphi) * sin(pz-pphi)
+      zt2t1i21 = sin(pz+pphi) * cos(pz-pphi)
+      zt2t1i22 = sin(pz+pphi) * sin(pz-pphi)
+      zd11 = cos(py)*cos(py)*(cos(px)+sin(px)*tan(py)*tan(py))
+      zd12 = cos(py)*cos(py)*tan(py)*(-cos(px)+sin(px))
+      zd22 = cos(py)*cos(py)*(sin(px)+cos(px)*tan(py)*tan(py))
+      zIIn1t2 = zn1t2i11 * zd11 + (zn1t2i12 + zn1t2i21) * zd12 + zn1t2i22 * zd22
+      zIIn2t1 = zn2t1i11 * zd11 + (zn2t1i12 + zn2t1i21) * zd12 + zn2t1i22 * zd22
+      zIIt1t2 = zt1t2i11 * zd11 + (zt1t2i12 + zt1t2i21) * zd12 + zt1t2i22 * zd22
+
+      IF (-zIIn1t2>=rsmall) THEN
+      zHen1t2 = 1._wp
+      ELSE
+      zHen1t2 = 0._wp
+      ENDIF
+
+      IF (-zIIn2t1>=rsmall) THEN
+      zHen2t1 = 1._wp
+      ELSE
+      zHen2t1 = 0._wp
+      ENDIF
+
+      s22ks = sign(1._wp,zIIt1t2+rsmall)*(zHen1t2 * zt1t2i22 + zHen2t1 * zt2t1i22)
+
+   END FUNCTION s22ks
+
+#else
+   !!----------------------------------------------------------------------
+   !!   Default option         Empty module           NO SI3 sea-ice model
+   !!----------------------------------------------------------------------
+#endif
+
+   !!==============================================================================
+END MODULE icedyn_rhg_eap
Index: /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/MY_SRC/icedyn_rhg_evp.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/MY_SRC/icedyn_rhg_evp.F90	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/MY_SRC/icedyn_rhg_evp.F90	(revision 14016)
@@ -0,0 +1,1077 @@
+MODULE icedyn_rhg_evp
+   !!======================================================================
+   !!                     ***  MODULE  icedyn_rhg_evp  ***
+   !!   Sea-Ice dynamics : rheology Elasto-Viscous-Plastic
+   !!======================================================================
+   !! History :   -   !  2007-03  (M.A. Morales Maqueda, S. Bouillon) Original code
+   !!            3.0  !  2008-03  (M. Vancoppenolle) adaptation to new model
+   !!             -   !  2008-11  (M. Vancoppenolle, S. Bouillon, Y. Aksenov) add surface tilt in ice rheolohy 
+   !!            3.3  !  2009-05  (G.Garric)    addition of the evp case
+   !!            3.4  !  2011-01  (A. Porter)   dynamical allocation 
+   !!            3.5  !  2012-08  (R. Benshila) AGRIF
+   !!            3.6  !  2016-06  (C. Rousset)  Rewriting + landfast ice + mEVP (Bouillon 2013)
+   !!            3.7  !  2017     (C. Rousset)  add aEVP (Kimmritz 2016-2017)
+   !!            4.0  !  2018     (many people) SI3 [aka Sea Ice cube]
+   !!----------------------------------------------------------------------
+#if defined key_si3
+   !!----------------------------------------------------------------------
+   !!   'key_si3'                                       SI3 sea-ice model
+   !!----------------------------------------------------------------------
+   !!   ice_dyn_rhg_evp : computes ice velocities from EVP rheology
+   !!   rhg_evp_rst     : read/write EVP fields in ice restart
+   !!----------------------------------------------------------------------
+   USE phycst         ! Physical constant
+   USE dom_oce        ! Ocean domain
+   USE sbc_oce , ONLY : ln_ice_embd, nn_fsbc, ssh_m
+   USE sbc_ice , ONLY : utau_ice, vtau_ice, snwice_mass, snwice_mass_b
+   USE ice            ! sea-ice: ice variables
+   USE icevar         ! ice_var_sshdyn
+   USE icedyn_rdgrft  ! sea-ice: ice strength
+   USE bdy_oce , ONLY : ln_bdy 
+   USE bdyice 
+#if defined key_agrif
+   USE agrif_ice_interp
+#endif
+   !
+   USE in_out_manager ! I/O manager
+   USE iom            ! I/O manager library
+   USE lib_mpp        ! MPP library
+   USE lib_fortran    ! fortran utilities (glob_sum + no signed zero)
+   USE lbclnk         ! lateral boundary conditions (or mpp links)
+   USE prtctl         ! Print control
+
+   USE netcdf         ! NetCDF library for convergence test
+   IMPLICIT NONE
+   PRIVATE
+
+   PUBLIC   ice_dyn_rhg_evp   ! called by icedyn_rhg.F90
+   PUBLIC   rhg_evp_rst       ! called by icedyn_rhg.F90
+
+   !! * Substitutions
+#  include "do_loop_substitute.h90"
+#  include "domzgr_substitute.h90"
+
+   !! for convergence tests
+   INTEGER ::   ncvgid   ! netcdf file id
+   INTEGER ::   nvarid   ! netcdf variable id
+   REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   zmsk00, zmsk15
+   !!----------------------------------------------------------------------
+   !! NEMO/ICE 4.0 , NEMO Consortium (2018)
+   !! $Id: icedyn_rhg_evp.F90 13612 2020-10-14 17:18:19Z clem $
+   !! Software governed by the CeCILL license (see ./LICENSE)
+   !!----------------------------------------------------------------------
+CONTAINS
+
+   SUBROUTINE ice_dyn_rhg_evp( kt, Kmm, pstress1_i, pstress2_i, pstress12_i, pshear_i, pdivu_i, pdelta_i )
+      !!-------------------------------------------------------------------
+      !!                 ***  SUBROUTINE ice_dyn_rhg_evp  ***
+      !!                             EVP-C-grid
+      !!
+      !! ** purpose : determines sea ice drift from wind stress, ice-ocean
+      !!  stress and sea-surface slope. Ice-ice interaction is described by 
+      !!  a non-linear elasto-viscous-plastic (EVP) law including shear 
+      !!  strength and a bulk rheology (Hunke and Dukowicz, 2002).	
+      !!
+      !!  The points in the C-grid look like this, dear reader
+      !!
+      !!                              (ji,jj)
+      !!                                 |
+      !!                                 |
+      !!                      (ji-1,jj)  |  (ji,jj)
+      !!                             ---------   
+      !!                            |         |
+      !!                            | (ji,jj) |------(ji,jj)
+      !!                            |         |
+      !!                             ---------   
+      !!                     (ji-1,jj-1)     (ji,jj-1)
+      !!
+      !! ** Inputs  : - wind forcing (stress), oceanic currents
+      !!                ice total volume (vt_i) per unit area
+      !!                snow total volume (vt_s) per unit area
+      !!
+      !! ** Action  : - compute u_ice, v_ice : the components of the 
+      !!                sea-ice velocity vector
+      !!              - compute delta_i, shear_i, divu_i, which are inputs
+      !!                of the ice thickness distribution
+      !!
+      !! ** Steps   : 0) compute mask at F point
+      !!              1) Compute ice snow mass, ice strength 
+      !!              2) Compute wind, oceanic stresses, mass terms and
+      !!                 coriolis terms of the momentum equation
+      !!              3) Solve the momentum equation (iterative procedure)
+      !!              4) Recompute delta, shear and divergence
+      !!                 (which are inputs of the ITD) & store stress
+      !!                 for the next time step
+      !!              5) Diagnostics including charge ellipse
+      !!
+      !! ** Notes   : There is the possibility to use aEVP from the nice work of Kimmritz et al. (2016 & 2017)
+      !!              by setting up ln_aEVP=T (i.e. changing alpha and beta parameters).
+      !!              This is an upgraded version of mEVP from Bouillon et al. 2013
+      !!              (i.e. more stable and better convergence)
+      !!
+      !! References : Hunke and Dukowicz, JPO97
+      !!              Bouillon et al., Ocean Modelling 2009
+      !!              Bouillon et al., Ocean Modelling 2013
+      !!              Kimmritz et al., Ocean Modelling 2016 & 2017
+      !!-------------------------------------------------------------------
+      INTEGER                 , INTENT(in   ) ::   kt                                    ! time step
+      INTEGER                 , INTENT(in   ) ::   Kmm                                   ! ocean time level index
+      REAL(wp), DIMENSION(:,:), INTENT(inout) ::   pstress1_i, pstress2_i, pstress12_i   !
+      REAL(wp), DIMENSION(:,:), INTENT(  out) ::   pshear_i  , pdivu_i   , pdelta_i      !
+      !!
+      INTEGER ::   ji, jj       ! dummy loop indices
+      INTEGER ::   jter         ! local integers
+      !
+      REAL(wp) ::   zrhoco                                              ! rho0 * rn_cio
+      REAL(wp) ::   zdtevp, z1_dtevp                                    ! time step for subcycling
+      REAL(wp) ::   ecc2, z1_ecc2                                       ! square of yield ellipse eccenticity
+      REAL(wp) ::   zalph1, z1_alph1, zalph2, z1_alph2                  ! alpha coef from Bouillon 2009 or Kimmritz 2017
+      REAl(wp) ::   zbetau, zbetav
+      REAL(wp) ::   zm1, zm2, zm3, zmassU, zmassV, zvU, zvV             ! ice/snow mass and volume
+      REAL(wp) ::   zp_delf, zds2, zdt, zdt2, zdiv, zdiv2               ! temporary scalars
+      REAL(wp) ::   zTauO, zTauB, zRHS, zvel                            ! temporary scalars
+      REAL(wp) ::   zkt                                                 ! isotropic tensile strength for landfast ice
+      REAL(wp) ::   zvCr                                                ! critical ice volume above which ice is landfast
+      !
+      REAL(wp) ::   zintb, zintn                                        ! dummy argument
+      REAL(wp) ::   zfac_x, zfac_y
+      REAL(wp) ::   zshear, zdum1, zdum2
+      REAL(wp) ::   zinvw                                                ! for test case
+
+      !
+      REAL(wp), DIMENSION(jpi,jpj) ::   zdelta, zp_delt                 ! delta and P/delta at T points
+      REAL(wp), DIMENSION(jpi,jpj) ::   zbeta                           ! beta coef from Kimmritz 2017
+      !
+      REAL(wp), DIMENSION(jpi,jpj) ::   zdt_m                           ! (dt / ice-snow_mass) on T points
+      REAL(wp), DIMENSION(jpi,jpj) ::   zaU  , zaV                      ! ice fraction on U/V points
+      REAL(wp), DIMENSION(jpi,jpj) ::   zmU_t, zmV_t                    ! (ice-snow_mass / dt) on U/V points
+      REAL(wp), DIMENSION(jpi,jpj) ::   zmf                             ! coriolis parameter at T points
+      REAL(wp), DIMENSION(jpi,jpj) ::   v_oceU, u_oceV, v_iceU, u_iceV  ! ocean/ice u/v component on V/U points
+      !
+      REAL(wp), DIMENSION(jpi,jpj) ::   zds                             ! shear
+      REAL(wp), DIMENSION(jpi,jpj) ::   zten_i                          ! tension
+      REAL(wp), DIMENSION(jpi,jpj) ::   zs1, zs2, zs12                  ! stress tensor components
+      REAL(wp), DIMENSION(jpi,jpj) ::   zsshdyn                         ! array used for the calculation of ice surface slope:
+      !                                                                 !    ocean surface (ssh_m) if ice is not embedded
+      !                                                                 !    ice bottom surface if ice is embedded   
+      REAL(wp), DIMENSION(jpi,jpj) ::   zfU  , zfV                      ! internal stresses
+      REAL(wp), DIMENSION(jpi,jpj) ::   zspgU, zspgV                    ! surface pressure gradient at U/V points
+      REAL(wp), DIMENSION(jpi,jpj) ::   zCorU, zCorV                    ! Coriolis stress array
+      REAL(wp), DIMENSION(jpi,jpj) ::   ztaux_ai, ztauy_ai              ! ice-atm. stress at U-V points
+      REAL(wp), DIMENSION(jpi,jpj) ::   ztaux_oi, ztauy_oi              ! ice-ocean stress at U-V points
+      REAL(wp), DIMENSION(jpi,jpj) ::   ztaux_bi, ztauy_bi              ! ice-OceanBottom stress at U-V points (landfast)
+      REAL(wp), DIMENSION(jpi,jpj) ::   ztaux_base, ztauy_base          ! ice-bottom stress at U-V points (landfast)
+      !
+      REAL(wp), DIMENSION(jpi,jpj) ::   zmsk01x, zmsk01y                ! dummy arrays
+      REAL(wp), DIMENSION(jpi,jpj) ::   zmsk00x, zmsk00y                ! mask for ice presence
+      REAL(wp), DIMENSION(jpi,jpj) ::   zfmask                          ! mask at F points for the ice
+
+      REAL(wp), PARAMETER          ::   zepsi  = 1.0e-20_wp             ! tolerance parameter
+      REAL(wp), PARAMETER          ::   zmmin  = 1._wp                  ! ice mass (kg/m2)  below which ice velocity becomes very small
+      REAL(wp), PARAMETER          ::   zamin  = 0.001_wp               ! ice concentration below which ice velocity becomes very small
+      !! --- check convergence
+      REAL(wp), DIMENSION(jpi,jpj) ::   zu_ice, zv_ice
+      !! --- diags
+      REAL(wp) ::   zsig1, zsig2, zsig12, zfac, z1_strength
+      REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   zsig_I, zsig_II, zsig1_p, zsig2_p         
+      !! --- SIMIP diags
+      REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   zdiag_xmtrp_ice ! X-component of ice mass transport (kg/s)
+      REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   zdiag_ymtrp_ice ! Y-component of ice mass transport (kg/s)
+      REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   zdiag_xmtrp_snw ! X-component of snow mass transport (kg/s)
+      REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   zdiag_ymtrp_snw ! Y-component of snow mass transport (kg/s)
+      REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   zdiag_xatrp     ! X-component of area transport (m2/s)
+      REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   zdiag_yatrp     ! Y-component of area transport (m2/s)      
+      !!-------------------------------------------------------------------
+
+      IF( kt == nit000 .AND. lwp )   WRITE(numout,*) '-- ice_dyn_rhg_evp: EVP sea-ice rheology'
+      !
+      ! for diagnostics and convergence tests
+      ALLOCATE( zmsk00(jpi,jpj), zmsk15(jpi,jpj) )
+      DO_2D( 1, 1, 1, 1 )
+         zmsk00(ji,jj) = MAX( 0._wp , SIGN( 1._wp , at_i(ji,jj) - epsi06  ) ) ! 1 if ice    , 0 if no ice
+         zmsk15(ji,jj) = MAX( 0._wp , SIGN( 1._wp , at_i(ji,jj) - 0.15_wp ) ) ! 1 if 15% ice, 0 if less
+      END_2D
+      !
+      !!gm for Clem:  OPTIMIZATION:  I think zfmask can be computed one for all at the initialization....
+      !------------------------------------------------------------------------------!
+      ! 0) mask at F points for the ice
+      !------------------------------------------------------------------------------!
+      ! ocean/land mask
+      DO_2D( 1, 0, 1, 0 )
+         zfmask(ji,jj) = tmask(ji,jj,1) * tmask(ji+1,jj,1) * tmask(ji,jj+1,1) * tmask(ji+1,jj+1,1)
+      END_2D
+      CALL lbc_lnk( 'icedyn_rhg_evp', zfmask, 'F', 1._wp )
+
+      ! Lateral boundary conditions on velocity (modify zfmask)
+      DO_2D( 0, 0, 0, 0 )
+         IF( zfmask(ji,jj) == 0._wp ) THEN
+            zfmask(ji,jj) = rn_ishlat * MIN( 1._wp , MAX( umask(ji,jj,1), umask(ji,jj+1,1), &
+               &                                          vmask(ji,jj,1), vmask(ji+1,jj,1) ) )
+         ENDIF
+      END_2D
+      DO jj = 2, jpjm1
+         IF( zfmask(1,jj) == 0._wp ) THEN
+            zfmask(1  ,jj) = rn_ishlat * MIN( 1._wp , MAX( vmask(2,jj,1), umask(1,jj+1,1), umask(1,jj,1) ) )
+         ENDIF
+         IF( zfmask(jpi,jj) == 0._wp ) THEN
+            zfmask(jpi,jj) = rn_ishlat * MIN( 1._wp , MAX( umask(jpi,jj+1,1), vmask(jpim1,jj,1), umask(jpi,jj-1,1) ) )
+        ENDIF
+      END DO
+      DO ji = 2, jpim1
+         IF( zfmask(ji,1) == 0._wp ) THEN
+            zfmask(ji, 1 ) = rn_ishlat * MIN( 1._wp , MAX( vmask(ji+1,1,1), umask(ji,2,1), vmask(ji,1,1) ) )
+         ENDIF
+         IF( zfmask(ji,jpj) == 0._wp ) THEN
+            zfmask(ji,jpj) = rn_ishlat * MIN( 1._wp , MAX( vmask(ji+1,jpj,1), vmask(ji-1,jpj,1), umask(ji,jpjm1,1) ) )
+         ENDIF
+      END DO
+      CALL lbc_lnk( 'icedyn_rhg_evp', zfmask, 'F', 1._wp )
+
+      !------------------------------------------------------------------------------!
+      ! 1) define some variables and initialize arrays
+      !------------------------------------------------------------------------------!
+      zrhoco = rho0 * rn_cio 
+!extra code for test case boundary conditions
+      zinvw=1._wp/(zrhoco*0.5_wp)
+
+      ! ecc2: square of yield ellipse eccenticrity
+      ecc2    = rn_ecc * rn_ecc
+      z1_ecc2 = 1._wp / ecc2
+
+      ! alpha parameters (Bouillon 2009)
+      IF( .NOT. ln_aEVP ) THEN
+         zdtevp   = rDt_ice / REAL( nn_nevp )
+         zalph1 =   2._wp * rn_relast * REAL( nn_nevp )
+         zalph2 = zalph1 * z1_ecc2
+
+         z1_alph1 = 1._wp / ( zalph1 + 1._wp )
+         z1_alph2 = 1._wp / ( zalph2 + 1._wp )
+      ELSE
+         zdtevp   = rdt_ice
+         ! zalpha parameters set later on adaptatively
+      ENDIF
+      z1_dtevp = 1._wp / zdtevp
+         
+      ! Initialise stress tensor 
+      zs1 (:,:) = pstress1_i (:,:) 
+      zs2 (:,:) = pstress2_i (:,:)
+      zs12(:,:) = pstress12_i(:,:)
+
+      ! Ice strength
+      CALL ice_strength
+
+      ! landfast param from Lemieux(2016): add isotropic tensile strength (following Konig Beatty and Holland, 2010)
+      IF( ln_landfast_L16 ) THEN   ;   zkt = rn_lf_tensile
+      ELSE                         ;   zkt = 0._wp
+      ENDIF
+      !
+      !------------------------------------------------------------------------------!
+      ! 2) Wind / ocean stress, mass terms, coriolis terms
+      !------------------------------------------------------------------------------!
+      ! sea surface height
+      !    embedded sea ice: compute representative ice top surface
+      !    non-embedded sea ice: use ocean surface for slope calculation
+      zsshdyn(:,:) = ice_var_sshdyn( ssh_m, snwice_mass, snwice_mass_b)
+
+      DO_2D( 0, 0, 0, 0 )
+
+         ! ice fraction at U-V points
+         zaU(ji,jj) = 0.5_wp * ( at_i(ji,jj) * e1e2t(ji,jj) + at_i(ji+1,jj) * e1e2t(ji+1,jj) ) * r1_e1e2u(ji,jj) * umask(ji,jj,1)
+         zaV(ji,jj) = 0.5_wp * ( at_i(ji,jj) * e1e2t(ji,jj) + at_i(ji,jj+1) * e1e2t(ji,jj+1) ) * r1_e1e2v(ji,jj) * vmask(ji,jj,1)
+
+         ! Ice/snow mass at U-V points
+         zm1 = ( rhos * vt_s(ji  ,jj  ) + rhoi * vt_i(ji  ,jj  ) )
+         zm2 = ( rhos * vt_s(ji+1,jj  ) + rhoi * vt_i(ji+1,jj  ) )
+         zm3 = ( rhos * vt_s(ji  ,jj+1) + rhoi * vt_i(ji  ,jj+1) )
+         zmassU = 0.5_wp * ( zm1 * e1e2t(ji,jj) + zm2 * e1e2t(ji+1,jj) ) * r1_e1e2u(ji,jj) * umask(ji,jj,1)
+         zmassV = 0.5_wp * ( zm1 * e1e2t(ji,jj) + zm3 * e1e2t(ji,jj+1) ) * r1_e1e2v(ji,jj) * vmask(ji,jj,1)
+
+         ! Ocean currents at U-V points
+         v_oceU(ji,jj)   = 0.25_wp * ( v_oce(ji,jj) + v_oce(ji,jj-1) + v_oce(ji+1,jj) + v_oce(ji+1,jj-1) ) * umask(ji,jj,1)
+         u_oceV(ji,jj)   = 0.25_wp * ( u_oce(ji,jj) + u_oce(ji-1,jj) + u_oce(ji,jj+1) + u_oce(ji-1,jj+1) ) * vmask(ji,jj,1)
+
+         ! Coriolis at T points (m*f)
+         zmf(ji,jj)      = zm1 * ff_t(ji,jj)
+
+         ! dt/m at T points (for alpha and beta coefficients)
+         zdt_m(ji,jj)    = zdtevp / MAX( zm1, zmmin )
+         
+         ! m/dt
+         zmU_t(ji,jj)    = zmassU * z1_dtevp
+         zmV_t(ji,jj)    = zmassV * z1_dtevp
+         
+         ! Drag ice-atm.
+         ztaux_ai(ji,jj) = zaU(ji,jj) * utau_ice(ji,jj)
+         ztauy_ai(ji,jj) = zaV(ji,jj) * vtau_ice(ji,jj)
+
+         ! Surface pressure gradient (- m*g*GRAD(ssh)) at U-V points
+         zspgU(ji,jj)    = - zmassU * grav * ( zsshdyn(ji+1,jj) - zsshdyn(ji,jj) ) * r1_e1u(ji,jj)
+         zspgV(ji,jj)    = - zmassV * grav * ( zsshdyn(ji,jj+1) - zsshdyn(ji,jj) ) * r1_e2v(ji,jj)
+
+         ! masks
+         zmsk00x(ji,jj) = 1._wp - MAX( 0._wp, SIGN( 1._wp, -zmassU ) )  ! 0 if no ice
+         zmsk00y(ji,jj) = 1._wp - MAX( 0._wp, SIGN( 1._wp, -zmassV ) )  ! 0 if no ice
+
+         ! switches
+         IF( zmassU <= zmmin .AND. zaU(ji,jj) <= zamin ) THEN   ;   zmsk01x(ji,jj) = 0._wp
+         ELSE                                                   ;   zmsk01x(ji,jj) = 1._wp   ;   ENDIF
+         IF( zmassV <= zmmin .AND. zaV(ji,jj) <= zamin ) THEN   ;   zmsk01y(ji,jj) = 0._wp
+         ELSE                                                   ;   zmsk01y(ji,jj) = 1._wp   ;   ENDIF
+
+      END_2D
+      CALL lbc_lnk_multi( 'icedyn_rhg_evp', zmf, 'T', 1.0_wp, zdt_m, 'T', 1.0_wp )
+      !
+      !                                  !== Landfast ice parameterization ==!
+      !
+      IF( ln_landfast_L16 ) THEN         !-- Lemieux 2016
+         DO_2D( 0, 0, 0, 0 )
+            ! ice thickness at U-V points
+            zvU = 0.5_wp * ( vt_i(ji,jj) * e1e2t(ji,jj) + vt_i(ji+1,jj) * e1e2t(ji+1,jj) ) * r1_e1e2u(ji,jj) * umask(ji,jj,1)
+            zvV = 0.5_wp * ( vt_i(ji,jj) * e1e2t(ji,jj) + vt_i(ji,jj+1) * e1e2t(ji,jj+1) ) * r1_e1e2v(ji,jj) * vmask(ji,jj,1)
+            ! ice-bottom stress at U points
+            zvCr = zaU(ji,jj) * rn_lf_depfra * hu(ji,jj,Kmm)
+            ztaux_base(ji,jj) = - rn_lf_bfr * MAX( 0._wp, zvU - zvCr ) * EXP( -rn_crhg * ( 1._wp - zaU(ji,jj) ) )
+            ! ice-bottom stress at V points
+            zvCr = zaV(ji,jj) * rn_lf_depfra * hv(ji,jj,Kmm)
+            ztauy_base(ji,jj) = - rn_lf_bfr * MAX( 0._wp, zvV - zvCr ) * EXP( -rn_crhg * ( 1._wp - zaV(ji,jj) ) )
+            ! ice_bottom stress at T points
+            zvCr = at_i(ji,jj) * rn_lf_depfra * ht(ji,jj)
+            tau_icebfr(ji,jj) = - rn_lf_bfr * MAX( 0._wp, vt_i(ji,jj) - zvCr ) * EXP( -rn_crhg * ( 1._wp - at_i(ji,jj) ) )
+         END_2D
+         CALL lbc_lnk( 'icedyn_rhg_evp', tau_icebfr(:,:), 'T', 1.0_wp )
+         !
+      ELSE                               !-- no landfast
+         DO_2D( 0, 0, 0, 0 )
+            ztaux_base(ji,jj) = 0._wp
+            ztauy_base(ji,jj) = 0._wp
+         END_2D
+      ENDIF
+
+      !------------------------------------------------------------------------------!
+      ! 3) Solution of the momentum equation, iterative procedure
+      !------------------------------------------------------------------------------!
+      !
+      !                                               ! ==================== !
+      DO jter = 1 , nn_nevp                           !    loop over jter    !
+         !                                            ! ==================== !        
+         l_full_nf_update = jter == nn_nevp   ! false: disable full North fold update (performances) for iter = 1 to nn_nevp-1
+         !
+         ! convergence test
+         IF( nn_rhg_chkcvg == 1 .OR. nn_rhg_chkcvg == 2  ) THEN
+            DO_2D( nn_hls, nn_hls, nn_hls, nn_hls )
+               zu_ice(ji,jj) = u_ice(ji,jj) * umask(ji,jj,1) ! velocity at previous time step
+               zv_ice(ji,jj) = v_ice(ji,jj) * vmask(ji,jj,1)
+            END_2D
+         ENDIF
+
+         ! --- divergence, tension & shear (Appendix B of Hunke & Dukowicz, 2002) --- !
+         DO_2D( 1, 0, 1, 0 )
+
+            ! shear at F points
+            zds(ji,jj) = ( ( u_ice(ji,jj+1) * r1_e1u(ji,jj+1) - u_ice(ji,jj) * r1_e1u(ji,jj) ) * e1f(ji,jj) * e1f(ji,jj)   &
+               &         + ( v_ice(ji+1,jj) * r1_e2v(ji+1,jj) - v_ice(ji,jj) * r1_e2v(ji,jj) ) * e2f(ji,jj) * e2f(ji,jj)   &
+               &         ) * r1_e1e2f(ji,jj) * zfmask(ji,jj)
+
+         END_2D
+
+         DO_2D( 0, 0, 0, 0 )
+
+            ! shear**2 at T points (doc eq. A16)
+            zds2 = ( zds(ji,jj  ) * zds(ji,jj  ) * e1e2f(ji,jj  ) + zds(ji-1,jj  ) * zds(ji-1,jj  ) * e1e2f(ji-1,jj  )  &
+               &   + zds(ji,jj-1) * zds(ji,jj-1) * e1e2f(ji,jj-1) + zds(ji-1,jj-1) * zds(ji-1,jj-1) * e1e2f(ji-1,jj-1)  &
+               &   ) * 0.25_wp * r1_e1e2t(ji,jj)
+           
+            ! divergence at T points
+            zdiv  = ( e2u(ji,jj) * u_ice(ji,jj) - e2u(ji-1,jj) * u_ice(ji-1,jj)   &
+               &    + e1v(ji,jj) * v_ice(ji,jj) - e1v(ji,jj-1) * v_ice(ji,jj-1)   &
+               &    ) * r1_e1e2t(ji,jj)
+            zdiv2 = zdiv * zdiv
+            
+            ! tension at T points
+            zdt  = ( ( u_ice(ji,jj) * r1_e2u(ji,jj) - u_ice(ji-1,jj) * r1_e2u(ji-1,jj) ) * e2t(ji,jj) * e2t(ji,jj)   &
+               &   - ( v_ice(ji,jj) * r1_e1v(ji,jj) - v_ice(ji,jj-1) * r1_e1v(ji,jj-1) ) * e1t(ji,jj) * e1t(ji,jj)   &
+               &   ) * r1_e1e2t(ji,jj)
+            zdt2 = zdt * zdt
+            
+            ! delta at T points
+            zdelta(ji,jj) = SQRT( zdiv2 + ( zdt2 + zds2 ) * z1_ecc2 )  
+
+         END_2D
+         CALL lbc_lnk( 'icedyn_rhg_evp', zdelta, 'T', 1.0_wp )
+
+         ! P/delta at T points
+         DO_2D( 1, 1, 1, 1 )
+            zp_delt(ji,jj) = strength(ji,jj) / ( zdelta(ji,jj) + rn_creepl )
+         END_2D
+
+         DO_2D( 0, 1, 0, 1 )   ! loop ends at jpi,jpj so that no lbc_lnk are needed for zs1 and zs2
+
+            ! divergence at T points (duplication to avoid communications)
+            zdiv  = ( e2u(ji,jj) * u_ice(ji,jj) - e2u(ji-1,jj) * u_ice(ji-1,jj)   &
+               &    + e1v(ji,jj) * v_ice(ji,jj) - e1v(ji,jj-1) * v_ice(ji,jj-1)   &
+               &    ) * r1_e1e2t(ji,jj)
+            
+            ! tension at T points (duplication to avoid communications)
+            zdt  = ( ( u_ice(ji,jj) * r1_e2u(ji,jj) - u_ice(ji-1,jj) * r1_e2u(ji-1,jj) ) * e2t(ji,jj) * e2t(ji,jj)   &
+               &   - ( v_ice(ji,jj) * r1_e1v(ji,jj) - v_ice(ji,jj-1) * r1_e1v(ji,jj-1) ) * e1t(ji,jj) * e1t(ji,jj)   &
+               &   ) * r1_e1e2t(ji,jj)
+            
+            ! alpha for aEVP
+            !   gamma = 0.5*P/(delta+creepl) * (c*pi)**2/Area * dt/m
+            !   alpha = beta = sqrt(4*gamma)
+            IF( ln_aEVP ) THEN
+               zalph1   = MAX( 50._wp, rpi * SQRT( 0.5_wp * zp_delt(ji,jj) * r1_e1e2t(ji,jj) * zdt_m(ji,jj) ) )
+               z1_alph1 = 1._wp / ( zalph1 + 1._wp )
+               zalph2   = zalph1
+               z1_alph2 = z1_alph1
+               ! explicit:
+               ! z1_alph1 = 1._wp / zalph1
+               ! z1_alph2 = 1._wp / zalph1
+               ! zalph1 = zalph1 - 1._wp
+               ! zalph2 = zalph1
+            ENDIF
+            
+            ! stress at T points (zkt/=0 if landfast)
+            zs1(ji,jj) = ( zs1(ji,jj)*zalph1 + zp_delt(ji,jj) * ( zdiv*(1._wp + zkt) - zdelta(ji,jj)*(1._wp - zkt) ) ) * z1_alph1
+            zs2(ji,jj) = ( zs2(ji,jj)*zalph2 + zp_delt(ji,jj) * ( zdt * z1_ecc2 * (1._wp + zkt) ) ) * z1_alph2
+          
+         END_2D
+
+         ! Save beta at T-points for further computations
+         IF( ln_aEVP ) THEN
+            DO_2D( 1, 1, 1, 1 )
+               zbeta(ji,jj) = MAX( 50._wp, rpi * SQRT( 0.5_wp * zp_delt(ji,jj) * r1_e1e2t(ji,jj) * zdt_m(ji,jj) ) )
+            END_2D
+         ENDIF
+         
+         DO_2D( 1, 0, 1, 0 )
+
+            ! alpha for aEVP
+            IF( ln_aEVP ) THEN
+               zalph2   = MAX( zbeta(ji,jj), zbeta(ji+1,jj), zbeta(ji,jj+1), zbeta(ji+1,jj+1) )
+               z1_alph2 = 1._wp / ( zalph2 + 1._wp )
+               ! explicit:
+               ! z1_alph2 = 1._wp / zalph2
+               ! zalph2 = zalph2 - 1._wp
+            ENDIF
+            
+            ! P/delta at F points
+            zp_delf = 0.25_wp * ( zp_delt(ji,jj) + zp_delt(ji+1,jj) + zp_delt(ji,jj+1) + zp_delt(ji+1,jj+1) )
+            
+            ! stress at F points (zkt/=0 if landfast)
+            zs12(ji,jj)= ( zs12(ji,jj) * zalph2 + zp_delf * ( zds(ji,jj) * z1_ecc2 * (1._wp + zkt) ) * 0.5_wp ) * z1_alph2
+
+         END_2D
+
+         ! --- Ice internal stresses (Appendix C of Hunke and Dukowicz, 2002) --- !
+         DO_2D( 0, 0, 0, 0 )
+            !                   !--- U points
+            zfU(ji,jj) = 0.5_wp * ( ( zs1(ji+1,jj) - zs1(ji,jj) ) * e2u(ji,jj)                                             &
+               &                  + ( zs2(ji+1,jj) * e2t(ji+1,jj) * e2t(ji+1,jj) - zs2(ji,jj) * e2t(ji,jj) * e2t(ji,jj)    &
+               &                    ) * r1_e2u(ji,jj)                                                                      &
+               &                  + ( zs12(ji,jj) * e1f(ji,jj) * e1f(ji,jj) - zs12(ji,jj-1) * e1f(ji,jj-1) * e1f(ji,jj-1)  &
+               &                    ) * 2._wp * r1_e1u(ji,jj)                                                              &
+               &                  ) * r1_e1e2u(ji,jj)
+            !
+            !                !--- V points
+            zfV(ji,jj) = 0.5_wp * ( ( zs1(ji,jj+1) - zs1(ji,jj) ) * e1v(ji,jj)                                             &
+               &                  - ( zs2(ji,jj+1) * e1t(ji,jj+1) * e1t(ji,jj+1) - zs2(ji,jj) * e1t(ji,jj) * e1t(ji,jj)    &
+               &                    ) * r1_e1v(ji,jj)                                                                      &
+               &                  + ( zs12(ji,jj) * e2f(ji,jj) * e2f(ji,jj) - zs12(ji-1,jj) * e2f(ji-1,jj) * e2f(ji-1,jj)  &
+               &                    ) * 2._wp * r1_e2v(ji,jj)                                                              &
+               &                  ) * r1_e1e2v(ji,jj)
+            !
+            !                !--- ice currents at U-V point
+            v_iceU(ji,jj) = 0.25_wp * ( v_ice(ji,jj) + v_ice(ji,jj-1) + v_ice(ji+1,jj) + v_ice(ji+1,jj-1) ) * umask(ji,jj,1)
+            u_iceV(ji,jj) = 0.25_wp * ( u_ice(ji,jj) + u_ice(ji-1,jj) + u_ice(ji,jj+1) + u_ice(ji-1,jj+1) ) * vmask(ji,jj,1)
+            !
+         END_2D
+         !
+         ! --- Computation of ice velocity --- !
+         !  Bouillon et al. 2013 (eq 47-48) => unstable unless alpha, beta vary as in Kimmritz 2016 & 2017
+         !  Bouillon et al. 2009 (eq 34-35) => stable
+         IF( MOD(jter,2) == 0 ) THEN ! even iterations
+            !
+            DO_2D( 0, 0, 0, 0 )
+               !                 !--- tau_io/(v_oce - v_ice)
+               zTauO = zaV(ji,jj) * zrhoco * SQRT( ( v_ice (ji,jj) - v_oce (ji,jj) ) * ( v_ice (ji,jj) - v_oce (ji,jj) )  &
+                  &                              + ( u_iceV(ji,jj) - u_oceV(ji,jj) ) * ( u_iceV(ji,jj) - u_oceV(ji,jj) ) )
+               !                 !--- Ocean-to-Ice stress
+               ztauy_oi(ji,jj) = zTauO * ( v_oce(ji,jj) - v_ice(ji,jj) )
+               !
+               !                 !--- tau_bottom/v_ice
+               zvel  = 5.e-05_wp + SQRT( v_ice(ji,jj) * v_ice(ji,jj) + u_iceV(ji,jj) * u_iceV(ji,jj) )
+               zTauB = ztauy_base(ji,jj) / zvel
+               !                 !--- OceanBottom-to-Ice stress
+               ztauy_bi(ji,jj) = zTauB * v_ice(ji,jj)
+               !
+               !                 !--- Coriolis at V-points (energy conserving formulation)
+               zCorV(ji,jj)  = - 0.25_wp * r1_e2v(ji,jj) *  &
+                  &    ( zmf(ji,jj  ) * ( e2u(ji,jj  ) * u_ice(ji,jj  ) + e2u(ji-1,jj  ) * u_ice(ji-1,jj  ) )  &
+                  &    + zmf(ji,jj+1) * ( e2u(ji,jj+1) * u_ice(ji,jj+1) + e2u(ji-1,jj+1) * u_ice(ji-1,jj+1) ) )
+               !
+               !                 !--- Sum of external forces (explicit solution) = F + tau_ia + Coriolis + spg + tau_io
+               zRHS = zfV(ji,jj) + ztauy_ai(ji,jj) + zCorV(ji,jj) + zspgV(ji,jj) + ztauy_oi(ji,jj)
+               !
+               !                 !--- landfast switch => 0 = static  friction : TauB > RHS & sign(TauB) /= sign(RHS)
+               !                                         1 = sliding friction : TauB < RHS
+               rswitch = 1._wp - MIN( 1._wp, ABS( SIGN( 1._wp, zRHS + ztauy_base(ji,jj) ) - SIGN( 1._wp, zRHS ) ) )
+               !
+               IF( ln_aEVP ) THEN !--- ice velocity using aEVP (Kimmritz et al 2016 & 2017)
+                  zbetav = MAX( zbeta(ji,jj), zbeta(ji,jj+1) )
+                  v_ice(ji,jj) = ( (          rswitch   * ( zmV_t(ji,jj) * ( zbetav * v_ice(ji,jj) + v_ice_b(ji,jj) )         & ! previous velocity
+                     &                                    + zRHS + zTauO * v_ice(ji,jj)                                       & ! F + tau_ia + Coriolis + spg + tau_io(only ocean part)
+                     &                                    ) / MAX( zepsi, zmV_t(ji,jj) * ( zbetav + 1._wp ) + zTauO - zTauB ) & ! m/dt + tau_io(only ice part) + landfast
+                     &            + ( 1._wp - rswitch ) * (  v_ice_b(ji,jj)                                                   & 
+                     &                                     + v_ice  (ji,jj) * MAX( 0._wp, zbetav - zdtevp * rn_lf_relax )     & ! static friction => slow decrease to v=0
+                     &                                    ) / ( zbetav + 1._wp )                                              &
+                     &             ) * zmsk01y(ji,jj) + v_oce(ji,jj) * 0.01_wp * ( 1._wp - zmsk01y(ji,jj) )                   & ! v_ice = v_oce/100 if mass < zmmin & conc < zamin
+                     &           )   * zmsk00y(ji,jj)
+               ELSE               !--- ice velocity using EVP implicit formulation (cf Madec doc & Bouillon 2009)
+                  v_ice(ji,jj) = ( (          rswitch   * ( zmV_t(ji,jj) * v_ice(ji,jj)                                       & ! previous velocity
+                     &                                    + zRHS + zTauO * v_ice(ji,jj)                                       & ! F + tau_ia + Coriolis + spg + tau_io(only ocean part)
+                     &                                    ) / MAX( zepsi, zmV_t(ji,jj) + zTauO - zTauB )                      & ! m/dt + tau_io(only ice part) + landfast
+                     &            + ( 1._wp - rswitch ) *   v_ice(ji,jj) * MAX( 0._wp, 1._wp - zdtevp * rn_lf_relax )         & ! static friction => slow decrease to v=0
+                     &             ) * zmsk01y(ji,jj) + v_oce(ji,jj) * 0.01_wp * ( 1._wp - zmsk01y(ji,jj) )                   & ! v_ice = v_oce/100 if mass < zmmin & conc < zamin
+                     &            )  * zmsk00y(ji,jj)
+               ENDIF
+!extra code for test case boundary conditions
+               IF (mjg(jj)<25 .or. mjg(jj)>975 .or. mig(ji)<25 .or. mig(ji)>975) THEN
+                  v_ice(ji,jj) = zinvw*(ztauy_ai(ji,jj) + zCorV(ji,jj) + zspgV(ji,jj) + ztauy_oi(ji,jj))
+               END IF
+            END_2D
+            CALL lbc_lnk( 'icedyn_rhg_evp', v_ice, 'V', -1.0_wp )
+            !
+#if defined key_agrif
+!!            CALL agrif_interp_ice( 'V', jter, nn_nevp )
+            CALL agrif_interp_ice( 'V' )
+#endif
+            IF( ln_bdy )   CALL bdy_ice_dyn( 'V' )
+            !
+            DO_2D( 0, 0, 0, 0 )
+               !                 !--- tau_io/(u_oce - u_ice)
+               zTauO = zaU(ji,jj) * zrhoco * SQRT( ( u_ice (ji,jj) - u_oce (ji,jj) ) * ( u_ice (ji,jj) - u_oce (ji,jj) )  &
+                  &                              + ( v_iceU(ji,jj) - v_oceU(ji,jj) ) * ( v_iceU(ji,jj) - v_oceU(ji,jj) ) )
+               !                 !--- Ocean-to-Ice stress
+               ztaux_oi(ji,jj) = zTauO * ( u_oce(ji,jj) - u_ice(ji,jj) )
+               !
+               !                 !--- tau_bottom/u_ice
+               zvel  = 5.e-05_wp + SQRT( v_iceU(ji,jj) * v_iceU(ji,jj) + u_ice(ji,jj) * u_ice(ji,jj) )
+               zTauB = ztaux_base(ji,jj) / zvel
+               !                 !--- OceanBottom-to-Ice stress
+               ztaux_bi(ji,jj) = zTauB * u_ice(ji,jj)
+               !
+               !                 !--- Coriolis at U-points (energy conserving formulation)
+               zCorU(ji,jj)  =   0.25_wp * r1_e1u(ji,jj) *  &
+                  &    ( zmf(ji  ,jj) * ( e1v(ji  ,jj) * v_ice(ji  ,jj) + e1v(ji  ,jj-1) * v_ice(ji  ,jj-1) )  &
+                  &    + zmf(ji+1,jj) * ( e1v(ji+1,jj) * v_ice(ji+1,jj) + e1v(ji+1,jj-1) * v_ice(ji+1,jj-1) ) )
+               !
+               !                 !--- Sum of external forces (explicit solution) = F + tau_ia + Coriolis + spg + tau_io
+               zRHS = zfU(ji,jj) + ztaux_ai(ji,jj) + zCorU(ji,jj) + zspgU(ji,jj) + ztaux_oi(ji,jj)
+               !
+               !                 !--- landfast switch => 0 = static  friction : TauB > RHS & sign(TauB) /= sign(RHS)
+               !                                         1 = sliding friction : TauB < RHS
+               rswitch = 1._wp - MIN( 1._wp, ABS( SIGN( 1._wp, zRHS + ztaux_base(ji,jj) ) - SIGN( 1._wp, zRHS ) ) )
+               !
+               IF( ln_aEVP ) THEN !--- ice velocity using aEVP (Kimmritz et al 2016 & 2017)
+                  zbetau = MAX( zbeta(ji,jj), zbeta(ji+1,jj) )
+                  u_ice(ji,jj) = ( (          rswitch   * ( zmU_t(ji,jj) * ( zbetau * u_ice(ji,jj) + u_ice_b(ji,jj) )         & ! previous velocity
+                     &                                    + zRHS + zTauO * u_ice(ji,jj)                                       & ! F + tau_ia + Coriolis + spg + tau_io(only ocean part)
+                     &                                    ) / MAX( zepsi, zmU_t(ji,jj) * ( zbetau + 1._wp ) + zTauO - zTauB ) & ! m/dt + tau_io(only ice part) + landfast
+                     &            + ( 1._wp - rswitch ) * (  u_ice_b(ji,jj)                                                   &
+                     &                                     + u_ice  (ji,jj) * MAX( 0._wp, zbetau - zdtevp * rn_lf_relax )     & ! static friction => slow decrease to v=0
+                     &                                    ) / ( zbetau + 1._wp )                                              &
+                     &             ) * zmsk01x(ji,jj) + u_oce(ji,jj) * 0.01_wp * ( 1._wp - zmsk01x(ji,jj) )                   & ! v_ice = v_oce/100 if mass < zmmin & conc < zamin 
+                     &           )   * zmsk00x(ji,jj)
+               ELSE               !--- ice velocity using EVP implicit formulation (cf Madec doc & Bouillon 2009)
+                  u_ice(ji,jj) = ( (          rswitch   * ( zmU_t(ji,jj) * u_ice(ji,jj)                                       & ! previous velocity
+                     &                                    + zRHS + zTauO * u_ice(ji,jj)                                       & ! F + tau_ia + Coriolis + spg + tau_io(only ocean part)
+                     &                                    ) / MAX( zepsi, zmU_t(ji,jj) + zTauO - zTauB )                      & ! m/dt + tau_io(only ice part) + landfast
+                     &            + ( 1._wp - rswitch ) *   u_ice(ji,jj) * MAX( 0._wp, 1._wp - zdtevp * rn_lf_relax )         & ! static friction => slow decrease to v=0
+                     &             ) * zmsk01x(ji,jj) + u_oce(ji,jj) * 0.01_wp * ( 1._wp - zmsk01x(ji,jj) )                   & ! v_ice = v_oce/100 if mass < zmmin & conc < zamin 
+                     &           )   * zmsk00x(ji,jj)
+               ENDIF
+!extra code for test case boundary conditions
+               IF (mjg(jj)<25 .or. mjg(jj)>975 .or. mig(ji)<25 .or. mig(ji)>975) THEN
+                  u_ice(ji,jj) = zinvw*(ztaux_ai(ji,jj) + zCorU(ji,jj) + zspgU(ji,jj) + ztaux_oi(ji,jj))
+               END IF
+            END_2D
+            CALL lbc_lnk( 'icedyn_rhg_evp', u_ice, 'U', -1.0_wp )
+            !
+#if defined key_agrif
+!!            CALL agrif_interp_ice( 'U', jter, nn_nevp )
+            CALL agrif_interp_ice( 'U' )
+#endif
+            IF( ln_bdy )   CALL bdy_ice_dyn( 'U' )
+            !
+         ELSE ! odd iterations
+            !
+            DO_2D( 0, 0, 0, 0 )
+               !                 !--- tau_io/(u_oce - u_ice)
+               zTauO = zaU(ji,jj) * zrhoco * SQRT( ( u_ice (ji,jj) - u_oce (ji,jj) ) * ( u_ice (ji,jj) - u_oce (ji,jj) )  &
+                  &                              + ( v_iceU(ji,jj) - v_oceU(ji,jj) ) * ( v_iceU(ji,jj) - v_oceU(ji,jj) ) )
+               !                 !--- Ocean-to-Ice stress
+               ztaux_oi(ji,jj) = zTauO * ( u_oce(ji,jj) - u_ice(ji,jj) )
+               !
+               !                 !--- tau_bottom/u_ice
+               zvel  = 5.e-05_wp + SQRT( v_iceU(ji,jj) * v_iceU(ji,jj) + u_ice(ji,jj) * u_ice(ji,jj) )
+               zTauB = ztaux_base(ji,jj) / zvel
+               !                 !--- OceanBottom-to-Ice stress
+               ztaux_bi(ji,jj) = zTauB * u_ice(ji,jj)
+               !
+               !                 !--- Coriolis at U-points (energy conserving formulation)
+               zCorU(ji,jj)  =   0.25_wp * r1_e1u(ji,jj) *  &
+                  &    ( zmf(ji  ,jj) * ( e1v(ji  ,jj) * v_ice(ji  ,jj) + e1v(ji  ,jj-1) * v_ice(ji  ,jj-1) )  &
+                  &    + zmf(ji+1,jj) * ( e1v(ji+1,jj) * v_ice(ji+1,jj) + e1v(ji+1,jj-1) * v_ice(ji+1,jj-1) ) )
+               !
+               !                 !--- Sum of external forces (explicit solution) = F + tau_ia + Coriolis + spg + tau_io
+               zRHS = zfU(ji,jj) + ztaux_ai(ji,jj) + zCorU(ji,jj) + zspgU(ji,jj) + ztaux_oi(ji,jj)
+               !
+               !                 !--- landfast switch => 0 = static  friction : TauB > RHS & sign(TauB) /= sign(RHS)
+               !                                         1 = sliding friction : TauB < RHS
+               rswitch = 1._wp - MIN( 1._wp, ABS( SIGN( 1._wp, zRHS + ztaux_base(ji,jj) ) - SIGN( 1._wp, zRHS ) ) )
+               !
+               IF( ln_aEVP ) THEN !--- ice velocity using aEVP (Kimmritz et al 2016 & 2017)
+                  zbetau = MAX( zbeta(ji,jj), zbeta(ji+1,jj) )
+                  u_ice(ji,jj) = ( (          rswitch   * ( zmU_t(ji,jj) * ( zbetau * u_ice(ji,jj) + u_ice_b(ji,jj) )         & ! previous velocity
+                     &                                    + zRHS + zTauO * u_ice(ji,jj)                                       & ! F + tau_ia + Coriolis + spg + tau_io(only ocean part)
+                     &                                    ) / MAX( zepsi, zmU_t(ji,jj) * ( zbetau + 1._wp ) + zTauO - zTauB ) & ! m/dt + tau_io(only ice part) + landfast
+                     &            + ( 1._wp - rswitch ) * (  u_ice_b(ji,jj)                                                   &
+                     &                                     + u_ice  (ji,jj) * MAX( 0._wp, zbetau - zdtevp * rn_lf_relax )     & ! static friction => slow decrease to v=0
+                     &                                    ) / ( zbetau + 1._wp )                                              &
+                     &             ) * zmsk01x(ji,jj) + u_oce(ji,jj) * 0.01_wp * ( 1._wp - zmsk01x(ji,jj) )                   & ! v_ice = v_oce/100 if mass < zmmin & conc < zamin 
+                     &           )   * zmsk00x(ji,jj)
+               ELSE               !--- ice velocity using EVP implicit formulation (cf Madec doc & Bouillon 2009)
+                  u_ice(ji,jj) = ( (          rswitch   * ( zmU_t(ji,jj) * u_ice(ji,jj)                                       & ! previous velocity
+                     &                                    + zRHS + zTauO * u_ice(ji,jj)                                       & ! F + tau_ia + Coriolis + spg + tau_io(only ocean part)
+                     &                                    ) / MAX( zepsi, zmU_t(ji,jj) + zTauO - zTauB )                      & ! m/dt + tau_io(only ice part) + landfast
+                     &            + ( 1._wp - rswitch ) *   u_ice(ji,jj) * MAX( 0._wp, 1._wp - zdtevp * rn_lf_relax )         & ! static friction => slow decrease to v=0
+                     &             ) * zmsk01x(ji,jj) + u_oce(ji,jj) * 0.01_wp * ( 1._wp - zmsk01x(ji,jj) )                   & ! v_ice = v_oce/100 if mass < zmmin & conc < zamin
+                     &           )   * zmsk00x(ji,jj)
+               ENDIF
+!extra code for test case boundary conditions
+               IF (mjg(jj)<25 .or. mjg(jj)>975 .or. mig(ji)<25 .or. mig(ji)>975) THEN
+                  u_ice(ji,jj) = zinvw*(ztaux_ai(ji,jj) + zCorU(ji,jj) + zspgU(ji,jj) + ztaux_oi(ji,jj))
+               END IF
+            END_2D
+            CALL lbc_lnk( 'icedyn_rhg_evp', u_ice, 'U', -1.0_wp )
+            !
+#if defined key_agrif
+!!            CALL agrif_interp_ice( 'U', jter, nn_nevp )
+            CALL agrif_interp_ice( 'U' )
+#endif
+            IF( ln_bdy )   CALL bdy_ice_dyn( 'U' )
+            !
+            DO_2D( 0, 0, 0, 0 )
+               !                 !--- tau_io/(v_oce - v_ice)
+               zTauO = zaV(ji,jj) * zrhoco * SQRT( ( v_ice (ji,jj) - v_oce (ji,jj) ) * ( v_ice (ji,jj) - v_oce (ji,jj) )  &
+                  &                              + ( u_iceV(ji,jj) - u_oceV(ji,jj) ) * ( u_iceV(ji,jj) - u_oceV(ji,jj) ) )
+               !                 !--- Ocean-to-Ice stress
+               ztauy_oi(ji,jj) = zTauO * ( v_oce(ji,jj) - v_ice(ji,jj) )
+               !
+               !                 !--- tau_bottom/v_ice
+               zvel  = 5.e-05_wp + SQRT( v_ice(ji,jj) * v_ice(ji,jj) + u_iceV(ji,jj) * u_iceV(ji,jj) )
+               zTauB = ztauy_base(ji,jj) / zvel
+               !                 !--- OceanBottom-to-Ice stress
+               ztauy_bi(ji,jj) = zTauB * v_ice(ji,jj)
+               !
+               !                 !--- Coriolis at v-points (energy conserving formulation)
+               zCorV(ji,jj)  = - 0.25_wp * r1_e2v(ji,jj) *  &
+                  &    ( zmf(ji,jj  ) * ( e2u(ji,jj  ) * u_ice(ji,jj  ) + e2u(ji-1,jj  ) * u_ice(ji-1,jj  ) )  &
+                  &    + zmf(ji,jj+1) * ( e2u(ji,jj+1) * u_ice(ji,jj+1) + e2u(ji-1,jj+1) * u_ice(ji-1,jj+1) ) )
+               !
+               !                 !--- Sum of external forces (explicit solution) = F + tau_ia + Coriolis + spg + tau_io
+               zRHS = zfV(ji,jj) + ztauy_ai(ji,jj) + zCorV(ji,jj) + zspgV(ji,jj) + ztauy_oi(ji,jj)
+               !
+               !                 !--- landfast switch => 0 = static  friction : TauB > RHS & sign(TauB) /= sign(RHS)
+               !                                         1 = sliding friction : TauB < RHS
+               rswitch = 1._wp - MIN( 1._wp, ABS( SIGN( 1._wp, zRHS + ztauy_base(ji,jj) ) - SIGN( 1._wp, zRHS ) ) )
+               !
+               IF( ln_aEVP ) THEN !--- ice velocity using aEVP (Kimmritz et al 2016 & 2017)
+                  zbetav = MAX( zbeta(ji,jj), zbeta(ji,jj+1) )
+                  v_ice(ji,jj) = ( (          rswitch   * ( zmV_t(ji,jj) * ( zbetav * v_ice(ji,jj) + v_ice_b(ji,jj) )         & ! previous velocity
+                     &                                    + zRHS + zTauO * v_ice(ji,jj)                                       & ! F + tau_ia + Coriolis + spg + tau_io(only ocean part)
+                     &                                    ) / MAX( zepsi, zmV_t(ji,jj) * ( zbetav + 1._wp ) + zTauO - zTauB ) & ! m/dt + tau_io(only ice part) + landfast
+                     &            + ( 1._wp - rswitch ) * (  v_ice_b(ji,jj)                                                   &
+                     &                                     + v_ice  (ji,jj) * MAX( 0._wp, zbetav - zdtevp * rn_lf_relax )     & ! static friction => slow decrease to v=0
+                     &                                    ) / ( zbetav + 1._wp )                                              & 
+                     &             ) * zmsk01y(ji,jj) + v_oce(ji,jj) * 0.01_wp * ( 1._wp - zmsk01y(ji,jj) )                   & ! v_ice = v_oce/100 if mass < zmmin & conc < zamin
+                     &           )   * zmsk00y(ji,jj)
+               ELSE               !--- ice velocity using EVP implicit formulation (cf Madec doc & Bouillon 2009)
+                  v_ice(ji,jj) = ( (          rswitch   * ( zmV_t(ji,jj) * v_ice(ji,jj)                                       & ! previous velocity
+                     &                                    + zRHS + zTauO * v_ice(ji,jj)                                       & ! F + tau_ia + Coriolis + spg + tau_io(only ocean part)
+                     &                                    ) / MAX( zepsi, zmV_t(ji,jj) + zTauO - zTauB )                      & ! m/dt + tau_io(only ice part) + landfast
+                     &            + ( 1._wp - rswitch ) *   v_ice(ji,jj) * MAX( 0._wp, 1._wp - zdtevp * rn_lf_relax )         & ! static friction => slow decrease to v=0
+                     &             ) * zmsk01y(ji,jj) + v_oce(ji,jj) * 0.01_wp * ( 1._wp - zmsk01y(ji,jj) )                   & ! v_ice = v_oce/100 if mass < zmmin & conc < zamin
+                     &           )   * zmsk00y(ji,jj)
+               ENDIF
+!extra code for test case boundary conditions
+               IF (mjg(jj)<25 .or. mjg(jj)>975 .or. mig(ji)<25 .or. mig(ji)>975) THEN
+                  v_ice(ji,jj) = zinvw*(ztauy_ai(ji,jj) + zCorV(ji,jj) + zspgV(ji,jj) + ztauy_oi(ji,jj))
+               END IF
+            END_2D
+            CALL lbc_lnk( 'icedyn_rhg_evp', v_ice, 'V', -1.0_wp )
+            !
+#if defined key_agrif
+!!            CALL agrif_interp_ice( 'V', jter, nn_nevp )
+            CALL agrif_interp_ice( 'V' )
+#endif
+            IF( ln_bdy )   CALL bdy_ice_dyn( 'V' )
+            !
+         ENDIF
+
+         ! convergence test
+         IF( nn_rhg_chkcvg == 2 )   CALL rhg_cvg( kt, jter, nn_nevp, u_ice, v_ice, zu_ice, zv_ice )
+         !
+         !                                                ! ==================== !
+      END DO                                              !  end loop over jter  !
+      !                                                   ! ==================== !
+      IF( ln_aEVP )   CALL iom_put( 'beta_evp' , zbeta )
+      !
+      !------------------------------------------------------------------------------!
+      ! 4) Recompute delta, shear and div (inputs for mechanical redistribution) 
+      !------------------------------------------------------------------------------!
+      DO_2D( 1, 0, 1, 0 )
+
+         ! shear at F points
+         zds(ji,jj) = ( ( u_ice(ji,jj+1) * r1_e1u(ji,jj+1) - u_ice(ji,jj) * r1_e1u(ji,jj) ) * e1f(ji,jj) * e1f(ji,jj)   &
+            &         + ( v_ice(ji+1,jj) * r1_e2v(ji+1,jj) - v_ice(ji,jj) * r1_e2v(ji,jj) ) * e2f(ji,jj) * e2f(ji,jj)   &
+            &         ) * r1_e1e2f(ji,jj) * zfmask(ji,jj)
+
+      END_2D
+      
+      DO_2D( 0, 0, 0, 0 )   ! no vector loop
+         
+         ! tension**2 at T points
+         zdt  = ( ( u_ice(ji,jj) * r1_e2u(ji,jj) - u_ice(ji-1,jj) * r1_e2u(ji-1,jj) ) * e2t(ji,jj) * e2t(ji,jj)   &
+            &   - ( v_ice(ji,jj) * r1_e1v(ji,jj) - v_ice(ji,jj-1) * r1_e1v(ji,jj-1) ) * e1t(ji,jj) * e1t(ji,jj)   &
+            &   ) * r1_e1e2t(ji,jj)
+         zdt2 = zdt * zdt
+
+         zten_i(ji,jj) = zdt
+         
+         ! shear**2 at T points (doc eq. A16)
+         zds2 = ( zds(ji,jj  ) * zds(ji,jj  ) * e1e2f(ji,jj  ) + zds(ji-1,jj  ) * zds(ji-1,jj  ) * e1e2f(ji-1,jj  )  &
+            &   + zds(ji,jj-1) * zds(ji,jj-1) * e1e2f(ji,jj-1) + zds(ji-1,jj-1) * zds(ji-1,jj-1) * e1e2f(ji-1,jj-1)  &
+            &   ) * 0.25_wp * r1_e1e2t(ji,jj)
+         
+         ! shear at T points
+         pshear_i(ji,jj) = SQRT( zdt2 + zds2 )
+
+         ! divergence at T points
+         pdivu_i(ji,jj) = ( e2u(ji,jj) * u_ice(ji,jj) - e2u(ji-1,jj) * u_ice(ji-1,jj)   &
+            &             + e1v(ji,jj) * v_ice(ji,jj) - e1v(ji,jj-1) * v_ice(ji,jj-1)   &
+            &             ) * r1_e1e2t(ji,jj)
+         
+         ! delta at T points
+         zfac            = SQRT( pdivu_i(ji,jj) * pdivu_i(ji,jj) + ( zdt2 + zds2 ) * z1_ecc2 ) ! delta  
+         rswitch         = 1._wp - MAX( 0._wp, SIGN( 1._wp, -zfac ) ) ! 0 if delta=0
+         pdelta_i(ji,jj) = zfac + rn_creepl * rswitch ! delta+creepl
+
+      END_2D
+      CALL lbc_lnk_multi( 'icedyn_rhg_evp', pshear_i, 'T', 1._wp, pdivu_i, 'T', 1._wp, pdelta_i, 'T', 1._wp, zten_i, 'T', 1._wp, &
+         &                                  zs1     , 'T', 1._wp, zs2    , 'T', 1._wp, zs12    , 'F', 1._wp )
+      
+      ! --- Store the stress tensor for the next time step --- !
+      pstress1_i (:,:) = zs1 (:,:)
+      pstress2_i (:,:) = zs2 (:,:)
+      pstress12_i(:,:) = zs12(:,:)
+      !
+
+      !------------------------------------------------------------------------------!
+      ! 5) diagnostics
+      !------------------------------------------------------------------------------!
+      ! --- ice-ocean, ice-atm. & ice-oceanbottom(landfast) stresses --- !
+      IF(  iom_use('utau_oi') .OR. iom_use('vtau_oi') .OR. iom_use('utau_ai') .OR. iom_use('vtau_ai') .OR. &
+         & iom_use('utau_bi') .OR. iom_use('vtau_bi') ) THEN
+         !
+         CALL lbc_lnk_multi( 'icedyn_rhg_evp', ztaux_oi, 'U', -1.0_wp, ztauy_oi, 'V', -1.0_wp, ztaux_ai, 'U', -1.0_wp, ztauy_ai, 'V', -1.0_wp, &
+            &                                  ztaux_bi, 'U', -1.0_wp, ztauy_bi, 'V', -1.0_wp )
+         !
+         CALL iom_put( 'utau_oi' , ztaux_oi * zmsk00 )
+         CALL iom_put( 'vtau_oi' , ztauy_oi * zmsk00 )
+         CALL iom_put( 'utau_ai' , ztaux_ai * zmsk00 )
+         CALL iom_put( 'vtau_ai' , ztauy_ai * zmsk00 )
+         CALL iom_put( 'utau_bi' , ztaux_bi * zmsk00 )
+         CALL iom_put( 'vtau_bi' , ztauy_bi * zmsk00 )
+      ENDIF
+       
+      ! --- divergence, shear and strength --- !
+      IF( iom_use('icediv') )   CALL iom_put( 'icediv' , pdivu_i  * zmsk00 )   ! divergence
+      IF( iom_use('iceshe') )   CALL iom_put( 'iceshe' , pshear_i * zmsk00 )   ! shear
+      IF( iom_use('icestr') )   CALL iom_put( 'icestr' , strength * zmsk00 )   ! strength
+
+      ! --- Stress tensor invariants (SIMIP diags) --- !
+      IF( iom_use('normstr') .OR. iom_use('sheastr') ) THEN
+         !
+         ALLOCATE( zsig_I(jpi,jpj) , zsig_II(jpi,jpj) )
+         !         
+         DO_2D( 1, 1, 1, 1 )
+            
+            ! Ice stresses
+            ! sigma1, sigma2, sigma12 are some useful recombination of the stresses (Hunke and Dukowicz MWR 2002, Bouillon et al., OM2013)
+            ! These are NOT stress tensor components, neither stress invariants, neither stress principal components
+            ! I know, this can be confusing...
+            zfac             =   strength(ji,jj) / ( pdelta_i(ji,jj) + rn_creepl ) 
+            zsig1            =   zfac * ( pdivu_i(ji,jj) - pdelta_i(ji,jj) )
+            zsig2            =   zfac * z1_ecc2 * zten_i(ji,jj)
+            zsig12           =   zfac * z1_ecc2 * pshear_i(ji,jj)
+            
+            ! Stress invariants (sigma_I, sigma_II, Coon 1974, Feltham 2008)
+            zsig_I (ji,jj)   =   zsig1 * 0.5_wp                                           ! 1st stress invariant, aka average normal stress, aka negative pressure
+            zsig_II(ji,jj)   =   SQRT ( MAX( 0._wp, zsig2 * zsig2 * 0.25_wp + zsig12 ) )  ! 2nd  ''       '', aka maximum shear stress
+               
+         END_2D         
+         !
+         ! Stress tensor invariants (normal and shear stress N/m) - SIMIP diags - definitions following Coon (1974) and Feltham (2008)
+         IF( iom_use('normstr') )   CALL iom_put( 'normstr', zsig_I (:,:) * zmsk00(:,:) ) ! Normal stress
+         IF( iom_use('sheastr') )   CALL iom_put( 'sheastr', zsig_II(:,:) * zmsk00(:,:) ) ! Maximum shear stress
+         
+         DEALLOCATE ( zsig_I, zsig_II )
+         
+      ENDIF
+
+      ! --- Normalized stress tensor principal components --- !
+      ! This are used to plot the normalized yield curve, see Lemieux & Dupont, 2020
+      ! Recommendation 1 : we use ice strength, not replacement pressure
+      ! Recommendation 2 : need to use deformations at PREVIOUS iterate for viscosities
+      IF( iom_use('sig1_pnorm') .OR. iom_use('sig2_pnorm') ) THEN
+         !
+         ALLOCATE( zsig1_p(jpi,jpj) , zsig2_p(jpi,jpj) , zsig_I(jpi,jpj) , zsig_II(jpi,jpj) )         
+         !         
+         DO_2D( 1, 1, 1, 1 )
+            
+            ! Ice stresses computed with **viscosities** (delta, p/delta) at **previous** iterates 
+            !                        and **deformations** at current iterates
+            !                        following Lemieux & Dupont (2020)
+            zfac             =   zp_delt(ji,jj)
+            zsig1            =   zfac * ( pdivu_i(ji,jj) - ( zdelta(ji,jj) + rn_creepl ) )
+            zsig2            =   zfac * z1_ecc2 * zten_i(ji,jj)
+            zsig12           =   zfac * z1_ecc2 * pshear_i(ji,jj)
+            
+            ! Stress invariants (sigma_I, sigma_II, Coon 1974, Feltham 2008), T-point
+            zsig_I(ji,jj)    =   zsig1 * 0.5_wp                                            ! 1st stress invariant, aka average normal stress, aka negative pressure
+            zsig_II(ji,jj)   =   SQRT ( MAX( 0._wp, zsig2 * zsig2 * 0.25_wp + zsig12 ) )   ! 2nd  ''       '', aka maximum shear stress
+            
+            ! Normalized  principal stresses (used to display the ellipse)
+            z1_strength      =   1._wp / MAX( 1._wp, strength(ji,jj) )
+            zsig1_p(ji,jj)   =   ( zsig_I(ji,jj) + zsig_II(ji,jj) ) * z1_strength
+            zsig2_p(ji,jj)   =   ( zsig_I(ji,jj) - zsig_II(ji,jj) ) * z1_strength
+         END_2D              
+         !
+         CALL iom_put( 'sig1_pnorm' , zsig1_p ) 
+         CALL iom_put( 'sig2_pnorm' , zsig2_p ) 
+
+         DEALLOCATE( zsig1_p , zsig2_p , zsig_I, zsig_II )
+         
+      ENDIF
+
+      ! --- SIMIP --- !
+      IF(  iom_use('dssh_dx') .OR. iom_use('dssh_dy') .OR. &
+         & iom_use('corstrx') .OR. iom_use('corstry') .OR. iom_use('intstrx') .OR. iom_use('intstry') ) THEN
+         !
+         CALL lbc_lnk_multi( 'icedyn_rhg_evp', zspgU, 'U', -1.0_wp, zspgV, 'V', -1.0_wp, &
+            &                                  zCorU, 'U', -1.0_wp, zCorV, 'V', -1.0_wp, zfU, 'U', -1.0_wp, zfV, 'V', -1.0_wp )
+
+         CALL iom_put( 'dssh_dx' , zspgU * zmsk00 )   ! Sea-surface tilt term in force balance (x)
+         CALL iom_put( 'dssh_dy' , zspgV * zmsk00 )   ! Sea-surface tilt term in force balance (y)
+         CALL iom_put( 'corstrx' , zCorU * zmsk00 )   ! Coriolis force term in force balance (x)
+         CALL iom_put( 'corstry' , zCorV * zmsk00 )   ! Coriolis force term in force balance (y)
+         CALL iom_put( 'intstrx' , zfU   * zmsk00 )   ! Internal force term in force balance (x)
+         CALL iom_put( 'intstry' , zfV   * zmsk00 )   ! Internal force term in force balance (y)
+      ENDIF
+
+      IF(  iom_use('xmtrpice') .OR. iom_use('ymtrpice') .OR. &
+         & iom_use('xmtrpsnw') .OR. iom_use('ymtrpsnw') .OR. iom_use('xatrp') .OR. iom_use('yatrp') ) THEN
+         !
+         ALLOCATE( zdiag_xmtrp_ice(jpi,jpj) , zdiag_ymtrp_ice(jpi,jpj) , &
+            &      zdiag_xmtrp_snw(jpi,jpj) , zdiag_ymtrp_snw(jpi,jpj) , zdiag_xatrp(jpi,jpj) , zdiag_yatrp(jpi,jpj) )
+         !
+         DO_2D( 0, 0, 0, 0 )
+            ! 2D ice mass, snow mass, area transport arrays (X, Y)
+            zfac_x = 0.5 * u_ice(ji,jj) * e2u(ji,jj) * zmsk00(ji,jj)
+            zfac_y = 0.5 * v_ice(ji,jj) * e1v(ji,jj) * zmsk00(ji,jj)
+
+            zdiag_xmtrp_ice(ji,jj) = rhoi * zfac_x * ( vt_i(ji+1,jj) + vt_i(ji,jj) ) ! ice mass transport, X-component
+            zdiag_ymtrp_ice(ji,jj) = rhoi * zfac_y * ( vt_i(ji,jj+1) + vt_i(ji,jj) ) !        ''           Y-   ''
+
+            zdiag_xmtrp_snw(ji,jj) = rhos * zfac_x * ( vt_s(ji+1,jj) + vt_s(ji,jj) ) ! snow mass transport, X-component
+            zdiag_ymtrp_snw(ji,jj) = rhos * zfac_y * ( vt_s(ji,jj+1) + vt_s(ji,jj) ) !          ''          Y-   ''
+
+            zdiag_xatrp(ji,jj)     = zfac_x * ( at_i(ji+1,jj) + at_i(ji,jj) )        ! area transport,      X-component
+            zdiag_yatrp(ji,jj)     = zfac_y * ( at_i(ji,jj+1) + at_i(ji,jj) )        !        ''            Y-   ''
+
+         END_2D
+
+         CALL lbc_lnk_multi( 'icedyn_rhg_evp', zdiag_xmtrp_ice, 'U', -1.0_wp, zdiag_ymtrp_ice, 'V', -1.0_wp, &
+            &                                  zdiag_xmtrp_snw, 'U', -1.0_wp, zdiag_ymtrp_snw, 'V', -1.0_wp, &
+            &                                  zdiag_xatrp    , 'U', -1.0_wp, zdiag_yatrp    , 'V', -1.0_wp )
+
+         CALL iom_put( 'xmtrpice' , zdiag_xmtrp_ice )   ! X-component of sea-ice mass transport (kg/s)
+         CALL iom_put( 'ymtrpice' , zdiag_ymtrp_ice )   ! Y-component of sea-ice mass transport 
+         CALL iom_put( 'xmtrpsnw' , zdiag_xmtrp_snw )   ! X-component of snow mass transport (kg/s)
+         CALL iom_put( 'ymtrpsnw' , zdiag_ymtrp_snw )   ! Y-component of snow mass transport
+         CALL iom_put( 'xatrp'    , zdiag_xatrp     )   ! X-component of ice area transport
+         CALL iom_put( 'yatrp'    , zdiag_yatrp     )   ! Y-component of ice area transport
+
+         DEALLOCATE( zdiag_xmtrp_ice , zdiag_ymtrp_ice , &
+            &        zdiag_xmtrp_snw , zdiag_ymtrp_snw , zdiag_xatrp , zdiag_yatrp )
+
+      ENDIF
+      !
+      ! --- convergence tests --- !
+      IF( nn_rhg_chkcvg == 1 .OR. nn_rhg_chkcvg == 2 ) THEN
+         IF( iom_use('uice_cvg') ) THEN
+            IF( ln_aEVP ) THEN   ! output: beta * ( u(t=nn_nevp) - u(t=nn_nevp-1) )
+               CALL iom_put( 'uice_cvg', MAX( ABS( u_ice(:,:) - zu_ice(:,:) ) * zbeta(:,:) * umask(:,:,1) , &
+                  &                           ABS( v_ice(:,:) - zv_ice(:,:) ) * zbeta(:,:) * vmask(:,:,1) ) * zmsk15(:,:) )
+            ELSE                 ! output: nn_nevp * ( u(t=nn_nevp) - u(t=nn_nevp-1) )
+               CALL iom_put( 'uice_cvg', REAL( nn_nevp ) * MAX( ABS( u_ice(:,:) - zu_ice(:,:) ) * umask(:,:,1) , &
+                  &                                             ABS( v_ice(:,:) - zv_ice(:,:) ) * vmask(:,:,1) ) * zmsk15(:,:) )
+            ENDIF
+         ENDIF
+      ENDIF      
+      !
+      DEALLOCATE( zmsk00, zmsk15 )
+      !
+   END SUBROUTINE ice_dyn_rhg_evp
+
+
+   SUBROUTINE rhg_cvg( kt, kiter, kitermax, pu, pv, pub, pvb )
+      !!----------------------------------------------------------------------
+      !!                    ***  ROUTINE rhg_cvg  ***
+      !!                     
+      !! ** Purpose :   check convergence of oce rheology
+      !!
+      !! ** Method  :   create a file ice_cvg.nc containing the convergence of ice velocity
+      !!                during the sub timestepping of rheology so as:
+      !!                  uice_cvg = MAX( u(t+1) - u(t) , v(t+1) - v(t) )
+      !!                This routine is called every sub-iteration, so it is cpu expensive
+      !!
+      !! ** Note    :   for the first sub-iteration, uice_cvg is set to 0 (too large otherwise)   
+      !!----------------------------------------------------------------------
+      INTEGER ,                 INTENT(in) ::   kt, kiter, kitermax       ! ocean time-step index
+      REAL(wp), DIMENSION(:,:), INTENT(in) ::   pu, pv, pub, pvb          ! now and before velocities
+      !!
+      INTEGER           ::   it, idtime, istatus
+      INTEGER           ::   ji, jj          ! dummy loop indices
+      REAL(wp)          ::   zresm           ! local real 
+      CHARACTER(len=20) ::   clname
+      REAL(wp), DIMENSION(jpi,jpj) ::   zres           ! check convergence
+      !!----------------------------------------------------------------------
+
+      ! create file
+      IF( kt == nit000 .AND. kiter == 1 ) THEN
+         !
+         IF( lwp ) THEN
+            WRITE(numout,*)
+            WRITE(numout,*) 'rhg_cvg : ice rheology convergence control'
+            WRITE(numout,*) '~~~~~~~'
+         ENDIF
+         !
+         IF( lwm ) THEN
+            clname = 'ice_cvg.nc'
+            IF( .NOT. Agrif_Root() )   clname = TRIM(Agrif_CFixed())//"_"//TRIM(clname)
+            istatus = NF90_CREATE( TRIM(clname), NF90_CLOBBER, ncvgid )
+            istatus = NF90_DEF_DIM( ncvgid, 'time'  , NF90_UNLIMITED, idtime )
+            istatus = NF90_DEF_VAR( ncvgid, 'uice_cvg', NF90_DOUBLE   , (/ idtime /), nvarid )
+            istatus = NF90_ENDDEF(ncvgid)
+         ENDIF
+         !
+      ENDIF
+
+      ! time
+      it = ( kt - 1 ) * kitermax + kiter
+      
+      ! convergence
+      IF( kiter == 1 ) THEN ! remove the first iteration for calculations of convergence (always very large)
+         zresm = 0._wp
+      ELSE
+         DO_2D( 1, 1, 1, 1 )
+            zres(ji,jj) = MAX( ABS( pu(ji,jj) - pub(ji,jj) ) * umask(ji,jj,1), &
+               &               ABS( pv(ji,jj) - pvb(ji,jj) ) * vmask(ji,jj,1) ) * zmsk15(ji,jj)
+         END_2D
+
+         ! cut of the boundary of the box (forced velocities)
+         IF (mjg(jj)<=30 .or. mjg(jj)>970 .or. mig(ji)<=30 .or. mig(ji)>970) THEN
+            zres(ji,jj) = 0._wp
+         END IF
+
+         zresm = MAXVAL( zres )
+         CALL mpp_max( 'icedyn_rhg_evp', zresm )   ! max over the global domain
+      ENDIF
+
+      IF( lwm ) THEN
+         ! write variables
+         istatus = NF90_PUT_VAR( ncvgid, nvarid, (/zresm/), (/it/), (/1/) )
+         ! close file
+         IF( kt == nitend - nn_fsbc + 1 )   istatus = NF90_CLOSE(ncvgid)
+      ENDIF
+      
+   END SUBROUTINE rhg_cvg
+
+
+   SUBROUTINE rhg_evp_rst( cdrw, kt )
+      !!---------------------------------------------------------------------
+      !!                   ***  ROUTINE rhg_evp_rst  ***
+      !!                     
+      !! ** Purpose :   Read or write RHG file in restart file
+      !!
+      !! ** Method  :   use of IOM library
+      !!----------------------------------------------------------------------
+      CHARACTER(len=*) , INTENT(in) ::   cdrw   ! "READ"/"WRITE" flag
+      INTEGER, OPTIONAL, INTENT(in) ::   kt     ! ice time-step
+      !
+      INTEGER  ::   iter            ! local integer
+      INTEGER  ::   id1, id2, id3   ! local integers
+      !!----------------------------------------------------------------------
+      !
+      IF( TRIM(cdrw) == 'READ' ) THEN        ! Read/initialize
+         !                                   ! ---------------
+         IF( ln_rstart ) THEN                   !* Read the restart file
+            !
+            id1 = iom_varid( numrir, 'stress1_i' , ldstop = .FALSE. )
+            id2 = iom_varid( numrir, 'stress2_i' , ldstop = .FALSE. )
+            id3 = iom_varid( numrir, 'stress12_i', ldstop = .FALSE. )
+            !
+            IF( MIN( id1, id2, id3 ) > 0 ) THEN      ! fields exist
+               CALL iom_get( numrir, jpdom_auto, 'stress1_i' , stress1_i , cd_type = 'T' )
+               CALL iom_get( numrir, jpdom_auto, 'stress2_i' , stress2_i , cd_type = 'T' )
+               CALL iom_get( numrir, jpdom_auto, 'stress12_i', stress12_i, cd_type = 'F' )
+            ELSE                                     ! start rheology from rest
+               IF(lwp) WRITE(numout,*)
+               IF(lwp) WRITE(numout,*) '   ==>>>   previous run without rheology, set stresses to 0'
+               stress1_i (:,:) = 0._wp
+               stress2_i (:,:) = 0._wp
+               stress12_i(:,:) = 0._wp
+            ENDIF
+         ELSE                                   !* Start from rest
+            IF(lwp) WRITE(numout,*)
+            IF(lwp) WRITE(numout,*) '   ==>>>   start from rest: set stresses to 0'
+            stress1_i (:,:) = 0._wp
+            stress2_i (:,:) = 0._wp
+            stress12_i(:,:) = 0._wp
+         ENDIF
+         !
+      ELSEIF( TRIM(cdrw) == 'WRITE' ) THEN   ! Create restart file
+         !                                   ! -------------------
+         IF(lwp) WRITE(numout,*) '---- rhg-rst ----'
+         iter = kt + nn_fsbc - 1             ! ice restarts are written at kt == nitrst - nn_fsbc + 1
+         !
+         CALL iom_rstput( iter, nitrst, numriw, 'stress1_i' , stress1_i  )
+         CALL iom_rstput( iter, nitrst, numriw, 'stress2_i' , stress2_i  )
+         CALL iom_rstput( iter, nitrst, numriw, 'stress12_i', stress12_i )
+         !
+      ENDIF
+      !
+   END SUBROUTINE rhg_evp_rst
+
+   
+#else
+   !!----------------------------------------------------------------------
+   !!   Default option         Empty module           NO SI3 sea-ice model
+   !!----------------------------------------------------------------------
+#endif
+
+   !!==============================================================================
+END MODULE icedyn_rhg_evp
Index: /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/MY_SRC/usrdef_hgr.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/MY_SRC/usrdef_hgr.F90	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/MY_SRC/usrdef_hgr.F90	(revision 14016)
@@ -0,0 +1,147 @@
+MODULE usrdef_hgr
+   !!======================================================================
+   !!                       ***  MODULE  usrdef_hgr  ***
+   !!
+   !!                      ===  ICE_RHEO configuration  ===
+   !!
+   !! User defined :   mesh and Coriolis parameter of a user configuration
+   !!======================================================================
+   !! History :  NEMO  ! 2016-08  (S. Flavoni, G. Madec)  Original code
+   !!----------------------------------------------------------------------
+
+   !!----------------------------------------------------------------------
+   !!   usr_def_hgr    : initialize the horizontal mesh for ICE_RHEO configuration
+   !!----------------------------------------------------------------------
+   USE dom_oce         ! ocean space and time domain
+   USE par_oce         ! ocean space and time domain
+   USE phycst          ! physical constants
+   USE usrdef_nam, ONLY: rn_dx, rn_dy, ln_corio, rn_ppgphi0   ! horizontal resolution in meters
+   !                                                            coriolis and reference latitude
+   USE in_out_manager  ! I/O manager
+   USE lib_mpp         ! MPP library
+   
+   IMPLICIT NONE
+   PRIVATE
+
+   PUBLIC   usr_def_hgr   ! called by domhgr.F90
+
+   !! * Substitutions
+#  include "do_loop_substitute.h90"
+   !!----------------------------------------------------------------------
+   !! NEMO/OCE 4.0 , NEMO Consortium (2018)
+   !! $Id: usrdef_hgr.F90 10074 2018-08-28 16:15:49Z nicolasmartin $ 
+   !! Software governed by the CeCILL license (see ./LICENSE)
+   !!----------------------------------------------------------------------
+CONTAINS
+
+   SUBROUTINE usr_def_hgr( plamt , plamu , plamv  , plamf  ,   &   ! geographic position (required)
+      &                    pphit , pphiu , pphiv  , pphif  ,   &   !
+      &                    kff   , pff_f , pff_t  ,            &   ! Coriolis parameter  (if domain not on the sphere)
+      &                    pe1t  , pe1u  , pe1v   , pe1f   ,   &   ! scale factors       (required)
+      &                    pe2t  , pe2u  , pe2v   , pe2f   ,   &   !
+      &                    ke1e2u_v      , pe1e2u , pe1e2v     )   ! u- & v-surfaces (if gridsize reduction is used in strait(s))
+      !!----------------------------------------------------------------------
+      !!                  ***  ROUTINE usr_def_hgr  ***
+      !!
+      !! ** Purpose :   user defined mesh and Coriolis parameter
+      !!
+      !! ** Method  :   set all intent(out) argument to a proper value
+      !!                ICE_RHEO configuration : uniform grid spacing (rn_dx)
+      !!                without Coriolis force (f=0)
+      !!
+      !! ** Action  : - define longitude & latitude of t-, u-, v- and f-points (in degrees) 
+      !!              - define coriolis parameter at f-point if the domain in not on the sphere (on beta-plane)
+      !!              - define i- & j-scale factors at t-, u-, v- and f-points (in meters)
+      !!              - define u- & v-surfaces (if gridsize reduction is used in some straits) (in m2)
+      !!----------------------------------------------------------------------
+      REAL(wp), DIMENSION(:,:), INTENT(out) ::   plamt, plamu, plamv, plamf   ! longitude outputs                     [degrees]
+      REAL(wp), DIMENSION(:,:), INTENT(out) ::   pphit, pphiu, pphiv, pphif   ! latitude outputs                      [degrees]
+      INTEGER                 , INTENT(out) ::   kff                          ! =1 Coriolis parameter computed here, =0 otherwise
+      REAL(wp), DIMENSION(:,:), INTENT(out) ::   pff_f, pff_t                 ! Coriolis factor at f-point                [1/s]
+      REAL(wp), DIMENSION(:,:), INTENT(out) ::   pe1t, pe1u, pe1v, pe1f       ! i-scale factors                             [m]
+      REAL(wp), DIMENSION(:,:), INTENT(out) ::   pe2t, pe2u, pe2v, pe2f       ! j-scale factors                             [m]
+      INTEGER                 , INTENT(out) ::   ke1e2u_v                     ! =1 u- & v-surfaces computed here, =0 otherwise 
+      REAL(wp), DIMENSION(:,:), INTENT(out) ::   pe1e2u, pe1e2v               ! u- & v-surfaces (if reduction in strait)   [m2]
+      !
+      INTEGER  ::   ji, jj   ! dummy loop indices
+      REAL(wp) ::   zphi0, zlam0, zbeta, zf0
+      REAL(wp) ::   zti, zui, ztj, zvj   ! local scalars
+      !!-------------------------------------------------------------------------------
+      !
+      IF(lwp) WRITE(numout,*)
+      IF(lwp) WRITE(numout,*) 'usr_def_hgr : ICE_RHEO configuration bassin'
+      IF(lwp) WRITE(numout,*)
+      IF(lwp) WRITE(numout,*) '          f-plane with regular gridcell size'
+
+      !                          ==========
+      zlam0 = -(jpiglo-1)/2 * 1.e-3 * rn_dx
+      zphi0 = -(jpjglo-1)/2 * 1.e-3 * rn_dy
+
+
+      DO_2D( 1, 1, 1, 1 )
+         zti = FLOAT( ji - 1 + nimpp - 1 )          ;  ztj = FLOAT( jj - 1 + njmpp - 1 )
+         zui = FLOAT( ji - 1 + nimpp - 1 ) + 0.5_wp ;  zvj = FLOAT( jj - 1 + njmpp - 1 ) + 0.5_wp
+
+         plamt(ji,jj) = zlam0 + rn_dx * 1.e-3 * zti
+         plamu(ji,jj) = zlam0 + rn_dx * 1.e-3 * zui
+         plamv(ji,jj) = plamt(ji,jj) 
+         plamf(ji,jj) = plamu(ji,jj) 
+  
+         pphit(ji,jj) = zphi0 + rn_dy * 1.e-3 * ztj
+         pphiv(ji,jj) = zphi0 + rn_dy * 1.e-3 * zvj
+         pphiu(ji,jj) = pphit(ji,jj) 
+         pphif(ji,jj) = pphiv(ji,jj) 
+      END_2D
+         
+         ! Horizontal scale factors (in meters)
+         !                              ======
+!! ==> EITHER 1) variable scale factors
+!! clem: This can be used with a 1proc simulation but I think it breaks repro when >1procs are used      
+!!         DO_2D( 1, 1, 1, 1 )
+!!               !!pe1t(ji,jj) = rn_dx * EXP( -0.8/REAL(jpiglo**2) * (mi0(ji)-REAL(jpiglo+1)*0.5)**2 )  ! gaussian shape
+!!               !!pe2t(ji,jj) = rn_dy * EXP( -0.8/REAL(jpjglo**2) * (mj0(jj)-REAL(jpjglo+1)*0.5)**2 )  ! gaussian shape
+!!               pe1t(ji,jj) = rn_dx * ( 1. -0.1 * ABS(REAL(mi0(ji))-REAL(jpiglo+1)*0.5) / (1.-REAL(jpiglo+1)*0.5) ) ! linear shape
+!!               pe2t(ji,jj) = rn_dy * ( 1. -0.1 * ABS(REAL(mj0(jj))-REAL(jpjglo+1)*0.5) / (1.-REAL(jpjglo+1)*0.5) ) ! linear shape
+!!         END_2D
+!!#if defined key_agrif 
+!!         IF( .NOT. Agrif_Root() ) THEN ! only works if the zoom is positioned at the center of the parent grid
+!!            DO_2D( 1, 1, 1, 1 )
+!!                  pe1t(ji,jj) = rn_dx * ( 1. -0.1 * ABS(REAL(mi0(ji))-REAL(jpiglo+1)*0.5) / (1.-REAL(jpiglo+1)*0.5)  &
+!!                     &                            * REAL(jpiglo) / REAL(Agrif_Parent(jpiglo) * Agrif_Rhox()) )       ! factor to match parent grid
+!!                  pe2t(ji,jj) = rn_dy * ( 1. -0.1 * ABS(REAL(mj0(jj))-REAL(jpjglo+1)*0.5) / (1.-REAL(jpjglo+1)*0.5)  &
+!!                     &                            * REAL(jpjglo) / REAL(Agrif_Parent(jpjglo) * Agrif_Rhoy()) )       ! factor to match parent grid
+!!            END_2D
+!!         ENDIF
+!!#endif
+!! ==> OR 2) constant scale factors
+         pe1t(:,:) = rn_dx
+         pe2t(:,:) = rn_dy
+!! ==> END
+         
+      pe1u(:,:) = pe1t(:,:)      ;      pe2u(:,:) = pe2t(:,:)
+      pe1v(:,:) = pe1t(:,:)      ;      pe2v(:,:) = pe2t(:,:)
+      pe1f(:,:) = pe1t(:,:)      ;      pe2f(:,:) = pe2t(:,:)
+
+      !                             ! NO reduction of grid size in some straits 
+      ke1e2u_v = 0                  !    ==>> u_ & v_surfaces will be computed in dom_ghr routine
+      pe1e2u(:,:) = 0._wp           !    CAUTION: set to zero to avoid error with some compilers that
+      pe1e2v(:,:) = 0._wp           !             require an initialization of INTENT(out) arguments
+      !
+      !
+      !                       !==  Coriolis parameter  ==!
+      kff = 1                       !  indicate not to compute Coriolis parameter afterward
+      !
+      IF( ln_corio ) THEN
+         zbeta = 2._wp * omega * COS( rad * rn_ppgphi0 ) / ra
+         zf0   = 2._wp * omega * SIN( rad * rn_ppgphi0 )
+         pff_f(:,:) = zf0 + zbeta * pphif(:,:) * 1.e+3
+         pff_t(:,:) = zf0 + zbeta * pphit(:,:) * 1.e+3
+      ELSE
+         pff_f(:,:) = 0.
+         pff_t(:,:) = 0.
+      ENDIF
+      !
+   END SUBROUTINE usr_def_hgr
+
+   !!======================================================================
+END MODULE usrdef_hgr
Index: /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/MY_SRC/usrdef_nam.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/MY_SRC/usrdef_nam.F90	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/MY_SRC/usrdef_nam.F90	(revision 14016)
@@ -0,0 +1,107 @@
+MODULE usrdef_nam
+   !!======================================================================
+   !!                       ***  MODULE  usrdef_nam  ***
+   !!
+   !!                      ===  ICE_RHEO configuration  ===
+   !!
+   !! User defined : set the domain characteristics of a user configuration
+   !!======================================================================
+   !! History :  NEMO ! 2016-03  (S. Flavoni, G. Madec)  Original code
+   !!----------------------------------------------------------------------
+
+   !!----------------------------------------------------------------------
+   !!   usr_def_nam   : read user defined namelist and set global domain size
+   !!   usr_def_hgr   : initialize the horizontal mesh 
+   !!----------------------------------------------------------------------
+   USE dom_oce  , ONLY: nimpp , njmpp , Agrif_Root           ! i- & j-indices of the local domain
+   USE par_oce        ! ocean space and time domain
+   USE phycst         ! physical constants
+   !
+   USE in_out_manager ! I/O manager
+   USE lib_mpp        ! MPP library
+   USE timing         ! Timing
+   
+   IMPLICIT NONE
+   PRIVATE
+
+   PUBLIC   usr_def_nam   ! called by nemogcm.F90
+
+   !                               !!* namusr_def namelist *!!
+   REAL(wp), PUBLIC ::   rn_dx      ! resolution in meters defining the horizontal domain size
+   REAL(wp), PUBLIC ::   rn_dy      ! resolution in meters defining the horizontal domain size
+   REAL(wp), PUBLIC ::   rn_ppgphi0 ! reference latitude for beta-plane 
+   LOGICAL , PUBLIC ::   ln_corio   ! set coriolis at 0 (ln_corio=F) or not 
+
+   !!----------------------------------------------------------------------
+   !! NEMO/OCE 4.0 , NEMO Consortium (2018)
+   !! $Id: usrdef_nam.F90 10161 2018-10-01 09:48:55Z clem $ 
+   !! Software governed by the CeCILL license (see ./LICENSE)
+   !!----------------------------------------------------------------------
+CONTAINS
+
+   SUBROUTINE usr_def_nam( cd_cfg, kk_cfg, kpi, kpj, kpk, kperio )
+      !!----------------------------------------------------------------------
+      !!                     ***  ROUTINE dom_nam  ***
+      !!                    
+      !! ** Purpose :   read user defined namelist and define the domain size
+      !!
+      !! ** Method  :   read in namusr_def containing all the user specific namelist parameter
+      !!
+      !!                Here ICE_RHEO configuration
+      !!
+      !! ** input   : - namusr_def namelist found in namelist_cfg
+      !!----------------------------------------------------------------------
+      CHARACTER(len=*)              , INTENT(out) ::   cd_cfg          ! configuration name
+      INTEGER                       , INTENT(out) ::   kk_cfg          ! configuration resolution
+      INTEGER                       , INTENT(out) ::   kpi, kpj, kpk   ! global domain sizes 
+      INTEGER                       , INTENT(out) ::   kperio          ! lateral global domain b.c. 
+      !
+      INTEGER ::   ios       ! Local integer
+      REAL(wp)::   zlx, zly  ! Local scalars
+      !!
+      NAMELIST/namusr_def/ rn_dx, rn_dy, ln_corio, rn_ppgphi0
+      !!----------------------------------------------------------------------
+      !
+      READ  ( numnam_cfg, namusr_def, IOSTAT = ios, ERR = 902 )
+902   IF( ios /= 0 )   CALL ctl_nam ( ios , 'namusr_def in configuration namelist' )
+      !
+      IF(lwm)   WRITE( numond, namusr_def )
+      !
+      cd_cfg = 'ICE_RHEO'           ! name & resolution (not used)
+      kk_cfg = NINT( rn_dx )
+      !
+      ! Global Domain size:  domain is  2000 km x 2000 Km x 10 m
+         kpi = NINT( 2000.e3 / rn_dx ) 
+         kpj = NINT( 2000.e3 / rn_dy ) 
+      kpk = 1
+      !
+!!      zlx = (kpi-2)*rn_dx*1.e-3
+!!      zly = (kpj-2)*rn_dy*1.e-3
+      zlx = kpi*rn_dx*1.e-3
+      zly = kpj*rn_dy*1.e-3
+      !
+      kperio = 0     ! closed periodicity for the zoom
+      !                             ! control print
+      IF(lwp) THEN
+         WRITE(numout,*) '   '
+         WRITE(numout,*) 'usr_def_nam  : read the user defined namelist (namusr_def) in namelist_cfg'
+         WRITE(numout,*) '~~~~~~~~~~~ '
+         WRITE(numout,*) '   Namelist namusr_def : ICE_RHEO test case'
+         WRITE(numout,*) '      horizontal resolution                    rn_dx  = ', rn_dx, ' meters'
+         WRITE(numout,*) '      horizontal resolution                    rn_dy  = ', rn_dy, ' meters'
+         WRITE(numout,*) '      ICE_RHEO domain = 2000 km x 2000 km'
+         WRITE(numout,*) '         LX [km]: ', zlx
+         WRITE(numout,*) '         LY [km]: ', zly
+         WRITE(numout,*) '         resulting global domain size :        jpiglo = ', kpi
+         WRITE(numout,*) '                                               jpjglo = ', kpj
+         WRITE(numout,*) '                                               jpkglo = ', kpk
+         WRITE(numout,*) '         Coriolis:', ln_corio
+         WRITE(numout,*) '   '
+         WRITE(numout,*) '   Lateral boundary condition of the global domain'
+         WRITE(numout,*) '      ICE_RHEO closed basin                    jperio = ', kperio
+      ENDIF
+      !
+   END SUBROUTINE usr_def_nam
+
+   !!======================================================================
+END MODULE usrdef_nam
Index: /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/MY_SRC/usrdef_sbc.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/MY_SRC/usrdef_sbc.F90	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/MY_SRC/usrdef_sbc.F90	(revision 14016)
@@ -0,0 +1,220 @@
+MODULE usrdef_sbc
+   !!======================================================================
+   !!                       ***  MODULE  usrdef_sbc  ***
+   !! 
+   !!                      ===  ICE_RHEO configuration  ===
+   !!
+   !! User defined :   surface forcing of a user configuration
+   !!======================================================================
+   !! History :  4.0   ! 2016-03  (S. Flavoni, G. Madec)  user defined interface
+   !!----------------------------------------------------------------------
+
+   !!----------------------------------------------------------------------
+   !!   usr_def_sbc    : user defined surface bounday conditions in ICE_RHEO case
+   !!----------------------------------------------------------------------
+   USE oce             ! ocean dynamics and tracers
+   USE dom_oce         ! ocean space and time domain
+   USE sbc_oce         ! Surface boundary condition: ocean fields
+   USE sbc_ice         ! Surface boundary condition: ice fields
+   USE phycst          ! physical constants
+   USE ice, ONLY       : at_i_b, a_i_b, at_i, u_ice, v_ice
+   USE icethd_dh       ! for CALL ice_thd_snwblow
+   !
+   USE in_out_manager  ! I/O manager
+   USE lib_mpp         ! distribued memory computing library
+   USE lbclnk          ! ocean lateral boundary conditions (or mpp link)
+   USE lib_fortran     ! Fortran utilities (allows no signed zero when 'key_nosignedzero' defined) 
+
+   IMPLICIT NONE
+   PRIVATE
+
+   PUBLIC   usrdef_sbc_oce      ! routine called by sbcmod.F90 for sbc ocean
+   PUBLIC   usrdef_sbc_ice_tau  ! routine called by icestp.F90 for ice dynamics
+   PUBLIC   usrdef_sbc_ice_flx  ! routine called by icestp.F90 for ice thermo
+
+   !! * Substitutions
+#  include "do_loop_substitute.h90"
+   !!----------------------------------------------------------------------
+   !! NEMO/OCE 4.0 , NEMO Consortium (2018)
+   !! $Id: usrdef_sbc.F90 10074 2018-08-28 16:15:49Z nicolasmartin $
+   !! Software governed by the CeCILL license (see ./LICENSE)
+   !!----------------------------------------------------------------------
+CONTAINS
+
+   SUBROUTINE usrdef_sbc_oce( kt, Kbb )
+      !!---------------------------------------------------------------------
+      !!                    ***  ROUTINE usr_def_sbc  ***
+      !!              
+      !! ** Purpose :   provide at each time-step the surface boundary
+      !!              condition, i.e. the momentum, heat and freshwater fluxes.
+      !!
+      !! ** Method  :   all 0 fields, for ICE_RHEO case
+      !!                CAUTION : never mask the surface stress field !
+      !!
+      !! ** Action  : - set to ZERO all the ocean surface boundary condition, i.e.   
+      !!                   utau, vtau, taum, wndm, qns, qsr, emp, sfx
+      !!
+      !!----------------------------------------------------------------------
+      INTEGER, INTENT(in) ::   kt   ! ocean time step
+      INTEGER, INTENT(in) ::   Kbb  ! ocean time index
+      INTEGER ::    ij0, ij1, ii0, ii1, jj, ji   ! loop indices
+      REAL(wp) ::   zrhoco          ! ocean density and drag coefficient product
+      !!---------------------------------------------------------------------
+      !
+      IF( kt == nit000 ) THEN
+         !
+         !IF(lwp)   WRITE(numout,*)' usrdef_sbc_oce : ICE_RHEO case: ocean boudary conditions'
+
+         utau(:,:) = 0._wp
+         utau(:,:) = 0._wp
+
+         !ij0 = 1   ;   ij1 = 25                       ! set boundary condition
+         !ii0 = 975   ;   ii1 = 1000
+         !DO jj = mj0(ij0), mj1(ij1)
+         !   DO ji = mi0(ii0), mi1(ii1)
+         !      utau(ji,jj) = -utau_ice(ji,jj) 
+         !      vtau(ji,jj) = -vtau_ice(ji,jj)
+         !   END DO
+         !END DO
+
+         taum(:,:) = 0._wp   ! assume these are not used
+         wndm(:,:) = 0._wp
+         !
+         emp (:,:) = 0._wp
+         sfx (:,:) = 0._wp
+         qns (:,:) = 0._wp
+         qsr (:,:) = 0._wp
+         !
+         utau_b(:,:) = 0._wp 
+         vtau_b(:,:) = 0._wp
+         emp_b (:,:) = 0._wp
+         sfx_b (:,:) = 0._wp
+         qns_b (:,:) = 0._wp
+         !
+      ENDIF
+      !
+   END SUBROUTINE usrdef_sbc_oce
+
+   SUBROUTINE usrdef_sbc_ice_tau( kt )
+      !!---------------------------------------------------------------------
+      !!                     ***  ROUTINE usrdef_sbc_ice_tau  ***
+      !!
+      !! ** Purpose :   provide the surface boundary (momentum) condition over
+      !sea-ice
+      !!---------------------------------------------------------------------
+      INTEGER, INTENT(in) ::   kt   ! ocean time step
+      INTEGER ::   jj, ji   ! loop indices
+
+      REAL(wp) ::   zwndi_f , zwndj_f, zwnorm_f       ! relative wind module and components at F-point
+      REAL(wp) ::   zwndi_t , zwndj_t                 ! relative wind components at T-point
+      REAL(wp), DIMENSION(jpi,jpj) ::   windu, windv  ! wind components (idealised forcing)
+      REAL(wp), PARAMETER ::   r_vfac = 1._wp         ! relative velocity (make 0 for absolute velocity) 
+      REAL(wp), PARAMETER ::   Rwind = -0.8_wp        ! ratio of wind components
+      REAL(wp), PARAMETER ::   Umax = 15._wp          ! maximum wind speed (m/s)
+      REAL(wp), PARAMETER ::   d = 2000._wp           ! size of the domain (km)
+      REAL(wp), PARAMETER ::   res = 2._wp            ! gridcell size
+      REAL(wp), PARAMETER ::   zrhoa  = 1.22          ! Air density kg/m3
+      REAL(wp), PARAMETER ::   Cd_atm =  1.4e-3       ! transfer coefficient over ice
+      !!---------------------------------------------------------------------
+      ! extra code for test case
+      IF( kt==nit000 .AND. lwp)   WRITE(numout,*)' usrdef_sbc_ice : ICE_RHEO case: analytical stress forcing'
+
+      DO_2D( 0, 0, 0, 0 )
+         ! wind spins up over 6 hours, factor 1000 to balance the units
+         windu(ji,jj) = Umax/sqrt(d*1000)*(d-2*mig(ji)*res)/((d-2*mig(ji)*res)**2+(d-2*mjg(jj)*res)**2*Rwind**2)**(1/4)*min(kt*30./21600,1.)
+         windv(ji,jj) = Umax/sqrt(d*1000)*(d-2*mjg(jj)*res)/((d-2*mig(ji)*res)**2+(d-2*mjg(jj)*res)**2*Rwind**2)**(1/4)*Rwind*min(kt*30./21600,1.)
+      END_2D
+      CALL lbc_lnk_multi( 'usrdef_sbc', windu, 'U', -1., windv, 'V', -1. )
+
+      wndm_ice(:,:) = 0._wp      !!gm brutal....
+
+      ! ------------------------------------------------------------ !
+      !    Wind module relative to the moving ice ( U10m - U_ice )   !
+      ! ------------------------------------------------------------ !
+      ! C-grid ice dynamics :   U & V-points (same as ocean)
+      DO_2D( 0, 0, 0, 0 )
+         zwndi_t = (  windu(ji,jj) - r_vfac * 0.5 * ( u_ice(ji-1,jj  ) + u_ice(ji,jj) )  )
+         zwndj_t = (  windv(ji,jj) - r_vfac * 0.5 * ( v_ice(ji,jj-1) + v_ice(ji,jj) )  )
+         wndm_ice(ji,jj) = SQRT( zwndi_t * zwndi_t + zwndj_t * zwndj_t ) * tmask(ji,jj,1)
+      END_2D
+      CALL lbc_lnk( 'usrdef_sbc', wndm_ice, 'T',  1. )
+
+      !!gm brutal....
+      utau_ice  (:,:) = 0._wp
+      vtau_ice  (:,:) = 0._wp
+      !!gm end
+
+      ! ------------------------------------------------------------ !
+      !    Wind stress relative to the moving ice ( U10m - U_ice )   !
+      ! ------------------------------------------------------------ !
+      ! C-grid ice dynamics :   U & V-points (same as ocean)
+      DO_2D( 0, 0, 0, 0 )
+         utau_ice(ji,jj) = 0.5 * zrhoa * Cd_atm * ( wndm_ice(ji+1,jj  ) + wndm_ice(ji,jj) )            &
+            &          * ( 0.5 * (windu(ji+1,jj) + windu(ji,jj) ) - r_vfac * u_ice(ji,jj) )
+         vtau_ice(ji,jj) = 0.5 * zrhoa * Cd_atm * ( wndm_ice(ji,jj+1  ) + wndm_ice(ji,jj) )            &
+            &          * ( 0.5 * (windv(ji,jj+1) + windv(ji,jj) ) - r_vfac * v_ice(ji,jj) )
+      END_2D
+      CALL lbc_lnk_multi( 'usrdef_sbc', utau_ice, 'U', -1., vtau_ice, 'V', -1. )
+      !
+   END SUBROUTINE usrdef_sbc_ice_tau
+
+   SUBROUTINE usrdef_sbc_ice_flx( kt, phs, phi )
+      !!---------------------------------------------------------------------
+      !!                     ***  ROUTINE usrdef_sbc_ice_flx  ***
+      !!
+      !! ** Purpose :   provide the surface boundary (flux) condition over sea-ice
+      !!---------------------------------------------------------------------
+      INTEGER, INTENT(in) ::   kt   ! ocean time step
+      REAL(wp), DIMENSION(:,:,:), INTENT(in) ::   phs    ! snow thickness
+      REAL(wp), DIMENSION(:,:,:), INTENT(in) ::   phi    ! ice thickness
+      !!
+      REAL(wp) ::   zfr1, zfr2                 ! local variables
+      REAL(wp), DIMENSION(jpi,jpj) ::   zsnw   ! snw distribution after wind blowing
+      !!---------------------------------------------------------------------
+      !
+      IF( kt==nit000 .AND. lwp)   WRITE(numout,*)' usrdef_sbc_ice : ICE_RHEO case: NO flux forcing'
+      !
+      ! ocean variables (renaming)
+      emp_oce (:,:)   = 0._wp   ! uniform value for freshwater budget (E-P)
+      qsr_oce (:,:)   = 0._wp   ! uniform value for     solar radiation
+      qns_oce (:,:)   = 0._wp   ! uniform value for non-solar radiation
+
+      ! ice variables
+      alb_ice (:,:,:) = 0.7_wp  ! useless
+      qsr_ice (:,:,:) = 0._wp   ! uniform value for     solar radiation
+      qns_ice (:,:,:) = 0._wp   ! uniform value for non-solar radiation
+      sprecip (:,:)   = 0._wp   ! uniform value for snow precip
+      evap_ice(:,:,:) = 0._wp   ! uniform value for sublimation
+
+      ! ice fields deduced from above
+      zsnw(:,:) = 1._wp
+      !!CALL lim_thd_snwblow( at_i_b, zsnw )  ! snow distribution over ice after wind blowing 
+      emp_ice  (:,:)   = SUM( a_i_b(:,:,:) * evap_ice(:,:,:), dim=3 ) - sprecip(:,:) * zsnw(:,:)
+      emp_oce  (:,:)   = emp_oce(:,:) - sprecip(:,:) * (1._wp - zsnw(:,:) )
+      qevap_ice(:,:,:) =   0._wp
+      qprec_ice(:,:)   =   rhos * ( sst_m(:,:) * rcpi - rLfus ) * tmask(:,:,1) !  in J/m3
+      qemp_oce (:,:)   = - emp_oce(:,:) * sst_m(:,:) * rcp
+      qemp_ice (:,:)   =   sprecip(:,:) * zsnw * ( sst_m(:,:) * rcpi - rLfus ) * tmask(:,:,1) ! solid precip (only)
+
+      ! total fluxes
+      emp_tot (:,:) = emp_ice  + emp_oce
+      qns_tot (:,:) = at_i_b(:,:) * qns_oce(:,:) + SUM( a_i_b(:,:,:) * qns_ice(:,:,:), dim=3 ) + qemp_ice(:,:) + qemp_oce(:,:)
+      qsr_tot (:,:) = at_i_b(:,:) * qsr_oce(:,:) + SUM( a_i_b(:,:,:) * qsr_ice(:,:,:), dim=3 )
+
+      ! --- shortwave radiation transmitted below the surface (W/m2, see Grenfell Maykut 77) --- !
+      zfr1 = ( 0.18 * ( 1.0 - pp_cldf ) + 0.35 * pp_cldf )            ! transmission when hi>10cm
+      zfr2 = ( 0.82 * ( 1.0 - pp_cldf ) + 0.65 * pp_cldf )            ! zfr2 such that zfr1 + zfr2 to equal 1
+      !
+      WHERE    ( phs(:,:,:) <= 0._wp .AND. phi(:,:,:) <  0.1_wp )       ! linear decrease from hi=0 to 10cm  
+         qtr_ice_top(:,:,:) = qsr_ice(:,:,:) * ( zfr1 + zfr2 * ( 1._wp - phi(:,:,:) * 10._wp ) )
+      ELSEWHERE( phs(:,:,:) <= 0._wp .AND. phi(:,:,:) >= 0.1_wp )       ! constant (zfr1) when hi>10cm
+         qtr_ice_top(:,:,:) = qsr_ice(:,:,:) * zfr1
+      ELSEWHERE                                                         ! zero when hs>0
+         qtr_ice_top(:,:,:) = 0._wp 
+      END WHERE
+          
+   END SUBROUTINE usrdef_sbc_ice_flx
+
+
+   !!======================================================================
+END MODULE usrdef_sbc
Index: /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/MY_SRC/usrdef_zgr.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/MY_SRC/usrdef_zgr.F90	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/MY_SRC/usrdef_zgr.F90	(revision 14016)
@@ -0,0 +1,109 @@
+MODULE usrdef_zgr
+   !!======================================================================
+   !!                   ***  MODULE  usrdef_zgr  ***
+   !!
+   !!                   ===      ICE_RHEO case     ===
+   !!
+   !! Ocean domain : user defined vertical coordinate system 
+   !!======================================================================
+   !! History :  4.0  ! 2016-08  (G. Madec, S. Flavoni)  Original code
+   !!----------------------------------------------------------------------
+
+   !!----------------------------------------------------------------------
+   !!   usr_def_zgr   : user defined vertical coordinate system (required)
+   !!---------------------------------------------------------------------
+   USE oce            ! ocean variables
+   USE usrdef_nam     ! User defined : namelist variables
+   !
+   USE in_out_manager ! I/O manager
+   USE lbclnk         ! ocean lateral boundary conditions (or mpp link)
+   USE lib_mpp        ! distributed memory computing library
+
+   IMPLICIT NONE
+   PRIVATE
+
+   PUBLIC   usr_def_zgr   ! called by domzgr.F90
+
+   !!----------------------------------------------------------------------
+   !! NEMO/OCE 4.0 , NEMO Consortium (2018)
+   !! $Id: usrdef_zgr.F90 10074 2018-08-28 16:15:49Z nicolasmartin $
+   !! Software governed by the CeCILL license (see ./LICENSE)
+   !!----------------------------------------------------------------------
+CONTAINS             
+
+   SUBROUTINE usr_def_zgr( ld_zco  , ld_zps  , ld_sco  , ld_isfcav,    &   ! type of vertical coordinate
+      &                    pdept_1d, pdepw_1d, pe3t_1d , pe3w_1d  ,    &   ! 1D reference vertical coordinate
+      &                    pdept , pdepw ,                             &   ! 3D t & w-points depth
+      &                    pe3t  , pe3u  , pe3v , pe3f ,               &   ! vertical scale factors
+      &                    pe3w  , pe3uw , pe3vw,                      &   !     -      -      -
+      &                    k_top  , k_bot    )                             ! top & bottom ocean level
+      !!---------------------------------------------------------------------
+      !!              ***  ROUTINE usr_def_zgr  ***
+      !!
+      !! ** Purpose :   User defined the vertical coordinates
+      !!
+      !!----------------------------------------------------------------------
+      LOGICAL                   , INTENT(out) ::   ld_zco, ld_zps, ld_sco      ! vertical coordinate flags
+      LOGICAL                   , INTENT(out) ::   ld_isfcav                   ! under iceshelf cavity flag
+      REAL(wp), DIMENSION(:)    , INTENT(out) ::   pdept_1d, pdepw_1d          ! 1D grid-point depth     [m]
+      REAL(wp), DIMENSION(:)    , INTENT(out) ::   pe3t_1d , pe3w_1d           ! 1D grid-point depth     [m]
+      REAL(wp), DIMENSION(:,:,:), INTENT(out) ::   pdept, pdepw                ! grid-point depth        [m]
+      REAL(wp), DIMENSION(:,:,:), INTENT(out) ::   pe3t , pe3u , pe3v , pe3f   ! vertical scale factors  [m]
+      REAL(wp), DIMENSION(:,:,:), INTENT(out) ::   pe3w , pe3uw, pe3vw         ! i-scale factors 
+      INTEGER , DIMENSION(:,:)  , INTENT(out) ::   k_top, k_bot                ! first & last ocean level
+      !
+      INTEGER  ::   jk, k_dz  ! dummy indices
+      !!----------------------------------------------------------------------
+      !
+      IF(lwp) WRITE(numout,*)
+      IF(lwp) WRITE(numout,*) 'usr_def_zgr : ICE_RHEO configuration '
+      IF(lwp) WRITE(numout,*) '~~~~~~~~~~~   (slab ocean - ice cover in square box domain)'
+      !
+      !
+      ! type of vertical coordinate  ==>>>   here ICE_RHEO : slab ocean always
+      ! ---------------------------
+      ld_zco    = .TRUE.       ! z-full-step coordinate
+      ld_zps    = .FALSE.      ! z-partial-step coordinate
+      ld_sco    = .FALSE.      ! s-coordinate
+      ld_isfcav = .FALSE.      ! ISF Ice Shelves Flag
+      !
+      !
+      ! Build the vertical coordinate system
+      ! ------------------------------------
+      !
+      !                       !==  UNmasked meter bathymetry  ==!
+      !
+      ! 
+      k_dz = 1
+      DO jk = 1, jpk
+         pdepw_1d(jk) =    k_dz
+         pdept_1d(jk) =    k_dz
+         pe3w_1d (jk) =    k_dz
+         pe3t_1d (jk) =    k_dz
+      END DO
+      !                       !==  top masked level bathymetry  ==!  (all coordinates)
+      !
+      ! no ocean cavities : top ocean level is ONE, except over land
+      k_top(:,:) = 1
+      !
+      !                       !==  z-coordinate  ==!   (step-like topography)
+      !                                !* bottom ocean compute from the depth of grid-points
+      jpkm1 = jpk
+      k_bot(:,:) = 1    ! here use k_top as a land mask
+      !                                !* horizontally uniform coordinate (reference z-co everywhere)
+      DO jk = 1, jpk
+         pdept(:,:,jk) = pdept_1d(jk)
+         pdepw(:,:,jk) = pdepw_1d(jk)
+         pe3t (:,:,jk) = pe3t_1d (jk)
+         pe3u (:,:,jk) = pe3t_1d (jk)
+         pe3v (:,:,jk) = pe3t_1d (jk)
+         pe3f (:,:,jk) = pe3t_1d (jk)
+         pe3w (:,:,jk) = pe3w_1d (jk)
+         pe3uw(:,:,jk) = pe3w_1d (jk)
+         pe3vw(:,:,jk) = pe3w_1d (jk)
+      END DO
+      !
+   END SUBROUTINE usr_def_zgr
+
+   !!======================================================================
+END MODULE usrdef_zgr
Index: /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/cpp_ICE_RHEO.fcm
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/cpp_ICE_RHEO.fcm	(revision 14016)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/ICE_RHEO/cpp_ICE_RHEO.fcm	(revision 14016)
@@ -0,0 +1,1 @@
+bld::tool::fppkeys key_si3 key_mpp_mpi key_nosignedzero key_iomput
Index: /NEMO/branches/2020/tickets_icb_1900/tests/ISOMIP+/MY_SRC/eosbn2.F90
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/ISOMIP+/MY_SRC/eosbn2.F90	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/ISOMIP+/MY_SRC/eosbn2.F90	(revision 14016)
@@ -56,5 +56,5 @@
    !                  !! * Interface
    INTERFACE eos
-      MODULE PROCEDURE eos_insitu, eos_insitu_pot, eos_insitu_2d
+      MODULE PROCEDURE eos_insitu, eos_insitu_pot, eos_insitu_2d, eos_insitu_pot_2d
    END INTERFACE
    !
@@ -624,4 +624,90 @@
 
 
+   SUBROUTINE eos_insitu_pot_2d( pts, prhop )
+      !!----------------------------------------------------------------------
+      !!                  ***  ROUTINE eos_insitu_pot  ***
+      !!
+      !! ** Purpose :   Compute the in situ density (ratio rho/rho0) and the
+      !!      potential volumic mass (Kg/m3) from potential temperature and
+      !!      salinity fields using an equation of state selected in the
+      !!     namelist.
+      !!
+      !! ** Action  :
+      !!              - prhop, the potential volumic mass (Kg/m3)
+      !!
+      !!----------------------------------------------------------------------
+      REAL(wp), DIMENSION(jpi,jpj,jpts), INTENT(in   ) ::   pts    ! 1 : potential temperature  [Celsius]
+      !                                                                ! 2 : salinity               [psu]
+      REAL(wp), DIMENSION(jpi,jpj     ), INTENT(  out) ::   prhop  ! potential density (surface referenced)
+      !
+      INTEGER  ::   ji, jj, jk, jsmp             ! dummy loop indices
+      INTEGER  ::   jdof
+      REAL(wp) ::   zt , zh , zstemp, zs , ztm   ! local scalars
+      REAL(wp) ::   zn , zn0, zn1, zn2, zn3      !   -      -
+      REAL(wp), DIMENSION(:), ALLOCATABLE :: zn0_sto, zn_sto, zsign    ! local vectors
+      !!----------------------------------------------------------------------
+      !
+      IF( ln_timing )   CALL timing_start('eos-pot')
+      !
+      SELECT CASE ( neos )
+      !
+      CASE( np_teos10, np_eos80 )                !==  polynomial TEOS-10 / EOS-80 ==!
+         !
+            DO_2D( 1, 1, 1, 1 )
+               !
+               zt  = pts (ji,jj,jp_tem) * r1_T0                           ! temperature
+               zs  = SQRT( ABS( pts(ji,jj,jp_sal) + rdeltaS ) * r1_S0 )   ! square root salinity
+               ztm = tmask(ji,jj,1)                                         ! tmask
+               !
+               zn0 = (((((EOS060*zt   &
+                  &   + EOS150*zs+EOS050)*zt   &
+                  &   + (EOS240*zs+EOS140)*zs+EOS040)*zt   &
+                  &   + ((EOS330*zs+EOS230)*zs+EOS130)*zs+EOS030)*zt   &
+                  &   + (((EOS420*zs+EOS320)*zs+EOS220)*zs+EOS120)*zs+EOS020)*zt   &
+                  &   + ((((EOS510*zs+EOS410)*zs+EOS310)*zs+EOS210)*zs+EOS110)*zs+EOS010)*zt   &
+                  &   + (((((EOS600*zs+EOS500)*zs+EOS400)*zs+EOS300)*zs+EOS200)*zs+EOS100)*zs+EOS000
+                  !
+               !
+               prhop(ji,jj) = zn0 * ztm                           ! potential density referenced at the surface
+               !
+            END_2D
+
+      CASE( np_seos )                !==  simplified EOS  ==!
+         !
+         DO_2D( 1, 1, 1, 1 )
+            zt  = pts  (ji,jj,jp_tem) - 10._wp
+            zs  = pts  (ji,jj,jp_sal) - 35._wp
+            ztm = tmask(ji,jj,1)
+            !                                                     ! potential density referenced at the surface
+            zn =  - rn_a0 * ( 1._wp + 0.5_wp*rn_lambda1*zt ) * zt   &
+               &  + rn_b0 * ( 1._wp - 0.5_wp*rn_lambda2*zs ) * zs   &
+               &  - rn_nu * zt * zs
+            prhop(ji,jj) = ( rho0 + zn ) * ztm
+            !
+         END_2D
+         !
+      CASE( np_leos )                !==  ISOMIP EOS  ==!
+         !
+         DO_2D( 1, 1, 1, 1 )
+            !
+            zt    = pts  (ji,jj,jp_tem)  - (-1._wp)
+            zs    = pts  (ji,jj,jp_sal)  - 34.2_wp
+            !zh    = pdep (ji,jj)                         ! depth at the partial step level
+            !
+            zn =  rho0 * ( - rn_a0 * zt + rn_b0 * zs )
+            !
+            prhop(ji,jj) = zn * r1_rho0               ! unmasked in situ density anomaly
+            !
+         END_2D
+         !
+      END SELECT
+      !
+      IF(sn_cfctl%l_prtctl)   CALL prt_ctl( tab2d_1=prhop, clinfo1=' eos-pot: ' )
+      !
+      IF( ln_timing )   CALL timing_stop('eos-pot')
+      !
+   END SUBROUTINE eos_insitu_pot_2d
+
+
    SUBROUTINE rab_3d( pts, pab, Kmm )
       !!
Index: /NEMO/branches/2020/tickets_icb_1900/tests/demo_cfgs.txt
===================================================================
--- /NEMO/branches/2020/tickets_icb_1900/tests/demo_cfgs.txt	(revision 14015)
+++ /NEMO/branches/2020/tickets_icb_1900/tests/demo_cfgs.txt	(revision 14016)
@@ -12,2 +12,4 @@
 STATION_ASF OCE
 CPL_OASIS  OCE TOP ICE NST
+C1D_ASICS OCE
+ICE_RHEO OCE SAS ICE
