0
votes

I have two FORTRAN codes that are almost identical except for one line of code, and the FILE names in my OPEN statements. This one different line of code produces the two distinct final results I am looking for hence why I have two different codes.

I would like to merge these two codes into one if possible.

What is the best way to do this? To be specific, I want to have only one FORTRAN file which will run either FORTRAN 'code'. That way, when I need to edit either code I am only doing so in one file. Now having two different files it is a bit of an inconvenience.

A simplified example code for the two different files are below, Sample.f90 and Sample2.f90. What IF statements can I use that will allow me to choose which initial condition to use, either R = COS(x+y) or R = SIN(x+y)? I am not familiar at all with how to use IF statements for this.

PROGRAM SAMPLE !Sample.f90

USE TestModule

IMPLICIT NONE

REAL, DIMENSION(-10,10) :: R
INTEGER :: i,j
REAL :: x,y

OPEN(UNIT = 100, FILE = 'Sample1.dat')

DO j = -10,10
DO i = -10,10

    x = i*0.1
    y = j*0.1

        R(i,j) = COS(x+y)

END DO
END DO

WRITE(100,*) R(0,1)

END PROGRAM 

!The next program is of the form:

PROGRAM SAMPLE2 !Sample2.f90

USE TestModule2

IMPLICIT NONE
REAL, DIMENSION(-10,10) :: R
INTEGER :: i,j
REAL :: x,y
OPEN(UNIT = 101, FILE = 'Sample2.dat')

DO j = -10,10
DO i = -10,10

    x = i*0.1
    y = j*0.1

        R(i,j) = SIN(x+y)

END DO
END DO

WRITE(101,*) R(0,1)

END PROGRAM
1
Why don't you just show us the code in question? Would be a bit easier to help you with a solution. - TomServo
@JLH The code is quite long though which is why I didn't post a complete/working verifiable example code. However if you still suggest OK - Jeff Faraci
Perhaps not all, just enough to give us a clue how to help you. - TomServo
Drop them both in a file, then write a function at the top to select which to run. Or better yet just drop one in, add the different line and select. - Kevin
@Kevin Thanks for the help. That sounds logical to me. How do you suggest putting them both, or just 1, in a file? Right now I have two different .f90 files. - Jeff Faraci

1 Answers

1
votes

There is the run-time approach. You have an interactive query of which version you want to run, then an IF statement that selects one of the two alternative statements based on that. Another version of this is to read the choice from the command line using the intrinsic GET_COMMAND_ARGUMENT.

A compile-time approach would be to use the preprocessor to select one statement or the other by defining a symbol (e.g., SYM) or not with your compile command (-DSYM), that using #ifdef SYM, #else, #endif to select which Fortran statement to compile.

Some code fragments. Declare choice as an integer and filename as a string.

read (*, '( "Input choice: " )',  advance="no" )  choice

or

call GET_COMMAND_ARGUMENT ( 1, string )
read (string, *)  choice

then:

if (choice /=1 .AND. choice /=2 ) then
   write (*, *) "bad choice"
   stop
end if

if (choice == 1) then
   filename = "FileOne.txt"
else
   filename = "FileTwo.txt"
end if

open (file=filename, ....

and similar IF statement to setup the initial condition.

Or instead you can include or not -DCHOICEONE in your compilation command and use preprocessor lines:

#ifdef CHOICEONE
   filename = "FileOne.txt"
#else
   filename = "FileTwo.txt"
#endif

etc.