sandbox/bugs/periodic.c

Advection of a cos function (problem with periodic boundary conditions)

This is specific to cartesian1D.h, a simple workaround is to replace this with multigrid1D.h

#if 1 // doesn't work
# include "grid/cartesian1D.h"
#else // works
# include "grid/multigrid1D.h"
#endif
#include "utils.h"
    
double uadv = 1.; 

void flux_upwind (scalar f, scalar df, double dt) 
{
    foreach()
        df[] = f[]*uadv*dt;
    boundary ({df});
}

int main() {
    int n = 100;
    double CFL = 0.1;
    double t = 0., tend = 1.; 
    double dt = CFL/n/uadv;

    init_grid (n);

    periodic (right); 

    scalar f[], df[];
    foreach() {
        f[] = cos(2.*M_PI*x);
    }   
    boundary ({f});


    FILE * fp    = fopen ("function.dat", "w");

    for (int i = 0; t <= tend; i++) {
    
      // we obtain the flux
      flux_upwind (f, df, dt);

      // numerical solution at t+dt
      foreach()
        f[] -= (df[]-df[-1])/Δ;
      boundary ({f});
 
      if (i % 100 == 0) {
        foreach ()  
          fprintf(fp, "%g %g %g \n", t, x, f[]);
        fprintf(fp, " \n");
      }   

      t += dt; 
    }   


    fclose(fp);

    free_grid();
}

Temporal evolution