Skip to content

Commit 1067aaf

Browse files
Merge release (#293)
* Fix EnergyAndForces tests (#277) * have them work in debug mode too * Move factor 4pi out og linear solvers (#278) * Move some code into PoissonSolverFactory (#279) * Clean up class Potentials (#280) * Clean up class Ions, add test for it (#281) * Add test MD_MVP (#290) * Clean up code related to DM restart data (#292) * Write dm (#291) * Update use of DM in restart * remove a redundant assignment in DensityMatrix::DensityMatrix --------- Co-authored-by: Jean-Luc Fattebert <[email protected]>
1 parent 77ff46f commit 1067aaf

22 files changed

+566
-301
lines changed

src/DensityMatrix.cc

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include "DistMatrix.h"
1313
#include "MGmol_MPI.h"
1414
#include "ReplicatedMatrix.h"
15+
#include "ReplicatedWorkSpace.h"
16+
#include "hdf_tools.h"
1517

1618
#include <cmath>
1719
#include <iomanip>
@@ -22,25 +24,32 @@ const double factor_kernel4dot = 10.;
2224

2325
#define PROCRUSTES 0
2426

27+
#define MGMOL_DENSITYMATRIX_FAIL(X) \
28+
{ \
29+
std::cerr << "DensityMatrix failure:" << std::endl; \
30+
std::cerr << "Error Message: " << X << std::endl; \
31+
}
32+
2533
// occupations in [0,1]
2634
// DM eigenvalues in [0,orbital_occupation]
2735

2836
template <class MatrixType>
2937
DensityMatrix<MatrixType>::DensityMatrix(const int ndim)
38+
: dim_(ndim),
39+
orbitals_index_(-1),
40+
occ_uptodate_(false),
41+
uniform_occ_(false),
42+
stripped_(false)
3043
{
3144
assert(ndim > 0);
3245

33-
dim_ = ndim;
34-
3546
occ_uptodate_ = false;
3647
stripped_ = false;
3748
uniform_occ_ = false;
3849

3950
MGmol_MPI& mmpi = *(MGmol_MPI::instance());
4051
orbital_occupation_ = mmpi.nspin() > 1 ? 1. : 2.;
4152

42-
orbitals_index_ = -1;
43-
4453
dm_ = new MatrixType("DM", ndim, ndim);
4554
kernel4dot_ = new MatrixType("K4dot", ndim, ndim);
4655
work_ = new MatrixType("work", ndim, ndim);
@@ -438,6 +447,43 @@ void DensityMatrix<MatrixType>::mix(
438447
orbitals_index_ = new_orbitals_index;
439448
}
440449

450+
template <class MatrixType>
451+
int DensityMatrix<MatrixType>::write(HDFrestart& h5f_file, std::string& name)
452+
{
453+
ReplicatedWorkSpace<double>& wspace(
454+
ReplicatedWorkSpace<double>::instance());
455+
456+
wspace.initSquareMatrix(*dm_);
457+
458+
DISTMATDTYPE* work_matrix = wspace.square_matrix();
459+
460+
hid_t file_id = h5f_file.file_id();
461+
return mgmol_tools::write_matrix(file_id, name, work_matrix, dim_);
462+
}
463+
464+
template <class MatrixType>
465+
int DensityMatrix<MatrixType>::read(HDFrestart& h5f_file, std::string& name)
466+
{
467+
ReplicatedWorkSpace<double>& wspace(
468+
ReplicatedWorkSpace<double>::instance());
469+
DISTMATDTYPE* work_matrix = wspace.square_matrix();
470+
471+
MGmol_MPI& mmpi = *(MGmol_MPI::instance());
472+
473+
int ierr = 0;
474+
if (mmpi.instancePE0())
475+
{
476+
hid_t file_id = h5f_file.file_id();
477+
ierr = mgmol_tools::read_matrix(file_id, name, work_matrix);
478+
}
479+
mmpi.bcast(&ierr, 1);
480+
481+
if (ierr >= 0) wspace.mpiBcastSquareMatrix();
482+
if (ierr >= 0) initMatrix(work_matrix);
483+
484+
return ierr;
485+
}
486+
441487
template class DensityMatrix<dist_matrix::DistMatrix<double>>;
442488
#ifdef HAVE_MAGMA
443489
template class DensityMatrix<ReplicatedMatrix>;

src/DensityMatrix.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#ifndef MGMOL_DENSITYMATRIX_H
1111
#define MGMOL_DENSITYMATRIX_H
1212

13+
#include "HDFrestart.h"
1314
#include "MGmol_MPI.h"
1415
#include "global.h"
1516

@@ -23,7 +24,7 @@
2324
template <class MatrixType>
2425
class DensityMatrix
2526
{
26-
int dim_;
27+
const int dim_;
2728
std::vector<double> occupation_;
2829

2930
MatrixType* dm_;
@@ -35,6 +36,10 @@ class DensityMatrix
3536
bool occ_uptodate_;
3637
bool uniform_occ_;
3738
bool stripped_;
39+
40+
/*!
41+
* Max. occupation of an orbital: 1 with spin, 2 otherwise
42+
*/
3843
double orbital_occupation_;
3944

4045
DensityMatrix();
@@ -146,6 +151,9 @@ class DensityMatrix
146151
double getExpectation(const MatrixType& A);
147152
void mix(
148153
const double mix, const MatrixType& matA, const int new_orbitals_index);
154+
155+
int write(HDFrestart& h5f_file, std::string& name);
156+
int read(HDFrestart& h5f_file, std::string& name);
149157
};
150158

151159
#endif

src/ExtendedGridOrbitals.cc

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,6 @@ int ExtendedGridOrbitals::read_hdf5(HDFrestart& h5f_file)
584584

585585
Control& ct = *(Control::instance());
586586

587-
hid_t file_id = h5f_file.file_id();
588587
std::string name = "Function";
589588
int ierr = read_func_hdf5(h5f_file, name);
590589
if (ierr < 0)
@@ -603,7 +602,7 @@ int ExtendedGridOrbitals::read_hdf5(HDFrestart& h5f_file)
603602
// Read DM
604603
if (!ct.fullyOccupied())
605604
{
606-
ierr = proj_matrices_->read_dm_hdf5(file_id);
605+
ierr = proj_matrices_->readDM(h5f_file);
607606
if (ierr < 0)
608607
{
609608
(*MPIdata::serr)
@@ -618,28 +617,7 @@ int ExtendedGridOrbitals::read_hdf5(HDFrestart& h5f_file)
618617
return ierr;
619618
}
620619

621-
int ExtendedGridOrbitals::write_hdf5(
622-
HDFrestart& h5f_file, const std::string& name)
623-
{
624-
assert(proj_matrices_ != nullptr);
625-
Control& ct = *(Control::instance());
626-
627-
if (!ct.fullyOccupied())
628-
{
629-
MGmol_MPI& mmpi(*(MGmol_MPI::instance()));
630-
mmpi.barrier();
631-
632-
int ierr = proj_matrices_->writeDM_hdf5(h5f_file);
633-
if (ierr < 0) return ierr;
634-
}
635-
636-
int ierr = write_func_hdf5(h5f_file, name);
637-
638-
return ierr;
639-
}
640-
641-
int ExtendedGridOrbitals::write_func_hdf5(
642-
HDFrestart& h5f_file, const std::string& name)
620+
int ExtendedGridOrbitals::write(HDFrestart& h5f_file, const std::string& name)
643621
{
644622
if (onpe0)
645623
(*MPIdata::sout) << "ExtendedGridOrbitals::write_func_hdf5()...\n";
@@ -829,7 +807,7 @@ int ExtendedGridOrbitals::read_func_hdf5(
829807
const std::string datasetname(getDatasetName(name, icolor));
830808

831809
// check if dataset exists...
832-
int err_id = h5f_file.dset_exists(datasetname);
810+
int err_id = h5f_file.checkDataExists(datasetname);
833811
if (h5f_file.gatherDataX()) mmpi.bcast(&err_id, 1);
834812
if (err_id == 0) break; // dataset does not exists
835813

src/ExtendedGridOrbitals.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,7 @@ class ExtendedGridOrbitals : public Orbitals
374374
void multiplyByMatrix2states(const int st1, const int st2,
375375
const double* mat, ExtendedGridOrbitals& product);
376376

377-
int write_hdf5(HDFrestart& h5f_file, const std::string& name = "Function");
378-
int write_func_hdf5(HDFrestart&, const std::string& name = "Function");
377+
int write(HDFrestart&, const std::string& name = "Function");
379378
int read_hdf5(HDFrestart& h5f_file);
380379
int read_func_hdf5(HDFrestart&, const std::string& name = "Function");
381380

src/HDFrestart.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -933,12 +933,12 @@ int HDFrestart::getLRCenters(std::multimap<std::string, Vector3D>& centers,
933933

934934
std::string datasetname(getDatasetName(name, color));
935935

936-
int err_id = dset_exists(datasetname);
936+
int err_id = checkDataExistsLocal(datasetname);
937937
if (err_id == 0)
938938
{ // dataset does not exists
939939
// try older version
940940
datasetname = getDatasetName_old(name, color);
941-
err_id = dset_exists(datasetname);
941+
err_id = checkDataExistsLocal(datasetname);
942942
}
943943

944944
if (err_id == 0)
@@ -1052,12 +1052,12 @@ int HDFrestart::getLRs(std::shared_ptr<LocalizationRegions> lrs,
10521052

10531053
std::string datasetname(getDatasetName(name, color));
10541054

1055-
int err_id = dset_exists(datasetname);
1055+
int err_id = checkDataExistsLocal(datasetname);
10561056
if (err_id == 0)
10571057
{ // dataset does not exists
10581058
// try older version
10591059
datasetname = getDatasetName_old(name, color);
1060-
err_id = dset_exists(datasetname);
1060+
err_id = checkDataExistsLocal(datasetname);
10611061
}
10621062
if (err_id == 0)
10631063
{ // dataset does not exists

src/HDFrestart.h

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -155,28 +155,30 @@ class HDFrestart
155155
return 0;
156156
}
157157

158-
hid_t dset_exists(const std::string& datasetname) const
158+
hid_t checkDataExists(const std::string& datasetname) const
159159
{
160-
if (active_)
161-
{
162-
return dset_exists(datasetname.c_str());
163-
}
164-
return 0;
160+
return checkDataExists(datasetname.c_str());
165161
}
166-
herr_t dset_exists(const char* const datasetname) const
162+
herr_t checkDataExists(const char* const datasetname) const
163+
{
164+
herr_t err_id = checkDataExistsLocal(datasetname);
165+
166+
short id = (short)err_id;
167+
MPI_Bcast(&id, 1, MPI_SHORT, 0, comm_data_);
168+
return (herr_t)id;
169+
}
170+
171+
hid_t checkDataExistsLocal(const std::string& datasetname) const
172+
{
173+
return checkDataExistsLocal(datasetname.c_str());
174+
}
175+
176+
herr_t checkDataExistsLocal(const char* const datasetname) const
167177
{
168178
herr_t err_id = 0;
169179
if (active_)
170180
{
171181
err_id = H5LTfind_dataset(file_id_, datasetname);
172-
173-
if (err_id < 0)
174-
{
175-
if (onpe0)
176-
(*MPIdata::sout)
177-
<< "HDFrestart::dset_exists() failed for dataset "
178-
<< datasetname << std::endl;
179-
}
180182
}
181183
return err_id;
182184
}

src/LBFGS_IonicStepper.cc

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -328,11 +328,11 @@ int LBFGS_IonicStepper::read_lbfgs(HDFrestart& h5f_file)
328328

329329
short check_data = 0;
330330
std::vector<double> attr_d(16);
331+
// Open an existing dataset.
332+
std::string datasetname("/LBFGS");
333+
int err_id = h5f_file.checkDataExists(datasetname);
331334
if (onpe0)
332335
{
333-
// Open an existing dataset.
334-
std::string datasetname("/LBFGS");
335-
int err_id = h5f_file.dset_exists(datasetname);
336336
if (err_id < 0)
337337
{ // dataset does not exists
338338
(*MPIdata::sout) << "Warning: no dataset " << datasetname
@@ -343,10 +343,9 @@ int LBFGS_IonicStepper::read_lbfgs(HDFrestart& h5f_file)
343343
dataset_id = H5Dopen2(file_id, "/LBFGS", H5P_DEFAULT);
344344
if (dataset_id < 0)
345345
{
346-
if (onpe0)
347-
(*MPIdata::sout) << "Warning: H5Dopen failed for /LBFGS-> "
348-
"no restart info for LBFGS"
349-
<< std::endl;
346+
(*MPIdata::sout) << "Warning: H5Dopen failed for /LBFGS-> "
347+
"no restart info for LBFGS"
348+
<< std::endl;
350349
}
351350
else
352351
{

src/LocGridOrbitals.cc

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,6 @@ int LocGridOrbitals::read_hdf5(HDFrestart& h5f_file)
966966

967967
Control& ct = *(Control::instance());
968968

969-
hid_t file_id = h5f_file.file_id();
970969
std::string name = "Function";
971970
int ierr = read_func_hdf5(h5f_file, name);
972971
if (ierr < 0)
@@ -984,7 +983,7 @@ int LocGridOrbitals::read_hdf5(HDFrestart& h5f_file)
984983
// Read DM
985984
if (!ct.fullyOccupied())
986985
{
987-
ierr = proj_matrices_->read_dm_hdf5(file_id);
986+
ierr = proj_matrices_->readDM(h5f_file);
988987
if (ierr < 0)
989988
{
990989
(*MPIdata::serr)
@@ -997,27 +996,7 @@ int LocGridOrbitals::read_hdf5(HDFrestart& h5f_file)
997996
return ierr;
998997
}
999998

1000-
int LocGridOrbitals::write_hdf5(HDFrestart& h5f_file, const std::string& name)
1001-
{
1002-
assert(proj_matrices_ != nullptr);
1003-
Control& ct = *(Control::instance());
1004-
1005-
if (!ct.fullyOccupied())
1006-
{
1007-
MGmol_MPI& mmpi(*(MGmol_MPI::instance()));
1008-
mmpi.barrier();
1009-
1010-
int ierr = proj_matrices_->writeDM_hdf5(h5f_file);
1011-
if (ierr < 0) return ierr;
1012-
}
1013-
1014-
int ierr = write_func_hdf5(h5f_file, name);
1015-
1016-
return ierr;
1017-
}
1018-
1019-
int LocGridOrbitals::write_func_hdf5(
1020-
HDFrestart& h5f_file, const std::string& name)
999+
int LocGridOrbitals::write(HDFrestart& h5f_file, const std::string& name)
10211000
{
10221001
Control& ct = *(Control::instance());
10231002
hid_t file_id = h5f_file.file_id();
@@ -1249,7 +1228,7 @@ int LocGridOrbitals::read_func_hdf5(
12491228
const std::string key(itcenter->first);
12501229

12511230
// checkif dataset exists...
1252-
int err_id = h5f_file.dset_exists(key);
1231+
int err_id = h5f_file.checkDataExistsLocal(key);
12531232
if (h5f_file.gatherDataX()) mmpi.bcast(&err_id, 1);
12541233
if (err_id == 0) break; // dataset does not exists
12551234

src/LocGridOrbitals.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,7 @@ class LocGridOrbitals : public Orbitals
410410
void multiplyByMatrix2states(const int st1, const int st2,
411411
const double* mat, LocGridOrbitals& product);
412412

413-
int write_hdf5(HDFrestart& h5f_file, const std::string& name = "Function");
414-
int write_func_hdf5(HDFrestart&, const std::string& name = "Function");
413+
int write(HDFrestart&, const std::string& name = "Function");
415414
int read_hdf5(HDFrestart& h5f_file);
416415
int read_func_hdf5(HDFrestart&, const std::string& name = "Function");
417416

src/MGmol.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class MGmol : public MGmolInterface
133133
KBPsiMatrixSparse* kbpsi, dist_matrix::DistMatrix<DISTMATDTYPE>& hij);
134134
void computeHnlPhiAndAdd2HPhi(Ions& ions, OrbitalsType& phi,
135135
OrbitalsType& hphi, const KBPsiMatrixSparse* const kbpsi);
136-
int dumpMDrestartFile(OrbitalsType** orbitals, Ions& ions,
136+
int dumpMDrestartFile(OrbitalsType& orbitals, Ions& ions,
137137
Rho<OrbitalsType>& rho, const bool write_extrapolated_wf,
138138
const short count);
139139

0 commit comments

Comments
 (0)