How do I ensure mass conservation in closed system?

Hello,

I am working on a model of unicellular organisms (plankton) that float around and consume particles of food (nuts). Nuts have size 0.2, plankton start from size 1. When they consume nuts, they add the size of the latter to their own. When they reach size 2, they devide into two plankton of size 1. If a plankton cell has gone without eating for 30 ticks, it dies and breaks down into n=size/0.2 nuts.

I have taken several steps to ensure mass conservation in the system.

If a plankton cell is e.g. size 1.8 and encounters two nuts, it will only consume one (generally, plankton can only eat as many nuts as will bring them up to size 2):

When they starve, they break into n=size/0.2 nuts of size size/n

Finally, when they reproduce, they halve their size:

However, total mass (sum [size] of plankton + sum [size] of nuts) in the system is not conserved; it slowlly decays at increments I cannot reconcile with the magnitudes involved. Plankton size also behaves unexpectedly; it should not be possible to take values other than 1, 1.2, 1.4, 1.6, 1.8, 2 but that is not the case. Nut size on the other hand stays at 0.2 as expected.

Weirdly, the problem goes away if I change the size of nuts from 0.2 to 0.1.

Can someone help me understand what is going on? I would welcome any advice on how to prevent this instability.

Here is the full model:

show code

Hi,
I think your problem might be “let num-to-hatch (size / nuts-size)”
num-to hatch needs to be an integer so you should use
“let num-to-hatch floor (size / nuts-size)”
instead of floor you could use ceiling or round.
Note: I haven’t tested this suggestion.

  • Aaron