Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
BrianCollinss opened this issue Feb 28, 2024 · 17 comments · May be fixed by #8726
Open

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

BrianCollinss opened this issue Feb 28, 2024 · 17 comments · May be fixed by #8726
Labels
Bug Science Software An error in the interface or structure of the software

Comments

@BrianCollinss
Copy link
Member

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

@par456 par456 added Bug Software An error in the interface or structure of the software labels Feb 29, 2024
@BrianCollinss
Copy link
Member Author

Hey @par456. This feels like a Science bug.

@par456
Copy link
Collaborator

par456 commented Feb 29, 2024

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

@par456 par456 reopened this Mar 1, 2024
@BrianCollinss
Copy link
Member Author

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

@BrianCollinss
Copy link
Member Author

BrianCollinss commented Mar 1, 2024

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
Copy link
Member Author

BrianCollinss commented Mar 3, 2024

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
Copy link
Contributor

ilhuber commented Apr 17, 2024

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
Copy link
Member Author

What crop are you working with?

@ilhuber
Copy link
Contributor

ilhuber commented Apr 18, 2024

This is with Soybean, which uses the SimpleLeaf Model.

@Kadogo1
Copy link

Kadogo1 commented Jun 28, 2024

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
Copy link
Contributor

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
Copy link
Member Author

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

@Kadogo1
Copy link

Kadogo1 commented Aug 30, 2024

Has anyone been able to solve this issue?

@BrianCollinss
Copy link
Member Author

BrianCollinss commented Aug 30, 2024

@Kadogo1 I proposed a 'solution' many months ago, but I am unsure whether there is a more important underlying issue somewhere. You can pull the fix from my branch and see if it helps (at least as a band-aid for now).

@Kadogo1
Copy link

Kadogo1 commented Sep 24, 2024

@Kadogo1 I proposed a 'solution' many months ago, but I am unsure whether there is a more important underlying issue somewhere. You can pull the fix from my branch and see if it helps (at least as a band-aid for now).

Thanks Brian. I tried using your Example but it still throws the error. How do I incorporate the changes you made in the actual cs file into my model?

@BrianCollinss
Copy link
Member Author

You need to pull the changes I made using Git commands or software like SourceTree.

@BrianCollinss
Copy link
Member Author

@peter-devoil Peter, did you manage to get to the root of this?

@peter-devoil
Copy link
Contributor

It ended up being specific to sorghum, IIRC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Science Software An error in the interface or structure of the software
Projects
Status: Backlog
Development

Successfully merging a pull request may close this issue.

5 participants