Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plotting example for Nonlinear problems #82

Closed
SebastianM-C opened this issue Apr 18, 2022 · 0 comments · Fixed by #99
Closed

Plotting example for Nonlinear problems #82

SebastianM-C opened this issue Apr 18, 2022 · 0 comments · Fixed by #99

Comments

@SebastianM-C
Copy link

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
@variables E(..) 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 = @animate for i in eachindex(t_min:dt:t_max)
    scatter(x_min:dx:x_max, sol[(i-1)*Nx+1:i*Nx], y_lims=(-1,1))
end

gif(anim, "wave.gif", fps=15)

wave

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant