# How does EPAnet find loops in a pipe network?

I’m working on a pipe network analysis hobby project as a web app where I let the user add pipes and sources and drag them to connect them together. Based on the pipe network analysis methods I’ve seen, all of them require the knowledge of which pipes are in a loop in order to satisfy the 0 pressure change around a loop criteria.

Unfortunately, I’m not able to find an efficient algorithm to find these loops programatically . Networks of more than 5 loops or so slow down to a crawl because the loop finding algorithm takes a long time.

Does EPAnet use a loop finding algorithm under the hood? If so, how does it do it? Does it even need one?

Thank You

EPAnet will be able to solve your networks without loops @polishdude20. It’s algorithm is based on mass balancing at nodes, not energy balance around loops, and its algorithm is very efficient. You can get past the problem you mention in the loop method by creating virtual loops if you want to get into that. However, I’ll strongly advise you use EPAnet for the hydraulic calculations.

I’m asking for this information more for educational purposes. In an industry setting I’d use EPAnet all the way. But for now, I’d like to implement the mass balancing algorithm myself in order to learn.

A good start is the EPANET manual that you can download from here: https://www.epa.gov/water-research/epanet

1 Like

@polishdude20 side question, which stack do you use for the web app?

Thank you! I see the algorithm is shown at the end. This is great stuff.

One question I have while looking at this is on page 188, we are solving for Fi the right hand term by solving sum(Qij) - Di . I understand that the Qij’s can initially be set to anything but where do I find Di? I see Di is the demand for a node but is this something I must calculate?

So if I have a node and I have 5 litres/min coming into the node and 2 litres/min coming out, does the Di demand equal 2 litres/min? And as there are more iterations, the Di term equals the sum(Qij) term?

I’m using Javascript with React as a front-end framework.

Did you try out EPANET 2.2 toolkit in Javascript

Demands are known in advance and imposed on the system by the user. You can use an emitter to simulate a pressure-dependent demand (e.g. a sprinkler) though.

So if there are nodes which have no external demands, I should just set Di to zero?

Nope, haven’t seen it. Looks interesting. I’m aiming to make mine from scratch based on the simulation algorithm of EPAnet rather than making it work with EPAnet files for now.

Yes, that’s what you should do.

BTW, welcome to the forum!

I should mention that the new release of the EPANET Toolkit allows programmatic construction of networks, not tied to any input file. Understanding of course that you describe this as a “hobby” project, I still think it would be very valuable for everyone here if you could some of your reasoning for starting from scratch.

Just to clarify - setting Di to zero is what you should do at nodes with no external demands. You’re welcome to write your own code for the simulation of course, but that will be ignoring decades of contributions to a efficient and trusted industry-standard software tool…