Mercurial > public > ostc4
annotate Discovery/Src/buehlmann.c @ 254:a17f7fb3b2b5 bm-3
Buehlmann: cleanup, remove global pDecolistBuehlmann
Yes, I hate global data, and a simple renaming of a pointer almost
qualifies for participation in the Obfuscated C Code Contest :-)
Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
author | Jan Mulder <jlmulder@xs4all.nl> |
---|---|
date | Fri, 12 Apr 2019 14:46:48 +0200 |
parents | 1663b3b204d7 |
children | dcf7a3435fe1 |
rev | line source |
---|---|
51
8f8ea3a32e82
Resolved warnings pointing to possible invalid memory access
Ideenmodellierer
parents:
38
diff
changeset
|
1 /* getrennte Gase f�r die verschiedenen Modi |
8f8ea3a32e82
Resolved warnings pointing to possible invalid memory access
Ideenmodellierer
parents:
38
diff
changeset
|
2 um Gaswechsel Eintr�ge zu vereinfachen |
38 | 3 das heisst: |
4 oc == bailout in cc mode | |
5 */ | |
6 | |
7 /* Konvention: | |
8 float extExample_variable_can_be_used_with_extern; | |
9 */ | |
10 | |
11 #include <string.h> | |
12 #include <math.h> | |
13 #include <stdbool.h> | |
14 #include "buehlmann.h" | |
15 #include "decom.h" | |
16 | |
17 | |
18 extern const float buehlmann_N2_a[]; | |
19 extern const float buehlmann_N2_b[]; | |
20 extern const float buehlmann_He_a[]; | |
21 extern const float buehlmann_He_b[]; | |
22 | |
23 typedef struct | |
24 { | |
25 float depth; | |
26 int id; | |
27 } SStop; | |
28 | |
29 #define DECO_STOPS_MAX_TTS_CALCULATON_IN_SECONDS 59940 // 999 minuten; before: 18000 // 5(h) * 60(min) * 60 sec = 18000 sec | |
30 #define DECO_STOPS_MAX_TTS_FOR_EVERY_SECOND_CALC_IN_SECONDS 7200 | |
31 #define NINETY_NINE_MINUTES_IN_SECONDS 59940 | |
32 | |
33 # define PRESSURE_TEN_METER 1.0f | |
34 # define PRESSURE_THREE_METER 0.333334f | |
35 # define PRESSURE_150_CM 0.15f | |
36 # define PRESSURE_HALF_METER 0.05f | |
37 | |
253
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
38 static void buehlmann_backup_and_restore(_Bool backup_restore_otherwise); |
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
39 static float tissue_tolerance(void); |
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
40 static void ambient_bar_to_deco_stop_depth_bar(float ceiling); |
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
41 static int ascend_with_all_gaschanges(float pressure_decrease); |
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
42 static float next_stop_depth_input_is_actual_stop_id(int actual_id); |
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
43 static float get_gf_at_pressure(float pressure); |
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
44 static void buehlmann_calc_ndl(void); |
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
45 static _Bool dive1_check_deco(void); |
38 | 46 |
253
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
47 static SDecoinfo gDecotable; |
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
48 static float gSurface_pressure_bar; |
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
49 static float gPressure; |
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
50 static int gGas_id; |
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
51 static float gTTS; |
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
52 static float gTissue_nitrogen_bar[16]; |
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
53 static float gTissue_helium_bar[16]; |
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
54 static float gGF_value; |
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
55 static float gCNS; |
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
56 static int gNDL; |
38 | 57 |
58 SDiveSettings *pBuDiveSettings; | |
59 float gGF_low_depth_bar; | |
60 SStop gStop; | |
61 | |
62 void buehlmann_init(void) | |
63 { | |
253
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
64 |
38 | 65 } |
66 | |
253
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
67 static void buehlmann_backup_and_restore(_Bool backup_restore_otherwise) |
38 | 68 { |
69 static float pressure; | |
70 static float gas_id; | |
71 static float tts; | |
72 static float tissue_nitrogen_bar[16]; | |
73 static float tissue_helium_bar[16]; | |
74 static float gf_value; | |
75 static int ndl; | |
76 static float cns; | |
77 | |
78 if(backup_restore_otherwise) | |
79 { | |
80 pressure = gPressure; | |
81 gas_id = gGas_id; | |
82 tts = gTTS; | |
83 gf_value = gGF_value; | |
84 ndl = gNDL; | |
85 cns = gCNS; | |
86 memcpy(tissue_nitrogen_bar, gTissue_nitrogen_bar, (4*16)); | |
87 memcpy(tissue_helium_bar, gTissue_helium_bar, (4*16)); | |
88 } | |
89 else | |
90 { | |
91 gPressure = pressure; | |
92 gGas_id = gas_id; | |
93 gTTS = tts; | |
94 gGF_value = gf_value; | |
95 gNDL = ndl; | |
96 gCNS = cns; | |
97 memcpy(gTissue_nitrogen_bar, tissue_nitrogen_bar, (4*16)); | |
98 memcpy(gTissue_helium_bar, tissue_helium_bar, (4*16)); | |
99 } | |
100 | |
101 } | |
102 | |
103 float buehlmann_get_gCNS(void) | |
104 { | |
105 return gCNS; | |
106 } | |
107 | |
108 void buehlmann_calc_deco(SLifeData* pLifeData, SDiveSettings * pDiveSettings, SDecoinfo * pDecoInfo) | |
109 { | |
110 float ceiling; | |
111 int ascend_time; | |
112 int tts_seconds; | |
113 float pressure_delta; | |
114 float next_depth; | |
115 _Bool deco_reached = false; | |
116 unsigned short *stoplist; | |
117 int i; | |
118 | |
119 gCNS = 0; | |
253
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
120 pDecoInfo->output_time_to_surface_seconds = 0; |
38 | 121 pDecoInfo->output_ndl_seconds = 0; |
122 for(int i=0;i<DECOINFO_STRUCT_MAX_STOPS;i++) | |
123 { | |
124 pDecoInfo->output_stop_length_seconds[i] = 0; | |
125 } | |
126 /* make input available global*/ | |
127 pBuDiveSettings = pDiveSettings; | |
128 | |
129 /* internal copying */ | |
130 gSurface_pressure_bar = pLifeData->pressure_surface_bar; | |
131 | |
132 gPressure = pLifeData->pressure_ambient_bar; | |
133 gGas_id = 0; | |
134 memcpy(gTissue_nitrogen_bar, pLifeData->tissue_nitrogen_bar, (4*16)); | |
135 memcpy(gTissue_helium_bar, pLifeData->tissue_helium_bar, (4*16)); | |
136 gGF_value = ((float)pBuDiveSettings->gf_low) / 100.0f; | |
137 | |
138 // | |
254
a17f7fb3b2b5
Buehlmann: cleanup, remove global pDecolistBuehlmann
Jan Mulder <jlmulder@xs4all.nl>
parents:
253
diff
changeset
|
139 memcpy(&gDecotable, pDecoInfo, sizeof(SDecoinfo)); |
38 | 140 stoplist = gDecotable.output_stop_length_seconds; |
141 | |
253
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
142 if(pLifeData->dive_time_seconds < 60) |
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
143 return; |
38 | 144 |
145 // clean stop list | |
146 for(i = 0; i < DECOINFO_STRUCT_MAX_STOPS; i++) | |
147 stoplist[i] = 0; | |
148 gTTS = 0; | |
149 gNDL = 0; | |
150 | |
253
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
151 if(pDiveSettings->internal__pressure_first_stop_ambient_bar_as_upper_limit_for_gf_low_otherwise_zero >= (gPressure - PRESSURE_150_CM)) |
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
152 { |
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
153 deco_reached = true; |
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
154 } |
38 | 155 |
156 gGF_value = ((float)pBuDiveSettings->gf_high) / 100.0f; | |
157 buehlmann_backup_and_restore(true); // includes backup for gGF_value | |
158 if(!dive1_check_deco() ) | |
159 { | |
160 buehlmann_backup_and_restore(false); | |
161 // no deco | |
254
a17f7fb3b2b5
Buehlmann: cleanup, remove global pDecolistBuehlmann
Jan Mulder <jlmulder@xs4all.nl>
parents:
253
diff
changeset
|
162 pDecoInfo->output_time_to_surface_seconds = 0; |
38 | 163 for(i = 0; i < DECOINFO_STRUCT_MAX_STOPS; i++) |
254
a17f7fb3b2b5
Buehlmann: cleanup, remove global pDecolistBuehlmann
Jan Mulder <jlmulder@xs4all.nl>
parents:
253
diff
changeset
|
164 pDecoInfo->output_stop_length_seconds[i] = 0; |
38 | 165 // calc NDL |
166 buehlmann_calc_ndl(); | |
254
a17f7fb3b2b5
Buehlmann: cleanup, remove global pDecolistBuehlmann
Jan Mulder <jlmulder@xs4all.nl>
parents:
253
diff
changeset
|
167 pDecoInfo->output_ndl_seconds = gNDL; |
38 | 168 return; |
169 } | |
170 buehlmann_backup_and_restore(false); | |
254
a17f7fb3b2b5
Buehlmann: cleanup, remove global pDecolistBuehlmann
Jan Mulder <jlmulder@xs4all.nl>
parents:
253
diff
changeset
|
171 pDecoInfo->output_ndl_seconds = 0; |
38 | 172 |
253
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
173 gGF_value = get_gf_at_pressure(gPressure); |
38 | 174 //current ceiling at actual position |
175 ceiling = tissue_tolerance(); | |
253
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
176 ambient_bar_to_deco_stop_depth_bar(ceiling); |
38 | 177 |
178 // set the base for all upcoming parameters | |
179 ceiling = gStop.depth + gSurface_pressure_bar; | |
180 tts_seconds = 0; | |
181 | |
182 // modify parameters if there is ascend or parameter fine adjustment | |
183 if(ceiling < (gPressure - PRESSURE_150_CM)) // more than 1.5 meter below ceiling | |
184 { | |
185 // ascend within 10 mtr to GF_low // speed 12 mtr/min -> 50 sec / 10 mtr; 15 sec / 3 mtr. | |
186 if(ceiling < (gPressure - PRESSURE_TEN_METER) ) | |
187 { do { | |
188 ascend_time = ascend_with_all_gaschanges(PRESSURE_TEN_METER); | |
189 tts_seconds += ascend_time; | |
190 ceiling = tissue_tolerance(); | |
191 if(tts_seconds > DECO_STOPS_MAX_TTS_CALCULATON_IN_SECONDS) | |
192 { | |
254
a17f7fb3b2b5
Buehlmann: cleanup, remove global pDecolistBuehlmann
Jan Mulder <jlmulder@xs4all.nl>
parents:
253
diff
changeset
|
193 pDecoInfo->output_time_to_surface_seconds = NINETY_NINE_MINUTES_IN_SECONDS; |
38 | 194 return;// NINETY_NINE_MINUTES_IN_SECONDS; |
195 } | |
196 } while ((ascend_time > 0 ) && ((gPressure - PRESSURE_TEN_METER ) > gSurface_pressure_bar) && (ceiling < (gPressure - PRESSURE_TEN_METER))); | |
197 } | |
198 do { | |
199 buehlmann_backup_and_restore(true); | |
200 ascend_time = ascend_with_all_gaschanges(PRESSURE_THREE_METER); | |
201 tts_seconds += ascend_time; | |
202 ceiling = tissue_tolerance(); | |
203 if(tts_seconds > DECO_STOPS_MAX_TTS_CALCULATON_IN_SECONDS) | |
204 { | |
254
a17f7fb3b2b5
Buehlmann: cleanup, remove global pDecolistBuehlmann
Jan Mulder <jlmulder@xs4all.nl>
parents:
253
diff
changeset
|
205 pDecoInfo->output_time_to_surface_seconds = NINETY_NINE_MINUTES_IN_SECONDS; |
38 | 206 return;// NINETY_NINE_MINUTES_IN_SECONDS; |
207 } | |
208 ambient_bar_to_deco_stop_depth_bar(ceiling); | |
209 } while ((ascend_time > 0 ) && ((gStop.depth + gSurface_pressure_bar) < gPressure)); | |
210 | |
211 if(gStop.depth + gSurface_pressure_bar > gPressure) | |
212 { | |
213 gPressure += PRESSURE_THREE_METER; | |
214 buehlmann_backup_and_restore(false); | |
215 tts_seconds -= ascend_time; | |
216 } | |
217 // calculate first stop based on tissue saturation within 10 meters of stop | |
218 //ambient_bar_to_deco_stop_depth_bar(ceiling); | |
219 } | |
220 else | |
221 { | |
222 // initial values, upper code might not be executed (is within 150 cm) | |
223 } | |
224 | |
253
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
225 if (ceiling > gSurface_pressure_bar) |
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
226 { |
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
227 ceiling = gStop.depth + gSurface_pressure_bar; |
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
228 // ascend the last meters to first stop (especially consider any gas changes around) |
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
229 pressure_delta = gPressure - ceiling; |
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
230 ascend_time = (int) ceil(pressure_delta * 50.0f); |
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
231 tts_seconds += ascend_with_all_gaschanges(pressure_delta); |
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
232 } |
38 | 233 // NDL check |
234 if(ceiling <= gSurface_pressure_bar) | |
235 { | |
236 // NDL with GF_low | |
254
a17f7fb3b2b5
Buehlmann: cleanup, remove global pDecolistBuehlmann
Jan Mulder <jlmulder@xs4all.nl>
parents:
253
diff
changeset
|
237 pDecoInfo->output_time_to_surface_seconds = 0; |
38 | 238 return; |
239 } | |
253
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
240 if (ceiling > pDiveSettings->internal__pressure_first_stop_ambient_bar_as_upper_limit_for_gf_low_otherwise_zero) |
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
241 pDiveSettings->internal__pressure_first_stop_ambient_bar_as_upper_limit_for_gf_low_otherwise_zero = ceiling; |
38 | 242 |
243 // calc gf loop | |
244 if(deco_reached) | |
245 gGF_low_depth_bar = pDiveSettings->internal__pressure_first_stop_ambient_bar_as_upper_limit_for_gf_low_otherwise_zero - gSurface_pressure_bar; | |
246 else | |
247 gGF_low_depth_bar = ceiling - gSurface_pressure_bar; | |
248 | |
249 while(gStop.depth > 0) | |
250 { | |
251 do | |
252 { | |
253 next_depth = next_stop_depth_input_is_actual_stop_id(gStop.id); | |
253
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
254 gGF_value = get_gf_at_pressure(next_depth + gSurface_pressure_bar); |
38 | 255 buehlmann_backup_and_restore(true); |
256 ascend_time = ascend_with_all_gaschanges(gStop.depth - next_depth); | |
257 ceiling = tissue_tolerance(); | |
258 /* pre check actual limit */ | |
259 if(gDecotable.output_stop_length_seconds[gStop.id] >= 999*60) | |
260 { | |
261 tts_seconds -= 999*60 - gDecotable.output_stop_length_seconds[gStop.id]; | |
262 gDecotable.output_stop_length_seconds[gStop.id] = 999*60; | |
263 } | |
264 else | |
265 /* more deco on the actual depth */ | |
266 if(ceiling > next_depth + gSurface_pressure_bar) | |
267 { | |
268 next_depth = -1; | |
269 buehlmann_backup_and_restore(false); | |
270 decom_tissues_exposure2(10, &pBuDiveSettings->decogaslist[gGas_id], gPressure,gTissue_nitrogen_bar,gTissue_helium_bar); // some seconds at least at each stop | |
271 decom_oxygen_calculate_cns_exposure(10, &pBuDiveSettings->decogaslist[gGas_id], gPressure, &gCNS); | |
272 gDecotable.output_stop_length_seconds[gStop.id] += 10; | |
273 tts_seconds += 10; | |
274 } | |
275 } while(next_depth == -1); | |
253
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
276 tts_seconds += ascend_time; |
38 | 277 gStop.depth = next_depth; |
278 for(i = gGas_id + 1; i < BUEHLMANN_STRUCT_MAX_GASES; i++) | |
279 { | |
280 if(pBuDiveSettings->decogaslist[i].change_during_ascent_depth_meter_otherwise_zero == 0) | |
281 break; | |
282 float pressureChange = ((float)pBuDiveSettings->decogaslist[i].change_during_ascent_depth_meter_otherwise_zero) / 10; | |
283 if(gStop.depth <= pressureChange + 0.00001f) | |
284 { | |
285 gGas_id = i; | |
286 } | |
287 else | |
288 { | |
289 break; | |
290 } | |
291 } | |
292 gStop.id--; | |
293 } | |
294 | |
253
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
295 gDecotable.output_time_to_surface_seconds = tts_seconds; |
254
a17f7fb3b2b5
Buehlmann: cleanup, remove global pDecolistBuehlmann
Jan Mulder <jlmulder@xs4all.nl>
parents:
253
diff
changeset
|
296 memcpy(pDecoInfo, &gDecotable, sizeof(SDecoinfo)); |
38 | 297 } |
298 | |
299 | |
253
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
300 static float tissue_tolerance(void) |
38 | 301 { |
302 float tissue_inertgas_saturation; | |
303 float inertgas_a; | |
304 float inertgas_b; | |
305 float ceiling; | |
306 float global_ceiling; | |
307 int ci; | |
308 | |
309 global_ceiling = -1; | |
310 | |
311 for (ci = 0; ci < 16; ci++) | |
312 { | |
313 if(gTissue_helium_bar[ci] == 0) | |
314 { | |
315 tissue_inertgas_saturation = gTissue_nitrogen_bar[ci]; | |
316 // | |
317 inertgas_a = buehlmann_N2_a[ci]; | |
318 inertgas_b = buehlmann_N2_b[ci]; | |
319 } | |
320 else | |
321 { | |
322 tissue_inertgas_saturation = gTissue_nitrogen_bar[ci] + gTissue_helium_bar[ci]; | |
323 // | |
324 inertgas_a = ( ( buehlmann_N2_a[ci] * gTissue_nitrogen_bar[ci]) + ( buehlmann_He_a[ci] * gTissue_helium_bar[ci]) ) / tissue_inertgas_saturation; | |
325 inertgas_b = ( ( buehlmann_N2_b[ci] * gTissue_nitrogen_bar[ci]) + ( buehlmann_He_b[ci] * gTissue_helium_bar[ci]) ) / tissue_inertgas_saturation; | |
326 } | |
327 // | |
328 ceiling = (inertgas_b * ( tissue_inertgas_saturation - gGF_value * inertgas_a ) ) / (gGF_value - (inertgas_b * gGF_value) + inertgas_b); | |
329 if(ceiling > global_ceiling) | |
330 global_ceiling = ceiling; | |
331 } | |
332 return global_ceiling; | |
333 } | |
334 | |
246
ff0d23625cd5
feature: replace Relative GF by saturation, computational only
Jan Mulder <jlmulder@xs4all.nl>
parents:
224
diff
changeset
|
335 void buehlmann_super_saturation_calculator(SLifeData* pLifeData, SDecoinfo * pDecoInfo) |
38 | 336 { |
337 float tissue_inertgas_saturation; | |
338 float inertgas_a; | |
339 float inertgas_b; | |
340 float ceiling; | |
246
ff0d23625cd5
feature: replace Relative GF by saturation, computational only
Jan Mulder <jlmulder@xs4all.nl>
parents:
224
diff
changeset
|
341 float super_saturation; |
ff0d23625cd5
feature: replace Relative GF by saturation, computational only
Jan Mulder <jlmulder@xs4all.nl>
parents:
224
diff
changeset
|
342 float pres_respiration = pLifeData->pressure_ambient_bar; |
38 | 343 int ci; |
344 | |
246
ff0d23625cd5
feature: replace Relative GF by saturation, computational only
Jan Mulder <jlmulder@xs4all.nl>
parents:
224
diff
changeset
|
345 pDecoInfo->super_saturation = 0; |
38 | 346 |
347 for (ci = 0; ci < 16; ci++) | |
348 { | |
349 if(gTissue_helium_bar[ci] == 0) | |
350 { | |
351 tissue_inertgas_saturation = gTissue_nitrogen_bar[ci]; | |
352 inertgas_a = buehlmann_N2_a[ci]; | |
353 inertgas_b = buehlmann_N2_b[ci]; | |
354 } | |
355 else | |
356 { | |
357 tissue_inertgas_saturation = gTissue_nitrogen_bar[ci] + gTissue_helium_bar[ci]; | |
358 inertgas_a = ( ( buehlmann_N2_a[ci] * gTissue_nitrogen_bar[ci]) + ( buehlmann_He_a[ci] * gTissue_helium_bar[ci]) ) / tissue_inertgas_saturation; | |
359 inertgas_b = ( ( buehlmann_N2_b[ci] * gTissue_nitrogen_bar[ci]) + ( buehlmann_He_b[ci] * gTissue_helium_bar[ci]) ) / tissue_inertgas_saturation; | |
360 } | |
246
ff0d23625cd5
feature: replace Relative GF by saturation, computational only
Jan Mulder <jlmulder@xs4all.nl>
parents:
224
diff
changeset
|
361 |
ff0d23625cd5
feature: replace Relative GF by saturation, computational only
Jan Mulder <jlmulder@xs4all.nl>
parents:
224
diff
changeset
|
362 ceiling = pres_respiration / inertgas_b + inertgas_a; |
ff0d23625cd5
feature: replace Relative GF by saturation, computational only
Jan Mulder <jlmulder@xs4all.nl>
parents:
224
diff
changeset
|
363 if(tissue_inertgas_saturation > pres_respiration) |
38 | 364 { |
246
ff0d23625cd5
feature: replace Relative GF by saturation, computational only
Jan Mulder <jlmulder@xs4all.nl>
parents:
224
diff
changeset
|
365 super_saturation = |
ff0d23625cd5
feature: replace Relative GF by saturation, computational only
Jan Mulder <jlmulder@xs4all.nl>
parents:
224
diff
changeset
|
366 (tissue_inertgas_saturation - pres_respiration) / (ceiling - pres_respiration); |
ff0d23625cd5
feature: replace Relative GF by saturation, computational only
Jan Mulder <jlmulder@xs4all.nl>
parents:
224
diff
changeset
|
367 |
ff0d23625cd5
feature: replace Relative GF by saturation, computational only
Jan Mulder <jlmulder@xs4all.nl>
parents:
224
diff
changeset
|
368 if (super_saturation > pDecoInfo->super_saturation) |
ff0d23625cd5
feature: replace Relative GF by saturation, computational only
Jan Mulder <jlmulder@xs4all.nl>
parents:
224
diff
changeset
|
369 pDecoInfo->super_saturation = super_saturation; |
38 | 370 } |
371 } | |
372 } | |
373 | |
374 | |
253
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
375 static float buehlmann_tissue_test_tolerance(float depth_in_bar_absolute) |
38 | 376 { |
377 float tissue_inertgas_saturation; | |
378 float inertgas_a; | |
379 float inertgas_b; | |
380 float inertgas_tolerance; | |
381 float gf_minus_1; | |
382 | |
383 gf_minus_1 = gGF_value - 1.0f; | |
384 | |
385 for (int ci = 0; ci < 16; ci++) | |
386 { | |
387 if(gTissue_helium_bar[ci] == 0) | |
388 { | |
389 tissue_inertgas_saturation = gTissue_nitrogen_bar[ci]; | |
390 inertgas_a = buehlmann_N2_a[ci]; | |
391 inertgas_b = buehlmann_N2_b[ci]; | |
392 } | |
393 else | |
394 { | |
395 tissue_inertgas_saturation = gTissue_nitrogen_bar[ci] + gTissue_helium_bar[ci]; | |
396 inertgas_a = ( ( buehlmann_N2_a[ci] * gTissue_nitrogen_bar[ci]) + ( buehlmann_He_a[ci] * gTissue_helium_bar[ci]) ) / tissue_inertgas_saturation; | |
397 inertgas_b = ( ( buehlmann_N2_b[ci] * gTissue_nitrogen_bar[ci]) + ( buehlmann_He_b[ci] * gTissue_helium_bar[ci]) ) / tissue_inertgas_saturation; | |
398 } | |
399 inertgas_tolerance = ( (gGF_value / inertgas_b - gf_minus_1) * depth_in_bar_absolute ) + ( gGF_value * inertgas_a ); | |
400 if(inertgas_tolerance < tissue_inertgas_saturation) | |
250
822416168585
Buelmann: new implementation for ceiling
Jan Mulder <jlmulder@xs4all.nl>
parents:
247
diff
changeset
|
401 return tissue_inertgas_saturation - inertgas_tolerance; // positive |
38 | 402 } |
250
822416168585
Buelmann: new implementation for ceiling
Jan Mulder <jlmulder@xs4all.nl>
parents:
247
diff
changeset
|
403 return tissue_inertgas_saturation - inertgas_tolerance; // negative |
38 | 404 } |
405 | |
406 | |
253
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
407 static void ambient_bar_to_deco_stop_depth_bar(float ceiling) |
38 | 408 { |
409 int i; | |
410 | |
411 ceiling -= gSurface_pressure_bar; | |
412 | |
413 if(ceiling <= 0) | |
414 { | |
415 gStop.depth = pBuDiveSettings->last_stop_depth_bar; | |
416 gStop.id = 0; | |
417 return; | |
418 } | |
419 | |
420 if((ceiling - pBuDiveSettings->last_stop_depth_bar) <= 0) | |
421 { | |
422 gStop.depth = pBuDiveSettings->last_stop_depth_bar; | |
423 gStop.id = 0; | |
424 return; | |
425 } | |
426 | |
427 gStop.depth = pBuDiveSettings->input_second_to_last_stop_depth_bar; | |
428 gStop.id = 1; | |
429 ceiling -= pBuDiveSettings->input_second_to_last_stop_depth_bar; | |
430 | |
431 if(ceiling <= 0) | |
432 return; | |
433 | |
434 for(i = 1; i < (DECOINFO_STRUCT_MAX_STOPS - 2); i++) | |
435 { | |
436 ceiling -= pBuDiveSettings->input_next_stop_increment_depth_bar; | |
437 if(ceiling <= 0) | |
438 break; | |
439 } | |
440 gStop.depth += i * pBuDiveSettings->input_next_stop_increment_depth_bar; | |
441 gStop.id += i; | |
442 return; | |
443 } | |
444 | |
253
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
445 static float next_stop_depth_input_is_actual_stop_id(int actual_id) |
38 | 446 { |
447 if(actual_id == 0) | |
448 return 0; | |
449 | |
450 if(actual_id == 1) | |
451 return pBuDiveSettings->last_stop_depth_bar; | |
452 | |
453 actual_id -= 2; | |
454 return pBuDiveSettings->input_second_to_last_stop_depth_bar + (actual_id * pBuDiveSettings->input_next_stop_increment_depth_bar); | |
455 } | |
456 | |
253
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
457 static int ascend_with_all_gaschanges(float pressure_decrease) |
38 | 458 { |
459 float pressureTop, pressureTop_tmp, pressureBottom, pressureChange, ascendrate_in_seconds_for_one_bar, pressure_difference; | |
51
8f8ea3a32e82
Resolved warnings pointing to possible invalid memory access
Ideenmodellierer
parents:
38
diff
changeset
|
460 int time_for_ascend = 0; |
38 | 461 int seconds; |
462 int i; | |
463 | |
464 ascendrate_in_seconds_for_one_bar = 60 * 10 / pBuDiveSettings->ascentRate_meterperminute; | |
465 | |
466 if(fabsf(gPressure - gSurface_pressure_bar) < PRESSURE_HALF_METER) | |
467 { | |
468 gPressure = gSurface_pressure_bar; | |
469 return 0; | |
470 } | |
471 | |
472 pressureTop = gPressure - pressure_decrease; | |
473 if( gSurface_pressure_bar > pressureTop) | |
474 pressureTop = gSurface_pressure_bar; | |
475 pressureBottom = gPressure; | |
476 seconds = 0; | |
477 do{ | |
478 pressureTop_tmp = pressureTop; | |
479 for(i = gGas_id + 1; i < BUEHLMANN_STRUCT_MAX_GASES; i++) | |
480 { | |
481 if(pBuDiveSettings->decogaslist[i].change_during_ascent_depth_meter_otherwise_zero == 0) | |
482 break; | |
483 pressureChange = gSurface_pressure_bar + ((float)pBuDiveSettings->decogaslist[i].change_during_ascent_depth_meter_otherwise_zero) / 10; | |
484 if(pressureBottom <= pressureChange) | |
485 { | |
486 gGas_id = i; | |
487 } | |
488 else | |
489 { | |
490 break; | |
491 } | |
492 | |
493 } | |
494 for(i = gGas_id + 1; i < BUEHLMANN_STRUCT_MAX_GASES; i++) | |
495 { | |
496 if(pBuDiveSettings->decogaslist[i].change_during_ascent_depth_meter_otherwise_zero == 0) | |
497 break; | |
498 pressureChange = gSurface_pressure_bar + ((float)pBuDiveSettings->decogaslist[i].change_during_ascent_depth_meter_otherwise_zero)/ 10; | |
499 if((pressureChange < pressureBottom) && (pressureChange > pressureTop)) | |
500 { | |
501 pressureTop_tmp = pressureChange; | |
502 } | |
503 } | |
504 pressure_difference = pressureBottom - pressureTop_tmp; | |
505 if(pressure_difference > 0.0001f) | |
506 { | |
507 time_for_ascend = (int)ceilf(pressure_difference * ascendrate_in_seconds_for_one_bar); | |
508 decom_tissues_exposure_stage_schreiner(time_for_ascend, &pBuDiveSettings->decogaslist[gGas_id], | |
509 pressureBottom, pressureTop_tmp, gTissue_nitrogen_bar, gTissue_helium_bar); | |
510 decom_oxygen_calculate_cns_stage_SchreinerStyle(time_for_ascend,&pBuDiveSettings->decogaslist[gGas_id], | |
511 pressureBottom, pressureTop_tmp, &gCNS); | |
512 } | |
513 pressureBottom = pressureTop_tmp; | |
514 seconds += time_for_ascend; | |
515 }while(pressureTop_tmp > pressureTop); | |
516 gPressure = pressureTop; | |
517 return seconds; | |
518 } | |
519 | |
520 | |
253
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
521 static float get_gf_at_pressure(float pressure) |
38 | 522 { |
523 float gfSteigung = 0.0f; | |
524 | |
525 if(gGF_low_depth_bar < 0) | |
526 gGF_low_depth_bar = PRESSURE_THREE_METER; // just to prevent erratic behaviour if variable is not set | |
527 | |
528 gfSteigung = ((float)(pBuDiveSettings->gf_high - pBuDiveSettings->gf_low))/ gGF_low_depth_bar; | |
529 | |
530 | |
531 if((pressure - gSurface_pressure_bar) <= PRESSURE_HALF_METER) | |
532 return ((float)pBuDiveSettings->gf_high) / 100.0f; | |
533 | |
534 if(pressure >= gSurface_pressure_bar + gGF_low_depth_bar) | |
535 return ((float)pBuDiveSettings->gf_low) / 100.0f; | |
536 | |
537 return (pBuDiveSettings->gf_high - gfSteigung * (pressure - gSurface_pressure_bar) )/ 100.0f; | |
538 } | |
539 | |
540 | |
253
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
541 static void buehlmann_calc_ndl(void) |
38 | 542 { |
543 float local_tissue_nitrogen_bar[16]; | |
544 float local_tissue_helium_bar[16]; | |
545 int i; | |
546 | |
547 gNDL = 0; | |
548 //Check ndl always use gHigh | |
253
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
549 gGF_value = ((float)pBuDiveSettings->gf_high) / 100.0f; |
38 | 550 //10 minutes steps |
551 while(gNDL < (300 * 60)) | |
552 { | |
553 memcpy(local_tissue_nitrogen_bar, gTissue_nitrogen_bar, (4*16)); | |
554 memcpy(local_tissue_helium_bar, gTissue_helium_bar, (4*16)); | |
555 // | |
556 gNDL += 600; | |
253
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
557 decom_tissues_exposure2(600, &pBuDiveSettings->decogaslist[gGas_id], gPressure,gTissue_nitrogen_bar,gTissue_helium_bar); |
38 | 558 decom_oxygen_calculate_cns_exposure(600,&pBuDiveSettings->decogaslist[gGas_id],gPressure,&gCNS); |
559 //tissues_exposure_at_gPressure_seconds(600); | |
560 buehlmann_backup_and_restore(true); | |
561 if(dive1_check_deco() == true) | |
562 { | |
563 buehlmann_backup_and_restore(false); | |
564 break; | |
565 } | |
566 buehlmann_backup_and_restore(false); | |
567 } | |
568 | |
569 if(gNDL < (300 * 60)) | |
570 gNDL -= 600; | |
571 | |
572 if(gNDL > (150 * 60)) | |
573 return; | |
574 | |
575 // refine | |
576 memcpy(gTissue_nitrogen_bar, local_tissue_nitrogen_bar, (4*16)); | |
577 memcpy(gTissue_helium_bar, local_tissue_helium_bar, (4*16)); | |
578 | |
579 //One minutes step | |
580 for(i = 0; i < 20; i++) | |
581 { | |
582 gNDL += 60; | |
583 //tissues_exposure_at_gPressure_seconds(60); | |
584 decom_tissues_exposure2(60, &pBuDiveSettings->decogaslist[gGas_id], gPressure,gTissue_nitrogen_bar,gTissue_helium_bar); | |
585 decom_oxygen_calculate_cns_exposure(60,&pBuDiveSettings->decogaslist[gGas_id],gPressure,&gCNS); | |
586 buehlmann_backup_and_restore(true); | |
587 if(dive1_check_deco() == true) | |
588 break; | |
589 buehlmann_backup_and_restore(false); | |
590 } | |
591 //gNDL -= 60; | |
592 return; | |
593 } | |
594 | |
595 | |
596 // =============================================================================== | |
597 // dive1_check_deco | |
598 /// @brief for NDL calculations | |
599 /// 160614 using ceilingOther and not ceiling | |
600 // =============================================================================== | |
253
1663b3b204d7
Buehlmann: cleanup, whitespace, static
Jan Mulder <jlmulder@xs4all.nl>
parents:
250
diff
changeset
|
601 static _Bool dive1_check_deco(void) |
38 | 602 { |
603 // gGF_value is set in call routine; | |
604 // internes Backup! | |
605 | |
606 // calc like in deco | |
607 float ceiling; | |
608 float ceilingOther; // new hw 160614 | |
609 | |
610 ceiling = tissue_tolerance(); | |
611 ambient_bar_to_deco_stop_depth_bar(ceiling); // this will set gStop.depth :-) (and gStop.id) | |
612 | |
613 // set the base for all upcoming parameters | |
614 ceilingOther = gStop.depth + gSurface_pressure_bar; | |
615 | |
616 // modify parameters if there is ascend or parameter fine adjustment | |
617 if(ceilingOther < (gPressure - PRESSURE_150_CM)) // more than 1.5 meter below ceiling | |
618 { | |
619 // ascend within 10 mtr to GF_low // speed 12 mtr/min -> 50 sec / 10 mtr; 15 sec / 3 mtr. | |
620 while(((gPressure - PRESSURE_TEN_METER ) > gSurface_pressure_bar) && (ceiling < (gPressure - PRESSURE_TEN_METER))) | |
621 { | |
622 ascend_with_all_gaschanges(PRESSURE_TEN_METER); | |
623 ceiling = tissue_tolerance(); | |
624 } | |
625 while(((gPressure - PRESSURE_THREE_METER )> gSurface_pressure_bar) && (ceiling < gPressure)) | |
626 { | |
627 ascend_with_all_gaschanges(PRESSURE_THREE_METER); | |
628 ceiling = tissue_tolerance(); | |
629 } | |
630 } | |
631 if(ceiling <= gSurface_pressure_bar) | |
632 return false; | |
633 else | |
634 return true; | |
635 } | |
636 | |
250
822416168585
Buelmann: new implementation for ceiling
Jan Mulder <jlmulder@xs4all.nl>
parents:
247
diff
changeset
|
637 // compute ceiling recursively, with a resolution of 10cm. Notice |
822416168585
Buelmann: new implementation for ceiling
Jan Mulder <jlmulder@xs4all.nl>
parents:
247
diff
changeset
|
638 // that the initial call shall guarantee that the found ceiling |
822416168585
Buelmann: new implementation for ceiling
Jan Mulder <jlmulder@xs4all.nl>
parents:
247
diff
changeset
|
639 // is between low and high parameters. |
822416168585
Buelmann: new implementation for ceiling
Jan Mulder <jlmulder@xs4all.nl>
parents:
247
diff
changeset
|
640 static float compute_ceiling(float low, float high) |
38 | 641 { |
250
822416168585
Buelmann: new implementation for ceiling
Jan Mulder <jlmulder@xs4all.nl>
parents:
247
diff
changeset
|
642 if ((high - low) < 0.01) |
822416168585
Buelmann: new implementation for ceiling
Jan Mulder <jlmulder@xs4all.nl>
parents:
247
diff
changeset
|
643 return low; |
822416168585
Buelmann: new implementation for ceiling
Jan Mulder <jlmulder@xs4all.nl>
parents:
247
diff
changeset
|
644 else { |
822416168585
Buelmann: new implementation for ceiling
Jan Mulder <jlmulder@xs4all.nl>
parents:
247
diff
changeset
|
645 float next_pressure_absolute = (low + high)/2; |
822416168585
Buelmann: new implementation for ceiling
Jan Mulder <jlmulder@xs4all.nl>
parents:
247
diff
changeset
|
646 float test_result = buehlmann_tissue_test_tolerance(next_pressure_absolute); |
822416168585
Buelmann: new implementation for ceiling
Jan Mulder <jlmulder@xs4all.nl>
parents:
247
diff
changeset
|
647 if (test_result < 0) |
822416168585
Buelmann: new implementation for ceiling
Jan Mulder <jlmulder@xs4all.nl>
parents:
247
diff
changeset
|
648 return compute_ceiling(low, next_pressure_absolute); |
822416168585
Buelmann: new implementation for ceiling
Jan Mulder <jlmulder@xs4all.nl>
parents:
247
diff
changeset
|
649 else |
822416168585
Buelmann: new implementation for ceiling
Jan Mulder <jlmulder@xs4all.nl>
parents:
247
diff
changeset
|
650 return compute_ceiling(next_pressure_absolute, high); |
822416168585
Buelmann: new implementation for ceiling
Jan Mulder <jlmulder@xs4all.nl>
parents:
247
diff
changeset
|
651 } |
822416168585
Buelmann: new implementation for ceiling
Jan Mulder <jlmulder@xs4all.nl>
parents:
247
diff
changeset
|
652 } |
38 | 653 |
250
822416168585
Buelmann: new implementation for ceiling
Jan Mulder <jlmulder@xs4all.nl>
parents:
247
diff
changeset
|
654 void buehlmann_ceiling_calculator(SLifeData *pLifeData, SDecoinfo *pDecoInfo) |
822416168585
Buelmann: new implementation for ceiling
Jan Mulder <jlmulder@xs4all.nl>
parents:
247
diff
changeset
|
655 { |
822416168585
Buelmann: new implementation for ceiling
Jan Mulder <jlmulder@xs4all.nl>
parents:
247
diff
changeset
|
656 float ceiling; |
224
ceecabfddb57
Bugfix, deco: fix 2 (small) problems with calculated ceiling
Jan Mulder <jlmulder@xs4all.nl>
parents:
130
diff
changeset
|
657 |
250
822416168585
Buelmann: new implementation for ceiling
Jan Mulder <jlmulder@xs4all.nl>
parents:
247
diff
changeset
|
658 // this is just performance optimizing. The code below runs just fine |
822416168585
Buelmann: new implementation for ceiling
Jan Mulder <jlmulder@xs4all.nl>
parents:
247
diff
changeset
|
659 // without this. There is never a ceiling in NDL deco state |
822416168585
Buelmann: new implementation for ceiling
Jan Mulder <jlmulder@xs4all.nl>
parents:
247
diff
changeset
|
660 if (!pDecoInfo->output_time_to_surface_seconds) { |
822416168585
Buelmann: new implementation for ceiling
Jan Mulder <jlmulder@xs4all.nl>
parents:
247
diff
changeset
|
661 pDecoInfo->output_ceiling_meter = 0; |
822416168585
Buelmann: new implementation for ceiling
Jan Mulder <jlmulder@xs4all.nl>
parents:
247
diff
changeset
|
662 return; |
38 | 663 } |
664 | |
665 memcpy(gTissue_nitrogen_bar, pLifeData->tissue_nitrogen_bar, (4*16)); | |
666 memcpy(gTissue_helium_bar, pLifeData->tissue_helium_bar, (4*16)); | |
224
ceecabfddb57
Bugfix, deco: fix 2 (small) problems with calculated ceiling
Jan Mulder <jlmulder@xs4all.nl>
parents:
130
diff
changeset
|
667 |
250
822416168585
Buelmann: new implementation for ceiling
Jan Mulder <jlmulder@xs4all.nl>
parents:
247
diff
changeset
|
668 ceiling = compute_ceiling(pLifeData->pressure_surface_bar, 1.0f + pLifeData->max_depth_meter/10.0f); |
822416168585
Buelmann: new implementation for ceiling
Jan Mulder <jlmulder@xs4all.nl>
parents:
247
diff
changeset
|
669 pDecoInfo->output_ceiling_meter = (ceiling - pLifeData->pressure_surface_bar) * 10.0f; |
38 | 670 } |