getting syntax error in compiling fortran code - fortran

I am trying to run some old Fortran code of my project team in ubuntu 14.04. I have not done any modifications to the existing code. All I have done is installed gfortran, opened a terminal, and gone to the file location using the cd command. Here I have many files, but just consider this table.f file
I am trying to compile the following old fortran code using f77 (fort77 compiler).
SUBROUTINE table(FACTOR, PRR, TRR)
IMPLICIT NONE
INCLUDE'../SOURCES_COUNTERFLOW/unsteadyf_inc.h'
DOUBLE PRECISION PRR, TRR, FACTOR, Y
DOUBLE PRECISION A01, B01, A02, B02
A01 = .038042d0; B01 = 1.52267d0
A02 = .067433d0; B02 = 2.16794d0
IF(TRR .LE. 1.0d0) THEN
TRR = 1.0d0
GO TO 10
ENDIF
10 CONTINUE
IF(PRR .LT. 0.2d0) Y = 1.01d0 * (1.0d0 - A01 * TRR**(-B01))
IF(PRR .GE. 5.0d0)
& Y = 1.07d0 * (1.0d0 - A50*TRR**(-B50))
FACTOR = Y
RETURN
END
I get the following error.
f77 -f -o ../SOURCES_COUNTERFLOW/table.o -c ../SOURCES_COUNTERFLOW/table.f
table:
Error on line 6: syntax error
Error on line 7: syntax error
/usr/bin/f77: aborting compilation
make: *** [../SOURCES_COUNTERFLOW/table.o] Error 25
Please help me to figure out the problem.

I solved the syntax error issue by splitting the statements A01 B01 A02 B02 in separate lines.

Related

How to solve error related to overwriting file in Fortran?

I have the following code:
if ((mod(kk,NINT(1/DT)).eq.0).or.((kk.eq.1).and.(count.eq.0))) then
! Write out .CON file
if ((kk.eq.1).and.(count.eq.0))then
9999 FORMAT(1X,F9.2,1X,F9.3,1X,F9.3,1X,F9.3,1X,F9.3,1X,F9.3,1X,F9.3,1X,F9.3,3E16.1)
open(9,file=NAME_CON,status='new',position='append')
WRITE(9,9999)
else
open(9,file=NAME_CON,status='old',position='append')
end if
WRITE(9,9999) &
TIME, &
sum(H(:,:,3), (H(:,:,3).gt.0))*DX*DY/1000000000, &
sum(G(:,:), (H(:,:,3).gt.0)) / sum(mask, (H(:,:,3).gt.0)), &
sum(mask, (H(:,:,3).gt.0))*DX*DY/1000000, &
maxval(H(:, :, 3)), &
maxval(sqrt(VY(:,:,1)*VY(:,:,1)+VX(:,:,1)*VX(:,:,1))), &
rmse, &
ANEWG, &
ASL
close(9)
end if
However, when I run the script the following error occurs on Line 603 (open(9,file=NAME_CON,status='new',position='append') ):
forrtl: severe (10): cannot overwrite existing file, unit 9, file /theia/home/bxl/101/vsc10101/3Dmod/model/workdir/CON25
Can anyone help to solve this error as I don't know why it is occurring? Thanks.

Fortran: reading file with unknown number of lines

