You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I tried to simulate a 1D wave on a string with fixed ends and plotted the resulting animation and I'm posing the code here so that it could be refined into a tutorial.
The code is:
using ModelingToolkit
using MethodOfLines
using DomainSets
using NonlinearSolve
using Plots
# using SciMLNLSolve@parameters t x
@variablesE(..) u(..)
∂t =Differential(t)
∂tt = ∂t^2
∂xx =Differential(x)^2
c =1
wave_eq1D =∂tt(u(x, t)) ~ c^2*∂xx(u(x, t))
f(x) =sin(x)
g(x) =0
bcs = [
u(0, t) ~0,
u(3, t) ~0,
u(x, 0) ~f(x),
∂t(u(x, 0)) ~g(x)
]
t_min =0.0
t_max =2.0
x_min =0.0
x_max =3.0# Space and time domains
domains = [
t ∈Interval(t_min, t_max),
x ∈Interval(x_min, x_max)
]
@named pde_system =PDESystem(wave_eq1D, bcs, domains, [t, x], [u(x, t)])
# Method of lines discretization
dx =0.03
dt =0.04
Nx =floor(Int64, (x_max - x_min) / dx) +1
order =2
discretization =MOLFiniteDifference([x => dx, t => dt], approx_order=2)
# Convert the PDE problem into an nonlinear problem
prob =discretize(pde_system, discretization)
sol =solve(prob, NewtonRaphson(), tol=1e-5)
anim =@animatefor i ineachindex(t_min:dt:t_max)
scatter(x_min:dx:x_max, sol[(i-1)*Nx+1:i*Nx], y_lims=(-1,1))
endgif(anim, "wave.gif", fps=15)
The code needs a bit of cleanup and I'll also have to check if the chosen dt and dx make sense given the stability criteria. Moreover, the problem is a bit big for an example / tutorial since the string length is quite big with respect to the natural wavelength of the problem. I'll try to find more appropriate parameters over the following days.
Thanks to @xtalax for helping me with getting the above to work.
The text was updated successfully, but these errors were encountered:
I tried to simulate a 1D wave on a string with fixed ends and plotted the resulting animation and I'm posing the code here so that it could be refined into a tutorial.
The code is:
The code needs a bit of cleanup and I'll also have to check if the chosen
dt
anddx
make sense given the stability criteria. Moreover, the problem is a bit big for an example / tutorial since the string length is quite big with respect to the natural wavelength of the problem. I'll try to find more appropriate parameters over the following days.Thanks to @xtalax for helping me with getting the above to work.
The text was updated successfully, but these errors were encountered: