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.

12 Replies to “Compile and Run SWAT with Photran + MinGW – the easiest way to remove mathematical errors from SWAT”

  1. Hey Michael,

    I would like to change latq_adjf in percmicro SWAT source code file. I have followed your instruction carefully, however I am not able to compile modparm.f. In cmd I get the error that this command does not exist. I do have installed MinGW but I am not sure if I set the PATH correctly or not. In Eclispe>Window>Preferences>C/C++>Build>Environment I have created the Variable PATH with Value C:\MinGW\bin. I am completely unfamiliar in this field and would appreciate it, if you could help me to solve the error.

    Thanks
    Sin

      1. Hi Michael,
        I solved the issue and gfortran is working. Now I have to change the latq_adjf in percmicro SWAT source code file from 1 (default value) to 0.02. Would you please instruct me how can I do it? I also downloaded the source files that you have uploaded but I am not sure which folder contains the modified codes that I have to use.

        Best regards
        Sin

      2. Hi Sin,

        I’d suggest you to search latq_adjf and find the place to change the default value. And it’s better to use the SWAT source codes from SWAT website.

        Best,
        Michael

  2. Dear Michael,

    This is very useful information.

    I have some concerns about the following note you made:

    “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.”

    Is there a specific compiler for which the official source code files will work “out of the box” (from the developers)?

    Would it be possible to see a list of the changes you made to the official source code files to get them to compile with gfortran? Maybe as a link to this webpage? That would help everyone to understand how to get the original source code to work with gfortran.

    Thank you,
    Steve

    1. Hi Steve,

      Yes, if you look into some of my posts, the official source code could be compiled without any changes. This is true for gfortran. You could find those posts in my blog.

      Please let me know if we have further questions.

      Best,
      Michael

  3. Michael,

    These two statements that you made (below) seem to contradict to one another. Maybe I’m misunderstanding something here.

    “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 official source code could be compiled without any changes. This is true for gfortran.”

    Steve

      1. Michael,

        OK, thanks for clarifying that. So I was misunderstanding. It sounds then like we can follow your instructions here on this webpage, but skip the part about modifying the source code (which you did at one time). Good to know this for future reference!

        Steve

      2. Hey Michael,

        I followed your instructions to compile SWAT source code version 670 downloaded from SWAT website. However, I get the following error: ‘atri’ of module ‘parm’, imported at (1), is also the name of the current program unit. I have tried a few ways to resolve this error, but to no end. Please help me with this.

        Saumya

  4. Hi Michael,
    I used the Source code from the SWAT website. But I got error at “Compile modparm.f”. As you mentioned, errors are related to formats.
    I got following error in the next step since I used the original source code.

    “An internal error occurred during: “Building Workspace”.
    Could not find a file to match the module name: parm”

    But, as you said, new source code is compatible with gfortran. I’m bit confused with this. I may didn’t get the correct steps to follow.

    Could you please help me on this regard.

    Thanks.
    Jeewa

Leave a Reply