Skip to content
Snippets Groups Projects
icedyn_rhg_vp.F90 87.8 KiB
Newer Older
Guillaume Samson's avatar
Guillaume Samson committed
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

   INTEGER  ::   nn_nvp              ! total number of VP iterations (n_out_vp*n_inn_vp)
   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-06 ! minimum velocity error, beyond which convergence is assumed
Guillaume Samson's avatar
Guillaume Samson committed

   !! for convergence tests
   INTEGER ::   ncvgid        ! netcdf file id
   INTEGER ::   nvarid_ures, nvarid_vres, nvarid_velres
   INTEGER ::   nvarid_uerr_max, nvarid_verr_max, nvarid_velerr_max
   INTEGER ::   nvarid_umad, nvarid_vmad, nvarid_velmad
   INTEGER ::   nvarid_umad_outer, nvarid_vmad_outer, nvarid_velmad_outer
   INTEGER ::   nvarid_mke

   REAL(wp), DIMENSION(:,:), ALLOCATABLE ::   fimask   ! mask at F points for the ice
   
   !! * Substitutions
#  include "do_loop_substitute.h90"
   !!----------------------------------------------------------------------
   !! 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(u)
      !!
      !!  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 ::   i_out, i_inn, i_inn_tot  ! 
      INTEGER ::   ji_min, jj_min      !
      INTEGER ::   nn_zebra_vp         ! number of zebra steps

      !
      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) ::   zds2, zdt, zdt2, zdiv, zdiv2                        ! temporary scalars
      REAL(wp) ::   zvisc_f                                        ! 
Guillaume Samson's avatar
Guillaume Samson committed
      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) ::   za_iU  , za_iV                  ! ice fraction on U/V points
Guillaume Samson's avatar
Guillaume Samson committed
      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) ::   zdelta                          ! Delta at T-points      (now value)
      REAL(wp), DIMENSION(jpi,jpj) ::   zten_i, zshear                  ! Tension, shear
      REAL(wp), DIMENSION(jpi,jpj) ::   zvisc_t                         ! Bulk viscosity (P/delta*) at T points
      REAL(wp), DIMENSION(jpi,jpj) ::   zvisc_t_prev                    ! Bulk viscosity (next to last iterate) - for yield curve diag
Guillaume Samson's avatar
Guillaume Samson committed
      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) ::   zvel_res                        ! Residual of the linear system at last iteration
      REAL(wp), DIMENSION(jpi,jpj) ::   zvel_diff                       ! Absolute velocity difference @last outer iteration
Guillaume Samson's avatar
Guillaume Samson committed
      !
      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) ::   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) ::   zmsk, zmsk00
Guillaume Samson's avatar
Guillaume Samson committed
      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, z1_strength, zfac_x, zfac_y
Guillaume Samson's avatar
Guillaume Samson committed
      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)
                        
      
      !!----------------------------------------------------------------------------------------------------------------------

      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
      !
      !------------------------------------------------------------------------------!
      IF ( lp_zebra_vp ) THEN; nn_zebra_vp = 2
                         ELSE; nn_zebra_vp = 1; ENDIF 

Guillaume Samson's avatar
Guillaume Samson committed
      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
      
      ! for diagnostics and convergence tests
      DO_2D( nn_hls, nn_hls, nn_hls, nn_hls )
         zmsk00(ji,jj) = MAX( 0._wp , SIGN( 1._wp , at_i(ji,jj) - epsi06  ) ) ! 1 if ice    , 0 if no ice
         zmsk  (ji,jj) = MAX( 0._wp , SIGN( 1._wp , at_i(ji,jj) - epsi10  ) ) ! 1 if ice    , 0 if no ice
      END_2D
