Skip to content
This repository has been archived by the owner on Feb 28, 2022. It is now read-only.

Commit

Permalink
Merge pull request #62 from gridap/Fixing_autodiff
Browse files Browse the repository at this point in the history
Fixing autodiff
  • Loading branch information
santiagobadia authored Dec 11, 2021
2 parents bc58289 + 7cb1ecc commit 1f6a5e1
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 31 deletions.
Binary file removed results.vtu
Binary file not shown.
3 changes: 1 addition & 2 deletions src/ODETools/ForwardEuler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,11 @@ function residual!(b::AbstractVector,op::ForwardEulerNonlinearOperator,x::Abstra
end

function jacobian!(A::AbstractMatrix,op::ForwardEulerNonlinearOperator,x::AbstractVector)
uF = x
vf = op.vf
vf = (x-op.u0)/op.dt
z = zero(eltype(A))
fillstored!(A,z)
jacobian!(A,op.odeop,op.tf,(op.u0,vf),1,(1/op.dt),op.ode_cache)
jacobians!(A,op.odeop,op.tf,(op.u0,vf),(0,1/op.dt),op.ode_cache)
end

function allocate_residual(op::ForwardEulerNonlinearOperator,x::AbstractVector)
Expand Down
17 changes: 13 additions & 4 deletions src/TransientFETools/TransientFEOperators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,18 @@ function TransientFEOperator(res::Function,jac::Function,jac_t::Function,
end

function TransientFEOperator(res::Function,trial,test;order::Integer=1)
jacs = ()
for i in 1:order+1
function jac_0(t,x,dx0,dv)
function res_0(y)
x0 = (y,x[2:end]...)
res(t,x0,dv)
end
jacobian(res_0,x[1])
end
jacs = (jac_0,)
for i in 2:order+1
function jac_i(t,x,dxi,dv)
function res_i(y)
x[i] = y
x = (x[1:i-1]...,y,x[i+1:end]...)
res(t,x,dv)
end
jacobian(res_i,x[i])
Expand Down Expand Up @@ -282,7 +289,9 @@ function jacobians!(
cache)
_matdata = ()
for i in 1:get_order(op)+1
_matdata = (_matdata...,matdata_jacobian(op,t,xh,i,γ[i]))
if (γ[i] > 0.0)
_matdata = (_matdata...,matdata_jacobian(op,t,xh,i,γ[i]))
end
end
matdata = vcat_matdata(_matdata)
assemble_matrix_add!(A,op.assem_t, matdata)
Expand Down
27 changes: 14 additions & 13 deletions test/TransientFEsTests/ForwardEulerHeatEquationTests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module HeatEquationTests
module ForwardEulerHeatEquationTests

using Gridap
using ForwardDiff
Expand Down Expand Up @@ -30,25 +30,28 @@ model = CartesianDiscreteModel(domain,partition)

order = 2

reffe = ReferenceFE(lagrangian,Float64,order)
V0 = FESpace(
reffe=lagrangian, order=order, valuetype=Float64,
conformity=:H1, model=model, dirichlet_tags="boundary")
model,
reffe,
conformity=:H1,
dirichlet_tags="boundary"
)
U = TransientTrialFESpace(V0,u)

trian = Triangulation(model)
Ω = Triangulation(model)
degree = 2*order
quad = CellQuadrature(trian,degree)
= Measure,degree)

#
a(u,v) = (v)(u)
b(v,t) = v*f(t)

res(t,(u,ut),v) = a(u,v) + ut*v - b(v,t)
jac(t,(u,ut),du,v) = a(du,v)
jac_t(t,(u,ut),dut,v) = dut*v
res(t,(u,ut),v) = ( a(u,v) + ut*v - b(v,t) )dΩ
jac(t,(u,ut),du,v) = ( a(du,v) )dΩ
jac_t(t,(u,ut),dut,v) = ( dut*v )dΩ

t_Ω = FETerm(res,jac,jac_t,trian,quad)
op = TransientFEOperator(U,V0,t_Ω)
op = TransientFEOperator(res,jac,jac_t,U,V0)

t0 = 0.0
tF = 1.0
Expand All @@ -58,8 +61,6 @@ U0 = U(0.0)
uh0 = interpolate_everywhere(u(0.0),U0)

