You are here

Preventing compartments going negative

Owen Buchner at Dalhousie Uni writes:

I'm attempting to make a model, but i'm having a problem keeping one of the
stocks from going negative.  I have one stock with an input that is influenced
by a variable parameter.  From this stock I'm taking a fixed quantity out.  My
trouble is that when I want the stock to be small and the fixed quantity large
only what is present within the stock to be removed, but when i have the stock
large whatever the fixed quantity wont remove stays there.  My problem is when
i make the stock small it goes negative because of the fixed quantity trying to
remouve so much.  Is there any type of programming code to use (if and
statements) to fix this problem of a stock going negative?  Thank you for your

Owen Buchner



The amount of flow required to empty a compartment in one time step is the level of the compartment divided by dt(), the duration of the time step. So if you want the outflow never to take more than this amount you can set its equation to min(x,level/dt()) where x is the amount it would otherwise be, and level is the level of the compartment.

This may not result in the compartment's subsequent value being 0 if x is large, because other flows will be adding to or subtracting from the compartment in the same time step. If you want the level to be 0 whenever there is not enough in the compartment to fill the outflow you can modify the above equation to min(x,level/dt() + sum_of_other_flows) where sum_of_other_flows is made by adding all the inflows to your compartment and subtracting all the other outflows.

Happy modelling