Debian Codegraph @

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

raw
1 c* ///////////////////////////////////////////////////////////////////////////
2 c* @file nwjd.f
3 c* @author Michael Holst
4 c* @brief Nonlinear weighted Jacobi iteration.
5 c* @version $Id: nwjd.f,v 1.2 2010/08/12 05:45:39 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 nwjac(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 nwjac7(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 nwjac27(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*,'% NWJAC: invalid stencil type given...'
94 endif
95 c*
96 c* *** return and end ***
97 return
98 end
99 subroutine nwjac7(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* nonlinear weighted jacobi-newton iteration (ortega-rheinboldt, page 220)
108 c*
109 c* algorithm parameters:
110 c* ---------------------
111 c*
112 c* U0 = initial guess
113 c* Llin = matrix representing linear part of equations
114 c* F = vector representing rhs of equations
115 c* nx = number of unknowns
116 c* itmax = number of iterations to perform
117 c* c_scal = function representing nonlinear part of equations
118 c* dc_scal = function representing derivative of nonlinear
119 c* part of equations
120 c* U = final result
121 c*
122 c* algorithm idea:
123 c* ---------------
124 c*
125 c* the ith nonlinear equation is represented as:
126 c*
127 c* N(UU) = AA * UU + c_scal(UU) - FF
128 c*
129 c* which is solved for UU using newton's method, where:
130 c*
131 c* AA = ith diagonal entry of Llin
132 c* FF = F(i), minus the nondiag of ith eqn of Llin times current U
133 c* UU = the unknown, initialized to U(i)
134 c*
135 c* algorithm (matlab language):
136 c* ----------------------------
137 c*
138 c* function [U] = wjac_go(U0,Llin,F,nx,itmax);
139 c*
140 c* omega = 0.5;
141 c* U = U0;
142 c* iters = 0;
143 c* while (iters < itmax)
144 c* iters = iters + 1;
145 c*
146 c* %%% relax each unknown
147 c* U1 = U;
148 c* for i = 1:nx
149 c*
150 c* %%% define the 1d rhs
151 c* FF = F(i) - Llin(i,1:nx) * U1 + Llin(i,i) * U1(i);
152 c* AA = Llin(i,i);
153 c* UU = U1(i);
154 c* BB = b(i);
155 c*
156 c* %%% perform 1d newton's method
157 c* %%% note that in the linear case, this is equivalent to: FF/AA
158 c* nitmax = 50;
159 c* ntol = 1.0e-12;
160 c* niters = 0;
161 c* N = AA * UU + c_scal(BB,UU) - FF ;
162 c* ndenom = N;
163 c* if (ndenom == 0)
164 c* ndenom = 1;
165 c* end;
166 c* nerror = N / ndenom;
167 c*
168 c* while ((nerror > ntol) & (niters < nitmax))
169 c* niters = niters + 1;
170 c*
171 c* %%% construct jacobian matrix of N
172 c* DN = AA + dc_scal(BB,UU);
173 c*
174 c* %%% solve linear system
175 c* change = - N / DN;
176 c*
177 c* %%% update solution
178 c* UU = UU + change;
179 c*
180 c* %%% construct nonlinear residual
181 c* N = AA * UU + c_scal(BB,UU) - FF ;
182 c*
183 c* %%% compute residual
184 c* nerror = N / ndenom;
185 c* end
186 c*
187 c* %%% update the unknown
188 c* U(i) = UU;
189 c* end
190 c*
191 c* %%% update the vector of unknowns
192 c* U = U1 + omega * (U - U1);
193 c* end
194 c*
195 c* author: michael holst
196 c* *********************************************************************
197 implicit none
198 integer ipc(*),itmax,iters,iresid,iadjoint,nx,ny,nz
199 integer i,j,k
200 double precision omega,errtol,fac
201 double precision rpc(*),oE(nx,ny,nz),oN(nx,ny,nz),uC(nx,ny,nz)
202 double precision fc(nx,ny,nz),oC(nx,ny,nz),cc(nx,ny,nz)
203 double precision x(nx,ny,nz),w1(nx,ny,nz),w2(nx,ny,nz),r(nx,ny,nz)
204 double precision c_scal,dc_scal
205 double precision UU,AA,BB,FF,zNN,DzNN,zntol,zndenom,znerror
206 double precision change
207 integer nitmax,niters,ifail_tol,ipkey
208 c*
209 cmdir 0 0
210 c*
211 c* *** nonlinear iteration tolerance and itmax ***
212 nitmax = 10
213 zntol = 1.0e-5
214 ifail_tol = 0
215 ipkey = ipc(10)
216 c*
217 c* *** do the jacobi iteration itmax times ***
218 do 30 iters = 1, itmax
219 c*
220 c* *** do it ***
221 cmdir 3 1
222 do 10 k=2,nz-1
223 cmdir 3 2
224 do 11 j=2,ny-1
225 cmdir 3 3
226 do 12 i=2,nx-1
227 c*
228 c* *** for this unknown, do a 1d newton's method
229 c*
230 c* *** determine the 1d equation
231 UU = x(i,j,k)
232 AA = oC(i,j,k)
233 BB = cc(i,j,k)
234 FF = fc(i,j,k)
235 2 + oN(i,j,k) * x(i,j+1,k)
236 3 + oN(i,j-1,k) * x(i,j-1,k)
237 4 + oE(i,j,k) * x(i+1,j,k)
238 5 + oE(i-1,j,k) * x(i-1,j,k)
239 6 + uC(i,j,k-1) * x(i,j,k-1)
240 7 + uC(i,j,k) * x(i,j,k+1)
241 c*
242 c* *** evaluate residual of 1d system
243 zNN = AA * UU + c_scal(BB,UU,ipkey) - FF
244 c*
245 c* *** setup for newton's method
246 niters = 0
247 zndenom = zNN
248 if (zndenom .eq. 0.0d0) zndenom = 1.0d0
249 znerror = zNN / zndenom
250 c*
251 c* *** the 1d newton's method ***
252 if (znerror .lt. zntol) goto 17
253 if (niters .gt. nitmax) goto 16
254 15 continue
255 niters = niters + 1
256 c*
257 c* *** construct jacobian matrix of NN ***
258 DzNN = AA + dc_scal(BB,UU,ipkey)
259 c*
260 c* *** solve the linear system ***
261 change = - zNN / DzNN
262 c*
263 c* *** update the solution ***
264 UU = UU + change
265 c*
266 c* *** evaluate residual of 1d system ***
267 zNN = AA * UU + c_scal(BB,UU,ipkey) - FF
268 c*
269 c* *** compute error ***
270 znerror = zNN / zndenom
271 c*
272 c* *** tolerance and itmax check ***
273 if (znerror .lt. zntol) goto 17
274 if (niters .gt. nitmax) goto 16
275 c*
276 c* *** loop ***
277 goto 15
278 c*
279 c* *** tolerance not reached ***
280 16 continue
281 ifail_tol = ifail_tol + 1
282 c*
283 c* *** tolerance reached ***
284 17 continue
285 c*
286 c* *** newton's method complete -- update solution value ***
287 w1(i,j,k) = UU
288 12 continue
289 11 continue
290 10 continue
291 c*
292 c* *** copy temp back to solution, with over-relaxation ***
293 fac = 1.0d0 - omega
294 cmdir 3 1
295 do 20 k=2,nz-1
296 cmdir 3 2
297 do 21 j=2,ny-1
298 cmdir 3 3
299 do 22 i=2,nx-1
300 x(i,j,k) = omega*w1(i,j,k) + fac*x(i,j,k)
301 22 continue
302 21 continue
303 20 continue
304 c*
305 c* *** main loop ***
306 30 continue
307 c*
308 c* *** if specified, return the new residual as well ***
309 if (iresid .eq. 1) then
310 call nmresid7_1s(nx,ny,nz,ipc,rpc,oC,cc,fc,oE,oN,uC,x,r,w1)
311 endif
312 c*
313 c* *** messages ***
314 if (ifail_tol .gt. 0) then
315 print*,'% NWJAC: 1d newton tolerance failures: ',ifail_tol
316 endif
317 c*
318 c* *** return and end ***
319 return
320 end
321 subroutine nwjac27(nx,ny,nz,ipc,rpc,
322 2 oC,cc,fc,
323 3 oE,oN,uC,oNE,oNW,uE,uW,uN,uS,uNE,uNW,uSE,uSW,
324 4 x,w1,w2,r,
325 5 itmax,iters,errtol,omega,iresid,iadjoint)
326 c* *********************************************************************
327 c* purpose:
328 c*
329 c* nonlinear weighted jacobi-newton iteration (ortega-rheinboldt, page 220)
330 c*
331 c* algorithm parameters:
332 c* ---------------------
333 c*
334 c* U0 = initial guess
335 c* Llin = matrix representing linear part of equations
336 c* F = vector representing rhs of equations
337 c* nx = number of unknowns
338 c* itmax = number of iterations to perform
339 c* c_scal = function representing nonlinear part of equations
340 c* dc_scal = function representing derivative of nonlinear
341 c* part of equations
342 c* U = final result
343 c*
344 c* algorithm idea:
345 c* ---------------
346 c*
347 c* the ith nonlinear equation is represented as:
348 c*
349 c* N(UU) = AA * UU + c_scal(UU) - FF
350 c*
351 c* which is solved for UU using newton's method, where:
352 c*
353 c* AA = ith diagonal entry of Llin
354 c* FF = F(i), minus the nondiag of ith eqn of Llin times current U
355 c* UU = the unknown, initialized to U(i)
356 c*
357 c* algorithm (matlab language):
358 c* ----------------------------
359 c*
360 c* function [U] = wjac_go(U0,Llin,F,nx,itmax);
361 c*
362 c* omega = 0.5;
363 c* U = U0;
364 c* iters = 0;
365 c* while (iters < itmax)
366 c* iters = iters + 1;
367 c*
368 c* %%% relax each unknown
369 c* U1 = U;
370 c* for i = 1:nx
371 c*
372 c* %%% define the 1d rhs
373 c* FF = F(i) - Llin(i,1:nx) * U1 + Llin(i,i) * U1(i);
374 c* AA = Llin(i,i);
375 c* UU = U1(i);
376 c* BB = b(i);
377 c*
378 c* %%% perform 1d newton's method
379 c* %%% note that in the linear case, this is equivalent to: FF/AA
380 c* nitmax = 50;
381 c* ntol = 1.0e-12;
382 c* niters = 0;
383 c* N = AA * UU + c_scal(BB,UU) - FF ;
384 c* ndenom = N;
385 c* if (ndenom == 0)
386 c* ndenom = 1;
387 c* end;
388 c* nerror = N / ndenom;
389 c*
390 c* while ((nerror > ntol) & (niters < nitmax))
391 c* niters = niters + 1;
392 c*
393 c* %%% construct jacobian matrix of N
394 c* DN = AA + dc_scal(BB,UU);
395 c*
396 c* %%% solve linear system
397 c* change = - N / DN;
398 c*
399 c* %%% update solution
400 c* UU = UU + change;
401 c*
402 c* %%% construct nonlinear residual
403 c* N = AA * UU + c_scal(BB,UU) - FF ;
404 c*
405 c* %%% compute residual
406 c* nerror = N / ndenom;
407 c* end
408 c*
409 c* %%% update the unknown
410 c* U(i) = UU;
411 c* end
412 c*
413 c* %%% update the vector of unknowns
414 c* U = U1 + omega * (U - U1);
415 c* end
416 c*
417 c* author: michael holst
418 c* *********************************************************************
419 implicit none
420 integer ipc(*),itmax,iters,iresid,iadjoint,nx,ny,nz
421 integer i,j,k
422 double precision omega,errtol,fac
423 double precision rpc(*),oE(nx,ny,nz),oN(nx,ny,nz),uC(nx,ny,nz)
424 double precision oNE(nx,ny,nz),oNW(nx,ny,nz),uE(nx,ny,nz)
425 double precision uW(nx,ny,nz),uN(nx,ny,nz),uS(nx,ny,nz)
426 double precision uNE(nx,ny,nz),uNW(nx,ny,nz),uSE(nx,ny,nz)
427 double precision uSW(nx,ny,nz)
428 double precision fc(nx,ny,nz),oC(nx,ny,nz),cc(nx,ny,nz)
429 double precision x(nx,ny,nz),w1(nx,ny,nz),w2(nx,ny,nz)
430 double precision r(nx,ny,nz)
431 double precision c_scal,dc_scal
432 double precision UU,AA,BB,FF,zNN,DzNN,zntol,zndenom,znerror
433 double precision change
434 integer nitmax,niters,ifail_tol,ipkey
435 double precision tmpO,tmpU,tmpD
436 c*
437 cmdir 0 0
438 c*
439 c* *** nonlinear iteration tolerance and itmax ***
440 nitmax = 10
441 zntol = 1.0e-5
442 ifail_tol = 0
443 ipkey = ipc(10)
444 c*
445 c* *** do the jacobi iteration itmax times ***
446 do 30 iters = 1, itmax
447 c*
448 c* *** do it ***
449 cmdir 3 1
450 do 10 k=2,nz-1
451 cmdir 3 2
452 do 11 j=2,ny-1
453 cmdir 3 3
454 do 12 i=2,nx-1
455 c*
456 c* *** for this unknown, do a 1d newton's method
457 c*
458 c* *** determine the 1d equation
459 UU = x(i,j,k)
460 AA = oC(i,j,k)
461 BB = cc(i,j,k)
462 tmpO =
463 2 + oN(i,j,k) * x(i,j+1,k)
464 3 + oN(i,j-1,k) * x(i,j-1,k)
465 4 + oE(i,j,k) * x(i+1,j,k)
466 5 + oE(i-1,j,k) * x(i-1,j,k)
467 6 + oNE(i,j,k) * x(i+1,j+1,k)
468 7 + oNW(i,j,k) * x(i-1,j+1,k)
469 8 + oNW(i+1,j-1,k) * x(i+1,j-1,k)
470 9 + oNE(i-1,j-1,k) * x(i-1,j-1,k)
471 tmpU =
472 2 + uC(i,j,k) * x(i,j,k+1)
473 3 + uN(i,j,k) * x(i,j+1,k+1)
474 4 + uS(i,j,k) * x(i,j-1,k+1)
475 5 + uE(i,j,k) * x(i+1,j,k+1)
476 6 + uW(i,j,k) * x(i-1,j,k+1)
477 7 + uNE(i,j,k) * x(i+1,j+1,k+1)
478 8 + uNW(i,j,k) * x(i-1,j+1,k+1)
479 9 + uSE(i,j,k) * x(i+1,j-1,k+1)
480 9 + uSW(i,j,k) * x(i-1,j-1,k+1)
481 tmpD =
482 2 + uC(i,j,k-1) * x(i,j,k-1)
483 3 + uS(i,j+1,k-1) * x(i,j+1,k-1)
484 4 + uN(i,j-1,k-1) * x(i,j-1,k-1)
485 5 + uW(i+1,j,k-1) * x(i+1,j,k-1)
486 6 + uE(i-1,j,k-1) * x(i-1,j,k-1)
487 7 + uSW(i+1,j+1,k-1) * x(i+1,j+1,k-1)
488 8 + uSE(i-1,j+1,k-1) * x(i-1,j+1,k-1)
489 9 + uNW(i+1,j-1,k-1) * x(i+1,j-1,k-1)
490 9 + uNE(i-1,j-1,k-1) * x(i-1,j-1,k-1)
491 FF = fc(i,j,k) + tmpO + tmpU + tmpD
492 c*
493 c* *** evaluate residual of 1d system
494 zNN = AA * UU + c_scal(BB,UU,ipkey) - FF
495 c*
496 c* *** setup for newton's method
497 niters = 0
498 zndenom = zNN
499 if (zndenom .eq. 0.0d0) zndenom = 1.0d0
500 znerror = zNN / zndenom
501 c*
502 c* *** the 1d newton's method ***
503 if (znerror .lt. zntol) goto 17
504 if (niters .gt. nitmax) goto 16
505 15 continue
506 niters = niters + 1
507 c*
508 c* *** construct jacobian matrix of NN ***
509 DzNN = AA + dc_scal(BB,UU,ipkey)
510 c*
511 c* *** solve the linear system ***
512 change = - zNN / DzNN
513 c*
514 c* *** update the solution ***
515 UU = UU + change
516 c*
517 c* *** evaluate residual of 1d system ***
518 zNN = AA * UU + c_scal(BB,UU,ipkey) - FF
519 c*
520 c* *** compute error ***
521 znerror = zNN / zndenom
522 c*
523 c* *** tolerance and itmax check ***
524 if (znerror .lt. zntol) goto 17
525 if (niters .gt. nitmax) goto 16
526 c*
527 c* *** loop ***
528 goto 15
529 c*
530 c* *** tolerance not reached ***
531 16 continue
532 ifail_tol = ifail_tol + 1
533 c*
534 c* *** tolerance reached ***
535 17 continue
536 c*
537 c* *** newton's method complete -- update solution value ***
538 w1(i,j,k) = UU
539 12 continue
540 11 continue
541 10 continue
542 c*
543 c* *** copy temp back to solution, with over-relaxation ***
544 fac = 1.0d0 - omega
545 cmdir 3 1
546 do 20 k=2,nz-1
547 cmdir 3 2
548 do 21 j=2,ny-1
549 cmdir 3 3
550 do 22 i=2,nx-1
551 x(i,j,k) = omega*w1(i,j,k) + fac*x(i,j,k)
552 22 continue
553 21 continue
554 20 continue
555 c*
556 c* *** main loop ***
557 30 continue
558 c*
559 c* *** if specified, return the new residual as well ***
560 if (iresid .eq. 1) then
561 call nmresid27_1s(nx,ny,nz,ipc,rpc,oC,cc,fc,
562 2 oE,oN,uC,oNE,oNW,uE,uW,uN,uS,uNE,uNW,uSE,uSW,
563 3 x,r,w1)
564 endif
565 c*
566 c* *** messages ***
567 if (ifail_tol .gt. 0) then
568 print*,'% NWJAC: 1d newton tolerance failures: ',ifail_tol
569 endif
570 c*
571 c* *** return and end ***
572 return
573 end