ls = LUSolver()
using Gridap.Algebra: NewtonRaphsonSolver
nls = NLSolver(ls;show_trace=true,method=:newton) #linesearch=BackTracking())
ode_solver = ThetaMethod(ls,dt,θ)

sol_t = solve(ode_solver,op,uh0,t0,tF)
Expand All @@ -73,7 +74,7 @@ for (uh_tn, tn) in sol_t
global _t_n
_t_n += dt
e = u(tn) - uh_tn
el2 = sqrt(sum( integrate(l2(e),trian,quad) ))
el2 = sqrt(sum( (l2(e))dΩ ))
@test el2 < tol
end

Expand Down
8 changes: 4 additions & 4 deletions test/TransientFEsTests/HeatEquationAutoDiffTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ dv = get_fe_basis(V0)
du = get_trial_fe_basis(U₀)
uh = FEFunction(U₀,rand(num_free_dofs(U₀)))

cell_j = get_array(jac(0.5,uh,uh,du,dv))
cell_j_t = get_array(jac_t(0.5,uh,uh,du,dv))
cell_j = get_array(jac(0.5,(uh,uh),du,dv))
cell_j_t = get_array(jac_t(0.5,(uh,uh),du,dv))

cell_j_auto = get_array(jacobian(x->res(0.5,x,uh,dv),uh))
cell_j_t_auto = get_array(jacobian(x->res(0.5,uh,x,dv),uh))
cell_j_auto = get_array(jacobian(x->res(0.5,(x,uh),dv),uh))
cell_j_t_auto = get_array(jacobian(x->res(0.5,(uh,x),dv),uh))

test_array(cell_j_auto,cell_j,)
test_array(cell_j_t_auto,cell_j_t,)
Expand Down
20 changes: 12 additions & 8 deletions test/TransientFEsTests/StokesEquationAutoDiffTests.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module StokesEquationTests
module StokesEquationAutoDiffTests

using Gridap
using ForwardDiff
using LinearAlgebra
using Test
using GridapODEs.ODETools
using GridapODEs.TransientFETools
using Gridap.FESpaces: get_algebraic_operator
using Gridap.FESpaces
using Gridap.Arrays: test_array

# using GridapODEs.ODETools: ThetaMethodLinear
Expand Down Expand Up @@ -76,14 +76,18 @@ dy = get_fe_basis(Y)
dx = get_trial_fe_basis(X₀)
xh = FEFunction(X₀,rand(num_free_dofs(X₀)))

cell_j = get_array(jac(0.5,xh,xh,dx,dy))
cell_j_t = get_array(jac_t(0.5,xh,xh,dx,dy))
cell_j = get_array(jac(0.5,(xh,xh),dx,dy))
cell_j_t = get_array(jac_t(0.5,(xh,xh),dx,dy))

cell_j_auto = get_array(jacobian(x->res(0.5,x,xh,dy),xh))
cell_j_t_auto = get_array(jacobian(x->res(0.5,xh,x,dy),xh))
cell_j_auto = get_array(jacobian(x->res(0.5,(x,xh),dy),xh))
cell_j_t_auto = get_array(jacobian(x->res(0.5,(xh,x),dy),xh))

test_array(cell_j_auto,cell_j,)
test_array(cell_j_t_auto,cell_j_t,)
for i in 1:length(cell_j)
test_array(cell_j[i].array[1,1],cell_j_auto[i].array[1,1],)
test_array(cell_j[i].array[1,2],cell_j_auto[i].array[1,2],)
test_array(cell_j[i].array[2,1],cell_j_auto[i].array[2,1],)
test_array(cell_j_t[i].array[1,1],cell_j_t_auto[i].array[1,1],)
end

op = TransientFEOperator(res,X,Y)

Expand Down
6 changes: 6 additions & 0 deletions test/TransientFEsTests/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ using Test

@testset "DGHeatEquationTests" begin include("DGHeatEquationTests.jl") end

@testset "HeatEquationAutoDiffTests" begin include("HeatEquationAutoDiffTests.jl") end

@testset "StokesEquationAutoDiffTests" begin include("StokesEquationAutoDiffTests.jl") end

@testset "ForwardEulerHeatEquationTests" begin include("ForwardEulerHeatEquationTests.jl") end

end # module

0 comments on commit 1f6a5e1

Please sign in to comment.