开发者

Fortran 90: How to use a module in a function

开发者 https://www.devze.com 2023-02-03 08:30 出处:网络
I am trying to write a fortran program that uses green\'s functions to solve the heat equation. I am useing fortran 90 as opposed to 77 in part because I am under the impression that it is pretty much

I am trying to write a fortran program that uses green's functions to solve the heat equation. I am useing fortran 90 as opposed to 77 in part because I am under the impression that it is pretty much a replica of fortran 77 but with a few very helpful features thrown in. Though the main reason is for free form coding. I have some "useful" constants and variables in a module named "useful." I want to include those variables and constants in most of my programs, subroutines, functions, etc. I am just learning fortran. I have programed in perl, C++, java, matlab, and mathematica. I am finding it very difficult. I do not understand the difference between a program and a subroutine. I definitely have no clear idea of what a module is. I have spent the past 12 hours researching these terms and have yet to get concise distinctions and definitions. I get an assortment of samples t开发者_Go百科hat show how to declare these things, but very little that actually delineates what they are supposed to be used for.

I would really appreciate an explanation as to why my function, "x" is not able to "use" my "useful" module.

Also, a clarification of the previously mentioned fortran features would be really helpful.

module useful

  integer, parameter :: N=2
  double precision, parameter :: xmin=1, xmax=10, pi=3.1415926535898
  double complex :: green(N,N), solution(N), k=(2.0,0.0)

end module useful

program main

  use useful

  !real*8 :: delta = 2**-7
  do n1 = 1, N
    do n2 = 1, N
      green(n1,n2) = exp((0,1)*k*abs(x(n2)-x(n1)))/(4*pi*abs(x(n2)-x(n1)))
      print *, x(n2)
    end do 
  end do

end program main

function x(n1)

  use useful

  real :: n1, x
  x=n1*(xmax-xmin)/N

end function x


Let start with some definitions.

Fortran programs are composed of program units. In so-called Fortran 2008 (current standard) there are five types of program units:

  • main program;
  • external subprogram;
  • module;
  • submodule;
  • block data program unit.

Let me concentrate your attention on the first three.

Main program
As standard claims it is

program unit that is not a subprogram, module, submodule, or block data program unit.

Not very useful definition. =) What you should know is that main program unit starts with keyword PROGRAM, it is the entry point of application and obviously a program shall consist of exactly one main program.

A program may also consist any number (including zero) of other kinds of program units.

External subprogram
A subprogram defines a procedure. There are two types of procedures and of course two types of subprograms to define them:

  • function subprograms to define functions;
  • subroutine subprograms to define subroutines.

A function subprogram is a subprogram that has a FUNCTION statement as its first statement. A subroutine subprogram is a subprogram that has a SUBROUTINE statement as its first statement.

Also both procedures and subprograms differ in place of their appearance in program. You can use:

  • external subprograms to define external procedures;
  • internal subprograms to define internal procedures;
  • module subprograms to define module procedures.

external subprogram
subprogram that is not contained in a main program, module, submodule, or another subprogram

internal subprogram
subprogram that is contained in a main program or another subprogram

module subprogram
subprogram that is contained in a module or submodule but is not an internal subprogram

Module
It is just a definitions (type denitions, procedure denitions, etc.) container.

Now small example.

main.f90

! Main program unit.
PROGRAM main

  USE foo

  IMPLICIT NONE

  CALL external_bar
  CALL internal_bar
  CALL module_bar

  CONTAINS

    ! Internal subprogram defines internal procedure.
    SUBROUTINE internal_bar
      PRINT *, "inside internal procedure"
    END SUBROUTINE internal_bar

END PROGRAM main

foo.f90

! Module program unit.
MODULE foo

  IMPLICIT NONE

  CONTAINS

    ! Module subprogram defines module procedure.
    SUBROUTINE module_bar
      PRINT *, "inside module procedure"
    END SUBROUTINE module_bar

END MODULE foo

bar.f90

! External subprogram program unit.
! External subprogram defines external procedure.
SUBROUTINE external_bar
  PRINT *, "inside external procedure"
END SUBROUTINE external_bar


A module is a higher level of organization than a subroutine or function.

Function "x" should be able to use the module useful. I think it more likely that program "main" is having difficulty correctly accessing "x". You would do better to also place function "x" into the module after a "contains" statement. In this case remove the "use useful" statement from "x". Then the program main would have full knowledge of the interface ... in Fortran nomenclature, the interface is explicit. This ensures that the passing of arguments is consistent.

It would help if you showed the error messages from the compiler.

Fortran 95/2003 is much more than FORTRAN 77 with a few extra useful features. The changes are much larger. Trying to learn it from the web is not a good idea. I suggest that you find a copy of the book "Fortran 95/2003 Explained" by Metcalf, Reid and Cohen.


I see two problems in your program.

  1. The main subroutine doesn't know what x() is. Is it a real function, integer function, etc.? You can either add the following to your main program real, external :: x Or (as others suggested) move function X() into your module. To do this, add a "contains" statement near the end of the module and put the function between the "contains" and the "end module".

  2. You have an argument mismatch. In the main program, N1 is explicitly declared as a real variable. In function X(), N1 is declared as a real. You need to fix this.

I would strongly suggest that you add "implicit none" statements to your main program, module, and function. This will force you to implicitly type each variable and function.

0

精彩评论

暂无评论...
验证码 换一张
取 消