凯恩方法和拉格朗日方法(Docstrings)#

class sympy.physics.mechanics.kane.KanesMethod(frame, q_ind, u_ind, kd_eqs=None, q_dependent=None, configuration_constraints=None, u_dependent=None, velocity_constraints=None, acceleration_constraints=None, u_auxiliary=None, bodies=None, forcelist=None, explicit_kinematics=True, kd_eqs_solver='LU', constraint_solver='LU')[源代码]#

凯恩的方法对象。

参数:

框架 :参考帧

The inertial reference frame for the system.

q_ind : iterable of dynamicsymbols

Independent generalized coordinates.

u_ind : iterable of dynamicsymbols

Independent generalized speeds.

kd_eqs : iterable of Expr, optional

Kinematic differential equations, which linearly relate the generalized speeds to the time-derivatives of the generalized coordinates.

q_dependent : iterable of dynamicsymbols, optional

Dependent generalized coordinates.

configuration_constraints : iterable of Expr, optional

Constraints on the system's configuration, i.e. holonomic constraints.

u_dependent : iterable of dynamicsymbols, optional

Dependent generalized speeds.

velocity_constraints : iterable of Expr, optional

Constraints on the system's velocity, i.e. the combination of the nonholonomic constraints and the time-derivative of the holonomic constraints.

acceleration_constraints : iterable of Expr, optional

Constraints on the system's acceleration, by default these are the time-derivative of the velocity constraints.

u_auxiliary : iterable of dynamicsymbols, optional

Auxiliary generalized speeds.

bodies : iterable of Particle and/or RigidBody, optional

The particles and rigid bodies in the system.

forcelist : iterable of tuple[Point | ReferenceFrame, Vector], optional

Forces and torques applied on the system.

explicit_kinematics : bool

Boolean whether the mass matrices and forcing vectors should use the explicit form (default) or implicit form for kinematics. See the notes for more details.

kd_eqs_solver : str, callable

Method used to solve the kinematic differential equations. If a string is supplied, it should be a valid method that can be used with the sympy.matrices.matrixbase.MatrixBase.solve(). If a callable is supplied, it should have the format f(A, rhs), where it solves the equations and returns the solution. The default utilizes LU solve. See the notes for more information.

constraint_solver : str, callable

Method used to solve the velocity constraints. If a string is supplied, it should be a valid method that can be used with the sympy.matrices.matrixbase.MatrixBase.solve(). If a callable is supplied, it should have the format f(A, rhs), where it solves the equations and returns the solution. The default utilizes LU solve. See the notes for more information.

解释

这个物体是用来做“簿记”当你通过和形成运动方程的方式凯恩提出:凯恩,T.,列文森,D.动力学理论和应用。1985年麦格劳希尔

这些属性用于 [M] udot=强制。

笔记

The mass matrices and forcing vectors related to kinematic equations are given in the explicit form by default. In other words, the kinematic mass matrix is \(\mathbf{k_{k\dot{q}}} = \mathbf{I}\). In order to get the implicit form of those matrices/vectors, you can set the explicit_kinematics attribute to False. So \(\mathbf{k_{k\dot{q}}}\) is not necessarily an identity matrix. This can provide more compact equations for non-simple kinematics.

Two linear solvers can be supplied to KanesMethod: one for solving the kinematic differential equations and one to solve the velocity constraints. Both of these sets of equations can be expressed as a linear system Ax = rhs, which have to be solved in order to obtain the equations of motion.

The default solver 'LU', which stands for LU solve, results relatively low number of operations. The weakness of this method is that it can result in zero division errors.

If zero divisions are encountered, a possible solver which may solve the problem is "CRAMER". This method uses Cramer's rule to solve the system. This method is slower and results in more operations than the default solver. However it only uses a single division by default per entry of the solution.

While a valid list of solvers can be found at sympy.matrices.matrixbase.MatrixBase.solve(), it is also possible to supply a \(callable\). This way it is possible to use a different solver routine. If the kinematic differential equations are not too complex it can be worth it to simplify the solution by using lambda A, b: simplify(Matrix.LUsolve(A, b)). Another option solver one may use is sympy.solvers.solveset.linsolve(). This can be done using \(lambda A, b: tuple(linsolve((A, b)))[0]\), where we select the first solution as our system should have only one unique solution.

实例

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

在这个例子中,我们首先需要做运动学。这包括创建广义速度和坐标及其导数。然后我们创建一个点并在帧中设置其速度。

>>> from sympy import symbols
>>> from sympy.physics.mechanics import dynamicsymbols, ReferenceFrame
>>> from sympy.physics.mechanics import Point, Particle, KanesMethod
>>> q, u = dynamicsymbols('q u')
>>> qd, ud = dynamicsymbols('q u', 1)
>>> m, c, k = symbols('m c k')
>>> N = ReferenceFrame('N')
>>> P = Point('P')
>>> P.set_vel(N, u * N.x)

Next we need to arrange/store information in the way that KanesMethod requires. The kinematic differential equations should be an iterable of expressions. A list of forces/torques must be constructed, where each entry in the list is a (Point, Vector) or (ReferenceFrame, Vector) tuple, where the Vectors represent the Force or Torque. Next a particle needs to be created, and it needs to have a point and mass assigned to it. Finally, a list of all bodies and particles needs to be created.

>>> kd = [qd - u]
>>> FL = [(P, (-k * q - c * u) * N.x)]
>>> pa = Particle('pa', P, m)
>>> BL = [pa]

最后我们可以生成运动方程。首先,我们创建KanesMethod对象并提供惯性系、坐标系、广义速度和运动学微分方程。这里还提供了配置和运动约束、相关坐标和速度以及辅助速度等附加量(请参阅联机文档)。下一步我们组成FR 和FR完成:FR+FR =0。我们有这一点的运动方程。但是重新排列它们是有意义的,所以我们计算了质量矩阵和强迫项,对于E.o.M.的形式是: [MM] udot=强迫,其中MM是质量矩阵,udot是广义速度的时间导数的向量,而forcing是表示“强迫”项的向量。

>>> KM = KanesMethod(N, q_ind=[q], u_ind=[u], kd_eqs=kd)
>>> (fr, frstar) = KM.kanes_equations(BL, FL)
>>> MM = KM.mass_matrix
>>> forcing = KM.forcing
>>> rhs = MM.inv() * forcing
>>> rhs
Matrix([[(-c*u(t) - k*q(t))/m]])
>>> KM.linearize(A_and_B=True)[0]
Matrix([
[   0,    1],
[-k/m, -c/m]])

有关如何处理非依赖性力的信息,请查看如何处理非依赖性的信息和文件。

属性

q、 美国

广义坐标和速度的矩阵

身体

(iterable) Iterable of Particle and RigidBody objects in the system.

荷载

(iterable)描述系统上力的(Point,vector)或(ReferenceFrame,vector)元组的iterable。

auxiliary_eqs

(矩阵)如果适用,用于求解非作用力的辅助凯恩方程组。

mass_matrix

(Matrix) The system's dynamics mass matrix: [k_d; k_dnh]

强迫

(Matrix) The system's dynamics forcing vector: -[f_d; f_dnh]

mass_matrix_kin

(Matrix) The "mass matrix" for kinematic differential equations: k_kqdot

forcing_kin

(Matrix) The forcing vector for kinematic differential equations: -(k_ku*u + f_k)

mass_matrix_full

(Matrix) The "mass matrix" for the u's and q's with dynamics and kinematics

forcing_full

(Matrix) The "forcing vector" for the u's and q's with dynamics and kinematics

property auxiliary_eqs#

包含辅助方程的矩阵。

property forcing#

系统的强迫向量。

property forcing_full#

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

property forcing_kin#

The kinematic "forcing vector" of the system.

kanes_equations(bodies=None, loads=None)[源代码]#

形成凯恩方程的方法,Fr+Fr*=0。

参数:

身体 :i可读取

系统中所有刚体和粒子的一个集合。一个系统必须至少有一个实体。

荷载 :i可读取

接受一个iterable(Particle,Vector)或(ReferenceFrame,Vector)元组,这些元组表示帧上某个点的力或转矩。必须是元组的非空iterable或与没有约束的系统相对应的None。

解释

Returns (Fr, Fr*). In the case where auxiliary generalized speeds are present (say, s auxiliary speeds, o generalized speeds, and m motion constraints) the length of the returned vectors will be o - m + s in length. The first o - m equations will be the constrained Kane's equations, then the s auxiliary Kane's equations. These auxiliary equations can be accessed with the auxiliary_eqs property.

kindiffdict()[源代码]#

返回一个将q'映射到u的字典。

linearize(*, new_method=None, linear_solver='LU', **kwargs)[源代码]#

将关于符号操作点的运动方程线性化。

参数:

new_method

Deprecated, does nothing and will be removed.

linear_solver : str, callable

Method used to solve the several symbolic linear systems of the form A*x=b in the linearization process. If a string is supplied, it should be a valid method that can be used with the sympy.matrices.matrixbase.MatrixBase.solve(). If a callable is supplied, it should have the format x = f(A, b), where it solves the equations and returns the solution. The default is 'LU' which corresponds to SymPy's A.LUsolve(b). LUsolve() is fast to compute but will often result in divide-by-zero and thus nan results.

**kwargs

