Skip to content

Commit

Permalink
doc: Move 2D example to Literate.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
musoke committed Jun 25, 2024
1 parent 522e260 commit ab5b7c1
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 314 deletions.
314 changes: 0 additions & 314 deletions examples/2d.ipynb

This file was deleted.

87 changes: 87 additions & 0 deletions examples/2d.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# # Example: 2D Simulation
#
# ## Set up Julia environment

import Pkg
Pkg.activate(mktempdir())

Pkg.add("CairoMakie")
Pkg.add("CSV")
Pkg.add("NPZ")
Pkg.add("PencilFFTs")
Pkg.develop(path = joinpath(@__DIR__, "..")) # Load version of UltraDark in this repo

using UltraDark
using Test
using NPZ
using CairoMakie
using CSV

Threads.nthreads()

# ## Define initial conditions
#
# Define a 2D grid.

resol = 128
len = 10.0

grids = Grids((len, len, len / resol), (resol, resol, 1));

# Add some solitons to the grid. Strictly speaking, these aren't solitons in 2D, but they'll do for demonstration purposes.

mass = 10
position_1 = [-len / 5, -len / 5, 0]
position_2 = [-len / 5, +len / 5, 0]
velocity = [1, 0, 0]
phase_1 = 0
phase_2 = π
t0 = 0

UltraDark.Initialise.add_fdm_soliton!(grids, mass, position_1, velocity, phase_1, t0)
UltraDark.Initialise.add_fdm_soliton!(grids, mass, position_2, velocity, phase_2, t0)

# ## Set options

output_dir = joinpath(mktempdir(), "output", "2D")

output_times = 0:0.1:5
output_config = OutputConfig(output_dir, output_times);

options = Config.SimulationConfig();

# ## Run simulation

@time simulate!(grids, options, output_config)

# ## Plot output

summary = CSV.File(joinpath(output_config.directory, "summary.csv"));

# rho_init = npzread("$(output_config.directory)/rho_1.npy");
rho_last = npzread("$(output_config.directory)/rho_$(length(output_times)).npy");
δ_lims = extrema(rho_last .- 1)

fig_anim = Figure()
ax_anim = Axis(fig_anim[1, 1], aspect = DataAspect())

hidedecorations!(ax_anim)

cb_density = Colorbar(fig_anim[1, 2], limits = δ_lims, label = L"$\rho/\rho_{\text{crit}}$")

frame = Observable(1)

rho = lift(i -> npzread(joinpath(output_config.directory, "rho_$(i).npy"))[:, :, 1], frame)
δ = @lift($rho .- 1)

contourf!(
ax_anim,
grids.x[:, 1, 1],
grids.y[1, :, 1],
δ,
levels = range(δ_lims[1], δ_lims[2], 10),
)

Record(fig_anim, 1:length(output_times); framerate = 5) do f
frame[] = f
end

0 comments on commit ab5b7c1

Please sign in to comment.