From 0232a628a711af43891d84e83df6fd2d0dd1586b Mon Sep 17 00:00:00 2001 From: "Robert C. Helling" Date: Mon, 31 Aug 2015 23:39:43 +0200 Subject: [PATCH 4/5] Introduce double depth_to_bar() as it leads to significant cleanup. Signed-off-by: Robert C. Helling --- dive.h | 5 +++++ divelist.c | 2 +- planner.c | 24 ++++++++++++------------ profile.c | 24 ++++++++++++------------ 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/dive.h b/dive.h index 108e5d1..eac6e72 100644 --- a/dive.h +++ b/dive.h @@ -419,6 +419,11 @@ static inline int depth_to_mbar(int depth, struct dive *dive) return calculate_depth_to_mbar(depth, dive->surface_pressure, dive->salinity); } +static inline double depth_to_bar(int depth, struct dive *dive) +{ + return depth_to_mbar(depth, dive) / 1000.0; +} + static inline double depth_to_atm(int depth, struct dive *dive) { return mbar_to_atm(depth_to_mbar(depth, dive)); diff --git a/divelist.c b/divelist.c index d9a3e2a..3cbff84 100644 --- a/divelist.c +++ b/divelist.c @@ -323,7 +323,7 @@ static void add_dive_to_deco(struct dive *dive) for (j = t0; j < t1; j++) { int depth = interpolate(psample->depth.mm, sample->depth.mm, j - t0, t1 - t0); - (void)add_segment(depth_to_mbar(depth, dive) / 1000.0, + add_segment(depth_to_bar(depth, dive), &dive->cylinder[sample->sensor].gasmix, 1, sample->setpoint.mbar, dive, dive->sac); } } diff --git a/planner.c b/planner.c index 6abe8ec..e0f889e 100644 --- a/planner.c +++ b/planner.c @@ -107,10 +107,10 @@ void interpolate_transition(struct dive *dive, duration_t t0, duration_t t1, dep for (j = t0.seconds; j < t1.seconds; j++) { int depth = interpolate(d0.mm, d1.mm, j - t0.seconds, t1.seconds - t0.seconds); - add_segment(depth_to_mbar(depth, dive) / 1000.0, gasmix, 1, po2.mbar, dive, prefs.bottomsac); + add_segment(depth_to_bar(depth, dive), gasmix, 1, po2.mbar, dive, prefs.bottomsac); } if (d1.mm > d0.mm) - calc_crushing_pressure(depth_to_mbar(d1.mm, &displayed_dive) / 1000.0); + calc_crushing_pressure(depth_to_bar(d1.mm, &displayed_dive)); } /* returns the tissue tolerance at the end of this (partial) dive */ @@ -875,7 +875,7 @@ bool trial_ascent(int trial_depth, int stoplevel, int avg_depth, int bottom_time // deeper than the next stop (thus the offgasing during the ascent is ignored). // However, we still need to make sure we don't break the ceiling due to on-gassing during ascent. if (prefs.deco_mode == VPMB && (deco_allowed_depth(tissue_tolerance_calc(&displayed_dive, - depth_to_mbar(stoplevel, &displayed_dive)), + depth_to_bar(stoplevel, &displayed_dive)), surface_pressure, &displayed_dive, 1) > stoplevel)) return false; @@ -884,10 +884,10 @@ bool trial_ascent(int trial_depth, int stoplevel, int avg_depth, int bottom_time int deltad = ascent_velocity(trial_depth, avg_depth, bottom_time) * TIMESTEP; if (deltad > trial_depth) /* don't test against depth above surface */ deltad = trial_depth; - add_segment(depth_to_mbar(trial_depth, &displayed_dive) / 1000.0, + add_segment(depth_to_bar(trial_depth, &displayed_dive), gasmix, TIMESTEP, po2, &displayed_dive, prefs.decosac); - if (deco_allowed_depth(tissue_tolerance_calc(&displayed_dive, depth_to_mbar(trial_depth, &displayed_dive) / 1000.0), + if (deco_allowed_depth(tissue_tolerance_calc(&displayed_dive, depth_to_bar(trial_depth, &displayed_dive)), surface_pressure, &displayed_dive, 1) > trial_depth - deltad) { /* We should have stopped */ clear_to_ascend = false; @@ -1039,7 +1039,7 @@ bool plan(struct diveplan *diveplan, char **cached_datap, bool is_planner, bool while (trial_ascent(depth, 0, avg_depth, bottom_time, &displayed_dive.cylinder[current_cylinder].gasmix, po2, diveplan->surface_pressure / 1000.0) && enough_gas(current_cylinder)) { - add_segment(depth_to_mbar(depth, &displayed_dive) / 1000.0, + add_segment(depth_to_bar(depth, &displayed_dive), &displayed_dive.cylinder[current_cylinder].gasmix, DECOTIMESTEP, po2, &displayed_dive, prefs.bottomsac); update_cylinder_pressure(&displayed_dive, depth, depth, DECOTIMESTEP, prefs.bottomsac, &displayed_dive.cylinder[current_cylinder], false); @@ -1059,7 +1059,7 @@ bool plan(struct diveplan *diveplan, char **cached_datap, bool is_planner, bool if (depth - deltad < 0) deltad = depth; - add_segment(depth_to_mbar(depth, &displayed_dive) / 1000.0, + add_segment(depth_to_bar(depth, &displayed_dive), &displayed_dive.cylinder[current_cylinder].gasmix, TIMESTEP, po2, &displayed_dive, prefs.decosac); clock += TIMESTEP; @@ -1125,7 +1125,7 @@ bool plan(struct diveplan *diveplan, char **cached_datap, bool is_planner, bool breakcylinder = 0; o2time = 0; first_ceiling_pressure.mbar = depth_to_mbar(deco_allowed_depth(tissue_tolerance_calc(&displayed_dive, - depth_to_mbar(depth, &displayed_dive) / 1000.0), + depth_to_bar(depth, &displayed_dive)), diveplan->surface_pressure / 1000.0, &displayed_dive, 1), @@ -1152,7 +1152,7 @@ bool plan(struct diveplan *diveplan, char **cached_datap, bool is_planner, bool if (depth - deltad < stoplevels[stopidx]) deltad = depth - stoplevels[stopidx]; - add_segment(depth_to_mbar(depth, &displayed_dive) / 1000.0, + add_segment(depth_to_bar(depth, &displayed_dive), &displayed_dive.cylinder[current_cylinder].gasmix, TIMESTEP, po2, &displayed_dive, prefs.decosac); clock += TIMESTEP; @@ -1186,7 +1186,7 @@ bool plan(struct diveplan *diveplan, char **cached_datap, bool is_planner, bool (get_o2(&gas) + 5) / 10, (get_he(&gas) + 5) / 10, gaschanges[gi].depth / 1000.0); #endif /* Stop for the minimum duration to switch gas */ - add_segment(depth_to_mbar(depth, &displayed_dive) / 1000.0, + add_segment(depth_to_bar(depth, &displayed_dive), &displayed_dive.cylinder[current_cylinder].gasmix, prefs.min_switch_duration, po2, &displayed_dive, prefs.decosac); clock += prefs.min_switch_duration; @@ -1232,7 +1232,7 @@ bool plan(struct diveplan *diveplan, char **cached_datap, bool is_planner, bool (get_o2(&gas) + 5) / 10, (get_he(&gas) + 5) / 10, gaschanges[gi + 1].depth / 1000.0); #endif /* Stop for the minimum duration to switch gas */ - add_segment(depth_to_mbar(depth, &displayed_dive) / 1000.0, + add_segment(depth_to_bar(depth, &displayed_dive), &displayed_dive.cylinder[current_cylinder].gasmix, prefs.min_switch_duration, po2, &displayed_dive, prefs.decosac); clock += prefs.min_switch_duration; @@ -1245,7 +1245,7 @@ bool plan(struct diveplan *diveplan, char **cached_datap, bool is_planner, bool int this_decotimestep; this_decotimestep = DECOTIMESTEP - clock % DECOTIMESTEP; - add_segment(depth_to_mbar(depth, &displayed_dive) / 1000.0, + add_segment(depth_to_bar(depth, &displayed_dive), &displayed_dive.cylinder[current_cylinder].gasmix, this_decotimestep, po2, &displayed_dive, prefs.decosac); clock += this_decotimestep; diff --git a/profile.c b/profile.c index 076ee9a..fc0faae 100644 --- a/profile.c +++ b/profile.c @@ -771,7 +771,7 @@ static void calculate_ndl_tts(struct plot_data *entry, struct dive *dive, double const int time_stepsize = 60; const int deco_stepsize = 3000; /* at what depth is the current deco-step? */ - int next_stop = ROUND_UP(deco_allowed_depth(tissue_tolerance_calc(dive, depth_to_mbar(entry->depth, dive) / 1000.0), + int next_stop = ROUND_UP(deco_allowed_depth(tissue_tolerance_calc(dive, depth_to_bar(entry->depth, dive)), surface_pressure, dive, 1), deco_stepsize); int ascent_depth = entry->depth; /* at what time should we give up and say that we got enuff NDL? */ @@ -786,9 +786,9 @@ static void calculate_ndl_tts(struct plot_data *entry, struct dive *dive, double return; } /* stop if the ndl is above max_ndl seconds, and call it plenty of time */ - while (entry->ndl_calc < max_ndl && deco_allowed_depth(tissue_tolerance_calc(dive, depth_to_mbar(entry->depth, dive) / 1000.0), surface_pressure, dive, 1) <= 0) { + while (entry->ndl_calc < max_ndl && deco_allowed_depth(tissue_tolerance_calc(dive, depth_to_bar(entry->depth, dive)), surface_pressure, dive, 1) <= 0) { entry->ndl_calc += time_stepsize; - add_segment(depth_to_mbar(entry->depth, dive) / 1000.0, + add_segment(depth_to_bar(entry->depth, dive), &dive->cylinder[cylinderindex].gasmix, time_stepsize, entry->o2pressure.mbar, dive, prefs.bottomsac); } /* we don't need to calculate anything else */ @@ -800,9 +800,9 @@ static void calculate_ndl_tts(struct plot_data *entry, struct dive *dive, double /* Add segments for movement to stopdepth */ for (; ascent_depth > next_stop; ascent_depth -= ascent_mm_per_step, entry->tts_calc += ascent_s_per_step) { - add_segment(depth_to_mbar(ascent_depth, dive) / 1000.0, + add_segment(depth_to_bar(ascent_depth, dive), &dive->cylinder[cylinderindex].gasmix, ascent_s_per_step, entry->o2pressure.mbar, dive, prefs.decosac); - next_stop = ROUND_UP(deco_allowed_depth(tissue_tolerance_calc(dive, depth_to_mbar(ascent_depth, dive) / 1000.0), surface_pressure, dive, 1), deco_stepsize); + next_stop = ROUND_UP(deco_allowed_depth(tissue_tolerance_calc(dive, depth_to_bar(ascent_depth, dive)), surface_pressure, dive, 1), deco_stepsize); } ascent_depth = next_stop; @@ -818,13 +818,13 @@ static void calculate_ndl_tts(struct plot_data *entry, struct dive *dive, double entry->stoptime_calc += time_stepsize; entry->tts_calc += time_stepsize; - add_segment(depth_to_mbar(ascent_depth, dive) / 1000.0, + add_segment(depth_to_bar(ascent_depth, dive), &dive->cylinder[cylinderindex].gasmix, time_stepsize, entry->o2pressure.mbar, dive, prefs.decosac); - if (deco_allowed_depth(tissue_tolerance_calc(dive, depth_to_mbar(ascent_depth,dive) / 1000.0), surface_pressure, dive, 1) <= next_stop) { + if (deco_allowed_depth(tissue_tolerance_calc(dive, depth_to_bar(ascent_depth,dive)), surface_pressure, dive, 1) <= next_stop) { /* move to the next stop and add the travel between stops */ for (; ascent_depth > next_stop; ascent_depth -= ascent_mm_per_deco_step, entry->tts_calc += ascent_s_per_deco_step) - add_segment(depth_to_mbar(ascent_depth, dive) / 1000.0, + add_segment(depth_to_bar(ascent_depth, dive), &dive->cylinder[cylinderindex].gasmix, ascent_s_per_deco_step, entry->o2pressure.mbar, dive, prefs.decosac); ascent_depth = next_stop; next_stop -= deco_stepsize; @@ -844,7 +844,7 @@ void calculate_deco_information(struct dive *dive, struct divecomputer *dc, stru int j, t0 = (entry - 1)->sec, t1 = entry->sec; int time_stepsize = 20; - entry->ambpressure = (double)depth_to_mbar(entry->depth, dive) / 1000.0; + entry->ambpressure = depth_to_bar(entry->depth, dive); entry->gfline = MAX((double)prefs.gflow, (entry->ambpressure - surface_pressure) / (gf_low_pressure_this_dive - surface_pressure) * (prefs.gflow - prefs.gfhigh) + prefs.gfhigh) * @@ -853,7 +853,7 @@ void calculate_deco_information(struct dive *dive, struct divecomputer *dc, stru time_stepsize = t1 - t0; for (j = t0 + time_stepsize; j <= t1; j += time_stepsize) { int depth = interpolate(entry[-1].depth, entry[0].depth, j - t0, t1 - t0); - add_segment(depth_to_mbar(depth, dive) / 1000.0, + add_segment(depth_to_bar(depth, dive), &dive->cylinder[entry->cylinderindex].gasmix, time_stepsize, entry->o2pressure.mbar, dive, entry->sac); if ((t1 - j < time_stepsize) && (j < t1)) time_stepsize = t1 - j; @@ -865,7 +865,7 @@ void calculate_deco_information(struct dive *dive, struct divecomputer *dc, stru entry->percentages[j] = (entry - 1)->percentages[j]; } } else { - entry->ceiling = deco_allowed_depth(tissue_tolerance_calc(dive, depth_to_mbar(entry->depth, dive) / 1000.0), surface_pressure, dive, !prefs.calcceiling3m); + entry->ceiling = deco_allowed_depth(tissue_tolerance_calc(dive, depth_to_bar(entry->depth, dive)), surface_pressure, dive, !prefs.calcceiling3m); for (j = 0; j < 16; j++) { double m_value = buehlmann_inertgas_a[j] + entry->ambpressure / buehlmann_inertgas_b[j]; entry->ceilings[j] = deco_allowed_depth(tolerated_by_tissue[j], surface_pressure, dive, 1); @@ -957,7 +957,7 @@ static void calculate_gas_information_new(struct dive *dive, struct plot_info *p struct plot_data *entry = pi->entry + i; int cylinderindex = entry->cylinderindex; - amb_pressure = depth_to_mbar(entry->depth, dive) / 1000.0; + amb_pressure = depth_to_bar(entry->depth, dive); fill_pressures(&entry->pressures, amb_pressure, &dive->cylinder[cylinderindex].gasmix, entry->o2pressure.mbar / 1000.0, dive->dc.divemode); fn2 = (int)(1000.0 * entry->pressures.n2 / amb_pressure); -- 1.9.5 (Apple Git-50.3)