System (Docstrings)#

class sympy.physics.mechanics.system.SymbolicSystem(coord_states, right_hand_side, speeds=None, mass_matrix=None, coordinate_derivatives=None, alg_con=None, output_eqns={}, coord_idxs=None, speed_idxs=None, bodies=None, loads=None)[源代码]#

SymbolicSystem是一个类,它以符号格式包含有关系统的所有信息,例如运动方程、系统中的实体和载荷。

符号系统的运动方程有三种描述方法:

[1] 运动学和动力学结合的显式形式

x'=F_1(x,t,r,p)

[2] 运动学和动力学相结合的隐式形式

M_2(x,p)x'=F_2(x,t,r,p)

[3] 运动学和动力学分离的隐式形式

M_3(q,p)u'=F_3(q,u,t,r,p)q'=G(q,u,t,r,p)

在哪里?

x:状态,例如。 [q, u] t:时间r:指定(外生)输入p:常数q:广义坐标u:广义速度F_1:显式组合方程的右侧F_2:隐式组合方程的右侧F_3:动力方程隐式形式的右侧M 2:组合方程的质量矩阵隐式M_3:动力学方程的质量矩阵隐式G:运动微分方程的右手边

参数:

coord_states :时间函数的有序iterable

该输入将是坐标或系统状态的集合,具体取决于速度是否也给定。否则将假定输入的是速度。

right_hand_side矩阵

这个变量是任何形式的运动方程的右边。具体形式取决于是否给出质量矩阵或坐标导数。

速度时间函数的有序iterable,可选

这是系统广义速度的集合。如果给定,则假定第一个参数(坐标状态)将表示系统的广义坐标。

mass_matrix矩阵,可选

运动方程隐式形式的矩阵 [2] 和 [3] ). 形式之间的区别取决于是否传入坐标导数。如果他们被给予形式 [3] 以其他形式出现 [2] 假设。

coordinate_derivatives矩阵,可选

运动方程的右端显式形式。如果给定,则假定运动方程以形式输入 [3] .

alg_conIterable,可选

运动方程中包含代数约束而不是微分方程的行的索引。如果方程以形式输入 [3] ,假设指数参考的是质量矩阵/右手边组合,而不是坐标导数。

output_eqns字典,可选

任何需要跟踪的输出方程都存储在一个字典中,其中的键对应于给定的特定表达式的名称,而值则是表达式本身的符号形式

coord_idxsIterable,可选

如果坐标状态对应于状态而不是坐标,这个变量将告诉SymbolicSystem哪些状态索引对应于广义坐标。

speed_idxsIterable,可选

如果坐标状态对应于状态而不是坐标,这个变量将告诉符号系统哪些状态指标对应于广义速度。

身体实体/刚体对象的iterable,可选

包含系统主体的Iterable

荷载iterable of load instances(如下所述),可选

Iterable包含系统的载荷,其中力由(作用点,力矢量)给出,转矩由(作用于的参考坐标系,转矩矢量)给出。前任 [(point, force), (ref_frame, torque)]

例子

作为一个简单的例子,单摆的动力学将被手动输入到SymbolicSystem对象中。首先需要一些输入,然后为摆锤的长度(l)、摆锤末端的质量(m)和重力常数(g)设置符号。:

>>> from sympy import Matrix, sin, symbols
>>> from sympy.physics.mechanics import dynamicsymbols, SymbolicSystem
>>> l, m, g = symbols('l m g')

系统将由与垂直方向的θ角定义,当ω=θu点时,将使用ω的广义速度。:

>>> theta, omega = dynamicsymbols('theta omega')

现在,运动方程已经准备好,可以传递给SymbolicSystem对象。:

>>> kin_explicit_rhs = Matrix([omega])
>>> dyn_implicit_mat = Matrix([l**2 * m])
>>> dyn_implicit_rhs = Matrix([-g * l * m * sin(theta)])
>>> symsystem = SymbolicSystem([theta], dyn_implicit_rhs, [omega],
...                            dyn_implicit_mat)