Guillaume Samson's avatar
Guillaume Samson committed
      
      !---------------------------
      ! -- F-mask (code from EVP)
      !---------------------------
      IF( kt == nit000 ) THEN
         ! MartinV: 
         ! In EVP routine, fimask 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
         ALLOCATE( fimask(jpi,jpj) )
         IF( rn_ishlat == 0._wp ) THEN
            DO_2D( 0, 0, 0, 0 )
               fimask(ji,jj) = tmask(ji,jj,1) * tmask(ji+1,jj,1) * tmask(ji,jj+1,1) * tmask(ji+1,jj+1,1)
            END_2D
         ELSE
            DO_2D( 0, 0, 0, 0 )
               fimask(ji,jj) = tmask(ji,jj,1) * tmask(ji+1,jj,1) * tmask(ji,jj+1,1) * tmask(ji+1,jj+1,1)
               ! Lateral boundary conditions on velocity (modify fimask)
               IF( fimask(ji,jj) == 0._wp ) THEN
                  fimask(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
Guillaume Samson's avatar
Guillaume Samson committed
         CALL lbc_lnk( 'icedyn_rhg_vp', fimask, 'F', 1._wp )
      ENDIF

      ! Initialise convergence checks
      IF( nn_rhg_chkcvg /= 0 ) THEN     
         ! ice area for global mean kinetic energy (m2)
         zglob_area = glob_sum( 'ice_rhg_vp', at_i(:,:) * e1e2t(:,:) * tmask(:,:,1) )
      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

      !------------------------------------------------------------------------------!
      !
      ! --- Time-independent quantities
      !
      !------------------------------------------------------------------------------!
      zrhoco = rho0 * rn_cio 

      ! ecc2: square of yield ellipse eccentricity
      ecc2    = rn_ecc * rn_ecc
      z1_ecc2 = 1._wp / ecc2
               
      
      CALL ice_strength ! strength at T points
      
Guillaume Samson's avatar
Guillaume Samson committed
      
      !----------------------------------------------------------------------------------------------------------
      ! -- 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)    

      DO_2D( nn_hls, nn_hls, nn_hls, nn_hls )
         zmt(ji,jj) = rhos * vt_s(ji,jj) + rhoi * vt_i(ji,jj)       ! Snow and ice mass at T-point
         zmf(ji,jj) = zmt(ji,jj) * ff_t(ji,jj)                      ! Coriolis factor at T points (m*f)
      END_2D
      
      DO_2D( nn_hls, nn_hls-1, nn_hls-1, nn_hls )
Guillaume Samson's avatar
Guillaume Samson committed

         ! 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)

         ! Snow and ice mass at U-V points
         zm1             = zmt(ji,jj)
         zm2             = zmt(ji+1,jj)
         zmassU          = 0.5_wp * ( zm1 * e1e2t(ji,jj) + zm2 * e1e2t(ji+1,jj) ) * r1_e1e2u(ji,jj) * umask(ji,jj,1)
         
         ! Mass per unit area divided by time step
         zmassU_t(ji,jj) = zmassU * 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)
         
         ! Ocean currents at U-V points
         ! (brackets added to fix the order of floating point operations for halo 1 - halo 2 compatibility)
         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)
Guillaume Samson's avatar
Guillaume Samson committed
         
         ! Wind stress
         ztaux_ai(ji,jj) = za_iU(ji,jj) * utau_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)
         !!spgU(ji,jj)    = - grav * ( zsshdyn(ji,jj) ) * r1_e1u(ji,jj)
Guillaume Samson's avatar
Guillaume Samson committed
         
         ! Mask for ice presence (1) or absence (0)
         zmsk00x(ji,jj)  = 1._wp - MAX( 0._wp, SIGN( 1._wp, -zmassU ) )  ! 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

      END_2D      
         
            
      DO_2D( nn_hls-1, nn_hls, nn_hls, nn_hls-1 ) ! 2->jpj-1; 2->jpi-1

         ! Ice fraction at U-V points
         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)
         zm3             = zmt(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
         zmassV_t(ji,jj) = zmassV * r1_Dt_ice
         
         ! Acceleration term contribution to RHS (depends on velocity at previous time step)            
         zmV_t(ji,jj)    = zmassV_t(ji,jj) * v_ice(ji,jj)
         
         ! Ocean currents at U-V points
         ! (brackets added to fix the order of floating point operations for halo 1 - halo 2 compatibility)
         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
         ztauy_ai(ji,jj) = za_iV(ji,jj) * vtau_ice(ji,jj)
         
         ! Force due to sea surface tilt(- m*g*GRAD(ssh))
         zspgV(ji,jj)    = - zmassV * grav * ( zsshdyn(ji,jj+1) - zsshdyn(ji,jj) ) * r1_e2v(ji,jj)
         
         ! Mask for ice presence (1) or absence (0)
         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)
