ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
bspline_3d_gradient.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
22#include "utils/Vector.hpp"
23
27
28#include <array>
29#include <cstddef>
30
31namespace Utils {
32namespace Interpolation {
33/**
34 * @brief cardinal B-spline gradient interpolation with internal iteration.
35 *
36 * @tparam order The number of interpolation points in each direction.
37 * @tparam Kernel Callable that can be called at every point with the
38 * index of the point and its weights as arguments.
39 *
40 * @param pos The point at which the interpolation should be run.
41 * @param kernel Function to run for every point.
42 * @param grid_spacing The distance between the grid points.
43 * @param offset Shift of the grid relative to the origin.
44 */
45template <int order, typename Kernel>
46void bspline_3d_gradient(Vector3d const &pos, Kernel const &kernel,
47 Vector3d const &grid_spacing, Vector3d const &offset) {
48 using Utils::bspline;
49
50 /* The coordinates and relative distance of the assignment cube. */
51 auto const block = detail::ll_and_dist<order>(pos, grid_spacing, offset);
52
53 /* Precalc weights that are used multiple times. */
54 std::array<double, order> w_y;
55 std::array<double, order> w_z;
56 std::array<double, order> dw_y;
57 std::array<double, order> dw_z;
58 for (int i = 0; i < order; i++) {
59 w_y[i] = bspline<order>(i, block.distance[1]);
60 w_z[i] = bspline<order>(i, block.distance[2]);
61 dw_y[i] = bspline_d<order>(i, block.distance[1]) / grid_spacing[1];
62 dw_z[i] = bspline_d<order>(i, block.distance[2]) / grid_spacing[2];
63 }
64
65 std::array<int, 3> ind;
66 for (int i = 0; i < order; i++) {
67 ind[0] = block.corner[0] + i;
68 auto const w_x = bspline<order>(i, block.distance[0]);
69 auto const dw_x = bspline_d<order>(i, block.distance[0]) / grid_spacing[0];
70 for (int j = 0; j < order; j++) {
71 ind[1] = block.corner[1] + j;
72 for (int k = 0; k < order; k++) {
73 ind[2] = block.corner[2] + k;
74 kernel(ind, Vector3d{dw_x * w_y[j] * w_z[k], w_x * dw_y[j] * w_z[k],
75 w_x * w_y[j] * dw_z[k]});
76 }
77 }
78 }
79}
80
81/**
82 * @brief cardinal B-spline weighted sum.
83 */
84template <int order, typename T, typename Kernel>
85T bspline_3d_gradient_accumulate(Vector3d const &pos, Kernel const &kernel,
86 Vector3d const &grid_spacing,
87 Vector3d const &offset, T const &init) {
88 T value = init;
89 bspline_3d_gradient<order>(
90 pos,
91 [&value, &kernel](const std::array<int, 3> &ind, const Vector3d &w) {
92 value += Utils::tensor_product(kernel(ind), w);
93 },
94 grid_spacing, offset);
95
96 return value;
97}
98} // namespace Interpolation
99} // namespace Utils
Vector implementation and trait types for boost qvm interoperability.
__shared__ int pos[MAXDEPTH *THREADS5/WARPSIZE]
static double * block(double *p, std::size_t index, std::size_t size)
Definition elc.cpp:174
T bspline_3d_gradient_accumulate(Vector3d const &pos, Kernel const &kernel, Vector3d const &grid_spacing, Vector3d const &offset, T const &init)
cardinal B-spline weighted sum.
void bspline_3d_gradient(Vector3d const &pos, Kernel const &kernel, Vector3d const &grid_spacing, Vector3d const &offset)
cardinal B-spline gradient interpolation with internal iteration.
DEVICE_QUALIFIER auto bspline(int i, T x) -> std::enable_if_t<(order > 0) &&(order<=7), T >
Formula of the B-spline.
Definition bspline.hpp:32
Matrix< T, N, M > tensor_product(const Vector< T, N > &x, const Vector< T, M > &y)