Skip to content
Snippets Groups Projects
icedyn_rhg_vp.F90 85.8 KiB
Newer Older
Guillaume Samson's avatar
Guillaume Samson committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
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-04 ! minimum velocity error, beyond which convergence is assumed

   !! 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) ::   zp_delstar_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) ::   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) ::   zdeltat, zdelstar_t             ! Delta & Delta* at T-points
      REAL(wp), DIMENSION(jpi,jpj) ::   ztens, zshear                   ! Tension, shear
      REAL(wp), DIMENSION(jpi,jpj) ::   zp_delstar_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) ::   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) ::   zmsk00
      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)

      REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   zvel_res                         ! Residual of the linear system at last iteration
      REAL(wp), ALLOCATABLE, DIMENSION(:,:) ::   zvel_diff                        ! Absolute velocity difference @last outer iteration
                        
      
      !!----------------------------------------------------------------------------------------------------------------------

      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
      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
      END_2D
      
      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 (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

      zs1_rhsu(:,:) = 0._wp; zs2_rhsu(:,:) = 0._wp; zs1_rhsv(:,:) = 0._wp; zs2_rhsv(:,:) = 0._wp
      zrhsu  (:,:)  = 0._wp; zrhsv  (:,:)  = 0._wp; zf_rhsu(:,:)  = 0._wp; zf_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

      !------------------------------------------------------------------------------!
      !
      ! --- Time-independent quantities
      !
      !------------------------------------------------------------------------------!
      
      CALL ice_strength ! strength at T points
      
      !---------------------------
      ! -- 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
         ENDIF
         
         CALL lbc_lnk( 'icedyn_rhg_vp', fimask, 'F', 1._wp )
      ENDIF
      
      !----------------------------------------------------------------------------------------------------------
      ! -- 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-1, nn_hls-1, nn_hls-1, nn_hls-1 ) ! 2->jpj-1; 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              

      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

         CALL lbc_lnk( 'icedyn_rhg_vp', zds, 'F', 1. ) ! necessary, zds2 uses jpi/jpj values for zds 

         DO_2D( nn_hls-1, nn_hls, nn_hls-1, nn_hls ) ! 2 -> jpj; 2,jpi !!! CHECK !!!
            ! 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
            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(ji,jj)        = SQRT( zdiv2 + ( zdt2 + zds2 ) * z1_ecc2 )  
               
            ! delta* at T points (following Lemieux and Dupont, GMD 2020)
            zdelstar_t(ji,jj)     = zdeltat(ji,jj) + rn_creepl ! OPT zdelstar_t can be totally removed and put into next line directly. Could change results
              
            ! P/delta* at T-points
            zp_delstar_t(ji,jj)   = strength(ji,jj) / zdelstar_t(ji,jj)
               
            ! Temporary zzt and zet factors at T-points
            zzt(ji,jj)            = zp_delstar_t(ji,jj) * r1_e1e2t(ji,jj)
            zet(ji,jj)            = zzt(ji,jj)     * z1_ecc2 
                          
         END_2D
         
         CALL lbc_lnk( 'icedyn_rhg_vp', zp_delstar_t , 'T', 1. ) ! necessary, used for ji = 1 and jj = 1

         DO_2D( nn_hls, nn_hls-1, nn_hls, nn_hls-1 )! 1-> jpj-1; 1->jpi-1
         
               ! P/delta* at F points
               zp_delstar_f = 0.25_wp * ( zp_delstar_t(ji,jj) + zp_delstar_t(ji+1,jj) + zp_delstar_t(ji,jj+1) + zp_delstar_t(ji+1,jj+1) )
               
               ! Temporary zef factor at F-point
               zef(ji,jj)      = zp_delstar_f * r1_e1e2f(ji,jj) * z1_ecc2 * fimask(ji,jj) * 0.5_wp
               
         END_2D
         
         !---------------------------------------------------
         ! -- Ocean-ice drag and Coriolis RHS contributions
         !---------------------------------------------------

         DO_2D( nn_hls-1, nn_hls-1, nn_hls-1, nn_hls-1 ) ! 2->jpj-1; 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)
            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_2D
         
         !-------------------------------------
         ! -- Internal stress RHS contribution
         !-------------------------------------

         ! --- Stress contributions at T-points
         DO_2D( nn_hls-1, nn_hls, nn_hls-1, nn_hls ) ! 2 -> jpj; 2,jpi !!! CHECK !!!
         
         ! loop to jpi,jpj to avoid making a communication for zs1 & zs2
            
            ! 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) )   &
                            &                - zp_delstar_t(ji,jj) * zdeltat(ji,jj)
                                            
            ! 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) )   & 
                            &                - zp_delstar_t(ji,jj) * zdeltat(ji,jj)

            ! 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-1, 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) ) )
                  
               ! --- 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_2D
         
         !---------------------------
         ! -- Sum RHS contributions
         !---------------------------
         !
         ! OPT: could use intermediate scalars to reduce memory access
         DO_2D( nn_hls-1, nn_hls-1, nn_hls-1, nn_hls-1 ) ! 2->jpj-1; 2->jpi-1
                     
            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_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-1, nn_hls-1, nn_hls-1, nn_hls-1 ) ! 2->jpj-1; 2->jpi-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)

            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
            !
            zAU(ji,jj) = -   zt11U           * e2u(ji-1,jj) -   zt21U           * r1_e2u(ji-1,jj)
            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)
              
            !
            ! --- 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
            !
            zAV(ji,jj) = -   zt11V           * e1v(ji,jj-1) -   zt21V           * r1_e1v(ji,jj-1)
            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
            !-----------------------------------------------------
            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_2D
         
      !------------------------------------------------------------------------------!
      !
      ! --- 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

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

            i_inn_tot             = i_inn_tot + 1
            ! 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

                  zFU(:,:)       = 0._wp ; zFU_prime(:,:) = 0._wp ; zBU_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

                     DO jj = jj_min, jpj - 1, nn_zebra_vp
                     
                        !------------------------
                        ! Independent term (zFU)
                        !------------------------
                        DO ji = 2, jpi - 1    
                           ! 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)

                           ! 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
                     
                     !---------------
                     ! Forward sweep
                     !---------------   
                     DO jj = jj_min, jpj - 1, nn_zebra_vp
      
                        zBU_prime(2,jj)     = zBU(2,jj)
                        zFU_prime(2,jj)     = zFU(2,jj)

                        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
                                                                                                     
                     !-----------------------------
                     ! 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 masking (for low-ice/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

                     CALL lbc_lnk( 'icedyn_rhg_vp', u_ice, 'U', -1. )
                     
                  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; 

                  DO jn = 1, nn_zebra_vp ! "zebra" loop
      
                     IF ( jn == 1 ) THEN   ;   ji_min = 2 
                     ELSE                  ;   ji_min = 3
                     ENDIF

                     DO ji = ji_min, jpi - 1, nn_zebra_vp 
                     
                        !------------------------
                        ! Independent term (zFV)
                        !------------------------
                        DO jj = 2, jpj - 1

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

                        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

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

                     CALL lbc_lnk( 'icedyn_rhg_vp', v_ice, 'V', -1. )
                     
                  END DO ! zebra loop
                                    
               ENDIF !   ll_v_iterate

               ! 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( nn_hls-1, nn_hls-1, nn_hls-1, nn_hls-1 ) ! 2->jpj-1; 2->jpi-1
                  
                     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_evp', zuerr_max )   ! max over the global domain - damned!

                  ! - 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   
                  DO_2D( nn_hls-1, nn_hls-1, nn_hls-1, nn_hls-1 ) ! 2->jpj-1; 2->jpi-1
                  
                        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_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 ) THEN

               CALL rhg_cvg_vp( kt, i_out, i_inn, i_inn_tot, nn_vp_nout, nn_vp_ninn, nn_nvp,        &
                      &         u_ice, v_ice, zu_b, zv_b, zu_c, zv_c,                               &
                      &         zmt, za_iU, za_iV, zuerr_max, zverr_max, zglob_area,                &
                      &         zrhsu, zAU, zBU, zCU, zDU, zEU, zFU,                                &
                      &         zrhsv, zAV, zBV, zCV, zDV, zEV, zFV,                                &
                                zvel_res, zvel_diff )

            ENDIF

         END DO ! i_inn, end of inner loop

      END DO ! End of outer loop (i_out) =============================================================================================

      IF( nn_rhg_chkcvg/=0  ) THEN
          
         IF( iom_use('velo_res') )   CALL iom_put( 'velo_res', zvel_res  )   ! linear system residual  @last inner&outer iteration
         IF( iom_use('velo_ero') )   CALL iom_put( 'velo_ero', zvel_diff )   ! abs velocity difference @last outer iteration
         IF( iom_use('uice_eri') )   CALL iom_put( 'uice_eri', zuerr     )   ! abs velocity difference @last inner iteration
         IF( iom_use('vice_eri') )   CALL iom_put( 'vice_eri', zverr     )   ! abs velocity difference @last inner iteration

         DEALLOCATE( zvel_res , zvel_diff )
        
      ENDIF ! nn_rhg_chkcvg

      !------------------------------------------------------------------------------!
      !
      ! --- Recompute delta, shear and div (inputs for mechanical redistribution) 
      !
      !------------------------------------------------------------------------------!
      !
      ! MV OPT: subroutinize ?
      DO_2D( nn_hls, nn_hls, nn_hls-1, nn_hls-1 ) ! 1->jpj-1; 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) * fimask(ji,jj)

      END_2D      
      
      DO_2D( nn_hls-1, nn_hls-1, nn_hls-1, nn_hls-1 ) ! 2->jpj-1; 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
            
            ztens(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)
            
            ! maximum shear rate at T points (includees tension, output only)
            pshear_i(ji,jj) = SQRT( zdt2 + zds2 ) ! i think this is maximum shear rate and not actual shear --- i'm not totally sure here