Search code examples
fortrangfortranfortran77

Error: Two main PROGRAMs in an f77 Program


I read a paper that has f77 fortran code. I want to run the program and step through it so I can port it over to MATLAB. However, when I try to run the program, it doesn't work. I get the following error:

$f77 -g Algorithm634.f 
Algorithm634.f:10.72:

      INTEGER FITDEG,DIMEN,NFPOLS,NFPTS,NEPTS,NEPOLS,EVLDEG,TOPS        
                                                                        1
Algorithm634.f:142.72:

      INTEGER DIMEN,FITDEG,NFPOLS,NFPTS,EVLDEG,NEPOLS,NEPTS             
                                                                        2
Error: Two main PROGRAMs at (1) and (2)

Clearly, I need to refactor the code. (1) listed above generates data that is then used in (2). However, I am not familiar with old f77 programs. I am not sure exactly what to do to refactor this.

Dropbox Fortran file and Papers

EDIT: I was told not to link to dropbox. Fine. Here is the whole program:

https://pastebin.com/ULLLPmdL


Solution

  • I downloaded Algorithm634.f from your dropbox. As the compiler message suggests, there are 2 separate programs, which I created as generate.for and driver.for.

    If you compile and link them as generate.exe and driver.exe, you can then run them.

    First the generate program, as:

    gFortran generate.for -o generate.exe
    generate > generate.txt
    

    this will produce the data file that is required for driver.

    Then use the driver program, as:

    gFortran driver.for -o driver.exe
    driver < generate.txt > driver.txt
    

    This will produce some output, which you can review.

    ( Actually, I changed generate.for, by including the first executable line as:

      open (unit=16, file='gendat.txt')
    

    then changed all write (6, to write (16, so that the output was written to a file, rather than redirected.

    I then changed driver.for, by including the first executable line as:

      open (unit=15, file='gendat.txt')
    

    then changed all read (5, to read (15, so that the input was read from a file, rather than redirected.

    Changing units 5,6 to 15,16 is to avoid potential problems with reserved file unit numbers in the range 1:9, which different Fortran compilers can apply. )

    I would leave it to you to confirm the program works. Most Fortran compilers have an associated "debugger" which will allow you to step through the program, so you may complete your next phase.

    code.f or code.for indicates the code layout is fixed format code. As well as F77 compilers, all F90+ Fortran compilers will recognise this code and should cope.

    In summary, the solution is to separate Algorithm634.f into the 2 separate files.