units: replace SURFACE_PRESSURE by 1_atm

Moreover, convert diveplan::surface_pressure from int to
pressure_t.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-09-09 13:55:37 +02:00 committed by bstoeger
parent ae81b42fe2
commit dd5def35f5
11 changed files with 56 additions and 64 deletions

View file

@ -2368,8 +2368,7 @@ bool dive::cache_is_valid() const
pressure_t dive::get_surface_pressure() const
{
return surface_pressure.mbar > 0 ? surface_pressure
: pressure_t { .mbar = SURFACE_PRESSURE };
return surface_pressure.mbar > 0 ? surface_pressure : 1_atm;
}
/* This returns the conversion factor that you need to multiply
@ -2387,17 +2386,14 @@ static double salinity_to_specific_weight(int salinity)
* and add that to the surface pressure (or to 1013 if that's unknown) */
static double calculate_depth_to_mbarf(int depth, pressure_t surface_pressure, int salinity)
{
double specific_weight;
int mbar = surface_pressure.mbar;
if (!mbar)
mbar = SURFACE_PRESSURE;
if (!surface_pressure.mbar)
surface_pressure = 1_atm;
if (!salinity)
salinity = SEAWATER_SALINITY;
if (salinity < 500)
salinity += FRESHWATER_SALINITY;
specific_weight = salinity_to_specific_weight(salinity);
return mbar + depth * specific_weight;
double specific_weight = salinity_to_specific_weight(salinity);
return surface_pressure.mbar + depth * specific_weight;
}
int dive::depth_to_mbar(int depth) const
@ -2452,7 +2448,7 @@ int dive::mbar_to_depth(int mbar) const
: dcs[0].surface_pressure;
if (!surface_pressure.mbar)
surface_pressure.mbar = SURFACE_PRESSURE;
surface_pressure = 1_atm;
return rel_mbar_to_depth(mbar - surface_pressure.mbar);
}

View file

