How to wrap the fortran write-statement - fortran

I want to wrap the fortran write-statement in a custom subroutine or function which includes some additional debug-logic.
But I'm currently stuck with defining the prototype of the function/subroutine.
Is this possible? If yes, how?

The title of your question exhibits a misunderstanding, though the text suggests you know better. Nevertheless, for the record, write is a Fortran statement, it is neither a subroutine nor a function.
I think you have a number of options. One, which I have used occasionally, would be to write a function which returns a string. Perhaps
function error_message(error)
character(len=*), intent(in) :: error
character(len=:), allocatable :: error_message
error_message = 'ERROR: '//trim(error)
end function error_message
which you can then use like this
write(*,*) error_message('Oh s**t')
You could certainly write a subroutine or a function with side effects which include writing to an ouput channel, but if you adopt this approach you have to be careful to observe the rules for recursive i/o.
EDIT
after OP's comment.
If you want to switch off debug messages another option you have is to direct them to a null device or file, eg /dev/null on Linux or NUL on Windows. Something like
integer, parameter :: debug_channel = 99
logical, parameter :: debugging = .false.
...
if (debugging) then
open(debug_channel, file='NUL')
else
open(debug_channel, file='debuglog'
end if
and then
write(debug_channel,*) 'message'

a relatively simple way to accomplish most of what you want is to simply put the if inline in front of every write that is subject to debug control:
if(debug)write(..,..)..
where debug is a global logical value, or even:
if(debugf(level))write(..,..)..
where the logical function debugf determines whether to write based on some argument.

In addition to the other answers, you may be able to avoid using if (debug) write... with derived type IO.
I say "may", as it is quite silly unless you already have a suitable structure, and compiler support is currently rare.
However, as an example, compiled with ifort 14.0.1:
module errormod
type error_t
character(len=:), allocatable :: message
contains
procedure write_error
generic :: write(formatted) => write_error
end type error_t
logical debug_verbose
contains
subroutine write_error(err, unit, iotype, v_list, iostat, iomsg)
class(error_t), intent(in) :: err
integer, intent(in) :: unit
character(len=*), intent(in) :: iotype
integer, intent(in), dimension(:) :: v_list
integer, intent(out) :: iostat
character(len=*), intent(inout) :: iomsg
if (debug_verbose) then
write(unit, '("Error: ", A)', iostat=iostat, iomsg=iomsg) err%message
else
write(unit, '()', advance='no')
end if
end subroutine write_error
end module errormod
program test
use errormod
implicit none
type(error_t) error
debug_verbose = .TRUE.
error%message = "This error will be reported."
write(*, '(dt)') error
debug_verbose = .FALSE.
error%message = "This error will not be reported."
write(*, '(dt)') error
debug_verbose = .TRUE.
error%message = "This final error will also be reported."
write(*, '(dt)') error
end program test
The first and third messages will appear, but not the second.

Related

Using an optional parameter with value attribute when it has not been passed [duplicate]

This question already has answers here:
Segmentation fault with optional arguments in Fortran functions
(1 answer)
Fortran 2003 / 2008: Elegant default arguments?
(7 answers)
Closed 4 years ago.
I want to use a function's parameter with attribute "value". It is an optional parameter and I want to use it when it has not been passed. I expect that there is a local variable associated with the optional parameter, because the optional paramter is passed by value and not by reference. But strange things occurr when I try to do it.
In the example below, the string "Hello" should be printed, but the contents of the character parameter "Str" seems to be empty.
module test_value_mod
implicit none
contains
subroutine printStr(Str, AddTip)
! Declaration of parameters
character(len=*), intent(in) :: Str
logical, optional, value :: AddTip
if (.not. present(AddTip)) AddTip = .False.
write (*,*) 'Str: ', Str
if (AddTip) write (*,*) 'Tip is printed.'
end subroutine printStr
end module test_value_mod
program test_value_prog
use test_value_mod
implicit none
call printStr('Hello')
end program test_value_prog
However, when I pass the charaacter length as another parameter, there is no problem:
module test_value_mod
implicit none
contains
subroutine printStr(Str, LenStr, AddTip)
! Declaration of parameters
integer, intent(in) :: LenStr
character(len=LenStr), intent(in) :: Str
logical, optional, value :: AddTip
if (.not. present(AddTip)) AddTip = .False.
write (*,*) 'Str: ', Str
if (AddTip) write (*,*) 'Tip is printed.'
end subroutine printStr
end module test_value_mod
program test_value_prog
use test_value_mod
implicit none
call printStr('Hello', len_trim('Hello'))
end program test_value_prog
I use gfortran 7.3.0. But if I compile with ifort 17.0.4, I get the following error for the two examples given above:
forrtl: severe (174): SIGSEGV, segmentation fault occurred
Image PC Routine Line Source
test_value_passin 0000000000402C64 Unknown Unknown Unknown
libpthread-2.17.s 00007EFDA90825E0 Unknown Unknown Unknown
test_value_passin 000000000040288D test_value_mod_mp 15 test_value_passing.f08
test_value_passin 0000000000402A08 MAIN__ 29 test_value_passing.f08
test_value_passin 00000000004027CE Unknown Unknown Unknown
libc-2.17.so 00007EFDA8CD1C05 __libc_start_main Unknown Unknown
test_value_passin 00000000004026E9 Unknown Unknown Unknown
Line 15 in "test_value_mod_mp" is:
if (.not. present(AddTip)) AddTip = .False.
So, I think that the use of an optional paramter pased by value when it has not been actually provided, is invalid. But, however, gcc permits it, failing only if you provide another input paramter of type character of assumed length.
Could anybody clarify?

Write in a new line on every write(33,*) comand in Fortran77

I create a fortran code to calculate the temperature of a cfd model. This code will be called on every iteration of a steady state simulation and calculate the temperature. On every calling of my code/iteration i want my fortran code also save the temperature field on a txt file and save. After calculating the temperature field and saving the values in TFIELD(100;1:6) the part with saving in txt file looks like:
OPEN(UNIT=35,FILE='W:\temperaturField.txt',
&FORM ='FORMATTED',STATUS='UNKNOWN',
&ACTION='READWRITE')
WRITE (35,FMT='5(F8.3,2X))') TFIELD(100,1:6)
With this code it only overwrites the first line of my txt file on every iteration. But i want to paste every TFIELD(100,1:6) array on a new line. How can i do this?
Add POSTITION='APPEND' to the OPEN:
OPEN(UNIT=35,FILE='W:\temperaturField.txt',
&FORM ='FORMATTED',STATUS='UNKNOWN',POSITION='APPEND',
&ACTION='READWRITE')
It seems like you are opening and closing the file for each iteration. This is a quick and dirty method if you need to debug, but it's slow.
If you want to do that, you might want to do what #Jack said: Include a POSITION='APPEND' to the OPEN statement to set the position to write the data to the end of the file. Also, you need to make sure that you close it every time.
A better (more efficient) method would be to keep the file open for the whole time. I'd do that with a module:
module temp_writer_module
implicit none
integer :: temp_writer_unit
logical :: is_opened = .FALSE.
private :: temp_writer_unit, is_opened
contains
subroutine temp_writer_open()
integer :: ios
character(len=100) :: iomsg
if (is_opened) then
print*, "Warning: Temperature file already openend"
return
end if
open(newunit=temp_writer_unit, file='W:\temperatureField', &
form='FORMATTED', status='UNKNOWN', action='WRITE', &
iostat=ios, iomsg=iomsg)
if (ios /= 0) then
print*, "Error opening temperature file:"
print*, trim(iomsg)
STOP
end if
is_opened = .TRUE.
end subroutine temp_writer_open
subroutine temp_writer_close()
if (.not. is_opened) return
close(temp_writer_unit)
is_opened = .FALSE.
end subroutine temp_writer_close
subroutine temp_writer(temps)
real, intent(in) :: temps(6)
integer :: ios
character(len=100) :: iomsg
if (.not. is_opened) call temp_writer_open()
write(temp_writer_unit, *, iostat=ios, iomsg=iomsg) temps
if (ios /= 0) then
print*, "Error writing to temperature file:"
print*, trim(iomsg)
end if
end subroutine temp_writer
end module temp_writer_module
Then you can use it in your program like this:
subroutine calc_temps(...)
use temp_writer_module
<variable declarations>
<calculations>
call temp_writer(tfield(100, 1:6))
end subroutine calc_temps
Just don't forget to call the temp_writer_close routine before your program ends.

Saved variable in Module changes value when recalled in subroutine

I am working with a FEM program written in Fortran language. It is a mechanical analysis that involves multiple set of material. The important variables are saved into modules that have the following form
MODULE element
implicit none
save
TYPE proel_type
INTEGER:: prop1
INTEGER:: prop2
REAL:: prop3
REAL:: prop4
...
INTEGER, ALLOCATABLE :: list1(:)
REAL, ALLOCATABLE :: array1(:)
...
INTEGER:: LTYPE
...
END TYPE proel_type
TYPE(proel_type), ALLOCATABLE:: proel(:)
INTEGER:: prop1
INTEGER:: prop2
REAL:: prop3
REAL:: prop4
...
INTEGER:: LTYPE
INTEGER:: ISETS
...
END MODULE element
As you can see, proel is an allocatable data structure that contains some information about the properties of the material, the element and so on. This data is taken from a text file through a I/O routine like
SUBROUTINE setdata
USE element
IMPLICIT NONE
ALLOCATE(PROEL(NSETS))
DO ISETS=1,NSETS
... ! here I am loading succefully the string variable STRVAR
! from the text file with data
LTYPE=GETVAL(STRVAR) ! here I retreive the value of LTYPE
PROEL(ISETS)%LTYPE=LTYPE ! here I save the (integer) variable for the set
IF(LTYPE.NE.0)THEN
CALL SETELM
END IF
END DO
END SUBROUTINE
Inside subroutine SETELM I am setting some particular parameters used in the program, but at some point I recall LTYPE saved in PROEL
SUBROUTINE SETELM
USE element
IMPLICIT NONE
LTYPE=PROEL(ISETS)%LTYPE
SELECT CASE(LTYPE)
CASE( 1)
...
CASE( 2)
...
CASE DEFAULT
...
END SELECT
END SUBROUTINE
Now, between the assignment LTYPE=PROEL(ISETS)%LTYPE and the SELECT CASE(LTYPE), the variable LTYPE changes value without any reason. The value is random (in the sense that I do not know where it comes from but it is not like uninitialized vars) and there are no others operations in the middle that work with LTYPE.
I am working with VS2008 and Fortran, so I turned on every warning, but neither compilation nor runtime give some possible hint.
Do you have any idea why is that happening?

Error: Expecting END PROGRAM statement in fortran

The code is here:
interface
subroutine csrcoo (nrow,job,nzmax,a,ja,ia,nnz,ao,ir,jc,ierr) bind(c,name="CSRCOO")
use iso_c_binding
implicit none
integer(c_int), value :: nrow,job,nzmax,nnz
real(c_double) :: a(*),ao(*)
integer(c_int) :: ir(*),jc(*),ja(*),ia(nrow+1),ierr
end subroutine csrcoo
end interface
subroutine csrcoo (nrow,job,nzmax,a,ja,ia,nnz,ao,ir,jc,ierr)
integer :: nrow,job,nzmax,nnz
real*8 :: a(*),ao(*)
integer :: ir(*),jc(*),ja(*),ia(nrow+1),ierr
integer :: i,k,k1,k2
ierr = 0
nnz = ia(nrow+1)-1
if (nnz > nzmax) then
ierr = 1
return
endif
goto (3,2,1) job
1 do 10 k=1,nnz
ao(k) = a(k)
10 END DO
2 do 11 k=1,nnz
jc(k) = ja(k)
11 END DO
3 do 13 i=nrow,1,-1
k1 = ia(i+1)-1
k2 = ia(i)
do 12 k=k1,k2,-1
ir(k) = i
12 END DO
13 END DO
return
end subroutine csrcoo
When I use the gfortran to compile this code like this: gfortran -O2 -c test.f90
It always shows this error:
test.f90:11:
subroutine csrcoo (nrow,job,nzmax,a,ja,ia,nnz,ao,ir,jc,ierr)
1
Error: Unclassifiable statement at (1)
test.f90:41.3:
end subroutine csrcoo
1
Error: Expecting END PROGRAM statement at (1)
Error: Unexpected end of file in 'test.f90'
Can anyone tell me what wrong with that code and give me some advice?
The problem is, that a standalone interface doesn't make any sense, because its name has to be accessible inside the part of the program, where you want to use it. Therefore, it has to be defined inside a module or a program.
I think, the error stems from the fact, that the program statement is not necessary and your interface, therefor, implicitely defines a program, which should be ended with an end statement, which is obviously missing.
You can solve your problem by wrapping your interface in a module and leaving the subroutine out of the module (otherwise, your subroutine would already have a module interface).
module [name]
[your interface]
end module
[your subroutine]
EDIT (Suggestion form #HighPerformanceMark): The better solution for the code you showed us would be to put the subroutine itself into the module and delete the interface. In this case, the interface for the subroutine is created automatically and you don't have to care for it.
module [name]
use iso_c_binding
implicit none
subroutine csrcoo (nrow,job,nzmax,a,ja,ia,nnz,ao,ir,jc,ierr) bind(c,name="CSRCOO")
[the content of your subroutine]
end subroutine
end module

unpacking data using a fortran code

i'm a grad student trying to work with atmospheric data files provided by NOAA. I have a code that reads the data but only writes the first four columns of data. I know that the subroutine at the end of the code unpacks the data. I was thinking maybe if I take out the conditional statements then it might write the entire data. But that doesn't work. I need to develop this code so that it writes the entire data file and not just the first four elements. Any help is appreciated. The code is shown below:
PROGRAM CHK_DATA
!-------------------------------------------------------------------------------
! Simple program to dump the first few elements of the data array for each
! record of an ARL packed meteorological file. Used for diagnostic testing.
! Created: 23 Nov 1999 (RRD)
! 14 Dec 2000 (RRD) - fortran90 upgrade
! 18 Oct 2001 (RRD) - expanded grid domain
! 03 Jun 2008 (RRD) - embedded blanks
!-------------------------------------------------------------------------------
REAL, ALLOCATABLE :: RDATA(:,:)
CHARACTER(1), ALLOCATABLE :: CPACK(:)
CHARACTER(4) :: KVAR, MODEL
CHARACTER(50) :: LABEL
CHARACTER(80) :: FDIR, FILE
CHARACTER(3072) :: HEADER
LOGICAL :: FTEST
!-------------------------------------------------------------------------------
INTERFACE
SUBROUTINE UNPACK(CPACK,RDATA,NX,NY,NEXP,VAR1)
CHARACTER(1),INTENT(IN) :: CPACK(:)
REAL, INTENT(OUT) :: RDATA(:,:)
INTEGER, INTENT(IN) :: NX,NY,NEXP
REAL, INTENT(IN) :: VAR1
END SUBROUTINE
END INTERFACE
!-------------------------------------------------------------------------------
! directory and file name
WRITE(*,*)'Enter directory name:'
READ(*,'(a)')FDIR
FDIR=ADJUSTL(FDIR)
WRITE(*,*)'Enter file name:'
READ(*,'(a)')FILE
FILE=ADJUSTL(FILE)
! test for meteo file existence
KLEN=LEN_TRIM(FDIR)
INQUIRE(FILE=FDIR(1:KLEN)//FILE,EXIST=FTEST)
IF(.NOT.FTEST)THEN
WRITE(*,*)'Unable to find file: ',FILE
WRITE(*,*)'On local directory : ',FDIR(1:KLEN)
STOP
END IF
! open file to decode the standard label (50) plus the
! fixed portion (108) of the extended header
OPEN(10,FILE=FDIR(1:KLEN)//FILE,RECL=158,ACCESS='DIRECT',FORM='UNFORMATTED')
! decode the standard portion of the index record
READ(10,REC=1)LABEL,HEADER(1:108)
READ(LABEL,'(5I2,4X,A4)')IYR,IMO,IDA,IHR,IFC,KVAR
WRITE(*,'(A,4I5)')'Opened file : ',IYR,IMO,IDA,IHR
IF(KVAR.NE.'INDX')THEN
WRITE(*,*)'WARNING Old format meteo data grid'
WRITE(*,*)LABEL
WRITE(*,*)HEADER(1:108)
STOP
END IF
! decode extended portion of the header
READ(HEADER(1:108),'(A4,I3,I2,12F7.0,3I3,I2,I4)',ERR=900) &
MODEL, ICX, MN, &
POLE_LAT, POLE_LON, REF_LAT, &
REF_LON, SIZE, ORIENT, &
TANG_LAT, SYNC_XP, SYNC_YP, &
SYNC_LAT, SYNC_LON, DUMMY, &
NX, NY, NZ, &
K_FLAG, LENH
! close file and reopen with proper length
CLOSE (10)
NXY = NX*NY
LEN = NXY+50
OPEN(10,FILE=FDIR(1:KLEN)//FILE,RECL=LEN,ACCESS='DIRECT',FORM='UNFORMATTED')
! print file diagnostic
WRITE(*,'(A,4I5)')'Grid size and lrec: ',NX,NY,NXY,LEN
WRITE(*,'(A,I5)') 'Header record size: ',LENH
! allocate array space
ALLOCATE (RDATA(NX,NY), STAT=KRET)
ALLOCATE (CPACK(NXY), STAT=KRET)
! read entire file and print headers
KREC=1
100 READ(10,REC=KREC,ERR=800)LABEL,(CPACK(K),K=1,NXY)
READ(LABEL,'(6I2,2X,A4,I4,2E14.7)',ERR=900) IY,IM,ID,IH,IF,KL, &
KVAR,NEXP,PREC,VAR1
WRITE(*,'(A)')LABEL
IF(KVAR.NE.'INDX') CALL UNPACK(CPACK,RDATA,NX,NY,NEXP,VAR1)
READ(*,*,END=800)
KREC=KREC+1
GO TO 100
800 STOP
900 WRITE(*,*)'ERROR: decoding header'
WRITE(*,*)LABEL
WRITE(*,*)HEADER(1:108)
END PROGRAM chk_data
!-------------------------------------------------------------------------------
SUBROUTINE UNPACK(CPACK,RDATA,NX,NY,NEXP,VAR1)
CHARACTER(1),INTENT(IN) :: CPACK(:)
REAL, INTENT(OUT) :: RDATA(:,:)
INTEGER, INTENT(IN) :: NX,NY,NEXP
REAL, INTENT(IN) :: VAR1
! only required when dealing with F95 compilers
! replace ICHAR below with internally defined JCHAR function
! CHARACTER MYCHR*1
! JCHAR(MYCHR)=IAND(ICHAR(MYCHR),255)
SCALE=2.0**(7-NEXP)
VOLD=VAR1
INDX=0
DO J=1,NY
DO I=1,NX
INDX=INDX+1
RDATA(I,J)=(ICHAR(CPACK(INDX))-127.)/SCALE+VOLD
VOLD=RDATA(I,J)
IF(I.LE.2.AND.J.LE.2) &
WRITE(*,'(3I5,E12.4)')J,I,ICHAR(CPACK(INDX)),RDATA(I,J)
IF(I.GE.(NX-1).AND.J.GE.(NY-1)) &
WRITE(*,'(3I5,E12.4)')J,I,ICHAR(CPACK(INDX)),RDATA(I,J)
END DO
VOLD=RDATA(1,J)
END DO
END SUBROUTINE unpack
Did you notice the comment
! only required when dealing with F95 compilers
! replace ICHAR below with internally defined JCHAR function
! CHARACTER MYCHR*1
! JCHAR(MYCHR)=IAND(ICHAR(MYCHR),255)
Try to uncomment the definition, and change the calls to CHAR to calls to JCHAR.
ICHAR is an intrinsic function in Fortran that behaves different, than the code expects. It all seems suspicious, because the JCHAR does nothing to numbers below 256.
For further porting consider using IMPLICIT NONE and modules.
---EDIT ---
Now I see your conditionals. If you remove
IF(I.LE.2.AND.J.LE.2) &
and
IF(I.GE.(NX-1).AND.J.GE.(NY-1)) &
what exactly does the code do? We do not have the data.
(I know, there are a lot of the data files on the page you downloaded your program, but I am not going to the all the work for you, sorry.)
Cant you find a complete code to work with the data anywhere?