Silverfrost Forums

Welcome to our forums

Fortran 95 code problem

17 Jul 2013 11:58 #12645

Your definition of qp is not accepted by the compiler because the numbers are out of range - try printing qp and you should find that it is likely to be a negative number, indicating an error (see the FTN95 help pages on SELECTED_REAL_KIND).

The other error messages are either a result of the invalid value for qp, or a result of the fact that the structure of the program seems to break down about 1/2 of the way through the first posting - there's an END statement at line 70 that appears to be unmatched. At line 52 - is this meant to be the start of a new module or of the PROGRAM? What is the structure after line 70?

17 Jul 2013 1:15 #12646

Quoted from simon Your definition of qp is not accepted by the compiler because the numbers are out of range - try printing qp and you should find that it is likely to be a negative number, indicating an error (see the FTN95 help pages on SELECTED_REAL_KIND).

The other error messages are either a result of the invalid value for qp, or a result of the fact that the structure of the program seems to break down about 1/2 of the way through the first posting - there's an END statement at line 70 that appears to be unmatched. At line 52 - is this meant to be the start of a new module or of the PROGRAM? What is the structure after line 70?

The structure from line 70 on is defining the variables and inputs that will be read in from the input parameter file, and I guess dimensioning arrays?

The END statement was referenced in John's code, so I just transferred that over, can it be deleted?

The code is public domain Fortran and is a geophysical program that is used to compute the full Bouguer anomaly (including local terrain correction). The reference if you need to check is:

Fullea et al 2008. FA2BOUG -A Fortran 90 code to compute Bouguer gravity anomalies from gridded free-air anomalies: Application to the Atlantic-Mediterranean transition zone. Computers and Geosciences, v34, 1665-1681.

I have tried to compile the code using various compilers, FTN95 and gFortran, and each gives a different response in terms of the errors. GFortran for example, takes exception to the ICHAR statements, whereas FTN95 does not. I am not an expert in Fortran, and why I am trying to see if the code can be compiled, clearly it must be possible because other people in papers published over the last few years have used it. Tried writing to the author, but to date have not had a response.

If needed, I can e-mail both the paper and the original code as published.

Cheers

Lester

18 Jul 2013 12:52 #12648

Lester,

You should not have placed the code I wrote before your declarations. There are only a few changes,

  1. remove my PI code
  2. include USE MOD_Precision in routine up_INT(n)
  3. change references to file units 1,2 & 5 to 21,22 & 25, as units 1:8 can be reserved file unit numbers with some compilers.

I see no problem with the definition of sp, dp or qp you now use ?

I copied your code then did my updates and used FC to identify all changes. The following is this list. Comparing files arctica_ver0.f90 and ARCTICA_VER1.F90 ***** arctica_ver0.f90 58: USE MOD_PARAM 59: ! 60: pi = 4.0_dp * ATAN (1.0_dp) 61: piq = 4.0_qp * ATAN (1.0_qp) 62: pis = 4.0_sp * ATAN (1.0_sp) 63: write (,) pis, kind(pis), precision(pis) 64: write (,) pi, kind(pi), precision(pi) 65: write (,) piq, kind(piq), precision(piq) 66: write (,'(f27.19)') pis 67: write (,'(f27.19)') pi 68: write (*,'(f27.19)') piq 69: ! 70: END 71:
72: REAL(dp) del_xd,del_xi,cx,cy,del_xBg,R_d,R_i,del_xdet,del_x_pr ***** ARCTICA_VER1.F90 58: USE MOD_PARAM 59: ! 60: REAL(dp) del_xd,del_xi,cx,cy,del_xBg,R_d,R_i,del_xdet,del_x_pr *****

***** arctica_ver0.f90
   90:  !
   91:  
   92:  !Last edited by arctica on Tue Jul 16, 2013 10:37 pm; edited 1 time in total 
***** ARCTICA_VER1.F90
   78:  !
   79:        call write_pi
   80:  !
   81:  !Last edited by arctica on Tue Jul 16, 2013 10:37 pm; edited 1 time in total 
*****

***** arctica_ver0.f90
  119:  
  120:  
  121:        OPEN(1, file='parameters.dat', status='OLD') 
  122:        READ(1,'(I1)',ADVANCE='NO') det 
  123:        IF (det==1) THEN 
  124:         READ(1,*)del_xd,del_xi,del_xBg,rho_c,rho_w,N,M,R_d,R_i,land, & 
  125:                 slab_Boug,N_det,M_det,del_xdet 
