Debug SWAT in Eclipse – Utilize Makefile

Don’t Want to Go through all these Processes?

No problem. The Eclipse project created in this post is uploaded to Google Code: https://swat-eclipse.googlecode.com/svn/trunk/SWAT_Makefile. Just check out and open in Eclipse. It’s done!

This version also add Make Target, which is a quick way to build and clean the project. Just double-click to build or clean specific version.

It’s recommended to use SVN Plugin for Eclipse to do this work. I would have another post on this topic in the near future. Please stay tuned.

Contents

  • Find Fortran Project… Menu
  • Create an Empty Makefile Project
  • Setup the Project
    • Binary Parser
    • Error Parsers
    • Fortran Build Configuration
  • Copy SWAT Source Codes and Generate Makefile
  • Build Project
  • Debug/Run SWAT.exe

Please note

  1. To compile SWAT, MinGW needs to be installed first. Please refer to another post MinGW Installation Guide for SWAT Debugging
  2. Eclipse KEPLER and PTP 7.0.3 was used in this post. These steps should also be able to be used in other version of Eclipse and PTP. For more information, please refer to my previous post Compile and Run SWAT with Photran + MinGW – the easiest way to remove mathematical errors from SWAT.

A Makefile is created in previous post. To use it in Eclipse (Photran/PTP), instead of Executable (Gnu Fortran on Windows) project, we should use the Makefile project, which would allow use of user-defined Makefile.

This post will show you how to create and configure a SWAT Makefile project to utilize the makefile.

Find Fortran Project… Menu

The Fortran Project is not list in menu File -> New when PTP is used the first time. In this case, select Projects… to open New Project window. In this window, you would find Fortran project under Fotran folder. Select it and click Next to create the project. When it’s done, you will be asked if you want to open Fortran perspective. Select Yes. The Fotran Project option would be list in menu File -> New.

Create an Empty Makefile Project

File -> New -> Fortran Project

The project name is SWAT. Select Empty Project under Makefile project and select — Other Toolchain —. Click finish to create the project..

Setup the Project

Open the Property window through right click on the project and select “Properties” to setup the project.

Binary Parser

In Fotran -> Build -> Setting, select PE Windows Parser for Binary Parsers (I’m working a Windows machine)..

Error Parsers

Select Photran Error Parser for GNU Fortran (gfortran) for Error Parsers and move it to the top. This option is not selected when the project is created.

Fortran Build Configuration

Select Fortran Build in the left panel, un-check Use default build command and change Build Command to mingw32-make.

Add debug/ to build directory as there will be two versions of Makefile, one is located in debug folder and another is located in release folder. This one is for debug version.

Change configuration name from Default to Debug.

The default configuration name is Default. It would be nice to rename it to debug. Click Manage Configurations… to open Manage Configurations window. Select default and click Rename… to change name to Debug.

Create Release Configuration

As mentioned before, there is also a release version of Makefile. It’s necessary to make a configuration for it. Here, we could copy all the settings from debug configuration to save time.

Click New… on Manage Configurations window to open Create New Configuration window. Input name as Release and select to copy settings from Debug configuration.

Change the Build directory to ${workspace_loc:/SWAT}/release/.

Copy SWAT Source Codes and Generate Makefile

Copy all the SWAT source codes to the project folder and refresh the project in Eclipse. It would show all the files. Remember to comment the first line in main.f.

Download the GenerateMakefile program and save it into the project folder. Run it to generate the Makefile for debug and release version.

Build Project

Now, it’s ready to build the project, i.e. compile SWAT source codes. Right-click the project and select Build Project to start compile SWAT source codes. You would see some output information from Console window in the bottom. From there, you would know whether or not the process is finished. All the warnings and errors would also be found in this window.

Debug/Run SWAT.exe

After SWAT is compiled successfully, it’s ready to run SWAT. Before that, there is one thing to do: setup Run/Debug Settings, which could be completed in Properties window.

Select Run/Debug Settings in the left panel and click New… to create a Run/Debug Settings. We will first create the settings for debug version. Same as the build configuration, the setting for release version could be also copied from debug version.

In the Main tab, input Fortran Application as debug\SWAT.exe.

In Arguments table, change Working directory to the model txtinout folder.

In Debugger tab, select MinGW gdb for Debugger and uncheck Stop on startup at: option.

In Common tab, select Debug in Display in favorites menu to show the setting in debug menu.

Click debug menu in the toolbar to start running SWAT.

The output information would be display in the Console window.

If some breakpoints are set, SWAT would stop running on breakpoints and ask if want to change to Debug perspective. Click yes to change to Debug perspective, which would re-arrange the windows to facilitate debugging.

You would has change to check all the variable values in Variables window under debugging mode.

Click Fortran button on the up right corner, Eclipse would go back to Fortran perspective, which is designed for coding.

Click the Debug button in the up right corner to return to Debug perspective.

Eclipse Compatible SWAT Updated to Rev 614 – Fix ET > PET bug

SWAT model was update to rev 614 on Jan 7, 2014. Based on the difference between rev 613 and rev 614, the Eclipse compatible version is also updated. The source codes are here: https://code.google.com/p/swat-eclipse/.

List of Changes (click the file name to see detailed changes)

  • main.f The version number is updated.
  • etact.f Line 131 and Line 172-182, to make sure the actual ET doesn’t exceed PET. This would have impact on ET and surface runoff for some cases.

It’s highly recommended to update to this version.

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.