Ejemplo n.º 1
0
        public void Run()
        {
            // Load parameters
            var parameters = new Parameters();

            parameters.ReadDirectory(@"Data\Base");

            // Get best guess parameter values
            var parameterValues = parameters.GetBestGuess();

            // Create a new model that inits itself from the parameters just loaded
            var model = FundModel.GetModel();

            // Run the model
            var rs = model.Run(parameterValues);

            // Display all variables in interactive window
            OutputHelper.ShowModel(rs);
        }
Ejemplo n.º 2
0
        public static void Run()
        {
            int monteCarloRuns = 10000;

            // Load parameters
            var parameters = new Parameters();

            parameters.ReadDirectory(@"Data\Base");

            // Do one best guess run
            {
                var model = FundModel.GetModel();
                //var model = new FundWorkflow(parameters.GetBestGuess());
                model.Run(parameters.GetBestGuess());
            }

            int currentRun = 0;

            var stopwatch = new Stopwatch();

            stopwatch.Start();

            ParallelMonteCarlo.DoParallelRun(
                parameters,
                monteCarloRuns,
                p =>
            {
                var m = FundModel.GetModel();
                m.Run(p);

                int tempCurrentCount = Interlocked.Increment(ref currentRun);
                Console.Write("\rRun {0}                ", tempCurrentCount);

                return(0.0);
            },
                d => 0.0);

            stopwatch.Stop();

            Console.WriteLine();
            Console.WriteLine(stopwatch.Elapsed);
        }
Ejemplo n.º 3
0
        private static Tuple <double, double> GetSCGasMonteCarlo(MarginalGas gas, double prtp, bool equityWeights, int monteCarloRuns)
        {
            var parameters = new Parameters();

            parameters.ReadDirectory(@"Data\Base");

            var fm = FundModel.GetModel();

            fm.Run(parameters.GetBestGuess());

            var rand = new jp.takel.PseudoRandom.MersenneTwister();

            var sccs = new System.Collections.Concurrent.ConcurrentBag <double>();

            Parallel.ForEach(parameters.GetRandom(rand, monteCarloRuns), pv =>
            {
                var m = new MarginalDamage3()
                {
                    EmissionYear     = Timestep.FromYear(2010),
                    Eta              = 1.0,
                    Gas              = gas,
                    Parameters       = pv,
                    Prtp             = prtp,
                    UseEquityWeights = equityWeights,
                    YearsToAggregate = 290
                };

                double scc = m.Start();

                sccs.Add(scc);
            });

            var stats = new DescriptiveStatistics(sccs);

            return(Tuple.Create(stats.Mean, Math.Sqrt(stats.Variance) / Math.Sqrt(stats.Count)));
        }
