Skip to content

Commit 1b54706

Browse files
Merge pull request #3207 from AayushSabharwal/as/fix-remake-initializeprob
fix: handle observed in `remake_initializeprob`
2 parents 9e5564a + 3bd65e0 commit 1b54706

File tree

2 files changed

+83
-3
lines changed

2 files changed

+83
-3
lines changed

src/systems/nonlinear/initializesystem.jl

Lines changed: 13 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,18 @@ 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(
254+
oldinitprob.f.sys, state_values(oldinitprob), u0idxs, u0vals)
255+
end
256+
if isempty(pidxs)
257+
newp = parameter_values(oldinitprob)
258+
else
259+
newp = remake_buffer(
260+
oldinitprob.f.sys, parameter_values(oldinitprob), pidxs, pvals)
261+
end
252262
initprob = remake(oldinitprob; u0 = newu0, p = newp)
253263
return initprob, odefn.update_initializeprob!, odefn.initializeprobmap,
254264
odefn.initializeprobpmap

test/initializationsystem.jl

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

0 commit comments

Comments
 (0)