Silverfrost Forums

Welcome to our forums

Error reading data from a text file

27 Apr 2017 12:58 #19479

I am having the following error while reading data from a text file:

nrerror: Error reading data from Options.txt **** STOP: program terminated by nrerror

Part of the code that reads the data is below and some of the variables are real in the text file and some are logical like .false. I am declaring the logical variables as: LOGICAL(LGT) :: variable_name and real variables as REAL(DP) :: variable_name

OPEN(UNIT=8,FILE='Options.txt',STATUS='OLD',iostat=SuccessFlag, &
		ERR=100)
	READ(8,*,iostat=SuccessFlag, ERR=100, END=200) system
	IF(system.eq.1) &
	READ(8,*,iostat=SuccessFlag, ERR=100, END=200) IntNaRatio
	READ(8,*,iostat=SuccessFlag, ERR=100, END=200) varopt
	READ(8,*,iostat=SuccessFlag, ERR=100, END=200) screen_write
	READ(8,*,iostat=SuccessFlag, ERR=100, END=200) NAGTimerOn
	READ(8,*,iostat=SuccessFlag, ERR=100, END=200) TimerOn
	READ(8,*,iostat=SuccessFlag, ERR=100, END=200) Filament
	READ(8,*,iostat=SuccessFlag, ERR=100, END=200) VapCalc
	READ(8,*,iostat=SuccessFlag, ERR=100, END=200) TempCalc
	READ(8,*,iostat=SuccessFlag, ERR=100, END=200) r_init
	READ(8,*,iostat=SuccessFlag, ERR=100, END=200) t_end
	READ(8,*,iostat=SuccessFlag, ERR=100, END=200) t_split
	READ(8,*,iostat=SuccessFlag, ERR=100, END=200) dataout_tstep
	READ(8,*,iostat=SuccessFlag, ERR=100, END=200) npts_in
	READ(8,*,iostat=SuccessFlag, ERR=100, END=200) npts_shell
	READ(8,*,iostat=SuccessFlag, ERR=100, END=200) npts_out
	READ(8,*,iostat=SuccessFlag, ERR=100, END=200) InitialA

	IF(InitialA.ge.one)THEN
		InitialA=one-small
		WRITE(6,*) '***-------PURE WATER DROPLET CALLED-------***'
		WRITE(6,*) 'In order to simulate a pure water droplet it'
		WRITE(6,*) '    is necessary to introduce a very small'
		WRITE(6,*) 'quantity of solute to initialise the equation'
		WRITE(6,*) '  system.  Therefore the initial water mass'
		WRITE(6,*) ' fraction will be set to',InitialA
		WRITE(6,*) '***---------------------------------------***'
		PAUSE
	END IF
	READ(8,*,iostat=SuccessFlag, ERR=100, END=200) pure_A_den
	READ(8,*,iostat=SuccessFlag, ERR=100, END=200) pure_B_den
	READ(8,*,iostat=SuccessFlag, ERR=100, END=200) pure_A_Cp
	READ(8,*,iostat=SuccessFlag, ERR=100, END=200) pure_B_Cp
	READ(8,*,iostat=SuccessFlag, ERR=100, END=200) Tdrop_init
	READ(8,*,iostat=SuccessFlag, ERR=100, END=200) SatSolMassFrac
	READ(8,*,iostat=SuccessFlag, ERR=100, END=200) UniformGrid
	bubble=.true. 
	READ(8,*,iostat=SuccessFlag, ERR=100, END=200) b_init
	READ(8,*,iostat=SuccessFlag, ERR=100, END=200) b_min
	READ(8,*,iostat=SuccessFlag, ERR=100, END=200) Solids
	IF(.not.Solids) InitialSolids=.false.
	IF(Solids)THEN
		READ(8,*,iostat=SuccessFlag, ERR=100, END=200) InitialD
		READ(8,*,iostat=SuccessFlag, ERR=100, END=200)&
										 InitialParticleSize
		READ(8,*,iostat=SuccessFlag, ERR=100, END=200) Lmin
		READ(8,*,iostat=SuccessFlag, ERR=100, END=200) pure_D_den
		READ(8,*,iostat=SuccessFlag, ERR=100, END=200) pure_D_Cp
		READ(8,*,iostat=SuccessFlag, ERR=100, END=200) Stokes
		READ(8,*,iostat=SuccessFlag, ERR=100, END=200) ShellDiff
		READ(8,*,iostat=SuccessFlag, ERR=100, END=200) ShellR
		READ(8,*,iostat=SuccessFlag, ERR=100, END=200) MaxSolVolFrac
		READ(8,*,iostat=SuccessFlag, ERR=100, END=200) IntWetShell
		RunWetShell=IntWetShell
		READ(8,*,iostat=SuccessFlag, ERR=100, END=200) IntDryShell
		RunDryShell=IntDryShell
		IF(IntWetShell.and.IntDryShell) THEN
			WRITE(6,*) '!*! Error - WetShell and DryShell options selected'
			WRITE(6,*) '!*! Check options in Options.txt'
			WRITE(6,*) '!*! Program will now terminate'
			STOP
		END IF
		READ(8,*,iostat=SuccessFlag, ERR=100, END=200) MinShellT
		READ(8,*,iostat=SuccessFlag, ERR=100, END=200) &
												MinShellThcknss
		READ(8,*,iostat=SuccessFlag, ERR=100, END=200) &
												ThckndShellThcknss
		READ(8,*,iostat=SuccessFlag, ERR=100, END=200) SinkDiff
		READ(8,*,iostat=SuccessFlag, ERR=100, END=200) Tort
		READ(8,*,iostat=SuccessFlag, ERR=100, END=200) Youngs
		READ(8,*,iostat=SuccessFlag, ERR=100, END=200) Poisson
	END IF
