Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prototype System class. #81

Merged
merged 18 commits into from
Aug 7, 2014
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions examples/double_pendulum/double_pendulum.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
KM = KanesMethod(N, q_ind=[q1, q2], u_ind=[u1, u2], kd_eqs=kd)


(fr, frstar) = KM.kanes_equations(FL, BL)
kdd = KM.kindiffdict()
mass_matrix = KM.mass_matrix_full
forcing_vector = KM.forcing_full
qudots = mass_matrix.inv() * forcing_vector
qudots = qudots.subs(kdd)
qudots.simplify()

KM.kanes_equations(FL, BL)
#kdd = KM.kindiffdict()
#mass_matrix = KM.mass_matrix_full
#forcing_vector = KM.forcing_full
#qudots = mass_matrix.inv() * forcing_vector
#qudots = qudots.subs(kdd)
#qudots.simplify()
#
41 changes: 7 additions & 34 deletions examples/double_pendulum/simulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,17 @@

from double_pendulum import *

# List the symbolic arguments
# ===========================

# Constants
# ---------

constants = {l: 10.0, m: 10.0, g: 9.81}

# Time-varying
# ------------

coordinates = [q1, q2]

speeds = [u1, u2]


# Generate function that returns state derivatives
# ================================================

xdot_function = generate_ode_function(mass_matrix, forcing_vector,
constants.keys(), coordinates, speeds)
initial_conditions = {q1: 1.0, q2: 0.0, u1: 0.0, u2: 0.0}


# Specify numerical quantities
# ============================

initial_coordinates = [1.0, 0.0]
initial_speeds = [0.0, 0.0]
x0 = concatenate((initial_coordinates, initial_speeds), axis=1)

args = {'constants': constants.values()}


# Simulate
# ========
sys = System(KM, constants=constants,
initial_conditions=initial_conditions)

frames_per_sec = 60
final_time = 5.0

t = linspace(0.0, final_time, final_time * frames_per_sec)
x = odeint(xdot_function, x0, t, args=(args,))
times = linspace(0.0, final_time, final_time * frames_per_sec)

x = sys.integrate(times)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I had been implying for re-integrating Scene visualization stuff.
So that we supply times and sys to the Scene class, and is easier to handle than the existing structure.
I am ready to work on integrating this code with Scene class(after tomorrow).

But also there will be some backwards incompatible changes(in Scene class as well), when this is integrated into Scene.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that is why we are creating this, so that you can have something easier to work with in your viz code. It won't be ready tomorrow, but please help contribute to this so we can get it done more quickly.


5 changes: 2 additions & 3 deletions examples/double_pendulum/visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,13 @@
# this, we create a world frame that is rotated +90 degrees about the N frame's
# z direction.
world_frame = N.orientnew('world', 'Axis', [0.5 * pi, N.z])
scene = Scene(world_frame, O,
scene = Scene(sys, world_frame, O,
linkP_viz_frame, linkR_viz_frame, sphereP_viz_frame, sphereR_viz_frame)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way, that we can use System to eliminate the ReferenceFrame and Point argument supplied here.
Maybe they can be saved in System class too?

so that all we need to do is:

scene = Scene(sys, vframes_as_a_list_or_tuple)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be nice to clean up the Scene constructor. However, I am not sure this is a good idea. Those two arguments are specific to how the visualization looks, and they have been useful for me before. We can consider this again down the line when System becomes bigger and potentially manages the viz info. But for now (this PR), System does not handle viz info.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you have to have the option of providing any frame or point as the base viz frame. You could snatch the base frame from the Methods as a default frame, and maybe you could go through all the points stored in the Methods class and find one that has zero velocity in the base frame as a default.



# Create the visualization
# ========================

scene.generate_visualization_json(coordinates + speeds, constants.keys(), x,
constants.values())
scene.generate_visualization_json(times)

scene.display()
18 changes: 15 additions & 3 deletions pydy/codegen/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import sympy.physics.mechanics as me


def generate_mass_spring_damper_equations_of_motion(external_force=True):
def generate_mass_spring_damper_equations_of_motion(external_force=True,
only_return_kane=False):
"""Returns the symbolic equations of motion and associated variables for a
simple one degree of freedom mass, spring, damper system with gravity
and an optional external specified force.
Expand All @@ -26,8 +27,16 @@ def generate_mass_spring_damper_equations_of_motion(external_force=True):
| F
V

Parameters
----------
only_return_kane : bool, optional (default: False)
If True, this method only returns the KanesMethod object.

Returns
-------
kane : KanesMethod
If the keyword argument `only_return_kane` is True, this is the only
object returned.
mass_matrix : sympy.MutableMatrix, shape(2,2)
The symbolic mass matrix of the system which are linear in
derivatives of the states.
Expand Down Expand Up @@ -90,8 +99,11 @@ def generate_mass_spring_damper_equations_of_motion(external_force=True):
else:
specified = None

return (mass_matrix, forcing_vector, constants, coordinates, speeds,
specified)
if only_return_kane:
return kane
else:
return (mass_matrix, forcing_vector, constants, coordinates, speeds,
specified)


def generate_n_link_pendulum_on_cart_equations_of_motion(n, cart_force=True,
Expand Down
Loading