GSOC 2016: Classical Mechanics: Efficient Equation of Motion Generation with C++

417 views
Skip to first unread message

Aravind Reddy

unread,
Mar 3, 2016, 9:28:24 AM3/3/16
to sympy
Hi all,

I am Aravind from IIIT-Hyderabad, India. I am new to open source contribution. From last week I have been working the issues of sympy in github. I will be applying to GSoC this year from Sympy. I have gone through various topics mentioned in the Ideas Page. Out of all I liked Classical Mechanics: Efficient Equation of Motion Generation with C++ more. I have a very good knowledge of both Python and C++ languages and would love to know more if the project demands. Out of both I like C++ more. 

As I pointed before, I am new to open source community. Also it is mentioned in the ideas page to ask in the group about the previous work done on this. So please anyone kindly help me in where and how to start and also about the previous work done on SymEngine. 

Thank you,
Aravind

Jason Moore

unread,
Mar 3, 2016, 9:38:59 AM3/3/16
to sy...@googlegroups.com
Aravind,

In sympy.physics.mechanics we have the KanesMethod and LangrangesMethod classes. For large problems with many degrees of freedom these take a substantial amount of time to compute. There is a N-link pendulum model in the PyDy package that can easily show this. See the graph near the bottom of this post for Symbolic derivation time:

http://www.moorepants.info/blog/pydy-code-gen.html

I would recommend starting by profiling the n-link pendulum code to find out what core sympy functions take the most time. Once you have an idea of this you can see whether SymEngine has this functionality. If it doesn't you can work on adding some of the functionality. Once it has functionality you can work on demonstrating how to swap out the core sympy routines with the symengine ones to see what kind of performance gain you can get. You'll need to work with the SymEngine team on their plan to make wrappers available for use in SymPy. See the SymEngine mailing list for a discussion about this topic.

One thing that definitely needs to be sped up is taking the jacobian of matrices with big expressions. This is required for the linearizion methods in sympy.physics.mechanics. Ondrej has done some benchmark work on this topic in SymEngine that you should investigate.

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/b970d644-dc1f-4f6b-b1c9-1fd5dd612f85%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Aravind Reddy

unread,
Mar 3, 2016, 12:45:54 PM3/3/16
to sympy
Jason,

Thank you for the guidance. I 'll start the work as soon as possible.

Isuru Fernando

unread,
Mar 4, 2016, 6:11:59 AM3/4/16
to sy...@googlegroups.com, syme...@googlegroups.com
Hi,

Some benchmark times for jacobian from SymEngine are here, https://github.com/symengine/symengine/pull/580. As you can see, it is faster than SymPy considerably.

First step would be to figure out what functions from sympy, outside of sympy.physics is called from running the n-link pendulum code. Then we can figure out which methods have to be implemented in symengine.

Link Jason gave has lots of broken links. Here's the benchmark 

On Thu, Mar 3, 2016 at 11:15 PM, Aravind Reddy <aravind...@gmail.com> wrote:
Jason,

Thank you for the guidance. I 'll start the work as soon as possible.

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.
Message has been deleted

Aravind Reddy

unread,
Mar 5, 2016, 2:02:56 PM3/5/16
to sympy
Isuru Fernando, 

Sir,
 Thanks for your valuable guidance. As you suggested I have gone through the generate_n_link_pendulum_on_cart_equations_of_motion() function to see whether it is calling any functions other than those that are in sympy.physics module. All I observed is that, in the code itself, it is not calling any other functions. But there may be a chance that the functions called here like kanes_equation() may call the functions we are looking for. So now I am looking for such dependencies. Sir please correct me if I am in a wrong way. 

Thank you, 
Aravind

Jason Moore

unread,
Mar 5, 2016, 3:22:27 PM3/5/16
to sy...@googlegroups.com
Aravind,

You need to learn about using a profiler to see which code paths take the most time. Python has a built-in profiler and i like visualizing that with snakeviz: https://jiffyclub.github.io/snakeviz/. Pyinstrument is really nice too. It gives you a different view.

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.

Jason Moore

unread,
Mar 5, 2016, 3:42:58 PM3/5/16
to sy...@googlegroups.com
For example, I created a file called profile_kane.py with this contents:

from pydy.models import n_link_pendulum_on_cart
sys = n_link_pendulum_on_cart(10)

I then profile it with pyinstrument using:

python -m pyinstrument -o profile.html --html profile_kane.py

This creates an HTML file, which I've attached that shows the time spent in the full call stack. You can see the _form_fstar takes most of the time in the kanes_equations method. There are a variety of core sympy functions being called in there that account for this time. You should work to identify the most time consuming function calls. In particular for this project we want to identify the core parts of sympy that are slow and think about how we can use SymEngine to speed them up.
profile.html
Message has been deleted

Aravind Reddy

unread,
Mar 7, 2016, 1:12:25 PM3/7/16
to sympy
Jason,
Sir, thanks for your help.I have gone through profiling in python. I felt snakeviz more comfortable. Using that I have got the following considerable remarks. 

Isuru Fernando, 
Sir, you gave me an advice to start by profiling the n-link-pendulum-code. I found that there are two such functions which are generating EOM for the n-link-pendulum problem in two different packages. One is in pydy-code-gen and the other in pydy. Now I took n, the number of links in pendulum as 10, 20, 30 one at a time and ran both the codes. I got the times as                                                                                                                       
A) n_link_pendulum_on_cart: 19s(10), 160s(20), 671s(30)
B) generate_n_link_pendulum_on_cart_equations_of_motion: 17.3s(10), 155s(20), 655s(30)

Time Taking Functions Analysis
_form_frstar function taking the most time in both the functions. 
A) 15.3s(10), 135s(20), 589s(30)
B) 13.9s(10), 134s(20), 581s(30)

