A Walk Through Some Openfoam Code: Vector: Håkan Nilsson 1
A Walk Through Some Openfoam Code: Vector: Håkan Nilsson 1
Learning outcomes
• You will gain experience in reading OpenFOAM classes and figure out how they work.
- The last two are for the templated Vector class (capital first letter V means that it is a
templated class).
- Inline functions must be implemented in the class declaration file, since they must be
inlined without looking at the class definition file. In OpenFOAM there are usually files
named as VectorI.H containing inline functions, and those files are included in the
corresponding Vector.H file. There is no *.C file in the Vector class, since all functions
are inlined.
- Directories including string {V,v}ector are typedefs for Vector of complex num-
bers, floats, labels, doubles and scalars. The directory lists defines lists of vec-
tors. What is a scalar? See $FOAM_SRC/OpenFOAM/primitives/Scalar/scalar/scalar.H
Vector inheritance
The Vector.H file shows that the Vector class inherits from (read: “is a”) VectorSpace:
template<class Cmpt>
class Vector
:
public VectorSpace<Vector<Cmpt>, Cmpt, 3>
{
The VectorSpace class is found in $FOAM_SRC/OpenFOAM/primitives/VectorSpace.
Here, this is a pointer to the current object of the current class, i.e. we here set the static
data member v_ (inherited from class VectorSpace.H) to the values supplied as argu-
ments to the constructor.
• It is here obvious that the member function Vector belongs to the class Vector, and that
it is a constructor since it has the same name as the class.
template<class Cmpt>
inline const Cmpt& Foam::Vector<Cmpt>::x() const
{
return this->v_[X];
}
template<class Cmpt>
inline Cmpt& Foam::Vector<Cmpt>::x()
{
return this->v_[X];
}
The first one is for const objects, and the second one can be used to manipulate the object.
Vector operators
• Some operators are defined in VectorI.H:
template<class Cmpt>
inline typename innerProduct<Vector<Cmpt>, Vector<Cmpt>>::type
operator&(const Vector<Cmpt>& v1, const Vector<Cmpt>& v2)
{
return Cmpt(v1.x()*v2.x() + v1.y()*v2.y() + v1.z()*v2.z());
}
template<class Cmpt>
inline Vector<Cmpt> operator^(const Vector<Cmpt>& v1, const Vector<Cmpt>& v2)
{
return Vector<Cmpt>
(
(v1.y()*v2.z() - v1.z()*v2.y()),
(v1.z()*v2.x() - v1.x()*v2.z()),
(v1.x()*v2.y() - v1.y()*v2.x())
);
}
VectorSpace
• The VectorSpace class does not inherit from any other class:
template<class Form, class Cmpt, direction Ncmpts>
class VectorSpace
{
However, it is used in many types of vector spaces (search for VectorSpace in Doxygen):
VectorSpace< Barycentric2D< Cmpt >, Cmpt, 3 >
VectorSpace< Barycentric< Cmpt >, Cmpt, 4 >
VectorSpace< Barycentric< scalar >, scalar, 4 >
VectorSpace< BarycentricTensor< Cmpt >, Cmpt, Mrows *Ncols >
VectorSpace< CompactSpatialTensor< Cmpt >, Cmpt, Mrows *Ncols >
VectorSpace< CompactSpatialTensor< scalar >, scalar, Mrows *Ncols >
VectorSpace< CompactSpatialTensorT< Cmpt >, Cmpt, Mrows *Ncols >
VectorSpace< cubicEqn, scalar, 4 >
VectorSpace< DiagTensor< Cmpt >, Cmpt, 3 >
VectorSpace< DiagTensor< scalar >, scalar, 3 >
VectorSpace< Form, Cmpt, Mrows *Ncols >
VectorSpace< linearEqn, scalar, 2 >
VectorSpace< Polynomial< PolySize >, scalar, PolySize >
VectorSpace< quadraticEqn, scalar, 3 >
VectorSpace< Roots< N >, scalar, N >
VectorSpace< RowVector< Cmpt >, Cmpt, Mrows *Ncols >
VectorSpace< SpatialTensor< Cmpt >, Cmpt, Mrows *Ncols >
VectorSpace< SpatialVector< Cmpt >, Cmpt, 6 >
VectorSpace< SpatialVector< scalar >, scalar, 6 >
VectorSpace< SphericalTensor2D< Cmpt >, Cmpt, 1 >
VectorSpace< SphericalTensor< Cmpt >, Cmpt, 1 >
VectorSpace< SymmTensor2D< Cmpt >, Cmpt, 3 >
VectorSpace< SymmTensor< Cmpt >, Cmpt, 6 >
VectorSpace< Tensor2D< Cmpt >, Cmpt, 4 >
VectorSpace< Tensor< Cmpt >, Cmpt, Mrows *Ncols >
VectorSpace< Tensor< scalar >, scalar, Mrows *Ncols >
VectorSpace< Vector2D< Cmpt >, Cmpt, 2 >
VectorSpace< Vector2D< scalar >, scalar, 2 >
VectorSpace< Vector< Cmpt >, Cmpt, 3 >
VectorSpace< Vector< label >, label, 3 >
VectorSpace< Vector< scalar >, scalar, 3 >
VectorSpace< Vector< vector >, vector, 3 >
• Find all member data and member functions, included inherited ones.