test_dos.py 3.34 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 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.

import numpy as np
Lauri Himanen's avatar
Lauri Himanen committed
16
# import matplotlib.pyplot as mpl
17

Lauri Himanen's avatar
Lauri Himanen committed
18
19
20
21
22
23
from nomad.parsing.legacy import Backend
from tests.normalizing.conftest import (  # pylint: disable=unused-import
    dos_si_fhiaims,
    dos_si_exciting,
    dos_si_vasp,
)
24

Lauri Himanen's avatar
Lauri Himanen committed
25
# from nomad_dos_fingerprints import DOSFingerprint, tanimoto_similarity
26

27

Lauri Himanen's avatar
Lauri Himanen committed
28
29
30
31
32
33
34
35
36
37
# def test_fingerprint(dos_unpolarized_vasp):
#    # Check if DOS fingerprint was created
#    backend_dos_fingerprint = dos_unpolarized_vasp['section_dos_fingerprint'][0]
#    dos_fingerprint = DOSFingerprint().from_dict(dict(
#        bins=backend_dos_fingerprint.bins,
#        indices=backend_dos_fingerprint.indices,
#        grid_id=backend_dos_fingerprint.grid_id,
#        stepsize=backend_dos_fingerprint.stepsize,
#        filling_factor=backend_dos_fingerprint.filling_factor))
#    assert tanimoto_similarity(dos_fingerprint, dos_fingerprint) == 1
38
39


Lauri Himanen's avatar
Lauri Himanen committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# def plot_dos_energies(dos_si_vasp: Backend, dos_si_exciting: Backend, dos_si_fhiaims: Backend):
#    """For debugging.
#    """
#    x_exciting = dos_si_exciting.get_value('dos_energies_normalized', 0)
#    y_exciting = dos_si_exciting.get_value('dos_values_normalized', 0)
#    x_vasp = dos_si_vasp.get_value('dos_energies_normalized', 0)
#    y_vasp = dos_si_vasp.get_value('dos_values_normalized', 0)
#    x_fhiaims = dos_si_fhiaims.get_value('dos_energies_normalized', 0)
#    y_fhiaims = dos_si_fhiaims.get_value('dos_values_normalized', 0)
#    mpl.plot(x_vasp, y_vasp[0], label="VAP")
#    mpl.plot(x_exciting, y_exciting[0], label="exciting")
#    mpl.plot(x_fhiaims, y_fhiaims[0], label="FHI-aims")
#    mpl.legend()
#    mpl.show()
54
55


Lauri Himanen's avatar
Lauri Himanen committed
56
57
58
59
60
61
62
63
64
def test_dos_magnitude(dos_si_vasp: Backend, dos_si_exciting: Backend, dos_si_fhiaims: Backend):
    """
    Ensure the DOS normalizer acted on the DOS values. The order of magnitude
    for normalized DOS values in VASP, exciting and FHIAims are currently
    tested.
    """
    dos_vasp = dos_si_vasp.get_value('dos_values_normalized', 0)
    dos_exciting = dos_si_exciting.get_value('dos_values_normalized', 0)
    dos_fhiaims = dos_si_fhiaims.get_value('dos_values_normalized', 0)
65

Lauri Himanen's avatar
Lauri Himanen committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
    dos_vasp_mean = mean_nonzero(dos_vasp)
    dos_exciting_mean = mean_nonzero(dos_exciting)
    dos_fhiaims_mean = mean_nonzero(dos_fhiaims)

    assert is_same_magnitude(dos_vasp_mean, dos_exciting_mean, dos_fhiaims_mean)


def mean_nonzero(dos: np.array):
    """Returns the mean value of all nonzero elements in the given array.
    """
    return dos[np.nonzero(dos)].mean()


def is_same_magnitude(*args):
    """Used to test that all given floating point numbers are of the expected
    order of magnitude.
    """
    correct_magnitude = 1e18
    tolerance = 10
    values = np.array(args)
    values_normalized = values / correct_magnitude
    return ((values_normalized <= tolerance) & (values_normalized >= 1 / tolerance)).all()