***** ARCTICA_VER1.F90
  108:  
  109:        OPEN(21, file='parameters.dat', status='OLD') 
  110:        READ(21,'(I1)',ADVANCE='NO') det 
  111:        IF (det==1) THEN 
  112:         READ(21,*)del_xd,del_xi,del_xBg,rho_c,rho_w,N,M,R_d,R_i,land, & 
  113:                 slab_Boug,N_det,M_det,del_xdet 
*****

***** arctica_ver0.f90
  128:        ELSE 
  129:         READ(1,*)del_xd,del_xi,del_xBg,rho_c,rho_w,N,M,R_d,R_i,land, & 
  130:                 slab_Boug 
***** ARCTICA_VER1.F90
  116:        ELSE 
  117:         READ(21,*)del_xd,del_xi,del_xBg,rho_c,rho_w,N,M,R_d,R_i,land, & 
  118:                 slab_Boug 
*****

***** arctica_ver0.f90
  131:        ENDIF 
  132:        CLOSE(1) 
  133:  !Open INPUT files 
  134:        open(2,file='topo_cart.xyz', status='OLD') 
  135:        open(5,file='gravi_cart.xyz',status='OLD') 
  136:  !Open detailed topography, if necessary 
  137:        IF (det==1) open(7,file='topo_cart_det.xyz', status='OLD') 
  138:  !Open OUTPUT files 
***** ARCTICA_VER1.F90
  119:        ENDIF 
  120:        CLOSE(21) 
  121:  !Open INPUT files 
  122:        open(22,file='topo_cart.xyz', status='OLD') 
  123:        open(25,file='gravi_cart.xyz',status='OLD') 
  124:  !Open detailed topography, if necessary 
  125:        IF (det==1) open(27,file='topo_cart_det.xyz', status='OLD') 
  126:  !Open OUTPUT files 
*****
18 Jul 2013 12:54 (Edited: 18 Jul 2013 1:17) #12649

the rest of the comparison :

***** arctica_ver0.f90
  178:         DO i=1,N 
  179:          read(2,*) E(i,j) 
  180:          read(5,*) FA_strg(i,j) 
  181:         ENDDO 
***** ARCTICA_VER1.F90
  166:         DO i=1,N 
  167:          read(22,*) E(i,j) 
  168:          read(25,*) FA_strg(i,j) 
  169:         ENDDO 
*****

***** arctica_ver0.f90
  182:        ENDDO 
  183:        REWIND (2) 
  184:  !Read the detailed topography file (if present) 
***** ARCTICA_VER1.F90
  170:        ENDDO 
  171:        REWIND (22) 
  172:  !Read the detailed topography file (if present) 
*****

***** arctica_ver0.f90
  190:          DO i=1,N_det 
  191:           read(7,*) E_det_strg(i,j) 
  192:           n_strg=ICHAR(E_det_strg(i,j)) 
***** ARCTICA_VER1.F90
  178:          DO i=1,N_det 
  179:           read(27,*) E_det_strg(i,j) 
  180:           n_strg=ICHAR(E_det_strg(i,j)) 
*****

***** arctica_ver0.f90
  472:  
  473:        OPEN(1, file='lat_ex.dat', status='UNKNOWN') 
  474:        WRITE(1,*) L_x,L_y,rtio*del_xi 
  475:        CLOSE(1) 
  476:        write(*,*)'*****************************' 
***** ARCTICA_VER1.F90
  460:  
  461:        OPEN(21, file='lat_ex.dat', status='UNKNOWN') 
  462:        WRITE(21,*) L_x,L_y,rtio*del_xi 
  463:        CLOSE(21) 
  464:        write(*,*)'*****************************' 
*****

***** arctica_ver0.f90
  488:        INTEGER FUNCTION up_INT(n) 
  489:        REAL(dp) :: n 
***** ARCTICA_VER1.F90
  476:        INTEGER FUNCTION up_INT(n) 
  477:        USE MOD_Precision 
  478:        REAL(dp) :: n 
*****

***** arctica_ver0.f90
***** ARCTICA_VER1.F90
  612:  
  613:        subroutine write_pi
  614:  !  example of kind options
  615:        USE MOD_Precision 
  616:        real(sp) :: pis
  617:        real(dp) :: pi
  618:        real(qp) :: piq
  619:  !
  620:        pi  = 4.0_dp * ATAN (1.0_dp) 
  621:        piq = 4.0_qp * ATAN (1.0_qp) 
  622:        pis = 4.0_sp * ATAN (1.0_sp) 
  623:        write (*,*) pis, kind(pis), sp, precision(pis) 
  624:        write (*,*) pi,  kind(pi),  dp, precision(pi) 
  625:        write (*,*) piq, kind(piq), qp, precision(piq) 
  626:        write (*,'(f27.19)') pis 
  627:        write (*,'(f27.19)') pi 
  628:        write (*,'(f27.19)') piq 
  629:  ! 
  630:        END 
