• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

materialsproject / pymatgen / 4075885785

pending completion
4075885785

push

github

Shyue Ping Ong
Merge branch 'master' of github.com:materialsproject/pymatgen

96 of 96 new or added lines in 27 files covered. (100.0%)

81013 of 102710 relevant lines covered (78.88%)

0.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

99.53
/pymatgen/io/tests/test_ase.py
1
# Copyright (c) Pymatgen Development Team.
2
# Distributed under the terms of the MIT License.
3

4

5
from __future__ import annotations
1✔
6

7
import os
1✔
8
import unittest
1✔
9

10
import numpy as np
1✔
11
import pytest
1✔
12

13
import pymatgen.io.ase as aio
1✔
14
from pymatgen.core.composition import Composition
1✔
15
from pymatgen.core.structure import Molecule, StructureError
1✔
16
from pymatgen.io.vasp.inputs import Poscar
1✔
17
from pymatgen.util.testing import PymatgenTest
1✔
18

19

20
class AseAtomsAdaptorTest(unittest.TestCase):
1✔
21
    @unittest.skipIf(not aio.ase_loaded, "ASE not loaded.")
1✔
22
    def test_get_atoms_from_structure(self):
1✔
23
        p = Poscar.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "POSCAR"))
1✔
24
        structure = p.structure
1✔
25
        atoms = aio.AseAtomsAdaptor.get_atoms(structure)
1✔
26
        ase_composition = Composition(atoms.get_chemical_formula())
1✔
27
        assert ase_composition == structure.composition
1✔
28
        assert atoms.cell is not None and atoms.cell.any()
1✔
29
        assert atoms.get_pbc() is not None and atoms.get_pbc().all()
1✔
30
        assert atoms.get_chemical_symbols() == [s.species_string for s in structure]
1✔
31
        assert not atoms.has("initial_magmoms")
1✔
32
        assert atoms.calc is None
1✔
33

34
        p = Poscar.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "POSCAR"))
1✔
35
        structure = p.structure
1✔
36
        prop = [3.14] * len(structure)
1✔
37
        structure.add_site_property("prop", prop)
1✔
38
        atoms = aio.AseAtomsAdaptor.get_atoms(structure)
1✔
39
        assert atoms.get_array("prop").tolist() == prop
1✔
40

41
    @unittest.skipIf(not aio.ase_loaded, "ASE not loaded.")
1✔
42
    def test_get_atoms_from_structure_mags(self):
1✔
43
        p = Poscar.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "POSCAR"))
1✔
44
        structure = p.structure
1✔
45
        mags = [1.0] * len(structure)
1✔
46
        structure.add_site_property("final_magmom", mags)
1✔
47
        atoms = aio.AseAtomsAdaptor.get_atoms(structure)
1✔
48
        assert not atoms.has("initial_magmoms")
1✔
49
        assert atoms.get_magnetic_moments().tolist() == mags
1✔
50

51
        p = Poscar.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "POSCAR"))
1✔
52
        structure = p.structure
1✔
53
        initial_mags = [0.5] * len(structure)
1✔
54
        structure.add_site_property("magmom", initial_mags)
1✔
55
        atoms = aio.AseAtomsAdaptor.get_atoms(structure)
1✔
56
        assert atoms.get_initial_magnetic_moments().tolist() == initial_mags
1✔
57
        assert atoms.calc is None
1✔
58

59
        p = Poscar.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "POSCAR"))
1✔
60
        structure = p.structure
1✔
61
        mags = [1.0] * len(structure)
1✔
62
        structure.add_site_property("final_magmom", mags)
1✔
63
        initial_mags = [2.0] * len(structure)
1✔
64
        structure.add_site_property("magmom", initial_mags)
1✔
65
        atoms = aio.AseAtomsAdaptor.get_atoms(structure)
1✔
66
        assert atoms.get_initial_magnetic_moments().tolist(), initial_mags
1✔
67
        assert atoms.get_magnetic_moments().tolist(), mags
1✔
68

69
    @unittest.skipIf(not aio.ase_loaded, "ASE not loaded.")
