Skip to content

Commit b1e5023

Browse files
mturianskyjanosh
andauthored
* add vect for xsf * add test for vect append * refactor TestXSF --------- Co-authored-by: Janosh Riebesell <[email protected]>
1 parent ca8f608 commit b1e5023

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

pymatgen/io/xcrysden.py

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ def to_str(self, atom_symbol=True):
4747
sp = site.specie.symbol if atom_symbol else f"{site.specie.Z}"
4848
x, y, z = coord
4949
app(f"{sp} {x:20.14f} {y:20.14f} {z:20.14f}")
50+
if "vect" in site.properties:
51+
vx, vy, vz = site.properties["vect"]
52+
lines[-1] += f" {vx:20.14f} {vy:20.14f} {vz:20.14f}"
5053

5154
return "\n".join(lines)
5255

tests/io/test_xcrysden.py

+24-10
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
11
from __future__ import annotations
22

3+
import numpy as np
4+
35
from pymatgen.core.structure import Structure
46
from pymatgen.io.xcrysden import XSF
57
from pymatgen.util.testing import PymatgenTest
68

79

810
class TestXSF(PymatgenTest):
9-
def test_xsf(self):
10-
coords = [[0, 0, 0], [0.75, 0.5, 0.75]]
11-
lattice = [
11+
def setUp(self):
12+
self.coords = [[0, 0, 0], [0.75, 0.5, 0.75]]
13+
self.lattice = [
1214
[3.8401979337, 0.00, 0.00],
1315
[1.9200989668, 3.3257101909, 0.00],
1416
[0.00, -2.2171384943, 3.1355090603],
1517
]
16-
structure = Structure(lattice, ["Si", "Si"], coords)
17-
xsf = XSF(structure)
18-
assert structure, XSF.from_str(xsf.to_str())
18+
self.struct = Structure(self.lattice, ["Si", "Si"], self.coords)
19+
20+
def test_xsf(self):
21+
xsf = XSF(self.struct)
22+
assert self.struct, XSF.from_str(xsf.to_str())
23+
xsf = XSF(self.struct)
24+
assert self.struct, XSF.from_str(xsf.to_str())
25+
26+
def test_append_vect(self):
27+
self.struct.add_site_property("vect", np.eye(2, 3))
28+
xsf_str = XSF(self.struct).to_str()
29+
last_line_split = xsf_str.split("\n")[-1].split()
30+
assert len(last_line_split) == 7
31+
assert last_line_split[-1] == "0.00000000000000"
32+
assert last_line_split[-2] == "1.00000000000000"
33+
assert last_line_split[-3] == "0.00000000000000"
1934

2035
def test_to_str(self):
2136
structure = self.get_structure("Li2O")
2237
xsf = XSF(structure)
23-
xsf_str = xsf.to_str()
2438
assert (
25-
xsf_str
39+
xsf.to_str()
2640
== """CRYSTAL
2741
# Primitive lattice vectors in Angstrom
2842
PRIMVEC
@@ -36,9 +50,9 @@ def test_to_str(self):
3650
Li 3.01213761017484 2.21364440998406 4.74632330032018
3751
Li 1.00309136982516 0.73718000001594 1.58060372967982"""
3852
)
39-
xsf_str = xsf.to_str(atom_symbol=False)
53+
4054
assert (
41-
xsf_str
55+
xsf.to_str(atom_symbol=False)
4256
== """CRYSTAL
4357
# Primitive lattice vectors in Angstrom
4458
PRIMVEC

0 commit comments

Comments
 (0)