remora-kit

RemoraKit, shared dive computer engine (Buhlmann ZHL-16C + GF) and SwiftUI components for Open Remora and Open Merou
git clone https://git.schurger.org/remora-kit.git
Log | Files | Refs | README | LICENSE

Physics.swift (3125B)


      1 // SPDX-License-Identifier: GPL-2.0-only
      2 import Foundation
      3 
      4 /// Engine units: pressures in bar (absolute), depths in metres, durations in minutes.
      5 /// The watch provides the ambient pressure directly: depth is only a display value.
      6 public enum Water: String, Sendable, Hashable, Codable, CaseIterable {
      7     case fresh      // 1000 kg/m³
      8     case en13319    // 1020 kg/m³, dive computer convention (EN 13319)
      9     case salt       // 1025 kg/m³
     10 
     11     public var density: Double {
     12         switch self {
     13         case .fresh: 1000
     14         case .en13319: 1020
     15         case .salt: 1025
     16         }
     17     }
     18 
     19     /// Pressure added per metre of water, in bar.
     20     public var barPerMetre: Double { density * 9.80665 / 100_000 }
     21 }
     22 
     23 /// Water vapour pressure in the lungs (Bühlmann value), in bar.
     24 public let waterVapourPressure = 0.0627
     25 
     26 /// Standard pressure at sea level, in bar.
     27 public let standardSurfacePressure = 1.01325
     28 
     29 /// Depth ↔ pressure conversion for a given surface pressure and water type.
     30 public struct DepthScale: Sendable, Hashable {
     31     public var surfacePressure: Double
     32     public var water: Water
     33 
     34     public init(surfacePressure: Double = standardSurfacePressure, water: Water = .en13319) {
     35         precondition(surfacePressure > 0.5 && surfacePressure < 1.2, "implausible surface pressure")
     36         self.surfacePressure = surfacePressure
     37         self.water = water
     38     }
     39 
     40     public func pressure(atDepth depth: Double) -> Double {
     41         surfacePressure + max(0, depth) * water.barPerMetre
     42     }
     43 
     44     public func depth(atPressure pressure: Double) -> Double {
     45         max(0, (pressure - surfacePressure) / water.barPerMetre)
     46     }
     47 }
     48 
     49 /// Breathing gas. Helium is not supported (recreational diving on air or nitrox, 40 m at most).
     50 public struct Gas: Sendable, Hashable, Codable, CustomStringConvertible {
     51     public let o2: Double
     52     public var n2: Double { 1 - o2 }
     53 
     54     public init(o2: Double) {
     55         precondition(o2 >= 0.16 && o2 <= 1.0, "oxygen fraction out of range")
     56         self.o2 = o2
     57     }
     58 
     59     public static let air = Gas(o2: 0.21)
     60     public static func nitrox(_ percent: Int) -> Gas { Gas(o2: Double(percent) / 100) }
     61 
     62     public var description: String { o2 == 0.21 ? "Air" : "EAN\(Int((o2 * 100).rounded()))" }
     63 
     64     /// Partial pressure of oxygen at an ambient pressure.
     65     public func ppO2(atPressure pressure: Double) -> Double { pressure * o2 }
     66 
     67     /// Maximum operating depth for a ppO2 limit.
     68     public func maximumOperatingDepth(ppO2Limit: Double, scale: DepthScale) -> Double {
     69         scale.depth(atPressure: ppO2Limit / o2)
     70     }
     71 
     72     /// Rebreather replays only (comparison with logged closed-circuit dives, see ShearwaterComparisonTests);
     73     /// no app offers it. The loop holds this ppO2 (bar) and `o2` describes the diluent, whose inert part
     74     /// is all nitrogen.
     75     var loopPPO2: Double?
     76 
     77     /// Inspired partial pressure of nitrogen (in the alveoli).
     78     func inspiredN2(atPressure pressure: Double) -> Double {
     79         let dry = max(0, pressure - waterVapourPressure)
     80         guard let loopPPO2 else { return dry * n2 }
     81         return max(0, dry - loopPPO2)
     82     }
     83 }