Skip to content

Commit 4fc889f

Browse files
authored
Merge pull request #2310 from gpetretto/bugfix
fix write Trajectory XDATACAR with variable lattice
2 parents e6c3366 + 9b8995f commit 4fc889f

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

pymatgen/core/tests/test_trajectory.py

+7
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,13 @@ def test_variable_lattice(self):
499499
all(np.allclose(struct.lattice.matrix, structures[i].lattice.matrix) for i, struct in enumerate(traj))
500500
)
501501

502+
# Check if the file is written correctly when lattice is not constant.
503+
traj.write_Xdatcar(filename="traj_test_XDATCAR")
504+
# Load trajectory from written xdatcar and compare to original
505+
written_traj = Trajectory.from_file("traj_test_XDATCAR", constant_lattice=False)
506+
self._check_traj_equality(traj, written_traj)
507+
os.remove("traj_test_XDATCAR")
508+
502509
def test_to_from_dict(self):
503510
d = self.traj.as_dict()
504511
traj = Trajectory.from_dict(d)

pymatgen/core/trajectory.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ def write_Xdatcar(self, filename="XDATCAR", system=None, significant_figures=6):
501501

502502
for si, frac_coords in enumerate(self.frac_coords):
503503
# Only print out the info block if
504-
if self.constant_lattice and si == 0:
504+
if si == 0 or not self.constant_lattice:
505505
lines.extend([system, "1.0"])
506506

507507
if self.constant_lattice:

0 commit comments

Comments
 (0)