Commit 35a90bed authored by Lauri Himanen's avatar Lauri Himanen

Started using the new way of giving the input file to the parser.

parent 31ca2e00
......@@ -153,4 +153,4 @@ class CP2KParser(ParserInterface):
)
raise
self.main_parser = parser_class(self.parser_context.main_file, self.parser_context)
self.main_parser = parser_class(self.parser_context)
......@@ -445,8 +445,8 @@ class CP2KCommonParser(CommonParser):
# If the input file is available, parse it
filepath = self.file_service.get_file_by_id("input")
if filepath is not None:
input_parser = CP2KInputParser(filepath, self.parser_context)
input_parser.parse()
input_parser = CP2KInputParser(self.parser_context)
input_parser.parse(filepath)
else:
logger.warning("The input file of the calculation could not be found.")
......
......@@ -16,10 +16,10 @@ class CP2KGeoOptParser(MainHierarchicalParser):
"""Used to parse the CP2K calculation with run types:
-GEO_OPT/GEOMETRY_OPTIMIZATION
"""
def __init__(self, file_path, parser_context):
def __init__(self, parser_context):
"""
"""
super(CP2KGeoOptParser, self).__init__(file_path, parser_context)
super(CP2KGeoOptParser, self).__init__(parser_context)
self.setup_common_matcher(CP2KCommonParser(parser_context))
self.traj_iterator = None
self.energy_reeval_quickstep = None
......
......@@ -28,7 +28,7 @@ class CP2KInputParser(AbstractBaseParser):
supports variables. This is currently not supported, but may be added at
some point.
"""
def __init__(self, file_path, parser_context):
def __init__(self, parser_context):
"""
Attributes:
input_tree: The input structure for this version of CP2K. The
......@@ -38,7 +38,7 @@ class CP2KInputParser(AbstractBaseParser):
variables have been stated explicitly and the additional input files have
been merged.
"""
super(CP2KInputParser, self).__init__(file_path, parser_context)
super(CP2KInputParser, self).__init__(parser_context)
self.input_tree = None
self.input_lines = None
self.unit_mapping = {
......@@ -68,16 +68,16 @@ class CP2KInputParser(AbstractBaseParser):
self.cache_service.add("traj_add_last")
# self.cache_service.add("electronic_structure_method")
def parse(self):
def parse(self, filepath):
#=======================================================================
# Preprocess to spell out variables and to include stuff from other
# files
self.preprocess_input()
self.preprocess_input(filepath)
#=======================================================================
# Gather the information from the input file
self.fill_input_tree(self.file_path)
self.fill_input_tree(filepath)
#=======================================================================
# Parse everything in the input to cp2k specific metadata
......@@ -459,14 +459,14 @@ class CP2KInputParser(AbstractBaseParser):
input_tree_pickle_file = open(pickle_path, 'rb')
self.input_tree = pickle.load(input_tree_pickle_file)
def preprocess_input(self):
def preprocess_input(self, filepath):
"""Preprocess the input file. Concatenate .inc files into the main
input file and explicitly state all variables.
"""
# Read the input file into memory. It shouldn't be that big so we can
# do this easily
input_lines = []
with open(self.file_path, "r") as f:
with open(filepath, "r") as f:
for line in f:
input_lines.append(line.strip())
......@@ -477,7 +477,7 @@ class CP2KInputParser(AbstractBaseParser):
if line.startswith("@INCLUDE") or line.startswith("@include"):
split = line.split(None, 1)
includepath = split[1]
basedir = os.path.dirname(self.file_path)
basedir = os.path.dirname(filepath)
filepath = os.path.join(basedir, includepath)
filepath = os.path.abspath(filepath)
if not os.path.isfile(filepath):
......
......@@ -18,10 +18,10 @@ class CP2KMDParser(MainHierarchicalParser):
-MD
-MOLECULAR_DYNAMICS
"""
def __init__(self, file_path, parser_context):
def __init__(self, parser_context):
"""
"""
super(CP2KMDParser, self).__init__(file_path, parser_context)
super(CP2KMDParser, self).__init__(parser_context)
self.setup_common_matcher(CP2KCommonParser(parser_context))
self.traj_iterator = None
self.vel_iterator = None
......
......@@ -9,13 +9,13 @@ class CP2KSinglePointForceParser(AbstractBaseParser):
calculation. It is not exactly an ZYX file, so here we define separate
parser.
"""
def __init__(self, file_path, parser_context):
super(CP2KSinglePointForceParser, self).__init__(file_path, parser_context)
def __init__(self, parser_context):
super(CP2KSinglePointForceParser, self).__init__(parser_context)
def parse(self):
def parse(self, filepath):
start = False
forces = []
with open(self.file_path) as f:
with open(filepath) as f:
for line in f:
if line.startswith(" # Atom"):
start = True
......
......@@ -13,10 +13,10 @@ class CP2KSinglePointParser(MainHierarchicalParser):
-ENERGY
-ENERGY_FORCE
"""
def __init__(self, file_path, parser_context):
def __init__(self, parser_context):
"""
"""
super(CP2KSinglePointParser, self).__init__(file_path, parser_context)
super(CP2KSinglePointParser, self).__init__(parser_context)
self.setup_common_matcher(CP2KCommonParser(parser_context))
#=======================================================================
......@@ -54,8 +54,8 @@ class CP2KSinglePointParser(MainHierarchicalParser):
if section["atom_forces"] is None:
force_file = self.file_service.get_file_by_id("force_file_single_point")
if force_file is not None:
force_parser = CP2KSinglePointForceParser(force_file, self.parser_context)
force_parser.parse()
force_parser = CP2KSinglePointForceParser(self.parser_context)
force_parser.parse(force_file)
else:
logger.warning("The file containing the forces printed by ENERGY_FORCE calculation could not be found.")
......
......@@ -17,8 +17,8 @@ def get_result(folder, metaname=None):
"""
dirname = os.path.dirname(__file__)
filename = os.path.join("cp2k_{}".format(VERSION), dirname, folder, "unittest.out")
parser = CP2KParser(filename, None, debug=True, log_level=logging.CRITICAL)
results = parser.parse()
parser = CP2KParser(None, debug=True, log_level=logging.CRITICAL)
results = parser.parse(filename)
if metaname is None:
return results
......@@ -32,7 +32,7 @@ class TestErrors(unittest.TestCase):
def test_no_file(self):
"""File is not no present.
"""
with self.assertRaises(IOError):
with self.assertRaises(ValueError):
get_result("errors/no_file", "XC_functional")
def test_invalid_file(self):
......
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