ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
angle_common.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2022 The ESPResSo project
3 * Copyright (C) 2002-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 * Common code for functions calculating angle forces.
26 */
27
28#include "config/config.hpp"
29
30#include <utils/Vector.hpp>
31
32#include <cmath>
33#include <tuple>
34
35namespace detail {
36inline double sanitize_cosine(double cosine) {
37 if (cosine > TINY_COS_VALUE)
38 cosine = TINY_COS_VALUE;
39 if (cosine < -TINY_COS_VALUE)
40 cosine = -TINY_COS_VALUE;
41 return cosine;
42}
43} // namespace detail
44
45/** Compute the cosine of the angle between three particles.
46 *
47 * @param[in] vec1 Vector from central particle to left particle.
48 * @param[in] vec2 Vector from central particle to right particle.
49 * @param[in] sanitize_cosine Sanitize the cosine of the angle.
50 * @return @f$ \vec{r_{ij}} @f$, @f$ \vec{r_{kj}} @f$,
51 * @f$ \left\|\vec{r_{ij}}\right\|^{-1} @f$,
52 * @f$ \left\|\vec{r_{kj}}\right\|^{-1} @f$,
53 * @f$ \cos(\theta_{ijk}) @f$
54 */
55inline double calc_cosine(Utils::Vector3d const &vec1,
56 Utils::Vector3d const &vec2,
57 bool sanitize_cosine = false) {
58 /* cosine of the angle between vec1 and vec2 */
59 auto cos_phi = (vec1 * vec2) / std::sqrt(vec1.norm2() * vec2.norm2());
60 if (sanitize_cosine) {
61 cos_phi = detail::sanitize_cosine(cos_phi);
62 }
63 return cos_phi;
64}
65
66/** Compute a three-body angle interaction force.
67 *
68 * See the details in @ref bondedIA_angle_force. The @f$ K(\theta_{ijk}) @f$
69 * term is provided as a lambda function in @p forceFactor.
70 *
71 * @param[in] vec1 Vector from central particle to left particle.
72 * @param[in] vec2 Vector from central particle to right particle.
73 * @param[in] forceFactor Angle force term.
74 * @param[in] sanitize_cosine Sanitize the cosine of the angle.
75 * @tparam ForceFactor Function evaluating the angle force term
76 * for a given angle.
77 * @return Forces on the second, first and third particles, in that order.
78 */
79template <typename ForceFactor>
80std::tuple<Utils::Vector3d, Utils::Vector3d, Utils::Vector3d>
82 ForceFactor forceFactor, bool sanitize_cosine) {
83 auto const d1 = vec1.norm();
84 auto const d2 = vec2.norm();
85 auto cos_phi = (vec1 * vec2) / (d1 * d2);
86 if (sanitize_cosine) {
87 cos_phi = detail::sanitize_cosine(cos_phi);
88 }
89 /* force factor */
90 auto const fac = forceFactor(cos_phi);
91 /* distribute forces */
92 auto const v1 = vec1 / d1;
93 auto const v2 = vec2 / d2;
94 auto f_left = (fac / d1) * (v1 * cos_phi - v2);
95 auto f_right = (fac / d2) * (v2 * cos_phi - v1);
96 auto f_mid = -(f_left + f_right);
97 return std::make_tuple(f_mid, f_left, f_right);
98}
Vector implementation and trait types for boost qvm interoperability.
std::tuple< Utils::Vector3d, Utils::Vector3d, Utils::Vector3d > angle_generic_force(Utils::Vector3d const &vec1, Utils::Vector3d const &vec2, ForceFactor forceFactor, bool sanitize_cosine)
Compute a three-body angle interaction force.
double calc_cosine(Utils::Vector3d const &vec1, Utils::Vector3d const &vec2, bool sanitize_cosine=false)
Compute the cosine of the angle between three particles.
T norm2() const
Definition Vector.hpp:130
T norm() const
Definition Vector.hpp:131
This file contains the defaults for ESPResSo.
#define TINY_COS_VALUE
Tiny angle cutoff for cosine calculations.
Definition config.hpp:75