sandbox/Antoonvh/timeaccuracy.c
The Navier-Stokes solver is second-order accurate in time
Let us test that for a 1D viscous-flow problem, evaluated on a 2D grid.
##set-up The set-up consists of a periodic (left-right) channel with no-slip boundary conditions (top-bottom). The initialized flow is:
\overrightarrow{u_0}(x,y)= (u_0,v_0) =(\mathrm{cos}(y),0),
for $(y) /2 . Due to the fluid's viscousity ($), momemtum will diffuse over time, according to the (Navier-)Stokes equation, that is solved by;
\overrightarrow{u}(t)= \overrightarrow{u_0}e^{-\nu t}.
This equation will help to determine the error due to the time integration.
#include "grid/multigrid.h"
#include "navier-stokes/centered.h"
const face vector muv[]={1.,1.};
double timestepp;
int j;
int main(){
The grid resolution is chosen so fine that the presented errors here are dominated by the time stepping and are not significantly affected by the spatial discretization.
init_grid(1<<9);
periodic(left);
.t[top]=dirichlet(0);
u.t[bottom]=dirichlet(0.);
u=muv;
mu=M_PI;
L0=Y0=-L0/2.; X0
Six experiments are run, the zeroth one (j=0) is not used because the timestepper thinks it has made a timestep with \Delta t = 0 for the -1-th timestep.
for (j=0;j<7;j++){
=2.*pow(0.5,(double)j);
timestepprun();
}
}
event init(t=0){
=10000.;
CFLforeach()
.x[]=cos(y);
uboundary(all);
=timestepp;
DT}
This event is used to let the run have an equidistant timestep for j>0.
event setDT(i++){
if (i==0){
=1.1*timestepp/0.1;
DT}else{
=timestepp;
DT}
}
Output
After a single ‘1/e’ timescale, the error in the numerically obtained solution is evaluated.
+=fabs(u.x[]-cos(y)*exp(-t))*sq(Delta);
errif (j>0)
fprintf(fp,"%d\t%g\n",i,err);
}
Results
set xr [0.5:40]
set yr [0.02:1]
set logscale y
set logscale x
set xlabel '{Used number of Timesteps}'
set ylabel 'Total Error'
set key box 1
set size square
plot (1*x**-1) lw 3 lc rgb 'purple' title '∝{dt}^{-1}',\
'resultvisc.dat' using 1:2 pt 4 title 'Error at t=1'