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

Poscar.get_str() cartesian vs direct coords #4188

Open
Mikluki opened this issue Nov 21, 2024 · 1 comment
Open

Poscar.get_str() cartesian vs direct coords #4188

Mikluki opened this issue Nov 21, 2024 · 1 comment
Labels

Comments

@Mikluki
Copy link

Mikluki commented Nov 21, 2024

First of all, thank you for this incredible library!

Python version

Python 3.12.7

Pymatgen version

2024.11.13

Operating system version

Pop!_OS 22.04 LTS

Current behavior

Steps to reproduce

  • define structure in cartesian coords
  • create Poscar object from structure
  • wirte poscar to file
    -⚡ POSCAR file contains cartesian coords with a vasp flag direct
    -⚡ POSCAR __repr__ & __str__ show direct coords.

Expected Behavior

  • POSCAR file contains cartesian coords with a vasp flag cartesian
  • POSCAR __repr__ & __str__ shows cartesian coords.

Additional info

Imho, this is a contrintuitive behavior.
In order for this to work one should explicitly call poscar.write_file() with direct=False argument: poscar.write_file("POSCAR", direct=False). However, it will not affect __repr__ & __str__ behavior. Reason being: direct property is not passed to dunder methods, but is overwritten by default get_str declaration.

def __repr__(self) -> str:
return self.get_str()
def __str__(self) -> str:
"""String representation of Poscar file."""
return self.get_str()

def get_str(
self,
direct: bool = True,
vasp4_compatible: bool = False,
significant_figures: int = 16,
) -> str:

def write_file(self, filename: PathLike, **kwargs) -> None:
"""Write POSCAR to a file. The supported kwargs are the same as those for
the Poscar.get_str method and are passed through directly.
"""
with zopen(filename, mode="wt") as file:
file.write(self.get_str(**kwargs))

Minimal example

from pymatgen.io.vasp import Poscar
from pymatgen.core import Structure, Lattice

lattice = Lattice.cubic(10.0)

species = ["Al"]  # Single aluminum atom
coords = [[0, 0, 0]]  # Position of the atom in Cartesian coordinates

structure = Structure(
    lattice,
    species,
    coords,
    coords_are_cartesian=False,
)

poscar = Poscar(structure)
poscar.write_file("POSCAR")
@Mikluki Mikluki added the bug label Nov 21, 2024
@shyuep
Copy link
Member

shyuep commented Nov 21, 2024

Erm, I am not sure what you mean. I don't think you can use 0, 0, 0 as the example. Those coordinates are the same whether in direct or cartesian coords by definition. Also, your code is confusing because your comment says "Position of the atom in Cartesian coordinates" and yet you set coords_are_cartesian to False. Those are contradictory.

You will need to show an example where the Cartesian coords lead to the wrong direct coordinates. The Poscar always write direct coordinates by default because that is usually the way people define coordinates in crystals. But you can set cartesian output if you'd like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants