1D FDTD simulation in C - stability problem
I've read about the FDTD quite a bit, both through resources I found on this board and in the local library. I have implemented a 1D FDTD algorithm in C, with a Gaussian pulse as the excitation. The code is posted below. If on line 97 i set currentTime == 0, I get the initial pulse as expected. However, as soon as I try to take a snapshot of the pulse further down the timeline, say at currentTime == 50, I can see that it is going terribly unstable, tending towards infinity.
I think my problem is that I am not introducing the source pulse correctly, but for the life of me can not figure out why.
Any clues to help me out?
Thanks!
Ok, I've got that problem of instability sorted out now. It turned out I forgot a bracket in one of the iteration equations.
However, with that fixed, I still have a problem. The EM wave is moving too "slow". I've seen the solution to this problem, and after 50 time steps the wave should be just in front of the dielectric. In my case, it is already reflecting from the dielectric although it should not have reached it yet. Also, the amplitude of the wave increases! This, of course, is impossible since the wave can not gain energy while travelling (in this case at least). I'm really stuck now since I don't know what could be causing this.
It would be great if another pair of eyes could take a look, since I believe it might be a simple mistake somewhere, that I just keep on overlooking.
Anyway, new code is attached, as well as a shirt description of problem in the .doc file.
Thanks in advance!
Have you taken care of the relationship between space steps and time steps?
FDTD is unstable if the two steps don't satisfy a condition.
Ok, turns out it was messing up because I defined one of the constants incorrectly. All fixed now. Posting the code, and problem description in the attachment if anyone ever needs it.
The code will generate a file of x-y values suitable for plotting in something like Gnuplot.
Cheers!