Extra keyword arguments are passed to sympy.physics.mechanics.linearize.Linearizer.linearize().

解释

如果kwarg A_和_B为False(默认值),则返回线性化形式M的M、A、B、r [[q', u']^T = A] [q_ind, u_ind] ^T+B*r。

如果kwarg A_和_B为真,则返回线性化形式dx=A的A、B、r x+B r、 其中x= [q_ind, u_ind] ^T、 请注意,如果有许多符号参数,这是计算密集型的。因此,使用默认的A_和_B=False,返回M、A和B可能更可取。然后将值替换到这些矩阵中,状态空间形式为A=P.T M、 发票() A、 B=P.T M、 发票() B、 其中P=线性化器.

在这两种情况下,r都是运动方程中所有不属于q,u,q'或u'的动力符号。它们按规范形式排序。

操作点也可以使用 op_point 夸克。它接受{symbol:value}的字典,或此类字典的iterable。值可以是数字或符号。预先指定的值越多,此计算的运行速度就越快。

有关更多文档,请参阅 Linearizer 班级。

property mass_matrix#

系统的质量矩阵。

property mass_matrix_full#

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

property mass_matrix_kin#

The kinematic "mass matrix" \(\mathbf{k_{k\dot{q}}}\) of the system.

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

以一阶形式返回系统的运动方程。输出位于的右侧:

x' = |q'| =: f(q, u, r, p, t)
     |u'|

右边是大多数数字编码积分器所需要的。

参数:

inv_method :结构

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

to_linearizer(linear_solver='LU')[源代码]#

返回从KanesMethod类中的数据初始化的线性化器类的实例。这可能比使用线性化类方法更可取,因为线性化器对象将允许更有效的重新计算(即关于变化的操作点)。

参数:

linear_solver : str, callable

Method used to solve the several symbolic linear systems of the form A*x=b in the linearization process. If a string is supplied, it should be a valid method that can be used with the sympy.matrices.matrixbase.MatrixBase.solve(). If a callable is supplied, it should have the format x = f(A, b), where it solves the equations and returns the solution. The default is 'LU' which corresponds to SymPy's A.LUsolve(b). LUsolve() is fast to compute but will often result in divide-by-zero and thus nan results.

返回:

线性化器

class sympy.physics.mechanics.lagrange.LagrangesMethod(Lagrangian, qs, forcelist=None, bodies=None, frame=None, hol_coneqs=None, nonhol_coneqs=None)[源代码]#

拉格朗日方法对象。

解释

该对象通过两步程序生成运动方程。第一步是通过提供拉格朗日和广义坐标,在最小值处初始化拉格朗日方法。如果有任何约束方程,它们可以作为关键字参数提供。拉格朗日乘子是自动生成的,在数量上与约束方程相等。同样地,在下面的例子中,也可以用非参考力来描述。这也将在中进一步讨论 __init__ 方法。

实例

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

在这个例子中,我们首先需要做运动学。这涉及到创建广义坐标及其导数。然后我们创建一个点并在帧中设置其速度。

>>> from sympy.physics.mechanics import LagrangesMethod, Lagrangian
>>> from sympy.physics.mechanics import ReferenceFrame, Particle, Point
>>> from sympy.physics.mechanics import dynamicsymbols
>>> from sympy import symbols
>>> q = dynamicsymbols('q')
>>> qd = dynamicsymbols('q', 1)
>>> m, k, b = symbols('m k b')
>>> N = ReferenceFrame('N')
>>> P = Point('P')
>>> P.set_vel(N, qd * N.x)

然后我们需要准备拉格朗日方法所需的信息来生成运动方程。首先我们创建一个粒子,它有一个附加的点。在此之后,拉格朗日由动能和势能产生。然后,必须构造一个非保守力/力矩的iterable,其中每个项都是一个(点,向量)或(ReferenceFrame,Vector)元组,向量代表非保守力或力矩。

>>> Pa = Particle('Pa', P, m)
>>> Pa.potential_energy = k * q**2 / 2.0
>>> L = Lagrangian(N, Pa)
>>> fl = [(P, -b * qd * N.x)]

最后我们可以生成运动方程。首先,我们创建LagrangesMethod对象。要做到这一点,必须提供拉格朗日和广义坐标。如果相关,还可以提供约束方程、力列表和惯性系。接下来我们生成拉格朗日运动方程,这样:拉格朗日运动方程=0。我们有这一点的运动方程。

>>> l = LagrangesMethod(L, [q], forcelist = fl, frame = N)
>>> print(l.form_lagranges_equations())
Matrix([[b*Derivative(q(t), t) + 1.0*k*q(t) + m*Derivative(q(t), (t, 2))]])

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

