SERiF 0.0.1a
3+1D Stellar Structure and Evolution
Loading...
Searching...
No Matches
serif::pybind::PyOperator Class Reference

Trampoline class for mfem::Operator. More...

#include <PyOperator.h>

Inheritance diagram for serif::pybind::PyOperator:

Public Member Functions

void Mult (const mfem::Vector &x, mfem::Vector &y) const override
 Perform the operator action: y = A*x.
 
void MultTranspose (const mfem::Vector &x, mfem::Vector &y) const override
 Perform the transpose operator action: y = A^T*x.
 
void AddMult (const mfem::Vector &x, mfem::Vector &y, const mfem::real_t a=1.0) const override
 Perform the action y += a*(A*x).
 
void AddMultTranspose (const mfem::Vector &x, mfem::Vector &y, const mfem::real_t a=1.0) const override
 Perform the action y += a*(A^T*x).
 
Operator & GetGradient (const mfem::Vector &x) const override
 Get the gradient operator (Jacobian) at a given point x.
 
void AssembleDiagonal (mfem::Vector &diag) const override
 Assemble the diagonal of the operator.
 
const mfem::Operator * GetProlongation () const override
 Get the prolongation operator.
 
const mfem::Operator * GetRestriction () const override
 Get the restriction operator.
 
void RecoverFEMSolution (const mfem::Vector &X, const mfem::Vector &b, mfem::Vector &x) override
 Recover the FEM solution.
 

Detailed Description

Trampoline class for mfem::Operator.

This class allows Python classes to inherit from mfem::Operator and correctly override its virtual functions. When a virtual function is called from C++, the trampoline ensures the call is forwarded to the Python implementation if one exists. This is crucial for integrating Python-defined linear or non-linear operators into MFEM's C++-based solvers and algorithms.

See also
mfem::Operator
Python Usage Example:
import mfem.ser_ext as mfem
import numpy as np
class MyPythonOperator(mfem.Operator):
def __init__(self, size):
super().__init__(size) # Call the base C++ constructor
# Or super().__init__() if default constructing and setting height/width later
self.matrix = np.random.rand(size, size) # Example: store a dense matrix
# Must override Mult
def Mult(self, x, y):
# x is an mfem.Vector (input)
# y is an mfem.Vector (output, y = A*x)
# Ensure y is correctly sized if not already
if y.Size() != self.Height():
y.SetSize(self.Height())
# Example: y = self.matrix * x
# This is a conceptual illustration. For actual matrix-vector products
# with numpy, you'd convert mfem.Vector to numpy array or iterate.
x_np = x.GetDataArray() # Get a numpy view if configured
y_np = np.dot(self.matrix, x_np)
y.Assign(y_np) # Assign result back to mfem.Vector
print("MyPythonOperator.Mult called")
# Optionally override other methods like MultTranspose, GetGradient, etc.
def MultTranspose(self, x, y):
if y.Size() != self.Width():
y.SetSize(self.Width())
# Example: y = self.matrix.T * x
x_np = x.GetDataArray()
y_np = np.dot(self.matrix.T, x_np)
y.Assign(y_np)
print("MyPythonOperator.MultTranspose called")
# Using the Python operator
op_size = 5
py_op = MyPythonOperator(op_size)
x_vec = mfem.Vector(op_size)
x_vec.Assign(np.arange(op_size, dtype=float))
y_vec = mfem.Vector(op_size)
py_op.Mult(x_vec, y_vec)
print("Result y:", y_vec.GetDataArray())
# py_op can now be passed to MFEM functions expecting an mfem::Operator

Definition at line 83 of file PyOperator.h.

Member Function Documentation

◆ AddMult()

void serif::pybind::PyOperator::AddMult ( const mfem::Vector & x,
mfem::Vector & y,
const mfem::real_t a = 1.0 ) const
override

Perform the action y += a*(A*x).

Optional override.

Parameters
xThe input vector.
yThe vector to which a*(A*x) is added.
aScalar multiplier (defaults to 1.0).
Note
This method forwards the call to the Python override if one exists.

Definition at line 33 of file PyOperator.cpp.

◆ AddMultTranspose()

