Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
nomad-lab
parser-cp2k
Commits
a91fbdd6
Commit
a91fbdd6
authored
Jun 02, 2016
by
Lauri Himanen
Browse files
Bug fixes.
parent
6755ba03
Changes
4
Show whitespace changes
Inline
Side-by-side
parser/parser-cp2k/cp2kparser/generic/inputparsing.py
View file @
a91fbdd6
...
...
@@ -87,23 +87,25 @@ class CP2KInput(object):
logger
.
warning
(
message
)
return
(
None
,
None
)
keyword
=
section
.
get_keyword
(
keyword
)
keyword
=
section
.
get_keyword
_object
(
keyword
)
if
keyword
and
section
:
return
(
keyword
,
section
)
else
:
return
(
None
,
section
)
def
get_keyword
(
self
,
path
):
"""Returns the keyword that is specified by the given path.
If the keyword has no value set, returns the default value defined in
the XML.
def
get_keyword_value_formatted
(
self
,
path
):
"""
"""
keyword
,
section
=
self
.
get_keyword_and_section
(
path
)
if
keyword
:
return
keyword
.
get_value_formatted
()
def
get_keyword_value
(
self
,
path
):
"""
"""
keyword
,
section
=
self
.
get_keyword_and_section
(
path
)
if
keyword
:
if
keyword
.
value
is
not
None
:
return
keyword
.
get_value
()
else
:
return
keyword
.
default_value
def
get_default_keyword
(
self
,
path
):
return
self
.
get_section
(
path
).
default_keyword
.
value
...
...
@@ -180,10 +182,17 @@ class Keyword(InputObject):
self
.
default_name
=
default_name
def
get_value
(
self
):
"""Returns the unformatted value of this keyword. This is exactly what
was set by the used in the input as a string.
"""
return
self
.
value
def
get_value_formatted
(
self
):
"""Returns the value stored in this keyword by removing the possible
unit definition and formatting the string into the correct data type.
"""
# Decode the unit and the value if not done before
proper_value
=
None
if
self
.
default_unit
:
if
not
self
.
value_no_unit
:
self
.
decode_cp2k_unit_and_value
()
...
...
@@ -191,6 +200,8 @@ class Keyword(InputObject):
proper_value
=
self
.
value_no_unit
else
:
proper_value
=
self
.
value
if
proper_value
is
None
:
proper_value
=
self
.
default_value
returned
=
None
dim
=
int
(
self
.
data_dimension
)
...
...
@@ -297,7 +308,7 @@ class Section(object):
self
.
sections
=
defaultdict
(
list
)
self
.
description
=
None
def
get_keyword
(
self
,
name
):
def
get_keyword
_object
(
self
,
name
):
keyword
=
self
.
keywords
.
get
(
name
)
if
keyword
:
if
len
(
keyword
)
==
1
:
...
...
@@ -305,6 +316,22 @@ class Section(object):
else
:
logger
.
error
(
"The keyword '{}' in '{}' does not exist or has too many entries."
.
format
(
name
,
self
.
name
))
def
get_keyword_value_formatted
(
self
,
name
):
"""Returns the keyword value formatted to the correct shape and type,
and returns the default value if nothing was specified.
"""
keyword_object
=
self
.
get_keyword_object
(
name
)
if
keyword_object
is
not
None
:
value
=
keyword_object
.
get_value_formatted
()
return
value
def
get_keyword_value
(
self
,
name
):
"""Returns the keyword value as a raw string as specfied by the used.
"""
keyword_object
=
self
.
get_keyword_object
(
name
)
if
keyword_object
is
not
None
:
return
keyword_object
.
get_value
()
def
get_subsection
(
self
,
name
):
subsection
=
self
.
sections
.
get
(
name
)
if
subsection
:
...
...
@@ -315,6 +342,16 @@ class Section(object):
else
:
logger
.
error
(
"The subsection '{}' in '{}' does not exist."
.
format
(
name
,
self
.
name
))
def
get_section_parameter
(
self
):
"""Get the section parameter, or if not specified the lone keyword
value.
"""
if
self
.
section_parameter
is
not
None
:
value
=
self
.
section_parameter
.
value
if
value
is
None
:
value
=
self
.
section_parameter
.
lone_keyword_value
return
value
.
upper
()
#===============================================================================
class
SectionParameters
(
InputObject
):
...
...
parser/parser-cp2k/cp2kparser/tools/xmlpreparser.py
View file @
a91fbdd6
...
...
@@ -57,9 +57,9 @@ def recursive_tree_generation(xml_element, for_metainfo=False, name_stack=[]):
section
=
Section
(
section_name
)
# Ignore sections that control the print settings
ignored
=
[
"EACH"
,
"PRINT"
]
if
section_name
in
ignored
:
return
#
ignored = ["EACH", "PRINT"]
#
if section_name in ignored:
#
return
if
for_metainfo
:
# Descriptions
...
...
@@ -223,7 +223,7 @@ def generate_input_object_metainfo_json(child, parent, name_stack):
path
=
"."
.
join
(
name_stack
)
json_obj
=
{}
json_obj
[
"name"
]
=
"x_cp2k_{}.{}"
.
format
(
path
,
child
.
name
)
json_obj
[
"superNames"
]
=
[
"x_cp2k_{}"
.
format
(
path
)]
json_obj
[
"superNames"
]
=
[
"x_cp2k_
section_
{}"
.
format
(
path
)]
# Description
description
=
child
.
description
...
...
@@ -260,9 +260,9 @@ def generate_section_metainfo_json(child, parent, name_stack):
path
=
"."
.
join
(
name_stack
[:
-
1
])
json_obj
=
{}
json_obj
[
"name"
]
=
"x_cp2k_{}"
.
format
(
name
)
json_obj
[
"name"
]
=
"x_cp2k_
section_
{}"
.
format
(
name
)
json_obj
[
"kindStr"
]
=
"type_section"
json_obj
[
"superNames"
]
=
[
"x_cp2k_{}"
.
format
(
path
)]
json_obj
[
"superNames"
]
=
[
"x_cp2k_
section_
{}"
.
format
(
path
)]
description
=
child
.
description
if
description
is
None
or
description
.
isspace
():
...
...
@@ -276,13 +276,13 @@ def generate_section_metainfo_json(child, parent, name_stack):
if
__name__
==
"__main__"
:
# xml to pickle
#
xml_file = open("../versions/cp2k262/input_data/cp2k_input.xml", 'r')
#
object_tree = CP2KInput(generate_object_tree(xml_file))
#
file_name = "../versions/cp2k262/input_data/cp2k_input_tree.pickle"
#
fh = open(file_name, "wb")
#
pickle.dump(object_tree, fh, protocol=2)
xml_file
=
open
(
"../versions/cp2k262/input_data/cp2k_input.xml"
,
'r'
)
object_tree
=
CP2KInput
(
generate_object_tree
(
xml_file
))
file_name
=
"../versions/cp2k262/input_data/cp2k_input_tree.pickle"
fh
=
open
(
file_name
,
"wb"
)
pickle
.
dump
(
object_tree
,
fh
,
protocol
=
2
)
# Metainfo generation
xml_file
=
open
(
"../versions/cp2k262/input_data/cp2k_input.xml"
,
'r'
)
object_tree
=
CP2KInput
(
generate_object_tree
(
xml_file
,
for_metainfo
=
True
))
generate_input_metainfos
(
object_tree
)
#
xml_file = open("../versions/cp2k262/input_data/cp2k_input.xml", 'r')
#
object_tree = CP2KInput(generate_object_tree(xml_file, for_metainfo=True))
#
generate_input_metainfos(object_tree)
parser/parser-cp2k/cp2kparser/versions/cp2k262/inputparser.py
View file @
a91fbdd6
...
...
@@ -103,12 +103,42 @@ class CP2KInputParser(BasicParser):
elif
section_parameter
==
"B3LYP"
:
xc_list
.
append
(
XCFunctional
(
"HYB_GGA_XC_B3LYP"
))
elif
section_parameter
==
"TPSS"
:
xc_list
.
append
(
XCFunctional
(
"MGGA_X_TPSS"
))
xc_list
.
append
(
XCFunctional
(
"MGGA_C_TPSS"
))
else
:
logger
.
warning
(
"Unknown XC functional given in XC_FUNCTIONAL section parameter."
)
# Otherwise one has to look at the individual functional settings
else
:
pass
pbe
=
xc
.
get_subsection
(
"PBE"
)
if
pbe
is
not
None
:
if
pbe
.
accessed
:
sp
=
pbe
.
get_section_parameter
()
if
sp
==
"T"
:
parametrization
=
pbe
.
get_keyword_value_formatted
(
"PARAMETRIZATION"
)
scale_x
=
pbe
.
get_keyword_value_formatted
(
"SCALE_X"
)
scale_c
=
pbe
.
get_keyword_value_formatted
(
"SCALE_C"
)
if
parametrization
==
"ORIG"
:
xc_list
.
append
(
XCFunctional
(
"GGA_X_PBE"
,
scale_x
))
xc_list
.
append
(
XCFunctional
(
"GGA_C_PBE"
,
scale_c
))
elif
parametrization
==
"PBESOL"
:
xc_list
.
append
(
XCFunctional
(
"GGA_X_PBE_SOL"
,
scale_x
))
xc_list
.
append
(
XCFunctional
(
"GGA_C_PBE_SOL"
,
scale_c
))
elif
parametrization
==
"REVPBE"
:
xc_list
.
append
(
XCFunctional
(
"GGA_X_PBE_R"
,
scale_x
))
xc_list
.
append
(
XCFunctional
(
"GGA_C_PBE"
,
scale_c
))
tpss
=
xc
.
get_subsection
(
"TPSS"
)
if
tpss
is
not
None
:
if
tpss
.
accessed
:
sp
=
tpss
.
get_section_parameter
()
if
sp
==
"T"
:
scale_x
=
tpss
.
get_keyword_value_formatted
(
"SCALE_X"
)
scale_c
=
tpss
.
get_keyword_value_formatted
(
"SCALE_C"
)
xc_list
.
append
(
XCFunctional
(
"MGGA_X_TPSS"
,
scale_x
))
xc_list
.
append
(
XCFunctional
(
"MGGA_C_TPSS"
,
scale_c
))
# Sort the functionals alphabetically by name
xc_list
.
sort
(
key
=
lambda
x
:
x
.
name
)
...
...
@@ -137,7 +167,7 @@ class CP2KInputParser(BasicParser):
#=======================================================================
# Cell periodicity
periodicity
=
self
.
input_tree
.
get_keyword
(
"FORCE_EVAL/SUBSYS/CELL/PERIODIC"
)
periodicity
=
self
.
input_tree
.
get_keyword
_value_formatted
(
"FORCE_EVAL/SUBSYS/CELL/PERIODIC"
)
if
periodicity
is
not
None
:
periodicity
=
periodicity
.
upper
()
periodicity_list
=
(
"X"
in
periodicity
,
"Y"
in
periodicity
,
"Z"
in
periodicity
)
...
...
@@ -155,7 +185,7 @@ class CP2KInputParser(BasicParser):
#=======================================================================
# Stress tensor calculation method
stress_tensor_method
=
self
.
input_tree
.
get_keyword
(
"FORCE_EVAL/STRESS_TENSOR"
)
stress_tensor_method
=
self
.
input_tree
.
get_keyword
_value_formatted
(
"FORCE_EVAL/STRESS_TENSOR"
)
if
stress_tensor_method
!=
"NONE"
:
mapping
=
{
"NUMERICAL"
:
"Numerical"
,
...
...
@@ -179,7 +209,7 @@ class CP2KInputParser(BasicParser):
normalized_path
=
path
# Path is relative, project name added
else
:
project_name
=
self
.
input_tree
.
get_keyword
(
"GLOBAL/PROJECT_NAME"
)
project_name
=
self
.
input_tree
.
get_keyword
_value_formatted
(
"GLOBAL/PROJECT_NAME"
)
if
path
:
normalized_path
=
"{}-{}"
.
format
(
project_name
,
path
)
else
:
...
...
@@ -248,7 +278,7 @@ class CP2KInputParser(BasicParser):
for
line
in
self
.
input_lines
:
# Remove comments and whitespaces
line
=
line
.
split
(
'!'
,
1
)[
0
].
strip
()
line
=
line
.
split
(
'!'
,
1
)[
0
].
split
(
'#'
,
1
)[
0
].
strip
()
# Skip empty lines
if
len
(
line
)
==
0
:
...
...
@@ -286,10 +316,14 @@ class CP2KInputParser(BasicParser):
else
:
split
=
line
.
split
(
None
,
1
)
if
len
(
split
)
<=
1
:
raise
IndexError
(
"A keyword in the CP2K input had no value associated with it. The line causing this is: '{}'"
.
format
(
line
))
keyword_name
=
split
[
0
].
upper
()
keyword_value
=
""
else
:
keyword_value
=
split
[
1
]
keyword_name
=
split
[
0
].
upper
()
try
:
self
.
input_tree
.
set_keyword
(
path
+
"/"
+
keyword_name
,
keyword_value
)
except
UnboundLocalError
:
print
line
# Here we store some exceptional print settings that are
# inportant to the parsing. These dont exist in the input tree
...
...
@@ -336,6 +370,7 @@ class CP2KInputParser(BasicParser):
section_parameter
=
section
.
section_parameter
if
section_parameter
is
not
None
:
name
=
"{}.SECTION_PARAMETERS"
.
format
(
path
)
if
section_parameter
.
value
is
not
None
:
self
.
backend
.
addValue
(
name
,
section_parameter
.
value
)
# Default keyword
...
...
test/unittests/cp2k_2.6.2/run_tests.py
View file @
a91fbdd6
...
...
@@ -622,12 +622,12 @@ if __name__ == '__main__':
suites
.
append
(
unittest
.
TestLoader
().
loadTestsFromTestCase
(
TestEnergyForce
))
suites
.
append
(
unittest
.
TestLoader
().
loadTestsFromTestCase
(
TestStressTensorMethods
))
suites
.
append
(
unittest
.
TestLoader
().
loadTestsFromTestCase
(
TestSelfInteractionCorrectionMethod
))
suites
.
append
(
unittest
.
TestLoader
().
loadTestsFromTestCase
(
TestConfigurationPeriodicDimensions
))
suites
.
append
(
unittest
.
TestLoader
().
loadTestsFromTestCase
(
TestSCFConvergence
))
suites
.
append
(
unittest
.
TestLoader
().
loadTestsFromTestCase
(
TestForceFiles
))
suites
.
append
(
unittest
.
TestLoader
().
loadTestsFromTestCase
(
TestPreprocessor
))
suites
.
append
(
unittest
.
TestLoader
().
loadTestsFromTestCase
(
TestGeoOpt
))
suites
.
append
(
unittest
.
TestLoader
().
loadTestsFromTestCase
(
TestGeoOptTrajFormats
))
suites
.
append
(
unittest
.
TestLoader
().
loadTestsFromTestCase
(
TestGeoOptOptimizers
))
#
suites.append(unittest.TestLoader().loadTestsFromTestCase(TestConfigurationPeriodicDimensions))
#
suites.append(unittest.TestLoader().loadTestsFromTestCase(TestSCFConvergence))
#
suites.append(unittest.TestLoader().loadTestsFromTestCase(TestForceFiles))
#
suites.append(unittest.TestLoader().loadTestsFromTestCase(TestPreprocessor))
#
suites.append(unittest.TestLoader().loadTestsFromTestCase(TestGeoOpt))
#
suites.append(unittest.TestLoader().loadTestsFromTestCase(TestGeoOptTrajFormats))
#
suites.append(unittest.TestLoader().loadTestsFromTestCase(TestGeoOptOptimizers))
alltests
=
unittest
.
TestSuite
(
suites
)
unittest
.
TextTestRunner
(
verbosity
=
0
).
run
(
alltests
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment