ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
sinc.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2022 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#ifndef UTILS_MATH_SINC_HPP
23#define UTILS_MATH_SINC_HPP
24
25#include "utils/constants.hpp"
27#include "utils/math/abs.hpp"
28
29#include <cmath>
30
31namespace Utils {
32/**
33 * @brief Calculates the sinc-function as sin(PI*x)/(PI*x).
34 *
35 * (same convention as in @cite hockney88a). In order to avoid
36 * divisions by 0, arguments, whose modulus is smaller than epsi, will
37 * be evaluated by an 8th order Taylor expansion of the sinc
38 * function. Note that the difference between sinc(x) and this
39 * expansion is smaller than 0.235e-12, if x is smaller than 0.1. (The
40 * next term in the expansion is the 10th order contribution
41 * PI^10/39916800 * x^10 = 0.2346...*x^12). This expansion should
42 * also save time, since it reduces the number of function calls to
43 * sin().
44 */
45template <typename T> DEVICE_QUALIFIER T sinc(T d) {
46 const constexpr T epsi = T(0.1);
47
48 const auto PId = pi<T>() * d;
49
50 if (::Utils::abs(d) > epsi)
51 return sin(PId) / PId;
52
53 /* Coefficients of the Taylor expansion of sinc */
54 const constexpr T c2 = T(-0.1666666666667e-0);
55 const constexpr T c4 = T(0.8333333333333e-2);
56 const constexpr T c6 = T(-0.1984126984127e-3);
57 const constexpr T c8 = T(0.2755731922399e-5);
58
59 const auto PId2 = PId * PId;
60 return T(1) + PId2 * (c2 + PId2 * (c4 + PId2 * (c6 + PId2 * c8)));
61}
62} // namespace Utils
63
64#endif
#define DEVICE_QUALIFIER
DEVICE_QUALIFIER T sinc(T d)
Calculates the sinc-function as sin(PI*x)/(PI*x).
Definition sinc.hpp:45
DEVICE_QUALIFIER double abs(double x)
Return the absolute value of x.
Definition abs.hpp:32