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 @ 2633

Last change on this file since 2633 was 2633, checked in by trackstand2, 13 years ago

Renamed wrk_use => wrk_in_use and wrk_release => wrk_not_released

  • Property svn:keywords set to Id
File size: 9.0 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
90      !                                      ! stopped in step
91      !                                      ! set to zero before the call of solpcg
92      !!
93      INTEGER ::   ji, jj, jn                ! dummy loop indices
94      REAL(wp) ::  zgcad                     ! temporary scalars
95      REAL(wp), DIMENSION(2) :: zsum
96      !!----------------------------------------------------------------------
97     
98      IF( wrk_in_use(2, 1) )THEN
99         CALL ctl_stop('sol_pcg: requested workspace array is unavailable')
100         RETURN
101      END IF
102
103      ! Initialization of the algorithm with standard PCG
104      ! -------------------------------------------------
105      zgcr = 0.e0 
106      gcr  = 0.e0 
107
108      CALL lbc_lnk( gcx, c_solver_pt, 1. )   ! lateral boundary condition
109
110      ! gcr   = gcb-a.gcx
111      ! gcdes = gcr
112      DO jj = 2, jpjm1
113         DO ji = fs_2, fs_jpim1   ! vector opt.
114            zgcad = bmask(ji,jj) * ( gcb(ji,jj  ) -                gcx(ji  ,jj  )   &
115               &                                  - gcp(ji,jj,1) * gcx(ji  ,jj-1)   &
116               &                                  - gcp(ji,jj,2) * gcx(ji-1,jj  )   &
117               &                                  - gcp(ji,jj,3) * gcx(ji+1,jj  )   &
118               &                                  - gcp(ji,jj,4) * gcx(ji  ,jj+1)   )
119            gcr  (ji,jj) = zgcad
120            gcdes(ji,jj) = zgcad
121         END DO
122      END DO
123
124      ! rnorme = (gcr,gcr)
125      rnorme = glob_sum(  gcr(:,:) * gcdmat(:,:) * gcr(:,:)  )
126
127      CALL lbc_lnk( gcdes, c_solver_pt, 1. )   ! lateral boundary condition
128
129      ! gccd = matrix . gcdes
130      DO jj = 2, jpjm1
131         DO ji = fs_2, fs_jpim1   ! vector opt.
132            gccd(ji,jj) = bmask(ji,jj)*( gcdes(ji,jj)   &
133               &        +gcp(ji,jj,1)*gcdes(ji,jj-1)+gcp(ji,jj,2)*gcdes(ji-1,jj)   &
134               &        +gcp(ji,jj,4)*gcdes(ji,jj+1)+gcp(ji,jj,3)*gcdes(ji+1,jj)   )
135         END DO
136      END DO 
137
138      ! alph = (gcr,gcr)/(gcdes,gccd)
139      radd = glob_sum(  gcdes(:,:) * gcdmat(:,:) * gccd(:,:)  )
140      alph = rnorme /radd
141
142      ! gcx = gcx + alph * gcdes
143      ! gcr = gcr - alph * gccd
144      DO jj = 2, jpjm1
145         DO ji = fs_2, fs_jpim1   ! vector opt.
146            gcx(ji,jj) = bmask(ji,jj) * ( gcx(ji,jj) + alph * gcdes(ji,jj) )
147            gcr(ji,jj) = bmask(ji,jj) * ( gcr(ji,jj) - alph * gccd (ji,jj) )
148         END DO
149      END DO
150
151      ! Algorithm wtih Eijkhout rearrangement
152      ! -------------------------------------
153       
154      !                                                !================
155      DO jn = 1, nn_nmax                               ! Iterative loop
156         !                                             !================
157
158         CALL lbc_lnk( gcr, c_solver_pt, 1. )   ! lateral boundary condition
159
160         ! zgcr = matrix . gcr
161         DO jj = 2, jpjm1
162            DO ji = fs_2, fs_jpim1   ! vector opt.
163               zgcr(ji,jj) = bmask(ji,jj)*( gcr(ji,jj)   &
164                  &        +gcp(ji,jj,1)*gcr(ji,jj-1)+gcp(ji,jj,2)*gcr(ji-1,jj)   &
165                  &        +gcp(ji,jj,4)*gcr(ji,jj+1)+gcp(ji,jj,3)*gcr(ji+1,jj)   )
166            END DO
167         END DO
168 
169         ! rnorme = (gcr,gcr)
170         rr = rnorme
171
172         ! zgcad = (zgcr,gcr)
173         zsum(1) = glob_sum(gcr(:,:) * gcdmat(:,:) * gcr(:,:))
174         zsum(2) = glob_sum(gcr(:,:) * gcdmat(:,:) * zgcr(:,:) * bmask(:,:))
175
176         !!RB we should gather the 2 glob_sum
177         rnorme = zsum(1) 
178         zgcad  = zsum(2)
179         ! test of convergence
180         IF( rnorme < epsr .OR. jn == nn_nmax ) THEN
181            res = SQRT( rnorme )
182            niter = jn
183            ncut = 999
184         ENDIF
185
186         ! beta = (rk+1,rk+1)/(rk,rk)
187         beta = rnorme / rr
188         radd = zgcad - beta*beta*radd
189         alph = rnorme / radd
190
191         ! gcx = gcx + alph * gcdes
192         ! gcr = gcr - alph * gccd
193         DO jj = 2, jpjm1
194            DO ji = fs_2, fs_jpim1   ! vector opt.
195               gcdes(ji,jj) = gcr (ji,jj) + beta * gcdes(ji,jj) 
196               gccd (ji,jj) = zgcr(ji,jj) + beta * gccd (ji,jj) 
197               gcx  (ji,jj) = gcx (ji,jj) + alph * gcdes(ji,jj) 
198               gcr  (ji,jj) = gcr (ji,jj) - alph * gccd (ji,jj) 
199            END DO
200         END DO
201       
202         ! indicator of non-convergence or explosion
203         IF( jn == nn_nmax .OR. SQRT(epsr)/eps > 1.e+20 ) kindic = -2
204         IF( ncut == 999 ) GOTO 999
205
206         !                                             !================
207      END DO                                           !    End Loop
208      !                                                !================
209     
210999   CONTINUE
211     
212     
213      ! Output in gcx with lateral b.c. applied
214      ! ---------------------------------------
215     
216      CALL lbc_lnk( gcx, c_solver_pt, 1. )
217     
218      !
219      IF( wrk_not_released(2, 1) )THEN
220         CALL ctl_stop('sol_pcg: failed to release workspace array')
221      END IF
222      !
223   END SUBROUTINE sol_pcg
224
225   !!=====================================================================
226END MODULE solpcg
Note: See TracBrowser for help on using the repository browser.