CLOSE(8)
27 Apr 2017 12:59 #19480

I am using windows compiler with 64 bit operating system.

27 Apr 2017 2:42 #19482

The error message is printed by one the routines (nrerror) that you compiled and linked to produce your program.

You have to see your own source code to see what condition was checked before deciding to print that error message.

You cannot expect us to tell you what to fix before you have shown us either the data or the program source.

27 Apr 2017 3:32 #19483

I think this is:

! READ OPTIONS ReadSuccessFlag=.false. !io_status = 0 !CALL ReadOptions(io_status) CALL ReadOptions(ReadSuccessFlag) ! IF(.not.ReadSuccessFlag .ne. 0) &

IF(.not.ReadSuccessFlag) &
!IF(io_status .ne. 0) &
CALL nrerror('Error reading data from Options.txt')
CALL TimerDriver(opt=-2,subtitle='Read Options',t=zero)
27 Apr 2017 6:52 #19484

Since ReadSuccessFlag is set to .FALSE. before calling ReadOptions, which is the routine that reads OPTIONS.TXT, if ReadOptions executes all the READs correctly but does not set ReadSuccessFlag to .TRUE. after doing so, the caller will incorrectly conclude that there were errors in reading the file.

27 Apr 2017 10:50 #19487

I am curious if SDBG64 or currently better 32bit SDBG was used for step by step analysis of reading process

28 Apr 2017 7:59 #19488

mecej4, I think you are right. The file is being read correctly, but for some reason, CALL ReadOptions(ReadSuccessFlag) is not working. I am not sure what is causing the error.

Is that a compiler version issue? I am using Fortran95.

28 Apr 2017 9:20 (Edited: 28 Apr 2017 10:21) #19489

Quoted from mali28 mecej4, I think you are right. The file is being read correctly, but for some reason, CALL ReadOptions(ReadSuccessFlag) is not working. I am not sure what is causing the error.

Is that a compiler version issue?

No, it is a programmer issue.

If the READ statements were simple and had no IOSTAT= and ERR= clauses, the Fortran runtime would have aborted the program and printed out information on which source line caused the crash and a traceback report. With those clauses present, if the programmer does nothing by way of testing the IOSTAT values and taking appropriate remedial action, it is the programmer who deserves the blame for the mess that is created.

You can run the program in the debugger and put hawk eyes on ReadSuccessFlags as you step through the lines of code, or you can remove all the IOSTAT= and ERR= clauses and let the Fortran runtime tell you where, if any, the read error occurred.

28 Apr 2017 7:31 #19491

If you need to be able to handle an arbitrary data set (and not just this one), you can create a line counter. Start at 1 and increment after each read statement. I do this in my product to let the user know when they have encountered an error (my users will occasionally muck with the data on their own, much to their ultimate grief).

You have some conditional reads, and this will necessarily require a bit more code to be put in place to handle these occurrences.

That said, mecej4's suggestion to remove all the error traps and let the system tell you which line is in error will certainly work. For this data set.

Please login to reply.