Fortran Solving Large/Infinite Results w/Yee 1D FDTD Code

AI Thread Summary
The discussion centers on a 1D Finite-Difference Time-Domain (FDTD) code experiencing issues where results become excessively large and eventually reach infinite or NaN values. The code is designed to solve Maxwell's equations, but a critical error is identified in the second loop where the index for E(i-1) is accessed when i equals 1, leading to an undefined reference since the array is allocated starting from index 1. This oversight is likely contributing to the numerical instability and erroneous results. The suggestion is made to use code formatting tags for better readability, emphasizing the importance of proper array indexing in FDTD implementations to avoid such issues.
angelos_physik
Messages
1
Reaction score
0
Hello to all!
I have written this 1D FDTD code but the results I take are very large and after some timestep they get to infinite/NaN... I really don't know what's wrong. The expressions I have used are the exact given solution for the Maxwell equations.

Thank you for your help.
Fortran:
program yee
    implicit none    real :: Delta, tau,omega,n,mu,time,Eprev,Hprev
    real, dimension(:), allocatable :: sigma,eps,A,B,C,D,J
    real, dimension(:), allocatable :: E,H
    real, parameter :: pi=3.1415926
    integer :: i,m=10000, Length=5000,ls,k    Allocate(E(Length),H(Length),sigma(Length),eps(Length))
    Allocate(A(Length),B(Length),C(Length),D(Length))
    Allocate(J(Length))

!Initialization
    open(unit=1,file="Elektrfeld.txt")
    Delta=0.02d0
    tau=0.9d0*Delta
    omega=2.0d0*pi
    n=1.46d0
    mu=1
    ls=1000
    E(:)=0.0d0
    H(:)=0.0d0
    A(:)=0.0d0
    B(:)=0.0d0
    C(:)=0.0d0
    D(:)=0.0d0
    J(:)=0.0d0
    Eprev=0.0d0
    Hprev=0.0d0    do i=1,Length
        if (i.le.6/Delta) then
            sigma(i)=1.0d0
        else if (i.gt.6/Delta .and. i.lt.((Length*Delta-6.0d0)/Delta))then
            sigma(i)=0.0d0
        else if (i.ge.(Length*Delta-6.0d0)/Delta .and. i.le.Length) then
            sigma(i)=1.0d0
        end if
    end do    do i=1,Length
        if (i.lt.Length*0.5d0) then
            eps(i)=1.
        else if (i.ge.1100 .and. i.lt.(1100+2)) then
            eps(i)=n**2.
        else if (i.ge.(1100+2)) then
            eps(i)=1.
        end if
    end do    do i=1,Length
        A(i)=(1.-sigma(i)*tau/(2.*mu))/(1.+sigma(i)*tau/(2.*mu))
        B(i)=(tau/mu)/(1.+(sigma(i)*tau)/(2.*mu))
        C(i)=(1.-sigma(i)*tau/(2.*eps(i)))/(1.+sigma(i)*tau/(2.*eps(i)))
        D(i)=(tau/eps(i))/(1.+sigma(i)*tau/(2.*eps(i)))
    end do    write(*,*) "time"
    read(*,*) time

!----------------Main calculation----------------
    do k=1,m        !time
        E(1)=0.0d0
        E(Length)=0.0d0
        J(ls)=sin(2.*pi*k*tau)*exp(-((k*tau-30.)/10.)**2.)
        do i=2,Length
            Eprev=E(i)
            E(i)=D(i)*(H(i)-H(i-1))/Delta+C(i)*E(i)-D(i)*J(i)
        end do
        do i=1,Length
            Hprev=H(i)
            H(i)=B(i)*(E(i)-E(i-1))/Delta+A(i)*Hprev
        end do
        if (k.eq.time ) then
            write(1,*) "-----",k
            do i=800,1200
                write(1,*) E(i)
            end do
        end if
    end do

    close(1)
end program yee
 
Last edited:
Technology news on Phys.org
we have "code" tags that get your code properly formatted. It would be a good idea if you modified your post to use them.
 
  • Like
Likes angelos_physik
Well, at quick glance, the second i-loop goes from 1 to Length and it contains the term E(i-1), this means that for i=1, you are calling on E(0) which is not defined...it is outside your program altogether given the way you allocated the arrays, their indexes start at 1.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
4
Views
2K
Replies
12
Views
3K
Replies
3
Views
3K
Replies
11
Views
3K
Replies
6
Views
3K
Replies
10
Views
2K
Replies
2
Views
6K
Back
Top