CallbackOperator
CallbackOperator provides a flexible interface for injecting custom assembly logic into the finite element workflow. It delegates the assembly of the system matrix and right-hand side to a user-supplied callback function, allowing for advanced or nonstandard modifications that are difficult to express with standard operators.
Constructor
ExtendableFEM.CallbackOperator — TypeCallbackOperator(callback; ...) -> CallbackOperator
CallbackOperator(
callback,
u_args;
kwargs...
) -> CallbackOperator{_A, _B, _C, Nothing} where {_A<:Union{Integer, Unknown}, _B, _C}
Creates an operator that delegates matrix and right-hand side assembly to a user-supplied callback function.
Arguments
callback!::Function: User-defined function called during assembly. Must have the signature:callback!(A, b, args; assemble_matrix=true, assemble_rhs=true, time=0, kwargs...)where:A: Matrix to assemble into (may benothingif only the RHS is assembled).b: Right-hand side vector (may benothingif only the matrix is assembled).args: Vector of solution blocks (typicallyFEVectorBlocks or similar).assemble_matrix,assemble_rhs: Booleans indicating which objects to assemble.time: Current time (for time-dependent problems).kwargs...: Additional keyword arguments.
u_args: (optional) Array ofUnknowns or integer indices specifying which solution blocks are passed asargsto the callback. Defaults to an empty array.
Keyword arguments:
linearized_dependencies: [uansatz, utest] when linearized. Default: automodifies_matrix: callback function modifies the matrix?. Default: truemodifies_rhs: callback function modifies the rhs?. Default: truename: name for operator used in printouts. Default: ''CallbackOperator''store: store matrix and rhs separately (and copy from there when reassembly is triggered). Default: falsetime_dependent: operator is time-dependent ?. Default: falseverbosity: verbosity level. Default: 0
Example
function my_callback!(A, b, args; assemble_matrix=true, assemble_rhs=true, time=0, kwargs...)
# Example: add a constant to the diagonal
if assemble_matrix && A !== nothing
for i in 1:min(size(A)...)
A[i, i] += 1.0
end
end
if assemble_rhs && b !== nothing
b .+= 2.0
end
end
op = CallbackOperator(my_callback!; u_args=[1], name="CustomOp")When to Use
- Custom or experimental PDE terms
- Coupling to external codes or data
- Advanced boundary or interface conditions
- Prototyping new assembly strategies
See also: Example265 for a practical use case.