Ejemplo n.º 4
0
        public double[] DoOneRun(int RunId, WeightingCombination[] i_weightingCombinations, ParameterValues parameters)
        {
            ModelOutput i_output2;
            Damages     i_marginalDamages;
            double      i_aggregatedDamage;
            ModelOutput i_output1;

            // Create Output object for run 1, set addmp to 0 so that
            // the extra greenhouse gases are not emitted and then run
            // the model
            i_output1 = new ModelOutput();

            var f1 = FundModel.GetModel();

            f1["ImpactWaterResources"].Variables["water"].StoreOutput   = true;
            f1["ImpactForests"].Variables["forests"].StoreOutput        = true;
            f1["ImpactHeating"].Variables["heating"].StoreOutput        = true;
            f1["ImpactCooling"].Variables["cooling"].StoreOutput        = true;
            f1["ImpactAgriculture"].Variables["agcost"].StoreOutput     = true;
            f1["ImpactSeaLevelRise"].Variables["drycost"].StoreOutput   = true;
            f1["ImpactSeaLevelRise"].Variables["protcost"].StoreOutput  = true;
            f1["ImpactSeaLevelRise"].Variables["entercost"].StoreOutput = true;
            f1["ImpactTropicalStorms"].Variables["hurrdam"].StoreOutput = true;
            f1["ImpactExtratropicalStorms"].Variables["extratropicalstormsdam"].StoreOutput = true;
            f1["ImpactBioDiversity"].Variables["species"].StoreOutput    = true;
            f1["ImpactDeathMorbidity"].Variables["deadcost"].StoreOutput = true;
            f1["ImpactDeathMorbidity"].Variables["morbcost"].StoreOutput = true;
            f1["ImpactSeaLevelRise"].Variables["wetcost"].StoreOutput    = true;
            f1["ImpactSeaLevelRise"].Variables["leavecost"].StoreOutput  = true;
            f1["SocioEconomic"].Variables["income"].StoreOutput          = true;
            f1["Population"].Variables["population"].StoreOutput         = true;

            if (AdditionalInitCode != null)
            {
                AdditionalInitCode(f1);
            }

            var result1 = f1.Run(parameters);

            i_output1.Load(result1);

            // Create Output object for run 2, set addmp to 1 so that
            // the extra greenhouse gases for the marginal run are
            // emitted and then run the model
            i_output2 = new ModelOutput();

            var f2 = FundModel.GetModel();

            f2["ImpactWaterResources"].Variables["water"].StoreOutput   = true;
            f2["ImpactForests"].Variables["forests"].StoreOutput        = true;
            f2["ImpactHeating"].Variables["heating"].StoreOutput        = true;
            f2["ImpactCooling"].Variables["cooling"].StoreOutput        = true;
            f2["ImpactAgriculture"].Variables["agcost"].StoreOutput     = true;
            f2["ImpactSeaLevelRise"].Variables["drycost"].StoreOutput   = true;
            f2["ImpactSeaLevelRise"].Variables["protcost"].StoreOutput  = true;
            f2["ImpactSeaLevelRise"].Variables["entercost"].StoreOutput = true;
            f2["ImpactTropicalStorms"].Variables["hurrdam"].StoreOutput = true;
            f2["ImpactExtratropicalStorms"].Variables["extratropicalstormsdam"].StoreOutput = true;
            f2["ImpactBioDiversity"].Variables["species"].StoreOutput    = true;
            f2["ImpactDeathMorbidity"].Variables["deadcost"].StoreOutput = true;
            f2["ImpactDeathMorbidity"].Variables["morbcost"].StoreOutput = true;
            f2["ImpactSeaLevelRise"].Variables["wetcost"].StoreOutput    = true;
            f2["ImpactSeaLevelRise"].Variables["leavecost"].StoreOutput  = true;
            f2["SocioEconomic"].Variables["income"].StoreOutput          = true;
            f2["Population"].Variables["population"].StoreOutput         = true;

            if (AdditionalInitCode != null)
            {
                AdditionalInitCode(f2);
            }

            f2.AddComponent("marginalemission", typeof(Fund.Components.MarginalEmissionComponent), "emissions");
            f2["marginalemission"].Parameters["emissionperiod"].SetValue(_emissionyear);
            switch (_gas)
            {
            case MarginalGas.C:
                f2["marginalemission"].Parameters["emission"].Bind("emissions", "mco2");
                f2["climateco2cycle"].Parameters["mco2"].Bind("marginalemission", "modemission");
                break;

            case MarginalGas.CH4:
                f2["marginalemission"].Parameters["emission"].Bind("emissions", "globch4");
                f2["climatech4cycle"].Parameters["globch4"].Bind("marginalemission", "modemission");
                break;

            case MarginalGas.N2O:
                f2["marginalemission"].Parameters["emission"].Bind("emissions", "globn2o");
                f2["climaten2ocycle"].Parameters["globn2o"].Bind("marginalemission", "modemission");
                break;

            case MarginalGas.SF6:
                f2["marginalemission"].Parameters["emission"].Bind("emissions", "globsf6");
                f2["climatesf6cycle"].Parameters["globsf6"].Bind("marginalemission", "modemission");
                break;

            default:
                throw new NotImplementedException();
            }

            var result2 = f2.Run(parameters);

            i_output2.Load(result2);

            Fund28LegacyWeightingCombinations.GetWeightingCombinationsFromName(this.WeightingCombination, out i_weightingCombinations, _emissionyear);

            // Take out growth effect effect of run 2 by transforming
            // the damage from run 2 into % of GDP of run 2, and then
            // multiplying that with GDP of run 1
            for (int year = 1; year < LegacyConstants.NYear; year++)
            {
                for (int region = 0; region < LegacyConstants.NoReg; region++)
                {
                    for (int sector = 0; sector < LegacyConstants.NoSector; sector++)
                    {
                        i_output2.Damages[year, region, (Sector)sector] = (i_output2.Damages[year, region, (Sector)sector] / i_output2.Incomes[year, region]) * i_output1.Incomes[year, region];
                    }
                }
            }

            // Calculate the marginal damage between run 1 and 2 for each
            // year/region/sector
            i_marginalDamages = Damages.CalculateMarginalDamage(i_output1.Damages, i_output2.Damages);

            double[] i_weightedAggregatedDamages = new double[i_weightingCombinations.Length];

            for (int i = 0; i < i_weightingCombinations.Length; i++)
            {
                i_weightingCombinations[i].CalculateWeights(i_output1);
                i_aggregatedDamage = i_weightingCombinations[i].AddDamagesUp(i_marginalDamages, YearsToAggregate, _emissionyear);

                i_weightedAggregatedDamages[i] = i_aggregatedDamage;

                WriteAggregateDamage(RunId, i, i_aggregatedDamage, i_weightingCombinations);

                // Console.Write(i_weightingCombinations[i].Name + ": ");
                // Console.WriteLine(Convert.ToString(i_aggregatedDamage));
            }

            if (m_YearRegionSectorWeightingSchemeCsv != null)
            {
                foreach (var i_Damage in i_marginalDamages)
                {
                    if ((i_Damage.Year >= _emissionyear.Value) && (i_Damage.Year < _emissionyear.Value + this.YearsToAggregate))
                    {
                        for (int k = 0; k < i_weightingCombinations.Length; k++)
                        {
                            WriteMarginalDamage(RunId, i_Damage, k, i_weightingCombinations[k][i_Damage.Year, i_Damage.Region], i_weightingCombinations);
                        }
                    }
                }
            }

            return(i_weightedAggregatedDamages);
        }
