public void SingleCohort_ZeroBiomass()
        {
            SiteCohorts  cohorts        = new SiteCohorts();
            const ushort initialBiomass = 100;

            cohorts.AddNewCohort(poputrem, initialBiomass);

            mockCalculator.CountCalled = 0;
            mockCalculator.Change      = -2;

            expectedSender   = cohorts[poputrem];
            expectedDistType = null;  // death during growth phase
            expectedSite     = activeSite;
            deadCohorts.Clear();

            for (int time = 1; time <= 60; time++)
            {
                if (time % successionTimestep == 0)
                {
                    Util.Grow(cohorts, successionTimestep, activeSite, true);
                }
            }

            expectedCohorts.Clear();
            Util.CheckCohorts(expectedCohorts, cohorts);

            Assert.AreEqual(1, deadCohorts.Count);
            ICohort deadCohort = deadCohorts[0];

            Assert.AreEqual(poputrem, deadCohort.Species);
            Assert.AreEqual(initialBiomass / -mockCalculator.Change, deadCohort.Age);
            Assert.AreEqual(0, deadCohort.Biomass);
        }
        public void SingleCohort()
        {
            SiteCohorts cohorts = new SiteCohorts();
            const ushort initialBiomass = 300;
            cohorts.AddNewCohort(abiebals, initialBiomass);

            expectedCohorts.Clear();
            expectedCohorts[abiebals] = new ushort[] { 1, initialBiomass };
            Util.CheckCohorts(expectedCohorts, cohorts);
        }
Exemplo n.º 3
0
        public void SingleCohort()
        {
            SiteCohorts  cohorts        = new SiteCohorts();
            const ushort initialBiomass = 300;

            cohorts.AddNewCohort(abiebals, initialBiomass);

            expectedCohorts.Clear();
            expectedCohorts[abiebals] = new ushort[] { 1, initialBiomass };
            Util.CheckCohorts(expectedCohorts, cohorts);
        }
Exemplo n.º 4
0
        public void CombineYoungCohorts()
        {
            SiteCohorts cohorts = new SiteCohorts();

            int[] initialBiomass = new int[] { 300, 700 };
            cohorts.AddNewCohort(abiebals, initialBiomass[0]);
            Assert.AreEqual(initialBiomass[0], cohorts.TotalBiomass);

            //  Grow 1st cohort for 4 years, adding 10 to its biomass per year
            mockCalculator.CountCalled = 0;
            mockCalculator.Change      = 10;
            Util.Grow(cohorts, 4, activeSite, false);

            Assert.AreEqual(4, mockCalculator.CountCalled);

            expectedCohorts.Clear();
            expectedCohorts[abiebals] = new int[] {
                5, (int)(300 + 4 * mockCalculator.Change)
            };
            Util.CheckCohorts(expectedCohorts, cohorts);

            //  Add 2nd cohort and then grow both cohorts 6 more years up to
            //  a succession timestep
            cohorts.AddNewCohort(abiebals, initialBiomass[1]);
            mockCalculator.CountCalled = 0;
            Util.Grow(cohorts, 6, activeSite, true);

            //  ComputeChange for both cohorts for 5 years, then combine them,
            //  and then one time for the combined cohort
            Assert.AreEqual(5 * 2 + 1, mockCalculator.CountCalled);

            expectedCohorts.Clear();
            expectedCohorts[abiebals] = new int[] {
                successionTimestep,
                (int)
                (300 + (4 + 5) * mockCalculator.Change     // first cohort before combining
                 + 700 + 5 * mockCalculator.Change         // 2nd cohort before combining
                 + mockCalculator.Change)                  // growth after combining
            };
            Util.CheckCohorts(expectedCohorts, cohorts);
        }
        public void SingleCohort_LongevityReached()
        {
            SiteCohorts  cohorts        = new SiteCohorts();
            const ushort initialBiomass = 300;

            cohorts.AddNewCohort(poputrem, initialBiomass);

            mockCalculator.CountCalled = 0;
            mockCalculator.Change      = 1;

            expectedSender   = cohorts[poputrem];
            expectedDistType = null;  // death during growth phase
            expectedSite     = activeSite;
            deadCohorts.Clear();

            //  Repeatedly grow for succession timesteps until longevity
            //  reached.
            int time = 0;

            do
            {
                time += successionTimestep;
                Util.Grow(cohorts, successionTimestep, activeSite, true);
            } while (time <= poputrem.Longevity);

            expectedCohorts.Clear();
            Util.CheckCohorts(expectedCohorts, cohorts);

            //  Calculator called L times where L is longevity.  Inituitively,
            //  one would think since initial cohort's age is 1, it'd only take
            //  L-1 times to get to the max age (= L).  So the calculator
            //  should be called L-1 times.  But the combining of young cohorts
            //  at the first succession timestep (t_succ = 20) results in the
            //  calculator being called twice with cohort age = t_succ-1 (19).
            //  At the end of year 19, the cohort's age is 20 and the
            //  calculator has been called 19 times.  But at the start of year
            //  20, the combine-young-cohorts operation is done because it's a
            //  succession timestep.  The combine operation takes all the young
            //  cohorts (age <= t_succ = 20) and replaces them with one cohort
            //  with age = t_succ-1 (= 19).  This ensures that after the growth
            //  phase, the cohort's age will be t_succ (20).  So the growth
            //  phase of year 20 calls the calculator for the 20th time with
            //  cohort age 19.
            Assert.AreEqual(poputrem.Longevity, mockCalculator.CountCalled);

            Assert.AreEqual(1, deadCohorts.Count);
            ICohort deadCohort = deadCohorts[0];

            Assert.AreEqual(poputrem, deadCohort.Species);
            Assert.AreEqual(poputrem.Longevity, deadCohort.Age);
            Assert.AreEqual(initialBiomass + (poputrem.Longevity * mockCalculator.Change),
                            deadCohort.Biomass);
        }
