Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lattice velocities formatting #3433

Merged
merged 6 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions pymatgen/io/vasp/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).
"""
Expand Down Expand Up @@ -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))
Expand All @@ -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.")

Expand Down
10 changes: 10 additions & 0 deletions tests/io/vasp/test_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import copy
import os
import pickle
import re
import unittest
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -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)
Expand Down