Skip to content

Commit

Permalink
python examples
Browse files Browse the repository at this point in the history
  • Loading branch information
lan496 committed Apr 29, 2024
1 parent cf3e4db commit c4f4011
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 3 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/ci-python.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,29 @@ permissions:
packages: write # for uraimo/run-on-arch-action to cache docker images

jobs:
examples:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2
- uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Build wheels
uses: PyO3/maturin-action@v1
with:
args: --release --out dist --manifest-path moyopy/Cargo.toml
sccache: 'true'
manylinux: auto
- name: examples
shell: bash
run: |
set -e
pip install moyopy[dev] --find-links dist --force-reinstall
pip install -r moyopy/examples/requirements.txt
python moyopy/examples/basic.py
python moyopy/examples/pymatgen_structure.py
linux:
runs-on: ubuntu-latest
strategy:
Expand Down
6 changes: 3 additions & 3 deletions bench/mp/analysis.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -255,7 +255,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 14,
"metadata": {},
"outputs": [
{
Expand All @@ -280,7 +280,7 @@
"ax.set_aspect('equal')\n",
"ax.set_title(\"mp_computed_structure_entries\")\n",
"ax.grid()\n",
"fig.savefig('mp.png', bbox_inches=\"tight\", pad_inches=0.0, dpi=500)"
"fig.savefig('mp.png', bbox_inches=\"tight\", pad_inches=0.0, dpi=200)"
]
},
{
Expand Down
Binary file added bench/mp/mp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions moyopy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@
[![image](https://img.shields.io/pypi/pyversions/moyopy.svg)](https://pypi.python.org/pypi/moyopy)

Python interface of Moyo

## Examples

See [example directory](examples)
31 changes: 31 additions & 0 deletions moyopy/examples/basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from __future__ import annotations

from math import sqrt

import moyopy

if __name__ == "__main__":
# https://next-gen.materialsproject.org/materials/mp-560588
a = 3.81
c = 6.24
basis = [
[a, 0.0, 0.0],
[-a / 2.0, a * sqrt(3.0) / 2.0, 0.0],
[0.0, 0.0, c],
]
z1_2b = 0.00014
z2_2b = 0.37486
positions = [
# 2b
[1 / 3, 2 / 3, z1_2b],
[2 / 3, 1 / 3, z1_2b + 0.5],
# 2b
[1 / 3, 2 / 3, z2_2b],
[2 / 3, 1 / 3, z2_2b + 0.5],
]
numbers = [0, 0, 1, 1]
cell = moyopy.Cell(basis, positions, numbers)

dataset = moyopy.MoyoDataset(cell, symprec=1e-5, angle_tolerance=None, setting=None)
assert dataset.number == 186
assert dataset.hall_number == 480
131 changes: 131 additions & 0 deletions moyopy/examples/pymatgen_structure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
from __future__ import annotations

import numpy as np
from pymatgen.core import Element, 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,
)


class MoyoSpacegroupAnalyzer:
def __init__(
self,
structure: Structure,
symprec: float = 1e-5,
angle_tolerance: float | None = None,
setting: moyopy.Setting | None = None,
):
self._cell = MoyoAdapter.get_moyo_cell(structure)
self._dataset = moyopy.MoyoDataset(
cell=self._cell,
symprec=symprec,
angle_tolerance=angle_tolerance,
setting=setting,
)

@property
def number(self) -> int:
return self._dataset.number

@property
def hall_number(self) -> int:
return self._dataset.hall_number

@property
def rotations(self) -> np.ndarray:
return np.array(self._dataset.operations.rotations)

@property
def translations(self) -> np.ndarray:
return np.array(self._dataset.operations.translations)

@property
def orbits(self) -> list[int]:
return self._dataset.orbits

@property
def wyckoffs(self) -> list[str]:
return self._dataset.wyckoffs

@property
def site_symmetry_symbols(self) -> list[str]:
return self._dataset.site_symmetry_symbols

@property
def std_structure(self) -> Structure:
return MoyoAdapter.get_structure(self._dataset.std_cell)

@property
def std_linear(self) -> np.ndarray:
return np.array(self._dataset.std_linear)

@property
def std_origin_shift(self) -> np.ndarray:
return np.array(self._dataset.std_origin_shift)

@property
def std_rotation_matrix(self) -> np.ndarray:
return np.array(self._dataset.std_rotation_matrix)

@property
def prim_std_structure(self) -> Structure:
return MoyoAdapter.get_structure(self._dataset.prim_std_cell)

@property
def prim_std_linear(self) -> np.ndarray:
return np.array(self._dataset.prim_std_linear)

@property
def prim_std_origin_shift(self) -> np.ndarray:
return np.array(self._dataset.prim_std_origin_shift)

@property
def mapping_std_prim(self) -> list[int]:
return self._dataset.mapping_std_prim

@property
def symprec(self) -> float:
return self._dataset.symprec

@property
def angle_tolerance(self) -> float | None:
return self._dataset.angle_tolerance


if __name__ == "__main__":
a = 4.0
structure = Structure(
lattice=np.eye(3) * a,
species=["Al"] * 4,
coords=np.array(
[
[0.0, 0.0, 0.0],
[0.0, 0.5, 0.5],
[0.5, 0.0, 0.5],
[0.5, 0.5, 0.0],
]
),
)
msa = MoyoSpacegroupAnalyzer(structure)
assert msa.number == 225
assert msa.rotations.shape == (48 * 4, 3, 3)
assert msa.orbits == [0, 0, 0, 0]
assert msa.prim_std_structure.num_sites == 1
1 change: 1 addition & 0 deletions moyopy/examples/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pymatgen

0 comments on commit c4f4011

Please sign in to comment.