dft.py 14.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Copyright 2018 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
DFT specific metadata
17
'''
18
19
20
21

import re

from nomad import utils, config
22
23
from nomad.metainfo import MSection, Section, Quantity, MEnum, SubSection
from nomad.metainfo.search_extension import Search
24

25
26
from .common import get_optional_backend_value
from .optimade import OptimadeEntry
27
28
29
30
31
32
33
34
35
36
37


xc_treatments = {
    'gga': 'GGA',
    'hf_': 'HF',
    'oep': 'OEP',
    'hyb': 'hybrid',
    'mgg': 'meta-GGA',
    'vdw': 'vdW',
    'lda': 'LDA',
}
38
''' https://gitlab.mpcdf.mpg.de/nomad-lab/nomad-meta-info/wikis/metainfo/XC-functional '''
39
40
41
42
43
44
45

basis_sets = {
    'gaussians': 'gaussians',
    'realspacegrid': 'real-space grid',
    'planewaves': 'plane waves'
}

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
compound_types = [
    'unary',
    'binary',
    'ternary',
    'quaternary',
    'quinary',
    'sexinary',
    'septenary',
    'octanary',
    'nonary',
    'decinary'
]

_energy_quantities = [
    'energy_total',
    'energy_total_T0',
    'energy_free',
    'energy_electrostatic',
    'energy_X',
    'energy_XC',
    'energy_sum_eigenvalues']

_electronic_quantities = [
    'dos_values',
    'eigenvalues_values',
    'volumetric_data_values',
    'electronic_kinetic_energy',
    'total_charge',
    'atomic_multipole_values']

_forces_quantities = [
    'atom_forces_free',
    'atom_forces_raw',
    'atom_forces_T0',
    'atom_forces',
    'stress_tensor']

_vibrational_quantities = [
    'thermodynamical_property_heat_capacity_C_v',
    'vibrational_free_energy_at_constant_volume',
    'band_energies']

_magnetic_quantities = [
    'spin_S2'
]

_optical_quantities = [
    'excitation_energies',
    'oscillator_strengths',
    'transition_dipole_moments'
]

98
99
100
101
version_re = re.compile(r'(\d+(\.\d+(\.\d+)?)?)')


def map_functional_name_to_xc_treatment(name):
Markus Scheidgen's avatar
Markus Scheidgen committed
102
    if name == config.services.unavailable_value:
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
        return name

    return xc_treatments.get(name[:3].lower(), name)


def map_basis_set_to_basis_set_label(name):
    key = name.replace('_', '').replace('-', '').replace(' ', '').lower()
    return basis_sets.get(key, name)


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


121
class Label(MSection):
122
    '''
123
124
125
126
127
128
129
    Label that further classify a structure.

    Attributes:
        label: The label as a string
        type: The type of the label
        source: The source that this label was taken from.

130
    '''
131
    label = Quantity(type=str, a_search=Search())
132
133
134

    type = Quantity(type=MEnum(
        'compound_class', 'classification', 'prototype', 'prototype_id'),
135
        a_search=Search())
136
137
138

    source = Quantity(
        type=MEnum('springer', 'aflow_prototype_library'),
139
        a_search=Search())
140
141
142
143
144
145
146
147


class DFTMetadata(MSection):
    m_def = Section(a_domain='dft')

    basis_set = Quantity(
        type=str, default='not processed',
        description='The used basis set functions.',
148
        a_search=Search(statistic_size=20, default_statistic=True))
149
150
151
152

    xc_functional = Quantity(
        type=str, default='not processed',
        description='The libXC based xc functional classification used in the simulation.',
153
        a_search=Search(statistic_size=20, default_statistic=True))
154
155
156
157

    system = Quantity(
        type=str, default='not processed',
        description='The system type of the simulated system.',
158
        a_search=Search(default_statistic=True))
159

160
161
162
163
164
165
    compound_type = Quantity(
        type=str, default='not processed',
        description='The compound type of the simulated system.',
        a_search=Search(statistic_size=11, default_statistic=True)
    )

166
167
168
    crystal_system = Quantity(
        type=str, default='not processed',
        description='The crystal system type of the simulated system.',
169
        a_search=Search(default_statistic=True))
170
171

    spacegroup = Quantity(
172
        type=int, default=-1,
173
        description='The spacegroup of the simulated system as number.',
174
        a_search=Search())
175
176
177
178

    spacegroup_symbol = Quantity(
        type=str, default='not processed',
        description='The spacegroup as international short symbol.',
179
        a_search=Search())
180
181
182
183

    code_name = Quantity(
        type=str, default='not processed',
        description='The name of the used code.',
184
        a_search=Search(statistic_size=40, default_statistic=True))
185
186
187
188

    code_version = Quantity(
        type=str, default='not processed',
        description='The version of the used code.',
189
        a_search=Search())
