Example #1
0
        //-----------------------------------------------------------------

        public SiteVariables(ILandscape landscape,
                             Ecoregions.Map ecoregionMap)
        {
            ecoregion      = ecoregionMap.CreateSiteVar(landscape);
            disturbed      = landscape.NewSiteVar <bool>();
            registeredVars = new Dictionary <string, ISiteVariable>();
        }
        public void Map8Bit()
        {
            Map map = new Map(path8Bit, dataset, rasterDriverMgr);
            using (IInputGrid<bool> inputGrid = map.OpenAsInputGrid()) {
                Assert.AreEqual(dims8Bit.Rows, inputGrid.Dimensions.Rows);
                Assert.AreEqual(dims8Bit.Columns, inputGrid.Dimensions.Columns);

                for (int row = 0; row < dims8Bit.Rows; ++row) {
                    for (int column = 0; column < dims8Bit.Columns; ++column) {
                        IEcoregion ecoregion = dataset.Find(ecoregions8Bit[row,column]);
                        Assert.AreEqual(ecoregion.Active, inputGrid.ReadValue());
                    }
                }
            }
        }
        public void Map8Bit()
        {
            Map map = new Map(path8Bit, dataset, rasterDriverMgr);
            using (IInputGrid<EcoregionCode> inputGrid = map.OpenAsInputGrid()) {
                Assert.AreEqual(dims8Bit.Rows, inputGrid.Dimensions.Rows);
                Assert.AreEqual(dims8Bit.Columns, inputGrid.Dimensions.Columns);

                for (int row = 0; row < dims8Bit.Rows; ++row) {
                    for (int column = 0; column < dims8Bit.Columns; ++column) {
                        ushort mapCode = ecoregions8Bit[row,column];
                        IEcoregion ecoregion = dataset.Find(mapCode);
                        EcoregionCode ecoregionCode = inputGrid.ReadValue();
                        Assert.AreEqual(mapCode, (int) ecoregionCode);
                        Assert.AreEqual(ecoregion.Active, ecoregionCode.IsActive);
                    }
                }
            }
        }
Example #4
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
        }
Example #5
0
            //-----------------------------------------------------------------

            internal static void Initialize(Ecoregions.Map ecoregionMap)
            {
                ecoregion = ecoregionMap.CreateSiteVar(Model.Landscape);
                disturbed = Model.Landscape.NewSiteVar <bool>();
            }
Example #6
0
        //---------------------------------------------------------------------

        /// <summary>
        /// Runs a model scenario.
        /// </summary>
        public void Run(string scenarioPath, IUserInterface ui)
        {
            this.ui = ui;

            siteVarRegistry.Clear();

            Scenario scenario = LoadScenario(scenarioPath);

            startTime      = scenario.StartTime;
            endTime        = scenario.EndTime;
            timeSinceStart = 0;
            currentTime    = startTime;
            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,
                                                              ecoregions,
                                                              rasterFactory);
            // -- ProcessMetadata(ecoregionsMap.Metadata, scenario);
            cellLength = scenario.CellLength.Value;
            cellArea   = (float)((cellLength * cellLength) / 10000);
            ui.WriteLine("Cell length = {0} m, cell area = {1} ha", cellLength, cellArea);

            using (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(grid);
                landscape = landscapeFactory.CreateLandscape(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));

            ecoregionSiteVar = ecoregionsMap.CreateSiteVar(landscape);

            disturbAndOtherExtensions = new List <ExtensionMain>();

            try {
                ui.WriteLine("Loading {0} extension ...", scenario.Succession.Info.Name);
                succession = Loader.Load <SuccessionMain>(scenario.Succession.Info);
                succession.LoadParameters(scenario.Succession.InitFile, this);
                succession.Initialize();

                ExtensionMain[] disturbanceExtensions = LoadExtensions(scenario.Disturbances);
                InitExtensions(disturbanceExtensions);

                ExtensionMain[] otherExtensions = LoadExtensions(scenario.OtherExtensions);
                InitExtensions(otherExtensions);


                //  Perform 2nd phase of initialization for non-succession extensions.
                foreach (ExtensionMain extension in disturbanceExtensions)
                {
                    extension.InitializePhase2();
                }
                foreach (ExtensionMain extension in otherExtensions)
                {
                    extension.InitializePhase2();
                }

                //  Run output extensions for TimeSinceStart = 0 (time step 0)
                foreach (ExtensionMain extension in otherExtensions)
                {
                    if (extension.Type.IsMemberOf("output"))
                    {
                        Run(extension);
                    }
                }

                //******************// for Rob
                //  Main time loop  //
                //******************//

                for (currentTime++, timeSinceStart++;
                     currentTime <= endTime;
                     currentTime++, timeSinceStart++)
                {
                    bool isSuccTimestep = IsExtensionTimestep(succession);

                    List <ExtensionMain> distExtensionsToRun;
                    distExtensionsToRun = GetExtensionsToRun(disturbanceExtensions);
                    bool isDistTimestep = distExtensionsToRun.Count > 0;

                    List <ExtensionMain> otherExtensionsToRun;
                    otherExtensionsToRun = GetExtensionsToRun(otherExtensions);

                    if (!isSuccTimestep && !isDistTimestep &&
                        otherExtensionsToRun.Count == 0)
                    {
                        continue;
                    }

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

                    if (isDistTimestep)
                    {
                        if (scenario.DisturbancesRandomOrder)
                        {
                            distExtensionsToRun = shuffle(distExtensionsToRun);
                        }
                        foreach (ExtensionMain distExtension in distExtensionsToRun)
                        {
                            Run(distExtension);
                        }
                    }

                    if (isSuccTimestep || isDistTimestep)
                    {
                        Run(succession);
                    }

                    foreach (ExtensionMain otherExtension in otherExtensionsToRun)
                    {
                        Run(otherExtension);
                    }
                }  // main time loop
            }
            finally {
                foreach (ExtensionMain extension in disturbAndOtherExtensions)
                {
                    extension.CleanUp();
                }
            }
            ui.WriteLine("Model run is complete.");
        }
