Compiling SWAT+ with gfortran (Linux)

252 views
Skip to first unread message

Paulo Santiago

unread,
Oct 5, 2021, 10:23:51 PM10/5/21
to SWAT+
Dear SWAT+ Community,

I'm new to SWAT+ and I would like to get some advice on compiling it using Linux (Ubuntu) gfortran.

I found source code here and I'm compiling based on these instructions. 

After replacing ifort by gfortran with -ffree-line-length-512 option, everything goes ok until the following command:

gfortran -c *.f90 -ffree-line-length-512

At this point, I get error messages I don't know how to address. For example:

-----------------------------------------------------------------

hru_soiltest_update.f90:16:56:

   16 |           soil1(isol)%mn(ly)%no3 = solt_db(isolt)%inorgn * dep_frac
      |                                                        1
Error:inorgn’ at (1) is not a member of the ‘soiltest_db’ structure; did you mean ‘inorgp’?

-----------------------------------------------------------------

ru_allo.f90:48:12:

   48 |             uhs(isb,i) = (q + ql) / 2.
      |            1
Error: Function ‘uhs’ at (1) has no IMPLICIT type

-----------------------------------------------------------------

sim_inityr.f90:19:28:

   19 |       use hru_module, only : lai_yrmx
      |                            1
Error: Symbol ‘lai_yrmx’ referenced at (1) not found in module ‘hru_module

-----------------------------------------------------------------

The number of error messages is bigger. However, I think it's enough to exemplify.  

My best guess is there are some ifort library missing in gfotran which I don't know, so that I can't provide it. 

Could a more experienced user please guide me on this task?

Thanks in advance. 

Cheers,
Paulo Santiago

Kai-Uwe Eckhardt

unread,
Oct 6, 2021, 5:16:02 AM10/6/21
to SWAT+
Hello,

the source_codes directory contains several files that are no longer used and that have to be excluded from compilation. I use the following CMake file for compilation. With a traditional Makefile you have to exclude the seven *.f90 files proc_allo.f90, ru_allo.f90 etc.

SWAT+ contains several bugs and invalid Fortran requiring further patches though. I have posted the most important ones lately in this group but I still have to patch bugs to get a working executable (patches for 60.5.3 at the end) and this is only sufficient for our modell but there are further bugs in other code paths triggered by running the examples.

Good luck,

Kai-Uwe



cmake_minimum_required(VERSION 3.17)

project(swatplus)
enable_language(Fortran)

if(CMAKE_Fortran_COMPILER_ID MATCHES "GNU")
    #set(dialect "-ffree-line-length-none -Wall -Wno-tabs -O3 -march=nehalem -mfpmath=sse -msse4.2 -fopt-info -ffpe-trap=invalid,zero,overflow -finit-local-zero")
    set(dialect "-flto -ffree-line-length-none -Wall -Wno-tabs -O3 -march=nehalem -mfpmath=sse -msse4.2 -frounding-math -ffpe-trap=invalid,zero,overflow -ffpe-summary=none -static" )
    set(bounds "-fcheck=all ")
endif()


set(CMAKE_Fortran_FLAGS_DEBUG "${CMAKE_Fortran_FLAGS_DEBUG} ${bounds} -fbacktrace")
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} ${dialect}")

