18.1 Features and functionality

NekPy uses the Boost.Python library to provide a set of high-quality, hand-written Python bindings for selected functions and classes in Nektar++.

It is worth noting that Python (CPython, the standard Python implementation written in C, in particular) includes C API and that everything in Python is strictly speaking a C structure called PyObject. Hence, defining a new class, method etc. in Python is in reality creating a new PyObject structure.

Boost.Python is essentially a wrapper for Python C API which conveniently exports C++ classes and methods into PyObjects. At compilation time a dynamic library is created which is then imported to Python, as shown in Figure 18.1.


PIC

Figure 18.1 A schematic diagram of how C++ code is converted into Python with Boost.Python [53]


A typical snippet could look something like:


Listing 18.1: NekPy sample snippet
1from NekPy.LibUtilities import PointsKey, PointsType, BasisKey, BasisType 
2from NekPy.StdRegions import StdQuadExp 
3import numpy as np 
4numModes = 8 
5numPts   = 9 
6ptsKey   = PointsKey(numPts, PointsType.GaussLobattoLegendre) 
7basisKey = BasisKey(BasisType.Modified_A, numModes, ptsKey) 
8quadExp  = StdQuadExp(basisKey, basisKey) 
9x, y     = quadExp.GetCoords() 
10fx       = np.sin(x) * np.cos(y) 
11proj     = quadExp.FwdTrans(fx)

NekPy uses the Boost.NumPy library, contained in Boost 1.63+, to automatically convert C++ Array<OneD, > objects to and from the commonly-used numpy.ndarray object, which makes the integration more seamless between Python and C++.