| 2 | 
 C $Name$ | 
 C $Name$ | 
| 3 | 
  | 
  | 
| 4 | 
 #include "SEAICE_OPTIONS.h" | 
 #include "SEAICE_OPTIONS.h" | 
| 5 | 
  | 
 #ifdef ALLOW_AUTODIFF | 
| 6 | 
  | 
 # include "AUTODIFF_OPTIONS.h" | 
| 7 | 
  | 
 #endif | 
| 8 | 
  | 
  | 
| 9 | 
 C--  File seaice_jfnk.F: seaice jfnk dynamical solver S/R: | 
 C--  File seaice_jfnk.F: seaice jfnk dynamical solver S/R: | 
| 10 | 
 C--   Contents | 
 C--   Contents | 
| 69 | 
       INTEGER newtonIter | 
       INTEGER newtonIter | 
| 70 | 
       INTEGER krylovIter, krylovFails | 
       INTEGER krylovIter, krylovFails | 
| 71 | 
       INTEGER totalKrylovItersLoc, totalNewtonItersLoc | 
       INTEGER totalKrylovItersLoc, totalNewtonItersLoc | 
| 72 | 
  | 
 C     FGMRES parameters | 
| 73 | 
  | 
 C     im      :: size of Krylov space | 
| 74 | 
  | 
 C     ifgmres :: interation counter | 
| 75 | 
  | 
       INTEGER im | 
| 76 | 
  | 
       PARAMETER ( im = 50 ) | 
| 77 | 
  | 
       INTEGER ifgmres | 
| 78 | 
 C     FGMRES flag that determines amount of output messages of fgmres | 
 C     FGMRES flag that determines amount of output messages of fgmres | 
| 79 | 
       INTEGER iOutFGMRES | 
       INTEGER iOutFGMRES | 
| 80 | 
 C     FGMRES flag that indicates what fgmres wants us to do next | 
 C     FGMRES flag that indicates what fgmres wants us to do next | 
| 105 | 
 C     precomputed (= constant per Newton iteration) versions of | 
 C     precomputed (= constant per Newton iteration) versions of | 
| 106 | 
 C     zeta, eta, and DWATN, press | 
 C     zeta, eta, and DWATN, press | 
| 107 | 
       _RL zetaPre (1-OLx:sNx+OLx,1-OLy:sNy+OLy,nSx,nSy) | 
       _RL zetaPre (1-OLx:sNx+OLx,1-OLy:sNy+OLy,nSx,nSy) | 
| 108 | 
  | 
       _RL zetaZPre(1-OLx:sNx+OLx,1-OLy:sNy+OLy,nSx,nSy) | 
| 109 | 
       _RL etaPre  (1-OLx:sNx+OLx,1-OLy:sNy+OLy,nSx,nSy) | 
       _RL etaPre  (1-OLx:sNx+OLx,1-OLy:sNy+OLy,nSx,nSy) | 
| 110 | 
       _RL etaZPre (1-OLx:sNx+OLx,1-OLy:sNy+OLy,nSx,nSy) | 
       _RL etaZPre (1-OLx:sNx+OLx,1-OLy:sNy+OLy,nSx,nSy) | 
| 111 | 
       _RL dwatPre (1-OLx:sNx+OLx,1-OLy:sNy+OLy,nSx,nSy) | 
       _RL dwatPre (1-OLx:sNx+OLx,1-OLy:sNy+OLy,nSx,nSy) | 
| 112 | 
  | 
 C     work arrays | 
| 113 | 
  | 
       _RL rhs(nVec,nSx,nSy), sol(nVec,nSx,nSy) | 
| 114 | 
  | 
       _RL vv(nVec,im+1,nSx,nSy), w(nVec,im,nSx,nSy) | 
| 115 | 
  | 
       _RL wk1(nVec,nSx,nSy), wk2(nVec,nSx,nSy) | 
| 116 | 
 CEOP | 
 CEOP | 
| 117 | 
  | 
  | 
| 118 | 
 C     Initialise | 
 C     Initialise | 
| 164 | 
           vIceNm1(I,J,bi,bj) = vIce(I,J,bi,bj) | 
           vIceNm1(I,J,bi,bj) = vIce(I,J,bi,bj) | 
| 165 | 
          ENDDO | 
          ENDDO | 
| 166 | 
         ENDDO | 
         ENDDO | 
| 167 | 
         IF ( .NOT.SEAICEuseIMEX ) THEN | 
 C     As long as IMEX is not properly implemented leave this commented out | 
