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