SIRIUS 7.5.0
Electronic structure library and applications
rotation.hpp
Go to the documentation of this file.
1// Copyright (c) 2013-2018 Anton Kozhevnikov, Thomas Schulthess
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without modification, are permitted provided that
5// the following conditions are met:
6//
7// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
8// following disclaimer.
9// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
10// and the following disclaimer in the documentation and/or other materials provided with the distribution.
11//
12// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
13// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
14// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
15// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
16// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
17// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
18// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19
20/** \file rotation.hpp
21 *
22 * \brief Generate rotation matrices and related entities.
23 */
24
25#ifndef __ROTATION_HPP__
26#define __ROTATION_HPP__
27
28#include "SDDK/memory.hpp"
29#include "core/r3/r3.hpp"
30#include "core/rte/rte.hpp"
31#include "core/constants.hpp"
32
33namespace sirius {
34
35/// Return angle phi in the range [0, 2Pi) by its values of sin(phi) and cos(phi).
36inline double phi_by_sin_cos(double sinp, double cosp)
37{
38 const double twopi = 6.2831853071795864769;
39 double phi = std::atan2(sinp, cosp);
40 if (phi < 0) {
41 phi += twopi;
42 }
43 return phi;
44}
45
46/// Generate SU(2) rotation matrix from the axes and angle.
47inline auto
48rotation_matrix_su2(std::array<double, 3> u__, double theta__)
49{
51
52 auto cost = std::cos(theta__ / 2);
53 auto sint = std::sin(theta__ / 2);
54
55 rotm(0, 0) = std::complex<double>(cost, -u__[2] * sint);
56 rotm(1, 1) = std::complex<double>(cost, u__[2] * sint);
57 rotm(0, 1) = std::complex<double>(-u__[1] * sint, -u__[0] * sint);
58 rotm(1, 0) = std::complex<double>( u__[1] * sint, -u__[0] * sint);
59
60 return rotm;
61}
62
63/// Generate SU2(2) rotation matrix from a 3x3 rotation matrix in Cartesian coordinates.
64/** Create quaternion components from the 3x3 matrix. The components are just a w = Cos(\Omega/2)
65 * and {x,y,z} = unit rotation vector multiplied by Sin(\Omega/2)
66 *
67 * See https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation
68 * and https://en.wikipedia.org/wiki/Rotation_group_SO(3)#Quaternions_of_unit_norm */
69inline auto
71{
72 double det = R__.det() > 0 ? 1.0 : -1.0;
73
74 r3::matrix<double> mat = R__ * det;
75
77
78 su2mat.zero();
79
80 /* make quaternion components*/
81 double w = std::sqrt(std::max(0.0, 1.0 + mat(0, 0) + mat(1, 1) + mat(2, 2))) / 2.0;
82 double x = std::sqrt(std::max(0.0, 1.0 + mat(0, 0) - mat(1, 1) - mat(2, 2))) / 2.0;
83 double y = std::sqrt(std::max(0.0, 1.0 - mat(0, 0) + mat(1, 1) - mat(2, 2))) / 2.0;
84 double z = std::sqrt(std::max(0.0, 1.0 - mat(0, 0) - mat(1, 1) + mat(2, 2))) / 2.0;
85
86 x = std::copysign(x, mat(2, 1) - mat(1, 2));
87 y = std::copysign(y, mat(0, 2) - mat(2, 0));
88 z = std::copysign(z, mat(1, 0) - mat(0, 1));
89
90 su2mat(0, 0) = std::complex<double>(w, -z);
91 su2mat(1, 1) = std::complex<double>(w, z);
92 su2mat(0, 1) = std::complex<double>(-y, -x);
93 su2mat(1, 0) = std::complex<double>(y, -x);
94
95 return su2mat;
96}
97
98/// Get axis and angle from rotation matrix.
99inline auto
101{
103 /* make proper rotation */
104 R__ = R__ * R__.det();
105 u[0] = R__(2, 1) - R__(1, 2);
106 u[1] = R__(0, 2) - R__(2, 0);
107 u[2] = R__(1, 0) - R__(0, 1);
108
109 double sint = u.length() / 2.0;
110 double cost = (R__(0, 0) + R__(1, 1) + R__(2, 2) - 1) / 2.0;
111
112 double theta = phi_by_sin_cos(sint, cost);
113
114 /* rotation angle is zero */
115 if (std::abs(theta) < 1e-12) {
116 u = {0, 0, 1};
117 } else if (std::abs(theta - pi) < 1e-12) { /* rotation angle is Pi */
118 /* rotation matrix for Pi angle has this form
119
120 [-1+2ux^2 | 2 ux uy | 2 ux uz]
121 [2 ux uy | -1+2uy^2 | 2 uy uz]
122 [2 ux uz | 2 uy uz | -1+2uz^2] */
123
124 if (R__(0, 0) >= R__(1, 1) && R__(0, 0) >= R__(2, 2)) { /* x-component is largest */
125 u[0] = std::sqrt(std::abs(R__(0, 0) + 1) / 2);
126 u[1] = (R__(0, 1) + R__(1, 0)) / 4 / u[0];
127 u[2] = (R__(0, 2) + R__(2, 0)) / 4 / u[0];
128 } else if (R__(1, 1) >= R__(0, 0) && R__(1, 1) >= R__(2, 2)) { /* y-component is largest */
129 u[1] = std::sqrt(std::abs(R__(1, 1) + 1) / 2);
130 u[0] = (R__(1, 0) + R__(0, 1)) / 4 / u[1];
131 u[2] = (R__(1, 2) + R__(2, 1)) / 4 / u[1];
132 } else {
133 u[2] = std::sqrt(std::abs(R__(2, 2) + 1) / 2);
134 u[0] = (R__(2, 0) + R__(0, 2)) / 4 / u[2];
135 u[1] = (R__(2, 1) + R__(1, 2)) / 4 / u[2];
136 }
137 } else {
138 u = u * (1.0 / u.length());
139 }
140
141 return std::pair<r3::vector<double>, double>(u, theta);
142}
143
144/// Generate rotation matrix from three Euler angles
145/** Euler angles \f$ \alpha, \beta, \gamma \f$ define the general rotation as three consecutive rotations:
146 * - about \f$ \hat e_z \f$ through the angle \f$ \gamma \f$ (\f$ 0 \le \gamma < 2\pi \f$)
147 * - about \f$ \hat e_y \f$ through the angle \f$ \beta \f$ (\f$ 0 \le \beta \le \pi \f$)
148 * - about \f$ \hat e_z \f$ through the angle \f$ \alpha \f$ (\f$ 0 \le \gamma < 2\pi \f$)
149 *
150 * The total rotation matrix is defined as a product of three rotation matrices:
151 * \f[
152 * R(\alpha, \beta, \gamma) =
153 * \left( \begin{array}{ccc} \cos(\alpha) & -\sin(\alpha) & 0 \\
154 * \sin(\alpha) & \cos(\alpha) & 0 \\
155 * 0 & 0 & 1 \end{array} \right)
156 * \left( \begin{array}{ccc} \cos(\beta) & 0 & \sin(\beta) \\
157 * 0 & 1 & 0 \\
158 * -\sin(\beta) & 0 & \cos(\beta) \end{array} \right)
159 * \left( \begin{array}{ccc} \cos(\gamma) & -\sin(\gamma) & 0 \\
160 * \sin(\gamma) & \cos(\gamma) & 0 \\
161 * 0 & 0 & 1 \end{array} \right) =
162 * \left( \begin{array}{ccc} \cos(\alpha) \cos(\beta) \cos(\gamma) - \sin(\alpha) \sin(\gamma) &
163 * -\sin(\alpha) \cos(\gamma) - \cos(\alpha) \cos(\beta) \sin(\gamma) &
164 * \cos(\alpha) \sin(\beta) \\
165 * \sin(\alpha) \cos(\beta) \cos(\gamma) + \cos(\alpha) \sin(\gamma) &
166 * \cos(\alpha) \cos(\gamma) - \sin(\alpha) \cos(\beta) \sin(\gamma) &
167 * \sin(\alpha) \sin(\beta) \\
168 * -\sin(\beta) \cos(\gamma) &
169 * \sin(\beta) \sin(\gamma) &
170 * \cos(\beta) \end{array} \right)
171 * \f]
172 */
173inline auto
175{
176 double alpha = euler_angles__[0];
177 double beta = euler_angles__[1];
178 double gamma = euler_angles__[2];
179
181 rm(0, 0) = std::cos(alpha) * std::cos(beta) * std::cos(gamma) - std::sin(alpha) * std::sin(gamma);
182 rm(0, 1) = -std::cos(gamma) * std::sin(alpha) - std::cos(alpha) * std::cos(beta) * std::sin(gamma);
183 rm(0, 2) = std::cos(alpha) * std::sin(beta);
184 rm(1, 0) = std::cos(beta) * std::cos(gamma) * std::sin(alpha) + std::cos(alpha) * std::sin(gamma);
185 rm(1, 1) = std::cos(alpha) * std::cos(gamma) - std::cos(beta) * std::sin(alpha) * std::sin(gamma);
186 rm(1, 2) = std::sin(alpha) * std::sin(beta);
187 rm(2, 0) = -std::cos(gamma) * std::sin(beta);
188 rm(2, 1) = std::sin(beta) * std::sin(gamma);
189 rm(2, 2) = std::cos(beta);
190
191 return rm;
192}
193
194/// Compute Euler angles corresponding to the proper rotation matrix.
195inline auto
196euler_angles(r3::matrix<double> const& rot__, double tolerance__)
197{
198 r3::vector<double> angles(0, 0, 0);
199
200 if (std::abs(rot__.det() - 1) > 1e-10) {
201 std::stringstream s;
202 s << "determinant of rotation matrix is " << rot__.det();
203 RTE_THROW(s);
204 }
205
206 auto rit = inverse(transpose(rot__));
207 for (int i = 0; i < 3; i++) {
208 for (int j = 0; j < 3; j++) {
209 if (std::abs(rot__(i, j) - rit(i, j)) > tolerance__) {
210 std::stringstream s;
211 s << "rotation matrix is not unitary" << std::endl
212 << "initial symmetry matrix:" << std::endl
213 << rot__ << std::endl
214 << "inverse transpose matrix:" << std::endl
215 << rit;
216 RTE_THROW(s);
217 }
218 }
219 }
220
221 if (std::abs(rot__(2, 2) - 1.0) < 1e-10) { // cos(beta) == 1, beta = 0
222 angles[0] = phi_by_sin_cos(rot__(1, 0), rot__(0, 0));
223 } else if (std::abs(rot__(2, 2) + 1.0) < 1e-10) { // cos(beta) == -1, beta = Pi
224 angles[0] = phi_by_sin_cos(-rot__(0, 1), rot__(1, 1));
225 angles[1] = pi;
226 } else {
227 double beta = std::acos(rot__(2, 2));
228 angles[0] = phi_by_sin_cos(rot__(1, 2) / std::sin(beta), rot__(0, 2) / std::sin(beta));
229 angles[1] = beta;
230 angles[2] = phi_by_sin_cos(rot__(2, 1) / std::sin(beta), -rot__(2, 0) / std::sin(beta));
231 }
232
233 auto rm1 = rot_mtrx_cart(angles);
234
235 for (int i = 0; i < 3; i++) {
236 for (int j = 0; j < 3; j++) {
237 if (std::abs(rot__(i, j) - rm1(i, j)) > tolerance__) {
238 std::stringstream s;
239 s << "matrices don't match" << std::endl
240 << "initial symmetry matrix: " << std::endl
241 << rot__ << std::endl
242 << "euler angles : " << angles[0] / pi << " " << angles[1] / pi << " " << angles[2] / pi << std::endl
243 << "computed symmetry matrix : " << std::endl
244 << rm1;
245 RTE_THROW(s);
246 }
247 }
248 }
249
250 return angles;
251}
252
253}
254
255#endif // __ROTATION_HPP__
T det() const
Return determinant of a matrix.
Definition: r3.hpp:358
double length() const
Return vector length (L2 norm).
Definition: r3.hpp:126
Multidimensional array with the column-major (Fortran) order.
Definition: memory.hpp:660
void zero(memory_t mem__, size_t idx0__, size_t n__)
Zero n elements starting from idx0.
Definition: memory.hpp:1316
Various constants.
Memory management functions and classes.
auto transpose(matrix< T > src)
Return transpose of the matrix.
Definition: r3.hpp:445
auto inverse(matrix< int > src)
Return inverse of the integer matrix.
Definition: r3.hpp:475
Namespace of the SIRIUS library.
Definition: sirius.f90:5
auto euler_angles(r3::matrix< double > const &rot__, double tolerance__)
Compute Euler angles corresponding to the proper rotation matrix.
Definition: rotation.hpp:196
auto rotation_matrix_su2(std::array< double, 3 > u__, double theta__)
Generate SU(2) rotation matrix from the axes and angle.
Definition: rotation.hpp:48
auto rot_mtrx_cart(r3::vector< double > euler_angles__)
Generate rotation matrix from three Euler angles.
Definition: rotation.hpp:174
const double twopi
Definition: constants.hpp:45
double phi_by_sin_cos(double sinp, double cosp)
Return angle phi in the range [0, 2Pi) by its values of sin(phi) and cos(phi).
Definition: rotation.hpp:36
const double pi
Definition: constants.hpp:42
auto axis_angle(r3::matrix< double > R__)
Get axis and angle from rotation matrix.
Definition: rotation.hpp:100
Simple classes and functions to work with vectors and matrices of the R^3 space.
Eror and warning handling during run-time execution.