]> git.mxchange.org Git - flightgear.git/blob - LaRCsim/ls_init.c
731f91162d7347891b8c8c7fbb9954581c5f1255
[flightgear.git] / LaRCsim / ls_init.c
1 /***************************************************************************
2
3         TITLE:  ls_init.c
4         
5 ----------------------------------------------------------------------------
6
7         FUNCTION:       Initializes simulation
8
9 ----------------------------------------------------------------------------
10
11         MODULE STATUS:  incomplete
12
13 ----------------------------------------------------------------------------
14
15         GENEALOGY:      Written 921230 by Bruce Jackson
16
17 ----------------------------------------------------------------------------
18
19         DESIGNED BY:    EBJ
20         
21         CODED BY:       EBJ
22         
23         MAINTAINED BY:  EBJ
24
25 ----------------------------------------------------------------------------
26
27         MODIFICATION HISTORY:
28         
29         DATE    PURPOSE                                         BY
30
31         950314  Added get_set, put_set, and init routines.      EBJ
32         
33         CURRENT RCS HEADER:
34
35 $Header$
36 $Log$
37 Revision 1.1  1997/05/29 00:09:57  curt
38 Initial Flight Gear revision.
39
40  * Revision 1.4  1995/03/15  12:15:23  bjax
41  * Added ls_init_get_set() and ls_init_put_set() and ls_init_init()
42  * routines. EBJ
43  *
44  * Revision 1.3  1994/01/11  19:09:44  bjax
45  * Fixed header includes.
46  *
47  * Revision 1.2  1992/12/30  14:04:53  bjax
48  * Added call to ls_step(0, 1).
49  *
50  * Revision 1.1  92/12/30  14:02:19  bjax
51  * Initial revision
52  * 
53  * Revision 1.1  92/12/30  13:21:21  bjax
54  * Initial revision
55  * 
56  * Revision 1.3  93/12/31  10:34:11  bjax
57  * Added $Log marker as well.
58  * 
59
60 ----------------------------------------------------------------------------
61
62         REFERENCES:
63
64 ----------------------------------------------------------------------------
65
66         CALLED BY:
67
68 ----------------------------------------------------------------------------
69
70         CALLS TO:
71
72 ----------------------------------------------------------------------------
73
74         INPUTS:
75
76 ----------------------------------------------------------------------------
77
78         OUTPUTS:
79
80 --------------------------------------------------------------------------*/
81 static char rcsid[] = "$Id$";
82
83 #include <string.h>
84 #include <stdio.h>
85 #include "ls_types.h"
86 #include "ls_sym.h"
87
88 #define MAX_NUMBER_OF_CONTINUOUS_STATES 100
89 #define MAX_NUMBER_OF_DISCRETE_STATES  20
90 #define HARDWIRED 13
91 #define NIL_POINTER 0L
92
93 #define FACILITY_NAME_STRING "init"
94 #define CURRENT_VERSION 10
95
96 typedef struct
97 {
98     symbol_rec  Symbol;
99     double      value;
100 } cont_state_rec;
101
102 typedef struct
103 {
104     symbol_rec  Symbol;
105     long        value;
106 } disc_state_rec;
107
108
109 extern SCALAR Simtime;
110
111 static int Symbols_loaded = 0;
112 static int Number_of_Continuous_States = 0;
113 static int Number_of_Discrete_States = 0;
114 static cont_state_rec   Continuous_States[ MAX_NUMBER_OF_CONTINUOUS_STATES ];
115 static disc_state_rec   Discrete_States[  MAX_NUMBER_OF_DISCRETE_STATES ];
116
117
118 void ls_init_init()
119 {
120     int i, error;
121
122     if (Number_of_Continuous_States == 0)
123         {
124             Number_of_Continuous_States = HARDWIRED;
125
126             for (i=0;i<HARDWIRED;i++)
127                 strcpy( Continuous_States[i].Symbol.Mod_Name, "*" );
128
129             strcpy( Continuous_States[ 0].Symbol.Par_Name, 
130                     "generic_.geodetic_position_v[0]");
131             strcpy( Continuous_States[ 1].Symbol.Par_Name, 
132                     "generic_.geodetic_position_v[1]");
133             strcpy( Continuous_States[ 2].Symbol.Par_Name, 
134                     "generic_.geodetic_position_v[2]");
135             strcpy( Continuous_States[ 3].Symbol.Par_Name, 
136                     "generic_.v_local_v[0]");
137             strcpy( Continuous_States[ 4].Symbol.Par_Name, 
138                     "generic_.v_local_v[1]");
139             strcpy( Continuous_States[ 5].Symbol.Par_Name, 
140                     "generic_.v_local_v[2]");
141             strcpy( Continuous_States[ 6].Symbol.Par_Name, 
142                     "generic_.euler_angles_v[0]");
143             strcpy( Continuous_States[ 7].Symbol.Par_Name, 
144                     "generic_.euler_angles_v[1]");
145             strcpy( Continuous_States[ 8].Symbol.Par_Name, 
146                     "generic_.euler_angles_v[2]");
147             strcpy( Continuous_States[ 9].Symbol.Par_Name, 
148                     "generic_.omega_body_v[0]");
149             strcpy( Continuous_States[10].Symbol.Par_Name, 
150                     "generic_.omega_body_v[1]");
151             strcpy( Continuous_States[11].Symbol.Par_Name, 
152                     "generic_.omega_body_v[2]");
153             strcpy( Continuous_States[12].Symbol.Par_Name, 
154                     "generic_.earth_position_angle");
155         }
156
157     /* commented out by CLO 
158     for (i=0;i<Number_of_Continuous_States;i++)
159         {
160             (void) ls_get_sym_val( &Continuous_States[i].Symbol, &error );
161             if (error) Continuous_States[i].Symbol.Addr = NIL_POINTER;
162         }
163
164     for (i=0;i<Number_of_Discrete_States;i++)
165         {
166             (void) ls_get_sym_val( &Discrete_States[i].Symbol, &error );
167             if (error) Discrete_States[i].Symbol.Addr = NIL_POINTER;
168         }
169     */
170 }
171
172 void ls_init()
173 {
174     int i;
175
176     Simtime = 0;
177
178     ls_init_init();
179     /* move the states to proper values */
180
181     /* commented out by CLO
182     for(i=0;i<Number_of_Continuous_States;i++)
183         if (Continuous_States[i].Symbol.Addr)
184             ls_set_sym_val( &Continuous_States[i].Symbol, 
185                             Continuous_States[i].value );
186
187     for(i=0;i<Number_of_Discrete_States;i++)
188         if (Discrete_States[i].Symbol.Addr)
189             ls_set_sym_val( &Discrete_States[i].Symbol, 
190                             (double) Discrete_States[i].value );
191     */
192   
193     model_init();
194
195     ls_step(0.0, -1);
196 }
197
198
199 char *ls_init_get_set(char *buffer, char *eob)
200 /* This routine parses the settings file for "init" entries. */
201 {
202
203     static char *fac_name = FACILITY_NAME_STRING;
204     char *bufptr;
205     char line[256];
206     int n, ver, i, error, abrt;
207
208     enum {cont_states_header, cont_states, disc_states_header, disc_states, done } looking_for;
209
210     abrt = 0;
211     looking_for = cont_states_header;
212
213     n = sscanf(buffer, "%s", line);
214     if (n == 0) return 0L;
215     if (strncasecmp( fac_name, line, strlen(fac_name) )) return 0L;
216
217     bufptr = strtok( buffer+strlen(fac_name)+1, "\n");
218     if (bufptr == 0) return 0L;
219
220     sscanf( bufptr, "%d", &ver );
221     if (ver != CURRENT_VERSION) return 0L;
222
223     ls_init_init();
224
225     while( !abrt && (eob > bufptr))
226       {
227         bufptr = strtok( 0L, "\n");
228         if (bufptr == 0) return 0L;
229         if (strncasecmp( bufptr, "end", 3) == 0) break;
230
231         sscanf( bufptr, "%s", line );
232         if (line[0] != '#') /* ignore comments */
233             {
234                 switch (looking_for)
235                     {
236                     case cont_states_header:
237                         {
238                             if (strncasecmp( line, "continuous_states", 17) == 0) 
239                                 {
240                                     n = sscanf( bufptr, "%s%d", line, 
241                                                 &Number_of_Continuous_States );
242                                     if (n != 2) abrt = 1;
243                                     looking_for = cont_states;
244                                     i = 0;
245                                 }
246                             break;
247                         }
248                     case cont_states:
249                         {
250                             n = sscanf( bufptr, "%s%s%le", 
251                                         Continuous_States[i].Symbol.Mod_Name,
252                                         Continuous_States[i].Symbol.Par_Name,
253                                         &Continuous_States[i].value );
254                             if (n != 3) abrt = 1;
255                             Continuous_States[i].Symbol.Addr = NIL_POINTER;
256                             i++;
257                             if (i >= Number_of_Continuous_States) 
258                                 looking_for = disc_states_header;
259                             break;
260                         }
261                     case disc_states_header:
262                         {
263                             if (strncasecmp( line, "discrete_states", 15) == 0) 
264                                 {
265                                     n = sscanf( bufptr, "%s%d", line, 
266                                                 &Number_of_Discrete_States );
267                                     if (n != 2) abrt = 1;
268                                     looking_for = disc_states;
269                                     i = 0;
270                                 }
271                             break;
272                         }
273                     case disc_states:
274                         {
275                             n = sscanf( bufptr, "%s%s%ld", 
276                                         Discrete_States[i].Symbol.Mod_Name,
277                                         Discrete_States[i].Symbol.Par_Name,
278                                         &Discrete_States[i].value );
279                             if (n != 3) abrt = 1;
280                             Discrete_States[i].Symbol.Addr = NIL_POINTER;
281                             i++;
282                             if (i >= Number_of_Discrete_States) looking_for = done;
283                         }  
284                     case done:
285                         {
286                             break;
287                         }
288                     }
289
290             }
291       }
292
293     Symbols_loaded = !abrt;
294
295     return bufptr;
296 }
297
298
299
300 void ls_init_put_set( FILE *fp )
301 {
302     int i;
303
304     if (fp==0) return;
305     fprintf(fp, "\n");
306     fprintf(fp, "#==============================  %s\n", FACILITY_NAME_STRING);
307     fprintf(fp, "\n");
308     fprintf(fp, FACILITY_NAME_STRING);
309     fprintf(fp, "\n");
310     fprintf(fp, "%04d\n", CURRENT_VERSION);
311     fprintf(fp, "  continuous_states: %d\n", Number_of_Continuous_States);
312     fprintf(fp, "#    module    parameter   value\n");
313     for (i=0; i<Number_of_Continuous_States; i++)
314         fprintf(fp, "    %s\t%s\t%E\n", 
315                 Continuous_States[i].Symbol.Mod_Name,
316                 Continuous_States[i].Symbol.Par_Name,
317                 Continuous_States[i].value );
318
319     fprintf(fp, "  discrete_states: %d\n", Number_of_Discrete_States);
320     fprintf(fp, "#    module    parameter   value\n");
321     for (i=0;i<Number_of_Discrete_States;i++)
322         fprintf(fp, "    %s\t%s\t%ld\n",
323                 Discrete_States[i].Symbol.Mod_Name,
324                 Discrete_States[i].Symbol.Par_Name,
325                 Discrete_States[i].value );
326     fprintf(fp, "end\n");
327     return;
328 }
329
330 void ls_save_current_as_ic()
331 {
332     /* Save current states as initial conditions */
333
334     int i, error;
335
336     /* commented out by CLO 
337     for(i=0;i<Number_of_Continuous_States;i++)
338         if (Continuous_States[i].Symbol.Addr)
339             Continuous_States[i].value = 
340                 ls_get_sym_val( &Continuous_States[i].Symbol, &error );
341
342     for(i=0;i<Number_of_Discrete_States;i++)
343         if (Discrete_States[i].Symbol.Addr)
344             Discrete_States[i].value = (long)
345                 ls_get_sym_val( &Discrete_States[i].Symbol, &error );
346     */
347 }