## 3.1The programming language of OpenFOAM

In order to understand the way in which the OpenFOAM library works, some background knowledge of C++, the base language of OpenFOAM, is required; the necessary information will be presented in this chapter. Before doing so, it is worthwhile addressing the concept of language in general terms to explain some of the ideas behind object-oriented programming and our choice of C++ as the main programming language of OpenFOAM.

### 3.1.1Language in general

The success of verbal language and mathematics is based on eﬃciency, especially in expressing abstract concepts. 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, we can represent velocity ﬁeld by a single symbol, e.g. , and express certain concepts using symbols, e.g. “the ﬁeld of velocity magnitude” by . The advantage of mathematics over verbal language is its greater eﬃciency, making it possible to express complex concepts with extreme clarity.

The problems that we wish to solve in continuum mechanics are not presented in terms of intrinsic entities, or types, known to a computer, e.g. bits, bytes, integers. They are usually presented ﬁrst in verbal language, then as partial diﬀerential equations in 3 dimensions of space and time. The equations contain the following concepts: scalars, vectors, tensors, and ﬁelds thereof; tensor algebra; tensor calculus; dimensional units. The solution to these equations involves discretisation procedures, matrices, solvers, and solution algorithms.

### 3.1.2Object-orientation and C++

Progamming languages that are object-oriented, such as C++, provide the mechanism — classes — to declare types and associated operations that are part of the verbal and mathematical languages used in science and engineering. Our velocity ﬁeld introduced earlier can be represented in programming code by the symbol U and “the ﬁeld of velocity magnitude” can be mag(U). The velocity is a vector ﬁeld for which there should exist, in an object-oriented code, a vectorField class. The velocity ﬁeld U would then be an instance, or object, of the vectorField class; hence the term object-oriented.

The clarity of having objects in programming that represent physical objects and abstract entities should not be underestimated. 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.

### 3.1.3Equation representation

A central theme of the OpenFOAM design is that the solver applications, written using the OpenFOAM classes, have a syntax that closely resembles the partial diﬀerential equations being solved. For example the equation

is represented by the code

solve
(
fvm::ddt(rho, U)
+ fvm::div(phi, U)
- fvm::laplacian(mu, U)
==