ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
Torus.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2026 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#include <cmath>
20#include <shapes/Torus.hpp>
21#include <utils/Vector.hpp>
22#include <utils/math/sqr.hpp>
23
24namespace Shapes {
25
26/*
27 * ASCII diagram of the cross-section of the torus.
28 * Only the rightmost circle is shown.
29 *
30 * Points:
31 * - O is the torus center
32 * - C is the torus core center
33 * - Z is an arbitrary point on the revolution axis
34 * - B is the inner equator
35 * - A is the point for which the normal vector is calculated
36 * - V is the projection of point A on the segment OC
37 *
38 * Vectors:
39 * - c_dist is segment OA
40 * - dir_vec is segment CA
41 * - r_vec is segment OV
42 * - z is segment VA
43 * - e_z is segment OZ
44 *
45 * Z
46 * | A
47 * | /|\
48 * | / | \
49 * | / | \
50 * | / | \ .....
51 * | / | ..
52 * | / | . \
53 * | / | . \
54 * | / | . \
55 * |/ |. \
56 * +---------++--------+
57 * O V .B C
58 * .
59 * .
60 * .
61 * ..
62 * .....
63 */
65 Utils::Vector3d &vec) const {
66 /* Coordinate transform to cylinder coords
67 with origin at m_center. */
68 auto const c_dist = pos - m_center;
69 auto const z = e_z * c_dist;
70 auto const r_vec = c_dist - z * e_z;
71 auto const r = r_vec.norm();
72
73 dist = (std::sqrt(Utils::sqr(r - m_rad) + z * z) - m_tube_rad) * m_direction;
74
75 /* If exactly on the symmetry axis (r == 0), r_vec / r is undefined (0/0).
76 Use the precomputed e_r_axis as a fallback, matching the convention in
77 Cylinder::calculate_dist. */
78 auto const e_r = (r == 0.) ? e_r_axis : r_vec / r;
79 /* Compute surface normal (distance vector from core circle) */
80 auto const dir_vec = r_vec - e_r * m_rad + z * e_z;
81 auto const dir_vec_norm = dir_vec.norm();
82 if (dir_vec_norm < 1e-14) {
83 /* Position is on the core circle, any orientation is possible,
84 return the vector pointing to the torus center */
85 vec = dist * c_dist / c_dist.norm();
86 } else {
88 }
89}
90} // namespace Shapes
Vector implementation and trait types for boost qvm interoperability.
Utils::Vector3d e_z
Unit vector in z direction.
void calculate_dist(const Utils::Vector3d &pos, double &dist, Utils::Vector3d &vec) const override
Definition Torus.cpp:64
Utils::Vector3d e_r_axis
Alternative e_r for the on-axis corner case.
Utils::Vector3d m_center
center of the shape.
double m_direction
direction -1: inside, +1 outside
double m_tube_rad
tube radius.
T norm() const
Definition Vector.hpp:159
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
DEVICE_QUALIFIER constexpr T sqr(T x)
Calculates the SQuaRe of x.
Definition sqr.hpp:28