1+ using Test
2+ using MechGluecode
3+ using MechanicalUnits
4+ using MechanicalUnits: Unitfu. promote_to_derived
5+ @import_expand (m, s, N) # Takes long if defined late)
6+ using DifferentialEquations
7+
8+
9+ @testset " Unitful time with unitful function" begin
10+ u0 = 30.0 N
11+ tspan = (0.0 , 10.0 )s
12+ prob1 = ODEProblem ((du,u,t,p) -> (du[1 ] = - 0.2 ∙ s⁻¹ * u[1 ]), [u0], tspan)
13+ prob2 = ODEProblem ((u,t,p) -> (- 0.2 ∙ s⁻¹ * u[1 ]), u0, tspan)
14+ prob3 = ODEProblem ((u,t,p) -> [- 0.2 ∙ s⁻¹* u[1 ]], [u0],tspan)
15+ so1 = solve (prob1, Euler (), dt = 1 s)
16+ so2 = solve (prob2, Midpoint ())
17+ so3 = solve (prob3, Tsit5 ())
18+ plot (so1, denseplot = false , marker = true )
19+ plot (so2, denseplot = false , marker = true )
20+ plot (so3, denseplot = false , marker = true )
21+ sf1 (t) = so1 (t)[1 ]
22+ sf2 (t) = so2 (t)[1 ]
23+ sf3 (t) = so3 (t)[1 ]
24+ plot ([sf1, sf2, sf3], xlims = (0 ,10 )s, ylims = (0 ,40 )N)
25+ plot ([t-> sf1 (t) / sf3 (t), t-> sf2 (t) / sf3 (t)], xlims = (0 ,10 )s, ylims = (0.5 ,1.1 ), minorgrid= true , grid = (:y , :olivedrab , :dot , 1 , 0.9 ))
26+
27+ end
28+ @testset " Unitful time with unitless function" begin
29+ u0 = 30.0
30+ tspan = (0.0 , 10.0 )s
31+ prob1 = ODEProblem ((du,u,t,p) -> (du[1 ] = - 0.2 ∙ s⁻¹ * u[1 ]), [u0], tspan)
32+ prob2 = ODEProblem ((u,t,p) -> (- 0.2 ∙ s⁻¹ * u[1 ]), u0, tspan)
33+ prob3 = ODEProblem ((u,t,p) -> [- 0.2 ∙ s⁻¹* u[1 ]], [u0],tspan)
34+ so1 = solve (prob1, Euler (), dt = 1 s)
35+ so2 = solve (prob2, Midpoint ())
36+ so3 = solve (prob3, Tsit5 ())
37+ # don't work plot(so1, denseplot = false, marker = true)
38+ # don't work plot(so2, denseplot = false, marker = true)
39+ # don't work plot(so3, denseplot = false, marker = true)
40+ sf1 (t) = so1 (t)[1 ]
41+ sf2 (t) = so2 (t)[1 ]
42+ sf3 (t) = so3 (t)[1 ]
43+ plot ([sf1, sf2, sf3], xlims = (0 ,10 )s, ylims = (0 ,40 ))
44+ plot ([t-> sf1 (t) / sf3 (t), t-> sf2 (t) / sf3 (t)], xlims = (0 ,10 )s, ylims = (0.5 ,1.1 ), minorgrid= true , grid = (:y , :olivedrab , :dot , 1 , 0.9 ))
45+ end
46+
47+ @testset " Plot ODE quantity" begin
48+ # Don't give us kN from from solution in this case, just defaults.
49+ promote_to_derived ()
50+ f1 = (y, p, t) -> - 1.5 y / 0.3 s
51+ u0 = 1.5 N
52+ tspan = (0.0 s, 1.0 s)
53+ prob = ODEProblem (f1, u0, tspan)
54+ sol = solve (prob)
55+ plot (sol)
56+ end
57+
58+ @testset " Plot ODE system same quantities" begin
59+ # This is not a good example, as the units are incorrect and have no
60+ # known physical interpretation
61+ σ = 10 / s
62+ ρ = 28 m
63+ β = (8 / 3 )m
64+ function lorenz! (du, u, p, t)
65+ du[1 ] = (u[2 ] - u[1 ])∙ σ
66+ du[2 ] = (u[1 ] * (ρ - u[3 ])) / (m∙ s) - u[2 ]/ s
67+ du[3 ] = u[1 ] * u[2 ] / (m∙ s) - β ∙ u[3 ]/ (m∙ s)
68+ end
69+ u0 = [1.0 , 0.0 , 0.0 ]m
70+ tspan = (0.0 ,100.0 )s
71+ prob = ODEProblem (lorenz!,u0,tspan)
72+ sol = solve (prob)
73+ # Plot all variables vs time
74+ plot (sol)
75+ # Plot in phase space
76+ plot (sol, vars= (1 ,2 ,3 ))
77+ end
78+ #=
79+ @testset "Plot ODE system single pendulum" begin
80+ # Second order non-linear, undamped pendulum:
81+ # mLθ'' + mg∙sin(θ)
82+ # where m is mass, unit kg
83+ # L is length, unit m
84+ # θ is angle to vertical, strictly no unit
85+ # g is gravity's acceleration
86+ # Acceleration
87+ # θ'' + k * sin(θ) = 0
88+ # where k = g / L , unit(k) = s⁻²
89+ # Reduced to first order non-linear system by defining:
90+ # θ = y[1]
91+ # θ' = y[2]
92+ # It follows that
93+ # θ'' = y'[2]
94+ # and
95+ # y'[1] = y[2]
96+ # y'[2] = -k * sin(y[1])
97+ # To solve numerically, we define the derivative.
98+ # For efficiency, it's evaluated in place, i.e. dy changes at every call.
99+ function f!(dy, y, k, t)
100+ dy[1] = y[2]
101+ dy[2] = -k ∙ sin(y[1])
102+ end
103+ # Fixed parameter
104+ k = upreferred(g / 1.0m)
105+ # Initial conditions
106+ θ0 = 0.1
107+ θ0_vel = 0.0s⁻¹
108+ θ0_acc = -k *sin(θ0)
109+
110+ y0 = [θ0, θ0_vel]
111+ y0 = ArrayPartition([θ0_vel, θ0_acc])
112+ tspan = (0.0, 100.0)s
113+ prob = ODEProblem(f!, y0, tspan, k)
114+ sol = solve(prob)
115+ # Plot all variables vs time
116+ plot(sol)
117+ # Plot in phase space
118+ plot(solve(prob), vars=(1,2))
119+ end
120+ =#
0 commit comments