Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
451 views
in Technique[技术] by (71.8m points)

fortran90 - How to use Fortran 77 subroutines in Fortran 90/95?

I'm writing a code with Fortran 90 and now I need to use the special functions in the*amos Fotran 77 library(http://www.netlib.org/amos/). Now I found a module interface for those routines(https://github.com/certik/fortran-utils/blob/master/src/amos.f90).

My question is: how can I combine them and use them in my Fortran 90 program and how to compile them correctly?

I have been struggling for this for one whole day and still could not figure it out.

The following is my test code:

PROGRAM TEST_ZBESI
USE set_precisions
USE amos
IMPLICIT NONE
INTEGER :: n, i, nz, ierr
!double precision :: zr,zi, cyr(5), cyi(5)
REAL(kind=DBL) :: zr, zi, cyr(5), cyi(5)

n=5
zr=1.0_DBL
zi=2.0_DBL

call ZBESI(zr,zi,0.0_DBL,1,n,cyr,cyi,nz,ierr)
print *,' '
do i=1, n
   write(*,10) i-1, cyr(i)
   write(*,11) i-1, cyi(i)
end do
print *,' NZ=', NZ
print *,' Error code:', ierr
print *,' '

10 format('  zr(',I1,') = ',F10.6)
11 format('  zi(',I1,') = ',F10.6)

END PROGRAM TEST_ZBESI

The result I got is the following:

  zr(0) =   0.000000
  zi(0) =   0.000000
  zr(1) =   0.000000
  zi(1) =   0.000000
  zr(2) =   0.000000
  zi(2) =   0.000000
  zr(3) =   0.000000
  zi(3) =   0.000000
  zr(4) =   0.000000
  zi(4) =   0.000000
  NZ=           0
  Error code:           4

It seems I could not get the correct answer no matter how.

I tried to convert the ZBESI.f Fortran 77 code to Fortran 90 code by hand. But the code is so long and it was a disaster.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With extremely few exceptions, FORTRAN 77 is a subset of Fortran 90/95/2003/2008. And in practice, compilers still support the obsolete features. Compiling the FORTRAN 77 and Fortran 90/59/2003/2008 source with the same compiler should produce compatible object modules. You will likely have to compile the two language versions separately since different compiler options will probably be necessary, e.g., for fixed and free-form source layout. With the interfaces in your Fortan 90/95/2003/2008 code, the compiler will use compatible calling conventions.

What specific problems are you having? Do you need to know the compiler options for FORTRAN 77? What compiler are you using?

EDIT: You have to compile the module before the source code that uses it. It is convenient to compile the FORTRAN 77 first, into an object file, and then use the fortran command that compiles the Fortran 95 to link everything. So try:

ifort -c -fixed ZBESI.f
ifort ZBESI.o set_precisions.f90 amos.f90 test_ZBESI.f90.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...