G+Smo  25.01.0
Geometry + Simulation Modules
 
Loading...
Searching...
No Matches
Python via cppyy

The cppyy-bindings are generated at runtime based on just-in-time compilation. For using G+Smo this is useful, since the bilinear forms are usually assembled using the expression assembler. Since gismo::gsExprAssembler is based on templated expressions, its assembly method can only be compiled once the bilinear form has been defined, which makes precompiled bindings impossible unless we restrict the available bilinear forms. Cppyy's just-in-time compilation solves this problem.

Configuration

Configure G+Smo with the options -DGISMO_WITH_CPPYY=On -DCMAKE_CXX_STANDARD=20. This creates the Python package gismo_cppyy in the subdirectory cppyy of your build folder.

Then, build the gismo library as usual.

Run make wheel in order to generate the pip wheel in cppyy/dist. It can be installed with make install-bindings or directly using pip.

The package looks for the G+Smo sources in the source directory and for the shared library in the build directory. This means that these directories need to stay in place.

Usage

The bindings can be imported in Python using

from gismo_cppyy import gismo

All classes and functions from G+Smo can be found in the gismo namespace, templates are resolved using brackets containing either a string or a Python type. For example, you can access the expression assembler using

A = gismo.gsExprAssembler["real_t"]()
Definition gsExprAssembler.h:33

and, after defining the spaces and variables, assemble the matrix using

A.assemble(gismo.expr.igrad(u, G) * gismo.expr.igrad(u, G).tr() * gismo.expr.meas(G), u * ff * gismo.expr.meas(G))
EIGEN_STRONG_INLINE meas_expr< T > meas(const gsGeometryMap< T > &G)
The measure of a geometry map.
Definition gsExpressions.h:4555

just as in C++. Before evaluating the assemble() method, the just-in-time compiler will compile the templated method

template<class... expr> void assemble(const expr &... args);

of gismo::gsExprAssembler for the employed expressions.

An example for solving the Poisson equation can be found in python_examples/poisson_example_cppyy.py. An additional example for adaptive fitting using THB-splines is in python_examples/fitting_example_cppyy.py.

Numpy compatibility

Data can be exchanged with numpy by converting numpy arrays into gismo objects and vice versa. The conversion from gismo objects to numpy arrays is done using the method tonumpy() of gismo.gsVector, gismo.gsMatrix and gismo.gsAsMatrix. The class method fromnumpy() of these classes constructs a gismo object from a numpy array.

For example, you can run

>>> from gismo_cppyy import gismo
>>> import numpy as np
>>> a = np.array([[1,0],[0,1]], dtype=np.double)
>>> A = gismo.gsMatrix["double"].fromnumpy(a)
>>> b = A.tonumpy()
>>> B = gismo.gsAsMatrix["double"].fromnumpy(b)
Creates a mapped object or data pointer to a matrix without copying data.
Definition gsAsMatrix.h:32
A matrix with arbitrary coefficient type and fixed or dynamic size.
Definition gsMatrix.h:41

Note that gismo.gsMatrix["double"].fromnumpy() copies the underlying data, while gismo.gsAsMatrix["double"].fromnumpy() does not. tonumpy() never copies the data.

Further information and examples

Todo:
@felixfeliz