Skip to content

Commit 6f549b5

Browse files
committed
test: add test making sure that the initialization works
this is the example in the docs
1 parent 95c24d8 commit 6f549b5

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

test/Blocks/sources.jl

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,4 +566,52 @@ end
566566

567567
@test SciMLBase.successful_retcode(sol)
568568
end
569+
570+
@testset "Initialization" begin
571+
function MassSpringDamper(; name)
572+
@named input = RealInput()
573+
vars = @variables f(t) x(t)=0 dx(t) [guess = 0] ddx(t)
574+
pars = @parameters m=10 k=1000 d=1
575+
576+
eqs = [f ~ input.u
577+
ddx * 10 ~ k * x + d * dx + f
578+
D(x) ~ dx
579+
D(dx) ~ ddx]
580+
581+
ODESystem(eqs, t, vars, pars; name, systems = [input])
582+
end
583+
584+
function MassSpringDamperSystem(data, time; name)
585+
@named src = ParametrizedInterpolation(LinearInterpolation, data, time)
586+
@named clk = ContinuousClock()
587+
@named model = MassSpringDamper()
588+
589+
eqs = [connect(model.input, src.output)
590+
connect(src.input, clk.output)]
591+
592+
ODESystem(eqs, t; name, systems = [src, clk, model])
593+
end
594+
595+
function generate_data()
596+
dt = 4e-4
597+
time = 0:dt:0.1
598+
data = sin.(2 * pi * time * 100)
599+
600+
return DataFrame(; time, data)
601+
end
602+
603+
df = generate_data() # example data
604+
605+
@named system = MassSpringDamperSystem(df.data, df.time)
606+
sys = structural_simplify(system)
607+
prob = ODEProblem(sys, [], (0, df.time[end]))
608+
sol = solve(prob)
609+
610+
@test SciMLBase.successful_retcode(sol)
611+
612+
prob2 = remake(prob, p = [sys.src.data => ones(length(df.data))])
613+
sol2 = solve(prob2)
614+
615+
@test SciMLBase.successful_retcode(sol2)
616+
end
569617
end

0 commit comments

Comments
 (0)