AscentPlanner.swift (7850B)
1 // SPDX-License-Identifier: GPL-2.0-only 2 import Foundation 3 4 /// Ascent and stop settings. 5 public struct DecoSettings: Sendable, Hashable { 6 public var gradientFactors: GradientFactors = .conservative 7 public var ascentRate: Double = 9 // m/min 8 /// Assumed rate used to credit off-gassing during the ascent in the no-stop time (m/min). 9 /// Deliberately fast: partial credit, matched to a Shearwater (see ShearwaterComparisonTests). 10 public var noStopAscentRate: Double = 30 11 public var stopIncrement: Double = 3 // m 12 public var lastStopDepth: Double = 3 // m 13 public var safetyStopDepth: Double = 5 // m 14 public var safetyStopMinutes: Double = 3 15 public var safetyStopTriggerDepth: Double = 10 // m of maximum depth reached 16 17 public init() {} 18 } 19 20 public struct DecoStop: Sendable, Hashable { 21 public let depth: Double // m 22 public let minutes: Int 23 /// Stop duration to the nearest 10 s (countdown display; first stop only). 24 public var seconds: Int? 25 26 public init(depth: Double, minutes: Int, seconds: Int? = nil) { 27 self.depth = depth 28 self.minutes = minutes 29 self.seconds = seconds 30 } 31 } 32 33 public struct AscentPlan: Sendable, Hashable { 34 /// Remaining no-stop time at the current depth (min); `nil` = unlimited; 0 = in decompression. 35 public let noDecompressionLimit: Double? 36 /// Mandatory stops, deepest first (empty outside decompression). 37 public let stops: [DecoStop] 38 /// Total ascent time (mandatory stops included, safety stop excluded), min. 39 public let timeToSurface: Double 40 /// Reference depth of the `low` factor (first stop), to be kept for the whole dive. 41 public let gradientAnchorDepth: Double? 42 43 public var inDecompression: Bool { !stops.isEmpty } 44 } 45 46 public enum AscentPlanner { 47 /// Computes the ascent from `pressure` with the current tissue state. 48 /// `anchorDepth`: first stop already set earlier in the dive (the `low` factor stays anchored there). 49 /// 50 /// Outside decompression, the criterion is the one commercial computers use: a direct ascent must 51 /// reach the surface under the `high` factor (the `low` factor only serves to place the stops). 52 /// The no-stop time is the longest stay (whole minutes, 99 at most) at the current depth that still 53 /// allows it; `nil` = more than 99 min. 54 public static func plan(tissues: TissueState, pressure: Double, gas: Gas, 55 scale: DepthScale, settings: DecoSettings, 56 anchorDepth: Double? = nil) -> AscentPlan { 57 let depthNow = scale.depth(atPressure: pressure) 58 59 func surfacesClear(after minutes: Int) -> Bool { 60 var t = tissues 61 t.stay(atPressure: pressure, gas: gas, minutes: Double(minutes)) 62 t.change(from: pressure, to: scale.surfacePressure, gas: gas, 63 minutes: depthNow / settings.noStopAscentRate) 64 return t.ceiling(gradientFactor: settings.gradientFactors.high) <= scale.surfacePressure + 1e-9 65 } 66 67 if anchorDepth != nil || !surfacesClear(after: 0) { 68 let now = ascent(tissues: tissues, pressure: pressure, gas: gas, scale: scale, 69 settings: settings, anchorDepth: anchorDepth) 70 if !now.stops.isEmpty || !surfacesClear(after: 0) { return now } 71 } 72 73 var limit: Double? 74 if !surfacesClear(after: 99) { 75 var low = 0, high = 99 // `low` passes, `high` does not 76 while high - low > 1 { 77 let middle = (low + high) / 2 78 if surfacesClear(after: middle) { low = middle } else { high = middle } 79 } 80 limit = Double(low) 81 } 82 return AscentPlan(noDecompressionLimit: limit, stops: [], timeToSurface: depthNow / settings.ascentRate, 83 gradientAnchorDepth: anchorDepth) 84 } 85 86 /// Immediate ascent: required stops (none = direct ascent possible) and duration. 87 static func ascent(tissues: TissueState, pressure: Double, gas: Gas, 88 scale: DepthScale, settings: DecoSettings, 89 anchorDepth: Double?) -> AscentPlan { 90 let gf = settings.gradientFactors 91 let depthNow = scale.depth(atPressure: pressure) 92 93 var t = tissues 94 var depth = depthNow 95 var anchor = anchorDepth 96 var stops: [DecoStop] = [] 97 var tts = 0.0 98 99 func gfAt(_ d: Double) -> Double { 100 guard let a = anchor, a > 0 else { return gf.low } 101 return gf.high - (gf.high - gf.low) * min(1, max(0, d / a)) 102 } 103 104 func nextStop(below d: Double) -> Double { 105 let step = settings.stopIncrement 106 let next = (((d - 1e-6) / step).rounded(.up) - 1) * step 107 return next < settings.lastStopDepth ? 0 : next 108 } 109 110 func ascend(_ state: TissueState, from: Double, to: Double) -> TissueState { 111 var s = state 112 s.change(from: scale.pressure(atDepth: from), to: scale.pressure(atDepth: to), 113 gas: gas, minutes: (from - to) / settings.ascentRate) 114 return s 115 } 116 117 func clear(_ state: TissueState, at d: Double) -> Bool { 118 state.ceiling(gradientFactor: gfAt(d)) <= scale.pressure(atDepth: d) + 1e-9 119 } 120 121 // Diver between two stops (or above their own) who cannot go up to the next one: their stop is on 122 // the grid, at their depth or deeper, never at an arbitrary depth. Revision 0.2: before, a diver 123 // who had come up to 2.4 m was offered a "stop at 2 m". 124 if depth > 0, !clear(t, at: nextStop(below: depth)) { 125 let step = settings.stopIncrement 126 var grid = max(settings.lastStopDepth, (depth / step - 1e-6).rounded(.up) * step) 127 if abs(grid - depth) > 1e-6 { 128 while !clear(t, at: grid) && grid < 60 { grid += step } 129 } 130 // Also for a diver right on the stop: a depth of 2.9999999999999996 m (pressure converted 131 // back to a depth) must not be reported, the display would truncate it to "2 m". 132 depth = grid 133 } 134 135 while depth > 0 { 136 let next = nextStop(below: depth) 137 let trial = ascend(t, from: depth, to: next) 138 // Conservative: the next stop must be tolerated BEFORE going up (no credit for the way up). 139 if clear(t, at: next) { 140 t = trial 141 tts += (depth - next) / settings.ascentRate 142 depth = next 143 continue 144 } 145 // Stop at `depth`: the anchor follows the first (deepest) stop, then we wait minute by minute. 146 if anchor.map({ depth > $0 }) ?? true { anchor = depth } 147 // First stop: duration to the nearest 10 s, for the countdown (on a copy: the plan keeps its 148 // whole minutes, which are more conservative). 149 var seconds: Int? 150 if stops.isEmpty { 151 var fine = t 152 var s = 0 153 repeat { 154 fine.stay(atPressure: scale.pressure(atDepth: depth), gas: gas, minutes: 10.0 / 60) 155 s += 10 156 } while !clear(fine, at: next) && s < 999 * 60 157 seconds = s 158 } 159 var waited = 0 160 repeat { 161 t.stay(atPressure: scale.pressure(atDepth: depth), gas: gas, minutes: 1) 162 waited += 1 163 } while !clear(t, at: next) && waited < 999 164 stops.append(DecoStop(depth: depth, minutes: waited, seconds: seconds)) 165 tts += Double(waited) 166 } 167 168 return AscentPlan(noDecompressionLimit: 0, stops: stops, 169 timeToSurface: tts, gradientAnchorDepth: anchor) 170 } 171 }