Skip to content

Continuity and Mass Conservation

Module Reference

Continuity equation, flow geometry and mass conservation.

All dimensional quantities use SI units.

Velocity represents cross-sectional mean normal velocity. Signed velocity and discharge are supported.

The Venturi solver assumes steady, incompressible flow through a contraction.

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

volumetric_flow_rate

volumetric_flow_rate(velocity: ArrayLike, area: ArrayLike) -> float | np.ndarray

Calculate volumetric flow rate in m³/s.

Q = U * A

Source code in src/fluidmec/core/continuity.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def volumetric_flow_rate(
    velocity: ArrayLike,
    area: ArrayLike,
) -> float | np.ndarray:
    """
    Calculate volumetric flow rate in m³/s.

    Q = U * A
    """

    velocity = as_float_array(velocity, "Velocity")

    area = validate_non_negative(area, "Area")

    return format_output(velocity * area)

mass_flow_rate

mass_flow_rate(velocity: ArrayLike, area: ArrayLike, density: ArrayLike = WATER_DENSITY) -> float | np.ndarray

Calculate mass flow rate in kg/s.

m_dot = rho * U * A

Source code in src/fluidmec/core/continuity.py
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
def mass_flow_rate(
    velocity: ArrayLike,
    area: ArrayLike,
    density: ArrayLike = WATER_DENSITY,
) -> float | np.ndarray:
    """
    Calculate mass flow rate in kg/s.

    m_dot = rho * U * A
    """

    density = validate_positive(density, "Density")

    discharge = volumetric_flow_rate(velocity, area)

    return format_output(density * discharge)

circular_area

circular_area(diameter: ArrayLike) -> float | np.ndarray

Calculate circular cross-sectional area in m².

A = pi * D² / 4

Source code in src/fluidmec/core/continuity.py
63
64
65
66
67
68
69
70
71
72
73
74
def circular_area(
    diameter: ArrayLike,
) -> float | np.ndarray:
    """
    Calculate circular cross-sectional area in m².

    A = pi * D² / 4
    """

    diameter = validate_positive(diameter, "Diameter")

    return format_output(np.pi * diameter**2 / 4)

rectangular_area

rectangular_area(width: ArrayLike, height: ArrayLike) -> float | np.ndarray

Calculate rectangular cross-sectional area in m².

A = b * h

Zero height is allowed for a dry channel.

Source code in src/fluidmec/core/continuity.py
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def rectangular_area(
    width: ArrayLike,
    height: ArrayLike,
) -> float | np.ndarray:
    """
    Calculate rectangular cross-sectional area in m².

    A = b * h

    Zero height is allowed for a dry channel.
    """

    width = validate_positive(width, "Width")

    height = validate_non_negative(height, "Height")

    return format_output(width * height)

annular_area

annular_area(outer_diameter: ArrayLike, inner_diameter: ArrayLike) -> float | np.ndarray

Calculate annular cross-sectional area in m².

A = pi * (Do² - Di²) / 4

Source code in src/fluidmec/core/continuity.py
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
def annular_area(
    outer_diameter: ArrayLike,
    inner_diameter: ArrayLike,
) -> float | np.ndarray:
    """
    Calculate annular cross-sectional area in m².

    A = pi * (Do² - Di²) / 4
    """

    outer = validate_positive(
        outer_diameter,
        "Outer diameter",
    )

    inner = validate_non_negative(
        inner_diameter,
        "Inner diameter",
    )

    if np.any(inner >= outer):
        raise ValueError("Inner diameter must be smaller than outer diameter.")

    # Factored form reduces cancellation
    # for thin annular sections.

    return format_output(np.pi * (outer - inner) * (outer + inner) / 4)

hydraulic_diameter

hydraulic_diameter(area: ArrayLike, wetted_perimeter: ArrayLike) -> float | np.ndarray

Calculate hydraulic diameter in metres.

Dh = 4 * A / Pw

Source code in src/fluidmec/core/continuity.py
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
def hydraulic_diameter(
    area: ArrayLike,
    wetted_perimeter: ArrayLike,
) -> float | np.ndarray:
    """
    Calculate hydraulic diameter in metres.

    Dh = 4 * A / Pw
    """

    area = validate_non_negative(area, "Area")

    perimeter = validate_positive(
        wetted_perimeter,
        "Wetted perimeter",
    )

    return format_output(4 * area / perimeter)

flow_velocity

flow_velocity(discharge: ArrayLike, area: ArrayLike) -> float | np.ndarray

Calculate mean flow velocity in m/s.

U = Q / A

Source code in src/fluidmec/core/continuity.py
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
def flow_velocity(
    discharge: ArrayLike,
    area: ArrayLike,
) -> float | np.ndarray:
    """
    Calculate mean flow velocity in m/s.

    U = Q / A
    """

    discharge = as_float_array(discharge, "Discharge")

    area = validate_positive(area, "Area")

    return format_output(discharge / area)

continuity_velocity

continuity_velocity(velocity_1: ArrayLike, area_1: ArrayLike, area_2: ArrayLike) -> float | np.ndarray

Solve steady incompressible continuity.

U2 = U1 * A1 / A2

Returns velocity in m/s.

Source code in src/fluidmec/core/continuity.py
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
def continuity_velocity(
    velocity_1: ArrayLike,
    area_1: ArrayLike,
    area_2: ArrayLike,
) -> float | np.ndarray:
    """
    Solve steady incompressible continuity.

    U2 = U1 * A1 / A2

    Returns velocity in m/s.
    """

    discharge = volumetric_flow_rate(velocity_1, area_1)

    return flow_velocity(discharge, area_2)

compressible_continuity_velocity

