bcube-project / Bcube.jl

MIT License
4 stars 2 forks source link

Gradient of LazyOp containing a test function #34

Open bmxam opened 9 months ago

bmxam commented 9 months ago

The following example fails, I don't know if it's hard to fix...

module mwe
using Bcube
using StaticArrays
using LinearAlgebra

mesh = one_cell_mesh(:quad)
dΩ = Measure(CellDomain(mesh), 2)

U = TrialFESpace(FunctionSpace(:Lagrange, 1), mesh; size = 2)
V = TestFESpace(U)

c = CellInfo(mesh, 1)

assemble_linear(v -> ∫(∇(2 * v) ⋅ SA[1, 0])dΩ, V)

end
ghislainb commented 7 months ago

The following expression works with v0.1.3 (and with size=1 for U):

v -> ∫(2 * ∇(v) ⋅ SA[1, 0])dΩ

Currently, it is not recommended (and not well supported!) to combine operations on a test function v before taking its gradient because there is a specific processing with MapOver on ∇(v). For CellFunction, the gradient of combined operations is supported and there should be no problem. For example:

u = FEFunction(U)
a = assemble_linear(v -> ∫(∇(v) ⋅ ∇(2 * u))dΩ, V)
b = assemble_linear(v -> ∫(∇(v) ⋅ ∇(u))dΩ, V)
@assert all(a .≈ 2 .* b) # true

Is this ok for you?

bmxam commented 7 months ago

The following expression works with v0.1.3 (and with size=1 for U):

v -> ∫(2 * ∇(v) ⋅ SA[1, 0])dΩ

Currently, it is not recommended (and not well supported!) to combine operations on a test function v before taking its gradient because there is a specific processing with MapOver on ∇(v). For CellFunction, the gradient of combined operations is supported and there should be no problem. For example:

u = FEFunction(U)
a = assemble_linear(v -> ∫(∇(v) ⋅ ∇(2 * u))dΩ, V)
b = assemble_linear(v -> ∫(∇(v) ⋅ ∇(u))dΩ, V)
@assert all(a .≈ 2 .* b) # true

Is this ok for you?

Yes it's ok for now. Maybe we could keep this issue opened with the "enhancement" label?