ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
bspline.hpp
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
20#pragma once
21
22#include "sqr.hpp"
24
25#if defined(__CUDACC__)
26#include <cfloat>
27#else
28#include <limits>
29#endif
30#include <stdexcept>
31
32#if defined(__clang__)
33#pragma clang diagnostic push
34#pragma clang diagnostic ignored "-Wimplicit-fallthrough"
35#elif defined(__GNUC__) or defined(__GNUG__)
36#pragma GCC diagnostic push
37#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
38#endif
39
40// NOLINTBEGIN(bugprone-unhandled-code-paths)
41
42namespace Utils {
43/** @brief Formula of the B-spline. */
44template <int order, typename T>
45DEVICE_QUALIFIER auto bspline(int i, T x) -> T
46 requires((order > 0) and (order <= 7))
47{
48#if defined(__CUDACC__)
49 // LCOV_EXCL_START
50 [[maybe_unused]] auto constexpr tolerance = T(6) * (T)(FLT_EPSILON);
51 // LCOV_EXCL_STOP
52#else
53 [[maybe_unused]] auto constexpr tolerance =
54 T(6) * std::numeric_limits<T>::epsilon();
55#endif
56 DEVICE_ASSERT(i < order);
57 DEVICE_ASSERT(x >= T(-0.5) or (T(-0.5) - x) < tolerance);
58 DEVICE_ASSERT(x <= T(0.5) or (x - T(0.5)) < tolerance);
59
60 switch (order) {
61 case 1:
62 return T(1.);
63 case 2:
64 switch (i) {
65 case 0:
66 return T(0.5) - x;
67 case 1:
68 return T(0.5) + x;
69 }
70 case 3:
71 switch (i) {
72 case 0:
73 return T(0.5) * sqr(T(0.5) - x);
74 case 1:
75 return T(0.75) - sqr(x);
76 case 2:
77 return T(0.5) * sqr(T(0.5) + x);
78 }
79 case 4:
80 switch (i) {
81 case 0:
82 return (T(1.) + x * (T(-6.) + x * (T(12.) - x * T(8.)))) / T(48.);
83 case 1:
84 return (T(23.) + x * (T(-30.) + x * (T(-12.) + x * T(24.)))) / T(48.);
85 case 2:
86 return (T(23.) + x * (T(30.) + x * (T(-12.) - x * T(24.)))) / T(48.);
87 case 3:
88 return (T(1.) + x * (T(6.) + x * (T(12.) + x * T(8.)))) / T(48.);
89 }
90 case 5:
91 switch (i) {
92 case 0:
93 return (T(1.) +
94 x * (T(-8.) + x * (T(24.) + x * (T(-32.) + x * T(16.))))) /
95 T(384.);
96 case 1:
97 return (T(19.) +
98 x * (T(-44.) + x * (T(24.) + x * (T(16.) - x * T(16.))))) /
99 T(96.);
100 case 2:
101 return (T(115.) + x * x * (T(-120.) + x * x * T(48.))) / T(192.);
102 case 3:
103 return (T(19.) +
104 x * (T(44.) + x * (T(24.) + x * (T(-16.) - x * T(16.))))) /
105 T(96.);
106 case 4:
107 return (T(1.) + x * (T(8.) + x * (T(24.) + x * (T(32.) + x * T(16.))))) /
108 T(384.);
109 }
110 case 6:
111 switch (i) {
112 case 0:
113 return (T(1.) +
114 x * (T(-10.) +
115 x * (T(40.) + x * (T(-80.) + x * (T(80.) - x * T(32.)))))) /
116 T(3840.);
117 case 1:
118 return (T(237.) +
119 x * (T(-750.) +
120 x * (T(840.) +
121 x * (T(-240.) + x * (T(-240.) + x * T(160.)))))) /
122 T(3840.);
123 case 2:
124 return (T(841.) +
125 x * (T(-770.) +
126 x * (T(-440.) +
127 x * (T(560.) + x * (T(80.) - x * T(160.)))))) /
128 T(1920.);
129 case 3:
130 return (T(841.) +
131 x * (+T(770.) +
132 x * (T(-440.) +
133 x * (T(-560.) + x * (T(80.) + x * T(160.)))))) /
134 T(1920.);
135 case 4:
136 return (T(237.) +
137 x * (T(750.) +
138 x * (T(840.) +
139 x * (T(240.) + x * (T(-240.) - x * T(160.)))))) /
140 T(3840.);
141 case 5:
142 return (T(1.) +
143 x * (T(10.) +
144 x * (T(40.) + x * (T(80.) + x * (T(80.) + x * T(32.)))))) /
145 T(3840.);
146 }
147 case 7:
148 switch (i) {
149 case 0:
150 return (T(1.) +
151 x * (T(-12.) +
152 x * (T(60.) +
153 x * (T(-160.) +
154 x * (T(240.) + x * (T(-192.) + x * T(64.))))))) /
155 T(46080.);
156 case 1:
157 return (T(361.) +
158 x * (T(-1416.) +
159 x * (T(2220.) +
160 x * (T(-1600.) +
161 x * (T(240.) + x * (T(384.) - x * T(192.))))))) /
162 T(23040.);
163 case 2:
164 return (T(10543.) +
165 x * (T(-17340.) +
166 x * (T(4740.) +
167 x * (T(6880.) + x * (T(-4080.) +
168 x * (T(-960.) + x * T(960.))))))) /
169 T(46080.);
170 case 3:
171 return (T(5887.) +
172 x * x * (T(-4620.) + x * x * (T(1680.) - x * x * T(320.)))) /
173 T(11520.);
174 case 4:
175 return (T(10543.) +
176 x * (T(17340.) +
177 x * (T(4740.) +
178 x * (T(-6880.) +
179 x * (T(-4080.) + x * (T(960.) + x * T(960.))))))) /
180 T(46080.);
181 case 5:
182 return (T(361.) +
183 x * (T(1416.) +
184 x * (T(2220.) +
185 x * (T(1600.) +
186 x * (T(240.) + x * (T(-384.) - x * T(192.))))))) /
187 T(23040.);
188 case 6:
189 return (T(1.) +
190 x * (T(12.) +
191 x * (T(60.) +
192 x * (T(160.) +
193 x * (T(240.) + x * (T(192.) + x * T(64.))))))) /
194 T(46080.);
195 }
196 }
197
198 DEVICE_THROW(std::runtime_error("Internal interpolation error."));
199 return T{};
200}
201
202/** @brief Derivative of the B-spline. */
203template <int order, typename T = double>
204DEVICE_QUALIFIER auto bspline_d(int i, T x) -> T
205 requires((order > 0) and (order <= 7))
206{
207#if defined(__CUDACC__)
208 // LCOV_EXCL_START
209 [[maybe_unused]] auto constexpr tolerance = T(6) * (T)(FLT_EPSILON);
210 // LCOV_EXCL_STOP
211#else
212 [[maybe_unused]] auto constexpr tolerance =
213 T(6) * std::numeric_limits<T>::epsilon();
214#endif
215 DEVICE_ASSERT(i < order);
216 DEVICE_ASSERT(x >= T(-0.5) or (T(-0.5) - x) < tolerance);
217 DEVICE_ASSERT(x <= T(0.5) or (x - T(0.5)) < tolerance);
218
219 switch (order - 1) {
220 case 0:
221 return 0.;
222 case 1:
223 switch (i) {
224 case 0:
225 return T(-1.);
226 case 1:
227 return T(1.);
228 }
229 case 2:
230 switch (i) {
231 case 0:
232 return x - T(0.5);
233 case 1:
234 return T(-2.) * x;
235 case 2:
236 return x + T(0.5);
237 }
238 case 3:
239 switch (i) {
240 case 0:
241 return (T(-1.) + x * (T(4.) + x * T(-4.))) / T(8.);
242 case 1:
243 return (T(-5.) + x * (T(-4.) + x * T(12.))) / T(8.);
244 case 2:
245 return (T(5.) + x * (T(-4.) + x * T(-12.))) / T(8.);
246 case 3:
247 return (T(1.) + x * (T(4.) + x * T(4.))) / T(8.);
248 }
249 case 4:
250 switch (i) {
251 case 0:
252 return (T(-1.) + x * (T(6.) + x * (T(-12.) + x * T(8.)))) / T(48.);
253 case 1:
254 return (T(-11.) + x * (T(12.) + x * (T(12.) + x * T(-16.)))) / T(24.);
255 case 2:
256 return (x * (T(-5.) + x * x * T(4.))) / T(4.);
257 case 3:
258 return (T(11.) + x * (T(12.) + x * (T(-12.) + x * T(-16.)))) / T(24.);
259 case 4:
260 return (T(1.) + x * (T(6.) + x * (T(12.) + x * T(8.)))) / T(48.);
261 }
262 case 5:
263 switch (i) {
264 case 0:
265 return (T(-1.) +
266 x * (T(8.) + x * (T(-24.) + x * (T(32.) + x * T(-16.))))) /
267 T(384.);
268 case 1:
269 return (T(-75.) +
270 x * (T(168.) + x * (T(-72.) + x * (T(-96.) + x * (T(80.)))))) /
271 T(384.);
272 case 2:
273 return (T(-77.) +
274 x * (T(-88.) + x * (T(168.) + x * (T(32.) + x * T(-80.))))) /
275 T(192.);
276 case 3:
277 return (T(77.) +
278 x * (T(-88.) + x * (T(-168.) + x * (T(32.) + x * T(80.))))) /
279 T(192.);
280 case 4:
281 return (T(75.) +
282 x * (T(168.) + x * (T(72.) + x * (T(-96.) + x * T(-80.))))) /
283 T(384.);
284 case 5:
285 return (T(1.) + x * (T(8.) + x * (T(24.) + x * (T(32.) + x * T(16.))))) /
286 T(384.);
287 }
288 case 6:
289 switch (i) {
290 case 0:
291 return (T(-1.) +
292 x * (T(10.) +
293 x * (T(-40.) + x * (T(80.) + x * (T(-80.) + x * T(32.)))))) /
294 T(3840.);
295 case 1:
296 return (T(-59.) +
297 x * (T(185.) +
298 x * (T(-200.) + x * (T(40.) + x * (T(80.) - x * T(48.)))))) /
299 T(960.);
300 case 2:
301 return (T(-289.) +
302 x * (T(158.) +
303 x * (T(344.) +
304 x * (T(-272.) + x * (T(-80.) + x * T(96.)))))) /
305 T(768.);
306 case 3:
307 return (x * (T(-77.) + x * x * (T(56.) - x * x * T(16.)))) / T(96.);
308 case 4:
309 return (T(289.) + x * (T(158.) + x * (T(-344.) +
310 x * (T(-272.) +
311 x * (T(80.) + x * T(96.)))))) /
312 T(768.);
313 case 5:
314 return (T(59.) +
315 x * (T(185.) +
316 x * (T(200.) + x * (T(40.) + x * (T(-80.) - x * T(48.)))))) /
317 T(960.);
318 case 6:
319 return (T(1.) +
320 x * (T(10.) +
321 x * (T(40.) + x * (T(80.) + x * (T(80.) + x * T(32.)))))) /
322 T(3840.);
323 }
324 }
325
326 DEVICE_THROW(std::runtime_error("Internal interpolation error."));
327 return T{};
328}
329} // namespace Utils
330
331// NOLINTEND(bugprone-unhandled-code-paths)
332
333#if defined(__clang__)
334#pragma clang diagnostic pop
335#elif defined(__GNUC__) or defined(__GNUG__)
336#pragma GCC diagnostic pop
337#endif
#define DEVICE_ASSERT(A)
#define DEVICE_QUALIFIER
#define DEVICE_THROW(E)
DEVICE_QUALIFIER auto bspline_d(int i, T x) -> T requires((order > 0) and(order<=7))
Derivative of the B-spline.
Definition bspline.hpp:204
DEVICE_QUALIFIER constexpr T sqr(T x)
Calculates the SQuaRe of x.
Definition sqr.hpp:28
DEVICE_QUALIFIER auto bspline(int i, T x) -> T requires((order > 0) and(order<=7))
Formula of the B-spline.
Definition bspline.hpp:45