コード例 #1
0
        //---------------------------------------------------------------------

        /// <summary>
        /// Runs a model scenario.
        /// </summary>
        public static void Run(string scenarioPath)
        {
            //  Initialize plug-ins manager with the default plug-ins
            //	database in the folder where application resides.
            PlugIns.Manager.Initialize(PlugIns.Database.DefaultPath);

            IScenario scenario = LoadScenario(scenarioPath);

            cellLength = 29.0f;              // 29m hard-wired until metadata implemented in raster driver

            LoadSpecies(scenario.Species);
            LoadEcoregions(scenario.Ecoregions);

            Log.Info("Initializing landscape from ecoregions map \"{0}\" ...", scenario.EcoregionsMap);
            Ecoregions.Map ecoregionsMap = new Ecoregions.Map(scenario.EcoregionsMap,
                                                              Model.Ecoregions);
            using (Landscape.IInputGrid <bool> grid = ecoregionsMap.OpenAsInputGrid()) {
                landscape = new Landscape.Landscape(grid);
            }
            landscapeMapDims = new Raster.Dimensions((int)landscape.Rows,
                                                     (int)landscape.Columns);
            SiteVars.Initialize(ecoregionsMap);

            //  Load and initialize plug-ins.

            Log.Info("Loading {0} plug-in ...", scenario.Succession.Info.Name);
            succession = PlugIns.Manager.Load <ISuccession>(scenario.Succession.Info);
            succession.Initialize(scenario.Succession.InitFile);
            succession.InitializeSites(scenario.InitialCommunities,
                                       scenario.InitialCommunitiesMap);

            IDisturbance[] disturbancePlugIns = LoadPlugIns <IDisturbance>(scenario.Disturbances);
            IOutput[]      outputPlugIns      = LoadPlugIns <IOutput>(scenario.Outputs);

            //  Run those output plug-ins whose next time to run is 0.
            foreach (IOutput outPlugIn in GetPlugInsToRun <IOutput>(outputPlugIns, 0))
            {
                outPlugIn.Run(0);
            }

            //******************// for Rob
            //  Main time loop  //
            //******************//
            // currentTime (years)
            for (int currentTime = 1; currentTime <= scenario.Duration; ++currentTime)
            {
                List <IDisturbance> distPlugInsToRun;
                distPlugInsToRun = GetPlugInsToRun <IDisturbance>(disturbancePlugIns, currentTime);
                bool isDistTimestep = distPlugInsToRun.Count > 0;

                List <IOutput> outPlugInsToRun;
                outPlugInsToRun = GetPlugInsToRun <IOutput>(outputPlugIns, currentTime);
                bool isOutTimestep = outPlugInsToRun.Count > 0;

                bool isSuccTimestep = succession.NextTimeToRun == currentTime;

                //  If not a succession timestep, a disturance timestep or
                //  an output timestep, then go to the next timestep.
                if (!(isSuccTimestep || isDistTimestep || isOutTimestep))
                {
                    continue;
                }

                Log.Info("Current time: {0}", currentTime);

                if (isDistTimestep)
                {
                    if (scenario.DisturbancesRandomOrder)
                    {
                        distPlugInsToRun = Shuffle(distPlugInsToRun);
                    }
                    foreach (IDisturbance distPlugIn in distPlugInsToRun)
                    {
                        distPlugIn.Run(currentTime);
                    }
                }

                if (isSuccTimestep || isDistTimestep)
                {
                    IEnumerable <ActiveSite> sites;
                    if (isSuccTimestep)
                    {
                        sites = Model.Landscape.ActiveSites;
                    }
                    else
                    {
                        sites = DisturbedSites();
                    }
                    succession.AgeCohorts(sites, currentTime);
                    succession.ComputeShade(sites);
                    succession.ReproducePlants(sites);
                }

                //  Run output plug-ins.
                foreach (IOutput outPlugIn in outPlugInsToRun)
                {
                    outPlugIn.Run(currentTime);
                }

                if (!isSuccTimestep)
                {
                    SiteVars.Disturbed.ActiveSiteValues = false;
                }
            }              // main time loop
        }
