Why am I getting a NaN Error when Running my Fortran Program?

In summary, the user was having trouble running a program due to a NaN error. They replaced the "C" in the first column of the code with "!" and removed continuation marks. After increasing the time step and compiling the program, they were able to run it successfully. They also discovered that NaN indicates a mathematical error, possibly caused by dividing by zero.
  • #1
shipman
3
0
Hi to everyone,

I am quite new to forum and fortran. Basically, i am trying to calculate the bubble radius using RP model and this is my code:
Code:
	IMPLICIT DOUBLE PRECISION (A-H,O-Z)
	IMPLICIT INTEGER (I-N)
C	! Declare local constant Pi
	REAL, PARAMETER :: Pi = 3.1415927D0	

C	*************************************************************
C	*    OUTPUT VARIABLES                                        *
C	*************************************************************
	OPEN (10,FILE='DT12.csv',STATUS='UNKNOWN')

C	************INPUT   VARIABLES**************
      DT=1.0D-12	!time step
	TEND=8.0D-4 ! ENDTIME
      DIN= 8.0D-4    !Initial BUBle Dia ometer  
      
C	********   SOME   VARIABLE DATA*****************
      ROL  =1.0D3  ! DENSITY   [LIQUID] kg/m-3
      DMYUL=1.002D-3 ! VISCOSITY [LIQUID]
     	PV=2.3D3    ! SATURATION PRESSURE 
	SIGMA=7.277D-2 ! SURFACE TENSION 
	BETA=1.4D0    ! pressure parameter
	VO=4.78D-9 !constant bubble volume 
	PO=1.00072D5 !used in pres. calculation 
      
	RO=0.5D0*DIN   ! BUBBLE RADIUS AT OLD TIME STEP
	R =0.5D0*DIN !0.5D0*1.07D-3  ! CURRENT BUBBLE RADIUS

	! Initial bubble volume

	VB= 4.0D0*Pi*(RO**3.0D0)/3.0D0 
	
	PG = PO*(VB/VO)**BETA ! PRESSURE OF NON-CONDENSING GAS

	PBUB  =PG +PV !bubble pressure
	
	PSUR=1.01325D5 !enviroment pressure=constant 

	TP    =0.0D0
	N     =0

C	************OUT PUT  INFOMATION*******************************

      NDATA=INT(TEND/DT)
      WRITE(*,100) DIN*1.0D3
      WRITE(*,300) NDATA

C	**************************************************************
C	 ***************LOOP 1  START*********************************
C	**************************************************************
10    TP=TP+DT

      N=N+1
	


	! Solving RP equation

      RR=(PBUB-PSUR
     &    -4.0D0*DMYUL/DT*(R-RO)/R
     &    -2.0D0*SIGMA/R)/ROL
      RNE=2.0D0*R-RO-1.5D0*(R-RO)**2.0D0/R+DT*DT*RR/R
	    
		
	 ! BUBBLE RADIUS AT NEW TIME STEP
      
	IF(RNE*2.0D0.LE.0)   RNE= 0.5d0*DIN
	IF(RNE*2.0D0.GE.2.0d-3)   RNE= 2.0d-3
      
	
	VB= 4.0D0*Pi*(RNE**3.0D0)/3.0D0 
	
	PG = PO*(VB/VO)**BETA ! PRESSURE OF NON-CONDENSING GAS

	PBUB  =PG+PV !bubble pressure

      DRDT=(RNE-R)/DT


	RO =R
	R  =RNE
	RNE=0.0D0

     
C	 *************************************************************
C	*						 OUTPUT                              *
C	*********************WRITE( *,400)*****************************************
     
      
	IF (MOD(N,NDATA/10000).EQ.1) THEN  !screen display data
       WRITE( *,400) N,TP,((3.0D0*VB)/(4.0d0*Pi))**(1.0d0/3.0D0)*1.0D3,
     & PG/1.0d6,DRDT
	end if
	
	if (MOD(N,NDATA/10000).EQ.1) THEN  !csv.data
	  WRITE( *,400) N,TP,((3.0D0*VB)/(4.0d0*Pi))**(1.0d0/3.0D0)*1.0D3,
     & PG/1.0d6,DRDT
      END IF
     
     	IF (TP.LT.TEND) GOTO 10  !time iteration

	
	goto 101
	
	
