11<script context =" module" lang =" ts" >
22 import { isaDeviation } from " $lib/calculations/atmosphere/isa-atmosphere.svelte" ;
3- import { subtract , parse , Unit , unit , add , compare , compareNatural , divide , abs } from ' mathjs'
3+ import { subtract , parse , Unit , unit , add , compare , compareNatural , divide , abs , multiply } from ' mathjs'
4+
5+ // Iteration method options
6+ const iteration_maxIter = 50 ;
7+ const iteration_tolerance = unit (0.000001 , " ft" );
48
59 // Constants and equations used in the calculation
610 const lapse_rate = unit (- 0.0019812 ," K/ft" );
3236 let aerodrome_isa_deviation_degC = isaDeviation (aerodrome_elevation , aerodrome_ground_temperature );
3337
3438 console .debug (" Calculating altitude correction using iterative method." );
35- console .debug (" Equation to solve: 0 == %s" , correction_function .toString ());
36- console .debug (" Equation to solve: 0 == %s" , correction_function .toTex ());
37- console .debug (" Derivative of the right hand side equation to solve: %s" , correction_function_derivative .toString ());
38- console .debug (" Derivative of the right hand side equation to solve: %s" , correction_function_derivative .toTex ());
39-
40- let hPAirplane: Unit = input_height ;
41- let hPAirplaneNew: Unit ;
42-
43- const maxIter = 50 ;
44- const tolerance = unit (0.0001 , " ft" );
45-
46- let iter = 0 ;
4739
48- while (iter ++ < maxIter ) {
49- const calculation_parameters = {
40+ const calculation_parameters = {
5041 DeltaTstd: aerodrome_isa_deviation_degC ,
5142 L0: lapse_rate ,
5243 T0: sea_level_temperature ,
53- hPAirplane: hPAirplane ,
5444 hGAirplane: input_height ,
5545 hPAerodrome: aerodrome_elevation
5646 };
5747
48+ let initial_value = input_height ;
49+ const hPAirplane = findIterativeSolution (calculation_parameters , initial_value );
50+
51+ // start an additional iterative solution search from 0 ft to cross check for convergence
52+ const hPAirplaneSecondCheck = findIterativeSolution (calculation_parameters , unit (0 , " ft" ));
53+ if (compareNatural (abs (subtract (hPAirplane , hPAirplaneSecondCheck )), multiply (iteration_tolerance , 100 )) > 0 ) {
54+ throw new Error (" The convergence crosscheck has failed between the iterative solution starting from the input height and from 0ft!" );
55+ }
56+
57+ console .debug (" Resulting correction to be added to the elevation: hPAirplane %s" , hPAirplane );
58+
59+ return add (aerodrome_elevation , hPAirplane );
60+ }
61+
62+ function findIterativeSolution(calculation_parameters : any , hPAirplane : Unit ): Unit {
63+ console .debug (" Starting to search for iterative solution" );
64+ console .debug (" Equation to solve: 0 == %s" , correction_function .toString ());
65+ console .debug (" Derivative of the right hand side equation to solve: %s" , correction_function_derivative .toString ());
66+
67+ let hPAirplaneNew: Unit ;
68+
69+ let iter = 0 ;
70+ while (iter ++ < iteration_maxIter ) {
71+ calculation_parameters [" hPAirplane" ] = hPAirplane ;
72+
5873 console .debug (" Performing the next step in the ESDU based iterative altitude temperature " +
5974 " correction calculation" );
6075 console .debug (" Using the calculation parameters:" );
6378 const value = correction_function .evaluate (calculation_parameters ).to (" ft" );
6479 const derivative_value = correction_function_derivative .evaluate (calculation_parameters );
6580
81+ if (abs (derivative_value ) < Number .EPSILON) {
82+ throw new Error (" Iterative height correction solution didn't converge since the derivative value was too small!" );
83+ }
84+
6685 hPAirplaneNew = <Unit >subtract (hPAirplane , divide (value , derivative_value ));
6786 console .debug (" New function value: %s new derivative value: %s and new guess: %s" ,value , derivative_value , hPAirplaneNew );
6887
69- hPAirplane = hPAirplaneNew ;
70-
71- if (compareNatural (abs ( subtract ( hPAirplane , hPAirplaneNew )), tolerance ) < 0 ) {
72- console .debug (" Found new iterative solution in %d iterations" , iter );
73- break ;
88+ // Check if we reached the convergance level
89+ const stepDelta = abs ( subtract ( hPAirplane , hPAirplaneNew ));
90+ if (compareNatural (stepDelta , iteration_tolerance ) < 0 ) {
91+ console .debug (" Found new iterative solution in %d iterations, difference: %s " , iter , stepDelta );
92+ return hPAirplaneNew ;
7493 }
75- }
7694
77- console .debug (" Resulting corrected airplane pressure height %s" , hPAirplane .toString ());
95+ hPAirplane = hPAirplaneNew ;
96+ }
7897
79- return add ( aerodrome_elevation , hPAirplane );
98+ throw new Error ( " Iterative solution search for altitude correction didn't converge! " );
8099 }
81- </script >
100+ </script >
0 commit comments