*****

This size limitation, or no capability of including, even small, attachments is annoying for us all.

David's comment about changing: REAL(dp) AB to REAL(dp) :: AB is probably a change you could make generally through the code, as it is the standard.

ICHAR should receive a single character, not a character string. FTN95 allows this as an extension and returns the value of the first character only.

John

18 Jul 2013 1:04 #12650

You might be interested, I once wrote the following program to test the valid values for defining KIND

module kind_precision
   integer, parameter :: int_1byte   = selected_int_kind (2)         ! 1
   integer, parameter :: int_2byte   = selected_int_kind (4)         ! 2
   integer, parameter :: int_4byte   = selected_int_kind (9)         ! 3
   integer, parameter :: int_8byte   = selected_int_kind (18)        ! 4
   integer, parameter :: real_float  = selected_real_kind (6,37)     ! 1
   integer, parameter :: real_double = selected_real_kind (15,307)   ! 2
   integer, parameter :: real_long   = selected_real_kind (18,4931)  ! 3
end module kind_precision

use kind_precision
!
   integer ( kind=int_4byte ) :: i,k, last_k
   character kind_name*10
   real ( kind=real_double ) :: x
   real ( kind=real_long )   :: y
!
!  confirm integer precision
      write (*,*) ' '
      write (*,*) 'Test of integer KIND'
      last_k = -99
      do i = 30,0,-1
         k = selected_int_kind (i)
         if (k == last_k) cycle
         kind_name = ' undefined'
         if ( k == int_1byte ) kind_name = '1 byte'
         if ( k == int_2byte ) kind_name = '2 byte'
         if ( k == int_4byte ) kind_name = '4 byte'
         if ( k == int_8byte ) kind_name = '8 byte'
         write (*,*) kind_name, ' precision =', i, ' kind =',k
         last_k = k
      end do
!
!  confirm real precision
      write (*,*) ' '
      write (*,*) 'Test of Real precision KIND'
      last_k = -99
      do i = 30,0,-1
         k = selected_real_kind (i,1)
         if (k == last_k) cycle
         kind_name = ' undefined'
         if ( k == real_float  ) kind_name = '4 byte'
         if ( k == real_double ) kind_name = '8 byte'
         if ( k == real_long   ) kind_name = '10 byte'
         write (*,*) kind_name, ' precision =', i, ' kind =',k
         last_k = k
      end do
!
!  confirm real exponent
      write (*,*) ' '
      write (*,*) 'Test of Real exponent KIND'
      last_k = -99
      do i = 5000,0,-1
         k = selected_real_kind (1,i)
         if (k == last_k) cycle
         kind_name = ' undefined'
         if ( k == real_float  ) kind_name = '4 byte'
         if ( k == real_double ) kind_name = '8 byte'
         if ( k == real_long   ) kind_name = '10 byte'
         write (*,*) kind_name, ' exponent =', i, ' kind =',k
         last_k = k
      end do
!
!   test real constants
!
      x = 1.0e300_real_double   ; write (*,*) 'x=',x
      y = 1.0e3000_real_long    ; write (*,*) 'y=',y
      x = 1.0d300               ; write (*,*) 'x=',x
!      x = 1.0e300               ; write (*,*) 'x=',x
!      y = 1.0e3000              ; write (*,*) 'y=',y
!
      end

There are so many ways of defining precision, which I think is totally over the top. As I have said, my preference is REAL*8 as it clearly states what is required and you don't have to go searching for what dp is or how it was defined.

John

18 Jul 2013 12:27 #12653

Thanks for the pointers John, will try the code edits out later.

With the MODULE statement, is it optional to leave off the module name from the END MODULE statement, i.e. is FTN95 flexible on that? Only MOD_Precision has the name at both ends, others do not.

Cheers

Lester

18 Jul 2013 12:41 #12654

Lester,

The module name is optional for the END MODULE statement, but I would recommend its use. I find the program source is easier to navigate where end statements are named, but that is my preference.

John

18 Jul 2013 5:56 #12657

Hi John,

I added you changes and the errors are greatly reduced, just warnings (especially with pi).

