generate_ode_function

70 views
Skip to first unread message

Anita Le Mair

unread,
Feb 17, 2016, 1:40:38 PM2/17/16
to PyDy
Hello Jason,

As a start to understand Pydy, I have created a very simple pendulum.

I have started very simple, with a 1 DOF, single body (a particle on distance r) pendulum. I am able to generate my mass matrix and forcing vector, from the equation of Kane, and I have entered them into the function.

My arguments are as follows (and are correct):
mass_matrix = matrix([[m*r**2]])
forcing_vector = matrix([[m*g*r*sin(q(t))]])
constants = [m g r]
coordinates = [q(t)]
speeds [u(t)]

I have entered them as follows:
RHS = generate_ode_function(mass_matrix, forcing_vector, constants, coordinates, speeds)

Unfortunately, the function  generate_ode_function does not work. The error that I get is: “ AttributeError: 'list' object has no attribute ‘shape’ “ referring to the mass_matrix doing a consistency check. I am having troubles finding out what the reason is, since there are no requirements on the order of entering the arguments. Do you know what I have done wrong?

Furthermore, I wanted to ask two questions:
- does the function work with just one body?
- which requirements there are on the description of the arguments. Do you always need to refer to “mass_matrix” etc or can a name as MM can be entered as well?

With kind regards,
Anita Le mair

Slinger 3.py

Jason Moore

unread,
Feb 17, 2016, 2:27:44 PM2/17/16
to py...@googlegroups.com
Anita,

You just about have everything correct. The issue here is that you are passing the mass matrix and forcing vector without passing any information about the kinematical differential equations. For the EoMs to be in first order form you need to pass in these equations. So line 55 should be:

RHS = generate_ode_function(forcing_vector, coordinates, speeds, constants,
                            mass_matrix=mass_matrix,
                            coordinate_derivatives=Matrix([u]))

Note the order here:

http://pydy.readthedocs.org/en/stable/codegen/api.html#pydy.codegen.ode_function_generators.ODEFunctionGenerator

And note that you have form #3:

http://pydy.readthedocs.org/en/stable/codegen/api.html#pydy.codegen.ode_function_generators.generate_ode_function

I fixed a few other minor errors too. I've attached the edited file.


- does the function work with just one body?

You can have as many bodies in the system as you want.

- which requirements there are on the description of the arguments. Do you always need to refer to “mass_matrix” etc or can a name as MM can be entered as well?

You can choose any names you want. The variable names (left side of equals sign) can be whatever you like in Python.

Let me know if you need any other tips. I'm teaching a mechanics course right now with PyDy too.

Ook, Ik spreek en beetje Nederlands, zo Ik begrijpt uw commentaren.

--
You received this message because you are subscribed to the Google Groups "PyDy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pydy+uns...@googlegroups.com.
To post to this group, send email to py...@googlegroups.com.
Visit this group at https://groups.google.com/group/pydy.
For more options, visit https://groups.google.com/d/optout.

slinger3.py

Anita Le Mair

unread,
Feb 17, 2016, 3:26:51 PM2/17/16
to PyDy
Hi Jason,

Thank you for your response. I have one last question:

I have tried entering the full mass matrix and full forcing vector, but that did not seem to work. What is the difference between this solution and entering full matrices?

Anita

Jason Moore

unread,
Feb 17, 2016, 3:42:58 PM2/17/16
to py...@googlegroups.com
If you provide the full mass matrix and the full forcing vector then the numerical algorithm in the evaluation of the right hand side will have to effectively invert the full mass matrix (mass matrix augmented with kdes). But if you've already "inverted" the kinematical differential equations (in this case you have), then you only have to invert the dynamical mass matrix. So you can save some computation time if you provided in form #3. This is especially helpful for large degree of freedom systems. All of this is not important for many small DoF problems. For small problems like this, just call KanesMethod.rhs() to symbolically invert and just pass this result as the "forcing vector" and don't worry about the mass matrix.

Jason
Reply all
Reply to author
Forward
0 new messages