You are here

calculating with values of former time steps

To the hopefully growing Simulistics community:
What´s the easiest way to calculate with values of variables, which they had some time steps ago?
The specific case: I would like to model the appearence of grass growth, flowering, fruiting etc. in dependence of rainfall. Rainfall itself is modelled from a monthly list, but modified with a random number for every time step. Now flowering and fruiting depend on the amount of rainfall some (and different) time steps ago, and I would like to use the values of the variable rainfall to calculate the possible extend of the growing. Therefore I need the values of the variable rainfall some time steps later than it occured...
Does anyone know a code to do so or can post/send a part of a model in which this function is implemented?
Thanks a lot,
Philipp

Forums: 

Hi Philipp,

The function last(Var) returns the value of Var one time step previously and calls to last can be stacked, e.g. last(last(last(Var))) will return the value 3 time steps ago. It would be possible to carry the stacking on further.

How long a delay are you looking for? The above solution (though not neat) works for short delays but is too cumbersome for long delays - I wouldn't want to type 50 lasts in to an equation. (Though I might if it was the only way I could get something important done.)

I know some else at Simulistics was working on a function to do what you want, neatly. I'll see what the status of the work is. In the long term (at least) we will have a neat function to solve this problem. If in the short term you can do want you want by stacking calls to last then I suggest you do that. If that really is too cumbersome I'll see about getting the neat function released soon.

Jonathan.

Jonathan, thank you very much,
for the first this was exactly what I was looking for!

All the best,
Philipp

Hi Philipp

As Jonathan has written, if you want a value from a previous time step, you can use last(). However, if you want to use last(last(last())) often, it might be convenient to write a macro. Macros are easy. Look in your Simile directory and find the Functions folder (in my case C:/Program Files/Simile32/Functions). Open macros.pl with your favourite editor, and you'll see several examples. You could do
last3(var, default) --> if time(1)/dt(1) Next time you open Simile, it will be there as a user-defined function.

Jerry

I wrote a macro to return the value of its argument from a given number of time steps ago. This has been tested on version 3.3. Here it is:

delay(val,steps) -->
[array]=makearray(if place_in(1)==1 then val
else element(last([array]),place_in(1)-1),1000),
element([array],steps+1).

What this does is create a 1000-place array. Each time step it puts its first arg into element 1 of this array and moves all the other elements up one. The result is the n+1 element of the array.

It occurs to me that in simple cases it is still better to use nested last(...) functions, as this macro might slow down the execution of the model.

--Jasper