Commit 3addfe45 authored by Chris Cantwell's avatar Chris Cantwell

Initial contributions to developers guide.

parent fae91167
\chapter{Coding Standard}
The purpose of this page is to detail the coding standards of the project which
all contributers are requested to follow.
This page describes the coding style standard for C++. A coding style standard
defines the visual layout of source code. Presenting source code in a uniform
fashion facilitates the use of code by different developers. In addition,
following a standard prevents certain types of coding errors.
All of the items below, unless otherwise noted, are guidelines. They are
recommendations about how to lay out a given block of code. Use common sense and
provide comments to describe any deviation from the standard. Sometimes,
violating a guideline may actually improve readability.
If you are working with code that does not follow the standard, bring the code
up-to-date or follow the existing style. Don’t mix styles.
\section{Code Layout}
The aim here is to maximise readability on all platforms and editors.
\item Code width of 80 characters maximum - hard-wrap longer lines.
\item Use sensible wrapping for long statements in a way which maximises
\item Do not put multiple statements on the same line.
\item Do not declare multiple variables on the same line.
\item Provide a default value on all variable declarations.
\item Enclose every program block (if, else, for, while, etc) in braces, even if
empty or just a single line.
\item Opening braces (\{) should be on their own line.
\item Braces at same indentation as preceeding statement.
\item One class per .cpp and .h file only, unless nested.
\item Define member functions in the .cpp file in the same order as defined in
the .h file.
\item Templated classes defined and implemented in a single .hpp file.
\item Do not put inline functions in the header file unless the function is
trivial (e.g. accessor, empty destructor), or profiling explicitly suggests to.
\item Inline functions should be declared within the class declaration but
defined outside the class declaration at the bottom of the header file.
Virtual and inline are mutually exclusive. Virtual functions should therefore be
implemented in the .cpp file.
Adding an appropriate amount of white space enhances readability. Too much white
space, on the other hand, detracts from that readability.
\item Indent using a four-space tab. Consistent tab spacing is necessary to
maintain formatting. Note that this means when a tab is pressed, four physical spaces are
inserted into the source instead.
\item Put a blank line at the end of a public/protected/private block.
\item Put a blank line at the end of every file.
\item Put a space after every keyword (if, while, for, etc.).
\item Put a space after every comma, unless the comma is at the end of the line.
\item Do not put a space before the opening parenthesis of an argument list to a
\item Declare pointers and references with the * or \& symbol next to the
declarator, not the type; e.g., Object *object. Do not put multiple variables in the same
\item Place a space on both sides of a binary operator.
\item Do not use a space to separate a unary operator from its operand.
\item Place open and close braces on their own line. No executable statements
should appear on the line with the brace, but comments are allowed. Indent opening
braces at the same level as the statement above and indent the closing brace at
the same level as the corresponding opening brace.
\item Indent all statements following an open brace by one tab. Developer Studio
puts any specifier terminated with a colon at the same indentation level as the
enclosing brace. Examples of such specifiers include case statements, access
specifiers (public, private, protected), and goto labels. This is not acceptable
and should be manually corrected so that all statements appearing within a block
and delineated by braces are indented.
\item Break a line into multiple lines when it becomes too long to read. Use at
least two tabs to start the new line, so it does not look like the start of a
\item Follow C++ style comments with one space. It is also preferable to
consider any text that follows C++ style comments as a sentence and to begin this text
with a capital letter. This helps to distinguish the line from a continuation of
a previous line; i.e., \inlsh{// This is my comment.}
\item As a general rule, don’t keep commented out source code in the final
baselined product. Such code leads the reader to believe there was uncertainty
in the code as it currently exists.
\item Place the \# of a preprocessor directive at column one. An exception is
the use of nested ifdefs where the bodies only contain other preprocessor directives.
Add tabs to enhance readability:
void foo() {
for(int i = 0; i < 10; ++i)
#ifdef BAR
\item Use tabular white space if it enhances
\item Use only one return statement. Structure the code so that only one return
statement is necessary.
\section{Naming Conventions}
Keep variable and function names meaningful but concise.
\item Begin variable names with lower-case letter.
\item Begin function names and class names with upper-case letter.
\item All function, class and variable names should be written in CamelCase,
e.g. \inlsh{MyClass, DoFunction() or myVariableName}.
\item All preprocessor definitions written in UPPER\_CASE with words separated
by underscores, e.g. USE\_SPECIFIC\_FEATURE.
\item All member variables prefixed with m\_.
\item All constants prefixed with a k.
\item All function parameters prefixed with a p.
\item All enumerations prefixed with an e.
\item Do not use leading underscores.
The top-level namespace is "Nektar". All code should reside in this namespace or
a sub-space of this.
\item Namespaces correspond to code structure.
\item Namespaces should be kept to a minimum to simplify the interface to their
\item Briefs for classes, functions and types in header files using
\inlsh{///} notation.
\item Full documentation with implementation using \inlsh{/** ... *\/}
\item Use @ symbol for @class, @param, @returns, etc for ease of identification.
\item Any separate documentation pages not directly associated with a portion of
the code should be in a separate file in /docs/html/doxygen.
\chapter{Core Concepts}
This section describes some of the key concepts which are useful when developing
code within the Nektar++ framework.
\section{Factory method pattern}
The factory method pattern is used extensively throughout Nektar++ as a
mechanism to instantiate objects. It provides the following benefits:
\item Encourages modularisation of code such that conceptually related
algorithms are grouped together
\item Structuring of code such that different implementations of the same
concept are encapsulated and share a common interface
\item Users of a factory-instantiated modules need only be concerned with the
interface and not the details of underlying implementations
\item Simplifies debugging since code relating to a specific implementation
resides in a single class
\item The code is naturally decoupled to reduce header-file dependencies and
improves compile times
\item Enables implementations (e.g. relating to third-party libraries) to be
disabled through the build process (CMake) by not compiling a specific
implementation, rather than scattering preprocessing statements throughout the
For conceptual details see the Wikipedia page.
\subsection{Using NekFactory}
The templated NekFactory class implements the factory pattern in Nektar++.
There are two distinct aspects to creating a factory-instantiated collection of
classes: defining the public interface, and registering specific
implementations. Both of these involve adding standard boilerplate code. It is
assumed that we are writing a code which implements a particular concept or
functionality within the code, for which there are multiple implementations. The
reasons for multiple implementations may be very low level such as alternative
algorithms for solving a linear system, or high level, such as selecting from a
range of PDEs to solve.
\subsubsection{Creating an interface (base class)}
A base class must be defined which prescribes an implementation-independent
interface. In Nektar++, the template method pattern is used, requiring public
interface functions to be defined which call private virtual implementation
methods. The latter will be overridden in the specific implementation classes.
In the base class these virtual methods should be defined as pure virtual, since
there is no implementation and we will not be instantiating this base class
As an example we will create a factory for instantiating different
implementations of some concept \inlsh{MyConcept}, defined in
\inlsh{MyConcept.h} and \inlsh{MyConcept.cpp}. First in \inlsh{MyConcept.h},
we need to include the NekFactory header
#include <LibUtilities/BasicUtils/NekFactory.hpp>
The following code should then be included just before the base class
declaration (in the same namespace as the class):
class MyConcept
// Datatype for the MyConcept factory
typedef LibUtilities::NekFactory< std::string, MyConcept,
ParamType2 > MyConceptFactory;
MyConceptFactory& GetMyConceptFactory();
The template parameters define the datatype of the key used to retrieve a
particular implementation (usually a string, enum or custom class such as
\inlsh{MyConceptKey}, the base class (in our case \inlsh{MyConcept} and a list
of zero or more parameters which are taken by the constructors of all
implementations of the type \inlsh{MyConcept} (in our case we have two). Note
that all implementations must take the same parameter list in their constructors.
The normal definition of our base class then follows:
class MyConcept
MyConcept(ParamType1 p1, ParamType2 p2);
We must also define a shared pointer for our base class for use later
typedef boost::shared_ptr<MyConcept> MyConceptShPtr;
\subsubsection{Creating a specific implementation (derived class)}
A class is defined for each specific implementation of a concept. It is these
specific implementations which are instantiated by the factory.
In our example we will have an implementations called \inlsh{MyConceptImpl1}
defined in \inlsh{MyConceptImpl1.h} and \inlsh{MyConceptImpl1.cpp}. In the
header file we include the base class header file
#include <Subdir/MyConcept.h>
We then define the derived class as normal:
class MyConceptImpl1 : public MyConcept
In order for the factory to work, it must know
\item that {{{MyConceptImpl1}}} exists, and
\item how to create it.
To allow the factory to create instances of our class we define a function in
our class:
/// Creates an instance of this class
static MyConceptSharedPtr create(
ParamType1 p1,
ParamType2 p2)
return MemoryManager<MyConceptImpl1>::AllocateSharedPtr(p1, p2);
This function simply creates an instance of \inlsh{MyConceptImpl1} using the
supplied parameters. It must be \inlsh{static} because we are not operating on
an existing instance and it should return a base class shared pointer (rather
than a \inlsh{MyConceptImpl1} shared pointer), since the point of the factory
is that the calling code does not know about specific implementations.
The last task is to register our implementation with the factory. This is done
using the \inlsh{RegisterCreatorFunction} member function of the factory.
However, we wish this to happen as early on as possible (so we can use the
factory straight away) and without needing to explicitly call the function for
every implementation at the beginning of our program (since this would again
defeat the point of a factory)! The solution is to use the function to
initialise a static variable: it will be executed prior to the start of the
\inlsh{main()} routine, and can be located within the very class it is
registering, satisfying our code decoupling requirements.
In \inlsh{MyConceptImpl1.h} we define a static variable with the same datatype
as the key used in our factory (in our case \inlsh{std::string})
static std::string className;
The above variable can be \inlsh{private} since it is typically never actually
used within the code. We then initialise it in \inlsh{MyConceptImpl1.cpp}
string MyConceptImpl1::className
= GetMyConceptFactory().RegisterCreatorFunction(
"Impl1", MyConceptImpl1::create, "First implementation of my
The first parameter specifies the value of the key which should be used to
select this implementation. The second parameter is a function pointer to our
static function used to instantiate our class. The third parameter provides a
description which can be printed when listing the available MyConcept
\subsection{Instantiating classes}
To create instances of MyConcept implementations elsewhere in the code, we must
first include the ''base class'' header file
#include <Subdir/MyConcept.h>
Note we do not include the header files for the specific MyConcept
implementations anywhere in the code (apart from \inlsh{MyConceptImpl1.cpp}).
If we modify the implementation, only the implementation itself requires
recompiling and the executable relinking.
We create an instance by retrieving the \inlsh{MyConceptFactory} and call the
\inlsh{CreateInstance} member function of the factory:
ParamType p1 = ...;
ParamType p2 = ...;
MyConceptShPtr p = GetMyConceptFactory().CreateInstance( "Impl1", p1, p2 );
Note that the class is used through the pointer \inlsh{p}, which is of type
\inlsh{MyConceptShPtr}, allowing the use of any of the public interface
functions in the base class (and therefore the specific implementations behind them) to be
called, but not directly any functions declared solely in a specific
......@@ -29,6 +29,7 @@ openany, % A chapter may start on either a recto or verso page.
% \usepackage{tikz} % Figures
\usepackage{graphicx} % Include figures
......@@ -151,6 +152,8 @@ openany, % A chapter may start on either a recto or verso page.
\usepackage{listings} % Display code / shell commands
%\newcommand{\shellcommand}[1]{\begin{lstlisting} \#1 \end{lstlisting}
......@@ -194,7 +197,90 @@ openany, % A chapter may start on either a recto or verso page.
\newcommand{\inltt}[1]{\tikz[anchor=base,baseline]\node[inner sep=3pt,
rounded corners,outer sep=0,draw=black!30,fill=black!05]{\small\texttt{#1}};}
\newcommand{\inlsh}[1]{\tikz[anchor=base,baseline]\node[inner sep=2pt,
outer sep=0,fill=black!05]{\texttt{#1}};}
\newcommand{\nekpp}{{\em Nektar++}\xspace}
% Highlight box
\node[inner sep=5pt,fill=black!10,draw=black!30] (box)
\node[inner sep=5pt,fill=red!10,draw=black!30] (box)
\node[inner sep=5pt,fill=green!10,draw=black!30] (box)
\node[inner sep=5pt,fill=#3!10,draw=black!30] (box)
......@@ -289,13 +375,13 @@ Welcome to the developer's guide for Nektar++\cite{nektar-website}.
\chapter{Library Design}
A major challenge which arises when one aims to develop a software package that
implements the spectral/hp element method is to implement the mathematical
structure of the method in a digestible and coherent matter. Obviously, there
are many ways to encapsulate the fundamental concepts related to the spectral/hp
element method, depending on e.g. the intended goal of the developer or the
chosen programming language. We will (without going in too much detail) give a
an overview of how we have chosen to abstract and implement spectral/hp elements
in the \nekpp library. However, we want to emphasise that this is not the
only possible choice.
Five different sublibraries, employing this characteristic pattern, are provided
in the full \nekpp library:
\item the standard elemental region sublibrary (StdRegions library)
\item the parametric mapping sublibrary (SpatialDomains library)
\item the local elemental region sublibrary (LocalRegions library)
\item the global region sublibrary (MultiRegions library)
\item the supporting utilities sublibrary (LibUtilities library)
This structure can also be related to the formulation of a global spectral/hp
element expansion, i.e.
library}}}\hat{u}^e_n}_{\mbox{\scriptsize{StdRegions library}}}
A more detailed overview of the \nekpp structure, including an overview of
the most important classes per sublibrary, is depicted in the figure below.
This contains the underlying building blocks for constructing a Spectral Element
formulation including linear algebra, polynomial routines and memory management.
\item Basic Constants
\item Basic Utilities ( Nektar++ Arrays)
\item Expression Templates
\item Foundations
\item Interpreter
\item Kernel
\item Linear Algebra
\item Memory Management
\item Nodal Data
\item Polynomial Subroutines
\item Time Integration
\subsection{The Polylib library}
These are routines for orthogonal polynomial calculus and interpolation based on
codes by Einar Ronquist and Ron Henderson.
\item z - Set of collocation/quadrature points
\item w - Set of quadrature weights
\item D - Derivative matrix
\item h - Lagrange Interpolant
\item I - Interpolation matrix
\item g - Gauss
\item k - Kronrod
\item gr - Gauss-Radau
\item gl - Gauss-Lobatto
\item j - Jacobi
\item m - point at minus 1 in Radau rules
\item p - point at plus 1 in Radau rules
\subsubsection{Main routines}
Points and Weights
\item zwgj Compute Gauss-Jacobi points and weights
\item zwgrjm Compute Gauss-Radau-Jacobi points and weights (z=-1)
\item zwgrjp Compute Gauss-Radau-Jacobi points and weights (z= 1)
\item zwglj Compute Gauss-Lobatto-Jacobi points and weights
\item zwgk Compute Gauss-Kronrod-Jacobi points and weights
\item zwrk Compute Radau-Kronrod points and weights
\item zwlk Compute Lobatto-Kronrod points and weights
\item JacZeros Compute Gauss-Jacobi points and weights
Derivative Matrices
\item Dgj Compute Gauss-Jacobi derivative matrix
\item Dgrjm Compute Gauss-Radau-Jacobi derivative matrix (z=-1)
\item Dgrjp Compute Gauss-Radau-Jacobi derivative matrix (z= 1)
\item Dglj Compute Gauss-Lobatto-Jacobi derivative matrix
Lagrange Interpolants
\item hgj Compute Gauss-Jacobi Lagrange interpolants
\item hgrjm Compute Gauss-Radau-Jacobi Lagrange interpolants (z=-1)
\item hgrjp Compute Gauss-Radau-Jacobi Lagrange interpolants (z= 1)
\item hglj Compute Gauss-Lobatto-Jacobi Lagrange interpolants
Interpolation Operators
\item Imgj Compute interpolation operator gj->m
\item Imgrjm Compute interpolation operator grj->m (z=-1)
\item Imgrjp Compute interpolation operator grj->m (z= 1)
\item Imglj Compute interpolation operator glj->m
Polynomial Evaluation
\item jacobfd Returns value and derivative of Jacobi poly. at point z
\item jacobd Returns derivative of Jacobi poly. at point z (valid at z=-1,1)
\subsubsection{Local routines}
\item jacobz Returns Jacobi polynomial zeros
\item gammaf Gamma function for integer values and halves
\item RecCoeff Calculates the recurrence coefficients for orthogonal poly
\item TriQL QL algorithm for symmetrix tridiagonal matrix
\item JKMatrix Generates the Jacobi-Kronrod matrix
\subsubsection{Useful references}
\item `[`1`]` Gabor Szego: Orthogonal Polynomials, American Mathematical
Society, Providence, Rhode Island, 1939.
\item `[`2`]` Abramowitz \& Stegun: Handbook of Mathematical Functions, Dover,
New York, 1972.
\item `[`3`]` Canuto, Hussaini, Quarteroni \& Zang: Spectral Methods in Fluid
Dynamics, Springer-Verlag, 1988.
\item `[`4`]` Ghizzetti \& Ossicini: Quadrature Formulae, Academic Press, 1970.
\item `[`5`]` Karniadakis \& Sherwin: Spectral/hp element methods for CFD, 1999
\item Legendre polynomial $\alpha = \beta = 0$
\item Chebychev polynomial $\alpha = \beta = -0.5$
\item All routines are double precision.
\item All array subscripts start from zero, i.e. vector $0..N-1$