Skip to content

Commit

Permalink
Refactor code for improved readability and consistency; adjust format…
Browse files Browse the repository at this point in the history
…ting in multiple files
  • Loading branch information
dominic-chang committed Feb 7, 2025
1 parent 4e34e0f commit db1105f
Show file tree
Hide file tree
Showing 31 changed files with 1,774 additions and 877 deletions.
37 changes: 16 additions & 21 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ GENERATED = joinpath(@__DIR__, "..", "examples")
OUTDIR = joinpath(@__DIR__, "src", "examples")

blacklist = ["gpu", "JuKeBOX", "Krang_logo", "enzyme"]
SOURCE_FILES = filter(x-> all(i->!occursin(i, x), blacklist), Glob.glob("*.jl", GENERATED))
foreach(fn -> Literate.markdown(fn, OUTDIR, documenter=true), SOURCE_FILES)
SOURCE_FILES =
filter(x -> all(i -> !occursin(i, x), blacklist), Glob.glob("*.jl", GENERATED))
foreach(fn -> Literate.markdown(fn, OUTDIR, documenter = true), SOURCE_FILES)
MD_FILES = [joinpath("examples", file) for file in readdir(OUTDIR)]

Documenter.DocMeta.setdocmeta!(Krang, :DocTestSetup, :(using Krang); recursive=true)
Documenter.DocMeta.setdocmeta!(Krang, :DocTestSetup, :(using Krang); recursive = true)

