Debian Codegraph @

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

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