I am trying to open an ASCII file with unknown number of lines (but fixed number of entries in each line - please see example file below). It goes to the end of the file, but fails after that.
Option 1: Using iostat statement: I get the same error when using either "Use, intrinsic :: iso_fortran_env, Only : iostat_end" or simply treating io as an integer.
Option 2: Using "end= .." option. This is what I have used earlier (F77).
Both approaches should work in principle, but neither does. Any help in fixing this will be greatly appreciated.
Thanks,
Pinaki.
Program:
!=======================================!
program read
!=======================================!
! Use, intrinsic :: iso_fortran_env, Only : iostat_end
implicit none
integer :: i,io,n
n=0
open(10,file='a.dat',status='old',action='read')
!=================================!
! option 1
!=================================!
do
read(10,*,iostat=io)
write(*,*)io
! if (io.eq.iostat_end) exit
if (io.ne.0) exit
n=n+1
write(*,*)'n=',n
enddo
!=================================!
!=================================!
! option 2
!=================================!
do 10 i=1,1000000
read(10,*,end=10)
n=n+1
write(*,*)n
10 continue
!================================!
close(10)
write(*,*)'n=',n
end program read
!======================!
Compiled using "gfortran --std=f2003 -o read.out read.f90"
Error message:
==============================================
dyld: lazy symbol binding failed: Symbol not found: ___emutls_get_address
Referenced from: /usr/local/opt/gcc/lib/gcc/11/libgfortran.5.dylib
Expected in: /usr/lib/libSystem.B.dylib
dyld: Symbol not found: ___emutls_get_address
Referenced from: /usr/local/opt/gcc/lib/gcc/11/libgfortran.5.dylib
Expected in: /usr/lib/libSystem.B.dylib
Program received signal SIGABRT: Process abort signal.
Backtrace for this error:
#0 0x105364f8e
#1 0x10536419d
#2 0x7fff6eed05fc zsh: abort ./read.out
=====================================================
File I am trying to read:
=====================================================
1 -1.1559859375 2.0399371094 0.1686166667 0.8242152778
2 -1.1618015625 2.1375250000 0.1765231481 0.8105046296
3 -1.1696710937 2.2325417969 0.1860513889 0.7936782407
1 -1.1730312500 2.3271382813 0.1975254630 0.7718773148
2 -1.1767945313 2.3942726563 0.2113162500 0.7446933333
3 -1.1694437500 2.4738000000 0.2281966667 0.7099266667
1 -1.1566140625 2.5312164063 0.2494466667 0.6636841667
2 -1.1293765625 2.5746707031 0.2747766667 0.6066154167
3 -1.0836390625 2.5938144531 0.3026616667 0.5403733333
1 -1.0380632812 2.5721433594 0.3302462121 0.4727159091
=======================================================================
I tested your code, the first option worked. Your second option would also work if you remove the label 10 from do 10 i=1,1000000 and add a end do right before 10 continue. You would also have to add a rewind(10) or close(10); open(10,file='a.dat',status='old',action='read') right before the second option's do loop. But more interestingly, this problem is very easy to solve in Fortran 2008 using the is_iostat_end() intrinsic function that checks for the end-of-file error code occurrence. Here is a modern implementation,
program read
implicit none
integer :: i, n, iostat, fileUnit
n = 0
open(newunit = fileUnit, file = 'a.dat', status = 'old', action = 'read')
do
read(fileUnit,*,iostat = iostat)
if (is_iostat_end(iostat)) exit
write(*,*) iostat
n = n + 1
write(*,*)'n = ', n
end do
close(fileUnit)
end program read
Compile and rune it with the following commands,
gfortran --std=f2008 -o read.out read.f90
./read.out
Even though the problem was trivial, you asked a very nice question by providing a full code, data files, a recipe to compile the code, and the errors encountered. It deserves an upvote.

Debugging access violation error: writing to 2071E05A0 instead of 3071E05A0

