Skip to content

Instantly share code, notes, and snippets.

@szaghi
Last active April 13, 2016 10:03
Show Gist options
  • Save szaghi/f1957cc3ddd472c1f9dfdd4f1356c78a to your computer and use it in GitHub Desktop.
Save szaghi/f1957cc3ddd472c1f9dfdd4f1356c78a to your computer and use it in GitHub Desktop.
Example of modules for Alessandro

A gist for Alessandro

Source files baseline status

Let us assume you have the following situation (for the sake of brevity let us have only 2 source files):

  • file_1.f: it contains procedures sub_1 and sub_2;
  • file_2.f: it contains procedure sub_3 that uses sub_2 of file_1.f90.

Refactoring by modules

I suggest you to modify your project in the following way:

file_1.f becomes file_1.f90

The new file file_1.f90 contains:

module file_1
!implicit none ! this can hurt your baseline code if you use implicit definitions
contains
  ! the following procedures are the one you have in your original files
  subroutine sub_1()
  ! do some stuff
  endsubroutine sub_1()
  
  subroutine sub_2()
  ! do some other stuff
  endsubroutine sub_2()
endmodule file_1

file_2.f becomes file_2.f90

The new file file_2.f90 contains:

module file_2
use file_1
!implicit none ! this can hurt your baseline code if you use implicit definitions
contains
  ! the following procedures are the one you have in your original files
  subroutine sub_3()
  ! do some stuff using sub_2 of file_1.f90
  call sub_2
  endsubroutine sub_3()
endmodule file_2

Implcations

Now all your procedures have automatically an interface for checking calling signature errors.

Complications

You have now a compilation hierarchy: because module file_2 depends on module file_1, this latter must be compiled before the first one.

For large projects having many modules I suggest to try FoBiS.

Final note

Now your main program must `use] your new shining modules...

program modularized
use file_1
use file_2

!do your stuff with your modules
endprogram modularized
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment