Skip to content

Hydrostatics

Module Reference

Hydrostatic pressure calculations for Fluidmec.

All dimensional quantities use SI units. Depth is measured positively downwards.

ATMOSPHERIC_PRESSURE module-attribute

ATMOSPHERIC_PRESSURE: Final[float] = 101325.0

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

hydrostatic_pressure

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

Calculate hydrostatic gauge pressure in Pa.

p = rho * g * h

Parameters:

Name Type Description Default
depth ArrayLike

Depth below the fluid surface in m.

required
density ArrayLike

Fluid density in kg/m³.

WATER_DENSITY
gravity ArrayLike

Gravitational acceleration in m/s².

ENGINEERING_GRAVITY
Source code in src/fluidmec/core/hydrostatics.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def hydrostatic_pressure(
    depth: ArrayLike,
    density: ArrayLike = WATER_DENSITY,
    gravity: ArrayLike = ENGINEERING_GRAVITY,
) -> float | np.ndarray:
    """
    Calculate hydrostatic gauge pressure in Pa.

    p = rho * g * h

    Parameters
    ----------
    depth : ArrayLike
        Depth below the fluid surface in m.
    density : ArrayLike
        Fluid density in kg/m³.
    gravity : ArrayLike
        Gravitational acceleration in m/s².
    """

    depth = validate_non_negative(depth, "Depth")
    density = validate_positive(density, "Density")
    gravity = validate_positive(gravity, "Gravity")

    return format_output(density * gravity * depth)

absolute_pressure

absolute_pressure(depth: ArrayLike, density: ArrayLike = WATER_DENSITY, surface_pressure: ArrayLike = ATMOSPHERIC_PRESSURE, gravity: ArrayLike = ENGINEERING_GRAVITY) -> float | np.ndarray

Calculate absolute hydrostatic pressure in Pa.

p_abs = p_surface + rho * g * h

Surface pressure must be absolute pressure.

Source code in src/fluidmec/core/hydrostatics.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
def absolute_pressure(
    depth: ArrayLike,
    density: ArrayLike = WATER_DENSITY,
    surface_pressure: ArrayLike = ATMOSPHERIC_PRESSURE,
    gravity: ArrayLike = ENGINEERING_GRAVITY,
) -> float | np.ndarray:
    """
    Calculate absolute hydrostatic pressure in Pa.

    p_abs = p_surface + rho * g * h

    Surface pressure must be absolute pressure.
    """

    surface_pressure = validate_non_negative(surface_pressure, "Surface pressure")

    gauge_pressure = hydrostatic_pressure(depth, density, gravity)

    return format_output(surface_pressure + gauge_pressure)

pressure_head

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

Calculate pressure head in metres.

H = p / (rho * g)

Pressure is a signed gauge pressure in Pa.

Source code in src/fluidmec/core/hydrostatics.py
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
def pressure_head(
    pressure: ArrayLike,
    density: ArrayLike = WATER_DENSITY,
    gravity: ArrayLike = ENGINEERING_GRAVITY,
) -> float | np.ndarray:
    """
    Calculate pressure head in metres.

    H = p / (rho * g)

    Pressure is a signed gauge pressure in Pa.
    """

    pressure = as_float_array(pressure, "Pressure")

    density = validate_positive(density, "Density")

    gravity = validate_positive(gravity, "Gravity")

    return format_output(pressure / (density * gravity))

hydrostatic_depth

hydrostatic_depth(pressure: ArrayLike, density: ArrayLike = WATER_DENSITY, surface_pressure: ArrayLike = ATMOSPHERIC_PRESSURE, gravity: ArrayLike = ENGINEERING_GRAVITY) -> float | np.ndarray

Calculate depth below the fluid surface in m.

h = (p_abs - p_surface) / (rho * g)

Both pressures must be absolute and in Pa.

Source code in src/fluidmec/core/hydrostatics.py
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
def hydrostatic_depth(
    pressure: ArrayLike,
    density: ArrayLike = WATER_DENSITY,
    surface_pressure: ArrayLike = ATMOSPHERIC_PRESSURE,
    gravity: ArrayLike = ENGINEERING_GRAVITY,
) -> float | np.ndarray:
    """
    Calculate depth below the fluid surface in m.

    h = (p_abs - p_surface) / (rho * g)

    Both pressures must be absolute and in Pa.
    """

    pressure = validate_non_negative(pressure, "Pressure")

    surface_pressure = validate_non_negative(surface_pressure, "Surface pressure")

    difference = pressure - surface_pressure

    if np.any(difference < 0):
        raise ValueError(
            "Pressure cannot be below surface pressure for a submerged depth."
        )

    return pressure_head(difference, density, gravity)

