Note

If only I had written down why I had done this. Life would be so much simpler. Documentation allows you to transfer the why behind code. Much in the same way code comments explain the why, and not the how, documentation serves the same purpose

Algorithms

Gravity

Warning

SO far, in the fydraulcis section, we described the theory using a simple water supply composed of a simple Y shape and three trenches. REF FIGURE LA PRIMA Now onwards, we follow a more generic approach, typical of mathematiccs, to describe the algorithm and we will consider a water supply system of a generic tree topology: it may therefore makde of more tahn 3 threnches. Starting from FIGURE we see a susystem of 7 trenches. In real life cases, this case of seven trenches and 5 reserovirs is likely one of the most difficutl cases you could face. Hpwever, the algorithm we describe here are balid of any numer of trenches and any topology, provied it has the topological shape of a tree.

As seen in Gravity, the network of pipes is shaped as a tree. Ruralwater models the network of pipes as a directed acyclic graph (i.e.: a tree) whose nodes are the pathways.

here is a pdf file pdf

Check problem

The hydraulic check problem algorithm is the following:
  • create the hydraulic energy equations of the law of Bernoulli, where the known data are: the friction per unit water flow, the elevation at the spring and at all the reservoirs.

  • create the water flow continuity equations for each pathway that has children.

  • the unknowns are: the water flow in each pathway

  • create the symbolic equations of the law of Bernoulli

  • create the symbolic equations of the law conservation of water flow

  • consider the equations above as a system of equations and solve it.

  • once the system of equation is solved, consider the water flow obtained in each pathway and calculate the hydraulic energy along each pathway.

  • detect if at any point in any of the pathways, the resulting water pressure (hydraulic energy minus elevation) exceeds the pressure limit for the pipe used in that position.

Design problem

The algorithm in a nutshell

Before illustrating the algorithm in detail, we outline it. This will make the comprehension of the steps, detailed below, more straightforward. In essence, the hydraulic design problem is an optimization problem, where the objective function is the cost of the piped segments, the variables are the hydraulic energy at each junction where the pipeline bisects and the constraints are the elevation, where the hydraulic energy is tied to be equal to the elevation at the root (the spring) and at the leaves (the reservoirs). The approach we adopt to solve the hydraulic design problem is to perform a “systematic search” (the reader may want to perform a search off the internet about such topics). This means that:

  • the physical water supply is a tree of trunks; a trunk is a trench where the pipes are laid down and a sequence of ‘segments’ which describe the pipe material, its diameter and the pressure it can withstand and the linear length where such pipe is used along the trench. Along the trunk the flow of water is constant, there are no abstraction points. Each trench has a ‘tail end’ which lies upstream (so, closer to the spring) and a ‘head end’. Water always flows from tail to head. Water flow is different from trunk to trunk since a trunk at its head end may bifurcate into two (or more) trunks, whose tail end correspond with the head end of the parent trunk. What we have described it the topology of a tree or, in more technical terms, an acyclic directed graph of node and each trunk is a node.

  • a spurious case of the tree is the water supply system composed by a single trunk. In this case we only have one trench, no bifurcations and the pipes connect a spring to a reservoir.

  • we model the water supply as a tree of trunks. Therefore each trunk is a ‘node’ of a ‘tree’ (aka an acyclic direcyed graph) in mathematical lingo. Each node has an adjancency set which is the list of trunks that stem from a trunk head end.

  • The equations of hydraulics are therefore translated as follows:

  • the continuity of water flow translates as: the water flow in a trunk is the sum of the water flows from each trunk in its adjacency list

  • the continuity of hydraulic energy translates as: for each trunk has a value of energy at its head end and the trunks belongin to its adjacncey list all have this value of energy at theit tails

  • we call a ‘inner junctions’ all the junctions where a trunk bisects into two or more trunks. Therefore, if we keep in mind the description of the tree of trunks, the junctions are the ‘head end’ of the root trunk (whose tail end is the water spring), the tail end of all trunks that discharge water into a reservoir (we can call them leaf trunks by similarity with the tree) and both the ends of all other trunks.

  • we keep the energy fixed at the spring and at the tanks; the fixed value is that of the elevation at such points (spring and tanks),

  • we perform a permutation of all possible values of energy in the the junctions. A permutation is valid only if, for each junction, its parent junction has a higher value of energy, otherwise the water could not flow. Remember that water flows from ‘tail’ to ‘head’ so the ‘tail junction’ must have higher energy than ‘head junction’.

  • for each permutation which is valid, we then solve the hydraulic design problem in each trunk with the tail and head values of energy fixed. The solution inside the single trunk may differ if the elevation profile is hilly and several algorithms are taken in account to try finding the optimal solution.

  • The we collect the cost from each trunk for the current permutation and continue looping in all permutations.

  • Once all permutations have been inspected, we identify the one which leads to the least economic cost.

