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.CallbackOperatorType
CallbackOperator(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 be nothing if only the RHS is assembled).
    • b: Right-hand side vector (may be nothing if only the matrix is assembled).
    • args: Vector of solution blocks (typically FEVectorBlocks 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 of Unknowns or integer indices specifying which solution blocks are passed as args to the callback. Defaults to an empty array.

Keyword arguments:

  • linearized_dependencies: [uansatz, utest] when linearized. Default: auto

  • modifies_matrix: callback function modifies the matrix?. Default: true

  • modifies_rhs: callback function modifies the rhs?. Default: true

  • name: name for operator used in printouts. Default: ''CallbackOperator''

  • store: store matrix and rhs separately (and copy from there when reassembly is triggered). Default: false

  • time_dependent: operator is time-dependent ?. Default: false

  • verbosity: verbosity level. Default: 0

source

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.