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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

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