Debian Codegraph @

debian/external_deps/fetk/punc/src/pmg/src_f77/ngsd.f

raw
1 c* ///////////////////////////////////////////////////////////////////////////
2 c* @file ngsd.f
3 c* @author Michael Holst
4 c* @brief Nonlinear Gauss-Seidel iteration.
5 c* @version $Id: ngsd.f,v 1.2 2010/08/12 05:45:36 fetk Exp $
6 c* @attention
7 c* @verbatim
8 c*
9 c* PMG -- Parallel algebraic MultiGrid
10 c* Copyright (C) 1994-- Michael Holst.
11 c*
12 c* Michael Holst <[email protected]>
13 c* University of California, San Diego
14 c* Department of Mathematics, 5739 AP&M
15 c* 9500 Gilman Drive, Dept. 0112
16 c* La Jolla, CA 92093-0112 USA
17 c* http://math.ucsd.edu/~mholst
18 c*
19 c* This file is part of PMG.
20 c*
21 c* PMG is free software; you can redistribute it and/or modify
22 c* it under the terms of the GNU General Public License as published by
23 c* the Free Software Foundation; either version 2 of the License, or
24 c* (at your option) any later version.
25 c*
26 c* PMG is distributed in the hope that it will be useful,
27 c* but WITHOUT ANY WARRANTY; without even the implied warranty of
28 c* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 c* GNU General Public License for more details.
30 c*
31 c* You should have received a copy of the GNU General Public License
32 c* along with PMG; if not, write to the Free Software
33 c* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 c*
35 c* Linking PMG statically or dynamically with other modules is making a
36 c* combined work based on PMG. Thus, the terms and conditions of the GNU
37 c* General Public License cover the whole combination.
38 c*
39 c* SPECIAL GPL EXCEPTION
40 c* In addition, as a special exception, the copyright holders of PMG
41 c* give you permission to combine the PMG program with free software
42 c* programs and libraries that are released under the GNU LGPL or with
43 c* code included in releases of ISIM, PMV, PyMOL, SMOL, VMD, and Vision.
44 c* Such combined software may be linked with PMG and redistributed together
45 c* in original or modified form as mere aggregation without requirement that
46 c* the entire work be under the scope of the GNU General Public License.
47 c* This special exception permission is also extended to any software listed
48 c* in the SPECIAL GPL EXCEPTION clauses by the FEtk and APBS libraries.
49 c*
50 c* Note that people who make modified versions of PMG are not obligated
51 c* to grant this special exception for their modified versions; it is
52 c* their choice whether to do so. The GNU General Public License gives
53 c* permission to release a modified version without this exception; this
54 c* exception also makes it possible to release a modified version which
55 c* carries forward this exception.
56 c*
57 c* @endverbatim
58 c* ///////////////////////////////////////////////////////////////////////////
59
60 subroutine ngsrb(nx,ny,nz,ipc,rpc,ac,cc,fc,x,w1,w2,r,
61 2 itmax,iters,errtol,omega,iresid,iadjoint)
62 c* *********************************************************************
63 c* purpose:
64 c*
65 c* call the fast diagonal iterative method.
66 c*
67 c* author: michael holst
68 c* *********************************************************************
69 implicit none
70 integer ipc(*),itmax,iters,iresid,iadjoint,nx,ny,nz
71 integer numdia
72 double precision omega,errtol
73 double precision rpc(*),ac(nx*ny*nz,*),cc(nx,ny,nz),fc(nx,ny,nz)
74 double precision x(nx,ny,nz),w1(nx,ny,nz),w2(nx,ny,nz)
75 double precision r(nx,ny,nz)
76 c*
77 cmdir 0 0
78 c*
79 c* *** do in one step ***
80 numdia = ipc(11)
81 if (numdia .eq. 7) then
82 call ngsrb7(nx,ny,nz,ipc,rpc,ac(1,1),cc,fc,
83 2 ac(1,2),ac(1,3),ac(1,4),
84 3 x,w1,w2,r,
85 4 itmax,iters,errtol,omega,iresid,iadjoint)
86 elseif (numdia .eq. 27) then
87 call ngsrb27(nx,ny,nz,ipc,rpc,ac(1,1),cc,fc,
88 2 ac(1,2),ac(1,3),ac(1,4),ac(1,5),ac(1,6),ac(1,7),ac(1,8),
89 3 ac(1,9),ac(1,10),ac(1,11),ac(1,12),ac(1,13),ac(1,14),
90 4 x,w1,w2,r,
91 5 itmax,iters,errtol,omega,iresid,iadjoint)
92 else
93 print*,'% NGSRB: invalid stencil type given...'
94 endif
95 c*
96 c* *** return and end ***
97 return
98 end
99 subroutine ngsrb7(nx,ny,nz,ipc,rpc,
100 2 oC,cc,fc,
101 3 oE,oN,uC,
102 4 x,w1,w2,r,
103 5 itmax,iters,errtol,omega,iresid,iadjoint)
104 c* *********************************************************************
105 c* purpose:
106 c*
107 c* 7 diagonal nonlinear gauss-seidel routine.
108 c*
109 c* this routine applies the gauss-seidel operator or its
110 c* adjoint depending on the flag iadjoint.
111 c*
112 c* algorithm parameters:
113 c* ---------------------
114 c*
115 c* U0 = initial guess
116 c* Llin = matrix representing linear part of equations
117 c* F = vector representing rhs of equations
118 c* nx = number of unknowns
119 c* itmax = number of iterations to perform
120 c* c_scal = function representing nonlinear part of equations
121 c* dc_scal = function representing derivative of nonlinear
122 c* part of equations
123 c* U = final result
124 c*
125 c* algorithm idea:
126 c* ---------------
127 c*
128 c* the ith nonlinear equation is represented as:
129 c*
130 c* N(UU) = AA * UU + c_scal(UU) - FF
131 c*
132 c* which is solved for UU using newton's method, where:
133 c*
134 c* AA = ith diagonal entry of Llin
135 c* FF = F(i), minus the nondiag of ith eqn of Llin times current U
136 c* UU = the unknown, initialized to U(i)
137 c*
138 c* algorithm (matlab language):
139 c* ----------------------------
140 c*
141 c* function [U,iterss,errors] = Zgs(U0,UTRUE,a,b,Llin,F,nx,tol,itmax,stp,plt);
142 c* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143 c* %%% nonlinear gauss-seidel-newton iteration (ortega-rheinboldt, page 219)
144 c* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
145 c*
146 c* %%% initial guess
147 c* iters = 0;
148 c* error = 1.0d0;
149 c* U = U0;
150 c* Lnon = c_vec(b,U,nx);
151 c* N = Llin * U + Lnon - F ;
152 c* denom = 1.0d0; if (stp == 1) denom = norm(N); end;
153 c*
154 c* %%% loop
155 c* while ((error > tol) & (iters < itmax))
156 c* iters = iters + 1;
157 c*
158 c* %%% save old guys
159 c* Lnon_OLD = Lnon;
160 c* U_OLD = U;
161 c*
162 c* %%% relax each unknown
163 c* for i = 1:nx
164 c*
165 c* %%% define the 1d rhs
166 c* FF = F(i) - Llin(i,1:nx) * U + Llin(i,i) * U(i);
167 c* AA = Llin(i,i);
168 c* UU = U(i);
169 c* BB = b(i);
170 c*
171 c* %%% perform 1d newton's method
172 c* %%% note that in the linear case, this is equivalent to: FF/AA
173 c* nitmax = 10;
174 c* ntol = 1.0e-7;
175 c* niters = 0;
176 c* N = AA * UU + c_scal(BB,UU) - FF ;
177 c* ndenom = N;
178 c* if (ndenom == 0)
179 c* ndenom = 1;
180 c* end;
181 c* nerror = N / ndenom;
182 c*
183 c* while ((nerror > ntol) & (niters < nitmax))
184 c* niters = niters + 1;
185 c*
186 c* %%% construct jacobian matrix of N
187 c* DN = AA + dc_scal(BB,UU);
188 c*
189 c* %%% solve linear system
190 c* change = - N / DN;
191 c*
192 c* %%% update solution
193 c* UU = UU + change;
194 c*
195 c* %%% construct nonlinear residual
196 c* N = AA * UU + c_scal(BB,UU) - FF ;
197 c*
198 c* %%% compute residual
199 c* nerror = N / ndenom;
200 c* end
201 c*
202 c* %%% update the unknown
203 c* U(i) = UU;
204 c* end
205 c*
206 c* %%% new residual %%%
207 c* Lnon = c_vec(b,U,nx);
208 c* N = Llin * U + Lnon - F ;
209 c* delta = zeros(U);
210 c* delta_s = zeros(U);
211 c*
212 c* end
213 c*
214 c* author: michael holst
215 c* *********************************************************************
216 implicit none
217 integer ipc(*),itmax,iters,iresid,iadjoint,nx,ny,nz
218 integer i,j,k,i1,i2,j1,j2,k1,k2,istep
219 double precision omega,errtol
220 double precision rpc(*),oE(nx,ny,nz),oN(nx,ny,nz),uC(nx,ny,nz)
221 double precision fc(nx,ny,nz),oC(nx,ny,nz),cc(nx,ny,nz)
222 double precision x(nx,ny,nz),w1(nx,ny,nz),w2(nx,ny,nz)
223 double precision r(nx,ny,nz)
224 double precision c_scal,dc_scal
225 double precision UU,AA,BB,FF,zNN,DzNN,zntol,zndenom,znerror
226 double precision change
227 integer nitmax,niters,ifail_tol,ipkey
228 c*
229 cmdir 0 0
230 c*
231 c* *** nonlinear iteration tolerance and itmax ***
232 nitmax = 10
233 zntol = 1.0e-5
234 ifail_tol = 0
235 ipkey = ipc(10)
236 c*
237 c* *** do the gauss-seidel iteration itmax times ***
238 i1 = (1-iadjoint) * 2 + iadjoint * (nx-1)
239 i2 = iadjoint * 2 + (1-iadjoint) * (nx-1)
240 j1 = (1-iadjoint) * 2 + iadjoint * (ny-1)
241 j2 = iadjoint * 2 + (1-iadjoint) * (ny-1)
242 k1 = (1-iadjoint) * 2 + iadjoint * (nz-1)
243 k2 = iadjoint * 2 + (1-iadjoint) * (nz-1)
244 istep = iadjoint*(-1) + (1-iadjoint)*(1)
245 do 30 iters = 1, itmax
246 c*
247 c* *** do the red points ***
248 cmdir 3 1
249 do 10 k=k1,k2,istep
250 cmdir 3 2
251 do 11 j=j1,j2,istep
252 cmdir 3 3
253 do 12 i=i1,i2,istep
254 c*
255 c* *** for this unknown, do a 1d newton's method
256 c*
257 c* *** determine the 1d equation
258 UU = x(i,j,k)
259 AA = oC(i,j,k)
260 BB = cc(i,j,k)
261 FF = fc(i,j,k)
262 2 + oN(i,j,k) * x(i,j+1,k)
263 3 + oN(i,j-1,k) * x(i,j-1,k)
264 4 + oE(i,j,k) * x(i+1,j,k)
265 5 + oE(i-1,j,k) * x(i-1,j,k)
266 6 + uC(i,j,k-1) * x(i,j,k-1)
267 7 + uC(i,j,k) * x(i,j,k+1)
268 c*
269 c* *** evaluate residual of 1d system
270 zNN = AA * UU + c_scal(BB,UU,ipkey) - FF
271 c*
272 c* *** setup for newton's method
273 niters = 0
274 zndenom = zNN
275 if (zndenom .eq. 0.0d0) zndenom = 1.0d0
276 znerror = zNN / zndenom
277 c*
278 c* *** the 1d newton's method ***
279 if (znerror .lt. zntol) goto 17
280 if (niters .gt. nitmax) goto 16
281 15 continue
282 niters = niters + 1
283 c*
284 c* *** construct jacobian matrix of NN ***
285 DzNN = AA + dc_scal(BB,UU,ipkey)
286 c*
287 c* *** solve the linear system ***
288 change = - zNN / DzNN
289 c*
290 c* *** update the solution ***
291 UU = UU + change
292 c*
293 c* *** evaluate residual of 1d system ***
294 zNN = AA * UU + c_scal(BB,UU,ipkey) - FF
295 c*
296 c* *** compute error ***
297 znerror = zNN / zndenom
298 c*
299 c* *** tolerance and itmax check ***
300 if (znerror .lt. zntol) goto 17
301 if (niters .gt. nitmax) goto 16
302 c*
303 c* *** loop ***
304 goto 15
305 c*
306 c* *** tolerance not reached ***
307 16 continue
308 ifail_tol = ifail_tol + 1
309 c*
310 c* *** tolerance reached ***
311 17 continue
312 c*
313 c* *** newton's method complete -- update solution value ***
314 x(i,j,k) = UU
315 12 continue
316 11 continue
317 10 continue
318 c*
319 c* *** main loop ***
320 30 continue
321 c*
322 c* *** if specified, return the new residual as well ***
323 if (iresid .eq. 1) then
324 call nmresid7_1s(nx,ny,nz,ipc,rpc,oC,cc,fc,oE,oN,uC,x,r,w1)
325 endif
326 c*
327 c* *** messages ***
328 if (ifail_tol .gt. 0) then
329 print*,'% NGSRB: 1d newton tolerance failures: ',ifail_tol
330 endif
331 c*
332 c* *** return and end ***
333 return
334 end
335 subroutine ngsrb27(nx,ny,nz,ipc,rpc,
336 2 oC,cc,fc,
337 3 oE,oN,uC,oNE,oNW,uE,uW,uN,uS,uNE,uNW,uSE,uSW,
338 4 x,w1,w2,r,
339 5 itmax,iters,errtol,omega,iresid,iadjoint)
340 c* *********************************************************************
341 c* purpose:
342 c*
343 c* 27 diagonal nonlinear gauss-seidel routine.
344 c*
345 c* this routine applies the gauss-seidel operator or its
346 c* adjoint depending on the flag iadjoint.
347 c*
348 c* algorithm parameters:
349 c* ---------------------
350 c*
351 c* U0 = initial guess
352 c* Llin = matrix representing linear part of equations
353 c* F = vector representing rhs of equations
354 c* nx = number of unknowns
355 c* itmax = number of iterations to perform
356 c* c_scal = function representing nonlinear part of equations
357 c* dc_scal = function representing derivative of nonlinear
358 c* part of equations
359 c* U = final result
360 c*
361 c* algorithm idea:
362 c* ---------------
363 c*
364 c* the ith nonlinear equation is represented as:
365 c*
366 c* N(UU) = AA * UU + c_scal(UU) - FF
367 c*
368 c* which is solved for UU using newton's method, where:
369 c*
370 c* AA = ith diagonal entry of Llin
371 c* FF = F(i), minus the nondiag of ith eqn of Llin times current U
372 c* UU = the unknown, initialized to U(i)
373 c*
374 c* algorithm (matlab language):
375 c* ----------------------------
376 c*
377 c* function [U,iterss,errors] = Zgs(U0,UTRUE,a,b,Llin,F,nx,tol,itmax,stp,plt);
378 c* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
379 c* %%% nonlinear gauss-seidel-newton iteration (ortega-rheinboldt, page 219)
380 c* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
381 c*
382 c* %%% initial guess
383 c* iters = 0;
384 c* error = 1.0d0;
385 c* U = U0;
386 c* Lnon = c_vec(b,U,nx);
387 c* N = Llin * U + Lnon - F ;
388 c* denom = 1.0d0; if (stp == 1) denom = norm(N); end;
389 c*
390 c* %%% loop
391 c* while ((error > tol) & (iters < itmax))
392 c* iters = iters + 1;
393 c*
394 c* %%% save old guys
395 c* Lnon_OLD = Lnon;
396 c* U_OLD = U;
397 c*
398 c* %%% relax each unknown
399 c* for i = 1:nx
400 c*
401 c* %%% define the 1d rhs
402 c* FF = F(i) - Llin(i,1:nx) * U + Llin(i,i) * U(i);
403 c* AA = Llin(i,i);
404 c* UU = U(i);
405 c* BB = b(i);
406 c*
407 c* %%% perform 1d newton's method
408 c* %%% note that in the linear case, this is equivalent to: FF/AA
409 c* nitmax = 10;
410 c* ntol = 1.0e-7;
411 c* niters = 0;
412 c* N = AA * UU + c_scal(BB,UU) - FF ;
413 c* ndenom = N;
414 c* if (ndenom == 0)
415 c* ndenom = 1;
416 c* end;
417 c* nerror = N / ndenom;
418 c*
419 c* while ((nerror > ntol) & (niters < nitmax))
420 c* niters = niters + 1;
421 c*
422 c* %%% construct jacobian matrix of N
423 c* DN = AA + dc_scal(BB,UU);
424 c*
425 c* %%% solve linear system
426 c* change = - N / DN;
427 c*
428 c* %%% update solution
429 c* UU = UU + change;
430 c*
431 c* %%% construct nonlinear residual
432 c* N = AA * UU + c_scal(BB,UU) - FF ;
433 c*
434 c* %%% compute residual
435 c* nerror = N / ndenom;
436 c* end
437 c*
438 c* %%% update the unknown
439 c* U(i) = UU;
440 c* end
441 c*
442 c* %%% new residual %%%
443 c* Lnon = c_vec(b,U,nx);
444 c* N = Llin * U + Lnon - F ;
445 c* delta = zeros(U);
446 c* delta_s = zeros(U);
447 c*
448 c* end
449 c*
450 c* author: michael holst
451 c* *********************************************************************
452 implicit none
453 integer ipc(*),itmax,iters,iresid,iadjoint,nx,ny,nz
454 integer i,j,k,i1,i2,j1,j2,k1,k2,istep
455 double precision omega,errtol
456 double precision rpc(*),oE(nx,ny,nz),oN(nx,ny,nz),uC(nx,ny,nz)
457 double precision oNE(nx,ny,nz),oNW(nx,ny,nz),uE(nx,ny,nz)
458 double precision uW(nx,ny,nz),uN(nx,ny,nz),uS(nx,ny,nz)
459 double precision uNE(nx,ny,nz),uNW(nx,ny,nz),uSE(nx,ny,nz)
460 double precision uSW(nx,ny,nz)
461 double precision fc(nx,ny,nz),oC(nx,ny,nz),cc(nx,ny,nz)
462 double precision x(nx,ny,nz),w1(nx,ny,nz),w2(nx,ny,nz)
463 double precision r(nx,ny,nz)
464 double precision c_scal,dc_scal
465 double precision UU,AA,BB,FF,zNN,DzNN,zntol,zndenom,znerror
466 double precision change
467 integer nitmax,niters,ifail_tol,ipkey
468 double precision tmpO,tmpU,tmpD
469 c*
470 cmdir 0 0
471 c*
472 c* *** nonlinear iteration tolerance and itmax ***
473 nitmax = 10
474 zntol = 1.0e-5
475 ifail_tol = 0
476 ipkey = ipc(10)
477 c*
478 c* *** do the gauss-seidel iteration itmax times ***
479 i1 = (1-iadjoint) * 2 + iadjoint * (nx-1)
480 i2 = iadjoint * 2 + (1-iadjoint) * (nx-1)
481 j1 = (1-iadjoint) * 2 + iadjoint * (ny-1)
482 j2 = iadjoint * 2 + (1-iadjoint) * (ny-1)
483 k1 = (1-iadjoint) * 2 + iadjoint * (nz-1)
484 k2 = iadjoint * 2 + (1-iadjoint) * (nz-1)
485 istep = iadjoint*(-1) + (1-iadjoint)*(1)
486 do 30 iters = 1, itmax
487 c*
488 c* *** do all of the red points ***
489 cmdir 3 1
490 do 10 k=k1,k2,istep
491 cmdir 3 2
492 do 11 j=j1,j2,istep
493 cmdir 3 3
494 do 12 i=i1,i2,istep
495 c*
496 c* *** for this unknown, do a 1d newton's method
497 c*
498 c* *** determine the 1d equation
499 UU = x(i,j,k)
500 AA = oC(i,j,k)
501 BB = cc(i,j,k)
502 tmpO =
503 2 + oN(i,j,k) * x(i,j+1,k)
504 3 + oN(i,j-1,k) * x(i,j-1,k)
505 4 + oE(i,j,k) * x(i+1,j,k)
506 5 + oE(i-1,j,k) * x(i-1,j,k)
507 6 + oNE(i,j,k) * x(i+1,j+1,k)
508 7 + oNW(i,j,k) * x(i-1,j+1,k)
509 8 + oNW(i+1,j-1,k) * x(i+1,j-1,k)
510 9 + oNE(i-1,j-1,k) * x(i-1,j-1,k)
511 tmpU =
512 2 + uC(i,j,k) * x(i,j,k+1)
513 3 + uN(i,j,k) * x(i,j+1,k+1)
514 4 + uS(i,j,k) * x(i,j-1,k+1)
515 5 + uE(i,j,k) * x(i+1,j,k+1)
516 6 + uW(i,j,k) * x(i-1,j,k+1)
517 7 + uNE(i,j,k) * x(i+1,j+1,k+1)
518 8 + uNW(i,j,k) * x(i-1,j+1,k+1)
519 9 + uSE(i,j,k) * x(i+1,j-1,k+1)
520 9 + uSW(i,j,k) * x(i-1,j-1,k+1)
521 tmpD =
522 2 + uC(i,j,k-1) * x(i,j,k-1)
523 3 + uS(i,j+1,k-1) * x(i,j+1,k-1)
524 4 + uN(i,j-1,k-1) * x(i,j-1,k-1)
525 5 + uW(i+1,j,k-1) * x(i+1,j,k-1)
526 6 + uE(i-1,j,k-1) * x(i-1,j,k-1)
527 7 + uSW(i+1,j+1,k-1) * x(i+1,j+1,k-1)
528 8 + uSE(i-1,j+1,k-1) * x(i-1,j+1,k-1)
529 9 + uNW(i+1,j-1,k-1) * x(i+1,j-1,k-1)
530 9 + uNE(i-1,j-1,k-1) * x(i-1,j-1,k-1)
531 FF = fc(i,j,k) + tmpO + tmpU + tmpD
532 c*
533 c* *** evaluate residual of 1d system
534 zNN = AA * UU + c_scal(BB,UU,ipkey) - FF
535 c*
536 c* *** setup for newton's method
537 niters = 0
538 zndenom = zNN
539 if (zndenom .eq. 0.0d0) zndenom = 1.0d0
540 znerror = zNN / zndenom
541 c*
542 c* *** the 1d newton's method ***
543 if (znerror .lt. zntol) goto 17
544 if (niters .gt. nitmax) goto 16
545 15 continue
546 niters = niters + 1
547 c*
548 c* *** construct jacobian matrix of NN ***
549 DzNN = AA + dc_scal(BB,UU,ipkey)
550 c*
551 c* *** solve the linear system ***
552 change = - zNN / DzNN
553 c*
554 c* *** update the solution ***
555 UU = UU + change
556 c*
557 c* *** evaluate residual of 1d system ***
558 zNN = AA * UU + c_scal(BB,UU,ipkey) - FF
559 c*
560 c* *** compute error ***
561 znerror = zNN / zndenom
562 c*
563 c* *** tolerance and itmax check ***
564 if (znerror .lt. zntol) goto 17
565 if (niters .gt. nitmax) goto 16
566 c*
567 c* *** loop ***
568 goto 15
569 c*
570 c* *** tolerance not reached ***
571 16 continue
572 ifail_tol = ifail_tol + 1
573 c*
574 c* *** tolerance reached ***
575 17 continue
576 c*
577 c* *** newton's method complete -- update solution value ***
578 x(i,j,k) = UU
579 12 continue
580 11 continue
581 10 continue
582 c*
583 c* *** main loop ***
584 30 continue
585 c*
586 c* *** if specified, return the new residual as well ***
587 if (iresid .eq. 1) then
588 call nmresid27_1s(nx,ny,nz,ipc,rpc,oC,cc,fc,
589 2 oE,oN,uC,oNE,oNW,uE,uW,uN,uS,uNE,uNW,uSE,uSW,
590 3 x,r,w1)
591 endif
592 c*
593 c* *** messages ***
594 if (ifail_tol .gt. 0) then
595 print*,'% NGSRB: 1d newton tolerance failures: ',ifail_tol
596 endif
597 c*
598 c* *** return and end ***
599 return
600 end
601 subroutine ngsrb7X(nx,ny,nz,ipc,rpc,
602 2 oC,cc,fc,
603 3 oE,oN,uC,
604 4 x,w1,w2,r,
605 5 itmax,iters,errtol,omega,iresid,iadjoint)
606 c* *********************************************************************
607 c* purpose:
608 c*
609 c* fast 7 diagonal nonlinear red/black gauss-seidel routine.
610 c*
611 c* this routine applies the red/black gauss-seidel operator or its
612 c* adjoint depending on the flag iadjoint. note that the adjoint
613 c* in this case is simply doing the red or black points first.
614 c*
615 c* note that the interior grid points from 2,...,nx-1, etc.
616 c* we then begin coloring with a red point, and do a point red/black
617 c* coloring.
618 c*
619 c* the red points are:
620 c*
621 c* if ((j even) and (k even)) then
622 c* begin row at first point, i=2 (i even), or ioff = 0
623 c* else if ((j odd) and (k even)) then
624 c* begin row at second point, i=3 (i odd), or ioff = 1
625 c* else if ((j even) and (k odd)) then
626 c* begin row at second point, i=3 (i odd), or ioff = 1
627 c* else if ((j odd) and (k odd)) then
628 c* begin row at first point, i=2 (i even), or ioff = 0
629 c* endif
630 c* then: begin row at: i=2+ioff
631 c*
632 c* the appropriate ioff function for the red points is then:
633 c* ioff = dabs(mod(j,2)-mod(k,2))
634 c*
635 c* the appropriate ioff function for the black points is then:
636 c* ioff = 1 - dabs(mod(j,2)-mod(k,2))
637 c*
638 c*
639 c* alternatively, the red points are:
640 c*
641 c* those whose indices add up to an even number.
642 c* to see this, consider that all surrounding points are
643 c* only one index different, hence the sum will differ by one.
644 c* thus, if a given point has an even sum, then the surrounding
645 c* points will have an odd sum.
646 c*
647 c* thus, the black points are:
648 c*
649 c* therefore those whose indices add up to an odd number.
650 c*
651 c* algorithm parameters:
652 c* ---------------------
653 c*
654 c* U0 = initial guess
655 c* Llin = matrix representing linear part of equations
656 c* F = vector representing rhs of equations
657 c* nx = number of unknowns
658 c* itmax = number of iterations to perform
659 c* c_scal = function representing nonlinear part of equations
660 c* dc_scal = function representing derivative of nonlinear
661 c* part of equations
662 c* U = final result
663 c*
664 c* algorithm idea:
665 c* ---------------
666 c*
667 c* the ith nonlinear equation is represented as:
668 c*
669 c* N(UU) = AA * UU + c_scal(UU) - FF
670 c*
671 c* which is solved for UU using newton's method, where:
672 c*
673 c* AA = ith diagonal entry of Llin
674 c* FF = F(i), minus the nondiag of ith eqn of Llin times current U
675 c* UU = the unknown, initialized to U(i)
676 c*
677 c* algorithm (matlab language):
678 c* ----------------------------
679 c*
680 c* function [U,iterss,errors] = Zgs(U0,UTRUE,a,b,Llin,F,nx,tol,itmax,stp,plt);
681 c* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
682 c* %%% nonlinear gauss-seidel-newton iteration (ortega-rheinboldt, page 219)
683 c* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
684 c*
685 c* %%% initial guess
686 c* iters = 0;
687 c* error = 1.0d0;
688 c* U = U0;
689 c* Lnon = c_vec(b,U,nx);
690 c* N = Llin * U + Lnon - F ;
691 c* denom = 1.0d0; if (stp == 1) denom = norm(N); end;
692 c*
693 c* %%% loop
694 c* while ((error > tol) & (iters < itmax))
695 c* iters = iters + 1;
696 c*
697 c* %%% save old guys
698 c* Lnon_OLD = Lnon;
699 c* U_OLD = U;
700 c*
701 c* %%% relax each unknown
702 c* for i = 1:nx
703 c*
704 c* %%% define the 1d rhs
705 c* FF = F(i) - Llin(i,1:nx) * U + Llin(i,i) * U(i);
706 c* AA = Llin(i,i);
707 c* UU = U(i);
708 c* BB = b(i);
709 c*
710 c* %%% perform 1d newton's method
711 c* %%% note that in the linear case, this is equivalent to: FF/AA
712 c* nitmax = 10;
713 c* ntol = 1.0e-7;
714 c* niters = 0;
715 c* N = AA * UU + c_scal(BB,UU) - FF ;
716 c* ndenom = N;
717 c* if (ndenom == 0)
718 c* ndenom = 1;
719 c* end;
720 c* nerror = N / ndenom;
721 c*
722 c* while ((nerror > ntol) & (niters < nitmax))
723 c* niters = niters + 1;
724 c*
725 c* %%% construct jacobian matrix of N
726 c* DN = AA + dc_scal(BB,UU);
727 c*
728 c* %%% solve linear system
729 c* change = - N / DN;
730 c*
731 c* %%% update solution
732 c* UU = UU + change;
733 c*
734 c* %%% construct nonlinear residual
735 c* N = AA * UU + c_scal(BB,UU) - FF ;
736 c*
737 c* %%% compute residual
738 c* nerror = N / ndenom;
739 c* end
740 c*
741 c* %%% update the unknown
742 c* U(i) = UU;
743 c* end
744 c*
745 c* %%% new residual %%%
746 c* Lnon = c_vec(b,U,nx);
747 c* N = Llin * U + Lnon - F ;
748 c* delta = zeros(U);
749 c* delta_s = zeros(U);
750 c*
751 c* end
752 c*
753 c* author: michael holst
754 c* *********************************************************************
755 implicit none
756 integer ipc(*),itmax,iters,iresid,iadjoint,nx,ny,nz
757 integer i,j,k,ioff
758 double precision omega,errtol
759 double precision rpc(*),oE(nx,ny,nz),oN(nx,ny,nz),uC(nx,ny,nz)
760 double precision fc(nx,ny,nz),oC(nx,ny,nz),cc(nx,ny,nz)
761 double precision x(nx,ny,nz),w1(nx,ny,nz),w2(nx,ny,nz)
762 double precision r(nx,ny,nz)
763 double precision c_scal,dc_scal
764 double precision UU,AA,BB,FF,zNN,DzNN,zntol,zndenom,znerror
765 double precision change
766 integer nitmax,niters,ifail_tol,ipkey
767 c*
768 cmdir 0 0
769 c*
770 c* *** nonlinear iteration tolerance and itmax ***
771 nitmax = 10
772 zntol = 1.0e-5
773 ifail_tol = 0
774 ipkey = ipc(10)
775 c*
776 c* *** do the gauss-seidel iteration itmax times ***
777 do 30 iters = 1, itmax
778 c*
779 c* *** do the red points ***
780 cmdir 3 1
781 do 10 k=2,nz-1
782 cmdir 3 2
783 do 11 j=2,ny-1
784 CZZZ ioff = mod((j+k+2),2)
785 ioff = (1-iadjoint)*mod((j+k+2),2)
786 2 + iadjoint *(1-mod((j+k+2),2))
787 cmdir 3 3
788 do 12 i=2+ioff,nx-1, 2
789 c*
790 c* *** for this unknown, do a 1d newton's method
791 c*
792 c* *** determine the 1d equation
793 UU = x(i,j,k)
794 AA = oC(i,j,k)
795 BB = cc(i,j,k)
796 FF = fc(i,j,k)
797 2 + oN(i,j,k) * x(i,j+1,k)
798 3 + oN(i,j-1,k) * x(i,j-1,k)
799 4 + oE(i,j,k) * x(i+1,j,k)
800 5 + oE(i-1,j,k) * x(i-1,j,k)
801 6 + uC(i,j,k-1) * x(i,j,k-1)
802 7 + uC(i,j,k) * x(i,j,k+1)
803 c*
804 c* *** evaluate residual of 1d system
805 zNN = AA * UU + c_scal(BB,UU,ipkey) - FF
806 c*
807 c* *** setup for newton's method
808 niters = 0
809 zndenom = zNN
810 if (zndenom .eq. 0.0d0) zndenom = 1.0d0
811 znerror = zNN / zndenom
812 c*
813 c* *** the 1d newton's method ***
814 if (znerror .lt. zntol) goto 17
815 if (niters .gt. nitmax) goto 16
816 15 continue
817 niters = niters + 1
818 c*
819 c* *** construct jacobian matrix of NN ***
820 DzNN = AA + dc_scal(BB,UU,ipkey)
821 c*
822 c* *** solve the linear system ***
823 change = - zNN / DzNN
824 c*
825 c* *** update the solution ***
826 UU = UU + change
827 c*
828 c* *** evaluate residual of 1d system ***
829 zNN = AA * UU + c_scal(BB,UU,ipkey) - FF
830 c*
831 c* *** compute error ***
832 znerror = zNN / zndenom
833 c*
834 c* *** tolerance and itmax check ***
835 if (znerror .lt. zntol) goto 17
836 if (niters .gt. nitmax) goto 16
837 c*
838 c* *** loop ***
839 goto 15
840 c*
841 c* *** tolerance not reached ***
842 16 continue
843 ifail_tol = ifail_tol + 1
844 c*
845 c* *** tolerance reached ***
846 17 continue
847 c*
848 c* *** newton's method complete -- update solution value ***
849 x(i,j,k) = UU
850 12 continue
851 11 continue
852 10 continue
853 c*
854 c* *** do the black points ***
855 cmdir 3 1
856 do 20 k=2,nz-1
857 cmdir 3 2
858 do 21 j=2,ny-1
859 CZZZ ioff = 1-mod((j+k+2),2)
860 ioff = iadjoint *mod((j+k+2),2)
861 2 + (1-iadjoint)*(1-mod((j+k+2),2))
862 cmdir 3 3
863 do 22 i=2+ioff,nx-1, 2
864 c*
865 c* *** for this unknown, do a 1d newton's method
866 c*
867 c* *** determine the 1d equation
868 UU = x(i,j,k)
869 AA = oC(i,j,k)
870 BB = cc(i,j,k)
871 FF = fc(i,j,k)
872 2 + oN(i,j,k) * x(i,j+1,k)
873 3 + oN(i,j-1,k) * x(i,j-1,k)
874 4 + oE(i,j,k) * x(i+1,j,k)
875 5 + oE(i-1,j,k) * x(i-1,j,k)
876 6 + uC(i,j,k-1) * x(i,j,k-1)
877 7 + uC(i,j,k) * x(i,j,k+1)
878 c*
879 c* *** evaluate residual of 1d system
880 zNN = AA * UU + c_scal(BB,UU,ipkey) - FF
881 c*
882 c* *** setup for newton's method
883 niters = 0
884 zndenom = zNN
885 if (zndenom .eq. 0.0d0) zndenom = 1.0d0
886 znerror = zNN / zndenom
887 c*
888 c* *** the 1d newton's method ***
889 if (znerror .lt. zntol) goto 27
890 if (niters .gt. nitmax) goto 26
891 25 continue
892 niters = niters + 1
893 c*
894 c* *** construct jacobian matrix of NN ***
895 DzNN = AA + dc_scal(BB,UU,ipkey)
896 c*
897 c* *** solve the linear system ***
898 change = - zNN / DzNN
899 c*
900 c* *** update the solution ***
901 UU = UU + change
902 c*
903 c* *** evaluate residual of 1d system ***
904 zNN = AA * UU + c_scal(BB,UU,ipkey) - FF
905 c*
906 c* *** compute error ***
907 znerror = zNN / zndenom
908 c*
909 c* *** tolerance and itmax check ***
910 if (znerror .lt. zntol) goto 27
911 if (niters .gt. nitmax) goto 26
912 c*
913 c* *** loop ***
914 goto 25
915 c*
916 c* *** tolerance not reached ***
917 26 continue
918 ifail_tol = ifail_tol + 1
919 c*
920 c* *** tolerance reached ***
921 27 continue
922 c*
923 c* *** newton's method complete -- update solution value ***
924 x(i,j,k) = UU
925 22 continue
926 21 continue
927 20 continue
928 c*
929 c* *** main loop ***
930 30 continue
931 c*
932 c* *** if specified, return the new residual as well ***
933 if (iresid .eq. 1) then
934 call nmresid7_1s(nx,ny,nz,ipc,rpc,oC,cc,fc,oE,oN,uC,x,r,w1)
935 endif
936 c*
937 c* *** messages ***
938 if (ifail_tol .gt. 0) then
939 print*,'% NGSRB: 1d newton tolerance failures: ',ifail_tol
940 endif
941 c*
942 c* *** return and end ***
943 return
944 end