.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/plot_forward_sim.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_plot_forward_sim.py: Forward Simulation ================== This example simulates and visualizes the uncontrolled motion of the model such that the model falls down on the treadmill. It also compares the evaluation speed of PyDy's and Autolev's models. .. GENERATED FROM PYTHON SOURCE LINES 9-21 .. code-block:: Python import timeit from algait2de.gait2de import evaluate_autolev_rhs from pydy.codegen.ode_function_generators import generate_ode_function from pygait2d import derive, simulate from pygait2d import utils from scipy.integrate import odeint import matplotlib.pyplot as plt import numpy as np import sympy as sm import yaml .. GENERATED FROM PYTHON SOURCE LINES 22-24 Derive the equations of motion, including a constant treadmill motion and passive joint torques. .. GENERATED FROM PYTHON SOURCE LINES 24-29 .. code-block:: Python symbolics = derive.derive_equations_of_motion(treadmill=True, passive_torques=True, stiffness_exp=3, ) .. GENERATED FROM PYTHON SOURCE LINES 30-32 Load a parameter mapping from pygait2d symbol to numerical value, as well as a mapping of the symbol string to numerical value. .. GENERATED FROM PYTHON SOURCE LINES 32-43 .. code-block:: Python try: par_map = simulate.load_constants(symbolics.constants, 'example_constants.yml') with open('example_constants.yml', 'r') as f: constants_dict = yaml.load(f, Loader=yaml.SafeLoader) except FileNotFoundError: par_map = simulate.load_constants(symbolics.constants, 'examples/example_constants.yml') with open('examples/example_constants.yml', 'r') as f: constants_dict = yaml.load(f, Loader=yaml.SafeLoader) .. GENERATED FROM PYTHON SOURCE LINES 44-48 Use PyDy to generate a function that can evaluate the right hand side of the ordinary differential equations of the multibody system. This uses PyDy's code generation settings that result in the fastest numerical evaluation times at the cost of a slower code generation and compilation time. .. GENERATED FROM PYTHON SOURCE LINES 48-62 .. code-block:: Python rhs = generate_ode_function( symbolics.kanes_method.forcing, symbolics.coordinates, symbolics.speeds, constants=list(par_map.keys()), mass_matrix=symbolics.kanes_method.mass_matrix, coordinate_derivatives=sm.Matrix(symbolics.speeds), specifieds=symbolics.specifieds, generator='cython', linear_sys_solver='sympy', constants_arg_type='array', specifieds_arg_type='array', ) .. GENERATED FROM PYTHON SOURCE LINES 63-64 Prepare numerical arrays to be passed to the ODE functions. .. GENERATED FROM PYTHON SOURCE LINES 64-74 .. code-block:: Python specifieds_vals = np.zeros(len(symbolics.specifieds)) # zero torques specifieds_vals[-1] = 1.0 # treadmill speed args = (specifieds_vals, np.array(list(par_map.values()))) initial_conditions = np.zeros(len(symbolics.states)) initial_conditions[1] = 1.0 # set hip above ground initial_conditions[3] = np.deg2rad(5.0) # right hip angle initial_conditions[6] = -np.deg2rad(5.0) # left hip angle .. GENERATED FROM PYTHON SOURCE LINES 75-76 Time the average execution of PyDy's ODE function evaluation. .. GENERATED FROM PYTHON SOURCE LINES 76-79 .. code-block:: Python print('PyDy evaluation time:', timeit.timeit(lambda: rhs(initial_conditions, 0.0, *args), number=1000)) .. rst-class:: sphx-glr-script-out .. code-block:: none PyDy evaluation time: 0.003741227999853436 .. GENERATED FROM PYTHON SOURCE LINES 80-81 Time the average execution of Autolev s ODE function evaluation. .. GENERATED FROM PYTHON SOURCE LINES 81-89 .. code-block:: Python autolev_constants_dict = simulate.map_values_to_autolev_symbols(constants_dict) print('Autolev evaluation time:', timeit.timeit(lambda: evaluate_autolev_rhs(initial_conditions[:9], initial_conditions[9:], specifieds_vals, autolev_constants_dict), number=1000)) .. rst-class:: sphx-glr-script-out .. code-block:: none Autolev evaluation time: 0.005319942999904015 .. GENERATED FROM PYTHON SOURCE LINES 90-92 Simulate the model for two seconds using the LSODA integrator (switches between stiff and non-stiff modes). .. GENERATED FROM PYTHON SOURCE LINES 92-95 .. code-block:: Python time_vector = np.linspace(0.0, 2.0, num=61) trajectories = odeint(rhs, initial_conditions, time_vector, args=args) .. GENERATED FROM PYTHON SOURCE LINES 96-97 Visualization of the resulting motion from the forward simulation. .. GENERATED FROM PYTHON SOURCE LINES 97-106 .. code-block:: Python scene, fig, ax = utils.plot(symbolics, time_vector, initial_conditions, args[0], args[1]) ax.set_xlim((-0.8, 0.8)) ax.set_ylim((-0.2, 1.4)) ani = utils.animate(scene, fig, time_vector, trajectories, np.zeros((len(time_vector), len(symbolics.specifieds))), args[1]) plt.show() .. container:: sphx-glr-animation .. raw:: html
.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 12.901 seconds) .. _sphx_glr_download_examples_plot_forward_sim.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_forward_sim.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_forward_sim.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_forward_sim.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_