def generate_symbolic_model(T, U, tt, F, simplify=True, **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) kwargs: optional assumptions like 'real=True' """ n = len(tt) for theta_i in tt: assert isinstance(theta_i, sp.Symbol) 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) #lagrange equation 2nd kind 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] # 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 # 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
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