Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix and test restart single hdf5 file #305

Merged
merged 1 commit into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Control.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ class Control
// 10 or larger means CG, otherwise MG V-cycles
bool MGPoissonSolver() { return (diel_flag_ / 10 == 0); }

bool LangevinThermostat() { return (thermostat_type == 1); }

//
// data
//
Expand Down
28 changes: 20 additions & 8 deletions src/HDFrestart.cc
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,8 @@ HDFrestart::HDFrestart(const std::string& filename, const pb::PEenv& pes,
verbosity_ = 0;
closed_ = false;

//(*MPIdata::sout)<<"HDFrestart::HDFrestart(), filename="<<filename<<endl;
//(*MPIdata::sout)<<"HDFrestart::HDFrestart(),
// filename="<<filename<<std::endl;
setActivity();

count_[0] = count_[1] = count_[2] = 1;
Expand Down Expand Up @@ -749,7 +750,7 @@ int writeListCentersAndRadii(
}

// if( onpe0 && ct.verbose>2 )
// (*MPIdata::sout)<<"Write attribute "<<attname<<endl;
// (*MPIdata::sout)<<"Write attribute "<<attname<<std::endl;
herr_t status = H5Awrite(attribute_id, H5T_NATIVE_DOUBLE, &attr_data[0]);
if (status < 0)
{
Expand Down Expand Up @@ -1921,14 +1922,19 @@ int HDFrestart::readAtomicData(std::string datasetname, std::vector<int>& data)
// send data to inactive PEs
if (gather_data_x_) gatherDataXdir(data);

if (useHdf5p())
{
data.erase(std::remove(data.begin(), data.end(), -1), data.end());
}

return 0;
}

int HDFrestart::readAtomicData(
std::string datasetname, std::vector<double>& data)
{
if (onpe0)
(*MPIdata::sout) << "Read ionic positions from hdf5 file" << std::endl;
(*MPIdata::sout) << "Read atomic data from hdf5 file" << std::endl;

if (active_)
{
Expand Down Expand Up @@ -1968,6 +1974,10 @@ int HDFrestart::readAtomicData(
}
}

if (useHdf5p())
{
data.erase(std::remove(data.begin(), data.end(), 1e+32), data.end());
}
if (gather_data_x_) gatherDataXdir(data);

return 0;
Expand Down Expand Up @@ -2094,6 +2104,11 @@ int HDFrestart::readAtomicData(
data.push_back(t);
}

if (useHdf5p())
{
data.erase(std::remove(data.begin(), data.end(), ""), data.end());
}

return 0;
}

Expand Down Expand Up @@ -2128,7 +2143,6 @@ int HDFrestart::readRestartRandomStates(std::vector<unsigned short>& data)
dim = (int)H5Dget_storage_size(dataset_id)
/ sizeof(unsigned short);
}

if (dim > 0)
{
data.resize(dim);
Expand All @@ -2148,12 +2162,10 @@ int HDFrestart::readRestartRandomStates(std::vector<unsigned short>& data)
}

if (!data.empty())
if (data[0] != data[0])
if (std::isnan(data[0]))
{
MGMOL_HDFRESTART_FAIL(
"ERROR: HDFrestart::readRestartRandomStates() "
"--- data[0]="
<< data[0]);
"readRestartRandomStates() is NaN");
return -2;
}
}
Expand Down
20 changes: 17 additions & 3 deletions src/Ions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -911,8 +911,10 @@ void Ions::initFromRestartFile(HDFrestart& h5_file)
assert(at_numbers.size() == at_nlprojIds.size());

num_ions_ = at_names.size();
mmpi.allreduce(&num_ions_, 1, MPI_SUM);

