variables = [qA, qB, qC] guess = [initialvalues[item] for item in variables] result = c.solve_numeric(variables, guess, system.constant_values) guess = [initialvalues[item] for item in variables] ini = [] for item in system.get_state_variables(): if item in variables: ini.append(result.x[variables.index(item)]) else: ini.append(initialvalues[item]) points = PointsOutput(points, constant_values=system.constant_values) points.calc(numpy.array([ini0, ini])) points.plot_time() eq_d = sympy.Matrix(eq_d) qi = sympy.Matrix([qA_d]) qd = sympy.Matrix([qB_d, qC_d]) C = eq_d.jacobian(qi) D = eq_d.jacobian(qd) J = -D.inv() * C J.simplify() qd2 = J * qi subs = dict([(ii, jj) for ii, jj in zip(qd, qd2)])
# In[28]: # KE = system.get_KE() # PE = system.getPEGravity(pNA) - system.getPESprings() # energy_output = Output([KE-PE],system) # energy_output.calc(states) # energy_output.plot_time() # ### Motion # In[29]: points = [pm1, pNA, pAB, pBC, pC1, pC2] points_output = PointsOutput(points, system) y = points_output.calc(states) points_output.plot_time(5) # #### Motion Animation # in normal Python the next lines of code produce an animation using matplotlib # In[30]: points_output.animate(fps=fps, movie_name='render.mp4', lw=2, marker='o', color=(1, 0, 0, 1), linestyle='-') #a() # To plot the animation in jupyter you need a couple extra lines of code...
'constants': constants }, )) return states input_data_all = run_sim([1.1e2, 9e2]) input_positions = input_data_all[:, :3] # input_positions = input_positions.copy() # pynamics.integration.logger.setLevel(logging.ERROR) pynamics.system.logger.setLevel(logging.ERROR) points = [pNA, pAB, pBC, pCtip] points_output = PointsOutput(points, system) y = points_output.calc(input_data_all) points_output.plot_time() r = numpy.random.randn(*(y.shape)) * .01 y_rand = y + r fy = scipy.interpolate.interp1d(t, y_rand.transpose((1, 2, 0))) fyt = fy(t) # points_output.animate(fps = fps,movie_name = 'render.mp4',lw=2,marker='o',color=(1,0,0,1),linestyle='-') def calc_error(args): states_guess = run_sim(args) y_guess = points_output.calc(states_guess).transpose((1, 2, 0)) error = fyt - y_guess error **= 2 error = error.sum()
# # ============================================================================= # KE = system.get_KE() # PE = system.getPEGravity(0*N.x) - system.getPESprings() # energy = Output([KE-PE], constant_values = constants) # energy.calc(states,t) # energy.plot_time() # # ============================================================================= # positions = Output(system.get_q(0), constant_values = constants) # positions.calc(states,t) # positions.plot_time() # speeds = Output(system.get_q(1), constant_values = constants) # speeds.calc(states,t) # speeds.plot_time() # y= Output([G*qB_d], constant_values=constants) # y.calc(states,t) # y.plot_time() points = [pAcm, pBcm] po = PointsOutput(points, constant_values=constants) po.calc(states, t) po.plot_time() po.animate(fps=fps, movie_name='triple_pendulum.mp4', lw=2, marker='o', color=(1, 0, 0, 1), linestyle='-')
ini, t, rtol=tol, atol=tol, args=({ 'constants': constants }, )) points = [pm1, pm2] po2 = PointsOutput(points, system, constant_values=system.constant_values) po2.calc(states) constants[b_constraint] = 0 states = pynamics.integration.integrate_odeint(func, ini, t, rtol=tol, atol=tol, args=({ 'constants': constants }, )) points = [pm1, pm2] po3 = PointsOutput(points, system, constant_values=system.constant_values) po3.calc(states) po.plot_time() po.animate(fps=fps, movie_name='bouncy-mod.mp4', lw=2, marker='o') po2.plot_time() po2.animate(fps=fps, movie_name='bouncy-mod.mp4', lw=2, marker='o') po3.plot_time() po3.animate(fps=fps, movie_name='bouncy-mod.mp4', lw=2, marker='o')
plt.legend(artists,['qA','qB','qC']) # Plot --- energy KE = system.get_KE() PE = system.getPEGravity(pNA) - system.getPESprings() energy_output = Output([KE-PE],system) energy_output.calc(states) energy_output.plot_time() # Motion points = [pNA,pAB,pBtip,pBC, pCD] points_output = PointsOutput(points,system) y = points_output.calc(states) points_output.plot_time(20) points_output.animate(fps = 30,lw=2) from matplotlib import animation, rc from IPython.display import HTML HTML(points_output.anim.to_html5_video()) """From the animation above, we see that because we set the neutral position of the device to stand straight (vertical sides for the four-bar mechanism), our device would not just freely drop to the ground. # 6. Tuning: Now adjust the damper value to something nonzero, that over 10s shows that the system is settling. """ b = Constant(1e0,'b',system) system.addforce(-b*wNA,wNA)
c=KinematicConstraint(eq) variables = [qA,qB,qC,qD] guess = [initialvalues[item] for item in variables] result = c.solve_numeric(variables,guess,system.constant_values) ini = [] for item in system.get_state_variables(): if item in variables: ini.append(result[item]) else: ini.append(initialvalues[item]) po1 = PointsOutput(points, constant_values=system.constant_values) po1.calc(numpy.array([ini0,ini]),[0,1]) po1.plot_time() # po1.calc(numpy.array([ini,ini]),[0,1]) # po1.plot_time() pAcm = pNA+lA/2*A.x pBcm = pAB+lA/2*B.x pCcm = pNC+lA/2*C.x pDcm = pCD+lA/2*D.x vBD = pBD.time_derivative() vDB = pDB.time_derivative() wNA = N.get_w_to(A) wAB = A.get_w_to(B)