outputparser.py 13.5 KB
Newer Older
1
import re
Lauri Himanen's avatar
Lauri Himanen committed
2
3
from nomadcore.simple_parser import SimpleMatcher as SM
from nomadcore.caching_backend import CachingLevel
4
from cp2kparser.utils.baseclasses import FileParser
Lauri Himanen's avatar
Lauri Himanen committed
5
6
7
8
import numpy as np


#===============================================================================
9
class CP2KOutputParser(FileParser):
Lauri Himanen's avatar
Lauri Himanen committed
10
11
12
    """The object that goes through the CP2K output file and parses everything
    it can using the SimpleParser architecture.
    """
13
    def __init__(self, files, parser_context):
14
        """Initialize an output parser.
Lauri Himanen's avatar
Lauri Himanen committed
15
        """
16
        FileParser.__init__(self, files, parser_context)
Lauri Himanen's avatar
Lauri Himanen committed
17
18
        self.f_regex = "-?\d+\.\d+(?:E(?:\+|-)\d+)?"  # Regex for a floating point value
        self.i_regex = "-?\d+"  # Regex for an integer
Lauri Himanen's avatar
Lauri Himanen committed
19
20

        # Define the output parsing tree for this version
21
        self.root_matcher = SM(
Lauri Himanen's avatar
Lauri Himanen committed
22
            startReStr="",
Lauri Himanen's avatar
Lauri Himanen committed
23
            sections=['section_run'],
Lauri Himanen's avatar
Lauri Himanen committed
24
25
            subMatchers=[
                SM(
26
27
28
29
30
31
                    sections=['cp2k_section_dbcsr'],
                    startReStr=r" DBCSR\| Multiplication driver",
                ),
                SM(
                    sections=['cp2k_section_startinformation'],
                    startReStr=r" \*\*\*\* \*\*\*\* \*\*\*\*\*\*  \*\*  PROGRAM STARTED AT",
Lauri Himanen's avatar
Lauri Himanen committed
32
                    forwardMatch=True,
Lauri Himanen's avatar
Lauri Himanen committed
33
34
                    subMatchers=[
                        SM(
35
                            startReStr=r" \*\*\*\* \*\*\*\* \*\*\*\*\*\*  \*\*  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})",
Lauri Himanen's avatar
Lauri Himanen committed
36
                        ),
37
38
39
40
41
42
43
                    ]
                ),
                SM(
                    sections=['cp2k_section_programinformation'],
                    startReStr=r" CP2K\|",
                    forwardMatch=True,
                    subMatchers=[
Lauri Himanen's avatar
Lauri Himanen committed
44
                        SM(
45
                            startReStr=r" CP2K\| version string:\s+(?P<program_version>[\w\d\W\s]+)",
Lauri Himanen's avatar
Lauri Himanen committed
46
47
                        ),
                        SM(
48
                            startReStr=r" CP2K\| source code revision number:\s+svn:(?P<cp2k_svn_revision>\d+)",
Lauri Himanen's avatar
Lauri Himanen committed
49
50
51
                        ),
                    ]
                ),
52
53
54
55
56
                SM(
                    # sections=["cp2k_section_cell"],
                    startReStr=" CELL\|",
                    adHoc=self.adHoc_cp2k_section_cell()
                ),
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
                SM(
                    sections=["cp2k_section_functional"],
                    startReStr=" FUNCTIONAL\|",
                    forwardMatch=True,
                    otherMetaInfo=["XC_functional"],
                    subMatchers=[
                        SM(
                            repeats=True,
                            startReStr=" FUNCTIONAL\| (?P<cp2k_functional_name>[\w\d\W]+):"
                        )
                    ]
                ),
                SM(
                    sections=["cp2k_section_total_numbers"],
                    startReStr=" TOTAL NUMBERS AND MAXIMUM NUMBERS",
                    subMatchers=[
                        SM(
                            startReStr="\s+- Atoms:\s+(?P<number_of_atoms>\d+)",
                            sections=["section_system_description"]
Lauri Himanen's avatar
Lauri Himanen committed
76
77
                        ),
                        SM(
Lauri Himanen's avatar
Lauri Himanen committed
78
79
80
81
                            startReStr="\s+- Shell sets:\s+(?P<cp2k_shell_sets>\d+)"
                        )
                    ]
                ),
82
83
84
85
                SM(
                    startReStr=" MODULE QUICKSTEP:  ATOMIC COORDINATES IN angstrom",
                    adHoc=self.adHoc_cp2k_section_quickstep_atom_information(),
                ),
Lauri Himanen's avatar
Lauri Himanen committed
86
87
88
89
90
91
92
93
94
95
96
97
                # SCF
                SM(
                    sections=["section_single_configuration_calculation"],
                    startReStr=" SCF WAVEFUNCTION OPTIMIZATION",
                    subMatchers=[
                        SM(
                            sections=["section_scf_iteration"],
                            startReStr=r"\s+\d+\s+\S+\s+{f}\s+{f}\s+{f}\s+(?P<energy_total_scf_iteration>{f})\s+{f}".format(f=self.f_regex),
                            repeats=True,
                        ),
                    ]
                ),
Lauri Himanen's avatar
Lauri Himanen committed
98
99
100
101
102
103
104
105
106
                SM(
                    sections=["cp2k_section_md"],
                    startReStr=" MD| Molecular Dynamics Protocol",
                    forwardMatch=True,
                    subMatchers=[
                        SM(
                            repeats=True,
                            startReStr=" ENERGY\| Total FORCE_EVAL",
                            sections=["cp2k_section_md_step"],
Lauri Himanen's avatar
Lauri Himanen committed
107
108
                            subMatchers=[
                                SM(
Lauri Himanen's avatar
Lauri Himanen committed
109
110
                                    startReStr=" ATOMIC FORCES in \[a\.u\.\]",
                                    sections=["cp2k_section_md_forces"],
Lauri Himanen's avatar
Lauri Himanen committed
111
112
                                    subMatchers=[
                                        SM(
Lauri Himanen's avatar
Lauri Himanen committed
113
114
                                            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"],
Lauri Himanen's avatar
Lauri Himanen committed
115
116
117
118
119
                                            repeats=True,
                                        )
                                    ]
                                ),
                                SM(
Lauri Himanen's avatar
Lauri Himanen committed
120
121
122
123
124
125
126
127
                                    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)
                                ),
Lauri Himanen's avatar
Lauri Himanen committed
128
                                SM(
Lauri Himanen's avatar
Lauri Himanen committed
129
130
131
132
                                    startReStr=" i =",
                                    sections=["cp2k_section_md_coordinates"],
                                    otherMetaInfo=["cp2k_md_coordinates"],
                                    dependencies={"cp2k_md_coordinates": ["cp2k_md_coordinate_atom_string"]},
Lauri Himanen's avatar
Lauri Himanen committed
133
134
                                    subMatchers=[
                                        SM(
Lauri Himanen's avatar
Lauri Himanen committed
135
136
137
138
                                            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,
Lauri Himanen's avatar
Lauri Himanen committed
139
140
141
142
143
144
145
146
147
                                        )
                                    ]
                                )
                            ]
                        )
                    ]
                )
            ]
        )
