Dice Python Frontend Documentation
repo.DicePy.dice_write Namespace Reference

Functions

def isint (s)
 Checks if the input string represents an integer. More...
 
def isfloat (s)
 Checks if the input string represents a float. More...
 
def query_default (param, default)
 Queries whether the user wants to use default values for a certain parameter. More...
 
def query_step (prompt, tag, defaults, idx)
 Queries an integer step value, using the input string as a prompt. More...
 
def query_posfloat (prompt, tag, defaults, idx=None)
 Queries a positive float value, using the input string as a prompt. More...
 
def query_grid (param)
 Queries user for the minimum, maximum and number of points of a parameter grid. More...
 
def query_params ()
 Major function that queries the user for all the inputs required to assemble params.txt. More...
 
def write_params (control, params)
 Major subroutine that writes the input file params.txt. More...
 

Function Documentation

◆ isint()

def repo.DicePy.dice_write.isint (   s)

Checks if the input string represents an integer.

Parameters
[in]sString with user-input
Return values
TrueIf s represents an integer
FalseIf s does not represent an integer

Definition at line 22 of file dice_write.py.

22 def isint(s):
23  try:
24  int(s)
25  return True
26  except ValueError:
27  return False
28 
def isint(s)
Checks if the input string represents an integer.
Definition: dice_write.py:22
+ Here is the caller graph for this function:

◆ isfloat()

def repo.DicePy.dice_write.isfloat (   s)

Checks if the input string represents a float.

Parameters
[in]sString with user-input
Return values
TrueIf s represents a float
FalseIf s does not represent a float

Definition at line 35 of file dice_write.py.

35 def isfloat(s):
36  try:
37  float(s)
38  return True
39  except ValueError:
40  return False
41 
def isfloat(s)
Checks if the input string represents a float.
Definition: dice_write.py:35
+ Here is the caller graph for this function:

◆ query_default()

def repo.DicePy.dice_write.query_default (   param,
  default 
)

Queries whether the user wants to use default values for a certain parameter.

Parameters
[in]paramString naming the parameter of interest
[in]defaultDefault values for grid
Return values
use_defBoolean indicating if default should be used

Definition at line 48 of file dice_write.py.

48 def query_default(param,default):
49  print('')
50  print('Would you like to use default values for the {:} grid? - Yes (y) or No (n)'.format(param))
51  print('Default: min={:}, max={:}, num={:}'.format(default[0],default[1],default[2]))
52  use_def = input('Your response ("y" or "n"): ')
53  options = ['y','n']
54  while not (use_def in options):
55  print('')
56  print('Error: Invalid response, please enter "y" or "n", without quotes.')
57  use_def = input('Your response: ')
58 
59  return use_def
60 
def query_default(param, default)
Queries whether the user wants to use default values for a certain parameter.
Definition: dice_write.py:48
+ Here is the caller graph for this function:

◆ query_step()

def repo.DicePy.dice_write.query_step (   prompt,
  tag,
  defaults,
  idx 
)

Queries an integer step value, using the input string as a prompt.

Checks the validity of the user input.

Parameters
[in]promptString to prompt user response
[in]tagParameter name
[in]defaultList of default values
[in]idxIndex of system in the default list
Return values
stepUser-input for the integert step value

Definition at line 70 of file dice_write.py.

70 def query_step(prompt,tag,defaults,idx):
71  step = 0
72  print('')
73  while not(step>0):
74  print(prompt)
75  print('Default: ',tag,'=',defaults[idx])
76  step = input('Your response: ')
77 
78  if step=='':
79  break # Use default
80  elif not(isint(step)):
81  print('')
82  print('Error: Invalid response. The program is expecting a positive integer, or blank for default.')
83  print('Please have another go.')
84  step = 0
85  continue
86  else:
87  step = int(step)
88  if not(step>0):
89  print('')
90  print('Error: Invalid response. The program is expecting an positive integer, or blank for default.')
91  print('Please have another go.')
92 
93  return step
94 
95 
def query_step(prompt, tag, defaults, idx)
Queries an integer step value, using the input string as a prompt.
Definition: dice_write.py:70
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ query_posfloat()

def repo.DicePy.dice_write.query_posfloat (   prompt,
  tag,
  defaults,
  idx = None 
)

Queries a positive float value, using the input string as a prompt.

Checks the validity of the user input.

Parameters
[in]promptString to prompt user response
[in]tagParameter name
[in]defaultsDefault values
[in]idxIndex of system in the default list
Return values
pfUser-input for the positive float value

