Debian Codegraph @

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

raw
1 /*
2 * Copyright(C) 1999-2020, 2022, 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 "../main/defs.h"
10 #include "../main/structs.h"
11 #include "smalloc.h"
12 #include <math.h>
13 #include <stdio.h>
14
15 /* This is based on lanczos_SO but is simplified and tailored to solve
16 the extended eigen-problem using Rafael's technique. Solves Nx = lambda*Wx + Wg.
17 In the terminal propagation context we know that e^T Wg = 0 by virue of the way
18 g is constructed. This code, however, is designed to work in the more general
19 case of arbitrary g (but this capability has not been tested). Returns 0 if it
20 thinks everything's OK, nonzero otherwise. */
21
22 int lanczos_ext(struct vtx_data **A, /* sparse matrix in row linked list format */
23 int n, /* problem size */
24 int d, /* problem dimension = number of eigvecs to find */
25 double **y, /* columns of y are eigenvectors of A */
26 double eigtol, /* tolerance on eigenvectors */
27 double *vwsqrt, /* square roots of vertex weights */
28 double maxdeg, /* maximum degree of graph */
29 int version, /* flags which version of sel. orth. to use */
30 double *gvec, /* the rhs n-vector in the extended eigen problem */
31 double sigma /* specifies the norm constraint on extended
32 eigenvector */
33 )
34 {
35 extern FILE *Output_File; /* output file or null */
36 extern int LANCZOS_SO_INTERVAL; /* interval between orthogonalizations */
37 extern int LANCZOS_MAXITNS; /* maximum Lanczos iterations allowed */
38 extern int DEBUG_EVECS; /* print debugging output? */
39 extern int DEBUG_TRACE; /* trace main execution path */
40 extern int WARNING_EVECS; /* print warning messages? */
41 extern double BISECTION_SAFETY; /* safety for T bisection algorithm */
42 extern double SRESTOL; /* resid tol for T evec comp */
43 extern double DOUBLE_EPSILON; /* machine precision */
44 extern double DOUBLE_MAX; /* largest double value */
45 extern double splarax_time; /* time matvec */
46 extern double orthog_time; /* time orthogonalization work */
47 extern double evec_time; /* time to generate eigenvectors */
48 extern double ql_time; /* time tridiagonal eigenvalue work */
49 extern double blas_time; /* time for blas. linear algebra */
50 extern double init_time; /* time to allocate, initialize variables */
51 extern double scan_time; /* time for scanning eval and bound lists */
52 extern double debug_time; /* time for (some of) debug computations */
53 extern double ritz_time; /* time to generate ritz vectors */
54 extern double pause_time; /* time to compute whether to pause */
55 int i, j, k; /* indices */
56 int maxj; /* maximum number of Lanczos iterations */
57 double *u, *r; /* Lanczos vectors */
58 double *alpha, *beta; /* the Lanczos scalars from each step */
59 double *ritz; /* copy of alpha for ql */
60 double *workj; /* work vector, e.g. copy of beta for ql */
61 double *workn; /* work vector, e.g. product Av for checkeig */
62 double *s; /* eigenvector of T */
63 double **q; /* columns of q are Lanczos basis vectors */
64 double *bj; /* beta(j)*(last el. of corr. eigvec s of T) */
65 double Sres; /* how well Tevec calculated eigvec s */
66 double Sres_max; /* Max value of Sres */
67 int inc_bis_safety; /* has Sres increased? */
68 double *Ares; /* how well Lanczos calc. eigpair lambda,y */
69 int *index; /* the Ritz index of an eigenpair */
70 struct orthlink **solist; /* vec. of structs with vecs. to orthog. against */
71 struct scanlink *scanlist; /* linked list of fields to do with min ritz vals */
72 struct scanlink *curlnk; /* for traversing the scanlist */
73 double bis_safety; /* real safety for T bisection algorithm */
74 int converged; /* has the iteration converged? */
75 double goodtol; /* error tolerance for a good Ritz vector */
76 int ngood; /* total number of good Ritz pairs at current step */
77 int maxngood; /* biggest val of ngood through current step */
78 int left_ngood; /* number of good Ritz pairs on left end */
79 int lastpause; /* Most recent step with good ritz vecs */
80 int nopauses; /* Have there been any pauses? */
81 int interval; /* number of steps between pauses */
82 double time; /* Current clock time */
83 int left_goodlim; /* number of ritz pairs checked on left end */
84 double Anorm; /* Norm estimate of the Laplacian matrix */
85 int pausemode; /* which Lanczos pausing criterion to use */
86 int pause; /* whether to pause */
87 int temp; /* used to prevent redundant index computations */
88 double *extvec; /* n-vector solving the extended A eigenproblem */
89 double *v; /* j-vector solving the extended T eigenproblem */
90 double extval = 0.0; /* computed extended eigenvalue (of both A and T) */
91 double *work1, *work2; /* work vectors */
92 double check; /* to check an orthogonality condition */
93 double numerical_zero; /* used for zero in presence of round-off */
94 int ritzval_flag; /* status flag for get_ritzvals() */
95 int memory_ok; /* TRUE until memory runs out */
96
97 if (DEBUG_TRACE > 0) {
98 printf("<Entering lanczos_ext>\n");
99 }
100
101 if (DEBUG_EVECS > 0) {
102 printf("Selective orthogonalization Lanczos for extended eigenproblem, matrix size = %d.\n", n);
103 }
104
105 /* Initialize time. */
106 time = lanc_seconds();
107
108 if (d != 1) {
109 bail("ERROR: Extended Lanczos only available for bisection.", 1);
110 /* ... something must be wrong upstream. */
111 }
112
113 if (n < d + 1) {
114 bail("ERROR: System too small for number of eigenvalues requested.", 1);
115 /* ... d+1 since don't use zero eigenvalue pair */
116 }
117
118 /* Allocate space. */
119 maxj = LANCZOS_MAXITNS;
120 u = mkvec(1, n);
121 r = mkvec(1, n);
122 workn = mkvec(1, n);
123 Ares = mkvec(0, d);
124 index = smalloc((d + 1) * sizeof(int));
125 alpha = mkvec(1, maxj);
126 beta = mkvec(0, maxj);
127 ritz = mkvec(1, maxj);
128 s = mkvec(1, maxj);
129 bj = mkvec(1, maxj);
130 workj = mkvec(0, maxj);
131 q = smalloc((maxj + 1) * sizeof(double *));
132 solist = smalloc((maxj + 1) * sizeof(struct orthlink *));
133 scanlist = mkscanlist(d);
134 extvec = mkvec(1, n);
135 v = mkvec(1, maxj);
136 work1 = mkvec(1, maxj);
137 work2 = mkvec(1, maxj);
138
139 /* Set some constants governing orthogonalization */
140 ngood = 0;
141 maxngood = 0;
142 Anorm = 2 * maxdeg; /* Gershgorin estimate for ||A|| */
143 goodtol = Anorm * sqrt(DOUBLE_EPSILON); /* Parlett & Scott's bound, p.224 */
144 interval = 2 + min(LANCZOS_SO_INTERVAL - 2, n / (2 * LANCZOS_SO_INTERVAL));
145 bis_safety = BISECTION_SAFETY;
146 numerical_zero = 1.0e-13;
147
148 if (DEBUG_EVECS > 0) {
149 printf(" maxdeg %g\n", maxdeg);
150 printf(" goodtol %g\n", goodtol);
151 printf(" interval %d\n", interval);
152 printf(" maxj %d\n", maxj);
153 }
154
155 /* Initialize space. */
156 cpvec(r, 1, n, gvec);
157 if (vwsqrt != NULL) {
158 scale_diag(r, 1, n, vwsqrt);
159 }
160 check = ch_norm(r, 1, n);
161 if (vwsqrt == NULL) {
162 orthog1(r, 1, n);
163 }
164 else {
165 orthogvec(r, 1, n, vwsqrt);
166 }
167 check = fabs(check - ch_norm(r, 1, n));
168 if (check > 10 * numerical_zero && WARNING_EVECS > 0) {
169 strout("WARNING: In terminal propagation, rhs should have no component in the");
170 printf(" nullspace of the Laplacian, so check val %g should be negligible.\n", check);
171 if (Output_File != NULL) {
172 fprintf(Output_File,
173 " nullspace of the Laplacian, so check val %g should be negligible.\n",
174 check);
175 }
176 }
177 beta[0] = ch_norm(r, 1, n);
178 q[0] = mkvec(1, n);
179 setvec(q[0], 1, n, 0.0);
180 setvec(bj, 1, maxj, DOUBLE_MAX);
181
182 if (beta[0] < numerical_zero) {
183 /* The rhs vector, Dg, of the transformed problem is numerically zero or is
184 in the null space of the Laplacian, so this is not a well posed extended
185 eigenproblem. Set maxj to zero to force a quick exit but still clean-up
186 memory and return(1) to indicate to eigensolve that it should call the
187 default eigensolver routine for the standard eigenproblem. */
188 maxj = 0;
189 }
190
191 /* Main Lanczos loop. */
192 j = 1;
193 lastpause = 0;
194 pausemode = 1;
195 left_ngood = 0;
196 left_goodlim = 0;
197 converged = FALSE;
198 Sres_max = 0.0;
199 inc_bis_safety = FALSE;
200 nopauses = TRUE;
201 memory_ok = TRUE;
202 init_time += lanc_seconds() - time;
203 while ((j <= maxj) && (!converged) && memory_ok) {
204 time = lanc_seconds();
205
206 /* Allocate next Lanczos vector. If fail, back up to last pause. */
207 q[j] = mkvec_ret(1, n);
208 if (q[j] == NULL) {
209 memory_ok = FALSE;
210 if (DEBUG_EVECS > 0 || WARNING_EVECS > 0) {
211 strout("WARNING: Lanczos_ext out of memory; computing best approximation available.\n");
212 }
213 if (nopauses) {
214 bail("ERROR: Sorry, can't salvage Lanczos_ext.", 1);
215 /* ... save yourselves, men. */
216 }
217 for (i = lastpause + 1; i <= j - 1; i++) {
218 frvec(q[i], 1);
219 }
220 j = lastpause;
221 }
222
223 /* Basic Lanczos iteration */
224 vecscale(q[j], 1, n, 1.0 / beta[j - 1], r);
225 blas_time += lanc_seconds() - time;
226 time = lanc_seconds();
227 splarax(u, A, n, q[j], vwsqrt, workn);
228 splarax_time += lanc_seconds() - time;
229 time = lanc_seconds();
230 update(r, 1, n, u, -beta[j - 1], q[j - 1]);
231 alpha[j] = dot(r, 1, n, q[j]);
232 update(r, 1, n, r, -alpha[j], q[j]);
233 blas_time += lanc_seconds() - time;
234
235 /* Selective orthogonalization */
236 time = lanc_seconds();
237 if (vwsqrt == NULL) {
238 orthog1(r, 1, n);
239 }
240 else {
241 orthogvec(r, 1, n, vwsqrt);
242 }
243 if ((j == (lastpause + 1)) || (j == (lastpause + 2))) {
244 sorthog(r, n, solist, ngood);
245 }
246 orthog_time += lanc_seconds() - time;
247 beta[j] = ch_norm(r, 1, n);
248 time = lanc_seconds();
249 pause = lanpause(j, lastpause, interval, q, n, &pausemode, version, beta[j]);
250 pause_time += lanc_seconds() - time;
251 if (pause) {
252 nopauses = FALSE;
253 lastpause = j;
254
255 /* Compute limits for checking Ritz pair convergence. */
256 if (version == 2) {
257 if (left_ngood + 2 > left_goodlim) {
258 left_goodlim = left_ngood + 2;
259 }
260 }
261
262 /* Special case: need at least d Ritz vals on left. */
263 left_goodlim = max(left_goodlim, d);
264
265 /* Special case: can't find more than j total Ritz vals. */
266 if (left_goodlim > j) {
267 left_goodlim = min(left_goodlim, j);
268 }
269
270 /* Find Ritz vals using faster of Sturm bisection or ql. */
271 time = lanc_seconds();
272 if (inc_bis_safety) {
273 bis_safety *= 10;
274 inc_bis_safety = FALSE;
275 }
276 ritzval_flag =
277 get_ritzvals(alpha, beta, j, Anorm, workj, ritz, d, left_goodlim, 0, eigtol, bis_safety);
278 ql_time += lanc_seconds() - time;
279
280 if (ritzval_flag != 0) {
281 bail("ERROR: Lanczos_ext failed in computing eigenvalues of T.", 1);
282 /* ... we recover from this in lanczos_SO, but don't worry here. */
283 }
284
285 /* Scan for minimum evals of tridiagonal. */
286 time = lanc_seconds();
287 scanmin(ritz, 1, j, &scanlist);
288 scan_time += lanc_seconds() - time;
289
290 /* Compute Ritz pair bounds at left end. */
291 time = lanc_seconds();
292 setvec(bj, 1, j, 0.0);
293 for (i = 1; i <= left_goodlim; i++) {
294 Sres = Tevec(alpha, beta - 1, j, ritz[i], s);
295 if (Sres > Sres_max) {
296 Sres_max = Sres;
297 }
298 if (Sres > SRESTOL) {
299 inc_bis_safety = TRUE;
300 }
301 bj[i] = s[j] * beta[j];
302 }
303 ritz_time += lanc_seconds() - time;
304
305 /* Show portion of spectrum checked for Ritz convergence. */
306 if (DEBUG_EVECS > 2) {
307 time = lanc_seconds();
308 printf("\nindex Ritz vals bji bounds\n");
309 for (i = 1; i <= left_goodlim; i++) {
310 printf(" %3d", i);
311 doubleout(ritz[i], 1);
312 doubleout(bj[i], 1);
313 printf("\n");
314 }
315 printf("\n");
316 curlnk = scanlist;
317 while (curlnk != NULL) {
318 temp = curlnk->indx;
319 if ((temp > left_goodlim) && (temp < j)) {
320 printf(" %3d", temp);
321 doubleout(ritz[temp], 1);
322 doubleout(bj[temp], 1);
323 printf("\n");
324 }
325 curlnk = curlnk->pntr;
326 }
327 printf(" -------------------\n");
328 printf(" goodtol: %19.16f\n\n", goodtol);
329 debug_time += lanc_seconds() - time;
330 }
331
332 get_extval(alpha, beta, j, ritz[1], s, eigtol, beta[0], sigma, &extval, v, work1, work2);
333
334 /* Check convergence of iteration. */
335 if (fabs(beta[j] * v[j]) < eigtol) {
336 converged = TRUE;
337 }
338 else {
339 converged = FALSE;
340 }
341
342 if (!converged) {
343 ngood = 0;
344 left_ngood = 0; /* for setting left_goodlim on next loop */
345
346 /* Compute converged Ritz pairs on left end */
347 time = lanc_seconds();
348 for (i = 1; i <= left_goodlim; i++) {
349 if (bj[i] <= goodtol) {
350 ngood += 1;
351 left_ngood += 1;
352 if (ngood > maxngood) {
353 maxngood = ngood;
354 solist[ngood] = makeorthlnk();
355 (solist[ngood])->vec = mkvec(1, n);
356 }
357 (solist[ngood])->index = i;
358 Sres = Tevec(alpha, beta - 1, j, ritz[i], s);
359 if (Sres > Sres_max) {
360 Sres_max = Sres;
361 }
362 if (Sres > SRESTOL) {
363 inc_bis_safety = TRUE;
364 }
365 setvec((solist[ngood])->vec, 1, n, 0.0);
366 for (k = 1; k <= j; k++) {
367 scadd((solist[ngood])->vec, 1, n, s[k], q[k]);
368 }
369 }
370 }
371 ritz_time += lanc_seconds() - time;
372
373 if (DEBUG_EVECS > 2) {
374 time = lanc_seconds();
375
376 /* Show some info on the orthogonalization. */
377 printf(" j %3d; goodlim lft %2d, rgt %2d; list ", j, left_goodlim, 0);
378 solistout(solist, ngood, j);
379
380 /* Assemble current approx. eigenvector, check residual directly. */
381 setvec(y[1], 1, n, 0.0);
382 for (k = 1; k <= j; k++) {
383 scadd(y[1], 1, n, v[k], q[k]);
384 }
385 printf(" extended eigenvalue %g\n", extval);
386 printf(" est. extended residual %g\n", fabs(v[j] * beta[j]));
387 checkeig_ext(workn, u, A, y[1], n, extval, vwsqrt, gvec, eigtol, FALSE);
388
389 printf("---------------------end of iteration---------------------\n\n");
390 debug_time += lanc_seconds() - time;
391 }
392 }
393 }
394 j++;
395 }
396 j--;
397
398 if (DEBUG_EVECS > 0) {
399 time = lanc_seconds();
400 if (maxj == 0) {
401 printf("Not extended eigenproblem -- calling ordinary eigensolver.\n");
402 }
403 else {
404 printf(" Lanczos_ext itns: %d\n", j);
405 printf(" extended eigenvalue: %g\n", extval);
406 if (j == maxj) {
407 strout("WARNING: Maximum number of Lanczos iterations reached.\n");
408 }
409 }
410 debug_time += lanc_seconds() - time;
411 }
412
413 if (maxj != 0) {
414 /* Compute (scaled) extended eigenvector. */
415 time = lanc_seconds();
416 setvec(y[1], 1, n, 0.0);
417 for (k = 1; k <= j; k++) {
418 scadd(y[1], 1, n, v[k], q[k]);
419 }
420 evec_time += lanc_seconds() - time;
421 /* Note: assign() will scale this y vector back to x (since y = Dx) */
422
423 /* Compute and check residual directly. */
424 if (DEBUG_EVECS > 0 || WARNING_EVECS > 0) {
425 time = lanc_seconds();
426 checkeig_ext(workn, u, A, y[1], n, extval, vwsqrt, gvec, eigtol, TRUE);
427 debug_time += lanc_seconds() - time;
428 }
429 }
430
431 /* free up memory */
432 time = lanc_seconds();
433 frvec(u, 1);
434 frvec(r, 1);
435 frvec(workn, 1);
436 frvec(Ares, 0);
437 sfree(index);
438 frvec(alpha, 1);
439 frvec(beta, 0);
440 frvec(ritz, 1);
441 frvec(s, 1);
442 frvec(bj, 1);
443 frvec(workj, 0);
444 for (i = 0; i <= j; i++) {
445 frvec(q[i], 1);
446 }
447
448 sfree(q);
449 while (scanlist != NULL) {
450 curlnk = scanlist->pntr;
451 sfree(scanlist);
452 scanlist = curlnk;
453 }
454
455 for (i = 1; i <= maxngood; i++) {
456 frvec((solist[i])->vec, 1);
457 sfree(solist[i]);
458 }
459
460 sfree(solist);
461 frvec(extvec, 1);
462 frvec(v, 1);
463 frvec(work1, 1);
464 frvec(work2, 1);
465 init_time += lanc_seconds() - time;
466
467 if (maxj == 0) {
468 return (1); /* see note on beta[0] and maxj above */
469 }
470
471 return (0);
472 }