1✔
70
    def test_get_atoms_from_structure_charge(self):
1✔
71
        p = Poscar.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "POSCAR"))
1✔
72
        structure = p.structure
1✔
73
        charges = [1.0] * len(structure)
1✔
74
        structure.add_site_property("final_charge", charges)
1✔
75
        atoms = aio.AseAtomsAdaptor.get_atoms(structure)
1✔
76
        assert not atoms.has("initial_charges")
1✔
77
        assert atoms.get_charges().tolist() == charges
1✔
78

79
        p = Poscar.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "POSCAR"))
1✔
80
        structure = p.structure
1✔
81
        charges = [0.5] * len(structure)
1✔
82
        structure.add_site_property("charge", charges)
1✔
83
        atoms = aio.AseAtomsAdaptor.get_atoms(structure)
1✔
84
        assert atoms.calc is None
1✔
85
        assert atoms.get_initial_charges().tolist() == charges
1✔
86

87
        p = Poscar.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "POSCAR"))
1✔
88
        structure = p.structure
1✔
89
        charges = [1.0] * len(structure)
1✔
90
        structure.add_site_property("final_charge", charges)
1✔
91
        initial_charges = [2.0] * len(structure)
1✔
92
        structure.add_site_property("charge", initial_charges)
1✔
93
        atoms = aio.AseAtomsAdaptor.get_atoms(structure)
1✔
94
        assert atoms.get_initial_charges().tolist(), initial_charges
1✔
95
        assert atoms.get_charges().tolist(), charges
1✔
96

97
    @unittest.skipIf(not aio.ase_loaded, "ASE not loaded.")
1✔
98
    def test_get_atoms_from_structure_oxistates(self):
1✔
99
        p = Poscar.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "POSCAR"))
1✔
100
        structure = p.structure
1✔
101
        oxi_states = [1.0] * len(structure)
1✔
102
        structure.add_oxidation_state_by_site(oxi_states)
1✔
103
        atoms = aio.AseAtomsAdaptor.get_atoms(structure)
1✔
104
        assert atoms.get_array("oxi_states").tolist() == oxi_states
1✔
105

106
    @unittest.skipIf(not aio.ase_loaded, "ASE not loaded.")
1✔
107
    def test_get_atoms_from_structure_dyn(self):
1✔
108
        p = Poscar.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "POSCAR"))
1✔
109
        structure = p.structure
1✔
110
        structure.add_site_property("selective_dynamics", [[False] * 3] * len(structure))
1✔
111
        atoms = aio.AseAtomsAdaptor.get_atoms(structure)
1✔
112
        assert atoms.constraints[0].get_indices().tolist() == [atom.index for atom in atoms]
1✔
113

114
    @unittest.skipIf(not aio.ase_loaded, "ASE not loaded.")
1✔
115
    def test_get_atoms_from_molecule(self):
1✔
116
        m = Molecule.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "acetylene.xyz"))
1✔
117
        atoms = aio.AseAtomsAdaptor.get_atoms(m)
1✔
118
        ase_composition = Composition(atoms.get_chemical_formula())
1✔
119
        assert ase_composition == m.composition
1✔
120
        assert atoms.cell is None or not atoms.cell.any()
1✔
121
        assert atoms.get_pbc() is None or not atoms.get_pbc().any()
1✔
122
        assert atoms.get_chemical_symbols() == [s.species_string for s in m]
1✔
123
        assert not atoms.has("initial_magmoms")
1✔
124
        assert atoms.calc is None
1✔
125

126
    @unittest.skipIf(not aio.ase_loaded, "ASE not loaded.")
1✔
127
    def test_get_atoms_from_molecule_mags(self):
1✔
128
        molecule = Molecule.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "acetylene.xyz"))
1✔
129
        atoms = aio.AseAtomsAdaptor.get_atoms(molecule)
1✔
130
        mags = [1.0] * len(molecule)
1✔
131
        molecule.add_site_property("final_magmom", mags)
1✔
132
        atoms = aio.AseAtomsAdaptor.get_atoms(molecule)
