Page 1 of 1

Changing the order of calculations

Posted: Tue Nov 13, 2018 8:13 pm
by kingle
I'm running a hydropower operations model using Vensim.

I noticed that the order of calculations is wrong and as such is causing unreasonable results... I have a stock for the spillway gate position, and the value of this should be computed before the gated spill release is calculated. Because the integration happens after all of the auxiliary variables are already calculated, the effects of the change in gate position are delayed by a full time step. This results in extreme reservoir conditions under non-extreme circumstances. Any way to force Vensim to perform that specific integration before the variables depending on the stock's value are computed?

Re: Changing the order of calculations

Posted: Tue Nov 13, 2018 8:18 pm
by Administrator
The order of calculations is not incorrect.

My guess is that your time step is far too large. It should be 1/4 of the smallest delay in your model. If you can upload the model we can take a look.

Re: Changing the order of calculations

Posted: Tue Nov 13, 2018 8:32 pm
by kingle
I'm running a daily time step - there are no delay functions in the model at all. I previously had an hourly time step so a one-hour lag in the change of gate position didn't cause serious issues - but it does in the daily model. The trouble is, I need a daily model to reduce the number of calculations being performed since I'm going to be running the model over 2 million times with different operating conditions. Converting the model to daily will result in a significant speed up and far less computational effort (months instead of years to run the scenarios).

Is there a way to somehow trick Vensim to integrate the gate position stock before gated spill release is calculated? If not, could I somehow get around the problem by modelling gate position as an auxiliary variable instead of a stock? I've interfaced Vensim with Python using VenPy so perhaps I can use some python script to represent the stock-like character of that variable instead of an actual Vensim stock.

Re: Changing the order of calculations

Posted: Tue Nov 13, 2018 10:37 pm
by tomfid
Making the gate position an auxiliary should work. You can store the previous gate position in a stock if you need to preserve the state.

Re: Changing the order of calculations

Posted: Wed Nov 14, 2018 12:06 am
by kingle
Doing that actually caused an additional day delay somehow.. No idea why that is happening given that I haven't added delays anywhere. Perhaps it has to do with my adding a game variable to hold the gate position to be used at the next time step - maybe the game variables are returned at a strange time... Perhaps the last variable time step should be done with a stock? Is it possible to set the order of calculations for auxiliary variables?

Re: Changing the order of calculations

Posted: Wed Nov 14, 2018 9:45 am
by Administrator
You don't need to change the order of calculations, Vensim does this correctly.

Your model definitely contains delays (even if you think it does not). A time step of one day means a decision takes a day before it can be implemented, you've seen this effect (you mention having an hour lag in change of gate position).
The trouble is, I need a daily model to reduce the number of calculations being performed since I'm going to be running the model over 2 million times with different operating conditions.
Once you've got the model running properly, you can compile to speed it up, change the SAVEPER to reduce the amount of results being saved and use a savelist to further reduce the results saved.
Converting the model to daily will result in a significant speed up and far less computational effort (months instead of years to run the scenarios).
It's only a significant speed up if the model is working correctly.

My guess is that you need to implement a correct time step for the problem you are trying to model and then look to other ways to speed it up. A time step of one day looks like it is completely unsuitable for your model.

Re: Changing the order of calculations

Posted: Wed Nov 14, 2018 1:41 pm
by tomfid
I think we could say more if we could see a submodel illustrating the problem.

Re: Changing the order of calculations

Posted: Wed Nov 14, 2018 7:39 pm
by kingle
It's hard to show an example of what the model does because it's implemented and run using VenPy which substitutes different values in for the gaming variables. These gaming variables are a key part of how the model functions (and perhaps have something to do with the calculation order problems..)

I'm actually planning to use the Vensim model-compiled C script and directly interface it with the Python script to run entirely in C/Python which offers a significant speed up. But still, the hourly time step (which works well) will take approximately 2 years to run all of my 2.21 million scenarios. Reducing the time step to one day offers a 24x speed up of the model (24x less calculations being done), allowing me to complete the scenarios in a few months (which is necessary for my research project timeline).

I tried a few different things and am still finding that the order of equations doesn't make sense - even when the gate position is modelled as an auxiliary variable there is a one day delay. Perhaps the issue is related to when the game variables are calculated? Even the reservoir level, which should remain constant for all calculations performed in a single time step, seems to take on different values depending on what function is being performed (this is assessed using print statements within Python).

So there is no way to somehow change the order of the calculations?

Re: Changing the order of calculations

Posted: Wed Nov 14, 2018 7:55 pm
by kingle
I wonder if I add some more inputs to what I want calculated last, just as a + 0*variable, whether it would trick Vensim into using the order I want... I'm now just trying to change the order of calculations for the Aux. variables (gate position is no longer a stock).

Re: Changing the order of calculations

Posted: Wed Nov 14, 2018 8:22 pm
by LAUJJL
Hi

Wanting to set the order of calculation, is like telling the computer to respect an order and another order at the same time, which is impossible to do.

In fact, by writing the equations, you are telling the computer the order of calculation.

For instance if d = a*b*c, before calculating d the program must calculate a and b and c, and to calculate a,b and c it must again calculate first their causes, etc…

but once this being done, you cannot at the same time tell the computer to change the order that you have told him to respect. It is anyhow obliged to calculate the causes first to preform all the calculations.

Maybe the problem comes from some gaming problems like the one I explain in a preceding post, that has too to do with calculations order.

http://www.ventanasystems.co.uk/forum/v ... f=2&t=7081

it is too possible to force the use of a variable later by using the delay fixed.

You can too during the same time step, use either the value of a stock at the beginning, which is the current way of calculation, or use the value of the stock at the end of the period, a value that is only calculated the next step, but that it is possible to calculate in an auxiliary, adding the value of the stock at the beginning of the step to the rate multiplied by the time step.

It is certainly possible to simplify your problem so as to describe it.

If you cannot do it, it is simply that you do not really understand your model and what it says about your problem (R. G. Coyle conditions for a good modelling).

I suggest too that before calculating millions of scenarios, it might be useful to be able to calculate at least one, and eventually some more.

Best regards.

JJ

Re: Changing the order of calculations

Posted: Wed Nov 14, 2018 8:38 pm
by kingle
Thanks. I do think it is a gaming issue. I'm starting to better understand now and made some progress on improving the order of calculations by changing the order in which the gaming variables are defined. You were right in that it is not a Vensim issue - It was a modelling mistake made by me in setting up those gaming variables!

I'm sure I could make a simplified model, but it would require the installation of Python and Anaconda as well as familiarization with running Vensim using VenPy from Python! Anyways, perhaps it's a pointless exercise since I've discovered the cause of the problem now and am working through it :)

Thanks for the help.