Skip to content

Commit

Permalink
Add python interface for magnetic space-group type
Browse files Browse the repository at this point in the history
  • Loading branch information
lan496 committed Feb 11, 2025
1 parent 5714727 commit 374d212
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 2 deletions.
3 changes: 2 additions & 1 deletion moyo/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ pub use hall_symbol::{HallSymbol, MagneticHallSymbol};
pub use hall_symbol_database::{hall_symbol_entry, HallNumber, HallSymbolEntry, Number};
pub use magnetic_hall_symbol_database::{magnetic_hall_symbol_entry, MagneticHallSymbolEntry};
pub use magnetic_space_group::{
get_magnetic_space_group_type, ConstructType, UNINumber, NUM_MAGNETIC_SPACE_GROUP_TYPES,
get_magnetic_space_group_type, ConstructType, MagneticSpaceGroupType, UNINumber,
NUM_MAGNETIC_SPACE_GROUP_TYPES,
};
pub use setting::Setting;

Expand Down
5 changes: 5 additions & 0 deletions moyo/src/data/magnetic_space_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ pub enum ConstructType {

#[derive(Debug, Clone)]
pub struct MagneticSpaceGroupType {
/// Serial number of UNI (and BNS) symbols
pub uni_number: UNINumber,
/// Serial number in Litvin's [Magnetic group tables](https://www.iucr.org/publ/978-0-9553602-2-0)
pub litvin_number: i32,
/// BNS number e.g. '151.32'
pub bns_number: &'static str,
/// OG number e.g. '153.4.1270'
pub og_number: &'static str,
/// ITA number for reference space group in BNS setting
pub number: Number,
/// Construct type of magnetic space group
pub construct_type: ConstructType,
}

Expand Down
22 changes: 22 additions & 0 deletions moyopy/python/moyopy/_data.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,26 @@ class SpaceGroupType:
for string values.
"""

class MagneticSpaceGroupType:
"""Magnetic space-group type information."""
def __init__(self, uni_number: int): ...
@property
def uni_number(self) -> int:
"""Serial number of UNI (and BNS) symbols."""
@property
def litvin_number(self) -> int:
"""Serial number in Litvin's `Magnetic group tables <https://www.iucr.org/publ/978-0-9553602-2-0>`_."""
@property
def bns_number(self) -> str:
"""BNS number e.g. '151.32'"""
@property
def og_number(self) -> str:
"""OG number e.g. '153.4.1270'"""
@property
def number(self) -> int:
"""ITA number for reference space group in BNS setting."""
@property
def construct_type(self) -> int:
"""Construct type of magnetic space group from 1 to 4."""

def operations_from_number(number: int, setting: Setting) -> Operations: ...
2 changes: 2 additions & 0 deletions moyopy/python/moyopy/_moyopy.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ from moyopy._base import ( # noqa: F401
from moyopy._data import (
Centering,
HallSymbolEntry,
MagneticSpaceGroupType,
Setting,
SpaceGroupType,
operations_from_number,
Expand All @@ -30,6 +31,7 @@ __all__ = [
"Centering",
"HallSymbolEntry",
"SpaceGroupType",
"MagneticSpaceGroupType",
"operations_from_number",
# dataset
"MoyoDataset",
Expand Down
8 changes: 8 additions & 0 deletions moyopy/python/tests/data/test_magnetic_space_group_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from moyopy import MagneticSpaceGroupType


def test_magnetic_space_group_type():
msgt = MagneticSpaceGroupType(1262)
assert msgt.bns_number == "151.32"
assert msgt.og_number == "153.4.1270"
assert msgt.construct_type == 4
File renamed without changes.
2 changes: 2 additions & 0 deletions moyopy/src/data.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
mod hall_symbol;
mod magnetic_space_group_type;
mod setting;
mod space_group_type;

pub use hall_symbol::{PyCentering, PyHallSymbolEntry};
pub use magnetic_space_group_type::PyMagneticSpaceGroupType;
pub use setting::PySetting;
pub use space_group_type::PySpaceGroupType;

Expand Down
56 changes: 56 additions & 0 deletions moyopy/src/data/magnetic_space_group_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;

use moyo::data::{
get_magnetic_space_group_type, ConstructType, MagneticSpaceGroupType, Number, UNINumber,
};

#[derive(Debug, Clone)]
#[pyclass(name = "MagneticSpaceGroupType", frozen)]
pub struct PyMagneticSpaceGroupType(MagneticSpaceGroupType);

#[pymethods]
impl PyMagneticSpaceGroupType {
#[new]
pub fn new(uni_number: UNINumber) -> Result<Self, PyErr> {
let magnetic_space_group_type = get_magnetic_space_group_type(uni_number).ok_or(
PyValueError::new_err(format!("Unknown uni_number: {}", uni_number)),
)?;
Ok(PyMagneticSpaceGroupType(magnetic_space_group_type))
}

#[getter]
pub fn uni_number(&self) -> UNINumber {
self.0.uni_number
}

#[getter]
pub fn litvin_number(&self) -> i32 {
self.0.litvin_number
}

#[getter]
pub fn bns_number(&self) -> String {
self.0.bns_number.to_string()
}

#[getter]
pub fn og_number(&self) -> String {
self.0.og_number.to_string()
}

#[getter]
pub fn number(&self) -> Number {
self.0.number
}

#[getter]
pub fn construct_type(&self) -> i32 {
match self.0.construct_type {
ConstructType::Type1 => 1,
ConstructType::Type2 => 2,
ConstructType::Type3 => 3,
ConstructType::Type4 => 4,
}
}
}
4 changes: 3 additions & 1 deletion moyopy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use crate::base::{
PyOperations, PyStructure,
};
use crate::data::{
operations_from_number, PyCentering, PyHallSymbolEntry, PySetting, PySpaceGroupType,
operations_from_number, PyCentering, PyHallSymbolEntry, PyMagneticSpaceGroupType, PySetting,
PySpaceGroupType,
};
use crate::dataset::{
PyMoyoCollinearMagneticDataset, PyMoyoDataset, PyMoyoNonCollinearMagneticDataset,
Expand Down Expand Up @@ -58,6 +59,7 @@ fn moyopy(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<PySetting>()?;
m.add_class::<PyCentering>()?;
m.add_class::<PySpaceGroupType>()?;
m.add_class::<PyMagneticSpaceGroupType>()?;
m.add_wrapped(wrap_pyfunction!(operations_from_number))?;

Ok(())
Expand Down

0 comments on commit 374d212

Please sign in to comment.