Suggestions on Compiling MODFLOW-2000

Top  Previous  Next  Print

Most users will not need to compile MODFLOW-2000 because you can download a compiled version of MODFLOW-2000 from the USGS web site.  However, if you wish to make changes to the source code, you will need to compile it yourself.

Nearly all the Fortran code in MODFLOW is standard Fortran that will compile the same way with any compiler.  However, a small portion of the code uses compiler-dependent code.  This compiler dependent code is included in the file "openspec.inc".  Be sure to modify this file to the correct form for your compiler.  There are extensive comments in openspec.inc that can help you.

Prior to version 1.15.00, MODFLOW-2000 was written entirely in Fortran.  However, the GMG package added in version 1.15.00 is written primarily in C (with a small amount of Fortran code to link to the C code).  This makes compiling MODFLOW-2000 a bit more challenging than previously. Now, instead of just a Fortran compiler, a C compiler is also required. If one is not aware that the C language is case-sensitive and  the Fortran language is case insensitive, some error messages returned by compilers may be difficult to understand. Often the C compiler will "decorate" the name of the C function and the linker can't figure out how to link Fortran object code with an undecorated name with C object code with a decorated name. Users should try to find the switches in the compilers and linkers that control how names are decorated so that the object code generated by the C and Fortran compilers use consistent names.

This web page gives advice on compiling MODFLOW-2000 with some compilers.  However, no attempt has been made to test all Fortran compilers.  If you have information on how to compile it with another compiler, please send to rbwinst@usgs.gov so it can be included on this web page.

If you wish to compile a parallel version of MODFLOW-2000, you will need to get a copy of mpif.h.  There is more info in the FAQ.

References to commercial vendors of software products or services are provided solely for the convenience of users when obtaining or using USGS software. Such references do not imply any endorsement by the U.S. Government.

The information on this web page was provided with the assistance of Edward Banta, Steffan Mehl, Henk den Adel, William R. Hutchison, and Richard Winston.  The instructions may be outdated or incorrect.  If you find that the instructions are in error, please send corrections to rbwinst@usgs.gov.

hmtoggle_plus1Lahey-Fujitsu Fortran 95  (LF95)
1.Make sure that you have installed the Fujitsu C compiler that comes with LF95.  The compiler is named fcc.exe.  It should be in the "bin" directory under the directory in which LF95 is installed.  If you have not installed the Fujitsu C compiler, install it from the same CD (or other media) on which you received LF95.  After installing it, be sure to also install the most recent patch file for your version of LF95 from the Lahey web site.
2.Download MODFLOW-2000.
3.Extract all the source code being sure to retain the directory structure inside the compressed download file.
4.Download automake.fig and place it in the same directory as the source code (for example C:\Wrdapp\mf2k.1_17\src\mf2k).
5.In the directory with the MODFLOW-2000 source code, create a subdirectory named "objLF95" (for example C:\Wrdapp\mf2k.1_17\src\mf2k\objLF95).
6.In the mf2k src directory create a subdirectory named "bin" (for example C:\Wrdapp\mf2k.1_17\src\bin).
7.Edit the line in Automake.fig that specifies the location of the include files of the Fujitsu C compiler to use the correct path for your system.  That line is

 

    COMPILE=@fcc /c /D _UF /I C:\Compilers\Lahey\LF9557\Include /o .\objLF95\%sf%oe %fi

 

8.You can run AutoMake to compile MODFLOW-2000 either by running the file AM.bat (which is distributed with LF95) from the command prompt or by doing the following.
9.Start the Lahey ED Developer.
10.Open any of the source code files for MODFLOW-2000.
11.Click on the Automake button.

AutomakeButton

