Skip to content

Pipe Flow

Module Reference

Pipe friction, mechanical energy losses and pump power.

All quantities use SI units. All friction factors use the Darcy convention.

Turbulent correlations assume fully developed, single-phase internal flow.

ENGINEERING_GRAVITY module-attribute

ENGINEERING_GRAVITY: Final[float] = 9.81

WATER_DENSITY module-attribute

WATER_DENSITY: Final[float] = 998.2

as_float_array

as_float_array(value: ArrayLike, name: str) -> np.ndarray

Convert input to a finite floating-point array.

Source code in src/fluidmec/core/_validation.py
13
14
15
16
17
18
19
20
21
22
23
24
def as_float_array(
    value: ArrayLike,
    name: str,
) -> np.ndarray:
    """Convert input to a finite floating-point array."""

    array = np.asarray(value, dtype=float)

    if not np.all(np.isfinite(array)):
        raise ValueError(f"{name} must be finite.")

    return array

format_output

format_output(result: ArrayLike) -> float | np.ndarray

Return a Python float for scalar results.

Source code in src/fluidmec/core/_validation.py
69
70
71
72
73
74
75
76
77
78
79
def format_output(
    result: ArrayLike,
) -> float | np.ndarray:
    """Return a Python float for scalar results."""

    result = np.asarray(result, dtype=float)

    if result.ndim == 0:
        return float(result)

    return result

validate_non_negative

validate_non_negative(value: ArrayLike, name: str) -> np.ndarray

Validate non-negative numerical inputs.

Source code in src/fluidmec/core/_validation.py
41
42
43
44
45
46
47
48
49
50
51
52
def validate_non_negative(
    value: ArrayLike,
    name: str,
) -> np.ndarray:
    """Validate non-negative numerical inputs."""

    array = as_float_array(value, name)

    if np.any(array < 0):
        raise ValueError(f"{name} must be non-negative.")

    return array

validate_positive

validate_positive(value: ArrayLike, name: str) -> np.ndarray

Validate strictly positive numerical inputs.

Source code in src/fluidmec/core/_validation.py
27
28
29
30
31
32
33
34
35
36
37
38
def validate_positive(
    value: ArrayLike,
    name: str,
) -> np.ndarray:
    """Validate strictly positive numerical inputs."""

    array = as_float_array(value, name)

    if np.any(array <= 0):
        raise ValueError(f"{name} must be positive.")

    return array

relative_roughness

relative_roughness(roughness: ArrayLike, diameter: ArrayLike) -> float | np.ndarray

Calculate dimensionless relative roughness.

Source code in src/fluidmec/core/pipe_flow.py
26
27
28
29
30
31
32
33
34
35
36
def relative_roughness(
    roughness: ArrayLike,
    diameter: ArrayLike,
) -> float | np.ndarray:
    """Calculate dimensionless relative roughness."""

    epsilon = validate_non_negative(roughness, "Absolute roughness")

    diameter = validate_positive(diameter, "Diameter")

    return format_output(epsilon / diameter)

_validate_turbulent_inputs

_validate_turbulent_inputs(reynolds_number: ArrayLike, relative_roughness: ArrayLike) -> tuple[np.ndarray, np.ndarray]

Validate turbulent correlation inputs.

Source code in src/fluidmec/core/pipe_flow.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
def _validate_turbulent_inputs(
    reynolds_number: ArrayLike,
    relative_roughness: ArrayLike,
) -> tuple[np.ndarray, np.ndarray]:
    """Validate turbulent correlation inputs."""

    re = validate_positive(reynolds_number, "Reynolds number")

    roughness = validate_non_negative(relative_roughness, "Relative roughness")

    if np.any(re < 4000):
        raise ValueError(
            "Turbulent friction correlations require Reynolds number >= 4000."
        )

    if np.any(roughness > 0.05):
        raise ValueError("Relative roughness exceeds the supported limit of 0.05.")

    return np.broadcast_arrays(re, roughness)

laminar_friction_factor

laminar_friction_factor(reynolds_number: ArrayLike) -> float | np.ndarray