| 168 | 
  | 
 CML        IF ( .NOT.SEAICEuseIMEX ) THEN | 
| 169 | 
 C     Compute things that do no change during the Newton iteration: | 
 C     Compute things that do no change during the Newton iteration: | 
| 170 | 
 C     sea-surface tilt and wind stress: | 
 C     sea-surface tilt and wind stress: | 
| 171 | 
 C     FORCEX/Y0 - mass*(1.5*u/vIceNm1+0.5*(u/vIceNm1-u/vIceNm2))/deltaT | 
 C     FORCEX/Y0 - mass*(1.5*u/vIceNm1+0.5*(u/vIceNm1-u/vIceNm2))/deltaT | 
| 177 | 
      &         + seaiceMassV(I,J,bi,bj)*dvIcNm1(I,J,bi,bj)*recip_deltaT | 
      &         + seaiceMassV(I,J,bi,bj)*dvIcNm1(I,J,bi,bj)*recip_deltaT | 
| 178 | 
          ENDDO | 
          ENDDO | 
| 179 | 
         ENDDO | 
         ENDDO | 
| 180 | 
         ENDIF | 
 CML        ENDIF | 
| 181 | 
        ENDDO | 
        ENDDO | 
| 182 | 
       ENDDO | 
       ENDDO | 
| 183 | 
 C     Start nonlinear Newton iteration: outer loop iteration | 
 C     Start nonlinear Newton iteration: outer loop iteration | 
| 184 | 
       DO WHILE ( newtonIter.LT.SEAICEnewtonIterMax .AND. | 
       DO WHILE ( newtonIter.LT.SEAICEnonLinIterMax .AND. | 
| 185 | 
      &     .NOT.JFNKconverged ) | 
      &     .NOT.JFNKconverged ) | 
| 186 | 
        newtonIter = newtonIter + 1 | 
        newtonIter = newtonIter + 1 | 