@ -57,7 +57,7 @@ void dump_plan(struct diveplan *diveplan)
printf("\nDiveplan @ %04d-%02d-%02d %02d:%02d:%02d (surfpres %dmbar):\n",
tm.tm_year, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec,
diveplan->surface_pressure);
diveplan->surface_pressure.mbar);
dp = diveplan->dp;
while (dp) {
printf("\t%3u:%02u: %6dmm cylid: %2d setpoint: %d\n", FRACTION_TUPLE(dp->time, 60), dp->depth, dp->cylinderid, dp->setpoint);
@ -216,7 +216,7 @@ static void create_dive_from_plan(struct diveplan &diveplan, struct dive *dive,
// dive-to-be-planned so we can restart
reset_cylinders(dive, track_gas);
dc->when = dive->when = diveplan.when;
dc->surface_pressure.mbar = diveplan.surface_pressure;
dc->surface_pressure = diveplan.surface_pressure;
dc->salinity = diveplan.salinity;
dc->samples.clear();
dc->events.clear();
@ -305,7 +305,7 @@ divedatapoint::divedatapoint(int time_incr, int depth, int cylinderid, int po2,
time(time_incr),
depth{ .mm = depth },
cylinderid(cylinderid),
minimum_gas = 0_bar;
minimum_gas(0_bar),
setpoint(po2),
entered(entered),
divemode(OC)
@ -640,16 +640,14 @@ std::vector<decostop> plan(struct deco_state *ds, struct diveplan &diveplan, str
set_gf(diveplan.gflow, diveplan.gfhigh);
set_vpmb_conservatism(diveplan.vpmb_conservatism);
if (!diveplan.surface_pressure) {
if (diveplan.surface_pressure.mbar == 0) {
// Lets use dive's surface pressure in planner, if have one...
if (dc->surface_pressure.mbar) { // First from DC...
diveplan.surface_pressure = dc->surface_pressure.mbar;
}
else if (dive->surface_pressure.mbar) { // After from user...
diveplan.surface_pressure = dive->surface_pressure.mbar;
}
else {
diveplan.surface_pressure = SURFACE_PRESSURE;
if (dc->surface_pressure.mbar != 0) { // First from DC...
diveplan.surface_pressure = dc->surface_pressure;
} else if (dive->surface_pressure.mbar != 0) { // After from user...
diveplan.surface_pressure = dive->surface_pressure;
} else {
diveplan.surface_pressure = 1_atm;
}
}
@ -744,7 +742,7 @@ std::vector<decostop> plan(struct deco_state *ds, struct diveplan &diveplan, str
update_cylinder_pressure(dive, depth, depth, timestep, prefs.bottomsac, dive->get_cylinder(current_cylinder), false, divemode);
clock += timestep;
} while (trial_ascent(ds, 0, depth, 0, avg_depth, bottom_time, dive->get_cylinder(current_cylinder)->gasmix,
po2, diveplan.surface_pressure / 1000.0, dive, divemode) &&
po2, diveplan.surface_pressure.mbar / 1000.0, dive, divemode) &&
enough_gas(dive, current_cylinder) && clock < 6 * 3600);
// We did stay one timestep too many.
@ -821,7 +819,7 @@ std::vector<decostop> plan(struct deco_state *ds, struct diveplan &diveplan, str
decostoptable.clear();
is_final_plan = (decoMode(true) == BUEHLMANN) || (previous_deco_time - ds->deco_time < 10); // CVA time converges
if (ds->deco_time != 10000000)
vpmb_next_gradient(ds, ds->deco_time, diveplan.surface_pressure / 1000.0, true);
vpmb_next_gradient(ds, ds->deco_time, diveplan.surface_pressure.mbar / 1000.0, true);
previous_deco_time = ds->deco_time;
bottom_cache.restore(ds, true);
@ -836,7 +834,7 @@ std::vector<decostop> plan(struct deco_state *ds, struct diveplan &diveplan, str
stopidx = bottom_stopidx;
ds->first_ceiling_pressure.mbar = dive->depth_to_mbar(
deco_allowed_depth(tissue_tolerance_calc(ds, dive, dive->depth_to_bar(depth), true),
diveplan.surface_pressure / 1000.0, dive, 1));
diveplan.surface_pressure.mbar / 1000.0, dive, 1));
if (ds->max_bottom_ceiling_pressure.mbar > ds->first_ceiling_pressure.mbar)
ds->first_ceiling_pressure.mbar = ds->max_bottom_ceiling_pressure.mbar;
@ -893,7 +891,7 @@ std::vector<decostop> plan(struct deco_state *ds, struct diveplan &diveplan, str
if (current_cylinder != gaschanges[gi].gasidx) {
if (!prefs.switch_at_req_stop ||
!trial_ascent(ds, 0, depth, stoplevels[stopidx - 1], avg_depth, bottom_time,
dive->get_cylinder(current_cylinder)->gasmix, po2, diveplan.surface_pressure / 1000.0, dive, divemode) || get_o2(dive->get_cylinder(current_cylinder)->gasmix) < 160) {
dive->get_cylinder(current_cylinder)->gasmix, po2, diveplan.surface_pressure.mbar / 1000.0, dive, divemode) || get_o2(dive->get_cylinder(current_cylinder)->gasmix) < 160) {
if (is_final_plan)
plan_add_segment(diveplan, clock - previous_point_time, depth, current_cylinder, po2, false, divemode);
stopping = true;
@ -929,7 +927,7 @@ std::vector<decostop> plan(struct deco_state *ds, struct diveplan &diveplan, str
while (1) {
/* Check if ascending to next stop is clear, go back and wait if we hit the ceiling on the way */
if (trial_ascent(ds, 0, depth, stoplevels[stopidx], avg_depth, bottom_time,
dive->get_cylinder(current_cylinder)->gasmix, po2, diveplan.surface_pressure / 1000.0, dive, divemode)) {
dive->get_cylinder(current_cylinder)->gasmix, po2, diveplan.surface_pressure.mbar / 1000.0, dive, divemode)) {
decostoptable.push_back( decostop { depth, 0 });
break; /* We did not hit the ceiling */
}
@ -971,7 +969,7 @@ std::vector<decostop> plan(struct deco_state *ds, struct diveplan &diveplan, str
}
int new_clock = wait_until(ds, dive, clock, clock, laststoptime * 2 + 1, timestep, depth, stoplevels[stopidx], avg_depth,
bottom_time, dive->get_cylinder(current_cylinder)->gasmix, po2, diveplan.surface_pressure / 1000.0, divemode);
bottom_time, dive->get_cylinder(current_cylinder)->gasmix, po2, diveplan.surface_pressure.mbar / 1000.0, divemode);
laststoptime = new_clock - clock;
/* Finish infinite deco */
if (laststoptime >= 48 * 3600 && depth >= 6000) {

View file

@ -39,7 +39,7 @@ struct diveplan {
diveplan &operator=(diveplan &&) = default;
timestamp_t when = 0;
int surface_pressure = 0; /* mbar */
pressure_t surface_pressure;
int bottomsac = 0; /* ml/min */
int decosac = 0; /* ml/min */
int salinity = 0;

View file

@ -15,7 +15,6 @@
#define O2_DENSITY 1331 // mg/Liter
#define N2_DENSITY 1165
#define HE_DENSITY 166
#define SURFACE_PRESSURE 1013 // mbar
#define ZERO_C_IN_MKELVIN 273150 // mKelvin
#define M_OR_FT(_m, _f) ((prefs.units.length == units::METERS) ? ((_m) * 1000) : (feet_to_mm(_f)))
@ -345,12 +344,12 @@ static inline double to_PSI(pressure_t pressure)
static inline double bar_to_atm(double bar)
{
return bar / SURFACE_PRESSURE * 1000;
return bar / (1_atm).mbar * 1000;
}
static inline double mbar_to_atm(int mbar)
{
return (double)mbar / SURFACE_PRESSURE;
return (double)mbar / (1_atm).mbar;
}
static inline double mbar_to_PSI(int mbar)
@ -359,15 +358,13 @@ static inline double mbar_to_PSI(int mbar)
return to_PSI(p);
}
static inline int32_t altitude_to_pressure(int32_t altitude) // altitude in mm above sea level
{ // returns atmospheric pressure in mbar
return (int32_t) (1013.0 * exp(- altitude / 7800000.0));
static inline pressure_t altitude_to_pressure(int32_t altitude) { // altitude in mm above sea level
return pressure_t { .mbar = int_cast<int32_t> (1013.0 * exp(- altitude / 7800000.0)) };
}
static inline int32_t pressure_to_altitude(int32_t pressure) // pressure in mbar
static inline int32_t pressure_to_altitude(pressure_t pressure)
{ // returns altitude in mm above sea level
return (int32_t) (log(1013.0 / pressure) * 7800000);
return (int32_t) (log(1013.0 / pressure.mbar) * 7800000);
}
/*

View file

@ -170,8 +170,9 @@ void DivePlannerWidget::settingsChanged()
ui.startTime->setDisplayFormat(QString::fromStdString(prefs.time_format));
}
void DivePlannerWidget::atmPressureChanged(const int pressure)
void DivePlannerWidget::atmPressureChanged(int pressure_in_mbar)
{
pressure_t pressure { .mbar = pressure_in_mbar };
DivePlannerPointsModel::instance()->setSurfacePressure(pressure);
ui.atmHeight->blockSignals(true);
ui.atmHeight->setValue((int) get_depth_units((int) pressure_to_altitude(pressure), NULL, NULL));
@ -180,9 +181,9 @@ void DivePlannerWidget::atmPressureChanged(const int pressure)
void DivePlannerWidget::heightChanged(const int height)
{ // height is in ft or in meters
int pressure = (int) (altitude_to_pressure(units_to_depth((double) height).mm));
pressure_t pressure = altitude_to_pressure(units_to_depth((double) height).mm);
ui.ATMPressure->blockSignals(true);
ui.ATMPressure->setValue(pressure);
ui.ATMPressure->setValue(pressure.mbar);
ui.ATMPressure->blockSignals(false);
DivePlannerPointsModel::instance()->setSurfacePressure(pressure);
}

View file

@ -439,18 +439,18 @@ void TabDiveInformation::updateTextBox(int event) // Either the text box has bee
altitudeVal = feet_to_mm(altitudeVal); // imperial: convert altitude from feet to mm
else
altitudeVal = altitudeVal * 1000; // metric: convert altitude from meters to mm
atmpress.mbar = altitude_to_pressure((int32_t) altitudeVal); // convert altitude (mm) to pressure (mbar)
atmpress = altitude_to_pressure((int32_t) altitudeVal); // convert altitude (mm) to pressure (mbar)
ui->atmPressVal->setText(QString::number(atmpress.mbar));
setIndexNoSignal(ui->atmPressType, 0); // reset combobox to mbar
} else { // i.e. event == COMBO_CHANGED, that is, "m" or "ft" was selected from combobox
// Show estimated altitude
bool ok;
double convertVal = 0.0010; // Metric conversion fro mm to m
int pressure_as_integer = ui->atmPressVal->text().toInt(&ok,10);
pressure_t pressure = { .mbar = ui->atmPressVal->text().toInt(&ok,10) };
if (ok && ui->atmPressVal->text().length()) { // Show existing atm press as an altitude:
if (prefs.units.length == units::FEET) // For imperial units
convertVal = mm_to_feet(1); // convert from mm to ft
ui->atmPressVal->setText(QString::number((int)(pressure_to_altitude(pressure_as_integer) * convertVal)));
ui->atmPressVal->setText(QString::number((int)(pressure_to_altitude(pressure) * convertVal)));
}
}
break;

View file

@ -410,7 +410,7 @@ bool CylindersModel::setData(const QModelIndex &index, const QVariant &value, in
cyl.gasmix.he.permille = 1000 - get_o2(cyl.gasmix);
pressure_t modpO2;
if (d->dcs[0].divemode == PSCR)
modpO2.mbar = prefs.decopo2 + (1000 - get_o2(cyl.gasmix)) * SURFACE_PRESSURE *
modpO2.mbar = prefs.decopo2 + (1000 - get_o2(cyl.gasmix)) * (1_atm).mbar *
prefs.o2consumption / prefs.decosac / prefs.pscr_ratio;
else
modpO2.mbar = prefs.decopo2;

View file

@ -609,7 +609,7 @@ void DivePlannerPointsModel::setVpmbConservatism(int level)
}
}
void DivePlannerPointsModel::setSurfacePressure(int pressure)
void DivePlannerPointsModel::setSurfacePressure(pressure_t pressure)
{
diveplan.surface_pressure = pressure;
emitDataChanged();
@ -621,7 +621,7 @@ void DivePlannerPointsModel::setSalinity(int salinity)
emitDataChanged();
}
int DivePlannerPointsModel::getSurfacePressure() const
pressure_t DivePlannerPointsModel::getSurfacePressure() const
{
return diveplan.surface_pressure;
}

View file

@ -52,7 +52,7 @@ public:
int ascratestopsDisplay() const;
int ascratelast6mDisplay() const;
int descrateDisplay() const;
int getSurfacePressure() const;
pressure_t getSurfacePressure() const;
int gfLow() const;
int gfHigh() const;
@ -74,7 +74,7 @@ slots:
void setGFHigh(const int gfhigh);
void setGFLow(const int gflow);
void setVpmbConservatism(int level);
void setSurfacePressure(int pressure);
void setSurfacePressure(pressure_t pressure);
void setSalinity(int salinity);
void setBottomSac(double sac);
void setDecoSac(double sac);

View file

@ -34,8 +34,8 @@ void TestAirPressure::testReadAirPressure()
void TestAirPressure::testConvertAltitudetoAirPressure()
{
QCOMPARE(891,altitude_to_pressure(1000000)); // 1000 m altitude in mm
QCOMPARE(1013,altitude_to_pressure(0)); // sea level
QCOMPARE(891, altitude_to_pressure(1000000).mbar); // 1000 m altitude in mm
QCOMPARE(1013, altitude_to_pressure(0).mbar); // sea level
}
void TestAirPressure::testWriteReadBackAirPressure()

View file

@ -41,7 +41,7 @@ diveplan setupPlan()
{
diveplan dp;
dp.salinity = 10300;
dp.surface_pressure = 1013;
dp.surface_pressure = 1_atm;
dp.gfhigh = 100;
dp.gflow = 100;
dp.bottomsac = prefs.bottomsac;
@ -76,7 +76,7 @@ diveplan setupPlanVpmb45m30mTx()
{
diveplan dp;
dp.salinity = 10300;
dp.surface_pressure = 1013;
dp.surface_pressure = 1_atm;
dp.gfhigh = 100;
dp.gflow = 100;
dp.bottomsac = prefs.bottomsac;
@ -111,7 +111,7 @@ diveplan setupPlanVpmb60m10mTx()
{
diveplan dp;
dp.salinity = 10300;
dp.surface_pressure = 1013;
dp.surface_pressure = 1_atm;
dp.gfhigh = 100;
dp.gflow = 100;
dp.bottomsac = prefs.bottomsac;
@ -146,7 +146,7 @@ diveplan setupPlanVpmb60m30minAir()
{
diveplan dp;
dp.salinity = 10300;
dp.surface_pressure = 1013;
dp.surface_pressure = 1_atm;
dp.bottomsac = prefs.bottomsac;
dp.decosac = prefs.decosac;
@ -168,7 +168,7 @@ diveplan setupPlanVpmb60m30minEan50()
{
diveplan dp;
dp.salinity = 10300;
dp.surface_pressure = 1013;
dp.surface_pressure = 1_atm;
dp.bottomsac = prefs.bottomsac;
dp.decosac = prefs.decosac;
@ -198,7 +198,7 @@ diveplan setupPlanVpmb60m30minTx()
{
diveplan dp;
dp.salinity = 10300;
dp.surface_pressure = 1013;
dp.surface_pressure = 1_atm;
dp.bottomsac = prefs.bottomsac;
dp.decosac = prefs.decosac;
@ -228,7 +228,7 @@ diveplan setupPlanVpmbMultiLevelAir()
{
diveplan dp;
dp.salinity = 10300;
dp.surface_pressure = 1013;
dp.surface_pressure = 1_atm;
dp.bottomsac = prefs.bottomsac;
dp.decosac = prefs.decosac;
@ -252,7 +252,7 @@ diveplan setupPlanVpmb100m60min()
{
diveplan dp;
dp.salinity = 10300;
dp.surface_pressure = 1013;
dp.surface_pressure = 1_atm;
dp.bottomsac = prefs.bottomsac;
dp.decosac = prefs.decosac;
@ -286,7 +286,7 @@ diveplan setupPlanVpmb100m10min()
{
diveplan dp;
dp.salinity = 10300;
dp.surface_pressure = 1013;
dp.surface_pressure = 1_atm;
dp.bottomsac = prefs.bottomsac;
dp.decosac = prefs.decosac;
@ -320,7 +320,7 @@ diveplan setupPlanVpmb30m20min()
{
diveplan dp;
dp.salinity = 10300;
dp.surface_pressure = 1013;
dp.surface_pressure = 1_atm;
dp.bottomsac = prefs.bottomsac;
dp.decosac = prefs.decosac;
@ -342,7 +342,7 @@ diveplan setupPlanVpmb100mTo70m30min()
{
diveplan dp;
dp.salinity = 10300;
dp.surface_pressure = 1013;
dp.surface_pressure = 1_atm;
dp.bottomsac = prefs.bottomsac;
dp.decosac = prefs.decosac;
@ -384,7 +384,7 @@ diveplan setupPlanSeveralGases()
{
diveplan dp;
dp.salinity = 10300;
dp.surface_pressure = 1013;
dp.surface_pressure = 1_atm;
dp.bottomsac = prefs.bottomsac;
dp.decosac = prefs.decosac;
@ -414,7 +414,7 @@ diveplan setupPlanCcr()
{
diveplan dp;
dp.salinity = 10300;
dp.surface_pressure = 1013;
dp.surface_pressure = 1_atm;
dp.gflow = 50;
dp.gfhigh = 70;
dp.bottomsac = prefs.bottomsac;