Skip to content

mxthmxn/BrownianMotion

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Brownian Motion Simulation

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.

Overview

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.

Architecture

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

Component Design

Simulation Engine

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:

  • D is the diffusion coefficient
  • dt is 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

Renderer

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

Configuration

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

Data Flow

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

Technical Requirements

  • 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)

Building

Windows (Visual Studio)

mkdir build && cd build
cmake .. -G "Visual Studio 17 2022" -A x64
cmake --build . --config Release
.\Release\BrownianMotion.exe

Windows (MinGW)

mkdir build && cd build
cmake .. -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release
cmake --build .
./BrownianMotion.exe

Linux / macOS

mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j$(nproc)
./BrownianMotion

Usage

Key Action
Space Skip animation, display final result
Esc Exit application

Physical Background

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.

Output

The application generates questions_answers.txt containing explanations of the random number generation methodology and the physical justification for the Gaussian model.

License

MIT License

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors