From c6a188a1fe1b96da102afbf714c1472f3be9b876 Mon Sep 17 00:00:00 2001 From: Philip Eisenlohr Date: Fri, 11 Feb 2022 15:40:14 -0500 Subject: [PATCH] added CrystalFamily, CrystalLattice, CrystalKinematics typehints --- python/damask/_crystal.py | 26 +++++++++++++------------- python/damask/_orientation.py | 10 +++++----- python/damask/_typehints.py | 6 +++++- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/python/damask/_crystal.py b/python/damask/_crystal.py index a63cb0c81..6da13f679 100644 --- a/python/damask/_crystal.py +++ b/python/damask/_crystal.py @@ -2,11 +2,11 @@ from typing import Union, Dict, List, Tuple import numpy as np -from ._typehints import FloatSequence +from ._typehints import FloatSequence, CrystalFamily, CrystalLattice, CrystalKinematics from . import util from . import Rotation -lattice_symmetries = { +lattice_symmetries: Dict[CrystalLattice, CrystalFamily] = { 'aP': 'triclinic', 'mP': 'monoclinic', @@ -32,8 +32,8 @@ class Crystal(): """Crystal lattice.""" def __init__(self,*, - family = None, - lattice = None, + family: CrystalFamily = None, + lattice: CrystalLattice = None, a: float = None, b: float = None, c: float = None, alpha: float = None, beta: float = None, gamma: float = None, degrees: bool = False): @@ -208,7 +208,7 @@ class Crystal(): ... } """ - _basis = { + _basis: Dict[CrystalFamily, Dict[str, np.ndarray]] = { 'cubic': {'improper':np.array([ [-1. , 0. , 1. ], [ np.sqrt(2.) , -np.sqrt(2.) , 0. ], [ 0. , np.sqrt(3.) , 0. ] ]), @@ -322,12 +322,12 @@ class Crystal(): Parameters ---------- - direction|plane : numpy.ndarray of shape (...,3) + direction|plane : numpy.ndarray, shape (...,3) Vector along direction or plane normal. Returns ------- - Miller : numpy.ndarray of shape (...,3) + Miller : numpy.ndarray, shape (...,3) Lattice vector of direction or plane. Use util.scale_to_coprime to convert to (integer) Miller indices. @@ -348,12 +348,12 @@ class Crystal(): Parameters ---------- - uvw|hkl : numpy.ndarray of shape (...,3) + uvw|hkl : numpy.ndarray, shape (...,3) Miller indices of crystallographic direction or plane normal. Returns ------- - vector : numpy.ndarray of shape (...,3) + vector : numpy.ndarray, shape (...,3) Crystal frame vector along [uvw] direction or (hkl) plane normal. """ @@ -366,7 +366,7 @@ class Crystal(): def kinematics(self, - mode: str) -> Dict[str, List[np.ndarray]]: + mode: CrystalKinematics) -> Dict[str, List[np.ndarray]]: """ Return crystal kinematics systems. @@ -381,7 +381,7 @@ class Crystal(): Directions and planes of deformation mode families. """ - _kinematics = { + _kinematics: Dict[CrystalLattice, Dict[CrystalKinematics, List[np.ndarray]]] = { 'cF': { 'slip': [np.array([ [+0,+1,-1, +1,+1,+1], @@ -626,7 +626,7 @@ class Crystal(): def relation_operations(self, - model: str) -> Tuple[str, Rotation]: + model: str) -> Tuple[CrystalLattice, Rotation]: """ Crystallographic orientation relationships for phase transformations. @@ -658,7 +658,7 @@ class Crystal(): https://doi.org/10.1016/j.actamat.2004.11.021 """ - _orientation_relationships = { + _orientation_relationships: Dict[str, Dict[CrystalLattice,np.ndarray]] = { 'KS': { 'cF' : np.array([ [[-1, 0, 1],[ 1, 1, 1]], diff --git a/python/damask/_orientation.py b/python/damask/_orientation.py index 5a9b7b141..54ffb6728 100644 --- a/python/damask/_orientation.py +++ b/python/damask/_orientation.py @@ -1,10 +1,10 @@ import inspect import copy -from typing import Union, Callable, Dict, Any, Tuple +from typing import Union, Callable, List, Dict, Any, Tuple import numpy as np -from ._typehints import FloatSequence, IntSequence +from ._typehints import FloatSequence, IntSequence, CrystalFamily, CrystalLattice from . import Rotation from . import Crystal from . import util @@ -98,8 +98,8 @@ class Orientation(Rotation,Crystal): def __init__(self, rotation: Union[FloatSequence, Rotation] = np.array([1.,0.,0.,0.]), *, - family: str = None, - lattice: str = None, + family: CrystalFamily = None, + lattice: CrystalLattice = None, a: float = None, b: float = None, c: float = None, alpha: float = None, beta: float = None, gamma: float = None, degrees: bool = False): @@ -787,7 +787,7 @@ class Orientation(Rotation,Crystal): @property def symmetry_operations(self) -> Rotation: """Symmetry operations as Rotations.""" - _symmetry_operations = { + _symmetry_operations: Dict[CrystalFamily, List] = { 'cubic': [ [ 1.0, 0.0, 0.0, 0.0 ], [ 0.0, 1.0, 0.0, 0.0 ], diff --git a/python/damask/_typehints.py b/python/damask/_typehints.py index 0b4a56a69..674f54721 100644 --- a/python/damask/_typehints.py +++ b/python/damask/_typehints.py @@ -1,6 +1,6 @@ """Functionality for typehints.""" -from typing import Sequence, Union, TextIO +from typing import Sequence, Union, Literal, TextIO from pathlib import Path import numpy as np @@ -10,5 +10,9 @@ FloatSequence = Union[np.ndarray,Sequence[float]] IntSequence = Union[np.ndarray,Sequence[int]] FileHandle = Union[TextIO, str, Path] NumpyRngSeed = Union[int, IntSequence, np.random.SeedSequence, np.random.Generator] +CrystalFamily = Union[None,Literal['triclinic', 'monoclinic', 'orthorhombic', 'tetragonal', 'hexagonal', 'cubic']] +CrystalLattice = Union[None,Literal['aP', 'mP', 'mS', 'oP', 'oS', 'oI', 'oF', 'tP', 'tI', 'hP', 'cP', 'cI', 'cF']] +CrystalKinematics = Literal['slip', 'twin'] + # BitGenerator does not exists in older numpy versions #NumpyRngSeed = Union[int, IntSequence, np.random.SeedSequence, np.random.BitGenerator, np.random.Generator]