Compile and Debug SWAT with gfortran and Eclipse

As a SWAT modeller and programmer, it’s very natural that I want to go deep into the SWAT source codes whenever questions come out. And again, it would be better to run SWAT live and look into values of all the variables to help trouble shooting. To do this, there are two options: commercial Intel fortran compiler and free gfortran. It’s hard to say which one is better. I chose gfortran for its price, freedom, cross-platform and multi-language compiling ability. For some reason, not all SWAT source code files follow the same standard. Compiling SWAT with gfortran is not as easy as Intel fortran. After some research, it comes out the best way to compile SWAT with gfortran. All the necessary steps are documented in one single user guide document with the hope it would be helpful.

All the efforts was recorded in following posts. And here are some important links.

Compile and Debug SWAT with GFortran and Eclipse (Windows version)

https://docs.google.com/document/d/16Do2U1_v4mZZBOV0hmcs6Gh1UvAUAXOMidpB-SE203A/edit?usp=sharing

Makefile

https://drive.google.com/file/d/0B16YhFB_9MejSG15ai0zYS1fMkU/edit?usp=sharing

Makefile Generator

https://drive.google.com/file/d/0B16YhFB_9MejZkxiakdLTHhMVlU/edit?usp=sharing

Posts

February 28, 2014 Single Document about Compiling and Debugging SWAT with GFortran and Eclipse

February 18, 2014 Makefile Updated – 1) No Need to Modify main.f any more; 2) A Single Makefile; 3) 64-bit Build

February 2, 2014 SWAT Makefile Updated – Ignore Underflow

January 28, 2014 MinGW Installation Guide for SWAT Debugging

January 28, 2014 Debug SWAT in Eclipse – Utilize Makefile

January 24, 2014 SWAT Makefile Updated – Stop running when overflow happens

January 23, 2014 Makefile – Compile SWAT using gfortran without modification

January 5, 2014 Compile and Run SWAT with Photran + MinGW – the easiest way to remove mathematical errors from SWAT

Advertisements

