ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
BoxGeometry.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
20#pragma once
21
24
25#include <utils/Vector.hpp>
26#include <utils/math/sgn.hpp>
27
28#include <bitset>
29#include <cassert>
30#include <cmath>
31#include <limits>
32#include <stdexcept>
33#include <utility>
34
35#if defined(__GNUG__) or defined(__clang__)
36#define ESPRESSO_ATTR_ALWAYS_INLINE [[gnu::always_inline]]
37#else
38#define ESPRESSO_ATTR_ALWAYS_INLINE
39#endif
40
41namespace detail {
42/**
43 * @brief Get the minimum-image distance between two coordinates.
44 * @param a Coordinate of the terminal point.
45 * @param b Coordinate of the initial point.
46 * @param box_length Box length.
47 * @param box_length_inv Inverse box length
48 * @param box_length_half Half box length
49 * @param periodic Box periodicity.
50 * @return Shortest distance from @p b to @p a across periodic images,
51 * i.e. <tt>a - b</tt>. Can be negative.
52 */
53template <typename T>
54T get_mi_coord(T a, T b, T box_length, T box_length_inv, T box_length_half,
55 bool periodic) {
56 auto const dx = a - b;
57
58 if (periodic && (std::abs(dx) > box_length_half)) {
59 return dx - std::round(dx * box_length_inv) * box_length;
60 }
61
62 return dx;
63}
64
65/**
66 * @brief Get the minimum-image distance between two coordinates.
67 * @param a Coordinate of the terminal point.
68 * @param b Coordinate of the initial point.
69 * @param box_length Box length.
70 * @param periodic Box periodicity.
71 * @return Shortest distance from @p b to @p a across periodic images,
72 * i.e. <tt>a - b</tt>. Can be negative.
73 */
74template <typename T> T get_mi_coord(T a, T b, T box_length, bool periodic) {
75 return get_mi_coord(a, b, box_length, 1. / box_length, 0.5 * box_length,
76 periodic);
77}
78
79/** @brief Calculate image box shift vector.
80 * @param image_box image box offset
81 * @param box box length
82 * @return Image box coordinates.
83 */
84inline auto image_shift(Utils::Vector3i const &image_box,
85 Utils::Vector3d const &box) {
86 return hadamard_product(image_box, box);
87}
88
89/** @brief Unfold particle coordinates to image box.
90 * @param pos coordinate to unfold
91 * @param image_box image box offset
92 * @param box box length
93 * @return Unfolded coordinates.
94 */
95inline auto unfolded_position(Utils::Vector3d const &pos,
96 Utils::Vector3i const &image_box,
97 Utils::Vector3d const &box) {
98 return pos + image_shift(image_box, box);
99}
100} // namespace detail
101
102enum class BoxType { CUBOID = 0, LEES_EDWARDS = 1 };
103
105public:
107 set_length(Utils::Vector3d{1., 1., 1.});
108 set_periodic(0u, true);
109 set_periodic(1u, true);
110 set_periodic(2u, true);
112 }
114 m_type = rhs.type();
115 set_length(rhs.length());
116 set_periodic(0u, rhs.periodic(0u));
117 set_periodic(1u, rhs.periodic(1u));
118 set_periodic(2u, rhs.periodic(2u));
119 m_lees_edwards_bc = rhs.m_lees_edwards_bc;
120 }
121
122private:
123 BoxType m_type = BoxType::CUBOID;
124 /** Flags for all three dimensions whether pbc are applied (default). */
125 std::bitset<3> m_periodic = 0b111;
126 /** Side lengths of the box */
127 Utils::Vector3d m_length = {1., 1., 1.};
128 /** Inverse side lengths of the box */
129 Utils::Vector3d m_length_inv = {1., 1., 1.};
130 /** Half side lengths of the box */
131 Utils::Vector3d m_length_half = {0.5, 0.5, 0.5};
132
133 /** Lees-Edwards boundary conditions */
134 LeesEdwardsBC m_lees_edwards_bc;
135
136public:
137 /**
138 * @brief Set periodicity for direction
139 *
140 * @param coord The coordinate to set the periodicity for.
141 * @param val True if this direction should be periodic.
142 */
143 void set_periodic(unsigned coord, bool val) { m_periodic.set(coord, val); }
144
145 /**
146 * @brief Check periodicity in direction.
147 *
148 * @param coord Direction to check
149 * @return true iff periodic in direction.
150 */
151 constexpr bool periodic(unsigned coord) const {
152 assert(coord <= 2u);
153 return m_periodic[coord];
154 }
155
156 /**
157 * @brief Box length
158 * @return Return vector of side-lengths of the box.
159 */
160 Utils::Vector3d const &length() const { return m_length; }
161
162 /**
163 * @brief Inverse box length
164 * @return Return vector of inverse side-lengths of the box.
165 */
166 Utils::Vector3d const &length_inv() const { return m_length_inv; }
167
168 /**
169 * @brief Half box length
170 * @return Return vector of half side-lengths of the box.
171 */
172 Utils::Vector3d const &length_half() const { return m_length_half; }
173
174 /**
175 * @brief Set box side lengths.
176 * @param box_l Length that should be set.
177 */
178 void set_length(Utils::Vector3d const &box_l) {
179 m_length = box_l;
180 m_length_inv = {1. / box_l[0], 1. / box_l[1], 1. / box_l[2]};
181 m_length_half = 0.5 * box_l;
182 }
183
184 /**
185 * @brief Box volume
186 * @return Return the volume of the box.
187 */
188 double volume() const { return Utils::product(m_length); }
189
190 /**
191 * @brief Get the minimum-image distance between two coordinates.
192 * @param a Coordinate of the terminal point.
193 * @param b Coordinate of the initial point.
194 * @param coord Direction
195 * @return Shortest distance from @p b to @p a across periodic images,
196 * i.e. <tt>a - b</tt>. Can be negative.
197 */
198 template <typename T> T inline get_mi_coord(T a, T b, unsigned coord) const {
199 assert(coord <= 2u);
200
201 return detail::get_mi_coord(a, b, m_length[coord], m_length_inv[coord],
202 m_length_half[coord], m_periodic[coord]);
203 }
204
205 /**
206 * @brief Get the minimum-image vector between two coordinates.
207 *
208 * @tparam T Floating point type.
209 *
210 * @param a Coordinate of the terminal point.
211 * @param b Coordinate of the initial point.
212 * @return Vector from @p b to @p a that minimizes the distance across
213 * periodic images, i.e. <tt>a - b</tt>.
214 */
215 template <typename T>
218 const Utils::Vector<T, 3> &b) const {
219 if (type() == BoxType::LEES_EDWARDS) {
220 auto const shear_plane_normal = lees_edwards_bc().shear_plane_normal;
221 auto a_tmp = a;
222 auto b_tmp = b;
223 a_tmp[shear_plane_normal] = Algorithm::periodic_fold(
224 a_tmp[shear_plane_normal], m_length[shear_plane_normal]);
225 b_tmp[shear_plane_normal] = Algorithm::periodic_fold(
226 b_tmp[shear_plane_normal], m_length[shear_plane_normal]);
227 return lees_edwards_bc().distance(a_tmp - b_tmp, m_length, m_length_half,
228 m_length_inv, m_periodic);
229 }
230 assert(type() == BoxType::CUBOID);
231 return {get_mi_coord(a[0], b[0], 0), get_mi_coord(a[1], b[1], 1),
232 get_mi_coord(a[2], b[2], 2)};
233 }
234
235 /**
236 * @brief Get the minimum-image vector between two coordinates.
237 *
238 * @tparam T Floating point type.
239 *
240 * @param a0 x element of the terminal point.
241 * @param a1 y element of the terminal point.
242 * @param a2 z element of the terminal point.
243 * @param b0 x element of the initial point.
244 * @param b1 y element of the initial point.
245 * @param b2 z element of the initial point.
246 * @return Vector from @p b to @p a that minimizes the distance across
247 * periodic images, i.e. <tt>a - b</tt>.
248 */
249 template <typename T>
251 get_mi_vector(T const &a0, T const &a1, T const &a2, T const &b0, T const &b1,
252 T const &b2) const {
253 if (type() == BoxType::LEES_EDWARDS) {
254 auto const shear_plane_normal = lees_edwards_bc().shear_plane_normal;
255 auto a_tmp = Utils::Vector<T, 3>{a0, a1, a2};
256 auto b_tmp = Utils::Vector<T, 3>{b0, b1, b2};
257 a_tmp[shear_plane_normal] = Algorithm::periodic_fold(
258 a_tmp[shear_plane_normal], m_length[shear_plane_normal]);
259 b_tmp[shear_plane_normal] = Algorithm::periodic_fold(
260 b_tmp[shear_plane_normal], m_length[shear_plane_normal]);
261 return lees_edwards_bc().distance(a_tmp - b_tmp, m_length, m_length_half,
262 m_length_inv, m_periodic);
263 }
264 assert(type() == BoxType::CUBOID);
265 return {get_mi_coord(a0, b0, 0), get_mi_coord(a1, b1, 1),
266 get_mi_coord(a2, b2, 2)};
267 }
268
269 BoxType type() const { return m_type; }
270 void set_type(BoxType type) { m_type = type; }
271
272 LeesEdwardsBC const &lees_edwards_bc() const { return m_lees_edwards_bc; }
273 void set_lees_edwards_bc(LeesEdwardsBC bc) { m_lees_edwards_bc = bc; }
274
275 /**
276 * @brief Update the Lees-Edwards parameters of the box geometry
277 * for the current simulation time.
278 */
279 void lees_edwards_update(double pos_offset, double shear_velocity) {
280 assert(type() == BoxType::LEES_EDWARDS);
281 m_lees_edwards_bc.pos_offset = pos_offset;
282 m_lees_edwards_bc.shear_velocity = shear_velocity;
283 }
284
285 /** Calculate the velocity difference including the Lees-Edwards velocity */
287 Utils::Vector3d const &y,
288 Utils::Vector3d const &u,
289 Utils::Vector3d const &v) const {
290 auto ret = u - v;
291 if (type() == BoxType::LEES_EDWARDS) {
292 auto const &le = m_lees_edwards_bc;
293 auto const shear_plane_normal = le.shear_plane_normal;
294 auto const shear_direction = le.shear_direction;
295 auto const dy = x[shear_plane_normal] - y[shear_plane_normal];
296 if (fabs(dy) > length_half()[shear_plane_normal]) {
297 ret[shear_direction] -= Utils::sgn(dy) * le.shear_velocity;
298 }
299 }
300 return ret;
301 }
302
303 /** @brief Fold coordinates to primary simulation box in-place.
304 * Lees-Edwards offset is ignored.
305 * @param[in,out] pos coordinates to fold
306 * @param[in,out] image_box image box offset
307 */
308 void fold_position(Utils::Vector3d &pos, Utils::Vector3i &image_box) const {
309 for (auto i = 0u; i < 3u; i++) {
310 if (m_periodic[i]) {
311 auto const result =
312 Algorithm::periodic_fold(pos[i], image_box[i], m_length[i]);
313 if (result.second == std::numeric_limits<int>::min() or
314 result.second == std::numeric_limits<int>::max()) {
315 throw std::runtime_error(
316 "Overflow in the image box count while folding a particle "
317 "coordinate into the primary simulation box. Maybe a particle "
318 "experienced a huge force.");
319 }
320 std::tie(pos[i], image_box[i]) = result;
321 }
322 }
323 }
324
325 /**
326 * @brief Calculate coordinates folded to primary simulation box.
327 * @param[in] pos coordinates to fold
328 * @return Folded coordinates.
329 */
330 auto folded_position(Utils::Vector3d const &pos) const {
331 auto pos_folded = pos;
332 for (unsigned int i = 0u; i < 3u; i++) {
333 if (m_periodic[i]) {
334 pos_folded[i] = Algorithm::periodic_fold(pos[i], m_length[i]);
335 }
336 }
337
338 return pos_folded;
339 }
340
341 /**
342 * @brief Calculate image box of coordinates folded to primary simulation box.
343 * @param[in] pos coordinates
344 * @param[in] image_box image box to fold
345 * @return Folded image box.
346 */
348 Utils::Vector3i const &image_box) const {
349 auto image_box_folded = image_box;
350 for (auto i = 0u; i < 3u; i++) {
351 if (m_periodic[i]) {
352 image_box_folded[i] =
353 Algorithm::periodic_fold(pos[i], image_box[i], m_length[i]).second;
354 }
355 }
356
357 return image_box_folded;
358 }
359
360 /** @brief Calculate image box shift vector */
361 auto image_shift(Utils::Vector3i const &image_box) const {
362 return detail::image_shift(image_box, m_length);
363 }
364
365 /** @brief Unfold particle coordinates to image box. */
367 Utils::Vector3i const &image_box) const {
368 return detail::unfolded_position(pos, image_box, m_length);
369 }
370};
BoxType
@ LEES_EDWARDS
#define ESPRESSO_ATTR_ALWAYS_INLINE
static int coord(std::string const &s)
Vector implementation and trait types for boost qvm interoperability.
auto unfolded_position(Utils::Vector3d const &pos, Utils::Vector3i const &image_box) const
Unfold particle coordinates to image box.
T get_mi_coord(T a, T b, unsigned coord) const
Get the minimum-image distance between two coordinates.
void lees_edwards_update(double pos_offset, double shear_velocity)
Update the Lees-Edwards parameters of the box geometry for the current simulation time.
auto folded_position(Utils::Vector3d const &pos) const
Calculate coordinates folded to primary simulation box.
Utils::Vector3d const & length() const
Box length.
LeesEdwardsBC const & lees_edwards_bc() const
BoxGeometry(BoxGeometry const &rhs)
constexpr bool periodic(unsigned coord) const
Check periodicity in direction.
double volume() const
Box volume.
BoxType type() const
auto image_shift(Utils::Vector3i const &image_box) const
Calculate image box shift vector.
ESPRESSO_ATTR_ALWAYS_INLINE Utils::Vector< T, 3 > get_mi_vector(const Utils::Vector< T, 3 > &a, const Utils::Vector< T, 3 > &b) const
Get the minimum-image vector between two coordinates.
ESPRESSO_ATTR_ALWAYS_INLINE Utils::Vector< T, 3 > get_mi_vector(T const &a0, T const &a1, T const &a2, T const &b0, T const &b1, T const &b2) const
Get the minimum-image vector between two coordinates.
Utils::Vector3d const & length_half() const
Half box length.
Utils::Vector3d const & length_inv() const
Inverse box length.
void set_periodic(unsigned coord, bool val)
Set periodicity for direction.
void set_length(Utils::Vector3d const &box_l)
Set box side lengths.
auto folded_image_box(Utils::Vector3d const &pos, Utils::Vector3i const &image_box) const
Calculate image box of coordinates folded to primary simulation box.
void set_lees_edwards_bc(LeesEdwardsBC bc)
void fold_position(Utils::Vector3d &pos, Utils::Vector3i &image_box) const
Fold coordinates to primary simulation box in-place.
Utils::Vector3d velocity_difference(Utils::Vector3d const &x, Utils::Vector3d const &y, Utils::Vector3d const &u, Utils::Vector3d const &v) const
Calculate the velocity difference including the Lees-Edwards velocity.
void set_type(BoxType type)
auto periodic_fold(std::floating_point auto x, std::integral auto i, std::floating_point auto l)
Fold value into primary interval.
T product(Vector< T, N > const &v)
Definition Vector.hpp:373
constexpr int sgn(T val)
Calculate signum of val.
Definition sgn.hpp:27
unsigned int shear_plane_normal
Utils::Vector3d distance(Utils::Vector3d const &d, Utils::Vector3d const &l, Utils::Vector3d const &, Utils::Vector3d const &l_inv, std::bitset< 3 > const periodic) const