Functions called that are out of sympy.physics
Majorly I have observed that the function binary_op_wrapper() from sympy.core,  functions __mul__(), applyfunc() and diff() from sympy.matrices modules are used the most. Now I got an exact idea of what I am doing and what should I do? can you guide me how to continue with this?

Thank you,
Aravind

Jason Moore

unread,
Mar 7, 2016, 1:28:15 PM3/7/16
to sy...@googlegroups.com
Nice work. Now you can investigate the core functions for speed. You should investigate whether SymPy supports all of these functions. I would read the symengine list to get an idea of how symengine will be used in SymPy.

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.
Message has been deleted
Message has been deleted
Message has been deleted

Aravind

unread,
Mar 10, 2016, 12:20:38 AM3/10/16
to sympy
Jason, 

I am stuck with my work. Can you please help me to gear up? As my project deals with Symengine, I need to start working on that. As mentioned in the ideas page, 
This project would be dedicated to ensuring SymEngine worked with all operations typically used in the sympy.physics.vector and sympy.physics.mechanics packages. So I need to start in that way, so that I can get a clear overview of how to complete the project in the prescribed time limit. And also it helps me to make a clear timeline of how to complete my work. 
Thank you, 
Aravind

Isuru Fernando

unread,
Mar 15, 2016, 10:26:16 AM3/15/16
to syme...@googlegroups.com, sy...@googlegroups.com
Hi,

You have to figure out which methods from sympy core and matrices are called and check if SymEngine has implemented those. If not, then you can start implementing them in SymEngine.

Isuru Fernando

On Fri, Mar 11, 2016 at 4:18 PM, Aravind Reddy <aravind...@gmail.com> wrote:
Sir, I have been working on this project. Now I have found various functions that have been called from sympy that are not in sympy.physics module when n-link-pendulam code was run. These are majorly from sympy.core and sympy.matrices module. Now what should I do sir to get a clear picture of whole work? Please guide me sir.

Thank you, 
Aravind

--
You received this message because you are subscribed to the Google Groups "symengine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to symengine+...@googlegroups.com.
To post to this group, send email to syme...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/symengine/5d1be54a-63eb-49d3-b02c-98cd72b46180%40googlegroups.com.

Jason Moore

unread,
Mar 15, 2016, 6:47:19 PM3/15/16
to syme...@googlegroups.com, sy...@googlegroups.com
Maybe you can list all of the slow functions in sympy.core and sympy.matrices and then find out if they exist in SymEngine as a next step.

Aravind

unread,
Mar 21, 2016, 2:22:24 AM3/21/16
to sympy
Sir, I would like to work for the following project which is a combination of two single projects. Some of the sympy members warned me that it may not get completed in time of 3 months. But with a clear timeline and plan I have divided my work so that, the final deliverable will be submitted within the prescribed time. This is my draft proposal. Please try to review it and give your valuable suggestions. The experience which I got from these days forced me to dot his project rather than the symengine one. 

Thank you, 
Aravind

Jason Moore

unread,
Mar 21, 2016, 4:43:38 PM3/21/16
to sy...@googlegroups.com
I commented in your google doc draft. Thanks.

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.

Aravind

unread,
Apr 14, 2016, 4:04:55 AM4/14/16
to sympy
Sorry for being passive these days. I was busy with some course projects and am done. I have gone through the two classes again to recall everything. I have a small doubt, a conceptual one. Can you please explain me the the difference between argument qs in LagrangesMethod and q_ind & q_dep in KanesMethod?

Aravind

unread,
Apr 17, 2016, 6:24:19 AM4/17/16
to sympy
I have done some study on Lagranges EOM. As per my knowledge, the coordinates in Lagrangian system are independent coordinates. So then to construct a new abstract base class, one can take q_ind and q_dep as parameters without any qs. Please help me and guide whether I am in a correct path or not. 

regards, 
Aravind

Aravind

unread,
Apr 17, 2016, 8:23:46 AM4/17/16
to sympy
Hello,

both the classes KanesMethod and LagrangesMethod uses forcelist attribute. Right now for LagrangesMethod, we are providing forcelist at the time of object creation(optional though) where as for KanesMethod it is done when we call kanes_equations() method. Actually for KanesMethod also we can provide forcelist and bodylist at the time of instance creation. This helps in having a property forcelist() in the abstract class. I have written an exclusive small code for this. Please review this and provide your inputs. 

regards, 
Aravind

Aravind

unread,
Apr 19, 2016, 1:30:33 PM4/19/16
to sympy
Sir, I don't know who is going to mentor this project. So please try to help me as this is an important doubt. I have tried several ways. Now I am finally continuing with the idea I have mentioned above. So please try to correct me if I am in wrong way and wasting my time.

Thank you, 
Aravind

Jason Moore

unread,
Apr 25, 2016, 7:17:33 PM4/25/16
to sy...@googlegroups.com
One unique thing about Kane's Method is that you can specify the definitions of the generalized speeds to be something other than u = q'. This can simplify the equations of motion significantly in many cases. So I think the LangrangesMethod class can simply output u = q' and KanesMethod can output u = f(q, q').
On Thu, Apr 14, 2016 at 1:04 AM, Aravind <aravind...@gmail.com> wrote:
Sorry for being passive these days. I was busy with some course projects and am done. I have gone through the two classes again to recall everything. I have a small doubt, a conceptual one. Can you please explain me the the difference between argument qs in LagrangesMethod and q_ind & q_dep in KanesMethod?

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.

Jason Moore

unread,
Apr 25, 2016, 7:18:50 PM4/25/16
to sy...@googlegroups.com
I personally think the API should be that we provide the force list at object creation. I'm not a fan of the .kanes_equations() api.

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.
Reply all
Reply to author
Forward
0 new messages