File: test_magres.py

package info (click to toggle)
python-ase 3.26.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,484 kB
  • sloc: python: 148,112; xml: 2,728; makefile: 110; javascript: 47
file content (109 lines) | stat: -rw-r--r-- 3,259 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
100
101
102
103
104
105
106
107
108
109
# fmt: off
import numpy as np
import pytest

from ase.build import bulk
from ase.calculators.singlepoint import SinglePointDFTCalculator
from ase.io import read, write

magres_with_complex_labels = """#$magres-abinitio-v1.0
# Fictional test data
[atoms]
units lattice Angstrom
lattice       8.0 0.0 0.0 0.0 10.2 0.0 0.0 0.0 15.9
units atom Angstrom
atom H H1   1    3.70 4.9 1.07
atom H H2   100  4.30 2.1 5.15
atom H H2a  101  4.30 2.1 5.15
atom H H2b  102  4.30 2.1 5.15
[/atoms]
[magres]
units ms ppm
ms H1  1    3.0 -5.08 -3.19 -4.34 3.42 -3.78  1.05 -1.89  2.85
ms H2100    3.0  5.08  3.19  4.34 3.42 -3.78 -1.05 -1.89  2.85
ms H2a101   3.0  5.08  3.19  4.34 3.42 -3.78 -1.05 -1.89  2.85
ms H2b 102  3.0  5.08  3.19  4.34 3.42 -3.78 -1.05 -1.89  2.85
units efg au
efg H1  1   9.6 -3.43  1.45 -3.43 8.52 -1.43  1.45 -1.43 -1.81
efg H2100   9.6  3.43 -1.45  3.43 8.52 -1.43 -1.45 -1.43 -1.81
efg H2a101  9.6  3.43 -1.45  3.43 8.52 -1.43 -1.45 -1.43 -1.81
efg H2b 102 9.6  3.43 -1.45  3.43 8.52 -1.43 -1.45 -1.43 -1.81
[/magres]
"""

magres_with_too_large_index = """#$magres-abinitio-v1.0
# Test data with index >999
[atoms]
units lattice Angstrom
lattice       10.0 0.0 0.0 0.0 10.0 0.0 0.0 0.0 10.0
units atom Angstrom
atom H H1 1000    0.0 0.0 0.0
[/atoms]
[magres]
units ms ppm
ms H11000    1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 1.0
[/magres]
"""


def test_magres():

    # Test with fictional data
    si2 = bulk('Si')

    ms = np.ones((2, 3, 3))
    si2.set_array('ms', ms)
    efg = np.repeat([[[1, 0, 0], [0, 1, 0], [0, 0, -2]]], 2, axis=0)
    si2.set_array('efg', efg)

    calc = SinglePointDFTCalculator(si2)
    calc.results['sus'] = np.eye(3) * 2
    si2.calc = calc

    si2.info['magres_units'] = {'ms': 'ppm',
                                'efg': 'au',
                                'sus': '10^-6.cm^3.mol^-1'}

    write('si2_test.magres', si2)
    si2 = read('si2_test.magres')

    assert (np.trace(si2.get_array('ms')[0]) == 3)
    assert (np.all(np.isclose(si2.get_array('efg')[:, 2, 2], -2)))
    assert (np.all(np.isclose(si2.calc.results['sus'], np.eye(3) * 2)))


def test_magres_large(datadir):

    # Test with big structure
    assert len(read(datadir / "large_atoms.magres")) == 240


def test_magres_sitelabels(datadir):
    """Test reading magres files with munged site labels and indices
    for cases where the site label has a number in it."""

    # Write temporary file
    with open('magres_with_complex_labels.magres', 'w') as f:
        f.write(magres_with_complex_labels)

    # Read it back
    atoms = read('magres_with_complex_labels.magres')

    labels_ref = ['H1', 'H2', 'H2a', 'H2b']
    labels = atoms.get_array('labels')
    np.testing.assert_array_equal(labels, labels_ref)

    indices_ref = [1, 100, 101, 102]
    indices = atoms.get_array('indices')
    np.testing.assert_array_equal(indices, indices_ref)


def test_magres_with_large_indices():
    """Test handling of magres files with indices >999"""
    # Write temporary file
    with open('magres_large_index.magres', 'w') as f:
        f.write(magres_with_too_large_index)

    # Check that reading raises the correct error
    with pytest.raises(RuntimeError, match="Index greater than 999 detected"):
        read('magres_large_index.magres')