| 187 | 
 C     Compute initial residual F(u), (includes computation of global | 
 C     Compute initial residual F(u), (includes computation of global | 
| 198 | 
          DO j=1-OLy,sNy+OLy | 
          DO j=1-OLy,sNy+OLy | 
| 199 | 
           DO i=1-OLx,sNx+OLx | 
           DO i=1-OLx,sNx+OLx | 
| 200 | 
            zetaPre(I,J,bi,bj) =  zeta(I,J,bi,bj) | 
            zetaPre(I,J,bi,bj) =  zeta(I,J,bi,bj) | 
| 201 | 
  | 
            zetaZPre(I,J,bi,bj)= zetaZ(I,J,bi,bj) | 
| 202 | 
             etaPre(I,J,bi,bj) =   eta(I,J,bi,bj) | 
             etaPre(I,J,bi,bj) =   eta(I,J,bi,bj) | 
| 203 | 
            etaZPre(I,J,bi,bj) =  etaZ(I,J,bi,bj) | 
            etaZPre(I,J,bi,bj) =  etaZ(I,J,bi,bj) | 
| 204 | 
            dwatPre(I,J,bi,bj) = DWATN(I,J,bi,bj) | 
            dwatPre(I,J,bi,bj) = DWATN(I,J,bi,bj) | 
| 237 | 
  | 
  | 
| 238 | 
         krylovConverged = .FALSE. | 
         krylovConverged = .FALSE. | 
| 239 | 
         FGMRESeps = JFNKgamma_lin * JFNKresidual | 
         FGMRESeps = JFNKgamma_lin * JFNKresidual | 
| 240 | 
  | 
 C     map first guess sol; it is zero because the solution is a correction | 
| 241 | 
  | 
        CALL SEAICE_MAP2VEC(nVec,duIce,dvIce,sol,.TRUE.,myThid) | 
| 242 | 
  | 
 C     map rhs and change its sign because we are solving J*u = -F | 
| 243 | 
  | 
         CALL SEAICE_MAP2VEC(nVec,uIceRes,vIceRes,rhs,.TRUE.,myThid) | 
| 244 | 
  | 
         DO bj=myByLo(myThid),myByHi(myThid) | 
| 245 | 
  | 
          DO bi=myBxLo(myThid),myBxHi(myThid) | 
| 246 | 
  | 
           DO j=1,nVec | 
| 247 | 
  | 
            rhs(j,bi,bj) = - rhs(j,bi,bj) | 
| 248 | 
  | 
           ENDDO | 
| 249 | 
  | 
          ENDDO | 
| 250 | 
  | 
         ENDDO | 
| 251 | 
         DO WHILE ( .NOT.krylovConverged ) | 
         DO WHILE ( .NOT.krylovConverged ) | 
| 252 | 
 C     solution vector sol = du/vIce | 
 C     solution vector sol = du/vIce | 
| 253 | 
 C     residual vector (rhs) Fu = u/vIceRes | 
 C     residual vector (rhs) Fu = u/vIceRes | 
| 254 | 
 C     output work vectors wk1, -> input work vector wk2 | 
 C     output work vectors wk1, -> input work vector wk2 | 
| 255 | 
  | 
  | 
| 256 | 
          CALL SEAICE_FGMRES_DRIVER( | 
 C     map preconditioner results or Jacobian times vector, | 
| 257 | 
      I        uIceRes, vIceRes, | 
 C     stored in du/vIce to wk2, for iCode=0, wk2 is set to zero, | 
| 258 | 
      U        duIce, dvIce, iCode, | 
 C     because du/vIce = 0 | 
| 259 | 
      I        FGMRESeps, iOutFGMRES, | 
          CALL SEAICE_MAP2VEC(nVec,duIce,dvIce,wk2,.TRUE.,myThid) | 
| 260 | 
      I        newtonIter, krylovIter, myTime, myIter, myThid ) | 
 C | 
| 261 | 
  | 
          CALL SEAICE_FGMRES (nVec,im,rhs,sol,ifgmres,krylovIter, | 
| 262 | 
  | 
      U        vv,w,wk1,wk2, | 
| 263 | 
  | 
      I        FGMRESeps,SEAICElinearIterMax,iOutFGMRES, | 
| 264 | 
  | 
      U        iCode, | 
| 265 | 
  | 
      I        myThid) | 
| 266 | 
  | 
 C      | 
| 267 | 
  | 
          IF ( iCode .EQ. 0 ) THEN | 
| 268 | 
  | 
 C     map sol(ution) vector to du/vIce | 
| 269 | 
  | 
           CALL SEAICE_MAP2VEC(nVec,duIce,dvIce,sol,.FALSE.,myThid) | 
| 270 | 
  | 
          ELSE | 
| 271 | 
  | 
 C     map work vector to du/vIce to either compute a preconditioner | 
| 272 | 
  | 
 C     solution (wk1=rhs) or a Jacobian times wk1 | 
| 273 | 
  | 
           CALL SEAICE_MAP2VEC(nVec,duIce,dvIce,wk1,.FALSE.,myThid) | 
| 274 | 
  | 
          ENDIF | 
| 275 | 
  | 
 C     Fill overlaps in updated fields | 
| 276 | 
  | 
          CALL EXCH_UV_XY_RL( duIce, dvIce,.TRUE.,myThid) | 
| 277 | 
 C     FGMRES returns iCode either asking for an new preconditioned vector | 
 C     FGMRES returns iCode either asking for an new preconditioned vector | 
| 278 | 
 C     or product of matrix (Jacobian) times vector. For iCode = 0, terminate | 
 C     or product of matrix (Jacobian) times vector. For iCode = 0, terminate | 
| 279 | 
 C     iteration | 
 C     iteration | 
| 280 | 
          IF (iCode.EQ.1) THEN | 
          IF (iCode.EQ.1) THEN | 
| 281 | 
 C     Call preconditioner | 
 C     Call preconditioner | 
| 282 | 
           IF ( SOLV_MAX_ITERS .GT. 0 ) | 
           IF ( SEAICEpreconLinIter .GT. 0 ) | 
| 283 | 
      &         CALL SEAICE_PRECONDITIONER( | 
      &         CALL SEAICE_PRECONDITIONER( | 
| 284 | 
      U         duIce, dvIce, | 
      U         duIce, dvIce, | 
| 285 | 
      I         zetaPre, etaPre, etaZpre, dwatPre, | 
      I         zetaPre, etaPre, etaZpre, zetaZpre, dwatPre, | 
| 286 | 
      I         newtonIter, krylovIter, myTime, myIter, myThid ) | 
      I         newtonIter, krylovIter, myTime, myIter, myThid ) | 
| 287 | 
          ELSEIF (iCode.GE.2) THEN | 
          ELSEIF (iCode.GE.2) THEN | 
| 288 | 
 C     Compute Jacobian times vector | 
 C     Compute Jacobian times vector | 
| 299 | 
         IF ( debugLevel.GE.debLevA ) THEN | 
         IF ( debugLevel.GE.debLevA ) THEN | 
| 300 | 
          _BEGIN_MASTER( myThid ) | 
          _BEGIN_MASTER( myThid ) | 
| 301 | 
          totalNewtonItersLoc = | 
          totalNewtonItersLoc = | 
| 302 | 
      &        SEAICEnewtonIterMax*(myIter-nIter0)+newtonIter | 
      &        SEAICEnonLinIterMax*(myIter-nIter0)+newtonIter | 
| 303 | 
          WRITE(msgBuf,'(2A,2(1XI6),2E12.5)') | 
          WRITE(msgBuf,'(2A,2(1XI6),2E12.5)') | 
| 304 | 
      &        ' S/R SEAICE_JFNK: Newton iterate / total, ', | 
      &        ' S/R SEAICE_JFNK: Newton iterate / total, ', | 
| 305 | 
      &        'JFNKgamma_lin, initial norm = ', | 
      &        'JFNKgamma_lin, initial norm = ', | 
| 315 | 
      &        SQUEEZE_RIGHT, myThid ) | 
      &        SQUEEZE_RIGHT, myThid ) | 
