def pendulum_spring_reference(x0, time=0.0, *args): # Initialize the parameters to bias spring 1 reference angle pendulum = args[0] pendulum.parameters.b1 = 0.0 pendulum.parameters.b2 = 0.0 pendulum.parameters.k1 = 10.0 pendulum.parameters.k2 = 10.0 pendulum.parameters.s_theta_ref1 = np.deg2rad(-10.0) pendulum.parameters.s_theta_ref2 = np.deg2rad(10.0) pylog.info( "1b. Running pendulum_system for analysing role of spring reference") pylog.info(pendulum.parameters.showParameters()) title = "{} Spring Reference 1(x0 = {})" res = integrate(pendulum_integration, x0, time, args=args) res.plot_state(title.format("State", x0)) res.plot_phase(title.format("Phase", x0)) # Initialize the pendulum.parameters to bias spring 2 reference angle pendulum.parameters.b1 = 0.0 pendulum.parameters.b2 = 0.0 pendulum.parameters.k1 = 10.0 pendulum.parameters.k2 = 10.0 pendulum.parameters.s_theta_ref1 = np.deg2rad(-75.0) pendulum.parameters.s_theta_ref2 = np.deg2rad(75.) pylog.info( "1b. Running pendulum_system for analysing role of spring reference") pylog.info(pendulum.parameters.showParameters()) title = "{} Spring Reference 2(x0 = {})" res = integrate(pendulum_integration, x0, time, args=args) res.plot_state(title.format("State", x0)) res.plot_phase(title.format("Phase", x0)) # Initialize the pendulum.parameters to bias spring 2 reference angle pendulum.parameters.b1 = 0.0 pendulum.parameters.b2 = 0.0 pendulum.parameters.k1 = 10.0 pendulum.parameters.k2 = 10.0 pendulum.parameters.s_theta_ref1 = np.deg2rad(0.0) pendulum.parameters.s_theta_ref2 = np.deg2rad(75.0) pylog.info( "1c. Running pendulum_system for analysing role of variable spring reference" ) pylog.info(pendulum.parameters.showParameters()) title = "{} Variable Spring Reference 1(x0 = {})" res = integrate(pendulum_integration, x0, time, args=args) res.plot_state(title.format("State", x0)) res.plot_phase(title.format("Phase", x0))
def exercise3(clargs): """ Exercise 3 """ parameters = PendulumParameters() # Checkout pendulum.py for more info pylog.info(parameters) # Simulation parameters time = np.arange(0, 30, 0.01) # Simulation time x0 = [1, 0.0] # Initial state # To use/modify pendulum parameters (See PendulumParameters documentation): # parameters.g = 9.81 # Gravity constant # parameters.m = 1. # Mass # parameters.L = 1. # Length # parameters.I = 1. # Inertia (Automatically computed!) # parameters.d = 0.3 # damping # parameters.sin = np.sin # Sine function # parameters.dry = False # Use dry friction (True or False) # --------------------------------------------------------------------------- # "Evolution of pendulum in normal conditions must be implemented" # --------------------------------------------------------------------------- res = integrate(pendulum_system, x0, time, args=(parameters,)) res.plot_state("State - normal") res.plot_phase("Phase - normal") # --------------------------------------------------------------------------- # "Evolution of pendulum without damping must be implemented" # --------------------------------------------------------------------------- res = integrate(pendulum_system, x0, time, args=(PendulumParameters(d=0),)) res.plot_state("State - d=0") res.plot_phase("Phase - d=0") # --------------------------------------------------------------------------- # "Evolution of pendulum with perturbations must be implemented" # --------------------------------------------------------------------------- res = integrate(pendulum_system, x0, time, args=(PendulumParameters(), -2.0)) res.plot_state("State - torque") res.plot_phase("Phase - torque") # --------------------------------------------------------------------------- # "Evolution of pendulum with dry friction must be implemented" # --------------------------------------------------------------------------- res = integrate(pendulum_system, x0, time, args=(PendulumParameters(dry=True), 0.0)) res.plot_state("State - dry") res.plot_phase("Phase - dry") # Show plots of all results if not clargs.save_figures: plt.show()
def simulate(self): #: Run the integrator fpr specified time self.res = integrate(self.derivative, self.x0, self.time, args=self.args, rk=True, tol=True)
def evolution_no_damping(x0, time): """ No damping simulation """ pylog.info("Evolution with no damping") parameters = PendulumParameters(d=0.0) pylog.info(parameters) title = "{} without damping (x0={})" res = integrate(pendulum_system, x0, time, args=(parameters, )) res.plot_state(title.format("State", x0)) res.plot_phase(title.format("Phase", x0))
def evolution_perturbation(x0, time): """ Perturbation and no damping simulation """ pylog.info("Evolution with perturbations") parameters = PendulumParameters(d=0.0) pylog.info(parameters) title = "{} with perturbation (x0={})" res = integrate(pendulum_perturbation, x0, time, args=(parameters, )) res.plot_state(title.format("State", x0)) res.plot_phase(title.format("Phase", x0))
def evolution_dry(x0, time): """ Dry friction simulation """ pylog.info("Evolution with dry friction") parameters = PendulumParameters(d=0.03, dry=True) pylog.info(parameters) title = "{} with dry friction (x0={})" res = integrate(pendulum_system, x0, time, args=(parameters, )) res.plot_state(title.format("State", x0)) res.plot_phase(title.format("Phase", x0))
def evolution_cases(time): """ Normal simulation """ pylog.info("Evolution with basic paramater") x0_cases = [["Normal", [0.1, 0]], ["Stable", [0.0, 0.0]], ["Unstable", [np.pi, 0.0]], ["Multiple loops", [0.1, 10.0]]] title = "{} case {} (x0={})" parameters = PendulumParameters() for name, x0 in x0_cases: res = integrate(pendulum_system, x0, time, args=(parameters, )) res.plot_state(title.format(name, "state", x0)) res.plot_phase(title.format(name, "phase", x0))
def pendulum_spring_constant(x0, time=0.0, *args): # Initialize the parameters to bias spring 1 pendulum = args[0] pendulum.parameters.b1 = 0.0 pendulum.parameters.b2 = 0.0 pendulum.parameters.k1 = 0.1 pendulum.parameters.k2 = 0.1 pylog.info( "1b. Running pendulum_system for analysing role of spring constant") pylog.info(pendulum.parameters.showParameters()) title = "{} Spring Constant 1(x0 = {})" res = integrate(pendulum_integration, x0, time, args=args) res.plot_state(title.format("State", x0)) res.plot_phase(title.format("Phase", x0)) # Initialize the parameters to bias spring 2 pendulum.parameters.b1 = 0.0 pendulum.parameters.b2 = 0.0 pendulum.parameters.k1 = 100. pendulum.parameters.k2 = 100. pylog.info( "1b. Running pendulum_system for analysing role of spring constant") pylog.info(pendulum.parameters.showParameters()) title = "{} Spring Constant 2(x0 = {})" res = integrate(pendulum_integration, x0, time, args=args) res.plot_state(title.format("State", x0)) res.plot_phase(title.format("Phase", x0)) # Initialize the pendulum.parameters to bias spring 1 pendulum.parameters.b1 = 0.0 pendulum.parameters.b2 = 0.0 pendulum.parameters.k1 = 1.0 pendulum.parameters.k2 = 100.0 pylog.info( "1b. Running pendulum_system for analysing role of variable spring constant" ) pylog.info(pendulum.parameters.showParameters()) title = "{} Variable Spring Constant 1(x0 = {})" res = integrate(pendulum_integration, x0, time, args=args) res.plot_state(title.format("State", x0)) res.plot_phase(title.format("Phase", x0))
def pendulum_limit_cycle(x0, time=0.0, *args): # Initialize the parameters without damping pendulum = args[0] pendulum.parameters.b1 = 0.0 pendulum.parameters.b2 = 0.0 pylog.info(pendulum.parameters.showParameters()) pylog.info( "1a. Running pendulum_system with springs to study limit cycle behavior") title = "{} Limit Cycle(x0 = {})" res = integrate(pendulum_perturbation, x0, time, args=args) res.plot_state(title.format("State", x0)) res.plot_phase(title.format("Phase", x0))
def pendulum_spring_damper(x0, time=0.0, *args): """ Function to analyse the pendulum spring damper system""" pendulum = args[0] pendulum.parameters.b1 = 0.5 pendulum.parameters.b2 = 0.5 pendulum.parameters.k1 = 50.0 pendulum.parameters.k2 = 50.0 pendulum.parameters.s_theta_ref1 = np.deg2rad(-45.0) pendulum.parameters.s_theta_ref2 = np.deg2rad(45.0) pylog.info( "20. Running pendulum_system for analysing role of spring and damper muscle") pylog.info(pendulum.parameters.showParameters()) title = "{} Spring Damper (x0 = {})" res = integrate(pendulum_perturbation, x0, time, args=args) res.plot_state(title.format("State", x0)) res.plot_phase(title.format("Phase", x0))
def hopf_ocillator(): """ 4a - Hopf oscillator simulation """ params = HopfParameters(mu=1., omega=1.0) time = np.arange(0, 30, 0.01) title = "Hopf oscillator {} (x0={})" label = ["x0", "x1"] x0_list = [[1e-3, 1e-3], [1.0, 1.0]] for x0 in x0_list: hopf = integrate(hopf_equation, x0, time, args=(params,)) hopf.plot_state(title.format("state", x0), label) hopf_multiple = integrate_multiple( hopf_equation, x0_list, time, args=(params,) ) hopf_multiple.plot_phase(title.format("phase", x0_list), label=label)
def pendulum_set_position(x0, time=0.0, *args): """ Function to analyse the pendulum spring damper system""" pendulum = args[0] pendulum.parameters.b1 = 1. pendulum.parameters.b2 = 1. pendulum.parameters.k1 = 50.0 pendulum.parameters.k2 = 50.0 pendulum.parameters.s_theta_ref1 = np.deg2rad(0.0) pendulum.parameters.s_theta_ref2 = np.deg2rad(65.6) pylog.info("1b. Running pendulum_system to set fixed position") pylog.info(pendulum.parameters.showParameters()) title = "{} Pendulum Fixed Position (x0 = {})" res = integrate(pendulum_integration, x0, time, args=args) pylog.debug('Position : {}'.format(np.rad2deg(res.state[-1]))) res.plot_state(title.format("State", x0)) res.plot_phase(title.format("Phase", x0))
def exercise3(clargs): """ Exercise 3 """ parameters = PendulumParameters() # Checkout pendulum.py for more info pylog.info(parameters) # Simulation parameters time = np.arange(0, 30, 0.01) # Simulation time x0 = [0.1, 0.0] # Initial state # To use/modify pendulum parameters (See PendulumParameters documentation): # parameters.g = 9.81 # Gravity constant # parameters.m = 1. # Mass # parameters.L = 1. # Length # parameters.I = 1. # Inertia (Automatically computed!) # parameters.d = 0.3 # damping # parameters.sin = np.sin # Sine function # parameters.dry = False # Use dry friction (True or False) # Example of system integration (Similar to lab1) # (NOTE: pendulum_equation must be imlpemented first) pylog.debug("Running integration example") res = integrate(pendulum_system, x0, time, args=(parameters,)) res.plot_state("State") res.plot_phase("Phase") # Evolutions # Write code here (You can add functions for the different cases) pylog.warning( "Evolution of pendulum in normal conditions must be implemented" ) pylog.warning( "Evolution of pendulum without damping must be implemented" ) pylog.warning( "Evolution of pendulum with perturbations must be implemented" ) pylog.warning( "Evolution of pendulum with dry friction must be implemented" ) # Show plots of all results if not clargs.save_figures: plt.show()
def coupled_hopf_ocillator(): """ 4b - Coupled Hopf oscillator simulation """ param_set = [ CoupledHopfParameters( mu=[1., 1.], omega=[1.0, 1.2], k=[-0.5, -0.5] ), CoupledHopfParameters( mu=[1., 1.], omega=[1.0, 2.0], k=[-0.5, -0.5] ) ] for param in param_set: for x0 in [[0.0, 1.0, 0.0, 1.0], [1e-3]*4]: time = np.arange(0, 30, 0.01) hopf = integrate(coupled_hopf_equation, x0, time, args=(param,)) title = "Coupled Hopf oscillator {} (x0={}, {})" label = ["x0", "x1", "x2", "x3"] label_angle = ["angle0", "angle1"] hopf.plot_state(title.format("state", x0, param), label, n_subs=2) hopf.plot_angle(title.format("angle", x0, param), label_angle)
def step(self, x0, time, stimulation, muscle_length=None): """ Step the system.""" args = (stimulation, muscle_length) res = integrate(self.muscle_mass_system, x0, time, args=args) return res
def integration(x0, time, A, name, **kwargs): """ System integration """ labels = kwargs.pop("label", ["State {}".format(i) for i in range(2)]) sys_int = integrate(ode, x0, time, args=(A, )) sys_int.plot_state("{}_state".format(name), labels) sys_int.plot_phase("{}_phase".format(name))
def exercise1(): """ Exercise 1 """ pylog.info("Executing Lab 4 : Exercise 1") pendulum = PendulumSystem() pendulum.parameters = PendulumParameters() pylog.info( "Find more information about Pendulum Parameters in SystemParameters.py" ) pylog.info("Executing Lab 4 : Exercise 1") # Initialize a new pendulum system with default parameters pendulum = PendulumSystem() pendulum.parameters = PendulumParameters() pylog.info( "Find more information about Pendulum Parameters in SystemParameters.py" ) # To change a parameter you could so by, # >>> pendulum.parameters.L = 1.5 # The above line changes the length of the pendulum # You can instantiate multiple pendulum models with different parameters # For example, """ >>> pendulum_1 = PendulumSystem() >>> pendulum_1.parameters = PendulumParameters() >>> pendulum_1.parameters.L = 0.5 >>> parameters_2 = PendulumParameters() >>> parameters_2.L = 0.5 >>> pendulum_2 = PendulumSystem(paramters=parameters_2) """ # Above examples shows how to create two istances of the pendulum # and changing the different parameters using two different approaches pylog.warning("Loading default pendulum pendulum.parameters") pylog.info(pendulum.parameters.showParameters()) # Simulation Parameters t_start = 0.0 t_stop = 10.0 dt = 0.01 pylog.warning("Using large time step dt={}".format(dt)) time = np.arange(t_start, t_stop, dt) x0 = [0.5, 0.0] res = integrate(pendulum_integration, x0, time, args=(pendulum, )) pylog.info("Instructions for applying pertubations") # Use pendulum_perturbation method to apply pertubations # Define the pertubations inside the function pendulum_perturbation # res = integrate(pendulum_perturbation, x0, time, args=(pendulum,)) res.plot_state("State") res.plot_phase("Phase") # To animate the model, use the SystemAnimation class # Pass the res(states) and systems you wish to animate simulation1 = SystemAnimation(res, pendulum, handler="Pendulum animation") simulation2 = SystemAnimation(res, pendulum, handler="Pendulum animation 2") # If you want to disable the parameters in the animation plot # simulation2 = SystemAnimation(res, pendulum, handler="Pendulum animation 2", show_parameters=False) # To start the animation, use either simulation.animate() or plt.show() # simulation.animate() if DEFAULT["save_figures"] is False: plt.show() return
def exercise1(): """ Exercise 1 """ pylog.info("Executing Lab 4 : Exercise 1") pendulum = PendulumSystem() pendulum.parameters = PendulumParameters() pylog.info( "Find more information about Pendulum Parameters in SystemParameters.py") pylog.info("Executing Lab 4 : Exercise 1") # Initialize a new pendulum system with default parameters pendulum = PendulumSystem() pendulum.parameters = PendulumParameters() pylog.info( "Find more information about Pendulum Parameters in SystemParameters.py") # To change a parameter you could so by, # >>> pendulum.parameters.L = 1.5 # The above line changes the length of the pendulum # You can instantiate multiple pendulum models with different parameters # For example, """ >>> pendulum_1 = PendulumSystem() >>> pendulum_1.parameters = PendulumParameters() >>> pendulum_1.parameters.L = 0.5 >>> parameters_2 = PendulumParameters() >>> parameters_2.L = 0.5 >>> pendulum_2 = PendulumSystem(paramters=parameters_2) """ # Above examples shows how to create two istances of the pendulum # and changing the different parameters using two different approaches pendulum.parameters.k1 = 50 pendulum.parameters.k2 = 50 pendulum.parameters.s_theta_ref1 = 1.0 pendulum.parameters.s_theta_ref2 = 1.0 pendulum.parameters.b1 = 0.5 pendulum.parameters.b2 = 0.5 pylog.warning("Loading default pendulum pendulum.parameters") pylog.info(pendulum.parameters.showParameters()) # Simulation Parameters t_start = 0.0 t_stop = 10.0 dt = 0.01 pylog.warning("Using large time step dt={}".format(dt)) time = np.arange(t_start, t_stop, dt) x0 = [0.5, 0.0] res = integrate(pendulum_integration, x0, time, args=(pendulum,)) pylog.info("Instructions for applying pertubations") # Use pendulum_perturbation method to apply pertubations # Define the pertubations inside the function pendulum_perturbation # res = integrate(pendulum_perturbation, x0, time, args=(pendulum,)) res.plot_state("State") res.plot_phase("Phase") x0 = [0.5, 0.1] pendulum_limit_cycle(x0, time, pendulum) pendulum_spring_constant(x0, time, pendulum) pendulum_spring_reference(x0, time, pendulum) pendulum_spring_damper(x0, time, pendulum) pendulum_set_position(x0, time, pendulum) if DEFAULT["save_figures"] is False: plt.show() return
def step(self, x0, time, *args): """ Step the system.""" res = integrate(self.muscle.dxdt, x0, time, args=args) return res