598 SUBROUTINE write_pi 599 ! Example of kind options 600 USE MOD_Precision 601 real(sp) :: pis 602 real(dp) :: pi 603 real(qp) :: piq ************* 604 605 pi = 4.0_dp * ATAN (1.0_dp) 606 piq = 4.0_qp * ATAN (1.0_qp) ************ 607 pis = 4.0_sp * ATAN (1.0_sp) 608 write (,) pis, kind(pis), precision(pis) 609 write (,) pi, kind(pi), precision(pi) 610 write (,) piq, kind(piq), precision(piq) 611 write (,'(f27.19)') pis 612 write (,'(f27.19)') pi 613 write (*,'(f27.19)') piq 614! 615 END

Compiling and linking file: FA2Boug_CGv1D.f95 C:\Temp\Fullea\FA2Boug_CGv1D.F95(63) : warning 197 - Variable X has been declared but not used C:\Temp\Fullea\FA2Boug_CGv1D.F95(63) : warning 197 - Variable Y has been declared but not used C:\Temp\Fullea\FA2Boug_CGv1D.F95(63) : warning 197 - Variable Z has been declared but not used C:\Temp\Fullea\FA2Boug_CGv1D.F95(63) : warning 197 - Variable DEL_G has been declared but not used C:\Temp\Fullea\FA2Boug_CGv1D.F95(66) : warning 197 - Variable DM has been declared but not used C:\Temp\Fullea\FA2Boug_CGv1D.F95(68) : warning 197 - Variable UP_INT has been declared but not used C:\Temp\Fullea\FA2Boug_CGv1D.F95(72) : warning 197 - Variable COORD has been declared but not used C:\Temp\Fullea\FA2Boug_CGv1D.F95(76) : warning 197 - Variable D_RHO_MHO has been declared but not used C:\Temp\Fullea\FA2Boug_CGv1D.F95(76) : warning 197 - Variable T has been declared but not used C:\Temp\Fullea\FA2Boug_CGv1D.F95(76) : warning 197 - Variable ISOS has been declared but not used C:\Temp\Fullea\FA2Boug_CGv1D.F95(76) : warning 197 - Variable Z_CREF has been declared but not used C:\Temp\Fullea\FA2Boug_CGv1D.F95(190) : warning 242 - Variable R_TL1 has been given a value but never used C:\Temp\Fullea\FA2Boug_CGv1D.F95(191) : warning 242 - Variable R_TL2 has been given a value but never used C:\Temp\Fullea\FA2Boug_CGv1D.F95(267) : warning 298 - Variable PI has been used without being given an initial value ************* C:\Temp\Fullea\FA2Boug_CGv1D.F95(237) : warning 179 - Comparing floating point quantities for equality may give misleading results C:\Temp\Fullea\FA2Boug_CGv1D.F95(349) : warning 179 - Comparing floating point quantities for equality may give misleading results C:\Temp\Fullea\FA2Boug_CGv1D.F95(469) : warning 179 - Comparing floating point quantities for equality may give misleading results C:\Temp\Fullea\FA2Boug_CGv1D.F95(484) : warning 197 - Variable RH has been declared but not used C:\Temp\Fullea\FA2Boug_CGv1D.F95(485) : warning 179 - Comparing floating point quantities for equality may give misleading results C:\Temp\Fullea\FA2Boug_CGv1D.F95(527) : warning 179 - Comparing floating point quantities for equality may give misleading results C:\Temp\Fullea\FA2Boug_CGv1D.F95(529) : warning 179 - Comparing floating point quantities for equality may give misleading results C:\Temp\Fullea\FA2Boug_CGv1D.F95(531) : warning 179 - Comparing floating point quantities for equality may give misleading results C:\Temp\Fullea\FA2Boug_CGv1D.F95(533) : warning 179 - Comparing floating point quantities for equality may give misleading results C:\Temp\Fullea\FA2Boug_CGv1D.F95(535) : warning 179 - Comparing floating point quantities for equality may give misleading results C:\Temp\Fullea\FA2Boug_CGv1D.F95(566) : warning 298 - Variable PI has been used without being given an initial value ************* C:\Temp\Fullea\FA2Boug_CGv1D.F95(603) : error 62 - Invalid KIND specifier C:\Temp\Fullea\FA2Boug_CGv1D.F95(606) : error 636 - KIND parameter out of range, permitted KINDs are 1, 2, or 3 C:\Temp\Fullea\FA2Boug_CGv1D.F95(606) : error 636 - KIND parameter out of range, permitted KINDs are 1, 2, or 3 Compilation failed.

It does not seem to like the real(qp) setting.

It is almost there!

Cheers Lester

18 Jul 2013 8:13 #12658

Hi all,

I made a small change to the code:

  SUBROUTINE write_pi

! Example of kind options USE MOD_Precision real(sp) :: pis real(dp) :: pi ! real(qp) :: piq

  pi  = 4.0_dp * ATAN (1.0_dp)