Final edit:
Some users on the silverfrost forums directed me very helpfully, to a simplification of the code and a solution.
The issue can be replicated using the following code:
PROGRAM ML14ERROR
INTEGER :: origzn, destzn
INTEGER,PARAMETER :: MXZMA = 1713, LXTZN = 1714, MXAV = 182
INTEGER,PARAMETER :: JTMPREL = 1003, av = 1
REAL(KIND=2) :: RANDOM#
REAL,dimension (1:mxav,lxtzn,lxtzn,JTMPREL:JTMPREL):: znzndaav
DO origzn=1,lxtzn
DO destzn=1,lxtzn
znzndaav(av,origzn,destzn,JTMPREL) = RANDOM#()
END DO
END DO
DO origzn=1,mxzma
DO destzn=1,mxzma
! This is where the error occurs
znzndaav(av,origzn,lxtzn,JTMPREL)=
$ znzndaav(av,origzn,lxtzn,JTMPREL)+
$ znzndaav(av,origzn,destzn,JTMPREL)
ENDDO
ENDDO
WRITE(6,*)'No errors'
END PROGRAM
The issue only arises when MXAV>182, which suggests a memory issue. Indeed, multiplying out the dimensions: 183 * 1714 * 1714 * 4 yields >2GB, exceeding the stack size.
A solution would be to use the heap as follows (Fortan 95):
PROGRAM ML14ERROR
INTEGER :: origzn, destzn
INTEGER,PARAMETER :: MXZMA = 1713, LXTZN = 1714, MXAV = 191
INTEGER,PARAMETER :: JTMPREL = 1003, av = 1
REAL(KIND=2) :: RANDOM#
REAL,allocatable :: znzndaav(:,:,:,:)
ALLOCATE( znzndaav(1:mxav,lxtzn,lxtzn,JTMPREL:JTMPREL) )
DO origzn=1,lxtzn
DO destzn=1,lxtzn
znzndaav(av,origzn,destzn,JTMPREL) = RANDOM#()
END DO
END DO
DO origzn=1,mxzma
DO destzn=1,mxzma
! This is where the error occurs
znzndaav(av,origzn,lxtzn,JTMPREL)= &
& znzndaav(av,origzn,lxtzn,JTMPREL)+ &
& znzndaav(av,origzn,destzn,JTMPREL)
ENDDO
ENDDO
DEALLOCATE(znzndaav)
WRITE(6,*)'No errors'
END PROGRAM
Once we do this, we can allocate more than 2GB and the array works fine. The program this small section of code stems from is a few years old, and we've only just now run into the issue because a model we've built is many times larger than any before. As Fortran 77 doesn't allow ALLOCATABLE arrays, we must either reduce stack usage, or port the code - or seek another optimisation.
Edited to add:
I have now put together a git repo which contains reproducible code.
Overview
I have a program that works fine when compiled to 32-bit, but presents an access violation error when compiled and run in 64-bit.
I'm using the Silverfrost Fortran compiler, FTN95 v8.51, though this issue occurs using v8.40 and v8.50.
Sample code
! .\relocmon.inc
INTEGER JTMPREL
PARAMETER(JTMPREL=1003)
REAL znda(lxtzn,JTMPREL:JTMPREL)
REAL zndaav(1:mxav,lxtzn,JTMPREL:JTMPREL)
REAL,dimension (lxtzn,lxtzn,JTMPREL:JTMPREL) :: znznda
REAL mlrlsum(lxtzn,lxtzn)
REAL,dimension (1:mxav,lxtzn,lxtzn,JTMPREL:JTMPREL):: znzndaav
COMMON /DDMON/ znda, znznda, mlrlsum,znzndaav, zndaav
! EOF .\relocmon.inc
! .\relocmon.inc with values
INTEGER JTMPREL
PARAMETER(JTMPREL=1003)
REAL znda(1714,JTMPREL:JTMPREL)
REAL zndaav(1:191,1714,JTMPREL:JTMPREL)
REAL,dimension (1714,1714,JTMPREL:JTMPREL) :: znznda
REAL mlrlsum(1714,1714)
REAL,dimension (1:191,1714,1714,JTMPREL:JTMPREL):: znzndaav
COMMON /DDMON/ znda, znznda, mlrlsum,znzndaav, zndaav
! EOF .\relocmon.inc
! .\main.for
INCLUDE 'relocmon.inc'
REAL,save,dimension(lxtzn,lxtzn,mxav) :: ddfuncval
DO origzn=1,mxzma
IF( zonedef(origzn,JZUSE) )THEN
DO destzn=1,mxzma
IF (zonedef(destzn,JZUSE)) THEN
znznda(origzn,destzn,JTMPREL)=znda(destzn,JTMPREL)*
$ ddfuncval(origzn,destzn,av)
znznda(origzn,lxtzn,JTMPREL)=znznda(origzn,lxtzn,JTMPREL)
$ +znznda(origzn,destzn,JTMPREL)
znzndaav(av,origzn,destzn,JTMPREL)=zndaav(av,destzn,JTMPREL)*
$ ddfuncval(origzn,destzn,av)
! LINE 309 -- where error occurs
znzndaav(av,origzn,lxtzn,JTMPREL)=
$ znzndaav(av,origzn,lxtzn,JTMPREL)
$ +znzndaav(av,origzn,destzn,JTMPREL)
ENDIF
ENDDO
ENDIF
ENDDO
! EOF .\main.for
NB the function zonedef simply checks that a zone is valid for the calculation we want to undertake. This function returns a logical.
Debugging
As I mentioned initially, the 32-bit compiled version of this program works fine. When attempting to run the 64-bit version, the output of the first loop is this:
from sdbg64.exe:
Error: Access Violation reading address
0x00000002071E05A0
main.for: 309
write exception to file:
Access violation (c0000005) at address 43a1f4
Within file ml14.exe
in main in line 309, at address 2b84
RAX = 0000000000000001 RBX = 000000027fff704c RCX = 000000000285e6b8 RDX = 00000002802296cc
RBP = 0000000000400000 RSI = 000000029ba3ad6c RDI = 0000000307695374 RSP = 000000000285be70
R8 = 0000000307695374 R9 = 00000002ffff5040 R10 = 000000029ba3ad6c R11 = 000000030731f0dc
R12 = 000000027fff5584 R13 = 00000002802296cc R14 = 000000028169f3ec R15 = 0000000281660928
43a1f4) addss XMM11,[85b401b4++R14]
For the rest of this... please bear with me. I'm not a trained software engineer or fortran developer by any stretch, so I'm stabbing in the dark a little to troubleshoot.
The value for ZNZNDAAV(1,337,337,1003) is 2.241640, and this is being added to ZNZNDAAV(1,337,1714,1003). This tallies with register XMM11 as detailed in the exception output. This value is at address 000000029BA3BD60. The other value is at address 00000003071E05A0.
IIUC, in relocmon.inc we're setting COMMON /DDMON/ to contain the dimensioned array znzndaav, so if the software were working nominally, the address of the value in question would be within the /DDMON/ block. The address range for /DDMON/ is z'000000027FFF6040' - z'0000000307421150'. If my logic is correct, the violation occurs outside of this block.
It appears to me that the program is attempting to write to 00000002071E05A0 when it should be using 00000003071E05A0.
Can anyone help me determine why this would be the case? There appears to be something systematic about it - could it be mere coincidence?