コード例 #2
0
        //---------------------------------------------------------------------

        /// <summary>
        /// Runs a model scenario.
        /// </summary>
        public static void Run(string scenarioPath)
        {
            //  Initialize plug-ins manager with the default plug-ins
            //	database in the folder where application resides.
            PlugIns.Manager.Initialize(PlugIns.Database.DefaultPath);

            IScenario scenario  = LoadScenario(scenarioPath);
            int       startTime = 1;

            InitializeRandomNumGenerator(scenario.RandomNumberSeed);

            LoadSpecies(scenario.Species);
            LoadEcoregions(scenario.Ecoregions);

            UI.WriteLine("Initializing landscape from ecoregions map \"{0}\" ...", scenario.EcoregionsMap);
            Ecoregions.Map ecoregionsMap = new Ecoregions.Map(scenario.EcoregionsMap,
                                                              Model.Ecoregions);
            ProcessMetadata(ecoregionsMap.Metadata, scenario);
            using (Landscape.IInputGrid <bool> grid = ecoregionsMap.OpenAsInputGrid()) {
                UI.WriteLine("Map dimensions: {0} = {1:#,##0} cell{2}", grid.Dimensions,
                             grid.Count, (grid.Count == 1 ? "" : "s"));
                landscape = new Landscape.Landscape(grid);
            }
            UI.WriteLine("Sites: {0:#,##0} active ({1:p1}), {2:#,##0} inactive ({3:p1})",
                         landscape.ActiveSiteCount, (landscape.Count > 0 ? ((double)landscape.ActiveSiteCount) / landscape.Count : 0),
                         landscape.InactiveSiteCount, (landscape.Count > 0 ? ((double)landscape.InactiveSiteCount) / landscape.Count : 0));
            landscapeMapDims = new RasterIO.Dimensions((int)landscape.Rows,
                                                       (int)landscape.Columns);
            siteVars = new SiteVariables(landscape, ecoregionsMap);

            DisturbedSiteEnumerator disturbedSites = new DisturbedSiteEnumerator(landscape,
                                                                                 SiteVars.Disturbed);

            plugInsWith2PhaseInit = new List <PlugIns.I2PhaseInitialization>();
            plugInsWithCleanUp    = new List <PlugIns.ICleanUp>();
            try {
                //  Load and initialize plug-ins.

                UI.WriteLine("Loading {0} plug-in ...", scenario.Succession.Info.Name);
                succession = PlugIns.Manager.Load <ISuccession>(scenario.Succession.Info);
                succession.Initialize(scenario.Succession.InitFile, startTime);
                succession.InitializeSites(scenario.InitialCommunities,
                                           scenario.InitialCommunitiesMap);

                IDisturbance[] disturbancePlugIns = LoadPlugIns <IDisturbance>(scenario.Disturbances, startTime);
                IOutput[]      outputPlugIns      = LoadPlugIns <IOutput>(scenario.Outputs, startTime);

                //	Perform 2nd phase of initialization for those plug-ins
                //	that have it.
                foreach (PlugIns.I2PhaseInitialization plugIn in plugInsWith2PhaseInit)
                {
                    plugIn.InitializePhase2();
                }

                //  Run those output plug-ins whose next time to run is startTime-1.
                foreach (IOutput outPlugIn in GetPlugInsToRun <IOutput>(outputPlugIns, startTime - 1))
                {
                    outPlugIn.Run(startTime - 1);
                }

                //******************// for Rob
                //  Main time loop  //
                //******************//
                // currentTime (years)
                int endTime = startTime + scenario.Duration - 1;
                for (int currentTime = startTime; currentTime <= endTime; ++currentTime)
                {
                    List <IDisturbance> distPlugInsToRun;
                    distPlugInsToRun = GetPlugInsToRun <IDisturbance>(disturbancePlugIns, currentTime);
                    bool isDistTimestep = distPlugInsToRun.Count > 0;

                    List <IOutput> outPlugInsToRun;
                    outPlugInsToRun = GetPlugInsToRun <IOutput>(outputPlugIns, currentTime);
                    bool isOutTimestep = outPlugInsToRun.Count > 0;

                    bool isSuccTimestep = succession.NextTimeToRun == currentTime;

                    //  If not a succession timestep, a disturance timestep or
                    //  an output timestep, then go to the next timestep.
                    if (!(isSuccTimestep || isDistTimestep || isOutTimestep))
                    {
                        continue;
                    }

                    UI.WriteLine("Current time: {0}", currentTime);

                    if (isDistTimestep)
                    {
                        if (scenario.DisturbancesRandomOrder)
                        {
                            Util.Random.Shuffle(distPlugInsToRun);
                        }
                        foreach (IDisturbance distPlugIn in distPlugInsToRun)
                        {
                            distPlugIn.Run(currentTime);
                        }
                    }

                    if (isSuccTimestep || isDistTimestep)
                    {
                        IEnumerable <MutableActiveSite> sites;
                        if (isSuccTimestep)
                        {
                            sites = Model.Landscape.ActiveSites;
                        }
                        else
                        {
                            sites = disturbedSites;
                        }
                        succession.AgeCohorts(sites, currentTime);
                        succession.ComputeShade(sites);
                        succession.ReproduceCohorts(sites);
                    }

                    //  Run output plug-ins.
                    foreach (IOutput outPlugIn in outPlugInsToRun)
                    {
                        outPlugIn.Run(currentTime);
                    }

                    if (!isSuccTimestep)
                    {
                        SiteVars.Disturbed.ActiveSiteValues = false;
                    }
                }                  // main time loop
            }
            finally {
                foreach (PlugIns.ICleanUp plugIn in plugInsWithCleanUp)
                {
                    plugIn.CleanUp();
                }
            }
        }