Skip to main content

deep_causality_physics/fluids/
mechanics.rs

1/*
2 * SPDX-License-Identifier: MIT
3 * Copyright (c) 2023 - 2026. The DeepCausality Authors and Contributors. All Rights Reserved.
4 */
5
6use crate::{Density, G, Length, PhysicsError, Pressure, Speed};
7
8/// Calculates hydrostatic pressure: $P = P_0 + \rho g h$.
9///
10/// # Arguments
11/// * `p0` - Surface pressure / known reference pressure.
12/// * `density` - Fluid density ($\rho$).
13/// * `depth` - Depth below the surface or reference point ($h$).
14///
15/// # Returns
16/// * `Ok(Pressure)` - Total pressure at depth.
17pub fn hydrostatic_pressure_kernel(
18    p0: &Pressure,
19    density: &Density,
20    depth: &Length,
21) -> Result<Pressure, PhysicsError> {
22    // P = P0 + rho * g * h
23    let rho_g_h = density.value() * G * depth.value();
24    let p_total = p0.value() + rho_g_h;
25
26    Pressure::new(p_total)
27}
28
29/// Calculates pressure $P_2$ using Bernoulli's principle.
30///
31/// $P_1 + \frac{1}{2}\rho v_1^2 + \rho g h_1 = P_2 + \frac{1}{2}\rho v_2^2 + \rho g h_2$
32///
33/// Solves for $P_2$:
34/// $P_2 = P_1 + \frac{1}{2}\rho(v_1^2 - v_2^2) + \rho g(h_1 - h_2)$
35///
36/// # Arguments
37/// * `p1` - Pressure at point 1.
38/// * `v1` - Velocity at point 1.
39/// * `h1` - Elevation at point 1.
40/// * `v2` - Velocity at point 2.
41/// * `h2` - Elevation at point 2.
42/// * `density` - Fluid density.
43///
44/// # Returns
45/// * `Ok(Pressure)` - Pressure at point 2.
46pub fn bernoulli_pressure_kernel(
47    p1: &Pressure,
48    v1: &Speed,
49    h1: &Length,
50    v2: &Speed,
51    h2: &Length,
52    density: &Density,
53) -> Result<Pressure, PhysicsError> {
54    // P1 + 0.5 * rho * v1^2 + rho * g * h1 = P2 + 0.5 * rho * v2^2 + rho * g * h2
55    // Solve for P2:
56    // P2 = P1 + 0.5*rho*(v1^2 - v2^2) + rho*g*(h1 - h2)
57    let rho = density.value();
58    // Use proper f64 methods
59    let term_kinetic = 0.5 * rho * (v1.value().powi(2) - v2.value().powi(2));
60    let term_potential = rho * G * (h1.value() - h2.value());
61
62    let p2 = p1.value() + term_kinetic + term_potential;
63
64    Pressure::new(p2)
65}