@@ -38,8 +38,7 @@ function MassSpringDamper(; name)
3838 @variables f(t)=0 x(t)=0 dx(t)=0 ddx(t)=0
3939 @parameters m=10 k=1000 d=1
4040
41- eqs = [
42- f ~ input.u
41+ eqs = [f ~ input.u
4342 ddx * 10 ~ k * x + d * dx + f
4443 D(x) ~ dx
4544 D(dx) ~ ddx]
@@ -52,10 +51,8 @@ function MassSpringDamperSystem(data, time; name)
5251 @named clk = ContinuousClock()
5352 @named model = MassSpringDamper()
5453
55- eqs = [
56- connect(src.input, clk.output)
57- connect(src.output, model.input)
58- ]
54+ eqs = [connect(src.input, clk.output)
55+ connect(src.output, model.input)]
5956
6057 ODESystem(eqs, t, [], []; name, systems = [src, clk, model])
6158end
@@ -100,11 +97,10 @@ using Plots
10097
10198function MassSpringDamper(; name)
10299 @named input = RealInput()
103- vars = @variables f(t) x(t)=0 dx(t) [guess= 0] ddx(t)
100+ vars = @variables f(t) x(t)=0 dx(t) [guess = 0] ddx(t)
104101 pars = @parameters m=10 k=1000 d=1
105102
106- eqs = [
107- f ~ input.u
103+ eqs = [f ~ input.u
108104 ddx * 10 ~ k * x + d * dx + f
109105 D(x) ~ dx
110106 D(dx) ~ ddx]
@@ -117,10 +113,8 @@ function MassSpringDamperSystem(data, time; name)
117113 @named clk = ContinuousClock()
118114 @named model = MassSpringDamper()
119115
120- eqs = [
121- connect(model.input, src.output)
122- connect(src.input, clk.output)
123- ]
116+ eqs = [connect(model.input, src.output)
117+ connect(src.input, clk.output)]
124118
125119 ODESystem(eqs, t; name, systems = [src, clk, model])
126120end
@@ -143,13 +137,15 @@ plot(sol)
143137```
144138
145139If we want to run a new data set, this requires only remaking the problem and solving again
140+
146141``` @example parametrized_interpolation
147142prob2 = remake(prob, p = [sys.src.data => ones(length(df.data))])
148143sol2 = solve(prob2)
149144plot(sol2)
150145```
151146
152147!!! note
148+
153149 Note that when changing the data, the length of the new data must be the same as the length of the original data.
154150
155151## Custom Component with External Data
@@ -224,7 +220,7 @@ using ModelingToolkitStandardLibrary.Blocks
224220using OrdinaryDiffEq
225221
226222function System (; name)
227- src = SampledData (Float64, name= :src )
223+ src = SampledData (Float64, name = :src )
228224
229225 vars = @variables f (t)= 0 x (t)= 0 dx (t)= 0 ddx (t)= 0
230226 pars = @parameters m= 10 k= 1000 d= 1
@@ -238,22 +234,22 @@ function System(; name)
238234end
239235
240236@named system = System ()
241- sys = structural_simplify (system, split= false )
237+ sys = structural_simplify (system, split = false )
242238s = complete (system)
243239
244240dt = 4e-4
245241time = 0 : dt: 0.1
246242data1 = sin .(2 * pi * time * 100 )
247243data2 = cos .(2 * pi * time * 50 )
248244
249- prob = ODEProblem (sys, [], (0 , time[end ]); split= false , tofloat = false , use_union= true )
245+ prob = ODEProblem (sys, [], (0 , time[end ]); split = false , tofloat = false , use_union = true )
250246defs = ModelingToolkit. defaults (sys)
251247
252248function get_prob (data)
253249 defs[s. src. buffer] = Parameter (data, dt)
254250 # ensure p is a uniform type of Vector{Parameter{Float64}} (converting from Vector{Any})
255251 p = Parameter .(ModelingToolkit. varmap_to_vars (defs, parameters (sys); tofloat = false ))
256- remake (prob; p, build_initializeprob= false )
252+ remake (prob; p, build_initializeprob = false )
257253end
258254
259255prob1 = get_prob (data1)
0 commit comments