How to solve error related to overwriting file in Fortran? - 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.

Related

getting syntax error in compiling fortran code

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.

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.

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.

int object not callable error (sum of multiples)

I am writing a program to print the sum of multiple of 3 or 5 less than 1000. I am using an arithmetic progression to do it. My code is:
def multiple(x,y):
a=(1000-(1000%x) - x)/x +1
b=(995-y)/y +1
c=(1000-(1000%x*y)-x*y)/x*y +1
Sa=int(a/2(2*x+(a-1)*a))
Sb=b/2(2*y+(b-1)*b)
Sc=c/2(2*x*y+(c-1)*x*y)
Sd=Sa+Sb-Sc
print Sd
When I call the function I get the error:
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\Python27\swampy-2.1.7\MULTIPLE.py", line 23, in multiple
Sa=int(a/2(2*x+(a-1)*a))
TypeError: 'int' object is not callable
Please point out the mistake in my code. Thanks.
P.S. Please forgive my "art" of question asking. I am new to Python and StackOverflow, so please bear with me. Thanks!
In Sa=int(a/2(2*x+(a-1)*a)) you forgot a * to multiply between a/2 and (2*x+(a-1)*a) You should have Sa=int(a/2*(2*x+(a-1)*a)).
Also, the same on Sb and Sc.

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"