Debian Codegraph @

packages/seacas/libraries/chaco/eigen/get_ritzvals.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"
10 #include <math.h>
11 #include <stdio.h>
12
13 /* Finds needed eigenvalues of tridiagonal T using either the QL algorithm
14 or Sturm sequence bisection, whichever is predicted to be faster based
15 on a simple complexity model. If one fails (which is rare), the other
16 is tried. The return value is 0 if one of the routines succeeds. If they
17 both fail, the return value is 1, and Lanczos should compute the best
18 approximation it can based on previous iterations. */
19 int get_ritzvals(double *alpha, /* vector of Lanczos scalars */
20 double *beta, /* vector of Lanczos scalars */
21 int j, /* number of Lanczos iterations taken */
22 double Anorm, /* Gershgorin estimate */
23 double *workj, /* work vector for Sturm sequence */
24 double *ritz, /* array holding evals */
25 int d, /* problem dimension = num. eigenpairs needed */
26 int left_goodlim, /* number of ritz pairs checked on left end */
27 int right_goodlim, /* number of ritz pairs checked on right end */
28 double eigtol, /* tolerance on eigenpair */
29 double bis_safety /* bisection tolerance function divisor */
30 )
31 {
32 extern int DEBUG_EVECS; /* debug flag for eigen computation */
33 extern int WARNING_EVECS; /* warning flag for eigen computation */
34 int nvals_left; /* numb. evals to find on left end of spectrum */
35 int nvals_right; /* numb. evals to find on right end of spectrum */
36 double bisection_tol; /* width of interval bisection should converge to */
37 int pred_steps; /* predicts # of required bisection steps per eval */
38 int tot_pred_steps; /* predicts total # of required bisection steps */
39 double *ritz_sav = NULL; /* copy of ritzvals for debugging */
40 int bisect_flag; /* return status of bisect() */
41 int ql_flag; /* return status of ql() */
42 int local_debug; /* whether to check bisection results with ql */
43 /* Determine number of ritzvals to find on left and right ends */
44 nvals_left = max(d, left_goodlim);
45 nvals_right = min(j - nvals_left, right_goodlim);
46
47 /* Estimate work for bisection vs. ql assuming bisection takes 5j flops per
48 step, ql takes 30j^2 flops per call. (Ignore sorts, copies, addressing.) */
49
50 bisection_tol = eigtol * eigtol / bis_safety;
51 pred_steps = (log10(Anorm / bisection_tol) / log10(2.0)) + 1;
52 tot_pred_steps = (nvals_left + nvals_right) * pred_steps;
53
54 bisect_flag = ql_flag = 0;
55
56 if (5 * tot_pred_steps < 30 * j) {
57 if (DEBUG_EVECS > 2) {
58 printf(" tridiagonal solver: bisection\n");
59 }
60
61 /* Set local_debug = TRUE for a table checking bisection against QL. */
62 local_debug = FALSE;
63 if (local_debug) {
64 ritz_sav = mkvec(1, j);
65 cpvec(ritz_sav, 1, j, alpha);
66 cpvec(workj, 0, j, beta);
67 ql_flag = ql(ritz_sav, workj, j);
68 if (ql_flag != 0) {
69 bail("Aborting debugging procedure in get_ritzvals().\n", 1);
70 }
71 shell_sort(j, &ritz_sav[1]);
72 }
73
74 bisect_flag = bisect(alpha, beta, j, Anorm, workj, ritz, nvals_left, nvals_right, bisection_tol,
75 ritz_sav, pred_steps + 10);
76
77 if (local_debug) {
78 frvec(ritz_sav, 1);
79 }
80 }
81
82 else {
83 if (DEBUG_EVECS > 2) {
84 printf(" tridiagonal solver: ql\n");
85 }
86 cpvec(ritz, 1, j, alpha);
87 cpvec(workj, 0, j, beta);
88 ql_flag = ql(ritz, workj, j);
89 shell_sort(j, &ritz[1]);
90 }
91
92 if (bisect_flag != 0 && ql_flag == 0) {
93 if (DEBUG_EVECS > 0 || WARNING_EVECS > 0) {
94 strout("WARNING: Sturm bisection of T failed; switching to QL.\n");
95 }
96 if (DEBUG_EVECS > 1 || WARNING_EVECS > 1) {
97 if (bisect_flag == 1) {
98 strout(" - failure detected in sturmcnt().\n");
99 }
100 if (bisect_flag == 2) {
101 strout(" - maximum number of bisection steps reached.\n");
102 }
103 }
104 cpvec(ritz, 1, j, alpha);
105 cpvec(workj, 0, j, beta);
106 ql_flag = ql(ritz, workj, j);
107 shell_sort(j, &ritz[1]);
108 }
109
110 if (ql_flag != 0 && bisect_flag == 0) {
111 if (DEBUG_EVECS > 0 || WARNING_EVECS > 0) {
112 strout("WARNING: QL failed for T; switching to Sturm bisection.\n");
113 }
114 bisect_flag = bisect(alpha, beta, j, Anorm, workj, ritz, nvals_left, nvals_right, bisection_tol,
115 ritz_sav, pred_steps + 3);
116 }
117
118 if (bisect_flag != 0 && ql_flag != 0) {
119 if (DEBUG_EVECS > 0 || WARNING_EVECS > 0) {
120 return (1); /* can't recover; bail out with error code */
121 }
122 }
123
124 return (0); /* ... things seem ok. */
125 }