Exemplo n.º 6
0
        //---------------------------------------------------------------------

        private void CreateInitialCohorts()
        {
            ushort[] abiebalsAges = new ushort[] { 30, 40, 50, 150, 170 };
            ushort[] betualleAges = new ushort[] { 100, 120, 280, 300 };

            //  Work with ages from oldest to youngest
            System.Array.Sort(abiebalsAges, Landis.AgeCohort.Util.WhichIsOlder);
            System.Array.Sort(betualleAges, Landis.AgeCohort.Util.WhichIsOlder);

            //  Loop through succession timesteps from the time when the
            //  oldest cohort would have been added to site (= -{its age}) to
            //  the present (time = 0).  Each cohort is added to the site
            //  when time = -{its age}.
            initialSiteCohorts = new SiteCohorts();
            const int     initialBiomass   = 55;
            List <ushort> abiebalsAgesLeft = new List <ushort>(abiebalsAges);
            List <ushort> betualleAgesLeft = new List <ushort>(betualleAges);
            ushort        maxAge           = System.Math.Max(abiebalsAgesLeft[0], betualleAgesLeft[0]);

            for (int time = -maxAge; time <= 0; time += successionTimestep)
            {
                Util.Grow(initialSiteCohorts, successionTimestep, activeSite, true);
                if (abiebalsAgesLeft.Count > 0)
                {
                    if (time == -(abiebalsAgesLeft[0]))
                    {
                        initialSiteCohorts.AddNewCohort(abiebals, initialBiomass);
                        abiebalsAgesLeft.RemoveAt(0);
                    }
                }
                if (betualleAgesLeft.Count > 0)
                {
                    if (time == -(betualleAgesLeft[0]))
                    {
                        initialSiteCohorts.AddNewCohort(betualle, initialBiomass);
                        betualleAgesLeft.RemoveAt(0);
                    }
                }
            }
        }
        //---------------------------------------------------------------------
        public static SiteCohorts Clone(ISiteCohorts site_cohorts)
        {
            SiteCohorts clone = new SiteCohorts();

            foreach (ISpeciesCohorts speciesCohorts in site_cohorts)
            {
                foreach (ICohort cohort in speciesCohorts)
                {
                    clone.AddNewCohort(cohort.Species, cohort.Age, cohort.WoodBiomass, cohort.LeafBiomass);
                }
            }
            return(clone);
        }
        //---------------------------------------------------------------------
        public static ISiteCohorts Clone(ActiveSite site, ISiteCohorts site_cohorts)
        {
            ISiteCohorts clone = new SiteCohorts();

            foreach (ISpeciesCohorts speciesCohorts in site_cohorts)
            {
                foreach (Cohort cohort in speciesCohorts)
                {
                    clone.AddNewCohort(cohort);
                }
            }
            return(clone);
        }
        //---------------------------------------------------------------------
        public static SiteCohorts Clone(SiteCohorts site_cohorts)
        {
            SiteCohorts clone = new SiteCohorts();

            foreach (ISpeciesCohorts speciesCohorts in site_cohorts)
            {
                foreach (ICohort cohort in speciesCohorts)
                {
                    clone.AddNewCohort(cohort.Species, cohort.Age, cohort.Biomass);   //species.cohorts.Add(speciesCohorts.Clone());
                }
            }
            return(clone);
        }
        public void CombineYoungCohorts()
        {
            SiteCohorts cohorts = new SiteCohorts();
            ushort[] initialBiomass = new ushort[] { 300, 700 };
            cohorts.AddNewCohort(abiebals, initialBiomass[0]);

            //  Grow 1st cohort for 4 years, adding 10 to its biomass per year
            mockCalculator.CountCalled = 0;
            mockCalculator.Change = 10;
            cohorts.Grow(4, activeSite, false);

            Assert.AreEqual(4, mockCalculator.CountCalled);

            expectedCohorts.Clear();
            expectedCohorts[abiebals] = new ushort[] {
                5, (ushort) (300 + 4 * mockCalculator.Change)
            };
            Util.CheckCohorts(expectedCohorts, cohorts);

            //  Add 2nd cohort and then grow both cohorts 6 more years up to
            //  a succession timestep
            cohorts.AddNewCohort(abiebals, initialBiomass[1]);
             	    mockCalculator.CountCalled = 0;
            cohorts.Grow(6, activeSite, true);

            //  ComputeChange for both cohorts for 5 years, then combine them,
            //  and then one time for the combined cohort
            Assert.AreEqual(5 * 2 + 1, mockCalculator.CountCalled);

            expectedCohorts.Clear();
            expectedCohorts[abiebals] = new ushort[] {
                successionTimestep,
                (ushort)
                    (300 + (4 + 5) * mockCalculator.Change // first cohort before combining
                     + 700 + 5 * mockCalculator.Change     // 2nd cohort before combining
                     + mockCalculator.Change)              // growth after combining
            };
            Util.CheckCohorts(expectedCohorts, cohorts);
        }
        public void SingleCohort()
        {
            SiteCohorts cohorts = new SiteCohorts();
            Assert.AreEqual(0, cohorts.TotalBiomass);

            const int initialBiomass = 300;
            cohorts.AddNewCohort(abiebals, initialBiomass);
            Assert.AreEqual(initialBiomass, cohorts.TotalBiomass);

            expectedCohorts.Clear();
            expectedCohorts[abiebals] = new int [] { 1, initialBiomass };
            Util.CheckCohorts(expectedCohorts, cohorts);
        }
