Debian Codegraph @

debian/external_deps/fetk/punc/src/pmg/ngsd.c

raw
1 /* ./src_f77/ngsd.f -- translated by f2c (version 20030320).
2 You must link the resulting object file with the libraries:
3 -lf2c -lm (in that order)
4 */
5
6 #include <punc/vf2c.h>
7
8 /* Table of constant values */
9
10 static integer c__9 = 9;
11 static integer c__1 = 1;
12 static integer c__3 = 3;
13
14 /* * /////////////////////////////////////////////////////////////////////////// */
15 /* * @file ngsd.f */
16 /* * @author Michael Holst */
17 /* * @brief Nonlinear Gauss-Seidel iteration. */
18 /* * @version $Id: ngsd.c,v 1.3 2010/08/12 05:45:25 fetk Exp $ */
19 /* * @attention */
20 /* * @verbatim */
21 /* * */
22 /* * PMG -- Parallel algebraic MultiGrid */
23 /* * Copyright (C) 1994-- Michael Holst. */
24 /* * */
25 /* * Michael Holst <[email protected]> */
26 /* * University of California, San Diego */
27 /* * Department of Mathematics, 5739 AP&M */
28 /* * 9500 Gilman Drive, Dept. 0112 */
29 /* * La Jolla, CA 92093-0112 USA */
30 /* * http://math.ucsd.edu/~mholst */
31 /* * */
32 /* * This file is part of PMG. */
33 /* * */
34 /* * PMG is free software; you can redistribute it and/or modify */
35 /* * it under the terms of the GNU General Public License as published by */
36 /* * the Free Software Foundation; either version 2 of the License, or */
37 /* * (at your option) any later version. */
38 /* * */
39 /* * PMG is distributed in the hope that it will be useful, */
40 /* * but WITHOUT ANY WARRANTY; without even the implied warranty of */
41 /* * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
42 /* * GNU General Public License for more details. */
43 /* * */
44 /* * You should have received a copy of the GNU General Public License */
45 /* * along with PMG; if not, write to the Free Software */
46 /* * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
47 /* * */
48 /* * Linking PMG statically or dynamically with other modules is making a */
49 /* * combined work based on PMG. Thus, the terms and conditions of the GNU */
50 /* * General Public License cover the whole combination. */
51 /* * */
52 /* * SPECIAL GPL EXCEPTION */
53 /* * In addition, as a special exception, the copyright holders of PMG */
54 /* * give you permission to combine the PMG program with free software */
55 /* * programs and libraries that are released under the GNU LGPL or with */
56 /* * code included in releases of ISIM, PMV, PyMOL, SMOL, VMD, and Vision. */
57 /* * Such combined software may be linked with PMG and redistributed together */
58 /* * in original or modified form as mere aggregation without requirement that */
59 /* * the entire work be under the scope of the GNU General Public License. */
60 /* * This special exception permission is also extended to any software listed */
61 /* * in the SPECIAL GPL EXCEPTION clauses by the FEtk and APBS libraries. */
62 /* * */
63 /* * Note that people who make modified versions of PMG are not obligated */
64 /* * to grant this special exception for their modified versions; it is */
65 /* * their choice whether to do so. The GNU General Public License gives */
66 /* * permission to release a modified version without this exception; this */
67 /* * exception also makes it possible to release a modified version which */
68 /* * carries forward this exception. */
69 /* * */
70 /* * @endverbatim */
71 /* * /////////////////////////////////////////////////////////////////////////// */
72 /* Subroutine */ int ngsrb_(integer *nx, integer *ny, integer *nz, integer *
73 ipc, doublereal *rpc, doublereal *ac, doublereal *cc, doublereal *fc,
74 doublereal *x, doublereal *w1, doublereal *w2, doublereal *r__,
75 integer *itmax, integer *iters, doublereal *errtol, doublereal *omega,
76 integer *iresid, integer *iadjoint)
77 {
78 /* System generated locals */
79 integer ac_dim1, ac_offset, cc_dim1, cc_dim2, cc_offset, fc_dim1, fc_dim2,
80 fc_offset, x_dim1, x_dim2, x_offset, w1_dim1, w1_dim2, w1_offset,
81 w2_dim1, w2_dim2, w2_offset, r_dim1, r_dim2, r_offset;
82
83 /* Builtin functions */
84 integer s_wsle(cilist *), do_lio(integer *, integer *, char *, ftnlen),
85 e_wsle(void);
86
87 /* Local variables */
88 extern /* Subroutine */ int ngsrb7_(integer *, integer *, integer *,
89 integer *, doublereal *, doublereal *, doublereal *, doublereal *,
90 doublereal *, doublereal *, doublereal *, doublereal *,
91 doublereal *, doublereal *, doublereal *, integer *, integer *,
92 doublereal *, doublereal *, integer *, integer *);
93 static integer numdia;
94 extern /* Subroutine */ int ngsrb27_(integer *, integer *, integer *,
95 integer *, doublereal *, doublereal *, doublereal *, doublereal *,
96 doublereal *, doublereal *, doublereal *, doublereal *,
97 doublereal *, doublereal *, doublereal *, doublereal *,
98 doublereal *, doublereal *, doublereal *, doublereal *,
99 doublereal *, doublereal *, doublereal *, doublereal *,
100 doublereal *, integer *, integer *, doublereal *, doublereal *,
101 integer *, integer *);
102
103 /* Fortran I/O blocks */
104 static cilist io___2 = { 0, 6, 0, 0, 0 };
105
106
107 /* * ********************************************************************* */
108 /* * purpose: */
109 /* * */
110 /* * call the fast diagonal iterative method. */
111 /* * */
112 /* * author: michael holst */
113 /* * ********************************************************************* */
114 /* * */
115 /* mdir 0 0 */
116 /* * */
117 /* * *** do in one step *** */
118 /* Parameter adjustments */
119 r_dim1 = *nx;
120 r_dim2 = *ny;
121 r_offset = 1 + r_dim1 * (1 + r_dim2);
122 r__ -= r_offset;
123 w2_dim1 = *nx;
124 w2_dim2 = *ny;
125 w2_offset = 1 + w2_dim1 * (1 + w2_dim2);
126 w2 -= w2_offset;
127 w1_dim1 = *nx;
128 w1_dim2 = *ny;
129 w1_offset = 1 + w1_dim1 * (1 + w1_dim2);
130 w1 -= w1_offset;
131 x_dim1 = *nx;
132 x_dim2 = *ny;
133 x_offset = 1 + x_dim1 * (1 + x_dim2);
134 x -= x_offset;
135 fc_dim1 = *nx;
136 fc_dim2 = *ny;
137 fc_offset = 1 + fc_dim1 * (1 + fc_dim2);
138 fc -= fc_offset;
139 cc_dim1 = *nx;
140 cc_dim2 = *ny;
141 cc_offset = 1 + cc_dim1 * (1 + cc_dim2);
142 cc -= cc_offset;
143 ac_dim1 = *nx * *ny * *nz;
144 ac_offset = 1 + ac_dim1;
145 ac -= ac_offset;
146 --ipc;
147 --rpc;
148
149 /* Function Body */
150 numdia = ipc[11];
151 if (numdia == 7) {
152 ngsrb7_(nx, ny, nz, &ipc[1], &rpc[1], &ac[ac_dim1 + 1], &cc[cc_offset]
153 , &fc[fc_offset], &ac[(ac_dim1 << 1) + 1], &ac[ac_dim1 * 3 +
154 1], &ac[(ac_dim1 << 2) + 1], &x[x_offset], &w1[w1_offset], &
155 w2[w2_offset], &r__[r_offset], itmax, iters, errtol, omega,
156 iresid, iadjoint);
157 } else if (numdia == 27) {
158 ngsrb27_(nx, ny, nz, &ipc[1], &rpc[1], &ac[ac_dim1 + 1], &cc[
159 cc_offset], &fc[fc_offset], &ac[(ac_dim1 << 1) + 1], &ac[
160 ac_dim1 * 3 + 1], &ac[(ac_dim1 << 2) + 1], &ac[ac_dim1 * 5 +
161 1], &ac[ac_dim1 * 6 + 1], &ac[ac_dim1 * 7 + 1], &ac[(ac_dim1
162 << 3) + 1], &ac[ac_dim1 * 9 + 1], &ac[ac_dim1 * 10 + 1], &ac[
163 ac_dim1 * 11 + 1], &ac[ac_dim1 * 12 + 1], &ac[ac_dim1 * 13 +
164 1], &ac[ac_dim1 * 14 + 1], &x[x_offset], &w1[w1_offset], &w2[
165 w2_offset], &r__[r_offset], itmax, iters, errtol, omega,
166 iresid, iadjoint);
167 } else {
168 s_wsle(&io___2);
169 do_lio(&c__9, &c__1, "% NGSRB: invalid stencil type given...", (
170 ftnlen)38);
171 e_wsle();
172 }
173 /* * */
174 /* * *** return and end *** */
175 return 0;
176 } /* ngsrb_ */
177
178 /* Subroutine */ int ngsrb7_(integer *nx, integer *ny, integer *nz, integer *
179 ipc, doublereal *rpc, doublereal *oc, doublereal *cc, doublereal *fc,
180 doublereal *oe, doublereal *on, doublereal *uc, doublereal *x,
181 doublereal *w1, doublereal *w2, doublereal *r__, integer *itmax,
182 integer *iters, doublereal *errtol, doublereal *omega, integer *
183 iresid, integer *iadjoint)
184 {
185 /* System generated locals */
186 integer oe_dim1, oe_dim2, oe_offset, on_dim1, on_dim2, on_offset, uc_dim1,
187 uc_dim2, uc_offset, fc_dim1, fc_dim2, fc_offset, oc_dim1,
188 oc_dim2, oc_offset, cc_dim1, cc_dim2, cc_offset, x_dim1, x_dim2,
189 x_offset, w1_dim1, w1_dim2, w1_offset, w2_dim1, w2_dim2,
190 w2_offset, r_dim1, r_dim2, r_offset, i__1, i__2, i__3, i__4, i__5,
191 i__6, i__7;
192
193 /* Builtin functions */
194 integer s_wsle(cilist *), do_lio(integer *, integer *, char *, ftnlen),
195 e_wsle(void);
196
197 /* Local variables */
198 static integer i__, j, k, ifail_tol__, i1, i2, j1, j2, k1, k2;
199 static doublereal aa, bb, ff, uu;
200 extern /* Subroutine */ int nmresid7_1s__(integer *, integer *, integer *,
201 integer *, doublereal *, doublereal *, doublereal *, doublereal *
202 , doublereal *, doublereal *, doublereal *, doublereal *,
203 doublereal *, doublereal *);
204 static doublereal znn, dznn;
205 static integer ipkey, istep;
206 static doublereal zntol;
207 extern doublereal c_scal__(doublereal *, doublereal *, integer *);
208 static doublereal change;
209 static integer nitmax, niters;
210 extern doublereal dc_scal__(doublereal *, doublereal *, integer *);
211 static doublereal zndenom, znerror;
212
213 /* Fortran I/O blocks */
214 static cilist io___27 = { 0, 6, 0, 0, 0 };
215
216
217 /* * ********************************************************************* */
218 /* * purpose: */
219 /* * */
220 /* * 7 diagonal nonlinear gauss-seidel routine. */
221 /* * */
222 /* * this routine applies the gauss-seidel operator or its */
223 /* * adjoint depending on the flag iadjoint. */
224 /* * */
225 /* * algorithm parameters: */
226 /* * --------------------- */
227 /* * */
228 /* * U0 = initial guess */
229 /* * Llin = matrix representing linear part of equations */
230 /* * F = vector representing rhs of equations */
231 /* * nx = number of unknowns */
232 /* * itmax = number of iterations to perform */
233 /* * c_scal = function representing nonlinear part of equations */
234 /* * dc_scal = function representing derivative of nonlinear */
235 /* * part of equations */
236 /* * U = final result */
237 /* * */
238 /* * algorithm idea: */
239 /* * --------------- */
240 /* * */
241 /* * the ith nonlinear equation is represented as: */
242 /* * */
243 /* * N(UU) = AA * UU + c_scal(UU) - FF */
244 /* * */
245 /* * which is solved for UU using newton's method, where: */
246 /* * */
247 /* * AA = ith diagonal entry of Llin */
248 /* * FF = F(i), minus the nondiag of ith eqn of Llin times current U */
249 /* * UU = the unknown, initialized to U(i) */
250 /* * */
251 /* * algorithm (matlab language): */
252 /* * ---------------------------- */
253 /* * */
254 /* * function [U,iterss,errors] = Zgs(U0,UTRUE,a,b,Llin,F,nx,tol,itmax,stp,plt); */
255 /* * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
256 /* * %%% nonlinear gauss-seidel-newton iteration (ortega-rheinboldt, page 219) */
257 /* * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
258 /* * */
259 /* * %%% initial guess */
260 /* * iters = 0; */
261 /* * error = 1.0d0; */
262 /* * U = U0; */
263 /* * Lnon = c_vec(b,U,nx); */
264 /* * N = Llin * U + Lnon - F ; */
265 /* * denom = 1.0d0; if (stp == 1) denom = norm(N); end; */
266 /* * */
267 /* * %%% loop */
268 /* * while ((error > tol) & (iters < itmax)) */
269 /* * iters = iters + 1; */
270 /* * */
271 /* * %%% save old guys */
272 /* * Lnon_OLD = Lnon; */
273 /* * U_OLD = U; */
274 /* * */
275 /* * %%% relax each unknown */
276 /* * for i = 1:nx */
277 /* * */
278 /* * %%% define the 1d rhs */
279 /* * FF = F(i) - Llin(i,1:nx) * U + Llin(i,i) * U(i); */
280 /* * AA = Llin(i,i); */
281 /* * UU = U(i); */
282 /* * BB = b(i); */
283 /* * */
284 /* * %%% perform 1d newton's method */
285 /* * %%% note that in the linear case, this is equivalent to: FF/AA */
286 /* * nitmax = 10; */
287 /* * ntol = 1.0e-7; */
288 /* * niters = 0; */
289 /* * N = AA * UU + c_scal(BB,UU) - FF ; */
290 /* * ndenom = N; */
291 /* * if (ndenom == 0) */
292 /* * ndenom = 1; */
293 /* * end; */
294 /* * nerror = N / ndenom; */
295 /* * */
296 /* * while ((nerror > ntol) & (niters < nitmax)) */
297 /* * niters = niters + 1; */
298 /* * */
299 /* * %%% construct jacobian matrix of N */
300 /* * DN = AA + dc_scal(BB,UU); */
301 /* * */
302 /* * %%% solve linear system */
303 /* * change = - N / DN; */
304 /* * */
305 /* * %%% update solution */
306 /* * UU = UU + change; */
307 /* * */
308 /* * %%% construct nonlinear residual */
309 /* * N = AA * UU + c_scal(BB,UU) - FF ; */
310 /* * */
311 /* * %%% compute residual */
312 /* * nerror = N / ndenom; */
313 /* * end */
314 /* * */
315 /* * %%% update the unknown */
316 /* * U(i) = UU; */
317 /* * end */
318 /* * */
319 /* * %%% new residual %%% */
320 /* * Lnon = c_vec(b,U,nx); */
321 /* * N = Llin * U + Lnon - F ; */
322 /* * delta = zeros(U); */
323 /* * delta_s = zeros(U); */
324 /* * */
325 /* * end */
326 /* * */
327 /* * author: michael holst */
328 /* * ********************************************************************* */
329 /* * */
330 /* mdir 0 0 */
331 /* * */
332 /* * *** nonlinear iteration tolerance and itmax *** */
333 /* Parameter adjustments */
334 r_dim1 = *nx;
335 r_dim2 = *ny;
336 r_offset = 1 + r_dim1 * (1 + r_dim2);
337 r__ -= r_offset;
338 w2_dim1 = *nx;
339 w2_dim2 = *ny;
340 w2_offset = 1 + w2_dim1 * (1 + w2_dim2);
341 w2 -= w2_offset;
342 w1_dim1 = *nx;
343 w1_dim2 = *ny;
344 w1_offset = 1 + w1_dim1 * (1 + w1_dim2);
345 w1 -= w1_offset;
346 x_dim1 = *nx;
347 x_dim2 = *ny;
348 x_offset = 1 + x_dim1 * (1 + x_dim2);
349 x -= x_offset;
350 uc_dim1 = *nx;
351 uc_dim2 = *ny;
352 uc_offset = 1 + uc_dim1 * (1 + uc_dim2);
353 uc -= uc_offset;
354 on_dim1 = *nx;
355 on_dim2 = *ny;
356 on_offset = 1 + on_dim1 * (1 + on_dim2);
357 on -= on_offset;
358 oe_dim1 = *nx;
359 oe_dim2 = *ny;
360 oe_offset = 1 + oe_dim1 * (1 + oe_dim2);
361 oe -= oe_offset;
362 fc_dim1 = *nx;
363 fc_dim2 = *ny;
364 fc_offset = 1 + fc_dim1 * (1 + fc_dim2);
365 fc -= fc_offset;
366 cc_dim1 = *nx;
367 cc_dim2 = *ny;
368 cc_offset = 1 + cc_dim1 * (1 + cc_dim2);
369 cc -= cc_offset;
370 oc_dim1 = *nx;
371 oc_dim2 = *ny;
372 oc_offset = 1 + oc_dim1 * (1 + oc_dim2);
373 oc -= oc_offset;
374 --ipc;
375 --rpc;
376
377 /* Function Body */
378 nitmax = 10;
379 zntol = 1e-5f;
380 ifail_tol__ = 0;
381 ipkey = ipc[10];
382 /* * */
383 /* * *** do the gauss-seidel iteration itmax times *** */
384 i1 = (1 - *iadjoint << 1) + *iadjoint * (*nx - 1);
385 i2 = (*iadjoint << 1) + (1 - *iadjoint) * (*nx - 1);
386 j1 = (1 - *iadjoint << 1) + *iadjoint * (*ny - 1);
387 j2 = (*iadjoint << 1) + (1 - *iadjoint) * (*ny - 1);
388 k1 = (1 - *iadjoint << 1) + *iadjoint * (*nz - 1);
389 k2 = (*iadjoint << 1) + (1 - *iadjoint) * (*nz - 1);
390 istep = -(*iadjoint) + (1 - *iadjoint);
391 i__1 = *itmax;
392 for (*iters = 1; *iters <= i__1; ++(*iters)) {
393 /* * */
394 /* * *** do the red points *** */
395 /* mdir 3 1 */
396 i__2 = k2;
397 i__3 = istep;
398 for (k = k1; i__3 < 0 ? k >= i__2 : k <= i__2; k += i__3) {
399 /* mdir 3 2 */
400 i__4 = j2;
401 i__5 = istep;
402 for (j = j1; i__5 < 0 ? j >= i__4 : j <= i__4; j += i__5) {
403 /* mdir 3 3 */
404 i__6 = i2;
405 i__7 = istep;
406 for (i__ = i1; i__7 < 0 ? i__ >= i__6 : i__ <= i__6; i__ +=
407 i__7) {
408 /* * */
409 /* * *** for this unknown, do a 1d newton's method */
410 /* * */
411 /* * *** determine the 1d equation */
412 uu = x[i__ + (j + k * x_dim2) * x_dim1];
413 aa = oc[i__ + (j + k * oc_dim2) * oc_dim1];
414 bb = cc[i__ + (j + k * cc_dim2) * cc_dim1];
415 ff = fc[i__ + (j + k * fc_dim2) * fc_dim1] + on[i__ + (j
416 + k * on_dim2) * on_dim1] * x[i__ + (j + 1 + k *
417 x_dim2) * x_dim1] + on[i__ + (j - 1 + k * on_dim2)
418 * on_dim1] * x[i__ + (j - 1 + k * x_dim2) *
419 x_dim1] + oe[i__ + (j + k * oe_dim2) * oe_dim1] *
420 x[i__ + 1 + (j + k * x_dim2) * x_dim1] + oe[i__ -
421 1 + (j + k * oe_dim2) * oe_dim1] * x[i__ - 1 + (j
422 + k * x_dim2) * x_dim1] + uc[i__ + (j + (k - 1) *
423 uc_dim2) * uc_dim1] * x[i__ + (j + (k - 1) *
424 x_dim2) * x_dim1] + uc[i__ + (j + k * uc_dim2) *
425 uc_dim1] * x[i__ + (j + (k + 1) * x_dim2) *
426 x_dim1];
427 /* * */
428 /* * *** evaluate residual of 1d system */
429 znn = aa * uu + c_scal__(&bb, &uu, &ipkey) - ff;
430 /* * */
431 /* * *** setup for newton's method */
432 niters = 0;
433 zndenom = znn;
434 if (zndenom == 0.) {
435 zndenom = 1.;
436 }
437 znerror = znn / zndenom;
438 /* * */
439 /* * *** the 1d newton's method *** */
440 if (znerror < zntol) {
441 goto L17;
442 }
443 if (niters > nitmax) {
444 goto L16;
445 }
446 L15:
447 ++niters;
448 /* * */
449 /* * *** construct jacobian matrix of NN *** */
450 dznn = aa + dc_scal__(&bb, &uu, &ipkey);
451 /* * */
452 /* * *** solve the linear system *** */
453 change = -znn / dznn;
454 /* * */
455 /* * *** update the solution *** */
456 uu += change;
457 /* * */
458 /* * *** evaluate residual of 1d system *** */
459 znn = aa * uu + c_scal__(&bb, &uu, &ipkey) - ff;
460 /* * */
461 /* * *** compute error *** */
462 znerror = znn / zndenom;
463 /* * */
464 /* * *** tolerance and itmax check *** */
465 if (znerror < zntol) {
466 goto L17;
467 }
468 if (niters > nitmax) {
469 goto L16;
470 }
471 /* * */
472 /* * *** loop *** */
473 goto L15;
474 /* * */
475 /* * *** tolerance not reached *** */
476 L16:
477 ++ifail_tol__;
478 /* * */
479 /* * *** tolerance reached *** */
480 L17:
481 /* * */
482 /* * *** newton's method complete -- update solution value *** */
483 x[i__ + (j + k * x_dim2) * x_dim1] = uu;
484 /* L12: */
485 }
486 /* L11: */
487 }
488 /* L10: */
489 }
490 /* * */
491 /* * *** main loop *** */
492 /* L30: */
493 }
494 /* * */
495 /* * *** if specified, return the new residual as well *** */
496 if (*iresid == 1) {
497 nmresid7_1s__(nx, ny, nz, &ipc[1], &rpc[1], &oc[oc_offset], &cc[
498 cc_offset], &fc[fc_offset], &oe[oe_offset], &on[on_offset], &
499 uc[uc_offset], &x[x_offset], &r__[r_offset], &w1[w1_offset]);
500 }
501 /* * */
502 /* * *** messages *** */
503 if (ifail_tol__ > 0) {
504 s_wsle(&io___27);
505 do_lio(&c__9, &c__1, "% NGSRB: 1d newton tolerance failures: ", (
506 ftnlen)42);
507 do_lio(&c__3, &c__1, (char *)&ifail_tol__, (ftnlen)sizeof(integer));
508 e_wsle();
509 }
510 /* * */
511 /* * *** return and end *** */
512 return 0;
513 } /* ngsrb7_ */
514
515 /* Subroutine */ int ngsrb27_(integer *nx, integer *ny, integer *nz, integer *
516 ipc, doublereal *rpc, doublereal *oc, doublereal *cc, doublereal *fc,
517 doublereal *oe, doublereal *on, doublereal *uc, doublereal *one,
518 doublereal *onw, doublereal *ue, doublereal *uw, doublereal *un,
519 doublereal *us, doublereal *une, doublereal *unw, doublereal *use,
520 doublereal *usw, doublereal *x, doublereal *w1, doublereal *w2,
521 doublereal *r__, integer *itmax, integer *iters, doublereal *errtol,
522 doublereal *omega, integer *iresid, integer *iadjoint)
523 {
524 /* System generated locals */
525 integer oe_dim1, oe_dim2, oe_offset, on_dim1, on_dim2, on_offset, uc_dim1,
526 uc_dim2, uc_offset, one_dim1, one_dim2, one_offset, onw_dim1,
527 onw_dim2, onw_offset, ue_dim1, ue_dim2, ue_offset, uw_dim1,
528 uw_dim2, uw_offset, un_dim1, un_dim2, un_offset, us_dim1, us_dim2,
529 us_offset, une_dim1, une_dim2, une_offset, unw_dim1, unw_dim2,
530 unw_offset, use_dim1, use_dim2, use_offset, usw_dim1, usw_dim2,
531 usw_offset, fc_dim1, fc_dim2, fc_offset, oc_dim1, oc_dim2,
532 oc_offset, cc_dim1, cc_dim2, cc_offset, x_dim1, x_dim2, x_offset,
533 w1_dim1, w1_dim2, w1_offset, w2_dim1, w2_dim2, w2_offset, r_dim1,
534 r_dim2, r_offset, i__1, i__2, i__3, i__4, i__5, i__6, i__7;
535
536 /* Builtin functions */
537 integer s_wsle(cilist *), do_lio(integer *, integer *, char *, ftnlen),
538 e_wsle(void);
539
540 /* Local variables */
541 static integer i__, j, k, ifail_tol__, i1, i2, j1, j2, k1, k2;
542 static doublereal aa, bb, ff, uu;
543 extern /* Subroutine */ int nmresid27_1s__(integer *, integer *, integer *
544 , integer *, doublereal *, doublereal *, doublereal *, doublereal
545 *, doublereal *, doublereal *, doublereal *, doublereal *,
546 doublereal *, doublereal *, doublereal *, doublereal *,
547 doublereal *, doublereal *, doublereal *, doublereal *,
548 doublereal *, doublereal *, doublereal *, doublereal *);
549 static doublereal znn, tmpd, dznn, tmpo, tmpu;
550 static integer ipkey, istep;
551 static doublereal zntol;
552 extern doublereal c_scal__(doublereal *, doublereal *, integer *);
553 static doublereal change;
554 static integer nitmax, niters;
555 extern doublereal dc_scal__(doublereal *, doublereal *, integer *);
556 static doublereal zndenom, znerror;
557
558 /* Fortran I/O blocks */
559 static cilist io___55 = { 0, 6, 0, 0, 0 };
560
561
562 /* * ********************************************************************* */
563 /* * purpose: */
564 /* * */
565 /* * 27 diagonal nonlinear gauss-seidel routine. */
566 /* * */
567 /* * this routine applies the gauss-seidel operator or its */
568 /* * adjoint depending on the flag iadjoint. */
569 /* * */
570 /* * algorithm parameters: */
571 /* * --------------------- */
572 /* * */
573 /* * U0 = initial guess */
574 /* * Llin = matrix representing linear part of equations */
575 /* * F = vector representing rhs of equations */
576 /* * nx = number of unknowns */
577 /* * itmax = number of iterations to perform */
578 /* * c_scal = function representing nonlinear part of equations */
579 /* * dc_scal = function representing derivative of nonlinear */
580 /* * part of equations */
581 /* * U = final result */
582 /* * */
583 /* * algorithm idea: */
584 /* * --------------- */
585 /* * */
586 /* * the ith nonlinear equation is represented as: */
587 /* * */
588 /* * N(UU) = AA * UU + c_scal(UU) - FF */
589 /* * */
590 /* * which is solved for UU using newton's method, where: */
591 /* * */
592 /* * AA = ith diagonal entry of Llin */
593 /* * FF = F(i), minus the nondiag of ith eqn of Llin times current U */
594 /* * UU = the unknown, initialized to U(i) */
595 /* * */
596 /* * algorithm (matlab language): */
597 /* * ---------------------------- */
598 /* * */
599 /* * function [U,iterss,errors] = Zgs(U0,UTRUE,a,b,Llin,F,nx,tol,itmax,stp,plt); */
600 /* * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
601 /* * %%% nonlinear gauss-seidel-newton iteration (ortega-rheinboldt, page 219) */
602 /* * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
603 /* * */
604 /* * %%% initial guess */
605 /* * iters = 0; */
606 /* * error = 1.0d0; */
607 /* * U = U0; */
608 /* * Lnon = c_vec(b,U,nx); */
609 /* * N = Llin * U + Lnon - F ; */
610 /* * denom = 1.0d0; if (stp == 1) denom = norm(N); end; */
611 /* * */
612 /* * %%% loop */
613 /* * while ((error > tol) & (iters < itmax)) */
614 /* * iters = iters + 1; */
615 /* * */
616 /* * %%% save old guys */
617 /* * Lnon_OLD = Lnon; */
618 /* * U_OLD = U; */
619 /* * */
620 /* * %%% relax each unknown */
621 /* * for i = 1:nx */
622 /* * */
623 /* * %%% define the 1d rhs */
624 /* * FF = F(i) - Llin(i,1:nx) * U + Llin(i,i) * U(i); */
625 /* * AA = Llin(i,i); */
626 /* * UU = U(i); */
627 /* * BB = b(i); */
628 /* * */
629 /* * %%% perform 1d newton's method */
630 /* * %%% note that in the linear case, this is equivalent to: FF/AA */
631 /* * nitmax = 10; */
632 /* * ntol = 1.0e-7; */
633 /* * niters = 0; */
634 /* * N = AA * UU + c_scal(BB,UU) - FF ; */
635 /* * ndenom = N; */
636 /* * if (ndenom == 0) */
637 /* * ndenom = 1; */
638 /* * end; */
639 /* * nerror = N / ndenom; */
640 /* * */
641 /* * while ((nerror > ntol) & (niters < nitmax)) */
642 /* * niters = niters + 1; */
643 /* * */
644 /* * %%% construct jacobian matrix of N */
645 /* * DN = AA + dc_scal(BB,UU); */
646 /* * */
647 /* * %%% solve linear system */
648 /* * change = - N / DN; */
649 /* * */
650 /* * %%% update solution */
651 /* * UU = UU + change; */
652 /* * */
653 /* * %%% construct nonlinear residual */
654 /* * N = AA * UU + c_scal(BB,UU) - FF ; */
655 /* * */
656 /* * %%% compute residual */
657 /* * nerror = N / ndenom; */
658 /* * end */
659 /* * */
660 /* * %%% update the unknown */
661 /* * U(i) = UU; */
662 /* * end */
663 /* * */
664 /* * %%% new residual %%% */
665 /* * Lnon = c_vec(b,U,nx); */
666 /* * N = Llin * U + Lnon - F ; */
667 /* * delta = zeros(U); */
668 /* * delta_s = zeros(U); */
669 /* * */
670 /* * end */
671 /* * */
672 /* * author: michael holst */
673 /* * ********************************************************************* */
674 /* * */
675 /* mdir 0 0 */
676 /* * */
677 /* * *** nonlinear iteration tolerance and itmax *** */
678 /* Parameter adjustments */
679 r_dim1 = *nx;
680 r_dim2 = *ny;
681 r_offset = 1 + r_dim1 * (1 + r_dim2);
682 r__ -= r_offset;
683 w2_dim1 = *nx;
684 w2_dim2 = *ny;
685 w2_offset = 1 + w2_dim1 * (1 + w2_dim2);
686 w2 -= w2_offset;
687 w1_dim1 = *nx;
688 w1_dim2 = *ny;
689 w1_offset = 1 + w1_dim1 * (1 + w1_dim2);
690 w1 -= w1_offset;
691 x_dim1 = *nx;
692 x_dim2 = *ny;
693 x_offset = 1 + x_dim1 * (1 + x_dim2);
694 x -= x_offset;
695 usw_dim1 = *nx;
696 usw_dim2 = *ny;
697 usw_offset = 1 + usw_dim1 * (1 + usw_dim2);
698 usw -= usw_offset;
699 use_dim1 = *nx;
700 use_dim2 = *ny;
701 use_offset = 1 + use_dim1 * (1 + use_dim2);
702 use -= use_offset;
703 unw_dim1 = *nx;
704 unw_dim2 = *ny;
705 unw_offset = 1 + unw_dim1 * (1 + unw_dim2);
706 unw -= unw_offset;
707 une_dim1 = *nx;
708 une_dim2 = *ny;
709 une_offset = 1 + une_dim1 * (1 + une_dim2);
710 une -= une_offset;
711 us_dim1 = *nx;
712 us_dim2 = *ny;
713 us_offset = 1 + us_dim1 * (1 + us_dim2);
714 us -= us_offset;
715 un_dim1 = *nx;
716 un_dim2 = *ny;
717 un_offset = 1 + un_dim1 * (1 + un_dim2);
718 un -= un_offset;
719 uw_dim1 = *nx;
720 uw_dim2 = *ny;
721 uw_offset = 1 + uw_dim1 * (1 + uw_dim2);
722 uw -= uw_offset;
723 ue_dim1 = *nx;
724 ue_dim2 = *ny;
725 ue_offset = 1 + ue_dim1 * (1 + ue_dim2);
726 ue -= ue_offset;
727 onw_dim1 = *nx;
728 onw_dim2 = *ny;
729 onw_offset = 1 + onw_dim1 * (1 + onw_dim2);
730 onw -= onw_offset;
731 one_dim1 = *nx;
732 one_dim2 = *ny;
733 one_offset = 1 + one_dim1 * (1 + one_dim2);
734 one -= one_offset;
735 uc_dim1 = *nx;
736 uc_dim2 = *ny;
737 uc_offset = 1 + uc_dim1 * (1 + uc_dim2);
738 uc -= uc_offset;
739 on_dim1 = *nx;
740 on_dim2 = *ny;
741 on_offset = 1 + on_dim1 * (1 + on_dim2);
742 on -= on_offset;
743 oe_dim1 = *nx;
744 oe_dim2 = *ny;
745 oe_offset = 1 + oe_dim1 * (1 + oe_dim2);
746 oe -= oe_offset;
747 fc_dim1 = *nx;
748 fc_dim2 = *ny;
749 fc_offset = 1 + fc_dim1 * (1 + fc_dim2);
750 fc -= fc_offset;
751 cc_dim1 = *nx;
752 cc_dim2 = *ny;
753 cc_offset = 1 + cc_dim1 * (1 + cc_dim2);
754 cc -= cc_offset;
755 oc_dim1 = *nx;
756 oc_dim2 = *ny;
757 oc_offset = 1 + oc_dim1 * (1 + oc_dim2);
758 oc -= oc_offset;
759 --ipc;
760 --rpc;
761
762 /* Function Body */
763 nitmax = 10;
764 zntol = 1e-5f;
765 ifail_tol__ = 0;
766 ipkey = ipc[10];
767 /* * */
768 /* * *** do the gauss-seidel iteration itmax times *** */
769 i1 = (1 - *iadjoint << 1) + *iadjoint * (*nx - 1);
770 i2 = (*iadjoint << 1) + (1 - *iadjoint) * (*nx - 1);
771 j1 = (1 - *iadjoint << 1) + *iadjoint * (*ny - 1);
772 j2 = (*iadjoint << 1) + (1 - *iadjoint) * (*ny - 1);
773 k1 = (1 - *iadjoint << 1) + *iadjoint * (*nz - 1);
774 k2 = (*iadjoint << 1) + (1 - *iadjoint) * (*nz - 1);
775 istep = -(*iadjoint) + (1 - *iadjoint);
776 i__1 = *itmax;
777 for (*iters = 1; *iters <= i__1; ++(*iters)) {
778 /* * */
779 /* * *** do all of the red points *** */
780 /* mdir 3 1 */
781 i__2 = k2;
782 i__3 = istep;
783 for (k = k1; i__3 < 0 ? k >= i__2 : k <= i__2; k += i__3) {
784 /* mdir 3 2 */
785 i__4 = j2;
786 i__5 = istep;
787 for (j = j1; i__5 < 0 ? j >= i__4 : j <= i__4; j += i__5) {
788 /* mdir 3 3 */
789 i__6 = i2;
790 i__7 = istep;
791 for (i__ = i1; i__7 < 0 ? i__ >= i__6 : i__ <= i__6; i__ +=
792 i__7) {
793 /* * */
794 /* * *** for this unknown, do a 1d newton's method */
795 /* * */
796 /* * *** determine the 1d equation */
797 uu = x[i__ + (j + k * x_dim2) * x_dim1];
798 aa = oc[i__ + (j + k * oc_dim2) * oc_dim1];
799 bb = cc[i__ + (j + k * cc_dim2) * cc_dim1];
800 tmpo = on[i__ + (j + k * on_dim2) * on_dim1] * x[i__ + (j
801 + 1 + k * x_dim2) * x_dim1] + on[i__ + (j - 1 + k
802 * on_dim2) * on_dim1] * x[i__ + (j - 1 + k *
803 x_dim2) * x_dim1] + oe[i__ + (j + k * oe_dim2) *
804 oe_dim1] * x[i__ + 1 + (j + k * x_dim2) * x_dim1]
805 + oe[i__ - 1 + (j + k * oe_dim2) * oe_dim1] * x[
806 i__ - 1 + (j + k * x_dim2) * x_dim1] + one[i__ + (
807 j + k * one_dim2) * one_dim1] * x[i__ + 1 + (j +
808 1 + k * x_dim2) * x_dim1] + onw[i__ + (j + k *
809 onw_dim2) * onw_dim1] * x[i__ - 1 + (j + 1 + k *
810 x_dim2) * x_dim1] + onw[i__ + 1 + (j - 1 + k *
811 onw_dim2) * onw_dim1] * x[i__ + 1 + (j - 1 + k *
812 x_dim2) * x_dim1] + one[i__ - 1 + (j - 1 + k *
813 one_dim2) * one_dim1] * x[i__ - 1 + (j - 1 + k *
814 x_dim2) * x_dim1];
815 tmpu = uc[i__ + (j + k * uc_dim2) * uc_dim1] * x[i__ + (j
816 + (k + 1) * x_dim2) * x_dim1] + un[i__ + (j + k *
817 un_dim2) * un_dim1] * x[i__ + (j + 1 + (k + 1) *
818 x_dim2) * x_dim1] + us[i__ + (j + k * us_dim2) *
819 us_dim1] * x[i__ + (j - 1 + (k + 1) * x_dim2) *
820 x_dim1] + ue[i__ + (j + k * ue_dim2) * ue_dim1] *
821 x[i__ + 1 + (j + (k + 1) * x_dim2) * x_dim1] + uw[
822 i__ + (j + k * uw_dim2) * uw_dim1] * x[i__ - 1 + (
823 j + (k + 1) * x_dim2) * x_dim1] + une[i__ + (j +
824 k * une_dim2) * une_dim1] * x[i__ + 1 + (j + 1 + (
825 k + 1) * x_dim2) * x_dim1] + unw[i__ + (j + k *
826 unw_dim2) * unw_dim1] * x[i__ - 1 + (j + 1 + (k +
827 1) * x_dim2) * x_dim1] + use[i__ + (j + k *
828 use_dim2) * use_dim1] * x[i__ + 1 + (j - 1 + (k +
829 1) * x_dim2) * x_dim1] + usw[i__ + (j + k *
830 usw_dim2) * usw_dim1] * x[i__ - 1 + (j - 1 + (k +
831 1) * x_dim2) * x_dim1];
832 tmpd = uc[i__ + (j + (k - 1) * uc_dim2) * uc_dim1] * x[
833 i__ + (j + (k - 1) * x_dim2) * x_dim1] + us[i__ +
834 (j + 1 + (k - 1) * us_dim2) * us_dim1] * x[i__ + (
835 j + 1 + (k - 1) * x_dim2) * x_dim1] + un[i__ + (j
836 - 1 + (k - 1) * un_dim2) * un_dim1] * x[i__ + (j
837 - 1 + (k - 1) * x_dim2) * x_dim1] + uw[i__ + 1 + (
838 j + (k - 1) * uw_dim2) * uw_dim1] * x[i__ + 1 + (
839 j + (k - 1) * x_dim2) * x_dim1] + ue[i__ - 1 + (j
840 + (k - 1) * ue_dim2) * ue_dim1] * x[i__ - 1 + (j
841 + (k - 1) * x_dim2) * x_dim1] + usw[i__ + 1 + (j
842 + 1 + (k - 1) * usw_dim2) * usw_dim1] * x[i__ + 1
843 + (j + 1 + (k - 1) * x_dim2) * x_dim1] + use[i__
844 - 1 + (j + 1 + (k - 1) * use_dim2) * use_dim1] *
845 x[i__ - 1 + (j + 1 + (k - 1) * x_dim2) * x_dim1]
846 + unw[i__ + 1 + (j - 1 + (k - 1) * unw_dim2) *
847 unw_dim1] * x[i__ + 1 + (j - 1 + (k - 1) * x_dim2)
848 * x_dim1] + une[i__ - 1 + (j - 1 + (k - 1) *
849 une_dim2) * une_dim1] * x[i__ - 1 + (j - 1 + (k -
850 1) * x_dim2) * x_dim1];
851 ff = fc[i__ + (j + k * fc_dim2) * fc_dim1] + tmpo + tmpu
852 + tmpd;
853 /* * */
854 /* * *** evaluate residual of 1d system */
855 znn = aa * uu + c_scal__(&bb, &uu, &ipkey) - ff;
856 /* * */
857 /* * *** setup for newton's method */
858 niters = 0;
859 zndenom = znn;
860 if (zndenom == 0.) {
861 zndenom = 1.;
862 }
863 znerror = znn / zndenom;
864 /* * */
865 /* * *** the 1d newton's method *** */
866 if (znerror < zntol) {
867 goto L17;
868 }
869 if (niters > nitmax) {
870 goto L16;
871 }
872 L15:
873 ++niters;
874 /* * */
875 /* * *** construct jacobian matrix of NN *** */
876 dznn = aa + dc_scal__(&bb, &uu, &ipkey);
877 /* * */
878 /* * *** solve the linear system *** */
879 change = -znn / dznn;
880 /* * */
881 /* * *** update the solution *** */
882 uu += change;
883 /* * */
884 /* * *** evaluate residual of 1d system *** */
885 znn = aa * uu + c_scal__(&bb, &uu, &ipkey) - ff;
886 /* * */
887 /* * *** compute error *** */
888 znerror = znn / zndenom;
889 /* * */
890 /* * *** tolerance and itmax check *** */
891 if (znerror < zntol) {
892 goto L17;
893 }
894 if (niters > nitmax) {
895 goto L16;
896 }
897 /* * */
898 /* * *** loop *** */
899 goto L15;
900 /* * */
901 /* * *** tolerance not reached *** */
902 L16:
903 ++ifail_tol__;
904 /* * */
905 /* * *** tolerance reached *** */
906 L17:
907 /* * */
908 /* * *** newton's method complete -- update solution value *** */
909 x[i__ + (j + k * x_dim2) * x_dim1] = uu;
910 /* L12: */
911 }
912 /* L11: */
913 }
914 /* L10: */
915 }
916 /* * */
917 /* * *** main loop *** */
918 /* L30: */
919 }
920 /* * */
921 /* * *** if specified, return the new residual as well *** */
922 if (*iresid == 1) {
923 nmresid27_1s__(nx, ny, nz, &ipc[1], &rpc[1], &oc[oc_offset], &cc[
924 cc_offset], &fc[fc_offset], &oe[oe_offset], &on[on_offset], &
925 uc[uc_offset], &one[one_offset], &onw[onw_offset], &ue[
926 ue_offset], &uw[uw_offset], &un[un_offset], &us[us_offset], &
927 une[une_offset], &unw[unw_offset], &use[use_offset], &usw[
928 usw_offset], &x[x_offset], &r__[r_offset], &w1[w1_offset]);
929 }
930 /* * */
931 /* * *** messages *** */
932 if (ifail_tol__ > 0) {
933 s_wsle(&io___55);
934 do_lio(&c__9, &c__1, "% NGSRB: 1d newton tolerance failures: ", (
935 ftnlen)42);
936 do_lio(&c__3, &c__1, (char *)&ifail_tol__, (ftnlen)sizeof(integer));
937 e_wsle();
938 }
939 /* * */
940 /* * *** return and end *** */
941 return 0;
942 } /* ngsrb27_ */
943
944 /* Subroutine */ int ngsrb7x_(integer *nx, integer *ny, integer *nz, integer *
945 ipc, doublereal *rpc, doublereal *oc, doublereal *cc, doublereal *fc,
946 doublereal *oe, doublereal *on, doublereal *uc, doublereal *x,
947 doublereal *w1, doublereal *w2, doublereal *r__, integer *itmax,
948 integer *iters, doublereal *errtol, doublereal *omega, integer *
949 iresid, integer *iadjoint)
950 {
951 /* System generated locals */
952 integer oe_dim1, oe_dim2, oe_offset, on_dim1, on_dim2, on_offset, uc_dim1,
953 uc_dim2, uc_offset, fc_dim1, fc_dim2, fc_offset, oc_dim1,
954 oc_dim2, oc_offset, cc_dim1, cc_dim2, cc_offset, x_dim1, x_dim2,
955 x_offset, w1_dim1, w1_dim2, w1_offset, w2_dim1, w2_dim2,
956 w2_offset, r_dim1, r_dim2, r_offset, i__1, i__2, i__3, i__4;
957
958 /* Builtin functions */
959 integer s_wsle(cilist *), do_lio(integer *, integer *, char *, ftnlen),
960 e_wsle(void);
961
962 /* Local variables */
963 static integer i__, j, k, ifail_tol__;
964 static doublereal aa, bb, ff, uu;
965 extern /* Subroutine */ int nmresid7_1s__(integer *, integer *, integer *,
966 integer *, doublereal *, doublereal *, doublereal *, doublereal *
967 , doublereal *, doublereal *, doublereal *, doublereal *,
968 doublereal *, doublereal *);
969 static doublereal znn;
970 static integer ioff;
971 static doublereal dznn;
972 static integer ipkey;
973 static doublereal zntol;
974 extern doublereal c_scal__(doublereal *, doublereal *, integer *);
975 static doublereal change;
976 static integer nitmax, niters;
977 extern doublereal dc_scal__(doublereal *, doublereal *, integer *);
978 static doublereal zndenom, znerror;
979
980 /* Fortran I/O blocks */
981 static cilist io___74 = { 0, 6, 0, 0, 0 };
982
983
984 /* * ********************************************************************* */
985 /* * purpose: */
986 /* * */
987 /* * fast 7 diagonal nonlinear red/black gauss-seidel routine. */
988 /* * */
989 /* * this routine applies the red/black gauss-seidel operator or its */
990 /* * adjoint depending on the flag iadjoint. note that the adjoint */
991 /* * in this case is simply doing the red or black points first. */
992 /* * */
993 /* * note that the interior grid points from 2,...,nx-1, etc. */
994 /* * we then begin coloring with a red point, and do a point red/black */
995 /* * coloring. */
996 /* * */
997 /* * the red points are: */
998 /* * */
999 /* * if ((j even) and (k even)) then */
1000 /* * begin row at first point, i=2 (i even), or ioff = 0 */
1001 /* * else if ((j odd) and (k even)) then */
1002 /* * begin row at second point, i=3 (i odd), or ioff = 1 */
1003 /* * else if ((j even) and (k odd)) then */
1004 /* * begin row at second point, i=3 (i odd), or ioff = 1 */
1005 /* * else if ((j odd) and (k odd)) then */
1006 /* * begin row at first point, i=2 (i even), or ioff = 0 */
1007 /* * endif */
1008 /* * then: begin row at: i=2+ioff */
1009 /* * */
1010 /* * the appropriate ioff function for the red points is then: */
1011 /* * ioff = dabs(mod(j,2)-mod(k,2)) */
1012 /* * */
1013 /* * the appropriate ioff function for the black points is then: */
1014 /* * ioff = 1 - dabs(mod(j,2)-mod(k,2)) */
1015 /* * */
1016 /* * */
1017 /* * alternatively, the red points are: */
1018 /* * */
1019 /* * those whose indices add up to an even number. */
1020 /* * to see this, consider that all surrounding points are */
1021 /* * only one index different, hence the sum will differ by one. */
1022 /* * thus, if a given point has an even sum, then the surrounding */
1023 /* * points will have an odd sum. */
1024 /* * */
1025 /* * thus, the black points are: */
1026 /* * */
1027 /* * therefore those whose indices add up to an odd number. */
1028 /* * */
1029 /* * algorithm parameters: */
1030 /* * --------------------- */
1031 /* * */
1032 /* * U0 = initial guess */
1033 /* * Llin = matrix representing linear part of equations */
1034 /* * F = vector representing rhs of equations */
1035 /* * nx = number of unknowns */
1036 /* * itmax = number of iterations to perform */
1037 /* * c_scal = function representing nonlinear part of equations */
1038 /* * dc_scal = function representing derivative of nonlinear */
1039 /* * part of equations */
1040 /* * U = final result */
1041 /* * */
1042 /* * algorithm idea: */
1043 /* * --------------- */
1044 /* * */
1045 /* * the ith nonlinear equation is represented as: */
1046 /* * */
1047 /* * N(UU) = AA * UU + c_scal(UU) - FF */
1048 /* * */
1049 /* * which is solved for UU using newton's method, where: */
1050 /* * */
1051 /* * AA = ith diagonal entry of Llin */
1052 /* * FF = F(i), minus the nondiag of ith eqn of Llin times current U */
1053 /* * UU = the unknown, initialized to U(i) */
1054 /* * */
1055 /* * algorithm (matlab language): */
1056 /* * ---------------------------- */
1057 /* * */
1058 /* * function [U,iterss,errors] = Zgs(U0,UTRUE,a,b,Llin,F,nx,tol,itmax,stp,plt); */
1059 /* * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1060 /* * %%% nonlinear gauss-seidel-newton iteration (ortega-rheinboldt, page 219) */
1061 /* * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1062 /* * */
1063 /* * %%% initial guess */
1064 /* * iters = 0; */
1065 /* * error = 1.0d0; */
1066 /* * U = U0; */
1067 /* * Lnon = c_vec(b,U,nx); */
1068 /* * N = Llin * U + Lnon - F ; */
1069 /* * denom = 1.0d0; if (stp == 1) denom = norm(N); end; */
1070 /* * */
1071 /* * %%% loop */
1072 /* * while ((error > tol) & (iters < itmax)) */
1073 /* * iters = iters + 1; */
1074 /* * */
1075 /* * %%% save old guys */
1076 /* * Lnon_OLD = Lnon; */
1077 /* * U_OLD = U; */
1078 /* * */
1079 /* * %%% relax each unknown */
1080 /* * for i = 1:nx */
1081 /* * */
1082 /* * %%% define the 1d rhs */
1083 /* * FF = F(i) - Llin(i,1:nx) * U + Llin(i,i) * U(i); */
1084 /* * AA = Llin(i,i); */
1085 /* * UU = U(i); */
1086 /* * BB = b(i); */
1087 /* * */
1088 /* * %%% perform 1d newton's method */
1089 /* * %%% note that in the linear case, this is equivalent to: FF/AA */
1090 /* * nitmax = 10; */
1091 /* * ntol = 1.0e-7; */
1092 /* * niters = 0; */
1093 /* * N = AA * UU + c_scal(BB,UU) - FF ; */
1094 /* * ndenom = N; */
1095 /* * if (ndenom == 0) */
1096 /* * ndenom = 1; */
1097 /* * end; */
1098 /* * nerror = N / ndenom; */
1099 /* * */
1100 /* * while ((nerror > ntol) & (niters < nitmax)) */
1101 /* * niters = niters + 1; */
1102 /* * */
1103 /* * %%% construct jacobian matrix of N */
1104 /* * DN = AA + dc_scal(BB,UU); */
1105 /* * */
1106 /* * %%% solve linear system */
1107 /* * change = - N / DN; */
1108 /* * */
1109 /* * %%% update solution */
1110 /* * UU = UU + change; */
1111 /* * */
1112 /* * %%% construct nonlinear residual */
1113 /* * N = AA * UU + c_scal(BB,UU) - FF ; */
1114 /* * */
1115 /* * %%% compute residual */
1116 /* * nerror = N / ndenom; */
1117 /* * end */
1118 /* * */
1119 /* * %%% update the unknown */
1120 /* * U(i) = UU; */
1121 /* * end */
1122 /* * */
1123 /* * %%% new residual %%% */
1124 /* * Lnon = c_vec(b,U,nx); */
1125 /* * N = Llin * U + Lnon - F ; */
1126 /* * delta = zeros(U); */
1127 /* * delta_s = zeros(U); */
1128 /* * */
1129 /* * end */
1130 /* * */
1131 /* * author: michael holst */
1132 /* * ********************************************************************* */
1133 /* * */
1134 /* mdir 0 0 */
1135 /* * */
1136 /* * *** nonlinear iteration tolerance and itmax *** */
1137 /* Parameter adjustments */
1138 r_dim1 = *nx;
1139 r_dim2 = *ny;
1140 r_offset = 1 + r_dim1 * (1 + r_dim2);
1141 r__ -= r_offset;
1142 w2_dim1 = *nx;
1143 w2_dim2 = *ny;
1144 w2_offset = 1 + w2_dim1 * (1 + w2_dim2);
1145 w2 -= w2_offset;
1146 w1_dim1 = *nx;
1147 w1_dim2 = *ny;
1148 w1_offset = 1 + w1_dim1 * (1 + w1_dim2);
1149 w1 -= w1_offset;
1150 x_dim1 = *nx;
1151 x_dim2 = *ny;
1152 x_offset = 1 + x_dim1 * (1 + x_dim2);
1153 x -= x_offset;
1154 uc_dim1 = *nx;
1155 uc_dim2 = *ny;
1156 uc_offset = 1 + uc_dim1 * (1 + uc_dim2);
1157 uc -= uc_offset;
1158 on_dim1 = *nx;
1159 on_dim2 = *ny;
1160 on_offset = 1 + on_dim1 * (1 + on_dim2);
1161 on -= on_offset;
1162 oe_dim1 = *nx;
1163 oe_dim2 = *ny;
1164 oe_offset = 1 + oe_dim1 * (1 + oe_dim2);
1165 oe -= oe_offset;
1166 fc_dim1 = *nx;
1167 fc_dim2 = *ny;
1168 fc_offset = 1 + fc_dim1 * (1 + fc_dim2);
1169 fc -= fc_offset;
1170 cc_dim1 = *nx;
1171 cc_dim2 = *ny;
1172 cc_offset = 1 + cc_dim1 * (1 + cc_dim2);
1173 cc -= cc_offset;
1174 oc_dim1 = *nx;
1175 oc_dim2 = *ny;
1176 oc_offset = 1 + oc_dim1 * (1 + oc_dim2);
1177 oc -= oc_offset;
1178 --ipc;
1179 --rpc;
1180
1181 /* Function Body */
1182 nitmax = 10;
1183 zntol = 1e-5f;
1184 ifail_tol__ = 0;
1185 ipkey = ipc[10];
1186 /* * */
1187 /* * *** do the gauss-seidel iteration itmax times *** */
1188 i__1 = *itmax;
1189 for (*iters = 1; *iters <= i__1; ++(*iters)) {
1190 /* * */
1191 /* * *** do the red points *** */
1192 /* mdir 3 1 */
1193 i__2 = *nz - 1;
1194 for (k = 2; k <= i__2; ++k) {
1195 /* mdir 3 2 */
1196 i__3 = *ny - 1;
1197 for (j = 2; j <= i__3; ++j) {
1198 /* ZZZ ioff = mod((j+k+2),2) */
1199 ioff = (1 - *iadjoint) * ((j + k + 2) % 2) + *iadjoint * (1 -
1200 (j + k + 2) % 2);
1201 /* mdir 3 3 */
1202 i__4 = *nx - 1;
1203 for (i__ = ioff + 2; i__ <= i__4; i__ += 2) {
1204 /* * */
1205 /* * *** for this unknown, do a 1d newton's method */
1206 /* * */
1207 /* * *** determine the 1d equation */
1208 uu = x[i__ + (j + k * x_dim2) * x_dim1];
1209 aa = oc[i__ + (j + k * oc_dim2) * oc_dim1];
1210 bb = cc[i__ + (j + k * cc_dim2) * cc_dim1];
1211 ff = fc[i__ + (j + k * fc_dim2) * fc_dim1] + on[i__ + (j
1212 + k * on_dim2) * on_dim1] * x[i__ + (j + 1 + k *
1213 x_dim2) * x_dim1] + on[i__ + (j - 1 + k * on_dim2)
1214 * on_dim1] * x[i__ + (j - 1 + k * x_dim2) *
1215 x_dim1] + oe[i__ + (j + k * oe_dim2) * oe_dim1] *
1216 x[i__ + 1 + (j + k * x_dim2) * x_dim1] + oe[i__ -
1217 1 + (j + k * oe_dim2) * oe_dim1] * x[i__ - 1 + (j
1218 + k * x_dim2) * x_dim1] + uc[i__ + (j + (k - 1) *
1219 uc_dim2) * uc_dim1] * x[i__ + (j + (k - 1) *
1220 x_dim2) * x_dim1] + uc[i__ + (j + k * uc_dim2) *
1221 uc_dim1] * x[i__ + (j + (k + 1) * x_dim2) *
1222 x_dim1];
1223 /* * */
1224 /* * *** evaluate residual of 1d system */
1225 znn = aa * uu + c_scal__(&bb, &uu, &ipkey) - ff;
1226 /* * */
1227 /* * *** setup for newton's method */
1228 niters = 0;
1229 zndenom = znn;
1230 if (zndenom == 0.) {
1231 zndenom = 1.;
1232 }
1233 znerror = znn / zndenom;
1234 /* * */
1235 /* * *** the 1d newton's method *** */
1236 if (znerror < zntol) {
1237 goto L17;
1238 }
1239 if (niters > nitmax) {
1240 goto L16;
1241 }
1242 L15:
1243 ++niters;
1244 /* * */
1245 /* * *** construct jacobian matrix of NN *** */
1246 dznn = aa + dc_scal__(&bb, &uu, &ipkey);
1247 /* * */
1248 /* * *** solve the linear system *** */
1249 change = -znn / dznn;
1250 /* * */
1251 /* * *** update the solution *** */
1252 uu += change;
1253 /* * */
1254 /* * *** evaluate residual of 1d system *** */
1255 znn = aa * uu + c_scal__(&bb, &uu, &ipkey) - ff;
1256 /* * */
1257 /* * *** compute error *** */
1258 znerror = znn / zndenom;
1259 /* * */
1260 /* * *** tolerance and itmax check *** */
1261 if (znerror < zntol) {
1262 goto L17;
1263 }
1264 if (niters > nitmax) {
1265 goto L16;
1266 }
1267 /* * */
1268 /* * *** loop *** */
1269 goto L15;
1270 /* * */
1271 /* * *** tolerance not reached *** */
1272 L16:
1273 ++ifail_tol__;
1274 /* * */
1275 /* * *** tolerance reached *** */
1276 L17:
1277 /* * */
1278 /* * *** newton's method complete -- update solution value *** */
1279 x[i__ + (j + k * x_dim2) * x_dim1] = uu;
1280 /* L12: */
1281 }
1282 /* L11: */
1283 }
1284 /* L10: */
1285 }
1286 /* * */
1287 /* * *** do the black points *** */
1288 /* mdir 3 1 */
1289 i__2 = *nz - 1;
1290 for (k = 2; k <= i__2; ++k) {
1291 /* mdir 3 2 */
1292 i__3 = *ny - 1;
1293 for (j = 2; j <= i__3; ++j) {
1294 /* ZZZ ioff = 1-mod((j+k+2),2) */
1295 ioff = *iadjoint * ((j + k + 2) % 2) + (1 - *iadjoint) * (1 -
1296 (j + k + 2) % 2);
1297 /* mdir 3 3 */
1298 i__4 = *nx - 1;
1299 for (i__ = ioff + 2; i__ <= i__4; i__ += 2) {
1300 /* * */
1301 /* * *** for this unknown, do a 1d newton's method */
1302 /* * */
1303 /* * *** determine the 1d equation */
1304 uu = x[i__ + (j + k * x_dim2) * x_dim1];
1305 aa = oc[i__ + (j + k * oc_dim2) * oc_dim1];
1306 bb = cc[i__ + (j + k * cc_dim2) * cc_dim1];
1307 ff = fc[i__ + (j + k * fc_dim2) * fc_dim1] + on[i__ + (j
1308 + k * on_dim2) * on_dim1] * x[i__ + (j + 1 + k *
1309 x_dim2) * x_dim1] + on[i__ + (j - 1 + k * on_dim2)
1310 * on_dim1] * x[i__ + (j - 1 + k * x_dim2) *
1311 x_dim1] + oe[i__ + (j + k * oe_dim2) * oe_dim1] *
1312 x[i__ + 1 + (j + k * x_dim2) * x_dim1] + oe[i__ -
1313 1 + (j + k * oe_dim2) * oe_dim1] * x[i__ - 1 + (j
1314 + k * x_dim2) * x_dim1] + uc[i__ + (j + (k - 1) *
1315 uc_dim2) * uc_dim1] * x[i__ + (j + (k - 1) *
1316 x_dim2) * x_dim1] + uc[i__ + (j + k * uc_dim2) *
1317 uc_dim1] * x[i__ + (j + (k + 1) * x_dim2) *
1318 x_dim1];
1319 /* * */
1320 /* * *** evaluate residual of 1d system */
1321 znn = aa * uu + c_scal__(&bb, &uu, &ipkey) - ff;
1322 /* * */
1323 /* * *** setup for newton's method */
1324 niters = 0;
1325 zndenom = znn;
1326 if (zndenom == 0.) {
1327 zndenom = 1.;
1328 }
1329 znerror = znn / zndenom;
1330 /* * */
1331 /* * *** the 1d newton's method *** */
1332 if (znerror < zntol) {
1333 goto L27;
1334 }
1335 if (niters > nitmax) {
1336 goto L26;
1337 }
1338 L25:
1339 ++niters;
1340 /* * */
1341 /* * *** construct jacobian matrix of NN *** */
1342 dznn = aa + dc_scal__(&bb, &uu, &ipkey);
1343 /* * */
1344 /* * *** solve the linear system *** */
1345 change = -znn / dznn;
1346 /* * */
1347 /* * *** update the solution *** */
1348 uu += change;
1349 /* * */
1350 /* * *** evaluate residual of 1d system *** */
1351 znn = aa * uu + c_scal__(&bb, &uu, &ipkey) - ff;
1352 /* * */
1353 /* * *** compute error *** */
1354 znerror = znn / zndenom;
1355 /* * */
1356 /* * *** tolerance and itmax check *** */
1357 if (znerror < zntol) {
1358 goto L27;
1359 }
1360 if (niters > nitmax) {
1361 goto L26;
1362 }
1363 /* * */
1364 /* * *** loop *** */
1365 goto L25;
1366 /* * */
1367 /* * *** tolerance not reached *** */
1368 L26:
1369 ++ifail_tol__;
1370 /* * */
1371 /* * *** tolerance reached *** */
1372 L27:
1373 /* * */
1374 /* * *** newton's method complete -- update solution value *** */
1375 x[i__ + (j + k * x_dim2) * x_dim1] = uu;
1376 /* L22: */
1377 }
1378 /* L21: */
1379 }
1380 /* L20: */
1381 }
1382 /* * */
1383 /* * *** main loop *** */
1384 /* L30: */
1385 }
1386 /* * */
1387 /* * *** if specified, return the new residual as well *** */
1388 if (*iresid == 1) {
1389 nmresid7_1s__(nx, ny, nz, &ipc[1], &rpc[1], &oc[oc_offset], &cc[
1390 cc_offset], &fc[fc_offset], &oe[oe_offset], &on[on_offset], &
1391 uc[uc_offset], &x[x_offset], &r__[r_offset], &w1[w1_offset]);
1392 }
1393 /* * */
1394 /* * *** messages *** */
1395 if (ifail_tol__ > 0) {
1396 s_wsle(&io___74);
1397 do_lio(&c__9, &c__1, "% NGSRB: 1d newton tolerance failures: ", (
1398 ftnlen)42);
1399 do_lio(&c__3, &c__1, (char *)&ifail_tol__, (ftnlen)sizeof(integer));
1400 e_wsle();
1401 }
1402 /* * */
1403 /* * *** return and end *** */
1404 return 0;
1405 } /* ngsrb7x_ */
1406