Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to get FUND working with class-based branch. #40

Merged
merged 7 commits into from
Oct 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
CSVFiles = "5d742f6a-9f54-50ce-8119-2520741973ca"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Query = "1a8c2f83-1ff3-5112-b086-8aa67b057ba1"

[compat]
julia = "1"
Expand All @@ -22,4 +23,4 @@ Mimi = "0.9.4"
Distributions = "0.20, 0.21"

[targets]
test = ["Test", "DataFrames", "CSVFiles"]
test = ["Test", "DataFrames", "CSVFiles", "Query"]
46 changes: 23 additions & 23 deletions src/MimiFUND.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function get_model(; nsteps = default_nsteps, datadir = default_datadir, params
# ---------------------------------------------

add_comp!(m, scenariouncertainty)
add_comp!(m, population)
add_comp!(m, population, :pop_component) # can't have external params and components with the same name
add_comp!(m, geography)
add_comp!(m, socioeconomic)
add_comp!(m, emissions)
Expand Down Expand Up @@ -109,23 +109,23 @@ function get_model(; nsteps = default_nsteps, datadir = default_datadir, params

connect_param!(m, :geography, :landloss, :impactsealevelrise, :landloss)

connect_param!(m, :population, :pgrowth, :scenariouncertainty, :pgrowth)
connect_param!(m, :population, :enter, :impactsealevelrise, :enter)
connect_param!(m, :population, :leave, :impactsealevelrise, :leave)
connect_param!(m, :population, :dead, :impactdeathmorbidity, :dead)
connect_param!(m, :pop_component, :pgrowth, :scenariouncertainty, :pgrowth)
connect_param!(m, :pop_component, :enter, :impactsealevelrise, :enter)
connect_param!(m, :pop_component, :leave, :impactsealevelrise, :leave)
connect_param!(m, :pop_component, :dead, :impactdeathmorbidity, :dead)

connect_param!(m, :socioeconomic, :area, :geography, :area)
connect_param!(m, :socioeconomic, :globalpopulation, :population, :globalpopulation)
connect_param!(m, :socioeconomic, :populationin1, :population, :populationin1)
connect_param!(m, :socioeconomic, :population, :population, :population)
connect_param!(m, :socioeconomic, :globalpopulation, :pop_component, :globalpopulation)
connect_param!(m, :socioeconomic, :populationin1, :pop_component, :populationin1)
connect_param!(m, :socioeconomic, :population, :pop_component, :population)
connect_param!(m, :socioeconomic, :pgrowth, :scenariouncertainty, :pgrowth)
connect_param!(m, :socioeconomic, :ypcgrowth, :scenariouncertainty, :ypcgrowth)
connect_param!(m, :socioeconomic, :eloss, :impactaggregation, :eloss)
connect_param!(m, :socioeconomic, :sloss, :impactaggregation, :sloss)
connect_param!(m, :socioeconomic, :mitigationcost, :emissions, :mitigationcost)

connect_param!(m, :emissions, :income, :socioeconomic, :income)
connect_param!(m, :emissions, :population, :population, :population)
connect_param!(m, :emissions, :population, :pop_component, :population)
connect_param!(m, :emissions, :forestemm, :scenariouncertainty, :forestemm)
connect_param!(m, :emissions, :aeei, :scenariouncertainty, :aeei)
connect_param!(m, :emissions, :acei, :scenariouncertainty, :acei)
Expand Down Expand Up @@ -154,58 +154,58 @@ function get_model(; nsteps = default_nsteps, datadir = default_datadir, params

connect_param!(m, :ocean, :temp, :climatedynamics, :temp)

connect_param!(m, :impactagriculture, :population, :population, :population)
connect_param!(m, :impactagriculture, :population, :pop_component, :population)
connect_param!(m, :impactagriculture, :income, :socioeconomic, :income)
connect_param!(m, :impactagriculture, :temp, :climateregional, :temp)
connect_param!(m, :impactagriculture, :acco2, :climateco2cycle, :acco2)

connect_param!(m, :impactbiodiversity, :temp, :climateregional, :temp)
connect_param!(m, :impactbiodiversity, :nospecies, :biodiversity, :nospecies)
connect_param!(m, :impactbiodiversity, :income, :socioeconomic, :income)
connect_param!(m, :impactbiodiversity, :population, :population, :population)
connect_param!(m, :impactbiodiversity, :population, :pop_component, :population)

connect_param!(m, :impactcardiovascularrespiratory, :population, :population, :population)
connect_param!(m, :impactcardiovascularrespiratory, :population, :pop_component, :population)
connect_param!(m, :impactcardiovascularrespiratory, :temp, :climateregional, :temp)
connect_param!(m, :impactcardiovascularrespiratory, :plus, :socioeconomic, :plus)
connect_param!(m, :impactcardiovascularrespiratory, :urbpop, :socioeconomic, :urbpop)

connect_param!(m, :impactcooling, :population, :population, :population)
connect_param!(m, :impactcooling, :population, :pop_component, :population)
connect_param!(m, :impactcooling, :income, :socioeconomic, :income)
connect_param!(m, :impactcooling, :temp, :climateregional, :temp)
connect_param!(m, :impactcooling, :cumaeei, :emissions, :cumaeei)

connect_param!(m, :impactdiarrhoea, :population, :population, :population)
connect_param!(m, :impactdiarrhoea, :population, :pop_component, :population)
connect_param!(m, :impactdiarrhoea, :income, :socioeconomic, :income)
connect_param!(m, :impactdiarrhoea, :regtmp, :climateregional, :regtmp)

connect_param!(m, :impactextratropicalstorms, :population, :population, :population)
connect_param!(m, :impactextratropicalstorms, :population, :pop_component, :population)
connect_param!(m, :impactextratropicalstorms, :income, :socioeconomic, :income)
connect_param!(m, :impactextratropicalstorms, :acco2, :climateco2cycle, :acco2)

connect_param!(m, :impactforests, :population, :population, :population)
connect_param!(m, :impactforests, :population, :pop_component, :population)
connect_param!(m, :impactforests, :income, :socioeconomic, :income)
connect_param!(m, :impactforests, :temp, :climateregional, :temp)
connect_param!(m, :impactforests, :acco2, :climateco2cycle, :acco2)

connect_param!(m, :impactheating, :population, :population, :population)
connect_param!(m, :impactheating, :population, :pop_component, :population)
connect_param!(m, :impactheating, :income, :socioeconomic, :income)
connect_param!(m, :impactheating, :temp, :climateregional, :temp)
connect_param!(m, :impactheating, :cumaeei, :emissions, :cumaeei)

connect_param!(m, :impactvectorbornediseases, :population, :population, :population)
connect_param!(m, :impactvectorbornediseases, :population, :pop_component, :population)
connect_param!(m, :impactvectorbornediseases, :income, :socioeconomic, :income)
connect_param!(m, :impactvectorbornediseases, :temp, :climateregional, :temp)

connect_param!(m, :impacttropicalstorms, :population, :population, :population)
connect_param!(m, :impacttropicalstorms, :population, :pop_component, :population)
connect_param!(m, :impacttropicalstorms, :income, :socioeconomic, :income)
connect_param!(m, :impacttropicalstorms, :regstmp, :climateregional, :regstmp)

connect_param!(m, :vslvmorb, :population, :population, :population)
connect_param!(m, :vslvmorb, :population, :pop_component, :population)
connect_param!(m, :vslvmorb, :income, :socioeconomic, :income)

connect_param!(m, :impactdeathmorbidity, :vsl, :vslvmorb, :vsl)
connect_param!(m, :impactdeathmorbidity, :vmorb, :vslvmorb, :vmorb)
connect_param!(m, :impactdeathmorbidity, :population, :population, :population)
connect_param!(m, :impactdeathmorbidity, :population, :pop_component, :population)
connect_param!(m, :impactdeathmorbidity, :dengue, :impactvectorbornediseases, :dengue)
connect_param!(m, :impactdeathmorbidity, :schisto, :impactvectorbornediseases, :schisto)
connect_param!(m, :impactdeathmorbidity, :malaria, :impactvectorbornediseases, :malaria)
Expand All @@ -217,11 +217,11 @@ function get_model(; nsteps = default_nsteps, datadir = default_datadir, params
connect_param!(m, :impactdeathmorbidity, :extratropicalstormsdead, :impactextratropicalstorms, :extratropicalstormsdead)
connect_param!(m, :impactdeathmorbidity, :diasick, :impactdiarrhoea, :diasick)

connect_param!(m, :impactwaterresources, :population, :population, :population)
connect_param!(m, :impactwaterresources, :population, :pop_component, :population)
connect_param!(m, :impactwaterresources, :income, :socioeconomic, :income)
connect_param!(m, :impactwaterresources, :temp, :climateregional, :temp)

connect_param!(m, :impactsealevelrise, :population, :population, :population)
connect_param!(m, :impactsealevelrise, :population, :pop_component, :population)
connect_param!(m, :impactsealevelrise, :income, :socioeconomic, :income)
connect_param!(m, :impactsealevelrise, :sea, :ocean, :sea)
connect_param!(m, :impactsealevelrise, :area, :geography, :area)
Expand Down
44 changes: 28 additions & 16 deletions src/new_marginaldamages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -266,35 +266,47 @@ function get_marginal_model(m::Model = get_model(); gas::Symbol = :CO2, year::Un
return mm
end

# A component for an emissions pulse to be used in social cost calculations. Computes the `output` vector by adding
# `add` to `input`. This is similar to the Mimi.adder component, except that it allows missing values to be passed through.
@defcomp emissionspulse begin
add = Parameter(index=[time])
input = Parameter(index=[time])
output = Variable(index=[time])

function run_timestep(p, v, d, t)
v.output[t] = Mimi.@allow_missing(p.input[t]) + p.add[t]
end
end

"""
Adds a marginalemission component to `m`, and sets the additional emissions if a year is specified.
Adds an emissionspulse component to `m`, and sets the additional emissions if a year is specified.
The size of the marginal emission pulse can be modified with the `pulse_size` keyword argument, in metric tonnes.
"""
function add_marginal_emissions!(m, year::Union{Int, Nothing} = nothing; gas::Symbol = :CO2, pulse_size::Float64 = 1e7)

# Add additional emissions to m
add_comp!(m, Mimi.adder, :marginalemission, before = :climateco2cycle, first = 1951)
add_comp!(m, emissionspulse, before = :climateco2cycle)
nyears = length(Mimi.time_labels(m))
addem = zeros(nyears - 1) # starts one year later, in 1951
addem = zeros(nyears)
if year != nothing
# pulse is spread over ten years, and emissions components is in Mt so divide by 1e7, and convert from CO2 to C if gas==:CO2 because emissions component is in MtC
addem[getindexfromyear(year)-1:getindexfromyear(year) + 8] .= pulse_size / 1e7 * (gas == :CO2 ? 12/44 : 1)
addem[getindexfromyear(year):getindexfromyear(year) + 9] .= pulse_size / 1e7 * (gas == :CO2 ? 12/44 : 1)
end
set_param!(m, :marginalemission, :add, addem)
set_param!(m, :emissionspulse, :add, addem)

# Reconnect the appropriate emissions in m
if gas == :CO2
connect_param!(m, :marginalemission, :input, :emissions, :mco2)
connect_param!(m, :climateco2cycle, :mco2, :marginalemission, :output, repeat([missing], nyears))
connect_param!(m, :emissionspulse, :input, :emissions, :mco2)
connect_param!(m, :climateco2cycle, :mco2, :emissionspulse, :output)
elseif gas == :CH4
connect_param!(m, :marginalemission, :input, :emissions, :globch4)
connect_param!(m, :climatech4cycle, :globch4, :marginalemission, :output, repeat([missing], nyears))
connect_param!(m, :emissionspulse, :input, :emissions, :globch4)
connect_param!(m, :climatech4cycle, :globch4, :emissionspulse, :output)
elseif gas == :N2O
connect_param!(m, :marginalemission, :input, :emissions, :globn2o)
connect_param!(m, :climaten2ocycle, :globn2o, :marginalemission, :output, repeat([missing], nyears))
connect_param!(m, :emissionspulse, :input, :emissions, :globn2o)
connect_param!(m, :climaten2ocycle, :globn2o, :emissionspulse, :output)
elseif gas == :SF6
connect_param!(m, :marginalemission, :input, :emissions, :globsf6)
connect_param!(m, :climatesf6cycle, :globsf6, :marginalemission, :output, repeat([missing], nyears))
connect_param!(m, :emissionspulse, :input, :emissions, :globsf6)
connect_param!(m, :climatesf6cycle, :globsf6, :emissionspulse, :output)
else
error("Unknown gas: $gas")
end
Expand All @@ -304,14 +316,14 @@ end
"""
Helper function to set the marginal emissions in the specified year.
"""
function perturb_marginal_emissions!(m::Model, year; comp_name::Symbol = :marginalemission, pulse_size::Float64 = 1e7, gas::Symbol = :CO2)
function perturb_marginal_emissions!(m::Model, year; comp_name::Symbol = :emissionspulse, pulse_size::Float64 = 1e7, gas::Symbol = :CO2)

ci = compinstance(m, comp_name)
emissions = Mimi.get_param_value(ci, :add)

nyears = length(Mimi.dimension(m, :time))
new_em = zeros(nyears - 1)
new_em[getindexfromyear(year)-1:getindexfromyear(year) + 8] .= pulse_size / 1e7 * (gas == :CO2 ? 12/44 : 1)
new_em = zeros(nyears)
new_em[getindexfromyear(year):getindexfromyear(year) + 9] .= pulse_size / 1e7 * (gas == :CO2 ? 12/44 : 1)
emissions[:] = new_em

end
Expand Down
Loading