{'fraction_non_alphanumeric': 0.03759218465664208, 'fraction_numerical': 0.014414328129489512, 'mean_word_length': 4.441115164147994, 'pattern_counts': {'":': 0, '<': 2, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Efficient resolution of the Colebrook equation

A robust, fast and accurate method for solving the Colebrook-like equations is presented. The algorithm is efficient for the whole range of parameters involved in the Colebrook equation. The computations are not more demanding than simplified approximations, but they are much more accurate. The algorithm is also faster and more robust than the Colebrook solution expressed in term of the Lambert W-function. Matlab and FORTRAN codes are provided.


Introduction
Turbulent fluid flows in pipes and open channels play an important role in hydraulics, chemical engineering, transportation of hydrocarbons, for example. These flows induce a significant loss of energy depending on the flow regime and the friction on the rigid boundaries. It is thus important to estimate the dissipation due to turbulence and wall friction.
The dissipation models involve a friction coefficient depending on the flow regime (via a Reynolds number) and on the geometry of the pipe or the channel (via an equivalent sand roughness parameter). This friction factor if often given by the well-known Colebrook-White equation, or very similar equations.
The Colebrook-White equation estimates the (dimensionless) Darcy-Weisbach friction factor λ for fluid flows in filled pipes. In its most classical form, the Colebrook-White equation is where R = U D/ν is a (dimensionless) Reynolds number and K = ǫ/D is a relative (dimensionless) pipe roughness (U the fluid mean velocity in the pipe, D the pipe hydraulic diameter, ν the fluid viscosity and ǫ the pipe absolute roughness height). There exist several variants of the Colebrook equation, e.g.
1 √ λ = 1.14 − 2 log 10 K + 9.3 R These variants can be recast into the form (1) with small changes in the numerical constants 2.51 and 3.7. Indeed, the latter numbers being obtained fitting experimental data, they are known with limited accuracy. Thus, the formulae (2) and (3) are not fundamentally different from (1). Similarly, there are variants of the Colebrook equations for open channels, which are very similar to (1). Thus, we shall focus on the formula (1), but it is trivial to adapt the resolution procedure introduced here to all variants, as demonstrated in this paper. The Colebrook equation is transcendent and thus cannot be solved in terms of elementary functions. Some explicit approximate solutions have then been proposed [6,10,12]. For instance, the well-known Haaland formula [6] reads 1 √ λ ≈ −1.81 × log 10 6.9 R + K 3.7 1.11 .
Haaland's approximation is explicit but is not as simple as it may look. Indeed, this approximation involves one logarithm only, but also a non-integer power. The computation of the latter requires the evaluation of one exponential and one logarithm, since it is generally computed via the relation where 'ln' is the natural (Napierian) logarithm. Hence, the overall evaluation of (4) requires the computation of three transcendant functions (exponentials and logarithms). We present in this paper much more accurate approximations requiring the evaluation of only two or three logarithms, plus some trivial operations (+, −, ×, ÷).
Only quite recently, it was noticed that the Colebrook-White equation (1) can be solved in closed form [8] using the long existing Lambert W-function [3]. However, when the Reynolds number is large, this exact solution in term of the Lambert function is not convenient for numerical computations due to overflow errors [11]. To overcome this problem, Sonnad and Goudar [11,12] proposed to combine several approximations depending on the Reynolds number. These approaches are somewhat involve and it is actually possible to develop a simpler and more efficient strategy, as we demonstrate in this paper.
A fast, accurate and robust resolution of the Colebrook equation is, in particular, necessary for scientific intensive computations. For instance, numerical simulations of pipe flows require the computation of the friction coefficient at each grid point and for each time step. For long term simulations of long pipes, the Colebrook equation must therefore be solved a huge number of times and hence a fast algorithm is required. An example of such demanding code is the program OLGA [1] which is widely used in the oil industry.
Although the Colebrook formula itself is not very accurate, its accurate resolution is nonetheless an issue for numerical simulations because a too crude resolution may affect the repeatability of the simulations. Robustness is also important since one understandably wants an algorithm capable of dealing with all the possible values of the physical parameters involved in the model. The method described in the present paper was developed to address all this issues. It is also very simple so it can be used for simple applications as well. The method proposed here aims at giving a definitive answer to the problem of solving numerically the Colebrook-like equations.
The paper is organized as follow. In section 2, a general Colebrook-like equation and its solution in term of the Lambert W-function are presented. For the sake of completeness, the Lambert function is briefly described in section 3, as well as a standard algorithm used for its computation. A severe drawback of using the Lambert function for solving the Colebrook equation is also pointed out. To overcome this problem, a new function is introduced in section 4 and an improved new numerical procedure is described. Though this function introduces a big improvement for the computation of the friction factor, it is still not fully satisfactory for solving the Colebrook equation. The reasons are explained in the section 5, where a modified function is derived to address the issue. The modified function is subsequently used in section 6 to solve the Colebrook equation efficiently. The accuracy and speed of the new algorithm is tested and compared with Haaland's approximation. For testing the method and for intensive practical applications, Matlab c and FORTRAN implementations of the algorithm are provided in the appendices. The algorithm is so simple that it can easily be implemented in any other language and modified to be adapted to any variants of the Colebrook equation.

Generic Colebrook equation and its solution
We consider here a generic Colebrook-like equation as where the c i are given constants such that c 1 c 3 > 0. The classical Colebrook-White formula (1) is obviously obtained as a special case of (5) with c 0 = 0, c 1 = 2/ ln 10, c 2 = K/3.7 and c 3 = 2.51/R. The equation (5) has the exact analytical solution which is real if c 1 c 3 > 0 and where W is the principal branch of the Lambert function, often denoted W 0 [3]. In this paper, only the principal branch of the Lambert function is considered because the other branches correspond to nonphysical solutions of the Colebrook equations, so the simplified notation W is not ambiguous.
The Lambert W-function solves the equation where, here, x is real -more precisely x − exp(−1) -and W(0) = 0. The Lambert function cannot be expressed in terms of elementary functions. An efficient algorithm for its computation is based on Halley's iterations [3] provided an initial guess y 0 . Halley's method is cubic (c.f. Appendix A), meaning that the number of exact digits is (roughly) multiplied by three after each iteration. Today, programs for computing the Lambert function are easily found. For instance, an efficient implementation in Matlab c (including complex argument and all the branches) is freely available [5]. The Taylor expansion around x = 0 of the Lambert function is This expansion is of little interest to solve the Colebrook equation because, in this context, the corresponding variable x is necessarily large (x ≫ 1). It is thus more relevant to consider the asymptotic expansion This expansion reveals that W behaves logarithmically for large x, while we must compute W(exp(x)) to solve the Colebrook equation, c.f. relation (6). For our applications, x is large and exp(x) is therefore necessarily huge, to an extend that the computation of exp(x) cannot be achieved due to overflow. Even when the intermediate computations can be done, the result can be very inaccurate due to large round-off errors. Therefore, the resolution of the Colebrook equation via the Lambert function [8] is not efficient for the whole range of parameter of practical interest [11].

The ω-function
To overcome the numerical difficulties related to the Lambert W-function, when used for solving the Colebrook-White equation, we introduce here a new function: the ω-function.
The ω-function is defined such that it solves the equation where we consider only real x. The ω-function is related to the W-function as Note that the Lambert W-function is also sometimes called the Omega function, that should not be confused with the ω-function defined here, where we follow the notation used in [9]. In terms of the ω-function, the solution of (5) is of For large arguments ω(x) behaves like x, i.e. we have the asymptotic behavior which is an interesting feature for the application considered in this paper. As noted by Corless et al. [4], the equation (11) is in some ways nicer than (7). In particular, its derivatives (with respect of y) are simpler, leading thus to algebraically simpler formulae for its numerical resolution. An efficient iterative quartic scheme (c.f. Appendix A) is thus with The computationally costless initial guess (y 0 = x− 1 5 ) was obtained considering the asymptotic behavior (10), minus an empirically found correction (the term − 1 5 ) to improve somewhat the accuracy of y 0 for small x without affecting the accuracy for large x. The relative error e j of the j-th iteration, i.e.
is displayed on the figure 1 for 1 x 10 6 and j = 0, 1, 2. (The accuracy of (14) were measured using the arbitrary precision capability of Mathematica c .) We can see that with j = 2 we have already reached the maximum accuracy possible when computing in double precision, since max(e 2 ) ≈ 4 × 10 −17 for x ∈ [1; ∞[. We note that the relative error continues to decay monotonically as x increases (even for x > 10 6 ) and that there are no overflow problems when computing y j even for very large x (i.e. x ≫ 10 6 ). We note also that for x 5700 the machine double precision is obtained after one iteration only.
The scheme (14) is quartic, meaning that the number of exact digits is multiplied by four after each iteration (c.f. Appendix A). Hence, starting with an initial guess with one correct digit, four digits are exact after one iteration and sixteen after two iterations. That is to say that the machine precision (if working in double precision) is achieved after two iterations only (Fig. 1). Moreover, the scheme (14) has a comparable algebraic complexity per iteration than the scheme (8), i.e. the computational times per iteration are almost identical. However, the iterative quartic scheme (14) converges faster than the cubic one (8), and there are no overflow problems as they appear when computing W(exp(x)) for large x. This algorithm could therefore be used to compute the solution of the Colebrook-White equation (1), but we will use instead an even better one defined in the next section. We note in passing that the iterations (14) are also efficient for computing the ω-function for any complex x, provided some changes in the initial guess y 0 depending on x.

Remarks:
i-With a more accurate initial guess y 0 , such as y 0 = x − ln(x), the desired accuracy may be obtained with fewer iterations. However, the computation of such an improved initial guess requires the evaluation of transcendent functions. Thus, it cannot be significantly faster than the evaluation of y 1 with (14) from the simplest guess y 0 = x − 1 5 , and most likely less accurate. ii-Higher-order iterations are generally more involved per iteration than the low-order ones. Higher-order iterations are thus interesting if the number of iterations is sufficiently reduced so that the total computation is faster to achieve the desired accuracy. This is precisely the case here.
iii-Intensive tests have convinced us that the choice of the simplest initial guess y 0 = x − 1 5 together with the quartic iterations (14) is probably the best possible scheme for computing the ω-function in the interval x ∈ [1; ∞[, at least when working in double precision. If improvements can be found, they are thus most likely very minor in terms of both robustness, speed and accuracy.

The ̟-function
Solving the Colebrook equation via the ω-function is a big improvement compared to its solution in term of the Lambert W-function. One can check that the numerical resolution of the Colebrook equation via the algorithm (14) is indeed very efficient when K = 0, even for very large R. However, when K > 0 the scheme (14) is not so effective for large R, meaning that not all the numerical shortcomings have been addressed introducing the ω-function. The cause for these numerical problems can be explained as follow.
The solution of the Colebrook equation requires the computation of an expression like ω(x 1 + x 2 ) − x 1 where x 1 ≫ x 2 when R is large and K = 0 (but x 1 = 0 if K = 0), see the relation (19) below. Assuming x 2 ∝ ln(x 1 ), as is the case here, the asymptotic expansion as x 1 → ∞, i.e.
exhibits the source of the numerical problems. Indeed, when K > 0 and R is large, we have x 1 ≫ x 2 and x 1 ≫ ln(x 1 ). Therefore |x 2 − ln(x 1 )|/x 1 can be smaller than the accuracy used in the computation and we thus obtain numerically x 1 + x 2 − ln(x 1 ) ≈ x 1 due to round-off errors. Hence ω( To overcome this problem we introduce yet another function: the ̟-function. Introducing the change of variable y = z + x 1 into the equation (11), the ̟-function is defined such that it solves the equation where the x i are real. The ̟-function is related to the ωand W-functions as In terms of the ̟-function, the solution of (5) is obviously The ̟-function is nothing more than the ω-function shifted by the quantity x 1 . This is a very minor analytic modification but this is a numerical significant improvement when x 1 is large. An efficient numerical algorithm for computing the ̟-function is directly derived from the scheme (14) used for the ω-function. We thus obtain at once with If x 1 = 0 the scheme (14) is recovered. The rate of convergence of (17) is of course identical to the scheme (14). Thus, the efficiency of (17) does not need to be re-discussed here (see section 4).

Resolution of the Colebrook-White equation
We test the new procedure with the peculiar Colebrook-White equation (1). Its general solution is where ℓ = ln(10) ≈ 2.302585093. All these analytic solutions are mathematically equivalent, but the relation (20) is more efficient for numerical computations if we use the scheme described in the previous section.

Numerical procedure
The solution of the Colebrook-White equation is obtained computing the ̟function with and using the iterative scheme (17) with j = 0, 1, 2. An approximation of the friction factor is eventually This way, the whole computation of λ j requires the evaluation of j+1 logarithms only, 1 i.e. one logarithm per iteration.
A Matlab c implementation of this algorithm is given in the appendix B. This (vectorized) code was written with clarity in mind, so that one can test and modify easily the program. This program is also fast, accurate and robust, so it can be used in real intensive applications developed in Matlab.
A FORTRAN implementation of this algorithm is given in the appendix C. This program was written with speed in mind, so there are no checks of the input parameters. The code is clear enough that it should be easy to modify and to translate into any programming language.

Accuracy
For the range of Reynolds numbers 10 3 R 10 13 and for four relative roughness K = {0, 10 −3 , 10 −2 , 10 −1 }, the accuracy of λ It appears clearly that λ −1/2 2 is accurate to machine double-precision (at least) for all Reynolds numbers and for all roughnesses (in the whole range of physical interest, and beyond).
It also appears that λ −1/2 1 is more accurate than Haaland's approximation, specially for large R and K. Moreover, the computation of λ 1 requires the evaluation of only two logarithms, so it is faster than Haaland's formula. Note that other explicit approximations having more or less the same accuracy as Haaland's formula, λ −1/2 1 is significantly more accurate than these approximations. Finally, we note that λ −1/2 0 is a too poor approximation to be of any practical interest.

Speed
Testing the actual speed of an algorithm is a delicate task because the running time depends of many factors independent of the algorithm itself (imple-mentation, system, compiler, hardware, etc.), specially on multi-tasking and multi-users computers. In order to estimate the speed of our scheme as fairly as possible, the following methodology was used.
The speeds of the computation of λ 1 and λ 2 are compared with the Haaland approximation λ H . The Matlab environnement and its built-in cputime function is used, for simplicity.
Two vectors of N components, with 1 N 10 5 , are created for R and K. The values are chosen randomly in the intervals 10 3 R 10 9 and 0 K < 1. The computational times are measured several times, the different procedures being called in different orders. For each value of N , the respective timings are averaged and divided by the averaged time used by the Haaland approximation (the latter having thus a relative computational time equal to one for all N ). The result of this test are displayed on the figure 3. (The whole procedure was repeated several times and the corresponding graphics were similar.) For small N , say N < 2000, the computations are so fast that the function cputime cannot measure the times. For larger values of N , we can see on the figure 3 that the computations of λ 1 are a bit faster than the Haaland formula, while the computations of λ 2 are a bit slower, in average. This is in agreement with the number of evaluations of transcendent functions needed for each approximations, as mentioned above.
These relative times may vary depending on the system, hardware and software, but we believe that the results would not be fundamentally different from the ones obtained here. The important result is that the procedure presented in this paper is comparable, in term of speed, to simplified formulae such as the Haaland approximation. The new procedure being much more accurate, it should thus be preferred.

Conclusion
We have introduced a simple, fast, accurate and robust algorithm for solving the Colebrook equation. The formula used is the same for the whole range of the parameters. The accuracy is around machine double precision (around sixteen digits). The present algorithm is more efficient than the solution of the Colebrook equation expressed in term of the Lambert W-function and than simple approximations, such as the Haaland formula.
We have also provided routines in Matlab and FORTRAN for its practical use. The algorithm is so simple that it can easily be implemented in any other language and can be adapted to any variant of the Colebrook equation.
To derive the algorithm, we introduced two special functions: the ωand ̟-functions. These functions could also be useful in other contexts than the Colebrook equation. The efficient algorithms introduced in this paper for their numerical computation could then be used, perhaps with some modifications of the initial guesses, specially if high accuracy is needed.
A High-order schemes for solving a single nonlinear equation Let be a single nonlinear equation f (y) = 0, where f is a sufficiently regular given function and y is unknown. This equation can be solved iteratively via the numerical scheme [7] y j+1 = y j + (p + 1) where p is a non-negative integer and F (p) denotes the p-th derivative of F with F (0) = F . The scheme (21) is of order p + 2, meaning that the number of exact digits is roughly multiplied by p + 2 after each iteration (when the procedure converges, of course). For p = 0 and p = 1, one obtains Newton's and Halley's schemes, respectively. The scheme (14) for solving the Colebrook equation is obtained with p = 2 together with the function f given by the equation (11), plus some elementary algebra. Intensive tests have convinced us that it is most probably the best choice for the problem at hand here.

C FORTRAN code
The FORTRAN function below was written with maximum speed in mind, so some trivial arithmetic simplifications were used and there are no check for errors in the input parameters. Note that, depending on the FORTRAN version and on the compiler, the command LOG may have to be replaced by DLOG to ensure that the logarithm is computed with a double-precision accuracy.