Debian Codegraph @

packages/seacas/libraries/chaco/eigen/rqi.c

raw
1 /*
2 * Copyright(C) 1999-2020, 2023 National Technology & Engineering Solutions
3 * of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
4 * NTESS, the U.S. Government retains certain rights in this software.
5 *
6 * See packages/seacas/LICENSE for details
7 */
8
9 #include "defs.h" // for TRUE, FALSE, max, min
10 #include "smalloc.h" // for sfree, smalloc
11 #include "structs.h"
12 #include <stdio.h> // for printf, NULL
13
14 /* Perform Rayleigh Quotient Iteration */
15
16 void rqi(struct vtx_data **A, /* matrix/graph being analyzed */
17 double **yvecs, /* eigenvectors to be refined */
18 int index, /* index of vector in yvecs to be refined */
19 int n, /* number of rows/columns in matrix */
20 double *r1, double *r2, double *v, double *w, double *x, double *y,
21 double *work, /* work space for symmlq */
22 double tol, /* error tolerance in eigenpair */
23 double initshift, /* initial shift */
24 double *evalest, /* returned eigenvalue */
25 double *vwsqrt, /* square roots of vertex weights */
26 struct orthlink *orthlist, /* lower evecs to orthogonalize against */
27 int cube_or_mesh, /* 0 => hypercube, d => d-dimensional mesh */
28 int nsets, /* number of sets to divide into */
29 int *assignment, /* set number of each vtx (length n+1) */
30 int *active, /* space for nvtxs integers */
31 int mediantype, /* which partitioning strategy to use */
32 double *goal, /* desired set sizes */
33 int vwgt_max, /* largest vertex weight */
34 int ndims /* dimensionality of partition */
35 )
36 {
37 extern int DEBUG_EVECS; /* debug flag for eigen computation */
38 extern int DEBUG_TRACE; /* trace main execution path */
39 extern int WARNING_EVECS; /* warning flag for eigen computation */
40 extern int RQI_CONVERGENCE_MODE; /* type of convergence monitoring to do */
41 int rqisteps; /* # rqi rqisteps */
42 double res; /* convergence quant for rqi */
43 double last_res; /* res on previous rqi step */
44 double macheps; /* machine precision calculated by symmlq */
45 double normxlim; /* a stopping criteria for symmlq */
46 double normx; /* norm of the solution vector */
47 int symmlqitns; /* # symmlq itns */
48 int inv_it_steps; /* initial steps of inverse iteration */
49 long itnmin; /* symmlq input */
50 double shift, rtol; /* symmlq input */
51 long precon, goodb, nout; /* symmlq input */
52 long checka, intlim; /* symmlq input */
53 double anorm, acond; /* symmlq output */
54 double rnorm, ynorm; /* symmlq output */
55 long istop, itn; /* symmlq output */
56 long long_n; /* copy of n for passing to symmlq */
57 int warning; /* warning on possible misconvergence */
58 double factor; /* ratio between previous res and new tol */
59 double minfactor; /* minimum acceptable value of factor */
60 int converged; /* has process converged yet? */
61 double *u; /* name of vector being refined */
62 int *old_assignment = NULL; /* previous assignment vector */
63 int *assgn_pntr; /* pntr to assignment vector */
64 int *old_assgn_pntr; /* pntr to previous assignment vector */
65 int assigndiff = 0; /* discrepancies between old and new assignment */
66 int assigntol = 0; /* tolerance on convergence of assignment vector */
67 int first; /* is this the first RQI step? */
68 int i; /* loop index */
69
70 if (DEBUG_TRACE > 0) {
71 printf("<Entering rqi>\n");
72 }
73
74 /* Initialize RQI loop */
75 u = yvecs[index];
76 splarax(y, A, n, u, vwsqrt, r1);
77 shift = dot(u, 1, n, y);
78 scadd(y, 1, n, -shift, u);
79 res = ch_norm(y, 1, n); /* eigen-residual */
80 rqisteps = 0; /* a counter */
81 symmlqitns = 0; /* a counter */
82
83 /* Set invariant symmlq parameters */
84 precon = FALSE; /* FALSE until we figure out a good way */
85 goodb = TRUE; /* should be TRUE for this application */
86 nout = 0; /* set to 0 for no Symmlq output; 6 for lots */
87 checka = FALSE; /* if don't know by now, too bad */
88 intlim = n; /* set to enforce a maximum number of Symmlq itns */
89 itnmin = 0; /* set to enforce a minimum number of Symmlq itns */
90 long_n = n; /* type change for alint */
91
92 if (DEBUG_EVECS > 0) {
93 printf("Using RQI/Symmlq refinement on graph with %d vertices.\n", n);
94 }
95 if (DEBUG_EVECS > 1) {
96 printf(" step lambda est. Ares Symmlq its. istop factor delta\n");
97 printf(" 0");
98 doubleout(shift, 1);
99 doubleout(res, 1);
100 printf("\n");
101 }
102
103 if (RQI_CONVERGENCE_MODE == 1) {
104 assigntol = tol * n;
105 old_assignment = smalloc((n + 1) * sizeof(int));
106 }
107
108 /* Perform RQI */
109 inv_it_steps = 2;
110 warning = FALSE;
111 factor = 10;
112 minfactor = factor / 2;
113 first = TRUE;
114 if (res < tol) {
115 converged = TRUE;
116 }
117 else {
118 converged = FALSE;
119 }
120 while (!converged) {
121 if (res / tol < 1.2) {
122 factor = max(factor / 2, minfactor);
123 }
124 rtol = res / factor;
125
126 /* exit Symmlq if iterate is this large */
127 normxlim = 1.0 / rtol;
128
129 if (rqisteps < inv_it_steps) {
130 shift = initshift;
131 }
132
133 symmlq(&long_n, &u[1], &r1[1], &r2[1], &v[1], &w[1], &x[1], &y[1], work, &checka, &goodb,
134 &precon, &shift, &nout, &intlim, &rtol, &istop, &itn, &anorm, &acond, &rnorm, &ynorm,
135 (double *)A, vwsqrt, (double *)orthlist, &macheps, &normxlim, &itnmin);
136 symmlqitns += itn;
137 normx = ch_norm(x, 1, n);
138 vecscale(u, 1, n, 1.0 / normx, x);
139 splarax(y, A, n, u, vwsqrt, r1);
140 shift = dot(u, 1, n, y);
141 scadd(y, 1, n, -shift, u);
142 last_res = res;
143 res = ch_norm(y, 1, n);
144 if (res > last_res) {
145 warning = TRUE;
146 }
147 rqisteps++;
148
149 if (res < tol) {
150 converged = TRUE;
151 }
152
153 if (RQI_CONVERGENCE_MODE == 1 && !converged && ndims == 1) {
154 if (first) {
155 assign(A, yvecs, n, 1, cube_or_mesh, nsets, vwsqrt, assignment, active, mediantype, goal,
156 vwgt_max);
157 x2y(yvecs, ndims, n, vwsqrt);
158 first = FALSE;
159 assigndiff = n; /* dummy value for debug chart */
160 }
161 else {
162 /* copy assignment to old_assignment */
163 assgn_pntr = assignment;
164 old_assgn_pntr = old_assignment;
165 for (i = n + 1; i; i--) {
166 *old_assgn_pntr++ = *assgn_pntr++;
167 }
168
169 assign(A, yvecs, n, ndims, cube_or_mesh, nsets, vwsqrt, assignment, active, mediantype,
170 goal, vwgt_max);
171 x2y(yvecs, ndims, n, vwsqrt);
172
173 /* count differences in assignment */
174 assigndiff = 0;
175 assgn_pntr = assignment;
176 old_assgn_pntr = old_assignment;
177 for (i = n + 1; i; i--) {
178 if (*old_assgn_pntr++ != *assgn_pntr++) {
179 assigndiff++;
180 }
181 }
182 assigndiff = min(assigndiff, n - assigndiff);
183 if (assigndiff <= assigntol) {
184 converged = TRUE;
185 }
186 }
187 }
188
189 if (DEBUG_EVECS > 1) {
190 printf(" %2d", rqisteps);
191 doubleout(shift, 1);
192 doubleout(res, 1);
193 printf(" %3ld", itn);
194 printf(" %ld", istop);
195 printf(" %g", factor);
196 if (RQI_CONVERGENCE_MODE == 1) {
197 printf(" %d\n", assigndiff);
198 }
199 else {
200 printf("\n");
201 }
202 }
203 }
204 *evalest = shift;
205
206 if (WARNING_EVECS > 0 && warning) {
207 strout("WARNING: Residual convergence not monotonic; RQI may have misconverged.\n");
208 }
209
210 if (DEBUG_EVECS > 0) {
211 printf("Eval ");
212 doubleout(*evalest, 1);
213 printf(" RQI steps %d, Symmlq iterations %d.\n\n", rqisteps, symmlqitns);
214 }
215
216 if (RQI_CONVERGENCE_MODE == 1) {
217 sfree(old_assignment);
218 }
219 }