Examples#

Basic Usage#

This example demonstrates the basic usage of the moyopy package. First, we create a moyopy.Cell representing a crystal structure, and then create a moyopy.MoyoDataset. The moyopy.MoyoDataset contains symmetry information of the input crystal structure: for example, the space group number, symmetry operations, and standardized cell. When we need a secondary symmetry information such as Hermann-Mauguin symbol for the space group type, we can use the moyopy.HallSymbolEntry to access the symmetry database.

from math import sqrt

import moyopy

# 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-4, angle_tolerance=None, setting=None)
assert dataset.number == 186
assert dataset.hall_number == 480

hall_symbol_entry = moyopy.HallSymbolEntry(hall_number=dataset.hall_number)
assert hall_symbol_entry.hm_short == "P 6_3 m c"

# MoyoDataset can be serialized to Python dictionary
dataset_as_dict = dataset.as_dict()
dataset2 = moyopy.MoyoDataset.from_dict(dataset_as_dict)
assert dataset2.number == dataset.number

# MoyoDataset can be serialized to JSON string
dataset_as_json = dataset.serialize_json()
assert isinstance(dataset_as_json, str)
dataset3 = moyopy.MoyoDataset.deserialize_json(dataset_as_json)
assert dataset3.number == dataset.number

Accessing space-group type information#

You can access the space-group classification information using moyopy.SpaceGroupType.

# ruff: noqa: E501
from moyopy import SpaceGroupType

s = SpaceGroupType(15)  # ITA space group number (1 - 230)
assert s.hm_short == "C 2/c"
assert s.crystal_system == "Monoclinic"

print(s)
# -> PySpaceGroupType { number: 15, hm_short: "C 2/c", hm_full: "C 1 2/c 1", arithmetic_number: 8, arithmetic_symbol: "2/mC", geometric_crystal_class: "2/m", crystal_system: "Monoclinic", bravais_class: "mC", lattice_system: "Monoclinic", crystal_family: "Monoclinic" }

You can reverse-lookup an ITA space group number from a Hermann-Mauguin symbol by looping over moyopy.SpaceGroupType.

# ruff: noqa: E501
from moyopy import SpaceGroupType

map_hm_short_to_number = {
    # SpaceGroupType(number).hm_short is separated by a space like "F m -3 m"
    SpaceGroupType(number).hm_short.replace(" ", ""): number
    for number in range(1, 230 + 1)
}

print(map_hm_short_to_number.get("Fm-3m"))  # -> 225
print(map_hm_short_to_number.get("P6_3/m"))  # -> 176
print(map_hm_short_to_number.get("Fmmm"))  # -> 69