Skip to content
This repository

Kinetic and potential energy functionality. #1407

Merged
merged 1 commit into from over 1 year ago

9 participants

Angadh Nanjangud Don't Add Me To Your Organization a.k.a The Travis Bot Stefan Krastanov Ondřej Čertík Jason K. Moore Aaron Meurer Gilbert Gede Bharath M R Thomas Hisch
Angadh Nanjangud

Also some minor edits to momenta functionality.

Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request passes (merged 71221bfd into 48f42f8).

Stefan Krastanov
Collaborator

SymPy Bot Summary: :eight_spoked_asterisk: All tests have passed.

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sY5oMeDA

Interpreter: /opt/pym32/bin/python2.5 (2.5.6-final-0)
Architecture: Linux (32-bit)
Cache: yes
Test command: setup.py test
master hash: 48f42f8
branch hash: 71221bf

Automatic review by SymPy Bot.

Stefan Krastanov
Collaborator

SymPy Bot Summary: :red_circle: There were test failures.

@angadhn: Please fix the test failures.

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYv-QdDA

Interpreter: /usr/bin/python2.7 (2.7.3-candidate-2)
Architecture: Linux (64-bit)
Cache: yes
Test command: setup.py test
master hash: 48f42f8
branch hash: 71221bf

Automatic review by SymPy Bot.

Stefan Krastanov
Collaborator

SymPy Bot Summary: :red_circle: There were test failures.

@angadhn: Please fix the test failures.

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYopseDA

Interpreter: /usr/bin/python3 (3.2.3-candidate-2)
Architecture: Linux (64-bit)
Cache: yes
Test command: setup.py test
master hash: 48f42f8
branch hash: 71221bf

Automatic review by SymPy Bot.

Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request passes (merged b411038f into 48f42f8).

Stefan Krastanov
Collaborator

SymPy Bot Summary: :eight_spoked_asterisk: All tests have passed.

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYqMUdDA

Interpreter: /opt/pym32/bin/python2.5 (2.5.6-final-0)
Architecture: Linux (32-bit)
Cache: yes
Test command: setup.py test
master hash: 48f42f8
branch hash: e831b7a

Automatic review by SymPy Bot.

Stefan Krastanov
Collaborator

SymPy Bot Summary: :red_circle: There were test failures.

@angadhn: Please fix the test failures.

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYo5seDA

Interpreter: /usr/bin/python2.7 (2.7.3-candidate-2)
Architecture: Linux (64-bit)
Cache: yes
Test command: setup.py test
master hash: 424160a
branch hash: e831b7a

Automatic review by SymPy Bot.

Stefan Krastanov
Collaborator

SymPy Bot Summary: :red_circle: There were test failures.

@angadhn: Please fix the test failures.

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sY27UdDA

Interpreter: /usr/bin/python3 (3.2.3-candidate-2)
Architecture: Linux (64-bit)
Cache: yes
Test command: setup.py test
master hash: 424160a
branch hash: e831b7a

Automatic review by SymPy Bot.

sympy/physics/mechanics/functions.py
... ...
@@ -557,3 +561,121 @@ def angularmomentum(bodylist, point, frame):
557 561
                 raise TypeError('Body list must have only Particle'
558 562
                         'or RigidBody')
559 563
     return angularmomentum_sys
  564
+
  565
+def kinetic_energy(bodylist, frame):
  566
+    """Kinetic energy of a multibody system.
  567
+
  568
+    This function returns the kinetic energy of a system of Particle's and/or
  569
+    RigidBody's. The kinertic energy of such a system is equal to the sum of
1

kinertic -> kinetic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
sympy/physics/mechanics/functions.py
((52 lines not shown))
  612
+    if not isinstance(bodylist, list):
  613
+        raise TypeError('System elements must be supplied as a list')
  614
+    else:
  615
+        KE_sys = 0
  616
+        for e in bodylist:
  617
+            if isinstance(e, (RigidBody, Particle)):
  618
+                KE_individual = e.kinetic_energy(frame)
  619
+                KE_sys = KE_sys + KE_individual
  620
+            else:
  621
+                raise TypeError('Body list must have only Particle'
  622
+                        'or RigidBody')
  623
+    return KE_sys
  624
+
  625
+def potential_energy(bodylist):
  626
+    """Kinetic energy of a multibody system.
  627
+
6

different docstring?

Angadh Nanjangud
angadhn added a note July 08, 2012

hah! embarrassing! thanks for the catch(es)!

Thomas Hisch
thisch added a note July 08, 2012

what about two body potentials (e.g. Coulomb potential) ? Your implementation only supports one body potentials, right ?

Angadh Nanjangud
angadhn added a note July 08, 2012

Yes, that is correct. This functionality is primarily geared towards mechanical systems. The idea is to use the energy functions to determine the Lagrangian of any mechanical system and finally use Lagrange's method to derive the equations of motion for said mechanical system.

Thomas Hisch
thisch added a note July 10, 2012

Anyway, in the future it would be great to extend the potential implementation to treat two body potentials :)

Angadh Nanjangud
angadhn added a note July 10, 2012

well, it can still handle 'coulomb potential' (electric potential due to a point charge). The function doesn't determine the potential energy but accepts a user defined function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
sympy/physics/mechanics/rigidbody.py
((29 lines not shown))
  208
+        >>> from sympy import symbols
  209
+        >>> M, v, r, omega = symbols('M v r omega')
  210
+        >>> N = ReferenceFrame('N')
  211
+        >>> b = ReferenceFrame('b')
  212
+        >>> b.set_ang_vel(N, omega * b.x)
  213
+        >>> P = Point('P')
  214
+        >>> P.set_vel(N, v * N.x)
  215
+        >>> I = outer (b.x, b.x)
  216
+        >>> Inertia_tuple = (I, P)
  217
+        >>> B = RigidBody('B', P, b, M, Inertia_tuple)
  218
+        >>> B.kinetic_energy(N)
  219
+        M*v**2/2 + omega**2/2
  220
+
  221
+        """
  222
+
  223
+        return (((self.frame.ang_vel_in(frame) & (self.inertia[0] &
2

I guess splitting it as assignments and then rewriting will be better because its too long an expression.

Angadh Nanjangud
angadhn added a note July 08, 2012

Noted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request passes (merged 9395da38 into 424160a).

Stefan Krastanov
Collaborator

SymPy Bot Summary: :eight_spoked_asterisk: All tests have passed.

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYweQdDA

Interpreter: /opt/pym32/bin/python2.5 (2.5.6-final-0)
Architecture: Linux (32-bit)
Cache: yes
Test command: setup.py test
master hash: 424160a
branch hash: 9395da3

Automatic review by SymPy Bot.

Stefan Krastanov
Collaborator

SymPy Bot Summary: :red_circle: There were test failures.

@angadhn: Please fix the test failures.

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sY2LIeDA

Interpreter: /usr/bin/python2.7 (2.7.3-candidate-2)
Architecture: Linux (64-bit)
Cache: yes
Test command: setup.py test
master hash: 424160a
branch hash: 9395da3

Automatic review by SymPy Bot.

Stefan Krastanov
Collaborator

SymPy Bot Summary: :red_circle: There were test failures.

@angadhn: Please fix the test failures.

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYj80dDA

Interpreter: /usr/bin/python3 (3.2.3-candidate-2)
Architecture: Linux (64-bit)
Cache: yes
Test command: setup.py test
master hash: 424160a
branch hash: 9395da3

Automatic review by SymPy Bot.

Ondřej Čertík
Owner

What is the status of this pull request? Is it ready to be merged?

Ondřej Čertík
Owner

@angadhn said privately "As for PR 1407, I think it's good to be merged unless anyone has any objections with the docstrings.", so I am running tests on this and if it passes, merge it.

sympy/physics/mechanics/functions.py
... ...
@@ -11,7 +11,9 @@
11 11
            'inertia_of_point_mass',
12 12
            'partial_velocity',
13 13
            'linearmomentum',
14  
-           'angularmomentum']
  14
+           'angularmomentum',
2
Jason K. Moore Collaborator

all of the other functions use an underscore as PEP8's suggestion. So why not angular_momentum and linear_momentum?

Ondřej Čertík Owner
certik added a note August 03, 2012

I agree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Ondřej Čertík
Owner

SymPy Bot Summary: :eight_spoked_asterisk: All tests have passed (merged angadhn/dev (f69c4a2) into master (9625918)).

Interpreter 1: :eight_spoked_asterisk: All tests have passed.
Interpreter: /usr/bin/python (2.7.3-final-0)
Architecture: Linux (64-bit)

sympy/physics/mechanics/functions.py
((6 lines not shown))
484 486
     10*N.x + 500*N.y
485 487
 
486 488
     """