Guillaume Samson's avatar
Guillaume Samson committed
         IF ( zmassV <= zmmin .AND. za_iV(ji,jj) <= zamin ) THEN   ;   zmsk01y(ji,jj) = 0._wp
         ELSE                                                      ;   zmsk01y(ji,jj) = 1._wp   ;   ENDIF              

      END_2D  
            
      !------------------------------------------------------------------------------!
      !
      ! --- Start outer loop
      !
      !------------------------------------------------------------------------------!
      
      zu_c(:,:) = u_ice(:,:)
      zv_c(:,:) = v_ice(:,:)
      
      i_inn_tot = 0

      DO i_out = 1, nn_vp_nout

         ! 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 "current" velocities (uc, vc)
      
         !------------------------------------------
         ! -- Strain rates, viscosities and P/Delta
         !------------------------------------------

         ! --- divergence, tension & shear (Appendix B of Hunke & Dukowicz, 2002) --- !
         DO_2D( nn_hls, nn_hls-1, nn_hls, nn_hls-1 ) ! 1->jpi-1
         
            ! loops start at 1 since there is no boundary condition (lbc_lnk) at i=1 and j=1 for F points
            ! 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) * fimask(ji,jj)

         END_2D

         DO_2D( 0, 0, 0, 0 )
Guillaume Samson's avatar
Guillaume Samson committed
            ! loop to jpi,jpj to avoid making a communication for zs1,zs2,zs12

            ! 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
            ! (brackets added to fix the order of floating point operations for halo 1 - halo 2 compatibility)
            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))   &
Guillaume Samson's avatar
Guillaume Samson committed
               &    ) * 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
            zdelta(ji,jj)  = SQRT( zdiv2 + ( zdt2 + zds2 ) * z1_ecc2 ) * zmsk(ji,jj) 
Guillaume Samson's avatar
Guillaume Samson committed
               
            ! P/delta at T points
            zvisc_t(ji,jj) = strength(ji,jj) / ( zdelta(ji,jj) + rn_creepl ) * zmsk(ji,jj)

Guillaume Samson's avatar
Guillaume Samson committed
            ! Temporary zzt and zet factors at T-points
            zzt(ji,jj)     = zvisc_t(ji,jj) * r1_e1e2t(ji,jj)
            zet(ji,jj)     = zzt(ji,jj)     * z1_ecc2 
Guillaume Samson's avatar
Guillaume Samson committed
                          
         END_2D
         CALL lbc_lnk( 'icedyn_rhg_vp', zdelta, 'T', 1.0_wp, zvisc_t, 'T', 1.0_wp, zzt, 'T', 1.0_wp, zet, 'T', 1.0_wp )
Guillaume Samson's avatar
Guillaume Samson committed
         
         ! Store bulk viscosity at last outer iteration for yield curve diagnostic
         IF ( i_out == nn_vp_nout .AND. ( iom_use('sig1_pnorm') .OR. iom_use('sig2_pnorm') ) ) THEN
            zvisc_t_prev(:,:) = zvisc_t(:,:)
         ENDIF
Guillaume Samson's avatar
Guillaume Samson committed

         DO_2D( nn_hls, nn_hls-1, nn_hls, nn_hls-1 )! 1-> jpj-1; 1->jpi-1
         
            ! P/delta* at F points
            ! (brackets added to fix the order of floating point operations for halo 1 - halo 2 compatibility)
            zvisc_f = 0.25_wp * ( (zvisc_t(ji,jj) + zvisc_t(ji+1,jj)) + (zvisc_t(ji,jj+1) + zvisc_t(ji+1,jj+1)) )
            
            ! Temporary zef factor at F-point
            zef(ji,jj) = zvisc_f * r1_e1e2f(ji,jj) * z1_ecc2 * fimask(ji,jj) * 0.5_wp
            
