Exemple #1
0
        private void OnDoSoilOrganicMatter(object sender, EventArgs e)
        {
            double[] source = solutes.GetSolute(sourceName);

            double[] destination = solutes.GetSolute(destinationName);

            for (int i = 0; i < soil.Thickness.Length; i++)
            {
                double nitrogenFlow = rate.Value(i) * source[i];
                double nitrogenFlowToDestination = nitrogenFlow * (1 - NLoss.Value(i));

                source[i]      -= nitrogenFlow;
                destination[i] += nitrogenFlowToDestination;
            }
            solutes.SetSolute(sourceName, source);
            solutes.SetSolute(destinationName, destination);
        }
Exemple #2
0
        private void OnDoSoilOrganicMatter(object sender, EventArgs e)
        {
            double[] source = solutes.GetSolute(Parent.Name);
            if (Value == null)
            {
                Value = new double[source.Length];
            }
            if (Loss == null)
            {
                Loss = new double[source.Length];
            }

            double[] destination = null;
            if (destinationName != null)
            {
                destination = solutes.GetSolute(destinationName);
            }

            for (int i = 0; i < source.Length; i++)
            {
                double nitrogenFlow = rate.Value(i) * source[i];
                Loss[i] = nitrogenFlow * NLoss.Value(i);  // keep value of loss for use in output
                double nitrogenFlowToDestination = nitrogenFlow - Loss[i];

                if (destination == null && NLoss.Value(i) != 1)
                {
                    throw new Exception("N loss fraction for N flow must be 1 if no destination is specified.");
                }

                source[i] -= nitrogenFlow;
                Value[i]   = nitrogenFlowToDestination; // keep value of flow for use in output
                if (destination != null)
                {
                    destination[i] += nitrogenFlowToDestination;
                }
            }
            solutes.SetSolute(sourceName, SoluteManager.SoluteSetterType.Soil, source);
            if (destination != null)
            {
                solutes.SetSolute(destinationName, SoluteManager.SoluteSetterType.Soil, destination);
            }
        }
Exemple #3
0
        private void OnDoSoilOrganicMatter(object sender, EventArgs e)
        {
            NutrientPool source = Parent as NutrientPool;

            double[] NH4 = solutes.GetSolute("NH4");
            double[] NO3 = solutes.GetSolute("NO3");

            for (int i = 0; i < source.C.Length; i++)
            {
                double carbonFlowFromSource   = Rate.Value(i) * source.C[i];
                double nitrogenFlowFromSource = MathUtilities.Divide(carbonFlowFromSource, source.CNRatio[i], 0);

                double[] carbonFlowToDestination   = new double[destinations.Count];
                double[] nitrogenFlowToDestination = new double[destinations.Count];

                for (int j = 0; j < destinations.Count; j++)
                {
                    carbonFlowToDestination[j]   = carbonFlowFromSource * CO2Efficiency.Value(i) * destinationFraction[j];
                    nitrogenFlowToDestination[j] = MathUtilities.Divide(carbonFlowToDestination[j], destinations[j].CNRatio[i], 0.0);
                }

                double TotalNitrogenFlowToDestinations = MathUtilities.Sum(nitrogenFlowToDestination);
                double NSupply = nitrogenFlowFromSource + NO3[i] + NH4[i];

                if (MathUtilities.Sum(nitrogenFlowToDestination) > NSupply)
                {
                    double NSupplyFactor = MathUtilities.Bound(MathUtilities.Divide(NO3[i] + NH4[i], TotalNitrogenFlowToDestinations - nitrogenFlowFromSource, 1.0), 0.0, 1.0);

                    for (int j = 0; j < destinations.Count; j++)
                    {
                        carbonFlowToDestination[j]   *= NSupplyFactor;
                        nitrogenFlowToDestination[j] *= NSupplyFactor;
                        if (nitrogenFlowToDestination[j] > 0.5)
                        {
                        }
                    }
                    TotalNitrogenFlowToDestinations *= NSupplyFactor;

                    carbonFlowFromSource   *= NSupplyFactor;
                    nitrogenFlowFromSource *= NSupplyFactor;
                }

                source.C[i] -= carbonFlowFromSource;
                source.N[i] -= nitrogenFlowFromSource;
                for (int j = 0; j < destinations.Count; j++)
                {
                    destinations[j].C[i] += carbonFlowToDestination[j];
                    destinations[j].N[i] += nitrogenFlowToDestination[j];
                }


                if (TotalNitrogenFlowToDestinations <= nitrogenFlowFromSource)
                {
                    NH4[i] += nitrogenFlowFromSource - TotalNitrogenFlowToDestinations;
                }
                else
                {
                    double NDeficit          = TotalNitrogenFlowToDestinations - nitrogenFlowFromSource;
                    double NH4Immobilisation = Math.Min(NH4[i], NDeficit);
                    NH4[i]   -= NH4Immobilisation;
                    NDeficit -= NH4Immobilisation;

                    double NO3Immobilisation = Math.Min(NO3[i], NDeficit);
                    NO3[i]   -= NO3Immobilisation;
                    NDeficit -= NO3Immobilisation;

                    if (MathUtilities.IsGreaterThan(NDeficit, 0.0))
                    {
                        throw new Exception("Insufficient mineral N for immobilisation demand for C flow " + Name);
                    }
                }
            }
            solutes.SetSolute("NH4", NH4);
            solutes.SetSolute("NO3", NO3);
        }