summaryrefslogtreecommitdiff
path: root/Dragon/src/MCGMRE.f
blob: 5b5e6b063934f5f883de5afcfe3696b57752d93e (plain)
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
*DECK MCGMRE
      SUBROUTINE MCGMRE(SUBFFI,SUBFFA,SUBLDC,SUBSCH,CYCLIC,KPSYS,
     1           IPRINT,IPTRK,IFTRAK,IPMACR,NDIM,KV,NUN,NLONG,PHIOUT,
     2           NZON,MATALB,M,NANI,NMU,NMAX,NANGL,NREG,NSOUT,SOUR,IAAC,
     3           ISCR,LC,LFORW,PACA,ITST,MAXI,QFR,PHIIN,CAZ0,CAZ1,CAZ2,
     4           CPO,ZMU,WZMU,VOL,EPS,ERRTOL,REPSI,NSTART,SIGAL,LPS,
     5           NGROUP,NGEFF,NGIND,NCONV,LNCONV,NLIN,NFUNL,KEYFLX,
     6           KEYCUR,STIS,NPJJM,REBFLG,LPRISM,N2REG,N2SOU,NZP,DELU,
     7           FACSYM,IDIR,NBATCH)
*
*-----------------------------------------------------------------------
*
*Purpose:
* Solve the linear system obtained by the characteristics formalism
* with GMRES iterative approach.
*
*Copyright:
* Copyright (C) 2015 Ecole Polytechnique de Montreal
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version
*
*Author(s): R. Le Tellier and A. Hebert
*
*Parameters:
* SUBFFI  flux integration subroutine with isotropic source.
* SUBFFA  flux integration subroutine with anisotropic source.
* SUBLDC  flux integration subroutine with linear-discontinuous source.
* SUBSCH  track coefficients calculation subroutine.
* CYCLIC  cyclic tracking flag.
* KPSYS   pointer array for each group properties.
* IPRINT  print parameter (equal to zero for no print).
* IPTRK   pointer to the tracking (L_TRACK signature).
* IFTRAK  tracking file unit number.
* IPMACR  pointer to the macrolib LCM object.
* NDIM    number of dimensions for the geometry.
* KV      total number of volumes for which specific values
*         of the neutron flux and reactions rates are required.
* NUN     total number of unknowns in vectors QFR and PHIIN.
* NLONG   number of spatial unknowns.
* PHIOUT  output flux vector.
* NZON    mixture-albedo index array in MCCG format.
* MATALB  albedo-mixture index array in MOCC format.
* M       number of material mixtures.
* NANI    scattering anisotropy (=1 for isotropic scattering).
* NMU     order of the polar quadrature set.
* NMAX    maximum number of elements in a track.
* NANGL   number of tracking angles in the plane.
* NREG    number of regions (volumes).
* NSOUT   number of outer surfaces.
* SOUR    undefined.
* IAAC    no acceleration / CDD acceleration of inner iterations (0/1).
* ISCR    no acceleration / SCR acceleration of inner iterations (0/1).
* LC      dimension of profiled matrices MCU and CQ.
* LFORW   flag set to .false. to transpose the coefficient matrix.
* PACA    type of preconditioner to solve the ACA corrective system.
* ITST    output: number of inner iterations.
* MAXI    maximum number of inner iterations allowed.
* QFR     input source vector.
* PHIIN   input flux vector.
* CAZ0    cosines of the tracking polar angles in 3D.
* CAZ1    first cosines of the different tracking azimuthal angles.
* CAZ2    second cosines of the different tracking azimuthal angles.
* CPO     cosines of the different tracking polar angles in 2D.
* ZMU     polar quadrature set.
* WZMU    polar quadrature set.
* VOL     volumes.
* EPS     precision reached after min(MAXI,ITST) iterations.
* ERRTOL  tolerance for stopping criterion. process is stopped
*         as soon as ||Phi(n+1)-Phi(n)||/||Phi(n)|| <= EPS
*         with ||.|| the euclidean norm.
* REPSI   array containing precision of each iteration.
* NSTART  undefined.
* SIGAL   total cross-section and albedo array.
* LPS     used in scr acceleration.
* NGROUP  number of groups.
* NGEFF   number of groups to process.
* NGIND   index of the groups to process.
* NCONV   array of convergence flag for each group.
* LNCONV  number of unconverged groups.
* NLIN    number of polynomial components in flux spatial expansion.
* NFUNL   number of moments of the flux (in 2D: NFUNL=NANI*(NANI+1)/2).
* KEYFLX  position of flux elements in PHIOUT vector.
* KEYCUR  position of current elements in PHIOUT vector.
* STIS    Source term isolation option for flux integration.
* NPJJM   number of pjj modes to store for STIS option.
* REBFLG  ACA or SCR rebalancing flag.
* LPRISM  3D prismatic extended tracking flag.
* N2REG   number of regions in the 2D tracking if LPRISM.
* N2SOU   number of external surfaces in the 2D tracking if LPRISM.
* NZP     number of z-plans if LPRISM.
* DELU    input track spacing for 3D track reconstruction if LPRISM.
* FACSYM  tracking symmetry factor for maximum track length if LPRISM.
* IDIR    direction of fundamental current for TIBERE with MoC 
*         (=0,1,2,3). 
* NBATCH  number of tracks processed in each OpenMP core (default: =1).
*
*-----------------------------------------------------------------------
*
      USE GANLIB
      IMPLICIT DOUBLE PRECISION(A-H,O-Z)
