Tissues.swift (5693B)
1 // SPDX-License-Identifier: GPL-2.0-only 2 import Foundation 3 4 /// One compartment of the Bühlmann model: half-time (min) and coefficients a (bar) and b. 5 public struct Compartment: Sendable, Hashable { 6 public let halfTime: Double 7 public let a: Double 8 public let b: Double 9 10 var k: Double { log(2) / halfTime } 11 } 12 13 /// Bühlmann ZHL-16C, nitrogen (compartment 1b for the first one). 14 public enum ZHL16C { 15 public static let nitrogen: [Compartment] = [ 16 Compartment(halfTime: 5.0, a: 1.1696, b: 0.5578), 17 Compartment(halfTime: 8.0, a: 1.0000, b: 0.6514), 18 Compartment(halfTime: 12.5, a: 0.8618, b: 0.7222), 19 Compartment(halfTime: 18.5, a: 0.7562, b: 0.7825), 20 Compartment(halfTime: 27.0, a: 0.6200, b: 0.8126), 21 Compartment(halfTime: 38.3, a: 0.5043, b: 0.8434), 22 Compartment(halfTime: 54.3, a: 0.4410, b: 0.8693), 23 Compartment(halfTime: 77.0, a: 0.4000, b: 0.8910), 24 Compartment(halfTime: 109.0, a: 0.3750, b: 0.9092), 25 Compartment(halfTime: 146.0, a: 0.3500, b: 0.9222), 26 Compartment(halfTime: 187.0, a: 0.3295, b: 0.9319), 27 Compartment(halfTime: 239.0, a: 0.3065, b: 0.9403), 28 Compartment(halfTime: 305.0, a: 0.2835, b: 0.9477), 29 Compartment(halfTime: 390.0, a: 0.2610, b: 0.9544), 30 Compartment(halfTime: 498.0, a: 0.2480, b: 0.9602), 31 Compartment(halfTime: 635.0, a: 0.2327, b: 0.9653), 32 ] 33 } 34 35 /// Gradient factors (Baker): `low` sets the first stop, `high` the surfacing. 36 public struct GradientFactors: Sendable, Hashable, Codable, CustomStringConvertible { 37 public let low: Double 38 public let high: Double 39 40 public init(low: Double, high: Double) { 41 precondition(low > 0 && low <= high && high <= 1, "inconsistent gradient factors") 42 self.low = low 43 self.high = high 44 } 45 46 /// Conservative default setting of Open Rémora (secondary computer). 47 public static let conservative = GradientFactors(low: 0.30, high: 0.70) 48 public static let pureBuhlmann = GradientFactors(low: 1, high: 1) 49 50 public var description: String { "GF \(Int((low * 100).rounded()))/\(Int((high * 100).rounded()))" } 51 } 52 53 /// Nitrogen loading of the 16 compartments, in bar. 54 public struct TissueState: Sendable, Hashable { 55 public private(set) var nitrogen: [Double] 56 let compartments: [Compartment] 57 58 /// Diver saturated at the surface, on air. 59 public static func saturated(surfacePressure: Double = standardSurfacePressure) -> TissueState { 60 let p = Gas.air.inspiredN2(atPressure: surfacePressure) 61 return TissueState(nitrogen: Array(repeating: p, count: ZHL16C.nitrogen.count), 62 compartments: ZHL16C.nitrogen) 63 } 64 65 /// Stay at constant pressure (Haldane equation). 66 public mutating func stay(atPressure pressure: Double, gas: Gas, minutes: Double) { 67 guard minutes > 0 else { return } 68 let pi = gas.inspiredN2(atPressure: pressure) 69 for i in nitrogen.indices { 70 let k = compartments[i].k 71 nitrogen[i] = pi + (nitrogen[i] - pi) * exp(-k * minutes) 72 } 73 } 74 75 /// Linear pressure change (Schreiner equation): exact between two samples. 76 public mutating func change(from start: Double, to end: Double, gas: Gas, minutes: Double) { 77 guard minutes > 0 else { return } 78 let pi0 = gas.inspiredN2(atPressure: start) 79 let rate = (gas.inspiredN2(atPressure: end) - pi0) / minutes 80 for i in nitrogen.indices { 81 let k = compartments[i].k 82 let p0 = nitrogen[i] 83 nitrogen[i] = pi0 + rate * (minutes - 1 / k) - (pi0 - p0 - rate / k) * exp(-k * minutes) 84 } 85 } 86 87 /// Tolerated ambient pressure (ceiling) with a given gradient factor, in bar. 88 public func ceiling(gradientFactor gf: Double) -> Double { 89 var worst = 0.0 90 for (p, c) in zip(nitrogen, compartments) { 91 let tolerated = (p - c.a * gf) / (gf / c.b - gf + 1) 92 worst = max(worst, tolerated) 93 } 94 return worst 95 } 96 97 /// Remaining no-stop time at constant pressure, in minutes (`nil` = unlimited). 98 /// Checks that a direct ascent would stay under the `gfHigh` factor at the surface. 99 public func noDecompressionLimit(atPressure pressure: Double, gas: Gas, 100 surfacePressure: Double, gfHigh gf: Double) -> Double? { 101 let pi = gas.inspiredN2(atPressure: pressure) 102 var limit = Double.infinity 103 for (p0, c) in zip(nitrogen, compartments) { 104 let maximum = surfacePressure * (gf / c.b - gf + 1) + c.a * gf 105 if p0 >= maximum { return 0 } 106 guard pi > maximum else { continue } 107 let t = log((pi - p0) / (pi - maximum)) / c.k 108 limit = min(limit, t) 109 } 110 return limit.isFinite ? limit : nil 111 } 112 } 113 114 /// Tissues saved with the dive (repetitive dives, no-fly time). 115 extension TissueState: Codable { 116 enum CodingKeys: String, CodingKey { case nitrogen } 117 118 public init(from decoder: Decoder) throws { 119 let values = try decoder.container(keyedBy: CodingKeys.self) 120 let n2 = try values.decode([Double].self, forKey: .nitrogen) 121 guard n2.count == ZHL16C.nitrogen.count else { 122 throw DecodingError.dataCorruptedError(forKey: .nitrogen, in: values, 123 debugDescription: "16 compartments expected") 124 } 125 self.init(nitrogen: n2, compartments: ZHL16C.nitrogen) 126 } 127 128 public func encode(to encoder: Encoder) throws { 129 var values = encoder.container(keyedBy: CodingKeys.self) 130 try values.encode(nitrogen, forKey: .nitrogen) 131 } 132 }