ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
rotation.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2026 The ESPResSo project
3 * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2010
4 * Max-Planck-Institute for Polymer Research, Theory Group
5 *
6 * This file is part of ESPResSo.
7 *
8 * ESPResSo is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * ESPResSo is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#pragma once
23
24/** \file
25 * This file contains all subroutines required to process rotational motion.
26 */
27
28#include <config/config.hpp>
29
30#ifdef ESPRESSO_ROTATION
31
32#include "Particle.hpp"
33#include "ParticleRange.hpp"
34
35#include <utils/Vector.hpp>
36#include <utils/mask.hpp>
38#include <utils/matrix.hpp>
39#include <utils/quaternion.hpp>
40
41#include <cassert>
42#include <cmath>
43#include <limits>
44#include <utility>
45
46/** @brief Propagate angular velocities and update quaternions on a
47 * particle.
48 */
49void propagate_omega_quat_particle(Particle &p, double time_step);
50
51void convert_torque_propagate_omega(Particle &p, double time_step);
52
53// Frame conversion routines
55 Utils::Vector3d const &vec) {
56 return quat * vec;
57}
58
60 Utils::Vector3d const &vec) {
61 assert(quat.norm() > 0.0);
62 return rotation_matrix(quat).transposed() * vec;
63}
64
66 auto const torque = convert_vector_space_to_body(p.quat(), p.torque());
67 p.torque() = Utils::mask(p.rotation(), torque);
68}
69
70/**
71 * @brief Transform matrix from body- to space-fixed frame.
72 *
73 * Given a linear map represented by \f$ A \in \mathbb{R}^{3 \times 3}\f$
74 * in the body-fixed frame, this returns the matrix \f$ A \in \mathbb{R}^{3
75 * \times 3}\f$ representing the map in the space-fixed frame. They are related
76 * by the map between the space-fixed and body-fixed frame \f$O\f$ like
77 *
78 * \f[
79 * A' = O A O^T.
80 * \f]
81 *
82 * @tparam T Scalar type
83 * @param quat quaternion to transform from, i.e. the rotation
84 * that transforms space- to body-fixed frame.
85 * @param A Matrix representation in body-fixed coordinates.
86 * @return Matrix representation in space-fixed coordinates.
87 */
88template <class T>
90 const Utils::Matrix<T, 3, 3> &A) {
91 auto const O = rotation_matrix(quat);
92 return O * A * O.transposed();
93}
94
95/**
96 * @brief Transform matrix from body- to space-fixed frame.
97 * @tparam T Scalar type
98 * @param p Particle transforming from.
99 * @param A Matrix representation in body-fixed coordinates.
100 * @return Matrix representation in space-fixed coordinates.
101 */
102template <class T>
104 return convert_body_to_space(p.quat(), A);
105}
106
107#ifdef ESPRESSO_DIPOLES
108
109/** convert a dipole moment to quaternions and dipolar strength */
110inline std::pair<Utils::Quaternion<double>, double>
113 return {quat, dip.norm()};
114}
115
116#endif
117
118/** Rotate the particle p around the body-frame defined NORMALIZED axis
119 * @p aBodyFrame by amount @p phi.
120 */
123 const Utils::Vector3d &axis_body_frame,
124 const double phi) {
125 // Rotation turned off entirely?
126 if (!p.can_rotate())
127 return p.quat();
128 if (std::abs(phi) > std::numeric_limits<double>::epsilon())
129 return p.quat() *
130 boost::qvm::rot_quat(mask(p.rotation(), axis_body_frame), phi);
131 return p.quat();
132}
133
134/** Rotate the particle p around the NORMALIZED axis aSpaceFrame by amount phi
135 */
137 const Utils::Vector3d &axis_space_frame,
138 const double phi) {
139 if (std::abs(phi) > std::numeric_limits<double>::epsilon()) {
140 // Convert rotation axis to body-fixed frame
141 auto const axis = convert_vector_space_to_body(p.quat(), axis_space_frame);
142 p.quat() = local_rotate_particle_body(p, axis, phi);
143 }
144}
145
146#endif // ESPRESSO_ROTATION
Vector implementation and trait types for boost qvm interoperability.
T norm() const
Definition Vector.hpp:174
Quaternion algebra.
Matrix implementation and trait types for boost qvm interoperability.
Quaternion< T > convert_director_to_quaternion(Vector< T, 3 > const &d)
Convert director to quaternion.
VectorXd< 3 > Vector3d
Definition Vector.hpp:207
T mask(std::unsigned_integral auto mask, T const &t)
Pick elements of a tuple-like by a bit mask.
Definition mask.hpp:55
Quaternion implementation and trait types for boost qvm interoperability.
auto convert_body_to_space(const Utils::Quaternion< double > &quat, const Utils::Matrix< T, 3, 3 > &A)
Transform matrix from body- to space-fixed frame.
Definition rotation.hpp:89
void convert_torque_to_body_frame_apply_fix(Particle &p)
Definition rotation.hpp:65
void propagate_omega_quat_particle(Particle &p, double time_step)
Propagate angular velocities and update quaternions on a particle.
Definition rotation.cpp:129
void convert_torque_propagate_omega(Particle &p, double time_step)
Definition rotation.cpp:163
auto convert_vector_space_to_body(Utils::Quaternion< double > const &quat, Utils::Vector3d const &vec)
Definition rotation.hpp:59
std::pair< Utils::Quaternion< double >, double > convert_dip_to_quat(const Utils::Vector3d &dip)
convert a dipole moment to quaternions and dipolar strength
Definition rotation.hpp:111
auto convert_vector_body_to_space(Utils::Quaternion< double > const &quat, Utils::Vector3d const &vec)
Definition rotation.hpp:54
Utils::Quaternion< double > local_rotate_particle_body(Particle const &p, const Utils::Vector3d &axis_body_frame, const double phi)
Rotate the particle p around the body-frame defined NORMALIZED axis aBodyFrame by amount phi.
Definition rotation.hpp:122
void local_rotate_particle(Particle &p, const Utils::Vector3d &axis_space_frame, const double phi)
Rotate the particle p around the NORMALIZED axis aSpaceFrame by amount phi.
Definition rotation.hpp:136
Struct holding all information for one particle.
Definition Particle.hpp:436
constexpr auto const & quat() const
Definition Particle.hpp:527
constexpr bool can_rotate() const
Definition Particle.hpp:507
constexpr auto const & rotation() const
Definition Particle.hpp:505
constexpr auto const & torque() const
Definition Particle.hpp:529
Matrix representation with static size.
Definition matrix.hpp:68
Matrix< T, Cols, Rows > transposed() const
Retrieve a transposed copy of the matrix.
Definition matrix.hpp:192
Quaternion representation.
value_type norm() const
Retrieve the norm of the quaternion.