using Plots
using MonteCarloMeasurements54 The problem-algorithm-solve interface
This section uses these add-on packages:
The DifferentialEquations.jl package is an entry point to a suite of Julia packages for numerically solving differential equations in Julia and other languages. A common interface is implemented that flexibly adjusts to the many different problems and algorithms covered by this suite of packages.
In this section, we review a very informative post by discourse user @genkuroki which very nicely demonstrates the usefulness of the problem-algorithm-solve approach used with DifferentialEquations.jl. We slightly modify the presentation below for our needs, but suggest a perusal of the original post.
Example: Free fall
The motion of an object under a uniform gravitational field is of interest.
The parameters that govern the equation of motions are the gravitational constant, g; the initial height, y0; and the initial velocity, v0. The time span for which a solution is sought is tspan.
A problem consists of these parameters. Typical Julia usage would be to create a structure to hold the parameters, which may be done as follows:
struct Problem{G, Y0, V0, TS}
g::G
y0::Y0
v0::V0
tspan::TS
end
Problem(;g=9.80665, y0=0.0, v0=30.0, tspan=(0.0,8.0)) = Problem(g, y0, v0, tspan)Problem
The above creates a type, Problem, and a default constructor with default values. (The original uses a more sophisticated setup that allows the two things above to be combined.)
Types, as used above, serve two purposes: they bundle together the parameters for later reference and they can be used to dispatch varying methods to solve problems. The solve generic in the Julia ecosystem dispatches on the type of problem it is given.
In the above code, just calling Problem() will create a problem suitable for the earth, passing different values for g would be possible for other planets.
To solve differential equations there are many different possible algorithms. Here is the construction of two types to indicate two algorithms:
struct EulerMethod{T}
dt::T
end
EulerMethod(; dt=0.1) = EulerMethod(dt)
struct ExactFormula{T}
dt::T
end
ExactFormula(; dt=0.1) = ExactFormula(dt)ExactFormula
The above just specifies a type for dispatch-–-the directions indicating what code to use to solve the problem and default constructors. As seen, each constructor specifies a default size for a time step of 0.1.
A type for solutions is useful for different show methods or other methods. One can be created through:
struct Solution{Y, V, T, P<:Problem, A}
y::Y
v::V
t::T
prob::P
alg::A
endThe different algorithms then can be implemented as part of a generic solve function. Following the post we have:
solve(prob::Problem) = solve(prob, default_algorithm(prob))
default_algorithm(prob::Problem) = EulerMethod()
function solve(prob::Problem, alg::ExactFormula)
(; g, y0, v0, tspan) = prob # property destructuring
dt = alg.dt # direct property access
t0, t1 = tspan
ts = range(t0, t1 + dt/2; step = dt)
y(t) = y0 + v0*(t - t0) - g*(t - t0)^2/2
v(t) = v0 - g*(t - t0)
Solution(y.(ts), v.(ts), ts, prob, alg)
endsolve (generic function with 2 methods)
The exact formulas:
\[ \begin{align*} y(t) &= y_0 + v_0\cdot(t - t_0) - g\cdot(t - t_0)^2/2\\ v(t) &= v_0 - g\cdot(t - t_0), \end{align*} \]
are well-known physics formulas, discussed previously, for motion under a constant acceleration. The ExactFormula code broadcasts these functions over a range of values in ts and then wraps the output up in a Solution object so that the answers found can be easily extracted in a uniform manner.
For the Euler method, a for loop is utilized to step through the algorithm, in preparation, the new command fill(y0, n) is technical. It sets up a storage vector of length n which is initially filled with y0 but for which the second through last are overwritten. There are many other means to do a similar task, including creating an uninitialized vector with Vector{typeof(y0)}(undef, n) for which all entries would be subsequently filled in.
function solve(prob::Problem, alg::EulerMethod)
(; g, y0, v0, tspan) = prob
dt = alg.dt
t0, t1 = tspan
ts = range(t0, t1 + dt/2; step = dt)
n = length(ts)
ys = fill(y0, n)
vs = fill(v0, n)
for i in 1:n-1
vs[i+1] = vs[i] - g*dt # F*h step of Euler
ys[i+1] = ys[i] + vs[i]*dt # F*h step of Euler
end
Solution(ys, vs, ts, prob, alg)
endsolve (generic function with 3 methods)
Plots of solutions generated by the default values for each method are produced in Figure 54.1.
earth = Problem()
sol_euler = solve(earth)
sol_exact = solve(earth, ExactFormula())
plot(sol_euler.t, sol_euler.y;
label="Euler's method (dt = $(sol_euler.alg.dt))", linestyle=:auto)
plot!(sol_exact.t, sol_exact.y;
label="exact solution", linestyle=:auto)
title!("On the Earth"; xlabel="t", legend=:bottomleft)Following the post, since the time step dt = 0.1 is not small enough, the error of the Euler method is readily identified in Figure 54.1.
Next we change the algorithm’s default parameter for dt to be smaller. Figure 54.2 shows a much improved agreement between the exact answer and the approximate one found with EulerMethod.
earth₂ = Problem()
sol_euler₂ = solve(earth₂, EulerMethod(dt = 0.01))
sol_exact₂ = solve(earth₂, ExactFormula())
plot(sol_euler₂.t, sol_euler₂.y;
label="Euler's method (dt = $(sol_euler₂.alg.dt))", linestyle=:auto)
plot!(sol_exact₂.t, sol_exact₂.y;
label="exact solution", linestyle=:auto)
title!("On the Earth"; xlabel="t", legend=:bottomleft)The code is mostly a template. It is worth noting that only one line of code was modified, and in that line only the method required a modification.
Were the moon to be considered, the gravitational constant would need adjustment. This parameter is a property of the problem, not the solution algorithm, as dt is.
Such adjustments are made by passing different values to the Problem constructor. Again, just the one line needs modification.
moon = Problem(g = 1.62, tspan = (0.0, 40.0))
sol_eulerₘ = solve(moon)
sol_exactₘ = solve(moon, ExactFormula(dt = sol_euler.alg.dt))
plot(sol_eulerₘ.t, sol_eulerₘ.y;
label="Euler's method (dt = $(sol_eulerₘ.alg.dt))", linestyle=:auto)
plot!(sol_exactₘ.t, sol_exactₘ.y;
label="exact solution", linestyle=:auto)
title!("On the Moon"; xlabel="t", legend=:bottomleft)The code above also adjusts the time span in addition to the graviational constant. The algorithm for the exact formula is set to use the dt value used in the euler formula, for easier comparison. Otherwise, outside of the labels, the patterns are the same. Only those things that need changing are changed, the rest comes from defaults.
The above shows the benefits of using a common interface—new problems can be approached through only minor adjustments to the parameters, yet the calling pattern remains the same.
Next, the post illustrates how other authors could extend this code. The solve method dispatches on the problem type and the method type. Adding a new method to solve requires defining new method type and the algorithm for that type in the extension of solve.
For example, the following adds a sympletic method which conserves a quantity, allowing the approximate solutions to track long-term evolution without drift.
struct Symplectic2ndOrder{T}
dt::T
end
Symplectic2ndOrder(; dt=0.1) = Symplectic2ndOrder(dt)
function solve(prob::Problem, alg::Symplectic2ndOrder)
g, y0, v0, tspan = prob.g, prob.y0, prob.v0, prob.tspan
dt = alg.dt
t0, t1 = tspan
ts = range(t0, t1 + dt/2; step = dt)
n = length(ts)
ys = fill(y0, n)
vs = fill(v0, n)
for i in 1:n-1
vs[i+1] = vs[i] - g*dt
ys[i+1] = ys[i] + (vs[i] + vs[i+1])/2 * dt
end
Solution(ys, vs, ts, prob, alg)
endsolve (generic function with 4 methods)
Had the two prior methods been in a package, the other user could still extend the interface, as above, with just a slight standard modification.
The exact same approach to solving a problem works for this new type:
earth₃ = Problem()
sol_sympl₃ = solve(earth₃, Symplectic2ndOrder(dt = 2.0))
sol_exact₃ = solve(earth₃, ExactFormula())
plot(sol_sympl₃.t, sol_sympl₃.y;
label="2nd order symplectic (dt = $(sol_sympl₃.alg.dt))", linestyle=:auto)
plot!(sol_exact₃.t, sol_exact₃.y;
label="exact solution", linestyle=:auto)
title!("On the Earth"; xlabel="t", legend=:bottomleft)Finally, the author of the post shows how the interface can compose with other packages in the Julia package ecosystem. This example uses the external package MonteCarloMeasurements which plots the behavior of the system for perturbations of the initial value, as seen in Figure 54.5.
using MonteCarloMeasurements # introduces ± operation
earth₄ = Problem(y0 = 0.0 ± 0.0, v0 = 30.0 ± 1.0)
sol_euler₄ = solve(earth₄)
sol_sympl₄ = solve(earth₄, Symplectic2ndOrder(dt = 2.0))
sol_exact₄ = solve(earth₄, ExactFormula())
P = plot(sol_euler₄.t, sol_euler₄.y;
label="Euler's method (dt = $(sol_euler₄.alg.dt))", linestyle=:auto)
Q = plot(sol_sympl₄.t, sol_sympl₄.y;
label="2nd order symplectic (dt = $(sol_sympl₄.alg.dt))", linestyle=:auto)
R = plot(sol_exact₄.t, sol_exact₄.y;
label="exact solution", linestyle=:auto)
title!.((P,Q,R), "On the Earth"; xlabel="t", legend=:bottomleft, ylims=(-100, 60))
plot(P, Q, R)The only change was in the problem, Problem(y0 = 0.0 ± 0.0, v0 = 30.0 ± 1.0), where a different number type is used which accounts for uncertainty. The rest follows the same pattern.
This example, shows the flexibility of the problem-algorithm-solve pattern while maintaining a consistent pattern for execution.