From f72c36236523540c08e67ba9abe437f70dbf2a8c Mon Sep 17 00:00:00 2001 From: lrennels Date: Mon, 10 Jun 2019 11:43:58 -0700 Subject: [PATCH 1/5] Update to use new Simulation API --- src/montecarlo/run_fund_mcs.jl | 15 ++++----------- src/montecarlo/run_fund_scc_mcs.jl | 26 +++++++++----------------- 2 files changed, 13 insertions(+), 28 deletions(-) diff --git a/src/montecarlo/run_fund_mcs.jl b/src/montecarlo/run_fund_mcs.jl index f891b640..97eaf8b0 100644 --- a/src/montecarlo/run_fund_mcs.jl +++ b/src/montecarlo/run_fund_mcs.jl @@ -13,20 +13,13 @@ function run_fund_mcs(trials = 10000; ntimesteps = MimiFUND.default_nsteps + 1, output_dir = output_dir == nothing ? joinpath(@__DIR__, "../../output/", "SCC $(Dates.format(now(), "yyyy-mm-dd HH-MM-SS")) MC$trials") : output_dir mkpath("$output_dir/results") + save_trials ? trials_output_filename = joinpath("$output_dir/trials.csv") : nothing + # Get an instance of FUND's mcs mcs = getmcs() - # Generate trials - if save_trials - filename = joinpath("$output_dir/trials.csv") - generate_trials!(mcs, trials; filename=filename) - else - generate_trials!(mcs, trials) - end - - # Run monte carlo trials - set_models!(mcs, get_model()) - run_sim(mcs; trials = trials, ntimesteps = ntimesteps, output_dir = "$output_dir/results") + # run monte carlo trials + run(mcs, get_model(), trials; trials_output_filename = trials_output_filename, ntimesteps = ntimesteps, results_output_dir = "$output_dir/results") return nothing end diff --git a/src/montecarlo/run_fund_scc_mcs.jl b/src/montecarlo/run_fund_scc_mcs.jl index 24b7664b..1aa9efcf 100644 --- a/src/montecarlo/run_fund_scc_mcs.jl +++ b/src/montecarlo/run_fund_scc_mcs.jl @@ -13,6 +13,7 @@ function run_fund_scc_mcs(trials = 10000; years = [2020], rates = [0.03], ntimes # Set up output directories output_dir = output_dir == nothing ? joinpath(@__DIR__, "../../output/", "SCC $(Dates.format(now(), "yyyy-mm-dd HH-MM-SS")) MC$trials") : output_dir mkpath("$output_dir/results") + save_trials ? trials_output_filename = joinpath(@__DIR__, "$output_dir/trials.csv") : trials_output_filename = nothing # Write header in SCC output file scc_file = joinpath(output_dir, "scc.csv") @@ -26,22 +27,13 @@ function run_fund_scc_mcs(trials = 10000; years = [2020], rates = [0.03], ntimes :emissionyear => years ] + # get models mcs = getmcs() - - # Generate trials - if save_trials - filename = joinpath(@__DIR__, "$output_dir/trials.csv") - generate_trials!(mcs, trials; filename=filename) - else - generate_trials!(mcs, trials) - end - - # Get FUND marginal model mm = create_marginal_FUND_model() - set_models!(mcs, mm) - + models = [mcs, mm] + # Define scenario function - function _scenario_func(mcs::Simulation, tup::Tuple) + function _scenario_func(mcs::SimulationInstance, tup::Tuple) # Unpack scenario tuple argument (rate, emissionyear) = tup @@ -55,7 +47,7 @@ function run_fund_scc_mcs(trials = 10000; years = [2020], rates = [0.03], ntimes end # Define post trial function - function scc_calculation(mcs::Simulation, trialnum::Int, ntimesteps::Int, tup::Tuple) + function scc_calculation(mcs::SimulationInstance, trialnum::Int, ntimesteps::Int, tup::Tuple) # Unpack scenario tuple argument (rate, emissionyear) = tup @@ -82,10 +74,10 @@ function run_fund_scc_mcs(trials = 10000; years = [2020], rates = [0.03], ntimes end # Run monte carlo trials - run_sim(mcs; - models_to_run = 2, + run(mcs, models, trials; ntimesteps = ntimesteps, - output_dir = "$output_dir/results", + trials_output_filename = trials_output_filename, + results_output_dir = "$output_dir/results", scenario_args = scenario_args, scenario_func = _scenario_func, post_trial_func = scc_calculation From ace7e41efa730c761585c84e754f128c6845f760 Mon Sep 17 00:00:00 2001 From: lrennels Date: Mon, 10 Jun 2019 12:35:36 -0700 Subject: [PATCH 2/5] Save results of simulation to a variable --- src/montecarlo/run_fund_mcs.jl | 4 ++-- src/montecarlo/run_fund_scc_mcs.jl | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/montecarlo/run_fund_mcs.jl b/src/montecarlo/run_fund_mcs.jl index 97eaf8b0..dffcaa96 100644 --- a/src/montecarlo/run_fund_mcs.jl +++ b/src/montecarlo/run_fund_mcs.jl @@ -19,7 +19,7 @@ function run_fund_mcs(trials = 10000; ntimesteps = MimiFUND.default_nsteps + 1, mcs = getmcs() # run monte carlo trials - run(mcs, get_model(), trials; trials_output_filename = trials_output_filename, ntimesteps = ntimesteps, results_output_dir = "$output_dir/results") + res = run(mcs, get_model(), trials; trials_output_filename = trials_output_filename, ntimesteps = ntimesteps, results_output_dir = "$output_dir/results") - return nothing + return res end diff --git a/src/montecarlo/run_fund_scc_mcs.jl b/src/montecarlo/run_fund_scc_mcs.jl index 1aa9efcf..3e444dfb 100644 --- a/src/montecarlo/run_fund_scc_mcs.jl +++ b/src/montecarlo/run_fund_scc_mcs.jl @@ -74,7 +74,7 @@ function run_fund_scc_mcs(trials = 10000; years = [2020], rates = [0.03], ntimes end # Run monte carlo trials - run(mcs, models, trials; + res = run(mcs, models, trials; ntimesteps = ntimesteps, trials_output_filename = trials_output_filename, results_output_dir = "$output_dir/results", @@ -83,6 +83,6 @@ function run_fund_scc_mcs(trials = 10000; years = [2020], rates = [0.03], ntimes post_trial_func = scc_calculation ) - return nothing + return res end From c84b930dfeadf4c09654cfbd22e04ee86d513864 Mon Sep 17 00:00:00 2001 From: Lisa Rennels <31779240+lrennels@users.noreply.github.com> Date: Sun, 1 Sep 2019 11:24:05 -0700 Subject: [PATCH 3/5] Update Project.toml --- Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Project.toml b/Project.toml index 18705ea1..9f589626 100644 --- a/Project.toml +++ b/Project.toml @@ -13,6 +13,7 @@ Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" Mimi = "0.9" Distributions = "0.20" StatsBase = "0.30" +CSVFiles = "0.16.0" [extras] CSVFiles = "5d742f6a-9f54-50ce-8119-2520741973ca" From e003caceba1e1b1e8b82d06808492c9d6c8d9837 Mon Sep 17 00:00:00 2001 From: Lisa Rennels <31779240+lrennels@users.noreply.github.com> Date: Sun, 1 Sep 2019 11:41:24 -0700 Subject: [PATCH 4/5] Update Project.toml --- Project.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/Project.toml b/Project.toml index 9f589626..18705ea1 100644 --- a/Project.toml +++ b/Project.toml @@ -13,7 +13,6 @@ Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" Mimi = "0.9" Distributions = "0.20" StatsBase = "0.30" -CSVFiles = "0.16.0" [extras] CSVFiles = "5d742f6a-9f54-50ce-8119-2520741973ca" From 422ca4ac487e243ecf94e82c3676ed75e1c1c1ed Mon Sep 17 00:00:00 2001 From: lrennels Date: Tue, 3 Sep 2019 01:35:45 -0700 Subject: [PATCH 5/5] Fix bug in scc test --- src/montecarlo/run_fund_mcs.jl | 4 ++-- src/montecarlo/run_fund_scc_mcs.jl | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/montecarlo/run_fund_mcs.jl b/src/montecarlo/run_fund_mcs.jl index dffcaa96..0d89ad90 100644 --- a/src/montecarlo/run_fund_mcs.jl +++ b/src/montecarlo/run_fund_mcs.jl @@ -10,10 +10,10 @@ Runs a Monte Carlo simulation with the FUND model over it's distributional param function run_fund_mcs(trials = 10000; ntimesteps = MimiFUND.default_nsteps + 1, output_dir = nothing, save_trials = false) # Set up output directories - output_dir = output_dir == nothing ? joinpath(@__DIR__, "../../output/", "SCC $(Dates.format(now(), "yyyy-mm-dd HH-MM-SS")) MC$trials") : output_dir + output_dir = output_dir === nothing ? joinpath(@__DIR__, "../../output/", "SCC $(Dates.format(now(), "yyyy-mm-dd HH-MM-SS")) MC$trials") : output_dir mkpath("$output_dir/results") - save_trials ? trials_output_filename = joinpath("$output_dir/trials.csv") : nothing + trials_output_filename = save_trials ? joinpath("$output_dir/trials.csv") : nothing # Get an instance of FUND's mcs mcs = getmcs() diff --git a/src/montecarlo/run_fund_scc_mcs.jl b/src/montecarlo/run_fund_scc_mcs.jl index 3e444dfb..462b0aa0 100644 --- a/src/montecarlo/run_fund_scc_mcs.jl +++ b/src/montecarlo/run_fund_scc_mcs.jl @@ -11,7 +11,7 @@ the Social Cost of Carbon for the specified `years` and discount `rates`. function run_fund_scc_mcs(trials = 10000; years = [2020], rates = [0.03], ntimesteps = MimiFUND.default_nsteps + 1, output_dir = nothing, save_trials = false) # Set up output directories - output_dir = output_dir == nothing ? joinpath(@__DIR__, "../../output/", "SCC $(Dates.format(now(), "yyyy-mm-dd HH-MM-SS")) MC$trials") : output_dir + output_dir = output_dir === nothing ? joinpath(@__DIR__, "../../output/", "SCC $(Dates.format(now(), "yyyy-mm-dd HH-MM-SS")) MC$trials") : output_dir mkpath("$output_dir/results") save_trials ? trials_output_filename = joinpath(@__DIR__, "$output_dir/trials.csv") : trials_output_filename = nothing @@ -27,10 +27,10 @@ function run_fund_scc_mcs(trials = 10000; years = [2020], rates = [0.03], ntimes :emissionyear => years ] - # get models + # get models and sim mcs = getmcs() mm = create_marginal_FUND_model() - models = [mcs, mm] + models = [mm.base, mm.marginal] #fixing bug in Mimi so this can go back to models = mm # Define scenario function function _scenario_func(mcs::SimulationInstance, tup::Tuple)