/* 1D FDTD simulation of a lossy dielectric region. */ #include #include #define SIZE 200 #define LOSS 0.01 int main() { double ez[SIZE], hy[SIZE - 1], ceze[SIZE], cezh[SIZE], imp0 = 377.0; int qTime, maxTime = 450, mm; char basename[80] = "sim", filename[100]; int frame = 0; FILE *snapshot; /* initialize electric field */ for (mm = 0; mm < SIZE; mm++) ez[mm] = 0.0; /* initialize magnetic field */ for (mm = 0; mm < SIZE - 1; mm++) hy[mm] = 0.0; /* set electric-field update coefficients */ for (mm = 0; mm < SIZE; mm++) if (mm < 100) { /* free space */ ceze[mm] = 1.0; cezh[mm] = imp0; } else { /* lossy dielectric */ ceze[mm] = (1.0 - LOSS) / (1.0 + LOSS); cezh[mm] = imp0 / 9.0 / (1.0 + LOSS); } /* do time stepping */ for (qTime = 0; qTime < maxTime; qTime++) { /* update magnetic field */ for (mm = 0; mm < SIZE - 1; mm++) hy[mm] = hy[mm] + (ez[mm + 1] - ez[mm]) / imp0; /* correction for Hy adjacent to TFSF boundary */ hy[49] -= exp(-(qTime - 30.) * (qTime - 30.) / 100.) / imp0; /* simple ABC for ez[0] */ ez[0] = ez[1]; /* update electric field */ for (mm = 1; mm < SIZE - 1; mm++) ez[mm] = ceze[mm] * ez[mm] + cezh[mm] * (hy[mm] - hy[mm - 1]); /* correction for Ez adjacent to TFSF boundary */ ez[50] += exp(-(qTime + 0.5 - (-0.5) - 30.) * (qTime + 0.5 - (-0.5) - 30.) / 100.); /* write snapshot if time a multiple of 10 */ if (qTime % 10 == 0) { sprintf(filename, "%s.%d", basename, frame++); snapshot=fopen(filename, "w"); for (mm = 0; mm < SIZE; mm++) fprintf(snapshot, "%g\n", ez[mm]); fclose(snapshot); } } /* end of time-stepping */ return 0; }