void serif::pybind::PyOperator::AddMultTranspose ( const mfem::Vector & x,
mfem::Vector & y,
const mfem::real_t a = 1.0 ) const
override

Perform the action y += a*(A^T*x).

Optional override.

Parameters
xThe input vector.
yThe vector to which a*(A^T*x) is added.
aScalar multiplier (defaults to 1.0).
Note
This method forwards the call to the Python override if one exists.

Definition at line 44 of file PyOperator.cpp.

◆ AssembleDiagonal()

void serif::pybind::PyOperator::AssembleDiagonal ( mfem::Vector & diag) const
override

Assemble the diagonal of the operator.

For discrete operators (e.g., matrices), this method should compute and store the diagonal entries of the operator in the vector diag. Optional override.

Parameters
diagOutput vector to store the diagonal entries.
Note
This method forwards the call to the Python override if one exists.

Definition at line 64 of file PyOperator.cpp.

◆ GetGradient()

mfem::Operator & serif::pybind::PyOperator::GetGradient ( const mfem::Vector & x) const
override

Get the gradient operator (Jacobian) at a given point x.

For non-linear operators, this method should return the linearization (Jacobian) of the operator at the point x. The returned Operator is owned by this Operator and should not be deleted by the caller. Optional override.

Parameters
xThe point at which to evaluate the gradient.
Returns
A reference to the gradient operator.
Note
This method forwards the call to the Python override if one exists.

Definition at line 55 of file PyOperator.cpp.

◆ GetProlongation()

const mfem::Operator * serif::pybind::PyOperator::GetProlongation ( ) const
override

Get the prolongation operator.

Used in multilevel methods (e.g., AMG). Returns a pointer to the prolongation operator (interpolation from a coarser level to this level). The returned Operator is typically owned by this Operator. Optional override.

Returns
A const pointer to the prolongation operator, or nullptr if not applicable.
Note
This method forwards the call to the Python override if one exists.

Definition at line 73 of file PyOperator.cpp.

◆ GetRestriction()

const mfem::Operator * serif::pybind::PyOperator::GetRestriction ( ) const
override

Get the restriction operator.

Used in multilevel methods (e.g., AMG). Returns a pointer to the restriction operator (projection from this level to a coarser level). Typically, this is the transpose of the prolongation operator. The returned Operator is typically owned by this Operator. Optional override.

Returns
A const pointer to the restriction operator, or nullptr if not applicable.
Note
This method forwards the call to the Python override if one exists.

Definition at line 81 of file PyOperator.cpp.

◆ Mult()

void serif::pybind::PyOperator::Mult ( const mfem::Vector & x,
mfem::Vector & y ) const
override

Perform the operator action: y = A*x.

This is a pure virtual function in mfem::Operator and must be overridden by any Python class inheriting from PyOperator.

Parameters
xThe input vector.
yThe output vector (result of A*x).
Note
This method forwards the call to the Python override. PYBIND11_OVERRIDE_PURE is used in the .cpp file to handle this.

Definition at line 8 of file PyOperator.cpp.

◆ MultTranspose()

void serif::pybind::PyOperator::MultTranspose ( const mfem::Vector & x,
mfem::Vector & y ) const
override

Perform the transpose operator action: y = A^T*x.

Optional override. If not overridden, MFEM's base implementation (which may raise an error or be a no-op) will be used.

Parameters
xThe input vector.
yThe output vector (result of A^T*x).
Note
This method forwards the call to the Python override if one exists. PYBIND11_OVERRIDE is used in the .cpp file to handle this.

Definition at line 23 of file PyOperator.cpp.

◆ RecoverFEMSolution()

void serif::pybind::PyOperator::RecoverFEMSolution ( const mfem::Vector & X,
const mfem::Vector & b,
mfem::Vector & x )
override

Recover the FEM solution.

For operators that are part of a system solve (e.g., static condensation), this method can be used to reconstruct the full finite element solution x from a reduced solution X and the right-hand side b. Optional override.

Parameters
XThe reduced solution vector.
bThe right-hand side vector.
xOutput vector for the full FEM solution.
Note
This method forwards the call to the Python override if one exists.

Definition at line 89 of file PyOperator.cpp.


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