sandbox/Antoonvh/tp.c
Particle advection test
The order of accuracy with a time-dependent flow field is diagnosed for two cases.
- The timestep is highly variable, to represent a worst-case scenario
(\alpha \in \langle0.01,
0.99\rangle).
- The timestep is fixed to represent the best-case scenario
An approximated particle path
set xr [100:3000]
set logscale y
set logscale x 2
set xlabel '#Steps'
set ylabel 'Error'
set grid
set size square
set key right outside
plot 'rk2log' t 'Rk2 fixed step', 'rk2out' t 'RK2 variable step',\
*x**(-2) lw 2 t '2nd order', 'log' t 'RK3 fixed step',\
9e3'out' t 'RK3 variable step', 2e5*x**(-3) lw 2 t '3rd order'
vector u[];
#include "view.h"
#define BVIEW 1
#include "particles.h"
int its;
double Ub = 1, amp = 1.5, periods = 2.412;
bool fixed = false;
int roundUp (int num, int mult) {
int rem = num % mult;
if (rem == 0)
return num;
return num + mult - rem;
}
FILE * fp;
int main () {
= 3;
L0 = Y0 = -L0/2;
X0 = fopen ("rk2out", "w");
fp for (DT = 0.1; DT > 0.005; DT /= 2) {
= roundUp (periods*2*pi/DT, 2);
its run();
}
= true;
fixed fclose (fp);
= fopen ("rk2log", "w");
fp for (DT = 0.1; DT > 0.005; DT /= 2) {
= roundUp (periods*2*pi/DT, 2);
its run();
}
fclose (fp);
= false;
fixed = true;
P_RK3 = stdout;
fp for (DT = 0.1; DT > 0.005; DT /= 2) {
= roundUp (periods*2*pi/DT, 2);
its run();
}
= true;
fixed = stderr;
fp for (DT = 0.1; DT > 0.005; DT /= 2) {
= roundUp (periods*2*pi/DT, 2);
its run();
}
}
event init (t = 0) {
= true;
start = 1;
n_part = malloc (sizeof(coord));
loc [0] = (coord){1., 0};
loc= dtnext (DT);
dt }
event set_dtmax (i++) {
if (fixed)
= dtnext (DT);
dt
else= dtnext ((0.99*noise() + 1)*DT);
dt }
event advance_particles (i++) {
foreach() {
= {y, -x};
coord v foreach_dimension()
.x[] = (amp*sin(t) + Ub)*v.x;
u}
boundary (all);
}
event movie (i += 4) {
if (DT < 0.06 && DT > 0.03 && fixed && P_RK3) {
box();
scatter (loc);
save ("mov.mp4");
}
}
Diagnose the approximate particle locations
loc_n
only represents the approximate particle positions
corresponding to t=\mathtt{t} at even
iterations. loc
stores intermediate RK-related guesses.