AutoPas  3.0.0
Loading...
Searching...
No Matches
TwoCellsInteractionHitrateGenerator.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <algorithm>
11#include <array>
12#include <cmath>
13#include <random>
14#include <vector>
15
18
19namespace autopasTools::generators::TwoCellsInteractionHitrateGenerator {
20
57template <class Container_T, class Particle_T>
58void fillWithParticles(Container_T &cell1, Container_T &cell2, const std::array<double, 3> &boxMin1,
59 const std::array<double, 3> &boxMax1, const std::array<double, 3> &boxMin2,
60 const std::array<double, 3> &boxMax2, std::size_t n, double hitrate, double cutoff,
61 const Particle_T &defaultParticle = Particle_T{}, unsigned int seed = 42) {
62 if (std::abs(boxMax1[0] - boxMin2[0]) > 1e-10) {
64 "TwoCellsInteractionHitrateGenerator: cells must share an x-boundary "
65 "(boxMax1[0]={} != boxMin2[0]={})",
66 boxMax1[0], boxMin2[0]);
67 }
68 if ((boxMax1[0] - boxMin1[0]) <= 1.2 * cutoff) {
70 "TwoCellsInteractionHitrateGenerator: cell1 x-extent ({}) must be > 1.2 * cutoff ({})", boxMax1[0] - boxMin1[0],
71 1.2 * cutoff);
72 }
73 if ((boxMax2[0] - boxMin2[0]) <= 1.2 * cutoff) {
75 "TwoCellsInteractionHitrateGenerator: cell2 x-extent ({}) must be > 1.2 * cutoff ({})", boxMax2[0] - boxMin2[0],
76 1.2 * cutoff);
77 }
78 if (hitrate < 0.0 || hitrate > 1.0) {
79 autopas::utils::ExceptionHandler::exception("TwoCellsInteractionHitrateGenerator: hitrate ({}) must be in [0, 1]",
80 hitrate);
81 }
82
83 const double boundary = boxMax1[0];
84 const auto k = static_cast<std::size_t>(std::round(static_cast<double>(n) * hitrate));
85 const std::size_t farCount = n - k;
86 const auto idBase = static_cast<std::size_t>(defaultParticle.getID());
87
88 std::mt19937 rng(seed);
89
90 // Small offset so paired particles are strictly inside the cutoff sphere and their cell bounds.
91 const double eps = cutoff * 1e-6;
92 std::uniform_real_distribution<double> dxDist(eps, cutoff / 2.0 - eps);
93
94 auto uniform = [&](double lo, double hi) { return std::uniform_real_distribution<double>(lo, hi)(rng); };
95
96 std::vector<Particle_T> particles1, particles2;
97 particles1.reserve(n);
98 particles2.reserve(n);
99
100 // k paired (in-range) particles: 3D distance between partners = 2·dx < cutoff
101 for (std::size_t i = 0; i < k; ++i) {
102 const double dx = dxDist(rng);
103 const double y = uniform(boxMin1[1], boxMax1[1]);
104 const double z = uniform(boxMin1[2], boxMax1[2]);
105
106 Particle_T p1(defaultParticle);
107 p1.setR({boundary - dx, y, z});
108 p1.setID(idBase + i);
109 p1.setOwnershipState(autopas::OwnershipState::owned);
110 particles1.push_back(p1);
111
112 Particle_T p2(defaultParticle);
113 p2.setR({boundary + dx, y, z});
114 p2.setID(idBase + n + i);
115 p2.setOwnershipState(autopas::OwnershipState::owned);
116 particles2.push_back(p2);
117 }
118
119 // n-k far particles in cell1: x-distance to any cell2 particle >= cutoff
120 for (std::size_t i = 0; i < farCount; ++i) {
121 Particle_T p(defaultParticle);
122 p.setR({uniform(boxMin1[0], boundary - cutoff), uniform(boxMin1[1], boxMax1[1]), uniform(boxMin1[2], boxMax1[2])});
123 p.setID(idBase + k + i);
124 p.setOwnershipState(autopas::OwnershipState::owned);
125 particles1.push_back(p);
126 }
127
128 // n-k far particles in cell2: x-distance to any cell1 particle >= cutoff
129 for (std::size_t i = 0; i < farCount; ++i) {
130 Particle_T p(defaultParticle);
131 p.setR({uniform(boundary + cutoff, boxMax2[0]), uniform(boxMin2[1], boxMax2[1]), uniform(boxMin2[2], boxMax2[2])});
132 p.setID(idBase + n + k + i);
133 p.setOwnershipState(autopas::OwnershipState::owned);
134 particles2.push_back(p);
135 }
136
137 std::shuffle(particles1.begin(), particles1.end(), rng);
138 std::shuffle(particles2.begin(), particles2.end(), rng);
139
140 for (auto &p : particles1) {
141 cell1.addParticle(p);
142 }
143 for (auto &p : particles2) {
144 cell2.addParticle(p);
145 }
146}
147
148} // namespace autopasTools::generators::TwoCellsInteractionHitrateGenerator
static void exception(const Exception e)
Handle an exception derived by std::exception.
Definition: ExceptionHandler.h:64
@ owned
Owned state, a particle with this state is an actual particle and owned by the current AutoPas object...