AquaticEcoDynamics / libaed-water

Code for the AED water quality model
GNU General Public License v3.0
6 stars 6 forks source link

Negatises anfix #46

Closed MichaelBarryBMT closed 2 years ago

MichaelBarryBMT commented 2 years ago

Hi Matt

I think this is what we were talking about today - nfix not conserving mass with internal nutrients simulated.

It is down to signs of various quantities. When the flux of N is computed on the line below, nuptake(phy_i,:) is summed to get total uptake, and the sum is negatised. This is fine for amm and no3 - their uptakes are negative quantities, so summing them and negatising the sum provides a positive uptake quantity.

Nitrogen fixing uptake at nuptake(phy_i,4) (anfix) however is computed as a positive uptake quantity and so adding it to the negative amm and no3 uptakes means that the terms partially cancel, and are not added up correctly: a positive anfix uptake is added to the two negative amm and no3 uptakes, and then the sum negatised. I included the debug info on the right hand side of the screen shot below so you can see the signs of the elements of the nuptake array.

I have suggested the below fix in aed_bio_utils, that is, reporting anfix uptake as a negative quantity back to aed_phytoplankton. This way, all the summed uptakes are the same sign (negative) before the sum is negatised. This suggestion might have flow on effects elsewhere in the code though, so I am not sure.

When I make this change in my local repo I get mass to conserve to 0.2% over a month. Without the fix the divergence was greater than 20%.

MB

image

Quick update: the new diagnostic below will also be affected by the sign of anfix. The sum on nuptake will be potentially cancelling rather than additive.

image