diff --git a/src/kernels.jl b/src/kernels.jl index d738a4e7..9fd8bac0 100644 --- a/src/kernels.jl +++ b/src/kernels.jl @@ -173,13 +173,14 @@ function emit_kernels(wf::WeakForm; coeffs = true, expression = Val(false)) end """ - emit_dirichlet(analysis) + emit_dirichlet(analysis; expression = Val(false)) One `(piece, field, g)` per essential condition of `analysis`, with `g(X, T, P)` the prescribed value. +`expression = Val(true)` returns the generated `Expr` in place of each `g`. """ -function emit_dirichlet(analysis) +function emit_dirichlet(analysis; expression = Val(false)) s = analysis.slots - return Tuple((bc.piece, bc.field, compile(assert_differentiated(bc.g), (s.X, s.T, s.P))) for bc in analysis.essential) + return Tuple((bc.piece, bc.field, compile(assert_differentiated(bc.g), (s.X, s.T, s.P); expression)) for bc in analysis.essential) end """ diff --git a/test/Kernels/dirichlet_expression.jl b/test/Kernels/dirichlet_expression.jl new file mode 100644 index 00000000..f7a002a5 --- /dev/null +++ b/test/Kernels/dirichlet_expression.jl @@ -0,0 +1,35 @@ +# `emit_dirichlet(analysis; expression = Val(true))` returns the generated `Expr`s, which build the function the +# default call compiles. +import FiniteElementMethod +using FiniteElementMethod: emit_dirichlet +using FiniteElementMethod.RuntimeGeneratedFunctions: RuntimeGeneratedFunction +using Ferrite: Vec +using Symbolics +using Test + +include(joinpath(@__DIR__, "..", "shared", "strongform_seam.jl")) +using .StrongFormSeam: analyse, BC + +@variables t x y k +@variables u(..) +Dx, Dy, Dt = Differential(x), Differential(y), Differential(t) +U = u(t, x, y) + +@testset "expression form of the Dirichlet data round-trips" begin + eq = Dt(U) ~ Dx(k * Dx(U)) + Dy(k * Dy(U)) + # Constant, space-dependent and time-dependent prescribed values. + bcs = [BC("left", U ~ 1), BC("right", U ~ 0.1 * y + x), BC("top", U ~ sin(t) * x)] + an = analyse([eq], bcs; depvars = [U], t, sivs = [x, y], params = [k]) + compiled, generated = emit_dirichlet(an), emit_dirichlet(an; expression = Val(true)) + @test length(generated) == length(compiled) == 3 + P = [2.0] + for ((piece, field, g), (epiece, efield, ex)) in zip(compiled, generated) + @test (epiece, efield) == (piece, field) + @test ex isa Expr + evaluated = RuntimeGeneratedFunction(FiniteElementMethod, FiniteElementMethod, ex) + for _ in 1:5 + X, T = Vec(rand(), rand()), rand() + @test isequal(evaluated(X, T, P), g(X, T, P)) + end + end +end