A Python Project For Lagrangian Mechanics
A Python Project For Lagrangian Mechanics
Abstract
Climate change mitigation imposes a world-wide technological challenge for both gov-
ernment and industry. The European commission translated this challenge into an En-
ergy Eciency Directive that establishes a common framework of measures to ensure
20% power reduction in 2020. The industrial sector is responsible for around 24 % of
the total energy consumption. Within the industrial sector the manufacturing indus-
try accounts for a large fraction of this energy consumption. Technological advances
like advanced control or mechanical redesign can signicantly improve the energy e-
ciency of many manufacturing processes. This paper presents a platform independent,
open source software application that enables end-users to specify a dynamic model and
analyse its response. The system dynamics are specied using Lagrangian mechanics.
The elegance of Lagrangian mechanics resides in the straightforward design procedure
based on the Lagrangian. However, conversion of the Lagrangian to a dynamic model is
computational intensive and often impossible to perform manually. These computations
are performed by the symbolic library 'Sympy' written in Python, a popular platform
independent general-purpose, high-level programming language. Simulation results of
common mechanical systems are shown that illustrate the user-friendly design interface
and the straightforward design procedure.
1 Introduction
Reduction of energy consumption is an important issue all over the world because of rising
energy prices and the increased attention for their direct or in-direct environmental pollution.
∗
Peter Slaets, email:<rstname>.<lastname>@groept.be,tel:+3216301075, fax:+3216301040
†
Pauwel Goethals, email:<rstname>.<lastname>@groept.be,tel:+3216301074, fax:+3216301040
‡
Dries Haeseldonckx, email:<rstname>.<lastname>@groept.be,tel:+3216301071, fax:+3216301040
§
Sietze Swolfs, email:<rstname>.<lastname>@groept.be,tel:+3216301071, fax:+3216301040
¶
Maarten Vanierschot, email:<rstname>.<lastname>@groept.be,tel:+3216301071, fax:+3216301040
Peter Slaets et al. /The 3rd International Symposium on Engineering, Energy and Environments
The European commission imposed an Energy Eciency Directive [1] that establishes a com-
mon framework of measures to ensure 20% power reduction in 2020. The European industrial
sector is responsible for around 24 % of the total energy consumption. Within the industrial
sector the manufacturing industry accounts for a large fraction of this energy consumption.
Technological advances in the eld of energy monitoring , energy management systems
and energy ecient actuation can signicantly improve the energy eciency of many manu-
facturing processes. Eort have been done to exploit inherent dynamical properties of the
dynamical system to build up a motion planning framework [2, 3]. Others try to improve the
motion trajectories of industrial machinery e.g. machine tools [4] or industrial robots [6]. In
all theses cases an analytic dynamic model of the system is required.
This paper presents a platform independent open source software application that simpli-
es the build-up and simulation of a mechanical dynamic model. The paper is build up as
follows: In a rst section Lagrangian mechanics [7] is explained and an elegant design proce-
dure for Lagrangian mechanics is proposed. However, conversion from the Lagrangian to a
dynamic model is computational intensive and often impossible to perform manually. There-
fore a Python software GUI application based on the symbolic library SymPy [5] is used to
perform these computations. The software application is free to use, even for commercial
products, and is distributed using a open source GPL license. In the last section the design
procedure is applied to two common mechanical systems i.e. the double pendulum and a dual
driven electric vehicle. The results shown illustrate the user-friendly design interface and the
straightforward design procedure.
2 Lagrangian mechanics
Lagrangian mechanics is build up around a quantity called the Lagrangian. Consider a sys-
tem with m motion degrees of freedom, denoted by the m-dimensional generalized coordinate
vector q1 = [q1 , . . . , qm ]T . The kinetic energy generally depends on both the coordinate and
velocity q̇2 , so we write it as T (q, q̇). Similarly, we write the potential energy of the system
as U (q, q̇).
The system dynamics are fully specied by the equation of motion. This is given by the
2nd kind of Lagrange equations based on a set of m dierential equations:
d ∂L(q, q̇) ∂L(q, q̇)
= + Fqj (2)
dt ∂ q̇j ∂qj
where j = 1, 2, ...m represents the j-th degree of freedom with corresponding generalized co-
ordinate qj , and generalized velocity q̇j . The generalized force for each degree of freedom Fqj
is chosen such that the product Fqj δqj quanties the work done by the driving forces when
1
All vectors are indicated in bold
2
The dot on top of a symbol refers to the time derivative q̇ = dq
dt
Peter Slaets et al. /The 3rd International Symposium on Engineering, Energy and Environments
qj is changed by δqj .
3. Compute ∂L(q,
∂ q̇j
q̇)
and derive dtd ∂L(q,
∂ q̇j
q̇)
for all degrees of freedom j = 1, 2, ...m . It
is important that q˙j is treated as a complete variable in its own right, and not as a
derivative.
4. Write down dtd ∂L(q,
∂ q̇j
q̇)
= ∂L(q,
∂qj
q̇)
for all degrees of freedom j = 1, 2, ...m . These are the
Euler-Lagrange equations.
5. Rewrite the set of dierential equations obtained in the previous step as a set of Ordinary
Dierential Equations (ODE's).
q̈1 = f (q̇2 , . . . , q̇m , Fq1 ) (3)
..
. (4)
q̈m = f (q̇1 , . . . , q̇m−1 , , Fqm )
At this point, q˙j is treated as a normal variable. Note that in general the system
dynamics consists of a set of m ODE's.
3 Applications
In this section the dynamic model equations of two common mechanical systems, i.e. a
double pendulum and a dual drive vehicle, are derived using the Lagrangian Python GUI and
a simulation is performed. Both examples illustrate the modelling and simulation capabilities
of the designed Python application. An installation packages for various platform (Unix,
Windows, Mac) can be found at http://code.google.com/p/euler-lagrange-dynamics/.
Figure 2: The python GUI for a double pendulum with degrees of freedom q1 = θ1 , q2 = θ2
and parameters m1, m2, l1, l2, g and corresponding initial value (q1 (0) = 0, q2 (0) = 0, q̇1 (0) =
0, q̇2 (0) = 0) and (m1 = 1, m2 = 1, l1 = 1, l2 = 1, g = 9.8). To specify the Lagrangian
the kinetic en potential energy function are given together with the generalized force and
simulation time.
Figure 3: The simulation results for the position and velocity of both degrees of freedom
q1 = θ1 , q2 = θ2 resulting from the input given in gure 2.
Peter Slaets et al. /The 3rd International Symposium on Engineering, Energy and Environments
The potential energy is equal to zero because we assume that there is no vertical displacement
and the kinetic energy is given by
1 1 1
T (q, q̇) = mv 2 + Jc θ̇2 + Jw (ωR
2
+ ω̇L2 )
2 2 2
The previously specied kinetic and potential energy are inserted in the Python GUI.
Figure 5: The python GUI lled in for a dual driven vehicle with degrees of freedom q1 =
ωL , q2 = ωR and parameters m, Jc, Jw, R, b and corresponding initial value (q1 (0) = 0, q2 (0) =
0, q̇1 (0) = 0, q̇2 (0) = 0) and (m = 10, Jc = 0.1, Jw = 0.01, R = 0.2, b = 0.3). To specify the
Lagrangian the kinetic en potential energy function are given together with the generalized
force (cos(t); sin(t)) and simulation time (T = 20 sec).
Peter Slaets et al. /The 3rd International Symposium on Engineering, Energy and Environments
Figure 6: This gure shows the simulation results for the position and velocity of both degrees
of freedom q1 = ωL , q2 = ωR resulting from the input given in gure 5.
4 Conclusions
This paper introduces a Python based simulation application for Lagrangian mechanics ap-
plied to two well-known mechanical systems. The resulting GUI generates the symbolic dy-
namic model equations based on the user input and performs a numerical simulation. Future
work will focus on the extension of the package to Hamiltonian mechanics to directly obtain
a set of rst order dierential equations.
References
[1] European Parliament and Council. Directive 2012/27/EU of the European Parliament and of the
Council of 25 October 2012 on energy eciency. ISSN 1977-0677, October 2012.
[2] K. Flaskamp and S. Ober-Blobaum. Energy ecient control for mechanical systems based on
inherent dynamical structures. In American Control Conference (ACC), 2012, pages 26092614,
2012.
[3] P. Slaets, J. Janssenswillen, and S. Swinnen, R. Swolfs. Optimal Dynamic Predictive Cruise
Control for dierential driven electric vehicle. In 2012 EVS26 Online Conference Proceedings, Los
Angeles, USA. EDTA Electric Drive Transportation Association, may 2012.
[4] V. Smirnov, V. Plyusnin, and G. Mirzaeva. Energy ecient trajectories of industrial machine tools
with parallel kinematics. In Industrial Technology (ICIT), 2013 IEEE International Conference
on, pages 12671272, 2013.
[5] SymPy Development Team. SymPy: Python library for symbolic mathematics , 2008.
[6] D. Verscheure, B. Demeulenaere, J. Swevers, J. De Schutter, and M. Diehl. Time-optimal path
tracking for robots: A convex optimization approach. Automatic Control, IEEE Transactions on,
54(10):23182327, 2009.
[7] D. Wells. Schaum's Outline of Lagrangian Dynamics . Schaum's Outline Series. McGraw-Hill
Education, 1967.