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

Add FlowDemand node type #1188

Merged
merged 50 commits into from
Mar 19, 2024
Merged

Add FlowDemand node type #1188

merged 50 commits into from
Mar 19, 2024

Conversation

SouthEndMusic
Copy link
Collaborator

Fixes #78.

@SouthEndMusic SouthEndMusic marked this pull request as draft February 28, 2024 09:47
@SouthEndMusic
Copy link
Collaborator Author

I've updated addnode.qmd as I went trough it, I'm not sure everything is now accurate. The description at the bottom of what happens when running pixi run codegen is probably outdated now, but that should be updated by someone else. More generally, would it be a good idea to have a (developer) docs page about all available pixi commands?

@SouthEndMusic
Copy link
Collaborator Author

Could there be conflicting/changing code formatting? Since my latest commit contains formatting changes to lines of code I didn't touch.

@SouthEndMusic
Copy link
Collaborator Author

SouthEndMusic commented Mar 1, 2024

Concept of flow demand within an optimization problem

  1. Flow conservation constraint of a node with a flow demand at priority other than the flow demand priority ($p \ne p_\text{fd}$):

$$ F_\text{flow out} = F_\text{flow in} + F_\text{buffer out} $$

where the value of $F_\text{buffer out}$ is bounded from above by the capacity of the buffer, which is set to $0.0$ before the optimization for the first priority.

  1. Flow conservation constraint of a node with a flow demand at the flow demand priority ($p = p_\text{fd}$):

$$ F_\text{buffer in} = F_\text{flow in} $$

where $F_\text{buffer in}$ is set as the capacity of the buffer (which is bounded from above by the flow demand) after optimization for this priority. $|F_\text{buffer in} - d|$ is added as a term to the objective function. Here $d = 0$ when $p \ne p_\text{fd}$ and when $p=p_\text{fd}$ then $d$ is what is left of the flow demand after subtracting the flows trough the node with demands at $p < p_\text{fd}$.

  1. The above can be combined into:

$$ F_\text{flow out} + F_\text{buffer in} = F_\text{flow in} + F_\text{buffer out} $$

This constraint can exist unmodified as a combination of 1. and 2. because:

  • For $p \ne p_\text{fd}$ we have that $F_\text{buffer in} = 0$ because $d = 0$ in the objective function;
  • For $p = p_\text{fd}$ we have that $F_\text{buffer out} = 0$ because the buffer capacity at that point is still $0.0$ and $F_\text{flow out} = 0$ because we can constrain it that way.

It must be noted that the choice to do it in this way makes it potentially hard in the future to allow flow demands for multiple priorities for the same node.

Concept of the user demand within an optimization problem

This is slightly different from the flow demand, because the user does not act as a 'conductor' of flow in the absence of demand. Therefore we can just add a buffer capacity constraint on the user outflow edge variable and add to this buffer the return fraction of the abstraction of the user.

Note that with this concept we are not all the way there when it comes to avoiding circular flows:

  • We do prevent that the optimizer makes up flow out of nothing by letting a user use its own return flow from the same priority, so the water balance aspect of the problem is solved;
  • We do not prevent that a user uses its own return flow at all, since the same user can still use the return flow from one priority as abstraction in a later priority.

To do

  • Initialize the described variables (also the ones needed for absolute values)
  • Update the flow conservation equation to the one above
  • Add the term to the objective function (preferably after merging of Deleted relevant code of alternative object functions in Ribasim python and core #1199)
  • Set the demand for the flow demand as described above
  • Add the capacity to the outflow that is $\infty$ for $p \ne p_{\text{fd}}$ and $0.0$ otherwise
  • Make new issue for update of user behavior: UserDemand outflow source #1212
  • Add tests of optimization behavior
  • Add docstrings to complex functions
  • Update allocation documentation

@SouthEndMusic SouthEndMusic mentioned this pull request Mar 5, 2024
6 tasks
Copy link
Contributor

@Huite Huite left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just drive-by review: would be good to break these into shorter functions.

core/src/allocation_init.jl Outdated Show resolved Hide resolved
core/src/allocation_init.jl Outdated Show resolved Hide resolved
core/src/allocation_init.jl Outdated Show resolved Hide resolved
docs/core/usage.qmd Outdated Show resolved Hide resolved
core/src/validation.jl Show resolved Hide resolved
python/ribasim/ribasim/config.py Show resolved Hide resolved
python/ribasim/ribasim/config.py Show resolved Hide resolved
python/ribasim_testmodels/ribasim_testmodels/allocation.py Outdated Show resolved Hide resolved
python/ribasim_testmodels/ribasim_testmodels/allocation.py Outdated Show resolved Hide resolved
python/ribasim_testmodels/ribasim_testmodels/allocation.py Outdated Show resolved Hide resolved
core/src/util.jl Show resolved Hide resolved
core/src/config.jl Outdated Show resolved Hide resolved
docs/core/allocation.qmd Outdated Show resolved Hide resolved
core/src/allocation_optim.jl Outdated Show resolved Hide resolved
@SouthEndMusic SouthEndMusic merged commit 487e6c5 into main Mar 19, 2024
24 checks passed
@SouthEndMusic SouthEndMusic deleted the flow_demand branch March 19, 2024 12:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add FlowDemand node for instream demand
3 participants