Delay Function cannot shut off flow using a test for negative stock level.

Use this forum to post Vensim related questions.
Post Reply
tc20852
Junior Member
Posts: 17
Joined: Tue Jan 12, 2021 4:22 am
Vensim version: PRO

Delay Function cannot shut off flow using a test for negative stock level.

Post by tc20852 »

This is what I am trying to model:

pipe_flow_sketch.pdf

basically we have a level (A) feeding into a pipe (actually this is a length of intestines in my larger model that is modeling uptake of a nutrient) that ends up at level (B) according to "delayed_Flow" rate. While it is in the pipe a portion of the nutrient is absorbed through the pipe wall to level C. This is "AC flow rate".

I dried to model the accumulation of material into the empty pipe using a Delay function, but I hit a problem in making sure that the level in "A" does not go negative. Models up to this point were fixed by checking for a negative value in the level using "If Then Else (A<0,0,flowRate)". This works with AC Flow rate, but the test does not seem to be working when it is nested inside the delay function. I tried moving the test outside the delay and putting it into a variable " AB_FlowthroughPipe" , but it still does not shut down flow when A reaches 0.

This simplified model is in the mdl file: TestDelayFunction_Simplified.mdl

It seems I am probably fundamentally misusing the Delay function, so is there a more elegant way of doing this in Vensim?

I think this type of model might have a lot of applications: plug flow reactors not at steady state, contaminants precipitating in a river, change in concentration in a reverse osmosis system, dialysis, renal function, traffic flow diversions off Highways onto side streets.
Attachments
TestDelayFunction_Simplified.mdl
(3.69 KiB) Downloaded 222 times
pipe_flow_sketch.pdf
(238.93 KiB) Downloaded 231 times
Administrator
Super Administrator
Posts: 4573
Joined: Wed Mar 05, 2003 3:10 am

Re: Delay Function cannot shut off flow using a test for negative stock level.

Post by Administrator »

While it is in the pipe a portion of the nutrient is absorbed through the pipe wall to level C. This is "AC flow rate".
Doesn't this suggest the structure you have drawn is incorrect? I'd suggest having a flow out of A, put this into a level "in transit" (or give it a real world name rather than A/B/C), and remove a proportion from that level? The remainder will go into B.
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
tc20852
Junior Member
Posts: 17
Joined: Tue Jan 12, 2021 4:22 am
Vensim version: PRO

Re: Delay Function cannot shut off flow using a test for negative stock level.

Post by tc20852 »

Replying to Tomfid:

Oh I didnt know it was called FONFOO, but isnt that what I did by testing for zero at A?

Delayed flow = DELAY FIXED( AB flowThrougPipe , Delay , 0 )

where AB flowThrougPipe = IF THEN ELSE(A<0, 0 , AB flow rate*A )

then on the other flow, "AC Flow Rate" = IF THEN ELSE(A<0, 0 , 0.1 )
tomfid
Administrator
Posts: 3804
Joined: Wed May 24, 2006 4:54 am

Re: Delay Function cannot shut off flow using a test for negative stock level.

Post by tomfid »

ABflowthroughpipe does impose the constraint for A<0, however, DelayedFlow does not respect the constraint. The DELAY FIXED means that the flow now depends on the value of A in the past, not the value of A now.

Also, the test for A<0 is not sufficient. A might have a small positive value, or 0 value, so that the flow in one time step causes it to fall below 0.
tc20852
Junior Member
Posts: 17
Joined: Tue Jan 12, 2021 4:22 am
Vensim version: PRO

Re: Delay Function cannot shut off flow using a test for negative stock level.

Post by tc20852 »

Hello Folks,

Thanks for your help. Attached is the solution I have so far.
I have attached the model with more descriptive labels, rather than A, B C etc, as administrator said, it makes it a bit more understandable.

As tomfid said, using DELAY FIXED to control flow does not work as it cannot do a logical test on current variables, the only variables available to it are the values that were in existence at the time DELAY was called.
Administrator also suggested putting the outflow into a new level and control flows in and out of this "in transit level". In this example the in transit level is "intestines" then I replaced the DELAY function with a IF THEN ELSE test on a <Time> shadow variable to start flow out to "Egestion" once the 8h is up (my guess for residence time in intestines).

You can imagine this is something of a simplification of what really happens in the gut, but its just thinking of the "stomach" as a CSTR reactor followed by the "intestines" modeled as a plug flow tube reactor. Refinement can come later.

