diff options
author | Robert C. Helling <helling@atdotde.de> | 2020-09-02 20:14:45 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2020-09-06 12:50:59 -0700 |
commit | b1a1dc36398530fa83038c9f71f480b9b955af22 (patch) | |
tree | 9108488c6c48e74767457e7a607371fce57d238f | |
parent | d404aa767feb24b19da50ba833723b3dd0cfb271 (diff) | |
download | subsurface-b1a1dc36398530fa83038c9f71f480b9b955af22.tar.gz |
Function for "gravity conversion"
This adds a common macro to convert salinity (which is
given as a density in terms of g per 10l) to a specific
weight with units of mbar / mm = bar / m that is used
to translate between pressures and depths.
The weired factor of 10 (from the unusual unit of salinity)
is included in the macro. It is there for historical reasons,
as it goes back to 05b55542c8 from 2012 where it was introduced
in code for downloading from Uemis dive computers.
Now, salinity appears in too many places to easily remove
this unconventional factor of 10 everywhere without breaking
to many things (including various dive computer downloads).
Signed-off-by: Robert C. Helling <helling@atdotde.de>
-rw-r--r-- | core/dive.c | 26 | ||||
-rw-r--r-- | core/dive.h | 1 |
2 files changed, 18 insertions, 9 deletions
diff --git a/core/dive.c b/core/dive.c index 6bed5966a..49a468e5a 100644 --- a/core/dive.c +++ b/core/dive.c @@ -38,6 +38,8 @@ const char *divemode_text_ui[] = { // For writing/reading files. const char *divemode_text[] = {"OC", "CCR", "PSCR", "Freedive"}; +static int calculate_depth_to_mbar(int depth, pressure_t surface_pressure, int salinity); + /* * Adding a cylinder pressure sample field is not quite as trivial as it * perhaps should be. @@ -3688,10 +3690,20 @@ int get_surface_pressure_in_mbar(const struct dive *dive, bool non_null) return mbar; } +/* This returns the conversion factor that you need to multiply + * a (relative) depth in mm to obtain a (relative) pressure in mbar. + * As everywhere in Subsurface, the expected unit of a salinity is + * g/10l such that sea water has a salinity of 10300 + */ +static double salinity_to_specific_weight(int salinity) +{ + return salinity * 0.981 / 100000.0; +} + /* Pa = N/m^2 - so we determine the weight (in N) of the mass of 10m * of water (and use standard salt water at 1.03kg per liter if we don't know salinity) * and add that to the surface pressure (or to 1013 if that's unknown) */ -int calculate_depth_to_mbar(int depth, pressure_t surface_pressure, int salinity) +static int calculate_depth_to_mbar(int depth, pressure_t surface_pressure, int salinity) { double specific_weight; int mbar = surface_pressure.mbar; @@ -3702,8 +3714,8 @@ int calculate_depth_to_mbar(int depth, pressure_t surface_pressure, int salinity salinity = SEAWATER_SALINITY; if (salinity < 500) salinity += FRESHWATER_SALINITY; - specific_weight = salinity / 10000.0 * 0.981; - mbar += lrint(depth / 10.0 * specific_weight); + specific_weight = salinity_to_specific_weight(salinity); + mbar += lrint(depth * specific_weight); return mbar; } @@ -3728,13 +3740,11 @@ double depth_to_atm(int depth, const struct dive *dive) * take care of this, but the Uemis we support natively */ int rel_mbar_to_depth(int mbar, const struct dive *dive) { - int cm; - double specific_weight = SEAWATER_SALINITY * 0.981 / 10000.0; + double specific_weight = salinity_to_specific_weight(SEAWATER_SALINITY); if (dive->dc.salinity) - specific_weight = dive->dc.salinity / 10000.0 * 0.981; + specific_weight = salinity_to_specific_weight(dive->dc.salinity); /* whole mbar gives us cm precision */ - cm = (int)lrint(mbar / specific_weight); - return cm * 10; + return (int)lrint(mbar / specific_weight); } int mbar_to_depth(int mbar, const struct dive *dive) diff --git a/core/dive.h b/core/dive.h index 7f4f805c6..4f94dba6b 100644 --- a/core/dive.h +++ b/core/dive.h @@ -212,7 +212,6 @@ extern fraction_t best_o2(depth_t depth, const struct dive *dive); extern fraction_t best_he(depth_t depth, const struct dive *dive, bool o2narcotic, fraction_t fo2); extern int get_surface_pressure_in_mbar(const struct dive *dive, bool non_null); -extern int calculate_depth_to_mbar(int depth, pressure_t surface_pressure, int salinity); extern int depth_to_mbar(int depth, const struct dive *dive); extern double depth_to_bar(int depth, const struct dive *dive); extern double depth_to_atm(int depth, const struct dive *dive); |