487 489
 
  490
+    bodylist = list(bodylist)
488 491
     if not isinstance(bodylist, list):
6
Jason K. Moore Collaborator

What's up with this? If you always convert bodylist to a list, then how will your "not and instance" ever checkout?

Angadh Nanjangud
angadhn added a note August 03, 2012

If users mistakenly pass a tuple for the bodylist in place of a list then list(bodylist) will change it to a list. If they pass a list, then it won't matter, Any other data type should bring the 'not isinstance' to the fore.

Jason K. Moore Collaborator

but you can turn all kinds of things into lists, like:

>>> list('test')
['t', 'e', 's', 't']

There are all kinds of sequences (lists, tuples, sets, etc) and all will turn into lists. So checking whether something is a list doesn't help that much. Your function shouldn't care whether something is a list. It just needs a group of 1 to N particles and bodies. You can group them in multiple ways. It should check for particles and bodies but not worry too much about how you pass them in.

Aaron Meurer Owner
asmeurer added a note August 03, 2012

The not isinstance block will never be run. list either returns a list or raises an exception. It never returns something that is not a list.

Also, why does it need to be a list in the first place? It looks like all you're doing is iterating through the elements. That will work just the same if a tuple is left as a tuple.

Ondřej Čertík Owner
certik added a note August 07, 2012

@angadhn, this comment still applies I think.

Angadh Nanjangud
angadhn added a note August 08, 2012

same as below. addressed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
sympy/physics/mechanics/functions.py
((6 lines not shown))
544 547
     10*N.z
545 548
 
546 549
     """
547 550
 
  551
+    bodylist = list(bodylist)
3
Jason K. Moore Collaborator

What is this here for? This doesn't seem like smart error checking. See line 490.

Ondřej Čertík Owner
certik added a note August 07, 2012

@angadhn, this comment still applies I think.

Angadh Nanjangud
angadhn added a note August 08, 2012

fixed this. I didn't realise this was in the momenta functions too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
sympy/physics/mechanics/functions.py
... ...
@@ -557,3 +561,121 @@ def angularmomentum(bodylist, point, frame):
557 561
                 raise TypeError('Body list must have only Particle'
558 562
                         'or RigidBody')
559 563
     return angularmomentum_sys
  564
+
  565
+def kinetic_energy(bodylist, frame):
3
Jason K. Moore Collaborator

why not do this?

def kinetic_energy(frame, *args):

Then the user doesn't have to put anything in a list as an extra step. They call:

ke = kinetic_energy(N, particle1, body2)

And if the user happens to have particles and bodies stored is some sort of sequence they can simply use this notation when calling:

bodies = (particle1, body2)
ke = kinetic_energy(N, *bodies)

This seems like less overhead and simpler and it also avoides having to error check for a list which is problematic because there is nothing wrong with supplying other types of sequences like a tuple.

Angadh Nanjangud
angadhn added a note August 03, 2012

When I wrote this, I was looking at how things were setup to derive the equations of motion of a system in Kane. Since the bodylist was anyhow required there, I thought it best to use what the user already prepared for Kane.

Angadh Nanjangud
angadhn added a note August 03, 2012

Also in the other comment above , I have addressed how the issue of a tuple can be dealt with.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request passes (merged 02547c0b into 9625918).

Ondřej Čertík
Owner

SymPy Bot Summary: :red_circle: There were test failures (merged angadhn/dev (02547c0) into master (9625918)).

@angadhn: Please fix the test failures.

Interpreter 1: :red_circle: There were test failures.
Interpreter: /usr/bin/python3 (3.2.3-final-0)
Architecture: Linux (64-bit)

Angadh Nanjangud

oops. fixed it.

Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request passes (merged 6524040b into 9625918).

sympy/physics/mechanics/functions.py
((67 lines not shown))
  604
+    >>> A = RigidBody('A', Ac, a, 20, (I, Ac))
  605
+    >>> BL = [Pa, A]
  606
+    >>> kinetic_energy(BL, N)
  607
+    350
  608
+
  609
+    """
  610
+
  611
+    bodylist = list(bodylist)
  612
+    if not isinstance(bodylist, list):
  613
+        raise TypeError('System elements must be supplied as a list')
  614
+    else:
  615
+        KE_sys = 0
  616
+        for e in bodylist:
  617
+            if isinstance(e, (RigidBody, Particle)):
  618
+                KE_individual = e.kinetic_energy(frame)
  619
+                KE_sys = KE_sys + KE_individual
1
Jason K. Moore Collaborator

all this can be written like:

ke_sys += e.kinetic_energy(frame)

No extra variable needed.

Check PEP8 too, leading caps in variable names is not really recommended.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
sympy/physics/mechanics/functions.py
((76 lines not shown))
  613
+        raise TypeError('System elements must be supplied as a list')
  614
+    else:
  615
+        KE_sys = 0
  616
+        for e in bodylist:
  617
+            if isinstance(e, (RigidBody, Particle)):
  618
+                KE_individual = e.kinetic_energy(frame)
  619
+                KE_sys = KE_sys + KE_individual
  620
+            else:
  621
+                raise TypeError('Body list must have only Particle'
  622
+                        'or RigidBody')
  623
+    return KE_sys
  624
+
  625
+def potential_energy(bodylist):
  626
+    """Potential energy of a multibody system.
  627
+
  628
+    This function returns the kinetic energy of a system of Particle's and/or
1
Jason K. Moore Collaborator

kinetic -> potential

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Jason K. Moore moorepants commented on the diff August 03, 2012
sympy/physics/mechanics/functions.py
((111 lines not shown))
  648
+    >>> from sympy.physics.mechanics import Point, Particle, ReferenceFrame
  649
+    >>> from sympy.physics.mechanics import RigidBody, outer, potential_energy
  650
+    >>> from sympy import symbols
  651
+    >>> M, m, g, h = symbols('M m g h')
  652
+    >>> N = ReferenceFrame('N')
  653
+    >>> O = Point('O')
  654
+    >>> O.set_vel(N, 0 * N.x)
  655
+    >>> P = O.locatenew('P', 1 * N.x)
  656
+    >>> Pa = Particle('Pa', P, m)
  657
+    >>> Ac = O.locatenew('Ac', 2 * N.y)
  658
+    >>> a = ReferenceFrame('a')
  659
+    >>> I = outer(N.z, N.z)
  660
+    >>> A = RigidBody('A', Ac, a, M, (I, Ac))
  661
+    >>> BL = [Pa, A]
  662
+    >>> Pa.set_potential_energy(m * g * h)
  663
+    >>> A.set_potential_energy(M * g * h)
3
Jason K. Moore Collaborator

Would it be more useful to have an optional argument in potential energy or some other way to pass in the potential energy definitions? Seems like if you have to call .set_potential_energy for everything seperately anyways, what is the point of this function? It only takes a short one liner to sum up the potential energies if you've already defined them. Maybe something like:

potential_energy(Pa, A, expressions=(m * g * h, M * g * h))

Then you at least don't have to type .set_potential_energy so many times.

Ondřej Čertík Owner
certik added a note August 07, 2012

@angadhn, this comment still applies I think.

Angadh Nanjangud
angadhn added a note August 08, 2012

So I have addressed this in one of the comments earlier where Jason discusses the 'decorators'. Let me tag you on that comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
sympy/physics/mechanics/functions.py
((125 lines not shown))
  662
+    >>> Pa.set_potential_energy(m * g * h)
  663
+    >>> A.set_potential_energy(M * g * h)
  664
+    >>> potential_energy(BL)
  665
+    M*g*h + g*h*m
  666
+
  667
+    """
  668
+
  669
+    bodylist = list(bodylist)
  670
+    if not isinstance(bodylist, list):
  671
+        raise TypeError('System elements must be supplied as a list')
  672
+    else:
  673
+        PE_sys = 0
  674
+        for e in bodylist:
  675
+            if isinstance(e, (RigidBody, Particle)):
  676
