| 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 "prototypes.h" |
| 10 |
#include <stdio.h> // for printf, NULL |
| 11 |
|
| 12 |
/* Finds selected eigenvalues of T using Sturm sequence bisection. Based |
| 13 |
on Wilkinson's algorithm, AEP, p.302. Returns 1 if sturmcnt() fails and |
| 14 |
2 if it hasn't converged in max_steps of bisection. If neither of these |
| 15 |
errors is detected the return value is 0. */ |
| 16 |
int bisect(double *alpha, /* vector of Lanczos scalars */ |
| 17 |
double *beta, /* vector of Lanczos scalars */ |
| 18 |
int j, /* number of Lanczos iterations taken */ |
| 19 |
double Anorm, /* Gershgorin estimate */ |
| 20 |
double *workj, /* work vector for Sturm sequence */ |
| 21 |
double *ritz, /* array holding evals */ |
| 22 |
int nevals_left, /* number of evals on right to find */ |
| 23 |
int nevals_right, /* number of evals on left to find */ |
| 24 |
double tol, /* tolerance on bracket width */ |
| 25 |
double *ritz_sav, /* space to copy ritzvals for debugging */ |
| 26 |
int max_steps /* maximum number of bisection steps allowed */ |
| 27 |
) |
| 28 |
{ |
| 29 |
extern int DEBUG_EVECS; /* debug flag for eigen computation */ |
| 30 |
extern double DOUBLE_MAX; /* largest double value */ |
| 31 |
int index; /* index of sturm polynomial */ |
| 32 |
int i; /* loop index */ |
| 33 |
double *pntr; /* pntr to double array */ |
| 34 |
double x1, x2; /* the bracketing interval */ |
| 35 |
int x1cnt, x2cnt; /* Sturm counts at x1 and x2 */ |
| 36 |
double x; /* the inserted point */ |
| 37 |
int xcnt; /* the Sturm count at x */ |
| 38 |
int steps; /* number of bisection steps for a Ritzval */ |
| 39 |
int tot_steps; /* number of bisection steps for all Ritzvals */ |
| 40 |
int numbracketed; /* number of evals between x1 and x2 */ |
| 41 |
int x1ck; /* debugging check on x1cnt */ |
| 42 |
int x2ck; /* debugging check on x2cnt */ |
| 43 |
int numck; /* debugging check on numbracketed */ |
| 44 |
double diff; /* debugging register */ |
| 45 |
int ii; /* debugging loop counter */ |
| 46 |
|
| 47 |
/* If space has been allocated for a copy of the ritz values, assume |
| 48 |
we are to check the Sturm sequence counts directly using ql(). */ |
| 49 |
if (ritz_sav != NULL) { |
| 50 |
printf("\nAnorm %g j %d nevals_left %d\n", Anorm, j, nevals_left); |
| 51 |
printf("step x1 x2 x1cnt ck x2cnt ck brack ck " |
| 52 |
"x2-x1\n"); |
| 53 |
ii = 0; |
| 54 |
} |
| 55 |
|
| 56 |
/* Initialize portion of ritz we will use (use max double so scanmin will work |
| 57 |
properly when called later on) */ |
| 58 |
pntr = &ritz[1]; |
| 59 |
for (i = j; i; i--) { |
| 60 |
*pntr++ = DOUBLE_MAX; |
| 61 |
} |
| 62 |
|
| 63 |
tot_steps = 0; |
| 64 |
|
| 65 |
/* find evals on left in decreasing index order */ |
| 66 |
x2 = Anorm; |
| 67 |
x2cnt = j; |
| 68 |
numbracketed = j; |
| 69 |
for (index = nevals_left; index >= 1; index--) { |
| 70 |
x1 = 0; |
| 71 |
x1cnt = 0; |
| 72 |
steps = 1; /* ... since started with Anorm bracketing j roots */ |
| 73 |
while ((x2 - x1) > tol || numbracketed > 1) { |
| 74 |
x = 0.5 * (x1 + x2); |
| 75 |
xcnt = sturmcnt(alpha, beta, j, x, workj); |
| 76 |
if (xcnt == -1) { |
| 77 |
return (1); |
| 78 |
/* ... sturmcnt() failed; bail out with error code */ |
| 79 |
} |
| 80 |
if (xcnt >= index) { |
| 81 |
x2 = x; |
| 82 |
x2cnt = xcnt; |
| 83 |
} |
| 84 |
else { |
| 85 |
x1 = x; |
| 86 |
x1cnt = xcnt; |
| 87 |
} |
| 88 |
numbracketed = x2cnt - x1cnt; |
| 89 |
steps++; |
| 90 |
if (steps == max_steps) { |
| 91 |
return (2); |
| 92 |
/* ... not converging; bail out with error code */ |
| 93 |
} |
| 94 |
|
| 95 |
if (ritz_sav != NULL) { |
| 96 |
diff = x2 - x1; |
| 97 |
cksturmcnt(ritz_sav, 1, j, x1, x2, &x1ck, &x2ck, &numck); |
| 98 |
printf("%4d %20.16f %20.16f %3d %3d %3d %3d %3d %3d %g", ii++, x1, x2, x1cnt, |
| 99 |
x1ck, x2cnt, x2ck, numbracketed, numck, diff); |
| 100 |
if (x1cnt != x1ck || x2cnt != x2ck || numbracketed != numck) { |
| 101 |
printf("**\n"); |
| 102 |
} |
| 103 |
else { |
| 104 |
printf("\n"); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
ritz[index] = 0.5 * (x1 + x2); |
| 109 |
if (ritz_sav != NULL) { |
| 110 |
printf("Ritzval #%d:\n", index); |
| 111 |
printf(" bisection %20.16f\n", ritz[index]); |
| 112 |
printf(" ql %20.16f\n", ritz_sav[index]); |
| 113 |
printf(" difference %20.16f\n", ritz[index] - ritz_sav[index]); |
| 114 |
printf("---------------------------------------------------\n"); |
| 115 |
} |
| 116 |
if (DEBUG_EVECS > 2) { |
| 117 |
printf(" index %d, bisection steps %d, root %20.16f\n", index, steps, ritz[index]); |
| 118 |
} |
| 119 |
tot_steps += steps; |
| 120 |
} |
| 121 |
|
| 122 |
/* find evals on right in increasing index order */ |
| 123 |
x1 = 0; |
| 124 |
x1cnt = 0; |
| 125 |
for (index = j - nevals_right + 1; index <= j; index++) { |
| 126 |
x2 = Anorm; |
| 127 |
x2cnt = j; |
| 128 |
steps = 1; /* ... since started with Anorm bracketing j roots */ |
| 129 |
while ((x2 - x1) > tol || numbracketed > 1) { |
| 130 |
x = 0.5 * (x1 + x2); |
| 131 |
xcnt = sturmcnt(alpha, beta, j, x, workj); |
| 132 |
if (xcnt == -1) { |
| 133 |
return (1); |
| 134 |
/* ... sturmcnt() failed; bail out with error code */ |
| 135 |
} |
| 136 |
if (xcnt >= index) { |
| 137 |
x2 = x; |
| 138 |
x2cnt = xcnt; |
| 139 |
} |
| 140 |
else { |
| 141 |
x1 = x; |
| 142 |
x1cnt = xcnt; |
| 143 |
} |
| 144 |
numbracketed = x2cnt - x1cnt; |
| 145 |
steps++; |
| 146 |
if (steps == max_steps) { |
| 147 |
return (2); |
| 148 |
/* ... not converging; bail out with error code */ |
| 149 |
} |
| 150 |
|
| 151 |
if (ritz_sav != NULL) { |
| 152 |
diff = x2 - x1; |
| 153 |
cksturmcnt(ritz_sav, 1, j, x1, x2, &x1ck, &x2ck, &numck); |
| 154 |
printf("%4d %20.16f %20.16f %3d %3d %3d %3d %3d %3d %g", ii++, x1, x2, x1cnt, |
| 155 |
x1ck, x2cnt, x2ck, numbracketed, numck, diff); |
| 156 |
if (x1cnt != x1ck || x2cnt != x2ck || numbracketed != numck) { |
| 157 |
printf("**\n"); |
| 158 |
} |
| 159 |
else { |
| 160 |
printf("\n"); |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
ritz[index] = 0.5 * (x1 + x2); |
| 165 |
if (ritz_sav != NULL) { |
| 166 |
printf("Ritzval #%d:\n", index); |
| 167 |
printf(" bisection %20.16f\n", ritz[index]); |
| 168 |
printf(" ql %20.16f\n", ritz_sav[index]); |
| 169 |
printf(" difference %20.16f\n", ritz[index] - ritz_sav[index]); |
| 170 |
printf("---------------------------------------------------\n"); |
| 171 |
} |
| 172 |
if (DEBUG_EVECS > 2) { |
| 173 |
printf(" index %d, bisection steps %d, root %20.16f\n", index, steps, ritz[index]); |
| 174 |
} |
| 175 |
tot_steps += steps; |
| 176 |
} |
| 177 |
if (DEBUG_EVECS > 2) { |
| 178 |
printf(" Total number of bisection steps %d.\n", tot_steps); |
| 179 |
} |
| 180 |
|
| 181 |
return (0); /* ... things seem ok. */ |
| 182 |
} |