1✔
133
        assert not atoms.has("initial_magmoms")
1✔
134
        assert atoms.get_magnetic_moments().tolist() == mags
1✔
135

136
        molecule = Molecule.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "acetylene.xyz"))
1✔
137
        atoms = aio.AseAtomsAdaptor.get_atoms(molecule)
1✔
138
        initial_mags = [0.5] * len(molecule)
1✔
139
        molecule.add_site_property("magmom", initial_mags)
1✔
140
        atoms = aio.AseAtomsAdaptor.get_atoms(molecule)
1✔
141
        assert atoms.calc is None
1✔
142
        assert atoms.get_initial_magnetic_moments().tolist() == initial_mags
1✔
143

144
    @unittest.skipIf(not aio.ase_loaded, "ASE not loaded.")
1✔
145
    def test_get_atoms_from_molecule_dyn(self):
1✔
146
        molecule = Molecule.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "acetylene.xyz"))
1✔
147
        molecule.add_site_property("selective_dynamics", [[False] * 3] * len(molecule))
1✔
148
        atoms = aio.AseAtomsAdaptor.get_atoms(molecule)
1✔
149
        assert atoms.constraints[0].get_indices().tolist() == [atom.index for atom in atoms]
1✔
150

151
    @unittest.skipIf(not aio.ase_loaded, "ASE not loaded.")
1✔
152
    def test_get_structure(self):
1✔
153
        from ase.io import read
1✔
154

155
        atoms = read(os.path.join(PymatgenTest.TEST_FILES_DIR, "POSCAR"))
1✔
156
        struct = aio.AseAtomsAdaptor.get_structure(atoms)
1✔
157
        assert struct.formula == "Fe4 P4 O16"
1✔
158
        assert [s.species_string for s in struct] == atoms.get_chemical_symbols()
1✔
159

160
        atoms = read(os.path.join(PymatgenTest.TEST_FILES_DIR, "POSCAR"))
1✔
161
        prop = np.array([3.14] * len(atoms))
1✔
162
        atoms.set_array("prop", prop)
1✔
163
        struct = aio.AseAtomsAdaptor.get_structure(atoms)
1✔
164
        assert struct.site_properties["prop"] == prop.tolist()
1✔
165

166
        atoms = read(os.path.join(PymatgenTest.TEST_FILES_DIR, "POSCAR_overlap"))
1✔
167
        struct = aio.AseAtomsAdaptor.get_structure(atoms)
1✔
168
        assert [s.species_string for s in struct] == atoms.get_chemical_symbols()
1✔
169
        with pytest.raises(StructureError):
1✔
170
            struct = aio.AseAtomsAdaptor.get_structure(atoms, validate_proximity=True)
1✔
171

172
    @unittest.skipIf(not aio.ase_loaded, "ASE not loaded.")
1✔
173
    def test_get_structure_mag(self):
1✔
174
        from ase.io import read
1✔
175

176
        atoms = read(os.path.join(PymatgenTest.TEST_FILES_DIR, "POSCAR"))
1✔
177
        mags = [1.0] * len(atoms)
1✔
178
        atoms.set_initial_magnetic_moments(mags)
1✔
179
        structure = aio.AseAtomsAdaptor.get_structure(atoms)
1✔
180
        assert structure.site_properties["magmom"] == mags
1✔
181
        assert "final_magmom" not in structure.site_properties
1✔
182
        assert "initial_magmoms" not in structure.site_properties
1✔
183

184
        atoms = read(os.path.join(PymatgenTest.TEST_FILES_DIR, "OUTCAR"))
1✔
185
        structure = aio.AseAtomsAdaptor.get_structure(atoms)
1✔
186
        assert structure.site_properties["final_magmom"] == atoms.get_magnetic_moments().tolist()
1✔
187
        assert "magmom" not in structure.site_properties
1✔
188
        assert "initial_magmoms" not in structure.site_properties
1✔
189

190
    @unittest.skipIf(not aio.ase_loaded, "ASE not loaded.")
1✔
191
    def test_get_structure_dyn(self):