https://github.com/claudiofinizio/ruralwater/blob/master/_algorithms/GravityDesign._solve_subtree.png

So far for the logical steps of the algorithm. However, when dealing with the ‘actual’ programming language (python) here we describe how we translated the above logic into language instructions:

  • to loop over all permutations we adopted the python’s concept of coroutine.

The overbanking energy, an introductory concept, is now introduced.

Overbanking energy

Water flows from higher to lower hydraulic energy… 💪 Therefore we set recursively the minimum hydraulic energy at a pathway’s tail end to be the energy at the head end plus “some energy” to allow a difference of hydraulic energy along the pathway and therefore the water flow. Typically the overbanking energy is the elevation plus “some energy”, but in the case that the pathway climbs a hill then the overbanking energy is the highest elevation point downstream plus “some energy”.

Steps of the algorithm

The idea is then to perform a “search” by inspecting all possible combinations of hydraulic energy the pathways. This is therefore a permutation. For the allowed value of energy, the root pathway has a tail end energy fixed (it is the elevation), each of the leaves have a head end fixed energy (their elevations). The permutations regard therefore the head energy of the root pathway, the tail energy of the leaves pathways and both tail and head energy of each pathway that are neither root nor leaf. Also, the permutation must be limited to those combinations that allow an energy differential between tail and head end. The goal of the coroutine is exactly to ensure the above conditions. This is so because the coroutine is a generator that allow to loop and ensure the above conditions. Since this process must be valid for each possible topology of the branched network, then the coroutine is in charge of tackling this problem in a generic approach valid for each topology.

Image “5_pathways_overbank - only maxmin hydr energy” shows this idea.

  • the “Tee Up” may have a max energy of X otherwise water could not flow from the spring

  • the “Tee left” must

  • the same logic applies to “Tee right”

  • The pathways are nodes and some of them are leaves.

  • the algorithm breaks down the tree of pathways into the pressure zones

  • for each pressure zone it executes the steps below

  • it sets the hydraulic energy at the tail end of the root node to be identical to the elevation at that end

  • it sets the hydraulic energy at the head end of each leaf node to be identical to the elevation at that end

  • it traverses the tree with a ‘depth-first search’ methodology

  • while performing such depth-first search, it sets the ‘over-banking’ hydraulic energy to each node (apart from the leaves nodes) tail end. QUESTA FRASE IN MAIUSCOLO EST INCOMPLETA : THIS OVERBANKING ENERGY IS SET RECURSIVELY TO BE “A BIT” GREATER THAN THE OVERBANKING ENERGY AT THE LEAF NODES. COSA FAI AI NODI INTERNI? NON LO HA DETTO

  • it permutes through all the combinations of over-banking energy at each non-leaf node. At this point each pathway has a value of tail end and head end hydraulic energy set.

  • it evaluates the most economic pipes for those values of energy; this is the XXX algorithm and is still source of study. The evaluation of which are the most economic pipes cannot be solved by use of the simplex algorithm (as other software do) because we are dealing with hilly profiles (rural indeed) and therefore the pipe pressure classes must be taken in account and the problem to solve becomes non-convex. So, no simplex algorithm of use here…. 😞

  • it sums the economic cost from each pathway

  • it selects the solution which led to the lowest economic cost.

The image below gives an idea:
_images/5_pathways_overbank.svg

Electric Station

Check problem

TODO

Design problem

TODO

Electricless Station

Check problem

TODO

Design problem

TODO