APSIMInitiative / ApsimX

ApsimX is the next generation of APSIM
http://www.apsim.info
Other
129 stars 159 forks source link

N retranslocation exceeds storage + metabolic nitrogen in organ: Leaf #8714

Open BrianCollinss opened 4 months ago

BrianCollinss commented 4 months ago

What happened?

Example.zip

Models.Core.SimulationException
 ---> System.Exception: N retranslocation exceeds storage + metabolic nitrogen in organ: Leaf
   at Models.PMF.Organs.SimpleLeaf.SetNitrogenAllocation(BiomassAllocationType nitrogen) in Repository\APSIMNextGen\Models\PMF\Organs\SimpleLeaf.cs:line 1118
   at Models.PMF.Arbitrator.NitrogenAllocationsMethod.Allocate(IArbitration[] Organs, BiomassArbitrationType N) in Repository\APSIMNextGen\Models\PMF\Arbitrator\Methods\NitrogenAllocationsMethod.cs:line 33
   at Models.PMF.BiomassTypeArbitrator.<>c__DisplayClass7_0.<DoAllocations>b__0(IAllocationMethod pm) in Repository\APSIMNextGen\Models\PMF\Arbitrator\BiomassTypeArbitrator.cs:line 46
   at System.Collections.Generic.List`1.ForEach(Action`1 action)
   at Models.PMF.BiomassTypeArbitrator.DoAllocations(IArbitration[] Organs, BiomassArbitrationType DM) in Repository\APSIMNextGen\Models\PMF\Arbitrator\BiomassTypeArbitrator.cs:line 46
   at Models.PMF.OrganArbitrator.OnDoActualPlantPartioning(Object sender, EventArgs e) in Repository\APSIMNextGen\Models\PMF\Arbitrator\OrganArbitrator.cs:line 243
   at Models.Clock.OnDoCommence(Object _, CommenceArgs e) in Repository\APSIMNextGen\Models\Clock.cs:line 414
   at Models.Core.Simulation.Run(CancellationTokenSource cancelToken) in Repository\APSIMNextGen\Models\Core\Simulation.cs:line 292
   --- End of inner exception stack trace ---
   at Models.Core.Simulation.Run(CancellationTokenSource cancelToken) in Repository\APSIMNextGen\Models\Core\Simulation.cs:line 301
   at Models.Core.Run.SimulationDescription.Run(CancellationTokenSource cancelToken) in Repository\APSIMNextGen\Models\Core\Run\SimulationDescription.cs:line 136
   at APSIM.Shared.JobRunning.JobRunner.Run(IRunnable job) in Repository\APSIMNextGen\APSIM.Shared\JobRunning\JobRunner.cs:line 226
   at APSIM.Shared.JobRunning.JobRunner.RunActualJob(IRunnable job, IJobManager jobManager) in Repository\APSIMNextGen\APSIM.Shared\JobRunning\JobRunner.cs:line 189

What Operating System are you on?

Windows

BrianCollinss commented 4 months ago

Hey @par456. This feels like a Science bug.

par456 commented 4 months ago

I am still seeing that issue, did you happen to change anything in the file?

BrianCollinss commented 4 months ago

One clue is that it almost always happens during "Maturing" phase.

BrianCollinss commented 4 months ago

I added the same check that exists in the Leaf class to the end of the SetNitrogenAllocation of the SimpleLeaf class:

        public virtual void SetNitrogenAllocation(BiomassAllocationType nitrogen)
        {
            double StartN = Live.StructuralN + Live.MetabolicN + Live.StorageN;

            Live.StructuralN += nitrogen.Structural;
            Live.StorageN += nitrogen.Storage;
            Live.MetabolicN += nitrogen.Metabolic;

            Allocated.StructuralN += nitrogen.Structural;
            Allocated.StorageN += nitrogen.Storage;
            Allocated.MetabolicN += nitrogen.Metabolic;

            // Retranslocation
            if (MathUtilities.IsGreaterThan(nitrogen.Retranslocation, startLive.StorageN + startLive.MetabolicN - nitrogen.Reallocation))
                throw new Exception("N retranslocation exceeds storage + metabolic nitrogen in organ: " + Name);
            double storageNRetranslocation = Math.Min(nitrogen.Retranslocation, startLive.StorageN * (1 - senescenceRate.Value()) * nRetranslocationFactor.Value());
            Live.StorageN -= storageNRetranslocation;
            Live.MetabolicN -= (nitrogen.Retranslocation - storageNRetranslocation);
            Allocated.StorageN -= nitrogen.Retranslocation;

            // Reallocation
            if (MathUtilities.IsGreaterThan(nitrogen.Reallocation, startLive.StorageN + startLive.MetabolicN))
                throw new Exception("N reallocation exceeds storage + metabolic nitrogen in organ: " + Name);
            double storageNReallocation = Math.Min(nitrogen.Reallocation, startLive.StorageN * senescenceRate.Value() * nReallocationFactor.Value());
            Live.StorageN -= storageNReallocation;
            Live.MetabolicN -= (nitrogen.Reallocation - storageNReallocation);
            Allocated.StorageN -= nitrogen.Reallocation;

            double endN = Live.StructuralN + Live.MetabolicN + Live.StorageN;
            double checkValue = StartN + nitrogen.Structural + nitrogen.Metabolic + nitrogen.Storage -
                                nitrogen.Reallocation - nitrogen.Retranslocation - nitrogen.Respired;
            double extentOfError = Math.Abs(endN - checkValue);
            if (extentOfError > 0.00000001)
                throw new Exception(Name + "Some Leaf N was not allocated.");
        }

When I remove the other two checks (the lines after // Retranslocation and // Reallocation), I expect the new check to catch the same error, but it does not. So, either this check (that exists in Leaf class as well) doesn't do what is supposed to do, or something else is wrong in the way Reallocation is calculated.

BrianCollinss commented 4 months ago

Hey, @hol353. Do you think this is an issue that might exist in the Leaf class as well? I tried to trace the issue but failed. Do you think the model must calculate the C/N flow components correctly in all growth conditions?

BTW, I think these two currently existing checks in SimpleLeaf are placed in the code too late. They must be placed when the calculation of nitrogen.Reallocation is done.

ilhuber commented 2 months ago

I've been running in to this issue as well. CroptimizR seems very adept at finding parameter combinations that will run afoul of these two checks. Very small changes can cause the exception to be thrown, too (e.g. a day length of 13 will not be fine, but 13.00001 or 12.99999 will be fine with ControlledEnvironment).

I also deleted the two checks in a local copy which allowed CroptimizR to finish. Interestingly, the optimized values do not cause the exception to be thrown with the checks back in place. Nothing major seemed to break upon removing the checks, but I am not familiar enough with the arbitrator to know for sure.

BrianCollinss commented 2 months ago

What crop are you working with?

ilhuber commented 2 months ago

This is with Soybean, which uses the SimpleLeaf Model.

Kadogo1 commented 1 week ago

I've also been getting this message with Soybean when trying to calibrate. "N retranslocation exceeds storage + metabolic nitrogen in organ: Leaf" Could it be some values are going out of bounds during optimization and are not captured and dealt with in the code? Has anyone solved this?

peter-devoil commented 6 days ago

I'm seeing similar in sorghumleaf and am trying to sort out some very convoluted code that gets it there. The conditions are "Exhausted N and plant about to die".

BrianCollinss commented 6 days ago

I'm unsure why it doesn't happen when I replaced the mass balance tests with those from Leaf. See my PR.