# Makie.jl - How to plot a 3D animation?

Hi,I have a question. I would like to ask
How to plot a time-evolution of a 3D Gaussian function with Makie.jl.
I tried to write a code in reference to a surface-version code of sin( r )/r.

1.Make a model and Solve a time evolution equation i.e., the matrix-form time-dependent Schrodinger equation : (∂/∂t)ψ = -i H ψ
Here, ψ is a vector and H is a (time-independent) matrix.

``````#Make a model
#Def of H

using LinearAlgebra
eye(T::Type,n) = Diagonal{T}(I, n)
eye(n) = eye(Float64,n)

function H(Lx,Ly,Lz) #def of H
N = Lx*Ly*Lz

mat_Htb = zeros(Complex{Float64},N,N)

for iz = 1:Lz
for ix = 1:Lx
for iy=1:Ly
for dz in -1:1
jz = iz + dz
for dx in -1:1
jx = ix + dx
for dy in -1:1
jy = iy + dy

ii = (iz-1)*Lx*Ly + (ix-1)*Ly + (iy-1) + 1
jj = (jz-1)*Lx*Ly + (jx-1)*Ly + (jy-1) + 1

if 1 <= jx <= Lx && 1 <= jy <= Ly && 1 <= jz <= Lz

if dx == +1 && dy == 0 && dz == 0
mat_Htb[ii,jj] += im
end

if dx == -1 && dy == 0 && dz == 0
mat_Htb[ii,jj] += im/4
end

if dx == 0 && dy == +1 && dz == 0
mat_Htb[ii,jj] += im/2
end

if dx == 0 && dy == -1 && dz == 0
mat_Htb[ii,jj] += im
end

if dx == 0 && dy == 0 && dz == +1
mat_Htb[ii,jj] += -im
end

if dx == 0 && dy == 0 && dz == -1
mat_Htb[ii,jj] += im*(3/7)
end

if dx == 0 && dy == 0 && dz == 0
mat_Htb[ii,jj] += im
end

end
end
end
end
end
end
end

return mat_Htb
end
``````

ix,iy,iz represent 3D coordinates.

``````#Def of Eq

using LinearAlgebra
using OrdinaryDiffEq
using DifferentialEquations

#Define the underlying equation
function time_evolution(ψdot,ψ,p,t)
ψdot.=-im.*H(Lx,Ly,Lz)*ψ
end

Lx = Ly = Lz = 10
ψ0 = [] #  Initial conditions

for iz = 1:Lz
for ix = 1:Lx
for iy = 1:Ly
gauss = exp(-((ix)^2 + (iy)^2 + (iz)^2))
push!(ψ0,gauss)
end
end
end

tspan = (0.0,20.0) #  Simulation time span

#Pass to Solvers
prob = ODEProblem(time_evolution,ψ0,tspan)
sol = solve(prob)

``````

2. 3D Plotting of the solutions

``````#3D plot(volume plot)

using Makie
using FileIO
using LinearAlgebra
using AbstractPlotting
using ColorSchemes

x = 1: Lx  # our value range
y = 1: Ly
z = 1: Lz

ρ(ix,iy,iz,nt) = abs2.((sol(nt)[(iz-1)*Lx*Ly + (ix-1)*Ly + (iy-1)])./norm(sol(nt)[(iz-1)*Lx*Ly + (ix-1)*Ly + (iy-1)]))
ψ(nt) = Float64[ρ(ix,iy,iz,nt) for ix in x, iy in y,iz in z]
scene = Scene(backgroundcolor = :white)

c = ψ(length(sol.t))

vol = volume!(
scene,
x, y, z,          # coordinates to plot on
c,                # charge density (functions as colorant)
algorithm = :mip, # maximum-intensity-projection
colorrange = (0,0.01),
transparency = true,
)[end]

update_cam!(scene, Vec3f0(1,0.5,0.1), Vec3f0(0))
scene[Axis].names.textcolor = :gray # let axis labels be seen on darkbackground

record(scene, "output.mp4", range(0, stop = 1.0, length = 20)) do nt
vol[4] = ψ(nt)
end
``````

Then, this code passed and a mp4 animation was obtained.
But the solutions can’t be seen in the mp4 file.

I checked a snapshot at initial time is displayed
like this.

``````using Makie
using FileIO
using LinearAlgebra
using AbstractPlotting

r = LinRange(-20, 20, 500);  # our value range

ρ(x, y, z) = exp(-((x)^2 + (y)^2 + (z)^2)) # function (charge density)

# create a Scene with the attribute `backgroundcolor = :black`,
# can be any compatible color.  Useful for better contrast and not killing your eyes with a white background.
scene = Scene(backgroundcolor = :black)

volume!(
scene,
r, r, r,          # coordinates to plot on
ρ,                # charge density (functions as colorant)
algorithm = :mip  # maximum-intensity-projection
)

scene[Axis].names.textcolor = :gray # let axis labels be seen on dark
background

save("sp.png",scene)
``````

I want to see the yellow region moving.
How should I fix the code?

P.S.
I posted the same question on stack overflow but I was recommended to use this place.
So I re-posted the question here.

2 Likes