Guillaume Samson's avatar
Guillaume Samson committed
         END_2D
         
         !---------------------------------------------------
         ! -- Ocean-ice drag and Coriolis RHS contributions
         !---------------------------------------------------

         DO_2D( nn_hls, nn_hls-1, nn_hls-1, nn_hls )
Guillaume Samson's avatar
Guillaume Samson committed
         
            !--- ice u-velocity @V points, v-velocity @U points (for non-linear drag computation)
            ! (brackets added to fix the order of floating point operations for halo 1 - halo 2 compatibility)
            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)
Guillaume Samson's avatar
Guillaume Samson committed
                
            !--- 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) ) )
                 
            !--- Ocean-ice drag contributions to RHS 
            ztaux_oi_rhsu(ji,jj) = zCwU(ji,jj) * u_oce(ji,jj)
                
            !--- U-component of Coriolis Force (energy conserving formulation)
            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) )) )
Guillaume Samson's avatar
Guillaume Samson committed
                           
         END_2D

         DO_2D( nn_hls-1, nn_hls, nn_hls, nn_hls-1 )
         
            !--- ice u-velocity @V points, v-velocity @U points (for non-linear drag computation)
            ! (brackets added to fix the order of floating point operations for halo 1 - halo 2 compatibility)
            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)
                
            !--- non-linear drag coefficients (need to be updated at each outer loop, see Lemieux and Tremblay JGR09, p.3, beginning of Section 3)
            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 
            ztauy_oi_rhsv(ji,jj) = zCwV(ji,jj) * v_oce(ji,jj)
                
Guillaume Samson's avatar
Guillaume Samson committed
            !--- 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) )) )
Guillaume Samson's avatar
Guillaume Samson committed

         END_2D
         
         !-------------------------------------
         ! -- Internal stress RHS contribution
         !-------------------------------------

         ! --- Stress contributions at T-points
         DO_2D( nn_hls, nn_hls, nn_hls-1, nn_hls ) ! 2 -> jpj; 2,jpi !!! CHECK !!!
                     
Guillaume Samson's avatar
Guillaume Samson committed
            ! 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) )   &
                            &                - zvisc_t(ji,jj) * zdelta(ji,jj)
Guillaume Samson's avatar
Guillaume Samson committed
                                            
            ! 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) 
         END_2D
         
         DO_2D( nn_hls-1, nn_hls, nn_hls, nn_hls ) ! 2 -> jpj; 2,jpi !!! CHECK !!!
Guillaume Samson's avatar
Guillaume Samson committed
            ! 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) )   & 
                            &                - zvisc_t(ji,jj) * zdelta(ji,jj)
Guillaume Samson's avatar
Guillaume Samson committed

            ! 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_2D
                  
         ! --- Stress contributions at F-points         
         ! MV NOTE: I applied fimask 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

         DO_2D( nn_hls, nn_hls-1, nn_hls, nn_hls-1 ) ! 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) * fimask(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) * fimask(ji,jj)

         END_2D
         
         ! --- 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_2D( nn_hls, nn_hls-1, nn_hls-1, nn_hls-1 ) ! 2->jpj-1; 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) ) )
         END_2D
         
         DO_2D( nn_hls-1, nn_hls-1, nn_hls, nn_hls-1 ) ! 2->jpj-1; 2->jpi-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) ) )
Guillaume Samson's avatar
Guillaume Samson committed

         END_2D
         
         !---------------------------
         ! -- Sum RHS contributions
         !---------------------------
         ! OPT: could use intermediate scalars to reduce memory access
         DO_2D( nn_hls, nn_hls-1, nn_hls-1, nn_hls-1 ) ! 2->jpj-1; 2->jpi-1
Guillaume Samson's avatar
Guillaume Samson committed
            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)
         END_2D
         
         DO_2D( nn_hls-1, nn_hls-1, nn_hls, nn_hls-1 ) ! 2->jpj-1; 2->jpi-1