Exemplo n.º 12
0
        public void SingleCohort()
        {
            SiteCohorts cohorts = new SiteCohorts();

            Assert.AreEqual(0, cohorts.TotalBiomass);

            const int initialBiomass = 300;

            cohorts.AddNewCohort(abiebals, initialBiomass);
            Assert.AreEqual(initialBiomass, cohorts.TotalBiomass);

            expectedCohorts.Clear();
            expectedCohorts[abiebals] = new int [] { 1, initialBiomass };
            Util.CheckCohorts(expectedCohorts, cohorts);
        }
		public void SingleCohort_LongevityReached()
		{
		    SiteCohorts cohorts = new SiteCohorts();
		    const ushort initialBiomass = 300;
		    cohorts.AddNewCohort(poputrem, initialBiomass);

		    mockCalculator.CountCalled = 0;
		    mockCalculator.Change = 1;

		    expectedSite = activeSite;
		    deadCohorts.Clear();

		    //  Repeatedly grow for succession timesteps until longevity
		    //  reached.
		    int time = 0;
		    do {
		         time += successionTimestep;
		        cohorts.Grow(successionTimestep, activeSite, true);
		    } while (time <= poputrem.Longevity);

		    expectedCohorts.Clear();
		    Util.CheckCohorts(expectedCohorts, cohorts);

		    //  Calculator called L times where L is longevity.  Inituitively,
		    //  one would think since initial cohort's age is 1, it'd only take
		    //  L-1 times to get to the max age (= L).  So the calculator
		    //  should be called L-1 times.  But the combining of young cohorts
		    //  at the first succession timestep (t_succ = 20) results in the
		    //  calculator being called twice with cohort age = t_succ-1 (19).
		    //  At the end of year 19, the cohort's age is 20 and the
		    //  calculator has been called 19 times.  But at the start of year
		    //  20, the combine-young-cohorts operation is done because it's a
		    //  succession timestep.  The combine operation takes all the young
		    //  cohorts (age <= t_succ = 20) and replaces them with one cohort
		    //  with age = t_succ-1 (= 19).  This ensures that after the growth
		    //  phase, the cohort's age will be t_succ (20).  So the growth
		    //  phase of year 20 calls the calculator for the 20th time with
		    //  cohort age 19.
		    Assert.AreEqual(poputrem.Longevity, mockCalculator.CountCalled);

		    Assert.AreEqual(1, deadCohorts.Count);
		    ICohort deadCohort = deadCohorts[0];
		    Assert.AreEqual(poputrem, deadCohort.Species);
		    Assert.AreEqual(poputrem.Longevity, deadCohort.Age);
		    Assert.AreEqual(initialBiomass + (poputrem.Longevity * mockCalculator.Change),
		                    deadCohort.Biomass);
		}
        public void Grow()
        {
            SiteCohorts cohorts = new SiteCohorts();
            const ushort initialBiomass = 35;
            cohorts.AddNewCohort(abiebals, initialBiomass);

            mockCalculator.CountCalled = 0;
            mockCalculator.Change = 8;

            cohorts.Grow(activeSite, true);

            expectedCohorts.Clear();
            expectedCohorts[abiebals] = new ushort[] {
                //  age  biomass
                     2,   (ushort) (initialBiomass + mockCalculator.Change)
            };
            Util.CheckCohorts(expectedCohorts, cohorts);
        }
Exemplo n.º 15
0
        public void Grow()
        {
            SiteCohorts  cohorts        = new SiteCohorts();
            const ushort initialBiomass = 35;

            cohorts.AddNewCohort(abiebals, initialBiomass);

            mockCalculator.CountCalled = 0;
            mockCalculator.Change      = 8;

            cohorts.Grow(activeSite, true);

            expectedCohorts.Clear();
            expectedCohorts[abiebals] = new ushort[] {
                //  age  biomass
                2, (ushort)(initialBiomass + mockCalculator.Change)
            };
            Util.CheckCohorts(expectedCohorts, cohorts);
        }
