//---------------------------------------------------------------------

        public override void Initialize()
        {
            PlugIn.ModelCore.UI.WriteLine("Initializing {0} ...", ExtensionName);
            Timestep            = parameters.Timestep;
            SuccessionTimeStep  = Timestep;
            sufficientLight     = parameters.LightClassProbabilities;
            ProbEstablishAdjust = parameters.ProbEstablishAdjustment;
            MetadataHandler.InitializeMetadata(Timestep, modelCore, SoilCarbonMapNames, SoilNitrogenMapNames, ANPPMapNames, ANEEMapNames, TotalCMapNames);
            CohortBiomass.SpinupMortalityFraction = parameters.SpinupMortalityFraction;

            //Initialize climate.
            Climate.Initialize(parameters.ClimateConfigFile, false, modelCore);
            FutureClimateBaseYear = Climate.Future_MonthlyData.Keys.Min();

            EcoregionData.Initialize(parameters);
            SpeciesData.Initialize(parameters);
            EcoregionData.ChangeParameters(parameters);

            OtherData.Initialize(parameters);
            FunctionalType.Initialize(parameters);

            //  Cohorts must be created before the base class is initialized
            //  because the base class' reproduction module uses the core's
            //  SuccessionCohorts property in its Initialization method.
            Library.LeafBiomassCohorts.Cohorts.Initialize(Timestep, new CohortBiomass());

            // Initialize Reproduction routines:
            Reproduction.SufficientResources = SufficientLight;
            Reproduction.Establish           = Establish;
            Reproduction.AddNewCohort        = AddNewCohort;
            Reproduction.MaturePresent       = MaturePresent;
            base.Initialize(modelCore, parameters.SeedAlgorithm);

            InitialBiomass.Initialize(Timestep);

            Landis.Library.BiomassCohorts.Cohort.DeathEvent            += CohortDied;
            Landis.Library.LeafBiomassCohorts.Cohort.PartialDeathEvent += CohortPartialMortality;
            AgeOnlyDisturbances.Module.Initialize(parameters.AgeOnlyDisturbanceParms);

            Dynamic.Module.Initialize(parameters.DynamicUpdates);
            FireEffects.Initialize(parameters);
            InitializeSites(parameters.InitialCommunities, parameters.InitialCommunitiesMap, modelCore); //the spinup is heppend within this fucntion
            if (parameters.CalibrateMode)
            {
                Outputs.CreateCalibrateLogFile();
            }

            Outputs.WritePrimaryLogFile(0);
            Outputs.WriteShortPrimaryLogFile(0);
        }
        /// <summary>
        /// Grows all cohorts at a site for a specified number of years.
        /// Litter is decomposed following growth.
        /// </summary>
        public static ISiteCohorts Run(ActiveSite site,
                                       int years,
                                       bool isSuccessionTimeStep)
        {
            ISiteCohorts siteCohorts = SiteVars.Cohorts[site];
            IEcoregion   ecoregion   = PlugIn.ModelCore.Ecoregion[site];

            for (int y = 0; y < years; ++y)
            {
                Year = y + 1;

                if (PlugIn.ModelCore.CurrentTime > 0 && Climate.Future_MonthlyData.ContainsKey(PlugIn.FutureClimateBaseYear + y + PlugIn.ModelCore.CurrentTime - years))
                {
                    EcoregionData.AnnualWeather[ecoregion] = Climate.Future_MonthlyData[PlugIn.FutureClimateBaseYear + y - years + PlugIn.ModelCore.CurrentTime][ecoregion.Index];
                }

                SiteVars.ResetAnnualValues(site);

                if (y == 0 && SiteVars.FireSeverity != null && SiteVars.FireSeverity[site] > 0)
                {
                    FireEffects.ReduceLayers(SiteVars.FireSeverity[site], site);
                }

                // Next, Grow and Decompose each month
                int[] months = new int[12] {
                    6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5
                };

                if (OtherData.CalibrateMode)
                {
                    months = new int[12] {
                        6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5
                    }
                }
                ;

                for (MonthCnt = 0; MonthCnt < 12; MonthCnt++)
                {
                    // Calculate mineral N fractions based on coarse root biomass
                    if (MonthCnt == 0)
                    {
                        AvailableN.CalculateMineralNfraction(site);
                    }
                    //PlugIn.ModelCore.UI.WriteLine("SiteVars.MineralN = {0:0.00}, month = {1}.", SiteVars.MineralN[site], i);

                    Month = months[MonthCnt];

                    SiteVars.MonthlyAGNPPcarbon[site][Month] = 0.0;
                    SiteVars.MonthlyBGNPPcarbon[site][Month] = 0.0;
                    SiteVars.MonthlyNEE[site][Month]         = 0.0;
                    SiteVars.MonthlyResp[site][Month]        = 0.0;
                    SiteVars.MonthlyStreamN[site][Month]     = 0.0;
                    SiteVars.SourceSink[site].Carbon         = 0.0;
                    SiteVars.TotalWoodBiomass[site]          = Main.ComputeWoodBiomass((ActiveSite)site);
                    //SiteVars.LAI[site] = Main.ComputeLAI((ActiveSite)site);

                    double ppt = EcoregionData.AnnualWeather[ecoregion].MonthlyPrecip[Main.Month];

                    double monthlyNdeposition;
                    if (EcoregionData.AtmosNintercept[ecoregion] != -1 && EcoregionData.AtmosNslope[ecoregion] != -1)
                    {
                        monthlyNdeposition = EcoregionData.AtmosNintercept[ecoregion] + (EcoregionData.AtmosNslope[ecoregion] * ppt);
                    }
                    else
                    {
                        monthlyNdeposition = EcoregionData.AnnualWeather[ecoregion].MonthlyNDeposition[Main.Month];
                    }

                    if (monthlyNdeposition < 0)
                    {
                        throw new System.ApplicationException("Error: Nitrogen deposition input data are not present in climate library");
                    }

                    EcoregionData.MonthlyNDeposition[ecoregion][Month] = monthlyNdeposition;
                    EcoregionData.AnnualNDeposition[ecoregion]        += monthlyNdeposition;
                    SiteVars.MineralN[site] += monthlyNdeposition;
                    //PlugIn.ModelCore.UI.WriteLine("Ndeposition={0},MineralN={1:0.00}.", monthlyNdeposition, SiteVars.MineralN[site]);

                    double liveBiomass = (double)ComputeLivingBiomass(siteCohorts);
                    double baseFlow, stormFlow;
                    SoilWater.Run(y, Month, liveBiomass, site, out baseFlow, out stormFlow);

                    // Calculate N allocation for each cohort
                    AvailableN.SetMineralNallocation(site);

                    if (MonthCnt == 11)
                    {
                        siteCohorts.Grow(site, (y == years && isSuccessionTimeStep), true);
                    }
                    else
                    {
                        siteCohorts.Grow(site, (y == years && isSuccessionTimeStep), false);
                    }

                    WoodLayer.Decompose(site);
                    LitterLayer.Decompose(site);
                    SoilLayer.Decompose(site);

                    //...Volatilization loss as a function of the mineral n which
                    //     remains after uptake by plants.  ML added a correction factor for wetlands since their denitrification rate is double that of wetlands
                    //based on a review paper by Seitziner 2006.

                    double volatilize = (SiteVars.MineralN[site] * EcoregionData.Denitrif[ecoregion]); // monthly value

                    //PlugIn.ModelCore.UI.WriteLine("BeforeVol.  MineralN={0:0.00}.", SiteVars.MineralN[site]);

                    SiteVars.MineralN[site]            -= volatilize;
                    SiteVars.SourceSink[site].Nitrogen += volatilize;
                    SiteVars.Nvol[site] += volatilize;

                    SoilWater.Leach(site, baseFlow, stormFlow);

                    SiteVars.MonthlyNEE[site][Month] -= SiteVars.MonthlyAGNPPcarbon[site][Month];
                    SiteVars.MonthlyNEE[site][Month] -= SiteVars.MonthlyBGNPPcarbon[site][Month];
                    SiteVars.MonthlyNEE[site][Month] += SiteVars.SourceSink[site].Carbon;
                }
            }

            ComputeTotalCohortCN(site, siteCohorts);

            return(siteCohorts);
        }
