Source code for spacetime.symmetry_group

# Copyright (C) 2024-2026 Dr. Bogdan Tanygin <info@deeptech.business>
#
# This file is part of spacetime-sym and is based on bsym by Benjamin J. Morgan.
# See licenses/bsym-MIT for the upstream license.

from math import pi, isclose
import numpy as np
from spacetime import SymmetryOperation, SymmetryOperationO3, SymmetryOperationSO3
from spacetime.physical_quantity import PhysicalQuantity
from spacetime.linear_algebra import is_scalar, is_scalar_extended, is_3D_vector, is_symmetrical_tensor, \
                                     get_tensor_axis
from itertools import product
from copy import deepcopy
from numpy.linalg import eig, norm, det
from scipy.spatial.transform import Rotation

[docs] class SymmetryGroup: """ :any:`SymmetryGroup` class. A :any:`SymmetryGroup` contains spatial operations with optional binary reversal labels. It generates finite group closure from supplied operations, distinguishing equal spatial matrices with different reversal labels. e.g.:: SymmetryGroup( symmetry_operations=[ s1, s2, s3 ] ) where `s1`, `s2`, and `s3` are :any:`SymmetryOperation` objects. """ class_str = 'SymmetryGroup' def __init__( self, symmetry_operations=[], label = "" ): """ Create a :any:`SymmetryGroup` object. Args: symmetry_operations (list): A list of :any:`SymmetryOperation` objects. label (default = "") (str): optional string label for this `SymmetryGroup` object. Returns: None """ self._symmetry_operations_check_and_init( symmetry_operations ) self._validate_and_correct() self.label = label @property def symmetry_operations( self ): """ List of symmetry transformations forming the group. Args: None Returns: List of symmetry operations. """ return self._symmetry_operations @symmetry_operations.setter def symmetry_operations( self, value ): self._symmetry_operations_check_and_init( value ) self._validate_and_correct() @property def label( self ): """ Label of the symmetry group. Args: None. Returns: (str): label. """ return self._label @label.setter def label( self, value ): """ Label of the symmetry group. Args: label (str): optional string label for this `SymmetryGroup` object. Returns: None. """ if not isinstance( value, str): raise ValueError('Label must be a string') else: self._label = value
[docs] def order( self, group_atol = 1e-4 ): """ Return the group order after validating the symmetry operations. Args: group_atol (float): absolute tolerance of the matrix elements comparison. Returns: Order of the symmetry group. """ self._validate_and_correct(group_atol = group_atol) return len( self._symmetry_operations )
def _add_so_if_new( self, so, group_atol ): self._gen_flag = False # if the matrix is new, we add symmetry operation anyway if not any( np.allclose( so.matrix, so_0_.matrix, atol = group_atol ) for so_0_ in self._symmetry_operations ): self._symmetry_operations.append( so ) self._gen_flag = True # if there is already such matrix, we need to compare dich properties elif isinstance( so, SymmetryOperationO3 ): # default inside this logical branch self._gen_flag = True for so_0_ in self._symmetry_operations: # this must be true for some operations if we are here if np.allclose( so.matrix, so_0_.matrix, atol = group_atol ): # if the existing symmetry operation is not dichromatic, we add the new one anyway # note: we enabled dichromatic properties for the group O3 and its subgroups only # allowing us automatic (spatial) parity control, etc. if isinstance( so_0_, SymmetryOperationO3 ): if ( so.dich_operations == so_0_.dich_operations ) or ( len( so.dich_operations ) == 0 and len( so_0_.dich_operations ) == 0 ): # at least one match is enough to skip adding a new one self._gen_flag = False break if ( self._gen_flag ): self._symmetry_operations.append( so ) def _validate_and_correct( self, group_atol = 1e-4 ): # identity check / add if needed if not self.identity_w_dich_flag: if not any( np.allclose( so.matrix, self._e_0.matrix, atol = group_atol) for so in self._symmetry_operations): self.add_and_generate( self._e_0 ) else: # identity operation must contain empty list of dichromatic reversals if not any( np.allclose( so.matrix, self._e_0.matrix, atol = group_atol) and len( so.dich_operations ) == 0 for so in self._symmetry_operations): self.add_and_generate( self._e_0 ) # first, let's deduplicate the group # group order (as of here and now): g_order = len( self._symmetry_operations ) if g_order > 1: indx_for_remove = list() for i in range( g_order ): if i not in indx_for_remove: for j in range( g_order ): if i != j: so = self._symmetry_operations[i] so_1 = self._symmetry_operations[j] if np.allclose( so.matrix, so_1.matrix, atol = group_atol ): if type(so) is SymmetryOperation and type(so_1) is SymmetryOperation: indx_for_remove.append( j ) # for SymmetryOperationO3 and its subclasses, hence, isinstance(), not type() elif isinstance( so, SymmetryOperationO3 ) and isinstance( so_1, SymmetryOperationO3 ): if ( so.dich_operations == so_1.dich_operations ) or ( len( so.dich_operations ) == 0 and len( so_1.dich_operations ) == 0 ): indx_for_remove.append( j ) for i in sorted(indx_for_remove, reverse = True): del self._symmetry_operations[i] # second, let's regenerate it. To do it, let's remove the symmetry operations & add them again. for so in self._symmetry_operations: self._symmetry_operations.remove( so ) self.add_and_generate( so )
[docs] def add_and_generate( self, so, group_atol = 1e-4 ): """ Add a :any:`SymmetryOperation` to this :any:`SymmetryGroup` and generate the remaining elements from the current set of generators. Args: so (:any:`SymmetryOperation`): The symmetry operation to add. group_atol (float): absolute tolerance of the matrix elements comparison. Raises: ValueError: If the symmetry-operation dimensions do not match. Returns: None """ # dimensions must match. They already match inside group. # Also, the matrices are already square in case of SymmetryOperation objects if any( so_0.matrix.shape[0] != so.matrix.shape[0] for so_0 in self._symmetry_operations ): ValueError('Wrong dimensions of the input symmetry operation') # Adding part self._add_so_if_new( so, group_atol ) # Generating part if self._gen_flag: # the flag is used to track whether group continues to generate new elements # it means that we need one more cycle unless it stops self._gen_flag = False so_list_0 = deepcopy( self._symmetry_operations ) for so_0 in so_list_0: so_1 = so_0 * so so_1_inv = so_1.invert() self.add_and_generate( so_1 ) self.add_and_generate( so_1_inv ) so_2 = so * so_0 so_2_inv = so_2.invert() self.add_and_generate( so_2 ) self.add_and_generate( so_2_inv )
def _save_basic_identity_so( self, dim_0, identity_w_dich_flag ): #identity is a must by the group definition if not identity_w_dich_flag: e = SymmetryOperation( matrix = np.identity( dim_0 )) e.label = 'E' else: e = SymmetryOperationSO3( matrix = np.identity( dim_0 ), dich_operations = set() ) e.label = 'E' self._e_0 = e def _symmetry_operations_check_and_init( self, symmetry_operations={} ): """ Check the consistency of the supplied symmetry operations. Assign the operations when validation succeeds. Args: symmetry_operations: a list or a set of :any:`SymmetryOperation` objects. Raises: TypeError: symmetry operations do not belong to :any:`SymmetryOperation` or its derived classes ValueError: dimensions of symmetry operations do not match Returns: None. """ # make sure, it is a list symmetry_operations = list( symmetry_operations ) # number of symmetry operations given at the init stage n_0 = len( symmetry_operations ) self.identity_w_dich_flag = False if n_0 > 0: # type check if any( not isinstance( so, SymmetryOperation ) for so in symmetry_operations ): raise TypeError('symmetry_operations must contain SymmetryOperation objects only') so_random = next( iter( symmetry_operations ) ) # it is already checked in the SymmetryOperation class that the matrix is square dim_0 = so_random.matrix.shape[0] # dimensions matching: matrix/vector case if dim_0 > 1: if not all( dim_0 == so.matrix.shape[0] for so in symmetry_operations ): raise ValueError('Different dimensions of input symmetry operations') # dimensions matching: scalar case else: if not all( is_scalar( so.matrix ) for so in symmetry_operations ): raise ValueError('Different dimensions of input symmetry operations') # set the number of dimensions of the group's transformation self._symmetry_operations = symmetry_operations if any( isinstance( so, SymmetryOperationO3 ) for so in symmetry_operations ): self.identity_w_dich_flag = True self._save_basic_identity_so( dim_0, self.identity_w_dich_flag ) else: # default 3-dimensional group. Can be changed after reassignment of symmetry_operations dim_0 = 3 self._save_basic_identity_so( dim_0, False ) self._symmetry_operations = [ self._e_0 ]
[docs] def by_label( self, label ): """ Returns the :any:`SymmetryOperation` with a matching label. Args: label (str): The label identifying the chosen symmetry operation. Returns: (:any:`SymmetryOperation`): The symmetry operation that matches this label. """ return next((so for so in self._symmetry_operations if so.label == label), None)
@property def labels( self ): """ List the labels of the symmetry operations in this group. Args: None Returns: (list): A list of label strings. """ return [ so.label for so in self._symmetry_operations ]
[docs] def is_invariant( self, physical_quantity, rtol = 1e-6, atol = 1e-14 ): """ Check whether a physical quantity is invariant under this symmetry group. Args: physical_quantity (PhysicalQuantity): Physical quantity to test. rtol (float): Relative tolerance used for comparisons. atol (float): Absolute tolerance used for comparisons. Raises: TypeError: If physical_quantity is not a PhysicalQuantity. Returns: bool: Whether the quantity is invariant. """ if not isinstance( physical_quantity, PhysicalQuantity ): raise TypeError('physical_quantity must belongs to the class PhysicalQuantity') invariant_flag = True for so in self.symmetry_operations: pq_updated = so * physical_quantity if not np.allclose( pq_updated.value, physical_quantity.value, rtol = rtol ): if not physical_quantity.bidirector: invariant_flag = False elif not np.allclose( pq_updated.value, - physical_quantity.value, rtol = rtol ): invariant_flag = False return invariant_flag
def __repr__( self ): to_return = '{}\n'.format( self.__class__.class_str ) for so in self._symmetry_operations: to_return += "{}\n".format( so ) return to_return def __mul__( self, other ): """ Direct product. """ return SymmetryGroup( [ s1 * s2 for s1, s2 in product( self._symmetry_operations, other.symmetry_operations ) ] )
[docs] class LimitingSymmetryGroupAxial(SymmetryGroup): """ Continuous axial Curie symmetry with compatible finite operations and optional binary reversal labels. The continuous rotations are not enumerated. """ class_str = 'LimitingSymmetryGroupAxial' def __init__( self, axis = [ 1, 0, 0 ], symmetry_operations = [ SymmetryOperationSO3( ) ], label = "" ): """ Create a :any:`LimitingSymmetryGroupAxial` for an axis, vector, line, or plane. Using Hermann-Mauguin notation, it is one of the following limiting Curie groups: ∞ (default), ∞2, ∞/m, ∞m, or ∞/mm. First two describe symmetry of chiral physical objects. Args: axis: the direction of the infinity-fold rotational symmetry axis. Must be either `numpy.ndarray` or `list`. symmetry_operations (list): a list of symmetry operations which supplement ∞. They can be different orientations of 2, m, and, also, identity (always) and a spatial inversion 1-. with (optionally) dichromatic reversals which make the group "grey" for the given property. For instance, 'T' for time-reversal can make ∞/m1'. If dichromatic reversals occur only together with an improper spatial transformation, they may describe chirality that is not invariant under these operations. For example, combining 'T' and 'P' can produce an m' operation in ∞/m'. label (default = "") (str): optional string label for this `SymmetryGroup` object. Raises: ValueError: if the axis is not an invariant of the rest symmetry operations Returns: None """ self._check_and_set_axis( axis = axis ) so_list = list( symmetry_operations ) self._symmetry_operations = so_list self._axial_symmetry_operations_check() super(LimitingSymmetryGroupAxial, self).__init__( symmetry_operations = so_list, label = label) def _check_and_set_axis( self, axis ): axis = deepcopy( axis ) if isinstance( axis, np.ndarray ) or isinstance( axis, list): self._axis = np.array( axis ) / norm( np.array( axis ) ) else: raise TypeError('Not a vector') if not is_3D_vector( self._axis ): raise TypeError('Not a 3D vector') @property def axis( self ): return self._axis @axis.setter def axis( self, value ): """ The direction of the infinity-fold rotational symmetry axis. Args: value: the axis' direction (3D vector). Must be either `numpy.ndarray` or `list`. Raises: TypeError/ValueError: in cases of not a vector / 3D vector. Returns: None """ self._check_and_set_axis( axis = value ) # we need to reinit the group if the axis has been changed self._axial_symmetry_operations_check() # Regenerate the group after changing the axis. super(LimitingSymmetryGroupAxial, self).__init__( symmetry_operations = self.symmetry_operations) # We can generate and assign a label after the axis and symmetry operations have been initialised #self._assign_label() @property def symmetry_operations( self ): """ List of symmetry transformations forming the group. Args: None Returns: List of symmetry operations. """ return self._symmetry_operations @symmetry_operations.setter def symmetry_operations( self, value ): so_list = list( value ) # A twofold rotation is part of the axial limiting group; it could be # added implicitly for the generation of the rest # so_list.append( self._so_2 ) # Regenerate the group after changing the axis. super(LimitingSymmetryGroupAxial, self).__init__( symmetry_operations = so_list) #self._assign_label() # Revalidate the axis after changing the symmetry operations. self._axial_symmetry_operations_check() def _axial_symmetry_operations_check( self ): # let's check that the given axis is an invariant of the rest symmetry operations # The set of symmetry operations has already been initialised. physical_quantity = PhysicalQuantity( value = self.axis, bidirector = True ) invariant_flag = super( LimitingSymmetryGroupAxial, self ).is_invariant( physical_quantity = physical_quantity ) if not invariant_flag: raise ValueError( 'The provided axis is not an invariant of the rest symmetry operations' )
[docs] def is_invariant( self, physical_quantity, rtol = 1e-6, atol = 1e-14 ): """ Check whether a physical quantity is invariant under this symmetry group. Args: physical_quantity (PhysicalQuantity): Physical quantity to test. rtol (float): Relative tolerance used for comparisons. atol (float): Absolute tolerance used for comparisons. Raises: TypeError: If physical_quantity is not a PhysicalQuantity. Returns: bool: Whether the quantity is invariant. """ invariant_flag = super( LimitingSymmetryGroupAxial, self ).is_invariant( physical_quantity = physical_quantity, rtol = rtol, atol = atol ) if invariant_flag: if is_3D_vector( physical_quantity.value ): # collinearity test for the vector cross_product = np.cross( self.axis, physical_quantity.value ) if not np.allclose( cross_product, np.array( np.zeros( (3) ) ), atol = atol): invariant_flag = False elif is_symmetrical_tensor( physical_quantity.value, rtol = rtol, atol = atol ): if not is_scalar_extended( physical_quantity.value ): eigvals, eigvecs_tmp = np.linalg.eigh( physical_quantity.value ) # represent the eigvecs as rows: eigvecs = eigvecs_tmp.transpose() eigvals_abs = np.zeros( 3 ) for i in range( 3 ): eigvals_abs[ i ] = abs( eigvals[ i ] ) max_val = np.max( eigvals_abs ) dec_tol = int( np.log10( max_val * rtol ) ) if dec_tol >= 0: dec_tol = 0 qty_unique_vals = len( np.unique( np.round( eigvals, decimals = - dec_tol ) ) ) # is the tensor axially symmetric? if qty_unique_vals == 2: three_indx = { 0, 1, 2 } for i in range( 2 ): for j in range( i + 1, 3 ): if isclose( eigvals[i], eigvals[j], rel_tol = rtol ): # The remaining eigenvalue identifies the tensor axis. indx_axial = next( iter( three_indx - { i, j } ) ) tensor_axis = eigvecs[ indx_axial ] cross_product = np.cross( self.axis, tensor_axis ) if not np.allclose( cross_product, np.array( np.zeros( (3) ) ), atol = atol): invariant_flag = False elif qty_unique_vals == 3: # anisotropic tensor. Hence, no axial symmetry invariant_flag = False #elif qty_unique_vals == 1: # isotropic tensor, nothing to do -- keep the defined invariance flag elif not is_scalar_extended( physical_quantity.value ): raise ValueError('Non-symmetrical tensor or another unsupported (in this version) type') return invariant_flag
def __repr__( self ): to_return = '{}\n'.format( self.label ) to_return += super( LimitingSymmetryGroupAxial, self ).__repr__() return to_return
[docs] class LimitingSymmetryGroupScalar(LimitingSymmetryGroupAxial): """ Isotropic Curie limiting symmetry for scalar and pseudoscalar quantities, with compatible spatial and binary reversal operations. """ class_str = 'LimitingSymmetryGroupScalar' def __init__( self, scalar_symmetry_operations = [ SymmetryOperationSO3( ) ], label = "" ): """ Create a :any:`LimitingSymmetryGroupScalar` for a scalar or pseudoscalar. Using Hermann-Mauguin notation, it is one of two limiting Curie groups: ∞∞ (default) or ∞∞m. In terms of a physical quantity that is invariant under these groups' transformations, ∞∞m describes a scalar and ∞∞ describes a pseudoscalar. Args: scalar_symmetry_operations (list): a list of identity- or inversion-based symmetry operations with (optionally) dichromatic reversals which make the group "grey" for the given property. For instance, 'T' for time-reversal makes ∞∞1' or ∞∞m1' The group ∞∞m is defined by 'P' reversal, i.e. a spatial inversion. If dichromatic reversals occur only together with an improper spatial transformation, they may describe chirality that is not invariant under these operations. For example, combining 'T' and 'P' can produce an m' operation in ∞∞m'. label (default = "") (str): optional string label for this `SymmetryGroup` object. Returns: None """ self._scalar_symmetry_operations_check( scalar_symmetry_operations = scalar_symmetry_operations) super(LimitingSymmetryGroupScalar, self).__init__( symmetry_operations = scalar_symmetry_operations, label = label ) if label == "": self._assign_label() def _scalar_symmetry_operations_check( self, scalar_symmetry_operations, rtol = 1e-6 ): if not isinstance( scalar_symmetry_operations, list ): raise TypeError('Must be a list of SymmetryOperation objects') for so in scalar_symmetry_operations: if not isinstance( so, SymmetryOperation ): raise TypeError('The objects in the list must belong to SymmetryOperation or its subclasses') n_dim = so.matrix.shape[0] if not ( np.allclose( so.matrix, np.identity( n_dim ), rtol = rtol) or np.allclose( so.matrix, - np.identity( n_dim ), rtol = rtol) ): raise ValueError('Must be an identity or inversion matrix')
[docs] def is_invariant( self, physical_quantity, rtol = 1e-6, atol = 1e-14 ): """ Check whether a physical quantity is invariant under this symmetry group. Args: physical_quantity (PhysicalQuantity): Physical quantity to test. rtol (float): Relative tolerance used for comparisons. atol (float): Absolute tolerance used for comparisons. Raises: TypeError: If physical_quantity is not a PhysicalQuantity. Returns: bool: Whether the quantity is invariant. """ invariant_flag = super( LimitingSymmetryGroupScalar, self ).is_invariant( physical_quantity = physical_quantity, rtol = rtol, atol = atol ) if not is_scalar_extended( physical_quantity.value ): invariant_flag = False return invariant_flag
def _assign_label( self, atol = 1e-14 ): label = '∞∞' for so in self.symmetry_operations: # for O3 and its subgroups if isinstance( so, SymmetryOperationO3 ): if 'P' in so.dich_operations: label += 'm' remaining_dich_set = so.dich_operations - { 'P' } else: if len( so.dich_operations ) > 0: label += '1' remaining_dich_set = so.dich_operations remaining_dich_set = list( remaining_dich_set ) remaining_dich_set.sort() for dich in remaining_dich_set: if dich == 'T': label += "'" elif dich == 'C': label += '*' else: label += dich self.label = label def __repr__( self ): to_return = '' to_return += super( LimitingSymmetryGroupScalar, self ).__repr__() return to_return