+                PE_individual = e.potential_energy
  677
+                PE_sys = PE_sys + PE_individual
1
Jason K. Moore Collaborator

pe_sys += e.potential_energy(frame)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
sympy/physics/mechanics/functions.py
((124 lines not shown))
  661
+    >>> BL = [Pa, A]
  662
+    >>> Pa.set_potential_energy(m * g * h)
  663
+    >>> A.set_potential_energy(M * g * h)
  664
+    >>> potential_energy(BL)
  665
+    M*g*h + g*h*m
  666
+
  667
+    """
  668
+
  669
+    bodylist = list(bodylist)
  670
+    if not isinstance(bodylist, list):
  671
+        raise TypeError('System elements must be supplied as a list')
  672
+    else:
  673
+        PE_sys = 0
  674
+        for e in bodylist:
  675
+            if isinstance(e, (RigidBody, Particle)):
  676
+                PE_individual = e.potential_energy
3
Jason K. Moore Collaborator

You can get an AttributeError here if the particle or body has no potential_energy defined. You should catch it and give the user a useful message about being sure to define it previously.

Aaron Meurer Owner
asmeurer added a note August 03, 2012

Perhaps RigitBody and Particle should define no-op potential_energy that has the error message there.

Angadh Nanjangud
angadhn added a note August 04, 2012

Fixed this using 'callable'. It should check if the potential energy has been set or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
sympy/physics/mechanics/particle.py
((12 lines not shown))
  144
+    def kinetic_energy(self, frame):
  145
+        """Kinetic energy of the particle
  146
+
  147
+        The kinetic energy, T, of a particle,P, is given by
  148
+
  149
+        T = 0.5 * m * v**2
  150
+
  151
+        where m is the mass of particle P, and v is the velocity of the
  152
+        particle in the supplied ReferenceFrame.
  153
+
  154
+        Parameters
  155
+        ==========
  156
+
  157
+        frame : ReferenceFrame
  158
+            The Particle's velocity is typically defined with respect to
  159
+            an inertial frame but any relevant frame in which the velcoity is
2
Jason K. Moore Collaborator

velcoity -> velocity

Make sure to run your spell checker. In vim it is:

:set spell "on

:set nospell "off
Angadh Nanjangud
angadhn added a note August 03, 2012

Thanks for both!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Stefan Krastanov
Collaborator

SymPy Bot Summary: :red_circle: There were test failures.

@angadhn: Please fix the test failures.

Test command: setup.py test
master hash: 9625918
branch hash: 6524040

Interpreter 1: :eight_spoked_asterisk: All tests have passed.

Interpreter: /usr/local/bin/python2.5 (2.5.6-final-0)
Architecture: Linux (64-bit)
Cache: yes

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYx6MjDA

Interpreter 2: :eight_spoked_asterisk: All tests have passed.

Interpreter: /usr/bin/python2.7 (2.7.3-candidate-2)
Architecture: Linux (64-bit)
Cache: yes

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYkO0iDA

Interpreter 3: :eight_spoked_asterisk: All tests have passed.

Interpreter: /usr/bin/python3.2 (3.2.3-candidate-2)
Architecture: Linux (64-bit)
Cache: yes

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sY1tUiDA

Build HTML Docs: :red_circle: There were test failures.

Docs build command: make html-errors
Sphinx version: 1.1.3

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYsN0iDA

Automatic review by SymPy Bot.

sympy/physics/mechanics/particle.py
((56 lines not shown))
  188
+
  189
+        Examples
  190
+        ========
  191
+
  192
+        >>> from sympy.physics.mechanics import Particle, Point
  193
+        >>> from sympy import symbols
  194
+        >>> m, g, h = symbols('m g h')
  195
+        >>> O = Point('O')
  196
+        >>> P = Particle('P', O, m)
  197
+        >>> P.set_potential_energy(m * g * h)
  198
+
  199
+        """
  200
+        scalar = sympify(scalar)
  201
+        self._pe = scalar
  202
+
  203
+    @property
4
Jason K. Moore Collaborator

Is the only point of have the @property decorator so that you can write a doc string for the .potential_energy attribute? I'm not sure what the reason for it is? What happens when you try to set the .potential_energy directly?

You could get rid of the set_method and the decorated function. What if it just went like this:

>>> P.potential_energy = m * g * h
>>> P.potential_energy
m*g*h

You can make use of the @property to at least error check whatever folks assign to .potential_energy but you aren't doing that here. I'm curious why you have the extra overhead of the set method and @property decorator?

Angadh Nanjangud
angadhn added a note August 04, 2012

Well, right now it looks redundant. But we felt that we can later add more functionality to the 'set' method i.e. having it compute the potential energy due to gravity or a constant force or a spring force, which are pretty standard. SO we felt that it could be useful to have a method to 'set' and then have it as a property.

Jason K. Moore Collaborator

ok

Angadh Nanjangud
angadhn added a note August 08, 2012

@certik here is that comment on 'decorators' I was talking about.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
sympy/physics/mechanics/rigidbody.py
... ...
@@ -180,3 +180,101 @@ def angularmomentum(self, point, frame):
180 180
         return ((self.inertia[0] & self.frame.ang_vel_in(frame)) +
181 181
                 (point.vel(frame) ^ -self.masscenter.pos_from(point)) *
182 182
                 self.mass)
  183
+
  184
+    def kinetic_energy(self, frame):
  185
+        """Kinetic energy of the particle
1
Jason K. Moore Collaborator

particle -> RigidBody

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
sympy/physics/mechanics/rigidbody.py
... ...
@@ -180,3 +180,101 @@ def angularmomentum(self, point, frame):
180 180
         return ((self.inertia[0] & self.frame.ang_vel_in(frame)) +
181 181
                 (point.vel(frame) ^ -self.masscenter.pos_from(point)) *
182 182
                 self.mass)
  183
+
  184
+    def kinetic_energy(self, frame):
  185
+        """Kinetic energy of the particle
  186
+
  187
+        The kinetic energy, T, of a rigid body, B, is given by
  188
+
  189
+        T = (omega . (I* . omega)) + (0.5 * m * v **2)
1
Jason K. Moore Collaborator

You may want to clarify that . = dot product, I haven't seen this anywhere else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
sympy/physics/mechanics/rigidbody.py
((22 lines not shown))
  201
+            any relevant frame in which the velocity is known can be supplied.
  202
+
  203
+        Examples
  204
+        ========
  205
+
  206
+        >>> from sympy.physics.mechanics import Point, ReferenceFrame, outer
  207
+        >>> from sympy.physics.mechanics import RigidBody
  208
+        >>> from sympy import symbols
  209
+        >>> M, v, r, omega = symbols('M v r omega')
  210
+        >>> N = ReferenceFrame('N')
  211
+        >>> b = ReferenceFrame('b')
  212
+        >>> b.set_ang_vel(N, omega * b.x)
  213
+        >>> P = Point('P')
  214
+        >>> P.set_vel(N, v * N.x)
  215
+        >>> I = outer (b.x, b.x)
  216
+        >>> Inertia_tuple = (I, P)
1
Jason K. Moore Collaborator

Inertia_tuple -> inertia_tuple (PEP8)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
sympy/physics/mechanics/rigidbody.py
((35 lines not shown))
  214
+        >>> P.set_vel(N, v * N.x)
  215
+        >>> I = outer (b.x, b.x)
  216
+        >>> Inertia_tuple = (I, P)
  217
+        >>> B = RigidBody('B', P, b, M, Inertia_tuple)
  218
+        >>> B.kinetic_energy(N)
  219
+        M*v**2/2 + omega**2/2
  220
+
  221
+        """
  222
+
  223
+        Rotational_KE = (self.frame.ang_vel_in(frame) & (self.inertia[0] &
  224
+                self.frame.ang_vel_in(frame)) / sympify(2))
  225
+
  226
+        Translational_KE = (self.mass * (self.masscenter.vel(frame) &
  227
+            self.masscenter.vel(frame)) / sympify(2))
  228
+
  229
+        return (Rotational_KE + Translational_KE)
1
Jason K. Moore Collaborator

the parentheses are not needed, in fact it may return a tuple...but maybe not

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
sympy/physics/mechanics/rigidbody.py
((29 lines not shown))
  208
+        >>> from sympy import symbols
  209
+        >>> M, v, r, omega = symbols('M v r omega')
  210
