4

I'm trying to solve the harmonic oscillator using DifferentialEquations in Julia. i.e.:

using DifferentialEquations
using Plots

m = 1.0                          
ω = 1.0                     

function mass_system!(ddu,du,u,p,t)
    # a(t) = (1/m) w^2 x 
    ddu[1] = (1/m)*(ω^2)*u[1]
end

v0 = 0.0                     
u0 = 1.0                  
tspan = (0.0,10.0)               

prob = SecondOrderODEProblem{isinplace}(mass_system!,v0,u0,tspan,callback=CallbackSet())
sol = solve(prob)

But it doesn't seem to understand the ODE constructor. Upon running, I get:

ERROR: LoadError: TypeError: non-boolean (typeof(isinplace)) used in boolean context
Stacktrace:
 [1] #_#219(::Base.Iterators.Pairs{Symbol,CallbackSet{Tuple{},Tuple{}},Tuple{Symbol},NamedTuple{(:callback,),Tuple{CallbackSet{Tuple{},Tuple{}}}}}, ::Type{SecondOrderODEProblem{DiffEqBas
e.isinplace}}, ::Function, ::Float64, ::Float64, ::Tuple{Float64,Float64}, ::DiffEqBase.NullParameters) at /Users/brandonmanley/.julia/packages/DiffEqBase/avuk1/src/problems/ode_problems
.jl:144
 [2] Type at ./none:0 [inlined] (repeats 2 times)
 [3] top-level scope at /Users/brandonmanley/Desktop/nBody/nBodyNN/test.jl:25
 [4] include at ./boot.jl:328 [inlined]
 [5] include_relative(::Module, ::String) at ./loading.jl:1105
 [6] include(::Module, ::String) at ./Base.jl:31
 [7] exec_options(::Base.JLOptions) at ./client.jl:287
 [8] _start() at ./client.jl:460

Any ideas?

phpHacky
  • 75
  • 4

1 Answers1

7

I would highly recommend looking at some of the tutorials. You have a few mistakes here which are addressed in this tutorial on classical physics models. Specifically, you shouldn't use an in-place modifying function if you choose a state variable that cannot be mutated, i.e. a scalar. If that's the case, just use the out-of-place form where you generate the output. That looks like:

using DifferentialEquations
using Plots

m = 1.0                          
ω = 1.0                     

function mass_system!(du,u,p,t)
    # a(t) = (1/m) w^2 x 
    (1/m)*(ω^2)*u[1]
end

v0 = 0.0                     
u0 = 1.0                  
tspan = (0.0,10.0)               

prob = SecondOrderODEProblem(mass_system!,v0,u0,tspan)
sol = solve(prob)
Chris Rackauckas
  • 18,645
  • 3
  • 50
  • 81
  • 2
    Note, from this I realized that the default algorithm you're getting for second order ODEs is something silly, so I'm fixing that up tonight: https://github.com/JuliaDiffEq/DifferentialEquations.jl/pull/567 – Chris Rackauckas Feb 12 '20 at 04:45
  • 2
    Fixed on DifferentialEquations v6.11. Now the default algorithm works for `SecondOrderODEProblem` – Chris Rackauckas Feb 12 '20 at 07:18
  • Ah I see what you mean, I still have much to learn about Julia. However when I try to implement your solution, it gives me the following: `ERROR: LoadError: type DynamicalODEFunction has no field jac_prototype Stacktrace: [1] getproperty at ./Base.jl:20 [inlined] [2] build_J_W(::Rosenbrock23{0,false,DefaultLinSolve,DataType}, ::ArrayPartition{Float64,Tuple{Float64,Float64}}, ::ArrayPartition{Float64,Tuple{Floa t64,Float64}}, ::DiffEqBase.NullParameters, ::Float64, ::Float64, ::Function, ::Type, ::Val{false}) ` – phpHacky Feb 12 '20 at 18:38
  • If you update your packages it's fixed. This is in last night's DiffEq v6.11 relelase. Without that release you'll need to do `sol = solve(prob,DPRKN6())`, or whatever method you want, but it has to specified. – Chris Rackauckas Feb 12 '20 at 21:45
  • Hi, I believe this no longer works? If I copy-paste the answer above as is, I get an error with a very long stacktrace that can't be typed here... the first line of the error reads: `MethodError: no method matching similar(::Float64, ::Type{Float64})` – guin0x Jan 21 '23 at 14:09
  • I just ran it and it runs fine on DifferentialEquations v7.6.0 with Julia v1.8.5. If there's a specific issue that you can find on release, please open an issue with the steps to reproduce it (i.e. a manifest file) – Chris Rackauckas Jan 22 '23 at 00:16
  • 1
    Weird... I just restarted my kernel and it ran just fine now... didn't change anything. Thanks anyway and sorry for the false alarm. – guin0x Jan 22 '23 at 10:42
  • No worries, thanks for confirming. – Chris Rackauckas Jan 23 '23 at 09:49