Source code for httk.atomistic.models.crystalpattern.view
"""Lazy anonymous-structure presentation view."""
from functools import cached_property
from typing import TYPE_CHECKING, Any, Self
from httk.core import unwrap
from httk.atomistic.models.cell.cell import Cell
from httk.atomistic.models.crystalpattern.backend import CrystalPatternBackend
from httk.atomistic.models.crystalpattern.crystalpattern import CrystalPattern
from httk.atomistic.models.crystalpattern.view_base import CrystalPatternViewBase
from httk.atomistic.models.sites.sites import Sites
from httk.atomistic.models.species.species import Species
if TYPE_CHECKING:
from httk.atomistic.models.crystalpattern.like import CrystalPatternLike
from httk.atomistic.models.structure.like import StructureLike
[docs]
class CrystalPatternView(CrystalPatternViewBase, CrystalPattern):
r"""Present an anonymous or ordinary structure lazily as an anonymous structure.
:param obj: The anonymous-structure-like or structure-like object to present.
:param \*\*hints: Backend-selection hints.
"""
_backend: CrystalPatternBackend
def __new__(cls, obj: "CrystalPatternLike | StructureLike", **hints: Any) -> Self:
if isinstance(obj, cls):
return obj
backend = cls._prepare_backend(obj, hints)
instance = super().__new__(cls)
instance._backend = backend
return instance
def __init__(self, obj: Any, **hints: Any) -> None:
pass
def _fill_cell(self) -> None:
object.__setattr__(self, "_cell", self._backend.cell)
def _fill_sites(self) -> None:
object.__setattr__(self, "_sites", self._backend.sites)
def _fill_species(self) -> None:
object.__setattr__(self, "_species", self._backend.species)
def _fill_species_at_sites(self) -> None:
object.__setattr__(self, "_species_at_sites", self._backend.species_at_sites)
@cached_property
def _cell(self) -> Cell: # type: ignore[override]
self._fill_cell()
return self.__dict__["_cell"]
@cached_property
def _sites(self) -> Sites: # type: ignore[override]
self._fill_sites()
return self.__dict__["_sites"]
@cached_property
def _species(self) -> tuple[Species, ...]: # type: ignore[override]
self._fill_species()
return self.__dict__["_species"]
@cached_property
def _species_at_sites(self) -> tuple[str, ...]: # type: ignore[override]
self._fill_species_at_sites()
return self.__dict__["_species_at_sites"]
@property
[docs]
def periodicity(self):
"""Return the presented periodic directions."""
return self.cell.periodicity
@property
[docs]
def nperiodic_dimensions(self):
"""Return the number of presented periodic directions."""
return self.cell.nperiodic_dimensions
@property
[docs]
def nsites(self):
"""Return the number of presented sites."""
return len(self.sites)
[docs]
def cartesian_sites(self):
"""Return the exact Cartesian presented site positions."""
from httk.core import SurdVector
return SurdVector(self.sites.reduced_coords) * self.cell.basis
@property
[docs]
def coordinate_precision(self):
"""Return the presented coordinate precision."""
return self.sites.precision
@property
[docs]
def basis_precision(self):
"""Return the presented basis precision."""
return self.cell.precision
[docs]
def unwrap(self) -> Any:
"""Return the raw object behind the backend.
:return: The unwrapped source object.
"""
return unwrap(self._backend)
[docs]
def unview(self) -> CrystalPattern:
"""Return the presented structure as a standalone value.
:return: The anonymous structure value.
"""
if type(self._backend) is CrystalPattern:
return self._backend
return CrystalPattern(self.cell, self.sites, self.species, self.species_at_sites)