Skip to content

Commit b0469e9

Browse files
authored
Format files using DocumentFormat
1 parent 9cfcad1 commit b0469e9

File tree

6 files changed

+127
-116
lines changed

6 files changed

+127
-116
lines changed

src/MimiRFFSPs.jl

+4-5
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ function get_model()
3131

3232
set_dimension!(m, :time, 1750:2300)
3333

34-
add_comp!(m, SPs, :rffsp, first = 2020, last = 2300)
34+
add_comp!(m, SPs, :rffsp, first=2020, last=2300)
3535

36-
all_countries = CSVFiles.load(joinpath(@__DIR__, "..", "data", "keys", "MimiRFFSPs_ISO3.csv")) |> DataFrame
36+
all_countries = CSVFiles.load(joinpath(@__DIR__, "..", "data", "keys", "MimiRFFSPs_ISO3.csv")) |> DataFrame
3737

3838
set_dimension!(m, :country, all_countries.ISO3)
3939

@@ -42,10 +42,9 @@ function get_model()
4242
return m
4343
end
4444

45-
function get_mcs(sampling_ids::Union{Vector{Int}, Nothing} = nothing)
45+
function get_mcs(sampling_ids::Union{Vector{Int},Nothing}=nothing)
4646
# define the Monte Carlo Simulation and add some simple random variables
47-
mcs = @defsim begin
48-
end
47+
mcs = @defsim begin end
4948

5049
distrib = isnothing(sampling_ids) ? EmpiricalDistribution(collect(1:10_000)) : SampleStore(sampling_ids)
5150
Mimi.add_RV!(mcs, :socio_id_rv, distrib)

src/components/RegionAggregatorSum.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@defcomp RegionAggregatorSum begin
2-
2+
33
inputregions = Index()
44
outputregions = Index()
55

@@ -12,17 +12,17 @@
1212
input = Parameter(index=[time, inputregions])
1313
output = Variable(index=[time, outputregions])
1414

15-
function init(p,v,d)
15+
function init(p, v, d)
1616
idxs = indexin(p.input_output_mapping, p.output_region_names)
1717
!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
1818
v.input_output_mapping_int[:] = idxs
1919
end
2020

21-
function run_timestep(p,v,d,t)
21+
function run_timestep(p, v, d, t)
2222
v.output[t, :] .= 0.
2323

2424
for i in d.inputregions
25-
v.output[t, v.input_output_mapping_int[i]] += p.input[t,i]
25+
v.output[t, v.input_output_mapping_int[i]] += p.input[t, i]
2626
end
2727
end
2828
end

src/components/SPs.jl

+35-35
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# (10/25/2021) BEA Table 1.1.9, line 1 GDP annual values as linked here: https://apps.bea.gov/iTable/iTable.cfm?reqid=19&step=3&isuri=1&select_all_years=0&nipa_table_list=13&series=a&first_year=2005&last_year=2020&scale=-99&categories=survey&thetable=
2-
const pricelevel_2011_to_2005 = 87.504/98.164
2+
const pricelevel_2011_to_2005 = 87.504 / 98.164
33

44
function fill_socioeconomics!(source_Year, source_Country, source_Pop, source_GDP, population, gdp, country_lookup, start_year, end_year)
55
for i in 1:length(source_Year)
@@ -60,50 +60,50 @@ end
6060
end_year = Parameter{Int}(default=Int(2300)) # year (annual) data should end
6161
country_names = Parameter{String}(index=[country]) # need the names of the countries from the dimension
6262
id = Parameter{Int64}(default=Int(6546)) # the sample (out of 10,000) to be used
63-
64-
population = Variable(index=[time, country], unit="million")
65-
population_global = Variable(index=[time], unit="million")
66-
deathrate = Variable(index=[time, country], unit="deaths/1000 persons/yr")
67-
gdp = Variable(index=[time, country], unit="billion US\$2005/yr")
68-
gdp_global = Variable(index=[time], unit="billion US\$2005/yr")
69-
70-
population1990 = Variable(index=[country], unit = "million")
71-
gdp1990 = Variable(index=[country], unit = unit="billion US\$2005/yr")
72-
73-
co2_emissions = Variable(index=[time], unit="GtC/yr")
74-
ch4_emissions = Variable(index=[time], unit="MtCH4/yr")
75-
n2o_emissions = Variable(index=[time], unit="MtN2/yr")
76-
77-
function init(p,v,d)
63+
64+
population = Variable(index=[time, country], unit="million")
65+
population_global = Variable(index=[time], unit="million")
66+
deathrate = Variable(index=[time, country], unit="deaths/1000 persons/yr")
67+
gdp = Variable(index=[time, country], unit="billion US\$2005/yr")
68+
gdp_global = Variable(index=[time], unit="billion US\$2005/yr")
69+
70+
population1990 = Variable(index=[country], unit="million")
71+
gdp1990 = Variable(index=[country], unit=unit = "billion US\$2005/yr")
72+
73+
co2_emissions = Variable(index=[time], unit="GtC/yr")
74+
ch4_emissions = Variable(index=[time], unit="MtCH4/yr")
75+
n2o_emissions = Variable(index=[time], unit="MtN2/yr")
76+
77+
function init(p, v, d)
7878

