repository.py 3.66 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Copyright 2018 Fawzi Mohamed, Danio Brambila, Markus Scheidgen
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an"AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15
16
import re

17
18
from nomad.parsing import BadContextURI

19
20
from .normalizer import Normalizer

Markus Scheidgen's avatar
Markus Scheidgen committed
21
22
unavailable_label = 'unavailable'

23
24
25
26
27
28

class RepositoryNormalizer(Normalizer):
    """
    The normalizer that turnes normalized parse results into a set of metadata
    quantities for the repository.
    """
29
30
31
32
33
34
35
    xc_treatments = {
        'gga': 'GGA',
        'hf_': 'HF',
        'oep': 'OEP',
        'hyb': 'hybrid',
        'mgga': 'meta-GGA',
        'vdw': 'vdW',
Markus Scheidgen's avatar
Markus Scheidgen committed
36
        'lda': 'LDA',
37
38
39
40
41
42
    }
    """ https://gitlab.mpcdf.mpg.de/nomad-lab/nomad-meta-info/wikis/metainfo/XC-functional """

    version_re = re.compile(r'(\d+(\.\d+(\.\d+)?)?)')

    def map_functional_name_to_xc_treatment(self, name):
Markus Scheidgen's avatar
Markus Scheidgen committed
43
44
45
        if name == unavailable_label:
            return name

46
47
48
49
50
51
52
53
54
        return RepositoryNormalizer.xc_treatments.get(name[:3].lower(), name)

    def simplify_version(self, version):
        match = RepositoryNormalizer.version_re.search(version)
        if match is None:
            return version
        else:
            return match.group(0)

Markus Scheidgen's avatar
Markus Scheidgen committed
55
56
57
58
59
60
    def get_optional_value(self, key):
        try:
            return self._backend.get_value(key, 0)
        except KeyError:
            return unavailable_label

61
62
63
64
    def normalize(self, logger=None) -> None:
        super().normalize(logger)
        b = self._backend

65
66
67
68
69
70
71
        repository_info_context = '/section_repository_info/0'
        try:
            b.openContext(repository_info_context)
        except BadContextURI:
            b.openNonOverlappingSection('section_repository_info')
            repository_info_context = None

72
73
        b.openNonOverlappingSection('section_repository_parserdata')

74
        b.addValue('repository_checksum', b.get_value('calc_hash', 0))
75
        b.addValue('repository_program_name', b.get_value('program_name', 0))
76
77
78
        b.addValue(
            'repository_code_version',
            self.simplify_version(b.get_value('program_version', 0)))
Markus Scheidgen's avatar
Markus Scheidgen committed
79
80
81
82
83
84
        b.addValue('repository_parser_id', b.get_value('parser_name', 0))

        b.addValue('repository_chemical_formula', b.get_value('chemical_composition_bulk_reduced', 0))
        atom_labels = b.get_value('atom_labels', 0)
        b.addValue('repository_atomic_elements', list(set(atom_labels)))
        b.addValue('repository_atomic_elements_count', len(atom_labels))
85
        b.addValue('repository_system_type', b.get_value('system_type', 0))
Markus Scheidgen's avatar
Markus Scheidgen committed
86
87
88
89
90

        b.addValue('repository_crystal_system', self.get_optional_value('crystal_system'))
        b.addValue('repository_spacegroup_nr', self.get_optional_value('space_group_number'))

        b.addValue('repository_basis_set_type', self.get_optional_value('program_basis_set_type'))
91
92
        b.addValue(
            'repository_xc_treatment',
Markus Scheidgen's avatar
Markus Scheidgen committed
93
            self.map_functional_name_to_xc_treatment(self.get_optional_value(('XC_functional_name'))))
94
95

        b.closeNonOverlappingSection('section_repository_parserdata')
96
97
98
99
        if repository_info_context is None:
            b.closeNonOverlappingSection('section_repository_info')
        else:
            b.closeContext(repository_info_context)
100
        b.finishedParsingSession("ParseSuccess", None)