Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you USE Fortran 90 module data

Let's say you have a Fortran 90 module containing lots of variables, functions and subroutines. In your USE statement, which convention do you follow:

  1. explicitly declare which variables/functions/subroutines you're using with the , only : syntax, such as USE [module_name], only : variable1, variable2, ...?
  2. Insert a blanket USE [module_name]?

On the one hand, the only clause makes the code a bit more verbose. However, it forces you to repeat yourself in the code and if your module contains lots of variables/functions/subroutines, things begin to look unruly.

Here's an example:

module constants   implicit none   real, parameter :: PI=3.14   real, parameter :: E=2.71828183   integer, parameter :: answer=42   real, parameter :: earthRadiusMeters=6.38e6 end module constants  program test ! Option #1:  blanket "use constants" !  use constants ! Option #2:  Specify EACH variable you wish to use.   use constants, only : PI,E,answer,earthRadiusMeters   implicit none    write(6,*) "Hello world.  Here are some constants:"   write(6,*) PI, &        E, &        answer, &        earthRadiusInMeters end program test 

Update Hopefully someone says something like "Fortran? Just recode it in C#!" so I can down vote you.


Update

I like Tim Whitcomb's answer, which compares Fortran's USE modulename with Python's from modulename import *. A topic which has been on Stack Overflow before:

  • ‘import module’ or ‘from module import’

    • In an answer, Mark Roddy mentioned:

      don't use 'from module import *'. For any reasonable large set of code, if you 'import *' your will likely be cementing it into the module, unable to be removed. This is because it is difficult to determine what items used in the code are coming from 'module', making it east to get to the point where you think you don't use the import anymore but its extremely difficult to be sure.

  • What are good rules of thumb for python imports?

    • dbr's answer contains

      don't do from x import * - it makes your code very hard to understand, as you cannot easily see where a method came from (from x import *; from y import *; my_func() - where is my_func defined?)

So, I'm leaning towards a consensus of explicitly stating all the items I'm using in a module via

USE modulename, only : var1, var2, ... 

And as Stefano Borini mentions,

[if] you have a module so large that you feel compelled to add ONLY, it means that your module is too big. Split it.

like image 925
Pete Avatar asked Aug 06 '09 18:08

Pete


People also ask

How do I use a module in Fortran 90?

To make use of the module in the main program, we employ the USE statement. We use a special form of the USE statement that specifies that we are only interested in the value radius in the main program, namely, USE Circle, ONLY : radius.

What do you mean by module in Fortran?

Advertisements. A module is like a package where you can keep your functions and subroutines, in case you are writing a very big program, or your functions or subroutines can be used in more than one program. Modules provide you a way of splitting your programs between multiple files.

What is module procedure?

A procedure is a unit of code enclosed either between the Suband End Sub statements or between the Function and End Function statements. A procedure should accomplish a simple well-defined task. Modules are workbook sheets that contain code. Each module can contain a declarations section followed by procedures.

What is data in Fortran?

The DATA statement initializes variables, substrings, arrays, and array elements.


1 Answers

I used to just do use modulename - then, as my application grew, I found it more and more difficult to find the source to functions (without turning to grep) - some of the other code floating around the office still uses a one-subroutine-per-file, which has its own set of problems, but it makes it much easier to use a text editor to move through the code and quickly track down what you need.

After experiencing this, I've become a convert to using use...only whenever possible. I've also started picking up Python, and view it the same way as from modulename import *. There's a lot of great things that modules give you, but I prefer to keep my global namespace tightly controlled.

like image 57
Tim Whitcomb Avatar answered Sep 28 '22 12:09

Tim Whitcomb