Definition at line 105 of file dice_write.py.

105 def query_posfloat(prompt,tag,defaults,idx=None):
106  pf = -1.0
107  print('')
108  while not(pf>0.0):
109  print(prompt)
110  if idx==None:
111  print('Default: ',tag,'=',defaults)
112  else:
113  print('Default: ',tag,'=',defaults[idx])
114  pf = input('Your response: ')
115 
116  if pf=='':
117  break # Use default
118  elif not(isfloat(pf)):
119  print('')
120  print('Error: Invalid response. The program is expecting a positive float, or blank for default.')
121  print('Please have another go.')
122  pf = -1.0
123  continue
124  else:
125  pf = float(pf)
126  if not(pf>0.0):
127  print('')
128  print('Error: Invalid response. The program is expecting an positive float, or blank for default.')
129  print('Please have another go.')
130 
131  return pf
132 
133 
def query_posfloat(prompt, tag, defaults, idx=None)
Queries a positive float value, using the input string as a prompt.
Definition: dice_write.py:105
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ query_grid()

def repo.DicePy.dice_write.query_grid (   param)

Queries user for the minimum, maximum and number of points of a parameter grid.

Checks the validity of the user input.

Parameters
[in]paramString naming the parameter of interest
Return values
miniString with minimum value of the grid
maxiString with maximum value of the grid
numString with number of points in the grid

Definition at line 142 of file dice_write.py.

142 def query_grid(param):
143  # Minimum
144  mini = input('Enter minimum value for {:} grid: '.format(param))
145  while not(isfloat(mini)):
146  print('')
147  print('Error: Invalid response. The program is expecting a float.')
148  mini = input('Enter minimum value for {:} grid: '.format(param))
149  mini=float(mini)
150 
151  # Maximum
152  maxi = mini - 1.0
153  while maxi < mini:
154  maxi = input('Enter maximum value for {:} grid: '.format(param))
155  while not(isfloat(maxi)):
156  print('')
157  print('Error: Invalid response. The program is expecting a float.')
158  maxi = input('Enter maximum value for {:} grid: '.format(param))
159  maxi = float(maxi)
160  if maxi < mini:
161  print('')
162  print('Error: Invalid response. Choose a maximum value that is larger than your chosen minimum={:}.'.format(mini))
163 
164  # Number of grid points
165  num = 0
166  while num < 1:
167  num = input('Enter integer number of grid points (inclusive of max & min): ')
168  while not(isint(num)):
169  print('')
170  print('Error: Invalid response. The program is expecting a positive integer.')
171  num = input('Enter integer number of grid points (inclusive of max & min): ')
172  num = int(num)
173  if num < 1:
174  print('')
175  print('Error: Invalid response. The program is expecting a positive integer.')
176 
177  # Return values as strings
178  return str(mini), str(maxi), str(num)
179 
180 #------------------------
181 # MAJOR WRITE FUNCTIONS -
182 #------------------------
183 
def query_grid(param)
Queries user for the minimum, maximum and number of points of a parameter grid.
Definition: dice_write.py:142
+ Here is the caller graph for this function:

◆ query_params()

def repo.DicePy.dice_write.query_params ( )

Major function that queries the user for all the inputs required to assemble params.txt.

Return values
controlDictionay with all inputs for the CONTROL namelist in params.txt
paramsDictionay with all inputs for the PARAMETER namelist in params.txt

Definition at line 189 of file dice_write.py.

