Dice Python Frontend Documentation
dice_check.py
Go to the documentation of this file.
1 
8 
9 # Import packages
10 from DicePy.dice_defaults import *
11 import f90nml
12 
13 #-----------------------------
14 # MINOR FUNCTION DEFINITIONS -
15 #-----------------------------
16 
17 
23 def isvalid(tag,value,options):
24  if not(value in options):
25  print('')
26  print('Error: Invalid input \'{:}={:}\'. The program is expecting one of the following inputs:'.format(tag,value))
27  print(options)
28  print(''); exit()
29 
30 
35 def default_message(tag,dval):
36  if len(dval)==1:
37  print('')
38  print('Note: Tag \'{:}\' is not set. Program will use the default \'{:}={:}\'.'.format(tag,tag,dval))
39  else:
40  print('')
41  print('Note: Tag \'{:}\' is not set. Program will use the following default \'{:}\' grid.'.format(tag,tag))
42  print(dval)
43 
44 
54 def check_step(tag,tags,params,defaults,idx=None):
55  if tag in tags:
56  val = params[tag]
57  if not(type(val) is int):
58  print('')
59  print('Error: Invalid input \'{:}={:}\': The program is expecting positive integer for tag \'{:}\'.'.format(tag,val,tag))
60  print(''); exit()
61  else:
62  return val
63  else:
64  if idx==None:
65  dval = defaults
66  else:
67  dval = defaults[idx]
68  default_message(tag,dval)
69  return dval
70 
71 
79 def check_float(tag,tags,params,defaults,idx=None):
80  if tag in tags:
81  val = params[tag]
82  if not(type(val) is float):
83  print('')
84  print('Error: Invalid input \'{:}={:}\': The program is expecting positive float for tag \'{:}\'.'.format(tag,val,tag))
85  print(''); exit()
86  else:
87  if idx==None:
88  dval = defaults
89  else:
90  dval = defaults[idx]
91  default_message(tag,dval)
92 
93 
100 def check_grid(tag,tags,params,default_grid):
101  if tag in tags:
102  length = len(params[tag])
103  if length == 3:
104  mini, maxi, num = params[tag]
105  if not(type(mini) is float):
106  print('')
107  print('Error: Invalid input \'{:}\' min = {:}: The program is expecting positive float for \'{:}\' grid minimum.'.format(tag,mini,tag))
108  print(''); exit()
109  elif not(type(maxi) is float):
110  print('')
111  print('Error: Invalid input \'{:}\' max = {:}: The program is expecting positive float for \'{:}\' grid maximum.'.format(tag,maxi,tag))
112  print(''); exit()
113  elif mini > maxi:
114  print('')
115  print('Error: Invalid input: The program requires minimum < maximum for \'{:}\' grid.'.format(tag))
116  print(''); exit()
117  elif not(type(num) is int):
118  print('')
119  print('Error: Invalid input \'{:}\' num = {:}: The program is expecting positive integer number of points in \'{:}\' grid.'.format(tag,num,tag))
120  print(''); exit()
121  elif num <= 0:
122  print('')
123  print('Error: Invalid input \'{:}\' num = {:}: The program is expecting positive integer number of points in \'{:}\' grid.'.format(tag,num,tag))
124  print(''); exit()
125  elif mini==maxi and num!=1:
126  print('')
127  print('Error: Invalid input: For \'{:}\' grid, if minimum=maximum, The program is expecting num=1.'.format(tag))
128  print(''); exit()
129  else:
130  print('')
131  print('Error: Invalid input: The program is expecting three input values (minimum, maximum, number of points) for grid \'{:}\'.'.format(tag))
132  print(''); exit()
133  else:
134  default_message(tag,default_grid)
135 
136 
137 #------------------------
138 # MAJOR CHECK FUNCTIONS -
139 #------------------------
140 
141 
151 def check_inputs(nml):
152 
153  # Check for CONTROL header
154  headers = [header for header in nml]
155  if not ('control' in headers):
156  print('')
157  print('Error: Invalid format: CONTROL namelist is missing.')
158  print(''); exit()
159 
160  # Extract tags of CONTROL namelist
161  control = nml['control']
162  tags = [tag for tag in control]
163  ctags = tags.copy()
164 
165  # Check for invalid tags in CONTROL namelist
166  invalid_tags = [tag for tag in ctags if not(tag in corder)]
167  if len(invalid_tags)!=0:
168  print('')
169  print('Error: Invalid tags present in the CONTROL namelist tags.')
170  print('Remove the following invalid tags:')
171  for tag in invalid_tags:
172  print(tag)
173  print(''); exit()
174 
175  # Check order of CONTROL namelist tags
176  order = [tag for tag in corder if tag in ctags]
177  if not(ctags==order):
178  print('')
179  print('Error: Invalid format. The CONTROL namelist tags need to be in the following order:')
180  print(order)
181  print(''); exit()
182 
183  # Check system
184  # Essential tag
185  tag = 'p_system'
186  if not(tag in tags):
187  print('')
188  print('Incomplete params.txt: The \'{:}\' tag is missing.'.format(tag))
189  print(''); exit()
190  p_system = control[tag]
191  isvalid(tag,p_system,systems)
192 
193  # Get system index for defaults
194  sys_idx = systems.index(p_system)
195 
196  # Special Test Case
197  if p_system == 'test':
198  return
199  else:
200  # Check for PARAMETERS namelist
201  if not ('parameters' in headers):
202  print('')
203  print('Error: Invalid format: PARAMETERS namelist is missing.')
204  print(''); exit()
205 
206  # Check values in CONTROL namelist
207 
208  # Check run_equil
209  # Default run_equil=.FALSE.
210  tag = 'run_equil'
211  if tag in tags:
212  equi = control[tag]
213  isvalid(tag,equi,boolean)
214  else:
215  default_message(tag,d_equi)
216 
217  # Check run_restart
218  # Default run_restart=.FALSE.
219  tag = 'run_restart'
220  if tag in tags:
221  rres = control[tag]
222  isvalid(tag,rres,boolean)
223  else:
224  default_message(tag,d_rres)
225 
226  # Check write_restart
227  # Default write_restart=.FALSE.
228  tag = 'write_restart'
229  if tag in tags:
230  wres = control[tag]
231  isvalid(tag,wres,boolean)
232  else:
233  wres = d_wres
234  default_message(tag,d_wres)
235 
236  # Check restart_step
237  # Default restart_step=1
238  if wres:
239  tag = 'restart_num'
240  if tag in tags:
241  rnum = control[tag]
242  if not(type(rnum) is int):
243  print('')
244  print('Error: Invalid input \'{:}={:}\': The program is expecting integer >=0 for tag \'{:}\'.'.format(tag,rnum,tag))
245  print(''); exit()
246  elif not(rnum>0):
247  print('')
248  print('Error: Invalid input \'{:}={:}\': The program is expecting a positive integer for tag \'{:}\'.'.format(tag,rnum,tag))
249  print(''); exit()
250  else:
251  default_message(tag,d_rnum)
252 
253  # Check write_chains
254  # Default write_chains=.FALSE.
255  tag = 'write_chains'
256  if tag in tags:
257  chn = control[tag]
258  isvalid(tag,chn,boolean)
259  if equi and chn:
260  # For equilibration runs, it is redundant to set write_chains=.TRUE.
261  print('')
262  print('Error: Cannot set write_chain=.TRUE. for equilibration runs.')
263  print(' Equilibration runs (run_equil=.TRUE.) automatically output the Markov chains.')
264  print(' It is hence redundant to set write_chains=.TRUE. along with run_equil=.TRUE.')
265  print(' Please remove the tag "write_chains" from this CONTROL namelist.')
266  print(''); exit()
267  elif (not equi):
268  default_message(tag,d_chn)
269 
270  # Check write_log
271  # Default write_log=.FALSE.
272  tag = 'write_log'
273  if tag in tags:
274  wlog = control[tag]
275  isvalid(tag,wlog,boolean)
276  else:
277  default_message(tag,d_log)
278 
279 
280  # Check p_system of PARAMETERS namelist
281  parameters = nml['parameters']
282  sys_small = ['qho','h2plus','h2','test']
283  par_sys = [sys for sys in parameters]
284  if len(par_sys)==0:
285  # Print defaults
286  print('')
287  print('Note: PARAMETERS namelist is empty.')
288  if (equi):
289  print('The program will use the following default parameters for equilibration mode.')
290  print('steps=',d_equi_steps[sys_idx],', burn_step=0, thin_step=1')
291  else:
292  print('The program will use the following default parameters for VQMC simulation mode.')
293  print('steps=',d_vqmc_steps[sys_idx],', burn_step=',d_bstep[sys_idx],', thin_step=',d_tstep[sys_idx])
294  print('rcut=',d_rcut[sys_idx],', sigma=',d_sigma[sys_idx])
295  if p_system=='QHO':
296  print('alpha=[{:},{:},{:}]'.format(d_alpha[0],d_alpha[1],d_alpha[2]))
297  elif p_system=='H2plus':
298  print('auto_params=',d_auto_h2plus)
299  if d_auto_h2plus=='.TRUE.':
300  print('c=',d_c)
301  else:
302  print('c_grid=[{:},{:},{:}]'.format(d_c_grid[0],d_c_grid[1],d_c_grid[2]))
303  print('bond=[{:},{:},{:}]'.format(d_bond_h2plus[0],d_bond_h2plus[1],d_bond_h2plus[2]))
304  elif p_system=='H2':
305  print('auto_params=',d_auto_h2)
306  if d_auto_h2=='.TRUE.':
307  print('beta=',d_beta)
308  else:
309  print('beta_grid=[{:},{:},{:}]'.format(d_beta_grid[0],d_beta_grid[1],d_beta_grid[2]))
310  print('bond=[{:},{:},{:}]'.format(d_bond_h2[0],d_bond_h2[1],d_bond_h2[2]))
311  return
312  elif len(par_sys)>1:
313  print('')
314  print('Error: Invalid input: Either multiple or no system defitions present in PARAMETERS namelist tags.')
315  print('The program is expecting one, and only one, of the following strings before a percent symbol:')
316  print(systems)
317  print(''); exit()
318  elif not (par_sys[0] in sys_small):
319  print('')
320  print('Error: Invalid system definition in PARAMETERS namelist tags. The program is expecting one of the following strings before a percent symbol:')
321  print(systems)
322  print(''); exit()
323 
324  # Extract tags of PARAMETERS namelist
325  params = parameters[par_sys[0]]
326  tags = [tag for tag in params]
327  ptags = tags.copy()
328 
329  # Check for invalid tags in PARAMMETERS namelist
330  if p_system=='QHO':
331  porder = porder_qho.copy()
332  elif p_system=='H2plus':
333  porder = porder_h2plus.copy()
334  elif p_system=='H2':
335  porder = porder_h2.copy()
336  invalid_tags = [tag for tag in ptags if not(tag in porder)]
337  if len(invalid_tags)!=0:
338  print('')
339  print('Error: Invalid tags present in the PARAMETERS namelist tags.')
340  print('Remove the following invalid tags:')
341  print(invalid_tags)
342  print(''); exit()
343 
344  # Check order of PARAMETERS namelist
345  order = [tag for tag in porder if tag in ptags]
346  if not(ptags==order):
347  print('')
348  print('Error: Invalid format. The PARAMETERS namelist tags need to be in the following order:')
349  print(order)
350  print(''); exit()
351 
352  # Check values in PARAMETERS namelist
353 
354  # Check total steps
355  steps = check_step('steps',tags,params,d_steps,sys_idx)
356 
357  # Check burn steps
358  bstep = check_step('burn_step',tags,params,d_bstep,sys_idx)
359 
360  # Check thin steps
361  tstep = check_step('thin_step',tags,params,d_tstep,sys_idx)
362 
363  # Check compatibility of steps
364  if bstep >= steps:
365  print('')
366  print('Error: Incompatable inputs steps={:} and burn_step={:}. The program is expecting burn_step < step.'.format(steps,bstep))
367  print(''); exit()
368  if tstep >= steps-bstep:
369  print('')
370  print('Error: Incompatable inputs steps={:}, burn_step={:} and thin_step={:}. The program is expecting thinstep < steps - burn_step.'.format(steps,bstep,tstep))
371  print(''); exit()
372  if (equi and bstep!=0) or (equi and tstep!=1):
373  print('')
374  print('Error: Invalid inputs: For equilibration run, the program requires (defaults) burn_step=0 and thin_step=1.')
375  print('Either specify these values, or just do not specify these tags so that the program uses the defaults.')
376  print(''); exit()
377 
378  # Check rcut
379  check_float('rcut',tags,params,d_rcut,sys_idx)
380 
381  # Check sigma
382  check_float('sigma',tags,params,d_sigma,sys_idx)
383 
384 
385  # System specific parameters
386  # QHO
387  if p_system=='QHO':
388 
389  # Check alpha
390  check_grid('alpha',tags,params,d_alpha)
391 
392  # H2plus
393  elif p_system=='H2plus':
394 
395  # Check auto_params
396  # Default auto_params=.TRUE.
397  tag = 'auto_params'
398  if tag in tags:
399  auto = control[tag]
400  isvalid(tag,auto,boolean)
401  else:
402  default_message(tag,d_auto_h2plus)
403 
404  if auto:
405  # Check c start guess
406  check_float('c',tags,params,d_c)
407  else:
408  # Check c_grid
409  check_grid('c_grid',tags,params,d_c_grid)
410 
411  # Check bond grid
412  check_grid('bond',tags,params,d_bond_h2plus)
413 
414 
415  # H2
416  elif p_system=='H2':
417 
418  # Check auto_params
419  # Default auto_params=.TRUE.
420  tag = 'auto_params'
421  if tag in tags:
422  auto = control[tag]
423  isvalid(tag,auto,boolean)
424  else:
425  default_message(tag,d_auto_h2)
426 
427  if auto:
428  # Check beta start guess
429  check_float('beta',tags,params,d_beta)
430  else:
431  # Check beta_grid
432  check_grid('beta_grid',tags,params,d_beta_grid)
433 
434  # Check bond grid
435  check_grid('bond',tags,params,d_bond_h2)
436 
437  # Check for invalid tags in CONTROL namelist
438  invalid_tags = [tag for tag in ctags if not(tag in corder)]
439  if len(invalid_tags)!=0:
440  print('')
441  print('Error: Invalid tags present in the CONTROL namelist tags.')
442  print('Remove the following invalid tags:')
443  print(invalid_tags)
444  print(''); exit()
445 
446  # Check order of CONTROL namelist tags
447  order = [tag for tag in corder if tag in ctags]
448  if not(ctags==order):
449  print('')
450  print('Error: Invalid format. The CONTROL namelist tags need to be in the following order:')
451  print(order)
452  print(''); exit()
Contains default values for input parameters of Dice QMC.
def check_float(tag, tags, params, defaults, idx=None)
Checks the input value of a parameter that is expected to be a float.
Definition: dice_check.py:79
def isvalid(tag, value, options)
Checks the validity of the input value given for a tag present in params.txt.
Definition: dice_check.py:23
def check_step(tag, tags, params, defaults, idx=None)
Checks the input value of a parameter that is expected to be an integer step.
Definition: dice_check.py:54
def check_grid(tag, tags, params, default_grid)
Checks the input values for a grid parameter.
Definition: dice_check.py:100
def check_inputs(nml)
Major subroutine that checks the inputs of file params.txt.
Definition: dice_check.py:151
def default_message(tag, dval)
Subroutine that notifies the user when defaults are being used for a certain parameter.
Definition: dice_check.py:35