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