How to implement an action counter in a model?

Use this forum to post Vensim related questions.
Post Reply
Nub_User
Member
Posts: 38
Joined: Mon Aug 16, 2021 11:55 pm
Vensim version: PLE+

How to implement an action counter in a model?

Post by Nub_User »

Dear colleagues,
I want to ask you if you can help me to implement an action counter in a model.
The idea is to establish an auxiliar variable that increments its value +1 for each output of a stock variable.
Thanks a lot.
tomfid
Administrator
Posts: 3808
Joined: Wed May 24, 2006 4:54 am

Re: How to implement an action counter in a model?

Post by tomfid »

A counter usually can't be an auxiliary, because it has a state. It has to be a stock.

You can do something like the following:

water level = RANDOM UNIFORM(0,1,0)~meters~just a test input|
flood flag = IF THEN ELSE( water level > flood level, 1, 0 )~dmnl~|
flood level = 0.9~meters~|
flood count = INTEG( flood flag/TIME STEP, 0 )~dmnl~|

Note that this counts the number of time steps with flooding, rather than the amount of time. If that matters, there are various ways to handle it, but it's hard to say what's appropriate without some information on the situation.

The above structure could also be implemented with SAMPLE IF TRUE.
Nub_User
Member
Posts: 38
Joined: Mon Aug 16, 2021 11:55 pm
Vensim version: PLE+

Re: How to implement an action counter in a model?

Post by Nub_User »

I made this module including a counter reset when it reaches the value 10.

The problem is when I running the simulation. In periods 57, 76, and 92 the counter restarts at 1 when it should be 0. :shock:

Why is that? Can you help me correct it? Thanks!
Attachments
test_counter.mdl
(2.22 KiB) Downloaded 42 times
Post Reply