Reputation: 51
I'm following this video tutorial titled 'Automated PDE Solving in Julia with MethodOfLines.jl by Alex Jones from JuliaCon 2022' on Youtube
The animate loop is giving this error as a result of a map operation in the second argument of the plot function call:
ERROR: Invalid indexing of solution. (u(t))[1] not found in solution
I have tried to print some of the variables in the loop but am not familiar with Julia animations. And (silly me) was expecting the example to just work.
Has something changed in the ecosystem to cause this error or did I make a mistake copying a line of code somewhere in the example?
I can't find a gist or repository with his code and typed this in from the video. Everything runs fine except the animation loop and of course the gif function call.
using MethodOfLines, ModelingToolkit, DomainSets
@parameters t x
@variables u(..)
Dt = Differential(t);
Dx = Differential(x)
Dxx = Dx^2
α = 1.1
eq = Dt(u(t,x)) ~ α * Dxx(u(t,x))
domain = [x ∈ Interval(0.0, 10.0) t ∈ Interval(0.0, 1.0)]
ic_bc = [u(0.0, x) ~ exp(-(x-4.0)^2) + exp(-(x-6.0)^2),
u(t, 0.0) ~ 0.0,
u(t, 10.0) ~ 0.0]
@named sys = PDESystem(eq, ic_bc, domain, [t,x], [u(t,x)])
dx = 0.1
discretization = MOLFiniteDifference([x => dx],t , approx_order = 2)
prob = discretize(sys, discretization)
using OrdinaryDiffEq
sol = solve(prob, Tsit5(), saveat = 0.05)
mygrid = get_discrete(sys, discretization)
using Plots
# plt = plot()
anim = @animate for (i, t_disc) in enumerate(sol[t])
# print("\n tdisc = ", t_disc)
print("tuple = ", map(d -> sol[d][i], mygrid[u(t, x)]) )
# plot(mygrid[x], map(d -> sol[d][i], mygrid[u(t,x)]), ylim = [0., 1.], label = "u", title = "t = $t_disc")
end
gif(anim, "plots/heat_rod.gif", fps = 10)
end # module mol_autopde
Upvotes: 0
Views: 27
Reputation: 1683
Apparently SciML simplified how to access the solution parameter discretizations and variable values. See the # added
lines below, and the # changed
plot line that uses them.
using ModelingToolkit: @parameters, @variables, Differential, PDESystem, @named, discretize
using DomainSets: Interval
using MethodOfLines: MOLFiniteDifference
@parameters t x
@variables u(..)
Dt = Differential(t)
Dx = Differential(x)
Dxx = Dx^2
α = 1.1
eq = Dt(u(t,x)) ~ α * Dxx(u(t,x))
domain = [x ∈ Interval(0.0, 10.0),
t ∈ Interval(0.0, 1.0)]
ic_bc = [u(0.0, x) ~ exp(-(x-4.0)^2) + exp(-(x-6.0)^2),
u(t, 0.0) ~ 0.0,
u(t, 10.0) ~ 0.0]
@named sys = PDESystem(eq, ic_bc, domain, [t,x], [u(t,x)])
dx = 0.1
discretization = MOLFiniteDifference([x => dx], t, approx_order = 2)
prob = discretize(sys, discretization)
using OrdinaryDiffEq: solve, Tsit5
sol = solve(prob, Tsit5(), saveat = 0.05)
discrete_t = sol[t] # added
discrete_x = sol[x] # added
solu = sol[u(t, x)] # added
using Plots: Plots, @animate, plot, gif # 'Plots' needed by '@animate'
# plt = plot()
anim = @animate for (i, t_disc) in enumerate(discrete_t) # changed
plot(discrete_x, solu[i, :], ylim = [0., 1.], label = "u", title = "t = $t_disc") # changed
end
gif(anim, "plots/heat_rod.gif", fps = 10)
See also the MethodOfLines.jl
tutorial "Solving the Heat Equation" without the animated plot.
Upvotes: 1