Debian Codegraph @

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

raw
1 /* ./src_f77/ncgd.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 doublereal c_b4 = 1.;
11 static doublereal c_b5 = -1.;
12
13 /* * /////////////////////////////////////////////////////////////////////////// */
14 /* * @file ncgd.f */
15 /* * @author Michael Holst */
16 /* * @brief Nonlinear (Fletcher-Reeves) variation of (Hestenes-Stiefel) CG. */
17 /* * @version $Id: ncgd.c,v 1.3 2010/08/12 05:45:24 fetk Exp $ */
18 /* * @attention */
19 /* * @verbatim */
20 /* * */
21 /* * PMG -- Parallel algebraic MultiGrid */
22 /* * Copyright (C) 1994-- Michael Holst. */
23 /* * */
24 /* * Michael Holst <[email protected]> */
25 /* * University of California, San Diego */
26 /* * Department of Mathematics, 5739 AP&M */
27 /* * 9500 Gilman Drive, Dept. 0112 */
28 /* * La Jolla, CA 92093-0112 USA */
29 /* * http://math.ucsd.edu/~mholst */
30 /* * */
31 /* * This file is part of PMG. */
32 /* * */
33 /* * PMG is free software; you can redistribute it and/or modify */
34 /* * it under the terms of the GNU General Public License as published by */
35 /* * the Free Software Foundation; either version 2 of the License, or */
36 /* * (at your option) any later version. */
37 /* * */
38 /* * PMG is distributed in the hope that it will be useful, */
39 /* * but WITHOUT ANY WARRANTY; without even the implied warranty of */
40 /* * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
41 /* * GNU General Public License for more details. */
42 /* * */
43 /* * You should have received a copy of the GNU General Public License */
44 /* * along with PMG; if not, write to the Free Software */
45 /* * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
46 /* * */
47 /* * Linking PMG statically or dynamically with other modules is making a */
48 /* * combined work based on PMG. Thus, the terms and conditions of the GNU */
49 /* * General Public License cover the whole combination. */
50 /* * */
51 /* * SPECIAL GPL EXCEPTION */
52 /* * In addition, as a special exception, the copyright holders of PMG */
53 /* * give you permission to combine the PMG program with free software */
54 /* * programs and libraries that are released under the GNU LGPL or with */
55 /* * code included in releases of ISIM, PMV, PyMOL, SMOL, VMD, and Vision. */
56 /* * Such combined software may be linked with PMG and redistributed together */
57 /* * in original or modified form as mere aggregation without requirement that */
58 /* * the entire work be under the scope of the GNU General Public License. */
59 /* * This special exception permission is also extended to any software listed */
60 /* * in the SPECIAL GPL EXCEPTION clauses by the FEtk and APBS libraries. */
61 /* * */
62 /* * Note that people who make modified versions of PMG are not obligated */
63 /* * to grant this special exception for their modified versions; it is */
64 /* * their choice whether to do so. The GNU General Public License gives */
65 /* * permission to release a modified version without this exception; this */
66 /* * exception also makes it possible to release a modified version which */
67 /* * carries forward this exception. */
68 /* * */
69 /* * @endverbatim */
70 /* * /////////////////////////////////////////////////////////////////////////// */
71 /* Subroutine */ int ncghs_(integer *nx, integer *ny, integer *nz, integer *
72 ipc, doublereal *rpc, doublereal *ac, doublereal *cc, doublereal *fc,
73 doublereal *x, doublereal *p, doublereal *ap, doublereal *r__,
74 doublereal *zk, doublereal *zkp1, doublereal *tmp, integer *itmax,
75 integer *iters, doublereal *errtol, doublereal *omega, integer *
76 iresid, integer *iadjoint)
77 {
78 /* System generated locals */
79 integer ac_dim1, ac_dim2, ac_dim3, ac_offset, fc_dim1, fc_dim2, fc_offset,
80 cc_dim1, cc_dim2, cc_offset, x_dim1, x_dim2, x_offset, r_dim1,
81 r_dim2, r_offset, p_dim1, p_dim2, p_offset, ap_dim1, ap_dim2,
82 ap_offset, zk_dim1, zk_dim2, zk_offset, zkp1_dim1, zkp1_dim2,
83 zkp1_offset, tmp_dim1, tmp_dim2, tmp_offset;
84 doublereal d__1;
85
86 /* Builtin functions */
87 double sqrt(doublereal);
88
89 /* Local variables */
90 extern /* Subroutine */ int linesearch_(integer *, integer *, integer *,
91 doublereal *, integer *, doublereal *, doublereal *, doublereal *,
92 doublereal *, doublereal *, doublereal *, doublereal *,
93 doublereal *, doublereal *, doublereal *);
94 static doublereal beta;
95 extern doublereal xdot_(integer *, integer *, integer *, doublereal *,
96 doublereal *);
97 static doublereal rhok1, rhok2, alpha;
98 extern /* Subroutine */ int xscal_(integer *, integer *, integer *,
99 doublereal *, doublereal *);
100 static doublereal rsnrm;
101 extern /* Subroutine */ int xcopy_(integer *, integer *, integer *,
102 doublereal *, doublereal *), xaxpy_(integer *, integer *, integer
103 *, doublereal *, doublereal *, doublereal *), nmresid_(integer *,
104 integer *, integer *, integer *, doublereal *, doublereal *,
105 doublereal *, doublereal *, doublereal *, doublereal *,
106 doublereal *);
107
108 /* * ********************************************************************* */
109 /* * purpose: */
110 /* * */
111 /* * solves the a(x)=b using nonlinar conjugate gradients (fletcher-reeves). */
112 /* * */
113 /* * author: michael holst */
114 /* * ********************************************************************* */
115 /* * */
116 /* mdir 0 0 */
117 /* * */
118 /* * *** setup for the looping *** */
119 /* Parameter adjustments */
120 tmp_dim1 = *nx;
121 tmp_dim2 = *ny;
122 tmp_offset = 1 + tmp_dim1 * (1 + tmp_dim2);
123 tmp -= tmp_offset;
124 zkp1_dim1 = *nx;
125 zkp1_dim2 = *ny;
126 zkp1_offset = 1 + zkp1_dim1 * (1 + zkp1_dim2);
127 zkp1 -= zkp1_offset;
128 zk_dim1 = *nx;
129 zk_dim2 = *ny;
130 zk_offset = 1 + zk_dim1 * (1 + zk_dim2);
131 zk -= zk_offset;
132 r_dim1 = *nx;
133 r_dim2 = *ny;
134 r_offset = 1 + r_dim1 * (1 + r_dim2);
135 r__ -= r_offset;
136 ap_dim1 = *nx;
137 ap_dim2 = *ny;
138 ap_offset = 1 + ap_dim1 * (1 + ap_dim2);
139 ap -= ap_offset;
140 p_dim1 = *nx;
141 p_dim2 = *ny;
142 p_offset = 1 + p_dim1 * (1 + p_dim2);
143 p -= p_offset;
144 x_dim1 = *nx;
145 x_dim2 = *ny;
146 x_offset = 1 + x_dim1 * (1 + x_dim2);
147 x -= x_offset;
148 fc_dim1 = *nx;
149 fc_dim2 = *ny;
150 fc_offset = 1 + fc_dim1 * (1 + fc_dim2);
151 fc -= fc_offset;
152 cc_dim1 = *nx;
153 cc_dim2 = *ny;
154 cc_offset = 1 + cc_dim1 * (1 + cc_dim2);
155 cc -= cc_offset;
156 ac_dim1 = *nx;
157 ac_dim2 = *ny;
158 ac_dim3 = *nz;
159 ac_offset = 1 + ac_dim1 * (1 + ac_dim2 * (1 + ac_dim3));
160 ac -= ac_offset;
161 --ipc;
162 --rpc;
163
164 /* Function Body */
165 *iters = 0;
166 if (*iters >= *itmax && *iresid == 0) {
167 goto L99;
168 }
169 nmresid_(nx, ny, nz, &ipc[1], &rpc[1], &ac[ac_offset], &cc[cc_offset], &
170 fc[fc_offset], &x[x_offset], &r__[r_offset], &zk[zk_offset]);
171 if (*iters >= *itmax) {
172 goto L99;
173 }
174 L30:
175 /* * */
176 /* * *** compute/check the current stopping test *** */
177 rhok2 = xdot_(nx, ny, nz, &r__[r_offset], &r__[r_offset]);
178 rsnrm = sqrt(rhok2);
179 if (rsnrm <= *errtol) {
180 goto L99;
181 }
182 if (*iters >= *itmax) {
183 goto L99;
184 }
185 /* * */
186 /* * *** form new direction vector from old one and residual *** */
187 if (*iters == 0) {
188 xcopy_(nx, ny, nz, &r__[r_offset], &p[p_offset]);
189 } else {
190 beta = rhok2 / rhok1;
191 d__1 = 1. / beta;
192 xaxpy_(nx, ny, nz, &d__1, &r__[r_offset], &p[p_offset]);
193 xscal_(nx, ny, nz, &beta, &p[p_offset]);
194 }
195 /* * */
196 /* * *** nonlinear case: do a line search *** */
197 /* * *** (note: "ap,zk,zkp1" passed back from line search as desired) *** */
198 xcopy_(nx, ny, nz, &r__[r_offset], &tmp[tmp_offset]);
199 linesearch_(nx, ny, nz, &alpha, &ipc[1], &rpc[1], &ac[ac_offset], &cc[
200 cc_offset], &fc[fc_offset], &p[p_offset], &x[x_offset], &tmp[
201 tmp_offset], &ap[ap_offset], &zk[zk_offset], &zkp1[zkp1_offset]);
202 /* * */
203 /* * *** save rhok2 for next iteration *** */
204 rhok1 = rhok2;
205 /* * */
206 /* * *** update solution in direction p of length alpha *** */
207 xaxpy_(nx, ny, nz, &alpha, &p[p_offset], &x[x_offset]);
208 /* * */
209 /* * *** update residual *** */
210 d__1 = -alpha;
211 xaxpy_(nx, ny, nz, &d__1, &ap[ap_offset], &r__[r_offset]);
212 xaxpy_(nx, ny, nz, &c_b4, &zk[zk_offset], &r__[r_offset]);
213 xaxpy_(nx, ny, nz, &c_b5, &zkp1[zkp1_offset], &r__[r_offset]);
214 /* * */
215 /* * *** some bookkeeping *** */
216 ++(*iters);
217 goto L30;
218 /* * */
219 /* * *** return and end *** */
220 L99:
221 return 0;
222 } /* ncghs_ */
223