Skip to content

Commit 51cc5c0

Browse files
committed
Test to ASE function
1 parent 0d7a352 commit 51cc5c0

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

tests/test_abstract_model.py

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
def extxyz_file():
1212
return StringIO(
1313
"""2
14-
Properties=species:S:1:pos:R:3:forces:R:3 energy=-1 pbc="F T F"
14+
Properties=species:S:1:pos:R:3:forces:R:3 energy=-1 pbc="F T F" info="test"
1515
Si 0.0 0.0 0.0 0.4 0.6 -0.4
1616
Si 0.0 0.0 0.0 -0.1 -0.5 -0.6
1717
"""
@@ -35,13 +35,15 @@ def test_from_atoms(extxyz_file):
3535
"formula",
3636
"calculator_name",
3737
"calculator_parameters",
38+
"info",
3839
}
3940
assert info_keys == set(data.info_keys)
4041
assert data["pbc"] == [False, True, False]
4142
assert data["n_atoms"] == 2
4243
assert len(data["cell"]) == 3
4344
assert all(arr == [0.0, 0.0, 0.0] for arr in data["cell"])
4445
assert data["formula"] == "Si2"
46+
assert data["info"] == "test"
4547

4648
# Test arrays
4749
assert {"numbers", "positions"} == set(data.arrays_keys)
@@ -74,12 +76,13 @@ def test_from_atoms_no_calc(extxyz_file):
7476
data = AbstractModel.from_atoms(atoms, store_calc=False)
7577

7678
# Test info
77-
assert {"pbc", "n_atoms", "cell", "formula"} == set(data.info_keys)
79+
assert {"pbc", "n_atoms", "cell", "formula", "info"} == set(data.info_keys)
7880
assert data["pbc"] == [False, True, False]
7981
assert data["n_atoms"] == 2
8082
assert len(data["cell"]) == 3
8183
assert all(arr == [0.0, 0.0, 0.0] for arr in data["cell"])
8284
assert data["formula"] == "Si2"
85+
assert data["info"] == "test"
8386

8487
# Test arrays
8588
assert {"numbers", "positions"} == set(data.arrays_keys)
@@ -105,3 +108,46 @@ def test_from_atoms_no_calc(extxyz_file):
105108
"hash_structure",
106109
}
107110
assert derived_keys == set(data.derived_keys)
111+
112+
113+
def test_to_ase(extxyz_file):
114+
"""Test returning data to ASE Atoms object with results."""
115+
atoms = read(extxyz_file, format="extxyz")
116+
data = AbstractModel.from_atoms(atoms, store_calc=True)
117+
118+
new_atoms = data.to_ase()
119+
120+
# Test info set
121+
assert new_atoms.cell == pytest.approx(atoms.cell)
122+
assert new_atoms.pbc == pytest.approx(atoms.pbc)
123+
assert new_atoms.positions == pytest.approx(atoms.positions)
124+
assert new_atoms.numbers == pytest.approx(atoms.numbers)
125+
126+
assert new_atoms.info["n_atoms"] == len(atoms)
127+
assert new_atoms.info["formula"] == atoms.get_chemical_formula()
128+
129+
assert new_atoms.calc.results["energy"] == pytest.approx(
130+
atoms.calc.results["energy"]
131+
)
132+
assert new_atoms.calc.results["forces"] == pytest.approx(
133+
atoms.calc.results["forces"]
134+
)
135+
136+
137+
def test_to_ase_no_results(extxyz_file):
138+
"""Test returning data to ASE Atoms object without results."""
139+
atoms = read(extxyz_file, format="extxyz")
140+
data = AbstractModel.from_atoms(atoms, store_calc=False)
141+
142+
new_atoms = data.to_ase()
143+
144+
# Test info set
145+
assert new_atoms.cell == pytest.approx(atoms.cell)
146+
assert new_atoms.pbc == pytest.approx(atoms.pbc)
147+
assert new_atoms.positions == pytest.approx(atoms.positions)
148+
assert new_atoms.numbers == pytest.approx(atoms.numbers)
149+
150+
assert new_atoms.info["n_atoms"] == len(atoms)
151+
assert new_atoms.info["formula"] == atoms.get_chemical_formula()
152+
153+
assert new_atoms.calc is None

0 commit comments

Comments
 (0)