Choose your language
Which programming language is best for coding your lattice Boltzmann simulations? The following list may help you to get an overview on (more or less) commonly used languages, their weaknesses and their strengths. Each language is accompanied by a LB BGK sample code for the simulation of a 2D fluid flow. The execution speed of the codes is indicated in units of site updates per second (su/s; 1k su/s = 1000 su/s; 1M su/s = 1000k su/s), the number of lattice sites the program is able to update, i.e. to drive through 1 collision and 1 propagation step, in a second on our test machine (it is, by current standards, a relatively slow machine). Please note that neither the efficiency of the codes is really optimal, nor their architecture, if they should be used on some larger project. They are rather designed to line out some programming concepts and the peculiarities of the various languages.
The sample application is a 2D flow in a channel past a cylinder. On the upper and lower boundaries, and on the surface of the cylinder, a vanishing fluid velocity is imposed (no-slip condition). The cylinder is placed slightly asymmetrically, with its center one lattice site above the center of the channel. The Reynolds number is Re=100, a value at which an unsteady, periodic flow is observed. This can be seen on the animation, by clicking on the picture.
|
3.15M su/s
C++: our choice
The OpenLB source code is fully developed in C++, and we present in the present rubric a code based on this library. C++ is a straightforward, well structured language in the same sense as C, and it includes useful techniques for advanced software engineering. Those techniques include object-oriented and generic (template based) programming. The C++ code is the fastest in the list, because the OpenLB package implements some optimizations that are not present in the other sample codes. It should be understood that this advantage has nothing to do with the choice of the language itself.
| Source code: | openlb_sample.cpp |
| Reference compiler: | g++ 3.4.2 with options -O3 -march=athlon |
| Where to find a free compiler: | gcc.gnu.org |
2.19M su/s
C: the classic one
Along with a reasonably good performance, C offers you a nice and well structured programming environment. Admittedly, there are no classes, no modules and no templates, you don't have a garbage collector and it makes you look like a Linux kernel programmer. But turn it any way you want, the code is and remains... er... well structured.
| Source code: | C-bgk.tar |
| Reference compiler: | gcc 3.4.2 with options -O3 -march=athlon |
| Where to find a free compiler: | gcc.gnu.org |
990k su/s
Fortran 90: the fast one
This legendary language goes back to the early 1950ies where, in absence of other storage media, programs were developed on punch cards. Since then, the language has terrorized two generations of scientific programmers with strange formatting rules that are due originally to the layout of those cards. In the Fortran 90 standard however, a free source form and some higher level language constructs were added, making it much more friendly to use.
The main features of the language where designed for the purpose of scientific and numerical work, compared to other languages that are rather oriented towards a higher level software architecture. For that reason, it is often the main language of choice in the scientific community, although it becomes quite rapidly awkward to use on larger software projects. One point is given for sure, Fortran 90 happens to regularly beat all other languages by a distinct speed advantage on various scientific problems.
Thanks to Orestis for this code, which, astonishingly enough, we managed to get compiled with the intel compiler as well as the free gfortran and g95 compilers. The benchmark result should be appreciated with some care, as the code can run much faster on some well chosen hardware/compiler combinations.
| Source code: | F90-bgk.tar |
| Reference compiler: | Intel ifc 7.0 with option -O3 |
| Where to find a free compiler: | gcc.gnu.org/fortran/ |
770k su/s
Java: the voguish one
This language is often cited as the state-of-the-art tool for the development of reusable, solid code. Ignored for a long time by numeric programmers, it has steadily increased its performance; as shown in this benchmark, Java does no longer need to hide behind other languages, even for number crunching applications. Furthermore, the code respects the guidelines for generic and reusable software and looks absolutely elegant.
Many thanks to Jean-Luc for this carefully designed code!
| Source code: | Java-bgk.tar |
| Reference compiler: | Sun javac 1.5.0_02 |
| Reference virtual machine: | Java HotSpot(TM) Server VM, build 1.5.0_02-b09 |
| Where to find a free compiler: | gcc.gnu.org/java/ |
134k su/s
Matlab: the short one
If you hate to spend ages on programming, debugging, data analysis and so on, Matlab is the choice for you. The complete program holds on one A4 sheet, including initialisation of the flow, simulation of the dynamics and visualization of the data (Please note that the presented script really contains all the code: there is no reference to an external solver or a linear algebra library whatsoever)! The performance is very reasonable if it is ranked against other interpreted languages.
| One page source: | cylinder.pdf |
| Source code: | cylinder.m |
| Reference interpreter: | Matlab 6.0.1 |
| No free interpreter available |
1.3k su/s
Ruby: the fancy one
In the same line as Perl and Python, Ruby is one of those general purpose scripting languages with which you can program just everything, from a web server over a spam filter to a database GUI. It implements the concepts of object-oriented programming in a particlularly appealing way, and rumors say you might feel some sensations during programming! At the current state, it is a purely interpreted language, which results in a loss of speed of three orders of magnitude compared to the compiled languages. As a matter of fact, Ruby was not really designed for numerical purposes either... but it remains interesting to see what a Ruby implementation of LB looks like anyway. Thanks again to Jean-Luc for this contribution.
| Source code: | Ruby-bgk.tar |
| Reference interpreter: | Ruby 1.8.4 |
| Where to find a free interpreter: | www.ruby-lang.org/ |