-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdmc.py
145 lines (127 loc) · 4.52 KB
/
dmc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
from __future__ import print_function
import os
import average_tools as avg
####################################################
class DMCWriter:
def __init__(self,options={}):
''' Object for producing input into a DMC QWalk run.
Args:
options (dict): editable options are as follows.
trialfunc (str): system and trial wavefunction section.
errtol (float): tolerance for the estimated energy error.
timestep (float): time step for DMC.
tmoves (bool): Use Casula's t-moves.
savetrace (bool): Leave with a trace (file).
extra_observables (list): see `average_tools.py` for how to use this.
minblocks (int): minimum number of DMC steps to take, considering equillibration time.
iterations (int): number of DMC steps to attempt.
'''
self.trialfunc=''
self.errtol=0.1
self.minblocks=10
self.nblock=30
self.timestep=0.01
self.tmoves=True
self.savetrace=True
self.extra_observables=[]
self.qmc_abr='dmc'
self.completed=False
self.set_options(options)
#-----------------------------------------------
def set_options(self, d):
''' Save setting of options.
Args:
d (dict): attributes to update.
'''
selfdict=self.__dict__
for k in d.keys():
if not k in selfdict.keys():
raise ValueError("Error:",k,"not a keyword for DMCWriter")
selfdict[k]=d[k]
# Check completeness of average generator options.
for avg_generator in self.extra_observables:
avg.check_opts(avg_generator)
#-----------------------------------------------
def qwalk_input(self,infile):
if self.trialfunc=='':
print(self.__class__.__name__,": Trial function not ready. Postponing input file generation.")
self.completed=False
else:
outlines=[
"method { dmc timestep %g nblock %i"%(self.timestep,self.nblock)
]
if self.tmoves:
outlines+=['tmoves']
if self.savetrace:
tracename = "%s.trace"%infile
outlines+=['save_trace %s'%tracename]
for avg_opts in self.extra_observables:
outlines+=avg.average_section(avg_opts)
outlines+=["}"]
outlines+=self.trialfunc.split('\n')
with open(infile,'w') as f:
f.write('\n'.join(outlines))
self.completed=True
####################################################
import subprocess as sub
import json
class DMCReader:
''' Reads results from a DMC calculation.
Attributes:
output (dict): results of calculation.
completed (bool): whether the run has converged to a final answer.
'''
def __init__(self,errtol=0.01,minblocks=15):
self.output={}
self.completed=False
self.errtol=errtol
self.minblocks=minblocks
self.gosling="gosling"
def read_outputfile(self,outfile):
''' Read output file results.
Args:
outfile (str): output to read.
'''
return json.loads(sub.check_output([self.gosling,"-json",outfile.replace('.o','.log')]).decode())
def check_complete(self):
''' Check if a DMC run is complete.
Returns:
bool: If self.results are within error tolerances.
'''
completed=True
if self.output['properties']['total_energy']['error'][0] > self.errtol:
print("DMC incomplete: (%f) does not meet tolerance (%f)"%\
(self.output['properties']['total_energy']['error'][0],self.errtol))
completed=False
if self.output['total blocks']-self.output['warmup blocks'] < self.minblocks:
print("DMC incomplete: Run completed %d blocks, but requires %d."%\
(self.output['total blocks']-self.output['warmup blocks'],self.minblocks))
completed=False
return completed
#------------------------------------------------
def collect(self,outfile):
''' Collect results for an output file and resolve if the run needs to be resumed.
Args:
outfiles (list): list of output file names to open and read.
Returns:
str: status of run = {'ok','restart'}
'''
# Gather output from files.
self.completed=True
status='unknown'
if os.path.exists(outfile):
self.output=self.read_outputfile(outfile)
self.output['file']=outfile
# Check files.
self.completed=self.check_complete()
if not self.completed:
status='restart'
else:
status='ok'
return status
#------------------------------------------------
def write_summary(self):
''' Print out all the items in output. '''
print("#### Diffusion Monte Carlo")
for f,out in self.output.items():
print(f,out)