Skip to content
Snippets Groups Projects
Commit 0622015a authored by Himanen, Lauri (himanel1)'s avatar Himanen, Lauri (himanel1)
Browse files

Better documentation.

parent b988039e
Branches
Tags
No related merge requests found
...@@ -3,13 +3,29 @@ ...@@ -3,13 +3,29 @@
import json import json
import os import os
import time import time
from abc import ABCMeta, abstractmethod
from cp2kparser.generics.util import * from cp2kparser.generics.util import *
#=============================================================================== #===============================================================================
class NomadParser(object): class NomadParser(object):
"""The base class for a NoMaD parser. """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): def __init__(self, input_json_string):
self.input_json_string = input_json_string self.input_json_string = input_json_string
...@@ -27,7 +43,10 @@ class NomadParser(object): ...@@ -27,7 +43,10 @@ class NomadParser(object):
self.results = {} self.results = {}
def get_file_contents(self, file_id): 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 cache_limit = 10000
contents = self.file_contents.get(file_id) contents = self.file_contents.get(file_id)
if not contents: if not contents:
...@@ -39,7 +58,9 @@ class NomadParser(object): ...@@ -39,7 +58,9 @@ class NomadParser(object):
return contents return contents
def get_file_size(self, file_id): 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) size = self.file_sizes.get(file_id)
if not size: if not size:
fh = self.file_handles[file_id] fh = self.file_handles[file_id]
...@@ -49,10 +70,11 @@ class NomadParser(object): ...@@ -49,10 +70,11 @@ class NomadParser(object):
return size return size
def get_file_handle(self, file_id): 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) handle = self.file_handles.get(file_id)
if not handle: if not handle:
path = self.file_ids[file_id] path = self.file_ids[file_id]
try: try:
handle = open(path, "r") handle = open(path, "r")
...@@ -64,7 +86,8 @@ class NomadParser(object): ...@@ -64,7 +86,8 @@ class NomadParser(object):
return handle return handle
def analyse_input_json(self): def analyse_input_json(self):
"""Analyze the JSON given as input.
"""
# Try to decode # Try to decode
self.input_json_object = json.loads(self.input_json_string) self.input_json_object = json.loads(self.input_json_string)
...@@ -80,17 +103,6 @@ class NomadParser(object): ...@@ -80,17 +103,6 @@ class NomadParser(object):
# See if the metainfos exist # 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): def get_quantity(self, name):
"""Given a unique quantity id which is present in the metainfo """Given a unique quantity id which is present in the metainfo
declaration, parses the corresponding quantity (if available) and declaration, parses the corresponding quantity (if available) and
...@@ -122,16 +134,30 @@ class NomadParser(object): ...@@ -122,16 +134,30 @@ class NomadParser(object):
print_debug("Elapsed time: {} ms".format((stop-start)*1000)) print_debug("Elapsed time: {} ms".format((stop-start)*1000))
return result 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): def parse_quantity(self, name):
"""Override this function in an actual implementation""" """Parse a quantity from the given files."""
pass pass
@abstractmethod
def check_quantity_availability(self, name): def check_quantity_availability(self, name):
"""Check quantity availability. """Check quantity availability.
-Check the list of available quantities declared in interface. -Check the list of available quantities declared in interface.
-Check if the run type actually produces the quantity -Check if the run type actually produces the quantity
-Check if the quantity is allowed by the 'metainfoToKeep' and -Check if the quantity is allowed by the 'metainfoToKeep' and
'metainfoToSkip' 'metainfoToSkip'
""" """
return True pass
...@@ -13,7 +13,8 @@ from cp2kparser.engines.cp2kinputengine import CP2KInputEngine ...@@ -13,7 +13,8 @@ from cp2kparser.engines.cp2kinputengine import CP2KInputEngine
#=============================================================================== #===============================================================================
class CP2KParser(NomadParser): 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): def __init__(self, input_json_string):
...@@ -167,9 +168,11 @@ class CP2KImplementation(object): ...@@ -167,9 +168,11 @@ class CP2KImplementation(object):
xc_shortcut = self.inputengine.get_subsection("FORCE_EVAL/DFT/XC/XC_FUNCTIONAL").get_parameter() xc_shortcut = self.inputengine.get_subsection("FORCE_EVAL/DFT/XC/XC_FUNCTIONAL").get_parameter()
return { return {
'B3LYP': "HYB_GGA_XC_B3LYP",
'BEEFVDW': "",
'BLYP': "",
'PADE': "LDA_XC_TETER93", 'PADE': "LDA_XC_TETER93",
'PBE': "GGA_X_PBE", 'PBE': "GGA_X_PBE",
'B3LYP': "HYB_GGA_XC_B3LYP",
}.get(xc_shortcut, None) }.get(xc_shortcut, None)
def particle_forces(self): def particle_forces(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment