diff --git a/pymatgen/io/vasp/inputs.py b/pymatgen/io/vasp/inputs.py index 710dfd489b1..8b6ad9fc3e7 100644 --- a/pymatgen/io/vasp/inputs.py +++ b/pymatgen/io/vasp/inputs.py @@ -73,9 +73,11 @@ class Poscar(MSONable): velocities: Velocities for each site (typically read in from a CONTCAR). A Nx3 array of floats. predictor_corrector: Predictor corrector coordinates and derivatives for each site; - i.e. a list of three 1x3 arrays for each site (typically read in from a MD CONTCAR). + i.e. a list of three 1x3 arrays for each site (typically read in from an MD CONTCAR). predictor_corrector_preamble: Predictor corrector preamble contains the predictor-corrector key, POTIM, and thermostat parameters that precede the site-specific predictor corrector data in MD CONTCAR. + lattice_velocities: Lattice velocities and current lattice (typically read + in from an MD CONTCAR). A 6x3 array of floats. temperature: Temperature of velocity Maxwell-Boltzmann initialization. Initialized to -1 (MB hasn't been performed). """ @@ -520,8 +522,8 @@ def get_str(self, direct: bool = True, vasp4_compatible: bool = False, significa format_str = f"{{:{significant_figures + 5}.{significant_figures}f}}" lines = [self.comment, "1.0"] - for v in latt.matrix: - lines.append(" ".join(format_str.format(c) for c in v)) + for vec in latt.matrix: + lines.append(" ".join(format_str.format(c) for c in vec)) if self.true_names and not vasp4_compatible: lines.append(" ".join(self.site_symbols)) @@ -544,16 +546,17 @@ def get_str(self, direct: bool = True, vasp4_compatible: bool = False, significa try: lines.append("Lattice velocities and vectors") lines.append(" 1") - for v in self.lattice_velocities: - lines.append(" ".join(format_str.format(i) for i in v)) + for velo in self.lattice_velocities: + # VASP is strict about the format when reading this quantity + lines.append(" ".join(f" {val: .7E}" for val in velo)) except Exception: warnings.warn("Lattice velocities are missing or corrupted.") if self.velocities: try: lines.append("") - for v in self.velocities: - lines.append(" ".join(format_str.format(i) for i in v)) + for velo in self.velocities: + lines.append(" ".join(format_str.format(val) for val in velo)) except Exception: warnings.warn("Velocities are missing or corrupted.") diff --git a/tests/io/vasp/test_inputs.py b/tests/io/vasp/test_inputs.py index 5ff0ef03bbd..3e2977d713a 100644 --- a/tests/io/vasp/test_inputs.py +++ b/tests/io/vasp/test_inputs.py @@ -3,6 +3,7 @@ import copy import os import pickle +import re import unittest from typing import TYPE_CHECKING @@ -297,6 +298,15 @@ def test_write_md_poscar(self): poscar = Poscar.from_file(f"{TEST_FILES_DIR}/CONTCAR.MD.npt", check_for_potcar=False) poscar.write_file(path) + + # check output produced for lattice velocities has required format and spaces + # added in https://github.com/materialsproject/pymatgen/pull/3433 + with open(path) as file: + lines = file.readlines() + pattern = (r" [-| ]?\d\.\d{7}E[+-]\d{2}" * 3)[1:] + for line in lines[18:24]: + assert re.match(pattern, line.rstrip()) + p3 = Poscar.from_file(path) assert_allclose(poscar.structure.lattice.abc, p3.structure.lattice.abc, 5)