Guillaume Samson's avatar
Guillaume Samson committed
            zrhsv(ji,jj) = zmV_t(ji,jj) + ztauy_ai(ji,jj) + ztauy_oi_rhsv(ji,jj) + zspgV(ji,jj) + zCorV(ji,jj) + zf_rhsv(ji,jj)
         END_2D
         
         !---------------------------------------------------------------------------------------!
         !
         ! --- 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_2D( nn_hls, nn_hls-1, nn_hls-1, nn_hls-1 )
Guillaume Samson's avatar
Guillaume Samson committed
         
            !-------------------------------------
            ! -- 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)

            zt11U      =   zfac1 * zzt(ji,jj)
            zt12U      =   zfac1 * zzt(ji+1,jj)
         
            zt21U      =   zfac2 * zet(ji,jj)   * e2t(ji,jj)   * e2t(ji,jj)   * e2t(ji,jj)   * e2t(ji,jj)
            zt22U      =   zfac2 * zet(ji+1,jj) * e2t(ji+1,jj) * e2t(ji+1,jj) * e2t(ji+1,jj) * e2t(ji+1,jj)
         
            zt121U     =   zfac3 * zef(ji,jj-1) * e1f(ji,jj-1) * e1f(ji,jj-1) * e1f(ji,jj-1) * e1f(ji,jj-1)
            zt122U     =   zfac3 * zef(ji,jj)   * e1f(ji,jj)   * e1f(ji,jj)   * e1f(ji,jj)   * e1f(ji,jj)
               
            !
            ! Linear system coefficients
            !
            zBU(ji,jj) =   ( zt11U + zt12U ) * e2u(ji,jj)   + ( zt21U + zt22U ) * r1_e2u(ji,jj)   + ( zt121U + zt122U ) * 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)
 
            !-----------------------------------------------------
            ! -- Ocean-ice drag and acceleration LHS contribution
            !-----------------------------------------------------
            zBU(ji,jj) = zBU(ji,jj) + zCwU(ji,jj) + zmassU_t(ji,jj)
         
         END_2D
         
         DO_2D( nn_hls-1, nn_hls-1, nn_hls, nn_hls-1 )
         
            !-------------------------------------
            ! -- Internal forces LHS contribution
            !-------------------------------------
Guillaume Samson's avatar
Guillaume Samson committed
            !
            ! --- V-component
            !
            ! "T" factors (intermediate results)
            !
            zfac       = 0.5_wp * r1_e1e2v(ji,jj)
            zfac1      =         zfac * e1v(ji,jj)
            zfac2      =         zfac * r1_e1v(ji,jj)
            zfac3      = 2._wp * zfac * r1_e2v(ji,jj)

            zt11V      =   zfac1 * zzt(ji,jj)
            zt12V      =   zfac1 * zzt(ji,jj+1)

            zt21V      =   zfac2 * zet(ji,jj)   * e1t(ji,jj)   * e1t(ji,jj)   * e1t(ji,jj)   * e1t(ji,jj)
            zt22V      =   zfac2 * zet(ji,jj+1) * e1t(ji,jj+1) * e1t(ji,jj+1) * e1t(ji,jj+1) * e1t(ji,jj+1)
         
            zt121V     =   zfac3 * zef(ji-1,jj) * e2f(ji-1,jj) * e2f(ji-1,jj) * e2f(ji-1,jj) * e2f(ji-1,jj)
            zt122V     =   zfac3 * zef(ji,jj)   * e2f(ji,jj)   * e2f(ji,jj)   * e2f(ji,jj)   * e2f(ji,jj)

            !
            ! Linear system coefficients
            !
            zBV(ji,jj) =   ( zt11V + zt12V ) * e1v(ji,jj)   + ( zt21V + zt22V ) * 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)
            zEV(ji,jj) =     zt122V * r1_e2v(ji+1,jj)
                  
            !-----------------------------------------------------
            ! -- Ocean-ice drag and acceleration LHS contribution
            !-----------------------------------------------------
            zBV(ji,jj) = zBV(ji,jj) + zCwV(ji,jj) + zmassV_t(ji,jj)
         
         END_2D

         DO_2D( nn_hls-1, nn_hls-1, nn_hls-1, nn_hls-1 )
            ! **U**
            zfac       = 0.5_wp * r1_e1e2u(ji,jj)
            zfac1      =         zfac * e2u(ji,jj)
            zfac2      =         zfac * r1_e2u(ji,jj)
            zt11U      =   zfac1 * zzt(ji,jj)
            zt21U      =   zfac2 * zet(ji,jj)   * e2t(ji,jj)   * e2t(ji,jj)   * e2t(ji,jj)   * e2t(ji,jj)
            !
            zAU(ji,jj) = -   zt11U           * e1u(ji-1,jj) -   zt21U           * r1_e1u(ji-1,jj) !!clem: because of this fuck we need to start at jpi=2

            ! **V**
            zfac       = 0.5_wp * r1_e1e2v(ji,jj)
            zfac1      =         zfac * e1v(ji,jj)
            zfac2      =         zfac * r1_e1v(ji,jj)
            zt11V      =   zfac1 * zzt(ji,jj)
            zt21V      =   zfac2 * zet(ji,jj)   * e1t(ji,jj)   * e1t(ji,jj)   * e1t(ji,jj)   * e1t(ji,jj)
            !
            zAV(ji,jj) = -   zt11V           * e1v(ji,jj-1) -   zt21V           * r1_e1v(ji,jj-1) !!clem: because of this fuck we need to start at jpj=2
         END_2D

