File: test_bundletrajectory.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 (116 lines) | stat: -rw-r--r-- 2,808 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
110
111
112
113
114
115
116
# fmt: off
import os

import pytest

from ase import Atoms
from ase.io.bundletrajectory import (
    BundleTrajectory,
    read_bundletrajectory,
    write_bundletrajectory,
)


@pytest.fixture
def atoms():
    return Atoms("H2", positions=[[0, 0, 0], [0, 0, 0.74]])


@pytest.fixture
def bundle_file(tmp_path):
    return os.path.join(tmp_path, "test.bundle")


def test_write_read(atoms, bundle_file):
    # Write atoms to BundleTrajectory
    traj = BundleTrajectory(bundle_file, "w", atoms=atoms)
    traj.write(atoms)
    traj.close()

    # Read atoms from BundleTrajectory
    traj = BundleTrajectory(bundle_file, "r")
    read_atoms = traj[0]
    traj.close()

    assert atoms == read_atoms


def test_append(atoms, bundle_file):
    # Write atoms to BundleTrajectory
    traj = BundleTrajectory(bundle_file, "w", atoms=atoms)
    traj.write(atoms)
    traj.close()

    # Append atoms to BundleTrajectory
    traj = BundleTrajectory(bundle_file, "a", atoms=atoms)
    traj.write(atoms)
    traj.close()

    # Read atoms from BundleTrajectory
    traj = BundleTrajectory(bundle_file, "r")
    assert len(traj) == 2
    read_atoms1 = traj[0]
    read_atoms2 = traj[1]
    traj.close()

    assert atoms == read_atoms1
    assert atoms == read_atoms2


def test_append_to_empty_bundle(atoms, bundle_file):
    # Create an empty BundleTrajectory
    traj = BundleTrajectory(bundle_file, "w")
    traj.close()

    # Append atoms to the empty BundleTrajectory
    traj = BundleTrajectory(bundle_file, "a", atoms=atoms)
    traj.write(atoms)
    traj.close()

    # Read atoms from BundleTrajectory
    traj = BundleTrajectory(bundle_file, "r")
    assert len(traj) == 1
    read_atoms = traj[0]
    traj.close()

    assert atoms == read_atoms


def test_append_to_nonexistent_bundle(atoms, bundle_file):
    # Append atoms to the nonexistent BundleTrajectory
    traj = BundleTrajectory(bundle_file, "a", atoms=atoms)
    traj.write(atoms)
    traj.close()

    # Read atoms from BundleTrajectory
    traj = BundleTrajectory(bundle_file, "r")
    assert len(traj) == 1
    read_atoms = traj[0]
    traj.close()

    assert atoms == read_atoms


def test_read_write_functions(atoms, bundle_file):
    # Write atoms using write_bundletrajectory
    write_bundletrajectory(bundle_file, atoms)

    # Read atoms using read_bundletrajectory
    read_atoms = next(read_bundletrajectory(bundle_file))

    assert atoms == read_atoms


def test_metadata(atoms, bundle_file):
    # Create a BundleTrajectory and write metadata
    traj = BundleTrajectory(bundle_file, "w")
    traj.write(atoms)
    traj.close()

    # Read metadata
    traj = BundleTrajectory(bundle_file, "r")
    metadata = traj.metadata
    traj.close()

    assert "format" in metadata
    assert metadata["format"] == "BundleTrajectory"