hmtoggle_plus1Compaq Digital Visual Fortran
1.Download MODFLOW-2000.
2.Extract all the source code being sure to retain the directory structure inside the compressed download file.
3.Copy parallel.inc from the mf2k.1_17\src\mf2k directory to the mf2k.1_17\src\mf2k\serial directory.
4.Open Solvers.C in a text editor, and change all copies of "resprint" to "RESPRINT". (lines 52, 173, 201, and 234)
5.Make sure you have Visual Studio installed. It comes with Compaq Digital Visual Fortran.
6.Make sure you have Microsoft Visual C++ installed.  You might need to purchase Microsoft Visual C++ separately.
7.Start Visual Studio.
8.Select File|New and create a new Project that is a Fortran Console Application.  A good location for the project would be in C:\MF2K.1_17\SRC which Visual studio will change to C:\MF2K.1_17\SRC\mf2k.  After you click the OK button, choose "An empty project" (the default) and click on the Finish button.  Then click OK to close the "New Project Information" dialog box.

CompaqNewProject

9.Change to "File View" and right click on "mf2k files". Select Add Files to Project.

CompaqAddNewFiles

10.Select all the files with the extensions .c and .h in the \src\mf2k directory.
11.Right click on "mf2k files" again and select Add Files to Project.  This time, change the file type to Fortran Source files and add all the Fortran source files in the \src\mf2k directory.
12.Right click on "mf2k files" again and select Add Files to Project.  Change the file type to Fortran Source files and add para-non.f  in the \src\mf2k\serial directory.
13.On line 398 of gmg1.f, add a “c” in column one in order to comment out the “_resprint” reference:

c      !DEC$ ATTRIBUTES ALIAS:'_resprint' :: RESPRINT

14.Select "Project|Settings" and on the C/C++ tab change the "Category" to "Code Generation" and then change the "Calling Convention" to "__stdcall".

CompaqProjectSettingStdcall

14.Change to the Link tab and add "DFOR.LIB" to the end of the list of  Object/libray modules.

CompaqProjectSettingDfor

15.Click the OK button to close the Project Settings dialog box.  Then select Build|Build mf2k.exe
hmtoggle_plus1Microsoft Visual Studio .NET 2003 and the Intel Fortran Compiler 8.1.

Thanks to Ingo Berg for the following instructions.

1.First, extract the source code being sure to retain the directory structure inside the compressed download file.
2.Copy par-non.f from the mf2k.1_17\src\mf2k\serial directory to the mf2k.1_17\src\mf2k directory.
3.Open Solvers.h in a text editor. At the beginning of the file, comment out the following three lines (6,7,8) by placing a "//" in front of them like:

 

    //#ifdef _UF

    //#define resprint resprint_

    //#endif

 

    Behind this block write the following line:

 

    #define resprint RESPRINT

 

4.Open Microsoft Visual Studio .NET and create a new project using the "File | New | Project menu". Select Intel(R) Fortran Projects on the left side of the window and choose "Console Application" on the right side. Name the project mf2k_net and select ...\mf2k.1_17\src\mf2k as the project location. The IDE will create a new directory with that name to store the project.

 

pic1

 

Press OK. A Window with the application wizard will open. Go to the application settings and select "Empty Project". Press finish afterwards.

 

pic2

 

5.You should now see the Microsoft .NET IDE with the project explorer treeview on the right side. In the File menu select "File | Add existing element". A File dialog box will appear. This dialog box shows the content of the newly created "mf2k_net" directory, which does not contain any Fortran files yet. Go to the parent directory. (Press the small yellow symbol with the buckled arrow pointing upwards on top of the window). You should now see the content of the "mf2k" directory which consists of the Fortran files. Select all of them. In order to select all of the files, hold the shift key down and select the first and the last file. After doing this press "open". Now you should see the files added to the project explorer treeview.

 

pic3

 

 

6.Now we have a project with the fortran source code files. The part that is missing are the C-source code files. These will be added next. Since It is not possible to mix Fortran and c code within the same project, we have to create another project containing the c files. From the main menu, select "File | New | Project". From the "Visual C++ Projects" node on the left side, select "Win32". From the right side, select "Win32-Project". Assign "mf2k_gmg" as the project name and assign ...\mf2k.1_17\src\mf2k as the project path. Make sure you have the "Add to project" radiobutton active; otherwise, the IDE would close the previousely created Fortran project.

 

