| 1 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 | |
| 3 | Module: FGOutputType.cpp |
| 4 | Author: Bertrand Coconnier |
| 5 | Date started: 09/10/11 |
| 6 | Purpose: Manage output of sim parameters to file or stdout |
| 7 | |
| 8 | ------------- Copyright (C) 2011 Bertrand Coconnier ------------- |
| 9 | |
| 10 | This program is free software; you can redistribute it and/or modify it under |
| 11 | the terms of the GNU Lesser General Public License as published by the Free |
| 12 | Software Foundation; either version 2 of the License, or (at your option) any |
| 13 | later version. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 18 | details. |
| 19 | |
| 20 | You should have received a copy of the GNU Lesser General Public License along |
| 21 | with this program; if not, write to the Free Software Foundation, Inc., 59 |
| 22 | Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 23 | |
| 24 | Further information about the GNU Lesser General Public License can also be |
| 25 | found on the world wide web at http://www.gnu.org. |
| 26 | |
| 27 | FUNCTIONAL DESCRIPTION |
| 28 | -------------------------------------------------------------------------------- |
| 29 | This is the place where you create output routines to dump data for perusal |
| 30 | later. |
| 31 | |
| 32 | HISTORY |
| 33 | -------------------------------------------------------------------------------- |
| 34 | 09/10/11 BC Created |
| 35 | |
| 36 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 37 | INCLUDES |
| 38 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 39 | |
| 40 | #include <ostream> |
| 41 | |
| 42 | #include "FGFDMExec.h" |
| 43 | #include "FGOutputType.h" |
| 44 | #include "input_output/FGXMLElement.h" |
| 45 | #include "input_output/FGPropertyManager.h" |
| 46 | #include "math/FGTemplateFunc.h" |
| 47 | #include "math/FGFunctionValue.h" |
| 48 | |
| 49 | using namespace std; |
| 50 | |
| 51 | namespace JSBSim { |
| 52 | |
| 53 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 54 | CLASS IMPLEMENTATION |
| 55 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 56 | |
| 57 | FGOutputType::FGOutputType(FGFDMExec* fdmex) : |
| 58 | FGModel(fdmex), |
| 59 | SubSystems(0), |
| 60 | enabled(true) |
| 61 | { |
| 62 | Aerodynamics = FDMExec->GetAerodynamics(); |
| 63 | Auxiliary = FDMExec->GetAuxiliary(); |
| 64 | Aircraft = FDMExec->GetAircraft(); |
| 65 | Winds = FDMExec->GetWinds(); |
| 66 | Propulsion = FDMExec->GetPropulsion(); |
| 67 | MassBalance = FDMExec->GetMassBalance(); |
| 68 | Propagate = FDMExec->GetPropagate(); |
| 69 | Accelerations = FDMExec->GetAccelerations(); |
| 70 | FCS = FDMExec->GetFCS(); |
| 71 | GroundReactions = FDMExec->GetGroundReactions(); |
| 72 | ExternalReactions = FDMExec->GetExternalReactions(); |
| 73 | BuoyantForces = FDMExec->GetBuoyantForces(); |
| 74 | |
| 75 | Debug(0); |
| 76 | } |
| 77 | |
| 78 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 79 | |
| 80 | FGOutputType::~FGOutputType() |
| 81 | { |
| 82 | for (auto param: OutputParameters) |
| 83 | delete param; |
| 84 | |
| 85 | Debug(1); |
| 86 | } |
| 87 | |
| 88 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 89 | |
| 90 | void FGOutputType::SetIdx(unsigned int idx) |
| 91 | { |
| 92 | string outputProp = CreateIndexedPropertyName("simulation/output", idx); |
| 93 | |
| 94 | PropertyManager->Tie(outputProp + "/log_rate_hz", this, &FGOutputType::GetRateHz, &FGOutputType::SetRateHz); |
| 95 | PropertyManager->Tie(outputProp + "/enabled", &enabled); |
| 96 | OutputIdx = idx; |
| 97 | } |
| 98 | |
| 99 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 100 | |
| 101 | bool FGOutputType::Load(Element* element) |
| 102 | { |
| 103 | if (element->FindElementValue("simulation") == string("ON")) |
| 104 | SubSystems += ssSimulation; |
| 105 | if (element->FindElementValue("aerosurfaces") == string("ON")) |
| 106 | SubSystems += ssAerosurfaces; |
| 107 | if (element->FindElementValue("rates") == string("ON")) |
| 108 | SubSystems += ssRates; |
| 109 | if (element->FindElementValue("velocities") == string("ON")) |
| 110 | SubSystems += ssVelocities; |
| 111 | if (element->FindElementValue("forces") == string("ON")) |
| 112 | SubSystems += ssForces; |
| 113 | if (element->FindElementValue("moments") == string("ON")) |
| 114 | SubSystems += ssMoments; |
| 115 | if (element->FindElementValue("atmosphere") == string("ON")) |
| 116 | SubSystems += ssAtmosphere; |
| 117 | if (element->FindElementValue("massprops") == string("ON")) |
| 118 | SubSystems += ssMassProps; |
| 119 | if (element->FindElementValue("position") == string("ON")) |
| 120 | SubSystems += ssPropagate; |
| 121 | if (element->FindElementValue("coefficients") == string("ON") || element->FindElementValue("aerodynamics") == string("ON")) |
| 122 | SubSystems += ssAeroFunctions; |
| 123 | if (element->FindElementValue("ground_reactions") == string("ON")) |
| 124 | SubSystems += ssGroundReactions; |
| 125 | if (element->FindElementValue("fcs") == string("ON")) |
| 126 | SubSystems += ssFCS; |
| 127 | if (element->FindElementValue("propulsion") == string("ON")) |
| 128 | SubSystems += ssPropulsion; |
| 129 | |
| 130 | Element *property_element = element->FindElement("property"); |
| 131 | |
| 132 | while (property_element) { |
| 133 | string property_str = property_element->GetDataLine(); |
| 134 | FGPropertyNode* node = PropertyManager->GetNode(property_str); |
| 135 | if (!node) { |
| 136 | cerr << property_element->ReadFrom() |
| 137 | << fgred << highint << endl << " No property by the name " |
| 138 | << property_str << " has been defined. This property will " << endl |
| 139 | << " not be logged. You should check your configuration file." |
| 140 | << reset << endl; |
| 141 | } else { |
| 142 | if (property_element->HasAttribute("apply")) { |
| 143 | string function_str = property_element->GetAttributeValue("apply"); |
| 144 | FGTemplateFunc* f = FDMExec->GetTemplateFunc(function_str); |
| 145 | if (f) |
| 146 | OutputParameters.push_back(new FGFunctionValue(node, f)); |
| 147 | else { |
| 148 | cerr << property_element->ReadFrom() |
| 149 | << fgred << highint << " No function by the name " |
| 150 | << function_str << " has been defined. This property will " |
| 151 | << "not be logged. You should check your configuration file." |
| 152 | << reset << endl; |
| 153 | } |
| 154 | } |
| 155 | else |
| 156 | OutputParameters.push_back(new FGPropertyValue(node)); |
| 157 | |
| 158 | if (property_element->HasAttribute("caption")) |
| 159 | OutputCaptions.push_back(property_element->GetAttributeValue("caption")); |
| 160 | else |
| 161 | OutputCaptions.push_back(""); |
| 162 | } |
| 163 | property_element = element->FindNextElement("property"); |
| 164 | } |
| 165 | |
| 166 | double outRate = 1.0; |
| 167 | if (element->HasAttribute("rate")) |
| 168 | outRate = element->GetAttributeValueAsNumber("rate"); |
| 169 | |
| 170 | SetRateHz(outRate); |
| 171 | |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 176 | |
| 177 | bool FGOutputType::InitModel(void) |
| 178 | { |
| 179 | bool ret = FGModel::InitModel(); |
| 180 | |
| 181 | Debug(2); |
| 182 | return ret; |
| 183 | } |
| 184 | |
| 185 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 186 | |
| 187 | bool FGOutputType::Run(void) |
| 188 | { |
| 189 | if (FGModel::Run(false)) return true; |
| 190 | if (!enabled) return true; |
| 191 | |
| 192 | RunPreFunctions(); |
| 193 | Print(); |
| 194 | RunPostFunctions(); |
| 195 | |
| 196 | Debug(4); |
| 197 | |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 202 | |
| 203 | void FGOutputType::SetRateHz(double rtHz) |
| 204 | { |
| 205 | rtHz = rtHz>1000?1000:(rtHz<0?0:rtHz); |
| 206 | if (rtHz > 0) { |
| 207 | SetRate(0.5 + 1.0/(FDMExec->GetDeltaT()*rtHz)); |
| 208 | Enable(); |
| 209 | } else { |
| 210 | SetRate(1); |
| 211 | Disable(); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 216 | |
| 217 | double FGOutputType::GetRateHz(void) const |
| 218 | { |
| 219 | return 1.0 / (rate * FDMExec->GetDeltaT()); |
| 220 | } |
| 221 | |
| 222 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 223 | |
| 224 | void FGOutputType::SetOutputProperties(vector<FGPropertyNode_ptr> & outputProperties) |
| 225 | { |
| 226 | for (auto prop: outputProperties) |
| 227 | OutputParameters.push_back(new FGPropertyValue(prop)); |
| 228 | } |
| 229 | |
| 230 | |
| 231 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 232 | // The bitmasked value choices are as follows: |
| 233 | // unset: In this case (the default) JSBSim would only print |
| 234 | // out the normally expected messages, essentially echoing |
| 235 | // the config files as they are read. If the environment |
| 236 | // variable is not set, debug_lvl is set to 1 internally |
| 237 | // 0: This requests JSBSim not to output any messages |
| 238 | // whatsoever. |
| 239 | // 1: This value explicity requests the normal JSBSim |
| 240 | // startup messages |
| 241 | // 2: This value asks for a message to be printed out when |
| 242 | // a class is instantiated |
| 243 | // 4: When this value is set, a message is displayed when a |
| 244 | // FGModel object executes its Run() method |
| 245 | // 8: When this value is set, various runtime state variables |
| 246 | // are printed out periodically |
| 247 | // 16: When set various parameters are sanity checked and |
| 248 | // a message is printed out when they go out of bounds |
| 249 | |
| 250 | void FGOutputType::Debug(int from) |
| 251 | { |
| 252 | if (debug_lvl <= 0) return; |
| 253 | |
| 254 | if (debug_lvl & 1) { // Standard console startup message output |
| 255 | if (from == 0) { // Constructor |
| 256 | } |
| 257 | if (from == 2) { |
| 258 | if (SubSystems & ssSimulation) cout << " Simulation parameters logged" << endl; |
| 259 | if (SubSystems & ssAerosurfaces) cout << " Aerosurface parameters logged" << endl; |
| 260 | if (SubSystems & ssRates) cout << " Rate parameters logged" << endl; |
| 261 | if (SubSystems & ssVelocities) cout << " Velocity parameters logged" << endl; |
| 262 | if (SubSystems & ssForces) cout << " Force parameters logged" << endl; |
| 263 | if (SubSystems & ssMoments) cout << " Moments parameters logged" << endl; |
| 264 | if (SubSystems & ssAtmosphere) cout << " Atmosphere parameters logged" << endl; |
| 265 | if (SubSystems & ssMassProps) cout << " Mass parameters logged" << endl; |
| 266 | if (SubSystems & ssAeroFunctions) cout << " Coefficient parameters logged" << endl; |
| 267 | if (SubSystems & ssPropagate) cout << " Propagate parameters logged" << endl; |
| 268 | if (SubSystems & ssGroundReactions) cout << " Ground parameters logged" << endl; |
| 269 | if (SubSystems & ssFCS) cout << " FCS parameters logged" << endl; |
| 270 | if (SubSystems & ssPropulsion) cout << " Propulsion parameters logged" << endl; |
| 271 | if (!OutputParameters.empty()) cout << " Properties logged:" << endl; |
| 272 | for (auto param: OutputParameters) |
| 273 | cout << " - " << param->GetName() << endl; |
| 274 | } |
| 275 | } |
| 276 | if (debug_lvl & 2 ) { // Instantiation/Destruction notification |
| 277 | if (from == 0) cout << "Instantiated: FGOutputType" << endl; |
| 278 | if (from == 1) cout << "Destroyed: FGOutputType" << endl; |
| 279 | } |
| 280 | if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects |
| 281 | } |
| 282 | if (debug_lvl & 8 ) { // Runtime state variables |
| 283 | } |
| 284 | if (debug_lvl & 16) { // Sanity checking |
| 285 | } |
| 286 | if (debug_lvl & 64) { |
| 287 | if (from == 0) { // Constructor |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | } |