Skip to content

Commit

Permalink
Add unit test for GPAW4QMCPACK converter
Browse files Browse the repository at this point in the history
  • Loading branch information
Juha Tiihonen committed Dec 20, 2021
1 parent 3295d77 commit a6e5c92
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/QMCTools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ if(USE_OBJECT_TARGET)
target_link_libraries(qmcfinitesize qmcparticle qmcutil)
endif()

configure_file(gpaw4qmcpack ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/gpaw4qmcpack COPYONLY)

install(TARGETS convert4qmc qmc-extract-eshdf-kvectors qmc-get-supercell qmc-check-affinity convertpw4qmc qmcfinitesize
RUNTIME DESTINATION bin)

Expand Down
18 changes: 18 additions & 0 deletions tests/converter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,21 @@ add_converter_test(test_LiH_qp)
add_converter_test(test_C4_MSD_excited)
add_converter_test(test_diamond2_rmg)
add_converter_test(test_O_rmg)

# add tests for GPAW4QMCPACK converter
function(ADD_GPAW4QMCPACK_TEST test_name)
set(EXE_NAME ${qmcpack_BINARY_DIR}/bin/gpaw4qmcpack)
execute_process(COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/gpaw4qmcpack_test.py"
"${CMAKE_CURRENT_BINARY_DIR}")
execute_process(COMMAND ${CMAKE_COMMAND} -E copy_directory "${CMAKE_CURRENT_SOURCE_DIR}/${test_name}"
"${CMAKE_CURRENT_BINARY_DIR}/${test_name}")
add_test(NAME gpaw4qmcpack_test COMMAND "${CMAKE_CURRENT_BINARY_DIR}/gpaw4qmcpack_test.py"
"${CMAKE_CURRENT_BINARY_DIR}/${test_name}"
--exe "${EXE_NAME}" --h5diff "${HDF5_DIFF_EXECUTABLE}")
message(${EXE_NAME})
set_tests_properties(gpaw4qmcpack_test PROPERTIES
FAIL_REGULAR_EXPRESSION " FAIL"
PASS_REGULAR_EXPRESSION " pass"
TIMEOUT 120 LABELS "converter;deterministic")
endfunction()
add_gpaw4qmcpack_test(test_Si_diamond)
88 changes: 88 additions & 0 deletions tests/converter/gpaw4qmcpack_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/usr/bin/env python3

import argparse
import os
import subprocess
import sys

# unit test GPAW4QMCPACK

# TODO: implement and test converter generated QMCPACK input

def run_gpaw4qmcpack_test(test_name, g4q_exe, h5diff_exe):
okay = True

# Example invocation of converter
#gpaw4qmcpack infile.gpw outfile.h5 --density

infile = 'restart.gpw'
outfile = 'test.orbs.h5'
cmd = [g4q_exe,infile,outfile]

p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
stdout, stderr = p.communicate()

file_out = open('stdout.txt', 'w')
file_out.write(stdout)
file_out.close()
if len(stderr) > 0 :
file_err = open('stderr.txt', 'w')
file_err.write(stderr)
file_err.close()

ret = p.returncode

if ret != 0:
print("Return code nonzero: ", ret)
okay = False

if len(stderr.strip()) != 0:
# some MPI output on stderr is okay
# TODO - more general way of checking acceptable stderr strings
if not stderr.startswith('Rank'):
print("Stderr not empty")
print(stderr)
okay = False

ret = os.system(h5diff_exe + ' -d 0.000001 gold.orbs.h5 test.orbs.h5')
# if it's okay up to this point
if ret==0 and okay:
print(" pass")
return True
else:
print("h5diff reported a difference")
print(" FAIL")
return False

return okay


if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Test gpaw4qmcpck')
parser.add_argument('test_name',
default='test_Si_diamond',
help='Name of test to run (name of directory)')
parser.add_argument('--exe',
default='gpaw4qmcpack',
help='Location of gpaw4qmcpack executable')
parser.add_argument('--h5diff',
default='h5diff',
help='Location of h5diff executable')
args = parser.parse_args()

test_dir = args.test_name
if not os.path.exists(test_dir):
print("Test not found: ", test_dir)
sys.exit(1)

curr_dir = os.getcwd()
os.chdir(test_dir)

ret = run_gpaw4qmcpack_test(test_dir, args.exe, args.h5diff)

os.chdir(curr_dir)

if ret:
sys.exit(0)
sys.exit(1)

Binary file added tests/converter/test_Si_diamond/gold.orbs.h5
Binary file not shown.
Binary file added tests/converter/test_Si_diamond/restart.gpw
Binary file not shown.
23 changes: 23 additions & 0 deletions tests/converter/test_Si_diamond/run_si.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env python3

# This file provided just for reference, not executed in the test

from ase.build import bulk
from ase.units import Ry
from gpaw import GPAW,PW,FermiDirac,setup_paths

silicon = bulk('Si', 'diamond', a=5.459)

econv = 1e-9*2*Ry
ecut = 30*Ry
silicon.calc = GPAW(xc='PBE',
setups = {'default':'sg15'}, # Based on standard library of soft SG15 ecps
mode = PW(ecut,force_complex_dtype=True), # give all modes explicitly
txt = 'silicon.txt',
kpts = (2,2,2),
occupations = FermiDirac(0.1),
convergence = {'energy':econv},
symmetry = 'off'
)
silicon.get_potential_energy()
silicon.calc.write('Si.gpw',mode='all') # important to request mode='all'

0 comments on commit a6e5c92

Please sign in to comment.