コード例 #1
0
ファイル: LifeCyclePhase.cs プロジェクト: lie112/ApsimX
        /// <summary>Loop through each cohort in this LifeCyclePhase to calculate development, mortality, graduation and reproduciton</summary>
        public void Process()
        {
            if (Cohorts?.Count > 13000)  //Check cohort number are not becomming silly
            {
                throw new Exception(FullPath + " has over 1500 cohorts which is to many really.  This is why your simulation is slow and the data store is about to chuck it in.  Check your " + this.Parent.Name.ToString() + " model to ensure development and mortality are sufficient.");
            }

            ZeorDeltas(); //Zero reporting properties for daily summing

            if (Cohorts != null)
            {
                // Calculate daily deltas
                foreach (Cohort c in Cohorts)
                {
                    CurrentCohort       = c;
                    c.ChronologicalAge += 1;
                    //Do development for each cohort
                    c.PhysiologicalAge = Math.Min(1.0, c.PhysiologicalAge + development.Value());
                    //Do mortality for each cohort
                    c.Mortalities = mortality.Value();
                    Mortalities  += c.Mortalities;
                    c.Population  = Math.Max(0.0, c.Population - c.Mortalities);
                    //Do reproduction for each cohort
                    c.Progeny = reproduction.Value();
                    Progeny  += c.Progeny;
                    //Do migration for each cohort
                    c.Emigrants  = migration.Value();
                    Emigrants   += c.Emigrants;
                    c.Population = Math.Max(0.0, c.Population - c.Emigrants);
                }

                //Add Migrants into destination phase
                if (Emigrants > 0)
                {
                    double SumMigrantAge  = 0;
                    double SumMigrantPAge = 0;

                    foreach (Cohort c in Cohorts)
                    {
                        if (c.Emigrants > 0)
                        {
                            SumMigrantAge  += c.Emigrants * c.ChronologicalAge;
                            SumMigrantPAge += c.Emigrants * c.PhysiologicalAge;
                        }
                    }
                    double MeanMigrantAge  = SumMigrantAge / Emigrants;
                    double MeanMigrantPAge = SumMigrantPAge / Emigrants;
                    if (MigrantDestinations.Count == 0)
                    {
                        throw new Exception(FullPath + " is predicting values for migration but has no MigrationDestinationPhase specified");
                    }
                    double MtotalProportion = 0;
                    foreach (MigrantDestinationPhase mdest in MigrantDestinations)
                    {
                        double destEmigrants = Emigrants * mdest.ProportionOfMigrants;
                        if ((MigrantDestinations.Count == 0) && (destEmigrants > 0))
                        {
                            throw new Exception(FullPath + " is predicting values for migration but has not MigrantDestinationPhase specified");
                        }
                        if (destEmigrants > 0)
                        {
                            IModel    zone = Parent.FindAncestor <Zone>();
                            LifeCycle mDestinationCycle = zone.FindInScope <LifeCycle>(mdest.NameOfLifeCycleForMigrants);
                            if (mDestinationCycle == null)
                            {
                                throw new Exception(FullPath + " could not find a destination LifeCycle for migrants called " + mdest.NameOfLifeCycleForMigrants);
                            }
                            LifeCyclePhase mDestinationPhase = mDestinationCycle.FindChild <LifeCyclePhase>(mdest.NameOfPhaseForMigrants);
                            if (mDestinationPhase == null)
                            {
                                throw new Exception(FullPath + " could not find a destination LifeCyclePhase for migrants called " + mdest.NameOfPhaseForMigrants);
                            }

                            SourceInfo ImigrantInfo = new SourceInfo();
                            ImigrantInfo.LifeCycle        = Parent.Name;
                            ImigrantInfo.LifeCyclePhase   = this.Name;
                            ImigrantInfo.Type             = SourceInfo.TypeOptions.Reproduction;
                            ImigrantInfo.Population       = Emigrants;
                            ImigrantInfo.ChronologicalAge = MeanMigrantAge;
                            ImigrantInfo.PhysiologicalAge = MeanMigrantPAge;
                            mDestinationPhase?.NewCohort(ImigrantInfo);
                            MtotalProportion += mdest.ProportionOfMigrants;
                        }
                    }
                    if ((MtotalProportion > 1.001) && (Emigrants > 0))
                    {
                        throw new Exception("The sum of ProportionOfMigrants values in " + FullPath + " ProgenyDestinationPhases is greater than 1.0");
                    }
                }

                // Add progeny into destination phases
                if (Progeny > 0)
                {
                    if ((ProgenyDestinations.Count == 0) && (Progeny > 0))
                    {
                        throw new Exception(FullPath + " is predicting values for reproduction but has no ProgenyDestinationPhase specified");
                    }
                    double PtotalProportion = 0;
                    foreach (ProgenyDestinationPhase pdest in ProgenyDestinations)
                    {
                        double arrivals = Progeny * pdest.ProportionOfProgeny;

                        if (arrivals > 0)
                        {
                            IModel    zone = Parent.FindAncestor <Zone>();
                            LifeCycle pDestinationCylce = zone.FindInScope <LifeCycle>(pdest.NameOfLifeCycleForProgeny);
                            if (pDestinationCylce == null)
                            {
                                throw new Exception(FullPath + " could not find a destination LifeCycle for progeny called " + pdest.NameOfLifeCycleForProgeny);
                            }
                            LifeCyclePhase pDestinationPhase = pDestinationCylce.FindChild <LifeCyclePhase>(pdest.NameOfPhaseForProgeny);
                            if (pDestinationPhase == null)
                            {
                                throw new Exception(FullPath + " could not find a destination LifeCyclePhase for progeny called " + pdest.NameOfPhaseForProgeny);
                            }

                            SourceInfo ArivalsInfo = new SourceInfo();
                            ArivalsInfo.LifeCycle        = Parent.Name;
                            ArivalsInfo.LifeCyclePhase   = this.Name;
                            ArivalsInfo.Type             = SourceInfo.TypeOptions.Reproduction;
                            ArivalsInfo.Population       = arrivals;
                            ArivalsInfo.ChronologicalAge = 0;
                            ArivalsInfo.PhysiologicalAge = 0;
                            pDestinationPhase?.NewCohort(ArivalsInfo);
                            PtotalProportion += pdest.ProportionOfProgeny;
                        }
                    }
                    if (((PtotalProportion < 0.999) || (PtotalProportion > 1.001)) && (Progeny > 0))
                    {
                        throw new Exception("The sum of ProportionOfProgeny values in " + FullPath + " ProgenyDestinationPhases does not equal 1.0");
                    }
                }

                // Move garduates to destination phase
                foreach (Cohort c in Cohorts.ToArray())
                {
                    if (c.PhysiologicalAge >= 1.0) //Members ready to graduate or die
                    {
                        if (LifeCyclePhaseForGraduates != null)
                        {
                            Graduates += c.Population; //Members graduate
                        }
                        else
                        {
                            Mortalities += c.Population; //Members die
                        }
                        Cohorts.Remove(c);               //Remove mature cohort
                    }

                    if (c.Population < 0.001)  //Remove cohort if all members dead
                    {
                        Cohorts.Remove(c);
                    }
                }

                if (Graduates > 0)  //Promote graduates to cohort in next LifeCyclePhase
                {
                    SourceInfo GraduateInfo = new SourceInfo();
                    GraduateInfo.LifeCycle        = Parent.Name;
                    GraduateInfo.LifeCyclePhase   = this.Name;
                    GraduateInfo.Type             = SourceInfo.TypeOptions.Graduation;
                    GraduateInfo.Population       = Graduates;
                    GraduateInfo.ChronologicalAge = 0;
                    GraduateInfo.PhysiologicalAge = 0;
                    LifeCyclePhaseForGraduates?.NewCohort(GraduateInfo);
                }
            }
        }