Calculate Darcy friction factor for laminar flow.

f = 64 / Re

Valid for fully developed circular pipe flow.

Source code in src/fluidmec/core/pipe_flow.py
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def laminar_friction_factor(
    reynolds_number: ArrayLike,
) -> float | np.ndarray:
    """
    Calculate Darcy friction factor for laminar flow.

    f = 64 / Re

    Valid for fully developed circular pipe flow.
    """

    re = validate_positive(reynolds_number, "Reynolds number")

    if np.any(re >= 2300):
        raise ValueError("Laminar model requires Re < 2300.")

    return format_output(64.0 / re)

haaland_friction_factor

haaland_friction_factor(reynolds_number: ArrayLike, relative_roughness: ArrayLike = 0.0) -> float | np.ndarray

Calculate turbulent Darcy factor using Haaland.

Source code in src/fluidmec/core/pipe_flow.py
79
80
81
82
83
84
85
86
87
88
89
def haaland_friction_factor(
    reynolds_number: ArrayLike,
    relative_roughness: ArrayLike = 0.0,
) -> float | np.ndarray:
    """Calculate turbulent Darcy factor using Haaland."""

    re, roughness = _validate_turbulent_inputs(reynolds_number, relative_roughness)

    x = -1.8 * np.log10((roughness / 3.7) ** 1.11 + 6.9 / re)

    return format_output(1.0 / x**2)

swamee_jain_friction_factor

swamee_jain_friction_factor(reynolds_number: ArrayLike, relative_roughness: ArrayLike = 0.0) -> float | np.ndarray

Calculate turbulent Darcy factor using Swamee-Jain.

Source code in src/fluidmec/core/pipe_flow.py
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
def swamee_jain_friction_factor(
    reynolds_number: ArrayLike,
    relative_roughness: ArrayLike = 0.0,
) -> float | np.ndarray:
    """Calculate turbulent Darcy factor using Swamee-Jain."""

    re, roughness = _validate_turbulent_inputs(reynolds_number, relative_roughness)

    # Conservative lower limit for this approximation.
    if np.any(re < 5000):
        raise ValueError("Swamee-Jain requires Re >= 5000 in this implementation.")

    denominator = np.log10(roughness / 3.7 + 5.74 / re**0.9)

    return format_output(0.25 / denominator**2)

colebrook_friction_factor

colebrook_friction_factor(reynolds_number: ArrayLike, relative_roughness: ArrayLike = 0.0, tolerance: float = 1e-10, max_iterations: int = 100) -> float | np.ndarray

Solve the Colebrook-White equation iteratively.

Uses fixed-point iteration with a Haaland initial estimate.

Raises RuntimeError if convergence fails.

Source code in src/fluidmec/core/pipe_flow.py
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
def colebrook_friction_factor(
    reynolds_number: ArrayLike,
    relative_roughness: ArrayLike = 0.0,
    tolerance: float = 1e-10,
    max_iterations: int = 100,
) -> float | np.ndarray:
    """
    Solve the Colebrook-White equation iteratively.

    Uses fixed-point iteration with a Haaland
    initial estimate.

    Raises RuntimeError if convergence fails.
    """

    re, roughness = _validate_turbulent_inputs(reynolds_number, relative_roughness)

    tol = validate_positive(tolerance, "Tolerance")

    if tol.ndim != 0:
        raise ValueError("Tolerance must be scalar.")

    if (
        isinstance(max_iterations, bool)
        or not isinstance(max_iterations, (int, np.integer))
        or max_iterations < 1
    ):
        raise ValueError("Maximum iterations must be a positive integer.")

    initial_f = haaland_friction_factor(re, roughness)

    x = 1.0 / np.sqrt(initial_f)

    for _ in range(max_iterations):
        x_next = -2.0 * np.log10(roughness / 3.7 + 2.51 * x / re)

        error = np.abs(x_next - x)

        threshold = tol * np.maximum(1.0, np.abs(x_next))

        if np.all(error <= threshold):
            return format_output(1.0 / x_next**2)

        x = x_next

    raise RuntimeError(
        f"Colebrook solver did not converge within {max_iterations} iterations."
    )