1✔
192
        from ase.constraints import FixAtoms
1✔
193
        from ase.io import read
1✔
194

195
        atoms = read(os.path.join(PymatgenTest.TEST_FILES_DIR, "POSCAR"))
1✔
196
        atoms.set_constraint(FixAtoms(mask=[True] * len(atoms)))
1✔
197
        structure = aio.AseAtomsAdaptor.get_structure(atoms)
1✔
198
        assert structure.site_properties["selective_dynamics"][-1][0] is False
1✔
199

200
    @unittest.skipIf(not aio.ase_loaded, "ASE not loaded.")
1✔
201
    def test_get_molecule(self):
1✔
202
        from ase.io import read
1✔
203

204
        atoms = read(os.path.join(PymatgenTest.TEST_FILES_DIR, "acetylene.xyz"))
1✔
205
        molecule = aio.AseAtomsAdaptor.get_molecule(atoms)
1✔
206
        assert molecule.formula == "H2 C2"
1✔
207
        assert [s.species_string for s in molecule] == atoms.get_chemical_symbols()
1✔
208
        assert molecule.charge == 0
1✔
209
        assert molecule.spin_multiplicity == 1
1✔
210

211
        atoms = read(os.path.join(PymatgenTest.TEST_FILES_DIR, "acetylene.xyz"))
1✔
212
        initial_charges = [2.0] * len(atoms)
1✔
213
        initial_mags = [1.0] * len(atoms)
1✔
214
        atoms.set_initial_charges(initial_charges)
1✔
215
        atoms.set_initial_magnetic_moments(initial_mags)
1✔
216
        molecule = aio.AseAtomsAdaptor.get_molecule(atoms)
1✔
217
        assert molecule.charge == np.sum(initial_charges)
1✔
218
        assert molecule.spin_multiplicity == np.sum(initial_mags) + 1
1✔
219
        assert molecule.site_properties.get("charge", None) == initial_charges
1✔
220
        assert molecule.site_properties.get("magmom", None) == initial_mags
1✔
221

222
    @unittest.skipIf(not aio.ase_loaded, "ASE not loaded.")
1✔
223
    def test_back_forth(self):
1✔
224
        from ase.constraints import FixAtoms
1✔
225
        from ase.io import read
1✔
226

227
        atoms = read(os.path.join(PymatgenTest.TEST_FILES_DIR, "OUTCAR"))
1✔
228
        atoms.set_constraint(FixAtoms(mask=[True] * len(atoms)))
1✔
229
        atoms.set_initial_charges([1.0] * len(atoms))
1✔
230
        atoms.set_initial_magnetic_moments([2.0] * len(atoms))
1✔
231
        atoms.set_array("prop", np.array([3.0] * len(atoms)))
1✔
232
        structure = aio.AseAtomsAdaptor.get_structure(atoms)
1✔
233
        atoms_back = aio.AseAtomsAdaptor.get_atoms(structure)
1✔
234
        structure_back = aio.AseAtomsAdaptor.get_structure(atoms)
1✔
235
        assert structure == structure_back
1✔
236

237
        atoms = read(os.path.join(PymatgenTest.TEST_FILES_DIR, "acetylene.xyz"))
1✔
238
        atoms.set_constraint(FixAtoms(mask=[True] * len(atoms)))
1✔
239
        atoms.set_initial_charges([1.0] * len(atoms))
1✔
240
        atoms.set_initial_magnetic_moments([2.0] * len(atoms))
1✔
241
        atoms.set_array("prop", np.array([3.0] * len(atoms)))
1✔
242
        molecule = aio.AseAtomsAdaptor.get_molecule(atoms)
1✔
243
        atoms_back = aio.AseAtomsAdaptor.get_atoms(molecule)
1✔
244
        assert atoms == atoms_back
1✔
245
        molecule_back = aio.AseAtomsAdaptor.get_molecule(atoms)
1✔
246
        assert molecule == molecule_back
1✔
247

248

249
if __name__ == "__main__":
1✔
250
    unittest.main()
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc