mardi 15 avril 2014

Building OpenFOAM on OS X

Previous patches I've posted in this blog (#1, #2, #3) have certain mistakes. Mainly because I forgot about DYLD_LIBRARY_PATH environment variable and did not test parallel execution of the solvers. Recently I've decided it is time to fix it. Also that tiny feedback I had, showed that there is two common errors during compilation: people create disk images with case insensitive file system and they try to apply patches in an arbitrary folders. The second problem is hard to solve by a blog post, while the first one can be solved if we change the process of disk image creation. Go from error-prone GUI way to just one command executed in terminal window. And here goes the guide.
Build process was tested on OS X version 10.9.2 with clang --version: Apple LLVM version 5.1 (clang-503.0.38) (based on LLVM 3.4svn).

Before we start

I've made certain assumptions about the software you have on your Mac:

Homebrew is used as a package manager to install open source software. If not, you can install it following instructions on the site.

All software from ThirdParty source pack will be installed using Homebrew.

All commands are executed in Terminal application (usually it is called Terminal.app).

$ in the beginning of the lines is a shell prompt and you don't need to copy it, if you're copy/pasting commands from the post. In general commands should be executed in the sequence shown in the post.

You've downloaded Paraview application from paraview.org and installed it in /Applications.

The last command will also install cmake, gmp, and mpfr as dependencies.

Downloading necessary pieces

At this point it is time to decide what version of OpenFOAM you'd like to build. As 2.3.0 introduced certain backward incompatible changes, I've made patches for 2.2.2 and 2.3.0. Further in the guide I assume you've downloaded source pack and patch into ~/Downloads/OpenFOAM folder.

Building OpenFOAM

!!! Cause certain file names in this part depend on the version of OpenFOAM you're building, I use N and M variables in the file names. For OpenFOAM version 2.2.2: N=2, M=2; for OpenFOAM version 2.3.0: N=3, M=0.

Also it is possible that you already have disk image with the same name in your home folder. Correct the names accordingly.
To build OpenFOAM first you need to create disk image with case sensitive file system. You can either follow the guide from OpenFOAM wiki, or just issue these commands in your terminal:

Last command will create disk image with the name OpenFOAM.sparsebundle with estimated size 4.4 G (as it is the sparse image, after creation the size of the file will be different), create HFS+ file system with a volume name OpenFOAM in the image. Finally -fsargs -s will instruct newfs_hfs utility to create case sensitive file system.
After the image was created you can mount it, I will assume it is mounted in $HOME/OpenFOAM

At this point maybe you'll need to edit etc/bashrc file. But since compiler settings are fixed (it is Clang), OpenMPI and other libraries settings are fixed (they are installed with Homebrew), in fact the only thing worth editing is path settings and only in case if you're installing OpenFOAM elsewhere (i.e. not in $HOME/OpenFOAM). Now you're more-or-less ready to compile.

$ source etc/bashrc
$ ./Allwmake > log.Allwmake 2>&1

The last command executes Allwmake script and redirects its output to log.Allwmake file; just in case, if anything goes wrong, it'll be easy to locate the source of error having this log file.
After Allwmake execution is finished, you can test the build with:

And finally third one is to transform second version in the set of functions. I.e. instead of automatic attachment of disk image upon terminal start, you'll need to execute function which will mount necessary disk image and source bashrc:

Unfortunately no, I don't have patch for 2.1.0 as I don't use this version. There are patches (http://www.cfd-online.com/Forums/openfoam-installation-windows-mac/95763-1-9-aka-2-1-mac-os-x.html) by Bernhard Gschaider, though for them you need to install Macports and gcc 4.6.

Thank you for your response. I heard it's not good to have Macport and Homebrew together. I have installed OF 2.3 with homebrew but need 2.1 as well. I have to remove homebrew to install Gcc 4.6, right?

Well, I've never tried to mix Homebrew and Macports but I guess there will be a mess cause both use /use/local for installation of software (well, technically Homebrew just creates symlinks).

You've got plenty of possibilities for installation of 2.3 and 2.1. a) Remove Homebrew, install both versions with Macports and Bernhard Gschaider's patches, b) Adapt Bernhard Gschaider's patches for 2.1 to use clang & Homebrew yourself, c) Wait till somebody creates patches for compilation of 2.1 with clang & Homebrew as package manager, d) Install gcc using Homebrew and try to use available patches for 2.1, e) Something else (buy separate Mac for 2.1 ;), run it in VM under Linux, adapt your software to use version 2.2.2)

My set of patches was a result of scratching my itch (like almost everywhere in OSS world), I didn't want to use Macports as a package manager, so I've adapted existing patches to compile OF with clang.

Hi, Mr Matveichev. Thank you very much for this article. It is very helpful. When I try to brew install scotch it suggested that there's no formula. So the first time I tried to compile without scotch. But blockMesh and icoFoam prompted error "can not find run/system/controlDict at line 0." and exited. Now I've remove the image and installed scotch from a github repo and I am compiling it again. Do you think that problem can be solved this way? Thanks for your time.

sorry about scotch, I forgot that it is from science repository (so before installing scotch with 'brew install scotch' you need to do 'brew tap homebrew/science').

And I'm sorry about the error you've got, I forgot to add 'cd cavity' command after 'cp -r $FOAM_TUTORIALS/incompressible/icoFoam/cavity .'. As you've run blockMesh utility in just $FOAM_RUN folder (and not in $FOAM_RUN/cavity), it wasn't able to find necessary dictionaries.

Thank you very much. The compilation has just finished and I followed your instructions to run the tests and they worked perfectly. Your article is of tremendous help and I really appreciate your patience when answering my "beginner questions". It's very kind of you.

disk0s2 is a HDD of your Mac, FS there is case insensible. I will surely recheck the guide as soon as I will be at my Mac but according to previous comments (and also comments from my colleagues), it's possible to build OpenFOAM using the guide without much problems. So I guess you've missed certain point (unfortunately I can't guess which one).

Thanks a lot for your post. I followed the instruction step by step but unfortunately when I try to compile I get the following error:

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/cwchar:129:9: error: no member named 'vwprintf' in the global namespaceusing ::vwprintf; ~~^/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/cwchar:131:9: error: no member named 'swscanf' in the global namespaceusing ::swscanf; ~~^/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/cwchar:132:9: error: no member named 'vfwscanf' in the global namespaceusing ::vfwscanf; ~~^/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/cwchar:133:9: error: no member named 'vswscanf' in the global namespaceusing ::vswscanf; ~~^/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/cwchar:134:9: error: no member named 'vwscanf' in the global namespaceusing ::vwscanf; ~~^/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/cwchar:136:9: error: no member named 'wprintf' in the global namespaceusing ::wprintf; ~~^/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/cwchar:137:9: error: no member named 'wscanf' in the global namespaceusing ::wscanf; ~~^/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/cwchar:138:9: error: no member named 'fgetwc' in the global namespaceusing ::fgetwc; ~~^/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/cwchar:139:9: error: no member named 'fgetws' in the global namespaceusing ::fgetws; ~~^/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/cwchar:140:9: error: no member named 'fputwc' in the global namespaceusing ::fputwc; ~~^/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/cwchar:141:9: error: no member named 'fputws' in the global namespaceusing ::fputws;

The error looks similar to one people usually get when they create disk image with case insensitive file system (as in this case compiler will include char.H from OpenFOAM instead of char.h from system include folder).

HiThis seems great! Exactly what I need. However, I have followed the steps, and every step is completed without any error messages, but when I am ready to test OpenFOAM, with the blockMesh command, I get "command not found". The same is the case for the icoFoam command, while the paraFoam command seems to work, as paraview is launched. Do you/anybody know whats wrong? Any help is greatly appreciated!

well, as paraFoam seems to be working and blockMesh is not, are you sure that compilation process was finished successfully? paraFoam is just a wrapper around paraview command which is available without compilation, while blockMesh requires compilation.

You need to check log.Allwmake file for errors during compilation to figure out what's gone wrong.

Hm, okei, so it seems OpenFOAM has not been compiled correctly after all. Lots of error messages, where the first ones seems to be connected to open-mpi. messages like "could not open file ompi/mpi/cxx/pmpicxx.h for source file UOPwrite.C due to No such file or directory", any many similar ones. I have tried re-installing open-mpi, and then performing the steps once more, without success. I have installed open-mpi through homebrew, as described, and the homebrew instalation process seems to complete without any errors, but they might be located somewhere else as well. Any tips? :S

These errors are not fatal for the compilation and in fact you can ignore them.

Do you have 'platforms' folder after compilation? you can go to 'platforms/darwinIntel64ClangDPOpt/bin' and check if blockMesh is there. And if it is actually there, then compilation is successful but something mess up environment variables.

Also you can upload your log.Allwmake somewhere (pastebin.com for example) so I can check if it contains any fatal errors.

The platforms/../bin folder contains some files, such as the mesh converter applications, "kivaToFoam", "cfx4ToFoam", etc, but not much else (i.e. no blockMesh). My log.Allwmake file is really long, so pastebin would not take it without a pro account. I uploaded it to dropbox, if that is okey? here's the link: https://www.dropbox.com/s/15ewmtcb3ewkzc6/log.Allwmake?dl=0

And I guess this is the reason (as clang++ uses FlexLexer.h from SDK while your version of the flex utility can be incompatible with it).

As a variant you can correct OpenFOAM-2.3.0/wmake/rules/General/flex and OpenFOAM-2.3.0/wmake/rules/General/flex++ files to explicitly use /usr/bin/flex instead of the one from $PATH variable (though if you installed flex in /usr/bin I don't know what to suggest).

It was the flex thing that was the problem. I actually have no idea how I have managed to install a different version of flex, so I decided to revert back to the default version, i.e. the one you have. This worked fine! OpenFOAM is now running :)

Edit $HOME/OpenFOAM/OpenFOAM-2.2.2/wmake/rules/General/flex and $HOME/OpenFOAM/OpenFOAM-2.2.2/wmake/rules/General/flex++ files (I guess you're building version 2.2.2). Change there flex to /usr/bin/flex, after that wmake will use Apple version of flex, and with this version everything builds fine.

Well, maybe I should also try to explain why you're doing it. If you just type flex on the command prompt shell will look to executable flex in PATH environment variable, and it will find it in '/opt/local/bin/' as it seems your Macports installation requires this path supersede '/usr/bin'.

So first of all you should change 'flex' to exactly '/usr/bin/flex'. I.e. you need all these slashes.

As you can see inside these files only one string 'flex' is present, so you should change the line

well, wherever you'd like in fact ;) if you have Dropbox, you can put it in Public folder and post a link here; there's pastebin.com (though I think your log-file is rather large so pastebin will reject it); if you have account on bitbucket.org, you can create repository for the file and post if there; if you have account on github.com, you can create repository for the file and post it there; there's SpiderOak, there's Wuala, there's Windows Live, there's Yandex Disk... finally, you can burn it to CD and send it via post to l'Institute Jean Lamour in Nancy.

Along with a link to your log-file please post the output, of:$ sw_vers$ flex --version$ which flex$ clang++ --version

I guess, you've got custom version of flex utility installed, and this causes problems during compilation.

Thanks Alexey; I'm considering sending the CD to this address: haha ​​jokeI think the problem was that from a previous article I used macports to install packages and then fell into this using homebrew; I will uninstall macports and try again; if I have no succeed I'll upload the logfile to my github account for you to look.

Maybe it's worth updating the post to mention about the problem with Macports. I thought Macports users usually use Bernhard Gschaider's patch (https://sourceforge.net/p/openfoam-extend/svn/HEAD/tree/trunk/Breeder_2.3/distroPatches/MacPatch/)

I managed to install OpenFOAM 2.3.1 using (https://github.com/mrklein/openfoam-os-x/wiki/OpenFOAM%20release%20&%20Homebrew) on Yosemite. Everything works fine but I am struggling to work on the post-configuration.

When I type in $HOME /.profile, I get the following message: -bash: /Users/Jiho/.profile: Permission denied

Could you be able to help me on this? I am only using the 2.3.1 version and I wish it to be good to go once I open my terminal.app.

Also, what happens if I eject the OpenFOAM folder? Would the post-configuration be able to mount it back automatically as long as my source file is available?

Why do you "type in $HOME /.profile"? This file is executed automatically every time you open Terminal.app. Since in general it does not have execution permission, you get the error from your message. If you would like to run commands from this file, you should use either "source $HOME/.profile" or ". $HOME/.profile" (there is a space between first dot and $HOME).

Concerning disk image mount. If you have chosen to use openfoam-env-setup.sh, then it will try to mount disk image if it can not find OpenFOAM's bashrc file (and this is the case if either you have ejected OpenFOAM folder, or deleted bashrc file).

Unfortunately I was not able to deduce from your comment, where did you stuck in Post-configuration phase, so my answer could be irrelevant.

Right.. I am very new to OpenFOAM, Linux and even to OS X so I am very ignorant in all this. If I wish to follow your first method for post-configuration (defining alias), do I just type in "source $HOME/.profile" and then "alias of2NM='hdiutil attach -quiet -mountpoint $HOME/OpenFOAM OpenFOAM.sparsebundle; sleep 1; source $HOME/OpenFOAM/OpenFOAM-2.N.M/etc/bashrc' " on my terminal ??

It works! I can't thank you more :) Thank you so much you made my day!

Regarding to disk image mount, do I have to leave the OpenFOAM disk mounted at all times? Is there a way to make it to be mounted automatically whenever I wish to use OpenFOAM instead of having it always mounted?

Well - "...like you would in Linux..." - I have never done this. I always have default version and environment is automatically set up for this version.

You can use this file: https://github.com/mrklein/openfoam-os-x/blob/master/files/openfoam-env-setup.sh (download it, add 'source /path/to/the/openfoam-env-setup.sh' to your profile). Then after if you have ~/.OpenFOAM/OpenFOAM-release file with version inside, disk i automatically mounted, environment is set up. Otherwise you get of22x, of231, etc functions to set up environment. Also it is explained in wiki: https://github.com/mrklein/openfoam-os-x/wiki/Post-configuration.

I am trying to compile OpenFOAM-3.0.0 following your guidelines and your newly posted patch for OF3.0.0 on your repository https://github.com/mrklein/openfoam-os-x. I tried both with macports and homebrew, and I managed to compile it both without any bad issues. However when I run the cavity case I always have this issue: