AutoPas  3.0.0
Loading...
Searching...
No Matches
GaussianGenerator.h
Go to the documentation of this file.
1
7#pragma once
8
9#include <random>
10
13#include "autopas/utils/inBox.h"
14
23constexpr size_t _maxAttempts = 100;
24
37template <class Container>
38void fillWithParticles(Container &container, const std::array<double, 3> &boxMin, const std::array<double, 3> &boxMax,
39 size_t numParticles,
40 const typename autopas::utils::ParticleTypeTrait<Container>::value &defaultParticle =
42 const std::array<double, 3> &distributionMean = {5., 5., 5.},
43 const std::array<double, 3> &distributionStdDev = {2., 2., 2.}) {
44 std::default_random_engine generator(42);
45 std::array<std::normal_distribution<double>, 3> distributions = {
46 std::normal_distribution<double>{distributionMean[0], distributionStdDev[0]},
47 std::normal_distribution<double>{distributionMean[1], distributionStdDev[1]},
48 std::normal_distribution<double>{distributionMean[2], distributionStdDev[2]}};
49
50 for (unsigned long i = defaultParticle.getID(); i < defaultParticle.getID() + numParticles; ++i) {
51 std::array<double, 3> position = {distributions[0](generator), distributions[1](generator),
52 distributions[2](generator)};
53 // verifies that position is valid
54 for (size_t attempts = 1; attempts <= _maxAttempts and (not autopas::utils::inBox(position, boxMin, boxMax));
55 ++attempts) {
56 if (attempts == _maxAttempts) {
57 std::ostringstream errormessage;
58 errormessage << "GaussianGenerator::fillWithParticles(): Could not find a valid position for particle " << i
59 << "after" << _maxAttempts << "attempts. Check if your parameters make sense:" << std::endl
60 << "BoxMin = " << autopas::utils::ArrayUtils::to_string(boxMin) << std::endl
61 << "BoxMax = " << autopas::utils::ArrayUtils::to_string(boxMax) << std::endl
62 << "Gauss mean = " << autopas::utils::ArrayUtils::to_string(distributionMean) << std::endl
63 << "Gauss stdDev = " << autopas::utils::ArrayUtils::to_string(distributionStdDev) << std::endl;
64 throw std::runtime_error(errormessage.str());
65 }
66 position = {distributions[0](generator), distributions[1](generator), distributions[2](generator)};
67 };
68 auto p = defaultParticle;
69 p.setR(position);
70 p.setID(i);
71 container.addParticle(p);
72 }
73}
74} // namespace autopasTools::generators::GaussianGenerator
Generator class for gaussian distributions.
Definition: GaussianGenerator.h:18
constexpr size_t _maxAttempts
Maximum number of attempts the random generator gets to find a valid position before considering the ...
Definition: GaussianGenerator.h:23
void fillWithParticles(Container &container, const std::array< double, 3 > &boxMin, const std::array< double, 3 > &boxMax, size_t numParticles, const typename autopas::utils::ParticleTypeTrait< Container >::value &defaultParticle=typename autopas::utils::ParticleTypeTrait< Container >::value(), const std::array< double, 3 > &distributionMean={5., 5., 5.}, const std::array< double, 3 > &distributionStdDev={2., 2., 2.})
Fills any container (also AutoPas object) with randomly 3D gaussian distributed particles.
Definition: GaussianGenerator.h:38
void to_string(std::ostream &os, const Container &container, const std::string &delimiter, const std::array< std::string, 2 > &surround, Fun elemToString)
Generates a string representation of a container which fulfills the Container requirement (provide cb...
Definition: ArrayUtils.h:54
bool inBox(const std::array< T, 3 > &position, const std::array< T, 3 > &low, const std::array< T, 3 > &high)
Checks if position is inside of a box defined by low and high.
Definition: inBox.h:26
typename Container::ParticleType value
The Particle Type.
Definition: ParticleTypeTrait.h:24