才想可以快速通過
筆記文本,輕輕飛越『動態模擬』
Dynamical simulation
Dynamical simulation, in computational physics, is the simulation of systems of objects that are free to move, usually in three dimensions according to Newton’s laws of dynamics, or approximations thereof. Dynamical simulation is used in computer animation to assist animators to produce realistic motion, in industrial design (for example to simulate crashes as an early step in crash testing), and in video games. Body movement is calculated using time integration methods.
Physics engines
In computer science, a program called a physics engine is used to model the behaviors of objects in space. These engines allow simulation of the way bodies of many types are affected by a variety of physical stimuli. They are also used to create Dynamical simulations without having to know anything about physics. Physics engines are used throughout the video game and movie industry, but not all physics engines are alike; They are generally broken into real-time and the high precision but these are not the only options. Most real-time physics engines are inaccurate and yield only the barest approximation of the real world, whereas most high-precision engines are far too slow for use in everyday applications. To understand how these Physics engines are built, a basic understanding of physics is required. Physics engines are based on the actual behaviors of the world as described by classical mechanics. Engines do not typically account for Modern Mechanics (see Theory of relativity and quantum mechanics) because most visualization deals with large bodies moving relatively slowly, but the most complicated engines perform calculations for Modern Mechanics as well as Classical. The models used in Dynamical simulations determine how accurate these simulations are.
語詞叢林!只需介紹一下 PyDy 之
codegen
Introduction
The pydy.codegen package contains various tools to generate numerical code from symbolic descriptions of the equations of motion of systems. It allows you to generate code using a variety of backends depending on your needs. The generated code can also be auto-wrapped for immediate use in a Python session or script. Each component of the code generators and wrappers are accessible so that you can use just the raw code or the wrapper versions.
We currently support three backends:
- lambdify
- This generates NumPy-aware Python code which is defined in a Python lambda function, using the sympy.utilities.lambdify module and is the default generator.
- Theano
- This generates Theano trees that are compiled into low level code, using thesympy.printers.theano_code module.
- Cython
- This generates C code that can be called from Python, using SymPy’s C code printer utilities and Cython.
的用法!!何況早有範例也。
Example Use
The simplest entry point to the code generation tools is through the System class.
>>> from pydy.models import multi_mass_spring_damper >>> sys = multi_mass_spring_damper() >>> type(sys) <class 'pydy.system.System'> >>> rhs = sys.generate_ode_function() >>> help(rhs) # rhs is a function: Returns the derivatives of the states, i.e. numerically evaluates the right hand side of the first order differential equation. x' = f(x, t, p) Parameters ========== x : ndarray, shape(2,) The state vector is ordered as such: - x0(t) - v0(t) t : float The current time. p : dictionary len(3) or ndarray shape(3,) Either a dictionary that maps the constants symbols to their numerical values or an array with the constants in the following order: - m0 - c0 - k0 Returns ======= dx : ndarray, shape(2,) The derivative of the state vector. >>> import numpy as np >>> rhs(np.array([1.0, 2.0]), 0.0, np.array([1.0, 2.0, 3.0])) array([ 2., -7.])
怎知一跑跑了 次?只因結果會不一樣勒??
突然發現 help 排列次序,為何每每不同耶??!!
故爾鼻子摸著閱讀『原始碼』︰
Source code for pydy.models
#!/usr/bin/env python # -*- coding: utf-8 -*- """This module contains some sample symbolic models used for testing and examples.""" # external libraries import sympy as sm import sympy.physics.mechanics as me # local from .system import System [docs]def multi_mass_spring_damper(n=1, apply_gravity=False, apply_external_forces=False): """Returns a system containing the symbolic equations of motion and associated variables for a simple mutli-degree of freedom point mass, spring, damper system with optional gravitational and external specified forces. For example, a two mass system under the influence of gravity and external forces looks like: :: ---------------- | | | | g \ | | | V k0 / --- c0 | | | | x0, v0 --------- V | m0 | ----- --------- | | | | | \ v | | | k1 / f0 --- c1 | | | | x1, v1 --------- V | m1 | ----- --------- | f1 V Parameters ---------- n : integer The number of masses in the serial chain. apply_gravity : boolean If true, gravity will be applied to each mass. apply_external_forces : boolean If true, a time varying external force will be applied to each mass. Returns ------- system : pydy.system.System A system constructed from the KanesMethod object. """
卻是一無所獲!!??
只能死馬當活馬醫,扒文
codegen API
pydy.codegen.ode_function_generators.
generate_ode_function
(*args, **kwargs)
Generates a numerical function which can evaluate the right hand side of the first order ordinary differential equations from a system described by one of the following three symbolic forms:
[1] x’ = F(x, t, r, p)
[2] M(x, p) x’ = F(x, t, r, p)
- [3] M(q, p) u’ = F(q, u, t, r, p)
- q’ = G(q, u, t, r, p)
where
x : states, i.e. [q, u] t : time r : specified (exogenous) inputs p : constants q : generalized coordinates u : generalized speeds M : mass matrix (full or minimum) F : right hand side (full or minimum) G : right hand side of the kinematical differential equations
The generated function is of the form F(x, t, p) or F(x, t, r, p) depending on whether the system has specified inputs or not.
Parameters: |
right_hand_side : SymPy Matrix, shape(n, 1)
coordinates : sequence of SymPy Functions
speeds : sequence of SymPy Functions
constants : sequence of SymPy Symbols
mass_matrix : sympy.Matrix, shape(n, n), optional
coordinate_derivatives : sympy.Matrix, shape(m, 1), optional
specifieds : sequence of SymPy Functions
linear_sys_solver : string or function
constants_arg_type : string
specifieds_arg_type : string
generator : string or and ODEFunctionGenerator, optional
|
---|---|
Returns: |
rhs : function
|
Notes
The generated function still supports the pre-0.3.0 extra argument style, i.e. args = {‘constants’: …, ‘specified’: …}, but only if constants_arg_type
and specifieds_arg_type
are both set to None. This functionality is deprecated and will be removed in 0.4.0, so it’s best to adjust your code to support the new argument types. See the docstring for the generated function for more info on the new style of arguments.
忽恍然大悟,後哈哈大笑呦◎