2013-01-03 05:21:36 +00:00
|
|
|
/* calculate deco values
|
|
|
|
* based on Bühlmann ZHL-16b
|
|
|
|
* based on an implemention by heinrichs weikamp for the DR5
|
2013-01-08 17:29:07 +00:00
|
|
|
* the original file was given to Subsurface under the GPLv2
|
|
|
|
* by Matthias Heinrichs
|
2013-01-03 05:21:36 +00:00
|
|
|
*
|
2013-01-08 17:29:07 +00:00
|
|
|
* The implementation below is a fairly complete rewrite since then
|
|
|
|
* (C) Robert C. Helling 2013 and released under the GPLv2
|
2013-01-03 05:21:36 +00:00
|
|
|
*
|
2013-01-08 17:29:07 +00:00
|
|
|
* add_segment() - add <seconds> at the given pressure, breathing gasmix
|
|
|
|
* deco_allowed_depth() - ceiling based on lead tissue, surface pressure, 3m increments or smooth
|
|
|
|
* set_gf() - set Buehlmann gradient factors
|
|
|
|
* clear_deco()
|
|
|
|
* cache_deco_state()
|
|
|
|
* restore_deco_state()
|
|
|
|
* dump_tissues()
|
2013-01-03 05:21:36 +00:00
|
|
|
*/
|
2013-01-04 04:45:20 +00:00
|
|
|
#include <math.h>
|
2013-01-06 19:13:46 +00:00
|
|
|
#include <string.h>
|
2013-01-03 05:21:36 +00:00
|
|
|
#include "dive.h"
|
|
|
|
|
|
|
|
//! Option structure for Buehlmann decompression.
|
|
|
|
struct buehlmann_config {
|
2014-02-28 04:09:57 +00:00
|
|
|
double satmult; //! safety at inert gas accumulation as percentage of effect (more than 100).
|
|
|
|
double desatmult; //! safety at inert gas depletion as percentage of effect (less than 100).
|
|
|
|
unsigned int last_deco_stop_in_mtr; //! depth of last_deco_stop.
|
|
|
|
double gf_high; //! gradient factor high (at surface).
|
|
|
|
double gf_low; //! gradient factor low (at bottom/start of deco calculation).
|
|
|
|
double gf_low_position_min; //! gf_low_position below surface_min_shallow.
|
|
|
|
bool gf_low_at_maxdepth; //! if true, gf_low applies at max depth instead of at deepest ceiling.
|
2013-01-03 05:21:36 +00:00
|
|
|
};
|
2014-01-15 18:54:41 +00:00
|
|
|
struct buehlmann_config buehlmann_config = { 1.0, 1.01, 0, 0.75, 0.35, 2.0, false };
|
2013-01-03 05:21:36 +00:00
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
const double buehlmann_N2_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 };
|
2013-01-03 05:21:36 +00:00
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
const double buehlmann_N2_b[] = { 0.5578, 0.6514, 0.7222, 0.7825,
|
|
|
|
0.8126, 0.8434, 0.8693, 0.8910,
|
|
|
|
0.9092, 0.9222, 0.9319, 0.9403,
|
|
|
|
0.9477, 0.9544, 0.9602, 0.9653 };
|
2013-01-03 05:21:36 +00:00
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
const double buehlmann_N2_t_halflife[] = { 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 };
|
2013-01-03 05:21:36 +00:00
|
|
|
|
|
|
|
const double buehlmann_N2_factor_expositon_one_second[] = {
|
|
|
|
2.30782347297664E-003, 1.44301447809736E-003, 9.23769302935806E-004, 6.24261986779007E-004,
|
|
|
|
4.27777107246730E-004, 3.01585140931371E-004, 2.12729727268379E-004, 1.50020603047807E-004,
|
|
|
|
1.05980191127841E-004, 7.91232600646508E-005, 6.17759153688224E-005, 4.83354552742732E-005,
|
2014-02-28 04:09:57 +00:00
|
|
|
3.78761777920511E-005, 2.96212356654113E-005, 2.31974277413727E-005, 1.81926738960225E-005
|
|
|
|
};
|
2013-01-03 05:21:36 +00:00
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
const double buehlmann_He_a[] = { 1.6189, 1.383, 1.1919, 1.0458,
|
|
|
|
0.922, 0.8205, 0.7305, 0.6502,
|
|
|
|
0.595, 0.5545, 0.5333, 0.5189,
|
|
|
|
0.5181, 0.5176, 0.5172, 0.5119 };
|
2013-01-03 05:21:36 +00:00
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
const double buehlmann_He_b[] = { 0.4770, 0.5747, 0.6527, 0.7223,
|
|
|
|
0.7582, 0.7957, 0.8279, 0.8553,
|
|
|
|
0.8757, 0.8903, 0.8997, 0.9073,
|
|
|
|
0.9122, 0.9171, 0.9217, 0.9267 };
|
2013-01-03 05:21:36 +00:00
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
const double buehlmann_He_t_halflife[] = { 1.88, 3.02, 4.72, 6.99,
|
|
|
|
10.21, 14.48, 20.53, 29.11,
|
|
|
|
41.20, 55.19, 70.69, 90.34,
|
|
|
|
115.29, 147.42, 188.24, 240.03 };
|
2013-01-03 05:21:36 +00:00
|
|
|
|
|
|
|
const double buehlmann_He_factor_expositon_one_second[] = {
|
|
|
|
6.12608039419837E-003, 3.81800836683133E-003, 2.44456078654209E-003, 1.65134647076792E-003,
|
|
|
|
1.13084424730725E-003, 7.97503165599123E-004, 5.62552521860549E-004, 3.96776399429366E-004,
|
|
|
|
2.80360036664540E-004, 2.09299583354805E-004, 1.63410794820518E-004, 1.27869320250551E-004,
|
2014-02-28 04:09:57 +00:00
|
|
|
1.00198406028040E-004, 7.83611475491108E-005, 6.13689891868496E-005, 4.81280465299827E-005
|
|
|
|
};
|
2013-01-03 05:21:36 +00:00
|
|
|
|
2013-02-02 18:38:51 +00:00
|
|
|
#define WV_PRESSURE 0.0627 // water vapor pressure in bar
|
2013-01-08 14:37:41 +00:00
|
|
|
#define DECO_STOPS_MULTIPLIER_MM 3000.0
|
|
|
|
|
2013-01-03 05:21:36 +00:00
|
|
|
double tissue_n2_sat[16];
|
|
|
|
double tissue_he_sat[16];
|
|
|
|
int ci_pointing_to_guiding_tissue;
|
2013-01-08 14:37:41 +00:00
|
|
|
double gf_low_pressure_this_dive;
|
2013-01-06 19:13:46 +00:00
|
|
|
#define TISSUE_ARRAY_SZ sizeof(tissue_n2_sat)
|
2013-01-03 07:22:07 +00:00
|
|
|
|
2013-05-30 18:56:00 +00:00
|
|
|
double tolerated_by_tissue[16];
|
|
|
|
|
|
|
|
|
2013-01-08 14:37:41 +00:00
|
|
|
static double tissue_tolerance_calc(const struct dive *dive)
|
2013-01-03 05:21:36 +00:00
|
|
|
{
|
|
|
|
int ci = -1;
|
|
|
|
double tissue_inertgas_saturation, buehlmann_inertgas_a, buehlmann_inertgas_b;
|
2013-01-04 07:56:10 +00:00
|
|
|
double ret_tolerance_limit_ambient_pressure = 0.0;
|
2013-01-08 14:37:41 +00:00
|
|
|
double gf_high = buehlmann_config.gf_high;
|
|
|
|
double gf_low = buehlmann_config.gf_low;
|
2014-01-15 18:54:41 +00:00
|
|
|
double surface = get_surface_pressure_in_mbar(dive, true) / 1000.0;
|
2013-01-03 05:21:36 +00:00
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
for (ci = 0; ci < 16; ci++) {
|
2013-02-08 10:42:34 +00:00
|
|
|
double tolerated;
|
|
|
|
|
2013-01-03 05:21:36 +00:00
|
|
|
tissue_inertgas_saturation = tissue_n2_sat[ci] + tissue_he_sat[ci];
|
|
|
|
buehlmann_inertgas_a = ((buehlmann_N2_a[ci] * tissue_n2_sat[ci]) + (buehlmann_He_a[ci] * tissue_he_sat[ci])) / tissue_inertgas_saturation;
|
|
|
|
buehlmann_inertgas_b = ((buehlmann_N2_b[ci] * tissue_n2_sat[ci]) + (buehlmann_He_b[ci] * tissue_he_sat[ci])) / tissue_inertgas_saturation;
|
|
|
|
|
2013-02-08 10:42:34 +00:00
|
|
|
/* tolerated = (tissue_inertgas_saturation - buehlmann_inertgas_a) * buehlmann_inertgas_b; */
|
2013-01-08 14:37:41 +00:00
|
|
|
|
2013-11-20 15:11:22 +00:00
|
|
|
if (!buehlmann_config.gf_low_at_maxdepth) {
|
|
|
|
double lowest_ceiling = (buehlmann_inertgas_b * tissue_inertgas_saturation - gf_low * buehlmann_inertgas_a * buehlmann_inertgas_b) /
|
2014-02-28 04:09:57 +00:00
|
|
|
((1.0 - buehlmann_inertgas_b) * gf_low + buehlmann_inertgas_b);
|
2013-11-20 15:11:22 +00:00
|
|
|
if (lowest_ceiling > gf_low_pressure_this_dive)
|
|
|
|
gf_low_pressure_this_dive = lowest_ceiling;
|
|
|
|
}
|
2013-01-10 10:24:42 +00:00
|
|
|
|
2013-02-08 10:42:34 +00:00
|
|
|
tolerated = (-buehlmann_inertgas_a * buehlmann_inertgas_b * (gf_high * gf_low_pressure_this_dive - gf_low * surface) -
|
2014-02-28 04:09:57 +00:00
|
|
|
(1.0 - buehlmann_inertgas_b) * (gf_high - gf_low) * gf_low_pressure_this_dive * surface +
|
|
|
|
buehlmann_inertgas_b * (gf_low_pressure_this_dive - surface) * tissue_inertgas_saturation) /
|
2013-02-08 10:42:34 +00:00
|
|
|
(-buehlmann_inertgas_a * buehlmann_inertgas_b * (gf_high - gf_low) +
|
2014-02-28 04:09:57 +00:00
|
|
|
(1.0 - buehlmann_inertgas_b) * (gf_low * gf_low_pressure_this_dive - gf_high * surface) +
|
|
|
|
buehlmann_inertgas_b * (gf_low_pressure_this_dive - surface));
|
2013-01-03 05:21:36 +00:00
|
|
|
|
2013-05-30 18:56:00 +00:00
|
|
|
tolerated_by_tissue[ci] = tolerated;
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
if (tolerated > ret_tolerance_limit_ambient_pressure) {
|
2013-01-03 05:21:36 +00:00
|
|
|
ci_pointing_to_guiding_tissue = ci;
|
2013-02-08 10:42:34 +00:00
|
|
|
ret_tolerance_limit_ambient_pressure = tolerated;
|
2013-01-03 05:21:36 +00:00
|
|
|
}
|
|
|
|
}
|
2013-01-04 07:56:10 +00:00
|
|
|
return ret_tolerance_limit_ambient_pressure;
|
2013-01-03 05:21:36 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 03:42:19 +00:00
|
|
|
/*
|
|
|
|
* Return buelman factor for a particular period and tissue index.
|
|
|
|
*
|
|
|
|
* We cache the last factor, since we commonly call this with the
|
|
|
|
* same values... We have a special "fixed cache" for the one second
|
|
|
|
* case, although I wonder if that's even worth it considering the
|
|
|
|
* more general-purpose cache.
|
|
|
|
*/
|
|
|
|
struct factor_cache {
|
|
|
|
int last_period;
|
|
|
|
double last_factor;
|
|
|
|
};
|
|
|
|
|
|
|
|
double n2_factor(int period_in_seconds, int ci)
|
|
|
|
{
|
|
|
|
static struct factor_cache cache[16];
|
|
|
|
|
|
|
|
if (period_in_seconds == 1)
|
|
|
|
return buehlmann_N2_factor_expositon_one_second[ci];
|
|
|
|
|
|
|
|
if (period_in_seconds != cache[ci].last_period) {
|
|
|
|
cache[ci].last_period = period_in_seconds;
|
2014-02-28 04:09:57 +00:00
|
|
|
cache[ci].last_factor = 1 - pow(2.0, -period_in_seconds / (buehlmann_N2_t_halflife[ci] * 60));
|
2013-09-26 03:42:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cache[ci].last_factor;
|
|
|
|
}
|
|
|
|
|
|
|
|
double he_factor(int period_in_seconds, int ci)
|
|
|
|
{
|
|
|
|
static struct factor_cache cache[16];
|
|
|
|
|
|
|
|
if (period_in_seconds == 1)
|
|
|
|
return buehlmann_He_factor_expositon_one_second[ci];
|
|
|
|
|
|
|
|
if (period_in_seconds != cache[ci].last_period) {
|
|
|
|
cache[ci].last_period = period_in_seconds;
|
2014-02-28 04:09:57 +00:00
|
|
|
cache[ci].last_factor = 1 - pow(2.0, -period_in_seconds / (buehlmann_He_t_halflife[ci] * 60));
|
2013-09-26 03:42:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cache[ci].last_factor;
|
|
|
|
}
|
|
|
|
|
2013-01-08 17:29:07 +00:00
|
|
|
/* add period_in_seconds at the given pressure and gas to the deco calculation */
|
2013-02-08 06:15:50 +00:00
|
|
|
double add_segment(double pressure, const struct gasmix *gasmix, int period_in_seconds, int ccpo2, const struct dive *dive)
|
2013-01-03 05:21:36 +00:00
|
|
|
{
|
|
|
|
int ci;
|
2013-03-28 17:06:43 +00:00
|
|
|
int fo2 = get_o2(gasmix), fhe = get_he(gasmix);
|
|
|
|
double ppn2 = (pressure - WV_PRESSURE) * (1000 - fo2 - fhe) / 1000.0;
|
|
|
|
double pphe = (pressure - WV_PRESSURE) * fhe / 1000.0;
|
2013-01-03 05:21:36 +00:00
|
|
|
|
2013-11-20 15:11:22 +00:00
|
|
|
if (buehlmann_config.gf_low_at_maxdepth && pressure > gf_low_pressure_this_dive)
|
2013-05-28 18:21:27 +00:00
|
|
|
gf_low_pressure_this_dive = pressure;
|
2013-01-08 14:37:41 +00:00
|
|
|
|
2013-02-02 18:38:51 +00:00
|
|
|
if (ccpo2) { /* CC */
|
2013-01-04 07:56:10 +00:00
|
|
|
double rel_o2_amb, f_dilutent;
|
2013-02-02 18:38:51 +00:00
|
|
|
rel_o2_amb = ccpo2 / pressure / 1000;
|
2013-01-05 07:09:04 +00:00
|
|
|
f_dilutent = (1 - rel_o2_amb) / (1 - fo2 / 1000.0);
|
2013-01-04 07:56:10 +00:00
|
|
|
if (f_dilutent < 0) { /* setpoint is higher than ambient pressure -> pure O2 */
|
|
|
|
ppn2 = 0.0;
|
|
|
|
pphe = 0.0;
|
|
|
|
} else if (f_dilutent < 1.0) {
|
|
|
|
ppn2 *= f_dilutent;
|
|
|
|
pphe *= f_dilutent;
|
|
|
|
}
|
|
|
|
}
|
2013-09-26 03:42:19 +00:00
|
|
|
|
|
|
|
for (ci = 0; ci < 16; ci++) {
|
|
|
|
double ppn2_oversat = ppn2 - tissue_n2_sat[ci];
|
|
|
|
double pphe_oversat = pphe - tissue_he_sat[ci];
|
|
|
|
double n2_f = n2_factor(period_in_seconds, ci);
|
|
|
|
double he_f = he_factor(period_in_seconds, ci);
|
|
|
|
double n2_satmult = ppn2_oversat > 0 ? buehlmann_config.satmult : buehlmann_config.desatmult;
|
|
|
|
double he_satmult = pphe_oversat > 0 ? buehlmann_config.satmult : buehlmann_config.desatmult;
|
|
|
|
|
|
|
|
tissue_n2_sat[ci] += n2_satmult * ppn2_oversat * n2_f;
|
|
|
|
tissue_he_sat[ci] += he_satmult * pphe_oversat * he_f;
|
2013-01-03 05:21:36 +00:00
|
|
|
}
|
2013-01-08 14:37:41 +00:00
|
|
|
return tissue_tolerance_calc(dive);
|
2013-01-03 05:21:36 +00:00
|
|
|
}
|
|
|
|
|
2013-12-19 21:11:43 +00:00
|
|
|
#ifdef DECO_CALC_DEBUG
|
2013-01-04 04:45:20 +00:00
|
|
|
void dump_tissues()
|
|
|
|
{
|
|
|
|
int ci;
|
|
|
|
printf("N2 tissues:");
|
|
|
|
for (ci = 0; ci < 16; ci++)
|
|
|
|
printf(" %6.3e", tissue_n2_sat[ci]);
|
|
|
|
printf("\nHe tissues:");
|
|
|
|
for (ci = 0; ci < 16; ci++)
|
|
|
|
printf(" %6.3e", tissue_he_sat[ci]);
|
|
|
|
printf("\n");
|
|
|
|
}
|
2013-12-19 21:11:43 +00:00
|
|
|
#endif
|
2013-01-04 04:45:20 +00:00
|
|
|
|
|
|
|
void clear_deco(double surface_pressure)
|
2013-01-03 05:21:36 +00:00
|
|
|
{
|
|
|
|
int ci;
|
|
|
|
for (ci = 0; ci < 16; ci++) {
|
2013-01-14 22:53:38 +00:00
|
|
|
tissue_n2_sat[ci] = (surface_pressure - WV_PRESSURE) * N2_IN_AIR / 1000;
|
2013-01-03 05:21:36 +00:00
|
|
|
tissue_he_sat[ci] = 0.0;
|
|
|
|
}
|
2013-01-08 17:29:07 +00:00
|
|
|
gf_low_pressure_this_dive = surface_pressure + buehlmann_config.gf_low_position_min;
|
2013-01-03 05:21:36 +00:00
|
|
|
}
|
2013-01-03 07:22:07 +00:00
|
|
|
|
2013-01-06 19:13:46 +00:00
|
|
|
void cache_deco_state(double tissue_tolerance, char **cached_datap)
|
|
|
|
{
|
|
|
|
char *data = *cached_datap;
|
|
|
|
|
|
|
|
if (!data) {
|
2013-02-08 10:42:34 +00:00
|
|
|
data = malloc(2 * TISSUE_ARRAY_SZ + 2 * sizeof(double) + sizeof(int));
|
2013-01-06 19:13:46 +00:00
|
|
|
*cached_datap = data;
|
|
|
|
}
|
|
|
|
memcpy(data, tissue_n2_sat, TISSUE_ARRAY_SZ);
|
|
|
|
data += TISSUE_ARRAY_SZ;
|
|
|
|
memcpy(data, tissue_he_sat, TISSUE_ARRAY_SZ);
|
|
|
|
data += TISSUE_ARRAY_SZ;
|
2013-01-08 14:37:41 +00:00
|
|
|
memcpy(data, &gf_low_pressure_this_dive, sizeof(double));
|
2013-01-06 19:13:46 +00:00
|
|
|
data += sizeof(double);
|
|
|
|
memcpy(data, &tissue_tolerance, sizeof(double));
|
|
|
|
data += sizeof(double);
|
|
|
|
memcpy(data, &ci_pointing_to_guiding_tissue, sizeof(int));
|
|
|
|
}
|
|
|
|
|
|
|
|
double restore_deco_state(char *data)
|
|
|
|
{
|
|
|
|
double tissue_tolerance;
|
|
|
|
|
|
|
|
memcpy(tissue_n2_sat, data, TISSUE_ARRAY_SZ);
|
|
|
|
data += TISSUE_ARRAY_SZ;
|
|
|
|
memcpy(tissue_he_sat, data, TISSUE_ARRAY_SZ);
|
|
|
|
data += TISSUE_ARRAY_SZ;
|
2013-01-08 14:37:41 +00:00
|
|
|
memcpy(&gf_low_pressure_this_dive, data, sizeof(double));
|
2013-01-06 19:13:46 +00:00
|
|
|
data += sizeof(double);
|
|
|
|
memcpy(&tissue_tolerance, data, sizeof(double));
|
|
|
|
data += sizeof(double);
|
|
|
|
memcpy(&ci_pointing_to_guiding_tissue, data, sizeof(int));
|
|
|
|
|
|
|
|
return tissue_tolerance;
|
|
|
|
}
|
|
|
|
|
2013-10-05 07:29:09 +00:00
|
|
|
unsigned int deco_allowed_depth(double tissues_tolerance, double surface_pressure, struct dive *dive, bool smooth)
|
2013-01-03 07:22:07 +00:00
|
|
|
{
|
2013-01-08 14:37:41 +00:00
|
|
|
unsigned int depth;
|
|
|
|
double pressure_delta;
|
|
|
|
|
2013-01-08 17:29:07 +00:00
|
|
|
/* Avoid negative depths */
|
|
|
|
pressure_delta = tissues_tolerance > surface_pressure ? tissues_tolerance - surface_pressure : 0.0;
|
2013-01-08 14:37:41 +00:00
|
|
|
|
|
|
|
depth = rel_mbar_to_depth(pressure_delta * 1000, dive);
|
|
|
|
|
2013-01-29 21:10:46 +00:00
|
|
|
if (!smooth)
|
2013-01-08 17:29:07 +00:00
|
|
|
depth = ceil(depth / DECO_STOPS_MULTIPLIER_MM) * DECO_STOPS_MULTIPLIER_MM;
|
2013-01-08 14:37:41 +00:00
|
|
|
|
2013-01-29 21:10:46 +00:00
|
|
|
if (depth > 0 && depth < buehlmann_config.last_deco_stop_in_mtr * 1000)
|
2013-01-08 17:29:07 +00:00
|
|
|
depth = buehlmann_config.last_deco_stop_in_mtr * 1000;
|
2013-01-03 07:22:07 +00:00
|
|
|
|
|
|
|
return depth;
|
|
|
|
}
|
2013-01-04 05:31:22 +00:00
|
|
|
|
2013-11-20 15:11:22 +00:00
|
|
|
void set_gf(short gflow, short gfhigh, bool gf_low_at_maxdepth)
|
2013-01-04 05:31:22 +00:00
|
|
|
{
|
2013-05-28 18:21:27 +00:00
|
|
|
if (gflow != -1)
|
|
|
|
buehlmann_config.gf_low = (double)gflow / 100.0;
|
|
|
|
if (gfhigh != -1)
|
|
|
|
buehlmann_config.gf_high = (double)gfhigh / 100.0;
|
2013-11-20 15:11:22 +00:00
|
|
|
buehlmann_config.gf_low_at_maxdepth = gf_low_at_maxdepth;
|
2013-01-04 05:31:22 +00:00
|
|
|
}
|