I created a pretty simple composed system:
using OrdinaryDiffEq, ModelingToolkit, Plots
@parameters t
d_dt = Differential(t)
function Cart(; init_pos, init_vel, mass, name=:cart)
@variables pos(t)=init_pos vel(t)=init_vel
@variables f(t)
@parameters mass=mass
eqs = [
d_dt(pos) ~ vel
d_dt(vel) ~ f / mass
]
return ODESystem(eqs; name)
end
function PDController(; kp=0, kd=0, name=:controller)
@variables x(t) v(t) f(t)
@parameters kp=kp kd=kd
eqs = [
f ~ -kp*x - kd*v
]
return ODESystem(eqs; name)
end
function ControlledCart(; cart, controller, name=:sys)
eqs = [
cart.pos ~ controller.x
cart.vel ~ controller.v
cart.f ~ controller.f
]
return ODESystem(eqs; name, systems=[cart, controller])
end
cart = Cart(init_pos=0.0, init_vel=1.0, mass=0.5)
controller = PDController(kp=1.0, kd=0.5)
controlled_cart = ControlledCart(; cart, controller)
When I go to structural_simplify
it with
sys = controlled_cart |> structural_simplify
it does it like so:
which, I guess is technically true. Trying to build the ODEProblem
gives the error
ArgumentError: SymbolicUtils.BasicSymbolic{Real}[controllerâ‚Šv(t)] are missing from the variable map.
which makes sense because I purposely didn’t set a default value for controller.v
because it was supposed to just be equal to cart.vel
, which does has a default. I could, of course, give controller.v
a default for this simple example, but if this was a larger more complicated sim, I wouldn’t want to be wondering which version of two defaults it was going to choose.
FWIW, writing the Controller
and ControlledCart
equations with a 0
on the LHS, not structural_simplify
ing it, giving default values to everything, and then solving it with a DAE solver does work as expected.