Cache all Buehlmann factors

not just the last one.

Signed-off-by: Robert C. Helling <helling@atdotde.de>
This commit is contained in:
Robert C. Helling 2017-08-25 23:21:27 +02:00 committed by Dirk Hohndel
parent 515b7b5fea
commit 456e2cec89
5 changed files with 43 additions and 33 deletions

View file

@ -321,46 +321,34 @@ double tissue_tolerance_calc(const struct dive *dive, double pressure)
/* /*
* Return buelman factor for a particular period and tissue index. * Return buelman factor for a particular period and tissue index.
* *
* We cache the last factor, since we commonly call this with the * We cache the factor, since we commonly call this with the
* same values... We have a special "fixed cache" for the one second * same values... We have a special "fixed cache" for the one second
* case, although I wonder if that's even worth it considering the * case, although I wonder if that's even worth it considering the
* more general-purpose cache. * more general-purpose cache.
*/ */
struct factor_cache {
int last_period;
double last_factor;
};
double n2_factor(int period_in_seconds, int ci)
double factor(int period_in_seconds, int ci, enum inertgas gas)
{ {
static struct factor_cache cache[16]; double factor;
if (period_in_seconds == 1) {
if (period_in_seconds == 1) if (gas == N2)
return buehlmann_N2_factor_expositon_one_second[ci]; return buehlmann_N2_factor_expositon_one_second[ci];
else
if (period_in_seconds != cache[ci].last_period) { return buehlmann_He_factor_expositon_one_second[ci];
cache[ci].last_period = period_in_seconds;
// ln(2)/60 = 1.155245301e-02
cache[ci].last_factor = 1 - exp(-period_in_seconds * 1.155245301e-02 / buehlmann_N2_t_halflife[ci]);
} }
return cache[ci].last_factor; factor = cache_value(ci, period_in_seconds, gas);
} if (!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;
// ln(2)/60 = 1.155245301e-02 // ln(2)/60 = 1.155245301e-02
cache[ci].last_factor = 1 - exp(-period_in_seconds * 1.155245301e-02 / buehlmann_He_t_halflife[ci]); if (gas == N2)
factor = 1 - exp(-period_in_seconds * 1.155245301e-02 / buehlmann_N2_t_halflife[ci]);
else
factor = 1 - exp(-period_in_seconds * 1.155245301e-02 / buehlmann_He_t_halflife[ci]);
cache_insert(ci, period_in_seconds, gas, factor);
} }
return cache[ci].last_factor; return factor;
} }
double calc_surface_phase(double surface_pressure, double he_pressure, double n2_pressure, double he_time_constant, double n2_time_constant) double calc_surface_phase(double surface_pressure, double he_pressure, double n2_pressure, double he_time_constant, double n2_time_constant)
@ -515,8 +503,8 @@ void add_segment(double pressure, const struct gasmix *gasmix, int period_in_sec
for (ci = 0; ci < 16; ci++) { for (ci = 0; ci < 16; ci++) {
double pn2_oversat = pressures.n2 - deco_state->tissue_n2_sat[ci]; double pn2_oversat = pressures.n2 - deco_state->tissue_n2_sat[ci];
double phe_oversat = pressures.he - deco_state->tissue_he_sat[ci]; double phe_oversat = pressures.he - deco_state->tissue_he_sat[ci];
double n2_f = n2_factor(period_in_seconds, ci); double n2_f = factor(period_in_seconds, ci, N2);
double he_f = he_factor(period_in_seconds, ci); double he_f = factor(period_in_seconds, ci, HE);
double n2_satmult = pn2_oversat > 0 ? buehlmann_config.satmult : buehlmann_config.desatmult; double n2_satmult = pn2_oversat > 0 ? buehlmann_config.satmult : buehlmann_config.desatmult;
double he_satmult = phe_oversat > 0 ? buehlmann_config.satmult : buehlmann_config.desatmult; double he_satmult = phe_oversat > 0 ? buehlmann_config.satmult : buehlmann_config.desatmult;

View file

@ -627,7 +627,6 @@ int wait_until(struct dive *dive, int clock, int min, int leap, int stepsize, in
{ {
// Round min + leap up to the next multiple of stepsize // Round min + leap up to the next multiple of stepsize
int upper = min + leap + stepsize - 1 - (min + leap - 1) % stepsize; int upper = min + leap + stepsize - 1 - (min + leap - 1) % stepsize;
printf("clock: %d min: %d leap: %d, depth %d\n", clock / 60, min / 60, leap, depth);
// Is the upper boundary too small? // Is the upper boundary too small?
if (!trial_ascent(upper - clock, depth, target_depth, avg_depth, bottom_time, gasmix, po2, surface_pressure, dive)) if (!trial_ascent(upper - clock, depth, target_depth, avg_depth, bottom_time, gasmix, po2, surface_pressure, dive))
return wait_until(dive, clock, upper, leap, stepsize, depth, target_depth, avg_depth, bottom_time, gasmix, po2, surface_pressure); return wait_until(dive, clock, upper, leap, stepsize, depth, target_depth, avg_depth, bottom_time, gasmix, po2, surface_pressure);

View file

@ -1710,3 +1710,21 @@ char *intdup(int index)
tmpbuf[20] = 0; tmpbuf[20] = 0;
return strdup(tmpbuf); return strdup(tmpbuf);
} }
QHash<int, double> factor_cache;
extern "C" double cache_value(int tissue, int timestep, enum inertgas inertgas)
{
int key = (timestep << 5) + (tissue << 1);
if (inertgas == HE)
++key;
return factor_cache.value(key);
}
extern "C" void cache_insert(int tissue, int timestep, enum inertgas inertgas, double value)
{
int key = (timestep << 5) + (tissue << 1);
if (inertgas == HE)
++key;
factor_cache.insert(key, value);
}

View file

@ -49,5 +49,8 @@ QString getUUID();
QStringList imageExtensionFilters(); QStringList imageExtensionFilters();
char *intdup(int index); char *intdup(int index);
extern "C" int parse_seabear_header(const char *filename, char **params, int pnr); extern "C" int parse_seabear_header(const char *filename, char **params, int pnr);
enum inertgas {N2, HE};
extern "C" double cache_value(int tissue, int timestep, enum inertgas gas);
extern "C" void cache_insert(int tissue, int timestep, enum inertgas gas, double value);
#endif // QTHELPER_H #endif // QTHELPER_H

View file

@ -22,6 +22,8 @@ const char *subsurface_user_agent();
enum deco_mode decoMode(); enum deco_mode decoMode();
int parse_seabear_header(const char *filename, char **params, int pnr); int parse_seabear_header(const char *filename, char **params, int pnr);
extern const char *get_current_date(); extern const char *get_current_date();
enum inertgas {N2, HE};
double cache_value(int tissue, int timestep, enum inertgas gas);
void cache_insert(int tissue, int timestep, enum inertgas gas, double value);
#endif // QTHELPERFROMC_H #endif // QTHELPERFROMC_H