pic4

 

7.Press OK. The Project C++ project wizard will open. Go to the application settings and select "Static library". Uncheck the "Precompiled headers" option.

 

pic5

 

Press the "Create" Button. You will see a second project appear in the Project explorer treeview. This project is the static library for the GMG components which are written in C. Next we have to add the source files. In the Project explorer select the "mf2k_gmg" project. From the main menu, select "File | Add existing element". In the file input dialog box, go to the parent directory. (Press the small yellow symbol with the buckled arrow pointing upwards on top of the window.) Select all files with the extension *.c and *.h. (Do not include the *.inc files!). Again, multiple files can be selected by holding the shift key down. Press open; the new files will appear in the Project explorer.

8.The next step is telling the IDE to link the gmg library file to the main program. For this reason first compile the "mf2k_gmg" project in order to create a library file. Select the project; press the right mouse button; and select the "build" menu entry. You should see the library compile. If successful, a file named "mf2k_gmg.lib" should be created in either the "mf2k\mf2k_gmg\Debug" or the "mf2k\mf2k_gmg\Release" directory. The location depends on whether "debug" or "release" is selected as the build option in your IDE. For this test, the file is assumed to be located in the "Debug" directory.
9.In the "Project Explorer" treeview, select the "Ressource Files" node from "mf2k_net" project. This is the place where we are going to add the gmg lib file. Press the right mouse button and select "Add | Existing Element". A file selector will open. Go to the "mf2k_gmg/Debug directory". In the File types selector at the bottom of the window, change the filter to *.*. Select the file "mf2k_gmg.lib" and press open. The gmg lib has now been added to the main program.

 

pic6

 

10.Finally select the "mf2k_net" Project and build it. The output file will be created either as "mf2k.1_17\src\mf2k\mf2k_net\Debug}mf2k_net" or as "mf2k.1_17\src\mf2k\mf2k_net\Debug}mf2k_net". If you wish, change the name of the output file in the Project settings or rename it manually to mf2k.
hmtoggle_plus1Microsoft Visual Studio C++ .NET 2003 and the Intel Fortran Compiler 9.0.

Thanks to Edward Banta for the following instructions.

1.First, extract the MODFLOW-2000 source code, being sure to retain the directory structure inside the compressed download file.
2.Copy para-non.f from the src\mf2k\serial folder to the src\mf2k folder.
3.Open Microsoft Visual Studio .NET 2003 and create a new solution using File … New … Blank Solution. Name the solution “MF2K_Solution” and for Location, browse to an existing folder; it would be convenient to use the folder that is the parent of the src folder of the MODFLOW-2000 distribution, but the location can be anywhere.  The new solution will be in a subfolder that will be created under the selected folder.  Click OK.New Console Application - MF2K

VS2003_image001

4.Create a project in the “MF2K_Solution” solution you just made. To do this, in the Solution Explorer panel right click Solution ‘MF2K_Solution’ and select Add…New Project.  In the Add New Project dialog, click on “Intel Fortran Projects” in the left panel and ensure that “Console Application” is selected in the right panel.  Change the name to “mf2k” and click OK.
5.The Fortran Console Application wizard will open. Click on Application Settings and select "Empty Project".  Click Finish.Empty Project.

VS2003_image003

