Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for DELAY FIXED #29

Closed
MITREDan opened this issue Jul 24, 2020 · 3 comments · Fixed by #108 or #190
Closed

Support for DELAY FIXED #29

MITREDan opened this issue Jul 24, 2020 · 3 comments · Fixed by #108 or #190
Assignees

Comments

@MITREDan
Copy link

MITREDan commented Jul 24, 2020

Support for the DELAY FIXED function would be very useful. Does not seem like there's a way to simulate that function using DELAY1, though if there is, that'd be equally good.

@MITREDan
Copy link
Author

Note: I Spent several hours today trying to do this myself, but couldn't wrap my head around how - if possible - I could "save" the value of a calculation at a certain time step for use at a later time step... everything seemed (?) to be calculated at each step. Would be more than happy to implement this myself with some guidance.

@travisfranck
Copy link
Collaborator

@MITREDan Apologies for the delay. This will be handled in the EquationReader.js file. The expandDelayFunction() has the code that is used to expand the other DELAY functions that have been implemented. DELAYFIXED is different enough that you'll probably include a case that will be different than either DELAY or DELAY3. Does this help get you started?

@ToddFincannon
Copy link
Collaborator

DELAY FIXED requires something none of the other Vensim functions implemented so far need: auxiliary storage of past variable values. The number of values to be stored depends on the delay time, which is quantized to a multiple of TIME STEP. The storage buffer must be allocated at runtime, because the delay time is not known until then. To support this, a new FixedDelay structure is added that implements a ring buffer sized to the number of delay steps.

I followed the Vensim docs which mention that DELAY FIXED is a level var with a special fixed delay subtype. It behaves very much like a level, with a separate initial value set in the init phase, and with evaluation at the same time as other levels. The subtype indicates the special handling that is required, starting with the FixedDelay support structure mentioned above. Code is generated to allocate and initialize this structure at init time just after setting the initial value for the DELAY FIXED variable. Note that the FixedDelay structure is not a Vensim variable. It is generated as a special case.

The other special handling that is needed is: rewriting the DELAY FIXED equation at eval time to remove the initialization arguments (similar to INTEG) and add the FixedDelay support argument; and declaring the FixedDelay pointer at decl time.

When the DELAY FIXED variable is subscripted, the FixedDelay struct is also subscripted. It uses the LHS subscripts. Other variables occurring in arguments on the RHS are still free to use different subscripts.

A new _DELAY_FIXED Vensim function in C rounds out the implementation. It is coupled with __new_fixed_delay to allocate and initialize the FixedDelay structure. _DELAY_FIXED implements a straightforward ring buffer with one twist: when pulling a delayed value from the buffer, it increments the current position first. This is because DELAY FIXED is a level with a value evaluated for the next time step.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants