A high-performance C++ simulation and visualization of Brownian motion using the Wiener process model. The application provides real-time rendering of particle trajectories with a state-machine based visualization system.
This project implements a physically accurate simulation of Brownian motion, the random movement of particles suspended in a fluid resulting from collisions with molecules in the surrounding medium. The mathematical foundation is the Wiener process (standard Brownian motion), which models the cumulative effect of countless molecular impacts.
src/
├── main.cxx # Application entry point, orchestrates components
├── Config.hxx # Compile-time configuration and constants
├── Simulation.hxx/cxx # Physics engine implementing Wiener process
├── Renderer.hxx/cxx # SFML-based visualization with state machine
└── QuestionsWriter.hxx/cxx # Educational content generator
The core physics module implements the Wiener process using discrete-time increments:
dX(t) = sqrt(2 * D * dt) * N(0,1)
dY(t) = sqrt(2 * D * dt) * N(0,1)
Where:
Dis the diffusion coefficientdtis the time step (1 ms)N(0,1)is a standard normal random variable
The random number generation uses std::mt19937_64 (Mersenne Twister) seeded from hardware entropy via std::random_device. This provides a period of 2^19937-1 and passes the Diehard statistical tests, ensuring high-quality randomness for accurate physical modeling.
Key implementation details:
- Pre-computed scaling factor
sqrt(2 * D * dt)to minimize per-step calculations - Memory pre-allocation based on simulation duration
- Waypoint recording at configurable intervals for result visualization
The visualization system operates as a finite state machine with two distinct states:
| State | Description | Visual Style |
|---|---|---|
Animation |
Real-time particle trajectory with fading trail | Dark background, cyan particle |
Result |
Static waypoint display mimicking laboratory paper | Beige background, ink-colored markers |
The renderer implements:
- Unified coordinate transformation system ensuring consistent scaling between states
- Automatic bounds calculation from trajectory data
- UTF-8 to sf::String conversion for Cyrillic text support
- Frame-rate independent animation using accumulator pattern
All tunable parameters are centralized in Config.hxx as compile-time constants within the Config namespace:
- Window dimensions and title
- Physics parameters (time step, duration, diffusion coefficient)
- Visualization settings (particle size, trail thickness, animation speed)
- Color definitions for both application states
main.cxx
│
├──> QuestionsWriter::write() // Generate educational output
│
├──> Simulation::run() // Execute physics simulation
│ │
│ └──> generateIncrement() // Wiener process step
│
└──> Renderer::run() // Visualization loop
│
├──> handleEvents() // Input processing
├──> renderAnimation() // State: Animation
└──> renderResult() // State: Result
- Compiler: C++20 compliant (MSVC 2019+, GCC 10+, Clang 12+)
- Build System: CMake 3.16+
- Dependencies: SFML 2.6.1 (fetched automatically via CMake FetchContent)
mkdir build && cd build
cmake .. -G "Visual Studio 17 2022" -A x64
cmake --build . --config Release
.\Release\BrownianMotion.exemkdir build && cd build
cmake .. -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release
cmake --build .
./BrownianMotion.exemkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j$(nproc)
./BrownianMotion| Key | Action |
|---|---|
Space |
Skip animation, display final result |
Esc |
Exit application |
The choice of Gaussian distribution for position increments is not arbitrary. The Central Limit Theorem states that the sum of a large number of independent random variables with finite variance converges to a normal distribution. A Brownian particle experiences approximately 10^21 molecular collisions per second, making the Gaussian approximation mathematically rigorous for any observable time scale.
The simulation generates positions at 1 ms intervals (10,000 steps for a 10-second simulation) and records waypoints every 0.5 seconds, producing 21 marked positions for the final display.
The application generates questions_answers.txt containing explanations of the random number generation methodology and the physical justification for the Gaussian model.
MIT License