walberla::python_coupling::PythonCallback Class Reference

Detailed Description

Run a Python function from C++ code.

Example:

python_coupling::PythonCallback callback ( "atEndOfTimestep" );
callback.data().exposePtr("blockStorage", blocks ); // blockStorage is available as global variable in script,
// the blockStorage can be modified from the python script
callback.data().exposeValue("someInteger", 42); // expose copy of variable to script
callback(); // run 'someFunction(data)' in script
// where data is a python dict that contains your exposed data
// Access data that was modified in script
int i = 0;
if ( callback.data().has<int>("integerSetInScript") ) {
i = callback.data().get<int>( "integerSetInScript" );
}

This example calls the python function "walberla_cpp.callbacks.atEndOfTimestep", which can be either set directly or using a decorator:

@waLBerla.callback( "atEndOfTimestep" )

The result of the function call will be stored in the data dictionary with the key "returnValue".

Depending on how many parameters the python function expects it is called in different ways:

  • no parameters: the data is not passed
  • multiple parameters: the data dictionary is unpacked i.e. yourFunction( **data )

#include <PythonCallback.h>

Public Member Functions

 PythonCallback ()
 
 PythonCallback (const std::string &functionName)
 
 PythonCallback (const std::string &moduleOrFile, const std::string &functionName, const std::vector< std::string > &argv=std::vector< std::string >())
 
DictWrapperdata ()
 
const DictWrapperdata () const
 
bool isCallable () const
 
void operator() ()
 

Protected Attributes

const std::string functionName_
 
shared_ptr< DictWrapperexposedVars_
 
shared_ptr< DictWrappercallbackDict_
 

Constructor & Destructor Documentation

◆ PythonCallback() [1/3]

walberla::python_coupling::PythonCallback::PythonCallback ( )

◆ PythonCallback() [2/3]

walberla::python_coupling::PythonCallback::PythonCallback ( const std::string &  functionName)

◆ PythonCallback() [3/3]

walberla::python_coupling::PythonCallback::PythonCallback ( const std::string &  moduleOrFile,
const std::string &  functionName,
const std::vector< std::string > &  argv = std::vector<std::string>() 
)

Member Function Documentation

◆ data() [1/2]

DictWrapper& walberla::python_coupling::PythonCallback::data ( )
inline

◆ data() [2/2]

const DictWrapper& walberla::python_coupling::PythonCallback::data ( ) const
inline

◆ isCallable()

bool walberla::python_coupling::PythonCallback::isCallable ( ) const

◆ operator()()

void walberla::python_coupling::PythonCallback::operator() ( )

Member Data Documentation

◆ callbackDict_

shared_ptr<DictWrapper> walberla::python_coupling::PythonCallback::callbackDict_
protected

◆ exposedVars_

shared_ptr<DictWrapper> walberla::python_coupling::PythonCallback::exposedVars_
protected

◆ functionName_

const std::string walberla::python_coupling::PythonCallback::functionName_
protected

The documentation for this class was generated from the following files: