[version 12][version 11][version 10][version 9][version 8][version 7][version 6]
3.1 The programming language of OpenFOAM
This chapter provides some information to help understand how OpenFOAM applications and libraries are compiled. It provides some background knowledge of C++, the base language of OpenFOAM. Henry Weller chose C++ as the main programming language of OpenFOAM when he created it in the late 1980s.
The idea was to use objectoriented programming to express abstract concepts eﬃciently, just as verbal language and mathematics can do. For example, in ﬂuid ﬂow, we use the term “velocity ﬁeld”, which has meaning without any reference to the nature of the ﬂow or any speciﬁc velocity data. The term encapsulates the idea of movement with direction and magnitude and relates to other physical properties. In mathematics, “velocity ﬁeld” can be replaced by a single symbol, e.g. , and other symbols express operations and functions, e.g. “the ﬁeld of velocity magnitude” by .
CFD deals with partial diﬀerential equations in 3 dimensions of space and time. The equations contain: ﬁelds of scalars, vectors and tensors; tensor algebra; tensor calculus; and, dimensional units. The solution to these equations involves discretisation procedures, matrices, solvers, and solution algorithms.
Rather than program CFD in terms of intrinsic entities known to a computer, e.g. bits, bytes, integers, ﬂoating point numbers, OpenFOAM provides classes that deﬁne the entities encountered in CFD. For example, a velocity ﬁeld can be deﬁned by a vectorField class, allowing a programmer to create an instance, or object, of that class. The object can be created with the name U to mimic the symbol used in mathematics. Associated functions can be created with names which also try to emulate the simplicity of mathematics, e.g. mag(U) can represent .
The class structure concentrates code development to contained regions of the code, i.e. the classes themselves, thereby making the code easier to manage. New classes can be derived or inherit properties from other classes, e.g. the vectorField can be derived from a vector class and a Field class. C++ provides the mechanism of template classes such that the template class Field<Type> can represent a ﬁeld of any <Type>, e.g.scalar, vector, tensor. The general features of the template class are passed on to any class created from the template. Templating and inheritance reduce duplication of code and create class hierarchies that impose an overall structure on the code.
A theme of the OpenFOAM design is that it has a syntax that closely resembles the partial diﬀerential equations being solved. For example the equation

solve
(
fvm::ddt(rho, U)
+ fvm::div(phi, U)
 fvm::laplacian(mu, U)
==
 fvc::grad(p)
);
The equation syntax is most evident in the code for solvers, both the modules and solver applications. Users do not need a deep knowledge of C++ programming to interpret the equations written in a solver. Instead, an understanding of the underlying equations, models and solution method and algorithms is perhaps more helpful, which can be found in Notes on Computational Fluid Dynamics: General Principles.
It does help to have a rudimentary understanding of the the principles behind objectorientation and classes, and to have a basic knowledge of some C++ code syntax. To program in OpenFOAM, there is often little need for a user to immerse themselves in the code of any of the OpenFOAM classes. The essence of objectorientation is that the user should not have to go through the code of each class they use; merely the knowledge of the class’ existence and its functionality are suﬃcient to use the class. A description of each class and its functions is supplied with the OpenFOAM distribution in HTML documentation generated with Doxygen at https://cpp.openfoam.org