34 thoughts on “Compile and Debug SWAT with gfortran and Eclipse

  1. Hello,
    (Sorry for posting this again, but i felt this was a more appropriate place to do so).
    I was installing the MinGW-w64 from (http://sourceforge.net/projects/mingwbuilds/files/mingwbuildsinstall/mingwbuildsinstall.exe/download) and my installation always showed the error “ERROR res” and yet the installation completed.
    Then there is no “C:\Program Files(x86)\mingwbuilds\x324.8.1win32dwarfrev5\mingw32\bin” folder – basically there is no “mingw32″ folder.
    I hope you can help me out with this.
    Thanks a lot for this blog – i hope to get a lot of use out of this!

    • Hi Soham,

      Good to hear from you.

      To be clear, which installation file did you use exactely as there are different options there? We could go from there.

      Cheers
      Michael

      • Hi Michael,

        Thanks for the prompt response!
        As far as i kept track, i first downloaded using “Auto-select”, and later tried all the other mirrors (if that’s what you mean by different options). Perhaps another thing i should mention is that i am currently in an educational institution in India, which uses proxy servers for internet. After each download, unless i used ‘no proxy’, i always got an error which didn’t let the installation start. Maybe that means something?

        Thanks!
        Soham

  2. Dear Zhiqiangyu,

    I received an error when trying to compile the SWAT2012 rev 637 source code. I am compiling from command line using mingw32-make and gfortran. The error message is below. I am not sure, but it sounds like the problem has something to do with the last parenthesis not being interpreted on line 10 of the ndenit.f file.

    Error message:

    ndenit.f:10.72:

    wdn = sol_no3(k,j) * (1. – Exp(-cdn(j) * cdg * vof * sol_cbn(k,j))
    1
    Error: Expected a right parenthesis in expression at (1)
    Makefile: 342: recipe for target ‘debug32/ndenit.o’ failed
    mingw32-make: *** [debug32/ndenit.o] Error 1

    Thank you,
    Steve

      • Zhiqiang,

        Thank you for your reply. I think I was able to resolve this single error by adding flag ${LONGFIX} to the line in the Makefile where ndedit.f is compiled (near line 341). Unfortunately, when I tried compiling again I received a cascade of errors that I was not able to resolve. I am not sure why I am receiving so many errors given how straightfoward your instructions (with Makefile) were. The instructions I followed are via the link below. Do you know if the current SWAT release (rev 637) works with these instructions and Makefile? I am using the MinGW and MSYS method in your instructions.

        Instructions I followed:

        https://docs.google.com/document/d/16Do2U1_v4mZZBOV0hmcs6Gh1UvAUAXOMidpB-SE203A/edit?pli=1#heading=h.vai8uo4ma0x2

        Thank you,
        Steve

      • Hi Steve,

        I don’t think rev 637 would have big difference apart from previous versions. It would be helpful if you could send the error messages. You may also try the methods I have suggested in last reply.

        Thanks
        Zhiqiang

      • Zhiqiang,

        I tried compiling SWAT using your Makefile after removing Strawberry Perl (I think there were some problems with multiple shared files). The debug32 version compiled okay after I added ${LONGFIX} to the Makefile lines to compile ndenit.f, nminrl.f, and readbsn.f. However, the rel32 version (which is faster and what I want) failed to compile. When I tried compiling, I received the output below (stdout, stderr) and got two pop up windows “There is no disk in the drive. Please insert a disk into drive \Device\Harddisk5\DR5” ??

        stdout and stderr:

        mkdir -p rel32
        gfortran -m32 -c -fmessage-length=0 -funderscoring -fbacktrace -ffpe-trap=invalid,zero,overflow -O3 -ffixed-line-length-132 ndenit.f -o rel32/ndenit.o -I rel32
        ndenit.f:4.9:

        use parm
        1
        Fatal Error: Cannot read module file ‘parm.mod’ opened at (1), because it was created by a different version of GNU Fortran
        Makefile:2187: recipe for target ‘rel32/ndenit.o’ failed
        mingw32-make: *** [rel32/ndenit.o] Error 1

        Do you think there is a problem with the “parm.mod” module? I feel like I am getting close here.

        Thanks,
        Steve

      • Hi Zhiqiang,

        That was a good suggestion. I deleted the “rel32” folder and tried recompiling. The parm.mod problem seemed to go away, but then every time gfortan was invoked in a compile statement, an “insert disk into drive” error window popped up. I didn’t want to close 300 windows, so I forced quit the make process. The problem may be that I’m working over a Remote Desktop connection, and so maybe one of my system drives is mapped? Not sure. I’ll try this again next time I’m in the office and report back.

        Thanks,
        Steve

      • Zhiqiang,

        Okay, I tried again to compile SWAT using the Makefile with MinGW make (mingw32-make). Although I nolonger receive compile errors, I am prevented from compiling because I get a pop-up window instructing me to insert a disk into my CD drive every time gfortran.exe or f951.exe are called during the make process. This window has to be closed before the compile process can continue, therefore I am unable to compile. Here are the error messages:

        Text in pop-up window opened from call to gfortran.exe:

        gfortran.exe – No Disk
        There is no disk in the drive. Please insert a disk into drive \Device\Harddisk5\DR5

        Text in pop-up window opened from call to f951.exe:

        f951.exe – No Disk
        There is no disk in the drive. Please insert a disk into drive \Device\Harddisk5\DR5

        What a strange error. Do you know of a workaround?

        Thanks,
        Steve

      • Hi Steve,

        It’s quite interesting. Where do you put your source files? Have you tried to run the gfortran in command window? You could write a simple Fortran program and compile it with gfortran to see what will happen. If you still get the same error message, it’s something related to the gfortran. If not, it should be related to the location of the source files.

        Thanks
        Zhiqiang

      • Hi Zhiqiang,

        I am running gfortran from a command window. I just compiled Hello World successfully. My source code is located in folder: D:\Steven_Jepsen\Hydrologic_models\SWAT\source_code_SWAT2012\rev637_source

        I reinstalled MGWIN this morning. I noticed three errors in the installation process relating to a gcc folder. Sorry, I did not write the errors down (I knew I should have, but still didn’t).

        I am going to shelve this (at least for a while) until I hear of a fix. Compiling SWAT is not a necessity at the present time.

        Thanks,
        Steve

  3. Dear sir

    I have found difficulty in downloading MinGW-w64
    I am in Nepal and i have used both the links above, the installation however is complete but

    mingw32 is missing in
    C:\Program Files (x86)\mingw-builds\x32-4.8.1-win32-dwarf-rev5\.

    My aim of compiling the swat codes is to print snowpack and sublimation which it computes
    but doesnot print ( it has commented the codes and wbl,out isnot printed)

    Thanks for your great blog.

    Regards
    Shiva
    Research assistant

    • Shiva,

      I am not sure if this is any help, but I have obtained SWE output by enabling print of the “SNO_HRU” variable in the HRU output. Also, I recall going through this HRU output and noticing that snow sublimation appeared to equal ET when snowcover is present. But perhaps this does not hold for all conditions (?), and would probably be good to doublecheck.

      Steve

  4. Dear zhiqiangyu

    I am new to this compiling. I dont really have a good knowledge on it.
    Actually i faced the same problem Soham Adla faced at the start of this thread.

    However,i have installed Rtools and followed as mentioned in the your doc file in the blog(comments).
    I tried it but it didnot work for me.
    I couldnot download C:\Program Files (x86)\mingw-builds\x32-4.8.1-win32-dwarf-rev5 successfully. Thats the only thing i missed in your doc.

    Dear Steve,
    I am not sure where you are getting this output.
    If you are talking about Sno_mm in the output.hru, thats not the snowpack and also in the documentation it
    says not accumulated or something like that.

    Regarding sublimation , can you please clarify how you noticed snow sublimation is equal to ET.
    I dont know how it is possible cause i think there is always ET from soil and plants added to ET that SWAT prints.

    Regards
    Shiva

    • Hi Shiva,

      sno_hru in the hru output is definitely the accumulated water equivalent of the snowpack. I found this by examining precip, snowfall, and sno_hru in the hru output. That is the only way I can make sense of what the output really contains. I could not understand the description of sno_hru clearly enough in the I/O documentation (the document has some rough spots).

      Regarding ET, you are probably correct. I noticed that for one of my HRUs with bare land conditions, ET was equal to snow sublimation (again I could tell this from the hru output). However, in general plant transpiration would likely change this.

      This equation should work to solve for snow sublimation from the HRU output:

      sublimation = delta(sno_hru) – snowfall + snomelt

      Steve

      • Hi Guys,

        Nice discussion here.

        For me, sno_hru is the SWE of each HRU.

        Sublimation is part of ET. It will be less than or equal to ET depending on conditions. For winter, the plan is not growing, the plant part will be zero, which would lead to ET = Sublimation.

        Sublimation is calculated with other equations. The it’s could be check using the equation given by Steve.

        Thanks
        Zhiqiang

  5. Dear Michael

    I amnot being able to instal the compiler properly..
    I couldnot download 32-4.8.1-2win32-dwarf-rev5 .
    Is there anyother way, i could sort this issues.

    2.in my swat model, when i used the outputs from output.sub
    and calculated effective yield at the basin outlet by sumproducting the yield and sub basin area and dividing it with total area, and the flow at the outlet from output.rch ,there is a discrepancy.

    Waterbalance has been a key issue in my swat model.
    Can you guide me the way to check waterbalance at HRU and subbasin level and correct this issue?

    Thanks for your kind attention

    Regards
    Shiva

    • Hi Shiva,

      What happened when you were trying to download MinGW-w64? I’m not quite sure why it’s like that. Have you try RTools?

      For water balance, you calculation didn’t consider the loss in the channel routing. It’s normal they are different. I’m not sure why you want to check the water balance as it’s maintained very well in different level. I don’t think we need to to check that. Do you have any special concern?

      Thanks
      Zhiqiang

  6. Hi Michael

    I have tried to compile swat code following your instruction but I have faced some problems.
    First, why in problem tab has type of error or warning in “C/C++ problem”.
    Second, I haven’t seen the “Fortran Local Application” in Run configurations.
    Third, I copied swat_debug32.exe and ran in txtinout of SWAT project I got an error: “Program received signal 8 (sigfpe) floating-point exception.”
    I installed jre-8u111-windows-x64:
    eclipse-parallel-neon-R-win32-x86_64: C:\eclipse
    Rtools31: C:\Rtools
    mingw-w64-install: C:\Program Files (x86)\mingw-w64\i686-6.2.0-win32-dwarf-rt_v5-rev1; C:\Program Files (x86)\mingw-builds\i686-4.9.1-release-win32-dwarf-rt_v3-rev2
    mingw-get-setup: C:\MinGW\bin
    Can you please tell me ways to solve these problems?

    Thank you,
    Tu Le.

  7. Hi Micheal,
    I am calibrating a basin in Nepal.I wanted to check my water balance considering snowfall. How would I do that in Swat. Also swat check shows my surface run off is low and water yield may be excessive. I have tried solving this problem but while i tried solving this issue my Nash-Sutcliffe and R2 decreases. What should i do with this issue.
    Precipitation 525.4mm
    Snow fall 114.87mm
    Snow melt 54.93mm
    Sublimation 27.84mm
    Surface Run off 2.63mm
    Lateral Soil 150.79mm
    Ground Water (Shallow AQ) 180.78mm
    REVAP 23.87mm
    Deep AQ recharge 96.09m
    Total AQ recharge 300.93mm
    Total water yield 334.20mm
    Percolation out of soil 300.93mm
    Actual evapotranspiration 101.3mm
    Potential evapotranspiration 265.2mm
    Transmission losses 0mm

    • Hi Ameya,

      The water balance of snow pack should be checked separately with new snow as gain and snow melt and sublimation as loss. These three are in the summary result but the snow accumulation is not. You need that number to check the balance.

      The runoff is quite low and groundwater water contributes a lot more. Consider to check the soil parameters and CN numbers.

      Best,
      Michael

      • Thank you michael for your reply.
        My CN number is 17 which is low if i increase the cn and other soil parameters my NSE and R2 decreases.
        So should i just leave the model like this. Also while checking water balance
        is this formula correct.
        Water Yield=Qsuf + Qgw + Qlat- Qloss

        Precipitation=AET+ water yield+ Deep AQ recharge

      • Hi Ameya,

        For me, the CN is too low. Reviewers will have questions.

        I think water yield should equal to Qsurf + Qgw + Qlat. Precipitation should also consider the snow accumulation and the change of soil moisture.

        I don’t think you need to check the water balance. The SWAT model has check all the water balances.

        Best,
        Michael

  8. With this value my observed and simulated discharge is okay but if i increase the cn value and soil parameters the error parameters increases. I just want to check the water balance. Can you please tell me the process to check it.

    • Thank you micheal for your advice. My sub basin consists of mostly forest area. This maybe one of the reason for low CN value. My CN value are in the range of 30 but swat check shows average cn as 17. Is there any reason why is it that so.

  9. Thank you micheal for your advice. My sub basin consists of mostly forest area. This maybe one of the reason for low CN value. My CN value are in the range of 30 but swat check shows average cn as 17. Is there any reason why is it that so.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s