shearwater_fit.py (5161B)
1 # SPDX-License-Identifier: GPL-2.0-only 2 """Fits the model to the tissues the Shearwater logs during the dive (records 0x70-0x75, ~every 5 min), 3 then looks for the criterion behind its no-decompression time. Usage: python3 -I shearwater_fit.py path/dive_data.db""" 4 import gzip, math, sqlite3, struct, sys 5 6 HALF = [5.0, 8.0, 12.5, 18.5, 27.0, 38.3, 54.3, 77.0, 109.0, 146.0, 187.0, 239.0, 305.0, 390.0, 498.0, 635.0] 7 A = [1.1696, 1.0, 0.8618, 0.7562, 0.62, 0.5043, 0.441, 0.4, 0.375, 0.35, 0.3295, 0.3065, 0.2835, 0.261, 0.248, 0.2327] 8 B = [0.5578, 0.6514, 0.7222, 0.7825, 0.8126, 0.8434, 0.8693, 0.891, 0.9092, 0.9222, 0.9319, 0.9403, 0.9477, 0.9544, 0.9602, 0.9653] 9 10 def load(db_path): 11 db = sqlite3.connect(f"file:{db_path}?mode=ro", uri=True) 12 out = [] 13 for log_id, name, blob in db.execute("select log_id, file_name, data_bytes_1 from log_data order by log_id"): 14 data = gzip.decompress(blob[4:]); recs = [data[i:i + 32] for i in range(0, len(data), 32)] 15 o0 = next(r for r in recs if r[0] == 0x10); o1 = next(r for r in recs if r[0] == 0x11) 16 samples, snaps, buf = [], [], b"" 17 for r in recs: 18 if r[0] == 0x01: 19 stop = struct.unpack(">H", r[3:5])[0] 20 samples.append(dict(depth=struct.unpack(">H", r[1:3])[0] / 10, o2=r[8] / 100, stop=stop, ndl=r[10], tts=struct.unpack(">H", r[5:7])[0])) 21 elif 0x70 <= r[0] <= 0x75: 22 buf += r[4:32] 23 if r[0] == 0x75: 24 snaps.append((len(samples), list(struct.unpack("<16f", buf[:64])))); buf = b"" 25 out.append(dict(name=name.split("]")[1][:20], surface=struct.unpack(">H", o1[16:18])[0] / 1000, gf=(o0[4] / 100, o0[5] / 100), 26 samples=samples, snaps=snaps)) 27 return out 28 29 def step_tissues(t, p0, p1, fn2, minutes, vapour, half): 30 pi0 = (p0 - vapour) * fn2; rate = ((p1 - vapour) * fn2 - pi0) / minutes 31 out = [] 32 for x, h in zip(t, half): 33 k = math.log(2) / h 34 out.append(pi0 + rate * (minutes - 1 / k) - (pi0 - x - rate / k) * math.exp(-k * minutes)) 35 return out 36 37 def fit(dives): 38 for vapour in (0.0627, 0.0567, 0.0493): 39 for density in (1000, 1020, 1025): 40 for half0 in (5.0, 4.0): 41 half = [half0] + HALF[1:]; bar_m = density * 9.80665 / 1e5; errs = []; fast = [] 42 for d in dives: 43 t = d["snaps"][0][1]; prev = d["surface"]; nxt = 1 44 for i, s in enumerate(d["samples"]): 45 p = d["surface"] + s["depth"] * bar_m 46 t = step_tissues(t, prev, p, 1 - s["o2"], 10 / 60, vapour, half); prev = p 47 if nxt < len(d["snaps"]) and d["snaps"][nxt][0] == i + 1: 48 diff = [a - b for a, b in zip(t, d["snaps"][nxt][1])] 49 errs += diff; fast.append(diff[0]); nxt += 1 50 rms = math.sqrt(sum(e * e for e in errs) / len(errs)) 51 print(f"vapour {vapour} density {density} t½₁ {half0}: mean {sum(errs)/len(errs):+.4f} bar, RMS {rms:.4f}, worst {max(abs(e) for e in errs):.4f}, compartment 1 mean {sum(fast)/len(fast):+.4f} ({len(errs)//16} snapshots)") 52 53 if __name__ == "__main__": 54 fit(load(sys.argv[1])) 55 56 def ceiling(t, gf): 57 return max((p - a * gf) / (gf / b - gf + 1) for p, a, b in zip(t, A, B)) 58 59 def ndl(t, p, surface, fn2, gf_high, vapour, credit, bar_m, rate=10.0, limit=99): 60 """Longest stay (whole minutes) after which one still surfaces under GF high; credit = ascent off-gassing counted.""" 61 def ok(m): 62 x = step_tissues(t, p, p, fn2, m, vapour, HALF) if m else list(t) 63 if credit: 64 minutes = max(1e-6, (p - surface) / bar_m / rate) 65 x = step_tissues(x, p, surface, fn2, minutes, vapour, HALF) 66 return ceiling(x, gf_high) <= surface + 1e-9 67 last = -1 68 for m in range(0, limit + 1): 69 if ok(m): last = m 70 elif last >= 0: break 71 return max(last, 0) 72 73 def criteria(dives, vapour, density): 74 bar_m = density * 9.80665 / 1e5 75 rows = {False: [], True: []} 76 for d in dives: 77 for idx, t in d["snaps"][1:]: 78 s = d["samples"][idx - 1] 79 if s["stop"] or s["ndl"] >= 99 or s["ndl"] == 0 or s["depth"] < 9: continue 80 p = d["surface"] + s["depth"] * bar_m 81 for credit in (False, True): 82 rows[credit].append((ndl(t, p, d["surface"], 1 - s["o2"], d["gf"][1], vapour, credit, bar_m) - s["ndl"], s["ndl"], s["depth"], d["name"])) 83 for credit, r in rows.items(): 84 e = [x[0] for x in r] 85 print(f"vapour {vapour} density {density} {'with' if credit else 'without'} ascent off-gassing: {len(e)} snapshots, mean diff {sum(e)/len(e):+.2f} min, absolute {sum(abs(x) for x in e)/len(e):.2f}, worst {max(e, key=abs):+d}") 86 return rows 87 88 if __name__ == "__main__": 89 dives = load(sys.argv[1]) 90 for vapour, density in ((0.0627, 1000), (0.0493, 1000), (0.0627, 1020)): 91 rows = criteria(dives, vapour, density) 92 for credit in (False, True): 93 print("detail", "with" if credit else "without", [(x[0], x[1], x[2]) for x in rows[credit]][:40])