Baron Error with Pyomo: NonLinearity Error in POW expression

I am trying to solve a nonlinear optimization problem in Pyomo with Baron.
The solving works fine when using solvers like ipopt, bonmin, couenne etc.
When using Baron I get the following error:
===========================================================================
BARON version 18.5.9. Built: WIN-64 Wed May 9 22:52:08 EDT 2018
BARON is a product of The Optimization Firm, LLC. http://www.minlp.com/
BARON: NonLinearity Error in POW expression
ERROR: Solver (baron) returned non-zero return code (9)
ERROR: See the solver log above for diagnostic information.
Traceback (most recent call last):
File "C:/Users/public.THREADRIPPER/Desktop/CGAM_SWP.py", line 602, in <module>
Results = opt.solve(comp, tee=True, keepfiles=True)
File "C:\Anaconda3\lib\site-packages\pyomo\opt\base\solvers.py", line 626, in solve
"Solver (%s) did not exit normally" % self.name)
pyutilib.common._exceptions.ApplicationError: Solver (baron) did not exit normally
Process finished with exit code 1
Any idea where the problem is?
Thanks!
I'm not sure how urgent the matter still is but I experienced the same issue as you. Actually, in the BARON.py file there is a note regarding this error message here (hope you can see my screenshot, for some reason I am not allowed to upload pictures).
Anyway, I experience the problem in a different way than noted in the BARON.py file. In my case I am using the pyomo.core.base.symbolic.differentiate() function to create first derivates of functions which are to the power of (1/3) which then result in negative exponents which baron apparently has issues with. If I manually exchange the negative exponents with division, it seems to work.
Hope this helps you.
UPDATE: I have to update my comment because it is not negative exponents (per se) which in my case caused the error message of interest ("NonLinearity Error in POW expression", Solver (baron) returned non-zero return code (9)) but it is the way I had introduced them in the input file. In the baron manual there is an example of the case (see screenshot) I am referring to.
So in my case the error message was caused because I had constraints defined like this:
... * ( -241.5 + 0.5 * x12 ) ^ -1.0 * ( x1 - x6 ) ^ -1.0 * ( -483.0 + x12 + x1 - x6 ) ^ -1.0 * ...
which are according to the baron manual misinterpreted by the solver compared to my intention. Actually, in my case a x^y situation was created with nested negative exponential expressions which probably could be solved by baron if it is introduced differently (see next screenshot). However, if I include parentheses around the exponents, to get the expressions I am aiming for, i.e.:
... * ( -241.5 + 0.5 * x12 ) ^ (-1.0) * ( x1 - x6 ) ^ (-1.0) * ( -483.0 + x12 + x1 - x6 ) ^ (-1.0) * ...
it works perfectly fine.

