A gradient algorithm for the analysis of pipe networks