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: make cell and structure backwards compatible #111

Merged
merged 6 commits into from
Oct 2, 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
183 changes: 104 additions & 79 deletions poetry.lock

Large diffs are not rendered by default.

12 changes: 10 additions & 2 deletions src/py4vasp/_data/contcar.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,17 @@ def _topology(self):


def _cell_lines(cell):
yield _float_format(cell.scale, scientific=False).lstrip()
yield _float_format(_cell_scale(cell.scale), scientific=False).lstrip()
yield from _vectors_to_lines(cell.lattice_vectors)


def _cell_scale(scale):
if not scale.is_none():
return scale[()]
else:
return 1.0


def _ion_position_lines(positions, selective_dynamics):
if selective_dynamics.is_none():
yield from _vectors_to_lines(positions)
Expand All @@ -92,7 +99,8 @@ def _lattice_velocity_lines(velocities, cell):
return
yield "Lattice velocities and vectors"
yield from _vectors_to_lines(velocities, scientific=True)
yield from _vectors_to_lines(cell.scale * cell.lattice_vectors, scientific=True)
lattice_vectors = _cell_scale(cell.scale) * cell.lattice_vectors
yield from _vectors_to_lines(lattice_vectors, scientific=True)


def _ion_velocity_lines(velocities):
Expand Down
13 changes: 10 additions & 3 deletions src/py4vasp/_data/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def _create_repr(self, format_=_Format()):
step = self._get_last_step()
lines = (
format_.comment_line(self._topology(), self._step_string()),
format_.scaling_factor(self._raw_data.cell.scale),
format_.scaling_factor(self._scale()),
format_.vectors_to_table(self._raw_data.cell.lattice_vectors[step]),
format_.ion_list(self._topology()),
format_.coordinate_system(),
Expand Down Expand Up @@ -288,7 +288,13 @@ def _topology(self):

def _lattice_vectors(self):
lattice_vectors = _LatticeVectors(self._raw_data.cell.lattice_vectors)
return self._raw_data.cell.scale * lattice_vectors[self._get_steps()]
return self._scale() * lattice_vectors[self._get_steps()]

def _scale(self):
if not self._raw_data.cell.scale.is_none():
return self._raw_data.cell.scale[()]
else:
return 1.0

def _positions(self):
return self._raw_data.positions[self._get_steps()]
Expand Down Expand Up @@ -341,7 +347,8 @@ def error_message(self, key, err):


def _cell_from_ase(structure):
return raw.Cell(lattice_vectors=np.array([structure.get_cell()]))
lattice_vectors = np.array([structure.get_cell()])
return raw.Cell(lattice_vectors, scale=raw.VaspData(1.0))


def _replace_or_set_elements(poscar, elements):
Expand Down
2 changes: 1 addition & 1 deletion src/py4vasp/_raw/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class Cell:

lattice_vectors: VaspData
"Lattice vectors defining the unit cell."
scale: float = 1.0
scale: float
"Global scaling factor applied to all lattice vectors."


Expand Down
1 change: 0 additions & 1 deletion src/py4vasp/_raw/definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ def selections(quantity):
#
schema.add(
raw.Cell,
required=raw.Version(6, 5),
scale="intermediate/ion_dynamics/scale",
lattice_vectors="intermediate/ion_dynamics/lattice_vectors",
)
Expand Down
7 changes: 4 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ def _Sr2TiO4_born_effective_charges():


def _Sr2TiO4_cell():
scale = 6.9229
scale = raw.VaspData(6.9229)
lattice_vectors = [
[1.0, 0.0, 0.0],
[0.678112209738693, 0.734958387251008, 0.0],
Expand Down Expand Up @@ -743,7 +743,8 @@ def _Fe3O4_cell():
[-1.3633791448, 0.0, 5.0446102592],
]
scaling = np.linspace(0.98, 1.01, number_steps)
return raw.Cell(lattice_vectors=np.multiply.outer(scaling, lattice_vectors))
lattice_vectors = np.multiply.outer(scaling, lattice_vectors)
return raw.Cell(lattice_vectors, scale=raw.VaspData(None))


def _Fe3O4_CONTCAR():
Expand Down Expand Up @@ -853,7 +854,7 @@ def _Fe3O4_velocity():

def _Ca3AsBr3_cell():
return raw.Cell(
scale=5.93,
scale=raw.VaspData(5.93),
lattice_vectors=_make_data(np.eye(3)),
)

Expand Down
7 changes: 5 additions & 2 deletions tests/data/test_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,11 @@ def Ca3AsBr3(raw_data):
def make_structure(raw_structure):
structure = Structure.from_data(raw_structure)
structure.ref = types.SimpleNamespace()
cell = raw_structure.cell
structure.ref.lattice_vectors = cell.scale * cell.lattice_vectors
if not raw_structure.cell.scale.is_none():
scale = raw_structure.cell.scale[()]
else:
scale = 1.0
structure.ref.lattice_vectors = scale * raw_structure.cell.lattice_vectors
structure.ref.positions = raw_structure.positions
return structure

Expand Down