Skip to content
Alexey Matveichev edited this page Jul 18, 2014 · 12 revisions

Environment setup

Why

Surely after installation one can just keep terminal open and do everything in that only opened terminal with environment being set up. Or one can execute every time source $HOME/OpenFOAM/OpenFOAM-2.X.Y/etc/bashrc after firing up a terminal (also one needs to mount disk image after reboot). But it's sucks. It's easier to automate the process. Below is a parts of ~/.profile file that can be copy-n-pasted to yours.

Default version approach

This approach assumes you're using certain default OpenFOAM version. You create a file with the version inside, script reads the version, mounts disk image if it is necessary and sets up environment upon launch of the terminal.

First you create a file with a version number:

$ mkdir -p .OpenFOAM
$ echo '2.X.Y' > .OpenFOAM/OpenFOAM-release

and the code below will read file, check if disk image is mounted, mount it if necessary and finally set up the environment:

if [ -f "$HOME/.OpenFOAM/OpenFOAM-release" ]; then
    FOAM_DISK_IMAGE=$HOME/OpenFOAM.sparsebundle
    FOAM_MOUNT_POINT=$HOME/OpenFOAM
    FOAM_VERSION=$(cat $HOME/.OpenFOAM/OpenFOAM-release)
    if [ ! -f "$HOME/OpenFOAM/OpenFOAM-$FOAM_VERSION/etc/bashrc" ]; then
        hdiutil attach -quiet -mountpoint "$FOAM_MOUNT_POINT" "$FOAM_DISK_IMAGE" &&
                . "$HOME/OpenFOAM/OpenFOAM-$FOAM_VERSION/etc/bashrc"
    else
        source "$HOME/OpenFOAM/OpenFOAM-$FOAM_VERSION/etc/bashrc"
    fi
fi

"Function" approach

This approach is suitable when you have several OpenFOAM versions you're working with. So there's no environment set up after terminal launch, you need to execute function corresponding to chosen version to set up environment.

mount_disk_image () {
    local foam_mount_point=$HOME/OpenFOAM
    local foam_disk_image=OpenFOAM.sparsebundle
    if [ -n $(lsof $foam_mount_point) ]; then
    fi
}

ofxxx () {
    local version=$1
    local foam_mount_point=$HOME/OpenFOAM
    [ ! -f "$foam_mount_point/OpenFOAM-$version/etc/bashrc" ] && mount_disk_image
    [ -n "$WM_PROJECT" ] && wmUNSET
    if [ -f "$foam_mount_point/OpenFOAM-$version/etc/bashrc" ]; then
        source "$foam_mount_point/OpenFOAM-$version/etc/bashrc"
    fi
}

of222 () {
    ofxxx '2.2.2'
}

of22x () {
    ofxxx '2.2.x'
}

of230 () {
    ofxxx '2.2.x'
}

You can add more functions corresponding for the versions you'd like to use.

Combining two approaches

This approach assumes you're using certain OpenFOAM version by default but then it allows you to switch between versions calling a function. If you'd like to utilize this variant of environment setup, you can download .openfoam.bash into home folder file and add source $HOME/.openfoam.bash to your $HOME/.profile.

$FOAM_RUN folder

As in general there's no need in case sensitive file system for case files, one can move $FOAM_RUN ($HOME/OpenFOAM/user-2.X.Y/run) folder from disk image to anywhere else (ex. Documents folder) and then create symlink. After you've set up environment, you can run the following commands:

$ mkdir -p $FOAM_RUN
$ mkdir -p $HOME/Documents/OpenFOAM
$ mv $HOME/OpenFOAM/user-2.X.Y/run $HOME/Documents/OpenFOAM/run-2.X.Y
$ ln -s $HOME/Documents/OpenFOAM/run-2.X.Y $FOAM_RUN
Clone this wiki locally