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

commit 4c1a6a25f0e987ec14c1c12ee2c594ef0dc2aafb
parent d6277d6b0a98f7370ea01d7f36c5c4a41c12dd1e
Author: Jean Schurger <jean@schurger.org>
Date:   Fri, 13 Nov 2026 15:08:39 -0500

Comparison with a Shearwater: decoder for the Shearwater Cloud export (PNF) and a test that replays the dives through the engine

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K7deYGcjJYQu2t1EUEKgbs

Diffstat:
ATests/RemoraDecoTests/ShearwaterComparisonTests.swift | 110+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 110 insertions(+), 0 deletions(-)

diff --git a/Tests/RemoraDecoTests/ShearwaterComparisonTests.swift b/Tests/RemoraDecoTests/ShearwaterComparisonTests.swift @@ -0,0 +1,110 @@ +import Foundation +import Testing +@testable import RemoraDeco + +/// Compare le moteur à un ordinateur certifié : rejoue des plongées Shearwater (même algorithme, mêmes GF) +/// et confronte le temps sans palier et les paliers à ceux que l'ordinateur affichait. +/// Données personnelles hors dépôt : `tools/shearwater_decode.py` produit le JSON, chemin dans SHEARWATER_JSON. +struct ShearwaterDive: Decodable { + struct Sample: Decodable { + let depth: Double + let stop_depth: Int + let tts: Int + let o2: Int + let ndl_or_stop: Int + let ceiling: Int + } + let number: String? + let site: String? + let date: String + let gf_low: Int + let gf_high: Int + let surface_mbar: Int + let sample_ms: Int + let samples: [Sample] +} + +@Suite("Comparaison Shearwater") +struct ShearwaterComparisonTests { + static let path = ProcessInfo.processInfo.environment["SHEARWATER_JSON"] + + @Test(.enabled(if: path != nil)) + func replayAgainstShearwater() throws { + let dives = try JSONDecoder().decode([ShearwaterDive].self, + from: Data(contentsOf: URL(fileURLWithPath: Self.path!))) + let dates = DateFormatter() + dates.locale = Locale(identifier: "en_US_POSIX") + dates.timeZone = TimeZone(secondsFromGMT: 0) + dates.dateFormat = "yyyy-MM-dd HH:mm:ss" + + var previous: (end: Date, tissues: TissueState)? + var allNDL: [Double] = [] + var shortNDL: [Double] = [] // quand le Shearwater affiche 30 min ou moins : là où ça compte + for dive in dives { + let start = try #require(dates.date(from: dive.date)) + let surface = Double(dive.surface_mbar) / 1000 + let step = Double(dive.sample_ms) / 1000 + + var config = DiveComputer.Configuration() + config.gas = Gas(o2: Double(dive.samples[0].o2) / 100) + config.water = .en13319 + config.deco.gradientFactors = GradientFactors(low: Double(dive.gf_low) / 100, high: Double(dive.gf_high) / 100) + config.deco.ascentRate = 10 + + var tissues: TissueState? + var interval: Double? + if let p = previous, start.timeIntervalSince(p.end) < 48 * 3600 { + interval = start.timeIntervalSince(p.end) / 60 + tissues = DiveComputer.surfaceInterval(p.tissues, minutes: interval!, surfacePressure: surface) + } + var computer = DiveComputer(configuration: config, surfacePressure: surface, tissues: tissues) + computer.update(time: 0, pressure: surface) + + var ndlDiffs: [Double] = [] + var stopRows: [String] = [] + var firstDeco: String? + for (i, s) in dive.samples.enumerated() { + computer.update(time: Double(i + 1) * step, pressure: computer.scale.pressure(atDepth: s.depth)) + let plan = computer.status.plan + if s.stop_depth == 0, s.ndl_or_stop > 0, s.ndl_or_stop < 99, s.depth > 9, !plan.inDecompression { + let diff = floor(plan.noDecompressionLimit ?? 99) - Double(s.ndl_or_stop) + ndlDiffs.append(diff) + if s.ndl_or_stop <= 30 { shortNDL.append(diff) } + if s.ndl_or_stop <= 30, abs(diff) >= 5, i % 6 == 0 { + print(" écart t=\(Int(Double(i + 1) * step / 60))' \(s.depth) m : Shearwater \(s.ndl_or_stop) min, Rémora \(Int(plan.noDecompressionLimit ?? 99)) min") + } + } + if s.stop_depth > 0 || plan.inDecompression { + let ours = plan.stops.first.map { "\(Int($0.depth)) m \($0.minutes) min, surface \(Int(plan.timeToSurface.rounded(.up)))" } ?? "aucun" + let theirs = s.stop_depth > 0 ? "\(s.stop_depth) m \(s.ndl_or_stop) min, surface \(s.tts)" : "aucun" + if firstDeco == nil { firstDeco = "t=\(Int(Double(i + 1) * step / 60)) min" } + if i % 12 == 0 { stopRows.append(" t=\(Int(Double(i + 1) * step / 60))' \(s.depth) m | Shearwater \(theirs) | Rémora \(ours)") } + } + } + allNDL.append(contentsOf: ndlDiffs) + let mean = ndlDiffs.isEmpty ? 0 : ndlDiffs.reduce(0, +) / Double(ndlDiffs.count) + let worst = ndlDiffs.map { abs($0) }.max() ?? 0 + print("Plongée \(dive.number ?? "?") \(dive.site ?? "") GF \(dive.gf_low)/\(dive.gf_high) O2 \(dive.samples[0].o2) %" + + (interval.map { " (intervalle \(Int($0)) min)" } ?? "") + + String(format: " : sans palier, %d points, écart moyen %+.1f min, pire %.0f min", ndlDiffs.count, mean, worst)) + stopRows.forEach { print($0) } + + let duration = Double(dive.samples.count) * step + previous = (start.addingTimeInterval(duration), computer.tissues) + } + let mean = allNDL.reduce(0, +) / Double(max(1, allNDL.count)) + let meanAbs = allNDL.map { abs($0) }.reduce(0, +) / Double(max(1, allNDL.count)) + print(String(format: "TOTAL sans palier : %d points, écart moyen %+.2f min, écart absolu moyen %.2f min", allNDL.count, mean, meanAbs)) + let shortMean = shortNDL.reduce(0, +) / Double(max(1, shortNDL.count)) + let shortAbs = shortNDL.map { abs($0) }.reduce(0, +) / Double(max(1, shortNDL.count)) + let within2 = Double(shortNDL.filter { abs($0) <= 2 }.count) / Double(max(1, shortNDL.count)) * 100 + print(String(format: "COURT (Shearwater ≤ 30 min) : %d points, écart moyen %+.2f min, absolu moyen %.2f min, pire %.0f min, %.0f %% à 2 min près", + shortNDL.count, shortMean, shortAbs, shortNDL.map { abs($0) }.max() ?? 0, within2)) + // Résultat du 2026-11-13 (10 plongées d'un Petrel 3) : paliers identiques à une minute près ; temps sans + // palier en moyenne 1,9 min plus court que le Shearwater quand il affiche 30 min ou moins. Une variante + // qui compte la désaturation pendant la remontée donnait 2 min de PLUS que lui : écartée, on reste prudent. + #expect(meanAbs < 3.5) + #expect(shortAbs < 3) + #expect(shortMean < 0, "le moteur doit rester du côté prudent par rapport au Shearwater") + } +}