笔记

m:广义速度数n:广义坐标数o:状态数

属性

协调

(矩阵,形状(n,1))这是一个包含系统广义坐标的矩阵

速度

(矩阵,形状(m,1))这是一个包含系统广义速度的矩阵

状态

这个系统的状态(包含一个变量的矩阵)

alg_con

(列表)此列表包含组合运动方程中代数约束的索引。这些约束的存在要求使用DAE解算器而不是ODE解算器。如果系统以表格形式给出 [3] alg_con变量将进行调整,使其表示运动学和动力学的组合,从而确保其始终与输入的质量矩阵相匹配。

dyn_implicit_mat

(矩阵,形状(m,m))这是形式上的m矩阵 [3] 运动方程(隐式运动方程的质量矩阵或广义惯性矩阵)。

dyn_implicit_rhs

(矩阵,形状(m,1))这是形式上的F向量 [3] 运动方程(运动方程的右侧隐式形式)。

comb_implicit_mat

(矩阵,形状(o,o))这是形式上的M矩阵 [2] 运动方程。该矩阵包含一个块对角结构,其中左上角的块(第一行)表示运动方程的隐式形式的矩阵,右下角的块(最后一行)表示动力学方程隐式形式的矩阵。

comb_implicit_rhs

(矩阵,形状(o,1))这是形式上的F向量 [2] 运动方程。矢量的顶部表示运动方程隐式形式的右侧,矢量的底部表示运动方程隐式形式的右侧。

comb_explicit_rhs

(矩阵,形状(o,1))这个向量以显式的形式表示组合运动方程的右侧(形式 [1] 从上面看)。

kin_explicit_rhs

(矩阵,形状(m,1))这是运动方程显式形式的右边,可以在形式中看到 [3] (G矩阵)。

output_eqns

(字典)如果给定了输出方程,则将它们存储在字典中,其中键对应于给定的特定表达式的名称,而值则是表达式本身的符号形式

身体

(Tuple)如果系统中的实体是给定的,它们将存储在Tuple中以备将来访问

荷载

(Tuple)如果系统中的负载是给定的,它们将存储在Tuple中以备将来访问。这包括力和力矩,其中力由(作用点,力矢量)给出,转矩由(作用于的参考坐标系,转矩矢量)给出。

property alg_con#

返回一个列表,其中包含运动方程组合形式的代数约束的行的索引

property bodies#

返回系统中的实体

property comb_explicit_rhs#

以显式形式返回运动方程的右侧,其中包括运动方程

property comb_implicit_mat#

返回与隐式形式的运动方程相对应的矩阵M(形式 [2] ),M x'=F,其中包括运动方程

property comb_implicit_rhs#

返回与隐式形式的运动方程相对应的列矩阵F(形式 [2] ),M x'=F,其中包括运动方程

compute_explicit_form()[源代码]#

如果在初始化时提供运动方程的右显式,则该方法将进行计算。这种计算可能需要一段时间才能计算。

constant_symbols()[源代码]#

返回一个列矩阵,其中包含系统中不依赖时间的所有符号

property coordinates#

返回广义坐标的列矩阵

property dyn_implicit_mat#

返回与隐式形式的动力学方程相对应的矩阵M,其中不包括运动方程

property dyn_implicit_rhs#

返回列矩阵F,对应于隐式形式的动力学方程mx'=F,其中不包括运动方程

dynamic_symbols()[源代码]#

返回一个列矩阵,其中包含系统中依赖时间的所有符号

property kin_explicit_rhs#

以显式形式返回运动方程的右侧,q'=G

property loads#

返回系统中的负载

property speeds#

返回广义速度的列矩阵

property states#

返回状态变量的列矩阵

class sympy.physics.mechanics.system.System(frame=None, fixed_point=None)[源代码]#

Class to define a multibody system and form its equations of motion.

解释

