diff options
author | Tim Segers <tsegers@pm.me> | 2023-08-08 20:48:27 +0200 |
---|---|---|
committer | Tim Segers <tsegers@pm.me> | 2023-08-21 20:54:41 +0200 |
commit | b4582977251dbf07c13c4d69a4f7be04e75612d6 (patch) | |
tree | 5d49111f98766bf06302b4dea851d580ae78d319 | |
parent | c46ebe4b35e3a0ffebf00b0eaa31b842279396b6 (diff) | |
download | opendeco-b4582977251dbf07c13c4d69a4f7be04e75612d6.tar.gz |
Split unit conversion functions out from deco.cdevelop
-rw-r--r-- | Makefile | 6 | ||||
-rw-r--r-- | src/deco.c | 54 | ||||
-rw-r--r-- | src/deco.h | 19 | ||||
-rw-r--r-- | src/opendeco-conf.h | 1 | ||||
-rw-r--r-- | src/opendeco.c | 1 | ||||
-rw-r--r-- | src/output.c | 1 | ||||
-rw-r--r-- | src/schedule.c | 1 | ||||
-rw-r--r-- | src/units.c | 59 | ||||
-rw-r--r-- | src/units.h | 30 | ||||
-rw-r--r-- | test/deco_test.c | 1 |
10 files changed, 98 insertions, 75 deletions
@@ -10,9 +10,9 @@ LDFLAGS = $(LFLAGS) PREFIX = /usr/local -OBJ_BIN = src/opendeco.o src/opendeco-cli.o src/opendeco-conf.o src/deco.o src/output.o src/schedule.o src/gas.o toml/toml.o -OBJ_LIB = src/deco.o src/output.o src/schedule.o src/gas.o -OBJ_TST = test/opendeco_test.o test/deco_test.o src/deco.o src/gas.o minunit/minunit.o +OBJ_BIN = src/opendeco.o src/opendeco-cli.o src/opendeco-conf.o src/deco.o src/output.o src/schedule.o src/gas.o src/units.o toml/toml.o +OBJ_LIB = src/deco.o src/output.o src/schedule.o src/gas.o src/units.o +OBJ_TST = test/opendeco_test.o test/deco_test.o src/deco.o src/gas.o src/units.o minunit/minunit.o LICENSES = minunit/LICENSE.h toml/LICENSE.h @@ -5,12 +5,11 @@ #include <stdbool.h> #include "deco.h" +#include "units.h" #define RND(x) (round((x) *10000) / 10000) enum ALGO ALGO_VER = ALGO_VER_DEFAULT; -enum UNITS UNITS = UNITS_DEFAULT; -double SURFACE_PRESSURE = SURFACE_PRESSURE_DEFAULT; double P_WV = P_WV_DEFAULT; double END_MAX = END_MAX_DEFAULT; @@ -65,57 +64,6 @@ const zhl_he_t ZHL16He[] = { {.t = 240.03, .a = 0.5119, .b = 0.9267}, }; -double bar_to_msw(double bar) -{ - return bar * 10; -} - -double msw_to_bar(double msw) -{ - return msw / 10; -} - -double bar_to_fsw(double bar) -{ - return (bar / 1.01325) * 33.0; -} - -double fsw_to_bar(double fsw) -{ - return (fsw * 1.01325) / 33.0; -} - -double msw_or_fsw(double msw, double fsw) -{ - assert(UNITS == METRIC || UNITS == IMPERIAL); - - return (UNITS == METRIC) ? msw : fsw; -} - -double xsw_to_bar(double xsw) -{ - assert(UNITS == METRIC || UNITS == IMPERIAL); - - return (UNITS == METRIC) ? msw_to_bar(xsw) : fsw_to_bar(xsw); -} - -double bar_to_xsw(double bar) -{ - assert(UNITS == METRIC || UNITS == IMPERIAL); - - return (UNITS == METRIC) ? bar_to_msw(bar) : bar_to_fsw(bar); -} - -double abs_depth(double gd) -{ - return gd + SURFACE_PRESSURE; -} - -double gauge_depth(double ad) -{ - return ad - SURFACE_PRESSURE; -} - void add_segment_ascdec(decostate_t *ds, double dstart, double dend, double time, const gas_t *gas) { assert(time > 0); @@ -16,16 +16,9 @@ #define P_WV_SCHR 0.0493 /* Schreiner value, Rq = 0.8, most conservative */ #define ALGO_VER_DEFAULT ZHL_16C -#define UNITS_DEFAULT METRIC -#define SURFACE_PRESSURE_DEFAULT 1.01325 #define P_WV_DEFAULT P_WV_BUHL /* types */ -enum UNITS { - METRIC, - IMPERIAL, -}; - enum ALGO { ZHL_16A = 0, ZHL_16B = 1, @@ -47,24 +40,12 @@ typedef struct decostate_t { /* global variables */ extern enum ALGO ALGO_VER; -extern enum UNITS UNITS; -extern double SURFACE_PRESSURE; extern double P_WV; extern double PO2_MAX; extern double END_MAX; /* functions */ -double bar_to_msw(double bar); -double msw_to_bar(double msw); -double bar_to_fsw(double bar); -double fsw_to_bar(double msw); -double msw_or_fsw(double msw, double fsw); -double xsw_to_bar(double xsw); -double bar_to_xsw(double bar); -double abs_depth(double gd); -double gauge_depth(double ad); - void add_segment_ascdec(decostate_t *ds, double dstart, double dend, double time, const gas_t *gas); void add_segment_const(decostate_t *ds, double depth, double time, const gas_t *gas); double get_gf(const decostate_t *ds, double depth); diff --git a/src/opendeco-conf.h b/src/opendeco-conf.h index 359889a..2f49d0f 100644 --- a/src/opendeco-conf.h +++ b/src/opendeco-conf.h @@ -4,6 +4,7 @@ #define OPENDECOCONF_H #include "deco.h" +#include "units.h" #ifndef VERSION #define VERSION "unknown version" diff --git a/src/opendeco.c b/src/opendeco.c index 926ff50..b57004b 100644 --- a/src/opendeco.c +++ b/src/opendeco.c @@ -13,6 +13,7 @@ #include "opendeco-conf.h" #include "output.h" #include "schedule.h" +#include "units.h" #define MOD_OXY (abs_depth(xsw_to_bar(msw_or_fsw(6, 20)))) diff --git a/src/output.c b/src/output.c index 43ee82a..07eacbe 100644 --- a/src/output.c +++ b/src/output.c @@ -6,6 +6,7 @@ #include <errno.h> #include "output.h" +#include "units.h" #define DEPTHUNIT ((UNITS == METRIC) ? "m " : "ft") diff --git a/src/schedule.c b/src/schedule.c index 5786159..fdd81a2 100644 --- a/src/schedule.c +++ b/src/schedule.c @@ -5,6 +5,7 @@ #include <stdbool.h> #include "schedule.h" +#include "units.h" #define STOPLEN_ROUGH 10 #define STOPLEN_FINE 1 diff --git a/src/units.c b/src/units.c new file mode 100644 index 0000000..519f4c2 --- /dev/null +++ b/src/units.c @@ -0,0 +1,59 @@ +/* SPDX-License-Identifier: MIT-0 */ + +#include <assert.h> + +#include "units.h" + +enum UNITS UNITS = UNITS_DEFAULT; +double SURFACE_PRESSURE = SURFACE_PRESSURE_DEFAULT; + +double bar_to_msw(double bar) +{ + return bar * 10; +} + +double msw_to_bar(double msw) +{ + return msw / 10; +} + +double bar_to_fsw(double bar) +{ + return (bar / 1.01325) * 33.0; +} + +double fsw_to_bar(double fsw) +{ + return (fsw * 1.01325) / 33.0; +} + +double msw_or_fsw(double msw, double fsw) +{ + assert(UNITS == METRIC || UNITS == IMPERIAL); + + return (UNITS == METRIC) ? msw : fsw; +} + +double xsw_to_bar(double xsw) +{ + assert(UNITS == METRIC || UNITS == IMPERIAL); + + return (UNITS == METRIC) ? msw_to_bar(xsw) : fsw_to_bar(xsw); +} + +double bar_to_xsw(double bar) +{ + assert(UNITS == METRIC || UNITS == IMPERIAL); + + return (UNITS == METRIC) ? bar_to_msw(bar) : bar_to_fsw(bar); +} + +double abs_depth(double gd) +{ + return gd + SURFACE_PRESSURE; +} + +double gauge_depth(double ad) +{ + return ad - SURFACE_PRESSURE; +} diff --git a/src/units.h b/src/units.h new file mode 100644 index 0000000..1557bc0 --- /dev/null +++ b/src/units.h @@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: MIT-0 */ + +#ifndef UNITS_H +#define UNITS_H + +#define UNITS_DEFAULT METRIC +#define SURFACE_PRESSURE_DEFAULT 1.01325 + +/* types */ +enum UNITS { + METRIC, + IMPERIAL, +}; + +/* global variables */ +extern enum UNITS UNITS; +extern double SURFACE_PRESSURE; + +/* functions */ +double bar_to_msw(double bar); +double msw_to_bar(double msw); +double bar_to_fsw(double bar); +double fsw_to_bar(double msw); +double msw_or_fsw(double msw, double fsw); +double xsw_to_bar(double xsw); +double bar_to_xsw(double bar); +double abs_depth(double gd); +double gauge_depth(double ad); + +#endif /* end of include guard: UNITS_H */ diff --git a/test/deco_test.c b/test/deco_test.c index 0662b46..c71b8de 100644 --- a/test/deco_test.c +++ b/test/deco_test.c @@ -3,6 +3,7 @@ #include "minunit/minunit.h" #include "src/deco.h" +#include "src/units.h" MU_TEST(test_bar_to_msw) { |