Installation/Windows/Cross-Compiling OpenFOAM 2.1.x in Linux For Windows with MinGW

From OpenFOAMWiki

Valid versions: OF Version 21x.png

1 Introduction

To Do

Note: Support thread for these instructions is available here: Windows (x64) Binaries for OpenFOAM-2.1.x using OpenMPI

2 Packages Required to Cross-Compile OpenFOAM

This article was written based on work done on ArchLinux. It has not been tested on any other distribution.

On ArchLinux MinGW-w64 is not available in any of the official repos and have to be compiled from AUR. For those who do not work on Arch but would like to see the option used to build the packages go to [1] and search of the package name. Selecting the package in the result and ckick on "View PKGBUILD". Here you can see all the options, the dependencies and the build options.

On Arch the aim is to get the c,c++ cross-compilers and zlib by building and installing mingw-w64-gcc[2]. To achieve this the following will have to be build and installed in the following order.

  1. mingw-w64-binutils
  2. mingw-w64-headers
  3. mingw-w64-headers-bootstrap
  4. mingw-w64-gcc-base
  5. mingw-w64-crt
  6. mingw-w64-winpthreads (replaces mingw-w64-headers-bootstrap)
  7. mingw-w64-gcc (replaces mingw-w64-gcc-base)
  8. mingw-w64-zlib

3 Preparing To Cross-Compile

