I chose trajectory analysis for 3D particle motion. The baseline computes the mean-squared displacement (MSD) as a function of lag time and estimates the diffusion coefficient from the linear MSD regime.
Given unwrapped trajectories (_p(t)) (particle index (p), frame index (t)), the MSD at lag () is
[ ()= _{p=0}{N-1}_{t_0=0}{T-} _p(t_0+)-_p(t_0)^2, ]
where (N) is number of particles and (T) is number of frames.
For ideal 3D Brownian motion, ((t)=6Dt), so if we fit ((t)a t+b) on a user-chosen interval, (D = a/6).
dt is in arbitrary time units.D is length(^2)/time.File: code/msd_diffusion.cc (self-contained C++17,
standard library only).
--particles, --frames,
--max-lag, --dt, --diffusion,
--seed--fit-start, --fit-end,
--csv, --run-checksmax_lag < frames, valid fit
interval, nonnegative diffusion1..max_lag:
[fit_start, fit_end] by ordinary
least squares:
D_est = slope/6lag,time,msd,count,expected_brownian_msdThe program can run built-in checks (--run-checks 1,
default):
r(t)=const): MSD
should be zero.r(t)=v t): MSD
should equal (|v|^2(dt)^2).These checks are deterministic (fixed seeds and fixed parameters).
This intentionally plain serial implementation is useful as a baseline before parallel optimization.
In this course notebook workflow:
%%bash (runs on a login node; keep this
short),%load_ext miyabi,%%sub (batch job on a compute
node).Example cells (cd code first):
# compile cell
%%bash
cd code
module purge && module load nvidia/25.9 cuda/12.9 && PATH=/work/gt81/share/env/llvm/bin:$PATH
clang++ -O3 -Wall -std=c++17 msd_diffusion.cc -o msd_diffusion# one-time initialization cell
%load_ext miyabi# run cell (compute node)
%%sub
cd code
./msd_diffusion --particles 2000 --frames 2000 --max-lag 400 --dt 0.01 --diffusion 1.0 --seed 12345 --fit-start 50 --fit-end 200 --csv msd.csv --run-checks 1For quick tests, reduce particles/frames/lags. For heavier serial runs, increase them but keep total runtime within assignment limits.
The following numbers are from an actual notebook %%sub
run on compute node mg1098 (lecture-mig, job
3750947) with:
--particles 2000 --frames 2000 --max-lag 400 --dt 0.01--diffusion 1 --seed 12345 --fit-start 50 --fit-end 200Observed checks and outputs:
max_abs_msd = 0, PASSmax_relative_error = 1.981250e-13, PASS
(tol 1e-12)mean_abs_relative_error = 0.009419749, PASS
(tol 0.12)2.698377, MSD
1.988603, CSV+fit 0.001778412, total
4.6887585.960500, intercept
0.02969518, D_est = 0.9934167, relative error
0.006583311msd.csv has 402 lines (header
+ lag 0..400)The estimated diffusion is statistically consistent with the Brownian
target in this finite-sample run: D_est is close to
D_true = 1, and MSD-vs-time remains near the expected
linear trend.
Major parallel opportunities:
Likely future mappings:
Potential speedup limits:
I should proofread and rewrite this in my own words before submission, because classmates will read it and I am responsible for the final text.