C *************************************************************
C *    LOOP END                                               *
C *************************************************************
C
101   write(*,*)RNE
  
  100 FORMAT('INITIAL DIAMETER(um)='F6.2)
  200 FORMAT('LIQUID PRESSURE(kPa)='F6.2)
  300 FORMAT('CYCLE NUMBER='8X,I16)
  400 FORMAT(I13,4(1X,1H,E13.6))
 
	CLOSE(10)

	
	
	STOP
	END

could someone tell me that why i get NaN error when i run the program?

Best Regards
 
Technology news on Phys.org
  • #2
Could you provide some more detail about how the program informs you that the NaN error has occurred?

What is the exact wording of the NaN error message?
 
  • #3
Do you know what NaN means?
 
  • #4
took the source
saved the file as *.f90
replaced all "C" in first column for "!"
removed continuation marks "&" and simply glued lines back together...there is room
increased time step from e-12 to e-8, for good measure
compiled program
run it

First few lines:
Code:
INITIAL DIAMETER(um)=  0.80
CYCLE NUMBER=                   79999
            1 , 0.100000E-07 , 0.400000E+00 , 0.177292E-02 ,-0.291038E-02
            1 , 0.100000E-07 , 0.400000E+00 , 0.177292E-02 ,-0.291038E-02
            8 , 0.800000E-07 , 0.399999E+00 , 0.177290E-02 ,-0.232831E-01
            8 , 0.800000E-07 , 0.399999E+00 , 0.177290E-02 ,-0.232831E-01
           15 , 0.150000E-06 , 0.399996E+00 , 0.177286E-02 ,-0.436557E-01
           15 , 0.150000E-06 , 0.399996E+00 , 0.177286E-02 ,-0.436557E-01
           22 , 0.220000E-06 , 0.399993E+00 , 0.177278E-02 ,-0.640284E-01
           22 , 0.220000E-06 , 0.399993E+00 , 0.177278E-02 ,-0.640284E-01

Last few lines:
Code:
        79927 , 0.799872E-03 , 0.200000E+01 , 0.152885E+01 , 0.000000E+00
        79927 , 0.799872E-03 , 0.200000E+01 , 0.152885E+01 , 0.000000E+00
        79934 , 0.799942E-03 , 0.200000E+01 , 0.152885E+01 , 0.000000E+00
        79934 , 0.799942E-03 , 0.200000E+01 , 0.152885E+01 , 0.000000E+00
   0.00000000
 
  • #5
Thank you so much for your all kind replies. Yeah "gsal" finally i could run the program correctly. I found that NaN shows mathematical error which can be derived from dividing by zero...Thank you
 

FAQ: Why am I getting a NaN Error when Running my Fortran Program?

What is a Fortran NAN error?

A Fortran NAN error is an error that occurs when a program written in the Fortran programming language encounters a "NaN" (not a number) value during computation. This can happen when the program tries to perform mathematical operations with invalid or undefined values, such as dividing by zero or taking the square root of a negative number.

Why does a Fortran NAN error occur?

A Fortran NAN error can occur for a variety of reasons, including programming errors, input data errors, and hardware errors. It can also occur when the program is trying to perform calculations that are mathematically impossible, such as taking the logarithm of zero.

How can I fix a Fortran NAN error?

The specific steps to fix a Fortran NAN error will depend on the cause of the error. One common solution is to carefully check the program's code for any potential errors, such as incorrect variable assignments or mathematical operations. Another solution is to ensure that the input data being used in the program is valid and does not contain any NaN values. If the error persists, it may be necessary to consult with a Fortran expert for assistance.

How can I prevent Fortran NAN errors?

To prevent Fortran NAN errors, it is important to carefully design and test programs before running them. This includes checking for potential errors in the code and ensuring that input data is valid. Additionally, using error handling techniques, such as checking for NaN values during computations, can help catch and prevent these errors from occurring.

Can a Fortran NAN error cause other problems in my program?

Yes, a Fortran NAN error can cause other problems in a program. Because these errors often occur during mathematical operations, they can lead to incorrect or unexpected results. This can have a domino effect on the rest of the program, causing further errors or making it difficult to identify the root cause of the issue. It is important to address Fortran NAN errors as soon as they occur to prevent any potential negative impacts on the program's functionality.

Back
Top