+        >>> N = ReferenceFrame('N')
  211
+        >>> b = ReferenceFrame('b')
  212
+        >>> b.set_ang_vel(N, omega * b.x)
  213
+        >>> P = Point('P')
  214
+        >>> P.set_vel(N, v * N.x)
  215
+        >>> I = outer (b.x, b.x)
  216
+        >>> Inertia_tuple = (I, P)
  217
+        >>> B = RigidBody('B', P, b, M, Inertia_tuple)
  218
+        >>> B.kinetic_energy(N)
  219
+        M*v**2/2 + omega**2/2
  220
+
  221
+        """
  222
+
  223
+        Rotational_KE = (self.frame.ang_vel_in(frame) & (self.inertia[0] &
1
Jason K. Moore Collaborator

I'd read over PEP8 and adjust many or your variable names. I don't think the leading caps (or caps at all) are preferred. mixedCase is potentially, but these stand out to me. But I'm not completely sure and variable names don't seem to be a super concern in PEP8, but it's worth reading into to it just to get refreshed every once in a while.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Jason K. Moore moorepants commented on the diff August 03, 2012
sympy/physics/mechanics/tests/test_functions.py
... ...
@@ -4,8 +4,9 @@
4 4
                                      kinematic_equations, Vector,
5 5
                                      inertia_of_point_mass, partial_velocity,
6 6
                                      outer, Particle,
7  
-                                     Point, RigidBody, angularmomentum,
2
Jason K. Moore Collaborator

Did you remove Point? Isn't that needed below?

Angadh Nanjangud
angadhn added a note August 03, 2012

It was an additional 'Point' so I took it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
sympy/physics/mechanics/tests/test_functions.py
((10 lines not shown))
  340
+    m, M, l1 = symbols('m M l1')
  341
+    omega = dynamicsymbols('omega')
  342
+    N = ReferenceFrame('N')
  343
+    O = Point('O')
  344
+    O.set_vel(N, 0 * N.x)
  345
+    Ac = O.locatenew('Ac', l1 * N.x)
  346
+    P = Ac.locatenew('P', l1 * N.x)
  347
+    a = ReferenceFrame('a')
  348
+    a.set_ang_vel(N, omega * N.z)
  349
+    Ac.v2pt_theory(O, N, a)
  350
+    P.v2pt_theory(O, N, a)
  351
+    Pa = Particle('Pa', P, m)
  352
+    I = outer(N.z, N.z)
  353
+    A = RigidBody('A', Ac, a, M, (I, Ac))
  354
+    BL =[Pa, A]
  355
+    assert kinetic_energy(BL, N) == (M*l1**2*omega**2/2 + 2*l1**2*m*omega**2
1
Jason K. Moore Collaborator

Does this return a tuple? I don't think it should.

Also, is it better to assert that:

assert 0 == kinetic_energy(BL, N) - (M*l1**2*omega**2/2 + 2*l1**2*m*omega**2+ omega**2/2)

I vaguely remember Gilbert asking about this last year.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Jason K. Moore
Collaborator

I just went through this and picked out some potential issues. Hope it helps.

sympy/physics/mechanics/functions.py
((16 lines not shown))
492 495
         for e in bodylist:
493 496
             if isinstance(e, (RigidBody, Particle)):
494  
-                lm_individual = e.linearmomentum(frame)
495  
-                linearmomentum_sys = linearmomentum_sys + lm_individual
  497
+                lm_individual = e.linear_momentum(frame)
  498
+                linear_momentum_sys = linear_momentum_sys + lm_individual
2
Aaron Meurer Owner
asmeurer added a note August 03, 2012

Just do linear_momentum_sys += e.linear_momentum(frame). There's no need to create the intermediate variable that is not used again.

Ondřej Čertík Owner
certik added a note August 07, 2012

@angadhn, this comment still applies I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request passes (merged ce96f6d5 into 7d92368).

sympy/physics/mechanics/functions.py
((65 lines not shown))
  602
+    >>> a.set_ang_vel(N, 10 * N.z)
  603
+    >>> I = outer(N.z, N.z)
  604
+    >>> A = RigidBody('A', Ac, a, 20, (I, Ac))
  605
+    >>> kinetic_energy(N, Pa, A)
  606
+    350
  607
+
  608
+    """
  609
+
  610
+    if not isinstance(frame, ReferenceFrame):
  611
+        raise TypeError('Please enter a valid ReferenceFrame')
  612
+    ke_sys = 0
  613
+    for e in body:
  614
+        if isinstance(e, (RigidBody, Particle)):
  615
+            ke_sys += e.kinetic_energy(frame)
  616
+        else:
  617
+            raise TypeError('*bodies must have only Particle or RigidBody')
2
Jason K. Moore Collaborator

You use the variable body in your source not bodies.

Angadh Nanjangud
angadhn added a note August 04, 2012

Fixed it. So do you think this is good to be merged?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request passes (merged 53b6bc2d into 41e968b).

Stefan Krastanov
Collaborator

SymPy Bot Summary: :red_circle: There were test failures.

@angadhn: Please fix the test failures.

Test command: setup.py test
master hash: 1627b32
branch hash: 53b6bc2

Interpreter 1: :eight_spoked_asterisk: All tests have passed.

Interpreter: /usr/local/bin/python2.5 (2.5.6-final-0)
Architecture: Linux (64-bit)
Cache: yes

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYhpQjDA

Interpreter 2: :eight_spoked_asterisk: All tests have passed.

Interpreter: /usr/bin/python2.7 (2.7.3-candidate-2)
Architecture: Linux (64-bit)
Cache: yes

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYuY8iDA

Interpreter 3: :eight_spoked_asterisk: All tests have passed.

Interpreter: /usr/bin/python3.2 (3.2.3-candidate-2)
Architecture: Linux (64-bit)
Cache: yes

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYgsYiDA

Build HTML Docs: :red_circle: There were test failures.

Docs build command: make html-errors
Sphinx version: 1.1.3

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sY0qMjDA

Automatic review by SymPy Bot.

Ondřej Čertík
Owner

@angadhn, can you please address all Aaron's and Jason's comments above? E.g. things about the "isinstance" as well as the "+=". Then we can merge it.

Angadh Nanjangud

Hi @certik, I believe I have already addressed the issues.
angadhn@53b6bc2

Ondřej Čertík
Owner

First one note for any PR ---- just keep adding patches to your branch, so that we can quickly see what has changed and one can read the whole PR as a history or "story". If you rebase, then suddenly all the test results stop having any sense.
Only at the very end, when the PR is ready to go in, and the history is really ugly (which actually is rarely the case), then one can do a quick rebase and merge it.

Now I am going to comment to all the comments that you didn't address, so they will show up below. Edit: so they don't appear below, but above.

Ondřej Čertík
Owner

Looks like you rebased again...

Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request passes (merged 1ba125ec into bd49e6a).

Angadh Nanjangud

Must've done that again mistakenly. Apologies.

sympy/physics/mechanics/rigidbody.py
((65 lines not shown))
  244
+
  245
+        >>> from sympy.physics.mechanics import Particle, Point, outer
  246
+        >>> from sympy.physics.mechanics import RigidBody, ReferenceFrame
  247
+        >>> from sympy import symbols
  248
+        >>> b = ReferenceFrame('b')
  249
+        >>> M, g, h = symbols('M g h')
  250
+        >>> P = Point('P')
  251
+        >>> I = outer (b.x, b.x)
  252
+        >>> Inertia_tuple = (I, P)
  253
+        >>> B = RigidBody('B', P, b, M, Inertia_tuple)
  254
+        >>> B.set_potential_energy(M * g * h)
  255
+
  256
