1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
//! Transformations under symmetry operations.
use std::error::Error;
use std::fmt;
use nalgebra::Vector3;
use ndarray::{Array, Array2, Axis, RemoveAxis};
use num_complex::Complex;
#[cfg(feature = "python")]
use pyo3::prelude::*;
use serde::{Deserialize, Serialize};
use crate::angmom::sh_conversion::{sh_cart2r, sh_r2cart};
use crate::angmom::sh_rotation_3d::rlmat;
use crate::angmom::spinor_rotation_3d::dmat_angleaxis;
use crate::basis::ao::{BasisAngularOrder, CartOrder, PureOrder, ShellOrder};
use crate::permutation::{PermutableCollection, Permutation};
use crate::symmetry::symmetry_element::symmetry_operation::{
SpecialSymmetryTransformation, SymmetryOperation,
};
#[cfg(test)]
#[path = "symmetry_transformation_tests.rs"]
mod symmetry_transformation_tests;
// ================
// Enum definitions
// ================
/// Enumerated type for managing the kind of symmetry transformation on an object.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "python", pyclass)]
pub enum SymmetryTransformationKind {
/// Spatial-only transformation.
Spatial,
/// Spatial-only transformation but with spin-including time reversal.
SpatialWithSpinTimeReversal,
/// Spin-only transformation.
Spin,
/// Spin-spatial coupled transformation.
SpinSpatial,
}
impl Default for SymmetryTransformationKind {
fn default() -> Self {
SymmetryTransformationKind::Spatial
}
}
impl fmt::Display for SymmetryTransformationKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Spatial => write!(f, "Spatial-only transformation"),
Self::SpatialWithSpinTimeReversal => write!(
f,
"Spatial-only transformation but with spin-including time reversal"
),
Self::Spin => write!(f, "Spin-only transformation"),
Self::SpinSpatial => write!(f, "Spin-spatial coupled transformation"),
}
}
}
// =================
// Trait definitions
// =================
#[derive(Debug, Clone)]
pub struct TransformationError(pub String);
impl fmt::Display for TransformationError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Transformation error: {}", self.0)
}
}
impl Error for TransformationError {}
/// Trait for spatial unitary transformation. A spatial unitary transformation also permutes
/// off-origin sites.
pub trait SpatialUnitaryTransformable: Clone {
// ----------------
// Required methods
// ----------------
/// Performs a spatial transformation in-place.
///
/// # Arguments
///
/// * `rmat` - The three-dimensional representation matrix of the transformation in the basis
/// of coordinate *functions* $`(y, z, x)`$.
/// * `perm` - An optional permutation describing how any off-origin sites are permuted amongst
/// each other under the transformation.
fn transform_spatial_mut(
&mut self,
rmat: &Array2<f64>,
perm: Option<&Permutation<usize>>,
) -> Result<&mut Self, TransformationError>;
// ----------------
// Provided methods
// ----------------
/// Performs a spatial transformation and returns the transformed result.
///
/// # Arguments
///
/// * `rmat` - The three-dimensional representation matrix of the transformation in the basis
/// of coordinate *functions* $`(y, z, x)`$.
///
/// # Returns
///
/// The transformed result.
fn transform_spatial(
&self,
rmat: &Array2<f64>,
perm: Option<&Permutation<usize>>,
) -> Result<Self, TransformationError> {
let mut tself = self.clone();
tself.transform_spatial_mut(rmat, perm)?;
Ok(tself)
}
}
/// Trait for spin unitary transformations. A spin unitary transformation has no spatial effects.
pub trait SpinUnitaryTransformable: Clone {
// ----------------
// Required methods
// ----------------
/// Performs a spin transformation in-place.
///
/// # Arguments
///
/// * `dmat` - The two-dimensional representation matrix of the transformation in the basis of
/// the $`\{ \alpha, \beta \}`$ spinors (*i.e.* decreasing $`m`$ order).
fn transform_spin_mut(
&mut self,
dmat: &Array2<Complex<f64>>,
) -> Result<&mut Self, TransformationError>;
// ----------------
// Provided methods
// ----------------
/// Performs a spin transformation and returns the transformed result.
///
/// # Arguments
///
/// * `dmat` - The two-dimensional representation matrix of the transformation in the basis of
/// the $`\{ \alpha, \beta \}`$ spinors (*i.e.* decreasing $`m`$ order).
///
/// # Returns
///
/// The transformed result.
fn transform_spin(&self, dmat: &Array2<Complex<f64>>) -> Result<Self, TransformationError> {
let mut tself = self.clone();
tself.transform_spin_mut(dmat)?;
Ok(tself)
}
}
/// Trait for complex-conjugation transformations.
pub trait ComplexConjugationTransformable: Clone {
// ----------------
// Required methods
// ----------------
/// Performs a complex conjugation in-place.
fn transform_cc_mut(&mut self) -> Result<&mut Self, TransformationError>;
// ----------------
// Provided methods
// ----------------
/// Performs a complex conjugation and returns the complex-conjugated result.
///
/// # Returns
///
/// The complex-conjugated result.
fn transform_cc(&self) -> Result<Self, TransformationError> {
let mut tself = self.clone();
tself.transform_cc_mut()?;
Ok(tself)
}
}
/// Trait for time-reversal transformations.
///
/// This trait has a blanket implementation for any implementor of the [`SpinUnitaryTransformable`]
/// trait and the [`ComplexConjugationTransformable`] trait together with the
/// [`DefaultTimeReversalTransformable`] marker trait.
pub trait TimeReversalTransformable: ComplexConjugationTransformable {
// ----------------
// Required methods
// ----------------
/// Performs a time-reversal transformation in-place.
fn transform_timerev_mut(&mut self) -> Result<&mut Self, TransformationError>;
// ----------------
// Provided methods
// ----------------
/// Performs a time-reversal transformation and returns the time-reversed result.
///
/// # Returns
///
/// The time-reversed result.
fn transform_timerev(&self) -> Result<Self, TransformationError> {
let mut tself = self.clone();
tself.transform_timerev_mut()?;
Ok(tself)
}
}
// ----------------------
// Blanket implementation
// ----------------------
/// Marker trait indicating that the implementing type should get the blanket implementation for
/// [`TimeReversalTransformable`].
pub trait DefaultTimeReversalTransformable {}
impl<T> TimeReversalTransformable for T
where
T: DefaultTimeReversalTransformable
+ SpinUnitaryTransformable
+ ComplexConjugationTransformable,
{
/// Performs a time-reversal transformation in-place.
///
/// The default implementation of the time-reversal transformation for any type that implements
/// [`SpinUnitaryTransformable`] and [`ComplexConjugationTransformable`] is a spin rotation by
/// $`\pi`$ about the space-fixed $`y`$-axis followed by a complex conjugation.
fn transform_timerev_mut(&mut self) -> Result<&mut Self, TransformationError> {
let dmat_y = dmat_angleaxis(std::f64::consts::PI, Vector3::y(), false);
self.transform_spin_mut(&dmat_y)?.transform_cc_mut()
}
}
/// Trait for transformations using [`SymmetryOperation`].
pub trait SymmetryTransformable:
SpatialUnitaryTransformable + SpinUnitaryTransformable + TimeReversalTransformable
{
// ----------------
// Required methods
// ----------------
/// Determines the permutation of sites (*e.g.* atoms in molecules) due to the action of a
/// symmetry operation.
///
/// # Arguments
///
/// * `symop` - A symmetry operation.
///
/// # Returns
///
/// The resultant site permutation under the action of `symop`, or an error if no such
/// permutation can be found.
fn sym_permute_sites_spatial(
&self,
symop: &SymmetryOperation,
) -> Result<Permutation<usize>, TransformationError>;
// ----------------
// Provided methods
// ----------------
/// Performs a spatial transformation according to a specified symmetry operation in-place.
///
/// Note that both $`\mathsf{SO}(3)`$ and $`\mathsf{SU}(2)`$ rotations effect the same spatial
/// transformation. Also note that, if the transformation contains time reversal, it will be
/// accompanied by a complex conjugation.
///
/// # Arguments
///
/// * `symop` - A symmetry operation.
fn sym_transform_spatial_mut(
&mut self,
symop: &SymmetryOperation,
) -> Result<&mut Self, TransformationError> {
let rmat = symop.get_3d_spatial_matrix();
let perm = self.sym_permute_sites_spatial(symop)?;
self.transform_spatial_mut(&rmat, Some(&perm))
.map_err(|err| TransformationError(err.to_string()))?;
if symop.contains_time_reversal() {
self.transform_cc_mut()
} else {
Ok(self)
}
}
/// Performs a spatial transformation according to a specified symmetry operation and returns
/// the transformed result.
///
/// Note that both $`\mathsf{SO}(3)`$ and $`\mathsf{SU}(2)`$ rotations effect the same spatial
/// transformation. Also note that, if the transformation contains time reversal, it will be
/// accompanied by a complex conjugation.
///
/// # Arguments
///
/// * `symop` - A symmetry operation.
///
/// # Returns
///
/// The transformed result.
fn sym_transform_spatial(
&self,
symop: &SymmetryOperation,
) -> Result<Self, TransformationError> {
let mut tself = self.clone();
tself.sym_transform_spatial_mut(symop)?;
Ok(tself)
}
/// Performs a spatial transformation according to a specified symmetry operation in-place, but
/// with spin-including time reversal.
///
/// Note that both $`\mathsf{SO}(3)`$ and $`\mathsf{SU}(2)`$ rotations effect the same spatial
/// transformation. Also note that, if the transformation contains time reversal, it will be
/// accompanied by a rotation by $`\pi`$ about the space-fixed $`y`$-axis followed by a complex
/// conjugation.
///
/// # Arguments
///
/// * `symop` - A symmetry operation.
fn sym_transform_spatial_with_spintimerev_mut(
&mut self,
symop: &SymmetryOperation,
) -> Result<&mut Self, TransformationError> {
let rmat = symop.get_3d_spatial_matrix();
let perm = self.sym_permute_sites_spatial(symop)?;
self.transform_spatial_mut(&rmat, Some(&perm))
.map_err(|err| TransformationError(err.to_string()))?;
if symop.contains_time_reversal() {
self.transform_timerev_mut()?;
}
Ok(self)
}
/// Performs a spatial transformation according to a specified symmetry operation but with
/// spin-including time reversal and returns the transformed result.
///
/// Note that both $`\mathsf{SO}(3)`$ and $`\mathsf{SU}(2)`$ rotations effect the same spatial
/// transformation. Also note that, if the transformation contains time reversal, it will be
/// accompanied by a rotation by $`\pi`$ about the space-fixed $`y`$-axis followed by a complex
/// conjugation.
///
/// # Arguments
///
/// * `symop` - A symmetry operation.
///
/// # Returns
///
/// The transformed result.
fn sym_transform_spatial_with_spintimerev(
&self,
symop: &SymmetryOperation,
) -> Result<Self, TransformationError> {
let mut tself = self.clone();
tself.sym_transform_spatial_with_spintimerev_mut(symop)?;
Ok(tself)
}
/// Performs a spin transformation according to a specified symmetry operation in-place.
///
/// Note that only $`\mathsf{SU}(2)`$ rotations can effect spin transformations. Also note
/// that, if the transformation contains a time reversal, the corresponding explicit time
/// reveral action will also be carried out.
///
/// # Arguments
///
/// * `symop` - A symmetry operation.
fn sym_transform_spin_mut(
&mut self,
symop: &SymmetryOperation,
) -> Result<&mut Self, TransformationError> {
if symop.is_su2() {
let angle = symop.calc_pole_angle();
let axis = symop.calc_pole().coords;
let dmat = if symop.is_su2_class_1() {
-dmat_angleaxis(angle, axis, false)
} else {
dmat_angleaxis(angle, axis, false)
};
self.transform_spin_mut(&dmat)?;
}
if symop.contains_time_reversal() {
self.transform_timerev_mut()?;
}
Ok(self)
}
/// Performs a spin transformation according to a specified symmetry operation and returns the
/// transformed result.
///
/// Note that only $`\mathsf{SU}(2)`$ rotations can effect spin transformations. Also note
/// that, if the transformation is antiunitary, it will be accompanied by a time reversal.
///
/// # Arguments
///
/// * `symop` - A symmetry operation.
///
/// # Returns
///
/// The transformed result.
fn sym_transform_spin(&self, symop: &SymmetryOperation) -> Result<Self, TransformationError> {
let mut tself = self.clone();
tself.sym_transform_spin_mut(symop)?;
Ok(tself)
}
/// Performs a coupled spin-spatial transformation according to a specified symmetry operation
/// in-place.
///
/// Note that only $`\mathsf{SU}(2)`$ rotations can effect spin transformations.
///
/// # Arguments
///
/// * `symop` - A symmetry operation.
fn sym_transform_spin_spatial_mut(
&mut self,
symop: &SymmetryOperation,
) -> Result<&mut Self, TransformationError> {
// We cannot do the following, because each of the two methods carries out its own
// antiunitary action, so we'd be double-acting the antiunitary action.
// self.sym_transform_spatial_mut(symop)?
// .sym_transform_spin_mut(symop)
// Spatial
let rmat = symop.get_3d_spatial_matrix();
let perm = self.sym_permute_sites_spatial(symop)?;
self.transform_spatial_mut(&rmat, Some(&perm))
.map_err(|err| TransformationError(err.to_string()))?;
// Spin -- only SU(2) rotations can effect spin transformations.
if symop.is_su2() {
let angle = symop.calc_pole_angle();
let axis = symop.calc_pole().coords;
let dmat = if symop.is_su2_class_1() {
-dmat_angleaxis(angle, axis, false)
} else {
dmat_angleaxis(angle, axis, false)
};
self.transform_spin_mut(&dmat)?;
}
// Time reversal, if any.
if symop.contains_time_reversal() {
self.transform_timerev_mut()?;
}
Ok(self)
}
/// Performs a coupled spin-spatial transformation according to a specified symmetry operation
/// and returns the transformed result.
///
/// Note that only $`\mathsf{SU}(2)`$ rotations can effect spin transformations.
///
/// # Arguments
///
/// * `symop` - A symmetry operation.
///
/// # Returns
///
/// The transformed result.
fn sym_transform_spin_spatial(
&self,
symop: &SymmetryOperation,
) -> Result<Self, TransformationError> {
let mut tself = self.clone();
tself.sym_transform_spin_spatial_mut(symop)?;
Ok(tself)
}
}
// =========
// Functions
// =========
/// Permutes the generalised rows of an array along one or more dimensions.
///
/// Each generalised row corresponds to a basis function, and consecutive generalised rows
/// corresponding to basis functions localised on a single atom are grouped together and then
/// permuted according to the permutation of the atoms.
///
/// # Arguments
///
/// * `arr` - A coefficient array of any dimensions.
/// * `atom_perm` - A permutation for the atoms.
/// * `axes` - The dimensions along which the generalised rows are to be permuted. The number of
/// generalised rows along each of these dimensions *must* be equal to the number of functions in
/// the basis.
/// * `bao` - A structure specifying the angular order of the underlying basis.
///
/// # Returns
///
/// The permuted array.
///
/// # Panics
///
/// Panics if the number of generalised rows along any of the dimensions in `axes` does not match
/// the number of functions in the basis, or if the permutation rank does not match the number of
/// atoms in the basis.
pub(crate) fn permute_array_by_atoms<T, D>(
arr: &Array<T, D>,
atom_perm: &Permutation<usize>,
axes: &[Axis],
bao: &BasisAngularOrder,
) -> Array<T, D>
where
D: RemoveAxis,
T: Clone,
{
assert_eq!(
atom_perm.rank(),
bao.n_atoms(),
"The rank of permutation does not match the number of atoms in the basis."
);
let atom_boundary_indices = bao.atom_boundary_indices();
let permuted_shell_indices: Vec<usize> = atom_perm
.image()
.iter()
.flat_map(|&i| {
let (shell_min, shell_max) = atom_boundary_indices[i];
shell_min..shell_max
})
.collect();
let mut r = arr.clone();
for axis in axes {
assert_eq!(
arr.shape()[axis.0],
bao.n_funcs(),
"The number of generalised rows along {axis:?} in the given array does not match the number of basis functions, {}.", bao.n_funcs()
);
r = r.select(*axis, &permuted_shell_indices);
}
r
}
/// Assembles spherical-harmonic rotation matrices for all shells.
///
/// # Arguments
///
/// * `bao` - A structure specifying the angular order of the underlying basis.
/// * `rmat` - The three-dimensional representation matrix of the transformation in the basis
/// of coordinate *functions* $`(y, z, x)`$.
/// * `perm` - An optional permutation describing how any off-origin sites are permuted amongst
/// each other under the transformation.
///
/// # Returns
///
/// A vector of spherical-harmonic rotation matrices, one for each shells in `bao`. Non-standard
/// orderings of functions in shells are taken into account.
pub(crate) fn assemble_sh_rotation_3d_matrices(
bao: &BasisAngularOrder,
rmat: &Array2<f64>,
perm: Option<&Permutation<usize>>,
) -> Result<Vec<Array2<f64>>, anyhow::Error> {
let pbao = if let Some(p) = perm {
bao.permute(p)?
} else {
bao.clone()
};
let mut rls = vec![Array2::<f64>::eye(1), rmat.clone()];
let lmax = pbao
.basis_shells()
.map(|shl| shl.l)
.max()
.expect("The maximum angular momentum cannot be found.");
for l in 2..=lmax {
let rl = rlmat(
l,
rmat,
rls.last()
.expect("The representation matrix for the last angular momentum cannot be found."),
);
rls.push(rl);
}
// All matrices in `rls` are in increasing-m order by default. See the function `rlmat` for
// the origin of this order. Hence, conversion matrices must also honour this.
let cart2rss_lex: Vec<Vec<Array2<f64>>> = (0..=lmax)
.map(|lcart| sh_cart2r(lcart, &CartOrder::lex(lcart), true, PureOrder::increasingm))
.collect();
let r2cartss_lex: Vec<Vec<Array2<f64>>> = (0..=lmax)
.map(|lcart| sh_r2cart(lcart, &CartOrder::lex(lcart), true, PureOrder::increasingm))
.collect();
let rmats = pbao.basis_shells()
.map(|shl| {
let l = usize::try_from(shl.l).unwrap_or_else(|_| {
panic!(
"Unable to convert the angular momentum order `{}` to `usize`.",
shl.l
);
});
let po_il = PureOrder::increasingm(shl.l);
match &shl.shell_order {
ShellOrder::Pure(pureorder) => {
// Spherical functions.
let rl = rls[l].clone();
if *pureorder != po_il {
// `rl` is in increasing-m order by default. See the function `rlmat` for
// the origin of this order.
let perm = pureorder
.get_perm_of(&po_il)
.expect("Unable to obtain the permutation that maps `pureorder` to the increasing order.");
rl.select(Axis(0), &perm.image()).select(Axis(1), &perm.image())
} else {
rl
}
}
ShellOrder::Cart(cart_order) => {
// Cartesian functions. Convert them to real solid harmonics first, then
// applying the transformation, then convert back.
// The actual Cartesian order will be taken into account.
// Perform the conversion using lexicographic order first. This allows for the
// conversion matrices to be computed only once in the lexicographic order.
let cart2rs = &cart2rss_lex[l];
let r2carts = &r2cartss_lex[l];
let rl = cart2rs.iter().zip(r2carts.iter()).enumerate().fold(
Array2::zeros((cart_order.ncomps(), cart_order.ncomps())),
|acc, (i, (xmat, wmat))| {
let lpure = l - 2 * i;
acc + wmat.dot(&rls[lpure]).dot(xmat)
},
);
let lex_cart_order = CartOrder::lex(shl.l);
// Now deal with the actual Cartesian order by permutations.
if *cart_order != lex_cart_order {
// `rl` is in lexicographic order (because of `wmat` and `xmat`) by default.
// Consider a transformation R and its representation matrix D in a
// lexicographically-ordered Cartesian basis b collected in a row vector.
// Then,
// R b = b D.
// If we now permute the basis functions in b by a permutation π, then the
// representation matrix for R changes:
// R πb = πb D(π).
// To relate D(π) to D, we first note the representation matrix for π, P:
// πb = π b = b P,
// which, when acts on a left row vector, permutes its entries normally, but
// when acts on a right column vector, permutes its entries inversely.
// Then,
// R πb = R b P = b P D(π) => R b = b PD(π)P^(-1).
// Thus,
// D(π) = P^(-1)DP,
// i.e., to obtain D(π), we permute the rows and columns of D normally
// according to π.
let perm = lex_cart_order
.get_perm_of(cart_order)
.unwrap_or_else(
|| panic!("Unable to find a permutation to map `{lex_cart_order}` to `{cart_order}`.")
);
rl.select(Axis(0), perm.image())
.select(Axis(1), perm.image())
} else {
rl
}
}
}
})
.collect::<Vec<Array2<f64>>>();
Ok(rmats)
}