sandbox/YiDai/BASI/advec1D.c
1D advection equation
\partial_{t}T + c \partial_{x}T=0
Lax - Friedrichs method is used to avoid the instablity of Forward-Time Central-Space (FTCS) Method.
#include "grid/cartesian1D.h"
#include "run.h"
scalar T[], dT[];
double dt;
double alpha = 1;
int main()
{
// periodic(left);
= 4 * pi;
L0 = -L0 / 2.;
X0 = 1 << 9;
N // stable condition of Lax - Friedrichs method dt < dx/c
= L0 / N / alpha;
DT run();
}
[left] = dirichlet(0.);
T[right] = dirichlet(0.);
T
event init(t = 0)
{
foreach ()
{
[] = 0.5 * (fabs(x) < 1);
T}
boundary({T});
}
// event printdata (t = 0; t <= 1000 * DT; t += 100 * DT) {
event printdata(t = 0; t <= 1; t += 0.1)
{
// event printdata (t = 0.5) {
static FILE *fp = fopen("AD1D", "w");
foreach ()
fprintf(fp, "%g %g %g %g\n", x, T[], dT[], t);
fprintf(fp, "\n\n");
fflush(fp);
}
event integration(i++)
{
double dt = DT;
= dtnext(dt);
dt foreach ()
// Lax - Friedrich method
[] = -alpha * (T[1] - T[-1]) / (2 * Delta);
dTforeach ()
[] = 0.5 * (T[1] + T[-1]) + dt * dT[];
Tboundary({T});
}
The results after some time steps become unstable a bit
reset
file="AD1D"
set terminal png size 1200,600 enhanced font 'Times-Roman,16'
set key samplen 2 spacing 1.5 font 'Times-Roman,16'
set key bottom right
plot[][-0.05:0.7] file u ($1):($2) t "T" w l