outputparsing.py 13.6 KB
Newer Older
Lauri Himanen's avatar
Lauri Himanen committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from nomadcore.simple_parser import SimpleMatcher as SM
from nomadcore.caching_backend import CachingLevel
import numpy as np


#===============================================================================
class MetaInfo(object):
    def __init__(self, name, description="", dtypeStr="c", shape=[], dependencies=[]):
        self.name = name
        self.description = description
        self.dtypeStr = dtypeStr
        self.shape = shape
        if not isinstance(dependencies, list):
            dependencies = [dependencies]
        self.dependencies = dependencies


#===============================================================================
class CP2KOutputParser262(object):
    """The object that goes through the CP2K output file and parses everything
    it can using the SimpleParser architecture.
    """

    def __init__(self, cp2kparser, metainfos):
        """Initialize an output parser. The outputparser will sometimes ask the
        CP2KParser for information. E.g. the auxiliary coordinate files are
        identified and read by the CP2KParser.
        """
        self.cp2kparser = cp2kparser
        self.metainfos = metainfos
        self.f_regex = "-?\d+\.\d+(E+|-\d+)?"

        # Define the output parsing tree for this version
        self.outputstructure = SM(
            startReStr="",
            subMatchers=[
                SM(
                    startReStr=r" DBCSR\| Multiplication driver",
                    endReStr="[.\*]+PROGRAM STOPPED IN",
                    required=True,
                    sections=['section_run'],
                    subMatchers=[
                        SM(
                            startReStr=r"[\*\s]+PROGRAM STARTED AT\s+(?P<cp2k_run_start_date>\d{4}-\d{2}-\d{2}) (?P<cp2k_run_start_time>\d{2}:\d{2}:\d{2}.\d{3})",
                        ),
                        SM(
                            startReStr=r" CP2K\| version string:\s+(?P<program_version>[\w\d\W\s]+)",
                        ),
                        SM(
                            startReStr=r" CP2K\| source code revision number:\s+svn:(?P<cp2k_svn_revision>\d+)",
                        ),
                        # System Description
                        SM(
                            startReStr="",
                            sections=["cp2k_system_description"],
56
                            otherMetaInfo=["number_of_atoms"],
Lauri Himanen's avatar
Lauri Himanen committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
                            dependencies={"atom_number": ["cp2k_atom_number"]},
                            subMatchers=[
                                SM(
                                    startReStr=" CELL\|",
                                    forwardMatch=True,
                                    sections=["cp2k_section_cell"],
                                    subMatchers=[
                                        SM(
                                            startReStr=" CELL\| Vector a \[angstrom\]:\s+(?P<cp2k_cell_vector_a>[\d\.]+\s+[\d\.]+\s+[\d\.]+)+"
                                        ),
                                        SM(
                                            startReStr=" CELL\| Vector b \[angstrom\]:\s+(?P<cp2k_cell_vector_b>[\d\.]+\s+[\d\.]+\s+[\d\.]+)+"
                                        ),
                                        SM(
                                            startReStr=" CELL\| Vector c \[angstrom\]:\s+(?P<cp2k_cell_vector_c>[\d\.]+\s+[\d\.]+\s+[\d\.]+)+"
                                        ),
                                    ]
                                ),
                                SM(
                                    startReStr=" FUNCTIONAL\|",
                                    forwardMatch=True,
                                    sections=["section_method", "cp2k_section_functionals"],
79
                                    otherMetaInfo=["XC_functional"],
Lauri Himanen's avatar
Lauri Himanen committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
                                    subMatchers=[
                                        SM(
                                            repeats=True,
                                            startReStr=" FUNCTIONAL\| (?P<cp2k_functional_name>[\w\d\W]+):"
                                        )
                                    ]
                                ),
                                SM(
                                    startReStr=" TOTAL NUMBERS AND MAXIMUM NUMBERS",
                                    sections=["cp2k_section_numbers"],
                                    subMatchers=[
                                        SM(
                                            startReStr="\s+- Atoms:\s+(?P<cp2k_atom_number>\d+)"
                                        ),
                                        SM(
                                            startReStr="\s+- Shell sets:\s+(?P<cp2k_shell_sets>\d+)"
                                        )
                                    ]
                                )
                            ]
                        ),
                        # Molecular Dynamics
                        SM(
                            startReStr=" MD| Molecular Dynamics Protocol",
                            forwardMatch=True,
                            sections=["cp2k_section_md"],
                            subMatchers=[
                                SM(
                                    repeats=True,
                                    startReStr=" ENERGY\| Total FORCE_EVAL",
                                    sections=["cp2k_section_md_step"],
                                    subMatchers=[
                                        SM(
                                            startReStr=" ATOMIC FORCES in \[a\.u\.\]",
                                            sections=["cp2k_section_md_forces"],
                                            subMatchers=[
                                                SM(
                                                    startReStr="\s+\d+\s+\d+\s+[\w\W\d]+\s+(?P<cp2k_md_force_atom_string>{0}\s+{0}\s+{0})".format(self.f_regex),
                                                    sections=["cp2k_section_md_force_atom"],
                                                    repeats=True,
                                                )
                                            ]
                                        ),
                                        SM(
                                            startReStr=" STEP NUMBER\s+=\s+(?P<cp2k_md_step_number>\d+)"
                                        ),
                                        SM(
                                            startReStr=" TIME \[fs\]\s+=\s+(?P<cp2k_md_step_time>\d+\.\d+)"
                                        ),
                                        SM(
                                            startReStr=" TEMPERATURE \[K\]\s+=\s+(?P<cp2k_md_temperature_instantaneous>{0})\s+(?P<cp2k_md_temperature_average>{0})".format(self.f_regex)
                                        ),
                                        SM(
                                            startReStr=" i =",
                                            sections=["cp2k_section_md_coordinates"],
                                            otherMetaInfo=["cp2k_md_coordinates"],
                                            dependencies={"cp2k_md_coordinates": ["cp2k_md_coordinate_atom_string"]},
                                            subMatchers=[
                                                SM(
                                                    startReStr=" \w+\s+(?P<cp2k_md_coordinate_atom_string>{0}\s+{0}\s+{0})".format(self.f_regex),
                                                    endReStr="\n",
                                                    sections=["cp2k_section_md_coordinate_atom"],
                                                    repeats=True,
                                                )
                                            ]
                                        )
                                    ]
                                )
                            ]
                        )
                    ]
                )
            ]
        )
        # The cache settings
        self.cachingLevelForMetaName = {
            'cp2k_cell_vector_a': CachingLevel.Cache,
            'cp2k_cell_vector_b': CachingLevel.Cache,
            'cp2k_cell_vector_c': CachingLevel.Cache,
            'cp2k_section_cell': CachingLevel.Cache,
            'cp2k_system_description': CachingLevel.Cache,
            'cp2k_section_atom_position': CachingLevel.Cache,
            'cp2k_functional_name': CachingLevel.Cache,
            'cp2k_section_functionals': CachingLevel.Cache,
            'cp2k_section_numbers': CachingLevel.Cache,
            'cp2k_atom_number': CachingLevel.Cache,
            'cp2k_shell_sets': CachingLevel.Cache,

            'cp2k_section_md_coordinates': CachingLevel.Cache,
            'cp2k_section_md_coordinate_atom': CachingLevel.Cache,
            'cp2k_md_coordinate_atom_string': CachingLevel.Cache,
            'cp2k_md_coordinate_atom_float': CachingLevel.Cache,

            'cp2k_section_md_forces': CachingLevel.Cache,
            'cp2k_section_md_force_atom': CachingLevel.Cache,
            'cp2k_md_force_atom_string': CachingLevel.Cache,
            'cp2k_md_force_atom_float': CachingLevel.Cache,
        }

    # The trigger functions
    def onClose_cp2k_system_description(self, backend, gIndex, section):
        """When the cell definition finishes, gather the results to a 3x3
        matrix. Or if not present, ask the cp2kparser for help.
        """
        # Open the common system description section
        backend.openSection("section_system_description")

        # Get the cell information
        cell = section["cp2k_section_cell"]
        if cell:
            cell = cell[0]

            # Get the cell vector strings
            a = cell["cp2k_cell_vector_a"][0]
            b = cell["cp2k_cell_vector_b"][0]
            c = cell["cp2k_cell_vector_c"][0]

            # Extract the components and put into numpy array
            a_comp = a.split()
            b_comp = b.split()
            c_comp = c.split()

            cell = np.zeros((3, 3))
            cell[0, :] = a_comp
            cell[1, :] = b_comp
            cell[2, :] = c_comp

