Commit e40ef590 authored by Marco Govoni's avatar Marco Govoni
Browse files

Updated fetch_input1

parent 33aa9646
......@@ -597,7 +597,7 @@ SUBROUTINE add_intput_parameters_to_json_file( num_drivers, driver, json )
END SUBROUTINE
SUBROUTINE fetch_input1( num_drivers, driver, verbose )
SUBROUTINE fetch_input1( num_drivers, driver, debug )
!
USE west_version, ONLY : start_forpy, end_forpy
USE io_push, ONLY : io_push_title,io_push_value,io_push_bar,io_push_es0,io_push_c512
......@@ -625,7 +625,7 @@ SUBROUTINE fetch_input1( num_drivers, driver, verbose )
!
INTEGER, INTENT(IN) :: num_drivers
INTEGER, INTENT(IN) :: driver(num_drivers)
LOGICAL, INTENT(IN) :: verbose
LOGICAL, INTENT(IN) :: debug
!
INTEGER :: IERR
TYPE(tuple) :: args
......@@ -646,18 +646,17 @@ SUBROUTINE fetch_input1( num_drivers, driver, verbose )
CALL start_clock('fetch_input')
!
IF ( mpime==root ) THEN
!
!CALL start_forpy()
!
IERR = import_py(pymod, "fetch_input")
!
IF ( ANY(driver(:)==1) ) THEN
!
IERR = tuple_create(args, 1)
IERR = tuple_create(args, 2)
IERR = args%setitem(0, TRIM(ADJUSTL(main_input_file)) )
IERR = args%setitem(1, "input_west" )
IERR = dict_create(kwargs)
!
IERR = call_py(return_obj, pymod, "read_input_west", args, kwargs)
IERR = call_py(return_obj, pymod, "read_keyword_from_file", args, kwargs)
IERR = cast(return_dict, return_obj)
!
CALL args%destroy
......@@ -682,10 +681,11 @@ SUBROUTINE fetch_input1( num_drivers, driver, verbose )
!
IERR = tuple_create(args, 2)
IERR = args%setitem(0, TRIM(ADJUSTL(main_input_file)) )
IERR = args%setitem(1, nq )
IERR = args%setitem(1, "wstat_control" )
IERR = dict_create(kwargs)
IERR = kwargs%setitem("nq",nq)
!
IERR = call_py(return_obj, pymod, "read_wstat_control", args, kwargs)
IERR = call_py(return_obj, pymod, "read_keyword_from_file", args, kwargs)
IERR = cast(return_dict, return_obj)
!
CALL args%destroy
......@@ -721,14 +721,6 @@ SUBROUTINE fetch_input1( num_drivers, driver, verbose )
!
CALL pymod%destroy
!
IERR = import_py(pymod, "sys")
IERR = pymod%getattribute(return_obj, "stdout")
IERR = call_py_noret(return_obj, "flush")
!
CALL pymod%destroy
CALL return_obj%destroy
!CALL end_forpy()
!
ENDIF
!
! BCAST & CHECKS
......@@ -788,13 +780,10 @@ SUBROUTINE fetch_input1( num_drivers, driver, verbose )
ENDIF
!
ENDIF
! OPEN(1000+mpime)
! WRITE(1000+mpime,*) driver, qe_prefix, west_prefix, outdir
! CLOSE(1000+mpime)
!
! REPORT
!
IF ( .TRUE. ) THEN
IF ( debug ) THEN
!
IF ( ANY(driver(:)==1) ) THEN
!
......@@ -893,19 +882,20 @@ SUBROUTINE fetch_input1( num_drivers, driver, verbose )
!
ENDIF
!
IF( mpime == root ) THEN
!
CALL json%initialize()
CALL json%load_file(filename=TRIM(logfile))
!
CALL add_intput_parameters_to_json_file( num_drivers, driver, json )
!
OPEN( NEWUNIT=iunit, FILE=TRIM(logfile) )
CALL json%print_file( iunit )
CLOSE( iunit )
CALL json%destroy()
!
ENDIF
!
ENDIF
!
IF( mpime == root ) THEN
!
CALL json%initialize()
CALL json%load_file(filename=TRIM(logfile))
!
CALL add_intput_parameters_to_json_file( num_drivers, driver, json )
!
OPEN( NEWUNIT=iunit, FILE=TRIM(logfile) )
CALL json%print_file( iunit )
CLOSE( iunit )
CALL json%destroy()
!
ENDIF
!
......
#!/usr/bin/python
# File: fetch_input.py
# Test: python3 fetch_input.py
from __future__ import print_function
import sys
......@@ -8,7 +9,45 @@ import os
import yaml
import json
def open_and_parse_file(fileName) :
#########################
# STATIC DEFAULT VALUES #
#########################
default = {}
# input_west
default["input_west"] = {}
default["input_west"]["qe_prefix"] = "pwscf"
default["input_west"]["west_prefix"] = "west"
default["input_west"]["outdir"] = "./"
# wstat_control
default["wstat_control"] = {}
default["wstat_control"]["wstat_calculation"] = "S"
default["wstat_control"]["n_pdep_eigen"] = 1
default["wstat_control"]["n_pdep_times"] = 4
default["wstat_control"]["n_pdep_maxiter"] = 100
default["wstat_control"]["n_dfpt_maxiter"] = 250
default["wstat_control"]["n_pdep_read_from_file"] = 0
default["wstat_control"]["trev_pdep"] = 1.e-3
default["wstat_control"]["trev_pdep_rel"] = 1.e-1
default["wstat_control"]["tr2_dfpt"] = 1.e-12
default["wstat_control"]["l_kinetic_only"] = False
default["wstat_control"]["l_minimize_exx_if_active"] = False
default["wstat_control"]["l_use_ecutrho"] = False
default["wstat_control"]["qlist"] = [ 1 ]
############################
# DYNAMICAL DEFAULT VALUES #
############################
def update_default_values(key,kwargs) :
assert key in default.keys()
#
if key == "wstat_control" :
assert("nq") in kwargs.keys()
nq = kwargs["nq"]
default[key]["qlist"] = [ i+1 for i in range(nq) ]
def open_and_parse_file(fileName="west.in") :
"""Opens a file and parses it using the YAML sintax
:param fileName: name of the file
......@@ -26,59 +65,111 @@ def open_and_parse_file(fileName) :
print("Cannot parse file")
except :
print("Cannot open file : ",fileName)
#
return data
def check_data(keyword, parsed_data, default) :
def print_bar(prefix="",nmarks=92) :
"""Prints bar.
:param prefix: prefix
:type prefix: ``string``
:param nmarks: number of marks
:type nmarks: ``int``
"""
#
s = prefix
for i in range(nmarks) :
s+="-"
print(s)
def check_dict(parsed_data={}, default_data={}) :
"""Check data: returns a dictionary with the same keys of default_data. If keys are matching, values of default_data are replaced with those of parsed_data.
:param parsed_data: parsed data
:type parsed_data: ``dict``
:param default_data: default data
:type default_data: ``dict``
:return: checked data
:rtype: ``dict``
"""
#
data = {}
#
if keyword in parsed_data.keys() :
for key in default.keys() :
if key in parsed_data[keyword].keys() :
data[key] = parsed_data[keyword][key]
else :
data[key] = default[key]
else :
print("Missing keyword in input file : ",keyword)
for key in default_data.keys() :
if key in parsed_data.keys() :
data[key] = parsed_data[key]
else :
data[key] = default_data[key]
#
return data
def read_input_west(*args, **kwargs):
def print_dict(title="input_west", data={}) :
"""Prints data.
:param title: title
:type title: ``string``
:param data: data to print
:type default_data: ``dict``
"""
#
nmarks = 92
nspaces = 5
s = ""
for i in range(nspaces) :
s+=" "
#
print_bar(s,nmarks)
print(s+"I/O Summary : "+str(title))
print_bar(s,nmarks)
for key in data.keys() :
print(s+key,"=",data[key])
print_bar(s,nmarks)
sys.stdout.flush()
def read_keyword_from_file(*args, **kwargs):
"""Read keyword from file
:return: read data
:rtype: ``dict``
"""
#
fileName = args[0]
keyword = args[1]
#
parsed_data = open_and_parse_file(fileName)[keyword]
default_data = default[keyword]
#
default = {}
default["qe_prefix"] = "pwscf"
default["west_prefix"] = "west"
default["outdir"] = "./"
update_default_values(keyword,kwargs)
#
parsed_data = open_and_parse_file(fileName)
data = check_dict( parsed_data, default_data )
#
checked_data = check_data( "input_west", parsed_data, default )
print( checked_data )
return checked_data
print_dict(keyword, data)
#
return data
def read_wstat_control(*args, **kwargs):
def test() :
#
fileName = args[0]
nq = args[1]
#
default = {}
default["wstat_calculation"] = "S"
default["n_pdep_eigen"] = 1
default["n_pdep_times"] = 4
default["n_pdep_maxiter"] = 100
default["n_dfpt_maxiter"] = 250
default["n_pdep_read_from_file"] = 0
default["trev_pdep"] = 1.e-3
default["trev_pdep_rel"] = 1.e-1
default["tr2_dfpt"] = 1.e-12
default["l_kinetic_only"] = False
default["l_minimize_exx_if_active"] = False
default["l_use_ecutrho"] = False
default["qlist"] = [ i+1 for i in range(nq) ]
#
parsed_data = open_and_parse_file(fileName)
#
checked_data = check_data( "wstat_control", parsed_data, default )
print( checked_data )
return checked_data
fileName = "west.in"
#
with open(fileName, "w") as file :
file.write("""
input_west :
qe_prefix : molecule
west_prefix : molecule
outdir : "./"
wstat_control :
wstat_calculation : R
""")
#
read_keyword_from_file(fileName,"input_west")
read_keyword_from_file(fileName,"wstat_control",nq=20)
#
os.remove(fileName)
if __name__ == "__main__":
# execute only if run as a script
test()
......@@ -36,7 +36,7 @@ SUBROUTINE wstat_readin()
!
! READ INPUT_WEST
!
CALL fetch_input1(1,(/1/),.TRUE.)
CALL fetch_input1(1,(/1/),.FALSE.)
!
! read the input file produced by the pwscf program
! allocate memory and recalculate what is needed
......@@ -52,7 +52,7 @@ SUBROUTINE wstat_readin()
!
! READ other sections of the input file
!
CALL fetch_input1(1,(/2/),.TRUE.)
CALL fetch_input1(1,(/2/),.FALSE.)
!
CALL stop_clock('wstat_readin')
!
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment