ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
raster.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"
24
25#include <boost/multi_array.hpp>
26
27namespace Utils {
28
29/**
30 * @brief Raster a function over a regular 3d grid.
31 *
32 * This evaluates a function over a regular grid and
33 * returns the function values at the grid points.
34 *
35 * @param offset Position of the lowest grid point
36 * @param grid_spacing Grid constant
37 * @param size Grid size
38 * @param f Function to evaluate
39 * @return Function values at the grid points.
40 */
41template <class T, class F>
42auto raster(Vector<T, 3> const &offset, Vector<T, 3> const &grid_spacing,
43 Vector3i size, F f) {
44 using R = decltype(f((offset)));
45
46 boost::multi_array<R, 3> res(size);
47
48 auto const end = offset + Vector<T, 3>{grid_spacing[0] * size[0],
49 grid_spacing[1] * size[1],
50 grid_spacing[2] * size[2]};
51
52 auto it = res.data();
53 for (auto x : make_lin_space(offset[0], end[0], size[0], false))
54 for (auto y : make_lin_space(offset[1], end[1], size[1], false))
55 for (auto z : make_lin_space(offset[2], end[2], size[2], false)) {
56 *it++ = f(Vector<T, 3>{x, y, z});
57 }
58
59 return res;
60}
61} // namespace Utils
Vector implementation and trait types for boost qvm interoperability.
auto make_lin_space(T start, T stop, std::size_t number, bool endpoint=true)
Equally spaced values in interval.
auto raster(Vector< T, 3 > const &offset, Vector< T, 3 > const &grid_spacing, Vector3i size, F f)
Raster a function over a regular 3d grid.
Definition raster.hpp:42
DEVICE_QUALIFIER constexpr pointer data() noexcept
Definition Array.hpp:120