!!         CALL lbc_lnk( 'icedyn_rhg_vp', zAU, 'U', -1._wp, zBU, 'U', -1._wp, zCU, 'U', -1._wp, zDU, 'U', -1._wp, zEU, 'U', -1._wp, &
!!            &                           zAV, 'V', -1._wp, zBV, 'V', -1._wp, zCV, 'V', -1._wp, zDV, 'V', -1._wp, zEV, 'V', -1._wp )
Guillaume Samson's avatar
Guillaume Samson committed
         
         !------------------------------------------------------------------------------!
         !
         ! --- Inner loop: solve linear system, check convergence
         !
         !------------------------------------------------------------------------------!
Guillaume Samson's avatar
Guillaume Samson committed
               
         ! 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

            !--- mitgcm computes initial value of residual here...

            i_inn_tot  = i_inn_tot + 1
Guillaume Samson's avatar
Guillaume Samson committed
            ! l_full_nf_update = i_inn_tot == nn_nvp   ! false: disable full North fold update (performances) for iter = 1 to nn_nevp-1

            zu_b(:,:)       = u_ice(:,:) ! velocity at previous inner-iterate
            zv_b(:,:)       = v_ice(:,:)

            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
                  
                  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 = ntsj-(nn_hls-1)
                     ELSE                  ;   jj_min = ntsj-(nn_hls-1)+1
Guillaume Samson's avatar
Guillaume Samson committed
                     ENDIF

                     DO jj = jj_min, jpj - 1, nn_zebra_vp
!!                     DO jj = jj_min, ntej+(nn_hls-1), nn_zebra_vp
                        
Guillaume Samson's avatar
Guillaume Samson committed
                        !------------------------
                        ! Independent term (zFU)
                        !------------------------
                        !!                        DO ji = ntsi-(nn_hls), ntei+(nn_hls-1)
                        DO ji = 1, jpi-1
                           
Guillaume Samson's avatar
Guillaume Samson committed
                           ! note: these are key lines linking information between processors
                           ! u_ice/v_ice need to be lbc_linked

                           ! sub-domain 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)
Guillaume Samson's avatar
Guillaume Samson committed

                           ! 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._wp )
Guillaume Samson's avatar
Guillaume Samson committed
                     !---------------
                     ! Forward sweep
                     !---------------   
                     DO jj = jj_min, jpj - 1, nn_zebra_vp
                        !!                     DO jj = jj_min, ntej+(nn_hls-1), nn_zebra_vp
Guillaume Samson's avatar
Guillaume Samson committed
      
