New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
solpcg.F90 in branches/dev_r2586_dynamic_mem/NEMOGCM/NEMO/OPA_SRC/SOL – NEMO

source: branches/dev_r2586_dynamic_mem/NEMOGCM/NEMO/OPA_SRC/SOL/solpcg.F90 @ 2636

Last change on this file since 2636 was 2636, checked in by gm, 13 years ago

dynamic mem: #785 ; move ctl_stop & warn in lib_mpp to avoid a circular dependency + ctl_stop improvment

  • Property svn:keywords set to Id
File size: 8.8 KB
Line 
1MODULE solpcg
2   !!======================================================================
3   !!                     ***  MODULE  solfet
4   !! Ocean solver :  preconditionned conjugate gradient solver
5   !!=====================================================================
6
7   !!----------------------------------------------------------------------
8   !!   sol_pcg    : preconditionned conjugate gradient solver
9   !!----------------------------------------------------------------------
10   USE oce             ! ocean dynamics and tracers variables
11   USE dom_oce         ! ocean space and time domain variables
12   USE sol_oce         ! ocean solver variables
13   USE lib_mpp         ! distributed memory computing
14   USE lbclnk          ! ocean lateral boundary conditions (or mpp link)
15   USE in_out_manager  ! I/O manager
16   USE lib_fortran
17
18   IMPLICIT NONE
19   PRIVATE
20
21   PUBLIC   sol_pcg    !
22
23   !! * Substitutions
24#  include "vectopt_loop_substitute.h90"
25   !!----------------------------------------------------------------------
26   !! NEMO/OPA 3.3 , NEMO Consortium (2010)
27   !! $Id$
28   !! Software governed by the CeCILL licence     (NEMOGCM/NEMO_CeCILL.txt)
29   !!----------------------------------------------------------------------
30CONTAINS
31
32   SUBROUTINE sol_pcg( kindic )
33      !!----------------------------------------------------------------------
34      !!                  ***  ROUTINE sol_pcg  ***
35      !!                   
36      !! ** Purpose :   Solve the ellipic equation for the transport
37      !!      divergence system  using a diagonal preconditionned
38      !!      conjugate gradient method.
39      !!
40      !! ** Method  :   Diagonal preconditionned conjugate gradient method.
41      !!      the algorithm is multitasked. (case of 5 points matrix)
42      !!      define              pa  = q^-1 * a
43      !!                        pgcb  = q^-1 * gcb
44      !!                 < . ; . >_q  = ( . )^t q ( . )
45      !!      where q is the preconditioning matrix = diagonal matrix of the
46      !!                                              diagonal elements of a
47      !!      Initialization  :
48      !!         x(o) = gcx
49      !!         r(o) = d(o) = pgcb - pa.x(o)
50      !!         rr(o)= < r(o) , r(o) >_q
51      !!      Iteration 1     :
52      !!         standard PCG algorithm
53      !!      Iteration n > 1 :
54      !!         s(n)   = pa.r(n)
55      !!         gam(n) = < r(n) , r(n) >_q
56      !!         del(n) = < r(n) , s(n) >_q
57      !!         bet(n) = gam(n) / gam(n-1)
58      !!         d(n)   = r(n) + bet(n) d(n-1)
59      !!         z(n)   = s(n) + bet(n) z(n-1)
60      !!         sig(n) = del(n) - bet(n)*bet(n)*sig(n-1)
61      !!         alp(n) = gam(n) / sig(n)
62      !!         x(n+1) = x(n) + alp(n) d(n)
63      !!         r(n+1) = r(n) - alp(n) z(n)
64      !!      Convergence test :
65      !!         rr(n+1) / < gcb , gcb >_q   =< epsr
66      !!
67      !! ** Action : - niter  : solver number of iteration done
68      !!             - res    : solver residu reached
69      !!             - gcx()  : solution of the elliptic system
70      !!
71      !! References :
72      !!      Madec et al. 1988, Ocean Modelling, issue 78, 1-6.
73      !!      D Azevedo et al. 1993, Computer Science Technical Report, Tennessee U.
74      !!
75      !! History :
76      !!        !  90-10  (G. Madec)  Original code
77      !!        !  91-11  (G. Madec)
78      !!        !  93-04  (M. Guyon)  loops and suppress pointers
79      !!        !  95-09  (M. Imbard, J. Escobar)  mpp exchange
80      !!        !  96-05  (G. Madec)  merge sor and pcg formulations
81      !!        !  96-11  (A. Weaver)  correction to preconditioning
82      !!   8.5  !  02-08  (G. Madec)  F90: Free form
83      !!        !  08-01  (R. Benshila) mpp optimization
84      !!----------------------------------------------------------------------
85      USE wrk_nemo, ONLY:   wrk_in_use, wrk_not_released
86      USE wrk_nemo, ONLY:   zgcr => wrk_2d_1
87      !!
88      INTEGER, INTENT(inout) ::   kindic   ! solver indicator, < 0 if the conver-
89      !                                    ! gence is not reached: the model is stopped in step
90      !                                    ! set to zero before the call of solpcg
91      !!
92      INTEGER  ::   ji, jj, jn   ! dummy loop indices
93      REAL(wp) ::   zgcad        ! temporary scalars
94      REAL(wp), DIMENSION(2) ::   zsum
95      !!----------------------------------------------------------------------
96     
97      IF( wrk_in_use(2, 1) )THEN
98         CALL ctl_stop('sol_pcg: requested workspace array is unavailable')   ;   RETURN
99      ENDIF
100
101      ! Initialization of the algorithm with standard PCG
102      ! -------------------------------------------------
103      zgcr = 0._wp
104      gcr  = 0._wp
105
106      CALL lbc_lnk( gcx, c_solver_pt, 1. )   ! lateral boundary condition
107
108      ! gcr   = gcb-a.gcx
109      ! gcdes = gcr
110      DO jj = 2, jpjm1
111         DO ji = fs_2, fs_jpim1   ! vector opt.
112            zgcad = bmask(ji,jj) * ( gcb(ji,jj  ) -                gcx(ji  ,jj  )   &
113               &                                  - gcp(ji,jj,1) * gcx(ji  ,jj-1)   &
114               &                                  - gcp(ji,jj,2) * gcx(ji-1,jj  )   &
115               &                                  - gcp(ji,jj,3) * gcx(ji+1,jj  )   &
116               &                                  - gcp(ji,jj,4) * gcx(ji  ,jj+1)   )
117            gcr  (ji,jj) = zgcad
118            gcdes(ji,jj) = zgcad
119         END DO
120      END DO
121
122      ! rnorme = (gcr,gcr)
123      rnorme = glob_sum(  gcr(:,:) * gcdmat(:,:) * gcr(:,:)  )
124
125      CALL lbc_lnk( gcdes, c_solver_pt, 1. )   ! lateral boundary condition
126
127      ! gccd = matrix . gcdes
128      DO jj = 2, jpjm1
129         DO ji = fs_2, fs_jpim1   ! vector opt.
130            gccd(ji,jj) = bmask(ji,jj)*( gcdes(ji,jj)   &
131               &        +gcp(ji,jj,1)*gcdes(ji,jj-1)+gcp(ji,jj,2)*gcdes(ji-1,jj)   &
132               &        +gcp(ji,jj,4)*gcdes(ji,jj+1)+gcp(ji,jj,3)*gcdes(ji+1,jj)   )
133         END DO
134      END DO 
135
136      ! alph = (gcr,gcr)/(gcdes,gccd)
137      radd = glob_sum(  gcdes(:,:) * gcdmat(:,:) * gccd(:,:)  )
138      alph = rnorme /radd
139
140      ! gcx = gcx + alph * gcdes
141      ! gcr = gcr - alph * gccd
142      DO jj = 2, jpjm1
143         DO ji = fs_2, fs_jpim1   ! vector opt.
144            gcx(ji,jj) = bmask(ji,jj) * ( gcx(ji,jj) + alph * gcdes(ji,jj) )
145            gcr(ji,jj) = bmask(ji,jj) * ( gcr(ji,jj) - alph * gccd (ji,jj) )
146         END DO
147      END DO
148
149      ! Algorithm wtih Eijkhout rearrangement
150      ! -------------------------------------
151       
152      !                                                !================
153      DO jn = 1, nn_nmax                               ! Iterative loop
154         !                                             !================
155
156         CALL lbc_lnk( gcr, c_solver_pt, 1. )   ! lateral boundary condition
157
158         ! zgcr = matrix . gcr
159         DO jj = 2, jpjm1
160            DO ji = fs_2, fs_jpim1   ! vector opt.
161               zgcr(ji,jj) = bmask(ji,jj)*( gcr(ji,jj)   &
162                  &        +gcp(ji,jj,1)*gcr(ji,jj-1)+gcp(ji,jj,2)*gcr(ji-1,jj)   &
163                  &        +gcp(ji,jj,4)*gcr(ji,jj+1)+gcp(ji,jj,3)*gcr(ji+1,jj)   )
164            END DO
165         END DO
166 
167         ! rnorme = (gcr,gcr)
168         rr = rnorme
169
170         ! zgcad = (zgcr,gcr)
171         zsum(1) = glob_sum(gcr(:,:) * gcdmat(:,:) * gcr(:,:))
172         zsum(2) = glob_sum(gcr(:,:) * gcdmat(:,:) * zgcr(:,:) * bmask(:,:))
173
174         !!RB we should gather the 2 glob_sum
175         rnorme = zsum(1) 
176         zgcad  = zsum(2)
177         ! test of convergence
178         IF( rnorme < epsr .OR. jn == nn_nmax ) THEN
179            res = SQRT( rnorme )
180            niter = jn
181            ncut = 999
182         ENDIF
183
184         ! beta = (rk+1,rk+1)/(rk,rk)
185         beta = rnorme / rr
186         radd = zgcad - beta*beta*radd
187         alph = rnorme / radd
188
189         ! gcx = gcx + alph * gcdes
190         ! gcr = gcr - alph * gccd
191         DO jj = 2, jpjm1
192            DO ji = fs_2, fs_jpim1   ! vector opt.
193               gcdes(ji,jj) = gcr (ji,jj) + beta * gcdes(ji,jj) 
194               gccd (ji,jj) = zgcr(ji,jj) + beta * gccd (ji,jj) 
195               gcx  (ji,jj) = gcx (ji,jj) + alph * gcdes(ji,jj) 
196               gcr  (ji,jj) = gcr (ji,jj) - alph * gccd (ji,jj) 
197            END DO
198         END DO
199       
200         ! indicator of non-convergence or explosion
201         IF( jn == nn_nmax .OR. SQRT(epsr)/eps > 1.e+20 ) kindic = -2
202         IF( ncut == 999 ) GOTO 999
203
204         !                                             !================
205      END DO                                           !    End Loop
206      !                                                !================
207999   CONTINUE
208         
209      CALL lbc_lnk( gcx, c_solver_pt, 1. )      ! Output in gcx with lateral b.c. applied
210      !
211      IF( wrk_not_released(2, 1) )   CALL ctl_stop('sol_pcg: failed to release workspace array')
212      !
213   END SUBROUTINE sol_pcg
214
215   !!=====================================================================
216END MODULE solpcg
Note: See TracBrowser for help on using the repository browser.