c++ fdtd code -matlab #include
时间:03-23
整理:3721RD
点击:
I am writing a 2D FDTD program and I have some problem, don't know what went wrong, here is a code I have written in C, can anyone give me suggestion where went wrong and help me to correct my mistake? Many thanks
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define GRIDX 10
#define GRIDY 10
main ()
{
float Ez[GRIDX][GRIDY], Hx[GRIDX][GRIDY], Hy[GRIDX][GRIDY];
int n, i, j, is, js, t, nsteps;
float dx, dy, dt;
float mu = 4e-7*M_PI;
float c = 3.0e8;
float epsz = 8.85e-12;
FILE *fp;
is = GRIDX/3;
js = GRIDY/2;
dx = 0.01; //Cell Size
dy = 0.01;
dt = 1/(sqrt((1/(dx*dx))+(1/(dy*dy))))/c; //Time Steps
float Ma=dt/(-mu*dy);
float Mb=dt/(mu*dx);
float Ea=dt/(epsz*dx);
float Eb=dt/(epsz*dy);
for (i=0; i<GRIDX; i++) //Inital
{
for (j=0; j<GRIDY; j++)
{
Ez[i][j]=0;
Hx[i][j]=0;
Hy[i][j]=0;
}
}
Ez[is][js]= 1; //Inital Ez
t = 0;
printf ("Time Steps -->");
scanf ("%d", &nsteps);
for (n=1; n<=nsteps; n++)
{
t = t+1;
//Start FDTD Loop
for (i=0; i<GRIDX-1; i++)
{
for (j=0; j<GRIDY-1; j++)
{
Hx[i][j]=Hx[i][j]+Ma*(Ez[i][j+1]-Ez[i][j]);
}
}
for (i=0; i<GRIDX-1; i++)
{
for (j=0; j<GRIDY-1; j++)
{
Hy[i][j]=Hy[i][j]+Mb*(Ez[i+1][j]-Ez[i][j]);
}
}
for (i=1; i<GRIDX; i++)
{
for (j=1; j<GRIDY; j++)
{
Ez[i][j]=Ez[i][j]+Ea*(Hy[i+1][j]-Hy[i][j])-Eb*(Hx[i][j+1]-Hx[i][j]);
}
}
//End Of FDTD Loop
fp = fopen("Ez.txt", "w");
for (i=0; i<GRIDX; i++)
{
for (j=0; j<GRIDY; j++)
{
fprintf (fp, "%6.5f ", Ez[i][j]);
}
fprintf (fp, " \n");
}
fclose(fp);
}
}
Added after 6 minutes:
Also I want to write a 2D FDTD code in C that can show the relfection, so does it mean it's ABC, PML or others? And also what does these mean?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define GRIDX 10
#define GRIDY 10
main ()
{
float Ez[GRIDX][GRIDY], Hx[GRIDX][GRIDY], Hy[GRIDX][GRIDY];
int n, i, j, is, js, t, nsteps;
float dx, dy, dt;
float mu = 4e-7*M_PI;
float c = 3.0e8;
float epsz = 8.85e-12;
FILE *fp;
is = GRIDX/3;
js = GRIDY/2;
dx = 0.01; //Cell Size
dy = 0.01;
dt = 1/(sqrt((1/(dx*dx))+(1/(dy*dy))))/c; //Time Steps
float Ma=dt/(-mu*dy);
float Mb=dt/(mu*dx);
float Ea=dt/(epsz*dx);
float Eb=dt/(epsz*dy);
for (i=0; i<GRIDX; i++) //Inital
{
for (j=0; j<GRIDY; j++)
{
Ez[i][j]=0;
Hx[i][j]=0;
Hy[i][j]=0;
}
}
Ez[is][js]= 1; //Inital Ez
t = 0;
printf ("Time Steps -->");
scanf ("%d", &nsteps);
for (n=1; n<=nsteps; n++)
{
t = t+1;
//Start FDTD Loop
for (i=0; i<GRIDX-1; i++)
{
for (j=0; j<GRIDY-1; j++)
{
Hx[i][j]=Hx[i][j]+Ma*(Ez[i][j+1]-Ez[i][j]);
}
}
for (i=0; i<GRIDX-1; i++)
{
for (j=0; j<GRIDY-1; j++)
{
Hy[i][j]=Hy[i][j]+Mb*(Ez[i+1][j]-Ez[i][j]);
}
}
for (i=1; i<GRIDX; i++)
{
for (j=1; j<GRIDY; j++)
{
Ez[i][j]=Ez[i][j]+Ea*(Hy[i+1][j]-Hy[i][j])-Eb*(Hx[i][j+1]-Hx[i][j]);
}
}
//End Of FDTD Loop
fp = fopen("Ez.txt", "w");
for (i=0; i<GRIDX; i++)
{
for (j=0; j<GRIDY; j++)
{
fprintf (fp, "%6.5f ", Ez[i][j]);
}
fprintf (fp, " \n");
}
fclose(fp);
}
}
Added after 6 minutes:
Also I want to write a 2D FDTD code in C that can show the relfection, so does it mean it's ABC, PML or others? And also what does these mean?