Page 1 of 1

[boskid] Fluid box "get_flow" returns wrong value

Posted: Wed Nov 25, 2020 1:51 pm
by PFQNiet
Simple test setup: offshore pump, couple of pipes, pump, storage tanks. Basically ensure the pump can get its full 1,200/s flow rate.

Hover the pump and run

Code: Select all

/c game.print(game.player.selected.fluidbox.get_flow(1))
It returns 11.79517552368, which I assume is supposed to be flow-per-tick. It should be 20.

After further testing I've determined that to get the "real" flow rate, you need:

Code: Select all

/c game.print(game.player.selected.fluidbox.get_flow(1) * 100/59)
This factor of 100/59 seems to correct the value.

Re: Fluid box "get_flow" returns wrong value

Posted: Wed Nov 25, 2020 3:16 pm
by boskid
Oh no... not this... Fluid system... I do not want to touch that...

Issue is that during the fluid flow update, there is a variable `speed` which is used to determine amount of fluid to transfer, but then there is another variable that is used as a flow state for next ticks to simulate some flow momentum and it is computed as `min(0.1*volumeOfSourceFluidBox, 0.59*speed)`. Original speed value is not accesible directly outside of the update phase, the get_flow is using those state values that are scaled by 0.59 (and sometimes capped by 0.1*volume).
I think the best solution would be to "wont fix" this issue as fixing it would most likely require extra memory used in every fluid box or shifting this `min(..., 0.59*speed)` computation to be closer to where it is used, not where it is produced.

I will think if there is some nicer solution for this.

Re: Fluid box "get_flow" returns wrong value

Posted: Wed Nov 25, 2020 4:06 pm
by kovarex
Why do we even expose the flow to the script? We should disable it.

Re: Fluid box "get_flow" returns wrong value

Posted: Wed Nov 25, 2020 4:31 pm
by TreefrogGreaken
Yeah its always been like this, been trying for years to work it out.

I use the FlowStatistics that the game generates for the graphs, this gives a way to get a flow rate without needing to go through all the offshore pumps on the map. You can get the FlowStatistics rate per tick, and i divide by the number of offshore pumps to give a rough estimate.

This also get around the issue of the flow rate dropping to 0 when the output pipe is full but flowing. To do with how its optimised i assume.

If you do need a fluid amount, I do with one of my entities, i look at the object connected to the offshore pump and get the fluid.amount from the fluid box to give a flow rate.

Bit of code from my mod.

Code: Select all

if ODL[c]["entity"].neighbours[1][1].get_fluid_count() > 0.005 then
	fluid = ODL[c]["entity"].neighbours[1][1].fluidbox[1]
	if ODL[c]["entity"].neighbours[1][1]["name"] == "storage-tank" then
		if fluid.amount >= 20 then
			WGA[a]["WtrAdd"] = WGA[a]["WtrAdd"] + 20
		elseif fluid.amount < 20 and fluid.amount > 0 then
			WGA[a]["WtrAdd"] = WGA[a]["WtrAdd"] + 1
		elseif fluid.amount == 0 then
			WGA[a]["WtrAdd"] = WGA[a]["WtrAdd"]
		end

Re: [boskid] Fluid box "get_flow" returns wrong value

Posted: Wed Nov 25, 2020 6:19 pm
by boskid
Ok i found relatively nice fix for this. Flow values will be correct and will also properly account for inflow (previously they were only accounting for outflow). Fixed for 1.1.2

Re: [boskid] Fluid box "get_flow" returns wrong value

Posted: Wed Nov 25, 2020 7:03 pm
by TreefrogGreaken
Does this also mean that the FlowRate wont go to 0 on full outputs? i.e a really long pipeline will have those first few pipes full.

Re: [boskid] Fluid box "get_flow" returns wrong value

Posted: Wed Nov 25, 2020 7:09 pm
by boskid
TreefrogGreaken wrote:
Wed Nov 25, 2020 7:03 pm
Does this also mean that the FlowRate wont go to 0 on full outputs? i.e a really long pipeline will have those first few pipes full.
I do not know what you are talking about. Fluid amount and fluid flow are 2 different values.