MAE 103: Differential Equations in Julia
Solve differential equations in Julia and pathlines
In today's lecture, we will learn how to solve ordinary differential equations. We will then use it to plot pathlines of fluid particles
A 1D differential equation
In this first example, we will solve the equation: with
We know the analytical solution for this problem :
In Julia, we can call the package OrdinaryDiffEq to solve numerically ODEs
using DifferentialEquationsThe first step is to define the right hand side of the differential equation, here f(u,p,t) = -u
f(u,p,t) = -uThe initial condition is u0 = 1
u0=1.0tspan = (0.0,1.0)prob = ODEProblem(f,u0,tspan)The variable prob holds all the useful informations about the differential problem : right-hand side, initial condition, time span...
We will solve this ODe with a Runge-Kutta 4 scheme
sol = solve(prob,RK4())Plotting the solution
using Plotsplot(sol.t, exp.(-sol.t), linewidth = 4, label = "Analytical solution")plot!(sol,linewidth=4, xaxis="t",yaxis="u", linestyle =:dash, label = "Numerical solution" ) # legend=falseusing InterpolationsSolve for the pathline of a fluid particle in a Couette Flow
For a Couette flow, the velocity profile is: with the velocity of the upper plate and the depth of the gap
The trajectory of a fluid particle A is denoted and a solution of:
with initial condition
Let's solve for the pathline of a fluid particle. For this problem we have :
Define the parameters for the Couette flow
U = 1H = 1couette_velocity(y) = U*y/HLet's define the right-hand side of the equation
function couette(du,u,p,t) du[1] = couette_velocity(u[2]) # U * (yₐ/H) du[2] = 0.0end## Initial position of the fluid particlex0=[0.0, 0.5]# Time spantspan = (0.0,1.0)prob_couette = ODEProblem(couette, x0, tspan)Now, we can solve the differential equation and get the pathline of this fluid particle
couette_sol = solve(prob_couette, RK4())plot(couette_sol, vars = 1, legend = false, xlabel = "t", ylabel = "xA(t)")plot(couette_sol, vars = 2, legend = false, xlabel = "t", ylabel = "yA(t)")plot(couette_sol, vars = (1,2), ylim =(0, H))plot!([1.0], line = :hline, linewidth = 8, label = "Upper wall")plot!([0.0], line = :hline, linewidth = 8, label = "Lower wall")scatter!([x0[1]], [x0[2]])Trajectory of a fluid particle in a cellular flow
A = 32.0B = 3.2ω = 9.2ψ(x, y, t) = A*cos(x + B*sin(ω*t))*cos(y)ucell(x, y, t) = A*cos(x + B*sin(ω*t))*sin(y)vcell(x, y, t) = -A*sin(x + B*sin(ω*t))*cos(y)xrange = -4.0:0.05:12.0yrange = -4.0:0.05:12.0function ψtab(t) out = zeros(xrange.len ,yrange.len)for (i, xi) in enumerate(xrange) for (j, yj) in enumerate(yrange) out[i, j] = ψ(xi, yj , t) endend return outendplot(xrange, yrange, ψtab(0.0)', ratio = 1, xlim = (xrange[1], xrange[end]), ylim = (yrange[1], yrange[end]), colorbar = false, xlabel ="x", ylabel = "y", title = "Streamlines at t = "*string(0.0))plot(xrange, yrange, ψtab(1.0)', ratio = 1, xlim = (xrange[1], xrange[end]), ylim = (yrange[1], yrange[end]), colorbar = false, xlabel ="x", ylabel = "y", title = "Streamlines at t = "*string(1.0))plot(xrange, yrange, ψtab(2.0)', ratio = 1, xlim = (xrange[1], xrange[end]), ylim = (yrange[1], yrange[end]), colorbar = false, xlabel ="x", ylabel = "y", title = "Streamlines at t = "*string(2.0))function cellular_flow(du,u,p,t) du[1] = ucell(u[1], u[2], t) du[2] = vcell(u[1], u[2], t)end## Initial position of the fluid particlex0_cell=[3.20, 1.88]# Time spantspan = (0.0,2.0)prob_cellular = ODEProblem(cellular_flow, x0_cell, tspan)cellular_sol = solve(prob_cellular, RK4())plot(xrange, yrange, ψtab(0.0)', ratio = 1, xlim = (xrange[1], xrange[end]), ylim = (0.0, 5.0), colorbar = false, xlabel ="x", ylabel = "y", title = "Streamlines at t = "*string(0.0))plot!(cellular_sol, vars = (1,2))scatter!([x0_cell[1]], [x0_cell[2]])