@@ -9,6 +9,8 @@ using SymbolicIndexingInterface
99using ModelingToolkit: t_nounits as t, D_nounits as D
1010import StateSelection
1111import SymbolicUtils as SU
12+ using ForwardDiff
13+
1214# ##
1315# ## Nonlinear system
1416# ##
291293 @test Initial (sys. resistor1. v) in Set (ModelingToolkit. get_ps (sys))
292294 @test Initial (sys. resistor2. v) in Set (ModelingToolkit. get_ps (sys))
293295end
296+
297+ @testset " AD through inline linear SCCs works" begin
298+ reassemble_alg = StructuralTransformations. DefaultReassembleAlgorithm (; inline_linear_sccs = true )
299+ @mtkcompile sys = RCModel () reassemble_alg = reassemble_alg
300+ prob = ODEProblem (sys, [], (0.0 , 10.0 ))
301+ @assert prob. p. nonnumeric[1 ] isa
302+ Vector{ModelingToolkitBase. DiffCacheAllocatorAPIWrapper{Float64}}
303+ @assert SciMLBase. has_initializeprob (prob. f)
304+
305+ setter = setsym_oop (prob, [sys. R, sys. C])
306+
307+ function loss (x)
308+ new_u0, new_p = setter (prob, x)
309+ new_prob = remake (prob; u0 = new_u0, p = new_p)
310+ sol = solve (new_prob, Tsit5 (); abstol = 1.0e-8 , reltol = 1.0e-8 )
311+ return sol[sys. capacitor. v][end ]
312+ end
313+
314+ # Primal works: returns ~0.993
315+ @test_nowarn loss ([1.0 , 1.0 ])
316+
317+ @test_nowarn ForwardDiff. gradient (loss, [1.0 , 1.0 ])
318+ end
0 commit comments