|
| 1 | +using DiffEqBase, ForwardDiff, Test |
| 2 | +using DiffEqBase: Void, FunctionWrappersWrappers, OrdinaryDiffEqTag, |
| 3 | + wrapfun_iip, wrapfun_iip_simple, AnyFunctionWrapper |
| 4 | +using FunctionWrappers: FunctionWrapper |
| 5 | + |
| 6 | +# Get the ForwardDiff extension module |
| 7 | +const FDExt = Base.get_extension(DiffEqBase, :DiffEqBaseForwardDiffExt) |
| 8 | + |
| 9 | +@testset "DEIIPFunctionWrapper struct (no ForwardDiff)" begin |
| 10 | + ff = (du, u, p, t) -> (du .= u; nothing) |
| 11 | + du = zeros(3) |
| 12 | + u = ones(3) |
| 13 | + p = [1.0, 2.0] |
| 14 | + t = 0.0 |
| 15 | + |
| 16 | + # wrapfun_iip_simple should produce a DEIIPFunctionWrapper |
| 17 | + wrapped = wrapfun_iip_simple(ff, du, u, p, t) |
| 18 | + @test wrapped isa DiffEqBase.DEIIPFunctionWrapper |
| 19 | + @test wrapped isa DiffEqBase.DEIIPFunctionWrapper{ |
| 20 | + Vector{Float64}, Vector{Float64}, Vector{Float64}, Float64} |
| 21 | + |
| 22 | + # VF64 alias should match |
| 23 | + @test wrapped isa DiffEqBase.DEIIPFunctionWrapperVF64{Vector{Float64}} |
| 24 | + |
| 25 | + # Should match AnyFunctionWrapper union |
| 26 | + @test wrapped isa AnyFunctionWrapper |
| 27 | + |
| 28 | + # The wrapped function should be callable and produce correct results |
| 29 | + du_test = zeros(3) |
| 30 | + wrapped(du_test, u, p, t) |
| 31 | + @test du_test == u |
| 32 | + |
| 33 | + # isfunctionwrapper should return true |
| 34 | + @test SciMLBase.isfunctionwrapper(wrapped) |
| 35 | + |
| 36 | + # Stack trace type string should be short |
| 37 | + type_str = string(typeof(wrapped)) |
| 38 | + @test occursin("DEIIPFunctionWrapper", type_str) |
| 39 | + @test !occursin("FunctionWrappersWrapper", type_str) |
| 40 | +end |
| 41 | + |
| 42 | +@testset "DEIIPFunctionWrapperVF64 with NullParameters" begin |
| 43 | + ff = (du, u, p, t) -> (du .= u; nothing) |
| 44 | + du = zeros(3) |
| 45 | + u = ones(3) |
| 46 | + p = SciMLBase.NullParameters() |
| 47 | + t = 0.0 |
| 48 | + |
| 49 | + wrapped = wrapfun_iip_simple(ff, du, u, p, t) |
| 50 | + @test wrapped isa DiffEqBase.DEIIPFunctionWrapper |
| 51 | + @test wrapped isa DiffEqBase.DEIIPFunctionWrapperVF64{SciMLBase.NullParameters} |
| 52 | +end |
| 53 | + |
| 54 | +@testset "ODEDualTag and ODEDualType (ForwardDiff extension)" begin |
| 55 | + @test FDExt.ODEDualTag === ForwardDiff.Tag{OrdinaryDiffEqTag, Float64} |
| 56 | + @test FDExt.ODEDualType === ForwardDiff.Dual{ |
| 57 | + ForwardDiff.Tag{OrdinaryDiffEqTag, Float64}, Float64, 1} |
| 58 | +end |
| 59 | + |
| 60 | +@testset "DEIIPFunctionWrapperForwardDiff struct" begin |
| 61 | + ff = (du, u, p, t) -> (du .= u; nothing) |
| 62 | + du = zeros(3) |
| 63 | + u = ones(3) |
| 64 | + p = [1.0, 2.0] |
| 65 | + t = 0.0 |
| 66 | + |
| 67 | + # wrapfun_iip with ForwardDiff loaded should produce a DEIIPFunctionWrapperForwardDiff |
| 68 | + wrapped = wrapfun_iip(ff, (du, u, p, t)) |
| 69 | + @test wrapped isa DiffEqBase.DEIIPFunctionWrapperForwardDiff |
| 70 | + |
| 71 | + # VF64 alias should match |
| 72 | + @test wrapped isa FDExt.DEIIPFunctionWrapperForwardDiffVF64{Vector{Float64}} |
| 73 | + |
| 74 | + # Should match AnyFunctionWrapper union |
| 75 | + @test wrapped isa AnyFunctionWrapper |
| 76 | + |
| 77 | + # The wrapped function should be callable |
| 78 | + du_test = zeros(3) |
| 79 | + wrapped(du_test, u, p, t) |
| 80 | + @test du_test == u |
| 81 | + |
| 82 | + # isfunctionwrapper should return true |
| 83 | + @test SciMLBase.isfunctionwrapper(wrapped) |
| 84 | + |
| 85 | + # Stack trace type string should NOT contain FunctionWrappersWrapper |
| 86 | + type_str = string(typeof(wrapped)) |
| 87 | + @test occursin("DEIIPFunctionWrapperForwardDiff", type_str) |
| 88 | + @test !occursin("FunctionWrappersWrapper", type_str) |
| 89 | +end |
| 90 | + |
| 91 | +@testset "DEIIPFunctionWrapperForwardDiffVF64 with NullParameters" begin |
| 92 | + ff = (du, u, p, t) -> (du .= u; nothing) |
| 93 | + |
| 94 | + # Default wrapfun_iip (no args) produces the 7-wrapper variant, not 4-wrapper |
| 95 | + wrapped_default = wrapfun_iip(ff) |
| 96 | + # The default 7-wrapper has a different structure (7 entries, not 4), |
| 97 | + # so it should NOT be a DEIIPFunctionWrapperForwardDiff |
| 98 | + @test !(wrapped_default isa DiffEqBase.DEIIPFunctionWrapperForwardDiff) |
| 99 | + # But it IS still an AnyFunctionWrapper (raw FunctionWrappersWrapper) |
| 100 | + @test wrapped_default isa AnyFunctionWrapper |
| 101 | + |
| 102 | + # With explicit 4-tuple args and NullParameters, it should match |
| 103 | + du = zeros(3) |
| 104 | + u = ones(3) |
| 105 | + p = SciMLBase.NullParameters() |
| 106 | + t = 0.0 |
| 107 | + wrapped = wrapfun_iip(ff, (du, u, p, t)) |
| 108 | + @test wrapped isa DiffEqBase.DEIIPFunctionWrapperForwardDiff |
| 109 | + @test wrapped isa FDExt.DEIIPFunctionWrapperForwardDiffVF64{SciMLBase.NullParameters} |
| 110 | +end |
| 111 | + |
| 112 | +@testset "wrapfun_iip_simple does not change behavior with ForwardDiff loaded" begin |
| 113 | + ff = (du, u, p, t) -> (du .= u; nothing) |
| 114 | + du = zeros(3) |
| 115 | + u = ones(3) |
| 116 | + p = [1.0, 2.0] |
| 117 | + t = 0.0 |
| 118 | + |
| 119 | + # wrapfun_iip_simple should ALWAYS produce a DEIIPFunctionWrapper, even with ForwardDiff loaded |
| 120 | + wrapped_simple = wrapfun_iip_simple(ff, du, u, p, t) |
| 121 | + @test wrapped_simple isa DiffEqBase.DEIIPFunctionWrapper |
| 122 | + |
| 123 | + # It should NOT match the ForwardDiff 4-wrapper struct |
| 124 | + @test !(wrapped_simple isa DiffEqBase.DEIIPFunctionWrapperForwardDiff) |
| 125 | + |
| 126 | + # wrapfun_iip should produce a DEIIPFunctionWrapperForwardDiff (ForwardDiff ext wraps it) |
| 127 | + wrapped_fd = wrapfun_iip(ff, (du, u, p, t)) |
| 128 | + @test wrapped_fd isa DiffEqBase.DEIIPFunctionWrapperForwardDiff |
| 129 | + @test !(wrapped_fd isa DiffEqBase.DEIIPFunctionWrapper) |
| 130 | +end |
0 commit comments