comparison code_part1/OSTC_code_c_part2/p2_deco.c @ 334:4ccdc72ec0e5

BUGFIX minor error in gas-switch algo (spurious surface decostop rarely added).
author JeanDo
date Thu, 12 May 2011 13:59:13 +0200
parents a7ed48daf597
children b75564fb3d4b
comparison
equal deleted inserted replaced
333:8b347e0eae26 334:4ccdc72ec0e5
74 // 2011/03/21: [jDG] Added gas consumption (CF56 & CF57) evaluation for OCR mode. 74 // 2011/03/21: [jDG] Added gas consumption (CF56 & CF57) evaluation for OCR mode.
75 // 2011/04/10: [jDG] Use timer TMR3 to limit loops in calc_hauptroutine_calc_deco(). 75 // 2011/04/10: [jDG] Use timer TMR3 to limit loops in calc_hauptroutine_calc_deco().
76 // 2011/04/15: [jDG] Store low_depth in 32bits (w/o rounding), for a better stability. 76 // 2011/04/15: [jDG] Store low_depth in 32bits (w/o rounding), for a better stability.
77 // 2011/04/25: [jDG] Added 1mn mode for CNS calculation, to allow it for decoplanning. 77 // 2011/04/25: [jDG] Added 1mn mode for CNS calculation, to allow it for decoplanning.
78 // 2011/04/27: [jDG] Fixed char_O_gradient_factor calculation when model uses gradient-factor. 78 // 2011/04/27: [jDG] Fixed char_O_gradient_factor calculation when model uses gradient-factor.
79 // 2011/05/02: [jDG] Added @+5min variant. 79 // 2011/05/02: [jDG] Added "Future TTS" function (CF58).
80 // 80 //
81 // TODO: 81 // TODO:
82 // + Allow to abort MD2 calculation (have to restart next time). 82 // + Allow to abort MD2 calculation (have to restart next time).
83 // 83 //
84 // Literature: 84 // Literature:
977 977
978 // If there is no gas-switch-delay running ? 978 // If there is no gas-switch-delay running ?
979 if( sim_gas_delay <= sim_dive_mins) 979 if( sim_gas_delay <= sim_dive_mins)
980 { 980 {
981 // Compute current depth: 981 // Compute current depth:
982 overlay unsigned char depth = (unsigned char)((pres_respiration - pres_surface) * BAR_TO_METER); 982 overlay unsigned char depth = (unsigned char)(0.5 + (pres_respiration - pres_surface) * BAR_TO_METER);
983 assert( depth < 130 ); 983 assert( depth < 130 );
984 984
985 // And if I'm above the last decostop (with the 3m margin) ? 985 // And if I'm above the last decostop (with the 3m margin) ?
986 if( (sim_gas_last_depth-3) > depth ) 986 if( (sim_gas_last_depth-3) > depth )
987 { 987 {
1460 ////////////////////////////////////////////////////////////////////////////// 1460 //////////////////////////////////////////////////////////////////////////////
1461 // Compute stops. 1461 // Compute stops.
1462 // 1462 //
1463 // Note: because this can be very long, break on 16 iterations, and set state 1463 // Note: because this can be very long, break on 16 iterations, and set state
1464 // to 0 when finished, or to 1 when needing to continue. 1464 // to 0 when finished, or to 1 when needing to continue.
1465 // Note: because this might be very long (~ 66 ms by iteration in 1.84beta), 1465 // Note: because each iteration might be very long too (~ 66 ms in 1.84beta),
1466 // break the loop when total time > 512msec. 1466 // break the loop when total time > 512msec.
1467 // 1467 //
1468 void calc_hauptroutine_calc_deco(void) 1468 void calc_hauptroutine_calc_deco(void)
1469 { 1469 {
1470 overlay unsigned char loop; 1470 overlay unsigned char loop;
1506 return; 1506 return;
1507 } 1507 }
1508 } 1508 }
1509 } 1509 }
1510 else 1510 else
1511 {
1512 // Note: if loop==0, temp_depth_limit might not be already set here.
1513 temp_depth_limit = (int)(0.5 + (temp_deco - pres_surface) * BAR_TO_METER);
1511 update_deco_table(); // Just pass one minute. 1514 update_deco_table(); // Just pass one minute.
1515 }
1512 1516
1513 //---- Then update tissue -------------------------------------------- 1517 //---- Then update tissue --------------------------------------------
1514 sim_dive_mins++; // Advance simulated time by 1 minute. 1518 sim_dive_mins++; // Advance simulated time by 1 minute.
1515 gas_switch_set(); // Apply any simulated gas change, once validated. 1519 gas_switch_set(); // Apply any simulated gas change, once validated.
1516 sim_alveolar_presures(); // Updates ppN2 and ppHe. 1520 sim_alveolar_presures(); // Updates ppN2 and ppHe.
1526 // 1530 //
1527 // Note: because we ascent with a constant speed (10m/mn, ie. 1bar/mn), 1531 // Note: because we ascent with a constant speed (10m/mn, ie. 1bar/mn),
1528 // there is no need to break on more that 16 iterations 1532 // there is no need to break on more that 16 iterations
1529 // (or we are already in deep shit). 1533 // (or we are already in deep shit).
1530 // 1534 //
1535 // Input: pres_respiration
1536 // Output: temp_deco
1537 //
1531 // if char_O_deco_status indicate @+5 variant, add extra time at current depth, 1538 // if char_O_deco_status indicate @+5 variant, add extra time at current depth,
1532 // before ascent. 1539 // before ascent.
1533 void sim_ascent_to_first_stop(void) 1540 void sim_ascent_to_first_stop(void)
1534 { 1541 {
1535 update_startvalues(); 1542 update_startvalues();
1567 temp_deco = pres_surface; // Yes: finished ! 1574 temp_deco = pres_surface; // Yes: finished !
1568 break; 1575 break;
1569 } 1576 }
1570 1577
1571 // Check for gas change below new depth ? 1578 // Check for gas change below new depth ?
1572 temp_depth_limit = (temp_deco - pres_surface) * BAR_TO_METER; 1579 temp_depth_limit = (int)(0.5 + (temp_deco - pres_surface) * BAR_TO_METER);
1580 assert( temp_depth_limit > 0);
1573 1581
1574 if( gas_switch_deepest() ) 1582 if( gas_switch_deepest() )
1575 { 1583 {
1584 assert( temp_depth_limit > 0);
1585
1576 temp_deco = temp_depth_limit * METER_TO_BAR + pres_surface; 1586 temp_deco = temp_depth_limit * METER_TO_BAR + pres_surface;
1577 break; 1587 break;
1578 } 1588 }
1579 1589
1580 sim_dive_mins++; // Advance simulated time by 1 minute. 1590 sim_dive_mins++; // Advance simulated time by 1 minute.
1892 assert( temp_depth_limit > 0 ); // No stop at surface... 1902 assert( temp_depth_limit > 0 ); // No stop at surface...
1893 1903
1894 for(x=0; x<32; ++x) 1904 for(x=0; x<32; ++x)
1895 { 1905 {
1896 // Make sure deco-stops are recorded in order: 1906 // Make sure deco-stops are recorded in order:
1897 assert( !internal_deco_depth[x] || temp_depth_limit <= internal_deco_depth[x] ); 1907 assert( !internal_deco_depth[x] || temp_depth_limit <= (internal_deco_depth[x]& 0x7F) );
1898 1908
1899 if( (internal_deco_depth[x] & 0x7F) == temp_depth_limit ) 1909 if( (internal_deco_depth[x] & 0x7F) == temp_depth_limit )
1900 { 1910 {
1901 // Do not overflow (max 255') 1911 // Do not overflow (max 255')
1902 if( internal_deco_time[x] < 255 ) 1912 if( internal_deco_time[x] < 255 )
2079 else 2089 else
2080 temp3 = - temp3 / (pres_tissue+16)[ci]; 2090 temp3 = - temp3 / (pres_tissue+16)[ci];
2081 if( 0.0 < temp3 && temp3 < 1.0 ) 2091 if( 0.0 < temp3 && temp3 < 1.0 )
2082 { 2092 {
2083 overlay float var_He_halftime = (buhlmann_ht+16)[ci]; 2093 overlay float var_He_halftime = (buhlmann_ht+16)[ci];
2084 assert( 1.51 <= var_He_halftime && var_He_halftime <= 240.03 ); 2094 assert( 1.5099 <= var_He_halftime && var_He_halftime <= 240.03 );
2085 2095
2086 temp3 = log(1.0 - temp3) / -0.6931; // temp1 is the multiples of half times necessary. 2096 temp3 = log(1.0 - temp3) / -0.6931; // temp1 is the multiples of half times necessary.
2087 // 0.6931 is ln(2), because the math function log() calculates with a base of e not 2 as requested. 2097 // 0.6931 is ln(2), because the math function log() calculates with a base of e not 2 as requested.
2088 // minus because log is negative 2098 // minus because log is negative
2089 temp4 = var_He_halftime * temp3 / float_desaturation_multiplier; // time necessary (in minutes ) for "complete" desaturation, new in v.101 float_desaturation_multiplier 2099 temp4 = var_He_halftime * temp3 / float_desaturation_multiplier; // time necessary (in minutes ) for "complete" desaturation, new in v.101 float_desaturation_multiplier