Molecular Dynamics  v0.4
Project for the Practical hosted by the Scientific Computing Chair
MaxwellBoltzmannDistribution.h
Go to the documentation of this file.
1 /*
2  * MaxwellBoltzmannDistribution.h
3  *
4  * @Date: 13.12.2019
5  * @Author: F. Gratl
6  */
7 
8 #pragma once
9 
10 #include <random>
11 #include <array>
12 
20 inline std::array<double, 3> maxwellBoltzmannDistributedVelocity(double averageVelocity, size_t dimensions) {
21  // we use a constant seed for repeatability.
22  // random engine needs static lifetime otherwise it would be recreated for every call.
23  static std::default_random_engine randomEngine(42);
24  // when adding independent normally distributed values to all velocity components
25  // the velocity change is maxwell boltzmann distributed
26  std::normal_distribution<double> normalDistribution{0, 1};
27  std::array<double, 3> randomVelocity{};
28  for (size_t i = 0; i < dimensions; ++i) {
29  randomVelocity[i] = averageVelocity * normalDistribution(randomEngine);
30  }
31  return randomVelocity;
32 }
std::array< double, 3 > maxwellBoltzmannDistributedVelocity(double averageVelocity, size_t dimensions)
Definition: MaxwellBoltzmannDistribution.h:20