diff --git a/src/MimiFUND.jl b/src/MimiFUND.jl index 759b5d75..0b796023 100644 --- a/src/MimiFUND.jl +++ b/src/MimiFUND.jl @@ -7,8 +7,6 @@ include("helper.jl") include("new_marginaldamages.jl") include("montecarlo/defmcs.jl") -include("montecarlo/run_fund_mcs.jl") -include("montecarlo/run_fund_scc_mcs.jl") include("components/SocioEconomicComponent.jl") include("components/PopulationComponent.jl") diff --git a/src/montecarlo/run_fund_mcs.jl b/src/montecarlo/run_fund_mcs.jl deleted file mode 100644 index 01ee2553..00000000 --- a/src/montecarlo/run_fund_mcs.jl +++ /dev/null @@ -1,28 +0,0 @@ -using Dates - -""" -Runs a Monte Carlo simulation with the FUND model over it's distributional parameters. -`trials`: the number of trials to run. -`ntimesteps`: how many timesteps to run. -`output_dir`: an output directory; if none provided, will create and use "output/yyyy-mm-dd HH-MM-SS MCtrials". -`save_trials`: whether or not to generate and save the MC trial values up front to a file. -""" -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 - mkpath("$output_dir/results") - - # Get an instance of FUND's mcs - mcs = getmcs() - - # Generate trials - filename = save_trials ? joinpath("$output_dir/trials.csv") : "" - generate_trials!(mcs, trials; filename=filename) - - # Run monte carlo trials - set_models!(mcs, getfund()) - run_sim(mcs; trials = trials, ntimesteps = ntimesteps, 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 deleted file mode 100644 index a3b49bcf..00000000 --- a/src/montecarlo/run_fund_scc_mcs.jl +++ /dev/null @@ -1,92 +0,0 @@ -using Dates - -""" -Runs a Monte Carlo simulation with the FUND model over it's distirbutional parameters, and calculates -the Social Cost of Carbon for the specified `years` and discount `rates`. -`trials`: the number of trials to run. -`ntimesteps`: how many timesteps to run. -`output_dir`: an output directory; if none provided, will create and use "output/SCC yyyy-mm-dd HH-MM-SS MCtrials". -`save_trials`: whether or not to generate and save the MC trial values up front to a file. -""" -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 - mkpath("$output_dir/results") - - # Write header in SCC output file - scc_file = joinpath(output_dir, "scc.csv") - open(scc_file, "w") do f - write(f, "trial, rate, year, scc\n") - end - - # Scenario set up - scenario_args = Any[ - :rate => rates - :emissionyear => years - ] - - mcs = getmcs() - - # Generate trials - filename = save_trials ? joinpath(@__DIR__, "$output_dir/trials.csv") : "" - generate_trials!(mcs, trials; filename=filename) - - # Get FUND marginal model - mm = create_marginal_FUND_model() - set_models!(mcs, mm) - - # Define scenario function - function _scenario_func(mcs::Simulation, tup::Tuple) - - # Unpack scenario tuple argument - (rate, emissionyear) = tup - - # Get models - base, marginal = mcs.models - - # Perturb emissons in the marginal model - perturb_marginal_emissions!(marginal, emissionyear) - - end - - # Define post trial function - function scc_calculation(mcs::Simulation, trialnum::Int, ntimesteps::Int, tup::Tuple) - - # Unpack scenario tuple argument - (rate, emissionyear) = tup - - # Get marginal damages - base, marginal = mcs.models - marginaldamages = (marginal[:impactaggregation, :loss] - base[:impactaggregation, :loss]) / 10000000.0 - - # Calculate discount factor - T = ntimesteps == typemax(Int) ? length(Mimi.dimension(base, :time)) : ntimesteps - discount_factor = zeros(T) - idx = getindexfromyear(emissionyear) - discount_factor[idx:T] = [1 / ((1 + rate) ^ t) for t in 0:T-idx] - - # Sum discounted global damages to scc - scc = sum(sum(marginaldamages, dims = 2)[2:T] .* discount_factor[2:end]) - - # Write output - open(scc_file, "a") do f - write(f, "$trialnum, $rate, $emissionyear, $scc\n") - end - - return nothing - end - - # Run monte carlo trials - run_sim(mcs; - models_to_run = 2, - ntimesteps = ntimesteps, - output_dir = "$output_dir/results", - scenario_args = scenario_args, - scenario_func = _scenario_func, - post_trial_func = scc_calculation - ) - - return nothing -end - diff --git a/test/runtests.jl b/test/runtests.jl index d0cb1e65..32ff9344 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -47,18 +47,6 @@ md = MimiFUND.getmarginaldamages() end #marginaldamages testset -#------------------------------------------------------------------------------ -# 4. Run basic test of MCS functionality -#------------------------------------------------------------------------------ - -@testset "test-mcs" begin - -# mcs -MimiFUND.run_fund_mcs(10) # Run 10 trials of basic FUND MCS -MimiFUND.run_fund_scc_mcs(10) # Run 10 trials of FUND MCS SCC calculations - -end #test-mcs testset - end #fund testset nothing