*----
*  Subroutine arguments
*----
      TYPE(C_PTR) KPSYS(NGEFF),IPTRK,IPMACR
      INTEGER NGEFF,IPRINT,IFTRAK,NDIM,KV,NUN,NLONG,NGROUP,NZON(NLONG),
     1 M,NANI,NMU,NMAX,IAAC,LC,PACA,ITST(NGEFF),NSTART,MAXI,NANGL,NREG,
     2 NSOUT,ISCR,LPS,NGIND(NGEFF),LNCONV,NLIN,NFUNL,KEYFLX(NREG,NLIN,
     3 NFUNL),KEYCUR(NLONG-NREG),STIS,NPJJM,MATALB(-NSOUT:NREG),N2REG,
     4 N2SOU,NZP,IDIR,NBATCH
      REAL QFR(NUN,NGEFF),PHIIN(NUN,NGEFF),CPO(NMU),ZMU(NMU),
     1 WZMU(NMU),VOL(NLONG),EPS(NGEFF),ERRTOL,REPSI(MAXI,NGEFF),
     2 SIGAL(-6:M,NGEFF),DELU,FACSYM
      DOUBLE PRECISION PHIOUT(NUN,NGEFF),CAZ0(NANGL),CAZ1(NANGL),
     1 CAZ2(NANGL),SOUR(NUN,NGEFF)
      LOGICAL LFORW,CYCLIC,NCONV(NGEFF),REBFLG,LPRISM
      EXTERNAL SUBFFI,SUBFFA,SUBLDC,SUBSCH
*---
*   Local variables
*---
      REAL EPSINTO,FAC
      LOGICAL RHSFLG
      PARAMETER (FAC=100.0,MAXINT=200)
*---
*   Allocatable arrays
*---
      INTEGER, ALLOCATABLE, DIMENSION(:) :: KMAX
      REAL, ALLOCATABLE, DIMENSION(:,:) :: RHS, GAR
      DOUBLE PRECISION, ALLOCATABLE, DIMENSION(:) :: DENOM, RHO
      DOUBLE PRECISION, ALLOCATABLE, DIMENSION(:,:) :: R, G, C, S, FLOUT
      DOUBLE PRECISION, ALLOCATABLE, DIMENSION(:,:,:) :: V, H
*---
*   Scratch storage allocation
*---
      ALLOCATE(DENOM(NGEFF),RHO(NGEFF),KMAX(NGEFF),FLOUT(NUN,NGEFF))
      ALLOCATE(RHS(NUN,NGEFF),GAR(NUN,NGEFF),V(NUN,NGEFF,NSTART+1),
     1 G(NSTART+1,NGEFF),H(NSTART+1,NSTART,NGEFF),C(NSTART+1,NGEFF),
     2 S(NSTART+1,NGEFF),R(NUN,NGEFF))
*
      IF(MAXI.LT.3) CALL XABORT('MCGMRE: MAXI MUST BE >= 3.')
      RHSFLG=.TRUE.
      EPSINTO=ERRTOL/FAC
      MAXIT=MAXI-1
      NCONV(:)=.FALSE.
      RHO(:)=0.0D0
      LNCONV=0
      DO II=1,NGEFF
        DENOM(II)=SQRT(DOT_PRODUCT(QFR(:,II),QFR(:,II)))
        RHO(II)=1.0D20
        NCONV(II)=(DENOM(II) /= 0.0D0)
        IF(NCONV(II)) LNCONV=LNCONV+1
        ITST(II)=0
        EPS(II)=0.0
      ENDDO
