Fortran - problem with double precision - fortran

I have a small program that read some data from binary file and stores it into normal (unformatted) files. Here is the source:
Program calki2e
IMPLICIT NONE
!
DOUBLE PRECISION VAL
INTEGER P,Q,R,S
INTEGER IREC2C
PARAMETER( IREC2C=15000)
INTEGER AND,RSHIFT,LABEL,IMBABS,NX,IB,NFT77
INTEGER IND
DIMENSION IND(IREC2C)
DOUBLE PRECISION XP
DIMENSION XP(IREC2C)
CHARACTER(LEN=12) :: FN77 = 'input08'
CONTINUE
NFT77=77
!----------------------------------------------------------------------
2 CONTINUE
c
open(unit=NFT77,file=FN77,STATUS='OLD',
+ACCESS='SEQUENTIAL',FORM='UNFORMATTED')
open(unit=13,file='calki2e.txt')
REWIND(77)
4100 continue
READ(77) NX,IND,XP
IMBABS=IABS(NX)
DO 100 IB=1,IMBABS
LABEL=IND(IB)
P= AND(RSHIFT(LABEL, 24),255)
Q= AND(RSHIFT(LABEL, 16),255)
R= AND(RSHIFT(LABEL, 8),255)
S= AND( LABEL ,255)
VAL=XP(ib)
IF(P.EQ. Q) VAL=VAL+VAL
IF(R .EQ. S) VAL=VAL+VAL
IF((P .EQ. R).AND.(Q .EQ. S)) VAL=VAL+VAL
write(13,*)P,Q,R,S,val
100 CONTINUE
IF (NX.GT.0) GOTO 4100
CRB
CLOSE(UNIT=NFT77)
!
END
When I compile it using gfortran I obtain double precision in output file but with g77 I get only single precision. What it wrong and how to change it?

Do you mean the "write (13, *) statement. This is "list directed" output. It is a convenience I/O with few rules -- what you get will depend upon the compiler -- it is best used for debugging and "quick and dirty" programs. To reliably get all the digits of double precision, change to a formatted output statement, specifying the number of digits that you need. (It is probably best to switch to gfortran anyway, as g77 is no longer under development.)

your numbers are double precision but you are printing them in free format. You have to specify an explicit format

If you want to keep your code F77, try something like
write(13,1000) P,Q,R,S,val
1000 format(1X,4I7,1X,1E14.10)
The "1X"s mean one space, "4I7" means four seven-width integers, and 1E14.10 means one fourteen-charater width scientific-notation real number with 10 significant digits. Feel free to mess around with the numbers to get it to look right.
This is a pretty good tutorial on the topic.

I would be tempted to set the format on your write statement to something explicit, rather than use * in write(13,*)P,Q,R,S,val.

Related

Reading variable length data in FORTRAN

