Modern Fortran: Output format without label - fortran

I am looking for a way to specify the output format without using a label.
To understand what I mean, with label:
write(*,1001) icount, x, y
1001 format (i5,f5.2,e12.3)
Without label should be that I put format (i5,f5.2,e12.3) somewhere in the write statement, something like write(*,format(i5,f5.2,e12.3)) icount, x, y
I think I saw this somewhere recently but unfortunately I cannot find this again. If it exists it is a feature of a newer Fortran version. Maybe Fortran 90? maybe Fortran 2008?

Try
write(*,'(i5,f5.2,e12.3)') icount, x, y

As #Jeff Irwin suggests, one can save format in a character string and
pass it to write or print.
character(*), parameter :: fmt1 = "(i5,f5.2,e12.3)"
!! character(100) :: fmt1 = ... !! to use a non-constant string
write(*,fmt1) icount, x, y
write(*,fmt=fmt1) i2, x2, y2 !! fmt= may be attached for clarity
Since fmt1 is a usual character string, it can also be stored as module
variables, type components, or passed as subroutine arguments, for example.

Related

How to compile a Fortran subroutine

I have a small fortran subroutine that I got from here: How to call a Fortran program from R I saved it on C:\Fortran\horner.f90, however, when I try compile it using the command line I get an error saying that R is not recognized. See screenshots below. I do have Rtools installed and on my PATH. Am I missing something? Thanks for any pointers.
And this is the error I get:
Here is the horner.f90 file as well:
subroutine horner(n, a, x, y)
implicit none
integer :: n, i
double precision :: a(n), x, y
y = a(n)
do i = n - 1, 1, -1
y = y * x + a(i)
end do
end subroutine
UPDATE:
Here is a screenshot of where my R icon properties look like:
In the "Edit environment variable" window, it should be one entry per path, and not several paths separated by a ;.
This window was enhanced in Windows 10, in previous versions of Windows you had to type everything as one line, managing the ; separator yourself. But this no longer holds.
When you type, say C:\a;C:\b as one entry here, then in a command line the path will have ...;"C:\a;C:\b", that is it will consider "C:\a;C:\b" as one single string describing a path, which is of course wrong, and the Console won't find anything in these directories.

Compiling Legacy Fortran Code with (more) Modern Fortran Code

I have been working with some old legacy code in Fortran used by a colleague. The actual code is proprietary, so the examples I use here are abbreviated compared to the code I'm working with.
Some of the procedures individually defined in *.f files included a file called variables.h:
Example contents of variables.h:
c VARIABLE DIMENSIONS FOR MODEL
c height_dim -- number of vertical (z) steps
c length_dim -- number of horizontal (x) steps
c width_dim -- number of horizontal (y) steps
INTEGER height_dim, length_dim, width_dim, nmodes, styleFlag
PARAMETER (height_dim=80, length_dim=50, width_dim=40)
PARAMETER (nmodes = 4,
$ styleFlag = 3)
I changed that to the following:
! VARIABLE DIMENSIONS FOR MODEL
! height_dim -- number of vertical (z) steps
! length_dim -- number of horizontal (x) steps
! width_dim -- number of horizontal (y) steps
INTEGER height_dim, length_dim, width_dim, nmodes, styleFlag
PARAMETER (height_dim=80, length_dim=50, width_dim=40)
PARAMETER (nmodes = 4, styleFlag = 3)
An example routine that uses these might be the following, called initial_conditions.f:
c This sets up the PDE's initial conditions
subroutine initial_conditions( temperature, density )
IMPLICIT NONE
INCLUDE 'variables.h'
real*8 temperature(height_dim,length_dim,width_dim)
real*8 density(height_dim)
temperature = 273.15D0
density = 1.0D0
return
end
I tried to compile a test routine written in F90 (or newer?) that included dimensions.h, but the compiler didn't like the fixed-form comments being included into the free-form *.f90 source file, so I changed all comments from c to !. Then I was able to compile my test program successfully. Let's call it test.f90:
program test
implicit none
include 'variables.h'
real*8, dimension(height_dim,length_dim,width_dim) :: vx, vy, vz
! <<Initialize data...>>
! << Output data...>>
end program test
Unfortunately, now the original code doesn't compile. It seems that code doesn't like commented lines to begin with ! (based on the fact that that was all I changed), but the actual errors it gives are the following:
variables.h(8): error #5082: Syntax error, found END-OF-STATEMENT when expecting one of: =
PARAMETER (nmodes = 4, styleFlag = 3)
------------------------------------------^
variables.h(5): error #6219: This variable, used in a specification expression, must be a dummy argument, a COMMON block object, or an object accessible through host or use association. [NMODES]
INTEGER height_dim, length_dim, width_dim, nmodes, styleFlag
---------------------------------------------^
What in the world is going on, and how can it be fixed?!
I imagine that comment styles are incompatible (free-form fortran thinks c is a variable, not a comment?), but I have no idea how it would produce these errors.

