sandbox/YiDai/BASI/heat2D.c
2D Heat equation
we solve T(x, y, t) \frac{\partial T}{\partial t} = c^{2} (\frac{\partial^2 T}{\partial x^2} + \frac{\partial^2 T}{\partial y^2})
The discretization form of the equation: \frac{\partial^2 T}{\partial x^2} |_{(i, j)} = \frac{1}{\Delta_x^{2}} [T_{i+1, j} - 2 T_{i, j} + T_{i-1, j}] \frac{\partial^2 T}{\partial y^2} |_{(i, j)} = \frac{1}{\Delta_y^{2}} [T_{i, j+1} - 2 T_{i, j} + T_{i, j-1}]
#include "run.h"
#include "diffusion.h"
#include "view.h"
scalar T1[], dTx[], dTy[];
scalar T2[];
scalar T3[];
double dt;
double alpha;
#define EPS 1.1
int main()
{
= 20;
L0 = -L0 / 2.;
X0 = -L0 / 2.;
Y0 = 1 << 6;
N = sq(L0 / N) / 4;
DT run();
}
[left] = dirichlet(3.);
T3[top] = dirichlet(0.);
T3
event init(t = 0)
{
foreach ()
{
[] = 10. * (sqrt(sq(x) + sq(y)) < EPS) / 2;
T1[] = y >= 0 ? 3 : 0;
T2[] = y >= 0 ? 3 : 0;
T3}
boundary({T1, T2, T3});
}
event integration(i++)
{
double alpha = 1; // uniform material
= sq(L0 / (1 << grid->maxdepth)) / 4.; // smaller time step when the grid is refined
DT double dt = DT;
= dtnext(dt);
dt foreach ()
{
[] = (T2[1, 0] - 2 * T2[0, 0] + T2[-1, 0]) / sq(Delta);
dTx[] = (T2[0, 1] - 2 * T2[0, 0] + T2[0, -1]) / sq(Delta);
dTy}
foreach ()
[] += dt * alpha * (dTx[] + dTy[]);
T2boundary({T2});
}
mgstats mgb1, mgb3;
event Diffusion(i++)
{
= sq(L0 / (1 << grid->maxdepth)) / 3.;
DT double dt = DT;
= diffusion(T1, dt);
mgb1 = diffusion(T3, dt);
mgb3 // boundary({T1, T3});
}
Let try different output options:
- directly print
- output_field
- output_ppm
- save movies
event printdata(t = 0; t <= 1; t += 0.05)
{
static FILE *fp = fopen("output_H2DT1", "w");
for (double y = -L0 / 2; y < L0 / 2; y += L0 / N)
{
fprintf(fp, "%g %g\n", y, interpolate(T1, 0, y));
}
fprintf(fp, "\n \n");
fflush(fp);
static FILE *fp2 = fopen("output_H2DT2", "w");
for (double y = -L0 / 2; y < L0 / 2; y += L0 / N)
{
fprintf(fp2, "%g %g\n", y, interpolate(T2, 0, y));
}
fprintf(fp2, "\n \n");
fflush(fp2);
static FILE *fp3 = fopen("output_H2DT3", "w");
for (double y = -L0 / 2; y < L0 / 2; y += L0 / N)
{
fprintf(fp3, "%g %g\n", y, interpolate(T3, 0, y));
}
fprintf(fp3, "\n \n");
fflush(fp3);
}
event outmatrix(t = 0.2)
{
FILE *fp1 = fopen("outputfield_H2DT1", "w");
output_field({T1}, fp1);
FILE *fp2 = fopen("outputfield_H2DT2", "w");
output_field({T2}, fp2);
FILE *fp3 = fopen("outputfield_H2DT3", "w");
output_field({T3}, fp3);
}
event movie(t = 0; t <= 0.4; t += 0.01)
{
output_ppm(T1, file = "T1_ap.mp4", min = 0, max = 5, linear = true, map = cool_warm);
output_ppm(T2, file = "T2_ap.mp4", min = 0, max = 5, linear = true, map = cool_warm);
output_ppm(T3, file = "T3_ap.mp4", min = 0, max = 5, linear = true, map = cool_warm);
}
event mov(i++)
{
squares("T1", linear = true, min = -0.1, max = 5, map = cool_warm); // not sure why my cells overlay on Tsquare
cells();
save("grid.mp4");
}
event adapt(i++)
{
({T1, T2, T3}, (double[]){3e-1, 3e-1, 3e-1}, minlevel = 4, maxlevel = 8);
adapt_wavelet}
file="H2DT"
set terminal png size 1200,600 enhanced font 'Times-Roman,16'
set key samplen 2 spacing 1.5 font 'Times-Roman,16'
set multiplot layout 1,3
set xlabel "x"
set ylabel "T"
p[][] 'output_'.file.'1' u ($1):($2) t 'IC1 BC1' w l
p[][] 'output_'.file.'2' u ($1):($2) t 'IC2 BC2' w l
p[][] 'output_'.file.'3' u ($1):($2) t 'IC2 BC2' w l
unset multiplot
T1 |
T2 |
T3 |
grid
So the grid changes for all variables not just one of them!