Skip to content

Commit

Permalink
Format files using DocumentFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
davidanthoff authored Aug 26, 2022
1 parent b8da5f4 commit 85a721d
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 111 deletions.
10 changes: 5 additions & 5 deletions src/components/RegionAggregatorSum.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Mimi

@defcomp RegionAggregatorSum begin

inputregions = Index()
outputregions = Index()

Expand All @@ -14,17 +14,17 @@ using Mimi
input = Parameter(index=[time, inputregions])
output = Variable(index=[time, outputregions])

function init(p,v,d)
function init(p, v, d)
idxs = indexin(p.input_output_mapping, p.output_region_names)
!isnothing(findfirst(i -> isnothing(i), idxs)) ? error("All provided region names in the RegionAggregatorSum's input_output_mapping Parameter must exist in the output_region_names Parameter.") : nothing
v.input_output_mapping_int[:] = idxs
end

function run_timestep(p,v,d,t)
v.output[t, :] .= 0.
function run_timestep(p, v, d, t)
v.output[t, :] .= 0.0

for i in d.inputregions
v.output[t, v.input_output_mapping_int[i]] += p.input[t,i]
v.output[t, v.input_output_mapping_int[i]] += p.input[t, i]
end
end
end
72 changes: 36 additions & 36 deletions src/components/SSPs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,37 @@ using Mimi, CSVFiles, DataFrames, Query, Interpolations

country = Index()

SSP_source = Parameter{String}() # can be one of IIASA GDP, OECD Env-Growth, PIK GDP_32, and Benveniste
SSP = Parameter{String}() # can be one of SSP1, SSP2, SSP3, SSP4, SSP5
emissions_source = Parameter{String}() # can be one of Leach
emissions_scenario = Parameter{String}() # can be one of SSP119, SSP126, SSP245, SSP370, SSP585
SSP_source = Parameter{String}() # can be one of IIASA GDP, OECD Env-Growth, PIK GDP_32, and Benveniste
SSP = Parameter{String}() # can be one of SSP1, SSP2, SSP3, SSP4, SSP5
emissions_source = Parameter{String}() # can be one of Leach
emissions_scenario = Parameter{String}() # can be one of SSP119, SSP126, SSP245, SSP370, SSP585

country_names = Parameter{String}(index=[country]) # need the names of the countries from the dimension
country_names = Parameter{String}(index=[country]) # need the names of the countries from the dimension

population = Variable(index=[time, country], unit="million")
population_global = Variable(index=[time], unit="million")
gdp = Variable(index=[time, country], unit="billion US\$2005/yr")
gdp_global = Variable(index=[time], unit="billion US\$2005/yr")
population = Variable(index=[time, country], unit="million")
population_global = Variable(index=[time], unit="million")
gdp = Variable(index=[time, country], unit="billion US\$2005/yr")
gdp_global = Variable(index=[time], unit="billion US\$2005/yr")

co2_emissions = Variable(index=[time], unit="GtC/yr")
ch4_emissions = Variable(index=[time], unit="MtCH4/yr")
n2o_emissions = Variable(index=[time], unit="MtN/yr")
sf6_emissions = Variable(index=[time], unit="MtSF6/yr")
co2_emissions = Variable(index=[time], unit="GtC/yr")
ch4_emissions = Variable(index=[time], unit="MtCH4/yr")
n2o_emissions = Variable(index=[time], unit="MtN/yr")
sf6_emissions = Variable(index=[time], unit="MtSF6/yr")

function init(p,v,d)
function init(p, v, d)

# ----------------------------------------------------------------------
# Checks

ssp_model_options = ["IIASA GDP", "OECD Env-Growth", "PIK GDP_23", "Benveniste"]
!(p.SSP_source in ssp_model_options) && error("Model $(p.SSP_source) provided to SSPs component SSP_source parameter not found in available list: $(ssp_model_options)")

ssp_options = ["SSP1", "SSP2", "SSP3", "SSP4", "SSP5"]
!(p.SSP in ssp_options) && error("SSP $(p.SSP) provided to SSPs component SSP parameter not found in available list: $(ssp_options)")

emissions_source_options = ["Leach"]
!(p.emissions_source in emissions_source_options) && error("Model $(p.emissions_source) provided to SSPs component emissions_source parameter not found in available list: $(emissions_source_options)")

emissions_scenario_options = ["SSP119", "SSP126", "SSP245", "SSP370", "SSP585"]
!(p.emissions_scenario in emissions_scenario_options) && error("emissions_scenario $(p.emissions_scenario) provided to SSPs component emissions_scenario parameter not found in available list: $(emissions_scenario_options)")

Expand Down Expand Up @@ -73,21 +73,21 @@ using Mimi, CSVFiles, DataFrames, Query, Interpolations

