Solitary waves are a source of happiness to a broad audiance. Image via ESA science.

    Solitary waves are a source of happiness to a broad audiance. Image via ESA science.

    Soliton Solutions to the Korteweg-De Vries Equation

    So-called soliton solutions may exist as a special case to special equations. This entails that a solution shape, while it moves through space, does not deform over time. Meaning that observations of such phenomenon may be expected to be either very rare (special solutions to special equations) or very common (since they persist over time for many to observe). In this example we follow the soliton solution as listed by wikipedia page on the Korteweg-De Vries equation. Fortunately, there is a solver to this equation in my sandbox.

    #include "grid/multigrid1D.h"
    #include "KdV.h"
    #include "run.h"

    We study two soliton solutions, both described by:

    \displaystyle c(x,t) = -\frac{v}{2 \mathrm{cosh}^2\left(\frac{\sqrt{v}}{2}(x - vt - b)\right)}

    for v = 1 and v = 2. To omit the most prominent issues at the boundaries, we place them far away from the region of interest.

    scalar C[], CC[];
    int main(){
      L0 = 50;
      X0 = -L0/2;


    We initialize the solution fields and we also, to help visualize the data, we open a pipeline for output plots with gnuplot.

    FILE * gnuplotPipe;
    event init(t = 0){
      double v = 2;
        C[] = (-v/(2.*sq(cosh(pow(v, 0.5)*0.5*(x + 12)))) -
    	   1/(2.*sq(cosh(0.5*(x + 3)))));
      DT = 0.01;
      gnuplotPipe = popen ("gnuplot", "w");
            "set term pngcairo\n"
            "set xr [-15: 15]\n"
            "set yr [-1.8: 0.1]\n"
            "set key bottom left\n"
            "set grid\n"
            "set title 'KdV Solitons'\n"
            "set xlabel 'x'\n"
            "set ylabel 'c'\n");

    Time integration

    User convienience is provided via the KdV()-fuction interface.

    event advance(i++) { 
      dt = dtnext (DT);
      KdV({C, CC}, dt);

    Output many plots

    Each two time steps we output a plot that is produced via the pipe.

    int frame = 0;
    event movie(i += 2){
      fprintf(gnuplotPipe, "set output 'plot%d.png'\n", frame);
      fprintf(gnuplotPipe, "plot '-' w l lw 5 t 'C', '-' w l lw 3 t 'CC'\n");
        fprintf(gnuplotPipe, "%g %g\n",x, C[]);
      fprintf(gnuplotPipe, "e\n");
        fprintf(gnuplotPipe, "%g %g\n",x, CC[]);
      fprintf(gnuplotPipe, "e\n");

    Creation of a movie from plots

    We use Gnu/Linux commands and ffmpeg to generate a movie from all our plots.

    event stop (t = 12){
      system("rm mov.mp4");
      system("ffmpeg -r 25 -f image2 -i plot%d.png -c:v libx264 -vf format=yuv420p -y mov.mp4");
      system("rm plot*");
      return 1;


    We can check if the solutions are indeed solitons.

    Well done KdV() function

    See also