190
191
192

    n_geometries = Quantity(
        type=int, description='Number of unique geometries.',
193
        a_search=Search(metric_name='geometries', metric='sum'))
194
195
196
197

    n_calculations = Quantity(
        type=int,
        description='Number of single configuration calculation sections',
198
        a_search=Search(metric_name='calculations', metric='sum'))
199
200
201

    n_total_energies = Quantity(
        type=int, description='Number of total energy calculations',
202
        a_search=Search(metric_name='total_energies', metric='sum'))
203
204
205

    n_quantities = Quantity(
        type=int, description='Number of metainfo quantities parsed from the entry.',
206
        a_search=Search(metric='sum', metric_name='quantities'))
207
208
209
210

    quantities = Quantity(
        type=str, shape=['0..*'],
        description='All quantities that are used by this entry.',
211
        a_search=Search(
212
213
            metric_name='distinct_quantities', metric='cardinality', many_and='append'))

214
215
216
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
    quantities_energy = Quantity(
        type=str, shape=['0..*'],
        description='Energy-related quantities.',
        a_search=Search(many_and='append', default_statistic=True))

    quantities_electronic = Quantity(
        type=str, shape=['0..*'],
        description='Electronic structure-related quantities.',
        a_search=Search(many_and='append', default_statistic=True))

    quantities_forces = Quantity(
        type=str, shape=['0..*'],
        description='Forces-related quantities.',
        a_search=Search(many_and='append', default_statistic=True))

    quantities_vibrational = Quantity(
        type=str, shape=['0..*'],
        description='Vibrational-related quantities.',
        a_search=Search(many_and='append', default_statistic=True))

    quantities_magnetic = Quantity(
        type=str, shape=['0..*'],
        description='Magnetic-related quantities.',
        a_search=Search(many_and='append', default_statistic=True))

    quantities_optical = Quantity(
        type=str, shape=['0..*'],
        description='Optical-related quantities.',
        a_search=Search(many_and='append', default_statistic=True))

244
245
246
    geometries = Quantity(
        type=str, shape=['0..*'],
        description='Hashes for each simulated geometry',
247
        a_search=Search(metric_name='unique_geometries', metric='cardinality'))
248
249
250
251

    group_hash = Quantity(
        type=str,
        description='Hashes that describe unique geometries simulated by this code run.',
252
        a_search=Search(many_or='append', group='groups_grouped', metric_name='groups', metric='cardinality'))
253
254
255
256
257
258

    labels = SubSection(
        sub_section=Label, repeats=True,
        description='The labels taken from AFLOW prototypes and springer.',
        a_search='labels')

259
260
261
262
263
264
265
266
267
268
    labels_springer_compound_class = Quantity(
        type=str, shape=['0..*'],
        description='Springer compund classification.',
        a_search=Search(many_and='append', default_statistic=True, statistic_size=15))

    labels_springer_classification = Quantity(
        type=str, shape=['0..*'],
        description='Springer classification by property.',
        a_search=Search(many_and='append', default_statistic=True, statistic_size=15))

269
    optimade = SubSection(
270
        sub_section=OptimadeEntry,
271
272
273
        description='Metadata used for the optimade API.',
        a_search='optimade')

274
    def apply_domain_metadata(self, backend):
275
        from nomad.normalizing.system import normalized_atom_labels
276
        entry = self.m_parent
277

278
        logger = utils.get_logger(__name__).bind(
279
            upload_id=entry.upload_id, calc_id=entry.calc_id, mainfile=entry.mainfile)
280

281
282
283
284
285
286
287
288
        if backend is None:
            if entry.parser_name is not None:
                from nomad.parsing import parser_dict
                parser = parser_dict.get(entry.parser_name)
                if hasattr(parser, 'code_name'):
                    self.code_name = parser.code_name
            return

289
        # code and code specific ids
290
        self.code_name = backend.get_value('program_name', 0)
291
292
293
294
        try:
            self.code_version = simplify_version(backend.get_value('program_version', 0))
        except KeyError:
            self.code_version = config.services.unavailable_value
295

296
297
298
        raw_id = get_optional_backend_value(backend, 'raw_id', 'section_run', None)
        if raw_id is not None:
            entry.raw_id = raw_id
Markus Scheidgen's avatar
Markus Scheidgen committed
299

300
        # metadata (system, method, chemistry)
301
302
303
304
305
306
307
        atoms = get_optional_backend_value(backend, 'atom_labels', 'section_system', [], logger=logger)
        if hasattr(atoms, 'tolist'):
            atoms = atoms.tolist()
        entry.n_atoms = len(atoms)
        atoms = list(set(normalized_atom_labels(set(atoms))))
        atoms.sort()
        entry.atoms = atoms