*---
*   Global GMRES(M) iteration
*---
      ITER=0
      DO WHILE((LNCONV /= 0) .AND. (ITER < MAXIT))
        ITER=ITER+1
        CALL MCGFL1(SUBFFI,SUBFFA,SUBLDC,SUBSCH,CYCLIC,KPSYS,IPRINT,
     1       IPTRK,IFTRAK,IPMACR,NDIM,KV,NUN,NLONG,PHIOUT(1,1),NZON,
     2       MATALB,M,NANI,NMU,NMAX,NANGL,NREG,NSOUT,NGROUP,NGEFF,NGIND,
     3       SOUR,IAAC,ISCR,LC,LFORW,PACA,EPSINTO,MAXINT,NLIN,NFUNL,
     4       KEYFLX,KEYCUR,QFR,PHIIN(1,1),CAZ0,CAZ1,CAZ2,CPO,ZMU,WZMU,
     5       VOL,SIGAL,LPS,NCONV,.FALSE.,STIS,NPJJM,REBFLG,LPRISM,N2REG,
     6       N2SOU,NZP,DELU,FACSYM,IDIR,NBATCH)
        ERROR=0.0D0
        DO II=1,NGEFF
          REPSI(ITER,II)=0.0
          IF(.NOT.NCONV(II)) CYCLE
          R(:,II)=PHIOUT(:,II)-PHIIN(:,II)
          RHO(II)=SQRT(DOT_PRODUCT(R(:,II),R(:,II)))
          REPSI(ITER,II)=REAL(RHO(II)/DENOM(II))
          IF(IPRINT.GT.4) WRITE(6,200) ITER,II,REPSI(ITER,II)
          EPS(II)=REPSI(ITER,II)
          ITST(II)=ITER
          ERROR=MAX(ERROR,RHO(II)/DENOM(II))
          IF(RHO(II) < ERRTOL*DENOM(II)) THEN
            NCONV(II)=.FALSE.
            LNCONV=LNCONV-1
          ENDIF
        ENDDO
*
*       Test do termination on entry
        IF(LNCONV == 0) EXIT
*
        H(:,:,:)=0.0D0
        V(:,:,:)=0.0D0
        C(:,:)=0.0D0
        S(:,:)=0.0D0
        G(:,:)=0.0D0
        KMAX(:)=0
        DO II=1,NGEFF
          IF(.NOT.NCONV(II)) CYCLE
          G(1,II)=RHO(II)
          V(:,II,1)=R(:,II)/RHO(II)
        ENDDO
*---
*   Evaluate RHS of the linear system
*---
        IF(RHSFLG) THEN
          IF(IPRINT > 3) WRITE(6,100) IAAC,ISCR
          RHS(:,:)=0.0
          CALL MCGFL1(SUBFFI,SUBFFA,SUBLDC,SUBSCH,CYCLIC,KPSYS,IPRINT,
     1         IPTRK,IFTRAK,IPMACR,NDIM,KV,NUN,NLONG,FLOUT(1,1),NZON,
     2         MATALB,M,NANI,NMU,NMAX,NANGL,NREG,NSOUT,NGROUP,NGEFF,
     3         NGIND,SOUR,IAAC,ISCR,LC,LFORW,PACA,EPSINTO,MAXINT,NLIN,
     4         NFUNL,KEYFLX,KEYCUR,QFR,RHS(1,1),CAZ0,CAZ1,CAZ2,CPO,ZMU,
     5         WZMU,VOL,SIGAL,LPS,NCONV,.FALSE.,STIS,NPJJM,REBFLG,
     6         LPRISM,N2REG,N2SOU,NZP,DELU,FACSYM,IDIR,NBATCH)
          DO II=1,NGEFF
            IF(NCONV(II)) RHS(:,II)=REAL(FLOUT(:,II))
          ENDDO
          RHSFLG=.FALSE.
        ENDIF
*---
*   GMRES(1) iteration
*---
        K=0
        DO WHILE((LNCONV /= 0) .AND. (K < NSTART) .AND. (ITER < MAXIT))
          K=K+1
          ITER=ITER+1
          GAR(:,:)=REAL(V(:,:,K))
          CALL MCGFL1(SUBFFI,SUBFFA,SUBLDC,SUBSCH,CYCLIC,KPSYS,IPRINT,
     1         IPTRK,IFTRAK,IPMACR,NDIM,KV,NUN,NLONG,FLOUT(1,1),NZON,
     2         MATALB,M,NANI,NMU,NMAX,NANGL,NREG,NSOUT,NGROUP,NGEFF,
     3         NGIND,SOUR,IAAC,ISCR,LC,LFORW,PACA,EPSINTO,MAXINT,NLIN,
     4         NFUNL,KEYFLX,KEYCUR,QFR,GAR(1,1),CAZ0,CAZ1,CAZ2,CPO,ZMU,
     5         WZMU,VOL,SIGAL,LPS,NCONV,.FALSE.,STIS,NPJJM,REBFLG,
     6         LPRISM,N2REG,N2SOU,NZP,DELU,FACSYM,IDIR,NBATCH)
          V(:,:,K+1)=V(:,:,K)-FLOUT(:,:)+RHS(:,:)
          ERROR=0.0D0
          DO II=1,NGEFF
            REPSI(ITER,II)=0.0
            IF(.NOT.NCONV(II)) CYCLE
            KMAX(II)=K