Fortran95 -- Reading from a formatted text file

I need to read some values from a table. These are the first five rows, to give you some idea of what it should look like:
1 + 3 98 96 1
2 + 337 2799 2463 1
3 + 2801 3733 933 1
4 + 3734 5020 1287 1
5 + 5234 5530 297 1
My interest is in the first four columns of each row. I need to read these into arrays. I used the following code:
program ----
implicit none
integer, parameter :: totbases = 4639675, totgenes = 4395
integer :: codtot, ks
integer, dimension(totgenes) :: ngene, lend, rend
character :: genome*4639675, sign*4
open(1,file='e_coli_g_info')
open(2,file='e_coli_g_str')
do ks = 1, totgenes
read(1,100) ngene(ks),sign(ks:ks),lend(ks), rend(ks)
end do
100 format(1x,i4,8x,a1, 2(5x,i7), 22x)
do ks = 1, 100
write(*,*) ngene(ks), sign(ks:ks),lend(ks), rend(ks)
end do
end program
The loop at the end of the program is to print the first hundred entries to test that they are being read correctly. The problem is that I am getting this garbage (the fourth row is the problem):
1 + 3 757934891
2 + 337 724249387
3 + 2801 757803819
4 + 3734 757803819
5 + 5234 757935405
Clearly, the fourth column is way off. In fact, I cannot find these values anywhere in the file that I am reading from. I am using the gfortran compiler for Ubuntu 12.04. I would greatly appreciate if somebody would point me in the right direction. I'm sure it's likely that I'm missing something very obvious because I'm new at Fortran.
Fortran formats are (traditionally, there's some newer stuff that I won't go into here) fixed format, that is, they are best suited for file formats with fixed columns. I.e. column N always starts at character position M, no ifs or buts. If your file format is more "free format"-like, that is, columns are separated by whitespace, it's often easier and more robust to read data using list formatting. That is, try to do your read loop as
do ks = 1, totgenes
read(1, *) ngene(ks), sign(ks:ks), lend(ks), rend(ks)
end do
Also, as a general advice, when opening your own files, start from unit 10 and go upwards from there. Fortran implementations typically use some of the low-numbered units for standard input, output, and error (a common choice is units 1, 5, and 6). You probably don't want to redirect those.
PS 2: I haven't tried your code, but it seems that you have a bounds overflow in the sign variable. It's declared of length 4, but then you assign to index ks which goes all the way up to totgenes. As you're using gfortran on Ubuntu 12.04 (that is, gfortran 4.6), when developing compile with options "-O1 -Wall -g -fcheck=all"