Example #1
0
        private void OnSimulationCommencing(object sender, EventArgs e)
        {
            ProportionOfTotalDM = new double[forages.Count];

            if (GrazingRotationType == GrazingRotationTypeEnum.TargetMass)
            {
                if (PreGrazeDMArray == null || PreGrazeDMArray.Length != 12)
                {
                    throw new Exception("There must be 12 values input for the pre-grazing DM");
                }
                if (PostGrazeDMArray == null || PostGrazeDMArray.Length != 12)
                {
                    throw new Exception("There must be 12 values input for the post-grazing DM");
                }
            }
            else if (GrazingRotationType == GrazingRotationTypeEnum.Flexible)
            {
                if (string.IsNullOrEmpty(FlexibleExpressionForTimingOfGrazing))
                {
                    throw new Exception("You must specify an expression for timing of grazing.");
                }
                expressionFunction            = new CSharpExpressionFunction();
                expressionFunction.Parent     = this;
                expressionFunction.Expression = "Convert.ToDouble(" + FlexibleExpressionForTimingOfGrazing + ")";
                expressionFunction.SetCompiler(compiler);
                expressionFunction.CompileExpression();
            }

            if (FractionExcretedNToDung != null && FractionExcretedNToDung.Length != 1 && FractionExcretedNToDung.Length != 12)
            {
                throw new Exception("You must specify either a single value for 'proportion of defoliated nitrogen going to dung' or 12 monthly values.");
            }

            if (SimpleGrazingFrequencyString != null && SimpleGrazingFrequencyString.Equals("end of month", StringComparison.InvariantCultureIgnoreCase))
            {
                simpleGrazingFrequency = 0;
            }
            else
            {
                simpleGrazingFrequency = Convert.ToInt32(SimpleGrazingFrequencyString);
            }

            if (FractionDefoliatedNToSoil == null || FractionDefoliatedNToSoil.Length == 0)
            {
                FractionDefoliatedNToSoil = new double[] { 0 }
            }
            ;

            // Initialise the days since grazing.
            if (GrazingRotationType == GrazingRotationTypeEnum.SimpleRotation)
            {
                DaysSinceGraze = simpleGrazingFrequency;
            }
            else if ((GrazingRotationType == GrazingRotationTypeEnum.TargetMass ||
                      GrazingRotationType == GrazingRotationTypeEnum.Flexible) &&
                     MinimumRotationLengthArray != null)
            {
                DaysSinceGraze = Convert.ToInt32(MinimumRotationLengthArray[clock.Today.Month - 1]);
            }
        }
Example #2
0
        private void OnSimulationCommencing(object sender, EventArgs e)
        {
            if (forages == null)
            {
                throw new Exception("No forages component found in simulation.");
            }
            var parentZone = Parent as Zone;

            if (parentZone == null)
            {
                throw new Exception("SimpleGrazing is not in a zone");
            }
            allForages          = forages.ModelsWithDigestibleBiomass.Where(forage => forage.Zone == parentZone).ToList();
            ProportionOfTotalDM = new double[allForages.Count()];

            if (GrazingRotationType == GrazingRotationTypeEnum.TargetMass)
            {
                if (PreGrazeDMArray == null || PreGrazeDMArray.Length != 12)
                {
                    throw new Exception("There must be 12 values input for the pre-grazing DM");
                }
                if (PostGrazeDMArray == null || PostGrazeDMArray.Length != 12)
                {
                    throw new Exception("There must be 12 values input for the post-grazing DM");
                }
            }
            else if (GrazingRotationType == GrazingRotationTypeEnum.Flexible)
            {
                if (string.IsNullOrEmpty(FlexibleExpressionForTimingOfGrazing))
                {
                    throw new Exception("You must specify an expression for timing of grazing.");
                }
                expressionFunction            = new CSharpExpressionFunction();
                expressionFunction.Parent     = this;
                expressionFunction.Expression = "Convert.ToDouble(" + FlexibleExpressionForTimingOfGrazing + ")";
                expressionFunction.SetCompiler(compiler);
                expressionFunction.CompileExpression();
            }

            if (FractionExcretedNToDung != null && FractionExcretedNToDung.Length != 1 && FractionExcretedNToDung.Length != 12)
            {
                throw new Exception("You must specify either a single value for 'proportion of defoliated nitrogen going to dung' or 12 monthly values.");
            }

            if (SpeciesCutProportions == null)
            {
                SpeciesCutProportions = MathUtilities.CreateArrayOfValues(1.0, allForages.Count());
            }

            if (SpeciesCutProportions.Sum() != allForages.Count)
            {
                throw new Exception("The species cut weightings must add up to the number of species.");
            }

            if (SimpleGrazingFrequencyString != null && SimpleGrazingFrequencyString.Equals("end of month", StringComparison.InvariantCultureIgnoreCase))
            {
                simpleGrazingFrequency = 0;
            }
            else
            {
                simpleGrazingFrequency = Convert.ToInt32(SimpleGrazingFrequencyString);
            }

            if (FractionDefoliatedNToSoil == null || FractionDefoliatedNToSoil.Length == 0)
            {
                FractionDefoliatedNToSoil = new double[] { 0 }
            }
            ;

            // Initialise the days since grazing.
            if (GrazingRotationType == GrazingRotationTypeEnum.SimpleRotation)
            {
                DaysSinceGraze = simpleGrazingFrequency;
            }
            else if ((GrazingRotationType == GrazingRotationTypeEnum.TargetMass ||
                      GrazingRotationType == GrazingRotationTypeEnum.Flexible) &&
                     MinimumRotationLengthArray != null)
            {
                DaysSinceGraze = Convert.ToInt32(MinimumRotationLengthArray[clock.Today.Month - 1]);
            }
        }