<s> from sympy . physics . mechanics import ( dynamicsymbols , ReferenceFrame , Point , <EOL> RigidBody , LagrangesMethod , Particle , <EOL> inertia , Lagrangian ) <EOL> from sympy import symbols , pi , sin , cos , tan , simplify , Function , Derivative , Matrix <EOL> def test_disc_on_an_incline_plane ( ) : <EOL> y , theta = dynamicsymbols ( '<STR_LIT>' ) <EOL> yd , thetad = dynamicsymbols ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> m , g , R , l , alpha = symbols ( '<STR_LIT>' ) <EOL> N = ReferenceFrame ( '<STR_LIT:N>' ) <EOL> A = N . orientnew ( '<STR_LIT:A>' , '<STR_LIT>' , [ pi / <NUM_LIT:2> - alpha , N . z ] ) <EOL> B = A . orientnew ( '<STR_LIT:B>' , '<STR_LIT>' , [ - theta , A . z ] ) <EOL> Do = Point ( '<STR_LIT>' ) <EOL> Do . set_vel ( N , yd * A . x ) <EOL> I = m * R ** <NUM_LIT:2> / <NUM_LIT:2> * B . z | B . z <EOL> D = RigidBody ( '<STR_LIT:D>' , Do , B , m , ( I , Do ) ) <EOL> D . potential_energy = m * g * ( l - y ) * sin ( alpha ) <EOL> L = Lagrangian ( N , D ) <EOL> q = [ y , theta ] <EOL> hol_coneqs = [ y - R * theta ] <EOL> m = LagrangesMethod ( L , q , hol_coneqs = hol_coneqs ) <EOL> m . form_lagranges_equations ( ) <EOL> rhs = m . rhs ( ) <EOL> rhs . simplify ( ) <EOL> assert rhs [ <NUM_LIT:2> ] == <NUM_LIT:2> * g * sin ( alpha ) / <NUM_LIT:3> <EOL> def test_simp_pen ( ) : <EOL> q , u = dynamicsymbols ( '<STR_LIT>' ) <EOL> qd , ud = dynamicsymbols ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> l , m , g = symbols ( '<STR_LIT>' ) <EOL> N = ReferenceFrame ( '<STR_LIT:N>' ) <EOL> A = N . orientnew ( '<STR_LIT:A>' , '<STR_LIT>' , [ q , N . z ] ) <EOL> A . set_ang_vel ( N , qd * N . z ) <EOL> O = Point ( '<STR_LIT:O>' ) <EOL> O . set_vel ( N , <NUM_LIT:0> ) <EOL> P = O . locatenew ( '<STR_LIT:P>' , l * A . x ) <EOL> P . v2pt_theory ( O , N , A ) <EOL> Pa = Particle ( '<STR_LIT>' , P , m ) <EOL> Pa . potential_energy = - m * g * l * cos ( q ) <EOL> L = Lagrangian ( N , Pa ) <EOL> lm = LagrangesMethod ( L , [ q ] ) <EOL> lm . form_lagranges_equations ( ) <EOL> RHS = lm . rhs ( ) <EOL> assert RHS [ <NUM_LIT:1> ] == - g * sin ( q ) / l <EOL> def test_nonminimal_pendulum ( ) : <EOL> q1 , q2 = dynamicsymbols ( '<STR_LIT>' ) <EOL> q1d , q2d = dynamicsymbols ( '<STR_LIT>' , level = <NUM_LIT:1> ) <EOL> L , m , t = symbols ( '<STR_LIT>' ) <EOL> g = <NUM_LIT> <EOL> N = ReferenceFrame ( '<STR_LIT:N>' ) <EOL> pN = Point ( '<STR_LIT>' ) <EOL> pN . set_vel ( N , <NUM_LIT:0> ) <EOL> P = pN . locatenew ( '<STR_LIT>' , q1 * N . x + q2 * N . y ) <EOL> P . set_vel ( N , P . pos_from ( pN ) . dt ( N ) ) <EOL> pP = Particle ( '<STR_LIT>' , P , m ) <EOL> f_c = Matrix ( [ q1 ** <NUM_LIT:2> + q2 ** <NUM_LIT:2> - L ** <NUM_LIT:2> ] ) <EOL> Lag = Lagrangian ( N , pP ) <EOL> LM = LagrangesMethod ( Lag , [ q1 , q2 ] , hol_coneqs = f_c , <EOL> forcelist = [ ( P , m * g * N . x ) ] , frame = N ) <EOL> LM . form_lagranges_equations ( ) <EOL> lam1 = LM . lam_vec [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> eom_sol = Matrix ( [ [ m * Derivative ( q1 , t , t ) - <NUM_LIT> * m + <NUM_LIT:2> * lam1 * q1 ] , <EOL> [ m * Derivative ( q2 , t , t ) + <NUM_LIT:2> * lam1 * q2 ] ] ) <EOL> assert LM . eom == eom_sol <EOL> lam_sol = Matrix ( [ ( <NUM_LIT> * q1 + <NUM_LIT:2> * q1d ** <NUM_LIT:2> + <NUM_LIT:2> * q2d ** <NUM_LIT:2> ) / ( <NUM_LIT:4> * q1 ** <NUM_LIT:2> / m + <NUM_LIT:4> * q2 ** <NUM_LIT:2> / m ) ] ) <EOL> assert LM . solve_multipliers ( sol_type = '<STR_LIT>' ) == lam_sol <EOL> def test_dub_pen ( ) : <EOL> q1 , q2 = dynamicsymbols ( '<STR_LIT>' ) <EOL> q1d , q2d = dynamicsymbols ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> q1dd , q2dd = dynamicsymbols ( '<STR_LIT>' , <NUM_LIT:2> ) <EOL> u1 , u2 = dynamicsymbols ( '<STR_LIT>' ) <EOL> u1d , u2d = dynamicsymbols ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> l , m , g = symbols ( '<STR_LIT>' ) <EOL> N = ReferenceFrame ( '<STR_LIT:N>' ) <EOL> A = N . orientnew ( '<STR_LIT:A>' , '<STR_LIT>' , [ q1 , N . z ] ) <EOL> B = N . orientnew ( '<STR_LIT:B>' , '<STR_LIT>' , [ q2 , N . z ] ) <EOL> A . set_ang_vel ( N , q1d * A . z ) <EOL> B . set_ang_vel ( N , q2d * A . z ) <EOL> O = Point ( '<STR_LIT:O>' ) <EOL> P = O . locatenew ( '<STR_LIT:P>' , l * A . x ) <EOL> R = P . locatenew ( '<STR_LIT:R>' , l * B . x ) <EOL> O . set_vel ( N , <NUM_LIT:0> ) <EOL> P . v2pt_theory ( O , N , A ) <EOL> R . v2pt_theory ( P , N , B ) <EOL> ParP = Particle ( '<STR_LIT>' , P , m ) <EOL> ParR = Particle ( '<STR_LIT>' , R , m ) <EOL> ParP . potential_energy = - m * g * l * cos ( q1 ) <EOL> ParR . potential_energy = - m * g * l * cos ( q1 ) - m * g * l * cos ( q2 ) <EOL> L = Lagrangian ( N , ParP , ParR ) <EOL> lm = LagrangesMethod ( L , [ q1 , q2 ] ) <EOL> lm . form_lagranges_equations ( ) <EOL> assert simplify ( l * m * ( <NUM_LIT:2> * g * sin ( q1 ) + l * sin ( q1 ) * sin ( q2 ) * q2dd <EOL> + l * sin ( q1 ) * cos ( q2 ) * q2d ** <NUM_LIT:2> - l * sin ( q2 ) * cos ( q1 ) * q2d ** <NUM_LIT:2> <EOL> + l * cos ( q1 ) * cos ( q2 ) * q2dd + <NUM_LIT:2> * l * q1dd ) - lm . eom [ <NUM_LIT:0> ] ) == <NUM_LIT:0> <EOL> assert simplify ( l * m * ( g * sin ( q2 ) + l * sin ( q1 ) * sin ( q2 ) * q1dd <EOL> - l * sin ( q1 ) * cos ( q2 ) * q1d ** <NUM_LIT:2> + l * sin ( q2 ) * cos ( q1 ) * q1d ** <NUM_LIT:2> <EOL> + l * cos ( q1 ) * cos ( q2 ) * q1dd + l * q2dd ) - lm . eom [ <NUM_LIT:1> ] ) == <NUM_LIT:0> <EOL> def test_rolling_disc ( ) : <EOL> q1 , q2 , q3 = dynamicsymbols ( '<STR_LIT>' ) <EOL> q1d , q2d , q3d = dynamicsymbols ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> r , m , g = symbols ( '<STR_LIT>' ) <EOL> N = ReferenceFrame ( '<STR_LIT:N>' ) <EOL> Y = N . orientnew ( '<STR_LIT:Y>' , '<STR_LIT>' , [ q1 , N . z ] ) <EOL> L = Y . orientnew ( '<STR_LIT:L>' , '<STR_LIT>' , [ q2 , Y . x ] ) <EOL> R = L . orientnew ( '<STR_LIT:R>' , '<STR_LIT>' , [ q3 , L . y ] ) <EOL> C = Point ( '<STR_LIT:C>' ) <EOL> C . set_vel ( N , <NUM_LIT:0> ) <EOL> Dmc = C . locatenew ( '<STR_LIT>' , r * L . z ) <EOL> Dmc . v2pt_theory ( C , N , R ) <EOL> I = inertia ( L , m / <NUM_LIT:4> * r ** <NUM_LIT:2> , m / <NUM_LIT:2> * r ** <NUM_LIT:2> , m / <NUM_LIT:4> * r ** <NUM_LIT:2> ) <EOL> BodyD = RigidBody ( '<STR_LIT>' , Dmc , R , m , ( I , Dmc ) ) <EOL> BodyD . potential_energy = - m * g * r * cos ( q2 ) <EOL> Lag = Lagrangian ( N , BodyD ) <EOL> q = [ q1 , q2 , q3 ] <EOL> q1 = Function ( '<STR_LIT>' ) <EOL> q2 = Function ( '<STR_LIT>' ) <EOL> q3 = Function ( '<STR_LIT>' ) <EOL> l = LagrangesMethod ( Lag , q ) <EOL> l . form_lagranges_equations ( ) <EOL> RHS = l . rhs ( ) <EOL> RHS . simplify ( ) <EOL> t = symbols ( '<STR_LIT:t>' ) <EOL> assert ( l . mass_matrix [ <NUM_LIT:3> : <NUM_LIT:6> ] == [ <NUM_LIT:0> , <NUM_LIT:5> * m * r ** <NUM_LIT:2> / <NUM_LIT:4> , <NUM_LIT:0> ] ) <EOL> assert RHS [ <NUM_LIT:4> ] . simplify ( ) == ( <EOL> ( - <NUM_LIT:8> * g * sin ( q2 ( t ) ) + r * ( <NUM_LIT:5> * sin ( <NUM_LIT:2> * q2 ( t ) ) * Derivative ( q1 ( t ) , t ) + <EOL> <NUM_LIT:12> * cos ( q2 ( t ) ) * Derivative ( q3 ( t ) , t ) ) * Derivative ( q1 ( t ) , t ) ) / ( <NUM_LIT:10> * r ) ) <EOL> assert RHS [ <NUM_LIT:5> ] == ( - <NUM_LIT:5> * cos ( q2 ( t ) ) * Derivative ( q1 ( t ) , t ) + <NUM_LIT:6> * tan ( q2 ( t ) <EOL> ) * Derivative ( q3 ( t ) , t ) + <NUM_LIT:4> * Derivative ( q1 ( t ) , t ) / cos ( q2 ( t ) ) <EOL> ) * Derivative ( q2 ( t ) , t ) </s>