Ejemplo n.º 3
0
        //---------------------------------------------------------------------

        /// <summary>
        /// Computes the change in a cohort's biomass due to Annual Net Primary
        /// Productivity (ANPP), age-related mortality (M_AGE), and development-
        /// related mortality (M_BIO).
        /// </summary>
        public float[] ComputeChange(ICohort cohort, ActiveSite site)
        {
            ecoregion = PlugIn.ModelCore.Ecoregion[site];

            // First call to the Calibrate Log:
            if (PlugIn.ModelCore.CurrentTime > 0 && OtherData.CalibrateMode)
            {
                Outputs.CalibrateLog.Write("{0},{1},{2},{3},{4},{5:0.0},{6:0.0},", PlugIn.ModelCore.CurrentTime, Main.Month + 1, ecoregion.Index, cohort.Species.Name, cohort.Age, cohort.WoodBiomass, cohort.LeafBiomass);
            }


            double siteBiomass = Main.ComputeLivingBiomass(SiteVars.Cohorts[site]);

            if (siteBiomass < 0)
            {
                throw new ApplicationException("Error: Site biomass < 0");
            }

            // ****** Mortality *******
            // Age-related mortality includes woody and standing leaf biomass.
            double[] mortalityAge = ComputeAgeMortality(cohort, site);

            //  Growth-related mortality
            double[] mortalityGrowth = ComputeGrowthMortality(cohort, site);

            double[] totalMortality = new double[2] {
                Math.Min(cohort.WoodBiomass, mortalityAge[0] + mortalityGrowth[0]), Math.Min(cohort.LeafBiomass, mortalityAge[1] + mortalityGrowth[1])
            };
            double nonDisturbanceLeafFall = totalMortality[1];

            // ****** Growth *******
            double[] actualANPP = ComputeActualANPP(cohort, site, siteBiomass, mortalityAge);

            double scorch = 0.0;

            defoliatedLeafBiomass = 0.0;

            if (Main.Month == 6)  //July = 6
            {
                if (SiteVars.FireSeverity != null && SiteVars.FireSeverity[site] > 0)
                {
                    scorch = FireEffects.CrownScorching(cohort, SiteVars.FireSeverity[site]);
                }

                if (scorch > 0.0)  // NEED TO DOUBLE CHECK WHAT CROWN SCORCHING RETURNS
                {
                    totalMortality[1] = Math.Min(cohort.LeafBiomass, scorch + totalMortality[1]);
                }

                // Defoliation (index) ranges from 1.0 (total) to none (0.0).
                if (PlugIn.ModelCore.CurrentTime > 0) //Skip this during initialization
                {
                    int cohortBiomass = (int)(cohort.LeafBiomass + cohort.WoodBiomass);
                    defoliation = Landis.Library.Biomass.CohortDefoliation.Compute(site, cohort.Species, cohortBiomass, (int)siteBiomass);
                }


                if (defoliation > 1.0)
                {
                    defoliation = 1.0;
                }

                if (defoliation > 0.0)
                {
                    defoliatedLeafBiomass = (cohort.LeafBiomass) * defoliation;
                    if (totalMortality[1] + defoliatedLeafBiomass - cohort.LeafBiomass > 0.001)
                    {
                        defoliatedLeafBiomass = cohort.LeafBiomass - totalMortality[1];
                    }
                    //PlugIn.ModelCore.UI.WriteLine("Defoliation.Month={0:0.0}, LeafBiomass={1:0.00}, DefoliatedLeafBiomass={2:0.00}, TotalLeafMort={2:0.00}", Main.Month, cohort.LeafBiomass, defoliatedLeafBiomass , mortalityAge[1]);

                    ForestFloor.AddFrassLitter(defoliatedLeafBiomass, cohort.Species, site);
                }
            }
            else
            {
                defoliation           = 0.0;
                defoliatedLeafBiomass = 0.0;
            }

            // RMS 03/2016: Additional mortality as reaching capacity limit:  SAVE FOR NEXT RELEASE
            //double maxBiomass = SpeciesData.B_MAX_Spp[cohort.Species][ecoregion];
            //double limitCapacity = Math.Min(1.0, Math.Exp(siteBiomass / maxBiomass * 5.0) / Math.Exp(5.0));  // 1.0 = total limit; 0.0 = No limit
            //totalMortality[0] += (actualANPP[0] * limitCapacity); // totalMortality not to exceed ANPP allocation


            if (totalMortality[0] <= 0.0 || cohort.WoodBiomass <= 0.0)
            {
                totalMortality[0] = 0.0;
            }

            if (totalMortality[1] <= 0.0 || cohort.LeafBiomass <= 0.0)
            {
                totalMortality[1] = 0.0;
            }


            if ((totalMortality[0]) > cohort.WoodBiomass)
            {
                PlugIn.ModelCore.UI.WriteLine("Warning: WOOD Mortality exceeds cohort wood biomass. M={0:0.0}, B={1:0.0}", (totalMortality[0]), cohort.WoodBiomass);
                PlugIn.ModelCore.UI.WriteLine("Warning: If M>B, then list mortality. Mage={0:0.0}, Mgrow={1:0.0},", mortalityAge[0], mortalityGrowth[0]);
                throw new ApplicationException("Error: WOOD Mortality exceeds cohort biomass");
            }
            if ((totalMortality[1] + defoliatedLeafBiomass - cohort.LeafBiomass) > 0.01)
            {
                PlugIn.ModelCore.UI.WriteLine("Warning: LEAF Mortality exceeds cohort biomass. Mortality={0:0.000}, Leafbiomass={1:0.000}", (totalMortality[1] + defoliatedLeafBiomass), cohort.LeafBiomass);
                PlugIn.ModelCore.UI.WriteLine("Warning: If M>B, then list mortality. Mage={0:0.00}, Mgrow={1:0.00}, Mdefo={2:0.000},", mortalityAge[1], mortalityGrowth[1], defoliatedLeafBiomass);
                throw new ApplicationException("Error: LEAF Mortality exceeds cohort biomass");
            }
            float deltaWood = (float)(actualANPP[0] - totalMortality[0]);
            float deltaLeaf = (float)(actualANPP[1] - totalMortality[1] - defoliatedLeafBiomass);

            float[] deltas = new float[2] {
                deltaWood, deltaLeaf
            };

            //if((totalMortality[1] + defoliatedLeafBiomass) > cohort.LeafBiomass)
            //   PlugIn.ModelCore.UI.WriteLine("Warning: Leaf Mortality exceeds cohort leaf biomass. M={0:0.0}, B={1:0.0}, DefoLeafBiomass={2:0.0}, defoliationIndex={3:0.0}", totalMortality[1], cohort.LeafBiomass, defoliatedLeafBiomass, defoliation);

            UpdateDeadBiomass(cohort, site, totalMortality);

            CalculateNPPcarbon(site, cohort, actualANPP);

            AvailableN.AdjustAvailableN(cohort, site, actualANPP);

            if (OtherData.CalibrateMode && PlugIn.ModelCore.CurrentTime > 0)
            {
                Outputs.CalibrateLog.WriteLine("{0:0.00},{1:0.00},{2:0.00},{3:0.00},", deltaWood, deltaLeaf, totalMortality[0], totalMortality[1]);
                //Outputs.CalibrateLog.WriteLine("{0:0.00}, {1:0.00}, {2:0.00}", resorbedNused, mineralNused, totalNdemand);
            }

            return(deltas);
        }