! piq = 4.0_qp * ATAN (1.0_qp) pis = 4.0_sp * ATAN (1.0_sp) write (,) pis, kind(pis), precision(pis) write (,) pi, kind(pi), precision(pi) write (,) piq, kind(piq), precision(piq) write (,'(f27.19)') pis write (,'(f27.19)') pi write (*,'(f27.19)') piq

Just commented out the real(qp) and its value piq, and the code then compiled and ran as expected. So I am not sure what it did not like about the entries and the precision - @ JohnC

Would be good to understand what the issue is, as I am going through the code putting commentary so I can get to grips with the flow.

Cheers Lester

19 Jul 2013 12:38 #12659

Lester,

to overcome the warning about PI, change write_pi to:

      subroutine write_pi
!  example of kind options
      USE MOD_PARAM 
!
      pi  = 4.0_dp * ATAN (1.0_dp) 
      piq = 4.0_qp * ATAN (1.0_qp) 
      pis = 4.0_sp * ATAN (1.0_sp) 
      write (*,*) pis, kind(pis), sp, precision(pis) 
      write (*,*) pi,  kind(pi),  dp, precision(pi) 
      write (*,*) piq, kind(piq), qp, precision(piq) 
      write (*,'(f27.19)') pis 
      write (*,'(f27.19)') pi 
      write (*,'(f27.19)') piq 
! 
      END 

The only warning that is important is #298, which implies that PI, that is defined in MOD_PARAM is not being used at line 267 and 566 ? You should make sure you either call write_pi, or include it's definition at the start. I really do not understand the comments about qp. The following is a valid definition for real*10, which should be in MOD_Precision INTEGER, PARAMETER :: qp=SELECTED_REAL_KIND(18,500)

John

19 Jul 2013 8:01 #12661

Hi John,

Thanks for the pointers.

I copied the compiled program to work for use and when run it gave a run-time error with the following:

C:\F95-Temp\FA2BOUG>FA2Boug_CGv1D

Unhandled Exception: System.TypeInitializationException: The type initializer fo r 'FA2Boug_CGv1D' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'ftn95lib, Version=1.1.0.0, Culture=neutral, PublicKeyToken=626494245e82c014' or one of its dependencies. The system cannot find the file specified. File name: 'ftn95lib, Version=1.1.0.0, Culture=neutral, PublicKeyToken=626494245 e82c014' at FA2Boug_CGv1D..cctor()

WRN: Assembly binding logging is turned OFF. To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1. Note: There is some performance penalty associated with assembly bind failure logging. To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

--- End of inner exception stack trace --- at FA2Boug_CGv1D.Main()

I am on Windows 7 enterprise, but do not as yet have ftn95 on this machine. Do I need that installed ? Compiled the code on my home PC (Win7 Home edition 64 bit).

Cheers Lester

19 Jul 2013 2:38 #12663

Got our IT guys to install FTN95 and compiled the program, although it had issues with Pi at run-time. Had to re-instate the value of pi as originally there to get it running.

Bit odd, as I used and compiled the same code on my home pc and it went fine with the changes made.

Bit slow running but guess its due to the /debug and /checkmate options - couldn't see how to remove that.

Cheers

Lester

20 Jul 2013 12:06 #12669

Quoted from JohnCampbell Lester,

to overcome the warning about PI, change write_pi to:

      subroutine write_pi
!  example of kind options
      USE MOD_PARAM 
!
      pi  = 4.0_dp * ATAN (1.0_dp) 
      piq = 4.0_qp * ATAN (1.0_qp) 
      pis = 4.0_sp * ATAN (1.0_sp) 
      write (*,*) pis, kind(pis), sp, precision(pis) 
      write (*,*) pi,  kind(pi),  dp, precision(pi) 
      write (*,*) piq, kind(piq), qp, precision(piq) 
      write (*,'(f27.19)') pis 
      write (*,'(f27.19)') pi 
      write (*,'(f27.19)') piq 
! 
      END 

The only warning that is important is #298, which implies that PI, that is defined in MOD_PARAM is not being used at line 267 and 566 ? You should make sure you either call write_pi, or include it's definition at the start. I really do not understand the comments about qp. The following is a valid definition for real*10, which should be in MOD_Precision INTEGER, PARAMETER :: qp=SELECTED_REAL_KIND(18,500)

John

I managed to fix the issue with pi, not sure how but I had commented out the qp entries and then uncommented and it seemed to be happy - odd!

Got a Fortran95 text on order so will brush up on things , but this has been a useful exrcise and thanks for the guidance John, much appreciated.

Please login to reply.