diff options
author | Robert C. Helling <helling@atdotde.de> | 2021-10-01 15:50:21 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2021-10-01 08:50:36 -0700 |
commit | 41258647d232a24adca394202b44b204409b4c9f (patch) | |
tree | a1c0b94973d5e8795cd3b8dba44542c2efa2bdbb /core | |
parent | 5e9ee9febb595ced1699e1f60a659e01266ec7e9 (diff) | |
download | subsurface-41258647d232a24adca394202b44b204409b4c9f.tar.gz |
Don't access gasmix.o2.fraction
Air is a special gas that does not contain oxygen according
to gasmix.o2.fraction. If you want to use the fo2, you
need to use get_o2() to treat this special case correctly.
This fixes a bug when setting the MND of a gas containing
21% oxygen when o2 is considered not narcotic.
Reported-by: Christoph Gruen <gruen.christoph@gmail.com>
Signed-off-by: Robert C. Helling <helling@atdotde.de>
Diffstat (limited to 'core')
-rw-r--r-- | core/gas-model.c | 4 | ||||
-rw-r--r-- | core/gas.c | 22 | ||||
-rw-r--r-- | core/gas.h | 5 |
3 files changed, 17 insertions, 14 deletions
diff --git a/core/gas-model.c b/core/gas-model.c index 44c64864a..115da0356 100644 --- a/core/gas-model.c +++ b/core/gas-model.c @@ -86,7 +86,9 @@ double isothermal_pressure(struct gasmix gas, double p1, int volume1, int volume double gas_density(struct gasmix gas, int pressure) { - int density = gas.he.permille * HE_DENSITY + gas.o2.permille * O2_DENSITY + (1000 - gas.he.permille - gas.o2.permille) * N2_DENSITY; + int fo2 = get_o2(gas); + int fhe = get_he(gas); + int density = fhe * HE_DENSITY + fo2 * O2_DENSITY + (1000 - fhe - fo2) * N2_DENSITY; return density * (double) pressure / gas_compressibility_factor(gas, pressure / 1000.0) / SURFACE_PRESSURE / 1000000.0; } diff --git a/core/gas.c b/core/gas.c index 8dbfe61cb..1e74dccfc 100644 --- a/core/gas.c +++ b/core/gas.c @@ -33,15 +33,15 @@ int same_gasmix(struct gasmix a, struct gasmix b) return 0; if (gasmix_is_air(a) && gasmix_is_air(b)) return 1; - return a.o2.permille == b.o2.permille && a.he.permille == b.he.permille; + return get_o2(a) == get_o2(b) && get_he(a) == get_he(b); } void sanitize_gasmix(struct gasmix *mix) { unsigned int o2, he; - o2 = mix->o2.permille; - he = mix->he.permille; + o2 = get_o2(*mix); + he = get_he(*mix); /* Regular air: leave empty */ if (!he) { @@ -74,12 +74,12 @@ int gasmix_distance(struct gasmix a, struct gasmix b) bool gasmix_is_air(struct gasmix gasmix) { - int o2 = gasmix.o2.permille; - int he = gasmix.he.permille; + int o2 = get_o2(gasmix); + int he = get_he(gasmix); return (he == 0) && (o2 == 0 || ((o2 >= O2_IN_AIR - 1) && (o2 <= O2_IN_AIR + 1))); } -static fraction_t make_fraction(int i) +fraction_t make_fraction(int i) { fraction_t res; res.permille = i; @@ -152,13 +152,13 @@ enum gastype gasmix_to_type(struct gasmix mix) { if (gasmix_is_air(mix)) return GASTYPE_AIR; - if (mix.o2.permille >= 980) + if (get_o2(mix) >= 980) return GASTYPE_OXYGEN; - if (mix.he.permille == 0) - return mix.o2.permille >= 230 ? GASTYPE_NITROX : GASTYPE_AIR; - if (mix.o2.permille <= 180) + if (get_he(mix) == 0) + return get_o2(mix) >= 230 ? GASTYPE_NITROX : GASTYPE_AIR; + if (get_o2(mix) <= 180) return GASTYPE_HYPOXIC_TRIMIX; - return mix.o2.permille <= 230 ? GASTYPE_NORMOXIC_TRIMIX : GASTYPE_HYPEROXIC_TRIMIX; + return get_o2(mix) <= 230 ? GASTYPE_NORMOXIC_TRIMIX : GASTYPE_HYPEROXIC_TRIMIX; } static const char *gastype_names[] = { diff --git a/core/gas.h b/core/gas.h index 83eef72e9..b3c539c61 100644 --- a/core/gas.h +++ b/core/gas.h @@ -44,12 +44,12 @@ extern double isothermal_pressure(struct gasmix gas, double p1, int volume1, int extern double gas_density(struct gasmix gas, int pressure); extern int same_gasmix(struct gasmix a, struct gasmix b); -static inline int get_o2(struct gasmix mix) +static inline int get_o2(const struct gasmix mix) { return mix.o2.permille ?: O2_IN_AIR; } -static inline int get_he(struct gasmix mix) +static inline int get_he(const struct gasmix mix) { return mix.he.permille; } @@ -74,6 +74,7 @@ extern bool gasmix_is_air(struct gasmix gasmix); extern bool gasmix_is_invalid(struct gasmix mix); extern enum gastype gasmix_to_type(struct gasmix mix); extern const char *gastype_name(enum gastype type); +extern fraction_t make_fraction(int f); #ifdef __cplusplus } |