Compiling Fortran code for Windows

This post is a continuation of a recent series, using Fortran code published in  Programming the Finite Element Method (5th ed. John Wiley & Sons, I.M. Smith, D.V. Griffiths and L. Margetts (2014)).

The compiled file provided in the download should run on any Windows computer, but to edit the code in any way, recompilation is required.  The process is not difficult, but it is not well documented, at least for those not familiar with Fortran, so this post will provide a detailed description of the process, and some alternative options.

For this exercise I have used the MinGW package (Minimalist GNU for Windows), which includes the GFortran compiler.  MinGW may be installed from the Anaconda Python package.  It is not included in a default install of Anaconda, but it may be easily added with the command: conda install mingw.

Alternatively installation instructions for just MinGW are provided here.

The Fortran code to be compiled consists of the main program and 17 sub-routines.  To keep the compilation process as simple as possible the sub-routines can be included in the same text file as the main program.  The code using this approach in the original download included a block of  “interface” statements:

INTERFACE
!
SUBROUTINE getname(argv,nlen)
IMPLICIT NONE
INTEGER::narg
INTEGER,INTENT(OUT)::nlen
CHARACTER(*),INTENT(OUT)::argv
INTEGER::lnblnk,iargc
END SUBROUTINE getname
!
SUBROUTINE emb_2d_bc(nx1,nx2,ny1,ny2,nf)
IMPLICIT NONE
INTEGER,INTENT(IN)::nx1,nx2,ny1,ny2
INTEGER,INTENT(OUT)::nf(:,:)
END SUBROUTINE emb_2d_bc
...
15 more subroutines
...
END SUBROUTINE
END INTERFACE

This is not necessary however. The revised code is shown in outline below, and is included in full in the download file:

PROGRAM p64
!-------------------------------------------------------------------------
! Program 6.4 Plane strain slope stability analysis of an elastic-plastic
!             (Mohr-Coulomb) material using 8-node rectangular
!             quadrilaterals. Viscoplastic strain method.
!-------------------------------------------------------------------------
 IMPLICIT NONE
 !
 INTEGER,PARAMETER::iwp=SELECTED_REAL_KIND(15)
 INTEGER::i,iel,iters,iy,limit,ndim=2,ndof=16,nels,neq,nip=4,nlen,nn,     &
 ! ... Main Program
 ! ...
    END DO
!
!   IF(iters==limit)EXIT
END DO srf_trials
RETURN
!
contains
! 17 subroutines:
SUBROUTINE getname(argv,nlen)
!
! This subroutine reads the base name of data file.
!
 IMPLICIT NONE
 …
CASE DEFAULT
   WRITE(*,*)"nst size not recognised in formm"
 END SELECT
RETURN   
END SUBROUTINE formm
END PROGRAM p64

This code, in P64-1.f03, is compiled by opening a command window and entering:

PlaneStrain4-1

This generates the file P64.exe, which can be called from PlateMC-exe.xlsb, as described in the previous posts.  Note that the exe file created requires MinGW to be installed.  See below for the compilation option to generate a stand-alone file.

As a step towards separating out the subroutines into a library of general purpose routines, the subroutines can be defined as a separate module, within the same file:

PROGRAM p64
!-------------------------------------------------------------------------
! Program 6.4 Plane strain slope stability analysis of an elastic-plastic
!             (Mohr-Coulomb) material using 8-node rectangular
!             quadrilaterals. Viscoplastic strain method.
!-------------------------------------------------------------------------
 USE main 
!
 IMPLICIT NONE
 !
 INTEGER,PARAMETER::iwp=SELECTED_REAL_KIND(15)
 …
    END DO
!
!   IF(iters==limit)EXIT
END DO srf_trials
RETURN
END PROGRAM p64
!
module main
contains
SUBROUTINE getname(argv,nlen)
!
! This subroutine reads the base name of data file.
!
 IMPLICIT NONE
 …
 CASE DEFAULT
   WRITE(*,*)"nst size not recognised in formm"
 END SELECT
RETURN   
END SUBROUTINE formm
end module main

Note the changes:

  • Addition of the USE MAIN statement
  • The END PROGRAM P64 is moved to the end of the top level code
  • The statement MODULE MAIN is added at the top of the subroutine code, and END MODULE MAIN at the end.

This code, saved as P64-2.f03, is compiled in the same format as before.

The next stage is to extract all the code from MODULE MAIN to END MODULE MAIN into a separate file, named main.f03.  This is then compiled in two stages as shown below:

PlaneStrain4-2

The command: gfortran -c main.f03 creates the file main.o, which is then compiled with p64-3.f03 to create P64.exe.

The final stage is to compile to a stand alone executable file, with the “-static” statement:

PlaneStrain4-3

The download file includes all the code files described above, as well as the stand alone executable file, the spreadsheet, and example results files.

This entry was posted in Excel, Finite Element Analysis, Fortran, Newton, VBA and tagged , , , , , , , . Bookmark the permalink.

2 Responses to Compiling Fortran code for Windows

  1. bigmarck says:

    I like a lot your posts

    On Mon, Jul 4, 2016 at 2:58 PM, Newton Excel Bach, not (just) an Excel Blog wrote:

    > dougaj4 posted: “This post is a continuation of a recent series, using > Fortran code published in Programming the Finite Element Method (5th ed. > John Wiley & Sons, I.M. Smith, D.V. Griffiths and L. Margetts (2014)). The > compiled file provided in the download should” >

    Like

  2. Pingback: Linking Excel to Fortran, via xlwings, Python and ctypes | Newton Excel Bach, not (just) an Excel Blog

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 )

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.