darcy_friction_factor

darcy_friction_factor(reynolds_number: ArrayLike, relative_roughness: ArrayLike = 0.0, method: str = 'colebrook', transition: str = 'raise') -> float | np.ndarray

Select a Darcy friction factor by flow regime.

Re < 2300: Laminar circular pipe model.

2300 <= Re < 4000: Raise ValueError by default.

Re >= 4000: Selected turbulent correlation.

transition="interpolate" enables an empirical linear interpolation across the transition region.

Source code in src/fluidmec/core/pipe_flow.py
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
def darcy_friction_factor(
    reynolds_number: ArrayLike,
    relative_roughness: ArrayLike = 0.0,
    method: str = "colebrook",
    transition: str = "raise",
) -> float | np.ndarray:
    """
    Select a Darcy friction factor by flow regime.

    Re < 2300:
        Laminar circular pipe model.

    2300 <= Re < 4000:
        Raise ValueError by default.

    Re >= 4000:
        Selected turbulent correlation.

    transition="interpolate" enables an empirical
    linear interpolation across the transition region.
    """

    re = validate_positive(reynolds_number, "Reynolds number")

    roughness = validate_non_negative(relative_roughness, "Relative roughness")

    if np.any(roughness > 0.05):
        raise ValueError("Relative roughness must not exceed 0.05.")

    methods = {
        "colebrook": colebrook_friction_factor,
        "haaland": haaland_friction_factor,
        "swamee_jain": swamee_jain_friction_factor,
    }

    if method not in methods:
        raise ValueError(f"Unknown friction factor method: {method}")

    if transition not in ("raise", "interpolate"):
        raise ValueError("Transition must be 'raise' or 'interpolate'.")

    solver = methods[method]

    re, roughness = np.broadcast_arrays(re, roughness)

    result = np.empty(re.shape, dtype=float)

    laminar = re < 2300

    transitional = (re >= 2300) & (re < 4000)

    turbulent = re >= 4000

    if np.any(laminar):
        result[laminar] = 64.0 / re[laminar]

    if np.any(turbulent):
        result[turbulent] = solver(
            re[turbulent],
            roughness[turbulent],
        )

    if np.any(transitional):
        if transition == "raise":
            raise ValueError(
                "Friction factor is uncertain in the transitional flow regime."
            )

        if method == "swamee_jain":
            raise ValueError(
                "Transition interpolation is unavailable "
                "with Swamee-Jain because its supported "
                "range begins at Re = 5000."
            )

        f_laminar = 64.0 / 2300

        f_turbulent = solver(
            4000,
            roughness[transitional],
        )

        weight = (re[transitional] - 2300) / (4000 - 2300)

        result[transitional] = (1 - weight) * f_laminar + weight * f_turbulent

    return format_output(result)

darcy_head_loss

darcy_head_loss(friction_factor: ArrayLike, length: ArrayLike, diameter: ArrayLike, velocity: ArrayLike, gravity: ArrayLike = ENGINEERING_GRAVITY) -> float | np.ndarray

Calculate major pipe head loss in metres.

hf = f * (L / D) * U² / (2g)

Source code in src/fluidmec/core/pipe_flow.py
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
def darcy_head_loss(
    friction_factor: ArrayLike,
    length: ArrayLike,
    diameter: ArrayLike,
    velocity: ArrayLike,
    gravity: ArrayLike = ENGINEERING_GRAVITY,
) -> float | np.ndarray:
    """
    Calculate major pipe head loss in metres.

    hf = f * (L / D) * U² / (2g)
    """

    f = validate_non_negative(friction_factor, "Friction factor")

    length = validate_non_negative(length, "Pipe length")

    diameter = validate_positive(diameter, "Diameter")

    velocity = as_float_array(velocity, "Velocity")

    gravity = validate_positive(gravity, "Gravity")

    result = f * length / diameter * velocity**2 / (2 * gravity)

    return format_output(result)

minor_head_loss

minor_head_loss(loss_coefficient: ArrayLike, velocity: ArrayLike, gravity: ArrayLike = ENGINEERING_GRAVITY) -> float | np.ndarray

