parser.py 4.9 KB
Newer Older
Ask Hjorth Larsen's avatar
Ask Hjorth Larsen committed
1
from __future__ import print_function
Ask Hjorth Larsen's avatar
Ask Hjorth Larsen committed
2
import os
Ask Hjorth Larsen's avatar
Ask Hjorth Larsen committed
3
4
5
import sys
import setup_paths

6
7
8
import numpy as np
from ase.data import chemical_symbols

Ask Hjorth Larsen's avatar
Ask Hjorth Larsen committed
9
10
11
from nomadcore.simple_parser import mainFunction, SimpleMatcher as SM
from nomadcore.local_meta_info import loadJsonFile, InfoKindEl
from nomadcore.unit_conversion.unit_conversion \
12
    import register_userdefined_quantity, convert_unit
Ask Hjorth Larsen's avatar
Ask Hjorth Larsen committed
13

Ask Hjorth Larsen's avatar
Ask Hjorth Larsen committed
14
from util import floating
Ask Hjorth Larsen's avatar
Ask Hjorth Larsen committed
15
16

arg = sys.argv[1]
Ask Hjorth Larsen's avatar
Ask Hjorth Larsen committed
17
18
19
20
21
22
23
24
25
26
27
metaInfoPath = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)),"../../../../nomad-meta-info/meta_info/nomad_meta_info/siesta.nomadmetainfo.json"))
metaInfoEnv, warnings = loadJsonFile(filePath=metaInfoPath,
                                     dependencyLoader=None,
                                     extraArgsHandling=InfoKindEl.ADD_EXTRA_ARGS,
                                     uri=None)

parser_info = {'name':'siesta-parser', 'version': '1.0'}

def siesta_energy(title, meta):
    return SM(r'siesta:\s*%s\s*=\s*(?P<%s__eV>\S*)' % (title, meta),
              name=meta)
Ask Hjorth Larsen's avatar
Ask Hjorth Larsen committed
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
def array_matcher():

    def getarray():
        pass

    sm = SM()
    return sm

#class ArrayParser(SM):
#    def __init__(self, *args, **kwargs):
#        SM.__init__(self, *args, adHoc=get_ar**kwargs)

#def array_line_matcher(*args, **kwargs):
#    def 
#    sm = SimpleMatcher(*args, repeats=True, **kwargs)
#    return sm

def get_positions_and_labels(parser):
    rows = []
    atomic_numbers = []
    line = parser.fIn.readline()
    while line.startswith('siesta:'):
        tokens = line.split()
        rows.append([float(x) for x in tokens[1:4]])
        atomic_numbers.append(int(tokens[4]))
        line = parser.fIn.readline()
    rows = np.array(rows, float)
    labels = np.array([chemical_symbols[Z] for Z in atomic_numbers])

    b = parser.backend.superBackend
    b.addArrayValues('atom_positions', convert_unit(rows, 'bohr'))
    b.addArrayValues('atom_labels', labels)

#def get_cell(parser):
#    


def ArraySM(header, row, end, build):
    lines = []

    def addrow(parser):
        line = parser.fIn.readline()
        lines.append(line)

    def _build_array(parser):
        build(parser.backend.superBackend, lines)

    sm = SM(header,
            name='startarray',
            required=True,
            subFlags=SM.SubFlags.Sequenced,
            subMatchers=[
                SM(row, name='array', repeats=True,
                   forwardMatch=True,
                   adHoc=addrow, required=True),
                SM(end, name='endarray', required=True),
                SM(r'', adHoc=_build_array, name='dummy', forwardMatch=True)
            ])
    return sm

def build_cell(backend, lines):
    cell = np.array([[float(x) for x in line.split()] for line in lines])
    backend.addArrayValues('simulation_cell', convert_unit(cell, 'angstrom'))

def add_positions_and_labels(backend, lines):
    matrix = np.array([line.split() for line in lines], object)
    positions = matrix[:, 1:4].astype(float)
    labels = np.array([chemical_symbols[i] for i in matrix[:, 5].astype(int)])
    backend.addArrayValues('atom_positions', convert_unit(positions, 'bohr'))
    backend.addArrayValues('atom_labels', labels)

Ask Hjorth Larsen's avatar
Ask Hjorth Larsen committed
100
101
102
103
104
105
infoFileDescription = SM(
    name='root',
    weak=True,
    startReStr='',
    fixedStartValues={'program_name': 'siesta'},
    sections=['section_run'],
Ask Hjorth Larsen's avatar
Ask Hjorth Larsen committed
106
    subFlags=SM.SubFlags.Sequenced,  # sequenced or not?
Ask Hjorth Larsen's avatar
Ask Hjorth Larsen committed
107
    subMatchers=[
Ask Hjorth Larsen's avatar
Ask Hjorth Larsen committed
108
109
        SM(r'Siesta Version: (?P<program_name>siesta)-(?P<program_version>\S*)',
           name='name&version'),
110
111
        ArraySM(r'siesta: Atomic coordinates \(Bohr\) and species',
                r'siesta:', r'', add_positions_and_labels),
Ask Hjorth Larsen's avatar
Ask Hjorth Larsen committed
112
113
114
115
        SM(r'\s*Single-point calculation',
           name='singleconfig',
           sections=['section_single_configuration_calculation'],
           subMatchers=[
116
117
118
               ArraySM(r'outcell: Unit cell vectors \(Ang\):',
                       r'\s*\S+\s*\S+\s*\S+',
                       r'\s*', build_cell),
Ask Hjorth Larsen's avatar
Ask Hjorth Larsen committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
               SM(r'siesta: Final energy \(eV\):',
                  name='energy_header',
                  subMatchers=[
                      siesta_energy('Band Struct\.', 'energy_sum_eigenvalues'),
                      siesta_energy('Kinetic', 'electronic_kinetic_energy'),
                      siesta_energy('Hartree', 'energy_electrostatic'),
                      #siesta_energy('Ext\. field', ''),
                      siesta_energy('Exch\.-corr\.', 'energy_XC'),
                      #siesta_energy('Ion-electron', ''),
                      #siesta_energy('Ion-Ion', ''),
                      #siesta_energy('Ekinion', ''),
                      siesta_energy('Total', 'energy_total')
                      ])
           ])
Ask Hjorth Larsen's avatar
Ask Hjorth Larsen committed
133
134
    ])

Ask Hjorth Larsen's avatar
Ask Hjorth Larsen committed
135
136
137
class SiestaContext(object):
    def startedParsing(self, *args, **kwargs):
        pass
Ask Hjorth Larsen's avatar
Ask Hjorth Larsen committed
138

Ask Hjorth Larsen's avatar
Ask Hjorth Larsen committed
139
140
141
142
143
mainFunction(mainFileDescription=infoFileDescription,
             metaInfoEnv=metaInfoEnv,
             parserInfo=parser_info,
             cachingLevelForMetaName={},
             superContext=SiestaContext())