[Fortran 90] Output is NaN in variables

In summary, the cause of NaN in variables ep, ht, and hr was due to dividing by infinity and multiplying by zero when j=k in the update equations.
  • #1
s_hy
61
0
Hi all,

For this problem, I have checked all possible factor that may cause NaN in variables such as
- division by zero --> not found
- undefined variables - set initialization for variables - ep, hr,ht
- parameter setting --> double precision problems checked, no issue

but still couldn't find the cause of the NaN in variable ep, ht and hr as well. need an advice from professionals ( i am an amateur anyway)

Code:
!this is code from berenger 2002 for vlf propagation in earth-ionosphere waveguide.
!equations 7a-7c
!to date, ionosphere is assumed as free space -29/10/2013

!**************************************************************************************

subroutine spherical
implicit none

integer                                              :: N, L, i, j, t, k
integer                                              :: ir !, rr
double precision                                :: c, f, lamda, dx, dt, sigma, eps0,u0
double precision                                :: epsr,sigmar,omegap
double precision                                :: dr, dth,rmh,rph
double precision,dimension(300,300)   :: ep, ht, hr, ga, gb
double precision,dimension(300)         :: r
double precision, parameter               :: pi = 3.14159265d0
character(len=20)                              :: filename


!parameter
 k = 201
 L = 100

 f = 3.e9
 u0 = 4*pi*1e-7
 eps0 = 8.8e-12
 c = 1D0/sqrt(u0*eps0)
 lamda = c/f
! print *,'c=',c
 
 dx = lamda/20 
 dt = dx / (2*c)
 dr = 0.05*lamda
 dth = 1*pi/180          !theta  = pi
 
 !initialize r
 do j = 1,k+1
   r(j) = 0.0
   !print *, 'j=',j,'r(j)=',r(j)
 end do


!do r = 1,dr*(k-1),dr
 do ir = 1, (k-1)
    r(ir) = (ir - 1)*dr
 end do !IR

 !material parameter , assumed to be free space
  epsr = 1d0
  sigma = 1d0
  sigmar = 1d0
         

!initialization for updated equations
 do i = 1,k+1
  do j =1 ,k+1
    ep(i,j) = 0
    ht(i,j) = 0
    hr(i,j) = 0
  end do
 end do


!material properties
do i = 1, k                                  
    do j = 1,k
        ga(i,j) = exp(-(sigmar*dt)/(eps0*epsr)) 
        gb(i,j) = (1-exp(-(sigmar*dt)/(eps0*epsr)))/sigmar              
        !print *,'ga(i,j)=',ga(i,j),'gb(i,j)=',gb(i,j)
    end do
end do

!**********************************************************************
!**********************************************************************
 ! update equations
 do t=1,L
 
      write (filename, "('data',I3.3,'.dat')") t
      open (unit=130,file=filename)
            
      ! source
      ep(1,1) = sin(2*pi*f*t*dt)
      !print *, 'ep(1,1)=',ep(1,1)
     
    !  update Ez field
    do i = 2,k
     do j = 2,k
       rph = r(j)+dr/2
       rmh = r(j)-dr/2
       ep(i,j) = ga(i,j)*ep(i,j) + gb(i,j) * (((1/r(j)*dth)*(hr(i,j) - hr(i-1,j)) - (1/r(j)*dr)*(rph*ht(i,j) + rmh*ht(i,j-1))))
       !print*, 'i=',i,'j=',j,'r(j) = ',r(j)
       write (130,*) i,j,ep(i,j)
       if (j == 200) write (130,*) ' '
     end do
    end do
     
     !update Ht (theta) field =Hx
     do  i = 1,k-1
      do j = 1,k-1
         rph = r(j)+dr
         rmh = r(j)
         ht(i,j) = ht(i,j) - (dt/u0/r(j)/dr)*(rph*ep(i,j+1) - rmh*ep(i,j))
         hr(i,j) = hr(i,j) + (dt/u0/r(j)/dth/sin(dth))*(sin(dth)*ep(i+1,j) - sin(dth)*ep(i,j))
         print*,'i=',i,'j=',j,'hr(i,j)  =',hr(i,j) 
       end do
      end do
   
   
    close (unit=130)
           
 end do

end subroutine
 
Technology news on Phys.org
  • #2
You are dividing by zero (infinity) and then multiplying by zero, which results in NaN.

Code:
!initialize r
 do j = 1,k+1
   r(j) = 0.0
   !print *, 'j=',j,'r(j)=',r(j)
 end do

!do r = 1,dr*(k-1),dr
 do ir = 1, (k-1)
    r(ir) = (ir - 1)*dr
 end do !IR
The first loop sets the first k+1 elements of r to zero. The second loop sets the first k-1 elements of r to (ir-1)*dr. Elements k and k+1? They're still zero. (Aside: r(1) is also zero.)

Code:
    !  update Ez field
    do i = 2,k
     do j = 2,k
       rph = r(j)+dr/2
       rmh = r(j)-dr/2
       ep(i,j) = ga(i,j)*ep(i,j) + gb(i,j) * (((1/r(j)*dth)*(hr(i,j) - hr(i-1,j)) - (1/r(j)*dr)*(rph*ht(i,j) + rmh*ht(i,j-1))))
       !print*, 'i=',i,'j=',j,'r(j) = ',r(j)
       write (130,*) i,j,ep(i,j)
       if (j == 200) write (130,*) ' '
     end do
    end do
When j=k that 1/r(j) is 1.0/0.0., or +infinity. On the first pass, hr(i,j) - hr(i-1,j) is 0, and infinity*0 is NaN.
 
  • Like
Likes 1 person
  • #3
solved! thank you
 

1. What does "NaN" mean in Fortran 90 output?

"NaN" stands for "not a number" and is used to represent undefined or invalid values in Fortran 90. It is often a result of mathematical operations involving special values such as infinity or zero.

2. Why am I getting NaN as the output for my variables in Fortran 90?

There are a few common reasons for this. One possibility is that your program is trying to divide by zero, which results in a NaN value. Another possibility is that your program is performing an illegal operation, such as taking the square root of a negative number, which also results in a NaN value.

3. How do I fix NaN output in my Fortran 90 program?

The best way to fix NaN output is to identify the source of the problem. Check your code for any mathematical operations involving special values or illegal operations. You may also want to use conditional statements to prevent dividing by zero or taking the square root of a negative number.

4. Can NaN values be used in calculations in Fortran 90?

Yes, NaN values can be used in calculations in Fortran 90. However, it is important to handle them properly to avoid unexpected results. For example, any operation involving a NaN value will result in a NaN value. It is also important to note that NaN values are not equal to any other value, including other NaN values.

5. Is it possible to check for NaN values in Fortran 90?

Yes, you can use the "isnan" function in Fortran 90 to check if a value is NaN. This function returns a logical value of true if the given value is NaN and false otherwise. This can be useful for error handling in your program.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
9K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
22
Views
4K
  • Programming and Computer Science
Replies
12
Views
3K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top