Skip to content

Instantly share code, notes, and snippets.

@fonsp
Last active June 14, 2020 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fonsp/e1f0a9eb6678b91d2a6b0ade1fa618ce to your computer and use it in GitHub Desktop.
Save fonsp/e1f0a9eb6678b91d2a6b0ade1fa618ce to your computer and use it in GitHub Desktop.
using Plots
using ClimateMARGO
using LaTeXStrings
using Colors
# Helpers
function pl_percent_formatter(y)
"$(Integer(round(y*100)))%"
end
function pl_plot_refline!(x, y)
plot!(x, y, color="rgba(0,0,0,.5)", label=nothing, linestyle=:dash)
end
function pl_fill_between!(x, y1, y2; kwargs...)
plot!(x, y1, fillrange=[y2, y1]; kwargs...)
end
function pl_fill_past!(model, ylim)
is_past = (model.domain .<= model.present_year)
pl_fill_between!(
model.domain[is_past],
ones(size(model.domain[is_past])) * ylim[1] * 2.,
ones(size(model.domain[is_past])) * ylim[2] * 2.,
facecolor = "b", alpha = 0.1,
ylim=ylim,
label=nothing,
)
end
function pl_finish_years_plot!(model::ClimateModel; when=missing, ylim=nothing, refline=nothing)
plot!(
xlabel="year",
ylim=ylim,
grid=true,
xlim=(model.domain[1], 2200.),
xticks=model.domain[1]:40.:2200.,
)
if refline !== nothing
t = if when === missing
model.domain
else
model.domain[when]
end
pl_plot_refline!(t, fill(refline, size(t)))
end
if ylim !== nothing
pl_fill_past!(model, ylim)
end
return plot!()
end
# Model plots
function pl_plot_emissions(model::ClimateModel)
data = [
effective_baseline_emissions(model),
effective_emissions(model),
]
labels = [L"$rq$ (no-policy baseline)" L"$rq(1-M) - q_{0}R$ (controlled)"]
limit = maximum(effective_baseline_emissions(model))
ylim = 1.1 .* (-limit, limit)
plot(
model.domain,
data,
title="Effective emissions",
ylabel=L"effective CO$_{2e}$ emissions [ppm / yr]",
label=labels,
)
pl_finish_years_plot!(model, ylim=ylim, refline=0.0)
end
function pl_plot_concentrations(model::ClimateModel)
data = [
CO₂_baseline(model),
CO₂(model),
]
labels = [L"$c$ (no-policy baseline)" L"$c_{M,R}$ (controlled)"]
limit = maximum(CO₂_baseline(model))
ylim = 1.1 .* (0, limit)
plot(
model.domain,
data,
title="Concentrations",
ylabel=L"CO$_{2e}$ concentration [ppm]",
label=labels,
)
pl_finish_years_plot!(model, ylim=ylim)
end
function pl_plot_temperatures(model::ClimateModel; hide_baseline=false)
if !hide_baseline
plot(model.domain, δT_baseline(model), color=1, label=L"$T$ (no-policy baseline)")
else
plot()
end
data = [
δT_no_geoeng(model),
δT(model),
δT(model).*sqrt.(1. .- model.controls.adapt),
]
labels = [L"$T_{M,R}$ (controlled with $G=0$)" L"$T_{M,R,G}$ (controlled)" L"$T_{M,R,G,A}$ (adapted)"]
colors = [2 4 3]
limit = maximum(δT_baseline(model))
ylim = 1.1 .* (0, limit)
plot!(
model.domain,
data,
title="Temparature change since 1850",
ylabel=L"temperature anomaly [$^{\circ}$C]",
label=labels,
color=colors,
)
pl_finish_years_plot!(model, ylim=ylim, refline=2.0)
end
function pl_plot_controls(model::ClimateModel)
data = [
model.controls.mitigate,
model.controls.remove,
model.controls.adapt,
model.controls.geoeng,
]
labels = ["Mitigation" "CO₂ Removal" "Adaptation" "Geoengineering"]
# labels = [L"$M$ (emissions mitigation)" L"$R$ (carbon dioxide removal)" L"$A$ (adaptation)" L"$G$ (solar geoengineering)"]
plot(
model.domain,
data,
title="Optimized control deployments",
yformatter=pl_percent_formatter,
label=labels,
width=3,
)
pl_finish_years_plot!(model, ylim=(0., 1.))
end
function pl_plot_benefits(model::ClimateModel; discounted=true)
discount = if discounted
discounting(model)
else
1.0
end
when = (model.domain .> model.present_year)
benefits = (damage_cost_baseline(model) - damage_cost(model))
t = model.domain[when]
plot()
pl_fill_between!(
t,
zeros(size(t)),
((benefits - control_cost(model)) .* discount)[when],
fillcolor = "rgba(0,0,0,.15)",
label=nothing
)
data = [
(benefits .* discount)[when],
(- control_cost(model) .* discount)[when],
((benefits - control_cost(model)) .* discount)[when],
]
labels = [L"$T_{M,R}$ (controlled with $G=0$)" L"$T_{M,R,G}$ (controlled)" L"$T_{M,R,G,A}$ (adapted)"]
colors = [2 4 3]
plot!(
model.domain[when],
data,
title="Cost-benefit analysis",
ylabel=L"discounted costs and benefits [10$^{12}$ \$ / year]",
label=labels,
color=colors,
)
pl_finish_years_plot!(model, when=when, refline=0.0)
end
function pl_plot_damages(model::ClimateModel; discounted=false, normalized=false)
discount = if discounted
deepcopy(discounting(model))
else
1.0
end
E = if normalized
deepcopy(model.economics.GWP)/100.
else
1.0
end
when = (model.domain .> model.present_year)
t = model.domain[when]
plot()
pl_fill_between!(
t,
zeros(size(t)),
(control_cost(model) .* discount ./ E)[when],
fillcolor = 4,
#alpha=.2,
label=nothing
)
data = [
(damage_cost_baseline(model) .* discount ./ E)[when],
(net_cost(model) .* discount ./ E)[when],
(damage_cost(model) .* discount ./ E)[when],
(control_cost(model) .* discount ./ E)[when],
]
labels = ["uncontrolled damages" "net costs (controlled damages + controls)" "controlled damages" "cost of controls"]
colors = [1 :black 2 4]
plot!(
model.domain[when],
data,
title="Costs of avoiding a damage threshold",
ylabel=L"discounted costs [10$^{12}$ \$ / year]",
label=labels,
color=colors,
)
plot!(
t,
(model.economics.β .* (model.economics.GWP ./ E) .* (2.0^2).*ones(size(model.domain)))[when],
color="rgba(0,0,0,.5)",
label=L"damage threshold at 2$^{\circ}$ C with $A=0$",
linestyle=:dash
)
pl_finish_years_plot!(model)
end
# State plot
function pl_plot_state(model::ClimateModel; plot_legends=true)
plot(
pl_plot_emissions(model),
pl_plot_concentrations(model),
pl_plot_temperatures(model),
pl_plot_controls(model),
pl_plot_benefits(model),
pl_plot_damages(model),
layout=(2,3),
size=(1300,800)
)
end
### A Pluto.jl notebook ###
# v0.9.7
using Markdown
macro bind(def, element)
quote
local el = $(esc(element))
global $(esc(def)) = Core.applicable(Base.peek, el) ? Base.peek(el) : missing
el
end
end
# ╔═╡ 85d394a6-ab2e-11ea-162a-bf287f53a2b9
begin
cd("/mnt/c/dev/julia/margo_tests/")
import Pkg
Pkg.activate(".")
using ClimateMARGO
import Plots
import PlutoUI
Plots.gr()
end
# ╔═╡ ad4c245a-ab2e-11ea-315d-7bd757b9d9a6
include("./custom_plots_pop.jl")
# ╔═╡ 7b158352-ab43-11ea-06bf-070326039159
t = 2020.0:20:2200.0
# ╔═╡ b6292bb6-ab2e-11ea-3763-734fa0bcc219
begin
model_initial = ClimateModel(; t=collect(t), dt=step(t))
end
# ╔═╡ 5bd5d6be-ab33-11ea-19e4-cfa028000899
md"# Maximum temperature"
# ╔═╡ d71ce3a0-ab31-11ea-2c25-51b58f110211
md"##### `0.0 °C` $(@bind T_max PlutoUI.Slider(0.0 : 0.01 : 4.0; default=4.0)) `4.0 °C` "
# ╔═╡ 48169fe0-ab48-11ea-18e6-37d29e7f279b
md"## Results"
# ╔═╡ 4b9298ac-ab4a-11ea-050e-2d26ce07187f
@bind show_damages html"<input type='checkbox' id='show-damage'><label for='show-damage'> Show economic damage</label>"
# ╔═╡ 06339a5c-ab38-11ea-07ff-1f724147d164
@bind reset PlutoUI.Button("Reset graph")
# ╔═╡ f84c8ec6-ab34-11ea-0b6e-67ff6c00c45e
md"## Appendix"
# ╔═╡ d0df7460-ab2e-11ea-2d5f-adb730df9082
model = begin
optimize_controls!(model_initial; temp_goal=T_max)
model_initial
end
# ╔═╡ 4fd40bba-ab33-11ea-1112-c503ffda1799
"""
<blockquote><p style="font-size: 1.3rem">Temperature limit of <b>$(T_max) °C</b> can be achieved for <b>$(round(discounted_total_control_cost(model); sigdigits=3)) trillion USD</b>.</p></blockquote>
"""|> HTML
# ╔═╡ fc8aa7e0-ab2e-11ea-100b-b3e79670d056
pl_plot_controls(model)
# ╔═╡ c4159248-ab36-11ea-0304-efb66384670e
begin
reset # reactive reference
explored = (Number[], Number[], Number[])
end;
# ╔═╡ e71f4126-ab36-11ea-19bf-db353b290d68
explored_current = (
push!(explored[1], T_max),
push!(explored[2], discounted_total_control_cost(model)),
push!(explored[3], discounted_total_damage_cost(model)),
);
# ╔═╡ a8db7eae-ab48-11ea-2678-eb14348b2172
r = [1 2 3] |> typeof
# ╔═╡ d0c275a8-ab48-11ea-0739-8d4049d5bdd8
[1:3 1:3]
# ╔═╡ 5eace9d4-ab49-11ea-23dc-2bdb9ce7cc46
(
1,
2
)
# ╔═╡ 69aa4df2-ab4b-11ea-388b-679feb1c3627
function results_text(short)
if short
md"Try changing the goal temperature above!"
else
md"A **higher goal temperature** can be achieved at a **lower cost**. "
end
end
# ╔═╡ f162108c-ab4a-11ea-192a-db8fe908fe3b
results_text(length(explored_current[1]) < 10)
# ╔═╡ 8d84c28e-ab4b-11ea-3d37-87665a18b296
function explored_graph(explored_current, show_damages, T_max)
begin
cost_lim = show_damages ? 1600.0 : 400.0
data = if show_damages
[explored_current[2] explored_current[3]]
else
explored_current[2]
end
labels = show_damages ? ["Control cost" "Economic damage"] : "Control cost"
Plots.scatter(
explored_current[1],
data,
xlabel="Maximum temperature",
ylabel="Costs [trillion USD]",
xlims=(0.0, 4.0),
ylims=(0.0, cost_lim),
label=labels
)
pl_fill_between!([0.0, 2.0], [0.0, 0.0], [cost_lim, cost_lim]; color="rgba(30,30,255,.05)", label="Paris agreement")
Plots.vline!([T_max]; width=3, color="rgba(0,0,0,.3)", label="Selected goal")
end
end
# ╔═╡ cdee4292-ab36-11ea-004a-d1051fca11ec
explored_graph(explored_current, show_damages, T_max)
# ╔═╡ Cell order:
# ╠═85d394a6-ab2e-11ea-162a-bf287f53a2b9
# ╠═ad4c245a-ab2e-11ea-315d-7bd757b9d9a6
# ╠═7b158352-ab43-11ea-06bf-070326039159
# ╠═b6292bb6-ab2e-11ea-3763-734fa0bcc219
# ╟─5bd5d6be-ab33-11ea-19e4-cfa028000899
# ╟─d71ce3a0-ab31-11ea-2c25-51b58f110211
# ╟─4fd40bba-ab33-11ea-1112-c503ffda1799
# ╟─fc8aa7e0-ab2e-11ea-100b-b3e79670d056
# ╟─48169fe0-ab48-11ea-18e6-37d29e7f279b
# ╟─f162108c-ab4a-11ea-192a-db8fe908fe3b
# ╟─cdee4292-ab36-11ea-004a-d1051fca11ec
# ╟─4b9298ac-ab4a-11ea-050e-2d26ce07187f
# ╟─06339a5c-ab38-11ea-07ff-1f724147d164
# ╟─f84c8ec6-ab34-11ea-0b6e-67ff6c00c45e
# ╠═d0df7460-ab2e-11ea-2d5f-adb730df9082
# ╠═c4159248-ab36-11ea-0304-efb66384670e
# ╠═e71f4126-ab36-11ea-19bf-db353b290d68
# ╠═a8db7eae-ab48-11ea-2678-eb14348b2172
# ╠═d0c275a8-ab48-11ea-0739-8d4049d5bdd8
# ╠═5eace9d4-ab49-11ea-23dc-2bdb9ce7cc46
# ╠═69aa4df2-ab4b-11ea-388b-679feb1c3627
# ╠═8d84c28e-ab4b-11ea-3d37-87665a18b296
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment