ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
coordinate_transformation.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2022 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19#ifndef UTILS_COORDINATE_TRANSFORMATION_HPP
20#define UTILS_COORDINATE_TRANSFORMATION_HPP
21
22/**
23 * @file
24 * Convert coordinates from the Cartesian system to the cylindrical system.
25 * The transformation functions are provided with three overloads:
26 * - one function for the trivial Cartesian <-> cylindrical transformation
27 * - one function to transform from/to a cylindrical system with custom axis
28 * (extra @p axis argument, keep in mind the angle phi is under-defined)
29 * - one function to transform from/to an oriented cylindrical system with
30 * custom axis (extra @p orientation argument, the angle phi is well-defined)
31 */
32
33#include "utils/Vector.hpp"
34#include "utils/constants.hpp"
36#include "utils/matrix.hpp"
37#include "utils/quaternion.hpp"
38
39#include <cassert>
40#include <cmath>
41
42namespace Utils {
43
44/**
45 * @brief Basis change.
46 */
47inline Vector3d basis_change(Vector3d const &b1, Vector3d const &b2,
48 Vector3d const &b3, Vector3d const &v,
49 bool reverse = false) {
50 auto const e_x = b1.normalized();
51 auto const e_y = b2.normalized();
52 auto const e_z = b3.normalized();
53 auto const M = Matrix<double, 3, 3>{
54 {e_x[0], e_x[1], e_x[2]},
55 {e_y[0], e_y[1], e_y[2]},
56 {e_z[0], e_z[1],
57 e_z[2]}}.transposed();
58 if (reverse) {
59 return M * v;
60 }
61 return M.inversed() * v;
62}
63
64/**
65 * @brief Coordinate transformation from Cartesian to cylindrical coordinates.
66 * The origins and z-axis of the coordinate systems co-incide.
67 * The @f$ \phi = 0 @f$ direction corresponds to the x-axis in the
68 * original coordinate system.
69 * @param pos Vector to transform
70 */
71inline Vector3d
73 auto const r = std::sqrt(pos[0] * pos[0] + pos[1] * pos[1]);
74 auto const phi = std::atan2(pos[1], pos[0]);
75 return {r, phi, pos[2]};
76}
77
78/**
79 * @brief Coordinate transformation from Cartesian to cylindrical coordinates
80 * with change of basis. The origins of the coordinate systems co-incide.
81 *
82 * If the parameter @p axis is not equal to <tt>[0, 0, 1]</tt>, the value
83 * of the angle @f$ \phi @f$ in cylindrical coordinates is under-defined.
84 * To fully define it, it is necessary to provide an orientation vector
85 * in Cartesian coordinates that will be used as the reference point
86 * (i.e. such that @f$ \phi = 0 @f$), by default it is the x-axis.
87 *
88 * @param pos Vector to transform
89 * @param axis Longitudinal axis of the cylindrical coordinates
90 * @param orientation Reference point (in untransformed coordinates) for
91 * which @f$ \phi = 0 @f$
92 */
94 Vector3d const &pos, Vector3d const &axis, Vector3d const &orientation) {
95 // check that axis and orientation are orthogonal
96 assert(std::abs(axis * orientation) <
97 5 * std::numeric_limits<double>::epsilon());
98 auto const rotation_axis = vector_product(axis, orientation);
99 auto const pos_t = basis_change(orientation, rotation_axis, axis, pos);
101}
102
103/**
104 * @brief Coordinate transformation from cylindrical to Cartesian coordinates.
105 * The origins and z-axis of the coordinate systems co-incide.
106 * The @f$ \phi = 0 @f$ direction corresponds to the x-axis in the
107 * transformed coordinate system.
108 * @param pos Vector to transform
109 */
110inline Vector3d
112 auto const &rho = pos[0];
113 auto const &phi = pos[1];
114 auto const &z = pos[2];
115 return {rho * std::cos(phi), rho * std::sin(phi), z};
116}
117
118/**
119 * @brief Coordinate transformation from cylindrical to Cartesian coordinates
120 * with change of basis. The origins of the coordinate systems co-incide.
121 *
122 * If the parameter @p axis is not equal to <tt>[0, 0, 1]</tt>, the value
123 * of the angle @f$ \phi @f$ in cylindrical coordinates is under-defined.
124 * To fully define it, it is necessary to provide an orientation vector
125 * in Cartesian coordinates that will be used as the reference point
126 * (i.e. such that @f$ \phi = 0 @f$).
127 *
128 * @param pos Vector to transform
129 * @param axis Longitudinal axis of the cylindrical coordinates
130 * @param orientation Reference point (in Cartesian coordinates) for
131 * which @f$ \phi = 0 @f$
132 */
134 Vector3d const &pos, Vector3d const &axis, Vector3d const &orientation) {
135 // check that axis and orientation are orthogonal
136 assert(std::abs(axis * orientation) <
137 5 * std::numeric_limits<double>::epsilon());
138 auto const rotation_axis = vector_product(axis, orientation);
140 return basis_change(orientation, rotation_axis, axis, pos_t, true);
141}
142
143/**
144 * @brief Vector transformation from Cartesian to cylindrical coordinates.
145 * @param vec Vector to transform
146 * @param axis Longitudinal axis of the cylindrical coordinates
147 * @param pos Origin of the vector
148 */
150 Vector3d const &axis,
151 Vector3d const &pos) {
152 static auto const z_axis = Vector3d{{0, 0, 1}};
153 auto const angle = angle_between(axis, z_axis);
154 auto const rotation_axis = Utils::vector_product(axis, z_axis).normalize();
155 auto const rotated_pos = vec_rotate(rotation_axis, angle, pos);
156 auto const rotated_vec = vec_rotate(rotation_axis, angle, vec);
157 auto const r = std::sqrt(rotated_pos[0] * rotated_pos[0] +
158 rotated_pos[1] * rotated_pos[1]);
159 // v_r = (x * v_x + y * v_y) / sqrt(x^2 + y^2)
160 auto const v_r =
161 (rotated_pos[0] * rotated_vec[0] + rotated_pos[1] * rotated_vec[1]) / r;
162 // v_phi = (x * v_y - y * v_x ) / sqrt(x^2 + y^2)
163 auto const v_phi =
164 (rotated_pos[0] * rotated_vec[1] - rotated_pos[1] * rotated_vec[0]) / r;
165 return Vector3d{v_r, v_phi, rotated_vec[2]};
166}
167
168} // namespace Utils
169#endif
Vector implementation and trait types for boost qvm interoperability.
__shared__ int pos[MAXDEPTH *THREADS5/WARPSIZE]
Vector normalized() const
Definition Vector.hpp:150
Matrix implementation and trait types for boost qvm interoperability.
VectorXd< 3 > Vector3d
Definition Vector.hpp:157
Vector< T, 3 > vector_product(Vector< T, 3 > const &a, Vector< T, 3 > const &b)
Definition Vector.hpp:353
Vector3d vec_rotate(const Vector3d &axis, double angle, const Vector3d &vector)
Rotate a vector around an axis.
double angle_between(Vector3d const &v1, Vector3d const &v2)
Determine the angle between two vectors.
Vector3d transform_vector_cartesian_to_cylinder(Vector3d const &vec, Vector3d const &axis, Vector3d const &pos)
Vector transformation from Cartesian to cylindrical coordinates.
Vector3d transform_coordinate_cylinder_to_cartesian(Vector3d const &pos)
Coordinate transformation from cylindrical to Cartesian coordinates.
Vector3d transform_coordinate_cartesian_to_cylinder(Vector3d const &pos)
Coordinate transformation from Cartesian to cylindrical coordinates.
Vector3d basis_change(Vector3d const &b1, Vector3d const &b2, Vector3d const &b3, Vector3d const &v, bool reverse=false)
Basis change.
Quaternion implementation and trait types for boost qvm interoperability.
Matrix representation with static size.
Definition matrix.hpp:65
Matrix< T, Cols, Rows > transposed() const
Retrieve a transposed copy of the matrix.
Definition matrix.hpp:189