コード例 #2
0
        /// <summary>Loop through each cohort in this LifeCyclePhase to calculate development, mortality, graduation and reproduciton</summary>
        public void Process()
        {
            if (Cohorts?.Count > 500)  //Check cohort number are not becomming silly
            {
                throw new Exception(Apsim.FullPath(this) + " has over 500 cohorts which is to many really.  This is why your simulation is slow and the data store is about to chuck it in.  Check your " + this.Parent.Name.ToString() + " model to ensure development and mortality are sufficient.");
            }

            ZeorDeltas(); //Zero reporting properties for daily summing

            foreach (ProgenyDestinationPhase pdest in ProgenyDestinations)
            {
                pdest.ProgenyToDestination = 0;
            }

            if (Cohorts != null)
            {
                // Calculate daily deltas
                foreach (Cohort c in Cohorts)
                {
                    CurrentCohort       = c;
                    c.ChronologicalAge += 1;
                    //Do development for each cohort
                    c.PhysiologicalAge = Math.Min(1.0, c.PhysiologicalAge + development.Value());
                    //Do mortality for each cohort
                    c.Mortalities = mortality.Value();
                    Mortalities  += c.Mortalities;
                    c.Population  = Math.Max(0.0, c.Population - c.Mortalities);
                    //Do reproduction for each cohort
                    Progeny += reproduction.Value();
                    foreach (ProgenyDestinationPhase dest in ProgenyDestinations)
                    {
                        dest.ProgenyToDestination += reproduction.Value() * dest.ProportionOfProgeny;
                    }
                    //Do migration for each cohort
                    Migrants += migration.Value();
                    if (Migrants > 0)
                    {
                        if (MigrantDestinations.Count == 0)
                        {
                            throw new Exception(Apsim.FullPath(this) + " is predicting values for migration but has no MigrationDestinationPhase specified");
                        }
                        double MtotalProportion = 0;
                        foreach (MigrantDestinationPhase mdest in MigrantDestinations)
                        {
                            double cohortMigrants = migration.Value() * mdest.ProportionOfMigrants;
                            if ((MigrantDestinations.Count == 0) && (cohortMigrants > 0))
                            {
                                throw new Exception(Apsim.FullPath(this) + " is predicting values for migration but has not MigrantDestinationPhase specified");
                            }
                            if (cohortMigrants > 0)
                            {
                                IModel    zone = Apsim.Parent(this.Parent, typeof(Zone));
                                LifeCycle mDestinationCycle = Apsim.Find(zone, mdest.NameOfLifeCycleForMigrants) as LifeCycle;
                                if (mDestinationCycle == null)
                                {
                                    throw new Exception(Apsim.FullPath(this) + " could not find a destination LifeCycle for migrants called " + mdest.NameOfLifeCycleForMigrants);
                                }
                                LifeCyclePhase mDestinationPhase = Apsim.Child(mDestinationCycle, mdest.NameOfPhaseForMigrants) as LifeCyclePhase;
                                if (mDestinationPhase == null)
                                {
                                    throw new Exception(Apsim.FullPath(this) + " could not find a destination LifeCyclePhase for migrants called " + mdest.NameOfPhaseForMigrants);
                                }
                                mDestinationPhase.NewCohort(cohortMigrants, c.ChronologicalAge, c.PhysiologicalAge);
                                MtotalProportion += mdest.ProportionOfMigrants;
                            }
                        }
                        if ((MtotalProportion > 1.001) && (Migrants > 0))
                        {
                            throw new Exception("The sum of ProportionOfMigrants values in " + Apsim.FullPath(this) + " ProgenyDestinationPhases is greater than 1.0");
                        }
                    }
                }

                // Add progeny into destination phase
                if ((ProgenyDestinations.Count == 0) && (Progeny > 0))
                {
                    throw new Exception(Apsim.FullPath(this) + " is predicting values for reproduction but has no ProgenyDestinationPhase specified");
                }
                double PtotalProportion = 0;
                foreach (ProgenyDestinationPhase pdest in ProgenyDestinations)
                {
                    if (pdest.ProgenyToDestination > 0)
                    {
                        IModel    zone = Apsim.Parent(this.Parent, typeof(Zone));
                        LifeCycle pDestinationCylce = Apsim.Find(zone, pdest.NameOfLifeCycleForProgeny) as LifeCycle;
                        if (pDestinationCylce == null)
                        {
                            throw new Exception(Apsim.FullPath(this) + " could not find a destination LifeCycle for progeny called " + pdest.NameOfLifeCycleForProgeny);
                        }
                        LifeCyclePhase pDestinationPhase = Apsim.Child(pDestinationCylce, pdest.NameOfPhaseForProgeny) as LifeCyclePhase;
                        if (pDestinationPhase == null)
                        {
                            throw new Exception(Apsim.FullPath(this) + " could not find a destination LifeCyclePhase for progeny called " + pdest.NameOfPhaseForProgeny);
                        }
                        pDestinationPhase.NewCohort(pdest.ProgenyToDestination, 0, 0);
                        PtotalProportion += pdest.ProportionOfProgeny;
                    }
                }
                if (((PtotalProportion < 0.999) || (PtotalProportion > 1.001)) && (Progeny > 0))
                {
                    throw new Exception("The sum of ProportionOfProgeny values in " + Apsim.FullPath(this) + " ProgenyDestinationPhases does not equal 1.0");
                }


                // Move garduates to destination phase
                foreach (Cohort c in Cohorts.ToArray())
                {
                    if (c.PhysiologicalAge >= 1.0) //Members ready to graduate or die
                    {
                        if (LifeCyclePhaseForGraduates != null)
                        {
                            Graduates += c.Population; //Members graduate
                        }
                        else
                        {
                            Mortalities += c.Population; //Members die
                        }
                        Cohorts.Remove(c);               //Remove mature cohort
                    }

                    if (c.Population < 0.001)  //Remove cohort if all members dead
                    {
                        Cohorts.Remove(c);
                    }
                }

                if (Graduates > 0)  //Promote graduates to cohort in next LifeCyclePhase
                {
                    LifeCyclePhaseForGraduates?.NewCohort(Graduates, 0.0, 0.0);
                }
            }
        }