Example #7
0
        //---------------------------------------------------------------------

        /// <summary>
        /// Runs a model scenario.
        /// </summary>
        public void Run(string scenarioPath)
        {
            siteVarRegistry.Clear();

            Scenario scenario = LoadScenario(scenarioPath);

            startTime      = scenario.StartTime;
            endTime        = scenario.EndTime;
            timeSinceStart = 0;
            currentTime    = startTime;
            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,
                                                              ecoregions,
                                                              rasterDriverMgr);
            ProcessMetadata(ecoregionsMap.Metadata, scenario);
            using (IInputGrid <EcoregionCode> grid = ecoregionsMap.OpenAsInputGrid()) {
                UI.WriteLine("Map dimensions: {0} = {1:#,##0} cell{2}", grid.Dimensions,
                             grid.Count, (grid.Count == 1 ? "" : "s"));
                landscape = new Landscape(grid, scenario.BlockSize.HasValue ? scenario.BlockSize.Value : 1);
            }
            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));

            ecoregionSiteVar = ecoregionsMap.CreateSiteVar(landscape);

            plugInsWith2PhaseInit = new List <PlugIns.I2PhaseInitialization>();
            plugInsWithCleanUp    = new List <PlugIns.ICleanUp>();

            try {
                UI.WriteLine("Loading {0} plug-in ...", scenario.Succession.Info.Name);
                succession = Loader.Load <SuccessionPlugIn>(scenario.Succession.Info);
                succession.Initialize(scenario.Succession.InitFile, this);
                succession.InitializeSites(scenario.InitialCommunities,
                                           scenario.InitialCommunitiesMap);

                PlugIn[] disturbancePlugIns = LoadAndInitPlugIns(scenario.Disturbances);
                PlugIn[] otherPlugIns       = LoadAndInitPlugIns(scenario.OtherPlugIns);

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

                //  Run output plug-ins for TimeSinceStart = 0 (time step 0)
                foreach (PlugIn plugIn in otherPlugIns)
                {
                    if (plugIn.PlugInType.IsMemberOf("output"))
                    {
                        Run(plugIn);
                    }
                }

                //******************// for Rob
                //  Main time loop  //
                //******************//

                for (currentTime++, timeSinceStart++;
                     currentTime <= endTime;
                     currentTime++, timeSinceStart++)
                {
                    bool isSuccTimestep = IsPlugInTimestep(succession);

                    List <PlugIn> distPlugInsToRun;
                    distPlugInsToRun = GetPlugInsToRun(disturbancePlugIns);
                    bool isDistTimestep = distPlugInsToRun.Count > 0;

                    List <PlugIn> otherPlugInsToRun;
                    otherPlugInsToRun = GetPlugInsToRun(otherPlugIns);

                    if (!isSuccTimestep && !isDistTimestep &&
                        otherPlugInsToRun.Count == 0)
                    {
                        continue;
                    }

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

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

                    if (isSuccTimestep || isDistTimestep)
                    {
                        Run(succession);
                    }

                    foreach (PlugIn otherPlugIn in otherPlugInsToRun)
                    {
                        Run(otherPlugIn);
                    }
                }  // main time loop
            }
            finally {
                foreach (PlugIns.ICleanUp plugIn in plugInsWithCleanUp)
                {
                    plugIn.CleanUp();
                }
            }
        }
Example #8
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();
                }
            }
        }