+        """
  257
+
  258
+        scalar = sympify(scalar)
  259
+        self._pe = scalar
1
Jason K. Moore Collaborator

Why two lines?

self._pe = sympify(scalar)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request passes (merged caa2aa88 into bd49e6a).

sympy/physics/mechanics/functions.py
((11 lines not shown))
488 491
     if not isinstance(bodylist, list):
489 492
         raise TypeError('System elements must be supplied as a list')
490 493
     else:
491  
-        linearmomentum_sys = 0
  494
+        linear_momentum_sys = 0
1

Would returning S(0) here be better, to get a SymPy object? I've had other difficulties in mechanics with returning integers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
sympy/physics/mechanics/functions.py
((11 lines not shown))
548 551
     if not isinstance(bodylist, list):
549 552
         raise TypeError('System elements must be supplied as a list')
550 553
     else:
551  
-        angularmomentum_sys = 0
  554
+        angular_momentum_sys = 0
2

Same comment about S(0)

Angadh Nanjangud
angadhn added a note August 08, 2012

done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Stefan Krastanov
Collaborator

SymPy Bot Summary: :red_circle: There were test failures.

@angadhn: Please fix the test failures.

Test command: setup.py test
master hash: 0d88b25
branch hash: caa2aa8

Interpreter 1: :eight_spoked_asterisk: All tests have passed.

Interpreter: /usr/local/bin/python2.5 (2.5.6-final-0)
Architecture: Linux (64-bit)
Cache: yes

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYwo8iDA

Interpreter 2: :eight_spoked_asterisk: All tests have passed.

Interpreter: /usr/bin/python2.7 (2.7.3-candidate-2)
Architecture: Linux (64-bit)
Cache: yes

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYwt0iDA

Interpreter 3: :eight_spoked_asterisk: All tests have passed.

Interpreter: /usr/bin/python3.2 (3.2.3-candidate-2)
Architecture: Linux (64-bit)
Cache: yes

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYnfAhDA

Build HTML Docs: :red_circle: There were test failures.

Docs build command: make html-errors
Sphinx version: 1.1.3

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYq4wjDA

Automatic review by SymPy Bot.

Angadh Nanjangud

I have also fixed the html doc errors in the latest commit and addressed all issues brought up thus far.

Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request passes (merged 57f1c042 into 0d88b25).

Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request passes (merged 2fa49af6 into 0d88b25).

Stefan Krastanov
Collaborator

SymPy Bot Summary: :red_circle: There were test failures.

@angadhn: Please fix the test failures.

Test command: setup.py test
master hash: 0d88b25
branch hash: 2fa49af

Interpreter 1: :eight_spoked_asterisk: All tests have passed.

Interpreter: /usr/local/bin/python2.5 (2.5.6-final-0)
Architecture: Linux (64-bit)
Cache: yes

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYxrYiDA

Interpreter 2: :eight_spoked_asterisk: All tests have passed.

Interpreter: /usr/bin/python2.7 (2.7.3-candidate-2)
Architecture: Linux (64-bit)
Cache: yes

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYofAhDA

Interpreter 3: :eight_spoked_asterisk: All tests have passed.

Interpreter: /usr/bin/python3.2 (3.2.3-candidate-2)
Architecture: Linux (64-bit)
Cache: yes

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sY59UiDA

Build HTML Docs: :red_circle: There were test failures.

Docs build command: make html-errors
Sphinx version: 1.1.3

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYjsYiDA

Automatic review by SymPy Bot.

Ondřej Čertík
Owner

@angadhn, did you see my comment about rebasing? See also:

https://groups.google.com/d/topic/sympy/T2NfDo17wqc/discussion

Please stop rebasing, just keep pushing new patches in.

You still didn't address the points, that I mentioned:

https://github.com/sympy/sympy/pull/1407/files#r1306927
https://github.com/sympy/sympy/pull/1407/files#r1306929
https://github.com/sympy/sympy/pull/1407/files#r1307067

Can you please do that soon? Just push new patches in, let's do final review and push it in. Thanks!

Angadh Nanjangud

@certik I have stopped rebasing. Sorry about that mess.

Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request passes (merged 50b2496f into 0d88b25).

Stefan Krastanov
Collaborator

SymPy Bot Summary: :red_circle: There were test failures.

@angadhn: Please fix the test failures.

Test command: setup.py test
master hash: 15c2c89
branch hash: 50b2496

Interpreter 1: :eight_spoked_asterisk: All tests have passed.

Interpreter: /usr/local/bin/python2.5 (2.5.6-final-0)
Architecture: Linux (64-bit)
Cache: yes

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYre0iDA

Interpreter 2: :eight_spoked_asterisk: All tests have passed.

Interpreter: /usr/bin/python2.7 (2.7.3-candidate-2)
Architecture: Linux (64-bit)
Cache: yes

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYr4wjDA

Interpreter 3: :eight_spoked_asterisk: All tests have passed.

Interpreter: /usr/bin/python3.2 (3.2.3-candidate-2)
Architecture: Linux (64-bit)
Cache: yes

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYo_AhDA

Build HTML Docs: :red_circle: There were test failures.

Docs build command: make html-errors
Sphinx version: 1.1.3

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sY7tUiDA

Automatic review by SymPy Bot.

Angadh Nanjangud

I think this error may be due to the whole rebasing issue. I have fixed this in my last commit. Sorry for the confusion.

Angadh Nanjangud

@certik I am uncertain of how to interpret the docstring error but I have corrected what I believe was causing the error. I believe I have addressed all the concerns.

Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request passes (merged 5492ef80 into 15c2c89).

Ondřej Čertík
Owner

Thanks. This PR is +1 from me. I am just waiting for one last pass of Stefan's bot to make sure the "make html" passes as well, and then I'll merge it.

Stefan Krastanov
Collaborator

SymPy Bot Summary: :red_circle: There were test failures.

@angadhn: Please fix the test failures.

Test command: setup.py test
master hash: 15c2c89
branch hash: 5492ef8

Interpreter 1: :eight_spoked_asterisk: All tests have passed.

Interpreter: /usr/local/bin/python2.5 (2.5.6-final-0)
Architecture: Linux (64-bit)
Cache: yes

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYzY8iDA

Interpreter 2: :eight_spoked_asterisk: All tests have passed.

Interpreter: /usr/bin/python2.7 (2.7.3-candidate-2)
Architecture: Linux (64-bit)
Cache: yes

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYyN0iDA

Interpreter 3: :eight_spoked_asterisk: All tests have passed.

Interpreter: /usr/bin/python3.2 (3.2.3-candidate-2)
Architecture: Linux (64-bit)
Cache: yes

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sY5KMjDA

Build HTML Docs: :red_circle: There were test failures.

Docs build command: make html-errors
Sphinx version: 1.1.3

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYr-0iDA

Automatic review by SymPy Bot.

Ondřej Čertík
Owner

Ok, so the "make html" has to be fixed. @angadhn, are you able to reproduce the warning? Just do "make html", try to build the docs and see if you can see the warning.

sympy/physics/mechanics/particle.py
((6 lines not shown))
137 138
         m*r*v*N.z
138 139
 
139 140
         """
140 141
 
141 142
         return self.point.pos_from(point) ^ (self.mass * self.point.vel(frame))
  143
+
  144
+    def kinetic_energy(self, frame):
  145