308
        self.compound_type = compound_types[len(atoms) - 1] if len(atoms) <= 10 else '>decinary'
309

310
311
312
313
314
        self.crystal_system = get_optional_backend_value(
            backend, 'crystal_system', 'section_symmetry', logger=logger)
        self.spacegroup = get_optional_backend_value(
            backend, 'space_group_number', 'section_symmetry', 0, logger=logger)
        self.spacegroup_symbol = get_optional_backend_value(
315
            backend, 'international_short_symbol', 'section_symmetry', logger=logger)
316
        self.basis_set = map_basis_set_to_basis_set_label(
317
318
319
            get_optional_backend_value(backend, 'program_basis_set_type', 'section_run', logger=logger))
        self.system = get_optional_backend_value(
            backend, 'system_type', 'section_system', logger=logger)
320
        entry.formula = get_optional_backend_value(
321
            backend, 'chemical_composition_bulk_reduced', 'section_system', logger=logger)
322
        self.xc_functional = map_functional_name_to_xc_treatment(
323
            get_optional_backend_value(backend, 'XC_functional_name', 'section_method', logger=logger))
324

325
        # grouping
326
        self.group_hash = utils.hash(
327
            entry.formula,
328
329
330
331
332
            self.spacegroup,
            self.basis_set,
            self.xc_functional,
            self.code_name,
            self.code_version,
333
334
            entry.with_embargo,
            entry.uploader)
335

336
        # metrics and quantities
337
338
        quantities = set()
        geometries = set()
339
340
341
342
343
344
345
        quantities_energy = set()
        quantities_electronic = set()
        quantities_forces = set()
        quantities_vibrational = set()
        quantities_magnetic = set()
        quantities_optical = set()

346
347
        n_quantities = 0
        n_calculations = 0
348
349
350
        n_total_energies = 0
        n_geometries = 0

351
352
        for section_run in backend.entry_archive.section_run:
            quantities.add(section_run.m_def.name)
353
            n_quantities += 1
354

355
            for section, property_def, _ in section_run.m_traverse():
356
357
                property_name = property_def.name
                quantities.add(property_name)
358
359
                n_quantities += 1

360
361
362
363
364
365
366
367
368
369
370
371
372
                if property_name in _energy_quantities:
                    quantities_energy.add(property_name)
                elif property_name in _electronic_quantities:
                    quantities_electronic.add(property_name)
                elif property_name in _forces_quantities:
                    quantities_forces.add(property_name)
                elif property_name in _vibrational_quantities:
                    quantities_vibrational.add(property_name)
                elif property_name in _magnetic_quantities:
                    quantities_magnetic.add(property_name)
                elif property_name in _optical_quantities:
                    quantities_optical.add(property_name)

373
                if property_name == 'energy_total':
374
375
                    n_total_energies += 1

376
377
                if property_name == 'configuration_raw_gid':
                    geometries.add(section.m_get(property_def))
378

379
                if property_name == 'section_single_configuration_calculation':
380
                    n_calculations += 1
381

382
                if property_name == 'section_system':
383
                    n_geometries += 1
384
385
386

        self.quantities = list(quantities)
        self.geometries = list(geometries)
387
388
389
390
391
392
        self.quantities_energy = list(quantities_energy)
        self.quantities_electronic = list(quantities_electronic)
        self.quantities_forces = list(quantities_forces)
        self.quantities_vibrational = list(quantities_vibrational)
        self.quantities_magnetic = list(quantities_magnetic)
        self.quantities_optical = list(quantities_optical)
393
394
        self.n_quantities = n_quantities
        self.n_calculations = n_calculations
395
396
397
        self.n_total_energies = n_total_energies
        self.n_geometries = n_geometries

398
399
400
401
402
403
404
405
406
407
408
        # labels
        compounds = set()
        classifications = set()
        for index in backend.get_sections('section_springer_material'):
            compounds.update(backend.get_value('springer_compound_class', index))
            classifications.update(backend.get_value('springer_classification', index))

        for compound in compounds:
            self.labels.append(Label(label=compound, type='compound_class', source='springer'))
        for classification in classifications:
            self.labels.append(Label(label=classification, type='classification', source='springer'))
409
410
        self.labels_springer_compound_class = list(compounds)
        self.labels_springer_classification = list(classifications)
411
412
413
414
415
416
417

        aflow_id = get_optional_backend_value(backend, 'prototype_aflow_id', 'section_prototype')
        aflow_label = get_optional_backend_value(backend, 'prototype_label', 'section_prototype')

        if aflow_id is not None and aflow_label is not None:
            self.labels.append(Label(label=aflow_label, type='prototype', source='aflow_prototype_library'))
            self.labels.append(Label(label=aflow_id, type='prototype_id', source='aflow_prototype_library'))