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

Nexus: Add unit tests and example for qdens-radial tool #3676

Merged
merged 15 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from 14 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
6 changes: 6 additions & 0 deletions nexus/bin/nxs-test
Original file line number Diff line number Diff line change
Expand Up @@ -1627,6 +1627,10 @@ def qdens():
#end def qdens


def qdens_radial():
nunit_all()
#end def qdens


def qmca():
nunit('help')
Expand Down Expand Up @@ -1968,6 +1972,7 @@ NexusTest( nxs_sim )
NexusTest( qmca )
NexusTest( qmc_fit )
NexusTest( qdens )
NexusTest( qdens_radial )

# ordered alphabetically (pytest order)
#NexusTest( basisset )
Expand Down Expand Up @@ -2005,6 +2010,7 @@ NexusTest( qdens )
#NexusTest( pyscf_input )
#NexusTest( pyscf_simulation )
#NexusTest( qdens )
#NexusTest( qdens_radial )
#NexusTest( qmc_fit )
#NexusTest( qmca )
#NexusTest( qmcpack_analyzer )
Expand Down
23 changes: 23 additions & 0 deletions nexus/bin/qdens-radial
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ class QMCDensityRadialProcessor(QDRBase):
default='None',
help='Location of DMC+err to be used for resampling (default=%default).'
)
parser.add_option('--seed',dest='random_seed',
default='None',
help='Random seed used for re-sampling. (default=%default).'
)
parser.add_option('-n','--nsamples',dest='nsamples',
default='50',
help='Number of samples for resampling (default=%default).'
Expand Down Expand Up @@ -249,6 +253,21 @@ class QMCDensityRadialProcessor(QDRBase):
#end if
#end if

# --seed option
if opt.random_seed is not None:
opt_failed = False
rseed = opt.random_seed
try:
opt.random_seed = int(opt.random_seed)
except:
opt_failed = True
#end try
if opt_failed:
self.error('seed must be an integer\nyou provided: {0}'.format(rseed))
#end if
#end if


#end def read_command_line(self)

def process(self):
Expand Down Expand Up @@ -337,6 +356,10 @@ class QMCDensityRadialProcessor(QDRBase):
sum_sq_dict = {}
std_dict = {}

if opt.random_seed is not None:
np.random.seed(opt.random_seed)
#end if

nsamples = opt.nsamples
self.log('Will compute {} samples...'.format(nsamples))
for i in range(nsamples):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#! /usr/bin/env python

from nexus import settings
from nexus import job
from nexus import run_project
from nexus import generate_physical_system
from nexus import generate_pwscf
from nexus import generate_projwfc
from nexus import generate_pw2qmcpack
from nexus import generate_qmcpack
from nexus import vmc

from structure import *

from qmcpack_input import spindensity

settings(
pseudo_dir = '../../pseudopotentials',
runs = 'runs_spin',
results = '',
status_only = 0,
generate_only = 0,
skip_submit = 0,
sleep = 3,
machine = 'ws4'
)

dia16 = generate_physical_system(
units = 'A',
axes = [[ 1.785, 1.785, 0. ],
[ 0. , 1.785, 1.785],
[ 1.785, 0. , 1.785]],
elem = ['C','C'],
pos = [[ 0. , 0. , 0. ],
[ 0.8925, 0.8925, 0.8925]],
tiling = (1,1,1),
C = 4
)

# k-mesh used for density
scf_kg = dia16.structure.kgrid_from_kspacing(0.5) # Get SCF kmesh from k-spacing

# twist-mesh used for qmc
dia16.structure.add_symmetrized_kmesh(kgrid=(2,2,2),kshift=(0,0,0))


number_of_ks_orbs = 11

scf = generate_pwscf(
identifier = 'scf',
path = 'scf',
job = job(cores=1,app='pw.x',hours=1),
input_type = 'generic',
calculation = 'scf',
nspin = 2,
tot_magnetization = 0,
nbnd = number_of_ks_orbs,
input_dft = 'lda',
ecutwfc = 200,
conv_thr = 1e-8,
nosym = False,
wf_collect = False,
system = dia16,
kgrid = scf_kg,
kshift = (0,0,0),
pseudos = ['C.BFD.upf'],
)

nscf = generate_pwscf(
identifier = 'nscf',
path = 'nscf',
job = job(cores=1,app='pw.x',hours=1),
input_type = 'generic',
calculation = 'nscf',
input_dft = 'lda',
ecutwfc = 200,
nspin = 2,
tot_magnetization = 0,
conv_thr = 1e-8,
nosym = True,
wf_collect = True,
system = dia16,
nbnd = number_of_ks_orbs,
verbosity = 'high', #verbosity must be set to high
pseudos = ['C.BFD.upf'],
dependencies = (scf,'charge_density'),
)

# Generate orbital h5 file
conv = generate_pw2qmcpack(
identifier = 'conv',
path = 'nscf',
job = job(cores=1,app='pw2qmcpack.x',hours=1),
write_psir = False,
dependencies = (nscf,'orbitals'),
)


qmc = generate_qmcpack(
identifier = 'vmc',
path = 'vmc',
job = job(cores=3,app='qmcpack_complex',hours=1),
input_type = 'basic',
system = dia16,
pseudos = ['C.BFD.xml'],
estimators = [spindensity(dr=(0.5,0.5,0.5))],
jastrows = [],
calculations = [
vmc(
walkers = 1,
warmupsteps = 20,
blocks = 200,
steps = 10,
substeps = 2,
timestep = .4
)
],
dependencies = (conv,'orbitals'),
)

run_project()

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

qdens-radial -s C -r 0.6 runs_spin/vmc/vmc.avg.s000.SpinDensity_u+d.xsf
qdens-radial -s C -r 0.6 -c runs_spin/vmc/vmc.avg.s000.SpinDensity_u+d.xsf

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

qdens -a -v -e 10 -f xsf -i runs_spin/vmc/vmc.g000.twistnum_0.in.xml runs_spin/vmc/*.stat.h5

2 changes: 1 addition & 1 deletion nexus/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ if(ADD_TEST)
foreach(TESTNAME ${TESTLIST})
#message("Adding test ntest_nexus_${TESTNAME}")
set(NTEST "${qmcpack_SOURCE_DIR}/nexus/bin/nxs-test")
add_test(NAME ntest_nexus_${TESTNAME} COMMAND ${NTEST} -R ${TESTNAME} --ctest
jtkrogel marked this conversation as resolved.
Show resolved Hide resolved
add_test(NAME ntest_nexus_${TESTNAME} COMMAND ${NTEST} -R ${TESTNAME}\$ --ctest
--pythonpath=${PROJECT_SOURCE_DIR}/nexus/lib)
set_property(
TEST ntest_nexus_${TESTNAME}
Expand Down
157 changes: 157 additions & 0 deletions nexus/tests/unit/test_qdens_radial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@

import versions
import testing
from testing import execute,text_eq,check_value_eq


if versions.spglib_available:
def test_radial_density():
import os

tpath = testing.setup_unit_test_output_directory('qdens_radial','test_radial_density')

exe = testing.executable_path('qdens-radial')

qr_vmc_files_path = testing.unit_test_file_path('qdens_radial','diamond_twist/vmc')
command = 'rsync -a {} {}'.format(qr_vmc_files_path,tpath)
out,err,rc = execute(command)
assert(rc==0)
vmc_path = os.path.join(tpath,'vmc')
vmc_infile = os.path.join(vmc_path,'vmc.g000.twistnum_0.in.xml')
assert(os.path.exists(vmc_infile))
vmc_infile = os.path.join(vmc_path,'vmc.g001.twistnum_1.in.xml')
assert(os.path.exists(vmc_infile))
vmc_infile = os.path.join(vmc_path,'vmc.g002.twistnum_2.in.xml')
assert(os.path.exists(vmc_infile))
vmc_infile = os.path.join(vmc_path,'vmc.g003.twistnum_3.in.xml')
assert(os.path.exists(vmc_infile))

files_bef = '''
vmc.avg.s000.SpinDensity_u+d+err.xsf vmc.avg.s000.SpinDensity_u+d-err.xsf
vmc.avg.s000.SpinDensity_u+d.xsf vmc.g000.twistnum_0.in.xml
vmc.g000.s000.scalar.dat vmc.g001.s000.scalar.dat
vmc.g000.twistnum_0.in.g000.qmc vmc.g001.twistnum_1.in.g001.qmc
vmc.g001.twistnum_1.in.xml vmc.g002.twistnum_2.in.xml
vmc.g002.s000.scalar.dat vmc.g003.s000.scalar.dat
vmc.g002.twistnum_2.in.g002.qmc vmc.g003.twistnum_3.in.g003.qmc
vmc.g003.twistnum_3.in.xml vmc.out
vmc.in vmc.info.xml
'''.split()

qr_dmc_files_path = testing.unit_test_file_path('qdens_radial','diamond_twist/dmc')
command = 'rsync -a {} {}'.format(qr_dmc_files_path,tpath)
out,err,rc = execute(command)
assert(rc==0)
dmc_path = os.path.join(tpath,'dmc')
dmc_infile = os.path.join(dmc_path,'dmc.g000.twistnum_0.in.xml')
assert(os.path.exists(dmc_infile))
dmc_infile = os.path.join(dmc_path,'dmc.g001.twistnum_1.in.xml')
assert(os.path.exists(dmc_infile))
dmc_infile = os.path.join(dmc_path,'dmc.g002.twistnum_2.in.xml')
assert(os.path.exists(dmc_infile))
dmc_infile = os.path.join(dmc_path,'dmc.g003.twistnum_3.in.xml')
assert(os.path.exists(dmc_infile))

files_bef = '''
dmc.avg.s001.SpinDensity_u+d+err.xsf dmc.avg.s001.SpinDensity_u+d-err.xsf
dmc.avg.s001.SpinDensity_u+d.xsf dmc.g001.s001.scalar.dat
dmc.g000.s000.scalar.dat dmc.g000.s001.scalar.dat
dmc.g000.twistnum_0.in.g000.qmc dmc.g001.twistnum_1.in.g001.qmc
dmc.g000.twistnum_0.in.xml dmc.g001.twistnum_1.in.xml
dmc.g001.s000.scalar.dat dmc.g002.s000.scalar.dat
dmc.g002.s001.scalar.dat dmc.g003.s001.scalar.dat
dmc.g002.twistnum_2.in.g002.qmc dmc.g003.twistnum_3.in.g003.qmc
dmc.g002.twistnum_2.in.xml dmc.g003.twistnum_3.in.xml
dmc.g003.s000.scalar.dat dmc.in
dmc.out
'''.split()

assert(check_value_eq(set(os.listdir(dmc_path)),set(files_bef)))

# VMC non-cumulative
command = '{0} -s C -r 0.6 {1}/vmc.avg.s000.SpinDensity_u+d.xsf'.format(exe,vmc_path)
out,err,rc = execute(command)

# Assert that return code is 0
assert(rc==0)

# Assert that output is consistent with reference
out_ref = '''
Norm: tot = 8.00000003

Non-Cumulative Value of C Species at Cutoff 0.6 is: 4.773264912162242
'''
assert(text_eq(out,out_ref))

# VMC cumulative
command = '{0} -s C -r 0.6 -c {1}/vmc.avg.s000.SpinDensity_u+d.xsf'.format(exe,vmc_path)
out,err,rc = execute(command)

# Assert that return code is 0
assert(rc==0)

# Assert that output is consistent with reference
out_ref = '''
Norm: tot = 8.00000003

Cumulative Value of C Species at Cutoff 0.6 is: 1.0684248641866259
'''

# DMC extrapolated non-cumulative
command = '{0} -s C -r 0.6 --vmc={1}/vmc.avg.s000.SpinDensity_u+d.xsf {2}/dmc.avg.s001.SpinDensity_u+d.xsf'.format(exe,vmc_path,dmc_path)
out,err,rc = execute(command)

# Assert that return code is 0
assert(rc==0)

# Assert that output is consistent with reference
out_ref = '''
Extrapolating from VMC and DMC densities...

Norm: tot = 7.999999969999998

Non-Cumulative Value of C Species at Cutoff 0.6 is: 4.910093964664309
'''
assert(text_eq(out,out_ref))

# DMC extrapolated cumulative
command = '{0} -s C -r 0.6 -c --vmc={1}/vmc.avg.s000.SpinDensity_u+d.xsf {2}/dmc.avg.s001.SpinDensity_u+d.xsf'.format(exe,vmc_path,dmc_path)
out,err,rc = execute(command)

# Assert that return code is 0
assert(rc==0)

# Assert that output is consistent with reference
out_ref = '''
Extrapolating from VMC and DMC densities...

Norm: tot = 7.999999969999998

Cumulative Value of C Species at Cutoff 0.6 is: 1.1078267486386275
'''
assert(text_eq(out,out_ref))


# DMC extrapolated cumulative with error bar
command = '{0} -s C -r 0.6 -c -n 3 --seed=0 --vmc={1}/vmc.avg.s000.SpinDensity_u+d.xsf --vmcerr={1}/vmc.avg.s000.SpinDensity_u+d+err.xsf --dmcerr={2}/dmc.avg.s001.SpinDensity_u+d+err.xsf {2}/dmc.avg.s001.SpinDensity_u+d.xsf'.format(exe,vmc_path,dmc_path)
out,err,rc = execute(command)

# Assert that return code is 0
assert(rc==0)

# Assert that output is consistent with reference
out_ref = '''
Extrapolating from VMC and DMC densities...
Resampling to obtain error bar (NOTE: This can be slow)...
Will compute 3 samples...
sample: 0
sample: 1
sample: 2

Norm: tot = 7.999999969999998

Cumulative Value of C Species at Cutoff 0.6 is: 1.1078267486386275+/-0.0016066467833404942
'''
assert(text_eq(out,out_ref))
#end def test_radial_density
#end if
Loading