jsbsim
jsbsim copied to clipboard
Generate Linear Model from XML Script
Does anyone have any experience with, or is it possible to generate a linear model via XML scripting? Looking to call into FGStateSpace to get x0, u0, A, B, C, D vectors. Thanks!
Your request is too vague. What do you mean by "generate a linear model" ?
No response from the issuer for 3 weeks: issue closed.
@bcoconni Sorry for the delay, i was not notified of a comment on this ticket. I thought generating a linear model is pretty standard, and I believe it is a part of the source code already. It is implemented in JSBSim Advanced Triming Branch by calling into FGLinearization.cpp.
The output should be something like this: `x0=.. [ 3.3500000001e+002; 1.0833708379e-001; 1.0833708380e-001; 0.0000000000e+000; -5.8423256804e-004; 0.0000000000e+000; 0.0000000000e+000; 0.0000000000e+000; 0.0000000000e+000; 0.0000000000e+000; 0.0000000000e+000; 1.5000000000e+002];
u0=.. [ 4.1507273608e-001; -3.5248398193e-003; -2.0575829692e-001; -2.9106407215e-003];
sys = syslin('c',.. [ -4.3427060360e-002, -9.8096550611e+000, -3.2087058179e+001, -2.0560557019e-010, 3.3305725641e+002, -1.8636402009e-002, -2.0618344994e-010, -2.1078986357e-010, 6.3607575515e-010, 2.0773530465e-010, -2.1567803515e-001, 1.4236057475e-005; -8.1166258462e-005, -1.3702931926e-001, 1.1657013512e-002, 1.0000000112e+000, -2.3830098715e-007, 1.5667392399e-005, 5.7283788879e-004, 6.2980734972e-005, -1.4375970466e-004, -1.7085689860e-007, -2.1476001587e-008, 3.9049977087e-007; 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 1.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 6.0540241507e-036, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000; 4.8014340544e-005, -2.6971631568e+000, -1.9094256891e-002, -2.2374890325e+000, 6.2364254569e-001, -2.5663288773e-005, -9.2158761873e-004, -1.0519342025e-004, 2.3547930880e-004, 2.7986858924e-007, 3.0142651432e-008, -5.6196960641e-007; 3.2484363763e-007, 1.4609051075e-004, -2.0180142393e-004, -3.0944937006e-015, 7.3392437136e-006, 9.5220718213e-002, 1.0812528411e-001, -9.9413727569e-001, -6.0973949052e-013, -2.4168673428e-015, 1.4546616224e-004, -3.2639002901e-015; 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 5.4871117678e-038, 0.0000000000e+000, 0.0000000000e+000, 1.0000000000e+000, 1.0876293119e-001, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000; 4.3186288961e-006, 3.6266092870e-002, -2.3172110931e-012, -1.5085023771e-005, -1.3973860910e+001, -3.2521186265e-011, -2.8602884424e+000, 2.5709013825e-002, 4.8483072664e-010, -1.7883824485e-011, -1.6154612370e-013, -2.1185981843e-008; -3.8410920579e-008, 2.7088800961e-003, 3.0836305031e-012, 1.5042210065e-005, 1.8357369617e+000, -4.5386746125e-010, -2.9805207306e-002, -3.9335120276e-001, 4.1956396647e-009, 2.3490235331e-012, 1.4995306610e-014, 1.8686946609e-010; 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 3.0448632692e-036, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 1.0058972985e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000; -2.7919215606e-011, -1.7922246605e-020, 1.1029074834e-020, 0.0000000000e+000, 0.0000000000e+000, -1.7309715278e-006, 0.0000000000e+000, 0.0000000000e+000, 1.6008943163e-005, 2.0413438883e-017, 0.0000000000e+000, 4.3213431698e-016; 4.7787890441e-008, 2.4281611155e-016, 1.2423149893e-016, 1.4117215788e-016, 9.0350181040e-017, -1.0112899773e-009, 1.4117215788e-016, 1.4117215788e-016, 9.3529470475e-009, 1.4117215788e-016, 9.3173624198e-017, -7.6492439679e-013; 1.3286536223e-010, -3.3500000001e+002, 3.3499994284e+002, 1.2199442845e-010, 1.2267097060e-010, 1.9457045900e-001, 1.6344275470e-010, 2.0440824959e-010, 1.7561183988e-010, 2.0440824959e-010, 3.8202369509e-010, 2.0440824959e-010],.. [ 2.5569127211e+001, 2.0899803878e-012, 1.5534093046e+000, -1.5323390704e-012; -1.1952405922e-003, 1.0162385181e-004, -8.7323683594e-003, 7.4279608648e-004; 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000; 4.6836146026e-002, -1.6649632294e-004, -1.1821223004e+000, -1.2000347358e-003; 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000; 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000; 0.0000000000e+000, 1.6676965113e+000, 1.2463432217e-005, 5.3727331192e-001; 0.0000000000e+000, -1.4953362323e-003, 9.5942719514e-007, -3.6441188893e-001; 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000; 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000; 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000; 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000],.. [ 1.0000000104e+000, 1.3737159558e-009, 7.0106883262e-009, 5.3053857603e-009, 6.4422541376e-009, 7.2949054205e-009, 5.3053857603e-009, 5.3053857603e-009, 6.5369931690e-009, 5.3053857603e-009, 4.8316906032e-009, 5.3053857603e-009; -9.1362103068e-013, 9.9999999998e-001, 1.9463597400e-011, -1.2143064332e-012, -2.7755575616e-013, -3.4694469520e-013, -1.6884641833e-012, -1.2143064332e-012, -3.0068540250e-013, -1.6653345369e-012, -1.1564823173e-012, -1.2143064332e-012; 0.0000000000e+000, -1.7066414254e-007, 1.0000000000e+000, 0.0000000000e+000, -1.5034270125e-013, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 4.0476881106e-013, 0.0000000000e+000; 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 1.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000; 1.8468480506e-012, 1.6424759411e-012, -1.6175392912e-012, 1.0751671544e-014, 1.0000000000e+000, 1.1021818585e-012, 1.0842021725e-014, 1.0842021725e-014, 4.1105718366e-012, 1.2988742026e-012, 9.9385199145e-015, -9.1723503792e-013; 0.0000000000e+000, 1.0337246237e-029, -7.7492183861e-029, 0.0000000000e+000, -3.2661079214e-017, 1.0000000000e+000, 4.0360161005e-032, 0.0000000000e+000, 0.0000000000e+000, 1.6028605840e-016, -8.9092157003e-048, 0.0000000000e+000; 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 1.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000; 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 1.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000; 0.0000000000e+000, 4.1887902048e+004, 4.7123889804e+004, 0.0000000000e+000, 3.6650914292e+004, 5.2359877560e+003, 0.0000000000e+000, 0.0000000000e+000, -3.6650914292e+004, 4.7123889804e+004, 5.2359877560e+003, 0.0000000000e+000; 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 1.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000; 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 1.0000000000e+000, 0.0000000000e+000; 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 1.0000231365e+000],.. [ 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000; 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000; 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000; 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000; 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000; 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000; 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000; 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000; 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000; 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000; 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000; 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000, 0.0000000000e+000]);
tfm = ss2tf(sys);
`
Please let me know if more information is needed. Generation a linear model at a flight condition is a commonly needed output in modern autopilot development processes.
Taking a quick look I see FGLinearization.h and FGLinearization.cpp are in the current source tree in the initialization directory, however they are not referenced in CMakeLists.txt so they aren't included in the build currently.
Also searching for FGLinearization in the Advanced Trimming Branch see https://github.com/arktools/jsbsim/search?q=FGLinearization&unscoped_q=FGLinearization comes up without any hits at all. I was trying to see how the linearization code is typically called.
I confirm. This code comes from the great work by James Goppert, back in 2012. https://github.com/arktools/jsbsim
Taking a quick look I see
FGLinearization.handFGLinearization.cppare in the current source tree in theinitializationdirectory, however they are not referenced inCMakeLists.txtso they aren't included in the build currently.
Well they were at some point during JSBSim history (commits listed below with their messages because they are quite enlightening):
- Commit 71e66e7f77447af0c0493fa0d52a659721a3f8d1 Added (FINALLY) James Goppert's trimming and state space determination routines (Oct 25,2012)
- Commit d724427f1117794cf43662ec442c0f11477226e1 Incorporated most of or all of JGoppert's trim improvements (Jan 25, 2013)
and then at some point Jon decided to remove all the code altogether:
- Commit 0c739604464f0f3eaceda676d200f33c89b20180 Removed unused, undocumented, and not working trim analysis and linearization code (Dec 9, 2015)
Interestingly, the linearization code was supposed to be run by setting a mode value in the property /simulation/do_linearization
https://github.com/JSBSim-Team/jsbsim/blob/d724427f1117794cf43662ec442c0f11477226e1/src/FGFDMExec.cpp#L157
and the linearization was made by calling FGLinearization
https://github.com/JSBSim-Team/jsbsim/blob/d724427f1117794cf43662ec442c0f11477226e1/src/FGFDMExec.cpp#L1203-L1211
Another interesting point is that the documentation of DoLinearization stipulated that it should be called after the aircraft was trimmed
https://github.com/JSBSim-Team/jsbsim/blob/d724427f1117794cf43662ec442c0f11477226e1/src/FGFDMExec.h#L470-L473
The good news are that we are not starting from a blank sheet. The bad news are that Jon's comment made it clear in the commit 0c739604464f0f3eaceda676d200f33c89b20180 (the one that removed James Goppert's code) that there were some problems with the code and I infer from that that we'd better know what we are doing before reintroducing this code in the current code base.
I am not surprised that you should trim the aircraft before running the linearization routine. This is a common stipulation, as linearization is only valid at a stable trim point.
@jonsberndt can you shed any light on what the problems were in the code when you removed it via commit https://github.com/JSBSim-Team/jsbsim/commit/0c739604464f0f3eaceda676d200f33c89b20180?
In particular I guess if you remember any particular issues with the linearization code. If the issue was only/mainly with the trim code then maybe the linearization code could be used with the current trim code?
I have come across automatic differentiation which should also allow to generate a linear model with much more flexibility than what was done by FGLinearization. With that technique you can virtually derive any quantity with respect to any other quantity without needing to hard code the formula for that particular differentiation.
The concept is quite simple, you need to implement a new class for dual numbers and replace most of, if not all, occurrences of double in JSBSim by that new class. Nothing really difficult but that would need a decent, although not necessarily extensive, knowledge of C++ (at the very least class design and templating) and no particular familiarity with JSBSim overall architecture since it would be a low level modification.
Any thoughts ?
Very interested in bringing this feature back. Along with more control of trim options. The two features go hand in hand, relying on generation of jacobians, etc. One issue will be dealing with certain classes of nonlinearities in the actuator and sensor models. Disabling freeplay, deadband, error models, noise in the subsystems makes the results more representative of the desired system.
I'd like to help if help is needed.
@rega0051 what additional control of the trim options are you looking for?
In terms of freeplay, deadband etc. if you don't want them included while generated the linearized model are you suggesting that the linearizing process automatically detect them and exclude them as opposed to you removing them from the model during the linearization process?
@seanmcleod As far as I know there isn't a means of either programmatically detecting or removing the presence of nonlinearities.
I've had many issues with the trim routines being hardcoded, I'm hopeful these issues won't propagate to the Linearization. A few of the issues off the top of my head: Trimming a flying wing without yaw control: can't use tFull, but tLongitudinal doesn't trim yaw at all (the desire is to have yaw trimmed with sideslip). The other recent issue I ran into is trimming a quad copter model in hover. The tCustom trim type would likely be useful, but there is no means of setting it up through a script or Python binding.
Goppert's Simplex linearization was also hardcoded if I recall (at least some of the parameters anyway, I think the GUI interface had ability to modify). The real difference with FGTrim is that the simplex algorithm will attempt to simultaneously minimize the objectives simultaneously.
@agodemar I'm trying to remember/figure out the relationship between your paper - 'A General Solution to the Aircraft Trim Problem' - http://wpage.unina.it/agodemar/DSV-DQV/AIAA-2007-6703-905_DeMarco_Duke_Berndt.pdf and the trim code you mention in the paper to James Goppert's trimming code in - https://github.com/arktools/jsbsim
Was James's code an updated/improved version of what you describe in the paper?
And do you remember why @jonsberndt removed it in 2015? Had something changed between 2013 and 2015 with lack of maintenance etc. to stop it working?
@rega0051
Very interested in bringing this feature back. Along with more control of trim options. The two features go hand in hand, relying on generation of jacobians, etc. One issue will be dealing with certain classes of nonlinearities in the actuator and sensor models. Disabling freeplay, deadband, error models, noise in the subsystems makes the results more representative of the desired system.
I'd like to help if help is needed.
Of course, help is always very appreciated :smile: You're very welcome to submit proposals. Just make sure that we are on the same page before writing too much code. First things first, I would like to clarify what you are volunteering for ? Is it bringing back James Goppert's FGLinearization code as the original poster @benhager asked? Or is it about coding automatic differentiation as I have suggested above ? Not trying to push either way but we need to discuss the same topic to avoid misunderstandings.
As far as I know there isn't a means of either programmatically detecting or removing the presence of nonlinearities.
Well, it might not be possible to detect non linearities in general but we can detect the usage of FGActuator, FGDeadBand and the likes. Special handling can then be coded for these particular flight controls.
@seanmcleod There were at some point 3 different trim algorithms in JSBSim.
Legacy trim routines
These are the older (and the last still active) trim routines. They were designed by Tony Peden. The algorithm is quite simple: it trims one axis at the time. Of course this fails when local axis minima do not match the global minimum, hence the development of algorithms that targeted the global minimum by trying to minimize all axes simultaneously.
Files: src/initialization/FGTrim.h, src/initialization/FGTrim.cpp, src/initialization/FGTrimAxis.h and src/initialization/FGTrimAxis.cpp.
De Marco/Durke/Berndt's trim algorithm
These are implementing the algorithms described in the paper 'A General Solution to the Aircraft Trim Problem'. Even though its source code is still in JSBSim repository, it is no longer compiled and linked with JSBSim making it dead, unmaintained code.
Files: src/initialization/FGTrimAnalysis.h, src/initialization/FGTrimAnalysis.cpp, src/initialization/FGTrimAnalysisControl.h and src/initialization/FGTrimAnalysisControl.cpp.
Goppert's trim routines
As @rega0051 mentioned, these are using the Simplex algorithm to optimize all the axis simultaneously. The differentiation (aka linearization) is indeed hardcoded in FGLinearization and the trim routines themselves are managed by FGTrimmer and FGSimplexTrim. There again, even though the source code is still in JSBSim repository, it is no longer compiled and linked with JSBSim making it dead, unmaintained code.
Files: src/initialization/FGTrimmer.h, src/initialization/FGTrimmer.cpp,src/initialization/FGSimplexTrim.h, src/initialization/FGSimplexTrim.cpp, src/initialization/FGLinearization.h and src/initialization/FGLinearization.cpp.
In the case where some or all of these routines would be brought back to JSBSim, keep in mind that they also need some "glue" code in FGFDMExec to be summoned from scripts.
@bcoconni thanks for the good summary.
I'm interested in trim routines that can potentially handle more cases than the current trim routines in JSBSim so I'll spend some time in a local branch adding Goppert's trim routines back in and testing them.
I'm interested in trim routines that can potentially handle more cases than the current trim routines in JSBSim so I'll spend some time in a local branch adding Goppert's trim routines back in and testing them.
Sure. That would be most interesting.
For the record, James Goppert has documented his algorithm in a document that is still accessible from the internet: On Some Trim Strategies for Nonlinear Aircraft Flight Dynamics Models with the Open Source Software JSBSim
Also note that James Goppert (@jgoppert) is on GitHub, no doubt he has some recollections about the algorithm he wrote almost 10 years ago :smiley:
When I was searching yesterday I also came across a couple of YouTube videos he posted demonstrating the trim functionality via a GUI he produced and also generating linearized models and loading them up in Scicoslab.
https://www.youtube.com/watch?v=JCjRGJTWe5k
https://www.youtube.com/watch?v=-B0nQbcyo-o
I got sick of hidden states in the c++ code. For instance trying to track down states in a jet engine model. I would push you toward automatic differentiation. I was using numerical differentiation with a Nelder Mead Simplex for trimming. Also being able to obtain a human readable state name would be useful. Using AD and getting a 20 state model and not knowing why can be annoying. I found the Modelica language is a bit friendlier in this regard as it maps directly to a differential algebraic equation and supports AD, also see pymoca and casadi which supports AD. Casadi is a C++ AD library and maybe you could leverage it. Let me know how I can help.
For the record, James Goppert has documented his algorithm in a document that is still accessible from the internet: On Some Trim Strategies for Nonlinear Aircraft Flight Dynamics Models with the Open Source Software JSBSim
For the record, that's a presentation I gave of a paper that @jgoppert and me co-authored quite a few years ago ;-)
For the record, that's a presentation I gave of a paper that @jgoppert and me co-authored quite a few years ago ;-)
Oops ! Sorry. And for the sake of completeness there was also a third author: Inseok Hwang 😉
After some more archeological unearthing, I found the following 2 discussions threads in the mailing list archives:
- Announcement of the addition of the simplex code to JSBSim dated Oct 25, 2012
- Jon reported that something is broken in the simplex code dated May 20, 2014. Not giving much details unfortunately.
This is just a heads up that the latest version of AeromatiC++ now also outputs the linear coefficients in JSON format. This might be enough for some uses.
This is just a heads up that the latest version of AeromatiC++ now also outputs the linear coefficients in JSON format. This might be enough for some uses.
@ermarch Just to clarify things : Aeromatic++ is not able to read an existing JSBSim model and build linear coefficients for any trimmed position, is it ?
Yes that's correct, it can only calculate them from geometric data.
I'm interested in using Goppert's linearization code (FGLinearization & FGStateSpace) to generate linear systems from the JSBsim models. Based on this thread I think there are more people who are interested in this as well. Since I'm planning on doing this either anyway, is there some specific way you'd like me to expose this functionality?
My plan for now was to add a DoLinearization function to FGFDMExec. This function would take in four std::vector< std::vector<double>> references to write the state space matrices to. I'm only planning on using this function through the python wrapper, so the next step for me would be to wrap this function so it returns 4 numpy arrays.
Do do this I want to do these modification to the FGLinearization class.
- Make the A, B, C and matrices private members of the class.
- Add a
GetStateSpace(...)function that takes fourstd::vector< std::vector<double>>references. - Move the scicoslab code generator to a seperate function, but since I don't need this I'm not planning planning on exposing this unless there is some interest.
This will 100% break any code that already uses this, but I don't think will be an issue considering these files are currently not being included in the build.
I'll make pull request once I have something working. Are there any requested features?
@aarondewindt That's a good start. Thanks. It would be great if you could double check the trimming features introduced by Goppert.
I've exposed the linearization function to python. My plan is to spend the next few days testing and checking it out.
Are there any known issues with it by any chance? Jon mentions that the state space and linearization code stopped working, but I'm getting models out of it. I still need to see how valid they are though. @jgoppert and @jonsberndt, do either of you have any input or tips for me?
@agodemar I might have a look at those at some point in the future, for now I'm focusing on the linearization part of it. From what I understand the issue that ended up with the Goppert's code being removed was with the linearization.