7979
# add countrys to a dictionary where each country key has a value holding it's
8080
# index in country_names
81-
country_lookup = Dict{String,Int}(name=>i for (i,name) in enumerate(p.country_names))
81+
country_lookup = Dict{String,Int}(name => i for (i, name) in enumerate(p.country_names))
8282
country_indices = d.country::Vector{Int} # helper for type stable country indices
8383

8484
# ----------------------------------------------------------------------
8585
# Socioeconomic Data
8686
# population in millions of individuals
8787
# GDP in billions of $2005 USD
88-
88+
8989
# Load Feather File
9090
t = Arrow.Table(joinpath(datadep"rffsps_v5", "pop_income", "rffsp_pop_income_run_$(p.id).feather"))
9191
fill_socioeconomics!(t.Year, t.Country, t.Pop, t.GDP, v.population, v.gdp, country_lookup, p.start_year, p.end_year)
9292

9393
for year in p.start_year:5:p.end_year-5, country in country_indices
9494
year_as_timestep = TimestepIndex(year - p.start_year + 1)
95-
pop_interpolator = LinearInterpolation(Float64[year, year+5], [log(v.population[year_as_timestep,country]), log(v.population[year_as_timestep+5,country])])
96-
gdp_interpolator = LinearInterpolation(Float64[year, year+5], [log(v.gdp[year_as_timestep,country]), log(v.gdp[year_as_timestep+5,country])])
95+
pop_interpolator = LinearInterpolation(Float64[year, year+5], [log(v.population[year_as_timestep, country]), log(v.population[year_as_timestep+5, country])])
96+
gdp_interpolator = LinearInterpolation(Float64[year, year+5], [log(v.gdp[year_as_timestep, country]), log(v.gdp[year_as_timestep+5, country])])
9797
for year2 in year+1:year+4
9898
year2_as_timestep = TimestepIndex(year2 - p.start_year + 1)
99-
v.population[year2_as_timestep,country] = exp(pop_interpolator[year2])
100-
v.gdp[year2_as_timestep,country] = exp(gdp_interpolator[year2])
99+
v.population[year2_as_timestep, country] = exp(pop_interpolator[year2])
100+
v.gdp[year2_as_timestep, country] = exp(gdp_interpolator[year2])
101101
end
102102
end
103-
103+
104104
# add global data for future accessibility and quality control
105-
v.gdp_global[:,:] = sum(v.gdp[:,:], dims = 2) # sum across countries, which are the second dimension
106-
v.population_global[:,:] = sum(v.population[:,:], dims = 2) # sum across countries, which are the second dimension
105+
v.gdp_global[:, :] = sum(v.gdp[:, :], dims=2) # sum across countries, which are the second dimension
106+
v.population_global[:, :] = sum(v.population[:, :], dims=2) # sum across countries, which are the second dimension
107107

