GSoC 2016 Application James Brandon Milam: Base Class and Increased Efficiency for Equation of Motion Generators - wrat/sympy GitHub Wiki
Personal Information
Name: James Brandon Milam (Prefer Brandon) College: University of Florida, Gainesville, Masters in Mechanical Engineering Email: [email protected] Github: jbm950
Short Bio
I'm from Missouri and have obtained my bachelor's degree in mechanical engineering from the University of Missouri - Columbia. I began working with python during my undergraduate experience around four years ago and have loved programming ever since. I am currently pursuing my master's degree also in mechanical engineering at the University of Florida.
Programming Details
I currently do the majority of my programming on a MacBook pro, however, I have access to a Windows platform and virtual Linux machines for testing cross platform compatibility. My preferred editor of choice is vim. Not needing the mouse in the editor streamlines and greatly enhances productivity and efficiency along with the myriad of available short cut keys. In addition, by working in vim I have immediate access to the terminal which is of extreme use whenever I need to run code or use Git commands, etc.
I spend the majority of my programming time working with python, though in my undergraduate experience I was taught and used MATLAB. In addition, I have taught myself some C coding in order to work with an Arduino micro controller. Using the Arduino, I have created a temperature controller for my senior design class, I have implemented a four-wheel robot platform and have done some basic interactions with the Arduino and python over a serial connection.
My python background consists of being completely self taught, including reading all the way through "Learning Python" which is a hefty book that covers all of the basic language fundamentals from lists to metaclasses to decorators. I have used python to make some simple video games, a flashcard application, solve complicated engineering coursework, create a fuzzy speed controller for an aircraft and communicate through UDP to a flight simulator to run the controller. While using python to make video games I made my own library to extend the functionality of the pygame library. My library, pygame_toolbox, is currently hosted on Github and is available through pypi (majority of the code lies in graphics/_init_.py). I feel this library best showcases my coding capabilities with object oriented design and cleanness/documentation of code. In addition to these projects I have also made contributions to python core.
The features that I love about python that I feel differentiate it from other programming languages is that it is open source, extremely versatile, it is dynamically typed and it was created with the specific intention of portraying clarity. The most advanced features/standard library functionalities I have used are communications through serial ports, creating and distributing my custom made library and designing class hierarchy trees to efficiently extend functionalities.
I have experience in using Git extensively for personal notes and projects as well as any other files that I do not want to lose. I have even created a PowerPoint presentation introducing the ideas of version control and Git that I have presented to friends and classmates. I use Git both with Github and with creating private repositories that I put on Dropbox. In addition to Git I have some experience working with Mercurial from contributing to python core, however, I am much more comfortable with Git.
Sympy Use/Involvement
Sympy drew me in with its ability to compute and create expressions
symbolically for design purposes. To show this I will expand on the simple
mass, spring, damper example in LagrangesMethod
's docstring with a simple
input force, F which produces the following output.
>>> from sympy import *
>>> from sympy.physics.mechanics import LagrangesMethod, Lagrangian
>>> from sympy.physics.mechanics import ReferenceFrame, Particle, Point
>>> from sympy.physics.mechanics import dynamicsymbols, kinetic_energy
>>> q = dynamicsymbols('q')
>>> qd = dynamicsymbols('q', 1)
>>> m, k, c, F = symbols('m k c F')
>>> N = ReferenceFrame('N')
>>> P = Point('P')
>>> P.set_vel(N, qd * N.x)
>>> Pa = Particle('Pa', P, m)
>>> Pa.potential_energy = k * q**2 / 2.0
>>> L = Lagrangian(N, Pa)
>>> fl = [(P, -c * qd * N.x), (P, F * N.x)]
>>> l = LagrangesMethod(L, [q], forcelist=fl, frame=N)
>>> l.form_lagranges_equations()
>>> print(l.rhs())
Matrix([Derivative(q(t), t)], [(F - c*Derivative(q(t), t) - 1.0*k*q(t))/m](/wrat/sympy/wiki/Derivative(q(t),-t)],-[(F---c*Derivative(q(t),-t)---1.0*k*q(t))/m))
This set of equations naturally lends to controls analysis because in order to
better design control laws the dynamics of the system need to be known. With
some simple by hand re-arranging the output from LagrangesMethod
can be put
into state space form (xdot = Ax + Bu). Now with sympy the controllability of
the system and the natural system response can be determined.
>>> A = Matrix([0, 1], [-k/m, -c/m](/wrat/sympy/wiki/0,-1],-[-k/m,--c/m))
>>> B = Matrix([0, 1/m])
>>> M = Matrix([B.transpose(), (A*B).transpose()]).transpose()
>>> print(M.rank())
2
>>> eigA = A.eigenvals()
>>> print(eigA)
{-c/(2*m) + sqrt(c**2 - 4*k*m)/(2*m): 1, -c/(2*m) - sqrt(c**2 - 4*k*m)/(2*m): 1}
The system is full rank (rank = the number of states, x) and is therefore controllable by the input force. Also the eigenvalues are solved symbolically and this is where sympy shines over numeric tools such as scipy. As a controls engineer, the system response due to choice of damping and spring stiffness for a given mass are clearly visible in this symbolic representation, allowing for a more streamlined design stage. Obviously this is a simple example but it is these sort of uses that drew me to sympy and is the reason I wish to contribute to its development.
Pull Requests:
- Tutorial Documentation Fix [Merged]
- Docstring Typo Fix in init_printing() [Merged]
- Pass Arbitrary Inputs from init_printing to the printer [Merged]
The Project
For the project I propose working on creating a base class for the equations of motion generators in the physics module and add a Newton-Euler equation of motion generator. The benefits of creating a base class for equations of motion generators would include ease of adding additional equations of motion generators, making the code more compact and so if an issue is found it can be fixed for all methods simultaneously and lastly any speed enhancements of the equations of motion generators would also affect all generators simultaneously rather than having to be implemented on a per generator basis. In addition to this work I would also be working on increasing the speed/efficiency of the existing python code for the equations of motion generators since I will already be digging into the base functionality of the generators.
Just last semester I took a graduate level course on analytical dynamics during which we learned the Newton-Euler and Lagrange methods of equation of motion generation. This will allow me to develop the Newton-Euler equation of motion generator in addition to having the base knowledge to be able to work with the Lagrange method and learn additional methods.
Another qualification I have is that I have experience in refactoring and making base classes to organize shared functionality from my work with python core and from the library I built to work with pygame. When working with python core I altered the base class for the Windows web browser and subclassed it to allow specific browsers (ie. Google Chrome, Firefox) to pass their own flags. With my work on my personal library I created a base class for screens and subclassed it to make menus, or a screen that read like a book. This prior work with subclassing aid greatly as I abstract the equations of motion generators in order to form a generic base class.
Lastly I have implemented python's multiprocessing capabilities while coding numerical methods. This is experience I can draw upon as I work to increase the speed of the python code any where that seems inherently parallelizable.
My background and qualifications directly point towards this specific project and is why I am excited to attempt to implement it. I will be able to put in-class theoretical learning to application and work towards creating a structure that promotes code growth. In addition, my mechanical engineering background includes an emphasis in control systems and so the equations of motion generators would model the dynamics of the system and streamline the control design.
No previous work has been done in creating a shared base class for the equations of motion generators, implementing a Newton-Euler equation of motion generator or specifically enhancing the run time of the python code of the existing equations of motion generators. There are existing equations of motion generators, however, that utilize Kane's and Lagrange's methods for equation of motion generation and this will be the starting point for the project.
I have already run a profiler on the n_link_pendulum_on_cart()
function found
in the pydy source as it makes use of sympy's KanesMethod
class. With this
profile I can prove that the work on speeding the code up for the equations of
motion generators will have an impact across all of sympy rather than just the
physics module. For instance, while running kanes_equations
the code spent
about 45% of its time in the sympy/matrices module. Therefore in order to speed
up the equations of motion generator the other parts of sympy will need to be
made to run more efficiently as well.
Before the project officially begins I will be spending time as I can while school is still in session. The school semester is completely over before May begins, however, and so I would be able to essentially start my project early by about two to three weeks. During this time, I would be familiarizing myself with Kane's method of equation of motion generation and beginning work in creating a benchmarking tool for use during the project.
During the project I have no other obligations and will be able to put in 40+ hours each week. I am thinking about taking a trip back home around the 4th of July, however, I will still be able to get work done and so this would not create a huge disturbance in the amount of weekly hours I put in.
After the summer ends I will be beginning my final semester of graduate school and so the amount of time I could continue to devote to sympy would be limited. This limitation would only last until December though and I would be able to more regularly return at the beginning of the new year.
*Comment from Jason: Please show some psuedo code or what kinds of classes/emethods you want to add. Show us that you've looked at and thought about our existing code. How will your Newton Euler class look and work? What may example code look like for a simple system? What are your plans for speeding up the code? What have your learned? What will the general EOM method class look like? How will it interact with PyDy.system.System?
Be sure to submit this application to both SymPy and the PSF (PyDy sub-org).
Timeline
Week 1 [May 23rd - May 30th]
-
Create benchmarking tool to track project progress
- The tool will run test code and simulations using both
KanesMethod
andLagrangesMethod
and save the results in an SQLite data base where each table represents a different test - In addition to the speed results, each row in the table will include information on who ran the tests (probably see if I can access Github username for this), what operating system was used and hardware details of the computer used (CPU speed and RAM)
- A profile of portions of the tested code will be saved in a subfolder
- A viewer of the results would be made using the matplotlib library
- Sub options for viewing results will be added as time allows
- Tool will be made such that it is easily extendable
- Can be used by other members of the community for their projects
- Can be used with the
NewtonEuler
class that will be created later in the project
- PR: Resulting tool could be introduced as a pull request depending on whether or not this is desired in the main code base
- The tool will run test code and simulations using both
-
Learn and practice Kane's method of generating equations of motion so that I will have a better understanding of how the code works
-
Note: The items listed for week 1 would begin at the beginning of May much before the project officially begins
Week 2, 3 [May 30th - June 13th]
-
Begin going through internals of the
KanesMethod
andLagrangesMethod
to determine what the commonalities are that could be drawn from a base class -
Create a basic implementation of the base class from which
KanesMethod
andLagrangesMethod
would be subclasses- PR: During the design stage of the base class the test code will be written and pull request will be made
Week 4, 5 [June 13th - June 27th]
-
Continue working out and condensing code to complete the base class for the equations of motion generators
- Keeping attention to enhancing speed and performance of code during consolidation
- PR: Would be a good time to create a pull request for the base class
-
Develop the supporting documentation for the base equations of motion generation class
- PR: Submit the documentation for the base class
-
Complete the midterm evaluation with the functioning base class, test code for the base class and the supporting documentation as the deliverable
Week 6, 7 [June 27th - July 11th]
- Look into best practices for coding a
NewtonEulerMethod
class and begin creation of that class. This will be a general design period and as the desired api is developed, test code will be written to verify the api.- PR: Turn in the test code from the design stages of
NewtonEulerMethod
- PR: Turn in the test code from the design stages of
Week 8, 9 [July 11th - July 25th]
-
Complete
NewtonEulerMethod
class- PR: This would be a good time for a pull request of the new method
-
Update the documentation explaining the new method
- PR: Submit the updated documentation for
NewtonEulerMethod
- PR: Submit the updated documentation for
Week 10 - 13 [July 25th - August 23rd]
-
Finish any documentation or code that is still needing to be completed
-
Continue to refactor and improve the quality of the code written for the project
-
By this point a complete understanding of the code underlying the methods should be obtained and thus effort towards performance enhancement and speed would return the greatest results
-
Make final preparations for the final evaluation
- Final deliverables would include
- Equations of motion generator base class
- Newton-Euler equations of motion generator class
- Benchmarking results
- Final deliverables would include
Note: Any spare time during the weeks will be used towards speeding up the python code.
Comment from Jason: This looks reasonable. Plan for test driven development, atomic pull requests that add in minimally viable features that include tests, code, and docs.
Comments
(This section will be removed from the final application)