Skip to content

Commit d3af4de

Browse files
Brad Carmanstaticfloat
authored andcommitted
fixed derivative
1 parent 6df9014 commit d3af4de

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

src/Blocks/sources.jl

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -525,14 +525,10 @@ Symbolics.@register_symbolic get_sample_time(memory)
525525

526526
Symbolics.@register_symbolic get_sampled_data(t, memory)
527527

528-
get_sampled_data_const(t, memory::Parameter) = get_sampled_data(t, memory)
529-
Symbolics.@register_symbolic get_sampled_data_const(t, memory)
530-
Symbolics.derivative(::typeof(get_sampled_data_const), args::NTuple{2, Any}, ::Val{1}) = 0
531-
532528
function first_order_backwards_difference(t, memory)
533529
Δt = get_sample_time(memory)
534530
x1 = get_sampled_data(t, memory)
535-
x0 = get_sampled_data_const(t - Δt, memory)
531+
x0 = get_sampled_data(t - Δt, memory)
536532

537533
return (x1 - x0) / Δt
538534
end
@@ -611,11 +607,11 @@ function Symbolics.derivative(::typeof(set_sampled_data!), args::NTuple{4, Any},
611607
Δt = @inbounds args[4]
612608
first_order_backwards_difference(t, x, Δt, memory)
613609
end
614-
Symbolics.derivative(::typeof(set_sampled_data!), args::NTuple{4, Any}, ::Val{3}) = 1
610+
Symbolics.derivative(::typeof(set_sampled_data!), args::NTuple{4, Any}, ::Val{3}) = 1 #set_sampled_data returns x, therefore d/dx (x) = 1
615611

616612
function first_order_backwards_difference(t, x, Δt, memory)
617613
x1 = set_sampled_data!(memory, t, x, Δt)
618-
x0 = get_sampled_data_const(t - Δt, memory)
614+
x0 = get_sampled_data(t - Δt, memory)
619615

620616
return (x1 - x0) / Δt
621617
end

test/Blocks/sources.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,12 +414,13 @@ end
414414
time = 0:dt:t_end
415415
x = @. time^2 + 1.0
416416

417-
vars = @variables y(t)=1 dy(t)=0
417+
vars = @variables y(t)=1 dy(t)=0 ddy(t)=0
418418

419419
@named src = SampledData(Float64)
420420
@named int = Integrator()
421421
@named iosys = ODESystem([y ~ src.output.u
422422
D(y) ~ dy
423+
D(dy) ~ ddy
423424
connect(src.output, int.input)],
424425
t,
425426
systems = [int, src])
@@ -436,4 +437,5 @@ end
436437
@test sol(time)[src.output.u]x atol=1e-3
437438
@test sol[int.output.u][end]1 / 3 * 10^3 + 10.0 atol=1e-3 # closed-form solution to integral
438439
@test sol[dy][end]2 * time[end] atol=1e-3
440+
@test sol[ddy][end]2 atol=1e-3
439441
end

0 commit comments

Comments
 (0)