Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Anyone can help me to correct my 2D FDTD code written in C?

Status
Not open for further replies.

eeywswch

Newbie level 4
Joined
Oct 21, 2004
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
56
c++ fdtd code -matlab #include

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[j]=0;
Hx[j]=0;
Hy[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[j]=Hx[j]+Ma*(Ez[j+1]-Ez[j]);
}
}
for (i=0; i<GRIDX-1; i++)
{
for (j=0; j<GRIDY-1; j++)
{
Hy[j]=Hy[j]+Mb*(Ez[i+1][j]-Ez[j]);
}
}
for (i=1; i<GRIDX; i++)
{
for (j=1; j<GRIDY; j++)
{
Ez[j]=Ez[j]+Ea*(Hy[i+1][j]-Hy[j])-Eb*(Hx[j+1]-Hx[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[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?
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top