if !haskey(g_emissions_scenario_datasets, emissions_scenario_dict_key)
if p.emissions_source == "Leach"
emissions_data = load(emissions_path, skiplines_begin = 6)|>
DataFrame |>
i -> rename!(i, Symbol.([:year, names(i)[2:end]...])) |>
DataFrame |>
@select(:year, :carbon_dioxide, :nitrous_oxide, :methane, :sf6) |>
DataFrame
emissions_data = load(emissions_path, skiplines_begin=6) |>
DataFrame |>
i -> rename!(i, Symbol.([:year, names(i)[2:end]...])) |>
DataFrame |>
@select(:year, :carbon_dioxide, :nitrous_oxide, :methane, :sf6) |>
DataFrame
else # already perfect formatted
emissions_data = load(emissions_path)|> DataFrame
emissions_data = load(emissions_path) |> DataFrame
end

g_emissions_scenario_datasets[emissions_scenario_dict_key]= emissions_data
end
g_emissions_scenario_datasets[emissions_scenario_dict_key] = emissions_data
end
end

function run_timestep(p,v,d,t)
function run_timestep(p, v, d, t)

ssp_dict_key = Symbol(p.SSP_source, "-", p.SSP)
emissions_scenario_dict_key = Symbol(p.emissions_source, "-", p.emissions_scenario)
Expand All @@ -109,27 +109,27 @@ using Mimi, CSVFiles, DataFrames, Query, Interpolations
# t and only the SSP countries found in the model countries list,
# already checked that all model countries are in SSP countries list
subset = g_ssp_datasets[ssp_dict_key] |>
@filter(_.year == gettime(t) && _.country in p.country_names) |>
DataFrame
@filter(_.year == gettime(t) && _.country in p.country_names) |>
DataFrame

# get the ordered indices of the SSP countries within the parameter
# of the model countries, already checked that all model countries
# are in SSP countries list
order = indexin(p.country_names, subset.country)

v.population[t,:] = subset.pop[order]
v.gdp[t,:] = subset.gdp[order]
v.population[t, :] = subset.pop[order]
v.gdp[t, :] = subset.gdp[order]

# add global data for future accessibility and quality control
v.population_global[t] = sum(v.population[t,:])
v.gdp_global[t] = sum(v.gdp[t,:])
v.population_global[t] = sum(v.population[t, :])
v.gdp_global[t] = sum(v.gdp[t, :])

# ----------------------------------------------------------------------
# Emissions

subset = g_emissions_scenario_datasets[emissions_scenario_dict_key] |>
@filter(_.year == gettime(t)) |>
DataFrame
@filter(_.year == gettime(t)) |>
DataFrame

v.co2_emissions[t] = subset.carbon_dioxide[1]
v.ch4_emissions[t] = subset.methane[1]
Expand Down
6 changes: 3 additions & 3 deletions test/test_Coupled.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ m = Model()
set_dimension!(m, :time, 1750:2300)

# Handle the MimiSSPs.SSPs component
add_comp!(m, MimiSSPs.SSPs, first = 2010, last = 2300)
add_comp!(m, MimiSSPs.SSPs, first=2010, last=2300)
set_dimension!(m, :country, inputregions)
update_param!(m, :SSPs, :country_names, inputregions)

Expand All @@ -19,7 +19,7 @@ update_param!(m, :SSPs, :emissions_source, "Leach")
update_param!(m, :SSPs, :emissions_scenario, "SSP119")

# Handle the MimiSSPs.RegionAggregatorSum component
add_comp!(m, MimiSSPs.RegionAggregatorSum, first = 2010, last = 2300)
add_comp!(m, MimiSSPs.RegionAggregatorSum, first=2010, last=2300)

set_dimension!(m, :inputregions, inputregions)
set_dimension!(m, :outputregions, outputregions)
Expand All @@ -33,7 +33,7 @@ connect_param!(m, :RegionAggregatorSum, :input, :SSPs, :population)
run(m)

# Should also work if Aggregator runs long, using backup data
Mimi.set_first_last!(m, :RegionAggregatorSum, first = 1750)
Mimi.set_first_last!(m, :RegionAggregatorSum, first=1750)
backup = zeros(551, 184)
connect_param!(m, :RegionAggregatorSum, :input, :SSPs, :population, backup, ignoreunits=true)

Expand Down
12 changes: 9 additions & 3 deletions test/test_RegionAggregatorSum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ run(m)

expected_output = zeros(length(2000:2010), length(outputregions))
for (i, output_region) in enumerate(outputregions)
idxs = findall(i -> i == output_region, dummy_input_output[:,2])
expected_output[:, i] = sum(data[:, idxs], dims = 2)
idxs = findall(i -> i == output_region, dummy_input_output[:, 2])
expected_output[:, i] = sum(data[:, idxs], dims=2)
end

@test (m[:RegionAggregatorSum, :output]) expected_output atol = 1e-9
Expand All @@ -31,5 +31,11 @@ bad_mapping = dummy_input_output.Output_Region
bad_mapping[5] = "Region-MISSING" # this does not exist in the output regions list
update_param!(m, :RegionAggregatorSum, :input_output_mapping, bad_mapping)

error_msg = (try eval(run(m)) catch err err end).msg
error_msg = (
try
eval(run(m))
catch err
err
end
).msg
@test occursin("All provided region names in the RegionAggregatorSum's input_output_mapping Parameter must exist in the output_region_names Parameter.", error_msg)
Loading

0 comments on commit 85a721d

Please sign in to comment.