]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/FGExternalReactions.cpp
Update to the latest version of JSBSim which supports Lighter Than Air craft
[flightgear.git] / src / FDM / JSBSim / models / FGExternalReactions.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGExternalReactions.cpp
4  Author:       David P. Culp
5  Date started: 17/11/06
6  Purpose:      Manages the External Forces
7  Called by:    FGAircraft
8
9  ------------- Copyright (C) 2006  David P. Culp (davidculp2@comcast.net) -------------
10
11  This program is free software; you can redistribute it and/or modify it under
12  the terms of the GNU Lesser General Public License as published by the Free Software
13  Foundation; either version 2 of the License, or (at your option) any later
14  version.
15
16  This program is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
19  details.
20
21  You should have received a copy of the GNU Lesser General Public License along with
22  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
23  Place - Suite 330, Boston, MA  02111-1307, USA.
24
25  Further information about the GNU Lesser General Public License can also be found on
26  the world wide web at http://www.gnu.org.
27
28 FUNCTIONAL DESCRIPTION
29 --------------------------------------------------------------------------------
30
31 HISTORY
32 --------------------------------------------------------------------------------
33 17/11/06   DC   Created
34
35 /%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 INCLUDES
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
38
39 #include "FGExternalReactions.h"
40 #include <string>
41
42 namespace JSBSim {
43
44 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
45 DEFINITIONS
46 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
47
48 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49 GLOBAL DATA
50 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
51
52 static const char *IdSrc = "$Id$";
53 static const char *IdHdr = ID_EXTERNALREACTIONS;
54
55 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
56 CLASS IMPLEMENTATION
57 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
58
59 FGExternalReactions::FGExternalReactions(FGFDMExec* fdmex) : FGModel(fdmex)
60 {
61   NoneDefined = true;
62   Debug(0);
63 }
64
65 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66
67 bool FGExternalReactions::Load(Element* el)
68 {
69   Debug(2);
70
71   // Interface properties are all stored in the interface properties array.
72   // ToDo: Interface properties should not be created if they already exist.
73   // A check should be done prior to creation. This ought to make it easier 
74   // to work with FlightGear, where some properties used in definitions may 
75   // already have been created, but would not be seen when JSBSim is run
76   // in standalone mode.
77
78   Element* property_element;
79   property_element = el->FindElement("property");
80   while (property_element) {
81     double value=0.0;
82     if ( ! property_element->GetAttributeValue("value").empty())
83       value = property_element->GetAttributeValueAsNumber("value");
84     interface_properties.push_back(new double(value));
85     string interface_property_string = property_element->GetDataLine();
86     PropertyManager->Tie(interface_property_string, interface_properties.back());
87     property_element = el->FindNextElement("property");
88   }
89
90   // Parse force elements
91
92   int index=0;
93   Element* force_element = el->FindElement("force");
94   while (force_element) {
95     Forces.push_back( new FGExternalForce(FDMExec, force_element, index) );
96     NoneDefined = false;
97     index++; 
98     force_element = el->FindNextElement("force");
99   }
100
101   return true;
102 }
103
104 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105
106 FGExternalReactions::~FGExternalReactions()
107 {
108   for (unsigned int i=0; i<Forces.size(); i++) delete Forces[i];
109   Forces.clear();
110   for (unsigned int i=0; i<interface_properties.size(); i++) delete interface_properties[i];
111   interface_properties.clear();
112   Debug(1);
113 }
114
115 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116
117 bool FGExternalReactions::InitModel(void)
118 {
119   if (!FGModel::InitModel()) return false;
120
121   return true;
122 }
123
124 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
125
126 bool FGExternalReactions::Run()
127 {
128   if (FGModel::Run()) return true;
129   if (FDMExec->Holding()) return false; // if paused don't execute
130   if (NoneDefined) return true;
131
132   vTotalForces.InitMatrix();
133   vTotalMoments.InitMatrix();
134
135   for (unsigned int i=0; i<Forces.size(); i++) {
136     vTotalForces  += Forces[i]->GetBodyForces();
137     vTotalMoments += Forces[i]->GetMoments();
138   }
139
140   return false;
141 }
142
143 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
144 //    The bitmasked value choices are as follows:
145 //    unset: In this case (the default) JSBSim would only print
146 //       out the normally expected messages, essentially echoing
147 //       the config files as they are read. If the environment
148 //       variable is not set, debug_lvl is set to 1 internally
149 //    0: This requests JSBSim not to output any messages
150 //       whatsoever.
151 //    1: This value explicity requests the normal JSBSim
152 //       startup messages
153 //    2: This value asks for a message to be printed out when
154 //       a class is instantiated
155 //    4: When this value is set, a message is displayed when a
156 //       FGModel object executes its Run() method
157 //    8: When this value is set, various runtime state variables
158 //       are printed out periodically
159 //    16: When set various parameters are sanity checked and
160 //       a message is printed out when they go out of bounds
161
162 void FGExternalReactions::Debug(int from)
163 {
164   if (debug_lvl <= 0) return;
165
166   if (debug_lvl & 1) { // Standard console startup message output
167     if (from == 0) { // Constructor - loading and initialization
168     }
169     if (from == 2) { // Loading
170       cout << endl << "  External Reactions: " << endl;
171     }
172   }
173   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
174     if (from == 0) cout << "Instantiated: FGExternalReactions" << endl;
175     if (from == 1) cout << "Destroyed:    FGExternalReactions" << endl;
176   }
177   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
178   }
179   if (debug_lvl & 8 ) { // Runtime state variables
180   }
181   if (debug_lvl & 16) { // Sanity checking
182   }
183   if (debug_lvl & 64) {
184     if (from == 0) { // Constructor
185       cout << IdSrc << endl;
186       cout << IdHdr << endl;
187     }
188   }
189 }
190
191 } // namespace JSBSim
192