#
# Compile.
#
file(GLOB_RECURSE sources  source_codes/*.f90 source_codes/*.F90)
get_filename_component(exclude_file ${CMAKE_CURRENT_SOURCE_DIR}/source_codes/proc_allo.f90 ABSOLUTE)
list(REMOVE_ITEM sources ${exclude_file})
#message(${exclude_file})
get_filename_component(exclude_file ${CMAKE_CURRENT_SOURCE_DIR}/source_codes/ru_allo.f90 ABSOLUTE)
list(REMOVE_ITEM sources ${exclude_file})
get_filename_component(exclude_file ${CMAKE_CURRENT_SOURCE_DIR}/source_codes/sim_inityr.f90 ABSOLUTE)
list(REMOVE_ITEM sources ${exclude_file})
get_filename_component(exclude_file ${CMAKE_CURRENT_SOURCE_DIR}/source_codes/water_hru.f90 ABSOLUTE)
list(REMOVE_ITEM sources ${exclude_file})
get_filename_component(exclude_file ${CMAKE_CURRENT_SOURCE_DIR}/source_codes/hru_soiltest_update.f90 ABSOLUTE)
list(REMOVE_ITEM sources ${exclude_file})
get_filename_component(exclude_file ${CMAKE_CURRENT_SOURCE_DIR}/source_codes/soiltest_all_init.f90 ABSOLUTE)
list(REMOVE_ITEM sources ${exclude_file})
get_filename_component(exclude_file ${CMAKE_CURRENT_SOURCE_DIR}/source_codes/soiltest_init.f90 ABSOLUTE)
list(REMOVE_ITEM sources ${exclude_file})
add_executable(swatplus ${sources})



=======================================================
Patches:

diff --git a/source_codes/calibration_data_module.f90 b/source_codes/calibration_data_module.f90
index 855d703..a9893b6 100644
--- a/source_codes/calibration_data_module.f90
+++ b/source_codes/calibration_data_module.f90
@@ -128,7 +128,7 @@
             
       type soft_data_calib_landscape
         character(len=16) :: name = "default"                               !name of region - (number of regions = db_mx%lsu_reg)
-        integer :: lum_num                                                  !number of land uses in each region
+        integer :: lum_num = 0                                                 !number of land uses in each region
         integer :: num_tot                                                  !number of hru"s in each region
         integer, dimension(:), allocatable :: num                           !hru"s that are included in the region
         integer :: num_reg                                                  !number of regions the soft data applies to
@@ -152,7 +152,7 @@
 
       type pl_parm_region
         character(len=16) :: name = "default"                           !name of region - (number of regions = db_mx%lsu_reg)
-        integer :: lum_num                                              !number of land uses in each region
+        integer :: lum_num = 0                                             !number of land uses in each region
         integer :: parms = 2                                            !number of plant parameters used in calibration
         integer :: num_tot                                              !number of hru"s in each region
         integer, dimension(:), allocatable :: num                       !hru"s that are included in the region
@@ -260,7 +260,7 @@
       
       type soft_data_calib_plant
         character(len=16) :: name = "default"   !name of region - (number of regions = db_mx%lsu_reg)
-        integer :: lum_num                                                  !number of land uses in each region
+        integer :: lum_num = 0                                                  !number of land uses in each region
         integer :: num_tot                                                  !number of hru"s in each region
         integer, dimension(:), allocatable :: num                           !hru"s that are included in the region
         type (pl_calib_regions), dimension(:), allocatable :: lum           !dimension for land uses within a region
@@ -306,4 +306,4 @@
       end type soft_data_calib_channel
       type (soft_data_calib_channel), dimension(:), allocatable :: chcal  !dimension by region
                                  
-      end module calibration_data_module
\ No newline at end of file
+      end module calibration_data_module
diff --git a/source_codes/hyd_connect.f90 b/source_codes/hyd_connect.f90
index 7a62448..ddb5663 100644
--- a/source_codes/hyd_connect.f90
+++ b/source_codes/hyd_connect.f90
@@ -537,7 +537,7 @@
       
       !! write calculated and input drainage areas for all objects except hru's
       !! the following file is for debugging purposes
-      open (9002,file="drareas.out",recl = 1500)
+      open (9002,file="drareas.out",recl = 8000)
       do iob = 1, sp_ob%objs
         if (ob(iob)%typ /= "hru" .and. ob(iob)%typ /= "ru") then
           write (9002, *) iob, ob(iob)%typ, ob(iob)%num, ob(iob)%area_ha, ob(iob)%area_ha_calc,             &
@@ -551,4 +551,4 @@
 1002  format (5x,/,"ERROR - An infinite loop is detected in the connect file(s)",/, 15x, "the simulation will end",       &
                        /, 9x, "(review diagnostics.out file for more info)",/)
       return
-      end subroutine hyd_connect
\ No newline at end of file
+      end subroutine hyd_connect
diff --git a/source_codes/hydrograph_module.f90 b/source_codes/hydrograph_module.f90
index 7d25c41..95a9da2 100644
--- a/source_codes/hydrograph_module.f90
+++ b/source_codes/hydrograph_module.f90
@@ -225,7 +225,7 @@
         integer :: src_tot = 0          !total number of outgoing (source) objects
         integer :: rcv_tot = 0          !total number of incoming (receiving) hydrographs
         integer :: dfn_tot = 0          !total number of defining objects (ie hru"s within a subbasin)
-        integer :: ru_tot               !number of routing units that contain this object
+        integer :: ru_tot = 0               !number of routing units that contain this object
         integer,  dimension (:), allocatable :: ru                  !subbasin the element is in
         integer :: elem                 !subbasins element number for this object- used for routing over (can only have one)
         integer :: flood_ch_lnk = 0     !channel the landscape unit is linked to
@@ -1201,4 +1201,4 @@
 !        dr1%grv = const
 !      end function dr_constant
       
-      end module hydrograph_module
\ No newline at end of file
+      end module hydrograph_module

Nancy B. Sammons

unread,
Oct 6, 2021, 7:40:26 AM10/6/21
to Kai-Uwe Eckhardt, SWAT+, Jeff Arnold

Kai_Uwe: 

I am sorry you are having problems.  The extra (test routines) should have been deleted in the next commit of the SWAT+ code and hopefully are no longer a problem.  It seems like a windows-based compiler seems a bit more forgiving than Linux.  I would be interested to see a list of the other patches/changes you are having to make in the code to compile in Linux.    We will review these and perhaps incorporate them into our main code so this transition will be easier for you next time. 

Thank you, we appreciate your interest in our model.

Have a good day,

Nancy

--
You received this message because you are subscribed to the Google Groups "SWAT+" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swatplus+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/swatplus/878cb6b7-fe7b-4a51-95b8-35ac738f1415n%40googlegroups.com.

Kai-Uwe Eckhardt

unread,
Oct 6, 2021, 8:24:19 AM10/6/21
to SWAT+
Hello Nancy,

> It seems like a windows-based compiler seems a bit more forgiving than Linux

It's not a matter of Linux vs Window but rather a different behaviour with buggy code and even bugs in the compilers with complex fortran constructs. In fact I test on Linux and Windows and use several warning flags of compilers. Modern Fortran has become so complex that it is easy to make mistakes. For instance the 'Bug in soils_init.f90, ihru loop variable' was found by gfortran and  is not easily spotted by just reading the code. I intend to set up the Intel Compiler for Windows and Linux as documented lately in  https://swatplus.gitbook.io/docs/source-code  for further checks.

Thanks for all your work,

Kai-Uwe

Reply all
Reply to author
Forward
0 new messages