Gas.swift (7146B)
1 // SPDX-License-Identifier: GPL-2.0-only 2 import Foundation 3 4 /// Where the dive takes place: pressure at the surface and density of the water. 5 public struct DiveEnvironment: Codable, Sendable, Equatable { 6 /// Bar. 1.01325 at sea level; lower in altitude. 7 public var surfacePressure = 1.01325 8 /// Kilograms per litre: 1.03 for sea water, 1.00 for fresh water. 9 public var waterDensity = 1.03 10 11 public init(surfacePressure: Double = 1.01325, waterDensity: Double = 1.03) { 12 self.surfacePressure = surfacePressure 13 self.waterDensity = waterDensity 14 } 15 16 public static let sea = DiveEnvironment() 17 public static let freshWater = DiveEnvironment(waterDensity: 1.0) 18 19 public var barPerMeter: Double { waterDensity * 0.0980665 } 20 /// Absolute pressure at a depth, in bar. 21 public func pressure(atDepth meters: Double) -> Double { surfacePressure + max(0, meters) * barPerMeter } 22 /// Depth where the absolute pressure is `bar`; 0 above the surface. 23 public func depth(atPressure bar: Double) -> Double { max(0, (bar - surfacePressure) / barPerMeter) } 24 } 25 26 /// A breathing gas: fractions of oxygen and helium, the rest being nitrogen. 27 public struct BreathingGas: Codable, Sendable, Hashable { 28 public var o2: Double 29 public var he: Double 30 31 public init(o2: Double, he: Double = 0) { 32 self.o2 = min(max(o2, 0.01), 1) 33 self.he = min(max(he, 0), 1 - self.o2) 34 } 35 36 public init(o2Percent: Int, hePercent: Int = 0) { 37 self.init(o2: Double(o2Percent) / 100, he: Double(hePercent) / 100) 38 } 39 40 public static let air = BreathingGas(o2: 0.21) 41 public static let oxygen = BreathingGas(o2: 1) 42 /// Fraction of nitrogen in air, as Subsurface takes it. 43 static let nitrogenInAir = 0.79 44 45 public var n2: Double { max(0, 1 - o2 - he) } 46 public var o2Percent: Int { Int((o2 * 100).rounded()) } 47 public var hePercent: Int { Int((he * 100).rounded()) } 48 public var isAir: Bool { o2Percent == 21 && hePercent == 0 } 49 50 /// "Air", "EAN32", "Tx18/45", "O2". 51 public var name: String { 52 if hePercent > 0 { return "Tx\(o2Percent)/\(hePercent)" } 53 if o2Percent >= 100 { return "O2" } 54 return isAir ? "Air" : "EAN\(o2Percent)" 55 } 56 57 /// Partial pressure of oxygen at a depth, in bar. 58 public func ppO2(atDepth meters: Double, in environment: DiveEnvironment = .sea) -> Double { 59 environment.pressure(atDepth: meters) * o2 60 } 61 62 /// Maximum operating depth for a limit on the partial pressure of oxygen, in metres. 63 public func maximumOperatingDepth(ppO2 limit: Double, in environment: DiveEnvironment = .sea) -> Double { 64 environment.depth(atPressure: limit / o2) 65 } 66 67 /// The same, rounded down to a multiple of `step` metres; a depth within a tenth of the next step counts for 68 /// it, so that oxygen at 1.6 bar is breathed from 6 m (Subsurface's rounding). 69 public func switchDepth(ppO2 limit: Double = 1.6, step: Double = 3, in environment: DiveEnvironment = .sea) -> Double { 70 (maximumOperatingDepth(ppO2: limit, in: environment) / step + 0.1).rounded(.down) * step 71 } 72 73 /// Shallowest depth where the gas gives at least `limit` bar of oxygen (0: breathable at the surface). 74 public func minimumDepth(ppO2 limit: Double = 0.16, in environment: DiveEnvironment = .sea) -> Double { 75 environment.depth(atPressure: limit / o2) 76 } 77 78 /// Equivalent narcotic depth: the depth where air would be as narcotic. With `oxygenNarcotic`, only helium is 79 /// counted as not narcotic (the cautious convention); otherwise only nitrogen is narcotic. 80 public func equivalentNarcoticDepth(atDepth meters: Double, oxygenNarcotic: Bool = true, in environment: DiveEnvironment = .sea) -> Double { 81 let pressure = environment.pressure(atDepth: meters) 82 let narcotic = oxygenNarcotic ? pressure * (1 - he) : pressure * n2 / Self.nitrogenInAir 83 return environment.depth(atPressure: narcotic) 84 } 85 86 /// Equivalent air depth of a nitrox: the depth where air gives the same partial pressure of nitrogen. 87 public func equivalentAirDepth(atDepth meters: Double, in environment: DiveEnvironment = .sea) -> Double { 88 environment.depth(atPressure: environment.pressure(atDepth: meters) * n2 / Self.nitrogenInAir) 89 } 90 91 /// Density of the gas at a depth, in grams per litre (5.2 is the usual recommended limit, 6.2 the hard one). 92 public func density(atDepth meters: Double, in environment: DiveEnvironment = .sea) -> Double { 93 (o2 * 1.429 + he * 0.1786 + n2 * 1.2506) * environment.pressure(atDepth: meters) / 1.01325 94 } 95 96 /// The richest mix for a depth: oxygen for the limit on its partial pressure, and just enough helium to keep 97 /// the equivalent narcotic depth at `maximumEND` (nil: no helium). 98 public static func bestMix(forDepth meters: Double, ppO2 limit: Double = 1.4, maximumEND: Double? = nil, 99 oxygenNarcotic: Bool = true, in environment: DiveEnvironment = .sea) -> BreathingGas { 100 let pressure = environment.pressure(atDepth: meters) 101 // Whole percents, rounded down: never above the limit. 102 let o2 = min(1, (limit / pressure * 100).rounded(.down) / 100) 103 var he = 0.0 104 if let maximumEND, meters > maximumEND { 105 let allowed = environment.pressure(atDepth: maximumEND) 106 let fraction = oxygenNarcotic ? 1 - allowed / pressure : 1 - o2 - allowed * nitrogenInAir / pressure 107 he = min(1 - o2, max(0, (fraction * 100).rounded(.up) / 100)) 108 } 109 return BreathingGas(o2: o2, he: he) 110 } 111 } 112 113 /// Oxygen exposure: share of the daily "CNS clock" (NOAA limits) and pulmonary units (OTU). 114 public enum OxygenExposure { 115 /// Minutes allowed at a partial pressure of oxygen, for a single exposure (NOAA). 116 private static let limits: [(ppO2: Double, minutes: Double)] = [ 117 (0.5, .infinity), (0.6, 720), (0.7, 570), (0.8, 450), (0.9, 360), (1.0, 300), (1.1, 240), 118 (1.2, 210), (1.3, 180), (1.4, 150), (1.5, 120), (1.6, 45), 119 ] 120 121 /// Fraction of the CNS clock used by `seconds` at a partial pressure of oxygen (1 = 100 %). 122 public static func cns(ppO2: Double, seconds: Double) -> Double { 123 guard ppO2 > 0.5 else { return 0 } 124 if ppO2 >= 1.6 { 125 // Beyond the table: the limit halves every 0.1 bar or so (it is already an emergency). 126 return seconds / 60 / (45 * pow(0.5, (ppO2 - 1.6) / 0.1)) 127 } 128 let upper = limits.firstIndex { $0.ppO2 >= ppO2 } ?? limits.count - 1 129 let high = limits[upper], low = limits[max(1, upper - 1)] 130 guard upper > 1, high.ppO2 > low.ppO2 else { return seconds / 60 / high.minutes } 131 let allowed = low.minutes + (high.minutes - low.minutes) * (ppO2 - low.ppO2) / (high.ppO2 - low.ppO2) 132 return seconds / 60 / allowed 133 } 134 135 /// Oxygen tolerance units for `seconds` at a partial pressure of oxygen. 136 public static func otu(ppO2: Double, seconds: Double) -> Double { 137 guard ppO2 > 0.5 else { return 0 } 138 return seconds / 60 * pow((ppO2 - 0.5) / 0.5, 5.0 / 6.0) 139 } 140 }