pressure_difference

pressure_difference(depth_1: ArrayLike, depth_2: ArrayLike, density: ArrayLike = WATER_DENSITY, gravity: ArrayLike = ENGINEERING_GRAVITY) -> float | np.ndarray

Calculate signed pressure difference in Pa.

delta_p = rho * g * (h2 - h1)

Source code in src/fluidmec/core/hydrostatics.py
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
def pressure_difference(
    depth_1: ArrayLike,
    depth_2: ArrayLike,
    density: ArrayLike = WATER_DENSITY,
    gravity: ArrayLike = ENGINEERING_GRAVITY,
) -> float | np.ndarray:
    """
    Calculate signed pressure difference in Pa.

    delta_p = rho * g * (h2 - h1)
    """

    depth_1 = validate_non_negative(depth_1, "Depth 1")

    depth_2 = validate_non_negative(depth_2, "Depth 2")

    density = validate_positive(density, "Density")

    gravity = validate_positive(gravity, "Gravity")

    return format_output(density * gravity * (depth_2 - depth_1))

stratified_pressure

stratified_pressure(depth: ArrayLike, density: ArrayLike, surface_pressure: float = 0.0, gravity: float = ENGINEERING_GRAVITY) -> np.ndarray

Calculate pressure in a variable-density fluid.

p(h) = p_surface + integral(rho * g * dh)

Uses cumulative trapezoidal integration.

Parameters:

Name Type Description Default
depth ArrayLike

One-dimensional depth coordinates in m. Must start at zero and increase strictly.

required
density ArrayLike

Density at each depth in kg/m³.

required
surface_pressure float

Pressure at depth zero in Pa. Default zero returns gauge pressure.

0.0
gravity float

Constant gravitational acceleration.

ENGINEERING_GRAVITY

Returns:

Type Description
ndarray

Pressure at every supplied depth in Pa.

Source code in src/fluidmec/core/hydrostatics.py
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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
def stratified_pressure(
    depth: ArrayLike,
    density: ArrayLike,
    surface_pressure: float = 0.0,
    gravity: float = ENGINEERING_GRAVITY,
) -> np.ndarray:
    """
    Calculate pressure in a variable-density fluid.

    p(h) = p_surface + integral(rho * g * dh)

    Uses cumulative trapezoidal integration.

    Parameters
    ----------
    depth : ArrayLike
        One-dimensional depth coordinates in m.
        Must start at zero and increase strictly.
    density : ArrayLike
        Density at each depth in kg/m³.
    surface_pressure : float
        Pressure at depth zero in Pa.
        Default zero returns gauge pressure.
    gravity : float
        Constant gravitational acceleration.

    Returns
    -------
    numpy.ndarray
        Pressure at every supplied depth in Pa.
    """

    depth = validate_non_negative(depth, "Depth")

    density = validate_positive(density, "Density")

    surface_pressure = validate_non_negative(surface_pressure, "Surface pressure")

    gravity = validate_positive(gravity, "Gravity")

    if surface_pressure.ndim != 0:
        raise ValueError("Surface pressure must be scalar.")

    if gravity.ndim != 0:
        raise ValueError("Gravity must be scalar.")

    if depth.ndim != 1 or density.ndim != 1:
        raise ValueError("Depth and density must be 1D arrays.")

    if depth.size < 2:
        raise ValueError("At least two depth points are required.")

    if depth.shape != density.shape:
        raise ValueError("Depth and density must have equal shapes.")

    if depth[0] != 0:
        raise ValueError("Depth must start at zero.")

    dh = np.diff(depth)

    if np.any(dh <= 0):
        raise ValueError("Depth must increase strictly.")

    average_density = (density[:-1] + density[1:]) / 2

    pressure_increments = average_density * gravity * dh

    pressure = float(surface_pressure) + np.concatenate(
        (
            [0.0],
            np.cumsum(pressure_increments),
        )
    )

    return pressure