!!$                        zBU_prime(2,jj)     = zBU(2,jj)
!!$                        zFU_prime(2,jj)     = zFU(2,jj)
Guillaume Samson's avatar
Guillaume Samson committed

                        DO ji = 2, jpi-1
                           !!                        DO ji = ntsi-(nn_hls-1), ntei+(nn_hls-1)
Guillaume Samson's avatar
Guillaume Samson committed

                           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
                                                                                                     
                     !-----------------------------
                     ! Backward sweep & relaxation
                     !-----------------------------

                     DO jj = jj_min, jpj - 1, nn_zebra_vp
                        !!DO jj = jj_min, ntej+(nn_hls-1), nn_zebra_vp
Guillaume Samson's avatar
Guillaume Samson committed
                    
                        ! --- 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)

                        !!clem => should be backward but then no repro!!!
                        !!DO ji = jpi - 1 , 2, -1 ! all other rows    !  ---> original backward loop
                        !!DO ji = ntei+(nn_hls-1), ntsi-(nn_hls-1), -1
                        DO ji = 2, jpi - 1 ! all other rows    ! 
Guillaume Samson's avatar
Guillaume Samson committed
                           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 masking (for low-ice/no-ice cases)
                        DO ji = 2, jpi - 1    
                           !!DO ji = ntsi-(nn_hls-1), ntei+(nn_hls-1)
Guillaume Samson's avatar
Guillaume Samson committed
                        
                           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

                  DO jn = 1, nn_zebra_vp ! "zebra" loop
      
                     IF ( jn == 1 ) THEN   ;   ji_min = 2
Guillaume Samson's avatar
Guillaume Samson committed
                     ELSE                  ;   ji_min = 3
                     ENDIF

                     DO ji = ji_min, jpi - 1, nn_zebra_vp 
                     
                        !------------------------
                        ! Independent term (zFV)
                        !------------------------
Guillaume Samson's avatar
Guillaume Samson committed

                           ! subdomain 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)
Guillaume Samson's avatar
Guillaume Samson committed
     
                           ! 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

                     !---------------
                     ! Forward sweep
                     !---------------
                     DO ji = ji_min, jpi - 1, nn_zebra_vp 
                     
!!$                        zBV_prime(ji,2)     = zBV(ji,2)
!!$                        zFV_prime(ji,2)     = zFV(ji,2)
Guillaume Samson's avatar
Guillaume Samson committed

Guillaume Samson's avatar
Guillaume Samson committed

                           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

                     !-----------------------------
                     ! 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
Guillaume Samson's avatar
Guillaume Samson committed

                        ! other rows
                        !!clem => should be backward but then no repro!!!
                        !!DO jj = jpj-1, 2, -1 ! original back loop
                        DO jj = 2, jpj-1
Guillaume Samson's avatar
Guillaume Samson committed
                           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 
                        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( 'icedyn_rhg_vp', u_ice, 'U', -1._wp, v_ice, 'V', -1._wp )

Guillaume Samson's avatar
Guillaume Samson committed
               ! I suspect the communication should go into the zebra loop if we want reproducibility
                              
               !--------------------------------------------------------------------------------------
               ! -- 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_2D( 0, 0, 0, 0 )
Guillaume Samson's avatar
Guillaume Samson committed
                  
                     zuerr(ji,jj) = ABS ( ( u_ice(ji,jj) - zu_b(ji,jj) ) ) * umask(ji,jj,1) 
                  
                  END_2D
         
                  zuerr_max = MAXVAL( zuerr )
                  CALL mpp_max( 'icedyn_rhg_vp', zuerr_max )   ! max over the global domain - damned!
Guillaume Samson's avatar
Guillaume Samson committed

                  ! - Stop if max 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   
Guillaume Samson's avatar
Guillaume Samson committed
                        zverr(ji,jj) = ABS ( ( v_ice(ji,jj) - zv_b(ji,jj) ) ) * vmask(ji,jj,1)
                  
                  END_2D
                           
                  zverr_max = MAXVAL( zverr )
                  CALL mpp_max( 'icedyn_rhg_vp', zverr_max )   ! max over the global domain - damned!
Guillaume Samson's avatar
Guillaume Samson committed