Skip to content

Instantly share code, notes, and snippets.

@Klafyvel
Created February 13, 2020 15:02
Show Gist options
  • Save Klafyvel/334b24a674388aebe12daa695023af2f to your computer and use it in GitHub Desktop.
Save Klafyvel/334b24a674388aebe12daa695023af2f to your computer and use it in GitHub Desktop.
Gaussian Beam animation
using Logging
using Plots
w0 = 1
zR = 1
λ = π * w0^2 / zR
k = 2π/λ
ω = 2π
w(z) = √(1+z^2)
R(z) = z + zR^2/z
E(x,z,t=0) = 1/w(z)*exp(1im*(k*z-ω*t) - x^2/w(z)^2 + 1im*(k*x^2/2/R(z)) - 1im*atan(z/zR))
x = -10:0.01:10
z = -20:0.01:20
X = repeat(reshape(x, 1, :), length(z), 1)
Z = repeat(z, 1, length(x));
anim = Animation()
duration = 2 # s
fps = 60
l = duration * fps
@info "Started computing plots"
for (i,t) in enumerate(range(0, 2, length=l))
field = real.(E.(X,Z, t))
heatmap(z,x,field', xlabel="z/zR", ylabel="x/w0", title="Gaussian beam field", aspect_ratio=1, fillcolor=:blues)
plot!([z z], [w.(z) -w.(z)], linecolor=:red, label=["" "w(z)"])
ylims!(-10, 10)
annotate!([(0, -9, Plots.text("cc-by-sa Hugo Levy-Falk 2020", 7, :white, :center))])
frame(anim)
@info "Frame $i / $l"
end
@info "Saving gif"
gif(anim, "gaussian.gif", fps=fps)
@info "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment