-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.jl
149 lines (116 loc) · 4.72 KB
/
main.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
include("./server.jl")
ENV["JULIA_MARGO_LOAD_PYPLOT"] = "no thank you"
import ClimateMARGO
using ClimateMARGO.Models
using ClimateMARGO.Optimization
using ClimateMARGO.Diagnostics
# some silly code to make our custom types work with MsgPack (JSON alternative)
import MsgPack
MsgPack.msgpack_type(::Type{ClimateModel}) = MsgPack.StructType()
MsgPack.msgpack_type(::Type{ClimateModelParameters}) = MsgPack.StructType()
MsgPack.msgpack_type(::Type{Domain}) = MsgPack.StructType()
MsgPack.msgpack_type(::Type{Economics}) = MsgPack.StructType()
MsgPack.msgpack_type(::Type{Physics}) = MsgPack.StructType()
MsgPack.msgpack_type(::Type{Controls}) = MsgPack.StructType()
function setfieldconvert!(value, name::Symbol, x)
setfield!(value, name, convert(typeof(getfield(value, name)), x))
end
# the main margo function
@expose function opt_controls_temp(;dt=20, opt_parameters, economics=Dict(), physics=Dict())
model_parameters = deepcopy(ClimateMARGO.IO.included_configurations["default"])::ClimateModelParameters
model_parameters.domain = Domain(Float64(dt), 2020.0, 2200.0)
model_parameters.economics.baseline_emissions = ramp_emissions(model_parameters.domain)
model_parameters.economics.extra_CO₂ = zeros(size(model_parameters.economics.baseline_emissions))
for (k, v) in economics
setfieldconvert!(model_parameters.economics, Symbol(k), v)
end
for (k, v) in physics
setfieldconvert!(model_parameters.physics, Symbol(k), v)
end
model = ClimateModel(model_parameters)
parsed = Dict((Symbol(k) => v) for (k, v) in opt_parameters)
model_optimizer = optimize_controls!(model; parsed..., print_raw_status=false)
return Dict(
:model_parameters => model_parameters,
:status => ClimateMARGO.Optimization.JuMP.termination_status(model_optimizer) |> string,
model_results(model)...
)
end
@expose function forward_controls_temp(;dt=20, controls=Dict(), economics=Dict(), physics=Dict())
model_parameters = deepcopy(ClimateMARGO.IO.included_configurations["default"])::ClimateModelParameters
model_parameters.domain = Domain(Float64(dt), 2020.0, 2200.0)
model_parameters.economics.baseline_emissions = ramp_emissions(model_parameters.domain)
model_parameters.economics.extra_CO₂ = zeros(size(model_parameters.economics.baseline_emissions))
for (k, v) in economics
setfieldconvert!(model_parameters.economics, Symbol(k), v)
end
for (k, v) in physics
setfieldconvert!(model_parameters.physics, Symbol(k), v)
end
model = ClimateModel(model_parameters)
translations = Dict(
:M => :mitigate,
:R => :remove,
:G => :geoeng,
:A => :adapt,
)
for (k, v) in controls
setfieldconvert!(model.controls, translations[Symbol(k)], v)
end
return Dict(
:model_parameters => model_parameters,
model_results(model)...
)
end
model_results(model::ClimateModel) = Dict(
:controls => model.controls,
:computed => Dict(
:temperatures => Dict(
:baseline => T_adapt(model),
:M => T_adapt(model; M=true),
:MR => T_adapt(model; M=true, R=true),
:MRG => T_adapt(model; M=true, R=true, G=true),
:MRGA => T_adapt(model; M=true, R=true, G=true, A=true),
),
:emissions => Dict(
:baseline => effective_emissions(model),
:M => effective_emissions(model; M=true),
:MRGA => effective_emissions(model; M=true, R=true),
),
:concentrations => Dict(
:baseline => c(model),
:M => c(model; M=true),
:MRGA => c(model; M=true, R=true),
),
:damages => Dict(
:baseline => costs_dict(damage(model; discounting=true), model),
:MRGA => costs_dict(damage(model; M=true, R=true, G=true, A=true, discounting=true), model),
),
:costs => Dict(
:M => costs_dict(cost(model; M=true, discounting=true), model),
:R => costs_dict(cost(model; R=true, discounting=true), model),
:G => costs_dict(cost(model; G=true, discounting=true), model),
:A => costs_dict(cost(model; A=true, discounting=true), model),
:MRGA => costs_dict(cost(model; M=true, R=true, G=true, A=true, discounting=true), model),
),
),
)
function costs_dict(costs, model)
Dict(
:discounted => costs,
:total_discounted => sum(costs .* model.domain.dt),
)
end
# simple function as baseline for connection speed
@expose function arithmetic(;a, b)
Dict(
:sum => a + b,
:difference => a - b,
)
end
@expose function randimage(;width, height)
img = rand(UInt8, width * height * 4)
Dict(
:img => img,
)
end