-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Random walk example, Improve vec class
- Loading branch information
1 parent
8cf2f13
commit f48c485
Showing
3 changed files
with
97 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
|
||
/// | ||
/// @file random_walk.cpp A 3D random walk generator | ||
/// This example may be compiled using 'make random_walk' | ||
/// | ||
|
||
#include "theoretica.h" | ||
using namespace th; | ||
|
||
#include <fstream> | ||
#include <ctime> | ||
#include <algorithm> | ||
|
||
|
||
// You can plot the data file using gnuplot: | ||
// splot "random_walk.dat" with lines title "3D Random Walk" | ||
|
||
|
||
int main(int argc, char* argv[]) { | ||
|
||
// Number of steps | ||
const unsigned int N = 10000; | ||
|
||
// Output file | ||
std::ofstream file("random_walk.dat"); | ||
|
||
// Random number generator | ||
PRNG g = PRNG::xoshiro(time(nullptr)); | ||
|
||
// Trajectory | ||
std::vector<vec3> pos(N); | ||
|
||
// Start from the origin | ||
pos[0] = {0, 0, 0}; | ||
file << pos[0].to_string(" ", false) << std::endl; | ||
|
||
for (unsigned int i = 1; i < N; ++i) { | ||
|
||
// Generate a random vector with fixed length | ||
// You can change the generation of r to test | ||
// different distributions (e.g. rand_gaussian(0, 1, g)) | ||
const real r = 1; | ||
const real theta = rand_uniform(0, PI, g); | ||
const real phi = rand_uniform(0, TAU, g); | ||
|
||
// Update the trajectory | ||
pos[i] = pos[i - 1] + vec3({ | ||
r * sin(theta) * cos(phi), | ||
r * sin(theta) * sin(phi), | ||
r * cos(theta) | ||
}); | ||
|
||
file << pos[i].to_string(" ", false) << std::endl; | ||
} | ||
|
||
|
||
// Compute the mean direction of the steps | ||
vec3 mean_dir = {0, 0, 0}; | ||
for (unsigned int i = 1; i < N; ++i) | ||
mean_dir += (pos[i] - pos[i - 1]) / N; | ||
|
||
std::cout << "Mean Direction: " << mean_dir << std::endl; | ||
|
||
|
||
// Compute the RMS displacement | ||
real sqr_disp = 0; | ||
for (unsigned int i = 0; i < N; ++i) | ||
sqr_disp += pos[i] * pos[i] / N; | ||
|
||
std::cout << "RMS Displacement: " << sqrt(sqr_disp) << std::endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters