Compile and Run SWAT with Photran + MinGW – the easiest way to remove mathematical errors from SWAT.

Eclipse-friendly SWAT rev 613 codes: https://code.google.com/p/swat-eclipse/. TortoiseSVN is recommended to check out the codes.

Quick Download Link

Photran/PTP/Eclipse, MinGW

Or you could send me (hawklorry@gmail.com) your error message. I couldn’t be more happy to help.

Contents

  • Compiler and IDE
  • Create a Fortan Project
  • Download SWAT Source Codes and Add into Fortran Project
  • Compile modparm.f
  • Modify Source Codes to Compile
  • Debug/Release Version
  • Set Breakpoint and Run
  • Remove Static Library Dependency
  • How to Locate the Errors

Have seen some very basic mathematical errors (e.g. overflow) given by SWAT in SWAT user group, like this one here.

After 30 years development, SWAT has been a very complicated system. The latest version of SWAT (rev. 613) consist of 302 Fortran files. It’s very easy to import some small errors when adding new algorithms. And the developing team couldn’t test SWAT on any possible watersheds. After all it’s a model for research not for commercial.

When these small errors come out in your model, the first thing we need to do is to check the model data first. After that, if the errors still persist, while waiting for answer from SWAT developing team to release another version, we could also go into the codes based on the error information and add some conditions around the mathematical equation to avoid these errors. It could save you from frustration and gain you some satisfaction. You also could also submit your codes and contribute to SWAT development.

Compiler and IDE

The first thing need to do is to get a Fortran compiler and IDE. Not sure what compiler is used by the SWAT developing team. There are usually two options here.

    1. Intel Visual Fortran
    2. Photran which is a component of the Eclipse Parallel Tools Platform (PTP). Please note that Photran is not a separated software. To use it, please download PTP and extract it to any folder. You will find eclipse.exe in the folder, which is the Eclipse main program. Please go to http://eclipse.org/downloads/ to download this software. In this page, choose “Eclipse for Parallel Application Developer”.

Compared to Intel Visual Fortran, Photran is free and cross-platform. It’s chosen as the main environment to compile and run SWAT.

Photran doesn’t come along with any Fortran compiler. GFortran is chosen as the compiler working together with Photran. In Windows, the easiest way to use GFortran is to install MinGW. Please refer to my another post MinGW Installation Guide for SWAT Debugging.

So, Photran + MinGW is the main development environment.

Create a Fortan Project

In Eclipse or PTP, File -> New -> Fortran Project

Give a project name and select project type as “Executable (Gnu Fortran on Windows) and toolchains as GCC Fortan. (I’m working on a Windows PC.) You need to remember the project location to be used laster.

Then click Finish button to create the project. You will see this project in left Fortran Projects window.

Download SWAT Source Codes and Add into Fortran Project

Download the lastest version of SWAT from official SWAT website: http://swat.tamu.edu/software/swat-model/.

Please note that the official source codes are not ready to use. Some necessary modification is needed to be able to compile with gfortran, which will be illustrated below. I have put the modified version in Google Codes: https://code.google.com/p/swat-eclipse/, which is ready to use in Eclipse.

Extract source codes into the project folder specified when creating the fortran project.

In Fortran Projects window, right-click the project you create and select Refresh. Eclipse will automatically add all the source codes into the project.

Compile modparm.f

All SWAT global variables are defined in modparm.f, which need to be compiled as parm.mod before the source codes could be successfully compiled.

  1. Open cmd (Windows Command Processor) and go into the project folder with CD command.
  2. Compile modparm.f with gfortran: gfortran –c modparm.f

Modify Source Codes to Compile

As mentioned before, the official source codes are not ready to compile with gfortran. You would get some errors most of which are related to format. That may come from the difference of compilers or settings.

To successfully compile SWAT with gfortan, the source codes are modified. The modified version could be found here: https://code.google.com/p/swat-eclipse/. It’s based on the latest ver 613. I will update once the official SWAT is updated.

Compile Source Codes

Select the project in the Fortan Projects window and use “Project -> Build All” to start compile and build the final exe file.

You would see the progress messages in Console window.

The first time you compile SWAT, it will need several minutes to compile all fortran source code file (*.f and *.f90) into *.o files and link them together. The second time will be much faster because only modified source file will be compiled and linked.

Debug/Release Version

The same as most IDE, you could build debug and release version of the fortran program. Usually debug version is used to debug and runs slow and release version is used to distribute to users and runs faster.

As we want to find where the mathematical errors are and fix it, the debug version should be used first.

Set Breakpoint and Run

Set a breakpoint is farely easy. Just double-click the left edge of the codes editor and you will see a blue point will be added. Double-click again to remove the breakpoint.

SWAT assume all the input files are in the same folder of the program. To run SWAT successfully, we need to set the model folder as the working directory to tell SWAT where the input files are.

To do this, right-click your project and select “Properties”. In the project properties window, chose “Run/Debug Settings” and select one of the configurations in the right box. On the right, click “Edit…” button to edit the select configuration.

In Edit Configuration window, select Arguments table and edit the working directory as the model input folder, which is usually located in [ArcSWAT Project]\Scenarios\Default\TxtInOut.

Now, it’s ready to run. Click the bebug button to run the debug version of SWAT.

Remove Static Library Dependency

When the errors are removed, you may want to send a release copy to someone. If gfortran environment is not available in their computer, the program will fail to run. That’s because two library files are required to run the program compiled with gfortan.

To avoid this, go to the project properties window and select Fortran Build -> Settings in the left panel and select GNU Fortran Liker -> General in Tolol Settings on the right. Check “No shared libraries” to force linker to include the static libraries into the final exe file.

Please note that this could be done for debug and release version separately. Make sure you chose the right version.

How to Locate the Errors

It’s easy to locate the errors from the error message, which gives the line number and the Fortran file name. This is the call stack information and would usually show all the running routines/functions. The routine/function shown in the first line is usually the Fortran file which should be looked into.

For example, from the below error message, the error comes from routine virtual and the location is Line 423, which is sub_gwq_d(sb) = sub_gwq_d(sb) / subfr_nowtr(sb). The problem probably is in the right part of the equation. Modify it and debug on the same model until the problem has gone.

In some cases, the first line is .*c file rather than *.f file, like the one shown below. This usually happens when errors comes from native Fortran (mathematical) functions, which is Exp in this case.

© 2000 – 2021 SWAT Output Viewer All rights reserved