diff --git a/cp2kparser/generics/nomadparser.py b/cp2kparser/generics/nomadparser.py index bcb899c90616f3d9dc59deaf79692e4a79269afb..d128488ff6734c8533791527aef68981ba1e4982 100644 --- a/cp2kparser/generics/nomadparser.py +++ b/cp2kparser/generics/nomadparser.py @@ -3,13 +3,29 @@ import json import os import time +from abc import ABCMeta, abstractmethod from cp2kparser.generics.util import * #=============================================================================== class NomadParser(object): """The base class for a NoMaD parser. + + This class should be inherited by the python based parsers. It provides + general utility methods and a single interface for input and output. + + The utilities that this class provides and the inherited classes should NOT + do: + - Converting results to JSON + - Converting units to SI + - Timing + - Caching of results + - Providing file contents, sizes and handles + + This class also defines some absract methods that each parser must + implement. """ + __metaclass__ = ABCMeta def __init__(self, input_json_string): self.input_json_string = input_json_string @@ -27,7 +43,10 @@ class NomadParser(object): self.results = {} def get_file_contents(self, file_id): - + """Get the contents for the file with the given id. Uses cached result + if available. Does not cache files that are bigger than a certain + limit. + """ cache_limit = 10000 contents = self.file_contents.get(file_id) if not contents: @@ -39,7 +58,9 @@ class NomadParser(object): return contents def get_file_size(self, file_id): - + """Get the size of a file with the given id. Uses cached result + if available. + """ size = self.file_sizes.get(file_id) if not size: fh = self.file_handles[file_id] @@ -49,10 +70,11 @@ class NomadParser(object): return size def get_file_handle(self, file_id): - + """Get the handle for a file with the given id. Uses cached result + if available. + """ handle = self.file_handles.get(file_id) if not handle: - path = self.file_ids[file_id] try: handle = open(path, "r") @@ -64,7 +86,8 @@ class NomadParser(object): return handle def analyse_input_json(self): - + """Analyze the JSON given as input. + """ # Try to decode self.input_json_object = json.loads(self.input_json_string) @@ -80,17 +103,6 @@ class NomadParser(object): # See if the metainfos exist - def setup_version(self): - """Setup a correct implementation for this version of CP2K. - """ - pass - - def determine_file_ids(self): - """If the files have not been given an id, try to determine the - correct ids by looking at the input file, contents and file extensions. - """ - pass - def get_quantity(self, name): """Given a unique quantity id which is present in the metainfo declaration, parses the corresponding quantity (if available) and @@ -122,16 +134,30 @@ class NomadParser(object): print_debug("Elapsed time: {} ms".format((stop-start)*1000)) return result + @abstractmethod + def setup_version(self): + """Setup a correct implementation for this version. + """ + pass + + @abstractmethod + def determine_file_ids(self): + """If the files have not been given an id, try to determine the + correct ids by looking at the input file, contents and file extensions. + """ + pass + + @abstractmethod def parse_quantity(self, name): - """Override this function in an actual implementation""" + """Parse a quantity from the given files.""" pass + @abstractmethod def check_quantity_availability(self, name): """Check quantity availability. - -Check the list of available quantities declared in interface. -Check if the run type actually produces the quantity -Check if the quantity is allowed by the 'metainfoToKeep' and 'metainfoToSkip' """ - return True + pass diff --git a/cp2kparser/implementation/parser.py b/cp2kparser/implementation/parser.py index 70429cb733b268f1873177352de4e22809f43d6e..64fb3d631345e971360bd9af53f6507af26a7184 100644 --- a/cp2kparser/implementation/parser.py +++ b/cp2kparser/implementation/parser.py @@ -13,7 +13,8 @@ from cp2kparser.engines.cp2kinputengine import CP2KInputEngine #=============================================================================== class CP2KParser(NomadParser): - """The interface to a NoMaD CP2K parser. + """The interface for a NoMaD CP2K parser. All parsing actions will go + through this class. """ def __init__(self, input_json_string): @@ -167,9 +168,11 @@ class CP2KImplementation(object): xc_shortcut = self.inputengine.get_subsection("FORCE_EVAL/DFT/XC/XC_FUNCTIONAL").get_parameter() return { + 'B3LYP': "HYB_GGA_XC_B3LYP", + 'BEEFVDW': "", + 'BLYP': "", 'PADE': "LDA_XC_TETER93", 'PBE': "GGA_X_PBE", - 'B3LYP': "HYB_GGA_XC_B3LYP", }.get(xc_shortcut, None) def particle_forces(self):