ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
sampling.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#ifndef UTILS_SAMPLING_HPP
20#define UTILS_SAMPLING_HPP
21
22#include "utils/Vector.hpp"
23#include "utils/constants.hpp"
25#include "utils/math/sqr.hpp"
26
27#include <algorithm>
28#include <cmath>
29#include <cstddef>
30#include <iterator>
31#include <utility>
32#include <vector>
33
34namespace Utils {
35
36/**
37 * @brief Generate sampling positions for a cylindrical histogram.
38 * @param r_limits Range for radial coordinate as std::pair.
39 * @param phi_limits Range for azimuthal angle as std::pair.
40 * @param z_limits Range for z coordinate as std::pair.
41 * @param n_r_bins Number of bins in radial direction.
42 * @param n_phi_bins Number of bins in azimuthal direction.
43 * @param n_z_bins Number of bins in z direction.
44 * @param sampling_density The number of samples per unit volume.
45 * @retval Cartesian sampling coordinates.
46 */
48 std::pair<double, double> const &r_limits,
49 std::pair<double, double> const &phi_limits,
50 std::pair<double, double> const &z_limits, std::size_t n_r_bins,
51 std::size_t n_phi_bins, std::size_t n_z_bins, double sampling_density) {
52 auto constexpr endpoint = false;
53 auto const delta_r =
54 (r_limits.second - r_limits.first) / static_cast<double>(n_r_bins);
55 auto const delta_phi =
56 (phi_limits.second - phi_limits.first) / static_cast<double>(n_phi_bins);
57
58 // For the smallest bin we chose samples along the z-direction for a single
59 // azimuthal angle per bin such that we fulfill the sampling density
60 // requirement.
61 auto const smallest_bin_volume =
62 Utils::sqr(r_limits.first + delta_r) * delta_phi / 2.;
63 auto const min_n_samples =
64 std::max(n_z_bins, static_cast<std::size_t>(std::round(
65 smallest_bin_volume * sampling_density)));
66 auto const delta_z =
67 (z_limits.second - z_limits.first) / static_cast<double>(min_n_samples);
68
69 auto const r_range = make_lin_space(r_limits.first + .5 * delta_r,
70 r_limits.second, n_r_bins, endpoint);
71 auto const phi_range =
72 make_lin_space(phi_limits.first + .5 * delta_phi, phi_limits.second,
73 n_phi_bins, endpoint);
74 auto const z_range = make_lin_space(z_limits.first + .5 * delta_z,
75 z_limits.second, min_n_samples, endpoint);
76
77 // Create the sampling positions for the innermost bin.
78 std::vector<Vector3d> sampling_positions;
79 for (auto const z : z_range) {
80 for (auto const phi : phi_range) {
81 sampling_positions.push_back(Vector3d{{*r_range.begin(), phi, z}});
82 }
83 }
84
85 // Scale the number of samples for larger bins
86 auto phis = [n_phi_bins, phi_limits](long r_bin) {
87 auto const phis_range = make_lin_space(
88 phi_limits.first, phi_limits.second,
89 n_phi_bins * (static_cast<std::size_t>(r_bin) + 1), endpoint);
90 return phis_range;
91 };
92 // Calculate the sampling positions
93 // Along z
94 for (auto const z : z_range) {
95 // Along r
96 for (auto r = ++r_range.begin(); r != r_range.end(); ++r) {
97 // Along phi
98 for (auto const phi : phis(std::distance(r_range.begin(), r))) {
99 sampling_positions.push_back(Vector3d{{*r, phi, z}});
100 }
101 }
102 }
103
104 return sampling_positions;
105}
106
107} // namespace Utils
108
109#endif
Vector implementation and trait types for boost qvm interoperability.
std::vector< Vector3d > get_cylindrical_sampling_positions(std::pair< double, double > const &r_limits, std::pair< double, double > const &phi_limits, std::pair< double, double > const &z_limits, std::size_t n_r_bins, std::size_t n_phi_bins, std::size_t n_z_bins, double sampling_density)
Generate sampling positions for a cylindrical histogram.
Definition sampling.hpp:47
auto make_lin_space(T start, T stop, std::size_t number, bool endpoint=true)
Equally spaced values in interval.
DEVICE_QUALIFIER constexpr T sqr(T x)
Calculates the SQuaRe of x.
Definition sqr.hpp:26
DEVICE_QUALIFIER constexpr iterator begin() noexcept
Definition Array.hpp:128