A System instance stores the different objects associated with a model, including bodies, joints, constraints, and other relevant information. With all the relationships between components defined, the System can be used to form the equations of motion using a backend, such as KanesMethod. The System has been designed to be compatible with third-party libraries for greater flexibility and integration with other tools.

实例

In the example below a cart with a pendulum is created. The cart moves along the x axis of the rail and the pendulum rotates about the z axis. The length of the pendulum is l with the pendulum represented as a particle. To move the cart a time dependent force F is applied to the cart.

We first need to import some functions and create some of our variables.

>>> from sympy import symbols, simplify
>>> from sympy.physics.mechanics import (
...     mechanics_printing, dynamicsymbols, RigidBody, Particle,
...     ReferenceFrame, PrismaticJoint, PinJoint, System)
>>> mechanics_printing(pretty_print=False)
>>> g, l = symbols('g l')
>>> F = dynamicsymbols('F')

The next step is to create bodies. It is also useful to create a frame for locating the particle with respect to the pin joint later on, as a particle does not have a body-fixed frame.

>>> rail = RigidBody('rail')
>>> cart = RigidBody('cart')
>>> bob = Particle('bob')
>>> bob_frame = ReferenceFrame('bob_frame')

Initialize the system, with the rail as the Newtonian reference. The body is also automatically added to the system.

>>> system = System.from_newtonian(rail)
>>> print(system.bodies[0])
rail

Create the joints, while immediately also adding them to the system.

>>> system.add_joints(
...     PrismaticJoint('slider', rail, cart, joint_axis=rail.x),
...     PinJoint('pin', cart, bob, joint_axis=cart.z,
...              child_interframe=bob_frame,
...              child_point=l * bob_frame.y)
... )
>>> system.joints
(PrismaticJoint: slider  parent: rail  child: cart,
PinJoint: pin  parent: cart  child: bob)

While adding the joints, the associated generalized coordinates, generalized speeds, kinematic differential equations and bodies are also added to the system.