Ejemplo n.º 5
0
        public static void Run()
        {
            // Load parameters
            var parameters = new Parameters();

            parameters.ReadDirectory(@"Data\Base");

            // Get best guess parameter values
            var parameterValues = parameters.GetBestGuess();

            // Create a new model that inits itself from the parameters just loaded
            var model1 = FundModel.GetModel();

            // Run the model
            var rs1 = model1.Run(parameterValues);

            var d1 = from p in rs1.GetDimensionalFieldsOperator()
                     where p.Values is IVariableWriter
                     orderby p.ComponentName, p.FieldName
                select new { ComponentName = p.ComponentName, FieldName = p.FieldName };

            // This will hold results from a BAU run
            Directory.CreateDirectory("JuliaComp1");

            foreach (var i in d1)
            {
                using (var file = new StreamWriter(@"JuliaComp1\" + i.ComponentName + "." + i.FieldName + ".csv"))
                {
                    (rs1[i.ComponentName, i.FieldName] as IVariableWriter).WriteData(file);
                }
            }

            // Create a new model that inits itself from the parameters just loaded
            var model2 = FundModel.GetModel();

            var tax_series = new List <double>();

            tax_series.AddRange(Enumerable.Range(0, 1050).Select(asdf => 25.0));

            model2["emissions"].Parameters["currtax"].SetValue <Timestep, Region, double>((i_t, i_r) => tax_series[i_t.Value]);
            model2["emissions"].Parameters["currtaxch4"].SetValue <Timestep, Region, double>((i_t, i_r) => tax_series[i_t.Value]);
            model2["emissions"].Parameters["currtaxn2o"].SetValue <Timestep, Region, double>((i_t, i_r) => tax_series[i_t.Value]);

            // Run the model
            var rs2 = model2.Run(parameterValues);

            var d2 = from p in rs2.GetDimensionalFieldsOperator()
                     where p.Values is IVariableWriter
                     orderby p.ComponentName, p.FieldName
                select new { ComponentName = p.ComponentName, FieldName = p.FieldName };

            // This will hold results from a policy run
            Directory.CreateDirectory("JuliaComp2");

            foreach (var i in d2)
            {
                using (var file = new StreamWriter(@"JuliaComp2\" + i.ComponentName + "." + i.FieldName + ".csv"))
                {
                    (rs2[i.ComponentName, i.FieldName] as IVariableWriter).WriteData(file);
                }
            }
        }
Ejemplo n.º 6
0
        public double Start()
        {
            int yearsToRun = Math.Min(1049, EmissionYear.Value + YearsToAggregate);

            var f1 = FundModel.GetModel(storeFullVariablesByDefault: false, years: yearsToRun);

            f1["impactwaterresources"].Variables["water"].StoreOutput   = true;
            f1["ImpactForests"].Variables["forests"].StoreOutput        = true;
            f1["ImpactHeating"].Variables["heating"].StoreOutput        = true;
            f1["ImpactCooling"].Variables["cooling"].StoreOutput        = true;
            f1["ImpactAgriculture"].Variables["agcost"].StoreOutput     = true;
            f1["ImpactSeaLevelRise"].Variables["drycost"].StoreOutput   = true;
            f1["ImpactSeaLevelRise"].Variables["protcost"].StoreOutput  = true;
            f1["ImpactSeaLevelRise"].Variables["entercost"].StoreOutput = true;
            f1["ImpactTropicalStorms"].Variables["hurrdam"].StoreOutput = true;
            f1["ImpactExtratropicalStorms"].Variables["extratropicalstormsdam"].StoreOutput = true;
            f1["ImpactBioDiversity"].Variables["species"].StoreOutput    = true;
            f1["ImpactDeathMorbidity"].Variables["deadcost"].StoreOutput = true;
            f1["ImpactDeathMorbidity"].Variables["morbcost"].StoreOutput = true;
            f1["ImpactSeaLevelRise"].Variables["wetcost"].StoreOutput    = true;
            f1["ImpactSeaLevelRise"].Variables["leavecost"].StoreOutput  = true;
            f1["socioeconomic"].Variables["income"].StoreOutput          = true;
            f1["Population"].Variables["population"].StoreOutput         = true;


            if (AdditionalInitMethod != null)
            {
                AdditionalInitMethod(f1);
            }

            var result1 = f1.Run(Parameters);

            var i_output1 = new ModelOutput();

            i_output1.Load(result1, years: yearsToRun);

            var f2 = FundModel.GetModel(storeFullVariablesByDefault: false, years: yearsToRun);

            f2["impactwaterresources"].Variables["water"].StoreOutput   = true;
            f2["ImpactForests"].Variables["forests"].StoreOutput        = true;
            f2["ImpactHeating"].Variables["heating"].StoreOutput        = true;
            f2["ImpactCooling"].Variables["cooling"].StoreOutput        = true;
            f2["ImpactAgriculture"].Variables["agcost"].StoreOutput     = true;
            f2["ImpactSeaLevelRise"].Variables["drycost"].StoreOutput   = true;
            f2["ImpactSeaLevelRise"].Variables["protcost"].StoreOutput  = true;
            f2["ImpactSeaLevelRise"].Variables["entercost"].StoreOutput = true;
            f2["ImpactTropicalStorms"].Variables["hurrdam"].StoreOutput = true;
            f2["ImpactExtratropicalStorms"].Variables["extratropicalstormsdam"].StoreOutput = true;
            f2["ImpactBioDiversity"].Variables["species"].StoreOutput    = true;
            f2["ImpactDeathMorbidity"].Variables["deadcost"].StoreOutput = true;
            f2["ImpactDeathMorbidity"].Variables["morbcost"].StoreOutput = true;
            f2["ImpactSeaLevelRise"].Variables["wetcost"].StoreOutput    = true;
            f2["ImpactSeaLevelRise"].Variables["leavecost"].StoreOutput  = true;
            f2["socioeconomic"].Variables["income"].StoreOutput          = true;
            f2["Population"].Variables["population"].StoreOutput         = true;

            if (AdditionalInitMethod != null)
            {
                AdditionalInitMethod(f2);
            }

            f2.AddComponent("marginalemission", typeof(Fund.Components.MarginalEmissionComponent), "emissions");
            f2["marginalemission"].Parameters["emissionperiod"].SetValue(EmissionYear);
            switch (Gas)
            {
            case MarginalGas.C:
                f2["marginalemission"].Parameters["emission"].Bind("emissions", "mco2");
                f2["climateco2cycle"].Parameters["mco2"].Bind("marginalemission", "modemission");
                break;

            case MarginalGas.CH4:
                f2["marginalemission"].Parameters["emission"].Bind("emissions", "globch4");
                f2["climatech4cycle"].Parameters["globch4"].Bind("marginalemission", "modemission");
                break;

            case MarginalGas.N2O:
                f2["marginalemission"].Parameters["emission"].Bind("emissions", "globn2o");
                f2["climaten2ocycle"].Parameters["globn2o"].Bind("marginalemission", "modemission");
                break;

            case MarginalGas.SF6:
                f2["marginalemission"].Parameters["emission"].Bind("emissions", "globsf6");
                f2["climatesf6cycle"].Parameters["globsf6"].Bind("marginalemission", "modemission");
                break;

            default:
                throw new NotImplementedException();
            }

            var result2 = f2.Run(Parameters);

            var i_output2 = new ModelOutput();

            i_output2.Load(result2, years: yearsToRun);

            // Take out growth effect effect of run 2 by transforming
            // the damage from run 2 into % of GDP of run 2, and then
            // multiplying that with GDP of run 1
            for (int year = 1; year < LegacyConstants.NYear; year++)
            {
                for (int region = 0; region < LegacyConstants.NoReg; region++)
                {
                    for (int sector = 0; sector < LegacyConstants.NoSector; sector++)
                    {
                        i_output2.Damages[year, region, (Sector)sector] = (i_output2.Damages[year, region, (Sector)sector] / i_output2.Incomes[year, region]) * i_output1.Incomes[year, region];
                    }
                }
            }

            // Calculate the marginal damage between run 1 and 2 for each
            // year/region/sector
            var i_marginalDamages = Damages.CalculateMarginalDamage(i_output1.Damages, i_output2.Damages);

            var weightingcom = new WeightingCombination();

            if (UseEquityWeights)
            {
                weightingcom.Add(new ConstantDiscountrate(Prtp, EmissionYear.Value));
                weightingcom.Add(new EquityWeighting(EmissionYear.Value, -1, Eta));
            }
            else
            {
                weightingcom.Add(new RamseyRegionalDiscounting(Prtp, Eta, EmissionYear.Value));
            }

            weightingcom.CalculateWeights(i_output1);
            var i_aggregatedDamage = weightingcom.AddDamagesUp(i_marginalDamages, YearsToAggregate, EmissionYear);

            return(i_aggregatedDamage);
        }