/* 1D FDTD simulation with an additive source. * All snapshots are written to a single output file. */ #include #include #define SIZE 200 int main() { double ez[SIZE] = {0.}, hy[SIZE] = {0.}, imp0 = 377.0; int qTime, maxTime = 200, mm; char filename[100] = "sim.dat"; FILE *snapshots; snapshots=fopen(filename, "w"); /* 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; /* update electric field */ for (mm = 1; mm < SIZE; mm++) ez[mm] = ez[mm] + (hy[mm] - hy[mm - 1]) * imp0; /* use additive source at node 50 */ ez[50] += exp(-(qTime - 30.) * (qTime - 30.) / 100.); /* write snapshot if time a multiple of 2 */ if (qTime % 2 == 0) { for (mm = 0; mm < SIZE; mm++) fprintf(snapshots, "%g ", ez[mm]); fprintf(snapshots, "\n"); } } /* end of time-stepping */ fclose(snapshots); return 0; }