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]);
            }
        }
 /// <summary>
 /// Try and parse a frequency line and return an instance of a IReportFrequency.
 /// </summary>
 /// <param name="line">The line to parse.</param>
 /// <param name="report">An instance of a report model.</param>
 /// <param name="events">An instance of an events publish/subcribe interface.</param>
 /// <param name="compiler">An instance of a c# compiler.</param>
 /// <returns>true if line was able to be parsed.</returns>
 public static bool TryParse(string line, Report report, IEvent events, ScriptCompiler compiler)
 {
     if (CSharpExpressionFunction.Compile(line, report, compiler, out IBooleanFunction function, out string errorMessages))
     {
         new ExpressionReportFrequency(report, events, function);
         return(true);
     }
     return(false);
 }
Example #3
0
        private void OnSimulationCommencing(object sender, EventArgs e)
        {
            ProportionOfTotalDM = new double[forages.Count];

            if (Verbose)
            {
                summary.WriteMessage(this, "Initialising the Manager for grazing, urine return and reporting");
            }

            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.CompileExpression();
            }

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

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

            if (Verbose)
            {
                summary.WriteMessage(this, "Finished initialising the Manager for grazing, urine return and reporting");
            }
        }
Example #4
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.");
                }
                if (CSharpExpressionFunction.Compile(FlexibleExpressionForTimingOfGrazing, this, compiler, out IBooleanFunction f, out string errors))
                {
                    expressionFunction = f;
                }
                else
                {
                    throw new Exception(errors);
                }
            }
Example #5
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]);
            }
        }