if (!h5_file.useHdf5p())
{
mmpi.allreduce(&num_ions_, 1, MPI_SUM);
}
if (onpe0 && ct.verbose > 0)
{
(*MPIdata::sout) << "Ions::setFromRestartFile(), read " << num_ions_
Expand Down Expand Up @@ -947,9 +949,21 @@ void Ions::initFromRestartFile(HDFrestart& h5_file)
}
readRestartPositions(h5_file);
readRestartVelocities(h5_file);
readRestartRandomStates(h5_file);
if (ct.LangevinThermostat()) readRestartRandomStates(h5_file);
readLockedAtomNames(h5_file);

// remove atoms from local list if not local
for (std::vector<Ion*>::iterator it = local_ions_.begin();
it != local_ions_.end();)
{
double p[3];
(*it)->getPosition(p);
if (!inLocalIons(p[0], p[1], p[2]))
it = local_ions_.erase(it);
else
++it;
}

// rescale all velocities by factor specified in input
rescaleVelocities(ct.VelocityScalingFactor());

Expand Down
2 changes: 1 addition & 1 deletion src/restart.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ int MGmol<OrbitalsType>::write_hdf5(HDFrestart& h5f_file,
ions.writeAtomicIDs(h5f_file);
ions.writeAtomicNLprojIDs(h5f_file);
ions.writePositions(h5f_file);
ions.writeRandomStates(h5f_file);
if (ct.LangevinThermostat()) ions.writeRandomStates(h5f_file);
ions.writeVelocities(h5f_file);
ions.writeForces(h5f_file);

Expand Down
8 changes: 8 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,14 @@ add_test(NAME testMD_D72
${CMAKE_CURRENT_SOURCE_DIR}/MD_D72/coords.in
${CMAKE_CURRENT_SOURCE_DIR}/MD_D72/lrs.in
${CMAKE_CURRENT_SOURCE_DIR}/../potentials)
add_test(NAME testHDF5single
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/HDF5single/test.py
${MPIEXEC} ${MPIEXEC_NUMPROC_FLAG} 4 ${MPIEXEC_PREFLAGS}
${CMAKE_CURRENT_BINARY_DIR}/../src/mgmol-opt
${CMAKE_CURRENT_SOURCE_DIR}/HDF5single/mgmol.cfg
${CMAKE_CURRENT_SOURCE_DIR}/HDF5single/md.cfg
${CMAKE_CURRENT_SOURCE_DIR}/HDF5single/h2o.xyz
${CMAKE_CURRENT_SOURCE_DIR}/../potentials)
add_test(NAME testMD_MVP
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/MD_MVP/test.py
${MPIEXEC} ${MPIEXEC_NUMPROC_FLAG} 4 ${MPIEXEC_PREFLAGS}
Expand Down
3 changes: 3 additions & 0 deletions tests/DMandEnergyAndForces/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import subprocess
import string
import shutil

print("Test DMandEnergyAndForces...")

Expand Down Expand Up @@ -33,6 +34,8 @@
output = subprocess.check_output(command,shell=True)
lines=output.split(b'\n')

shutil.rmtree('WF')

#analyse output
energies=[]
for line in lines:
Expand Down
6 changes: 6 additions & 0 deletions tests/HDF5single/h2o.xyz
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
3

O 0.00 0.00 0.00
H -0.76 0.59 0.00
H 0.76 0.59 0.00

34 changes: 34 additions & 0 deletions tests/HDF5single/md.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
verbosity=3
xcFunctional=PBE
FDtype=4th
[Mesh]
nx=48
ny=48
nz=48
[Domain]
ox=-4.5
oy=-4.5
oz=-4.5
lx=9.
ly=9.
lz=9.
[Potentials]
pseudopotential=pseudo.O_ONCV_PBE_SG15
pseudopotential=pseudo.H_ONCV_PBE_SG15
[Run]
type=MD
[MD]
num_steps=5
dt=40.
[Quench]
max_steps=24
atol=1.e-8
[Restart]
input_level=4
input_filename=WF
input_type=single_file
output_level=4
output_filename=WF_MD
output_type=single_file
[Coloring]
scope=global
31 changes: 31 additions & 0 deletions tests/HDF5single/mgmol.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
verbosity=2
xcFunctional=PBE
FDtype=4th
[Mesh]
nx=48
ny=48
nz=48
[Domain]
ox=-4.5
oy=-4.5
oz=-4.5
lx=9.
ly=9.
lz=9.
[Potentials]
pseudopotential=pseudo.O_ONCV_PBE_SG15
pseudopotential=pseudo.H_ONCV_PBE_SG15
[Run]
type=QUENCH
[Quench]
max_steps=120
atol=1.e-8
[Orbitals]
initial_type=Random
initial_width=1.5
[Restart]
output_level=4
output_filename=WF
output_type=single_file
[Coloring]
scope=global
75 changes: 75 additions & 0 deletions tests/HDF5single/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/usr/bin/env python
import sys
import os
import subprocess
import string

print("Test test_rho_restart...")

nargs=len(sys.argv)

mpicmd = sys.argv[1]+" "+sys.argv[2]+" "+sys.argv[3]
for i in range(4,nargs-7):
mpicmd = mpicmd + " "+sys.argv[i]
print("MPI run command: {}".format(mpicmd))

mgmol_exe = sys.argv[nargs-5]
input1 = sys.argv[nargs-4]
input2 = sys.argv[nargs-3]
coords = sys.argv[nargs-2]
print("coordinates file: %s"%coords)

#create links to potentials files
dst1 = 'pseudo.H_ONCV_PBE_SG15'
src1 = sys.argv[-1] + '/' + dst1

dst2 = 'pseudo.O_ONCV_PBE_SG15'
src2 = sys.argv[-1] + '/' + dst2

if not os.path.exists(dst1):
print("Create link to %s"%dst1)
os.symlink(src1, dst1)

if not os.path.exists(dst2):
print("Create link to %s"%dst2)
os.symlink(src2, dst2)

#run mgmol to generate initial ground state
command = "{} {} -c {} -i {}".format(mpicmd,mgmol_exe,input1,coords)
print("Run command: {}".format(command))

output = subprocess.check_output(command,shell=True)
lines=output.split(b'\n')

#run MD
command = "{} {} -c {} -i {}".format(mpicmd,mgmol_exe,input2,coords)
print("Run command: {}".format(command))
output = subprocess.check_output(command,shell=True)
lines=output.split(b'\n')

os.remove('WF')

print("Check energy conservation...")
tol = 1.e-4
energy = 0.
count = 0
for line in lines:
if line.count(b'Total') and line.count(b'Energy'):
print(line)
count=count+1
words=line.split()

energy=eval(words[2])
if count==1:
first_energy=energy

if count>1 and abs(energy-first_energy)>tol:
print("ERROR Energy = {} != {}".format(energy,first_energy))
sys.exit(1)

if count<4:
print("ERROR needs 4 energy values for checking conservation!")
sys.exit(1)

print("Test SUCCESSFUL!")
sys.exit(0)
5 changes: 3 additions & 2 deletions tests/RestartEnergyAndForces/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import subprocess
import string
import shutil

print("Test RestartEnergyAndForces...")

Expand Down Expand Up @@ -54,14 +55,14 @@
ref_energy=energy
break

#sys.exit(0)

#run test
command = "{} {} -c {} -i {}".format(mpicmd,test_exe,input2,coords)
print("Run command: {}".format(command))
output = subprocess.check_output(command,shell=True)
lines=output.split(b'\n')

shutil.rmtree('WF')

test_energy=1.e18
for line in lines:
if line.count(b'%%'):
Expand Down
3 changes: 3 additions & 0 deletions tests/WFEnergyAndForces/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import subprocess
import string
import shutil

print("Test WFEnergyAndForces...")

Expand Down Expand Up @@ -37,6 +38,8 @@
output = subprocess.check_output(command,shell=True)
lines=output.split(b'\n')

shutil.rmtree('WF')

#analyse output
energies=[]
for line in lines:
Expand Down