1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
/* Ergo, version 3.8, a program for linear scaling electronic structure
* calculations.
* Copyright (C) 2019 Elias Rudberg, Emanuel H. Rubensson, Pawel Salek,
* and Anastasia Kruchinina.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Primary academic reference:
* Ergo: An open-source program for linear-scaling electronic structure
* calculations,
* Elias Rudberg, Emanuel H. Rubensson, Pawel Salek, and Anastasia
* Kruchinina,
* SoftwareX 7, 107 (2018),
* <http://dx.doi.org/10.1016/j.softx.2018.03.005>
*
* For further information about Ergo, see <http://www.ergoscf.org>.
*/
/** @file MatrixTridiagSymmetric.h Class for tridiagonal symmetric matrices
*
* Copyright(c) Emanuel Rubensson 2006
*
* @author Emanuel Rubensson
* @date December 2006
*
*/
#ifndef MAT_MATRIXTRIDIAGSYMMETRIC
#define MAT_MATRIXTRIDIAGSYMMETRIC
#include "mat_gblas.h"
namespace mat { /* Matrix namespace */
namespace arn { /* Arnoldi type methods namespace */
/** Tridiagonal symmetric matrix class template
*
*/
template<typename Treal>
class MatrixTridiagSymmetric {
public:
explicit MatrixTridiagSymmetric(int k = 100)
: alphaVec(new Treal[k]), betaVec(new Treal[k]),
size(0), capacity(k) {}
void increase(Treal const & alpha, Treal const & beta) {
if (size + 1 > capacity)
increaseCapacity(capacity * 2);
++size;
alphaVec[size - 1] = alpha;
betaVec[size - 1] = beta;
}
virtual ~MatrixTridiagSymmetric() {
delete[] alphaVec;
delete[] betaVec;
}
void update_beta(Treal const & beta)
{
betaVec[size-1] = beta;
}
void getEigsByInterval(Treal* eigVals,
Treal* eigVectors,
Treal* acc,
int & nEigsFound,
Treal const lowBound,
Treal const uppBound,
Treal const abstol = 0) const;
void getEigsByIndex(Treal* eigVals,
Treal* eigVectors,
Treal* acc,
int const lowInd,
int const uppInd,
Treal const abstol = 0) const;
inline void clear() {
size = 0;
}
protected:
Treal* alphaVec;
Treal* betaVec;
int size;
int capacity;
void increaseCapacity(int const newCapacity);
private:
};
template<typename Treal>
void MatrixTridiagSymmetric<Treal>::
getEigsByInterval(Treal* eigVals, /* length: >= nEigsFound */
Treal* eigVectors, /* length: >= size * nEigsFound */
Treal* acc, /* length: size */
int & nEigsFound, /* The number of found eigenpairs. */
Treal const lowBound,
Treal const uppBound,
Treal const absTol) const {
Treal* eigArray = new Treal[size];
Treal* alphaCopy = new Treal[size];
Treal* betaCopy = new Treal[size];
Treal* work = new Treal[5 * size];
int* iwork = new int[5 * size];
int* ifail = new int[size];
for (int ind = 0; ind < size; ind++){
alphaCopy[ind] = alphaVec[ind];
betaCopy[ind] = betaVec[ind];
}
int dummy = -1;
int info;
/* Find eigenvalues */
/* FIXME: change to stevr */
mat::stevx("V", "V", &size, alphaCopy, betaCopy,
&lowBound, &uppBound, &dummy, &dummy,
&absTol,
&nEigsFound, eigArray, eigVectors, &size,
work, iwork, ifail,
&info);
assert(info == 0);
for (int ind = 0; ind < nEigsFound; ind++) {
eigVals[ind] = eigArray[ind];
acc[ind] = betaCopy[size - 1] *
template_blas_fabs(eigVectors[(ind * size) + size - 1]) / 0.9; // FIXME: WHY IS THERE A FACTOR 0.9 HERE ?!?
}
delete[] eigArray;
delete[] alphaCopy;
delete[] betaCopy;
delete[] work;
delete[] iwork;
delete[] ifail;
}
template<typename Treal>
void MatrixTridiagSymmetric<Treal>::
getEigsByIndex(Treal* eigVals, /* length: uppInd-lowInd+1 */
Treal* eigVectors, /* length: size*(uppInd-lowInd+1) */
Treal* acc, /* length: size */
int const lowInd,
int const uppInd,
Treal const abstol) const {
Treal* eigArray = new Treal[size];
Treal* alphaCopy = new Treal[size];
Treal* betaCopy = new Treal[size];
for (int ind = 0; ind < size; ind++){
alphaCopy[ind] = alphaVec[ind];
betaCopy[ind] = betaVec[ind];
}
#if 1
// Emanuel note 2010-03-14:
// The following code uses stevr instead of stevx for two reasons:
// 1) Due to a bug in LAPACK we previously computed all
// eigenvalues (see Elias' note below) which turned out to be
// too time consuming in some cases.
// 2) Contrary to stevx, stevr should never fail to compute the
// desired eigenpairs unless there is a bug in the implementation
// or erroneous input.
int const lowIndNew(lowInd + 1);
int const uppIndNew(uppInd + 1);
int nEigsWanted = uppInd - lowInd + 1;
int nEigsFound = 0;
int* isuppz = new int[2*nEigsWanted];
Treal* work;
int lwork = -1;
int* iwork;
int liwork = -1;
Treal dummy = -1.0;
int info;
// First do a workspace query:
Treal work_query;
int iwork_query;
mat::stevr("V", "I", &size, alphaCopy, betaCopy,
&dummy, &dummy, &lowIndNew, &uppIndNew,
&abstol,
&nEigsFound, eigArray, eigVectors, &size,
isuppz,
&work_query, &lwork, &iwork_query, &liwork, &info);
lwork = int(work_query);
liwork = iwork_query;
work = new Treal[lwork];
iwork = new int[liwork];
mat::stevr("V", "I", &size, alphaCopy, betaCopy,
&dummy, &dummy, &lowIndNew, &uppIndNew,
&abstol,
&nEigsFound, eigArray, eigVectors, &size,
isuppz,
work, &lwork, iwork, &liwork, &info);
if (info)
std::cout << "info = " << info <<std::endl;
assert(info == 0);
assert(nEigsFound == nEigsWanted);
for (int ind = 0; ind < nEigsFound; ind++) {
eigVals[ind] = eigArray[ind];
acc[ind] = betaCopy[size - 1] *
template_blas_fabs(eigVectors[(ind * size) + size - 1]) / 0.9; // FIXME: WHY IS THERE A FACTOR 0.9 HERE ?!?
}
delete[] eigArray;
delete[] alphaCopy;
delete[] betaCopy;
delete[] isuppz;
delete[] work;
delete[] iwork;
#else
Treal* work = new Treal[5 * size];
int* iwork = new int[5 * size];
int* ifail = new int[size];
Treal dummy = -1.0;
int info;
int nEigsFound = 0;
/*
Elias note 2007-07-02:
There have been some problems with stevx returning with info=0
at the same time as nEigsFound != uppInd - lowInd + 1.
This is due to a bug in LAPACK which has been reported and confirmed.
To avoid this problem Elias changed the code so that ALL eigenvectors
are computed and then the desired ones are selected from the
complete list.
*/
#if 1
/* Original version of code calling stevx to get only the
desired eigenvalues/vectors. */
int const lowIndNew(lowInd + 1);
int const uppIndNew(uppInd + 1);
mat::stevx("V", "I", &size, alphaCopy, betaCopy,
&dummy, &dummy, &lowIndNew, &uppIndNew,
&abstol,
&nEigsFound, eigArray, eigVectors, &size,
work, iwork, ifail,
&info);
assert(info == 0);
assert(nEigsFound == uppInd - lowInd + 1);
for (int ind = 0; ind < nEigsFound; ind++) {
eigVals[ind] = eigArray[ind];
acc[ind] = betaCopy[size - 1] *
template_blas_fabs(eigVectors[(ind * size) + size - 1]) / 0.9; // FIXME: WHY IS THERE A FACTOR 0.9 HERE ?!?
}
#else
/* Modified version of code calling stevx to get ALL
eigenvalues/vectors, and then picking out the desired ones. */
Treal* eigVectorsTmp = new Treal[size*size];
int const lowIndNew(1);
int const uppIndNew(size);
mat::stevx("V", "A", &size, alphaCopy, betaCopy,
&dummy, &dummy, &lowIndNew, &uppIndNew,
&abstol,
&nEigsFound, eigArray, eigVectorsTmp, &size,
work, iwork, ifail,
&info);
assert(info == 0);
assert(nEigsFound == size);
int nEigsWanted = uppInd - lowInd + 1;
/* Copy desired eigenvectors from eigVectorsTmp to eigVectors. */
for(int i = 0; i < nEigsWanted; i++)
for(int j = 0; j < size; j++)
eigVectors[i*size+j] = eigVectorsTmp[(lowInd+i)*size+j];
delete [] eigVectorsTmp;
for (int ind = 0; ind < nEigsWanted; ind++) {
eigVals[ind] = eigArray[lowInd+ind];
acc[ind] = betaCopy[size - 1] *
template_blas_fabs(eigVectors[(ind * size) + size - 1]) / 0.9; // FIXME: WHY IS THERE A FACTOR 0.9 HERE ?!?
}
#endif
delete[] eigArray;
delete[] alphaCopy;
delete[] betaCopy;
delete[] work;
delete[] iwork;
delete[] ifail;
#endif
}
template<typename Treal>
void MatrixTridiagSymmetric<Treal>::
increaseCapacity(int const newCapacity) {
capacity = newCapacity;
Treal* alphaNew = new Treal[capacity];
Treal* betaNew = new Treal[capacity];
for (int ind = 0; ind < size; ind++){
alphaNew[ind] = alphaVec[ind];
betaNew[ind] = betaVec[ind];
}
delete[] alphaVec;
delete[] betaVec;
alphaVec = alphaNew;
betaVec = betaNew;
}
} /* end namespace arn */
} /* end namespace mat */
#endif
|