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 ) ...@@ -597,7 +597,7 @@ SUBROUTINE add_intput_parameters_to_json_file( num_drivers, driver, json )
END SUBROUTINE 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 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 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 ) ...@@ -625,7 +625,7 @@ SUBROUTINE fetch_input1( num_drivers, driver, verbose )
! !
INTEGER, INTENT(IN) :: num_drivers INTEGER, INTENT(IN) :: num_drivers
INTEGER, INTENT(IN) :: driver(num_drivers) INTEGER, INTENT(IN) :: driver(num_drivers)
LOGICAL, INTENT(IN) :: verbose LOGICAL, INTENT(IN) :: debug
! !
INTEGER :: IERR INTEGER :: IERR
TYPE(tuple) :: args TYPE(tuple) :: args
...@@ -646,18 +646,17 @@ SUBROUTINE fetch_input1( num_drivers, driver, verbose ) ...@@ -646,18 +646,17 @@ SUBROUTINE fetch_input1( num_drivers, driver, verbose )
CALL start_clock('fetch_input') CALL start_clock('fetch_input')
! !
IF ( mpime==root ) THEN IF ( mpime==root ) THEN
!
!CALL start_forpy()
! !
IERR = import_py(pymod, "fetch_input") IERR = import_py(pymod, "fetch_input")
! !
IF ( ANY(driver(:)==1) ) THEN 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(0, TRIM(ADJUSTL(main_input_file)) )
IERR = args%setitem(1, "input_west" )
IERR = dict_create(kwargs) 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) IERR = cast(return_dict, return_obj)
! !
CALL args%destroy CALL args%destroy
...@@ -682,10 +681,11 @@ SUBROUTINE fetch_input1( num_drivers, driver, verbose ) ...@@ -682,10 +681,11 @@ SUBROUTINE fetch_input1( num_drivers, driver, verbose )
! !
IERR = tuple_create(args, 2) IERR = tuple_create(args, 2)
IERR = args%setitem(0, TRIM(ADJUSTL(main_input_file)) ) 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 = 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) IERR = cast(return_dict, return_obj)
! !
CALL args%destroy CALL args%destroy
...@@ -721,14 +721,6 @@ SUBROUTINE fetch_input1( num_drivers, driver, verbose ) ...@@ -721,14 +721,6 @@ SUBROUTINE fetch_input1( num_drivers, driver, verbose )
! !
CALL pymod%destroy 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 ENDIF
! !
! BCAST & CHECKS ! BCAST & CHECKS
...@@ -788,13 +780,10 @@ SUBROUTINE fetch_input1( num_drivers, driver, verbose ) ...@@ -788,13 +780,10 @@ SUBROUTINE fetch_input1( num_drivers, driver, verbose )
ENDIF ENDIF
! !
ENDIF ENDIF
! OPEN(1000+mpime)
! WRITE(1000+mpime,*) driver, qe_prefix, west_prefix, outdir
! CLOSE(1000+mpime)
! !
! REPORT ! REPORT
! !
IF ( .TRUE. ) THEN IF ( debug ) THEN
! !
IF ( ANY(driver(:)==1) ) THEN IF ( ANY(driver(:)==1) ) THEN
! !
...@@ -893,19 +882,20 @@ SUBROUTINE fetch_input1( num_drivers, driver, verbose ) ...@@ -893,19 +882,20 @@ SUBROUTINE fetch_input1( num_drivers, driver, verbose )
! !
ENDIF ENDIF
! !
IF( mpime == root ) THEN !
! ENDIF
CALL json%initialize() !
CALL json%load_file(filename=TRIM(logfile)) IF( mpime == root ) THEN
! !
CALL add_intput_parameters_to_json_file( num_drivers, driver, json ) CALL json%initialize()
! CALL json%load_file(filename=TRIM(logfile))
OPEN( NEWUNIT=iunit, FILE=TRIM(logfile) ) !
CALL json%print_file( iunit ) CALL add_intput_parameters_to_json_file( num_drivers, driver, json )
CLOSE( iunit ) !
CALL json%destroy() OPEN( NEWUNIT=iunit, FILE=TRIM(logfile) )
! CALL json%print_file( iunit )
ENDIF CLOSE( iunit )
CALL json%destroy()
! !
ENDIF ENDIF
! !
......
#!/usr/bin/python #!/usr/bin/python
# File: fetch_input.py # File: fetch_input.py
# Test: python3 fetch_input.py
from __future__ import print_function from __future__ import print_function
import sys import sys
...@@ -8,7 +9,45 @@ import os ...@@ -8,7 +9,45 @@ import os
import yaml import yaml
import json 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 """Opens a file and parses it using the YAML sintax
:param fileName: name of the file :param fileName: name of the file
...@@ -26,59 +65,111 @@ def open_and_parse_file(fileName) : ...@@ -26,59 +65,111 @@ def open_and_parse_file(fileName) :
print("Cannot parse file") print("Cannot parse file")
except : except :
print("Cannot open file : ",fileName) print("Cannot open file : ",fileName)
#
return data 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 = {} data = {}
# #
if keyword in parsed_data.keys() : for key in default_data.keys() :
for key in default.keys() : if key in parsed_data.keys() :
if key in parsed_data[keyword].keys() : data[key] = parsed_data[key]
data[key] = parsed_data[keyword][key] else :
else : data[key] = default_data[key]
data[key] = default[key] #
else :
print("Missing keyword in input file : ",keyword)
return data 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] fileName = args[0]
keyword = args[1]
#
parsed_data = open_and_parse_file(fileName)[keyword]
default_data = default[keyword]
# #
default = {} update_default_values(keyword,kwargs)
default["qe_prefix"] = "pwscf"
default["west_prefix"] = "west"
default["outdir"] = "./"
# #
parsed_data = open_and_parse_file(fileName) data = check_dict( parsed_data, default_data )
# #
checked_data = check_data( "input_west", parsed_data, default ) print_dict(keyword, data)
print( checked_data ) #
return checked_data return data
def read_wstat_control(*args, **kwargs): def test() :
# #
fileName = args[0] fileName = "west.in"
nq = args[1] #
# with open(fileName, "w") as file :
default = {} file.write("""
default["wstat_calculation"] = "S" input_west :
default["n_pdep_eigen"] = 1 qe_prefix : molecule
default["n_pdep_times"] = 4 west_prefix : molecule
default["n_pdep_maxiter"] = 100 outdir : "./"
default["n_dfpt_maxiter"] = 250 wstat_control :
default["n_pdep_read_from_file"] = 0 wstat_calculation : R
default["trev_pdep"] = 1.e-3 """)
default["trev_pdep_rel"] = 1.e-1 #
default["tr2_dfpt"] = 1.e-12 read_keyword_from_file(fileName,"input_west")
default["l_kinetic_only"] = False read_keyword_from_file(fileName,"wstat_control",nq=20)
default["l_minimize_exx_if_active"] = False #
default["l_use_ecutrho"] = False os.remove(fileName)
default["qlist"] = [ i+1 for i in range(nq) ]
# if __name__ == "__main__":
parsed_data = open_and_parse_file(fileName) # execute only if run as a script
# test()
checked_data = check_data( "wstat_control", parsed_data, default )
print( checked_data )
return checked_data
...@@ -36,7 +36,7 @@ SUBROUTINE wstat_readin() ...@@ -36,7 +36,7 @@ SUBROUTINE wstat_readin()
! !
! READ INPUT_WEST ! READ INPUT_WEST
! !
CALL fetch_input1(1,(/1/),.TRUE.) CALL fetch_input1(1,(/1/),.FALSE.)
! !
! read the input file produced by the pwscf program ! read the input file produced by the pwscf program
! allocate memory and recalculate what is needed ! allocate memory and recalculate what is needed
...@@ -52,7 +52,7 @@ SUBROUTINE wstat_readin() ...@@ -52,7 +52,7 @@ SUBROUTINE wstat_readin()
! !
! READ other sections of the input file ! READ other sections of the input file
! !
CALL fetch_input1(1,(/2/),.TRUE.) CALL fetch_input1(1,(/2/),.FALSE.)
! !
CALL stop_clock('wstat_readin') 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