Error in fortran, undefined reference to subroutine

I am writing a subroutine and main function to call it, but getting error as undefined reference to ___. I found one reason: When I save the main and subroutine in the same file, compile and run that file, everything runs perfectly. However, when I save them into different .f90 files and try to run the main file, I get error. Is there any way I can make subroutine into a separate file and call into main calling program?
I got confused with another place - in the main program at !------ERROR------ place. I referred to Automatic width integer descriptor in fortran 90 I can use I0 as automatic width display indicator. But when I used the same, there is run time error expected integer but got character. Any idea about this?
! saved as sub_program.f90 file
SUBROUTINE sub_program (v1,v2,ctr)
IMPLICIT NONE
INTEGER, INTENT(IN) :: ctr
INTEGER, INTENT (OUT) :: v1,v2
SELECT CASE (ctr)
CASE (1)
v1=1
v2=0
CASE (2)
v1=0
v2=1
END SELECT
RETURN
END SUBROUTINE
! main calling program, saved as caller.f90
PROGRAM caller
IMPLICIT NONE
INTEGER :: v1,v2,ctr
DO ctr = 1,2,1
CALL sub_program (v1,v2,ctr)
WRITE (*,100) 'STEP = ',ctr,'V1 = ',v1,'V2 = ',v2 !------ERROR------
100 FORMAT (I0)
END DO
END PROGRAM
Thanks!
What is your compile command? For me, this compiles and runs normally
gfortran caller.f90 foo.f90 && ./a.out
I0 is an integer indicator, but some items following your WRITE statement are character strings. You can try, for example,
100 FORMAT (3(A, I0, 1X))
where 1X refers to a space.
As a note, if formatting is not terribly important and you're only interested in seeing some quick results, you can use the free format output (WRITE(*,*) ...).
EDIT: I had incorrectly referred to FORMAT as obsolete.

Interpolate string length into fortran format specifier

I am trying to compile someone else's FORTRAN code using gfortran 4.4.6. The original coder used something like Compaq FORTRAN.
The code in question is supposed to read a filename like 'foo.txt' and create a new file called 'foo_c.txt':
file_label_end = SCAN(filename, '.') - 1
WRITE (output_filename,5) filename
5 FORMAT (A<file_label_end>, '_c.txt' )
gfortran complains about the opening angle bracket of the character specifier. That is, rather than "A3" he has "A<(a variable with value 3)>". I cannot find any information about interpolating format widths from variables... Is this possible? If not, what's the best way to fix this?
Update:
This seems to be working (compiling):
file_label_end = SCAN(par_filename, '.', .TRUE. ) + 1
output_filename = par_filename(1:file_label_end) // '_c.par'
but I later have a similar case:
12 FORMAT (<n-1>F10.5)
...
READ(1,12) (cat_parm (blk,j), j = 1,n-1)
which I attempted to fix by creating a format string:
write(fmt12,'(I0,A4)') n-1, 'F10.5'
!12 FORMAT (fmt12)
12 FORMAT (fmt=fmt12)
But the "t" in "fmt" gets flagged with an error about "Nonnegative width required in format string"
The use of <> in Fortran edit descriptors is non-standard, though widely implemented. You certainly can build edit descriptors from variables, the standard way is through an internal write, something like this (omitting declarations):
format_string = ''
write(format_string,'(a1,i0,a6)') 'A', file_label_end,'''_c.txt''' ! WARNING I haven't tested this
and then
write(output_filename,fmt=format_string) filename
But in your case this is not entirely necessary. You could try this instead
file_label_end = SCAN(filename, '.') - 1
WRITE (output_filename,'(a)') filename(1:file_label_end)//'_c.txt'
With the 'a' edit descriptor omitting a width means that all the characters in the expression will be written. You may want to declare
character(len=*), allocatable :: output_filename
if you haven't already done so.
The easiest is to use modern fortran
WRITE (output_filename,'(a)') trim(filename) // '_c.txt'
Otherwise I would write the number in the format string using a similar internal write.

Fortran - problem with double precision

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.