Exemplo n.º 16
0
        public SiteConditions GetFromKey(uint key)
        {
            SiteConditions s = null;

            if (initialSites.TryGetValue(key, out s) && siteoutput == null)
            {
                hydrology     = s.hydrology;
                establishment = s.Establishment;

                cohorts = new SiteCohorts();
                foreach (ISpeciesCohorts speciesCohorts in s.cohorts)
                {
                    foreach (Cohort cohort in speciesCohorts)
                    {
                        Cohort newcohort = new Cohort(cohort);
                        cohorts.AddNewCohort(newcohort, PlugIn.TStep);
                    }
                }
                forestfloor  = s.forestfloor;
                canopylaimax = s.CanopyLAImax;
            }
            return(s);
        }
        //---------------------------------------------------------------------

        private void CreateInitialCohorts()
        {
            ushort[] abiebalsAges = new ushort[]{30, 40, 50, 150, 170};
            ushort[] betualleAges = new ushort[]{100, 120, 280, 300};

            //  Work with ages from oldest to youngest
            System.Array.Sort(abiebalsAges, Landis.AgeCohort.Util.WhichIsOlder);
            System.Array.Sort(betualleAges, Landis.AgeCohort.Util.WhichIsOlder);

            //  Loop through succession timesteps from the time when the
            //  oldest cohort would have been added to site (= -{its age}) to
            //  the present (time = 0).  Each cohort is added to the site
            //  when time = -{its age}.
            initialSiteCohorts = new SiteCohorts();
            const int initialBiomass = 55;
            List<ushort> abiebalsAgesLeft = new List<ushort>(abiebalsAges);
            List<ushort> betualleAgesLeft = new List<ushort>(betualleAges);
            ushort maxAge = System.Math.Max(abiebalsAgesLeft[0], betualleAgesLeft[0]);
            for (int time = -maxAge; time <= 0; time += successionTimestep) {
                Util.Grow(initialSiteCohorts, successionTimestep, activeSite, true);
                if (abiebalsAgesLeft.Count > 0) {
                    if (time == -(abiebalsAgesLeft[0])) {
                        initialSiteCohorts.AddNewCohort(abiebals, initialBiomass);
                        abiebalsAgesLeft.RemoveAt(0);
                    }
                }
                if (betualleAgesLeft.Count > 0) {
                    if (time == -(betualleAgesLeft[0])) {
                        initialSiteCohorts.AddNewCohort(betualle, initialBiomass);
                        betualleAgesLeft.RemoveAt(0);
                    }
                }
            }
        }
