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
|
/* Ergo, version 3.8.2, a program for linear scaling electronic structure
* calculations.
* Copyright (C) 2023 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 general.h
\brief Some general utilities used by other parts of the
hierarchical matrix library.
*/
#ifndef MAT_GENERAL
#define MAT_GENERAL
#include <cassert>
namespace mat {
template<class Treal>
static Treal maxdiff(const Treal* f1,const Treal* f2,int size) {
Treal diff = 0;
Treal tmpdiff;
for(int i = 0; i < size * size; i++) {
tmpdiff = template_blas_fabs(f1[i] - f2[i]);
if (tmpdiff > 0)
diff = (diff > tmpdiff ? diff : tmpdiff);
}
return diff;
}
template<class Treal>
static Treal maxdiff_tri(const Treal* f1,const Treal* f2,int size) {
Treal diff = 0;
Treal tmpdiff;
for (int col = 0; col < size; col++)
for (int row = 0; row < col + 1; row++) {
tmpdiff = template_blas_fabs(f1[col * size + row] - f2[col * size + row]);
diff = (diff > tmpdiff ? diff : tmpdiff);
}
return diff;
}
template<class Treal>
static Treal frobdiff(const Treal* f1,const Treal* f2,int size) {
Treal diff = 0;
Treal tmp;
for(int i = 0; i < size * size; i++) {
tmp = f1[i] - f2[i];
diff += tmp * tmp;
}
return template_blas_sqrt(diff);
}
#if 0
template<class T>
static void fileread(T *ptr,int size,FILE*) {
std::cout<<"error reading file"<<std::endl;
}
template<>
void fileread<double>(double *ptr,int size,FILE* file) {
fread(ptr,sizeof(double),size*size,file);
}
template<>
void fileread<float>(float *ptr,int size,FILE* file) {
double* tmpptr=new double [size*size];
fread(tmpptr,sizeof(double),size*size,file);
for (int i=0;i<size*size;i++)
{
ptr[i]=(float)tmpptr[i];
}
delete[] tmpptr;
}
#else
template<typename Treal, typename Trealonfile>
static void fileread(Treal *ptr, int size, FILE* file) {
if (sizeof(Trealonfile) == sizeof(Treal))
fread(ptr,sizeof(Treal),size,file);
else {
Trealonfile* tmpptr=new Trealonfile[size];
fread(tmpptr,sizeof(Trealonfile),size,file);
for (int i = 0; i < size; i++) {
ptr[i]=(Treal)tmpptr[i];
}
delete[] tmpptr;
}
}
#endif
template<typename Treal, typename Tmatrix>
static void read_matrix(Tmatrix& A,
char const * const matrixPath,
int const size) {
FILE* matrixfile=fopen(matrixPath,"rb");
if (!matrixfile) {
throw Failure("read_matrix: Cannot open inputfile");
}
Treal* matrixfull = new Treal [size*size];
fileread<Treal, double>(matrixfull, size*size, matrixfile);
/* A must already have built data structure */
A.assign_from_full(matrixfull, size, size);
delete[] matrixfull;
return;
}
template<typename Treal, typename Trealonfile, typename Tmatrix>
static void read_sparse_matrix(Tmatrix& A,
char const * const rowPath,
char const * const colPath,
char const * const valPath,
int const nval) {
FILE* rowfile=fopen(rowPath,"rb");
if (!rowfile) {
throw Failure("read_matrix: Cannot open inputfile rowfile");
}
FILE* colfile=fopen(colPath,"rb");
if (!colfile) {
throw Failure("read_matrix: Cannot open inputfile colfile");
}
FILE* valfile=fopen(valPath,"rb");
if (!valfile) {
throw Failure("read_matrix: Cannot open inputfile valfile");
}
int* row = new int[nval];
int* col = new int[nval];
Treal* val = new Treal[nval];
fileread<int, int>(row, nval, rowfile);
fileread<int, int>(col, nval, colfile);
fileread<Treal, Trealonfile>(val, nval, valfile);
/* A must already have built data structure */
A.assign_from_sparse(row, col, val, nval);
#if 0
Treal* compval = new Treal[nval];
A.get_values(row, col, compval, nval);
Treal maxdiff = 0;
Treal diff;
for (int i = 0; i < nval; i++) {
diff = template_blas_fabs(compval[i] - val[i]);
maxdiff = diff > maxdiff ? diff : maxdiff;
}
std::cout<<"Maxdiff: "<<maxdiff<<std::endl;
#endif
delete[] row;
delete[] col;
delete[] val;
return;
}
template<typename Treal>
static void read_xyz(Treal* x, Treal* y, Treal* z,
char * atomsPath,
int const natoms,
int const size) {
char* atomfile(atomsPath);
std::ifstream input(atomfile);
if (!input) {
throw Failure("read_xyz: Cannot open inputfile");
}
input >> std::setprecision(10);
Treal* xtmp = new Treal[natoms];
Treal* ytmp = new Treal[natoms];
Treal* ztmp = new Treal[natoms];
int* atomstart = new int[natoms+1];
for(int i = 0 ; i < natoms ; i++) {
input >> x[i];
input >> y[i];
input >> z[i];
input >> atomstart[i];
}
atomstart[natoms] = size;
for (int atom = 0; atom < natoms; atom++)
for (int bf = atomstart[atom]; bf < atomstart[atom + 1]; bf++) {
x[bf] = x[atom];
y[bf] = y[atom];
z[bf] = z[atom];
}
delete[] xtmp;
delete[] ytmp;
delete[] ztmp;
delete[] atomstart;
}
} /* end namespace mat */
#endif
|