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:

u0(x,y)=(u0,v0)=(cos(y),0),

for abs(y)π/2. Due to the fluid’s viscousity (ν), momemtum will diffuse over time, according to the (Navier-)Stokes equation, that is solved by;

u(t)=u0eν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);
  u.t[top]=dirichlet(0);
  u.t[bottom]=dirichlet(0.);
  μ=muv;
  L0=M_PI;
  X0=Y0=-L0/2.;

Six experiments are run, the zeroth one (j=0) is not used because the timestepper thinks it has made a timestep with Δt=0 for the -1-th timestep.

  for (j=0;j<7;j++){
    timestepp=2.*pow(0.5,(double)j);
    run();
  }
}

event init(t=0){
  CFL=10000.;
  foreach()
    u.x[]=cos(y);
  boundary(all);
  DT=timestepp;
}

This event is used to let the run have an equidistant timestep for j>0.

event setDT(i++){
if (i==0){
    DT=1.1*timestepp/0.1;
  }else{
    DT=timestepp;
  }
}

Output

After a single ‘1/e’ timescale, the error in the numerically obtained solution is evaluated.

event check(t=1.){
  static FILE * fp = fopen("resultvisc.dat","w");
  double err=0.;
  foreach()
    err+=fabs(u.x[]-cos(y)*exp(-t))*sq(Δ);
  if (j>0)
    fprintf(fp,"%d\t%g\n",i,err);
}

Results

A first-order accurate (global) timestepping can be diagnosed?

A first-order accurate (global) timestepping can be diagnosed?