qmcchem/scripts/create_properties_ezfio.py

186 lines
3.5 KiB
Python
Executable File

#!/usr/bin/env python2
#
# Creates the properties.config file in the EZFIO directory. This is
# done by reading all the properties written in the src/PROPERTIES
# directory.
#
import os, sys
root = os.environ['QMCCHEM_PATH']
os.chdir(root+'/src/')
sys.path.insert(0,'./')
from properties import properties
# Write file if file has changed
# ==============================
def write_if_modified(filename,tmp_filename):
try:
file = open(filename,'r')
except IOError:
f1 = ""
else:
f1 = file.read()
file.close()
file = open(tmp_filename,'r')
f2 = file.read()
file.close()
if f1 != f2:
os.rename(tmp_filename,filename)
else:
os.remove(tmp_filename)
# Create the EZFIO file for properties
# ====================================
filename = root+'/ezfio_config/properties.config'
tmp_filename = filename + '.new'
# Write temporary file
# --------------------
file = open(tmp_filename,'w')
print >>file, 'properties'
for p in properties:
print >>file, ' %30s logical'%(p[1].ljust(30))
file.close()
write_if_modified(filename,tmp_filename)
# Create the ${QMCCHEM_PATH}/ocaml/Property.ml file
# =================================================
filename = root+'/ocaml/Property.ml'
tmp_filename = filename + '.new'
properties_qmcvar = properties + map(lambda x: (x[0], x[1]+"_qmcvar", x[2]), properties)
file = open(tmp_filename,'w')
# type
# ----
print >>file, """
(* File generated by ${QMCCHEM_PATH}/scripts/create_properties.py. Do not
modify here
*)
type t =
| Cpu
| Wall
| Accep"""
for p in properties_qmcvar:
print >>file, "| %s"%(p[1].capitalize())
# calc function
# -------------
print >>file, """;;
let calc = function
| Cpu
| Wall
| Accep -> true"""
for p in properties:
if p[1] == "e_loc":
tf = "true"
else:
tf = "false"
print >>file, """| %(P)s
| %(P)s_qmcvar ->
begin
if (Ezfio.has_properties_%(p)s ()) then
Ezfio.get_properties_%(p)s ()
else
%(true_false)s
end
"""%{'P':p[1].capitalize(), 'p':p[1], 'true_false': tf}
# set_calc
# --------
print >>file, """;;
let u _ = ();;
let set_calc = function
| Cpu
| Wall
| Accep -> u"""
for p in properties:
print >>file, """| %(P)s
| %(P)s_qmcvar ->
Ezfio.set_properties_%(p)s
"""%{'P':p[1].capitalize(), 'p':p[1]}
# of_string
# ---------
print >>file, """;;
let of_string s =
match (String.lowercase_ascii s) with
| "cpu" -> Cpu
| "wall" -> Wall
| "accep" -> Accep"""
for p in properties_qmcvar:
print >>file, """ | "%(p)s" -> %(P)s"""%{'P':p[1].capitalize(), 'p':p[1]}
print >>file, """ | p -> failwith ("unknown property "^p) ;;
"""
# to_string
# ---------
print >>file, """
let to_string = function
| Cpu -> "Cpu"
| Wall -> "Wall"
| Accep -> "Accep" """
for p in properties_qmcvar:
print >>file, """| %(P)s -> "%(P)s" """%{'P':p[1].capitalize(), 'p':p[1]}
print >>file, """;;
let of_bytes x =
Bytes.to_string x
|> of_string
let to_bytes x =
to_string x
|> Bytes.of_string
"""
# is_scalar
# ---------
print >>file, """
let is_scalar = function
| Cpu -> true
| Wall -> true
| Accep -> true """
for p in properties:
if p[2] == "":
print >>file, """| %(P)s | %(P)s_qmcvar -> true """%{'P':p[1].capitalize()}
else:
print >>file, """| %(P)s | %(P)s_qmcvar -> false """%{'P':p[1].capitalize()}
print >>file, """;;
"""
# all properties
# --------------
print >>file, """
let all = [ Cpu ; Wall ; Accep ; """
for p in properties:
print >>file, " %s ;"%(p[1].capitalize()),
print >>file, "];;"
file.close()
write_if_modified(filename,tmp_filename)