Skip to content

Commit

Permalink
Replace Plots usage with Makie (#1097)
Browse files Browse the repository at this point in the history
  • Loading branch information
imreddyTeja authored Dec 1, 2024
1 parent d9156a6 commit e168347
Show file tree
Hide file tree
Showing 14 changed files with 1,398 additions and 1,662 deletions.
998 changes: 604 additions & 394 deletions experiments/ClimaCore/Manifest-v1.11.toml

Large diffs are not rendered by default.

964 changes: 586 additions & 378 deletions experiments/ClimaCore/Manifest.toml

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions experiments/ClimaCore/Project.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
[deps]
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
ClimaCore = "d414da3d-4745-48bb-8d80-42e94e092884"
ClimaCorePlots = "cf7c7e5a-b407-4c48-9047-11a94a308626"
ClimaCoreMakie = "908f55d8-4145-4867-9c14-5dad1a479e4d"
ClimaCoupler = "4ade58fe-a8da-486c-bd89-46df092ec0c7"
ClimaTimeSteppers = "595c0a79-7f3d-439a-bc5a-b232dc3bde79"
DiffEqCallbacks = "459566f4-90b8-5000-8ac3-15dfb0a30def"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
Expand Down
66 changes: 31 additions & 35 deletions experiments/ClimaCore/heat-diffusion/run.jl
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ sol_atm, sol_lnd = integ_atm.sol, integ_lnd.sol;
# `parent(sol_atm.u[<time-index>].atm_F_sfc)[<z-index>,<variable-index>]`

ENV["GKSwstype"] = "nul"
import Plots
Plots.GRBackend()
import Makie
import CairoMakie

ARTIFACTS_DIR = joinpath("experiments/ClimaCore/output/heat-diffusion/artifacts")
mkpath(ARTIFACTS_DIR)
Expand All @@ -311,18 +311,17 @@ mkpath(ARTIFACTS_DIR)
t0_ = parent(sol_atm.u[1].T_atm)[:, 1];
tend_ = parent(sol_atm.u[end].T_atm)[:, 1];
z_centers = parent(CC.Fields.coordinate_field(center_space_atm))[:, 1];
Plots.png(
Plots.plot(
[t0_ tend_],
z_centers,
labels = ["t=0" "t=end"],
xlabel = "T (K)",
ylabel = "z (m)",
title = "Atmos profile at start & end of Simulation",
linewidth = 2,
),
joinpath(ARTIFACTS_DIR, "atmos_profile.png"),
profile_fig = Makie.Figure();
profile_ax = Makie.Axis(
profile_fig[1, 1],
xlabel = "T (K)",
ylabel = "z (m)",
title = "Atmos profile at start & end of Simulation",
)
Makie.lines!(profile_ax, t0_, z_centers, label = "t=0"; linewidth = 2)
Makie.lines!(profile_ax, tend_, z_centers, label = "t=end"; linewidth = 2)
Makie.axislegend(profile_ax, position = :lt)
Makie.save(joinpath(ARTIFACTS_DIR, "atmos_profile.png"), profile_fig)

# - Conservation: absolute "energy" of both models with time
# convert to the same units (analogous to energy conservation, assuming that is both domains density=1 and thermal capacity=1)
Expand All @@ -331,35 +330,32 @@ atm_sum_u_t =
[sum(parent(u.T_atm)[:]) for u in sol_atm.u] .* (parameters.zmax_atm - parameters.zmin_atm) ./ parameters.n;
v1 = lnd_sfc_u_t .- lnd_sfc_u_t[1];
v2 = atm_sum_u_t .- atm_sum_u_t[1];
Plots.png(
Plots.plot(
sol_lnd.t,
[v1 v2 v1 + v2],
labels = ["lnd" "atm" "tot"],
xlabel = "time (s)",
ylabel = "energy flux (J / m2)",
title = "Component Model Energy during Simulation",
linewidth = 2,
),
joinpath(ARTIFACTS_DIR, "component_energy.png"),
component_energy_fig = Makie.Figure();
component_energy_ax = Makie.Axis(
component_energy_fig[1, 1],
xlabel = "time (s)",
ylabel = "energy flux (J / m2)",
title = "Component Model Energy during Simulation",
)
Makie.lines!(component_energy_ax, sol_lnd.t, v1, label = "lnd"; linewidth = 2)
Makie.lines!(component_energy_ax, sol_lnd.t, v2, label = "atm"; linewidth = 2)
Makie.lines!(component_energy_ax, sol_lnd.t, v1 .+ v2, label = "tot"; linewidth = 2)
Makie.axislegend(component_energy_ax, position = :lt)
Makie.save(joinpath(ARTIFACTS_DIR, "component_energy.png"), component_energy_fig)

# - Conservation: relative error with time
mean(arr) = sum(arr) / length(arr)
total = atm_sum_u_t + lnd_sfc_u_t;
rel_error = abs.(total .- total[1]) / mean(total);
Plots.png(
Plots.plot(
sol_lnd.t,
rel_error,
labels = nothing,
xlabel = "simulation time (s)",
ylabel = "relative error",
title = "Total Energy Conservation",
linewidth = 2,
),
joinpath(ARTIFACTS_DIR, "energy_conservation.png"),
energy_conservation_fig = Makie.Figure();
energy_conservation_ax = Makie.Axis(
energy_conservation_fig[1, 1],
xlabel = "simulation time (s)",
ylabel = "relative error",
title = "Total Energy Conservation",
)
Makie.lines!(energy_conservation_ax, sol_lnd.t, rel_error, linewidth = 2)
Makie.save(joinpath(ARTIFACTS_DIR, "energy_conservation.png"), energy_conservation_fig)

#src # - Animation
#src anim = Plots.@animate for u in sol_atm.u
Expand Down
44 changes: 29 additions & 15 deletions experiments/ClimaCore/sea_breeze/run.jl
Original file line number Diff line number Diff line change
Expand Up @@ -294,30 +294,44 @@ cpl_run(sim)
# ### References
# - [Antonelli & Rotunno 2007](https://journals.ametsoc.org/view/journals/atsc/64/12/2007jas2261.1.xml?tab_body=pdf)
## Post-processing
import Plots, ClimaCorePlots #hide
import Makie, CairoMakie, ClimaCoreMakie #hide

sol = sim.atmos.integrator.sol #hide
path = joinpath(@__DIR__, "output") #hide
mkpath(path) #hide
# JLD2.save(joinpath(path, "last_sim.jld2"), "coupled_sim", sim) #hide

Plots.GRBackend() #hide

# Plot atmospheric potential temperature [K] throughout the simulation
anim = Plots.@animate for u in sol.u #hide
Plots.contourf(u.Yc.ρθ ./ u.Yc.ρ) #hide
end #hide
Plots.mp4(anim, joinpath(path, "theta.mp4"), fps = 20) #hide
theta_fig = Makie.Figure();
theta_ax = Makie.Axis(theta_fig[1, 1])
# initial plot to setup axis and make consistent levels
theta_plot = ClimaCoreMakie.fieldcontourf!(theta_ax, sol.u[end].Yc.ρθ ./ sol.u[end].Yc.ρ)
theta_cb = Makie.Colorbar(theta_fig[1, 2], theta_plot)
theta_levels = [k for k in range(theta_cb.limits[][1], theta_cb.limits[][2], theta_plot.levels[])]
Makie.record(theta_fig, joinpath(path, "theta.mp4"), 1:length(sol.u); framerate = 20) do i
ClimaCoreMakie.fieldcontourf!(theta_ax, sol.u[i].Yc.ρθ ./ sol.u[i].Yc.ρ, levels = theta_levels)
end

If2c = CC.Operators.InterpolateF2C() #hide
anim = Plots.@animate for u in sol.u #hide
Plots.contourf(If2c.(u.ρw) ./ u.Yc.ρ) #hide
end #hide

# Plot atmospheric vertical velocity [m/s] throughout the simulation
Plots.mp4(anim, joinpath(path, "vel_w.mp4"), fps = 20) #hide
anim = Plots.@animate for u in sol.u #hide
Plots.contourf(u.Yc.ρuₕ ./ u.Yc.ρ) #hide
end #hide
vel_w_fig = Makie.Figure();
vel_w_ax = Makie.Axis(vel_w_fig[1, 1])
# initial plot to setup axis and make consistent levels
vel_w_plot = ClimaCoreMakie.fieldcontourf!(vel_w_ax, If2c.(sol.u[end].ρw) ./ sol.u[end].Yc.ρ)
vel_w_cb = Makie.Colorbar(vel_w_fig[1, 2], vel_w_plot)
vel_w_levels = [k for k in range(vel_w_cb.limits[][1], vel_w_cb.limits[][2], vel_w_plot.levels[])]
Makie.record(vel_w_fig, joinpath(path, "vel_w.mp4"), 1:length(sol.u); framerate = 20) do i
ClimaCoreMakie.fieldcontourf!(vel_w_ax, If2c.(sol.u[i].ρw) ./ sol.u[i].Yc.ρ, levels = vel_w_levels)
end

# Plot atmospheric longitudinal velocity [m/s] throughout the simulation
Plots.mp4(anim, joinpath(path, "vel_u.mp4"), fps = 20) #hide
vel_u_fig = Makie.Figure();
vel_u_ax = Makie.Axis(vel_u_fig[1, 1])
# initial plot to setup axis and make consistent levels
vel_u_plot = ClimaCoreMakie.fieldcontourf!(vel_u_ax, sol.u[end].Yc.ρuₕ ./ sol.u[end].Yc.ρ)
vel_u_cb = Makie.Colorbar(vel_u_fig[1, 2], vel_u_plot)
vel_u_levels = [k for k in range(vel_u_cb.limits[][1], vel_u_cb.limits[][2], vel_u_plot.levels[])]
Makie.record(vel_u_fig, joinpath(path, "vel_u.mp4"), 1:length(sol.u); framerate = 20) do i
ClimaCoreMakie.fieldcontourf!(vel_u_ax, sol.u[i].Yc.ρuₕ ./ sol.u[i].Yc.ρ, levels = vel_u_levels)
end
Loading

0 comments on commit e168347

Please sign in to comment.