>>> system.q
Matrix([
[q_slider],
[   q_pin]])
>>> system.u
Matrix([
[u_slider],
[   u_pin]])
>>> system.kdes
Matrix([
[u_slider - q_slider'],
[      u_pin - q_pin']])
>>> [body.name for body in system.bodies]
['rail', 'cart', 'bob']

With the kinematics established, we can now apply gravity and the cart force F.

>>> system.apply_uniform_gravity(-g * system.y)
>>> system.add_loads((cart.masscenter, F * rail.x))
>>> system.loads
((rail_masscenter, - g*rail_mass*rail_frame.y),
 (cart_masscenter, - cart_mass*g*rail_frame.y),
 (bob_masscenter, - bob_mass*g*rail_frame.y),
 (cart_masscenter, F*rail_frame.x))

With the entire system defined, we can now form the equations of motion. Before forming the equations of motion, one can also run some checks that will try to identify some common errors.

>>> system.validate_system()
>>> system.form_eoms()
Matrix([
[bob_mass*l*u_pin**2*sin(q_pin) - bob_mass*l*cos(q_pin)*u_pin'
 - (bob_mass + cart_mass)*u_slider' + F],
[                   -bob_mass*g*l*sin(q_pin) - bob_mass*l**2*u_pin'
 - bob_mass*l*cos(q_pin)*u_slider']])
>>> simplify(system.mass_matrix)
Matrix([
[ bob_mass + cart_mass, bob_mass*l*cos(q_pin)],
[bob_mass*l*cos(q_pin),         bob_mass*l**2]])
>>> system.forcing
Matrix([
[bob_mass*l*u_pin**2*sin(q_pin) + F],
[          -bob_mass*g*l*sin(q_pin)]])

The complexity of the above example can be increased if we add a constraint to prevent the particle from moving in the horizontal (x) direction. This can be done by adding a holonomic constraint. After which we should also redefine what our (in)dependent generalized coordinates and speeds are.

>>> system.add_holonomic_constraints(
...     bob.masscenter.pos_from(rail.masscenter).dot(system.x)
... )
>>> system.q_ind = system.get_joint('pin').coordinates
>>> system.q_dep = system.get_joint('slider').coordinates
>>> system.u_ind = system.get_joint('pin').speeds
>>> system.u_dep = system.get_joint('slider').speeds

With the updated system the equations of motion can be formed again.

>>> system.validate_system()
>>> system.form_eoms()
Matrix([[-bob_mass*g*l*sin(q_pin)
         - bob_mass*l**2*u_pin'
         - bob_mass*l*cos(q_pin)*u_slider'
         - l*(bob_mass*l*u_pin**2*sin(q_pin)
         - bob_mass*l*cos(q_pin)*u_pin'
         - (bob_mass + cart_mass)*u_slider')*cos(q_pin)
         - l*F*cos(q_pin)]])
>>> simplify(system.mass_matrix)
Matrix([
[bob_mass*l**2*sin(q_pin)**2, -cart_mass*l*cos(q_pin)],
[               l*cos(q_pin),                       1]])
>>> simplify(system.forcing)
Matrix([
[-l*(bob_mass*g*sin(q_pin) + bob_mass*l*u_pin**2*sin(2*q_pin)/2
 + F*cos(q_pin))],
[
l*u_pin**2*sin(q_pin)]])

属性

框架

(ReferenceFrame) Inertial reference frame of the system.

fixed_point

(Point) A fixed point in the inertial reference frame.

X

(Vector) Unit vector fixed in the inertial reference frame.

Y

(Vector) Unit vector fixed in the inertial reference frame.

Z

(Vector) Unit vector fixed in the inertial reference frame.

q

(ImmutableMatrix) Matrix of all the generalized coordinates, i.e. the independent generalized coordinates stacked upon the dependent.

u

(ImmutableMatrix) Matrix of all the generalized speeds, i.e. the independent generealized speeds stacked upon the dependent.

q_ind

(ImmutableMatrix) Matrix of the independent generalized coordinates.

q_dep

(ImmutableMatrix) Matrix of the dependent generalized coordinates.

u_ind

(ImmutableMatrix) Matrix of the independent generalized speeds.

u_dep

(ImmutableMatrix) Matrix of the dependent generalized speeds.

u_aux

(ImmutableMatrix) Matrix of auxiliary generalized speeds.

kdes

(ImmutableMatrix) Matrix of the kinematical differential equations as expressions equated to the zero matrix.

身体

(tuple of BodyBase subclasses) Tuple of all bodies that make up the system.

joints

(tuple of Joint) Tuple of all joints that connect bodies in the system.

荷载

(tuple of LoadBase subclasses) Tuple of all loads that have been applied to the system.

actuators

(tuple of ActuatorBase subclasses) Tuple of all actuators present in the system.

holonomic_constraints

(ImmutableMatrix) Matrix with the holonomic constraints as expressions equated to the zero matrix.

nonholonomic_constraints

(ImmutableMatrix) Matrix with the nonholonomic constraints as expressions equated to the zero matrix.

velocity_constraints

(ImmutableMatrix) Matrix with the velocity constraints as expressions equated to the zero matrix. These are by default derived as the time derivatives of the holonomic constraints extended with the nonholonomic constraints.

eom_method

(subclass of KanesMethod or LagrangesMethod) Backend for forming the equations of motion.

property actuators#

Tuple of actuators present in the system.

add_actuators(*actuators)[源代码]#

Add actuator(s) to the system.

参数:

*actuators : subclass of ActuatorBase

One or more actuators.

add_auxiliary_speeds(*speeds)[源代码]#

Add auxiliary speed(s) to the system.

参数:

*speeds : dynamicsymbols

One or more auxiliary speeds to be added to the system.

add_bodies(*bodies)[源代码]#

Add body(ies) to the system.

参数:

bodies : Particle or RigidBody

One or more bodies.

add_coordinates(*coordinates, independent=True)[源代码]#

Add generalized coordinate(s) to the system.

参数:

*coordinates : dynamicsymbols

One or more generalized coordinates to be added to the system.

independent : bool or list of bool, optional

Boolean whether a coordinate is dependent or independent. The default is True, so the coordinates are added as independent by default.

add_holonomic_constraints(*constraints)[源代码]#

Add holonomic constraint(s) to the system.

参数:

*constraints : Expr

One or more holonomic constraints, which are expressions that should be zero.

add_joints(*joints)[源代码]#

Add joint(s) to the system.

参数:

*joints : subclass of Joint

One or more joints.

解释

This methods adds one or more joints to the system including its associated objects, i.e. generalized coordinates, generalized speeds, kinematic differential equations and the bodies.

笔记

For the generalized coordinates, generalized speeds and bodies it is checked whether they are already known by the system instance. If they are, then they are not added. The kinematic differential equations are however always added to the system, so you should not also manually add those on beforehand.

add_kdes(*kdes)[源代码]#

Add kinematic differential equation(s) to the system.

参数:

*kdes : Expr

One or more kinematic differential equations.

add_loads(*loads)[源代码]#

Add load(s) to the system.

参数:

*loads : Force or Torque

One or more loads.

add_nonholonomic_constraints(*constraints)[源代码]#

Add nonholonomic constraint(s) to the system.

参数:

*constraints : Expr

One or more nonholonomic constraints, which are expressions that should be zero.

add_speeds(*speeds, independent=True)[源代码]#

Add generalized speed(s) to the system.

参数:

*speeds : dynamicsymbols

One or more generalized speeds to be added to the system.

independent : bool or list of bool, optional

Boolean whether a speed is dependent or independent. The default is True, so the speeds are added as independent by default.

apply_uniform_gravity(acceleration)[源代码]#

Apply uniform gravity to all bodies in the system by adding loads.

参数:

加快 :矢量

The acceleration due to gravity.

property bodies#

Tuple of all bodies that have been added to the system.

property eom_method#

Backend for forming the equations of motion.

property fixed_point#

Fixed point in the inertial reference frame.

property forcing#

系统的强迫向量。

property forcing_full#

The forcing vector of the system, augmented by the kinematic differential equations in explicit or implicit form.

form_eoms(eom_method=<class 'sympy.physics.mechanics.kane.KanesMethod'>, **kwargs)[源代码]#

Form the equations of motion of the system.

参数:

eom_method : subclass of KanesMethod or LagrangesMethod

Backend class to be used for forming the equations of motion. The default is KanesMethod.

返回:

ImmutableMatrix

Vector of equations of motions.

实例

这是一个单自由度平动弹簧质量阻尼器的简单例子。

>>> from sympy import S, symbols
>>> from sympy.physics.mechanics import (
...     LagrangesMethod, dynamicsymbols, PrismaticJoint, Particle,
...     RigidBody, System)
>>> q = dynamicsymbols('q')
>>> qd = dynamicsymbols('q', 1)
>>> m, k, b = symbols('m k b')
>>> wall = RigidBody('W')
>>> system = System.from_newtonian(wall)
>>> bob = Particle('P', mass=m)
>>> bob.potential_energy = S.Half * k * q**2
>>> system.add_joints(PrismaticJoint('J', wall, bob, q, qd))
>>> system.add_loads((bob.masscenter, b * qd * system.x))
>>> system.form_eoms(LagrangesMethod)
Matrix([[-b*Derivative(q(t), t) + k*q(t) + m*Derivative(q(t), (t, 2))]])

我们也可以用“rhs”方法求解状态。

>>> system.rhs()
Matrix([
[               Derivative(q(t), t)],
[(b*Derivative(q(t), t) - k*q(t))/m]])
property frame#

Inertial reference frame of the system.

classmethod from_newtonian(newtonian)[源代码]#

Constructs the system with respect to a Newtonian body.

get_body(name)[源代码]#

Retrieve a body from the system by name.

参数:

name :结构

The name of the body to retrieve.

返回:

RigidBody or Particle

The body with the given name, or None if no such body exists.

get_joint(name)[源代码]#

Retrieve a joint from the system by name.

参数:

name :结构

The name of the joint to retrieve.

返回:

subclass of Joint

The joint with the given name, or None if no such joint exists.

property holonomic_constraints#

Matrix with the holonomic constraints as expressions equated to the zero matrix.

property joints#

Tuple of all joints that have been added to the system.

property kdes#

Kinematical differential equations as expressions equated to the zero matrix. These equations describe the coupling between the generalized coordinates and the generalized speeds.

property loads#

Tuple of loads that have been applied on the system.

property mass_matrix#

系统的质量矩阵。

解释

The mass matrix \(M_d\) and the forcing vector \(f_d\) of a system describe the system's dynamics according to the following equations:

\[M_d \dot{u} = f_d\]

where \(\dot{u}\) is the time derivative of the generalized speeds.

property mass_matrix_full#

The mass matrix of the system, augmented by the kinematic differential equations in explicit or implicit form.

解释

The full mass matrix \(M_m\) and the full forcing vector \(f_m\) of a system describe the dynamics and kinematics according to the following equation:

\[M_m \dot{x} = f_m\]

where \(x\) is the state vector stacking \(q\) and \(u\).

property nonholonomic_constraints#

Matrix with the nonholonomic constraints as expressions equated to the zero matrix.

property q#

Matrix of all the generalized coordinates with the independent stacked upon the dependent.

property q_dep#

Matrix of the dependent generalized coordinates.

property q_ind#

Matrix of the independent generalized coordinates.

rhs(inv_method=None)[源代码]#

Compute the equations of motion in the explicit form.

参数:

inv_method :结构

The specific sympy inverse matrix calculation method to use. For a list of valid methods, see inv()

返回:

ImmutableMatrix

Equations of motion in the explicit form.

参见

sympy.physics.mechanics.kane.KanesMethod.rhs

KanesMethod's rhs function.

sympy.physics.mechanics.lagrange.LagrangesMethod.rhs

LagrangesMethod's rhs function.

property u#

Matrix of all the generalized speeds with the independent stacked upon the dependent.

property u_aux#

Matrix of auxiliary generalized speeds.

property u_dep#

Matrix of the dependent generalized speeds.

property u_ind#

Matrix of the independent generalized speeds.

validate_system(eom_method=<class 'sympy.physics.mechanics.kane.KanesMethod'>, check_duplicates=False)[源代码]#

Validates the system using some basic checks.

参数:

eom_method : subclass of KanesMethod or LagrangesMethod

Backend class that will be used for forming the equations of motion. There are different checks for the different backends. The default is KanesMethod.

check_duplicates : bool

Boolean whether the system should be checked for duplicate definitions. The default is False, because duplicates are already checked when adding objects to the system.

解释

This method validates the system based on the following checks:

  • The number of dependent generalized coordinates should equal the number of holonomic constraints.

  • All generalized coordinates defined by the joints should also be known to the system.

  • If KanesMethod is used as a eom_method:
    • All generalized speeds and kinematic differential equations defined by the joints should also be known to the system.

    • The number of dependent generalized speeds should equal the number of velocity constraints.

    • The number of generalized coordinates should be less than or equal to the number of generalized speeds.

    • The number of generalized coordinates should equal the number of kinematic differential equations.

  • If LagrangesMethod is used as eom_method:
    • There should not be any generalized speeds that are not derivatives of the generalized coordinates (this includes the generalized speeds defined by the joints).

笔记

This method is not guaranteed to be backwards compatible as it may improve over time. The method can become both more and less strict in certain areas. However a well-defined system should always pass all these tests.

property velocity_constraints#

Matrix with the velocity constraints as expressions equated to the zero matrix. The velocity constraints are by default derived from the holonomic and nonholonomic constraints unless they are explicitly set.

property x#

Unit vector fixed in the inertial reference frame.

property y#

Unit vector fixed in the inertial reference frame.

property z#

Unit vector fixed in the inertial reference frame.