I have an input file that I cannot alter the format. One of the lines in particular can contain either 6 or 7 reals and I don't have any way of knowing ahead of time.
After some reading, my understanding of the list-formatted read statement is that if I attempt to read 7 reals on a line containing 6, it will attempt to read from the next line. The author of the code says that when it was written, it would read the 6 reals and then default the 7th to 0. I am assuming he relied on some compiler specific behavior, because I cannot find a mention of this behavior anywhere.
I am using gfortran as my compiler, is there a way to specify this behavior? Or is there a good way to count a number of inputs on a line and rewind to then chose to read the correct number?
here is a little trick to accomplish that
character*100 line
real array(7)
read(unit,'(a)')line !read whole line as string'
line=trim(line)//' 0' !add a zero to the string
read(line,*)array !list read
If the input line only had 6 values, the zero is now the seventh.
If there were seven to begin with it will do nothing.
I try to avoid using format specifiers on input as much as possible.
Maybe you should use the IOSTAT statement for detecting the wrong format when you attempt to read 7 values when there are only 6. And you should use the ADVANCE statement to be able to retry to read the same line.
READ(LU,'7(F10.3)', IOSTAT=iError, ADVANCE='NO') MyArray(1:7)
IF(iError > 0) THEN
! Error when trying to read 7 values => try to read 6 !
READ(LU, '6(F10.3)') MyArray(1:6)
ELSEIF(iError == 0) THEN
READ(LU, *) ! For skipping the line read with success with 7 values
ENDIF
IOSTAT takes a negative value for example when you reach the end of the file, positive for problem of reading (typically formatting error) and 0 when the read succeed. See this link for a complete definition of gfortran error code: http://www.hep.manchester.ac.uk/u/samt/misc/gfortran_errors.html
Another way to do it could be to read the line as a string and manipulating the string in order to get the vector values :
CHARACTER(LEN=1000) :: sLine
...
READ(LU, '(A)') sLine
READ(sLine,'7(F10.3)', IOSTAT=iError) MyArray(1:7)
IF(iError > 0) THEN
! Error when trying to read 7 values => try to read 6 !
READ(sLine, '6(F10.3)') MyArray(1:6)
ENDIF
If the values are written in fixed format, you can determine the lenght of the vector by testing the lenght of the line:
CHARACTER(LEN=1000) :: sLine
INTEGER :: nbValues
CHARACTER(LEN=2) :: sNbValues
...
READ(LU, '(A)') sLine
nbValues = LEN_TRIM(sLine) / 10 ! If format is like '(F10.x)'
WRITE(sNbValues, '(I2)') nbValues
READ(sLine, '('//TRIM(sNbValues)//'(F10.3))') MyArray(1:nbValues)

Error: Invalid form of PROGRAM statement at (1)

I am trying to run a fortran code with the below command:
f95 -lm extrapolate3-node-irregularv2.f
and I got the below error
extrapolate3-node-irregularv2.f:1.14:
PROGRAM extrapolate3c--------------------------------------------
1
Error: Invalid form of PROGRAM statement at (1)
The beginning of my program like this:
PROGRAM extrapolate3
c------------------------------------------------------
c 2nd step after runnin extrapolate-node-master...-v5(all dat).f
c so take the xxx-NODUP.dat file and proceed ..
c ------- This extrapolates the int pts to the nodal values
c and then it outputs as per TECPLOT requirements~
c ---------------------------------------
IMPLICIT NONE
integer tot_node,dim,tot_elem,d1
parameter (dim=8,tot_elem=25,tot_node=72)
INTEGER i,j,k,writecount,count
DOUBLE PRECISION x(tot_elem*8),y(tot_elem*8),
$ z(tot_elem*8)
double precision val(tot_elem*8),d3
integer kstep,KINC
integer jelem(tot_elem*8),
$ kintk(tot_elem*8)
integer l_jelem,l_kintk(dim)
double precision l_x(dim),l_y(dim),l_z(dim)
double precision l_val(dim),l_nodal(dim)
double precision xi(dim),eta(dim),zeta(dim),wvar
double precision shfn(dim,dim),shfninv(dim,dim),one,eighth
double precision det
integer ii,jj,kk,err10
integer conn(tot_elem,dim+2)
double precision g_nodal(tot_node)
double precision g_x_node(tot_node),
$ g_y_node(tot_node),
$ g_z_node(tot_node)
double precision v1,v2,v3,dummy
double precision l_x_node(dim),l_y_node(dim),l_z_node(dim)
integer g_common_node(tot_node)
Did anyone meet the same problem before?
Look closely at the error message, which reports an error in the statement
PROGRAM extrapolate3c--------------------------------------------
which appears to be the first two lines of your program, i.e.
PROGRAM extrapolate3
c------------------------------------------------------
run together. I suspect an invalid line-end or carriage-return character between the lines. Do some editing, with a proper programmer's editor such as vi or emacs and make sure that all the whitespace in the source file is space characters (no tabs, no funny invisible non-printing characters) and all line-endings are, well, whatever is the default on your platform. That kind of error sometimes occurs when copying source from, say, Windows to, say, Linux, and recompiling.

Fortran's warning on a program for evaluating elliptic integrals

On internet, I found this program that demonstrate Evaluating elliptic integrals of first and second kinds (complete)
implicit none
real*8 e,e1,e2,xk
integer i, n
e=1.d-7
print *,' K K(K) E(K) STEPS '
print *,'------------------------------------------'
xk=0.d0
do i = 1, 20
call CElliptic(e,xk,e1,e2,n)
write(*,50) xk,e1,e2,n
xk = xk + 0.05d0
end do
print *,'1.00 INFINITY 1.0000000 0'
stop
50 format(' ',f4.2,' ',f9.7,' ',f9.7,' ',i2)
end
Complete elliptic integral of the first and second kind. The input parameter is xk, which should be between 0 and 1. Technique uses Gauss' formula for the arithmogeometrical mean. e is a measure of the convergence accuracy. The returned values are e1, the elliptic integral of the first kind, and e2, the elliptic integral of the second kind.
Subroutine CElliptic(e,xk,e1,e2,n)
! Label: et
real*8 e,xk,e1,e2,pi
real*8 A(0:99), B(0:99)
integer j,m,n
pi = 4.d0*datan(1.d0)
A(0)=1.d0+xk ; B(0)=1.d0-xk
n=0
if (xk < 0.d0) return
if (xk > 1.d0) return
if (e <= 0.d0) return
et n = n + 1
! Generate improved values
A(n)=(A(n-1)+B(n-1))/2.d0
B(n)=dsqrt(A(n-1)*B(n-1))
if (dabs(A(n)-B(n)) > e) goto et
e1=pi/2.d0/A(n)
e2=2.d0
m=1
do j = 1, n
e2=e2-m*(A(j)*A(j)-B(j)*B(j))
m=m*2
end do
e2 = e2*e1/2.d0
return
end
I have compiled it but I have received the following errors:
gfortran -Wall -c "gauss.f" (nel direttorio: /home/pierluigi/Scrivania)
gauss.f:53.9:
50 format(' ',f4.2,' ',f9.7,' ',f9.7,' ',i2)
1
Error: Invalid character in name at (1)
gauss.f:83.72:
if (dabs(A(n)-B(n)) > e) goto et
1
Warning: Deleted feature: Assigned GOTO statement at (1)
gauss.f:83.35:
if (dabs(A(n)-B(n)) > e) goto et
1
Error: ASSIGNED GOTO statement at (1) requires an INTEGER variable
gauss.f:48.18:
write(*,50) xk,e1,e2,n
1
Error: FORMAT label 50 at (1) not defined
Compilation failed.
Any suggestions please?
EDIT
I have read all your answers and thanks to you I managed to compile the program. I also have another curiosity and I do not know whether to write another question. In the meantime I modify this question. In my program, xk is increased by 0.05. Now I will that the program to read data from a file containing: the minimum value of xk; the maximum value of xk; the number of intervals. I thought:
open (10,file='data/test')
read (10,*) xkmi, xkma
read (10,*) nk
close (10)
lkmi = dlog(xkmi)
lkma = dlog(xkma)
ldk = (lkma-lkmi)/dfloat(nk-1)
In addition, the program must be modified in such a way that the result is written to another file. How can I change the rest of the program? Thank you very much.
Your source code file extension is f which, I think (check the documentation), tells gfortran that the file contains fixed source form. Until Fortran 90 Fortran was still written as if onto punched cards and the location of various bits and pieces of a line is confined to certain columns. A statement label, such as 50 in the first of the error messages, had to be in columns 1 - 6. Two solutions:
Make sure the label is in (some of) those columns. Or, better
Move to free source form, perhaps by changing the file extension to f90, perhaps by using a compilation option (check your documentation).
The error raised by the goto et phrase is, as your compiler has told you, an example of a deleted feature, in which the goto jumps to a statement whose label is provided at run-time, ie the value of et. Either tell your compiler (check ...) to conform to an old standard, or modernise your source.
Fix those errors and, I suspect, the other error messages will disappear. They are probably raised as a consequence of the compiler not correctly parsing the source after the errors.
Because the file has type ".f" gfortan is interpreting it as fixed-source layout. Trying compiling with the free-form layout by using compiler option -ffree-form and see if that works. This probably explains the error about the "invalid character". That statement not being recognized explains the "format not defined error". The "computed goto" is obsolete but valid Fortran. You can ignore that warning. If you wish, later you can modernize the code. For the remaining error, for the "assigned goto", declare "et" as an integer.
I would just do this
10 n = n + 1
! Generate improved values
A(n)=(A(n-1)+B(n-1))/2.d0
B(n)=dsqrt(A(n-1)*B(n-1))
if (dabs(A(n)-B(n)) > e) goto 10
and possibly compile as free form source as others have shown. The label et seems weird and non-standard, possibly a rare vendor extension.
You could also change the lines above to a do-loop with an exit statement (Fortran 90).
(The program compiled for me after the change).
I tested the subroutine and compared with matlab and it was not the same. It is very similar to the algorithm used in Abramowitz's book. Here is the one I wrote that works well, just for comparing.
subroutine CElliptic(m,K,E)
implicit none
real*8 m,alpha,E,K,A,B,C,A_p,B_p,C_0,pi,suma
integer j,N
N=100
alpha=asin(sqrt(m))
pi = 4.d0*datan(1.d0)
A_p=1.0
B_p=cos(alpha)
C_0=sin(alpha)
suma=0.0
do j=1,N
A=(A_p+B_p)/2.0d0
B=dsqrt(A_p*B_p)
C=(A_p-B_p)/2.0d0
suma=suma+2**(j)*C**2
A_p=A
B_p=B
end do
K=pi/(2*A)
E=(1-1.d0/2.d0*(C_0**2+suma))*K
end Subroutine CElliptic
best regards
Ed.

Fortran: output large array - exceeded length, split on two lines

In a Fortran program, I need to write an array into a file with a specific format.
I perfectly works for smaller array (e.g. alen=10 in the example below), but won't work for bigger arrays: it then splits each line into two, as if a maximum number of characters per line was exceeded.
Example (very similar to the structure in my program):
PROGRAM output_probl
IMPLICIT NONE
INTEGER, PARAMETER :: alen=110
DOUBLE PRECISION, DIMENSION(alen)::a
INTEGER :: i,j
OPEN(20,file='output.dat')
30 format(I5,1x,110(e14.6e3,1x))
DO i=1,15
DO j=1,alen
a(j)=(i*j**2)*0.0123456789
ENDDO
write(20,30)i,(a(j),j=1,alen)
ENDDO
END PROGRAM output_probl
It compiles and runs properly (with Compaq Visual Fortran). Just the output file is wrong. If I for example change the field width per array item from 14 to 8, it'll work fine (this is of course not a satisfactory solution).
I thought about an unsuitable default maximum record length, but can't find how to change it (even with RECL which doesn't seem to work - if you think it should, a concrete example with RECL is welcome).
This might be basic, but I've been stuck with it for some time... Any help is welcome, thanks a lot!
Why not stream access? With sequential there is allways some processor dependent record length limit.
PROGRAM output_probl
IMPLICIT NONE
INTEGER, PARAMETER :: alen=110
DOUBLE PRECISION, DIMENSION(alen)::a
INTEGER :: i,j
OPEN(20,file='output.dat',access='stream', form='formatted',status='replace')
30 format(I5,1x,110(e14.6e3,1x))
DO i=1,15
DO j=1,alen
a(j)=(i*j**2)*0.0123456789
ENDDO
write(20,30)i,(a(j),j=1,alen)
ENDDO
END PROGRAM output_probl
As a note, I would use a character variable for the format string, or place it directly in the write statement, instead of the FORMAT statement with a label.
Fortran 95 version:
PROGRAM output_probl
IMPLICIT NONE
INTEGER, PARAMETER :: alen=110
DOUBLE PRECISION, DIMENSION(alen)::a
INTEGER :: i,j,rl
character(2000) :: ch
inquire(iolength=rl) ch
OPEN(20,file='output.dat',access='direct', form='unformatted',status='replace',recl=rl)
30 format(I5,1x,110(e14.6e3,1x))
DO i=1,15
DO j=1,alen
a(j)=(i*j**2)*0.0123456789
ENDDO
write(ch,30)i,(a(j),j=1,alen)
ch(2000:2000) = achar(10)
write(20,rec=i) ch
ENDDO
END PROGRAM output_probl
The program below should test. With Absoft compiler it works fine for n=10000, 10 character words, that is a line 100000 characters wide (plus a couple) in all. With G95 I get a message "Not enough storage is available to process this command" for n=5000 (n=4000 works).
character*10,dimension(:),allocatable:: test
integer,dimension(:),allocatable::itest
1 write(,)'Enter n > 0'
read , n
if(n.le.0) then
write(,)'requires value n > 0'
go to 1
endif
write(,*)'n=',n
allocate(test(n),itest(n))
write(test,'((i10))')(i,i=1,n)
write(*,*)test
open(10,file='test.txt')
write(10,*)test
write(*,*)'file test.txt written'
close(10)
open(11,file='test.txt')
read(11,*)itest
write(*,*)itest
end