207
            backend.addArrayValues("simulation_cell", cell, unit="angstrom")
Lauri Himanen's avatar
Lauri Himanen committed
208
209
210
211
212
213
214
215

        # Get the number of atoms
        numbers = section["cp2k_section_numbers"]
        if numbers:
            numbers = numbers[0]
            n_atoms = numbers["cp2k_atom_number"]
            if n_atoms:
                n_atoms = n_atoms[0]
216
                backend.addValue("number_of_atoms", n_atoms)
Lauri Himanen's avatar
Lauri Himanen committed
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246

        # Close the common system description section
        backend.closeSection("section_system_description", 0)

    def onClose_cp2k_section_functionals(self, backend, gIndex, section):
        """When all the functional definitions have been gathered, matches them
        with the nomad correspondents and combines into one single string which
        is put into the backend.
        """
        # Get the list of functional names
        functional_names = section["cp2k_functional_name"]

        # Define a mapping for the functionals
        functional_map = {
            "LYP": "GGA_C_LYP",
            "BECKE88": "GGA_X_B88",
            "PADE": "LDA_XC_TETER93",
            "LDA": "LDA_XC_TETER93",
            "BLYP": "HYB_GGA_XC_B3LYP",
        }

        # Match eatch cp2k functional name and sort the matches into a list
        functionals = []
        for name in functional_names:
            match = functional_map.get(name)
            if match:
                functionals.append(match)
        functionals = "_".join(sorted(functionals))

        # Push the functional string into the backend
