CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > General Forums > Main CFD Forum

Trying to resolve -NaN problem

Register Blogs Community New Posts Updated Threads Search

Like Tree3Likes
  • 1 Post By praveen
  • 1 Post By AliE
  • 1 Post By AliE

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   November 5, 2018, 10:14
Default Trying to resolve -NaN problem
  #1
New Member
 
Floyd Pfeffer
Join Date: Nov 2018
Location: Portugal
Posts: 3
Rep Power: 7
Floyd_Pfeffer is on a distinguished road
I am writing a program with Lahey/Fujitu Fortran 95. I have a subroutine which calls another subroutine. On the first call to this subroutine, it give a -NaN (not a number) answer. It only does this on the first time the subroutine is called. All subsquent calls return the correct answer. I have checked for a divide by zero which usually results in a -NaN but can't see it. Here is the weird part. When I put a screen print (Print *, var) in the subroutine, the problem goes away?? When I remove the print statement the problem comes back.
Has anyone ever had this kind of problem? What other things can cause a -NaN error? Any help would be appreciated.
Floyd Pfeffer
Floyd_Pfeffer is offline   Reply With Quote

Old   November 5, 2018, 10:38
Default
  #2
Super Moderator
 
Praveen. C
Join Date: Mar 2009
Location: Bangalore
Posts: 342
Blog Entries: 6
Rep Power: 18
praveen is on a distinguished road
Some more things to check

Check for uninitialized variables
check for accessing an array outside its range
sqrt of a negative number
FMDenaro likes this.
praveen is offline   Reply With Quote

Old   November 5, 2018, 11:54
Default
  #3
Senior Member
 
Join Date: Dec 2017
Posts: 153
Rep Power: 8
AliE is on a distinguished road
Quote:
Originally Posted by praveen View Post
Some more things to check

Check for uninitialized variables
check for accessing an array outside its range
sqrt of a negative number
I would add also divisions by zero

Edit: sorry just realized you have already excluded this option. Are you using implicit none statment to exclude badly declared variables?
AliE is offline   Reply With Quote

Old   November 5, 2018, 12:37
Default
  #4
Senior Member
 
Filippo Maria Denaro
Join Date: Jul 2010
Posts: 6,783
Rep Power: 71
FMDenaro has a spectacular aura aboutFMDenaro has a spectacular aura aboutFMDenaro has a spectacular aura about
The first thing to do is to compile without any optimization and using the debug options, check bounds array and so on. The run-time error should also provide the infos about the line of your error. While NaN is a signal of a division by zero, it could be do to a wrong access to the memory location.
FMDenaro is offline   Reply With Quote

Old   November 5, 2018, 12:54
Default
  #5
Senior Member
 
Join Date: Dec 2017
Posts: 153
Rep Power: 8
AliE is on a distinguished road
Filippo has provided a good answer as usual.

In your makefile add the following flags:
-oO
-fbounds-check

In this way the compiler avoids vectorization and it gives you an error if you are using an index out of bounds.

It looks to me an error that you can fix just commenting the code and running it line by line to find out your issue.

If you want to use a machinegun against a little ant, then valgrind is what you are looking for.
FMDenaro likes this.
AliE is offline   Reply With Quote

Old   November 7, 2018, 04:01
Default Success
  #6
New Member
 
Floyd Pfeffer
Join Date: Nov 2018
Location: Portugal
Posts: 3
Rep Power: 7
Floyd_Pfeffer is on a distinguished road
Taking the optimization off solved the problem. Thanks to all that helped
Floyd Pfeffer
Floyd_Pfeffer is offline   Reply With Quote

Old   November 7, 2018, 04:09
Default
  #7
Senior Member
 
Filippo Maria Denaro
Join Date: Jul 2010
Posts: 6,783
Rep Power: 71
FMDenaro has a spectacular aura aboutFMDenaro has a spectacular aura aboutFMDenaro has a spectacular aura about
Quote:
Originally Posted by Floyd_Pfeffer View Post
Taking the optimization off solved the problem. Thanks to all that helped
Floyd Pfeffer



Without changing the error in some line in the code? Be careful, this is a signal of an error.
FMDenaro is offline   Reply With Quote

Old   November 7, 2018, 04:22
Default
  #8
Senior Member
 
Join Date: Dec 2017
Posts: 153
Rep Power: 8
AliE is on a distinguished road
Quote:
Originally Posted by FMDenaro View Post
Without changing the error in some line in the code? Be careful, this is a signal of an error.
Yes, Filippo is again right. Using -o0 can just mask your problem, actually -03 flag is not so unsafe. Also note that there is a huge performance gap between -o0 and -o3 in term of speed (at least order 2) , so you should really continue with debugging!

Try to download "valgring" and run the code until that point (valgrind --leak-check=yes myprog ). You shuold be able to find out more about your error.
Otherwise use the brute force and comment all the routine that is giving you the NaN and sequentially uncomment the lines and the code to find out the one that is giving you problems. It is a time consuming option, but can be effective if your routine is not too long.
FMDenaro likes this.
AliE is offline   Reply With Quote

Old   November 7, 2018, 07:50
Default
  #9
Super Moderator
 
Praveen. C
Join Date: Mar 2009
Location: Bangalore
Posts: 342
Blog Entries: 6
Rep Power: 18
praveen is on a distinguished road
Yes, dont ignore such errors. Changing optimization level is not the solution.

Try compiling/running with other compilers like gfortran. Use flags -Wall and also -fbounds-check which was suggested before. Look at all warnings and convince yourself they are not errors.
praveen is offline   Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mesh& steptime independant: conduction-convection problem Fati1 Main CFD Forum 1 October 28, 2018 13:52
problem during mpi in server: expected Scalar, found on line 0 the word 'nan' muth OpenFOAM Running, Solving & CFD 3 August 27, 2018 04:18
the problem of the application(icoFoam) touff00 OpenFOAM Running, Solving & CFD 1 October 18, 2015 06:12
F-packing problem Streamlet FLOW-3D 4 April 29, 2011 03:41
Cells with t below lower limit Purushothama Siemens 2 May 31, 2010 21:58


All times are GMT -4. The time now is 07:26.