parser_gaussian.py 4.91 KB
Newer Older
1
import setup_paths
2
from nomadcore.simple_parser import mainFunction, SimpleMatcher as SM
3
from nomadcore.local_meta_info import loadJsonFile, InfoKindEl
4
5
6
from nomadcore.caching_backend import CachingLevel
import os, sys, json, logging
import numpy as np
7
import ase
8

Rosendo Valero Montero's avatar
Rosendo Valero Montero committed
9
# description of the output
10
11
12
13
14
15
mainFileDescription = SM(
    name = 'root',
    weak = True,
    startReStr = "",
    subMatchers = [
        SM(name = 'newRun',
16
           startReStr = r"\s*Entering Link 1 ",
17
18
19
           repeats = True,
           required = True,
           forwardMatch = True,
20
           fixedStartValues={ 'program_basis_set_type': 'gaussians' },
21
           sections   = ['section_run','section_method'],
22
23
           subMatchers = [
               SM(name = 'header',
24
25
26
27
28
29
30
31
32
33
34
35
36
                  startReStr = r"\s*Entering Link 1 ",
                  subMatchers = [
                      SM(r"\s*Cite this work as:"),
                      SM(r"\s*Gaussian [0-9]+, Revision [A-Za-z0-9.]*,"),
                      SM(r"\s\*\*\*\*\*\*\*\*\*\*\*\**"),
                      SM(r"\s*(?P<program_name>Gaussian)\s*(?P<program_version>[0-9]*:\s.*)")
                      ]
              ),
               SM(name = 'globalparams',
                  startReStr = r"\s*%\w*=",
                  subFlags = SM.SubFlags.Unordered,
                  forwardMatch = True,
                  subMatchers = [
37
38
39
                      SM(r"\s*%[Cc]hk=(?P<x_gaussian_chk_file>[A-Za-z0-9.]*)"),
                      SM(r"\s*%[Mm]em=(?P<x_gaussian_memory>[A-Za-z0-9.]*)"),
                      SM(r"\s*%[Nn][Pp]roc=(?P<x_gaussian_number_of_processors>[A-Za-z0-9.]*)")
40
41
                      ]
              ),
42
               SM(name = 'charge_multiplicity',
43
	          sections  = ['section_system_description','x_gaussian_section_chargemult'],
Rosendo Valero Montero's avatar
Rosendo Valero Montero committed
44
45
46
47
		  startReStr = r"\s*Charge =",
                  subFlags = SM.SubFlags.Sequenced,
                  forwardMatch = True,
                  subMatchers = [
48
49
                      SM(r"\s*Charge =\s*(?P<total_charge>[-+0-9]+) Multiplicity =\s*(?P<target_multiplicity>[0-9]+)"),
                      ]
50
51
              ),
               SM(name = 'geometry',
52
53
54
                  sections  = ['section_system_description','x_gaussian_section_geometry'],
                  startReStr = r"\s*Input orientation:|\s*Z-Matrix orientation:|\s*Standard orientation:",
                  subFlags = SM.SubFlags.Sequenced,
55
                      subMatchers = [
56
57
                      SM(r"\s+[0-9]+\s+(?P<x_gaussian_atomic_number>[0-9]+)\s+[0-9]+\s+(?P<x_gaussian_atom_x_coord__angstrom>[-+0-9EeDd.]+)\s+(?P<x_gaussian_atom_y_coord__angstrom>[-+0-9EeDd.]+)\s+(?P<x_gaussian_atom_z_coord__angstrom>[-+0-9EeDd.]+)",repeats = True),
                      SM(r"\s*Distance matrix|\s*Rotational constants|\s*Stoichiometry")
58
59
                      ]
              ), 
60
61
           ])
])
62
63
64
65
66
67
68
69
70
71

# loading metadata from nomad-meta-info/meta_info/nomad_meta_info/gaussian.nomadmetainfo.json
metaInfoPath = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)),"../../../../nomad-meta-info/meta_info/nomad_meta_info/gaussian.nomadmetainfo.json"))
metaInfoEnv, warnings = loadJsonFile(filePath = metaInfoPath, dependencyLoader = None, extraArgsHandling = InfoKindEl.ADD_EXTRA_ARGS, uri = None)

parserInfo = {
  "name": "parser_gaussian",
  "version": "1.0"
}

72
73
74
class GaussianParserContext(object):
    """main place to keep the parser status, open ancillary files,..."""
    def __init__(self):
75
        pass
76

77
78
    def startedParsing(self, path, parser):
        self.parser = parser
79

80
81
82
83
84
    def onClose_x_gaussian_section_geometry(self, backend, gIndex, section):
	xCoord = section["x_gaussian_atom_x_coord"]
	yCoord = section["x_gaussian_atom_y_coord"]
        zCoord = section["x_gaussian_atom_z_coord"]
        numbers = section["x_gaussian_atomic_number"]
85
        atom_positions = np.zeros((len(xCoord),3), dtype=float)
86
87
        atom_numbers = np.zeros(len(xCoord), dtype=int)
        atomic_symbols = np.empty((len(xCoord)), dtype=object) 
88
89
90
91
	for i in range(len(xCoord)):
	    atom_positions[i,0] = xCoord[i]
            atom_positions[i,1] = yCoord[i]
            atom_positions[i,2] = zCoord[i]
92
93
94
        for i in range(len(xCoord)):
            atom_numbers[i] = numbers[i]
            atomic_symbols[i] = ase.data.chemical_symbols[atom_numbers[i]] 
95
	backend.addArrayValues("atom_position", atom_positions)
96
        backend.addArrayValues("atom_label", atomic_symbols)
97
98

# which values to cache or forward (mapping meta name -> CachingLevel)
99
cachingLevelForMetaName = {
100
101
102
103
104
	"x_gaussian_atom_x_coord": CachingLevel.Cache,
        "x_gaussian_atom_y_coord": CachingLevel.Cache,
        "x_gaussian_atom_z_coord": CachingLevel.Cache,
        "x_gaussian_atomic_number": CachingLevel.Cache,
	"x_gaussian_section_geometry": CachingLevel.Ignore,
105
}
106
107

if __name__ == "__main__":
108
109
    mainFunction(mainFileDescription, metaInfoEnv, parserInfo,
                 cachingLevelForMetaName = cachingLevelForMetaName,
Mohamed, Fawzi Roberto (fawzi)'s avatar
cleanup    
Mohamed, Fawzi Roberto (fawzi) committed
110
                 superContext = GaussianParserContext())