+        """Kinetic energy of the particle
  146
+
  147
+        The kinetic energy, T, of a particle,P, is given by
  148
+
  149
+        T = 0.5 * m * v**2
3
Ondřej Čertík Owner
certik added a note August 09, 2012

I think that the "make html" failure is caused by this line. Do we have some way to specify math in sympy? If not, just write it as:

T = 1/2 * m * v^2

That should be clear. Also, while you are at it ---- put one space before "P" in "of a particle,P, is".

Aaron Meurer Owner
asmeurer added a note August 09, 2012

Yes, use single backticks, like `T = 1/2 m v^2`. Note that you need to use LaTeX notation here.

Angadh Nanjangud
angadhn added a note August 09, 2012

Thanks for all the help. It passed the doctest now. Hopefully it goes smoothly from hereon. Thanks a bunch again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Ondřej Čertík
Owner

@angadhn, there are several warnings, see my log here: https://gist.github.com/3305562, and no warnings in master.
Do:

cd doc
make html

and let me know if you can reproduce my log. Then the next step will be to fix the warnings.

Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request passes (merged e16691a into dd8352a).

Aaron Meurer
Owner

SymPy Bot Summary: :red_circle: There were test failures (merged angadhn/dev (e16691a) into master (dd8352a)).
@angadhn: Please fix the test failures.
Build HTML Docs: :red_circle: There were test failures: 1.1.3

Angadh Nanjangud

I'm unable to recreate these errors with 'make html'. Just checked again to make sure I did run things on the right branch.

Stefan Krastanov
Collaborator

SymPy Bot Summary: :eight_spoked_asterisk: All tests have passed.

Test command: setup.py test
master hash: dd8352a
branch hash: e16691a

Interpreter 1: :eight_spoked_asterisk: All tests have passed.

Interpreter: /usr/local/bin/python2.5 (2.5.6-final-0)
Architecture: Linux (64-bit)
Cache: yes

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYqPAhDA

Interpreter 2: :eight_spoked_asterisk: All tests have passed.

Interpreter: /usr/bin/python2.7 (2.7.3-candidate-2)
Architecture: Linux (64-bit)
Cache: yes

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYlcYiDA

Interpreter 3: :eight_spoked_asterisk: All tests have passed.

Interpreter: /usr/bin/python3.2 (3.2.3-candidate-2)
Architecture: Linux (64-bit)
Cache: yes

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sYzKsjDA

Build HTML Docs: :eight_spoked_asterisk: All tests have passed.

Docs build command: make html-errors
Sphinx version: 1.1.3

Test results html report: http://reviews.sympy.org/report/agZzeW1weTNyDAsSBFRhc2sY07YiDA

Automatic review by SymPy Bot.

Aaron Meurer
Owner

@angadhn try make clean; make html.

Angadh Nanjangud

still nothing. but it looks like the tests passed.

Aaron Meurer
Owner

SymPy Bot Summary: :red_circle: There were test failures (merged angadhn/dev (e16691a) into master (dd8352a)).
@angadhn: Please fix the test failures.
Build HTML Docs: :red_circle: There were test failures: 1.1.3

Aaron Meurer
Owner

I also can't reproduce it outside of sympy-bot. So I don't know what's going on. Sphinx seems to be quite buggy.

Aaron Meurer
Owner

SymPy Bot Summary: :eight_spoked_asterisk: All tests have passed (merged angadhn/dev (e16691a) into master (dd8352a)).
Build HTML Docs: :eight_spoked_asterisk: All tests have passed: 1.1.3
Docs build command: make clean; make html

Ondřej Čertík
Owner

@angadhn, you seem to have rebased yet again... Since I asked you three times and you didn't manage not to rebase a single time, there must be some technical difficulties. Are you doing "git rebase master"? Or are you doing "git commit --amend"? Please don't do any of these once your branch us up there. You can notice that when you push, git will refuse to push and you have to force it (typically with "+"). This is a sign that you have rebased. (If that happens, you can just rebase on top of the commit that is out here --- feel free to ask on the mailinglist if you have any problems with git.)

Anyway, this patch is +1 from me. Well, you did not address my comment about adding a space before "P"... But let's just get this merged, I don't want to delay this anymore.

Ondřej Čertík certik merged commit 85a4a4e into from August 10, 2012
Ondřej Čertík certik closed this August 10, 2012
Angadh Nanjangud

I haven't been rebasing but I have been amending and the forcing it, like you said. I will keep your advice in mind from here on. Sorry about this, again.

Gilbert Gede

Thanks everyone for getting this in.

Ondřej Čertík
Owner

No problem. Let's get the other PRs in as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Aug 09, 2012
Angadh Nanjangud Modified 13 -Kinetic and potential energy functionality. e16691a
This page is out of date. Refresh to see the latest.
12  doc/src/modules/physics/mechanics/api/part_bod.rst
Source Rendered
@@ -28,13 +28,13 @@ inertia_of_point_mass
28 28
 .. autofunction:: sympy.physics.mechanics.functions.inertia_of_point_mass
29 29
 
30 30
 
31  
-linearmomentum
32  
-==============
  31
+linear_momentum
  32
+===============
33 33
 
34  
-.. autofunction:: sympy.physics.mechanics.functions.linearmomentum
  34
+.. autofunction:: sympy.physics.mechanics.functions.linear_momentum
35 35
 
36 36
 
37  
-angularmomentum
38  
-===============
  37
+angular_momentum
  38
+================
39 39
 
40  
-.. autofunction:: sympy.physics.mechanics.functions.angularmomentum
  40
+.. autofunction:: sympy.physics.mechanics.functions.angular_momentum
176  sympy/physics/mechanics/functions.py
@@ -10,8 +10,10 @@
10 10
            'kinematic_equations',
11 11
            'inertia_of_point_mass',
12 12
            'partial_velocity',
13  
-           'linearmomentum',
14  
-           'angularmomentum']
  13
+           'linear_momentum',
  14
+           'angular_momentum',
  15
+           'kinetic_energy',
  16
+           'potential_energy']
15 17
 
16 18
 from sympy.physics.mechanics.essential import (Vector, Dyadic, ReferenceFrame,
17 19
                                                MechanicsStrPrinter,
@@ -20,7 +22,9 @@
20 22
                                                dynamicsymbols)
21 23
 from sympy.physics.mechanics.particle import Particle
22 24
 from sympy.physics.mechanics.rigidbody import RigidBody
  25
+from sympy.physics.mechanics.point import Point
23 26
 from sympy import sympify, diff, sin, cos, Matrix
  27
+from sympy.core.basic import S
24 28
 
25 29
 def cross(vec1, vec2):
26 30
     """Cross product convenience wrapper for Vector.cross(): \n"""
@@ -448,7 +452,7 @@ def partial_velocity(vel_list, u_ind_list):
448 452
             i = i + 1
449 453
     return list_of_pvlists
450 454
 
451  
-def linearmomentum(bodylist, frame):
  455
+def linear_momentum(frame, *body):
452 456
     """Linear momentum of the system.
453 457
 
454 458
     This function returns the linear momentum of a system of Particle's and/or
@@ -460,17 +464,18 @@ def linearmomentum(bodylist, frame):
460 464
 
461 465
     L = L1 + L2
462 466
 
463  
-    bodylist : list
464  
-        A list of all RigidBody's and Particle's in the system.
465  
-
466 467
     frame : ReferenceFrame
467 468
         The frame in which linear momentum is desired.
468 469
 
  470
+    body1, body2, body3... : Particle and/or RigidBody
  471
+        The body (or bodies) whose kinetic energy is required.
  472
+
  473
+
469 474
     Examples
470 475
     ========
471 476
 
472 477
     >>> from sympy.physics.mechanics import Point, Particle, ReferenceFrame
473  
-    >>> from sympy.physics.mechanics import RigidBody, outer, linearmomentum
  478
+    >>> from sympy.physics.mechanics import RigidBody, outer, linear_momentum
474 479
     >>> N = ReferenceFrame('N')
475 480
     >>> P = Point('P')
476 481
     >>> P.set_vel(N, 10 * N.x)
@@ -479,26 +484,23 @@ def linearmomentum(bodylist, frame):
479 484
     >>> Ac.set_vel(N, 25 * N.y)
480 485
     >>> I = outer(N.x, N.x)
481 486
     >>> A = RigidBody('A', Ac, N, 20, (I, Ac))
482  
-    >>> BL = [Pa, A]
483  
-    >>> print linearmomentum(BL, N)
  487
+    >>> linear_momentum(N, A, Pa)
484 488
     10*N.x + 500*N.y
485 489
 
486 490
     """
487 491
 
488  
-    if not isinstance(bodylist, list):
489  
-        raise TypeError('System elements must be supplied as a list')
  492
+    if not isinstance(frame, ReferenceFrame):
  493
+        raise TypeError('Please specify a valid ReferenceFrame')
490 494
     else:
491  
-        linearmomentum_sys = 0
492  
-        for e in bodylist:
  495
+        linear_momentum_sys = S(0)
  496
+        for e in body:
493 497
             if isinstance(e, (RigidBody, Particle)):
494  
-                lm_individual = e.linearmomentum(frame)
495  
-                linearmomentum_sys = linearmomentum_sys + lm_individual
  498
+                linear_momentum_sys += e.linear_momentum(frame)
496 499
             else:
497  
-                raise TypeError('Body list must have only Particle'
498  
-                        'or RigidBody')
499  
-    return linearmomentum_sys
  500
+                raise TypeError('*body must have only Particle or RigidBody')
  501
+    return linear_momentum_sys
500 502
 
501  
-def angularmomentum(bodylist, point, frame):
  503
+def angular_momentum(point, frame, *body):
502 504
     """Angular momentum of a system
503 505
 
504 506
     This function returns the angular momentum of a system of Particle's and/or
@@ -513,20 +515,20 @@ def angularmomentum(bodylist, point, frame):
513 515
     Parameters
514 516
     ==========
515 517
 