6.The next step is to add Fortran source code files to the mf2k project.  In the Solution Explorer panel under the “mf2k” project, right click “Source Files” and select Add…Add Existing Item.  Navigate to the src\mf2k folder of the MODFLOW-2000 distribution.  Ensure that the “Files of type” filter is “Common Intel Fortran files”.  Select all the *.f and *.f90 files (click on the first one and <Shift>-click on the last one).  Click Open.  Add more files to the “Source Files” list, but this time set the “Files of type” filter to “All Files”.  Select ground.com then use <Ctrl>-click to add hydmod.inc, lmt6.inc, openspec.inc, param.inc, params.inc, and startdaf.com to the selection.  Click Open.
7.Now we have a project with the Fortran source code files. The C source code will be added next. Since it is not possible to mix Fortran and C code within the same project, we have to create another project containing the C files.  In the Solution Explorer panel, right click Solution ‘MF2K_Solution’ and select Add…New Project.  In the Add New Project dialog, expand “Visual C++ Projects” in the left panel and select Win32.  In the right panel, select “Win32 Project” and enter “mf2k_gmg” as the name.  Click OK.  In the Win32 Application Wizard dialog, click “Application Settings” and then under Application type, select “static library” and clear the “Precompiled header” check box.  Click Finish.
8.You will see a second project appear in the Solution Explorer panel. This project will be used to create a static library for the GMG components that are written in C. Next we have to add the C source files. In the Solution Explorer panel, under the “mf2k_gmg” project, right click “Source Files” and select Add…Add Existing Item.  Navigate to the src\mf2k folder of the MODFLOW-2000 distribution and select all the *.c files (four of them).  In a similar manner, add the four *.h files in the src\mf2k folder under “Header Files” in the Solution Explorer panel.
9.The next step to tell the IDE to link the GMG library file to the mf2k project, but first the GMG static library needs to be compiled. In the drop-down list near the top of the Visual Studio window, change the configuration (which is not labeled as such) from “Debug” to “Release”. The next step is to compile the "mf2k_gmg" project in order to create a library file. Select the project; press the right mouse button; and select the "build" menu entry. You should see the library compile. If successful, a file named "mf2k_gmg.lib" should be created in the "MF2K_Solution\mf2k_gmg\Release" directory.
10.In the Solution Explorer panel, select "Resource Files" under the "mf2k" project. This is the place where we are going to add the GMG library file. Press the right mouse button and select Add … Add Existing Item". A file selector will open. Go to the "MF2K_Solution\mf2k_gmg\Release directory". In the File types selector at the bottom of the window, change the filter to All Files. Select the file "mf2k_gmg.lib" and press open. The GMG library has now been added to the solution.
11.Finally, select the "mf2k" project and build it. The output file will be created as mf2k.exe in the MF2K_Solution\mf2k\Release folder.  To save the solution, on the File menu click “Save All”.
12.The steps above generate a mixed precision executable file, like the one posted on the MODFLOW-2000 web page.  In some cases, it is advantageous to use a fully double precision version of MODFLOW-2000 to generate satisfactory results.  A fully double precision executable can be generated as follows.  Select the mf2k project in the Solution Explorer panel.  On the main menu bar select Project…Properties. In the mf2k Property Page dialog, select Fortran and under Fortran select Data.  Change the Default Real KIND from 4 to 8.  Click OK.  Again, build the mf2k project.  The resulting mf2k.exe program is now fully double-precision. You might want to rename it as “mf2k-dp.exe”.  Be aware that programs that read binary files produced by MODFLOW-2000 (e.g. Modpath, Zonebudget, ModelViewer) expect to read single-precision data, and they will be unable to read data from binary files produced by a double-precision version of MODFLOW-2000.  Those programs, however, could be similarly recompiled as fully double precision, which would enable them to read double precision binary files.
hmtoggle_plus1Microsoft Visual Studio 2005 with Intel Fortran Compiler 9.1

Thanks to Christian D Langevin and David Ahlfeld for the following instructions.

1.First, extract the source code being sure to retain the directory structure inside the compressed download file.
2.Copy par-non.f from the mf2k.1_17\src\mf2k\serial directory to the mf2k.1_17\src\mf2k directory.
3.Open Microsoft Visual Studio
4.File>New>Project: Make sure to select the settings according to the following window.  You can give the project a name, such as mf2k, and set the location to the location of the MODFLOW Fortran source files.  Then click OK.

VS2005_NewProject

Click Next to the "Welcome to the Fortran Console Application Wizard".

5.Click "Empty Project" under the "Application Settings" window, and then click Finish.

VS2005_ApplicationSettings

6.Now you need to add all of the Fortran files to the mf2k project that you just created.  You can do this by selecting Project>Add Existing Item...  Select all of the Fortran source files in the mf2k subdirectory and click Add.