108108
# ----------------------------------------------------------------------
109109
# Death Rate Data
@@ -115,7 +115,7 @@ end
115115
g_datasets[:pop_trajectory_key] = (load(joinpath(datadep"rffsps_v5", "sample_numbers", "sampled_pop_trajectory_numbers.csv")) |> DataFrame).x
116116
end
117117
deathrate_trajectory_id = convert(Int64, g_datasets[:pop_trajectory_key][p.id])
118-
118+
119119
# Load Feather File
120120
t = Arrow.Table(joinpath(datadep"rffsps_v5", "death_rates", "rffsp_death_rates_run_$(deathrate_trajectory_id).feather"))
121121
fill_deathrates!(t.Year, t.ISO3, t.DeathRate, v.deathrate, country_lookup, p.start_year, p.end_year)
@@ -126,7 +126,7 @@ end
126126
# carbon dioxide emissions in GtC
127127
# nitrous oxide emissions in MtN2
128128
# methane emissions in MtCH4
129-
129+
130130
# add data to the global dataset if it's not there
131131
if !haskey(g_datasets, :ch4)
132132
g_datasets[:ch4] = load(joinpath(datadep"rffsps_v5", "emissions", "rffsp_ch4_emissions.csv")) |> DataFrame
@@ -147,13 +147,13 @@ end
147147
# Population and GDP 1990 Values
148148

149149
if !haskey(g_datasets, :ypc1990)
150-
g_datasets[:ypc1990] = load(joinpath(datadep"rffsps_v5", "ypc1990", "rffsp_ypc1990.csv")) |>
151-
DataFrame |>
152-
i -> insertcols!(i, :sample => 1:10_000) |>
153-
i -> stack(i, Not(:sample)) |>
154-
DataFrame |>
155-
i -> rename!(i, [:sample, :country, :value]) |>
156-
DataFrame
150+
g_datasets[:ypc1990] = load(joinpath(datadep"rffsps_v5", "ypc1990", "rffsp_ypc1990.csv")) |>
151+
DataFrame |>
152+
i -> insertcols!(i, :sample => 1:10_000) |>
153+
i -> stack(i, Not(:sample)) |>
154+
DataFrame |>
155+
i -> rename!(i, [:sample, :country, :value]) |>
156+
DataFrame
157157
end
158158
if !haskey(g_datasets, :pop1990)
159159
g_datasets[:pop1990] = load(joinpath(@__DIR__, "..", "..", "data/population1990.csv")) |> DataFrame
@@ -164,7 +164,7 @@ end
164164

165165
end
166166

167-
function run_timestep(p,v,d,t)
167+
function run_timestep(p, v, d, t)
168168

169169
if !(gettime(t) in p.start_year:p.end_year)
170170
error("Cannot run SP component in year $(gettime(t)), SP data is not available for this model and year.")

test/test_Coupled.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ m = Model()
99
set_dimension!(m, :time, 2000:2300)
1010

1111
# Handle the MimiRFFSPs.SPs component
12-
add_comp!(m, MimiRFFSPs.SPs, first = 2020)
12+
add_comp!(m, MimiRFFSPs.SPs, first=2020)
1313
set_dimension!(m, :country, inputregions)
1414
update_param!(m, :SPs, :country_names, inputregions)
1515

1616
# Handle the MimiRFFSPs.RegionAggregatorSum component
17-
add_comp!(m, MimiRFFSPs.RegionAggregatorSum, first = 2020)
17+
add_comp!(m, MimiRFFSPs.RegionAggregatorSum, first=2020)
1818

1919
set_dimension!(m, :inputregions, inputregions)
2020
set_dimension!(m, :outputregions, outputregions)
@@ -28,7 +28,7 @@ connect_param!(m, :RegionAggregatorSum, :input, :SPs, :population)
2828
run(m)
2929

3030
# Should also work if Aggregator runs long, using backup data
31-
Mimi.set_first_last!(m, :RegionAggregatorSum, first = 2000)
31+
Mimi.set_first_last!(m, :RegionAggregatorSum, first=2000)
3232
backup = zeros(301, 184)
3333
connect_param!(m, :RegionAggregatorSum, :input, :SPs, :population, backup, ignoreunits=true)
3434

test/test_RegionAggregatorSum.jl

+9-3
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ run(m)
2121

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

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

34-
error_msg = (try eval(run(m)) catch err err end).msg
34+
error_msg = (
35+
try
36+
eval(run(m))
37+
catch err
38+
err
39+
end
40+
).msg
3541
@test occursin("All provided region names in the RegionAggregatorSum's input_output_mapping Parameter must exist in the output_region_names Parameter.", error_msg)

0 commit comments

Comments
 (0)