Cantera/Extending C API
From charlesreid1
This article discusses extending Cantera's C API. This allows you to create a new class or function in Cantera, and create a handle for that object or function from Cantera's Python API.
To illustrate this procedure, I'll be adding a method to Cantera's ReactorBase class to remove all of the walls in a reactor. This article covers the following steps:
- Adding a C++ function to the ReactorBase object
- Extending Cantera's C API to include the new function
- Extending Cantera's Python-C interface to utilize the new function
- Calling the method from Python
Add New C++ Method
I'll add the following two methods to ReactorBase.cpp:
===================================================================
--- ReactorBase.cpp (revision 2)
+++ ReactorBase.cpp (working copy)
@@ -72,6 +72,30 @@
m_nwalls++;
}
+void ReactorBase::removeWall(Wall& w)
+{
+ vector_int::iterator iL = m_lr.begin();
+ for( std::vector<Wall*>::iterator iW = m_wall.begin();
+ iW != m_wall.end();
+ ++iW, ++iL ) {
+ if ( (*iW) == &w) {
+ m_wall.erase( iW );
+ m_lr.erase( iL );
+ }
+ }
+}
+
+void ReactorBase::removeWalls()
+{
+ vector_int::iterator iL = m_lr.begin();
+ for( std::vector<Wall*>::iterator iW = m_wall.begin();
+ iW != m_wall.end();
+ ++iW, ++iL ) {
+ m_wall.erase(iW);
+ m_lr.erase(iL);
+ }
+}
+
Wall& ReactorBase::wall(size_t n)
{
return *m_wall[n];
and modify the header file as follows:
===================================================================
--- include/cantera/zeroD/ReactorBase.h (revision 2)
+++ include/cantera/zeroD/ReactorBase.h (working copy)
@@ -91,6 +91,11 @@
}
void addWall(Wall& w, int lr);
+
+ void removeWall(Wall& w);
+
+ void removeWalls();
+
Wall& wall(size_t n);