Implement custom coefficients using Python classes
masterTo use a Python function as a coefficient in MFEM, you must inherit from the appropriate PyMFEM coefficient classes and implement the EvalValue(self, x) method.
Available Classes:
mfem.PyCoefficient: For scalar coefficients.mfem.PyCoefficientT: Time-dependent scalar coefficients.mfem.VectorPyCoefficient: For vector coefficients.mfem.VectorPyCoefficientT: Time-dependent vector coefficients.
Implementation Pattern:
class MyCoefficient(mfem.PyCoefficient):
def EvalValue(self, x):
return 0.01 # Must return a float
class MyVectorCoefficient(mfem.VectorPyCoefficient):
def EvalValue(self, x):
return [0., 0., np.cos(x[2])] # Must return a list/arrayConstant Coefficients:
MatrixConstantCoefficient and VectorConstantCoefficient support natural Python objects (lists/nested lists) as input. The input is converted via np.array(x, dtype=float) internally.
class Jt(mfem.VectorPyCoefficient):
def EvalValue(self, x):
return [0.,0., np.cos(np.abs(x[2]-0.03)/0.03*np.pi/2)]
class Sigma(mfem.PyCoefficient):
def EvalValue(self, x):
return 0.01
Cof_Jt = Jt(3)
dd = mfem.VectorFEBoundaryTangentLFIntegrator(Cof_Jt)