Getting NaN for a write output in FORTRAN

In summary: Once you have a bit more experience, you will find some other techniques to help avoid these kinds of problems.In summary, the conversation discusses a program being written in FORTRAN to calculate neutron flux in a reactor. However, when trying to get a numerical output, the program returns NaN. The code and variables used in the program are also mentioned. It is suggested that NaNs can arise from certain computations or compiler issues, and the importance of using a debugger is emphasized to avoid such errors.
  • #1
antony1103
3
0
Hi all,

I'm writing a program in FORTRAN to calculate neutron flux in a reactor, but when I try to get a numerical output it simply says NaN. Anyone know what this means or how to fix it? Code is below, and this is happening on the flux2(N) output on the last write statement. Thanks!

Code:
      program slabflux

         implicit none



         ! VARIABLES

         integer, parameter :: N=30		! Number of slab increments

         real :: thickness         		! slab thickness in meters
         real :: diff_const        		! diffusion constant of the slab
         real :: macro_cross_sec   		! macroscopic cross section of the slab
         real :: source            		! neutron source rate
         real :: distance          		! distance of neutron flux calculation in the slab
         real, dimension(-1:N+1) :: flux1       ! flux calculation at a certain distance in the slab
         real, dimension(0:N) :: flux2		! used for iterative flux calculation
	 real, dimension(0:N) :: S		! source array
	 real :: del				! increment length
	 real :: a				! flux coefficient
	 real :: b				! flux coefficient
	 real, dimension(-1:N) :: prev_flux	! previous flux summation for loop
         real, dimension(0:N) :: prev_iter	! flux summation from previous iteration
         integer :: i				! do loop counter
	 integer :: j				! do loop counter
         integer :: k				! do loop counter
         real :: e				! iteration error

	 



         ! INPUT

         write (*,'(a,$)') "Slab thickness (cm)?: "
         read *, thickness

         write (*,'(a,$)') "Diffusion constant (cm)?: "
         read *, diff_const

         write (*,'(a,$)') "Macroscopic cross section (1/cm)?: "
         read *, macro_cross_sec

         write (*,'(a,$)') "Neutron source rate (neutrons/cm^3*s)?: "
         read *, source



	 ! CALCULATION

	 del=thickness/N					! increment length

	 S(N/2)=source				         	! source centered in slab
						

         flux1(1:29)=1						! initial flux
         prev_flux(-1:N)=0
         prev_flux(1:29)=0

	 a=-(diff_const/(del**2))				! flux coefficients

	 b=(diff_const/(del**2))+macro_cross_sec

         if (0.997<e .AND. e<1.003) then
            flux2(0:N)=flux1(0:N)
	    do i=0,N,1						! first iteration
	       prev_flux(i)=flux2(i-1)+prev_flux(i-1)
               do j=i,N,1
                  prev_iter(i)=prev_flux(i)+flux1(i+1)
               end do
	       flux2(i)=(1/b)*(S(j)-(a*prev_flux(i))-(a*prev_iter(i)))
	    end do
            e=abs(flux2(15)/flux1(15))
         else
            do k=0,N,1
               distance=del*N
               write (*,'(a,f6.2,3x,a,f6.2)') "Distance (cm): ",
     +                                       distance,
     +                                       "Flux (neutrons/cm^2*s): ",
     +                                       flux2(N)
            end do
         end if

      end program slabflux
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
First off, please use the [ code ] ... [ /code ] tags, sans the spaces, when you post code. I edited your post for you.

As far as NaNs are concerned:
NaNs can arise from some computations such as 0/0. They can also result from the compiler. I suspect you are using a compiler that initializes uninitialized floating point variables with NaNs. In particular, you did not initialize the array flux2 or the variable e. Since e is not set, the then part of the if statement never executes, and flux2 keeps its (uninitialized) initial values.

The best way to find these kinds of errors is to use your debugger. Learn to use it.
 

1. What does "NaN" mean in FORTRAN?

NaN stands for "Not a Number" and is a special value used to represent an undefined or invalid numerical result.

2. Why am I getting NaN for a write output in FORTRAN?

This can happen if your code is attempting to perform a mathematical operation that results in an undefined or invalid numerical result. It could also be caused by attempting to write a variable that has not been initialized.

3. How can I fix a NaN write output in FORTRAN?

To fix this issue, you will need to identify the source of the NaN value in your code and make sure the necessary variables are properly initialized and that any mathematical operations are properly defined.

4. Can NaN values be used in calculations in FORTRAN?

Yes, NaN values can be used in calculations in FORTRAN. However, it is important to be aware of their presence and handle them appropriately to avoid unexpected results.

5. Are there any specific data types that can result in NaN values in FORTRAN?

NaN values can occur with any data type in FORTRAN, including real, integer, and complex numbers. It is important to properly handle these values to avoid errors in your code.

Similar threads

  • Programming and Computer Science
Replies
4
Views
589
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
26
Views
3K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
Back
Top