| 316 | 
          _END_MASTER( myThid ) | 
          _END_MASTER( myThid ) | 
| 317 | 
         ENDIF | 
         ENDIF | 
| 318 | 
         IF ( krylovIter.EQ.SEAICEkrylovIterMax ) THEN | 
         IF ( krylovIter.EQ.SEAICElinearIterMax ) THEN | 
| 319 | 
          krylovFails = krylovFails + 1 | 
          krylovFails = krylovFails + 1 | 
| 320 | 
         ENDIF | 
         ENDIF | 
| 321 | 
 C     Set the stopping criterion for the Newton iteration and the | 
 C     Set the stopping criterion for the Newton iteration and the | 
| 322 | 
 C     criterion for the transition from accurate to approximate FGMRES | 
 C     criterion for the transition from accurate to approximate FGMRES | 
| 323 | 
         IF ( newtonIter .EQ. 1 ) THEN | 
         IF ( newtonIter .EQ. 1 ) THEN | 
| 324 | 
          JFNKtol=JFNKgamma_nonlin*JFNKresidual | 
          JFNKtol=SEAICEnonLinTol*JFNKresidual | 
| 325 | 
          IF ( JFNKres_tFac .NE. UNSET_RL ) | 
          IF ( JFNKres_tFac .NE. UNSET_RL ) | 
| 326 | 
      &        JFNKres_t = JFNKresidual * JFNKres_tFac | 
      &        JFNKres_t = JFNKresidual * JFNKres_tFac | 
| 327 | 
         ENDIF | 
         ENDIF | 
| 360 | 
        totalKrylovIters   = totalKrylovIters + totalKrylovItersLoc | 
        totalKrylovIters   = totalKrylovIters + totalKrylovItersLoc | 
| 361 | 
 C     Record failure | 
 C     Record failure | 
| 362 | 
        totalKrylovFails   = totalKrylovFails + krylovFails | 
        totalKrylovFails   = totalKrylovFails + krylovFails | 
| 363 | 
        IF ( newtonIter .EQ. SEAICEnewtonIterMax ) THEN | 
        IF ( newtonIter .EQ. SEAICEnonLinIterMax ) THEN | 
| 364 | 
         totalNewtonFails = totalNewtonFails + 1 | 
         totalNewtonFails = totalNewtonFails + 1 | 
| 365 | 
        ENDIF | 
        ENDIF | 
| 366 | 
       ENDIF | 
       ENDIF | 
| 434 | 
  | 
  | 
| 435 | 
 C     Print more debugging information | 
 C     Print more debugging information | 
| 436 | 
       IF ( debugLevel.GE.debLevA ) THEN | 
       IF ( debugLevel.GE.debLevA ) THEN | 
| 437 | 
        IF ( newtonIter .EQ. SEAICEnewtonIterMax ) THEN | 
        IF ( newtonIter .EQ. SEAICEnonLinIterMax ) THEN | 
| 438 | 
         _BEGIN_MASTER( myThid ) | 
         _BEGIN_MASTER( myThid ) | 
| 439 | 
         WRITE(msgBuf,'(A,I10)') | 
         WRITE(msgBuf,'(A,I10)') | 
| 440 | 
      &       ' S/R SEAICE_JFNK: JFNK did not converge in timestep ', | 
      &       ' S/R SEAICE_JFNK: JFNK did not converge in timestep ', |