|  | 
| 1 |  | -using ModelingToolkit, OrdinaryDiffEq, DataInterpolations, Test | 
|  | 1 | +using ModelingToolkit, OrdinaryDiffEq, DataInterpolations, DynamicQuantities, Test | 
| 2 | 2 | 
 | 
| 3 | 3 | @independent_variables t | 
| 4 | 4 | D = Differential(t) | 
|  | 
| 215 | 215 |     M = ODESystem([D(x(t)) ~ x(t - 1)], t; name = :M) | 
| 216 | 216 |     @test_throws "DDE" change_independent_variable(M, x(t)) | 
| 217 | 217 | end | 
|  | 218 | + | 
|  | 219 | +@testset "Change independent variable w/ units (free fall with 2nd order horizontal equation)" begin | 
|  | 220 | +    @independent_variables t_units [unit = u"s"] | 
|  | 221 | +    D_units = Differential(t_units) | 
|  | 222 | +    @variables x(t_units) [unit = u"m"] y(t_units) [unit = u"m"] | 
|  | 223 | +    @parameters g = 9.81 [unit = u"m * s^-2"] # gravitational acceleration | 
|  | 224 | +    Mt = ODESystem([D_units(D_units(y)) ~ -g, D_units(D_units(x)) ~ 0], t_units; name = :M) # gives (x, y) as function of t, ... | 
|  | 225 | +    Mx = change_independent_variable(Mt, x; add_old_diff = true) # ... but we want y as a function of x | 
|  | 226 | +    Mx = structural_simplify(Mx; allow_symbolic = true) | 
|  | 227 | +    Dx = Differential(Mx.x) | 
|  | 228 | +    u0 = [Mx.y => 0.0, Dx(Mx.y) => 1.0, Mx.t_units => 0.0, Mx.xˍt_units => 10.0] | 
|  | 229 | +    prob = ODEProblem(Mx, u0, (0.0, 20.0), []) # 1 = dy/dx = (dy/dt)/(dx/dt) means equal initial horizontal and vertical velocities | 
|  | 230 | +    sol = solve(prob, Tsit5(); reltol = 1e-5) | 
|  | 231 | +    # compare to analytical solution (x(t) = v*t, y(t) = v*t - g*t^2/2) | 
|  | 232 | +    @test all(isapprox.(sol[Mx.y], sol[Mx.x - g * (Mx.t_units)^2 / 2]; atol = 1e-10)) | 
|  | 233 | +end | 
0 commit comments