From 184e27473fda55f16f1510369113a341d16e034a Mon Sep 17 00:00:00 2001 From: dominic-chang Date: Fri, 11 Oct 2024 16:47:09 -0400 Subject: [PATCH] Add new example that defines a custom materian and saves the output --- .gitignore | 3 +- docs/Project.toml | 3 +- docs/make.jl | 1 - docs/src/api.md | 1 - examples/Project.toml | 1 + examples/custom-material-example.jl | 84 +++++++++++++++++++++++++++++ examples/polarization-example.jl | 5 +- 7 files changed, 92 insertions(+), 6 deletions(-) create mode 100644 examples/custom-material-example.jl diff --git a/.gitignore b/.gitignore index 8ed6038..77f30f1 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,5 @@ Manifest.toml .DS_Store *.gif *.png -docs/node_modules \ No newline at end of file +docs/node_modules +*.npy \ No newline at end of file diff --git a/docs/Project.toml b/docs/Project.toml index b3efc8c..3991d2c 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -14,6 +14,7 @@ Krang = "54806c32-d51a-438d-8447-e0041be2fbfb" Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306" Lux = "b2108857-7c20-44ae-9111-449ecde12c47" MeshIO = "7269a6da-0436-5bbc-96c2-40638cbb6118" +NPZ = "15e1cf62-19b3-5cfa-8e77-841668bca605" Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba" OptimizationOptimisers = "42dfb2eb-d2b4-4451-abcd-913932933ac1" Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" @@ -21,4 +22,4 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91" [compat] -Enzyme = "0.13" \ No newline at end of file +Enzyme = "0.13" diff --git a/docs/make.jl b/docs/make.jl index 9f62f51..94d6f42 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -48,7 +48,6 @@ makedocs(; ], "api.md", ], - checkdocs=:none ) deploydocs(; diff --git a/docs/src/api.md b/docs/src/api.md index 85af277..39ae21e 100644 --- a/docs/src/api.md +++ b/docs/src/api.md @@ -72,7 +72,6 @@ Krang.Mesh Krang.MeshGeometry Krang.ElectronSynchrotronPowerLawIntensity Krang.ElectronSynchrotronPowerLawPolarization -Krang.UnionGeometry ``` ### Pixel Related Functions diff --git a/examples/Project.toml b/examples/Project.toml index 64bb18d..24b8ba0 100644 --- a/examples/Project.toml +++ b/examples/Project.toml @@ -12,6 +12,7 @@ JacobiElliptic = "2a8b799e-c098-4961-872a-356c768d184c" KernelAbstractions = "63c18a36-062a-441e-b654-da1e3ab1ce7c" Krang = "54806c32-d51a-438d-8447-e0041be2fbfb" Lux = "b2108857-7c20-44ae-9111-449ecde12c47" +NPZ = "15e1cf62-19b3-5cfa-8e77-841668bca605" Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba" OptimizationOptimisers = "42dfb2eb-d2b4-4451-abcd-913932933ac1" Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" diff --git a/examples/custom-material-example.jl b/examples/custom-material-example.jl new file mode 100644 index 0000000..20ea417 --- /dev/null +++ b/examples/custom-material-example.jl @@ -0,0 +1,84 @@ +# # Defining a Custom Material and Saving Output +# We will define a redshift material for raytracing, and export the raytraced quantities to a .npy file. + +using Krang + +# Materials should be functors (i.e. types with callable objects). +# Our material will raytrace the redshifts associated with a zero angular momentum observer (ZAMO) on a cone for a given sub-image. +# If the cone is self obscuring, then only the redshift on the side that is closest to the observer will be raytraced. +struct ZAMORedshifts{T} <: Krang.AbstractMaterial + n::Int + rmin::T + rmax::T +end + +# Define the function for the material. +# 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, geometry::Krang.ConeGeometry{T,A}) where {T,A} + + observation = zero(T) + α,β = Krang.screen_coordinate(pix) + + θs = geometry.opening_angle + isindir = false + for _ in 1:2 + isindir ⊻= true + rs, νr, νθ, _, issuccess = emission_radius(pix, geometry.opening_angle, isindir, m.n) + if issuccess && m.rmin ≤ rs < m.rmax + ηtemp = η(metric, α, β, θo) + λtemp = λ(metric, α, θo) + curr_p_bl_d = p_bl_d(metric, rs, θs, ηtemp, λtemp, νr, νθ) + + curr_p_bl_u = metric_uu(metric, rs, θs) * curr_p_bl_d + p_zamo_u = jac_zamo_u_bl_d(metric, rs, θs) * curr_p_bl_u + redshift = inv(p_zamo_u[1]) + + observation = max(redshift, eps(T))^(T(3)) + end + end + return observation +end + +# We will use a $0.94$ spin Kerr black hole viewed by an asymptotic observer at an inclination angle of $θo=17^\circ$. +# The emission to be raytraced is +metric = Krang.Kerr(0.99); +θo = 17 * π / 180; +ρmax = 15.0; +rmin = Krang.horizon(metric); # minimum radius to truncate cone +rmax = 10.0; # maximum radius to truncate cone +n = 1; # sub-image to raytrace + +# Let's create a camera with a resolution of 400x400 pixels and our mesh. +camera = Krang.IntensityCamera(metric, θo, -ρmax, ρmax, -ρmax, ρmax, 400); +mesh = Krang.Mesh(Krang.ConeGeometry((75 * π / 180)), ZAMORedshifts(n, rmin, rmax)) +scene = Krang.Scene((mesh,)) + +# Finally, we will render the scene with the camera and plot the redshifts. +redshifts = render(camera, scene) + +import CairoMakie as CMk + +theme = CMk.Theme( + Axis = ( + 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) +CMk.save("redshifts.png", fig) + +# ![redshifts](redshifts.png) + +# Saving the redshifts to a .npy file +using NPZ +npzwrite("redshifts_n1.npy", redshifts) \ No newline at end of file diff --git a/examples/polarization-example.jl b/examples/polarization-example.jl index 2f65caf..110efbe 100644 --- a/examples/polarization-example.jl +++ b/examples/polarization-example.jl @@ -63,9 +63,10 @@ geometry2 = Krang.ConeGeometry((π-θs)) # We will create two meshes, one for each geometry anc create a scene with both meshes. mesh1 = Krang.Mesh(geometry1, material1) mesh2 = Krang.Mesh(geometry2, material2) -scene = Krang.Scene((mesh1, mesh2)) # Finally, we will render the scene with the camera and plot the Stokes parameters. + +scene = Krang.Scene((mesh1, mesh2)) stokesvals = render(camera, scene) fig = Figure(resolution=(700, 700)); @@ -80,4 +81,4 @@ fig save("polarization_example.png", fig) -# ![image](polarization_example.png) \ No newline at end of file +# ![polarization of emission model](polarization_example.png) \ No newline at end of file