Simon Danisch / Aug 18 2020
MIMI DICE2013 Climate Impact
We're using the MIMI framework to create a simple climate impact simulation.
We first install all the Julia & Python dependencies in a Nextjournal runtime.
This will be backed into a docker container, which can be used in other articles, or downloaded and run locally, with everything installed.
apt update
apt install python3 python3-venv python3-pip
pip3 install xlrd
19.9s
install (Bash in Julia)
cp(Manifest.toml, "Manifest.toml")
cp(Project.toml, "Project.toml")
ENV["PYTHON"] = "python3" # use system python
0.5s
install (Julia)
"Project.toml"
]registry add https://github.com/mimiframework/MimiRegistry.git
2.4s
install (Julia)
]activate .; instantiate
23.0s
install (Julia)
Now, lets create the simulation & interactive visualization in another runtime:
]activate .
12.2s
Julia
install
using Markdown
using Mimi, MimiDICE2013, DataFrames, AbstractPlotting, WGLMakie
using Observables, JSServe
using JSServe: Application, Slider, styled_slider
using JSServe.DOM
98.1s
Julia
install
function create_model()
# create a dice 2013 model
m = MimiDICE2013.get_model()
# Use higher climate sensitivity value
update_param!(m, :t2xco2, 5)
run(m)
return m
end
m_dice1 = create_model()
m_dice2 = create_model();
12.2s
Julia
install
# Add a better styled slider
function JSServe.styled_slider(slider, value)
JSServe.rows(DOM.div(slider, class="w-64"),
DOM.span(value, class="p-2"),
class="p-2 items-center"
)
end
function run_model(param)
update_param!(m_dice2, :MIU, fill(param, 60))
run(m_dice2)
something.(m_dice2[:climatedynamics, :TATM], 0.0)
end
set_theme!(font = "Dejavu Sans", resolution = (500, 400))
function handler(session, request)
# get the temperature forecasts:
df_dice1 = dropmissing(getdataframe(m_dice1, :climatedynamics, :TATM))
df_dice2 = dropmissing(getdataframe(m_dice2, :climatedynamics, :TATM))
s = JSServe.Slider(range(0, stop=1, step=0.01))
data = map(run_model, s)
scene = scatter(df_dice1.time, df_dice1.TATM, markersize = 5)
scatter!(df_dice2.time, data, color = :red, markersize = 5)
scene[Axis].names.axisnames = ("Year", "Temperature Increase")
scene[Axis].names.textsize = (8, 8)
t = df_dice2.time
year = JSServe.Slider(1:length(t))
dmg_estimated = map(year, s) do year_idx, s
dmg = round(m_dice2[:damages, :DAMAGES][year_idx], digits = 3)
return "$(dmg) trillion USD"
end
year_sel = map(i-> t[i], year)
b = DOM.font("● baseline", color = :black)
ec = DOM.font("● with emission control", color = :red)
styled_s = styled_slider(s, map(x-> round(x, digits = 3), s.value))
styled_damage = styled_slider(year, DOM.div(dmg_estimated))
dom = md"""
# Explore Climate
Set amount of emission control: $styled_s
# Temperature Increase
$b
$ec
$(scene)
# Estimated damage in year $(year_sel)
$(styled_damage)
"""
dom = DOM.div(JSServe.MarkdownCSS, JSServe.TailwindCSS, JSServe.Styling, dom, class="w-2/3")
# Record all states of the simulation, to have them
# available when the julia server is offline:
return JSServe.record_state_map(session, dom).dom
end
JSServe.with_session(handler)
2.8s
Julia
install