Lattice Boltzmann

Lattice Boltzmann sample codes in various other programming languages

Which programming language is best for coding your lattice Boltzmann simulations?

The following list may help you get an overview on (more or less) commonly used languages, their weaknesses and their strengths. For each language, a free sample code for 2D LB simulations can be downloaded. 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). This measures 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.

In most codes, 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. The test computer has an AMD Athlon processor at 1600 MHz with a 256 kb L2 cache.

That's the flow field resulting from the simulations (color=velocity norm)

flow_past_cylinder.gif

C: the classic one [2.19 M su/s]

Along with 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 you feel 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

C++: efficiency with modern software constructs [3.15 M su/s]

C++ is a straightforward, well structured language in the same sense as C, and it offers useful techniques for advanced software engineering. These techniques include object-oriented and generic (template based) programming. As the open-source library Palabos is written in C++, we haven't produced a stand-alone C++ example. Instead, the following is an application program using Palabos. This example is the fastest in the list, because the Palabos package implements some optimizations that are not present in the other sample codes. It does not mean that C++ is inherently faster than other languages.

Source code The example cylinder2d in Palabos
Reference compiler g++ 3.4.2 with options -O3 -march=athlon

Fortran 90: the number cruncher [990 k su/s]

This legendary language goes back to the early 1950ies where, in absence of other storage media, programs were developed on punch cards. Since then, Fortran has terrorized two generations of scientific programmers with strange formatting rules that are originally due 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.

Astonishingly enough, we managed to get the code 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.

Users note : In this code the “y” indexes are numbered in a computer scientist's way (from top to bottom), instead of the normal physicist's way (from bottom to top). Therefore when saving your data you should invert the “y” indexes of your loop.

Source code f90-bgk.tar
Reference compiler Intel ifc 7.0 with option -O3

Java: the voguish one [770 k su/s]

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

Python: A big classic in scientific computing

The Python script is probably the shortest in our collection… and yet impressively fast! Get the simulation results in just a few minutes.

Source code lbmFlowAroundCylinder.py.zip

Matlab: the short one [134 k su/s]

If you hate spending months in the programming - debugging - data analysis cycle, Matlab is the right choice for you. The complete program holds on one A4 sheet, including initialization 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 linear algebra library whatsoever)! The performance is very reasonable if it is ranked against other interpreted languages. Adriano wrote the cavity flow example, and Andrea the thermal flow, illustrating Rayleigh-Benard instability.

Source code cylinder.m
Reference interpreter Matlab 6.0.1

 

Other Matlab codes:

It is extremely easy to implement lattice Boltzmann applications in regular geometries. The following Matlab scripts are only one or two pages long. Furthermore, they are self-consistent and merely use the basic matrix formalism of Matlab. They do not invoke external routines such as linear algebra packages or differential equation solvers. And yet, complex flows are simulated with high accuracy. The showcases include a lid-driven cavity, Rayleigh-Bénard convection and segregation of species in a multi-component fluid. In each case, a free Matlab script is available for download.

 

Ruby: the fancy one [1.3 k su/s]

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 particularly appealing way, and satisfaction is guaranteed for programmers with a sense of structural beauty. 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