-
-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix Symbolics derivative wrapping for structural simplification #170
Conversation
Unfortunately this still fails. If I run the following at the end of f = LinearInterpolation(u, t)
expand_derivatives(D(f)) If I understand correctly this is what ModelingToolkit will be doing |
MWE using ModelingToolkit
using ModelingToolkitStandardLibrary.Blocks: TimeVaryingFunction
using DataInterpolations
dt = 4e-4
t_end = 10.0
time = 0:dt:t_end
x = @. time^2 + 1.0
vars = @variables y(t)=1 dy(t)=0 ddy(t)=0
@named src = TimeVaryingFunction(LinearInterpolation(x, time))
@named int = Integrator()
@named iosys = ODESystem([y ~ src.output.u
D(y) ~ dy
D(dy) ~ ddy
connect(src.output, int.input)],
t,
systems = [int, src])
sys = structural_simplify(iosys) This gives: However...
So it seems somehow |
OK, I think I understand now what's happening. In this case using ModelingToolkit
using ModelingToolkitStandardLibrary.Blocks: TimeVaryingFunction, Integrator
using DataInterpolations
@parameters t
D = Differential(t)
dt = 4e-4
t_end = 10.0
time = 0:dt:t_end
x = @. time^2 + 1.0
vars = @variables y(t)=1 dy(t)=0 ddy(t)=0
@named src = TimeVaryingFunction(LinearInterpolation(x, time))
@named int = Integrator()
@named sys = ODESystem([y ~ src.output.u
D(y) ~ dy
# D(dy) ~ ddy # <-- including this gives the error
connect(src.output, int.input)],
t,
systems = [int, src])
sys = structural_simplify(sys) This error can be recreated simply with... u = [0.0, 1.5, 0.0]
t = [0.0, 0.5, 1.0]
@variables τ
D = Symbolics.Differential(τ)
f = LinearInterpolation(u, t)
df = expand_derivatives(D(f(τ))) # this is OK
ddf = expand_derivatives(D(D(f(τ)))) # ERROR: Differentiation with array expressions is not yet supported |
Okay so at least this work with first derivatives though? |
@bradcarman I just tried using ModelingToolkit
using ModelingToolkitStandardLibrary.Blocks: TimeVaryingFunction, Integrator
using DataInterpolations
@parameters t
D = Differential(t)
dt = 4e-4
t_end = 10.0
time = 0:dt:t_end
x = @. time^2 + 1.0
vars = @variables y(t) = 1 dy(t) = 0 ddy(t) = 0
@named src = TimeVaryingFunction(LinearInterpolation(x, time))
@named int = Integrator()
@named sys = ODESystem([y ~ src.output.u,
D(y) ~ dy,
connect(src.output, int.input)],
t,
systems=[int, src])
sys = structural_simplify(sys) with the patch from this PR applied and I'm still getting the
even if g = LinearInterpolation(x, time)
expand_derivatives(D(g(t))) works. Am I missing something? |
Hi @SebastianM-C , actually I can confirm the same error as you. I suspect |
@sathvikbhagavan can you put the final touches on this so we can get it merged? |
@ChrisRackauckas I was looking into this and was having some issues in defining second order derivatives in symbolics. I want to define a function like https://github.com/SciML/DataInterpolations.jl/blob/ChrisRackauckas-patch-1/ext/DataInterpolationsSymbolicsExt.jl#L22 for second order which then can be lowered to computing the values using ForwardDiff numerically. |
Let's just get first derivatives merged first. |
Should fix SciML/ModelingToolkit.jl#2190
@bradcarman can you run your test case with this?