Vensim not correctly calculating stock from flows

Use this forum to post Vensim related questions.
Post Reply
kleemax
Senior Member
Posts: 125
Joined: Tue Jan 26, 2016 3:50 pm
Vensim version: PRO

Vensim not correctly calculating stock from flows

Post by kleemax »

What I am used to is that Vensim calculates the value of the flows based on the values of the stocks of the previous time step and the equations of the flows and that the value of the stock of this time step is then calculated based on the flows and the equation of the stock. However, I have built a model where this does not hold and I do not understand why. I am attaching an excel spreadsheet where I have exported the data from a run for the flows and the stock. Please look at the yellow cells. I am also attaching the model.
I wonder whether I have accidentally changed some setting that changes the way stocks are calculated from flows.
I am using PLE+ 10.1.3 on MacOS 10.14.6. I have simulated using Euler.
Attachments
Apfelmus 11-2.mdl
(31.41 KiB) Downloaded 78 times
Problem in calculating stocks from flows and previous stocks.xlsx
(24 KiB) Downloaded 78 times
Administrator
Super Administrator
Posts: 4590
Joined: Wed Mar 05, 2003 3:10 am

Re: Vensim not correctly calculating stock from flows

Post by Administrator »

Can you provide more detail please?

Looking at the spreadsheet, there are two columns titled "calculated stock value", but it's not clear to me what stock this is referring to (there are numerous stocks in the model).

Also, are you including time step in your Excel calcs?

https://www.vensim.com/documentation/euler.html?q=euler
Advice to posters seeking help (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391

Units are important!
http://www.bbc.co.uk/news/magazine-27509559
tomfid
Administrator
Posts: 3811
Joined: Wed May 24, 2006 4:54 am

Re: Vensim not correctly calculating stock from flows

Post by tomfid »

I think that the computation is actually correct.

The Euler update in your spreadsheet is:

stock(t + dt) = stock(t) + flows

This is fine if the TIME STEP (dt) = 1, but in this model it's 0.25.

The correct Euler update is:

stock(t + dt) = stock(t) + flows*dt

In this case, the value 1618 in cell F37 is exactly .25* the value of the net flow 6475 in B36.

It's also easy to get tripped up by beginning/end balance reporting. The Diff integration method shifts storage to account for this (but is otherwise the same as Euler).
kleemax
Senior Member
Posts: 125
Joined: Tue Jan 26, 2016 3:50 pm
Vensim version: PRO

Re: Vensim not correctly calculating stock from flows

Post by kleemax »

Thanks Tom, very helpful. I had indeed forgotten to multiply with 0.25 (=time step) because the flow values are defined for a full time unit (here months). Plus, I had another thinking error: The flow values need to be viewed to belong to the same simulation time as the stock value from which they are calculated, I thought they were calculated from the Stock value of the previous time step.
It should hence be:
stock(t + dt) = stock(t) + flows(t)*dt
And flows(t)= f(Stocks(t))
(all for Euler of course)
tomfid
Administrator
Posts: 3811
Joined: Wed May 24, 2006 4:54 am

Re: Vensim not correctly calculating stock from flows

Post by tomfid »

Right. Personally I find it hard to remember the beginning/ending balance sequencing. It would be nice if we could update the table tool to make it easy to toggle display of stocks as start/end of period, though that might be tricky for some variables using functions like SAMPLE IF TRUE.

It would also be useful to have a way to display flows integrated over the time step, or even over SAVEPER, rather than instantaneous flow rates. I think this is problematic though, because Vensim flows aren't really a distinct variable type (a stock can integrate whatever it wants). This might be something to consider for Ventity.
Post Reply