From here on it is assumed that the cross compilation is done in a folder, say for example `CrossCompiledOF'.

This path will be referred to as FOAM_INST_DIR, and its best to set it as an environment variable to enable copy-past-execute commands with little modification.

export FOAM_INST_DIR=/path/to/CrossCompiledOF

3.1 Preparing OpenMPI

Install 64-bit OpenMPI on your Windows machine. It is also recommended that you install OpenMPI on your Linux machine used to cross-compile, this article is written with this assumption.

The installer for Windows is shown here (http://www.open-mpi.org/software/ompi/v1.6/downloads/OpenMPI_v1.6.2-2_win64.exe).

On your Windows machine copy the OpenMPI folder to a place where you can access it on your Linux Machine, like a USB drive, shared folder on VMs etc. This folder is located in "Program Files" or "Program Files (x86)" depending where you chose to install it (while installation the default is "Program Files (x86)"). Rename this folder as MSOPENMPI.

Copy this folder to your cross-compiling folder on linux.

 cp -vr /path/to/MSOPENMPI $FOAM_INST_DIR/.

3.2 Getting The Source

There are two options to get the source prepared:

  1. Clone the official repository, checkout the version used to generate the Symscape patch and patch the source
  2. Clone the repository maintained by J-Light at aims to keep up the official repository in terms of bug fixes.

The second may not always be the best option. After updating the code the only criteria used to push the source is if it compiles without any errors. If the first one is enough then go ahead and use it. However, if you need source from a more recent commit then use the repo maintained by J-Light.

To start off go to the build directory.

cd $FOAM_INST_DIR

3.2.1 Source: Official + Symscape Patch

  1. Clone the official repo using git
    git clone https://github.com/OpenFOAM/OpenFOAM-2.1.x.git
    git clone https://github.com/OpenFOAM/ThirdParty-2.1.x.git
  2. Get and extract the Symscape patch
    wget http://www.symscape.com/files/articles/openfoam21x-windows/v7-mingw-openfoam-2-1-x.patch.gz
    gzip -d v7-mingw-openfoam-2-1-x.patch.gz
  3. Checkout the correct version of OpenFOAM
    cd OpenFOAM-2.1.x
    git checkout -b Symscape 971882b657c1730e27a8df8dcf06e6d5e5a47991
  4. Check and apply patch
    git apply --check -p0 ../v7-mingw-openfoam-2-1-x.patch
    git apply -p0 ../v7-mingw-openfoam-2-1-x.patch

3.2.2 Source: Official + Symscape Patch + JLight updates

  1. Clone the required repos
    git clone -b Win64 git://github.com/J-Light/OpenFOAM-2.1.x.git
    git clone https://github.com/OpenFOAM/ThirdParty-2.1.x.git

3.3 Build Wmake

First we need to build wmake. To do so edit etc/bashrc. ( I use 'nano' to edit, you can change this to which ever you like)

nano $FOAM_INST_DIR/etc/bashrc

The following need to be edited

  1. Edit line foamInstall (line 45) such that
    foamInstall=/path/to/CrossCompiledOF
  2. Edit WM_MPLIB (line 84) to use the system openmpi.
    export WM_MPLIB=SYSTEMOPENMPI

Source the file

source $FOAM_INST_DIR/etc/bashrc

Build wmake

cd $WM_PROJECT_DIR/wmake/src
make

Symlink the folder containing the binary to the folder that will be searched during cross-compiling

cd $WM_PROJECT_DIR/wmake/platforms
ln -s linux64Gcc linux64mingw-w64

3.4 Getting Scotch

The ThirdParty-2.1.x folder only contains build scripts. The files for Scotch Library needs to be downloaded first. This can be done by

cd $WM_THIRD_PARTY_DIR
wget https://gforge.inria.fr/frs/download.php/28043/scotch_5.1.11.tar.gz
tar -xf scotch_5.1.11.tar.gz
ln -s scotch_5.1.11 scotch

4 Building The Scotch Library

Building Scotch is not straight forward. A bit of effort need to go into this. A tip for faster build time set WM_NCOMPPROCS=K, where K=No of cpus+1.

export WM_NCOMPPROCS=5
  1. The first step is a bit well...odd, one can even say it is redundant (is a good chance it is, I do it because I needed to check for other issues), however, I do not recommend skipping it. Build scotch like you would compile OpenFOAM in Linux.
    cd $WM_THIRD_PARTY_DIR
    ./Allwmake
  2. If Scotch build without any errors then all's good. If errors were reported, its most likely that openmpi was not correctly installed on your Linux Machine. If it built without errors go on and build the libscotch.
    cp -vrf $WM_PROJECT_DIR/extra/scotch/src/Makefile .
    cp -vrf $WM_PROJECT_DIR/extra/scotch/src/libscotch/* $WM_THIRD_PARTY_DIR/scotch/src/libscotch/.
    cd $WM_THIRD_PARTY_DIR/scotch/src/libscotch
    make -j$WM_NCOMPPROCS
  3. It should build without errors. If it did, then check first that an executable called ‘dummysize’ is present. If yes, then there is nothing to worry. All this steps so far in this section was necessary to just get ‘dummysize’. Go a head and remove the object files (do not use make clean!).
    cd $WM_THIRD_PARTY_DIR/scotch/src/libscotch
    rm -rvf *.o
  4. Cross compile the library
    cd $WM_THIRD_PARTY_DIR/scotch/src
    rm -rf Makefile.inc
    ln -s $WM_PROJECT_DIR/extra/scotch/src/Make.inc/Makefile.inc.mingw-w64 $WM_THIRD_PARTY_DIR/scotch/src/Makefile.inc
    make -j$WM_NCOMPPROCS libscotch
  5. Copy the binaries to OpenFOAM's binary folder.
    mkdir -p $FOAM_LIBBIN
    cd $WM_THIRD_PARTY_DIR/scotch/src/libscotch
    cp -vr libscotch.* $FOAM_LIBBIN/.

5 Building OpenFOAM

It should be simple here on.

  1. Export the following enviroment variables
    export WM_OSTYPE=MSwindows
    export WM_COMPILER=mingw-w64
    export WM_CC=x86_64-w64-mingw32-gcc
    export WM_CXX=x86_64-w64-mingw32-g++
    export MPI_ARCH_PATH=$FOAM_INST_DIR/MSOPENMPI
    export FOAM_MPI=openmpi
  2. This step is necessary only if you are using 'Source: Official + Symscape Patch'. The patch creates a few Allwmake files that do not have permission to execute. The following fixes it:
    cd $WM_PROJECT_DIR
    find . -name Allwmake -exec chmod +x '{}' \;
  3. The final step in the build process. (Good luck!)
    cd $WM_PROJECT_DIR
    ./Allwmake

Hopefully there were no errors.

6 Preparing the move to Windows

This step is based on the work and scripts (copied in verbatim in some cases) to Bruno Santos (wyldckat).

  1. Copy the binary files to a location that can be accessed in Windows (USB drive, Shared Folder in VMs, etc)
    export OF_DEST_FOLD=/path/to/location
    cd $WM_PROJECT_DIR
    mkdir -p $OF_DEST_FOLD/OpenFOAM-2.1.x
    cp -r platforms/linux64GccDPOpt/bin $OF_DEST_FOLD/OpenFOAM-2.1.x/.
    mkdir -p $OF_DEST_FOLD/OpenFOAM-2.1.x/lib
    mkdir -p $OF_DEST_FOLD/OpenFOAM-2.1.x/lib/dymmy
    mkdir -p $OF_DEST_FOLD/OpenFOAM-2.1.x/lib/gpuless
    mkdir -p $OF_DEST_FOLD/OpenFOAM-2.1.x/lib/openmpi
    cp -r platforms/linux64GccDPOpt/lib/*.dll $OF_DEST_FOLD/OpenFOAM-2.1.x/lib/.
    cp -r platforms/linux64GccDPOpt/lib/dymmy/*.dll $OF_DEST_FOLD/OpenFOAM-2.1.x/lib/dymmy/.
    cp -r platforms/linux64GccDPOpt/lib/gpuless/*.dll $OF_DEST_FOLD/OpenFOAM-2.1.x/lib/gpuless/.
    cp -r platforms/linux64GccDPOpt/lib/openmpi/*.dll $OF_DEST_FOLD/OpenFOAM-2.1.x/lib/openmpi/.
  2. Copy the folder OpenFOAM-2.1.x to a location of your choice in Windows E.g. Copy OpenFOAM-2.1.x into c:\OpenFOAM
  3. Open notepad enter the following and while saving choose 'All Files' in the "Save as type" drop down list it enter the name DOS_MOD.bat. Save this file in folder you copied OpenFOAM-2.1.x (in example it is c:\OpenFOAM)
    @echo off
    %COMSPEC% /k "%~dp0\setvars.bat" %1 %2 %3 %4 %5 %6 %7 %8 %9
  4. Similarly create setvars.bat containing the following
    @echo off
     
    %~d0
    cd %~dp0
    set HOME=%~dps0
    set HOME=%HOME:~0,-1%
     
    rem quick jump if just one available
     
    :ww64
    set WM_COMPILER=mingw-w64
    set WM_PRECISION_OPTION=DP
    set WM_ARCH_OPTION=64
    echo "Setting environment for mingw-w64 Double Precision (custom mingw-w64)"
    set WM_COMPILER=x86_64-w64-mingw32
    set WM_MPLIB=OPENMPI
    set WM_PRECISION_OPTION=DP
    set USER=ofuser
    set USERNAME=ofuser
    set WM_PROJECT=OpenFOAM
    set WM_PROJECT_VERSION=2.1.x
    set FOAM_INST_DIR=%HOME%
    set WM_PROJECT_INST_DIR=%FOAM_INST_DIR%
    set WM_PROJECT_DIR=%FOAM_INST_DIR%\%WM_PROJECT%-%WM_PROJECT_VERSION%
    set WM_PROJECT_USER_DIR=%HOME%\%USER%-%WM_PROJECT_VERSION%
    set WM_THIRD_PARTY_DIR=%WM_PROJECT_INST_DIR%\ThirdParty-%WM_PROJECT_VERSION%
    set WM_OS=MSwindows
    set WM_ARCH=linux
    set WM_COMPILE_OPTION=Opt
    set WM_JAVAC_OPTION=Opt
    set FOAM_SIGFPE=""
    set WM_COMPILER_ARCH=""
    set WM_COMPILER_LIB_ARCH=""
    set WM_CC=%WM_COMPILER%-gcc
    set WM_CXX=%WM_COMPILER%-g++
    set FOAM_JOB_DIR=%FOAM_INST_DIR%\jobControl
    set WM_DIR=%WM_PROJECT_DIR%\wmake
    set WM_LINK_LANGUAGE=c++
    set WM_OPTIONS=%WM_ARCH%%WM_COMPILER%%WM_PRECISION_OPTION%%WM_COMPILE_OPTION%
    set FOAM_SRC=%WM_PROJECT_DIR%\src
    set FOAM_LIB=%WM_PROJECT_DIR%\lib
    set FOAM_LIBBIN=%FOAM_LIB%
    set FOAM_APP=%WM_PROJECT_DIR%\applications
    set FOAM_APPBIN=%WM_PROJECT_DIR%\bin
    set FOAM_TUTORIALS=%WM_PROJECT_DIR%\tutorials
    set FOAM_UTILITIES=%FOAM_APP%\utilities
    set FOAM_SOLVERS=%FOAM_APP%\solvers
    set FOAM_USER_LIBBIN=%WM_PROJECT_USER_DIR%\lib
    set FOAM_USER_APPBIN=%WM_PROJECT_USER_DIR%\bin
    set FOAM_RUN=%WM_PROJECT_USER_DIR%\run
    IF "%WM_MPLIB%"=="OPENMPI" set mpi_version=openmpi
    set FOAM_MPI_LIBBIN=%FOAM_LIBBIN%\%mpi_version%
    set MPI_BUFFER_SIZE=20000000
    set PATH=%PATH%;%FOAM_MPI_LIBBIN%;%FOAM_JOB_DIR%;%WM_PROJECT_USER_DIR%\lib\%WM_OPTIONS%;^
    %WM_PROJECT_USER_DIR%\applications\bin\%WM_OPTIONS%;%FOAM_APPBIN%;%FOAM_LIBBIN%;^
    %WM_DIR%;%WM_PROJECT_DIR%\bin;%ParaView_DIR%\bin
    %1 %2 %3 %4 %5 %6 %7 %8 %9

To use OpenFOAM run DOS_MODE.bat.


--Nishit Joseph (talk) 14:08, 5 December 2012 (CET)