1 /***************************************************************************
5 ----------------------------------------------------------------------------
7 FUNCTION: Trims the simulated aircraft by using certain
8 controls to null out a similar number of outputs.
10 This routine used modified Newton-Raphson method to find the vector
11 of control corrections, delta_U, to drive a similar-sized vector of
12 output errors, Y, to near-zero. Nearness to zero is to within a
13 tolerance specified by the Criteria vector. An optional Weight
14 vector can be used to improve the numerical properties of the
15 Jacobian matrix (called H_Partials).
17 Using a single-sided difference method, each control is
18 independently perturbed and the change in each output of
19 interest is calculated, forming a Jacobian matrix H (variable
25 The columns of H correspond to the control effect; the rows of
26 H correspond to the outputs affected.
28 We wish to find dU such that for U = U0 + dU,
33 One solution is dU = inv(H)*(-Y0); however, inverting H
34 directly is not numerically sound, since it may be singular
35 (especially if one of the controls is on a limit, or the
36 problem is poorly posed). An alternative is to either weight
37 the elements of dU to make them more normalized; another is to
38 multiply both sides by the transpose of H and invert the
39 resulting [H' H]. This routine does both:
42 W (-Y0) = W H dU premultiply by W
43 H' W (-Y0) = H' W H dU premultiply by H'
45 dU = [inv(H' W H)][ H' W (-Y0)] Solve for dU
47 As a further refinement, dU is limited to a smallish magnitude
48 so that Y approaches 0 in small steps (to avoid an overshoot
49 if the problem is inherently non-linear).
51 Lastly, this routine can be easily fooled by "local minima",
52 or depressions in the solution space that don't lead to a Y =
53 0 solution. The only advice we can offer is to "go somewheres
54 else and try again"; often approaching a trim solution from a
55 different (non-trimmed) starting point will prove beneficial.
58 ----------------------------------------------------------------------------
60 MODULE STATUS: developmental
62 ----------------------------------------------------------------------------
64 GENEALOGY: Created from old CASTLE SHELL$TRIM.PAS
65 on 6 FEB 95, which was based upon an Ames
66 CASPRE routine called BQUIET.
68 ----------------------------------------------------------------------------
70 DESIGNED BY: E. B. Jackson
76 ----------------------------------------------------------------------------
82 950307 Modified to make use of ls_get_sym_val and ls_put_sym_val
84 950329 Fixed bug in making use of more than 3 controls;
85 removed call by ls_trim_get_set() to ls_trim_init(). EBJ
91 Revision 1.1 1999/04/05 21:32:45 curt
94 * Revision 1.9 1995/03/29 16:09:56 bjax
95 * Fixed bug in having more than three trim controls; removed unnecessary
96 * call to ls_trim_init in ls_trim_get_set. EBJ
98 * Revision 1.8 1995/03/16 12:28:40 bjax
99 * Fixed problem where ls_trim() returns non-zero if
100 * symbols are not loaded - implies vehicle trimmed when
101 * actually no trim attempt is made. This results in storing of non-
102 * trimmed initial conditions in sims without defined trim controls.
104 * Revision 1.7 1995/03/15 12:17:12 bjax
105 * Added flag marker line to ls_trim_put_set() routine output.
107 * Revision 1.6 1995/03/08 11:49:07 bjax
108 * Minor improvements to ls_trim_get_set; deleted weighting parameter
109 * for output definition; added comment lines to settings file output.
111 * Revision 1.5 1995/03/07 22:38:04 bjax
112 * Removed ls_generic.h; this version relies entirely on symbol table routines to
113 * set and get variable values. Added additional fields to Control record structure;
114 * created Output record with appropriate fields. Added ls_trim_put_set() and
115 * ls_trim_get_set() routines. Heavily modified initialization routine; most of this
116 * logic now resides in ls_trim_get_set(). Renamed all routines so that they being
117 * with "ls_trim_" to avoid conflicts.
120 * Revision 1.4 1995/03/07 13:04:16 bjax
121 * Configured to use ls_get_sym_val() and ls_set_sym_val().
123 * Revision 1.3 1995/03/03 01:59:53 bjax
124 * Moved definition of SYMBOL_NAME and SYMBOL_TYPE to ls_sym.h
125 * and removed from this module. EBJ
127 * Revision 1.2 1995/02/27 19:53:41 bjax
128 * Moved symbol routines to ls_sym.c to declutter this file. EBJ
130 * Revision 1.1 1995/02/27 18:14:10 bjax
134 ----------------------------------------------------------------------------
138 ----------------------------------------------------------------------------
142 ----------------------------------------------------------------------------
146 ----------------------------------------------------------------------------
150 ----------------------------------------------------------------------------
154 --------------------------------------------------------------------------*/
156 static char rcsid[] = "$Id$";
163 #include "ls_constants.h"
164 #include "ls_types.h"
166 #include "ls_matrix.h"
167 #include "ls_interface.h"
175 #define MAX_NUMBER_OF_CONTROLS 10
176 #define MAX_NUMBER_OF_OUTPUTS 10
177 #define STEP_LIMIT 0.01
178 #define NIL_POINTER 0L
180 #define FACILITY_NAME_STRING "trim"
181 #define CURRENT_VERSION 10
187 double Min_Val, Max_Val, Curr_Val, Authority;
188 double Percent, Requested_Percent, Pert_Size;
189 int Ineffective, At_Limit;
195 double Curr_Val, Weighting, Trim_Criteria;
200 static int Symbols_loaded = 0;
202 static int Trim_Cycles;
207 static int Number_of_Controls;
208 static int Number_of_Outputs;
209 static control_rec Controls[ MAX_NUMBER_OF_CONTROLS ];
210 static output_rec Outputs[ MAX_NUMBER_OF_OUTPUTS ];
212 static double **H_Partials;
214 static double Baseline_Output[ MAX_NUMBER_OF_OUTPUTS ];
215 static double Saved_Control, Saved_Control_Percent;
217 static double Cost, Previous_Cost;
223 /* Initialize partials matrix */
235 for (i=0;i<Number_of_Controls;i++)
237 Controls[i].Curr_Val = ls_get_sym_val( &Controls[i].Symbol, &error );
238 if (error) Controls[i].Symbol.Addr = NIL_POINTER;
239 Controls[i].Requested_Percent =
240 (Controls[i].Curr_Val - Controls[i].Min_Val)
241 /Controls[i].Authority;
244 H_Partials = nr_matrix( 1, Number_of_Controls, 1, Number_of_Controls );
245 if (H_Partials == 0) return -1;
250 void ls_trim_get_vals()
251 /* Load the Output vector, and calculate control percent used */
255 for (i=0;i<Number_of_Outputs;i++)
257 Outputs[i].Curr_Val = ls_get_sym_val( &Outputs[i].Symbol, &error );
258 if (error) Outputs[i].Symbol.Addr = NIL_POINTER;
262 for (i=0;i<Number_of_Controls;i++)
264 Controls[i].Curr_Val = ls_get_sym_val( &Controls[i].Symbol, &error );
265 if (error) Controls[i].Symbol.Addr = NIL_POINTER;
266 Controls[i].Percent =
267 (Controls[i].Curr_Val - Controls[i].Min_Val)
268 /Controls[i].Authority;
274 void ls_trim_move_controls()
275 /* This routine moves the current control to specified percent of authority */
279 for(i=0;i<Number_of_Controls;i++)
281 Controls[i].At_Limit = 0;
282 if (Controls[i].Requested_Percent <= 0.0)
284 Controls[i].Requested_Percent = 0.0;
285 Controls[i].At_Limit = 1;
287 if (Controls[i].Requested_Percent >= 1.0)
289 Controls[i].Requested_Percent = 1.0;
290 Controls[i].At_Limit = 1;
292 Controls[i].Curr_Val = Controls[i].Min_Val +
293 (Controls[i].Max_Val - Controls[i].Min_Val) *
294 Controls[i].Requested_Percent;
298 void ls_trim_put_controls()
299 /* Put current control requests out to controls themselves */
303 for (i=0;i<Number_of_Controls;i++)
304 if (Controls[i].Symbol.Addr)
305 ls_set_sym_val( &Controls[i].Symbol, Controls[i].Curr_Val );
308 void ls_trim_calc_cost()
309 /* This routine calculates the current distance, or cost, from trim */
314 for(i=0;i<Number_of_Outputs;i++)
315 Cost += pow((Outputs[i].Curr_Val/Outputs[i].Trim_Criteria),2.0);
318 void ls_trim_save_baseline_outputs()
322 for (i=0;i<Number_of_Outputs;i++)
323 Baseline_Output[i] = ls_get_sym_val( &Outputs[i].Symbol, &error );
326 int ls_trim_eval_outputs()
331 for(i=0;i<Number_of_Outputs;i++)
332 if( fabs(Outputs[i].Curr_Val) > Outputs[i].Trim_Criteria) trimmed = 0;
336 void ls_trim_calc_h_column()
339 double delta_control, delta_output;
341 delta_control = (Controls[Index].Curr_Val - Saved_Control)/Controls[Index].Authority;
343 for(i=0;i<Number_of_Outputs;i++)
345 delta_output = Outputs[i].Curr_Val - Baseline_Output[i];
346 H_Partials[i+1][Index+1] = delta_output/delta_control;
350 void ls_trim_do_step()
352 int i, j, l, singular;
353 double **h_trans_w_h;
354 double delta_req_mag, scaling;
355 double delta_U_requested[ MAX_NUMBER_OF_CONTROLS ];
356 double temp[ MAX_NUMBER_OF_CONTROLS ];
358 /* Identify ineffective controls (whose partials are all near zero) */
360 for (j=0;j<Number_of_Controls;j++)
362 Controls[j].Ineffective = 1;
363 for(i=0;i<Number_of_Outputs;i++)
364 if (fabs(H_Partials[i+1][j+1]) > EPS) Controls[j].Ineffective = 0;
367 /* Identify uncontrollable outputs */
369 for (j=0;j<Number_of_Outputs;j++)
371 Outputs[j].Uncontrollable = 1;
372 for(i=0;i<Number_of_Controls;i++)
373 if (fabs(H_Partials[j+1][i+1]) > EPS) Outputs[j].Uncontrollable = 0;
376 /* Calculate well-conditioned partials matrix [ H' W H ] */
378 h_trans_w_h = nr_matrix(1, Number_of_Controls, 1, Number_of_Controls);
379 if (h_trans_w_h == 0)
381 fprintf(stderr, "Memory error in ls_trim().\n");
384 for (l=1;l<=Number_of_Controls;l++)
385 for (j=1;j<=Number_of_Controls;j++)
387 h_trans_w_h[l][j] = 0.0;
388 for (i=1;i<=Number_of_Outputs;i++)
390 H_Partials[i][l]*H_Partials[i][j]*Outputs[i-1].Weighting;
393 /* Invert the partials array [ inv( H' W H ) ]; note: h_trans_w_h is replaced
394 with its inverse during this function call */
396 singular = nr_gaussj( h_trans_w_h, Number_of_Controls, 0, 0 );
398 if (singular) /* Can't invert successfully */
400 nr_free_matrix( h_trans_w_h, 1, Number_of_Controls,
401 1, Number_of_Controls );
402 fprintf(stderr, "Singular matrix in ls_trim().\n");
406 /* Form right hand side of equality: temp = [ H' W (-Y) ] */
408 for(i=0;i<Number_of_Controls;i++)
411 for(j=0;j<Number_of_Outputs;j++)
412 temp[i] -= H_Partials[j+1][i+1]*Baseline_Output[j]*Outputs[j].Weighting;
415 /* Solve for dU = [inv( H' W H )][ H' W (-Y)] */
416 for(i=0;i<Number_of_Controls;i++)
418 delta_U_requested[i] = 0.0;
419 for(j=0;j<Number_of_Controls;j++)
420 delta_U_requested[i] += h_trans_w_h[i+1][j+1]*temp[j];
423 /* limit step magnitude to certain size, but not direction */
426 for(i=0;i<Number_of_Controls;i++)
427 delta_req_mag += delta_U_requested[i]*delta_U_requested[i];
428 delta_req_mag = sqrt(delta_req_mag);
429 scaling = STEP_LIMIT/delta_req_mag;
431 for(i=0;i<Number_of_Controls;i++)
432 delta_U_requested[i] *= scaling;
434 /* Convert deltas to percent of authority */
436 for(i=0;i<Number_of_Controls;i++)
437 Controls[i].Requested_Percent = Controls[i].Percent + delta_U_requested[i];
439 /* free up temporary matrix */
441 nr_free_matrix( h_trans_w_h, 1, Number_of_Controls,
442 1, Number_of_Controls );
450 const int Max_Cycles = 100;
454 if (Symbols_loaded) {
456 ls_trim_init(); /* Initialize Outputs & controls */
457 ls_trim_get_vals(); /* Limit the current control settings */
459 ls_trim_move_controls(); /* Write out the new values of controls */
460 ls_trim_put_controls();
461 ls_loop( 0.0, -1 ); /* Cycle the simulation once with new limited
464 /* Main trim cycle loop follows */
466 while((!Trimmed) && (Trim_Cycles < Max_Cycles))
473 ls_trim_save_baseline_outputs();
474 Trimmed = ls_trim_eval_outputs();
478 ls_trim_calc_h_column();
479 Controls[Index].Curr_Val = Saved_Control;
480 Controls[Index].Percent = Saved_Control_Percent;
481 Controls[Index].Requested_Percent = Saved_Control_Percent;
486 if (Index >= Number_of_Controls)
493 { /* Save the current value & pert next control */
495 Saved_Control = Controls[Index].Curr_Val;
496 Saved_Control_Percent = Controls[Index].Percent;
498 if (Controls[Index].Percent <
499 (1.0 - Controls[Index].Pert_Size) )
501 Controls[Index].Requested_Percent =
502 Controls[Index].Percent +
503 Controls[Index].Pert_Size ;
507 Controls[Index].Requested_Percent =
508 Controls[Index].Percent -
509 Controls[Index].Pert_Size;
512 ls_trim_move_controls();
513 ls_trim_put_controls();
519 nr_free_matrix( H_Partials, 1, Number_of_Controls, 1, Number_of_Controls );
522 if (!Trimmed) fprintf(stderr, "Trim unsuccessful.\n");
528 char *ls_trim_get_set(char *buffer, char *eob)
529 /* This routine parses the settings file for "trim" entries. */
532 static char *fac_name = FACILITY_NAME_STRING;
533 char *bufptr, **lasts, *nullptr, null = '\0';
535 int n, ver, i, error, abrt;
536 enum {controls_header, controls, outputs_header, outputs, done} looking_for;
541 looking_for = controls_header;
544 n = sscanf(buffer, "%s", line);
545 if (n == 0) return 0L;
546 if (strncasecmp( fac_name, line, strlen(fac_name) )) return 0L;
548 bufptr = strtok_r( buffer+strlen(fac_name)+1, "\n", lasts);
549 if (bufptr == 0) return 0L;
551 sscanf( bufptr, "%d", &ver );
552 if (ver != CURRENT_VERSION) return 0L;
554 while( !abrt && (eob > bufptr))
556 bufptr = strtok_r( 0L, "\n", lasts );
557 if (bufptr == 0) return 0L;
558 if (strncasecmp( bufptr, "end", 3) == 0) break;
560 sscanf( bufptr, "%s", line );
561 if (line[0] != '#') /* ignore comments */
565 case controls_header:
567 if (strncasecmp( line, "controls", 8) == 0)
569 n = sscanf( bufptr, "%s%d", line, &Number_of_Controls );
570 if (n != 2) abrt = 1;
571 looking_for = controls;
578 n = sscanf( bufptr, "%s%s%le%le%le",
579 Controls[i].Symbol.Mod_Name,
580 Controls[i].Symbol.Par_Name,
581 &Controls[i].Min_Val,
582 &Controls[i].Max_Val,
583 &Controls[i].Pert_Size);
584 if (n != 5) abrt = 1;
585 Controls[i].Symbol.Addr = NIL_POINTER;
587 if (i >= Number_of_Controls) looking_for = outputs_header;
592 if (strncasecmp( line, "outputs", 7) == 0)
594 n = sscanf( bufptr, "%s%d", line, &Number_of_Outputs );
595 if (n != 2) abrt = 1;
596 looking_for = outputs;
603 n = sscanf( bufptr, "%s%s%le",
604 Outputs[i].Symbol.Mod_Name,
605 Outputs[i].Symbol.Par_Name,
606 &Outputs[i].Trim_Criteria );
607 if (n != 3) abrt = 1;
608 Outputs[i].Symbol.Addr = NIL_POINTER;
610 if (i >= Number_of_Outputs) looking_for = done;
622 (Number_of_Controls > 0) &&
623 (Number_of_Outputs == Number_of_Controls))
627 for(i=0;i<Number_of_Controls;i++) /* Initialize fields in Controls data */
629 Controls[i].Curr_Val = ls_get_sym_val( &Controls[i].Symbol, &error );
631 Controls[i].Symbol.Addr = NIL_POINTER;
632 Controls[i].Authority = Controls[i].Max_Val - Controls[i].Min_Val;
633 if (Controls[i].Authority == 0.0)
634 Controls[i].Authority = 1.0;
635 Controls[i].Requested_Percent =
636 (Controls[i].Curr_Val - Controls[i].Min_Val)
637 /Controls[i].Authority;
638 Controls[i].Pert_Size = Controls[i].Pert_Size/Controls[i].Authority;
641 for (i=0;i<Number_of_Outputs;i++) /* Initialize fields in Outputs data */
643 Outputs[i].Curr_Val = ls_get_sym_val( &Outputs[i].Symbol, &error );
644 if (error) Outputs[i].Symbol.Addr = NIL_POINTER;
645 Outputs[i].Weighting =
646 Outputs[0].Trim_Criteria/Outputs[i].Trim_Criteria;
656 void ls_trim_put_set( FILE *fp )
662 fprintf(fp, "#============================== %s\n", FACILITY_NAME_STRING);
664 fprintf(fp, FACILITY_NAME_STRING);
666 fprintf(fp, "%04d\n", CURRENT_VERSION);
667 fprintf(fp, " controls: %d\n", Number_of_Controls);
668 fprintf(fp, "# module parameter min_val max_val pert_size\n");
669 for (i=0; i<Number_of_Controls; i++)
670 fprintf(fp, " %s\t%s\t%E\t%E\t%E\n",
671 Controls[i].Symbol.Mod_Name,
672 Controls[i].Symbol.Par_Name,
675 Controls[i].Pert_Size*Controls[i].Authority);
676 fprintf(fp, " outputs: %d\n", Number_of_Outputs);
677 fprintf(fp, "# module parameter trim_criteria\n");
678 for (i=0;i<Number_of_Outputs;i++)
679 fprintf(fp, " %s\t%s\t%E\n",
680 Outputs[i].Symbol.Mod_Name,
681 Outputs[i].Symbol.Par_Name,
682 Outputs[i].Trim_Criteria );
683 fprintf(fp, "end\n");