Lattice Boltzmann in various 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)

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
Where to find a free compiler: http://gcc.gnu.org

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, www.palabos.org/download-ql
Reference compiler: g++ 3.4.2 with options -O3 -march=athlon
Where to find a free compiler: http://gcc.gnu.org

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
Where to find a free compiler: http://gcc.gnu.org/fortran/

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
Reference virtual machine: Java HotSpot(TM) Server VM, build 1.5.0_02-b09
Where to find a free compiler: http://gcc.gnu.org/java/

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.

One page printout of source: lbmcylinder.png
Source code, cylinder flow: lbmFlowAroundCylinder.py.zip
Video of the results: YouTube

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 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 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.

One page printout of source: cylinder.pdf
Source code, cylinder flow: cylinder.m
Other Matlab codes: LB-Matlab Wiki
Reference interpreter: Matlab 6.0.1
Where to find a free interpreter: http://www.gnu.org/software/octave/

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
Where to find a free interpreter: http://www.ruby-lang.org/
 
numerics/codes.txt · Last modified: 2013/05/30 13:04 by jonas
 
RSS - 2011 © FlowKit.com
Except where otherwise noted, text, pictures and animations on this site are licensed under a Creative Commons Attribution-Share Alike 3.0 License.