Exemplo n.º 1
0
    def test_lie_deriv_covf(self):
        xx = st.symb_vector('x1:4')
        st.make_global(xx)

        # we test this by building the observability matrix with two different but equivalent approaches
        f = sp.Matrix([x1 + x3 * x2, 7 * exp(x1), cos(x2)])
        y = x1**2 + sin(x3) * x2
        ydot = st.lie_deriv(y, f, xx)
        yddot = st.lie_deriv(ydot, f, xx)

        cvf1 = st.gradient(y, xx)
        cvf2 = st.gradient(ydot, xx)
        cvf3 = st.gradient(yddot, xx)

        # these are the rows of the observability matrix

        # second approach
        dh0 = cvf1
        dh1 = st.lie_deriv_covf(dh0, f, xx)
        dh2a = st.lie_deriv_covf(dh1, f, xx)
        dh2b = st.lie_deriv_covf(dh0, f, xx, order=2)

        zero = dh0 * 0

        self.assertEqual((dh1 - cvf2).expand(), zero)
        self.assertEqual((dh2a - cvf3).expand(), zero)
        self.assertEqual((dh2b - cvf3).expand(), zero)
Exemplo n.º 2
0
    def test_integrate2(self):
        x1, x2, x3, x4, x5 = self.xx

        a, b = sp.symbols('a, b', nonzero=True)

        if 1:
            # tests for some simplification problem

            y1 = sp.log(x2) + sp.log(cos(x1))
            dx1, dx2, dx3, dx4, dx5 = self.dx
            dy1 = (-sp.tan(x1)) * dx1 + (1 / x2) * dx2
            y1b = dy1.integrate()
            self.assertEqual(y1, y1b)

            y1 = sp.log(x2) + sp.log(sin(x1) - 1) / 2 + sp.log(sin(x1) + 1) / 2
            dx1, dx2, dx3, dx4, dx5 = self.dx
            dy1 = (-sp.tan(x1)) * dx1 + (1 / x2) * dx2
            y1b = dy1.integrate()
            difference = y1 - y1b
            # this is not zero but it does not depend on xx:
            grad = sp.simplify(st.gradient(difference, self.xx))
            self.assertEqual(grad, grad * 0)

            # another variant
            y1 = sp.log(x2) + sp.log(sin(x1) - 1) / 2 + sp.log(sin(x1) + 1) / 2
            dx1, dx2, dx3, dx4, dx5 = self.dx
            dy1 = (-sp.sin(x1) / sp.cos(x1)) * dx1 + (1 / x2) * dx2
            y1b = dy1.integrate()
            difference = y1 - y1b
            # this is not zero but it does not depend on xx:
            grad = sp.simplify(st.gradient(difference, self.xx))
            self.assertEqual(grad, grad * 0)

            y1 = a * sp.log(cos(b * x1))
            dy1 = pc.d(y1, self.xx)
            y1b = dy1.integrate()
            self.assertEqual(sp.simplify(y1 - y1b), 0)
Exemplo n.º 3
0
def generate_symbolic_model(T, U, tt, F, simplify=True, constraints=None, dissipation_function=0, **kwargs):
    """
    T:             kinetic energy
    U:             potential energy
    tt:            sequence of independent deflection variables ("theta")
    F:             external forces
    simplify:      determines whether the equations of motion should be simplified
                   (default=True)
    constraints:   None (default) or sequence of constraints (will introduce lagrange multipliers)

    dissipation_function:
                    Rayleigh dissipation function. Its Differential w.r.t. tthetadot is added to the systems equation

    kwargs: optional assumptions like 'real=True'

    :returns SymbolicModel instance mod. The equations are contained in mod.eqns
    """
    n = len(tt)

    for theta_i in tt:
        assert isinstance(theta_i, sp.Symbol)

    if constraints is None:
        constraints_flag = False
        # ensure well define calculations (jacobian of empty matrix would not be possible)
        constraints = [0]
    else:
        constraints_flag = True
    assert len(constraints) > 0
    constraints = sp.Matrix(constraints)
    assert constraints.shape[1] == 1
    nC = constraints.shape[0]
    jac_constraints = constraints.jacobian(tt)

    dissipation_function = sp.sympify(dissipation_function)
    assert isinstance(dissipation_function, sp.Expr)

    llmd = st.symb_vector("lambda_1:{}".format(nC + 1))

    F = sp.Matrix(F)

    if F.shape[0] == 1:
        # convert to column vector
        F = F.T
    if not F.shape == (n, 1):
        msg = "Vector of external forces has the wrong length. Should be " + \
              str(n) + " but is %i!" % F.shape[0]
        raise ValueError(msg)

    # introducing symbols for the derivatives
    tt = sp.Matrix(tt)
    ttd = st.time_deriv(tt, tt, **kwargs)
    ttdd = st.time_deriv(tt, tt, order=2, **kwargs)

    # Lagrange function
    L = T - U

    if not T.atoms().intersection(ttd) == set(ttd):
        raise ValueError('Not all velocity symbols do occur in T')

    # partial derivatives of L
    L_diff_tt = st.jac(L, tt)
    L_diff_ttd = st.jac(L, ttd)

    # prov_deriv_symbols = [ttd, ttdd]

    # time-depended_symbols
    tds = list(tt) + list(ttd)
    L_diff_ttd_dt = st.time_deriv(L_diff_ttd, tds, **kwargs)

    # constraints

    constraint_terms = list(llmd.T * jac_constraints)

    # lagrange equation 1st kind (which include 2nd kind as special case if constraints are empty)

    model_eq = sp.zeros(n, 1)
    for i in range(n):
        model_eq[i] = L_diff_ttd_dt[i] - L_diff_tt[i] - F[i] - constraint_terms[i]

    model_eq += st.gradient(dissipation_function, ttd).T
    # create object of model
    mod = SymbolicModel()  # model_eq, qs, f, D)
    mod.eqns = model_eq

    mod.extforce_list = F
    reduced_F = sp.Matrix([s for s in F if st.is_symbol(s)])
    mod.F = reduced_F
    mod.tau = reduced_F
    if constraints_flag:
        mod.llmd = llmd
        mod.constraints = constraints
    else:
        # omit fake constraint [0]
        mod.constraints = None
        mod.llmd = None

    # coordinates velocities and accelerations
    mod.tt = tt
    mod.ttd = ttd
    mod.ttdd = ttdd

    mod.qs = tt
    mod.qds = ttd
    mod.qdds = ttdd

    # also store kinetic and potential energy
    mod.T = T
    mod.U = U

    if simplify:
        mod.eqns.simplify()

    return mod