format=DocumenterVitepress.MarkdownVitepress(
format = DocumenterVitepress.MarkdownVitepress(
repo = "https://github.com/dominic-chang/Krang.jl", # this must be the full URL!
devbranch = "main",
devurl = "dev",
Expand All @@ -24,35 +25,29 @@ format=DocumenterVitepress.MarkdownVitepress(
)

makedocs(;
sitename="Krang.jl",
authors="Dominic <[email protected]> and contributors",
modules=[Krang],
repo="https://github.com/dominic-chang/Krang.jl/blob/{commit}{path}#{line}",
format=format,
sitename = "Krang.jl",
authors = "Dominic <[email protected]> and contributors",
modules = [Krang],
repo = "https://github.com/dominic-chang/Krang.jl/blob/{commit}{path}#{line}",
format = format,
draft = false,
source = "src",
build = "build",
pages=[
pages = [
"Home" => "index.md",
"Getting Started" => "getting_started.md",
"Examples" => MD_FILES,
"Theory" => [
"Raytracing" => [
"kerr_geodesic_summary.md",
"time_regularization.md",
],
"Polarization" => [
"newmann_penrose.md",
"polarization.md",
]
"Raytracing" => ["kerr_geodesic_summary.md", "time_regularization.md"],
"Polarization" => ["newmann_penrose.md", "polarization.md"],
],
"api.md",
],
)

deploydocs(;
repo="github.com/dominic-chang/Krang.jl",
repo = "github.com/dominic-chang/Krang.jl",
target = "build", # this is where Vitepress stores its output
devbranch="main",
push_preview=true,
devbranch = "main",
push_preview = true,
)
67 changes: 42 additions & 25 deletions examples/coordinate-example.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ using CairoMakie

curr_theme = Theme(
Axis = (
xticksvisible = false,
xticksvisible = false,
xticklabelsvisible = false,
yticksvisible = false,
yticklabelsvisible = false,
aspect=1
),
Heatmap = (
rasterize=true,
)
aspect = 1,
),
Heatmap = (rasterize = true,),
)
set_theme!(merge!(curr_theme, theme_latexfonts()))

Expand All @@ -35,7 +33,7 @@ rmax = 10.0; # maximum radius to be ray traced
ρmax = 15.0; # horizontal and vertical limits of the screen

# Initialize Camera and pre-allocate memory for data to be plotted
coordinates = (zeros(sze, sze) for _ in 1:3)
coordinates = (zeros(sze, sze) for _ = 1:3)
camera = Krang.SlowLightIntensityCamera(metric, θo, -ρmax, ρmax, -ρmax, ρmax, sze);
colormaps = (:afmhot, :afmhot, :hsv)
colorrange = ((-20, 20), (0, rmax), (0, 2π))
Expand All @@ -46,58 +44,77 @@ colorrange = ((-20, 20), (0, rmax), (0, 2π))
# coordiante information for a sub-image `n` associated with the ray at the pixel `pixel` which intersects a cone with opening angle `θs`.
# We will define a function which introduces some basic occlusion effects by checking if the ray has intersected with the cone on
# the 'far-side' or the 'near-side' from the obsever.
function coordinate_point(pix::Krang.AbstractPixel, geometry::Krang.ConeGeometry{T,A}) where {T, A}
function coordinate_point(
pix::Krang.AbstractPixel,
geometry::Krang.ConeGeometry{T,A},
) where {T,A}
n, rmin, rmax = geometry.attributes
θs = geometry.opening_angle

coords = ntuple(j -> zero(T), Val(4))

isindir = false
for _ in 1:2 # Looping over isindir this way is needed to get Metal.jl to work
isindir = false
for _ = 1:2 # Looping over isindir this way is needed to get Metal.jl to work
isindir ⊻= true
ts, rs, ϕs = emission_coordinates(pix, geometry.opening_angle, isindir, n)
ts, rs, ϕs = emission_coordinates(pix, geometry.opening_angle, isindir, n)
if rs rmin || rs rmax
continue
end
coords = isnan(rs) ? observation : (ts, rs, θs, ϕs)
coords = isnan(rs) ? observation : (ts, rs, θs, ϕs)
end
return coords
return coords
end

# ## Drawing coordinate points
# This function plots the coordinates associated with the n=0,1,2 subimages of a cone with opening angle θs.
function draw!(axes_list, camera, coordinates, rmin, rmax, θs)
times, radii, azimuths = coordinates
times, radii, azimuths = coordinates
map(axes -> empty!.(axes), axes_list)

geometries = (Krang.ConeGeometry(θs, (i, rmin, rmax)) for i in 0:2)
geometries = (Krang.ConeGeometry(θs, (i, rmin, rmax)) for i = 0:2)

for (i, geometry) in enumerate(geometries)
rendered_scene = coordinate_point.(camera.screen.pixels, Ref(geometry))
for I in CartesianIndices(rendered_scene)
times[I] = rendered_scene[I][1]
radii[I] = rendered_scene[I][2]
azimuths[I] = rendered_scene[I][4]
end
coordinates = (times, radii, mod2pi.(azimuths ))
for j in 1:3
heatmap!(axes_list[i][j], coordinates[j], colormap = colormaps[j], colorrange=colorrange[j])
coordinates = (times, radii, mod2pi.(azimuths))
for j = 1:3
heatmap!(
axes_list[i][j],
coordinates[j],
colormap = colormaps[j],
colorrange = colorrange[j],
)
end
end
end

# Create Figure
fig = Figure(resolution=(700, 700));
fig = Figure(resolution = (700, 700));
axes_list = [
[
Axis(fig[i, 1], title=(i==1 ? "Regularized Time" : ""), titlesize=20, ylabel=(i==1 ? L"n=0" : i==2 ? L"n=1" : L"n=2"), ylabelsize=20),
Axis(fig[i, 2], title=(i==1 ? "Radius" : ""), titlesize=20),
Axis(fig[i, 3], title=(i==1 ? "Azimuth" : ""), titlesize=20),
] for i in 1:3
Axis(
fig[i, 1],
title = (i == 1 ? "Regularized Time" : ""),
titlesize = 20,
ylabel = (i == 1 ? L"n=0" : i == 2 ? L"n=1" : L"n=2"),
ylabelsize = 20,
),
Axis(fig[i, 2], title = (i == 1 ? "Radius" : ""), titlesize = 20),
Axis(fig[i, 3], title = (i == 1 ? "Azimuth" : ""), titlesize = 20),
] for i = 1:3
]

# Create the animation of Cone of Emission Coordinates
recording = CairoMakie.record(fig, "coordinate.gif", range(0.0, π, length=180), framerate=12) do θs
recording = CairoMakie.record(
fig,
"coordinate.gif",
range(0.0, π, length = 180),
framerate = 12,
) do θs
draw!(axes_list, camera, coordinates, rmin, rmax, θs)
end

Expand Down
23 changes: 13 additions & 10 deletions examples/custom-material-example.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ end
# This functor will take in a pixel and a geometry and return the redshift associated with a given sub image.
# You must include the relevant physics in the functor definition.
# Here we will include redshift effects associated with a zero angular momentum observer (ZAMO).
function (m::ZAMORedshifts)(pix::Krang.AbstractPixel{T}, intersection::Krang.Intersection) where {T}
function (m::ZAMORedshifts)(
pix::Krang.AbstractPixel{T},
intersection::Krang.Intersection,
) where {T}
(; rs, θs, νr, νθ) = intersection
α,β=Krang.screen_coordinate(pix)
α, β = Krang.screen_coordinate(pix)

ηtemp = η(metric, α, β, θo)
λtemp = λ(metric, α, θo)
Expand All @@ -34,7 +37,7 @@ function (m::ZAMORedshifts)(pix::Krang.AbstractPixel{T}, intersection::Krang.Int
end

# We need to define the return type or the material for ray tracing. This can be things like a `Float` of a `StokesParams`. Let's use a float for this case.
function Krang.yield(::ZAMORedshifts{T}) where T
function Krang.yield(::ZAMORedshifts{T}) where {T}
return zero(T)
end

Expand All @@ -61,23 +64,23 @@ import CairoMakie as CMk

theme = CMk.Theme(
Axis = (
xticksvisible = false,
xticksvisible = false,
xticklabelsvisible = false,
yticksvisible = false,
yticklabelsvisible = false,
),
),
)

CMk.set_theme!(CMk.merge!(theme, CMk.theme_latexfonts()))

fig = CMk.Figure(resolution=(700, 700));
ax = CMk.Axis(fig[1, 1], title="Redshifts", titlesize=20, aspect=1)
hm = CMk.heatmap!(ax, redshifts, colormap=:afmhot)
CMk.Colorbar(fig[1, 2], hm, label="Redshifts", labelsize=20)
fig = CMk.Figure(resolution = (700, 700));
ax = CMk.Axis(fig[1, 1], title = "Redshifts", titlesize = 20, aspect = 1)
hm = CMk.heatmap!(ax, redshifts, colormap = :afmhot)
CMk.Colorbar(fig[1, 2], hm, label = "Redshifts", labelsize = 20)
CMk.save("redshifts.png", fig)

# ![redshifts](redshifts.png)

# Saving the redshifts to a .npy file
using NPZ
npzwrite("redshifts_n1.npy", redshifts)
npzwrite("redshifts_n1.npy", redshifts)
Loading

0 comments on commit db1105f

Please sign in to comment.