MolSim
Loading...
Searching...
No Matches
MaxwellBoltzmannDistribution.h
Go to the documentation of this file.
1
11#pragma once
12
13#include <array>
14#include <random>
15
26inline std::array<double, 3> maxwellBoltzmannDistributedVelocity(double averageVelocity, size_t dimensions) {
27 // we use a constant seed for repeatability.
28 // random engine needs static lifetime otherwise it would be recreated for
29 // every call.
30 static std::default_random_engine randomEngine(42);
31
32 // when adding independent normally distributed values to all velocity
33 // components the velocity change is maxwell boltzmann distributed
34 std::normal_distribution<double> normalDistribution{0, 1};
35 std::array<double, 3> randomVelocity{};
36 for (size_t i = 0; i < dimensions; ++i) {
37 randomVelocity[i] = averageVelocity * normalDistribution(randomEngine);
38 }
39 return randomVelocity;
40}
std::array< double, 3 > maxwellBoltzmannDistributedVelocity(double averageVelocity, size_t dimensions)
Generate a random velocity vector according to the Maxwell-Boltzmann distribution,...
Definition MaxwellBoltzmannDistribution.h:26