There is also a FONFOO performed at the stomachOutFlow (IF THEN ELSE( Stomach<=0 , 0 , Stomach*stomach flow constant ), and at IntestinalFlow Rate (IF THEN ELSE( Time<8 :OR: Intestines<=0 , 0 , gut flow). Note this is also checking time < 8 before starting up the flow out of intestines using :OR: in the logical test.

One thing i have noticed is that if you drag the IO object tool slider for gut flow over 2.8 g/h, the intestinal flow rate starts oscillating after 23h. This is because there is still material dribbling in from the stomach at a low rate at some time steps there is inadequate intestinal level to result in the intestinal flow to be positive. Eventually intestinal level goes positive and the intestinal flow rate switches on, but only enough for it to hit <0 and it shuts down. Not sure how to do this more elegantly. Tying intestinalFlowRate to the level of material in intestines (e.g. intestinalFlowRate = ki*intestines) would be mathematically incorrect as this would not be a first order type of situation (in this simple model, flow of nutrients down the intestines has a constant velocity or zero order in the intestines).

Not sure what to do about that. If you have any suggestions, that would be great.

Thanks for the help so far!
Attachments
IntestinalModel_2.mdl
(3.38 KiB) Downloaded 188 times
tomfid
Administrator
Posts: 3804
Joined: Wed May 24, 2006 4:54 am

Re: Delay Function cannot shut off flow using a test for negative stock level.

Post by tomfid »

I think the oscillation is due to the discrete logic in the outflow constraint. You have:

intestinal Flow Rate=
IF THEN ELSE( Time<8 :OR: Intestines<=0 , 0 , gut flow)

This enforces "nothing in intestines, no outflow" but it doesn't enforce "tiny amount in intestines, limited outflow".

A better construction might be:

IF THEN ELSE( Time<8, 0, MIN( gut flow, intestines/min intestinal flow time ) )

where min intestinal flow time could be replaced with (or set to) TIME STEP as a shortcut.

I would tend to rewrite as:

STEP(1,intestinal flow time)*MIN( gut flow, intestines/min intestinal flow time )
intestinal flow time = 8
tc20852
Junior Member
Posts: 17
Joined: Tue Jan 12, 2021 4:22 am
Vensim version: PRO

Re: Delay Function cannot shut off flow using a test for negative stock level.

Post by tc20852 »

Hello Tom, hope you are still there! Or any one else out there!

Gosh it seems a long time since looking at this problem again. I've made a simpler model of a much bigger model for testing this intestinal flow.

I am still trying to model something like filling of a tube and using a FIXED DELAY to get the appearance of material at the other end, BUT I still have to have the ability to draw material flow out of the "tube" as this is material that is digested and flows into the blood. I did this with a bit of a cludge:

Take the flow rate after delay but "scale" it by multiplying the DELAYED rate by the fraction not drawn off and going to the blood:

IF THEN ELSE( Small Intestine<=0, 0, Delay*(Small Intestine/(Small Intestine+Blood)))

Can you take a look at this and see if this is properly doing what I want it to do. Is there a better way to approach this? How would you tackle this problem?

Thomas
Attachments
IntestinalFlowAsDelay.pdf
concept sketch
(12.33 KiB) Downloaded 112 times
MonogastricTest2b_DELAY.mdl
vensim model
(4.05 KiB) Downloaded 102 times
tc20852
Junior Member
Posts: 17
Joined: Tue Jan 12, 2021 4:22 am
Vensim version: PRO

Re: Delay Function cannot shut off flow using a test for negative stock level.

Post by tc20852 »

Here is another sketch of what I was trying to do.
Attachments
IntestinalFlowAsDelay2.pdf
(14.15 KiB) Downloaded 106 times
tomfid
Administrator
Posts: 3804
Joined: Wed May 24, 2006 4:54 am

Re: Delay Function cannot shut off flow using a test for negative stock level.

Post by tomfid »

Because the intestine might have complex dynamics for each segment, and it's central to the model, I'm going to suggest that you use arrays rather than delays. I'll post an example.

A question: is the intestine really like a plug flow reactor? As I understand it, those take off material only at the end. What little I know about the intestine suggests that there's a lot of material exchange along the way.
tomfid
Administrator
Posts: 3804
Joined: Wed May 24, 2006 4:54 am

Re: Delay Function cannot shut off flow using a test for negative stock level.

Post by tomfid »

A question: is the intestine really like a plug flow reactor? As I understand it, those take off material only at the end. What little I know about the intestine suggests that there's a lot of material exchange along the way.
Also, it's elastic and has active transport, right?

Anyway, here are some examples.
Ventestine 1.mdl
(8.69 KiB) Downloaded 97 times
In each model, the intestine is broken up into a [segment] vector with 10 subscript elements. The outflow from early segments becomes the inflow to later segments. This is implemented with mapping (look at the next segment and previous segment equations).

The top variation is a standard aging chain, as in the DELAY3 or DELAY N functions, but implemented with subscripting. The interesting feature is that the outflow becomes nearly constant, even though the inflow is pulsed. That's because the transit time is a lot longer than the inflow pulse interval, and there's a lot of dispersion in the aging chain. You'd have to have a lot more segments to change the dynamics I think.

The middle version is I think close to your flow reactor - once a segment is full, any inflow requires an exactly corresponding outflow. So, starting with an empty intestine, ingesting fills the segments sequentially, and the outflow to the large intestine commences in pulses once the system is full. Experience suggests that this isn't quite the way it works, but it might be fine for some purposes.

The third version takes a stab at a more physically explicit system, where intestinal walls are elastic, so pressure depends on the contents in each segment. Flow depends on the pressure differential between segments. I think I might have the physics roughly correct, but it's incomplete. What it implies is that there has to be a big gradient in contents in order to sustain a pressure gradient down the pipe, and that doesn't seem right. (Possibly that relates to my "active transport" comment.) Also, this formulation is a little finicky about stability - slightly lower values cause explosive oscillations. I'd probably change to RK4auto integration or shrink the time step.
tc20852
Junior Member
Posts: 17
Joined: Tue Jan 12, 2021 4:22 am
Vensim version: PRO

Re: Delay Function cannot shut off flow using a test for negative stock level.

Post by tc20852 »

Thanks Tom,

there is a lot of interesting stuff there that I am still going through.
You are, of course way ahead of me. There are a lot more things going on in a more realistic model and its this is interesting stuff, but for the sake of trying to understand fundamentals, lets make this problem simpler by making it a little more abstract.

SEE PDF HERE, its easier with diagrams. A->B->C flow rate B to C is the one subject to a delay, BUT during this delay flow B to D is still occurring.
|
D

So the crux of the problem is that one can get flows DURING the delay, and this is going to reduce the flow rate.

The original example was a crude digestion model with a SINGLE bolus ingested....my cartoon diagram made this look like multiple doses....but I was just trying to show how this one bolus would be proportionally decreased over time as material was drawn off into blood, thus reducing the apparent flow rate from intestine to large intestine. However using a set of ingestions is quite right in a more normal feeding pattern, similarly movement from stomach is pulsed as well..but its best to test out these much simpler models first and build up from a firm foundation....I've looked a bit at that recently, but this work is a simpler study looking at the dynamics of isotopically labeled materials to calculate bioavailability.

Take a quick look at the attached PDF and tell me what you think?

Thomas
tc20852
Junior Member
Posts: 17
Joined: Tue Jan 12, 2021 4:22 am
Vensim version: PRO

Re: Delay Function cannot shut off flow using a test for negative stock level.

Post by tc20852 »

Thanks Tom,

there is a lot of interesting stuff there that I am still going through.
You are, of course way ahead of me. There are a lot more things going on in a more realistic model and its this is interesting stuff, but for the sake of trying to understand fundamentals, lets make this problem simpler by making it a little more abstract.

SEE PDF HERE, its easier with diagrams. A->B->C flow rate B to C is the one subject to a delay, BUT during this delay flow B to D is still occurring.
|
D

So the crux of the problem is that one can get flows DURING the delay, and this is going to reduce the flow rate.

The original example was a crude digestion model with a SINGLE bolus ingested....my cartoon diagram made this look like multiple doses....but I was just trying to show how this one bolus would be proportionally decreased over time as material was drawn off into blood, thus reducing the apparent flow rate from intestine to large intestine. However using a set of ingestions is quite right in a more normal feeding pattern, similarly movement from stomach is pulsed as well..but its best to test out these much simpler models first and build up from a firm foundation....I've looked a bit at that recently, but this work is a simpler study looking at the dynamics of isotopically labeled materials to calculate bioavailability.

Take a quick look at the attached PDF and tell me what you think?

Thomas
Attachments
FlowFromStockPlusDelayedFlow.pdf
(18.17 KiB) Downloaded 90 times
tomfid
Administrator
Posts: 3804
Joined: Wed May 24, 2006 4:54 am

Re: Delay Function cannot shut off flow using a test for negative stock level.

Post by tomfid »

In this case, I think you can forget about my rigid and elastic pipe variants, and just use the classic aging chain.

There are two options for this:

- If the loss function is a simple fractional rate (uptake, or radioactive decay), you can use the DELAY CONVEYOR with leakage. There's an example installed with the help system (DELAY CONVEYOR.mdl).

- If you want a more general uptake rate, or just more flexibility in general, you can use my aging chain variant, and simply add an outflow to each segment of the stock.
tc20852
Junior Member
Posts: 17
Joined: Tue Jan 12, 2021 4:22 am
Vensim version: PRO

Re: Delay Function cannot shut off flow using a test for negative stock level.

Post by tc20852 »

Hello Tom,

I looked at the Conveyer Delay with a Loss in the example: DELAY CONVEYOR.mdl
The rate of loss looks odd. Why is there no loss flow during the delay? Presumably Boxes will be being lost as they accumulate in "shipments in transit".
See screenshot.
Capture_delayConveyor.PNG
Capture_delayConveyor.PNG (26.81 KiB) Viewed 1531 times
Thomas
tc20852
Junior Member
Posts: 17
Joined: Tue Jan 12, 2021 4:22 am
Vensim version: PRO

Re: Delay Function cannot shut off flow using a test for negative stock level.

Post by tc20852 »

Gosh I am so Dumb. Its because the "fractional loss rate" is a step function.
thomas
tomfid
Administrator
Posts: 3804
Joined: Wed May 24, 2006 4:54 am

Re: Delay Function cannot shut off flow using a test for negative stock level.

Post by tomfid »

Well I must be dumb too, because I hadn't figured it out yet either!

I generally prefer the array version over the conveyor function. It's more flexible, because you can have multiple outflows (separate decay and uptake, for example), or nonlinear uptake.
tc20852
Junior Member
Posts: 17
Joined: Tue Jan 12, 2021 4:22 am
Vensim version: PRO

Re: Delay Function cannot shut off flow using a test for negative stock level.

Post by tc20852 »

Using Conveyor Delay:

Simplest setup a linear flow into A with a delayed outflow setup on k1 using DELAY CONVEYOR (k0, residenceT, leak, initprofile, A, residenceT).

From the manual:
DELAY CONVEYOR(input,ctime,leak,initprofile,inittot,initctime)
Units: DELAY CONVEYOR(units,time,1/time,dmnl,units*time,time)
Input is the flow into A (k0, fixed linear rate 10g/Hour) delay time is “residenceT” (5Hour) with the leak set at 10% (0.1 1/Hour) which also used to calculate flow A-> B at “Loss” = A*Leak as a first order process (since the Leak has units 1/Hour).
Fig1.png
Fig1.png (33.75 KiB) Viewed 1518 times
Initprofile has to be a lookup table. We are modeling this as an empty “pipe” so initially the pipe or conveyor is empty so the lookup table is very simple:
Fig2.png
Fig2.png (26.04 KiB) Viewed 1518 times
In this simulation flow in (k0) was set to 10g/Hour, Leak comprised 0.1 1/Hour, so 10% of the flow rate is diverted from A to B as Loss flow, 90% carries on to C. The sum of A -> B and A->C must equal flow into A using this conveyor delay setup so in the graph we can see an initial filling of A prior to the delay ending. Note that some flow to B is occurring during this delay period, which is what we want to happen, and at the end of the delay period material starts to accumulate into C.
So, this looks well validated in this simple model.
Lets add a first order flow before the “pipe”. This would be like a CSTR reactor feeding to a Plug Flow Reactor in series:
Fig3.png
Fig3.png (29.5 KiB) Viewed 1518 times
Checking on that flow k1, A->C starts out at around 11.8 at the delay end (5h)
tc20852
Junior Member
Posts: 17
Joined: Tue Jan 12, 2021 4:22 am
Vensim version: PRO

Re: Delay Function cannot shut off flow using a test for negative stock level.

Post by tc20852 »

Fig4.png
Fig4.png (29.67 KiB) Viewed 1518 times
Summing up a 10% every Hour, starting at 20g/h initially places us at 11.81 g/Hour at this 5h mark. The flow A->B or “Loss” looks right. There is a first order filling with the rate highest at T zero, slowing down as contents of A filling slows and AA is depleted, this then changes direction and decreases in rate when the delay ends and A contents flows into C.
I’ve controlled negative stocks in this case by just putting a test for =< 0 for AA in the AA -> A flow (k0). See discussion about FONFOO earlier in this thread. I’ve got away with a IF THEN ELSE solution here, but often this can give oscillations if the test is on a stock that can receive a small dribble from somewhere else (every few time steps there is enough content in the stock to fail the Zero test so it would switch back on flow and go negative again. There are solutions to this, for those new to this like myself look up MIN).

Tom, am I using CONVEYOR DELAY correctly here?
Especially the part where I am starting out with an "empty" pipe and setting the lookup table to 0,0. I couldnt find another way as the compiler is checking that a lookup table is used for this arg.

A problem I can see with this approach is what would I do if there was yet another Loss to another stock? Also I have not tried to see if I can use an inertial delays to reduce the abruptness of the flows.

Thomas
Attachments
DELAY CONVEYOR_TCModV2.mdl
(3.04 KiB) Downloaded 98 times
DELAY CONVEYOR_TCModV1.mdl
(3.04 KiB) Downloaded 88 times
Post Reply