516  
-    bodylist : list
517  
-        A list of all RigidBody's and Particle's in the system.
518  
-
519 518
     point : Point
520 519
         The point about which angular momentum of the system is desired.
521 520
 
522 521
     frame : ReferenceFrame
523 522
         The frame in which angular momentum is desired.
524 523
 
  524
+    body1, body2, body3... : Particle and/or RigidBody
  525
+        The body (or bodies) whose kinetic energy is required.
  526
+
525 527
     Examples
526 528
     ========
527 529
 
528 530
     >>> from sympy.physics.mechanics import Point, Particle, ReferenceFrame
529  
-    >>> from sympy.physics.mechanics import RigidBody, outer, angularmomentum
  531
+    >>> from sympy.physics.mechanics import RigidBody, outer, angular_momentum
530 532
     >>> N = ReferenceFrame('N')
531 533
     >>> O = Point('O')
532 534
     >>> O.set_vel(N, 0 * N.x)
@@ -539,21 +541,127 @@ def angularmomentum(bodylist, point, frame):
539 541
     >>> a.set_ang_vel(N, 10 * N.z)
540 542
     >>> I = outer(N.z, N.z)
541 543
     >>> A = RigidBody('A', Ac, a, 20, (I, Ac))
542  
-    >>> BL = [Pa, A]
543  
-    >>> print angularmomentum(BL, O, N)
  544
+    >>> angular_momentum(O, N, Pa, A)
544 545
     10*N.z
545 546
 
546 547
     """
547 548
 
548  
-    if not isinstance(bodylist, list):
549  
-        raise TypeError('System elements must be supplied as a list')
  549
+    if not isinstance(frame, ReferenceFrame):
  550
+        raise TypeError('Please enter a valid ReferenceFrame')
  551
+    if not isinstance(point, Point):
  552
+        raise TypeError('Please specify a valid Point')
550 553
     else:
551  
-        angularmomentum_sys = 0
552  
-        for e in bodylist:
  554
+        angular_momentum_sys = S(0)
  555
+        for e in body:
553 556
             if isinstance(e, (RigidBody, Particle)):
554  
-                angmom_individual = e.angularmomentum(point, frame)
555  
-                angularmomentum_sys = angularmomentum_sys + angmom_individual
  557
+                angular_momentum_sys += e.angular_momentum(point, frame)
556 558
             else:
557  
-                raise TypeError('Body list must have only Particle'
558  
-                        'or RigidBody')
559  
-    return angularmomentum_sys
  559
+                raise TypeError('*body must have only Particle or RigidBody')
  560
+    return angular_momentum_sys
  561
+
  562
+def kinetic_energy(frame, *body):
  563
+    """Kinetic energy of a multibody system.
  564
+
  565
+    This function returns the kinetic energy of a system of Particle's and/or
  566
+    RigidBody's. The kinetic energy of such a system is equal to the sum of
  567
+    the kinetic energies of its constituents. Consider a system, S, comprising
  568
+    a rigid body, A, and a particle, P. The kinetic energy of the system, T,
  569
+    is equal to the vector sum of the kinetic energy of the particle, T1, and
  570
+    the kinetic energy of the rigid body, T2, i.e.
  571
+
  572
+    T = T1 + T2
  573
+
  574
+    Kinetic energy is a scalar.
  575
+
  576
+    Parameters
  577
+    ==========
  578
+
  579
+    frame : ReferenceFrame
  580
+        The frame in which angular momentum is desired.
  581
+
  582
+    body1, body2, body3... : Particle and/or RigidBody
  583
+        The body (or bodies) whose kinetic energy is required.
  584
+
  585
+    Examples
  586
+    ========
  587
+
  588
+    >>> from sympy.physics.mechanics import Point, Particle, ReferenceFrame
  589
+    >>> from sympy.physics.mechanics import RigidBody, outer, kinetic_energy
  590
+    >>> N = ReferenceFrame('N')
  591
+    >>> O = Point('O')
  592
+    >>> O.set_vel(N, 0 * N.x)
  593
+    >>> P = O.locatenew('P', 1 * N.x)
  594
+    >>> P.set_vel(N, 10 * N.x)
  595
+    >>> Pa = Particle('Pa', P, 1)
  596
+    >>> Ac = O.locatenew('Ac', 2 * N.y)
  597
+    >>> Ac.set_vel(N, 5 * N.y)
  598
+    >>> a = ReferenceFrame('a')
  599
+    >>> a.set_ang_vel(N, 10 * N.z)
  600
+    >>> I = outer(N.z, N.z)
  601
+    >>> A = RigidBody('A', Ac, a, 20, (I, Ac))
  602
+    >>> kinetic_energy(N, Pa, A)
  603
+    350
  604
+
  605
+    """
  606
+
  607
+    if not isinstance(frame, ReferenceFrame):
  608
+        raise TypeError('Please enter a valid ReferenceFrame')
  609
+    ke_sys = S(0)
  610
+    for e in body:
  611
+        if isinstance(e, (RigidBody, Particle)):
  612
+            ke_sys += e.kinetic_energy(frame)
  613
+        else:
  614
+            raise TypeError('*body must have only Particle or RigidBody')
  615
+    return ke_sys
  616
+
  617
+def potential_energy(*body):
  618
+    """Potential energy of a multibody system.
  619
+
  620
+    This function returns the potential energy of a system of Particle's and/or
  621
+    RigidBody's. The potential energy of such a system is equal to the sum of
  622
+    the potential energy of its constituents. Consider a system, S, comprising
  623
+    a rigid body, A, and a particle, P. The potential energy of the system, V,
  624
+    is equal to the vector sum of the potential energy of the particle, V1, and
  625
+    the potential energy of the rigid body, V2, i.e.
  626
+
  627
+    V = V1 + V2
  628
+
  629
+    Potential energy is a scalar.
  630
+
  631
+    Parameters
  632
+    ==========
  633
+
  634
+    body1, body2, body3... : Particle and/or RigidBody
  635
+        The body (or bodies) whose potential energy is required.
  636
+
  637
+    Examples
  638
+    ========
  639
+
  640
+    >>> from sympy.physics.mechanics import Point, Particle, ReferenceFrame
  641
+    >>> from sympy.physics.mechanics import RigidBody, outer, potential_energy
  642
+    >>> from sympy import symbols
  643
+    >>> M, m, g, h = symbols('M m g h')
  644
+    >>> N = ReferenceFrame('N')
  645
+    >>> O = Point('O')
  646
+    >>> O.set_vel(N, 0 * N.x)
  647
+    >>> P = O.locatenew('P', 1 * N.x)
  648
+    >>> Pa = Particle('Pa', P, m)
  649
+    >>> Ac = O.locatenew('Ac', 2 * N.y)
  650
+    >>> a = ReferenceFrame('a')
  651
+    >>> I = outer(N.z, N.z)
  652
+    >>> A = RigidBody('A', Ac, a, M, (I, Ac))
  653
+    >>> BL = [Pa, A]
  654
+    >>> Pa.set_potential_energy(m * g * h)
  655
+    >>> A.set_potential_energy(M * g * h)
  656
+    >>> potential_energy(Pa, A)
  657
+    M*g*h + g*h*m
  658
+
  659
+    """
  660
+
  661
+    pe_sys = S(0)
  662
+    for e in body:
  663
+        if isinstance(e, (RigidBody, Particle)):
  664
+            pe_sys += e.potential_energy
  665
+        else:
  666
+            raise TypeError('*body must have only Particle or RigidBody')
  667
+    return pe_sys
91  sympy/physics/mechanics/particle.py
@@ -41,6 +41,7 @@ def __init__(self, name, point, mass):
41 41
         self._name = name
42 42
         self.set_mass(mass)
43 43
         self.set_point(point)
  44
+        self._pe = None
44 45
 
45 46
     def __str__(self):
46 47
         return self._name
@@ -67,7 +68,7 @@ def set_point(self, p):
67 68
 
68 69
     point = property(get_point, set_point)
69 70
 
70  
-    def linearmomentum(self, frame):
  71
+    def linear_momentum(self, frame):
71 72
         """Linear momentum of the particle.
72 73
 
73 74
         The linear momentum L, of a particle P, with respect to frame N is
@@ -94,14 +95,14 @@ def linearmomentum(self, frame):
94 95
         >>> P = Point('P')
95 96
         >>> A = Particle('A', P, m)
96 97
         >>> P.set_vel(N, v * N.x)
97  
-        >>> print A.linearmomentum(N)
  98
+        >>> A.linear_momentum(N)
98 99
         m*v*N.x
99 100
 
100 101
         """
101 102
 
102 103
         return self.mass * self.point.vel(frame)
103 104
 
104  
-    def angularmomentum(self, point, frame):
  105
+    def angular_momentum(self, point, frame):
105 106
         """Angular momentum of the particle about the point.
106 107
 
107 108
         The angular momentum H, about some point O of a particle, P, is given
@@ -133,9 +134,91 @@ def angularmomentum(self, point, frame):
133 134
         >>> A = O.locatenew('A', r * N.x)
134 135
         >>> P = Particle('P', A, m)
135 136
         >>> P.point.set_vel(N, v * N.y)
136  
-        >>> print P.angularmomentum(O, N)
  137
+        >>> P.angular_momentum(O, N)
137 138
         m*r*v*N.z
138 139
 
139 140
         """
140 141
 
141 142
         return self.point.pos_from(point) ^ (self.mass * self.point.vel(frame))
  143
+
  144
+    def kinetic_energy(self, frame):
  145
+        """Kinetic energy of the particle
  146
+
  147
+        The kinetic energy, T, of a particle,P, is given by
  148
+
  149
+        'T = 1/2 m v^2'
  150
+
  151
+        where m is the mass of particle P, and v is the velocity of the
  152
+        particle in the supplied ReferenceFrame.
  153
+
  154
+        Parameters
  155
+        ==========
  156
+
  157
+        frame : ReferenceFrame
  158
+            The Particle's velocity is typically defined with respect to
  159
+            an inertial frame but any relevant frame in which the velocity is
  160
+            known can be supplied.
  161
+
  162
+        Examples
  163
+        ========
  164
+
  165
+        >>> from sympy.physics.mechanics import Particle, Point, ReferenceFrame
  166
+        >>> from sympy import symbols
  167
+        >>> m, v, r = symbols('m v r')
  168
+        >>> N = ReferenceFrame('N')
  169
+        >>> O = Point('O')
  170
+        >>> P = Particle('P', O, m)
  171
+        >>> P.point.set_vel(N, v * N.y)
  172
+        >>> P.kinetic_energy(N)
  173
+        m*v**2/2
  174
+
  175
+        """
  176
+
  177
+        return (self.mass / sympify(2) * self.point.vel(frame) &
  178
+                self.point.vel(frame))
  179
+
  180
+    def set_potential_energy(self, scalar):
  181
+        """Used to set the potential energy of the Particle.
  182
+
  183
+        Parameters
  184
+        ==========
  185
+
  186
+        scalar : Sympifyable
  187
+            The potential energy (a scalar) of the Particle.
  188
+
  189
+        Examples
  190
+        ========
  191
+
  192
+        >>> from sympy.physics.mechanics import Particle, Point
  193
+        >>> from sympy import symbols
  194
+        >>> m, g, h = symbols('m g h')
  195
+        >>> O = Point('O')
  196
+        >>> P = Particle('P', O, m)
  197
+        >>> P.set_potential_energy(m * g * h)
  198
+
  199
+        """
  200
+
  201
+        self._pe = sympify(scalar)
  202
+
  203
+    @property
  204
+    def potential_energy(self):
  205
+        """The potential energy of the Particle.
  206
+
  207
+        Examples
  208
+        ========
  209
+
  210
+        >>> from sympy.physics.mechanics import Particle, Point
  211
+        >>> from sympy import symbols
  212
+        >>> m, g, h = symbols('m g h')
  213
+        >>> O = Point('O')
  214
+        >>> P = Particle('P', O, m)
  215
+        >>> P.set_potential_energy(m * g * h)
  216
+        >>> P.potential_energy
  217
+        g*h*m
  218
+
  219
+        """
  220
+
  221
+        if callable(self._pe) == True:
  222
+            return self._pe
  223
+        else:
  224
+            raise ValueError('Please set the potential energy of the Particle')
108  sympy/physics/mechanics/rigidbody.py
@@ -100,7 +100,7 @@ def set_inertia(self, I):
100 100
 
101 101
     inertia = property(get_inertia, set_inertia)
102 102
 
103  
-    def linearmomentum(self, frame):
  103
+    def linear_momentum(self, frame):
104 104
         """ Linear momentum of the rigid body.
105 105
 
106 106
         The linear momentum L, of a rigid body B, with respect to frame N is
@@ -129,14 +129,14 @@ def linearmomentum(self, frame):
129 129
         >>> I = outer (N.x, N.x)
130 130
         >>> Inertia_tuple = (I, P)
131 131
         >>> B = RigidBody('B', P, N, M, Inertia_tuple)
132  
-        >>> B.linearmomentum(N)
  132
+        >>> B.linear_momentum(N)
133 133
         M*v*N.x
134 134
 
135 135
         """
136 136
 
137 137
         return self.mass * self.masscenter.vel(frame)
138 138
 
139  
-    def angularmomentum(self, point, frame):
  139
+    def angular_momentum(self, point, frame):
140 140
         """ Angular momentum of the rigid body.
141 141
 
142 142
         The angular momentum H, about some point O, of a rigid body B, in a
@@ -172,7 +172,7 @@ def angularmomentum(self, point, frame):
172 172
         >>> I = outer (b.x, b.x)
173 173
         >>> Inertia_tuple = (I, P)
174 174
         >>> B = RigidBody('B', P, b, M, Inertia_tuple)
175  
-        >>> B.angularmomentum(P, N)
  175
+        >>> B.angular_momentum(P, N)
176 176
         omega*b.x
177 177
 
178 178
         """
@@ -180,3 +180,103 @@ def angularmomentum(self, point, frame):
180 180
         return ((self.inertia[0] & self.frame.ang_vel_in(frame)) +
181 181
                 (point.vel(frame) ^ -self.masscenter.pos_from(point)) *
182 182
                 self.mass)
  183
+
  184
+    def kinetic_energy(self, frame):
  185
+        """Kinetic energy of the rigid body
  186
+
  187
+        The kinetic energy, T, of a rigid body, B, is given by
  188
+
  189
+        'T = 1/2 (I omega^2 + m v^2)'
  190
+
  191
+        where I and m are the central inertia dyadic and mass of rigid body B,
  192
+        respectively, omega is the body's angular velocity and v is the
  193
+        velocity of the body's mass center in the supplied ReferenceFrame.
  194
+
  195
+        Parameters
  196
+        ==========
  197
+
  198
+        frame : ReferenceFrame
  199
+            The RigidBody's angular velocity and the velocity of it's mass
  200
+            center is typically defined with respect to an inertial frame but
  201
+            any relevant frame in which the velocity is known can be supplied.
  202
+
  203
+        Examples
  204
+        ========
  205
+
  206
+        >>> from sympy.physics.mechanics import Point, ReferenceFrame, outer
  207
+        >>> from sympy.physics.mechanics import RigidBody
  208
+        >>> from sympy import symbols
  209
+        >>> M, v, r, omega = symbols('M v r omega')
  210
+        >>> N = ReferenceFrame('N')
  211
+        >>> b = ReferenceFrame('b')
  212
+        >>> b.set_ang_vel(N, omega * b.x)
  213
+        >>> P = Point('P')
  214
+        >>> P.set_vel(N, v * N.x)
  215
+        >>> I = outer (b.x, b.x)
  216
+        >>> inertia_tuple = (I, P)
  217
+        >>> B = RigidBody('B', P, b, M, inertia_tuple)
  218
+        >>> B.kinetic_energy(N)
  219
+        M*v**2/2 + omega**2/2
  220
+
  221
+        """
  222
+
  223
+        rotational_KE = (self.frame.ang_vel_in(frame) & (self.inertia[0] &
  224
+                self.frame.ang_vel_in(frame)) / sympify(2))
  225
+
  226
+        translational_KE = (self.mass * (self.masscenter.vel(frame) &
  227
+            self.masscenter.vel(frame)) / sympify(2))
  228
+
  229
+        return rotational_KE + translational_KE
  230
+
  231
+    def set_potential_energy(self, scalar):
  232
+        """Used to set the potential energy of this RigidBody.
  233
+
  234
+        Parameters
  235
+        ==========
  236
+
  237
+        scalar: Sympifyable