Lauri Himanen's avatar
Lauri Himanen committed
148
        #=======================================================================
Lauri Himanen's avatar
Lauri Himanen committed
149
        # The cache settings
150
        self.caching_level_for_metaname = {
Lauri Himanen's avatar
Lauri Himanen committed
151
152
153
154
155
156
157
158
159
160
161
162
            'cp2k_functional_name': 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,
        }

Lauri Himanen's avatar
Lauri Himanen committed
163
164
165
    #===========================================================================
    # The functions that trigger when sections are closed
    def onClose_cp2k_section_functional(self, backend, gIndex, section):
Lauri Himanen's avatar
Lauri Himanen committed
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
        """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
Lauri Himanen's avatar
Lauri Himanen committed
191
        gIndex = backend.openSection("section_method")
192
        backend.addValue('XC_functional', functionals)
Lauri Himanen's avatar
Lauri Himanen committed
193
        backend.closeSection("section_method", gIndex)
Lauri Himanen's avatar
Lauri Himanen committed
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227

    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")
Lauri Himanen's avatar
Lauri Himanen committed
228
229
230

    #===========================================================================
    # adHoc functions that are used to do custom parsing.
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
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
289
290
291
292
293
294
295
296
297
298
299
300
301
    def adHoc_cp2k_section_cell(self):
        """Used to extract the cell information.
        """
        def wrapper(parser):
            # Read the lines containing the cell vectors
            a_line = parser.fIn.readline()
            b_line = parser.fIn.readline()
            c_line = parser.fIn.readline()

            # Define the regex that extracts the components and apply it to the lines
            regex_string = r" CELL\| Vector \w \[angstrom\]:\s+({0})\s+({0})\s+({0})".format(self.f_regex)
            regex_compiled = re.compile(regex_string)
            a_result = regex_compiled.match(a_line)
            b_result = regex_compiled.match(b_line)
            c_result = regex_compiled.match(c_line)

            # Convert the string results into a 3x3 numpy array
            cell = np.zeros((3, 3))
            cell[0, :] = [float(x) for x in a_result.groups()]
            cell[1, :] = [float(x) for x in b_result.groups()]
            cell[2, :] = [float(x) for x in c_result.groups()]

            # Push the results to the correct section
            gIndex = parser.backend.openSection("section_system_description")
            parser.backend.addArrayValues("simulation_cell", cell, unit="angstrom")
            parser.backend.closeSection("section_system_description", gIndex)

        return wrapper

    def adHoc_cp2k_section_quickstep_atom_information(self):
        """Used to extract the initial atomic coordinates and names in the
        Quickstep module.
        """
        def wrapper(parser):

            # Define the regex that extracts the information
            regex_string = r"\s+\d+\s+\d+\s+(\w+)\s+\d+\s+({0})\s+({0})\s+({0})".format(self.f_regex)
            regex_compiled = re.compile(regex_string)

            match = True
            coordinates = []
            labels = []

            # Currently these three lines are not processed
            parser.fIn.readline()
            parser.fIn.readline()
            parser.fIn.readline()

            while match:
                line = parser.fIn.readline()
                result = regex_compiled.match(line)

                if result:
                    match = True
                    label = result.groups()[0]
                    labels.append(label)
                    coordinate = [float(x) for x in result.groups()[1:]]
                    coordinates.append(coordinate)
                else:
                    match = False
            coordinates = np.array(coordinates)
            labels = np.array(labels)

            # If anything found, push the results to the correct section
            if len(coordinates) != 0:
                gIndex = parser.backend.openSection("section_system_description")
                parser.backend.addArrayValues("atom_position", coordinates, unit="angstrom")
                parser.backend.addArrayValues("atom_label", labels)
                parser.backend.closeSection("section_system_description", gIndex)

        return wrapper