singlepointparser.py 4.71 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 cp2kparser.versions.cp2k262.singlepointforceparser import CP2KSinglePointForceParser
5
from nomadcore.caching_backend import CachingLevel
6
from cp2kparser.versions.cp2k262.commonparser import CP2KCommonParser
7
8
9
10
11
12
13
14
15
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
    """
16
    def __init__(self, parser_context):
17
18
        """
        """
19
        super(CP2KSinglePointParser, self).__init__(parser_context)
20
        self.setup_common_matcher(CP2KCommonParser(parser_context))
21

22
23
        #=======================================================================
        # Cache levels
24
        self.caching_levels.update({
25
26
27
28
29
30
31
            '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,
        })

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

    #===========================================================================
47
    # onClose triggers
48
49
50
51
52
53
54
55
56
    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:
57
58
                force_parser = CP2KSinglePointForceParser(self.parser_context)
                force_parser.parse(force_file)
59
60
61
            else:
                logger.warning("The file containing the forces printed by ENERGY_FORCE calculation could not be found.")

62
63
64
        # 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.
65
        self.cache_service.addValue("number_of_scf_iterations")
66
67
68
69
70

    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")
71
72
73
        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")
74
75
76
77
        backend.closeSection("section_scf_iteration", gId)

    def onClose_x_cp2k_section_quickstep_calculation(self, backend, gIndex, section):
        """"""
78
79
80
81
        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")
82
83
84
85

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

89
90
91
92
    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.
        """
93
94
        self.cache_service.addArrayValues("atom_positions", unit="angstrom")
        self.cache_service.addArrayValues("simulation_cell", unit="angstrom")
95

96
    #===========================================================================
97
    # adHoc functions