Bennie and Everyone Else:

            This is a classic example of the general Module Decomposition Problem.  Module decomposition is a standard practice of software engineering.  You break a large, complex problem down into smaller, more understandable components and continue iteratively until you are done.

 

            What you need to do is to decompose each program into component modules and continue this process until the final set of modules consists solely of modules that are completely self-contained.  Depending on the size and complexity of your program(s), you may need to decompose your modules iteratively two, three, four, or even more times.

 

            Here is an example.  Suppose you have two very large programs that I will call Behemoth and Leviathan.  Suppose that Behemoth and Leviathan include procedures in these categories:

 

            Behemoth

                        Electrical

                        Mechanical

                        Math

 

            Leviathan

                        Fluids

                        Mechanical

                        Thermodynamic

                        Math

 

            In this first level of modular decomposition, each of these categories would compose a Fortran module.  If each of these modules is completely self-contained (i.e., no references to procedures outside of the module), then you are done.  If a module has references to procedures outside of itself, then you decompose that module into a second set of modules.    You continue this process until you have a set of modules that have no references outside of themselves.

 

            Modules have been part of Fortran since Fortran 90 (June 1991).

 

            You create modules by placing the source code for your procedures into module declarations:

            Module Module_Name_M

            !   Place module-level data here.

            Contains           ! This statement separates module data from module procedures.

            !   Place the source code for your subroutines and functions here.

            End Module Module_Name_M

 

            You reference a module in another module or procedure through the Use statement, e.g.,

            Use Module_Name_M

 

            Once you have this final set of completely self-contained modules, you build your program from the bottom up.  In your make file, you compile the completely self-contained modules first.  Since there are no references outside of each module, all of the modules should compile correctly, assuming there are no other errors.  Then you compile the modules that use the self-contained modules.  You continue compiling up the chain until you compile and link your executable programs.

 

            Don’t worry about duplicate references to modules.  The Fortran standard takes care of that problem for you.  It says, “More than one USE statement for a given module may appear in a specification part.”  (Fortran 2008, sec. 11.2.2)

 

            Hope this helps.

 

Sincerely,

Craig T. Dedo

17130 W. Burleigh Place

P. O. Box 423                         Mobile Phone:  (414) 412-5869

Brookfield, WI   53008-0423    E-mail:  <[log in to unmask]>

USA

Linked-In:  http://www.linkedin.com/in/craigdedo

 

From: Fortran 90 List [mailto:[log in to unmask]] On Behalf Of bennie blackwell
Sent: Thursday, September 29, 2011 08:28
To: [log in to unmask]
Subject: F90 code organization question

 

I have written two independent codes and now want to make code 2 (est) as a subroutine of code 1 (chaleur). Within chaleur, there are hooks for a subroutine named “usr_subr_get_tchem”. Since I knew from the beginning that I wanted to couple the two codes, est also includes a subroutine named “usr_subr_get_tchem” with the idea of simply dropping est/usr_subr_get_tchem.f90 into chaleur and I would be ready to compile the combined programs. Unfortunately, both chaleur and est have some common text processing routines as well as common modules; this makes things more complicated than I anticipated. For the purposes of discussion, suppose the two codes are structured as follows:

 

program chaleur

                various modules

                subroutine chal_1

                subroutine chal_2

                …

                subroutine chal_23

                subroutine usr_subr_get_tchem

 

program est

                various modules, some of which are also in chaleur

                subroutine usr_subr_get_tchem

                                subroutine chal_7

                                subroutine chal_9

                                subroutine chal_11

                                subroutine est_1

                                subroutine est_2

                                …

                                subroutine est_14

 

The reason for the two code structure is that program est could be debugged independent of chaleur and I want to preserve this into the future.

 

When I try to compile chaleur (makefile) using subroutine usr_subr_get_tchem from program est, subroutines chal_7, chal_9, and chal_11 are doubly defined and I get errors. Can you suggest a way around this dilemma that does not require that I delete all the duplicate routines contained in est/usr_subr_get_tchem? An additional constraint is that est needs to continue to function as a standalone code in addition to providing usr_subr_get_tchem for chaleur.

 

Thanks,

Ben

 

Ben Blackwell

Blackwell Consulting

PO Box 2879

Corrales, NM 87048

505-897-5090

(fax) 505-890-4992

[log in to unmask]