Some Applications of Automatic Differentiation to Rigid, Flexible, and Constrained Multibody Dynamics D. T. Griffith Sandia National Laboratories J. D. Turner Dynacs J. L. Junkins Texas A&M University ASME 2005 International Design Engineering Technical Conferences September 24, 2005 Folk theorem The Chain Rule, when applied by hand recursively to even moderately dimensioned nonlinear systems, for more than two orders of differentiation, stops being fun around age 45. Empirical Evidence: The master differentiator, James Turner tried to automate the process completely during his late forties. 2 Presentation Outline • Introduction to Automatic Differentiation • Modeling • Rigid and flexible multibody systems • Constrained multibody systems • Automatic generation of exact PDE/ODEs and BCs for hybrid systems validation of approximate solutions • Numerical Examples 3 Comparison of Computerized Differentiation Approaches • Symbolic Differentiation – Very useful and popular – Matlab, Maple, Mathcad, Macsyma – Pros: Can view expressions & control simplification/ approximation on screen – Cons: Result in lengthy expressions and require code generation/porting/de-bugging • Automatic Differentiation (AD) – Generally, not as well-known as Symbolic Differentiation – Recursive, automated use of Chain Rule … – Begins with FORTRAN, C or …code that computes the functions to be differentiated – ADIFOR, AD01, ADOL-C, AUTODERIVE, OCEA – Pro: Derivatives computed & possibly coded without (*much*) user intervention – Historical Cons: Limited order of differentiation and also, usually requires code generation OCEA is a novel AD approach that enables 1st- through 4th-order derivatives, with zero code generation required. There are very important implications in many fields, including multi-body dynamics. 4 Overview of automatic differentiation by OCEA (1) – OCEA (Object Oriented Coordinate Embedding Method) – Define new data structure (OCEA second-order method), for example for the function scalar f : f ( x ) : f f 2 f – All intrinsic operators ( +, - , * , / , = ) are overloaded to enable, for example, addition and multiplication of OCEA variables. Examples: You code: OCEA computes: f1 f 2 : f1 f 2 f1 f 2 2 f1 2 f 2 f1 * f 2 : f1 * f 2 f1 * f 2 f1 * f 2 2 f1 * f 2 2f1 * f 2T f1 * 2 f2 – Composite Functions (chain rule, use recursively, numerically!): You code: OCEA computes: 2 f f f 2 T 2 f g : f g , g , g : f g , g , 2 g g g g g g – Let’s take a look at an illustrative example…. 5 Overview of automatic differentiation by OCEA (2) • A second-order OCEA example 3 2 f x y , f y sin( x), • Suppose: 1 2 and f 3 f1 f 2 OCEA-FORTRAN ! X(1) = x, X(2) = y f3 : f1 f 2 Type(EB):: X(2) We Declare OCEA independent 2 variables , type EB 2 f1 f 2 f1 f 2 desire to Type(EB)::compute F1, F2, F3 f3 and most : f3 f3 Declare 2 f3 OCEA dependent variables, type EB importantly its partials: Real(DP):: S_F3, JAC_F3(2), HES_F3(2,2) ! Code Functions F1 = X(1)**3 + X(2) function Jacobian F2 = (X(2)**2)*sin( X(1) ) F3 = F1 + F2 S_F3 = F3 JAC_F3 = F3 HES_F3 = F3 Declare function and partial derivative arrays , type DP Hessian Code functions partial You never had to derive, code,Extract or debug: 2 f3 2 2 2 x 3 x y cos x x ! Extract Jacobian (2x1) , 2 f 3 1 2 y sin x f 3 ! Extract Hessian (2x2) y xy ! Extractfunction f 3 2 f 3 derivative xy information 6 x y 2 sin x 2 f 3 2 y cos x y 2 2 y cos x 2sin x 6 Equation of motion formulation using automatic differentiation Lagrange’s equations: Lagrangian: L T V d L L T QC λ dt q q T, V: kinetic and potential energy Cq b C(q): constraint Jacobian matrix subject to q: generalized coordinates Q: generalized force l: Lagrange multipliers Of course, many choices for equation of motion formulation exist. The OCEA approach is broadly applicable, however, the utility of automatic differentiation can be immediately seen and appreciated for implementing Lagrange’s Equations… 7 Implementation of Lagrange’s Equations: A Direct Approach (1) d L L T Q C λ dt q q d T 2T 2T qj qj dt qi qi q j qi q j M mij T qi q j 2 2T M mij qi q j d T L T Q C λ dt q q L q Q C l L Automatic Differentiation (AD) Specified & coded AD or specified & coded Many numerical methods AD or specified & coded Mass matrix and its time derivative extracted as second-order Automatic Differentiation of T … 8 Implementation of Lagrange’s Equations: A Direct Approach (2) Can also compute constraint matrix, C, automatically for holonomic type constraint. (q, t ) 0 (q , t ) q = Cq 0 C q t t q Now forming the equations: L Mq + Mq Q CT λ q L T q = M Mq + Q C λ q -1 2T M mij qi q j 2T M mij qi q j Accelerations computed after generating or prescribing all terms here. Now, can proceed with numerical integration……. 9 Illustrative Example: Spring Pendulum (1) r V 12 k (r r0 ) 2 mg (r0 r cos ) T 12 m(r 2 r 2 2 ) L T V θ 2T 2T M (q ) mij M (q ) mij qi q j qi q j Lagranges Eqs: L -1 T q = M Mq + Q C λ q Directly invoke ODE solver in OCEA to get solution W/O hand derivation, coding, or checkout … only derive & code T, V 10 Spring Pendulum (2) SUBROUTINE SPRING_PEND_EQNS( PASS, TIME, X, DXDT, FLAG ) USE EB_HANDLING IMPLICIT NONE **************************************** !.....LOCAL VARIABLES TYPE(EB)::L, T, V ! LAGRANGIAN, KINETIC, POTENTIAL REAL(DP):: M, K ! MASS AND STIFFNESS VALUES REAL(DP), DIMENSION(NV):: JAC_L REAL(DP), DIMENSION(NV,NV):: HES_L **************************************** ! X(1) = Q(1) = R, X(2) = Q(2) = THETA, X(3) = dR/dt, X(4) = dTHETA/dt R0 = 0.55D0 GRAV = 9.81D0 T = 0.5D0*M*(X(3)**2 + X(1)**2*X(4)**2) ! DEFINE KE V = 0.5D0*K*(X(1)-R0)**2 + M*GRAV*(R0-X(1)*COS(X(2))) ! DEFINE PE L=T–V ! DEFINE LAGRANGIAN FUNCTION JAC_L = L JAC_L_Q = JAC_L(1:NV/2) ! EXTRACT dL/(dq,dqdot) ! EXTRACT dL/dq HES_L = L ! EXTRACT 2nd ORDER PARTIALS OF LAGRANGIAN MASS = HES_L(NV/2+1:NV,NV/2+1:NV) ! EXTRACT MASS MATRIX MASSDOT = HES_L(NV/2+1:NV,1:NV/2) ! EXTRACT MDOT ! QDOTDOT = INV(MASS)*(- MASSDOT*Q + JAC_L_Q ) ! SEE PAPER … **************************************** DXDT(1)%E = X(3)%E ! RDOT DXDT(2)%E = X(4)%E ! THETADOT DXDT(3)%E = QDOTDOT(1) ! RDOTDOT DXDT(4)%E = QDOTDOT(2) ! THETADOTDOT END SUBROUTINE SPRING_PEND_EQNS 11 Geometry of Multiple Flexible Link Configuration ˆj 2 iˆ2 2 iˆ1 ˆj p+1 iˆp+ 1 p 1 ˆj 1 1 Note from the figure of a series of deformed links that the local coordinate frames attached to the links are defined such that the elastic deformation vanishes at the endpoints. This choice greatly simplifies the “downstream” velocity expressions. 12 Kinetic Energy Generalization for Flexible Links Tp 1 1 2 L p 1 0 p 1 ( x p 1 ) rp 1 ( x p 1 , t ) rp 1 ( x p 1 , t ) dx p 1 Assumed Modes Method used to transform the kinetic energy expression into a form which can be tamed by Lagrange’s Equations. The details can be found in the paper. Tp 1 Tp 1 (q, q, θ , θ ) 12 m p 1 Li i L j j cos j i p 1qTp 1bp 1 Li i sin p 1 i p p p i 1 j 1 q b T p 1 p 1 L cos i 1 p i i i 1 i 12 p 1m p 1 Lp 1 Li i cos p 1 i p p 1 i 1 12 p21qTp 1 M p 1q p 1 12 qTp 1 M p 1q p 1 p 1qTp 1a p 1 16 m p 1 L2p 1 p21 Along with the potential energy expression, we can define the Lagrangian and proceed to generate the equations of motion………….. 13 Example: Closed-chain 5-link Manipulator (1) The Lagrangian expressions are developed for each link and summed to form the total system Lagrangian. 5 link manipulator g D Additionally, we specify two holonomic constraints of the form: 1 5 2 4 3 5 Li cos( i ) D i 1 φ= 0 5 Li sin( i ) i 1 And, we specify damping at all joints with the exception of the base end of link 5. OCEA automatically generates the equations of motion for the constrained system 14 Example: Closed-chain 5-link Manipulator (2) 15 Example: 4-link Planar Truss (1) 4-link planar truss with “springs” across diagonal Now, we consider translation as well. Additionally, we specify two holonomic constraints of the form: 3 2 Y The Lagrangian expressions are developed for each link and summed to form the total system Lagrangian. 4 1 X 4 Li cos( i ) i 1 φ= 0 4 Li sin( i ) i 1 OCEA automatically generates the equations of motion for the constrained system 16 Example: 4-link Planar Truss (2) Starting with zero flexible energy initially, we can assess the suitability of rigid body modeling. Question: How much flexibility exists in the system? 17 Accuracy of Solutions: Comparison with Hard-coded Equations of Motion for Flexible Double Pendulum(1) Angular coordinates and angular velocities 20 Accuracy of Solutions: Comparison with Hardcoded Equations of Motion for Double Pendulum(2) Flexible coordinates for link one 21 Methods for Validating Solution Accuracy 1) Testing special cases that have exact analytical solutions 2) Evaluation of error in exact motion integrals 3) The method of manufactured solutions 4) The method of “nearby problems” Method #1 is a standard approach to validating solution accuracy. Here we look at a simple system which has an exact analytical solution. Methods #3 and #4 rely upon computing analytical source terms (fictitious generalized forces) by inverse dynamics which define a benchmark problem for validation studies. The difference is that with method #3 a benchmark solution is chosen a priori and may have no important physical meaning. With method #4, a benchmark solution is constructed as a neighbor of a candidate approximate solution for the motion being studied and thus has physical meaning. 22 Addendum to JLJ Folk Theorem Generating the exact ODE/PDE and boundary conditions for a distributed parameter system by hand or symbolic manipulation stops being fun at age 29! 27 Generating Exact PDE/ODEs and BCs for Many Body Distributed Parameter Systems li ˆ L LD Li dxi LB i 1 0 You code this. d L L QT dt q q OCEA/AD computes these. n Lagrangian: Discrete/ODE: Flexible/PDE: BCs: d Lˆi Lˆi Lˆi 2 Lˆi ˆ T 2 fi ' '' dt vi vi xi vi xi vi li Lˆ Lˆi i vi ' '' vi xi vi 0 LB d LB 1T ˆ v ( l ) f i i i vi (li ) 0 vi (li ) dt vi (li ) i L ˆ Li d LB ' ' B vi (li ) fˆi 2T vi ' (li ) 0 vi ' (l ) dt v ' (l ) vi '' v 28 i i i i 0 l Generating Exact PDE/ODEs: Double Pendulum We can hard-code the exact equations of motion for the double pendulum and compare the difference with between these and the OCEA derived numerically evaluated equations. LD 16 m1 L1212 16 m2 L22 22 LB 12 m2 L1212 12 m2 L1 L21 2 cos( 2 1 ) Lˆ1 12 1 v v 2 x1v11 12 EI1 v1 2 2 1 1 2 1 '' 2 2 2 2 1 v v 2 x v 2 L v sin( ) 2 2 2 2 2 2 1 1 2 2 2 1 '' 1 Lˆ2 2 2 2 EI 2 v2 2 L11v2 cos( 2 1 ) 2 31 Errors in ODEs: Double Pendulum 32 Errors in PDEs: Double Pendulum Link one Link two 33 Summary and Conclusions •Automatic Differentiation has broad potential in Dynamics and Control • Automating differentiation by operator overloading is a beautiful idea whose time has come – the applications are endless! • Ideal for implementing Lagrange’s Equations – this presentation provides some simple illustrations – a few important first steps along the path. • No hand derivation effort beyond code specifying the building of the Lagrangian, constraints and external forces is required. • OCEA also gives rise to new methods for solving differential equations – the time derivative computations can be automated, so that formal analytical continuation is easily done in lieu of traditional Runge-Kutta methods, for example. • Can automatically derive systems of PDEs and BCs for use in validating the accuracy of approximate solutions of flexible systems corresponding to the same modeling assumptions? 34