Skip to content

Commit

Permalink
Add Cell-like types interface
Browse files Browse the repository at this point in the history
  • Loading branch information
lan496 committed Jul 19, 2024
1 parent 4cab887 commit a302e99
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-python.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
run: |
set -e
pip install moyopy --no-index --find-links dist
pip install -r moyopy/examples/requirements.txt
pip install moyopy[interface]
python moyopy/examples/basic.py
python moyopy/examples/pymatgen_structure.py
Expand Down
8 changes: 8 additions & 0 deletions moyopy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@

Python interface of Moyo

## Installation

```shell
pip install moyopy
# If you want to convert structures into Pymatgen or ASE objects
pip install moyopy[interface]
```

## Examples

See [example directory](examples)
24 changes: 3 additions & 21 deletions moyopy/examples/pymatgen_structure.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,10 @@
from __future__ import annotations

import numpy as np
from pymatgen.core import Element, Structure
from pymatgen.core import Structure

import moyopy


class MoyoAdapter:
@staticmethod
def get_structure(cell: moyopy.Cell) -> Structure:
species = [Element.from_Z(number) for number in cell.numbers]
return Structure(lattice=cell.basis, species=species, coords=cell.positions)

@staticmethod
def get_moyo_cell(structure: Structure) -> moyopy.Cell:
basis = structure.lattice.matrix
positions = structure.frac_coords
numbers = [site.specie.Z for site in structure]

return moyopy.Cell(
basis=basis.tolist(),
positions=positions.tolist(),
numbers=numbers,
)
from moyopy.interface import MoyoAdapter


class MoyoSpacegroupAnalyzer:
Expand All @@ -33,7 +15,7 @@ def __init__(
angle_tolerance: float | None = None,
setting: moyopy.Setting | None = None,
):
self._cell = MoyoAdapter.get_moyo_cell(structure)
self._cell = MoyoAdapter.from_structure(structure)
self._dataset = moyopy.MoyoDataset(
cell=self._cell,
symprec=symprec,
Expand Down
2 changes: 0 additions & 2 deletions moyopy/examples/requirements.txt

This file was deleted.

7 changes: 6 additions & 1 deletion moyopy/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,15 @@ classifiers = [
]

[project.optional-dependencies]
interface = [
"numpy<2", # pymatgen seems to be still imcompatible with numpy>=2
"pymatgen",
"ase>=3.23",
]
dev = [
"moyopy[interface]",
"pytest",
"pre-commit",
"numpy",
]

[tool.maturin]
Expand Down
50 changes: 50 additions & 0 deletions moyopy/python/moyopy/interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from __future__ import annotations

try:
import ase
from pymatgen.core import Element, Structure
except ImportError:
raise ImportError("Try installing dependencies with `pip install moyopy[interface]`")

import moyopy


class MoyoAdapter:
@staticmethod
def get_structure(cell: moyopy.Cell) -> Structure:
species = [Element.from_Z(number) for number in cell.numbers]
return Structure(lattice=cell.basis, species=species, coords=cell.positions)

@staticmethod
def get_atoms(cell: moyopy.Cell) -> ase.Atoms:
atoms = ase.Atoms(
cell=cell.basis,
scaled_positions=cell.positions,
numbers=cell.numbers,
pbc=True,
)
return atoms

@staticmethod
def from_structure(structure: Structure) -> moyopy.Cell:
basis = structure.lattice.matrix
positions = structure.frac_coords
numbers = [site.specie.Z for site in structure]

return moyopy.Cell(
basis=basis.tolist(),
positions=positions.tolist(),
numbers=numbers,
)

@staticmethod
def from_atoms(atoms: ase.Atoms) -> moyopy.Cell:
basis = list(atoms.cell)
positions = atoms.get_scaled_positions()
numbers = atoms.get_atomic_numbers()

return moyopy.Cell(
basis=basis,
positions=positions,
numbers=numbers,
)
2 changes: 1 addition & 1 deletion moyopy/python/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def wurtzite() -> moyopy.Cell:
[1 / 3, 2 / 3, z2_2b],
[2 / 3, 1 / 3, z2_2b + 0.5],
]
numbers = [0, 0, 1, 1]
numbers = [1, 1, 2, 2]

cell = moyopy.Cell(basis, positions, numbers)
return cell
22 changes: 22 additions & 0 deletions moyopy/python/tests/test_interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from __future__ import annotations

import numpy as np

import moyopy
from moyopy.interface import MoyoAdapter


def test_pymatgen_moyopy(wurtzite: moyopy.Cell):
structure = MoyoAdapter.get_structure(wurtzite)
cell = MoyoAdapter.from_structure(structure)
assert np.allclose(cell.basis, wurtzite.basis)
assert np.allclose(cell.positions, wurtzite.positions)
assert cell.numbers == wurtzite.numbers


def test_ase_moyopy(wurtzite: moyopy.Cell):
atoms = MoyoAdapter.get_atoms(wurtzite)
cell = MoyoAdapter.from_atoms(atoms)
assert np.allclose(cell.basis, wurtzite.basis)
assert np.allclose(cell.positions, wurtzite.positions)
assert cell.numbers == wurtzite.numbers

0 comments on commit a302e99

Please sign in to comment.