VS2005_AddItems

7.Confirm that the following line is present gmg1.f after the line containing "SUBROUTINE RESPRINT":

 

          !DEC$ ATTRIBUTES ALIAS:'_resprint' :: RESPRINT

 

8.Next, you need to create a new project for the GMG solver because a project can contain only a single programming language.  Click File>New>Project.  Then select the settings as follows making sure to "Add to Solution".

VS2005_NewCProject

9.Now you should see that the gmg project has been added to the solution.  This will be shown in the Solution Explorer Window, which may be on the right-side of the screen.  To add the gmg C files to the gmg project, select Project>Add Existing Item... (make sure that the gmg project is selected in the solution window).  Add only the .c and .h files as follows and click Add:

VS2005_AddCFiles

10.Now select Project>Project Dependencies.  Then select the mf2k project and check the underlying box that says gmg as follows:

VS2005_ProjectDependencies

11.Now select the project properties for the gmg project and change the configuration type from .exe to a static library (.lib) as follows:

VS2005_ConfigurationType

12.Now, open the solvers.h file, which is a Header File under the gmg project.  As Ingo Berg mentioned in his description of compiling MODFLOW-2000 with Microsoft Visual Studio .NET 2003, you need make the following changes.  Comment out the following three lines by placing a // in front of them

 

//#ifdef_UF

//#define resprint resprint_

//#endif

 

(According the David Ahlfeld, MODFLOW will still compile if this instruction is omitted.)

 

13.After these commented lines add the following line:

 

#define resprint RESPRINT

 

(The original instructions from Christian D Langevin included the instruction above but according to David Ahlfeld they are not required and they cause a linker error.)

14.You are now ready to compile MODFLOW-2000, including the GMG solver.  Click Build>Build Solution.  Keep in mind that "debug" is the default configuration.  You may want to change to the "release" configuration and make adjustments to the optimization settings to improve efficiency.  If you have a problem with the "release" configuration, make sure that the GMG project is set to a "Static Library (.lib)".
hmtoggle_plus1GNU Fortran (gfortran)

Thanks to Henk den Adel for the following instructions.

The GNU version of the Fortran 95 compiler (gfortran, i.e. not the G95 project) allows one to compile the .f files and the .c files.

1.Comment out lines 5, 6 and 7 of solvers.h

 

    /* Append underscore to fortran modual name */

    //#ifdef _UF/

    /#define resprint resprint

    //#endif

 

2.  2. Change lines 11..17 of mf2kgmg.f into

 

    /* Append underscore to functions called from FORTRAN GMG interface */

    #ifdef _UF

    #define MF2KGMG_ALLOCATE   mf2kgmg_allocate

    #define MF2KGMG_FREE       mf2kgmg_free

    #define MF2KGMG_ASSEMBLE   mf2kgmg_assemble

    #define MF2KGMG_EVAL       mf2kgmg_eval

    #define MF2KGMG_UPDATE     mf2kgmg_update

    #define MF2KGMG_BIGR       mf2kgmg_bigr

    #define MF2KGMG_BIGH       mf2kgmg_bigh

    #endif

 

    Do not append the tailing underscore. The -fno-underscoring makes it unnecessary. However, functions are case sensitive for this compiler.

 

3.To compile the .f files use:

 

    gfortran -c -fno-underscoring file.f

 

4.To compile the .c files use:

 

    gfortran -c -D_UF -Ilocation file.c

 

    where 'location' points to the drive and directory where the include files for gfortran are located.

 

5.To link the object files into an executable use:

 

    gfortran *.o -o mf2k

 

Final remark: Henk den Adel says he did not check whether the program runs; however all errors due to 'undefined references' were gone.

 

Thanks to Jaime Carrera for the following additional suggestions regarding compiling with gfortran.

 

1.Modify the makefile file:

    F77FLAGS = -fno-second-underscore

    F77= gfortran

2.On the command line just type (under src/mf2k/)

    make install

 

Jaime Carrera says he tested it on Linux - Fedora Core 4.