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

A trampoline class for mfem::Matrix. More...

#include <PyMatrix.h>

Inheritance diagram for serif::pybind::PyMatrix:

Public Member Functions

mfem::real_t & Elem (int i, int j) override
 Access element (i,j) for read/write. Pure virtual in mfem::Matrix. Must be overridden in Python.
 
const mfem::real_t & Elem (int i, int j) const override
 Access element (i,j) for read-only. Pure virtual in mfem::Matrix. Must be overridden in Python.
 
mfem::MatrixInverse * Inverse () const override
 Get the inverse of the matrix. Pure virtual in mfem::Matrix. Must be overridden in Python. The caller is responsible for deleting the returned MatrixInverse object.
 
void Finalize (int skip_zeros) override
 Finalize matrix assembly. For sparse matrices, this typically involves finalizing the sparse structure. Can be overridden in Python.
 
void Mult (const mfem::Vector &x, mfem::Vector &y) const override
 Perform the operator action: y = A*x. Inherited from mfem::Operator. Can be overridden in Python. If not overridden, mfem::Matrix's default implementation is used.
 
void MultTranspose (const mfem::Vector &x, mfem::Vector &y) const override
 Perform the transpose operator action: y = A^T*x. Inherited from mfem::Operator. Can be overridden in Python.
 
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). Inherited from mfem::Operator. Can be overridden in Python.
 
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). Inherited from mfem::Operator. Can be overridden in Python.
 
mfem::Operator & GetGradient (const mfem::Vector &x) const override
 Get the gradient operator (Jacobian) at a given point x. Inherited from mfem::Operator. Can be overridden in Python. For a linear matrix operator, the gradient is typically the matrix itself.
 
void AssembleDiagonal (mfem::Vector &diag) const override
 Assemble the diagonal of the operator. Inherited from mfem::Operator. Can be overridden in Python.
 
const mfem::Operator * GetProlongation () const override
 Get the prolongation operator. Inherited from mfem::Operator. Can be overridden in Python.
 
const mfem::Operator * GetRestriction () const override
 Get the restriction operator. Inherited from mfem::Operator. Can be overridden in Python.
 
void RecoverFEMSolution (const mfem::Vector &X, const mfem::Vector &b, mfem::Vector &x) override
 Recover the FEM solution. Inherited from mfem::Operator. Can be overridden in Python.
 

Detailed Description

A trampoline class for mfem::Matrix.

This class allows Python classes to inherit from mfem::Matrix and correctly override its virtual functions, including those inherited from its base, mfem::Operator. This is useful for creating custom matrix types in Python that can interact seamlessly with MFEM's C++ components.

See also
mfem::Matrix
mfem::Operator
PyOperator
Python Usage Example:
import mfem.ser_ext as mfem
import numpy as np
class MyPythonMatrix(mfem.Matrix):
def __init__(self, size=0):
super().__init__(size) # Call base C++ constructor
if size > 0:
# Example: Initialize with some data if size is provided
# Note: Direct data manipulation might be complex due to
# C++ ownership. This example is conceptual.
# For a real-world scenario, you might manage data in Python
# and use Eval or Mult to reflect its state.
self._py_data = np.zeros((size, size))
else:
self._py_data = None
# --- mfem.Matrix pure virtual overrides ---
def Elem(self, i, j):
# This is problematic for direct override for write access
# due to returning a reference.
# Typically, you'd manage data and use it in Mult/GetRow etc.
# For read access (const version), it's more straightforward.
if self._py_data is not None:
return self._py_data[i, j]
raise IndexError("Matrix data not initialized or index out of bounds")
# const Elem version for read access
# def GetElem(self, i, j): # A helper in Python if Elem is tricky
# return self._py_data[i,j]
def Inverse(self):
# Return an mfem.MatrixInverse object
# This is a simplified example; a real inverse is complex.
print("MyPythonMatrix.Inverse() called, returning dummy inverse.")
# For a real implementation, you'd compute and return an actual
# mfem.MatrixInverse or a Python-derived version of it.
# This might involve creating a new PyMatrix representing the inverse.
identity_inv = mfem.DenseMatrix(self.Width())
for i in range(self.Width()):
identity_inv[i,i] = 1.0
return mfem.MatrixInverse(identity_inv) # Example
# --- mfem.Matrix regular virtual overrides ---
def Finalize(self, skip_zeros=1):
super().Finalize(skip_zeros) # Call base
print(f"MyPythonMatrix.Finalize({skip_zeros}) called.")
# --- mfem.Operator virtual overrides ---
def Mult(self, x, y):
# x is mfem.Vector (input), y is mfem.Vector (output)
if self._py_data is not None:
x_np = x.GetDataArray()
y_np = np.dot(self._py_data, x_np)
y.SetSize(self._py_data.shape[0])
y.Assign(y_np)
else:
# Fallback to base class if no Python data
# super().Mult(x,y) # This might not work as expected if base is abstract
# or if the C++ mfem.Matrix itself doesn't have data.
# For a sparse matrix, this would call its sparse Mult.
# For a dense matrix, it would use its data.
# If this PyMatrix is purely Python defined, it must implement Mult.
raise NotImplementedError("Mult not implemented or data not set")
print("MyPythonMatrix.Mult called.")
# Using the Python Matrix
mat_size = 3
py_mat = MyPythonMatrix(mat_size)
py_mat._py_data = np.array([[1,2,3],[4,5,6],[7,8,9]], dtype=float)
x_vec = mfem.Vector(mat_size)
x_vec.Assign(np.array([1,1,1], dtype=float))
y_vec = mfem.Vector(mat_size)
py_mat.Mult(x_vec, y_vec)
print("Result y:", y_vec.GetDataArray())
# inv_op = py_mat.Inverse() # Be cautious with dummy implementations
# print("Inverse type:", type(inv_op))
# Note: Overriding Elem(i,j) to return a C++ reference from Python
# for write access (mfem::real_t&) is complex and often not directly feasible
# in a straightforward way with pybind11 for typical Python data structures.
# Python users would typically interact via methods like Set, Add, or by
# providing data that the C++ side can access (e.g., via GetData).
# The const version of Elem (read-only) is more manageable.

Definition at line 118 of file PyMatrix.h.

Member Function Documentation

◆ AddMult()

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

Perform the action y += a*(A*x). Inherited from mfem::Operator. Can be overridden in Python.

Parameters
xThe input vector.
yThe vector to which a*(A*x) is added.
aScalar multiplier (defaults to 1.0).
Note
PYBIND11_OVERRIDE is used in the .cpp file.

Definition at line 70 of file PyMatrix.cpp.

◆ AddMultTranspose()

void serif::pybind::PyMatrix::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). Inherited from mfem::Operator. Can be overridden in Python.

Parameters
xThe input vector.
yThe vector to which a*(A^T*x) is added.
aScalar multiplier (defaults to 1.0).
Note
PYBIND11_OVERRIDE is used in the .cpp file.

Definition at line 81 of file PyMatrix.cpp.

◆ AssembleDiagonal()

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

Assemble the diagonal of the operator. Inherited from mfem::Operator. Can be overridden in Python.

Parameters
diagOutput vector to store the diagonal entries.
Note
PYBIND11_OVERRIDE is used in the .cpp file.

Definition at line 101 of file PyMatrix.cpp.

◆ Elem() [1/2]

const mfem::real_t & serif::pybind::PyMatrix::Elem ( int i,
int j ) const
override

Access element (i,j) for read-only. Pure virtual in mfem::Matrix. Must be overridden in Python.

Parameters
iRow index.
jColumn index.
Returns
Const reference to the matrix element.
Note
PYBIND11_OVERRIDE_PURE is used in the .cpp file.

Definition at line 18 of file PyMatrix.cpp.

◆ Elem() [2/2]

mfem::real_t & serif::pybind::PyMatrix::Elem ( int i,
int j )
override

Access element (i,j) for read/write. Pure virtual in mfem::Matrix. Must be overridden in Python.

Parameters
iRow index.
jColumn index.
Returns
Reference to the matrix element.
Note
Returning a C++ reference from Python for write access can be complex. Consider alternative data management strategies in Python. PYBIND11_OVERRIDE_PURE is used in the .cpp file.

Definition at line 8 of file PyMatrix.cpp.

◆ Finalize()

void serif::pybind::PyMatrix::Finalize ( int skip_zeros)
override

Finalize matrix assembly. For sparse matrices, this typically involves finalizing the sparse structure. Can be overridden in Python.

Parameters
skip_zerosSee mfem::SparseMatrix::Finalize documentation.
Note
PYBIND11_OVERRIDE is used in the .cpp file.

Definition at line 38 of file PyMatrix.cpp.

◆ GetGradient()

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

Get the gradient operator (Jacobian) at a given point x. Inherited from mfem::Operator. Can be overridden in Python. For a linear matrix operator, the gradient is typically the matrix itself.

Parameters
xThe point at which to evaluate the gradient (often unused for linear operators).
Returns
A reference to the gradient operator.
Note
PYBIND11_OVERRIDE is used in the .cpp file.

Definition at line 92 of file PyMatrix.cpp.

◆ GetProlongation()

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

Get the prolongation operator. Inherited from mfem::Operator. Can be overridden in Python.

Returns
A const pointer to the prolongation operator, or nullptr if not applicable.
Note
PYBIND11_OVERRIDE is used in the .cpp file.

Definition at line 110 of file PyMatrix.cpp.

◆ GetRestriction()

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

Get the restriction operator. Inherited from mfem::Operator. Can be overridden in Python.

Returns
A const pointer to the restriction operator, or nullptr if not applicable.
Note
PYBIND11_OVERRIDE is used in the .cpp file.

Definition at line 118 of file PyMatrix.cpp.

◆ Inverse()

mfem::MatrixInverse * serif::pybind::PyMatrix::Inverse ( ) const
override

Get the inverse of the matrix. Pure virtual in mfem::Matrix. Must be overridden in Python. The caller is responsible for deleting the returned MatrixInverse object.

Returns
Pointer to an mfem::MatrixInverse object.
Note
PYBIND11_OVERRIDE_PURE is used in the .cpp file.

Definition at line 28 of file PyMatrix.cpp.

◆ Mult()

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

Perform the operator action: y = A*x. Inherited from mfem::Operator. Can be overridden in Python. If not overridden, mfem::Matrix's default implementation is used.

Parameters
xThe input vector.
yThe output vector (result of A*x).
Note
PYBIND11_OVERRIDE is used in the .cpp file.

Definition at line 49 of file PyMatrix.cpp.

◆ MultTranspose()

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

Perform the transpose operator action: y = A^T*x. Inherited from mfem::Operator. Can be overridden in Python.

Parameters
xThe input vector.
yThe output vector (result of A^T*x).
Note
PYBIND11_OVERRIDE is used in the .cpp file.

Definition at line 60 of file PyMatrix.cpp.

◆ RecoverFEMSolution()

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

Recover the FEM solution. Inherited from mfem::Operator. Can be overridden in Python.

Parameters
XThe reduced solution vector.
bThe right-hand side vector.
xOutput vector for the full FEM solution.
Note
PYBIND11_OVERRIDE is used in the .cpp file.

Definition at line 126 of file PyMatrix.cpp.


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