Skip to content

Commit 9e8cd74

Browse files
fix: handle observed in remake_initializeprob
1 parent eaf72ac commit 9e8cd74

File tree

2 files changed

+83
-3
lines changed

2 files changed

+83
-3
lines changed

src/systems/nonlinear/initializesystem.jl

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ function SciMLBase.remake_initializeprob(sys::ODESystem, odefn, u0, t0, p)
225225
u0idxs = Int[]
226226
u0vals = []
227227
for sym in variable_symbols(oldinitprob)
228-
if is_variable(sys, sym)
228+
if is_variable(sys, sym) || has_observed_with_lhs(sys, sym)
229229
u0 !== missing || continue
230230
idx = variable_index(oldinitprob, sym)
231231
push!(u0idxs, idx)
@@ -247,8 +247,16 @@ function SciMLBase.remake_initializeprob(sys::ODESystem, odefn, u0, t0, p)
247247
end
248248
end
249249
end
250-
newu0 = remake_buffer(oldinitprob.f.sys, state_values(oldinitprob), u0idxs, u0vals)
251-
newp = remake_buffer(oldinitprob.f.sys, parameter_values(oldinitprob), pidxs, pvals)
250+
if isempty(u0idxs)
251+
newu0 = state_values(oldinitprob)
252+
else
253+
newu0 = remake_buffer(oldinitprob.f.sys, state_values(oldinitprob), u0idxs, u0vals)
254+
end
255+
if isempty(pidxs)
256+
newp = parameter_values(oldinitprob)
257+
else
258+
newp = remake_buffer(oldinitprob.f.sys, parameter_values(oldinitprob), pidxs, pvals)
259+
end
252260
initprob = remake(oldinitprob; u0 = newu0, p = newp)
253261
return initprob, odefn.update_initializeprob!, odefn.initializeprobmap,
254262
odefn.initializeprobpmap

test/initializationsystem.jl

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,3 +877,75 @@ end
877877
sol = solve(prob, Rodas5P())
878878
@test SciMLBase.successful_retcode(sol)
879879
end
880+
881+
@testset "Issue#3205" begin
882+
using ModelingToolkit
883+
using ModelingToolkitStandardLibrary.Electrical
884+
using ModelingToolkitStandardLibrary.Mechanical.Rotational
885+
using ModelingToolkitStandardLibrary.Blocks
886+
using ModelingToolkit: t_nounits as t
887+
using SciMLBase
888+
889+
function dc_motor(R1=0.5)
890+
R = R1 # [Ohm] armature resistance
891+
L = 4.5e-3 # [H] armature inductance
892+
k = 0.5 # [N.m/A] motor constant
893+
J = 0.02 # [kg.m²] inertia
894+
f = 0.01 # [N.m.s/rad] friction factor
895+
tau_L_step = -0.3 # [N.m] amplitude of the load torque step
896+
897+
@named ground = Ground()
898+
@named source = Voltage()
899+
@named ref = Blocks.Step(height=0.2, start_time=0)
900+
@named pi_controller = Blocks.LimPI(k=1.1, T=0.035, u_max=10, Ta=0.035)
901+
@named feedback = Blocks.Feedback()
902+
@named R1 = Resistor(R=R)
903+
@named L1 = Inductor(L=L)
904+
@named emf = EMF(k=k)
905+
@named fixed = Fixed()
906+
@named load = Torque()
907+
@named load_step = Blocks.Step(height=tau_L_step, start_time=3)
908+
@named inertia = Inertia(J=J)
909+
@named friction = Damper(d=f)
910+
@named speed_sensor = SpeedSensor()
911+
912+
connections = [connect(fixed.flange, emf.support, friction.flange_b)
913+
connect(emf.flange, friction.flange_a, inertia.flange_a)
914+
connect(inertia.flange_b, load.flange)
915+
connect(inertia.flange_b, speed_sensor.flange)
916+
connect(load_step.output, load.tau)
917+
connect(ref.output, feedback.input1)
918+
connect(speed_sensor.w, :y, feedback.input2)
919+
connect(feedback.output, pi_controller.err_input)
920+
connect(pi_controller.ctr_output, :u, source.V)
921+
connect(source.p, R1.p)
922+
connect(R1.n, L1.p)
923+
connect(L1.n, emf.p)
924+
connect(emf.n, source.n, ground.g)]
925+
926+
@named model = ODESystem(connections, t,
927+
systems=[
928+
ground,
929+
ref,
930+
pi_controller,
931+
feedback,
932+
source,
933+
R1,
934+
L1,
935+
emf,
936+
fixed,
937+
load,
938+
load_step,
939+
inertia,
940+
friction,
941+
speed_sensor
942+
])
943+
end
944+
945+
model = dc_motor()
946+
sys = structural_simplify(model)
947+
948+
prob = ODEProblem(sys, [sys.L1.i => 0.0], (0, 6.0))
949+
950+
@test_nowarn remake(prob, p=prob.p)
951+
end

0 commit comments

Comments
 (0)