189 def query_params():
190 
191  # Initialize Dictionaries
192  control, params = {}, {}
193 
194  # Query the System
195  print('')
196  print('Which system would you like to solve?')
197  print('a) Quantum Harmonic Oscillator')
198  print('b) Hydrogen Molecule Ion H2+')
199  print('c) Hydrogen Molecule H2')
200  print('d) Run Test Cases')
201  sys = input('Your response ("a", "b", "c" or "d"): ')
202  # Check system value
203  options = ['a','b','c','d']
204  while not (sys in options):
205  print('')
206  print('Error: Invalid response, please enter "a", "b", "c" or "d", without quotes.')
207  sys = input('Your response: ')
208  # Report system setting
209  settings = ["'QHO'","'H2plus'","'H2'","'test'"]
210  messages = ['Quantum Harmonic Oscillator','Hydrogen Molecule Ion','Hydrogen Molecule','Test Cases']
211  sys_idx = options.index(sys)
212  control['p_system'] = settings[sys_idx]
213  print('')
214  print('You have chosen the ' + messages[options.index(sys)] + '.')
215 
216 
217  # For Test Cases (d), no more parameters are required
218  if sys=='d':
219  return control,'test'
220 
221  # Query the Calculation
222  print('')
223  print('What type of calculation would you like to perform?')
224  print('a) Equilibration run, to determine burning & thinning step sizes')
225  print('b) VQMC calculation')
226  calc = input('Your response ("a" or "b"): ')
227  # Check calculation value
228  options = ['a','b']
229  while not (calc in options):
230  print('Error: Invalid response, please enter "a" or "b", without quotes.')
231  calc = input('Your response: ')
232  # Report calculation setting
233  settings = ['.TRUE.','.FALSE.']
234  messages = ['an Equilibration run.','a VQMC calculation.',]
235  control['run_equil'] = settings[options.index(calc)]
236  print('')
237  print('You have chosen to do ' + messages[options.index(calc)])
238 
239  # Assume this is not a restart run
240  control['run_restart'] = '.FALSE.'
241 
242  # Query if restart files should be written
243  print('')
244  print('Would you like the program to write restart files? - Yes (y) or No (n)')
245  w_res = input('Your response ("y" or "n"): ')
246  # Check response
247  options = ['y','n']
248  while not (w_res in options):
249  print('')
250  print('Error: Invalid response, please enter "y" or "n", without quotes.')
251  w_res = input('Your response: ')
252  # Report response
253  settings = ['.TRUE.','.FALSE.']
254  messages = ['to','not to',]
255  control['write_restart'] = settings[options.index(w_res)]
256  print('')
257  print('You have requested the program {:} write restart files.'. format(messages[options.index(w_res)]))
258 
259  # Query restart frequency
260  if w_res=='y':
261  print('')
262  print('By default, restart files will be written after the MMC loop of every grid point in the parameter optimization.')
263  print('If you want the restart files to be written less frequently, enter the required interval of grid points.')
264  print('Else, for the default behaviour, leave a blank response below (ie., press \'Enter\').')
265  prompt='Enter interval of points in grid-search for writing restart files.'
266  tag='restart_num'
267  steps = query_step(prompt, tag, d_rnum, sys_idx)
268  if steps == '':
269  control[tag] = 1 # Use default
270  else:
271  control[tag] = str(steps)
272 
273  # For QMC calculations, query if Markov chains should be written
274  if calc=='b':
275  # Query write_chains
276  print('')
277  print('Would you like the program to save the Markov chains (after burning & thinning)?')
278  print('y) Yes, I would like a detailed output for wavefunction visualization.')
279  print('n) No, just give me the final energies and optimized parameters.')
280  temp = input('Your response ("y" or "n"): ')
281  # Check response
282  options = ['y','n']
283  while not (temp in options):
284  print('')
285  print('Error: Invalid response, please enter "y" or "n", without quotes.')
286  temp = input('Your response: ')
287  # Report setting
288  settings = ['.TRUE.','.FALSE.']
289  messages = ['output', 'not output']
290  control['write_chains'] = settings[options.index(temp)]
291  print('')
292  print('Your have requested the program to {:} the Markov chains.'.format(messages[options.index(temp)]))
293 
294  # Query if logfile should be written
295  print('')
296  print('Would you like the program to write a logfile? - Yes (y) or No (n)')
297  temp = input('Your response ("y" or "n"): ')
298  # Check response
299  options = ['y','n']
300  while not (temp in options):
301  print('')
302  print('Error: Invalid response, please enter "y" or "n", without quotes.')
303  temp = input('Your response: ')
304  # Report setting
305  settings = ['.TRUE.','.FALSE.']
306  messages = ['to','not to',]
307  control['write_log'] = settings[options.index(temp)]
308  print('')
309  print('You have requested the program {:} write a logfile.'. format(messages[options.index(temp)]))
310 
311 
312  # Message on defaults
313  print('')
314  print('###')
315  print('If you wish to use defaults for any prompt that follows, leave a blank response for it (ie., press \'Enter\').')
316  print('###')
317 
318 
319  # Query total number of steps
320  tag = 'steps'
321  prompt = 'Enter total number of MMC steps.'
322  if calc=='a':
323  d_steps=d_equi_steps.copy()
324  else:
325  d_steps=d_vqmc_steps.copy()
326  steps = query_step(prompt,tag,d_steps,sys_idx)
327  if steps=='':
328  steps = d_steps[sys_idx] # Use default
329  else:
330  params[tag] = str(steps) # Store user input
331 
332 
333 
334  # For QMC calculations only, include burning & thinning steps
335  if calc=='b':
336 
337  # Query step size for burning
338  tag = 'burn_step'
339  prompt = 'Enter an integer step-size for burning.'
340  b_step = steps + 1
341  while not(b_step<steps or b_step==''):
342  b_step = query_step(prompt,tag,d_bstep,sys_idx)
343  if b_step=='':
344  b_step = d_bstep[sys_idx] # Use default
345  elif b_step<steps:
346  params[tag] = str(b_step) # Store user input
347  else:
348  print('')
349  print('Error: Invalid response. The program is expecting burn_step < {:} as you chose steps={:}.'.format(steps,steps))
350  print ('Please have another go.')
351 
352  # Query step size for thinning
353  tag = 'thin_step'
354  prompt = 'Enter an integer step-size for thinning.'
355  t_max = steps - b_step
356  t_step = t_max + 1
357  while not(t_step<t_max or t_step==''):
358  t_step = query_step(prompt,tag,d_tstep,sys_idx)
359  if t_step=='':
360  t_step = d_tstep[sys_idx] # Use default
361  elif b_step<steps:
362  params[tag] = str(t_step) # Store user input
363  else:
364  print('')
365  print('Error: Invalid response. The program is expecting thin_step < {:} as you chose steps-burn_steps={:}.'.format(t_max,t_max))
366  print ('Please have another go.')
367 
368 
369  # Query rcut
370  tag='rcut'
371  prompt='Enter cutoff radial distance (in Bohr) for MMC search.'
372  temp = query_posfloat(prompt,tag,d_rcut,sys_idx)
373  if temp!='':
374  params[tag] = str(temp)
375 
376 
377  # Query sigma
378  tag='sigma'
379  prompt='Enter sigma, the standard deviation of the Gaussian used to propose MMC steps.'
380  temp = query_posfloat(prompt,tag,d_rcut,sys_idx)
381  if temp!='':
382  params[tag] = str(temp)
383 
384 
385  # Problem specific parameters
386 
387  # QHO
388  if sys=='a':
389 
390  # Parameter information - alpha
391  print('')
392  print('Quantity of interest "alpha" is the factor in the exponent of the Quantum Harmonic Oscillator system.')
393 
394 
395  # Check if user wants defaults
396  tag = 'alpha'
397  use_def = query_default(tag,d_alpha)
398 
399  # If not defaults, query grid for alpha
400  if use_def == 'n':
401  mini, maxi, num = query_grid(tag)
402  params[tag] = '{:}, {:}, {:}'.format(mini, maxi, num)
403 
404 
405  # H2plus
406  elif sys=='b':
407 
408  # Query parameter search method
409  tag = 'auto_params'
410  print('')
411  print('Which method would you like to use for parameter optimization at each bondlength?')
412  print('a) Automatic search by conjugate-gradient method, starting from a trial value for c.')
413  print('b) Grid search, over an input grid of trial values for c')
414  auto = input('Your response ("a" or "b"): ')
415  # Check response
416  options = ['a','b']
417  while not (auto in options):
418  print('Error: Invalid response, please enter "a" or "b", without quotes.')
419  auto = input('Your response: ')
420  # Record response
421  settings = ['.TRUE.','.FALSE.']
422  params[tag] = settings[options.index(auto)]
423 
424  # Query ham_fdstep - only for developers
425  #tag='ham_fdstep'
426  #prompt='Enter finite difference step for differentiation of the H2-ion Hamiltonian.'
427  #temp = query_posfloat(prompt,tag,d_ham_fdstep)
428  #if temp!='':
429  # params[tag] = str(temp)
430 
431  # Parameter information - c
432  print('')
433  print('Parameter "c" is a coefficient in the Hydrogen Ion wavefunction.')
434  print('')
435 
436  if auto=='a':
437 
438  # Query c_fdstep - only for developers
439  #tag='c_fdstep'
440  #prompt='Enter finite difference step for conjugate-gradient search of parameter c.'
441  #temp = query_posfloat(prompt,tag,d_c_fdstep)
442  #if temp!='':
443  # params[tag] = str(temp)
444 
445  # Query start value for auto-search of parameter c
446  tag='c'
447  prompt='Enter starting guess value for the conjugate-gradient search of parameter c.'
448  temp = query_posfloat(prompt,tag,d_c)
449  if temp!='':
450  params[tag] = str(temp)
451 
452  else:
453 
454  # Check if user wants defaults
455  tag = 'c_grid'
456  use_def = query_default(tag, d_c_grid)
457 
458  # If not defaults, query grid for c
459  if use_def == 'n':
460  mini, maxi, num = query_grid(tag)
461  params[tag] = '{:}, {:}, {:}'.format(mini, maxi, num)
462 
463 
464  # Parameter information - bond
465  print('')
466  print('Quantity of interest "bond" is the Hydrogen bondlength in Bohr units.')
467  print('')
468 
469  # Check if user wants defaults
470  tag = 'bond'
471  use_def = query_default(tag,d_bond_h2plus)
472 
473  # If not defaults, query grid for c
474  if use_def == 'n':
475  mini, maxi, num = query_grid(tag)
476  params[tag] = '{:}, {:}, {:}'.format(mini, maxi, num)
477 
478  # H2
479  elif sys=='c':
480 
481  # Query parameter search method
482  tag = 'auto_params'
483  print('')
484  print('Which method would you like to use for parameter optimization at each bondlength?')
485  print('a) Automatic search by conjugate-gradient method, starting from a trial value for beta.')
486  print('b) Grid search, over an input grid of trial values for beta')
487  auto = input('Your response ("a" or "b"): ')
488  # Check response
489  options = ['a','b']
490  while not (auto in options):
491  print('Error: Invalid response, please enter "a" or "b", without quotes.')
492  auto = input('Your response: ')
493  # Record response
494  settings = ['.TRUE.','.FALSE.']
495  params[tag] = settings[options.index(auto)]
496 
497  # Parameter information - c
498  print('')
499  print('Parameter "beta" is a coefficient in the Hydrogen Molecule wavefunction.')
500  print('')
501 
502  if auto=='a':
503 
504  # Query start value for auto-search of parameter c
505  tag='beta'
506  prompt='Enter starting guess value for the conjugate-gradient search of parameter beta.'
507  temp = query_posfloat(prompt,tag,d_beta)
508  if temp!='':
509  params[tag] = str(temp)
510 
511  else:
512 
513  # Check if user wants defaults
514  tag = 'beta_grid'
515  use_def = query_default(tag, d_beta_grid)
516 
517  # If not defaults, query grid for c
518  if use_def == 'n':
519  mini, maxi, num = query_grid(tag)
520  params[tag] = '{:}, {:}, {:}'.format(mini, maxi, num)
521 
522 
523  # Parameter information - bond
524  print('')
525  print('Quantity of interest "bond" is the Hydrogen bondlength in Bohr units.')
526  print('')
527 
528  # Check if user wants defaults
529  tag = 'bond'
530  use_def = query_default(tag,d_bond_h2)
531 
532  # If not defaults, query grid for c
533  if use_def == 'n':
534  mini, maxi, num = query_grid(tag)
535  params[tag] = '{:}, {:}, {:}'.format(mini, maxi, num)
536 
537 
538  # Return dictionaries with user inputs
539  return control, params
540 
541 
def query_params()
Major function that queries the user for all the inputs required to assemble params....
Definition: dice_write.py:189
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ write_params()

def repo.DicePy.dice_write.write_params (   control,
  params 
)

Major subroutine that writes the input file params.txt.

Parameters
[in]controlDictionay with all inputs for the CONTROL namelist in params.txt
[in]paramsDictionay with all inputs for the PARAMETER namelist in params.txt

Definition at line 547 of file dice_write.py.

547 def write_params(control,params):
548 
549  # Open the file
550  f = open('params.txt','w')
551 
552  # CONTROL namelist
553  f.write('&CONTROL\n')
554  for tag in control.keys():
555  f.write(tag+' \t= '+str(control[tag])+'\n')
556  f.write('/\n')
557 
558  # PARAMETERS namelist
559  if params == 'test':
560  f.write('&PARAMETERS\n')
561  f.write('\\ \n')
562  else:
563  f.write('&PARAMETERS\n')
564  sys = control['p_system'].replace("'","")
565 
566  for tag in params.keys():
567  f.write(sys+'%'+tag+' \t= '+params[tag]+'\n')
568  f.write('/\n')
569 
570  # Close the file
571  f.close()
def write_params(control, params)
Major subroutine that writes the input file params.txt.
Definition: dice_write.py:547
+ Here is the caller graph for this function: