Silverfrost Forums

Welcome to our forums

Problem in program

3 Nov 2009 8:36 #5314

I am having a simple problem with a Fortran program I have built in Fortran. The program will run as desired in the NAG compiler but not in Silverfrost (plato). Can anybody see the problem?

The program imports external data. Using a subroutine it calculates enginnering strain and writes to an external file. Using a second subroutine it calculates true strain and writes to an external file.

PROGRAM array_true_stress

INTEGER, PARAMETER :: MAX_SIZE = 5970 REAL, DIMENSION(MAX_SIZE) :: a REAL, DIMENSION(MAX_SIZE) :: b REAL, DIMENSION(MAX_SIZE) :: c

CHARACTER(len=20) :: filename INTEGER :: i INTEGER :: nvals = 0 INTEGER :: status REAL :: temp

WRITE(,1000) 1000 FORMAT(1X,'Enter the filename with the data to be manipulated: ') READ(,'(A20)') filename

OPEN(UNIT=9, FILE=filename, STATUS='OLD', ACTION='READ', IOSTAT=status) fileopen: IF( STATUS == 0 ) THEN

DO
	READ(9,*,IOSTAT=status) temp
	IF( status /= 0) EXIT
	nvals=nvals+1
	a(nvals)=temp
END DO

CALL engineering_strain (i, nvals, b, a)
OPEN(UNIT=10, FILE='eng_strain.txt', STATUS='OLD', ACTION='WRITE')
WRITE(10,960) (b(i), i=1, nvals)
WRITE(*,*) 'Engineering strain data written to an external file'
960 FORMAT (4X,F16.9)	

CALL true_strain (i, nvals, a, c)
OPEN(UNIT=11, FILE='true_strain.txt', STATUS='OLD', ACTION='WRITE')
WRITE(11,960) (c(i), i=1, nvals)
WRITE(*,*) 'True strain data written to an external file'

ELSE fileopen WRITE(*,1050) status 1050 FORMAT(1X,'File open failed--status=',I6) END IF fileopen

CONTAINS SUBROUTINE engineering_strain (i, nvals, b, a) REAL, INTENT(IN):: a(i) REAL, INTENT(OUT):: b(i) convert_eng: DO i=1, nvals b(i)=a(i)/100 END DO convert_eng END SUBROUTINE engineering_strain

SUBROUTINE true_strain (i, nvals, a, c)
	REAL, INTENT(IN):: a(i)
	REAL, INTENT(OUT):: c(i)
	add: DO i=1, nvals
	c(i)=LOG((a(i)/100)+1)
	END DO add
END SUBROUTINE true_strain

END PROGRAM array_true_stress

3 Nov 2009 10:47 #5319

I tried your program and it works fine for me. First I created a file with some data points (eng.dat) and some empty files true_strain.txt and eng_strain.txt. The program executes with the desired external files written.

I thought that CONTAINS is only used in modules: learned something new 😉

3 Nov 2009 2:34 #5323

Hi jjgermi,

Thank you for your reply.

This is really confusing me! When I run the previous source code I receive the following error:

Runtime error from program:g:\test.exe Run-time Error *** Error 112, Reference to undefined variable, array element or function result (/UNDEF)

ARRAY_TRUE_STRESS~ENGINEERING_STRAIN - in file test.f95 at line 44 [+002f]

main - in file test.f95 at line 28 [+027f]

This is in reference to the CONTAINS use for the first subroutine. I reinstalled silverfrost and still receive this error message. Can you think of any reason why this program works for you and not for me? It works for me in the NAG compiler. In silverfrost it will compile and ctreate the executable file but the previous error message is generated when executed.

Any suggestions?

3 Nov 2009 3:02 #5324

Reference to undefined variable means just that. You have used a value that was not previously set.

If you run the program through the debugger then it will stop at the offending value.

3 Nov 2009 3:54 #5326

Change the array declarations in the subroutines to the following:-

REAL, INTENT(IN):: a(nvals) REAL, INTENT(OUT):: b(nvals)

3 Nov 2009 5:26 #5329

Thank you JohnHorspool. That has solved my problem.

Can you briefly explain why that worked?

Thank you for your time.

3 Nov 2009 6:25 #5331

If I read properly your code, in the first call to subroutine you pass the argument 'i' without having assigned it a value yet.

This causes an exception - as Paul said - if you compile your program with the 'CheckMate Win32' option (default with Plato). Instead if you compile your program in 'Debug Win32' or 'Release Win32' mode you will not see any error (may be this is why jjgeremis had no problem).

In any case I think that your code works better with John’s corrections.

Good luck!

3 Nov 2009 6:51 #5332

Subroutine, as Emanuele has said 'i' was unassigned with the call to the first subroutine, which then attempts to declare an array size using it. This is incorrect, the array contains 'nvals' items, thus use 'nvals' for its size declaration.

3 Nov 2009 7:16 #5333

Emanuele is correct: I did compile the program with 'Debug Win32'. One can only learn here 😃

4 Nov 2009 8:15 #5335

Thanks guys for the informed response. I know can see why my program wasn't working. I will now annotate my program for future reference!

That is an interesting point regarding the 'debug win32' and 'releasewin32'.

Please login to reply.