Calculate local head loss in metres.

hm = K * U² / (2g)

Source code in src/fluidmec/core/pipe_flow.py
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
def minor_head_loss(
    loss_coefficient: ArrayLike,
    velocity: ArrayLike,
    gravity: ArrayLike = ENGINEERING_GRAVITY,
) -> float | np.ndarray:
    """
    Calculate local head loss in metres.

    hm = K * U² / (2g)
    """

    coefficient = validate_non_negative(loss_coefficient, "Loss coefficient")

    velocity = as_float_array(velocity, "Velocity")

    gravity = validate_positive(gravity, "Gravity")

    return format_output(coefficient * velocity**2 / (2 * gravity))

pressure_loss

pressure_loss(head_loss: ArrayLike, density: ArrayLike = WATER_DENSITY, gravity: ArrayLike = ENGINEERING_GRAVITY) -> float | np.ndarray

Convert dissipative head loss to pressure equivalent.

delta_p = rho * g * hL

Returns pressure loss magnitude in Pa.

Source code in src/fluidmec/core/pipe_flow.py
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
def pressure_loss(
    head_loss: ArrayLike,
    density: ArrayLike = WATER_DENSITY,
    gravity: ArrayLike = ENGINEERING_GRAVITY,
) -> float | np.ndarray:
    """
    Convert dissipative head loss to pressure equivalent.

    delta_p = rho * g * hL

    Returns pressure loss magnitude in Pa.
    """

    head_loss = validate_non_negative(head_loss, "Head loss")

    density = validate_positive(density, "Density")

    gravity = validate_positive(gravity, "Gravity")

    return format_output(density * gravity * head_loss)

extended_bernoulli_head

extended_bernoulli_head(initial_head: ArrayLike, pump_head: ArrayLike = 0.0, turbine_head: ArrayLike = 0.0, head_loss: ArrayLike = 0.0) -> float | np.ndarray

Calculate downstream mechanical energy head.

H2 = H1 + hp - ht - hL

Returns total energy head in metres.

Source code in src/fluidmec/core/pipe_flow.py
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
def extended_bernoulli_head(
    initial_head: ArrayLike,
    pump_head: ArrayLike = 0.0,
    turbine_head: ArrayLike = 0.0,
    head_loss: ArrayLike = 0.0,
) -> float | np.ndarray:
    """
    Calculate downstream mechanical energy head.

    H2 = H1 + hp - ht - hL

    Returns total energy head in metres.
    """

    initial_head = as_float_array(initial_head, "Initial head")

    pump_head = validate_non_negative(pump_head, "Pump head")

    turbine_head = validate_non_negative(turbine_head, "Turbine head")

    head_loss = validate_non_negative(head_loss, "Head loss")

    return format_output(initial_head + pump_head - turbine_head - head_loss)

pump_power

pump_power(discharge: ArrayLike, pump_head: ArrayLike, efficiency: ArrayLike = 1.0, density: ArrayLike = WATER_DENSITY, gravity: ArrayLike = ENGINEERING_GRAVITY) -> float | np.ndarray

Calculate required pump input power in watts.

P = rho * g * Q * hp / efficiency

Source code in src/fluidmec/core/pipe_flow.py
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
def pump_power(
    discharge: ArrayLike,
    pump_head: ArrayLike,
    efficiency: ArrayLike = 1.0,
    density: ArrayLike = WATER_DENSITY,
    gravity: ArrayLike = ENGINEERING_GRAVITY,
) -> float | np.ndarray:
    """
    Calculate required pump input power in watts.

    P = rho * g * Q * hp / efficiency
    """

    discharge = validate_non_negative(discharge, "Discharge")

    pump_head = validate_non_negative(pump_head, "Pump head")

    efficiency = validate_positive(efficiency, "Efficiency")

    if np.any(efficiency > 1):
        raise ValueError("Efficiency must not exceed 1.")

    density = validate_positive(density, "Density")

    gravity = validate_positive(gravity, "Gravity")

    return format_output(density * gravity * discharge * pump_head / efficiency)