Skip to content

Commit

Permalink
Merge pull request #104 from fund-model/params
Browse files Browse the repository at this point in the history
Use `update_param!` instead of `set_param!`
  • Loading branch information
davidanthoff authored Jun 8, 2023
2 parents 09545c8 + 61e2c09 commit ea4408b
Show file tree
Hide file tree
Showing 154 changed files with 114 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ scc = MimiFUND.compute_scco2(year = 2020, eta = 0., prtp = 0.03, equity_weights

# Or, you can also compute the SC-CO2 from a modified version of a MimiFUND model:
m = MimiFUND.get_model() # Get the default version of the FUND model
set_param!(m, :climatesensitivity, 5) # make any modifications to your model using Mimi
update_param!(m, :climatedynamics, :climatesensitivity, 5) # make any modifications to your model using Mimi
scc = MimiFUND.compute_scco2(m, year = 2020) # Compute the SC-CO2 from your model
```
There are also functions for computing the Social Cost of CH4, N2O, and SF6: `compute_scch4`, `compute_scn2o`, and `compute_scsf6`.
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
17 changes: 17 additions & 0 deletions data/unshared_parameters/climateregional-bregtmp.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# FUND scenario
USA,1.1941
CAN,1.4712
WEU,1.1248
JPK,1.0555
ANZ,0.9676
EEU,1.1676
FSU,1.2866
MDE,1.1546
CAM,0.8804
LAM,0.8504
SAS,0.9074
SEA,0.7098
CHI,1.1847
MAF,1.143
SSA,0.878
SIS,0.7517
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
73 changes: 70 additions & 3 deletions src/MimiFUND.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function get_model(; nsteps = default_nsteps, datadir = default_datadir, params
# ---------------------------------------------

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

parameters = params === nothing ? load_default_parameters(datadir) : params

for (name, value) in parameters
set_param!(m, name, value)
# Set unshared parameters - name is a Tuple{Symbol, Symbol} of (component_name, param_name)
for (name, value) in parameters[:unshared]
update_param!(m, name[1], name[2], value)
end

# Set shared parameters - name is a Symbol representing the param_name, here
# we will create a shared model parameter with the same name as the component
# parameter and then connect our component parameters to this shared model parameter

# * for convenience later, name shared model parameter same as the component
# parameters, but this is not required could give a unique name *
add_shared_param!(m, :ch4pre, parameters[:shared][:ch4pre])
connect_param!(m, :climateforcing, :ch4pre, :ch4pre)
connect_param!(m, :climatech4cycle, :ch4pre, :ch4pre)

add_shared_param!(m, :n2opre, parameters[:shared][:n2opre])
connect_param!(m, :climateforcing, :n2opre, :n2opre)
connect_param!(m, :climaten2ocycle, :n2opre, :n2opre)

add_shared_param!(m, :sf6pre, parameters[:shared][:sf6pre])
connect_param!(m, :climateforcing, :sf6pre, :sf6pre)
connect_param!(m, :climatesf6cycle, :sf6pre, :sf6pre)

add_shared_param!(m, :co2pre, parameters[:shared][:co2pre])
connect_param!(m, :climateforcing, :co2pre, :co2pre)
connect_param!(m, :impactagriculture, :co2pre, :co2pre)
connect_param!(m, :impactextratropicalstorms, :co2pre, :co2pre)
connect_param!(m, :impactforests, :co2pre, :co2pre)

add_shared_param!(m, :nospecbase, parameters[:shared][:nospecbase])
connect_param!(m, :biodiversity, :nospecbase, :nospecbase)
connect_param!(m, :impactbiodiversity, :nospecbase, :nospecbase)

add_shared_param!(m, :dbsta, parameters[:shared][:dbsta])
connect_param!(m, :biodiversity, :dbsta, :dbsta)
connect_param!(m, :impactbiodiversity, :dbsta, :dbsta)

add_shared_param!(m, :bregtmp, parameters[:shared][:bregtmp], dims=[:regions])
connect_param!(m, :climateregional, :bregtmp, :bregtmp)
connect_param!(m, :impactdiarrhoea, :bregtmp, :bregtmp)

add_shared_param!(m, :plus90, parameters[:shared][:plus90], dims=[:regions])
connect_param!(m, :impactcardiovascularrespiratory, :plus90, :plus90)
connect_param!(m, :socioeconomic, :plus90, :plus90)

add_shared_param!(m, :gdp90, parameters[:shared][:gdp90], dims=[:regions])
connect_param!(m, :emissions, :gdp90, :gdp90)
connect_param!(m, :impactagriculture, :gdp90, :gdp90)
connect_param!(m, :impactcooling, :gdp90, :gdp90)
connect_param!(m, :impactdiarrhoea, :gdp90, :gdp90)
connect_param!(m, :impactextratropicalstorms, :gdp90, :gdp90)
connect_param!(m, :impactforests, :gdp90, :gdp90)
connect_param!(m, :impactheating, :gdp90, :gdp90)
connect_param!(m, :impacttropicalstorms, :gdp90, :gdp90)
connect_param!(m, :impactvectorbornediseases, :gdp90, :gdp90)
connect_param!(m, :impactwaterresources, :gdp90, :gdp90)
connect_param!(m, :socioeconomic, :gdp90, :gdp90)

add_shared_param!(m, :pop90, parameters[:shared][:pop90], dims=[:regions])
connect_param!(m, :emissions, :pop90, :pop90)
connect_param!(m, :impactagriculture, :pop90, :pop90)
connect_param!(m, :impactcooling, :pop90, :pop90)
connect_param!(m, :impactdiarrhoea, :pop90, :pop90)
connect_param!(m, :impactextratropicalstorms, :pop90, :pop90)
connect_param!(m, :impactforests, :pop90, :pop90)
connect_param!(m, :impactheating, :pop90, :pop90)
connect_param!(m, :impacttropicalstorms, :pop90, :pop90)
connect_param!(m, :impactvectorbornediseases, :pop90, :pop90)
connect_param!(m, :impactwaterresources, :pop90, :pop90)
connect_param!(m, :socioeconomic, :pop90, :pop90)

# Reset the time dimension if needed
reset_time_dimension ? set_dimension!(m, :time, collect(1950:1950 + nsteps)) : nothing

Expand Down
26 changes: 20 additions & 6 deletions src/helper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,31 @@ end


"""
Reads parameter csvs from data directory into a dictionary (parameter_name => default_value).
Reads parameter csvs from data directory into a dictionary with two keys:
* :shared => (parameter_name => default_value) for parameters shared in the model
* :unshared => ((component_name, parameter_name) => default_value) for component specific parameters that are not shared
For parameters defined as distributions, this sets the value to their mode.
"""
function load_default_parameters(datadir = joinpath(dirname(@__FILE__), "..", "data"))
files = readdir(datadir)
filter!(i -> i != "desktop.ini", files)
parameters = Dict{Symbol, Any}(Symbol(splitext(file)[1]) => readdlm(joinpath(datadir,file), ',', comments = true) for file in files)

prepparameters!(parameters)
# Load the unshared parameters
files = readdir(joinpath(datadir, "unshared_parameters"))
filter!(i -> (i != "desktop.ini" && i != ".DS_Store"), files)
unshared_parameters = Dict{Tuple{Symbol,Symbol}, Any}()
for file in files
param_info = Symbol.(split(splitext(file)[1], "-"))
unshared_parameters[(param_info[1], param_info[2])] = readdlm(joinpath(datadir,"unshared_parameters",file), ',', comments = true)
end
prepparameters!(unshared_parameters)

# Handle the shared parameters
files = readdir(joinpath(datadir, "shared_parameters"))
filter!(i -> (i != "desktop.ini" && i != ".DS_Store"), files)
shared_parameters = Dict{Symbol, Any}(Symbol(splitext(file)[1]) => readdlm(joinpath(datadir,"shared_parameters",file), ',', comments = true) for file in files)
prepparameters!(shared_parameters)

return parameters
return Dict(:shared => shared_parameters, :unshared => unshared_parameters)
end


Expand Down
2 changes: 1 addition & 1 deletion src/marginaldamage3.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function getmarginalmodels(; gas = :C, emissionyear = 2010, parameters = nothing
add_comp!(m2, Mimi.adder, :marginalemission, before = :climateco2cycle, first = 1951)
addem = zeros(yearstorun)
addem[getindexfromyear(emissionyear)-1:getindexfromyear(emissionyear) + 8] .= 1.0
set_param!(m2, :marginalemission, :add, addem)
update_param!(m2, :marginalemission, :add, addem)

# Reconnect the appropriate emissions in the marginal model
if gas == :C
Expand Down
2 changes: 1 addition & 1 deletion src/marginaldamages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function getmarginaldamages(; emissionyear=2010, parameters = nothing, yearstoag
add_comp!(m2, Mimi.adder, :marginalemission, before = :climateco2cycle, first = 1951)
addem = zeros(yearstorun)
addem[getindexfromyear(emissionyear)-1:getindexfromyear(emissionyear) + 8] .= 1.0
set_param!(m2, :marginalemission, :add, addem)
update_param!(m2, :marginalemission, :add, addem)

# Reconnect the appropriate emissions in the marginal model
if gas == :C
Expand Down
2 changes: 1 addition & 1 deletion src/montecarlo/defmcs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fund_default_mcs = @defsim begin
# none

##
## unshared model parameters, or parameters specific to only one component that are shared due to old set_param! usage
## unshared model parameters
##

scenariouncertainty.aceiadd = ["USA" => Normal(0.0,0.025), "CAN" => Normal(0.0,0.025), "WEU" => Normal(0.0,0.025), "JPK" => Normal(0.0,0.0375), "ANZ" => Normal(0.0,0.0375), "EEU" => Normal(0.0,0.025), "FSU" => Normal(0.0,0.025), "MDE" => Normal(0.0,0.0875), "CAM" => Normal(0.0,0.05625), "LAM" => Normal(0.0,0.05625), "SAS" => Normal(0.0,0.0875), "SEA" => Normal(0.0,0.0875), "CHI" => Normal(0.0,0.0375), "MAF" => Normal(0.0,0.11875), "SSA" => Normal(0.0,0.11875), "SIS" => Normal(0.0,0.05625)]
Expand Down
2 changes: 1 addition & 1 deletion src/montecarlo/load_mcs_RVs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This function loads the FUND input parameters stored in datadir into the syntax
needed for the @defmcs macro. This script writes to a text file which can then
be copied into a @defmcs macro.
"""
function load_mcs_RVs(; txt_out = joinpath(@__DIR__, "mcs_RVs.txt"), datadir = joinpath(@__DIR__, "../../data_for_load_mcs_RVs"), string_labels = false)
function load_mcs_RVs(; txt_out = joinpath(@__DIR__, "mcs_RVs.txt"), datadir = joinpath(@__DIR__, "../../data/data_for_load_mcs_RVs"), string_labels = false)

files = readdir(datadir)
filter!(i->i!="desktop.ini", files)
Expand Down
2 changes: 1 addition & 1 deletion src/new_marginaldamages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ function add_marginal_emissions!(m, year::Union{Int, Nothing} = nothing; gas::Sy
# units ie. CO2 to expected units ie. C
addem[getindexfromyear(year):getindexfromyear(year) + 9] .= pulse_size / (10 * _weight_normalization(gas)) * _gas_normalization(gas)
end
set_param!(m, :emissionspulse, :add, addem)
update_param!(m, :emissionspulse, :add, addem)

# Reconnect the appropriate emissions in m
if gas == :CO2
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ scch4 = MimiFUND.compute_scch4(year = 2020)

# Test with a modified model
m_high_cs = MimiFUND.get_model()
set_param!(m_high_cs, :climatesensitivity, 5)
update_param!(m_high_cs, :climatedynamics, :climatesensitivity, 5)
scc6 = MimiFUND.compute_scco2(m_high_cs, year=2020, last_year=2300)
@test scc6 > scc2 # test that it's higher than the default because of a higher climate sensitivity

Expand Down

0 comments on commit ea4408b

Please sign in to comment.