Anyone familiar with FDTD method?

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.

Born2bwire

Diamond Member
Oct 28, 2005
9,840
6
71
Originally posted by: TecHNooB
Hm. My 2-d fdtd code for maxwell's equations freaks out after several iterations (everything goes to infinity or -infinity). Any idea why this is happening? Just a prod in the right direction will do :)

I set all the boundaries to E = 0 and set one of the E vectors to a gaussian pulse. The matrices fill up and then blow up lol :(

Edit: actually, i think it might be a stability criterion. didnt put those in yet. ~_~

Edit2: still blows up :(

Edit3: Hm.. my magnitudes seem way to high, i wonder why that is O_O

Make sure that it is a single pulse so that you can track it or do a sine wave. For a sine wave, ramp up and down the amplitude at the turn on and turn off so that it is not discontinuous. Does it blow up before it hits the boundaries, at, or after? Calculate the fields by hand and run the code in debugger mode (or have the appropriate cout stuff) and check to see the calculation is correct. Make sure your dx, dy, and dt satisfy the stability conditions (and then reduce them further).
 

CycloWizard

Lifer
Sep 10, 2001
12,348
1
81
Originally posted by: TecHNooB
Hm. My 2-d fdtd code for maxwell's equations freaks out after several iterations (everything goes to infinity or -infinity). Any idea why this is happening? Just a prod in the right direction will do :)

I set all the boundaries to E = 0 and set one of the E vectors to a gaussian pulse. The matrices fill up and then blow up lol :(

Edit: actually, i think it might be a stability criterion. didnt put those in yet. ~_~

Edit2: still blows up :(

Edit3: Hm.. my magnitudes seem way to high, i wonder why that is O_O
What Born2bwire said, plus make sure your mesh is fine enough. I would also recommend trying it out on a simple case with an analytical solution before trying something complicated.
 

TecHNooB

Diamond Member
Sep 10, 2005
7,458
1
76
What's some quick matlab gui code I can run to see what these matrices are doing? My grad advisor suggested colormap so something related to that.
 

CycloWizard

Lifer
Sep 10, 2001
12,348
1
81
Look at griddata and contour functions. Griddata should be able to give you a function representing the discrete points in the matrices, while contour will convert that function into a contour plot. It should be pretty straightforward, but let me know if you need help with the code.
 

CycloWizard

Lifer
Sep 10, 2001
12,348
1
81
edit: This is how I would animate the results of the FDTD when an ASCII file is saved with the results of each time step. I didn't test the code yet, so it might be slightly buggy. If anyone knows a better way to do this or has any improvements/suggestions to the code, please post them. I have no training as a coder and usually make it up as I go along, so the way I do things is rarely a proper/good/efficient way to do things. :p


----------------------------------------
Just in case anyone comes looking for more info here, I PMed the following to TecHNooB:

I generally just use dlmread/dlmwrite for input/output of ASCII files. These are relatively high level calls that will do all of the work for you as long as you tell them what delimiter the data are stored with. While it can probably be done faster using fopen/fwrite, which do the same thing for binary files, I prefer not to mess with those unless I have to.

So, in your code, you would just include the following line in your loop for each time step:

dlmwrite(Data,'Timestep_j.txt','\t');%write array Data to tab-separated ASCII file

Then, in your plotting function, you might have something like this:

%Initialize the mesh for plotting
XI=linspace(x0,x1,NumPoints);
YI=linspace(y0,y1,NumPoints);

for(i=1:NumSteps)%for each time step
Data=dlmread('Timestep_j.txt','\t');%write array Data to tab-separated ASCII file
[X,Y,Z]=griddata(Data:),1),Data:),2),Data:),3),XI,YI);
contourf(X,Y,Z);%make contour plot Z=f(X,Y)
end
 

TecHNooB

Diamond Member
Sep 10, 2005
7,458
1
76
I ended up using the movie function although I dont know what the limitations are for the function. I think it stores all the frames in memory which is bad but I havent ran the thing on an extremely large data set so I dont know what's gonna happen :p For the frames, i just used 'image'. The axis aren't really meaningful but at least I can visualize what's going on.
 

eLiu

Diamond Member
Jun 4, 2001
6,407
1
0
Yeah if you store all the time steps to memory, you could run out quickly. If you have a lot of unknowns per time step and/or a lot of time steps, I would consider processing the data before saving... like only store every 10th or 100th time step and only store every other space point or something.

Another note... you should talk to files using fread/fwrite in binary mode. binary read/writes are A LOT faster than ASCII, and it isn't hard to learn to use binary. The downside is of course that it's impossible to view what's in the files by opening them in a text editor.
 

TecHNooB

Diamond Member
Sep 10, 2005
7,458
1
76
Originally posted by: eLiu
Yeah if you store all the time steps to memory, you could run out quickly. If you have a lot of unknowns per time step and/or a lot of time steps, I would consider processing the data before saving... like only store every 10th or 100th time step and only store every other space point or something.

I will keep this in mind :)

Originally posted by: eLiu
Another note... you should talk to files using fread/fwrite in binary mode. binary read/writes are A LOT faster than ASCII, and it isn't hard to learn to use binary. The downside is of course that it's impossible to view what's in the files by opening them in a text editor.

I used this :D And yes I am sad that I cannot read it :(

 

CycloWizard

Lifer
Sep 10, 2001
12,348
1
81
Originally posted by: TecHNooB
I ended up using the movie function although I dont know what the limitations are for the function. I think it stores all the frames in memory which is bad but I havent ran the thing on an extremely large data set so I dont know what's gonna happen :p For the frames, i just used 'image'. The axis aren't really meaningful but at least I can visualize what's going on.
Of course eLiu's points are correct, so if speed is important, I would definitely do as he suggested. As far as adding individual frames goes, you can use the im2frame, or perhaps addframe, then maybe the movie2avi function if you need an external file.