Expressions

This package supports type-stable expression functions that can be parsed from strings at runtime. This enables arbitrary boundary condition support in compiled binaries. This feature heavily leverages the package DynamicExpressions.jl

ScalarExpressionFunction

julia> import FiniteElementContainers.Expressions: ScalarExpressionFunction
julia> using StaticArrays
julia> expr = "5.0 * exp(-2 * t) * cos(2 * pi * t)""5.0 * exp(-2 * t) * cos(2 * pi * t)"
julia> var_names = ["x", "y", "z", "t"]4-element Vector{String}: "x" "y" "z" "t"
julia> func = ScalarExpressionFunction{Float64}(expr, var_names)(::FiniteElementContainers.Expressions.ScalarExpressionFunction{Float64}) (generic function with 4 methods)
julia> X = SVector{3, Float64}(1., 2., 3.)3-element StaticArraysCore.SVector{3, Float64} with indices SOneTo(3): 1.0 2.0 3.0
julia> t = 5.05.0
julia> val = func(X, t)0.00022699964881242428

VectorExpressionFunction

julia> import FiniteElementContainers.Expressions: VectorExpressionFunction
julia> using StaticArrays
julia> exprs = ["0", "5.0 * exp(-2 * t) * cos(2 * pi * t)", "0"]3-element Vector{String}: "0" "5.0 * exp(-2 * t) * cos(2 * pi * t)" "0"
julia> var_names = ["x", "y", "z", "t"]4-element Vector{String}: "x" "y" "z" "t"
julia> func = VectorExpressionFunction{3, Float64}(exprs, var_names)(::FiniteElementContainers.Expressions.VectorExpressionFunction{3, Float64}) (generic function with 4 methods)
julia> X = SVector{3, Float64}(1., 2., 3.)3-element StaticArraysCore.SVector{3, Float64} with indices SOneTo(3): 1.0 2.0 3.0
julia> t = 5.05.0
julia> val = func(X, t)3-element StaticArraysCore.SVector{3, Float64} with indices SOneTo(3): 0.0 0.00022699964881242428 0.0

API

FiniteElementContainers.Expressions.FlatNodeType
struct FlatNode{T<:Number}
  • degree::UInt8

  • op::UInt8

  • constant::Bool

  • val::Number

  • feature::UInt16

  • l_idx::UInt16

  • r_idx::UInt16

One node of a flattened expression tree. Children are referenced by 1-based index into the parent array; index 0 marks "no child". Leaves carry either a constant val or a feature (1-based variable index).

source
FiniteElementContainers.Expressions.ScalarExpressionFunctionType
struct ScalarExpressionFunction{T<:Number} <: FiniteElementContainers.Expressions.AbstractExpressionFunction{T<:Number, FiniteElementContainers.Expressions.FlatNode{T<:Number}, @NamedTuple{operators::DynamicExpressions.OperatorEnumModule.OperatorEnum{Tuple{Tuple{typeof(cos), typeof(cosh), typeof(exp), typeof(log), typeof(FiniteElementContainers.Expressions._minus), typeof(sin), typeof(sinh), typeof(sqrt), typeof(tan), typeof(tanh)}, Tuple{typeof(+), typeof(-), typeof(*), typeof(/), typeof(^)}}}, var_names::Vector{String}}}
  • nodes::NTuple{256, FiniteElementContainers.Expressions.FlatNode{T}} where T<:Number

  • n_active::UInt16

  • num_vars::UInt8

Scalar expression function as a flat, isbits value — usable as a KernelAbstractions kernel argument and trim-mode safe under juliac. The trailing variable is conventionally time; FEC's juliac-safe DirichletBCs constructor uses num_vars as the time-derivative index.

source
FiniteElementContainers.Expressions.VectorExpressionFunctionType
  • exprs::StaticArraysCore.SArray{Tuple{N}, FiniteElementContainers.Expressions.ScalarExpressionFunction{T}, 1, N} where {N, T<:Number}

  • num_vars::Int64

struct VectorExpressionFunction{N, T<:Number} <: FiniteElementContainers.Expressions.AbstractExpressionFunction{T<:Number, DynamicExpressions.NodeModule.Node{T<:Number, 2}, @NamedTuple{operators::DynamicExpressions.OperatorEnumModule.OperatorEnum{Tuple{Tuple{typeof(cos), typeof(cosh), typeof(exp), typeof(log), typeof(FiniteElementContainers.Expressions._minus), typeof(sin), typeof(sinh), typeof(sqrt), typeof(tan), typeof(tanh)}, Tuple{typeof(+), typeof(-), typeof(*), typeof(/), typeof(^)}}}, var_names::Vector{String}}}
source
FiniteElementContainers.Expressions.differentiateMethod
differentiate(
    f::FiniteElementContainers.Expressions.ScalarExpressionFunction{T},
    var_names::AbstractVector{<:AbstractString},
    var_name::AbstractString
) -> FiniteElementContainers.Expressions.ScalarExpressionFunction

Convenience overload that resolves var_name against an explicit var_names list, then delegates to the integer form. Useful when the caller still has the var-name list in scope (typical at TOML parse time); runtime hot paths should call the integer form directly.

source
FiniteElementContainers.Expressions.differentiateMethod
differentiate(
    f::FiniteElementContainers.Expressions.ScalarExpressionFunction{T},
    var_idx::Integer
) -> FiniteElementContainers.Expressions.ScalarExpressionFunction

Return the symbolic derivative of f with respect to the variable whose 1-based feature index is var_idx. Differentiation is implemented as a recursive tree rewrite over FEC's closed grammar (10 unary + 5 binary operators) — no dependency on ForwardDiff, Zygote, or Symbolics.

The result is a fresh ScalarExpressionFunction over the same variable slots; the trailing variable is conventionally time.

source