compressible_continuity_velocity(velocity_1: ArrayLike, area_1: ArrayLike, area_2: ArrayLike, density_1: ArrayLike, density_2: ArrayLike) -> float | np.ndarray

Solve steady continuity for variable density.

U2 = rho1 * U1 * A1 / (rho2 * A2)

Densities must be supplied by the caller. Returns velocity in m/s.

Source code in src/fluidmec/core/continuity.py
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
def compressible_continuity_velocity(
    velocity_1: ArrayLike,
    area_1: ArrayLike,
    area_2: ArrayLike,
    density_1: ArrayLike,
    density_2: ArrayLike,
) -> float | np.ndarray:
    """
    Solve steady continuity for variable density.

    U2 = rho1 * U1 * A1 / (rho2 * A2)

    Densities must be supplied by the caller.
    Returns velocity in m/s.
    """

    mass_rate = mass_flow_rate(
        velocity_1,
        area_1,
        density_1,
    )

    density_2 = validate_positive(density_2, "Density 2")

    area_2 = validate_positive(area_2, "Area 2")

    return format_output(mass_rate / (density_2 * area_2))

mass_conservation_residual

mass_conservation_residual(mass_in: ArrayLike, mass_out: ArrayLike, accumulation: ArrayLike = 0.0, axis: int | None = None) -> float | np.ndarray

Calculate mass conservation residual in kg/s.

R = sum(m_in) - sum(m_out) - dM/dt

Inlet and outlet rates must be non-negative magnitudes.

axis=None sums all inlet and outlet values. Specify an axis for batched calculations.

Positive accumulation means increasing mass inside the control volume.

Source code in src/fluidmec/core/continuity.py
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
def mass_conservation_residual(
    mass_in: ArrayLike,
    mass_out: ArrayLike,
    accumulation: ArrayLike = 0.0,
    axis: int | None = None,
) -> float | np.ndarray:
    """
    Calculate mass conservation residual in kg/s.

    R = sum(m_in) - sum(m_out) - dM/dt

    Inlet and outlet rates must be non-negative
    magnitudes.

    axis=None sums all inlet and outlet values.
    Specify an axis for batched calculations.

    Positive accumulation means increasing mass
    inside the control volume.
    """

    mass_in = validate_non_negative(mass_in, "Incoming mass flow")

    mass_out = validate_non_negative(mass_out, "Outgoing mass flow")

    accumulation = as_float_array(accumulation, "Mass accumulation")

    total_in = np.sum(mass_in, axis=axis)
    total_out = np.sum(mass_out, axis=axis)

    return format_output(total_in - total_out - accumulation)

venturi_flow_rate

venturi_flow_rate(pressure_1: ArrayLike, pressure_2: ArrayLike, area_1: ArrayLike, area_2: ArrayLike, density: ArrayLike = WATER_DENSITY, elevation_1: ArrayLike = 0.0, elevation_2: ArrayLike = 0.0, discharge_coefficient: ArrayLike = 1.0, gravity: ArrayLike = ENGINEERING_GRAVITY) -> float | np.ndarray

Calculate forward Venturi discharge in m³/s.

Q = Cd * A2 * sqrt( 2 * delta_p_eff / (rho * (1 - (A2/A1)²)) )

Requires a contraction, A2 < A1, and non-negative effective pressure drop.

Cd = 1 represents ideal flow.

Source code in src/fluidmec/core/continuity.py
242
243
244
245
246
247
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
def venturi_flow_rate(
    pressure_1: ArrayLike,
    pressure_2: ArrayLike,
    area_1: ArrayLike,
    area_2: ArrayLike,
    density: ArrayLike = WATER_DENSITY,
    elevation_1: ArrayLike = 0.0,
    elevation_2: ArrayLike = 0.0,
    discharge_coefficient: ArrayLike = 1.0,
    gravity: ArrayLike = ENGINEERING_GRAVITY,
) -> float | np.ndarray:
    """
    Calculate forward Venturi discharge in m³/s.

    Q = Cd * A2 * sqrt(
        2 * delta_p_eff /
        (rho * (1 - (A2/A1)²))
    )

    Requires a contraction, A2 < A1,
    and non-negative effective pressure drop.

    Cd = 1 represents ideal flow.
    """

    p1 = as_float_array(pressure_1, "Pressure 1")

    p2 = as_float_array(pressure_2, "Pressure 2")

    a1 = validate_positive(area_1, "Area 1")

    a2 = validate_positive(area_2, "Area 2")

    rho = validate_positive(density, "Density")

    g = validate_positive(gravity, "Gravity")

    z1 = as_float_array(elevation_1, "Elevation 1")

    z2 = as_float_array(elevation_2, "Elevation 2")

    cd = validate_positive(
        discharge_coefficient,
        "Discharge coefficient",
    )

    if np.any(cd > 1):
        raise ValueError("Discharge coefficient must not exceed 1 for this model.")

    if np.any(a2 >= a1):
        raise ValueError("Venturi throat area must be smaller than inlet area.")

    pressure_term = p1 - p2
    elevation_term = rho * g * (z1 - z2)

    delta_p = pressure_term + elevation_term

    # Permit tiny floating-point cancellation
    # errors near zero effective pressure drop.

    scale = np.abs(pressure_term) + np.abs(elevation_term)

    tolerance = 32 * np.finfo(float).eps * scale

    if np.any(delta_p < -tolerance):
        raise ValueError("Effective pressure drop must be non-negative.")

    delta_p = np.maximum(delta_p, 0.0)

    # Numerically stable form of
    # 1 - (A2/A1)**2.

    geometry_factor = (a1 - a2) / a1 * (1 + a2 / a1)

    result = cd * a2 * np.sqrt(2 * delta_p / (rho * geometry_factor))

    return format_output(result)