singlepointparser.py 4.76 KB
Newer Older
1
from __future__ import absolute_import
2
3
from nomadcore.simple_parser import SimpleMatcher as SM
from nomadcore.baseclasses import MainHierarchicalParser
4
from .singlepointforceparser import CP2KSinglePointForceParser
5
from nomadcore.caching_backend import CachingLevel
6
from .commonparser import CP2KCommonParser
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import logging
logger = logging.getLogger("nomad")


#===============================================================================
class CP2KSinglePointParser(MainHierarchicalParser):
    """The main parser class. Used to parse the CP2K calculation with run types:
        -ENERGY
        -ENERGY_FORCE
    """
    def __init__(self, file_path, parser_context):
        """
        """
        super(CP2KSinglePointParser, self).__init__(file_path, parser_context)
21
        self.setup_common_matcher(CP2KCommonParser(parser_context))
22

23
24
        #=======================================================================
        # Cache levels
25
        self.caching_levels.update({
26
27
28
29
30
31
32
            'x_cp2k_energy_total_scf_iteration': CachingLevel.ForwardAndCache,
            'x_cp2k_energy_XC_scf_iteration': CachingLevel.ForwardAndCache,
            'x_cp2k_energy_change_scf_iteration': CachingLevel.ForwardAndCache,
            'x_cp2k_stress_tensor': CachingLevel.ForwardAndCache,
            'x_cp2k_section_stress_tensor': CachingLevel.ForwardAndCache,
        })

33
34
        #=======================================================================
        # SimpleMatchers
35
36
        self.root_matcher = SM("",
            forwardMatch=True,
37
            sections=['section_run', "section_single_configuration_calculation", "section_system", "section_method"],
38
            otherMetaInfo=["atom_forces"],
39
40
            subMatchers=[
                self.cm.header(),
41
42
                self.cm.quickstep_header(),
                self.cm.quickstep_calculation(),
43
                self.cm.footer(),
44
45
46
47
            ]
        )

    #===========================================================================
48
    # onClose triggers
49
50
51
52
53
54
55
56
57
58
59
60
61
62
    def onClose_section_single_configuration_calculation(self, backend, gIndex, section):
        """
        """
        # If the force file for a single point calculation is available, and
        # the forces were not parsed fro the output file, parse the separate
        # file
        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()
            else:
                logger.warning("The file containing the forces printed by ENERGY_FORCE calculation could not be found.")

63
64
65
66
        # Only in the single configuration calculations the number of scf
        # iterations is given. E.g. in geometry optimization there are multiple
        # scf calculations so this loses it's meaning sort of.
        self.cache_service.push_value("number_of_scf_iterations")
67
68
69
70
71

    def onClose_x_cp2k_section_scf_iteration(self, backend, gIndex, section):
        """Keep track of how many SCF iteration are made."""
        self.cache_service["number_of_scf_iterations"] += 1
        gId = backend.openSection("section_scf_iteration")
72
73
74
        section.add_latest_value("x_cp2k_energy_total_scf_iteration", "energy_total_scf_iteration")
        section.add_latest_value("x_cp2k_energy_XC_scf_iteration", "energy_XC_scf_iteration")
        section.add_latest_value("x_cp2k_energy_change_scf_iteration", "energy_change_scf_iteration")
75
76
77
78
        backend.closeSection("section_scf_iteration", gId)

    def onClose_x_cp2k_section_quickstep_calculation(self, backend, gIndex, section):
        """"""
79
80
81
82
        section.add_latest_value("x_cp2k_energy_total", "energy_total")
        section.add_latest_value("x_cp2k_electronic_kinetic_energy", "electronic_kinetic_energy")
        section.add_latest_value("x_cp2k_quickstep_converged", "single_configuration_calculation_converged")
        section.add_latest_array_values("x_cp2k_atom_forces", "atom_forces")
83
84
85
86

    def onClose_x_cp2k_section_stress_tensor(self, backend, gIndex, section):
        """"""
        gId = backend.openSection("section_stress_tensor")
87
        section.add_latest_array_values("x_cp2k_stress_tensor", "stress_tensor")
88
        backend.closeSection("section_stress_tensor", gId)
89

90
91
92
93
94
    def onClose_section_system(self, backend, gIndex, section):
        """Stores the index of the section method. Should always be 0, but
        let's get it dynamically just in case there's something wrong.
        """
        self.cache_service.push_array_values("atom_positions", unit="angstrom")
95
        self.cache_service.push_array_values("simulation_cell", unit="angstrom")
96

97
    #===========================================================================
98
    # adHoc functions