Exemplo n.º 18
0
 public void AddNewCohort(Cohort cohort, int SuccessionTimeStep)
 {
     cohorts.AddNewCohort(cohort, SuccessionTimeStep);
 }
        public SiteConditions GetFromKey(uint key)
        {
            SiteConditions s=null;
            if (initialSites.TryGetValue(key, out s) && siteoutput == null)
            {
                hydrology = s.hydrology;
                establishment = s.Establishment;

                cohorts = new SiteCohorts();
                foreach (ISpeciesCohorts speciesCohorts in s.cohorts)
                {
                    foreach (Cohort cohort in speciesCohorts)
                    {
                        Cohort newcohort = new Cohort(cohort);
                        cohorts.AddNewCohort(newcohort, PlugIn.TStep);
                    }
                }
                forestfloor = s.forestfloor;
                canopylaimax = s.CanopyLAImax;
            }
            return s;
        }
        //---------------------------------------------------------------------

        private void CohortsRemoved(object disturbance)
        {
            SiteCohorts  cohorts        = new SiteCohorts();
            const ushort initialBiomass = 40;

            mockCalculator.CountCalled = 0;
            mockCalculator.Change      = 1;

            int timeOfLastSucc = 0;

            for (int time = 1; time <= 50; time++)
            {
                //  Simulate the site being disturbed every 8 years which
                //  results in a new cohort being added.
                bool siteDisturbed = (time % 8 == 0);

                bool isSuccTimestep = (time % successionTimestep == 0);

                if (siteDisturbed || isSuccTimestep)
                {
                    Util.Grow(cohorts, (ushort)(time - timeOfLastSucc),
                              activeSite, isSuccTimestep);
                    timeOfLastSucc = time;
                }

                if (siteDisturbed)
                {
                    cohorts.AddNewCohort(poputrem, initialBiomass);
                }
            }

            //  Expected sequence of cohort changes:
            //
            //        Time  Grow_________
            //        Last         Cohorts               New
            //  Time  Succ  years  afterwards            Cohort
            //  ----  ----  -----  ---------------       ------
            //    8     0     8                           1(40)
            //   16     8     8    9(48)                  1(40)
            //   20    16     4    20(95*)                       * = 48+3 + 40+3 + 1
            //   24    20     4    24(99)                 1(40)
            //   32    24     8    32(107),9(48)          1(40)
            //   40    32     8    40(115),20(103*)       1(40)  * = 48+7 + 40+7 + 1
            //   48    40     8    48(123),28(111),9(48)  1(40)

            expectedCohorts.Clear();
            expectedCohorts[poputrem] = new ushort[] {
                //  age  biomass
                48, 123,
                28, 111,
                9, 48,
                1, 40
            };
            Util.CheckCohorts(expectedCohorts, cohorts);

            //  Remove cohorts whose ages are between 10 and 30
            expectedSender = cohorts[poputrem];
            deadCohorts.Clear();
            if (disturbance is IDisturbance)
            {
                cohorts.DamageBy((IDisturbance)disturbance);
            }
            else if (disturbance is AgeCohort.ICohortDisturbance)
            {
                ((AgeCohort.ISiteCohorts)cohorts).DamageBy((AgeCohort.ICohortDisturbance)disturbance);
            }

            expectedCohorts.Clear();
            expectedCohorts[poputrem] = new ushort[] {
                //  age  biomass
                48, 123,
                1, 40
            };
            Util.CheckCohorts(expectedCohorts, cohorts);

            CheckDeadCohorts(deadCohorts);
        }
        //---------------------------------------------------------------------
        /// <summary>
        /// Makes the set of biomass cohorts at a site based on the age cohorts
        /// at the site, using a specified method for computing a cohort's
        /// initial biomass.
        /// </summary>
        /// <param name="ageCohorts">
        /// A sorted list of age cohorts, from oldest to youngest.
        /// </param>
        /// <param name="site">
        /// Site where cohorts are located.
        /// </param>
        /// <param name="initialBiomassMethod">
        /// The method for computing the initial biomass for a new cohort.
        /// </param>
        public static SiteCohorts MakeBiomassCohorts(List<AgeCohort.ICohort> ageCohorts,
            ActiveSite              site,
            ComputeMethod           initialBiomassMethod)
        {
            SiteCohorts biomassCohorts = new SiteCohorts();
            if (ageCohorts.Count == 0)
                return biomassCohorts;

            int indexNextAgeCohort = 0;
                //  The index in the list of sorted age cohorts of the next
                //  cohort to be considered

            //  Loop through time from -N to 0 where N is the oldest cohort.
            //  So we're going from the time when the oldest cohort was "born"
            //  to the present time (= 0).  Because the age of any age cohort
            //  is a multiple of the succession timestep, we go from -N to 0
            //  by that timestep.  NOTE: the case where timestep = 1 requires
            //  special treatment because if we start at time = -N with a
            //  cohort with age = 1, then at time = 0, its age will N+1 not N.
            //  Therefore, when timestep = 1, the ending time is -1.
            int endTime = (successionTimestep == 1) ? -1 : 0;
            for (int time = -(ageCohorts[0].Age); time <= endTime; time += successionTimestep) {
                //  Grow current biomass cohorts.
                Util.GrowCohorts(biomassCohorts, site, successionTimestep, true);

                //  Add those cohorts that were born at the current year
                while (indexNextAgeCohort < ageCohorts.Count &&
                       ageCohorts[indexNextAgeCohort].Age == -time) {
                    ushort initialBiomass = initialBiomassMethod(biomassCohorts,
                                                                 site,
                                                                 ageCohorts[indexNextAgeCohort].Species);
                    biomassCohorts.AddNewCohort(ageCohorts[indexNextAgeCohort].Species,
                                                initialBiomass);
                    indexNextAgeCohort++;
                }
            }

            return biomassCohorts;
        }
        public void CohortsRemoved()
        {
            SiteCohorts cohorts = new SiteCohorts();
            const ushort initialBiomass = 40;

            mockCalculator.CountCalled = 0;
            mockCalculator.Change = 1;

            int timeOfLastSucc = 0;
            for (int time = 1; time <= 50; time++) {
                //  Simulate the site being disturbed every 8 years which
                //  results in a new cohort being added.
                bool siteDisturbed = (time % 8 == 0);

                bool isSuccTimestep = (time % successionTimestep == 0);

                if (siteDisturbed || isSuccTimestep) {
                    cohorts.Grow((ushort) (time - timeOfLastSucc), activeSite,
                                 isSuccTimestep);
                    timeOfLastSucc = time;
                }

                if (siteDisturbed)
                    cohorts.AddNewCohort(poputrem, initialBiomass);
            }

            //  Expected sequence of cohort changes:
            //
            //        Time  Grow_________
            //        Last         Cohorts               New
            //  Time  Succ  years  afterwards            Cohort
            //  ----  ----  -----  ---------------       ------
            //    8     0     8                           1(40)
            //   16     8     8    9(48)                  1(40)
            //   20    16     4    20(95*)                       * = 48+3 + 40+3 + 1
            //   24    20     4    24(99)                 1(40)
            //   32    24     8    32(107),9(48)          1(40)
            //   40    32     8    40(115),20(103*)       1(40)  * = 48+7 + 40+7 + 1
            //   48    40     8    48(123),28(111),9(48)  1(40)

            expectedCohorts.Clear();
            expectedCohorts[poputrem] = new ushort[] {
                //  age  biomass
                    48,    123,
                    28,    111,
                     9,     48,
                     1,     40
            };
            Util.CheckCohorts(expectedCohorts, cohorts);

            //  Remove cohorts whose ages are between 10 and 30
            expectedSite = null;
            deadCohorts.Clear();
            cohorts.Remove(AgeBetween5And30);

            expectedCohorts.Clear();
            expectedCohorts[poputrem] = new ushort[] {
                //  age  biomass
                    48,    123,
                     1,     40
            };
            Util.CheckCohorts(expectedCohorts, cohorts);

            Assert.AreEqual(2, deadCohorts.Count);
            ushort[] cohortData = new ushort[] {
                //  age  biomass (in young to old order because Remove goes
                //                from back to front)
                     9,     48,
                    28,    111
            };
            for (int i = 0; i < deadCohorts.Count; i++) {
                ICohort deadCohort = deadCohorts[i];
                Assert.AreEqual(poputrem, deadCohort.Species);
                Assert.AreEqual(cohortData[i*2], deadCohort.Age);
                Assert.AreEqual(cohortData[i*2+1], deadCohort.Biomass);
            }
        }
        public void SingleCohort_ZeroBiomass()
        {
            SiteCohorts cohorts = new SiteCohorts();
            const ushort initialBiomass = 100;
            cohorts.AddNewCohort(poputrem, initialBiomass);

            mockCalculator.CountCalled = 0;
            mockCalculator.Change = -2;

            expectedSender = cohorts[poputrem];
            expectedDistType = null;  // death during growth phase
            expectedSite = activeSite;
            deadCohorts.Clear();

            for (int time = 1; time <= 60; time++) {
                if (time % successionTimestep == 0)
                    Util.Grow(cohorts, successionTimestep, activeSite, true);
            }

            expectedCohorts.Clear();
            Util.CheckCohorts(expectedCohorts, cohorts);

            Assert.AreEqual(1, deadCohorts.Count);
            ICohort deadCohort = deadCohorts[0];
            Assert.AreEqual(poputrem, deadCohort.Species);
            Assert.AreEqual(initialBiomass / -mockCalculator.Change, deadCohort.Age);
            Assert.AreEqual(0, deadCohort.Biomass);
        }
        public void Clone()
        {
            SiteCohorts cohorts = new SiteCohorts();
            const ushort initialBiomass = 55;
            cohorts.AddNewCohort(abiebals, initialBiomass);

            mockCalculator.CountCalled = 0;
            mockCalculator.Change = 1;

            for (int time = successionTimestep; time <= 70; time += successionTimestep) {
                Util.Grow(cohorts, successionTimestep, activeSite, true);
                if (time % 20 == 0)
                    cohorts.AddNewCohort(abiebals, initialBiomass);
                if (time % 30 == 0)
                    cohorts.AddNewCohort(betualle, initialBiomass);
            }

            //  Expected cohort changes:
            //
            //  Time  Cohorts
            //  ----  -------
            //    0   abiebals 1(55)
            //   10   abiebals 10(65)
            //   20   abiebals 20(75) 1(55)
            //   30   abiebals 30(85) 10(65)
            //        betualle 1(55)
            //   40   abiebals 40(95) 20(75) 1(55)
            //        betualle 10(65)
            //   50   abiebals 50(105) 30(85) 10(65)
            //        betualle 20(75)
            //   60   abiebals 60(115) 40(95) 20(75) 1(55)
            //        betualle 30(85) 1(55)
            //   70   abiebals 70(125) 50(105) 30(85) 10(65)
            //        betualle 40(95) 10(65)
            expectedCohorts.Clear();
		    expectedCohorts[abiebals] = new ushort[] {
		        //  age  biomass
		            70,    125,
		            50,    105,
		            30,     85,
		            10,     65
		    };
		    expectedCohorts[betualle] = new ushort[] {
		        //  age  biomass
		            40,     95,
		            10,     65
		    };
		    Util.CheckCohorts(expectedCohorts, cohorts);

		    SiteCohorts clone = cohorts.Clone();
		    Util.CheckCohorts(expectedCohorts, clone);

		    //  Modify the original set of cohorts by growing them for 2 more
		    //  succession timesteps.  Check that clone doesn't change.
            for (int time = 80; time <= 90; time += successionTimestep) {
                Util.Grow(cohorts, successionTimestep, activeSite, true);
		    }
		    Util.CheckCohorts(expectedCohorts, clone);

            expectedCohorts.Clear();
		    expectedCohorts[abiebals] = new ushort[] {
		        //  age  biomass
		            90,    145,
		            70,    125,
		            50,    105,
		            30,     85
		    };
		    expectedCohorts[betualle] = new ushort[] {
		        //  age  biomass
		            60,    115,
		            30,     85
		    };
		    Util.CheckCohorts(expectedCohorts, cohorts);
        }
 //---------------------------------------------------------------------
 public static ISiteCohorts Clone(ActiveSite site, ISiteCohorts site_cohorts)
 {
     ISiteCohorts clone = new SiteCohorts();
      foreach (ISpeciesCohorts speciesCohorts in site_cohorts)
          foreach (Cohort cohort in speciesCohorts)
          {
              clone.AddNewCohort(cohort);
          }
      return clone;
 }
 //---------------------------------------------------------------------
 public static SiteCohorts Clone(SiteCohorts site_cohorts)
 {
     SiteCohorts clone = new SiteCohorts();
      foreach (ISpeciesCohorts speciesCohorts in site_cohorts)
          foreach (ICohort cohort in speciesCohorts)
              //clone.AddNewCohort(cohort.Species, cohort.Age, cohort.Biomass);
              clone.AddNewCohort(cohort.Species, cohort.Age, cohort.Biomass, cohort.ANPP);
         //species.cohorts.Add(speciesCohorts.Clone());
      return clone;
 }
        public void Clone()
        {
            SiteCohorts  cohorts        = new SiteCohorts();
            const ushort initialBiomass = 55;

            cohorts.AddNewCohort(abiebals, initialBiomass);

            mockCalculator.CountCalled = 0;
            mockCalculator.Change      = 1;

            for (int time = successionTimestep; time <= 70; time += successionTimestep)
            {
                Util.Grow(cohorts, successionTimestep, activeSite, true);
                if (time % 20 == 0)
                {
                    cohorts.AddNewCohort(abiebals, initialBiomass);
                }
                if (time % 30 == 0)
                {
                    cohorts.AddNewCohort(betualle, initialBiomass);
                }
            }

            //  Expected cohort changes:
            //
            //  Time  Cohorts
            //  ----  -------
            //    0   abiebals 1(55)
            //   10   abiebals 10(65)
            //   20   abiebals 20(75) 1(55)
            //   30   abiebals 30(85) 10(65)
            //        betualle 1(55)
            //   40   abiebals 40(95) 20(75) 1(55)
            //        betualle 10(65)
            //   50   abiebals 50(105) 30(85) 10(65)
            //        betualle 20(75)
            //   60   abiebals 60(115) 40(95) 20(75) 1(55)
            //        betualle 30(85) 1(55)
            //   70   abiebals 70(125) 50(105) 30(85) 10(65)
            //        betualle 40(95) 10(65)
            expectedCohorts.Clear();
            expectedCohorts[abiebals] = new ushort[] {
                //  age  biomass
                70, 125,
                50, 105,
                30, 85,
                10, 65
            };
            expectedCohorts[betualle] = new ushort[] {
                //  age  biomass
                40, 95,
                10, 65
            };
            Util.CheckCohorts(expectedCohorts, cohorts);

            SiteCohorts clone = cohorts.Clone();

            Util.CheckCohorts(expectedCohorts, clone);

            //  Modify the original set of cohorts by growing them for 2 more
            //  succession timesteps.  Check that clone doesn't change.
            for (int time = 80; time <= 90; time += successionTimestep)
            {
                Util.Grow(cohorts, successionTimestep, activeSite, true);
            }
            Util.CheckCohorts(expectedCohorts, clone);

            expectedCohorts.Clear();
            expectedCohorts[abiebals] = new ushort[] {
                //  age  biomass
                90, 145,
                70, 125,
                50, 105,
                30, 85
            };
            expectedCohorts[betualle] = new ushort[] {
                //  age  biomass
                60, 115,
                30, 85
            };
            Util.CheckCohorts(expectedCohorts, cohorts);
        }
 //---------------------------------------------------------------------
 public static ISiteCohorts Clone(ISiteCohorts site_cohorts)
 {
     ISiteCohorts clone = new SiteCohorts();
     foreach (ISpeciesCohorts speciesCohorts in site_cohorts)
         foreach (ICohort cohort in speciesCohorts)
             clone.AddNewCohort(cohort.Species, cohort.Age, cohort.WoodBiomass, cohort.LeafBiomass);  //species.cohorts.Add(speciesCohorts.Clone());
     return clone;
 }
        public void CohortsRemoved()
        {
            SiteCohorts  cohorts        = new SiteCohorts();
            const ushort initialBiomass = 40;

            mockCalculator.CountCalled = 0;
            mockCalculator.Change      = 1;

            int timeOfLastSucc = 0;

            for (int time = 1; time <= 50; time++)
            {
                //  Simulate the site being disturbed every 8 years which
                //  results in a new cohort being added.
                bool siteDisturbed = (time % 8 == 0);

                bool isSuccTimestep = (time % successionTimestep == 0);

                if (siteDisturbed || isSuccTimestep)
                {
                    Util.Grow(cohorts, (ushort)(time - timeOfLastSucc),
                              activeSite, isSuccTimestep);
                    timeOfLastSucc = time;
                }

                if (siteDisturbed)
                {
                    cohorts.AddNewCohort(poputrem, initialBiomass);
                }
            }

            //  Expected sequence of cohort changes:
            //
            //        Time  Grow_________
            //        Last         Cohorts               New
            //  Time  Succ  years  afterwards            Cohort
            //  ----  ----  -----  ---------------       ------
            //    8     0     8                           1(40)
            //   16     8     8    9(48)                  1(40)
            //   20    16     4    20(95*)                       * = 48+3 + 40+3 + 1
            //   24    20     4    24(99)                 1(40)
            //   32    24     8    32(107),9(48)          1(40)
            //   40    32     8    40(115),20(103*)       1(40)  * = 48+7 + 40+7 + 1
            //   48    40     8    48(123),28(111),9(48)  1(40)

            expectedCohorts.Clear();
            expectedCohorts[poputrem] = new ushort[] {
                //  age  biomass
                48, 123,
                28, 111,
                9, 48,
                1, 40
            };
            Util.CheckCohorts(expectedCohorts, cohorts);

            //  Remove cohorts whose ages are between 10 and 30
            expectedSite = null;
            deadCohorts.Clear();
            cohorts.DamageBy(new RemoveAgeBetween5And30(expectedSite));

            expectedCohorts.Clear();
            expectedCohorts[poputrem] = new ushort[] {
                //  age  biomass
                48, 123,
                1, 40
            };
            Util.CheckCohorts(expectedCohorts, cohorts);

            Assert.AreEqual(2, deadCohorts.Count);
            ushort[] cohortData = new ushort[] {
                //  age  biomass (in young to old order because Remove goes
                //                from back to front)
                9, 48,
                28, 111
            };
            for (int i = 0; i < deadCohorts.Count; i++)
            {
                ICohort deadCohort = deadCohorts[i];
                Assert.AreEqual(poputrem, deadCohort.Species);
                Assert.AreEqual(cohortData[i * 2], deadCohort.Age);
                Assert.AreEqual(cohortData[i * 2 + 1], deadCohort.Biomass);
            }
        }
        //---------------------------------------------------------------------

        private void CohortsRemoved(object disturbance)
        {
            SiteCohorts cohorts = new SiteCohorts();
            const ushort initialBiomass = 40;

            mockCalculator.CountCalled = 0;
            mockCalculator.Change = 1;

            int timeOfLastSucc = 0;
            for (int time = 1; time <= 50; time++) {
                //  Simulate the site being disturbed every 8 years which
                //  results in a new cohort being added.
                bool siteDisturbed = (time % 8 == 0);

                bool isSuccTimestep = (time % successionTimestep == 0);

                if (siteDisturbed || isSuccTimestep) {
                    Util.Grow(cohorts, (ushort) (time - timeOfLastSucc),
                              activeSite, isSuccTimestep);
                    timeOfLastSucc = time;
                }

                if (siteDisturbed)
                    cohorts.AddNewCohort(poputrem, initialBiomass);
            }

            //  Expected sequence of cohort changes:
            //
            //        Time  Grow_________
            //        Last         Cohorts               New
            //  Time  Succ  years  afterwards            Cohort
            //  ----  ----  -----  ---------------       ------
            //    8     0     8                           1(40)
            //   16     8     8    9(48)                  1(40)
            //   20    16     4    20(95*)                       * = 48+3 + 40+3 + 1
            //   24    20     4    24(99)                 1(40)
            //   32    24     8    32(107),9(48)          1(40)
            //   40    32     8    40(115),20(103*)       1(40)  * = 48+7 + 40+7 + 1
            //   48    40     8    48(123),28(111),9(48)  1(40)

            expectedCohorts.Clear();
            expectedCohorts[poputrem] = new ushort[] {
                //  age  biomass
                    48,    123,
                    28,    111,
                     9,     48,
                     1,     40
            };
            Util.CheckCohorts(expectedCohorts, cohorts);

            //  Remove cohorts whose ages are between 10 and 30
            expectedSender = cohorts[poputrem];
            deadCohorts.Clear();
            if (disturbance is IDisturbance)
                cohorts.DamageBy((IDisturbance) disturbance);
            else if (disturbance is AgeCohort.IDisturbance)
                ((AgeCohort.ISiteCohorts) cohorts).DamageBy((AgeCohort.IDisturbance) disturbance);

            expectedCohorts.Clear();
            expectedCohorts[poputrem] = new ushort[] {
                //  age  biomass
                    48,    123,
                     1,     40
            };
            Util.CheckCohorts(expectedCohorts, cohorts);
            
            CheckDeadCohorts(deadCohorts);
        }
        //---------------------------------------------------------------------
        /// <summary>
        /// Makes the set of biomass cohorts at a site based on the age cohorts
        ///   at the site, using a specified method for computing a cohort's
        ///   initial biomass.
        /// </summary>
        /// <param name="ageCohorts">
        /// A sorted list of age cohorts, from oldest to youngest.
        /// </param>
        /// <param name="site">
        /// Site where cohorts are located.
        /// </param>
        /// <param name="initialBiomassMethod">
        /// The method for computing the initial biomass for a new cohort.
        /// </param>
        public static SiteCohorts MakeBiomassCohorts(List<AgeCohort.ICohort> ageCohorts,
                                                     ActiveSite site,
                                                     ComputeMethod initialBiomassMethod)
        {
            // Fix to keep initial mineral N and P as paramterized for year 1.
            double minN = SiteVars.MineralSoil[site].ContentN;
            double minP = SiteVars.MineralSoil[site].ContentP;

            SiteCohorts biomassCohorts = new SiteCohorts();
            if (ageCohorts.Count == 0)
                return biomassCohorts;

            int indexNextAgeCohort = 0;
            //The index in the list of sorted age cohorts of the next
            //  cohort to be considered.

            //Loop through time from -N to 0 where N is the oldest cohort.
            //  So we're going from the time when the oldest cohort was "born"
            //  to the present time (= 0).  Because the age of any age cohort
            //  is a multiple of the succession timestep, we go from -N to 0
            //  by that timestep.  NOTE: the case where timestep = 1 requires
            //  special treatment because if we start at time = -N with a
            //  cohort with age = 1, then at time = 0, its age will N+1 not N.
            //  Therefore, when timestep = 1, the ending time is -1.
            int endTime = (successionTimestep == 1) ? -1 : 0;
            for (int time = -(ageCohorts[0].Age); time <= endTime; time += successionTimestep)
            {
                //Grow current biomass cohorts.
                NutrientSuccession.InitGrowCohorts(biomassCohorts, site, successionTimestep, true);

                //Add those cohorts that were born at the current year
                while (indexNextAgeCohort < ageCohorts.Count &&
                       ageCohorts[indexNextAgeCohort].Age == -time)
                {
                    float[] initialBiomass = initialBiomassMethod(biomassCohorts,
                                                                 site, ageCohorts[indexNextAgeCohort].Species);
                    float initialWoodBiomass = initialBiomass[0];
                    float initialLeafBiomass = initialBiomass[1];

                    biomassCohorts.AddNewCohort(ageCohorts[indexNextAgeCohort].Species,
                        initialWoodBiomass, initialLeafBiomass);
                    indexNextAgeCohort++;
                }
            }

            // Reset mineral nutrients
            SiteVars.MineralSoil[site].ContentN = minN;
            SiteVars.MineralSoil[site].ContentP = minP;

            return biomassCohorts;
        }