Finite-difference solver and OpenGL/GLUT visualization for the one-dimensional wave equation with fixed ends. Eigen is included as a Git submodule.
git submodule update --init --recursive
cmake --preset msys2-clang64
cmake --build --preset msys2-clang64
ctest --preset msys2-clang64Run the simulation and visualization:
./build/msys2-clang64/string-waveThe physical and grid parameters remain constants in main/main.cpp, as in the
original project. OpenGL and GLUT (or FreeGLUT) must be installed on the system.
The included Windows preset uses the installed MSYS2 Clang64 toolchain and keeps
it separate from Visual Studio build files.
The project separates numerical integration from presentation:
WaveSimulationowns the finite-difference operator and the three rolling displacement vectors. It has no dependency on OpenGL or GLUT.WaveProblemis supplied by the application and contains all simulation constants together with the initial and boundary-condition functions.SimulationObserverdefines the notification contract for a completed time step.OpenGLVisualizerimplementsSimulationObserver. Its GLUT timer advances the simulation, while observer notifications request screen redraws.main.cppis the composition root that creates the simulation and visualizer.
This direction of dependency keeps the mathematical model reusable in tests or
other frontends. A new observer can record selected frames, calculate energy,
or export data without changing WaveSimulation.
Let
Here
where
The boundary conditions are
Both functions currently return zero, so the string is fixed at both ends. The initial conditions are
The problem configured in main.cpp supplies zero initial velocity and an
initial-displacement function equal to
Space and time are discretised on uniform grids:
For every interior point, second-order central differences give
Solving for the next time layer produces the explicit update
The implementation writes this update in matrix form:
Eigen stores
The current implementation initializes the first time layer as
Since the current initial velocity is zero,
| Parameter | Value | Meaning |
|---|---|---|
0.5 |
string length | |
10 |
simulated duration | |
0.2 |
wave speed | |
0.0005 |
spatial step | |
0.00025 |
time step | |
1001 |
number of spatial points | |
40001 |
number of time layers | |
0.1 |
Courant number |
The initial displacement separates into two waves travelling from the centre towards the fixed endpoints. A wave reflected from a fixed endpoint changes sign and travels back along the string.
This is an ideal model: it contains no damping, external forcing, bending stiffness, or nonlinear effects. Its physical energy is therefore expected to remain constant up to discretisation and floating-point errors.
OpenGL renders each computed time layer as the curve
This project is available under the MIT License.