Compilation of Elmer on Linux using Cmake

From Elmer Wiki
Jump to: navigation, search

As of February 2015, the main Elmer distribution channel has changed to GitHub. The code is found under the elmerfem-link.

Retrieving the source

There are different ways of retrieving the source code from the repository. If you check on the repository, you find hints for retrieving a ZIP-file or using HTTPS,SSH or Subversion protocol for downloading. The most convenient way is to use git itself. The checkout command is

git clone git://www.github.com/ElmerCSC/elmerfem

Some network connection settings (e.g., proxies) do not allow for the git-protocol, hence one could replace it with

git clone https://www.github.com/ElmerCSC/elmerfem

Please, bear in mind that there are different branches in this repository. The most important branch, "devel", contains the latest updates of Elmer and preferentially should be used for compilation. It should be the default branch (see below how to verify). There are special branches for different projects within Elmer (e.g. "elmerice" - see instructions on here). In order to check the branch one is in (and also how up-to-date the current copy of the repository is), do a

git status

which should report something like

git status
On branch master
Your branch is behind 'origin/master' by 330 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)

The example above shows that git usually gives instructions on what actions to take. In order to change to the branch "devel" one has to enter

git checkout devel

Compilation using Cmake

Naturally, if you use Cmake for compilation one has to take care that the package (included in most standard Linux distributions) is installed. A version of Cmake 2.8.9 or newer is needed for successful configuration. Additionally one needs GNU make for the building process. Cmake allows for a lot of automated testing of the system, nevertheless, the user can add directives either as arguments to the cmake command or within a toolchain file. An installation script could read as (script proved to work on Linux Mint 17)

#!/bin/bash

# Example build-script for Elmer on Linux Mint 17(LTS)

CMAKE=cmake

# Installation directory (set these!)
TIMESTAMP=$(date +"%m-%d-%y")
ELMER_REV="Elmer_devel_${TIMESTAMP}"
ELMERSRC="/path/to/github/source/elmerfem"
BUILDDIR="$ELMERSRC/builddir"
IDIR="/usr/local/$ELMER_REV"
TOOLCHAIN="$ELMERSRC/../Elmer-linux-linuxmint17.cmake"

echo "Building Elmer from within " ${BUILDDIR}
echo "using following toolchain file " ${TOOLCHAIN}
echo "installation into " ${IDIR}
cd ${BUILDDIR}
pwd
ls -ltr

echo $CMAKE $ELMERSRC -DCMAKE_TOOLCHAIN_FILE=$TOOLCHAIN

$CMAKE $ELMERSRC -DCMAKE_TOOLCHAIN_FILE=$TOOLCHAIN \
    -DCMAKE_INSTALL_PREFIX=$IDIR \
    -DCMAKE_C_COMPILER=/usr/bin/gcc \
    -DCMAKE_Fortran_COMPILER=/usr/bin/gfortran \
    -DWITH_MPI:BOOL=TRUE \
    -DWITH_Mumps:BOOL=TRUE \
    -DWITH_Hypre:BOOL=TRUE \
    -DWITH_TRILINOS:BOOL=FALSE \
    -DWITH_ELMERGUI:BOOL=TRUE \
    -DWITH_OCC:BOOL=TRUE \
    -DWITH_PARAVIEW:BOOL=TRUE \
    -DWITH_QWT:BOOL=TRUE \
    -DWITH_VTK:BOOL=TRUE \
    -DWITH_PYTHONQT:BOOL=FALSE \
    -DWITH_MATC:BOOL=TRUE                            

make -j4 && sudo make install

sudo rm /usr/local/Elmer-devel
sudo ln -s $IDIR /usr/local/Elmer-devel

This should build a parallel (=MPI) version of Elmer including the parallel sparse matrix solver packages of Hypre and MUMPS - you need to install the "devel" versions (i.e., versions containing the header files) in advance. Installation will be done under /usr/local/Elmer-devel, which is expected to be a symbolic link (please, make sure that this is the case). If you lack sudo-rights, just change the installation directory to a path that provides you the correct permissions. Furthermore, ElmerGUI will be compiled - this expects the "devel" versions of OpenCascade, QWT and a binary (should the post-processing link work) of ParaView to be installed. Clearly, the user also has to take care that the GNU C (gcc), Fortran (gfortran) and C++ (g++) alongside a MPI library (e.g. OpenMPI) is installed on the system. The optional (actually, toolchain files should be avoided in native compilations) toolchain file for Linux Mint 17, Elmer-linux-linuxmint17.cmake, reads as

# CMake toolchain file for building on Linux Mint 17 (LTS) 64bit
#
# CSC - IT Center for Science Ltd.
# Version: 0.1

SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_PROCESSOR x86_64)
SET(CMAKE_SYSTEM_VERSION 1)

# Specify the cross compilers (serial)
SET(CMAKE_C_COMPILER gcc)
SET(CMAKE_Fortran_COMPILER gfortran)
SET(CMAKE_CXX_COMPILER g++)

# Specify the cross compilers (parallel)
SET(MPI_C_COMPILER mpicc)
SET(MPI_CXX_COMPILER mpicxx)
SET(MPI_Fortran_COMPILER mpif90)

# Compilation flags (i.e. with optimization)
SET(CMAKE_C_FLAGS "-O3" CACHE STRING "")
SET(CMAKE_CXX_FLAGS "-O3" CACHE STRING "")
SET(CMAKE_Fortran_FLAGS "-O3" CACHE STRING "")

It is also important to create the separate build directory (in this case builddir under elmerfem), or, in case of an earlier compilation, to clear it from old files. With all these per-requisites in place, the compilation the simply is triggered by running the script (don't forget to make it executable). Hint: if you need to use sudo rights to install, sometimes the -E option is in order to redirect the current environment to the process.

Adjusting the environment

You should (best place would be in ~/.bashrc) alter/create the following environment variables

export ELMER_HOME="/usr/local/Elmer-devel"
export PATH="$PATH:$ELMER_HOME/bin"
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ELMER_HOME/lib

in order to be able to utilize Elmer without being forced to give the complete paths to the executable/libraries.

Fast Recompilation

Sometimes the source has been already built but changes have been applied to some module. In this case the fastest way to compile is to command in the build dir as follows:

Assuming changes have been made to the MagnetoDynamics2D.F90, then

make MagnetoDynamics2D/fast install/fast