>>> print(l.rhs())
Matrix([[Derivative(q(t), t)], [(-b*Derivative(q(t), t) - 1.0*k*q(t))/m]])

有关详细信息,请参阅每个方法的docstrings。

属性

q、 美国

广义坐标和速度的矩阵

荷载

(iterable)描述系统上力的(Point,vector)或(ReferenceFrame,vector)元组的iterable。

身体

(iterable)包含系统的刚体和粒子的iterable。

mass_matrix

(矩阵)系统的质量矩阵

强迫

(矩阵)系统的强制向量

mass_matrix_full

(矩阵)量子点、量子点和拉格朗日乘子(lam)的“质量矩阵”

forcing_full

(矩阵)qdot、qdoubledot和lagrange乘子(lam)的强迫向量

property forcing#

从“拉格朗日方程”方法返回强制向量。

property forcing_full#

将量子点增加到上面的强制向量。

form_lagranges_equations()[源代码]#

形成拉格朗日运动方程的方法。

使用第二类拉格朗日方程返回运动方程的矢量。

linearize(q_ind=None, qd_ind=None, q_dep=None, qd_dep=None, linear_solver='LU', **kwargs)[源代码]#

将关于符号操作点的运动方程线性化。

参数:

linear_solver : str, callable

Method used to solve the several symbolic linear systems of the form A*x=b in the linearization process. If a string is supplied, it should be a valid method that can be used with the sympy.matrices.matrixbase.MatrixBase.solve(). If a callable is supplied, it should have the format x = f(A, b), where it solves the equations and returns the solution. The default is 'LU' which corresponds to SymPy's A.LUsolve(b). LUsolve() is fast to compute but will often result in divide-by-zero and thus nan results.

**kwargs

Extra keyword arguments are passed to sympy.physics.mechanics.linearize.Linearizer.linearize().

解释

如果kwarg A_和_B为False(默认值),则返回线性化形式M的M、A、B、r [[q', u']^T = A] [q_ind, u_ind] ^T+B*r。

如果kwarg A_和_B为真,则返回线性化形式dx=A的A、B、r x+B r、 其中x= [q_ind, u_ind] ^T、 请注意,如果有许多符号参数,这是计算密集型的。因此,使用默认的A_和_B=False,返回M、A和B可能更可取。然后将值替换到这些矩阵中,状态空间形式为A=P.T M、 发票() A、 B=P.T M、 发票() B、 其中P=线性化器.

在这两种情况下,r都是运动方程中所有不属于q,u,q'或u'的动力符号。它们按规范形式排序。

操作点也可以使用 op_point 夸克。它接受{symbol:value}的字典,或此类字典的iterable。值可以是数字或符号。预先指定的值越多,此计算的运行速度就越快。

有关更多文档,请参阅 Linearizer 班级。

property mass_matrix#

返回质量矩阵,如有必要,该矩阵由拉格朗日乘子增加。

解释

如果系统用n个广义坐标描述,并且没有约束方程,则返回nxn矩阵。

如果在初始化过程中提供了'n'个广义坐标和'm'约束方程,则返回nx(n+m)矩阵。第(n+m-1)列和第(n+m)列包含拉格朗日乘数的系数。

property mass_matrix_full#

将量子点的系数增加到质量矩阵。

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

Returns equations that can be solved numerically.

参数:

inv_method :结构

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

solve_multipliers(op_point=None, sol_type='dict')[源代码]#

Solves for the values of the lagrange multipliers symbolically at the specified operating point.

参数:

op_point :dict或iterable of dicts,可选

求解点。操作点被指定为{symbol:value}的字典或iterable。值本身可以是数字或符号。

sol_type :str,可选

解决方案返回类型。有效的选项是:-“dict”:一个{symbol:value}的dict(默认)—“Matrix”:解决方案的有序列矩阵

to_linearizer(q_ind=None, qd_ind=None, q_dep=None, qd_dep=None, linear_solver='LU')[源代码]#

返回从LagrangeMethod类中的数据初始化的线性化器类的实例。这可能比使用线性化类方法更可取,因为线性化器对象将允许更有效的重新计算(即关于变化的操作点)。

参数:

q_ind, qd_ind :类似数组,可选

独立的广义坐标和速度。

q_dep, qd_dep :类似数组,可选

相关的广义坐标和速度。

linear_solver : str, callable

Method used to solve the several symbolic linear systems of the form A*x=b in the linearization process. If a string is supplied, it should be a valid method that can be used with the sympy.matrices.matrixbase.MatrixBase.solve(). If a callable is supplied, it should have the format x = f(A, b), where it solves the equations and returns the solution. The default is 'LU' which corresponds to SymPy's A.LUsolve(b). LUsolve() is fast to compute but will often result in divide-by-zero and thus nan results.

返回:

线性化器