Debugging DO loop / IF blocks in a Fortran 90 program (Beginner level)

I would be grateful if someone could help me out here. I'm just starting out learning how to program, so there is a great chance I'm missing something very obvious. I'm trying to write a program in Fortran 90 that solves question 4 i) on page 45 of this pdf. I have finally managed to get my code to compile to something, but now that something is somewhat rubbish, the data it produces is crazy (as time increases, I get a decrease in distance after whatever I input at t0). Can someone spot my mistake? I realize this is quite a lot of code to look through, I'm sorry for asking so much of you. Thanks in advance for looking through!
PROGRAM PARACHUTIST
! Tabulation of parachutist's descent z and and speed zdot
! as functions of time t
!Assign the program's associated constants
IMPLICIT NONE
REAL z, zdot, g, U1, U2, z0, u0, t0, q0, t, x,c,s
INTEGER I
g=9.8
U1=54
U2=5
!Break z0 down a little with q0
q0=COSH(g*t0/U1)
z0=U1**2/g*LOG(q0)
u0=U1*TANH(g*t0/U1)
!Prompt for and read in the free-fall time
Print*, 'Input free-fall time in seconds:'
Read*, t0
!Print the table headings
WRITE(*,1000)
1000 FORMAT (6X, 'TIME', 6X, 'DISTANCE', 6X, 'VELOCITY', /6X, '(SEC)', 7X, '(M)', 10X, '(M/SEC)',&
/6X, '0.0', 10X, '0.0', 10X, '0.0' )
!Loop covering the specified times
t=0
DO I=0,20
! Calculate the distance above ground
200 IF(t<=t0) THEN
x=g*t/U1
z=U1**2/g*LOG(COSH(x))
zdot=U1*TANH(x)
Elseif(t>t0) THEN
x=g*(t-t0)/U2
!store re-used expressions
c=cosh(x)
s=sinh(x)
z= z0 + (U2**2/g)*LOG(c+ (u0/U2)*s)
zdot=U2*(U2*s+u0*c)/(U2*c+u0*s)
Endif
!Print a line of table using T formats
WRITE(*,100) t, z, zdot
100 Format(4X, F5.2, 6X, F7.2, 6X, F7.2)
!Stop with message if landed
If(z.GE.500) THEN
PRINT*, 'LANDED'
STOP
!If we haven't yet landed then increment t as in
! problem specs
Elseif(t<15) then
t=t+1
Elseif(t.GE.15) then
t=t+10
ENDIF
!End of the t-loop
END DO
END PROGRAM PARACHUTIST
I wrote this as two comments, but it was really too lengthy. Go ahead and delete it all if you had planned to do so. I just browsed through a document comparing Fortran77 and "modern" Fortran90. (I coded in Fortran77 when I was just starting school, awhile ago...). Here are some suggestions:
Be careful with your use of "ELSEIF". It is usually okay for ELSE and IF to have the space omitted, but that is not true otherwise with free-format code (I think the only other instances of space-optional are DOUBLE PRECISION, ELSE IF, GO TO, END DO, and END IF).
An advantage of using Fortran90 is that you shouldn't even need ELSE IF's, (nor computed GOTO's!) as there is SELECT CASE.
You shouldn't need FORMAT either, as it can be incorporated directly with a format string in the READ or WRITE statement itself.
Yes, you can use either the old Fortran 77 operators .GE..GT..EQ..NE..LE..LT. or the new ones >= > == /= <= <. However, I'm not sure if you should mix them, which I noticed in your code.
EDIT: The second link above, about Control Structures, describes how you can use DO loops instead of IF's in Fortran90, sections 3.2 - 3.5. You can use named DO's, indefinite DO loops, DO WHILE's, all sorts of things! There are examples too. (The name of the entire document is Fortran90 for Fortran77 Programmers.)