2021-02-16 10:49:15 +01:00
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include "hdf5/serial/hdf5.h"
|
2021-02-28 09:28:42 +01:00
|
|
|
#include "hdf5/serial/H5Cpp.h"
|
2021-02-16 10:49:15 +01:00
|
|
|
|
2021-02-26 17:28:52 +01:00
|
|
|
#include "SM_MaponiA3.hpp"
|
2021-03-05 17:00:48 +01:00
|
|
|
#include "SM_Standard.hpp"
|
2021-02-26 17:28:52 +01:00
|
|
|
#include "Helpers.hpp"
|
2021-02-16 10:49:15 +01:00
|
|
|
|
|
|
|
using namespace H5;
|
2021-03-12 16:18:03 +01:00
|
|
|
// #define DEBUG
|
2021-02-16 10:49:15 +01:00
|
|
|
|
2021-03-18 14:05:58 +01:00
|
|
|
const H5std_string FILE_NAME( "dataset.hdf5" );
|
2021-02-16 10:49:15 +01:00
|
|
|
|
2021-03-18 14:05:58 +01:00
|
|
|
double residual_max(double * A, unsigned int Dim) {
|
|
|
|
double max= 0.0;
|
|
|
|
for (unsigned int i = 0; i < Dim; i++) {
|
|
|
|
for (unsigned int j = 0; j < Dim; j++) {
|
|
|
|
double delta = (A[i * Dim + j] - (i == j));
|
|
|
|
if (delta > max) max = delta;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return max;
|
|
|
|
}
|
2021-03-15 14:01:38 +01:00
|
|
|
|
|
|
|
double residual2(double * A, unsigned int Dim) {
|
|
|
|
double res = 0.0;
|
|
|
|
for (unsigned int i = 0; i < Dim; i++) {
|
|
|
|
for (unsigned int j = 0; j < Dim; j++) {
|
|
|
|
double delta = (A[i * Dim + j] - (i == j));
|
|
|
|
res += delta*delta;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-02-16 10:49:15 +01:00
|
|
|
void read_int(H5File file, std::string key, unsigned int * data) {
|
|
|
|
DataSet ds = file.openDataSet(key);
|
|
|
|
ds.read(data, PredType::STD_U32LE);
|
|
|
|
ds.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void read_double(H5File file, std::string key, double * data) {
|
|
|
|
DataSet ds = file.openDataSet(key);
|
|
|
|
ds.read(data, PredType::IEEE_F64LE);
|
|
|
|
ds.close();
|
|
|
|
}
|
|
|
|
|
2021-03-15 15:59:20 +01:00
|
|
|
int test_cycle(H5File file, int cycle, std::string version) {
|
2021-02-16 10:49:15 +01:00
|
|
|
|
|
|
|
/* Read the data */
|
|
|
|
|
|
|
|
std::string group = "cycle_" + std::to_string(cycle);
|
|
|
|
|
2021-02-24 18:41:48 +01:00
|
|
|
unsigned int dim, nupdates, col, i, j;
|
2021-02-16 10:49:15 +01:00
|
|
|
read_int(file, group + "/slater_matrix_dim", &dim);
|
|
|
|
read_int(file, group + "/nupdates", &nupdates);
|
|
|
|
|
|
|
|
double * slater_matrix = new double[dim*dim];
|
|
|
|
read_double(file, group + "/slater_matrix", slater_matrix);
|
|
|
|
|
|
|
|
double * slater_inverse = new double[dim*dim];
|
|
|
|
read_double(file, group + "/slater_inverse", slater_inverse);
|
2021-03-18 14:05:58 +01:00
|
|
|
//slater_inverse = transpose(slater_inverse, dim);
|
2021-02-16 10:49:15 +01:00
|
|
|
|
|
|
|
unsigned int * col_update_index = new unsigned int[nupdates];
|
|
|
|
read_int(file, group + "/col_update_index", col_update_index);
|
|
|
|
|
|
|
|
double * updates = new double[nupdates*dim];
|
|
|
|
read_double(file, group + "/updates", updates);
|
|
|
|
|
2021-03-12 16:18:03 +01:00
|
|
|
double * u = new double[nupdates*dim];
|
|
|
|
|
2021-02-16 10:49:15 +01:00
|
|
|
/* Test */
|
|
|
|
#ifdef DEBUG
|
2021-02-25 12:17:45 +01:00
|
|
|
showMatrix(slater_matrix, dim, "OLD Slater");
|
2021-02-16 10:49:15 +01:00
|
|
|
#endif
|
|
|
|
|
2021-02-24 18:41:48 +01:00
|
|
|
#ifdef DEBUG
|
2021-02-25 12:17:45 +01:00
|
|
|
showMatrix(slater_inverse, dim, "OLD Inverse");
|
2021-02-24 18:41:48 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
for (j = 0; j < nupdates; j++) {
|
|
|
|
for (i = 0; i < dim; i++) {
|
|
|
|
col = col_update_index[j];
|
2021-03-12 16:18:03 +01:00
|
|
|
u[i + j*dim] = updates[i + j*dim] - slater_matrix[i*dim + (col - 1)];
|
|
|
|
slater_matrix[i*dim + (col - 1)] = updates[i + j*dim];
|
2021-02-24 18:41:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 15:59:20 +01:00
|
|
|
if (version == "maponia3") {
|
|
|
|
MaponiA3(slater_inverse, dim, nupdates, u, col_update_index);
|
|
|
|
} else if (version == "sm1") {
|
|
|
|
SM1(slater_inverse, dim, nupdates, u, col_update_index);
|
|
|
|
} else if (version == "sm2") {
|
|
|
|
SM2(slater_inverse, dim, nupdates, u, col_update_index);
|
|
|
|
} else if (version == "sm3") {
|
|
|
|
SM3(slater_inverse, dim, nupdates, u, col_update_index);
|
|
|
|
} else {
|
|
|
|
std::cerr << "Unknown version " << version << std::endl;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2021-02-24 18:41:48 +01:00
|
|
|
#ifdef DEBUG
|
2021-02-25 12:17:45 +01:00
|
|
|
showMatrix(slater_matrix, dim, "NEW Slater");
|
2021-02-24 18:41:48 +01:00
|
|
|
#endif
|
|
|
|
|
2021-02-16 10:49:15 +01:00
|
|
|
#ifdef DEBUG
|
2021-02-25 12:17:45 +01:00
|
|
|
showMatrix(slater_inverse, dim, "NEW Inverse");
|
2021-02-16 10:49:15 +01:00
|
|
|
#endif
|
|
|
|
|
2021-03-11 15:04:35 +01:00
|
|
|
double * res = new double[dim*dim] {0};
|
|
|
|
matMul(slater_matrix, slater_inverse, res, dim);
|
2021-03-15 15:59:20 +01:00
|
|
|
bool ok = is_identity(res, dim, 1e-3);
|
2021-03-15 14:01:38 +01:00
|
|
|
|
2021-03-18 14:05:58 +01:00
|
|
|
double res_max = residual_max(res, dim);
|
2021-03-15 14:01:38 +01:00
|
|
|
double res2 = residual2(res, dim);
|
2021-03-18 14:05:58 +01:00
|
|
|
std::cout << "Residual = " << version << " " << cycle << " " << res_max << " " << res2 << std::endl;
|
2021-02-16 10:49:15 +01:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
showMatrix(res, dim, "Result");
|
|
|
|
#endif
|
|
|
|
|
2021-03-12 16:18:03 +01:00
|
|
|
delete [] res, updates, u, col_update_index,
|
2021-02-25 12:17:45 +01:00
|
|
|
slater_matrix, slater_inverse;
|
2021-02-16 10:49:15 +01:00
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2021-03-15 15:59:20 +01:00
|
|
|
if (argc != 4) {
|
2021-02-28 09:28:42 +01:00
|
|
|
std::cerr << "Execute from within 'datasets/'" << std::endl;
|
2021-03-15 15:59:20 +01:00
|
|
|
std::cerr << "usage: test_internal_h5 <version> <start cycle> <stop cycle>" << std::endl;
|
2021-02-16 10:49:15 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2021-03-15 15:59:20 +01:00
|
|
|
std::string version(argv[1]);
|
|
|
|
int start_cycle = std::stoi(argv[2]);
|
|
|
|
int stop_cycle = std::stoi(argv[3]);
|
2021-02-16 10:49:15 +01:00
|
|
|
H5File file(FILE_NAME, H5F_ACC_RDONLY);
|
|
|
|
|
2021-02-28 09:28:42 +01:00
|
|
|
bool ok;
|
|
|
|
for (int cycle = start_cycle; cycle < stop_cycle+1; cycle++) {
|
2021-03-15 15:59:20 +01:00
|
|
|
ok = test_cycle(file, cycle, version);
|
2021-02-28 09:28:42 +01:00
|
|
|
if (ok) {
|
|
|
|
std::cerr << "ok -- cycle " << std::to_string(cycle)
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
std::cerr << "failed -- cycle " << std::to_string(cycle)
|
|
|
|
<< std::endl;
|
|
|
|
}
|
2021-02-16 10:49:15 +01:00
|
|
|
}
|
2021-02-28 09:28:42 +01:00
|
|
|
|
2021-02-16 10:49:15 +01:00
|
|
|
return ok;
|
|
|
|
}
|