After several posts about compiling and debugging SWAT with GFortran and Eclipse, I think it would be good to compile all the posts into an single document as a guide on this topic. So I create a google doc here and would like invite all of you to give some comments.
Note: This version is updated in next post SWAT Makefile Updated – Ignore Underflow, where underflow flag is removed.
The Makefile published in previous blog Makefile – Compile SWAT using gfortran without modification is updated. It’s highly recommended to update.
- More gfortran flags are added to make sure the SWAT will stop running when errors happen.
In previous version, SWAT would ignore all the errors (e.g. overflow) and run to the end. It’s impossible to know if there are some errors. For some cases, the results are not safe to use.
The official SWAT would stop whenever an error happens and give where the error comes from. It’s the right way to compile SWAT.
In gfortran, the flag -ffpe-trap could used to do this work. The following command flag is added to the Makefile, which would stop the program whenever invalid, zero, overflow or underflow happens.
- rm command is used to replace del command in the clean target.
The del command could only be called from cmd.exe. Eclipse don’t know where is this command and would fail to clean the project.
rm.exe comes along with MSYS in MinGW (package mingw-developer-toolkit and msys-base) and is usually located at C:\MinGW\msys\1.0\bin. Adding this location to the PATH variable in Windows setting or Eclipse project setting would allow Eclipse to call this function when “Clear Project” is used.
Note: The makefile here is an older version. For the latest version, please visit another post SWAT Makefile Updated – Stop running when overflow happens.
Quick Download Link
Next post will be: Use Makefile in Photran to Debug SWAT
Please note that the modparm.f doesn’t need to be compiled as parm.mod any more. That process is already included in the Makefile.
- Why some files fail to be compiled in Photran?
- Use additional gfortran command options to compile problematic files
- Generate Makefile
- The only modification
- Use Makefile
In my previous post Compile and Run SWAT with Photran + MinGW – the easiest way to remove mathematical errors from SWAT, several swat source code files need to be modified to be able to compilable within Photran. It’s nice, but it’s even better if the source codes could be compiled without any modification.
Why some files fail to be compiled in Photran?
Compare the original source codes and the modified source codes through Google codes, and find that only following files need to be modified.
Except for main.f, these files have a common problem: one or more lines exceed the default line length set for Fotran fix format file (*.f) and free format file (*.f90). The default limitation of the line length for *.f is 72 and for *.f90 is 132. The default Fortran compile command (shown below) in Photran doesn’t consider this situable. If the length of one line exceed the limitation, the compiler (gfortran) would give errors.
- Debug version: gfortran -funderscoring -O0 -g -Wall -c -fmessage-length=0
- Release version: gfortran -funderscoring -O3 -Wall -c -fmessage-length=0
Use additional gfortran command options to compile problematic files
To solve this problem, these files are modified to make sure they meet the requirement on line length in previous post. Besides this, there is also another solution: change the line length limitation using gfortran command options.
- Fixed format (*.f): add -ffixed-line-length-132 option to set the line length limitation of fixed format Fortran file to 132
- Freeformat (*.f): add -ffree-line-length-200
option to set the line length limitation of free format Fortran file to 200
For example, the gfortran command for debug version of biozone.f would be
gfortran -funderscoring -O0 -g -Wall -c -fmessage-length=0 -ffixed-line-length-132
And the gfortran command for release version of carbon_zhang2.f90 would be
gfortran -funderscoring -O0 -g -Wall -c -fmessage-length=0 -ffree-line-length-200
These files could be compiled successfully with the new command.
In default Photran settings, it’s impossible to use different command options for different source files. Applying -ffixed-line-length-132 to other *.f files would also generate compile errors. How to solve this? Using Makefile.
Makefile defines how to compile and link a program. With it, it’s possible to define different commands for different source files and compile SWAT source codes without modification. For more information, please refer to GNU make document.
There are more than 300 Fortran source files in SWAT. It’s time-consuming work to write the Makefile manually. We already know the gfortran commands and the files which need to re-set the line length limitation. A program, which would get all *.f and *.f90 in the SWAT source codes folder and generate the compile commands in the makefile, is a quite straightforward and simple way to do the work.
A prompt window would pop-up to ask which version should be the Makefile generated for, debug or release. Input d or debug for debug version, r or release for release version. Then the program would start to work and generate the corresponding Makefile in debug or release folder. The debug/release folder would be created if it doesn’t exist.
The two version of Makefile for rev 615 are also available to be download directly (debug version, release version). These two files could be also used for also version as long as the number and name of source codes files is same. Create the debug/release folder in the source codes folder and put the file into the corresponding folder.
The only modification
The only modification is to comment the first line of main.f (include modparm.f). There should be some command option to ignore this line but haven’t found it:)
Makefile is fairly easy to use. Just use the GNU make command which comes along with MinGW. There are two version of make in MinGW: mingw32-make and make. Either one is ok.
Open cmd.exe and navigate to the Makefile folder. Execute mingw32-make or make. It’s done!