Related
I have a fortran program where I have to open a binary output file. normally one specifies.
RECL=num
However for my particular case, a file opened with that option does not work. What works is
RECORDTYPE=stream
The gfortran which i normally does not accept the second option. With the RECL option, the program works, but the data is unusable. The Intel compiler does. Intel no longer gives a free compiler and I lost the free Intel compiler I had.
I would appreciate any suggestion to write a stream with gfortran, or if someone can backup their compiler folder, put it on google drive and give me the link (hoping this is not an abuse).
I work on Ubuntu 18.04.
I paste below, a sample program I use and the data to be written to binary.
Program stndat
implicit none
integer :: iflag=0,itime=0
integer :: nlev,ihrold,nflag,icount
integer :: ihr !,iwx,delta,cld,vis
integer,dimension(1:3) :: today,now
Real :: lat,lon,iu,iv,itemp,idp,ipress
CHARACTER(len=3) :: stnid
character(len=8) :: date
character(len=10) :: time
character(len=5) :: zone
character(len=8) :: year,inst*10,hour*2
!
integer,dimension(1:8) :: values
! using keyword arguments
call date_and_time(date,time,zone,values)
!
open(1,file='tmp.txt')
write(1,'(1x,a8,2x,a10)') date,time
Program stndat
implicit none
integer :: iflag=0,itime=0
integer :: nlev,ihrold,nflag,icount
integer :: ihr !,iwx,delta,cld,vis
integer,dimension(1:3) :: today,now
Real :: lat,lon,iu,iv,itemp,idp,ipress
CHARACTER(len=3) :: stnid
character(len=8) :: date
character(len=10) :: time
character(len=5) :: zone
character(len=8) :: year,inst*10,hour*2
!
integer,dimension(1:8) :: values
! using keyword arguments
call date_and_time(date,time,zone,values)
!
open(1,file='tmp.txt')
write(1,'(1x,a8,2x,a10)') date,time
rewind(1)
read(1,'(1x,a8,2x,a10)') year,inst
close(1,status='delete')
iflag=0
itime=0
icount=1
hour = time(:2)
hour = '09'
! Open up I/O Files
Open(unit=11,file='../../Output/synop4grads.txt',status='old')
Open(unit=12,file='../../Output/Obs_out.bin',form='unformatted',recordtype='stream',status='unknown')
10 continue
! Read in the line of code
read(11,'(1x,a3,2x,f5.2,2x,f6.2,2x,i2,8(2x,f5.1))',END=40) stnid,lon,lat,ihr,iu,iv,itemp,idp,ipress
IF (iflag.EQ.0) THEN
iflag=1
ihrold=ihr
ENDIF
!
! Check to see if this is an old time group. If so, write a terminator
IF (ihrold.NE.ihr) THEN
nlev=0
write(12)stnid,lat,lon,ihr,nlev,nflag
ENDIF
! Now make ihrold equal to the current ihr
ihrold=ihr
!
! Now we can go ahead and write the report
! First, define some variables to be used in the header
itime=0.
nlev=1
nflag=1
write(12) stnid,lat,lon,ihr,nlev,nflag
!
! read in the following: itemp,idp,ipress,iu,iv,iwx,ic,icl
write(12) iu,iv,itemp,idp,ipress
! When its done writing one report, instruct the program
! to read-in the next line
go to 10
!
! When there are no more lines to read-in, the program
! goes to 40
40 continue
nlev=0
!
write(12)stnid,lat,lon,ihr,nlev,nflag
close(11)
close(12)
end
And the data
403 29.08 -9.80 01 -0.5 -3.0 24.0 16.5 496.0
441 24.43 -11.75 01 -99.0 -99.0 18.5 16.8 533.0
461 28.85 -11.10 01 -1.4 -9.9 26.0 17.7 485.0
475 31.12 -10.21 01 -1.0 -9.9 24.4 14.0 509.0
476 31.25 -10.10 01 -0.7 -6.0 24.0 14.5 -99.0
477 31.43 -11.90 01 -1.7 -49.0 -9.9 0.0 500.0
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?
I wrote this program that reads daily gridded climate model data (6 variables) from a file and uses it in further calculations. When running the pgm for a relatively short period (e.g. 5 years) it works fine, but when I want to run it for the required 30 year period I get a "Segmentation fault".
System description: Lenovo Thinkpad with Core i7 vPro with Windows 10 Pro
Program run in Fedora (64-bit) inside Oracle VM VirtualBox
After commenting out everything and checking section-by-section I found that:
everything works fine for 30 years as long as it reads 4 variables only
as soon as the 5th or 6th variable is added, the problem creeps in
alternatively, I can run it with all 6 variables but then it only works for a shorter analysis period (e.g. 22 years)
So the problem might lie with:
the statement: recl=AX*AY*4 which I borrowed from another pgm, yet changing the 4 doesn't fix it
the system I'm running the pgm on
I have tried the "ulimit -s unlimited" command suggested elsewhere, but only get the response "cannot modify limit: Operation not permitted".
File = par_query.h
integer AX,AY,startyr,endyr,AT
character pperiod*9,GCM*4
parameter(AX=162,AY=162) ! dim of GCM array
parameter(startyr=1961,endyr=1990,AT=endyr-startyr+1,
& pperiod="1961_1990")
parameter(GCM='ukmo')
File = query.f
program query
!# A FORTRAN program that reads global climate model (GCM) data to
!# be used in further calculations
!# uses parameter file: par_query.h
!# compile as: gfortran -c -mcmodel=large query.f
!# gfortran query.o
!# then run: ./a.out
! Declarations ***************************************************
implicit none
include 'par_query.h' ! parameter file
integer :: i,j,k,m,n,nn,leapa,leapb,leapc,leapn,rec1,rec2,rec3,
& rec4,rec5,rec6
integer, dimension(12) :: mdays
real :: ydays,nyears
real, dimension(AX,AY,31,12,AT) :: tmax_d,tmin_d,rain_d,rhmax_d,
& rhmin_d,u10_d
character :: ipath*43,fname1*5,fname2*3,nname*14,yyear*4,mmonth*2,
& ext1*4
! Data statements and defining characters ************************
data mdays/31,28,31,30,31,30,31,31,30,31,30,31/ ! Days in month
ydays=365. ! Days in year
nyears=real(AT) ! Analysis period (in years)
ipath="/run/media/stephan/SS_Elements/CCAM_africa/" ! Path to
! input data directory
fname1="ccam_" ! Folder where data is located #1
fname2="_b/" ! Folder where data is located #2
nname="ccam_africa_b." ! Input filename (generic part)
ext1=".dat"
leapa=0
leapb=0
leapc=0
leapn=0
! Read daily data from GCM ***************************************
do n=startyr,endyr ! Start looping through years --------------
write(yyear,'(i4.4)')n
nn=n-startyr+1
! Test for leap years
leapa=mod(n,4)
leapb=mod(n,100)
leapc=mod(n,400)
if (leapa==0) then
if (leapb==0) then
if (leapc==0) then
leapn=1
else
leapn=0
endif
else
leapn=1
endif
else
leapn=0
endif
if (leapn==1) then
mdays(2)=29
ydays=366.
else
mdays(2)=28
ydays=365.
endif
do m=1,12 ! Start looping through months --------------------
write(mmonth,'(i2.2)')m
! Reading daily data from file
print*,"Reading data for ",n,mmonth
open(101,file=ipath//fname1//GCM//fname2//nname//GCM//"."//
& yyear//mmonth//ext1,access='direct',recl=AX*AY*4)
do k=1,mdays(m) ! Start looping through days --------------
rec1=(k-1)*6+1
rec2=(k-1)*6+2
rec3=(k-1)*6+3
rec4=(k-1)*6+4
rec5=(k-1)*6+5
rec6=(k-1)*6+6
read(101,rec=rec1)((tmax_d(i,j,k,m,nn),i=1,AX),j=1,AY)
read(101,rec=rec2)((tmin_d(i,j,k,m,nn),i=1,AX),j=1,AY)
read(101,rec=rec3)((rain_d(i,j,k,m,nn),i=1,AX),j=1,AY)
read(101,rec=rec4)((rhmax_d(i,j,k,m,nn),i=1,AX),j=1,AY)
read(101,rec=rec5)((rhmin_d(i,j,k,m,nn),i=1,AX),j=1,AY)
read(101,rec=rec6)((u10_d(i,j,k,m,nn),i=1,AX),j=1,AY)
enddo ! k-loop (days) ends --------------------------------
close(101)
enddo ! m-loop (months) ends --------------------------------
enddo ! n-loop (years) ends -----------------------------------
end program query
I try in a small code to write output results with numerical values between various text.
For the moment, I do :
! Print results
write(*,*)
write(*,*) ' Time step = ',dt
write(*,*)
write(*,1001) epsilon,step
write(*,*)
write(*,*) ' Problem size = ',size_x*size_y
write(*,*)
write(*,1002) elapsed_time
write(*,*)
write(*,*) ' Computed solution in seq.dat file '
write(*,*)
! Formats available to display the computed values on the grid
1001 format(' Convergence = ',f11.9,' after ',i9,' steps ')
1002 format(' Wall Clock = ',f15.6)
which produces at the execution :
Time step = 0.000003755783907217
Convergence = 0.100000000 after 8882 steps
Problem size = 24576
Wall Clock = 5.213814
Computed solution in Seq.dat
My issue is about the line "Wall Clock = 5.213814", I would like to get only one space juste after "Wall Clock =" before the value "5.213814". Currently, I think these multiple spaces that I get come from the "f15.6" with 1002 format(' Wall Clock = ',f15.6).
Here's what I want to get (with another value for steps) :
Time step = 0.000003755783907217
Convergence = 0.100000000 after 20910988821 steps
Problem size = 24576
Wall Clock = 5.213814
Computed solution in Seq.dat
I have set "f15.6" since I can get high number for "Wall Clock", same thing for espilon and step variables.
I don't know in all cases how to set just one space between words and values to write between them, as when I printf, in C language, different values and words on the same line.
I know there's a simple solution but can't find it.
UPDATE 1 :
I tried the solution indicated in the first answer.
Here's what I have done :
write(*,1001) epsilon,step
write(*,1002) elapsed_time
1001 format(' Convergence = ',f0.9,' after ',i9,' steps ')
1002 format(' Wall Clock = ',f0.6)
and I get :
Convergence = .100000000 after 8882 steps
Problem size = 24576
Wall Clock = 2.492813
As you can see, "Convergence" value is .100000000 instead of 0.100000000 (leading zero has disappeared).
And what about the integers values, can I write "i0" to have as few as possible ?
Thanks
Modern Fortran compilers understand a 'length' of 0 to mean: As few as possible:
program write_format
use iso_fortran_env, only: real64
implicit none
print 1001, 5.213814
print 1001, 12345678.901234_real64
1001 format("Wall Clock = ", f0.6)
end program write_format
Output:
Wall Clock = 5.213814
Wall Clock = 12345678.901234
Cheers
Usually it's not liked to update the question after the answer to ask additional questions, but since they're quite similar, I think it's okay.
Firstly, yes, format I0 means as few digits as necessary, and probably is what you want.
The second part is trickier, it seems to boil down to 'at least that many digits, but more if needed' -- and I don't think there's a format specifier for that (but I might be wrong).
I'd probably cheat and use something like this:
if (epsilon < 10.) then
write(*, 1002) epsilon
else
write(*, 1003) epsilon
end if
1002 format("Convergenge = ", f11.9)
1003 format("Convergence = ", f0.9)
But then again, I also found this answer quite intuitive: How to pad FORTRAN floating point output with leading zeros?
Adapted for you, it would mean splitting the floating point number into an integer and the rest, and putting it back together again:
write(*, 1002) int(epsilon), epsilon-int(epsilon)
1002 format("Convergence = ", I0, F0.9)
this is a bit cumbersome, but one way to get minimum width and preserve the lead zero is to use an internal write like this:
character*30 val
write(val,'(f11.9)')0.1d0
write(*,'(3a,i0,a)')'converge = ',trim(adjustl(val)),' after ',32432,' steps'
converge = 0.100000000 after 32432 steps
I have recently changed my f90 editor to CodeBlocks for Mac OS X, and when I try to open a file located in the project folder to read the data, the next error message appears on screen when the code is run:
Fortran runtime error: Bad integer for item 0 in list input
I have introduced the same code I used to write in Windows 7 using the intel compiler for fortran and Visual Studio.
The code itself is:
subroutine read_input_data
use input_data
implicit none
integer i,j
open(UNIT=5, FILE='lifting_line_input_data.txt', STATUS='old', FORM='formatted', ACCESS='sequential')
read(5,*) C
read(5,*) U
read(5,*) alpha
read(5,*) rho
read(5,*) wake_length
read(5,*) wake_eps
read(5,*) n_chord
read(5,*) n_twist
if (n_chord .GE. n_twist ) then
i = n_chord
else
i = n_twist
end if
allocate(chord_twist(5,i))
do j = 1, i
read(5,*) chord_twist(:,j)
end do
close(5)
end subroutine read_input_data
Could you help me to solve this problem? Thank you very much.
PD. the data file is obtain from an Excel sheet saved as a .txt delimited by tabulations
! LIFTING-LINE WING
! Number of panels
6
! Free stream speed [m/s]
50
! Angle of attack [rad]
0.15
! Air density [kg/m^3]
1.225
! Wake length [m]
100
! Convergence parameter
0.01
! Number of data points given for the chord distribution
2
! Number of data points given for the twist distribution
2
! Y coord [m] ! X_LE [m] ! X_TE [m] ! Y coord [m] ! Twist [rad]
0 0 2 0 0
10 0 0.5 10 0.052359878
PD2. I have change the format of the .txt file to make it equal to the input files I had used in Visual Studio. Now the file is:
6 ! Number of panels
50 ! Free stream speed [m/s]
0.15 ! Angle of attack [rad]
1.225 ! Air density [kg/m^3]
100 ! Wake length [m]
0.01 ! Convergence parameter
2 ! Number of data points given for the chord distribution
2 ! Number of data points given for the twist distribution
0 0 2 0 0 ! Y coord [m] ! X_LE [m] ! X_TE [m] ! Y coord [m] !Twist [rad]
10 0 0.5 10 0.052359878
And now the error given at the terminal is that the file is not found. As I am a beginner in CodeBlocks, I will explain what I have done step by step because I do not find where I am wrong and I am starting to get desperate:
New Project -> Fortran application -> I indicate where I want to create the project file.
I remove the main.f95 file and I add the .f90 file with the code.
I write the code.
I save the .txt file in the same folder than all the files of the Project.
When I run the code it appears the error message of file not found.
The code is:
!************************************************
subroutine read_input_data
use input_data
implicit none
integer i,j
open(UNIT=10, FILE='lifting_line_wing_input.txt', STATUS='old', ACCESS='sequential')
read(10,*) C
read(10,*) U
read(10,*) alpha
read(10,*) rho
read(10,*) wake_length
read(10,*) wake_eps
read(10,*) n_chord
read(10,*) n_twist
if (n_chord .GE. n_twist ) then
i = n_chord
else
i = n_twist
end if
allocate(chord_twist(5,i))
do j = 1, i
read(10,*) chord_twist(:,j)
end do
close(10)
end subroutine read_input_data
!************************************************
Thank you very much for your time and help
This looks like your old system did something non-standard with exclamation marks on list-directed input.
Try reformatting your input data like
6 / number of panels
(the slash will terminate the READ).
i don't believe any fortran compiler ever automagically handled those comments.
If you want to read this file the way it is, one approach is to make each read handle the error, eg,
integer ios
ios = 1
do while(ios.ne.0)
read(unit,*,iostat=ios)c
end do
ios=1
do while(ios.ne.0)
read(unit,*,iostat=ios)u
end do
etc..
if its a one-off you could just edit the file and delete all the comments as well.