247
        backend.addValue('XC_functional', functionals)
Lauri Himanen's avatar
Lauri Himanen committed
248
249
250
251

    def onClose_cp2k_section_atom_position(self, backend, gIndex, section):
        """Get the initial atomic positions from cp2kparser.
        """
252
253
254
        pass
        # positions, unit = self.cp2kparser.get_initial_atom_positions_and_unit()
        # backend.addArrayValues("atom_position", positions)
Lauri Himanen's avatar
Lauri Himanen committed
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288

    def onClose_cp2k_section_md_coordinate_atom(self, backend, gIndex, section):
        """Given the string with the coordinate components for one atom, make it
        into a numpy array of coordinate components and store for later
        concatenation.
        """
        force_string = section["cp2k_md_coordinate_atom_string"][0]
        components = np.array([float(x) for x in force_string.split()])
        backend.addArrayValues("cp2k_md_coordinate_atom_float", components)

    def onClose_cp2k_section_md_coordinates(self, backend, gIndex, section):
        """When all the coordinates for individual atoms have been gathered,
        concatenate them into one big array and forward to the backend.
        """
        forces = section["cp2k_md_coordinate_atom_float"]
        forces = np.array(forces)
        backend.addArrayValues("cp2k_md_coordinates", forces)

    def onClose_cp2k_section_md_force_atom(self, backend, gIndex, section):
        """Given the string with the force components for one atom, make it
        into a numpy array of force components and store for later
        concatenation.
        """
        force_string = section["cp2k_md_force_atom_string"][0]
        components = np.array([float(x) for x in force_string.split()])
        backend.addArrayValues("cp2k_md_force_atom_float", components)

    def onClose_cp2k_section_md_forces(self, backend, gIndex, section):
        """When all the forces for individual atoms have been gathered,
        concatenate them into one big array and forward to the backend.
        """
        forces = section["cp2k_md_force_atom_float"]
        forces = np.array(forces)
        backend.addArrayValues("cp2k_md_forces", forces, unit="force_au")