|
| 1 | + |
| 2 | +#include <CompactNSearch> |
| 3 | + |
| 4 | +#include <iostream> |
| 5 | +#include <vector> |
| 6 | +#include <array> |
| 7 | +#include <cmath> |
| 8 | +#include <limits> |
| 9 | +#include <chrono> |
| 10 | +#include <random> |
| 11 | + |
| 12 | +#include <omp.h> |
| 13 | + |
| 14 | +using namespace CompactNSearch; |
| 15 | + |
| 16 | +std::vector<std::array<Real, 3>> positions; |
| 17 | + |
| 18 | +std::size_t const N = 150; |
| 19 | +Real const r_omega = 0.15; |
| 20 | +Real const r_omega2 = r_omega * r_omega; |
| 21 | +Real const radius = 2.0 * (2.0 * r_omega / static_cast<Real>(N-1)); |
| 22 | + |
| 23 | +std::size_t const N_enright_steps = 50; |
| 24 | + |
| 25 | +Real |
| 26 | +compute_average_number_of_neighbors(NeighborhoodSearch const& nsearch) |
| 27 | +{ |
| 28 | + unsigned long res = 0; |
| 29 | + auto const& d = nsearch.point_set(0); |
| 30 | + for (int i = 0; i < d.n_points(); ++i) |
| 31 | + { |
| 32 | + res += static_cast<unsigned long>(d.n_neighbors(i)); |
| 33 | + } |
| 34 | + return static_cast<Real>(res) / static_cast<Real>(d.n_points()); |
| 35 | +} |
| 36 | + |
| 37 | +Real |
| 38 | +compute_average_distance(NeighborhoodSearch const& nsearch) |
| 39 | +{ |
| 40 | + unsigned long long res = 0; |
| 41 | + auto const& d = nsearch.point_set(0); |
| 42 | + unsigned long long count = 0; |
| 43 | + for (int i = 0; i < d.n_points(); ++i) |
| 44 | + { |
| 45 | + std::size_t nn = d.n_neighbors(i); |
| 46 | + for (int j = 0; j < nn; ++j) |
| 47 | + { |
| 48 | + CompactNSearch::PointID const& k = d.neighbor(i, j); |
| 49 | + res += std::abs(i - static_cast<int>(k.point_id)); |
| 50 | + count++; |
| 51 | + } |
| 52 | + } |
| 53 | + return static_cast<Real>(res) / static_cast<Real>(count); |
| 54 | +} |
| 55 | + |
| 56 | +std::vector<std::vector<unsigned int>> |
| 57 | +brute_force_search() |
| 58 | +{ |
| 59 | + std::vector<std::vector<unsigned int>> brute_force_neighbors(positions.size()); |
| 60 | + for (int i = 0; i < positions.size(); ++i) |
| 61 | + { |
| 62 | + std::vector<unsigned int>& neighbors = brute_force_neighbors[i]; |
| 63 | + for (int j = 0; j < positions.size(); ++j) |
| 64 | + { |
| 65 | + if (i == j) |
| 66 | + continue; |
| 67 | + std::array<Real, 3> const& xa = positions[i]; |
| 68 | + std::array<Real, 3> const& xb = positions[j]; |
| 69 | + Real l2 = |
| 70 | + (xa[0] - xb[0])*(xa[0] - xb[0]) + |
| 71 | + (xa[1] - xb[1])*(xa[1] - xb[1]) + |
| 72 | + (xa[2] - xb[2])*(xa[2] - xb[2]); |
| 73 | + if (l2 <= radius * radius) |
| 74 | + { |
| 75 | + neighbors.push_back(j); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + return std::move(brute_force_neighbors); |
| 80 | +} |
| 81 | + |
| 82 | +void |
| 83 | +compare_with_bruteforce_search(NeighborhoodSearch const& nsearch) |
| 84 | +{ |
| 85 | + auto brute_force_neighbors = brute_force_search(); |
| 86 | + PointSet const& d0 = nsearch.point_set(0); |
| 87 | + for (int i = 0; i < N; ++i) |
| 88 | + { |
| 89 | + auto const& bfn = brute_force_neighbors[i]; |
| 90 | + if (bfn.size() != d0.n_neighbors(i)) |
| 91 | + { |
| 92 | + std::cerr << "ERROR: Not the same number of neighbors." << std::endl; |
| 93 | + } |
| 94 | + for (int j = 0; j < d0.n_neighbors(i); ++j) |
| 95 | + { |
| 96 | + if (std::find(bfn.begin(), bfn.end(), d0.neighbor(i, j).point_id) == bfn.end()) |
| 97 | + { |
| 98 | + std::cerr << "ERROR: Neighbor not found in brute force list." << std::endl; |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +std::array<Real, 3> |
| 105 | +enright_velocity_field(std::array<Real, 3> const& x) |
| 106 | +{ |
| 107 | + Real sin_pi_x_2 = std::sin(M_PI * x[0]); |
| 108 | + Real sin_pi_y_2 = std::sin(M_PI * x[1]); |
| 109 | + Real sin_pi_z_2 = std::sin(M_PI * x[2]); |
| 110 | + sin_pi_x_2 *= sin_pi_x_2; |
| 111 | + sin_pi_y_2 *= sin_pi_y_2; |
| 112 | + sin_pi_z_2 *= sin_pi_z_2; |
| 113 | + |
| 114 | + Real sin_2_pi_x = std::sin(2.0 * M_PI * x[0]); |
| 115 | + Real sin_2_pi_y = std::sin(2.0 * M_PI * x[1]); |
| 116 | + Real sin_2_pi_z = std::sin(2.0 * M_PI * x[2]); |
| 117 | + return {{ |
| 118 | + 2.0 * sin_pi_x_2 * sin_2_pi_y * sin_2_pi_z, |
| 119 | + -sin_2_pi_x * sin_pi_y_2 * sin_2_pi_z, |
| 120 | + -sin_2_pi_x * sin_2_pi_y * sin_pi_z_2}}; |
| 121 | + |
| 122 | +} |
| 123 | + |
| 124 | +void |
| 125 | +advect() |
| 126 | +{ |
| 127 | +#ifdef _MSC_VER |
| 128 | + concurrency::parallel_for_each |
| 129 | +#else |
| 130 | + __gnu_parallel::for_each |
| 131 | +#endif |
| 132 | + (positions.begin(), positions.end(), [&](std::array<Real, 3>& x) |
| 133 | + { |
| 134 | + std::array<Real, 3> v = enright_velocity_field(x); |
| 135 | + x[0] += 0.005 * v[0]; |
| 136 | + x[1] += 0.005 * v[1]; |
| 137 | + x[2] += 0.005 * v[1]; |
| 138 | + } |
| 139 | + ); |
| 140 | +} |
| 141 | + |
| 142 | +int main(int argc, char* argv[]) |
| 143 | +{ |
| 144 | + Real min_x = std::numeric_limits<Real>::max(); |
| 145 | + Real max_x = std::numeric_limits<Real>::min(); |
| 146 | + positions.reserve(N * N * N); |
| 147 | + for (unsigned int i = 0; i < N; ++i) |
| 148 | + { |
| 149 | + for (unsigned int j = 0; j < N; ++j) |
| 150 | + { |
| 151 | + for (unsigned int k = 0; k < N; ++k) |
| 152 | + { |
| 153 | + std::array<Real, 3> x = {{ |
| 154 | + r_omega * (2.0 * static_cast<Real>(i) / static_cast<Real>(N-1)-1.0), |
| 155 | + r_omega * (2.0 * static_cast<Real>(j) / static_cast<Real>(N-1)-1.0), |
| 156 | + r_omega * (2.0 * static_cast<Real>(k) / static_cast<Real>(N-1)-1.0)}}; |
| 157 | + |
| 158 | + Real l2 = x[0] * x[0] + x[1] * x[1] + x[2] * x[2]; |
| 159 | + if (l2 < r_omega2) |
| 160 | + { |
| 161 | + x[0] += 0.35; |
| 162 | + x[1] += 0.35; |
| 163 | + x[2] += 0.35; |
| 164 | + positions.push_back(x); |
| 165 | + if (min_x > x[0]) |
| 166 | + { |
| 167 | + min_x = x[0]; |
| 168 | + } |
| 169 | + if (max_x < x[0]) |
| 170 | + { |
| 171 | + max_x = x[0]; |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + std::random_shuffle(positions.begin(), positions.end()); |
| 178 | + |
| 179 | + NeighborhoodSearch nsearch(radius, true); |
| 180 | + nsearch.add_point_set(positions.front().data(), positions.size(), true, true); |
| 181 | + nsearch.find_neighbors(); |
| 182 | + |
| 183 | + std::cout << "#Points = " << positions.size() << std::endl; |
| 184 | + std::cout << "Search radius = " << radius << std::endl; |
| 185 | + std::cout << "Min x = " << min_x << std::endl; |
| 186 | + std::cout << "Max x = " << max_x << std::endl; |
| 187 | + std::cout << "Average number of neighbors = " << compute_average_number_of_neighbors(nsearch) << std::endl; |
| 188 | + std::cout << "Average index distance prior to z-sort = " << compute_average_distance(nsearch) << std::endl; |
| 189 | + |
| 190 | + nsearch.z_sort(); |
| 191 | + for (auto const& d : nsearch.point_sets()) |
| 192 | + { |
| 193 | + d.sort_field(positions.data()); |
| 194 | + } |
| 195 | + nsearch.find_neighbors(); |
| 196 | + //compare_with_bruteforce_search(nsearch); |
| 197 | + |
| 198 | + std::cout << "Average index distance after z-sort = " << compute_average_distance(nsearch) << std::endl; |
| 199 | + |
| 200 | + std::cout << "Moving points:" << std::endl; |
| 201 | + for (int i = 0; i < N_enright_steps; ++i) |
| 202 | + { |
| 203 | + std::cout << "Enright step " << i << ". "; |
| 204 | + advect(); |
| 205 | + auto t0 = std::chrono::high_resolution_clock::now(); |
| 206 | + nsearch.find_neighbors(); |
| 207 | + std::cout << "Neighborhood search took " << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - t0).count() << "ms" << std::endl; |
| 208 | + //compare_with_bruteforce_search(nsearch); |
| 209 | + } |
| 210 | + |
| 211 | + return 0; |
| 212 | +} |
0 commit comments