*
*           Modified Gram-Schmidt
            DO J=1,K
              H(J,K,II)=DOT_PRODUCT(V(:,II,J),V(:,II,K+1))
              V(:,II,K+1)=V(:,II,K+1)-H(J,K,II)*V(:,II,J)
            ENDDO
            H(K+1,K,II)=SQRT(DOT_PRODUCT(V(:,II,K+1),V(:,II,K+1)))
*
*           Reorthogonalize
            DO J=1,K
              HR=DOT_PRODUCT(V(:,II,J),V(:,II,K+1))
              H(J,K,II)=H(J,K,II)+HR
              V(:,II,K+1)=V(:,II,K+1)-HR*V(:,II,J)
            ENDDO
            H(K+1,K,II)=SQRT(DOT_PRODUCT(V(:,II,K+1),V(:,II,K+1)))
*
            ! Watch out do happy breakdown 
            IF(H(K+1,K,II) /= 0.0D0) V(:,II,K+1)=V(:,II,K+1)/H(K+1,K,II)
*
*           Form and store the information for the new Givens rotation
            DO I=1,K-1
              W1=C(I,II)*H(I,K,II)-S(I,II)*H(I+1,K,II)
              W2=S(I,II)*H(I,K,II)+C(I,II)*H(I+1,K,II)
              H(I,K,II)=W1
              H(I+1,K,II)=W2
            ENDDO
            ZNU=SQRT(H(K,K,II)**2+H(K+1,K,II)**2)
            IF(ZNU /= 0.0D0) THEN
              C(K,II)=H(K,K,II)/ZNU
              S(K,II)=-H(K+1,K,II)/ZNU
              H(K,K,II)=C(K,II)*H(K,K,II)-S(K,II)*H(K+1,K,II)
              H(K+1,K,II)=0.0D0
              W1=C(K,II)*G(K,II)-S(K,II)*G(K+1,II)
              W2=S(K,II)*G(K,II)+C(K,II)*G(K+1,II)
              G(K,II)=W1
              G(K+1,II)=W2
            ENDIF
*
*           Update the residual norm
            RHO(II)=ABS(G(K+1,II))
            REPSI(ITER,II)=REAL(RHO(II)/DENOM(II))
            IF(IPRINT.GT.4) WRITE(6,200) ITER,II,REPSI(ITER,II)
            EPS(II)=REPSI(ITER,II)
            ITST(II)=ITER
            IF(RHO(II) < ERRTOL*DENOM(II)) THEN
              NCONV(II)=.FALSE.
              LNCONV=LNCONV-1
            ENDIF
            ERROR=MAX(ERROR,RHO(II)/DENOM(II))
          ENDDO
        ENDDO
*---
*   At this point either K > NSTART or RHOGRP < ERRTOL. It's time to
*   compute PHIIN and cycle.
*---
        DO II=1,NGEFF
          K=KMAX(II)
          IF(K == 0) CYCLE
          G(K,II)=G(K,II)/H(K,K,II)
          DO L=K-1,1,-1
            W1=G(L,II)-DOT_PRODUCT(H(L,L+1:K,II),G(L+1:K,II))
            G(L,II)=W1/H(L,L,II)
          ENDDO
          DO J=1,K
            PHIIN(:,II)=PHIIN(:,II)+REAL(G(J,II)*V(:,II,J))
          ENDDO
        ENDDO
      ENDDO
*----
*  Scratch storage deallocation
*----
      DEALLOCATE(R, S, C, H, G, V, GAR, RHS)
      DEALLOCATE(FLOUT, KMAX, RHO, DENOM)
      RETURN
*
  100 FORMAT(' MCGMRE: RHS CALCULATED WITH AAC-SCR : ',I2,1H-,I1)
  200 FORMAT(' MCGMRE: EPS Iteration ',I5,' Group ',I5,2X,1P,E16.7)
      END