Physics Simulation Objects¶
Newtonian Pendulum¶
- class deepbench.physics_object.Pendulum(pendulum_arm_length, starting_angle_radians, noise_std_percent={'acceleration_due_to_gravity': 0.0, 'big_G_newton': None, 'pendulum_arm_length': 0.0, 'phi_planet': None, 'starting_angle_radians': 0.0}, acceleration_due_to_gravity=None, big_G_newton=None, phi_planet=None, mass_pendulum_bob=10.0, coefficient_friction=0.0)¶
- Parameters:
pendulum_arm_length (float)
starting_angle_radians (float)
noise_std_percent (dict)
acceleration_due_to_gravity (float | None)
big_G_newton (float | None)
phi_planet (float | None)
mass_pendulum_bob (float | None)
coefficient_friction (float | None)
- create_noise(noiseless=False, seed=None, n_steps=10, verbose=False)¶
Creates noise on top of simulate_pendulum_dynamics Also deals with the hierarchical case, where acceleration_due_to_gravity is defined via big_G_newton and phi_planet
- Parameters:
seed (int) – Random seed used to generate Gaussian noise
n_steps (int or Tuple[int,int]) – The shape of the noise to be created. This is specified in create_object using the shape of the input time array (or float).
noiseless (bool)
verbose (bool)
- Return type:
array
Examples (see create_object)
- create_object(time, noiseless=False, seed=None, verbose=False)¶
Given a single or array of times, simulates the pendulum position at each of these times and optionally adds Gaussian noise to each parameter.
- Parameters:
time (Union[float, np.array]) – A single moment in time, or an array of times (s)
noiseless (bool) – Enables a noise realization if True. Default is set to False
seed (int) – Random seed used to generate Gaussian noise
verbose (bool)
Example
>>> pendulum = Pendulum() >>> time = np.array(np.linspace(0, 10, 20)) >>> pend_position = pendulum.create_object(time, noiseless=True)
- destroy_noise()¶
Remove noise from the parameters
- displayObject(time)¶
Display the pendulum over times.
- Parameters:
time (Union[float, np.array]) – times to display the pendulum position
- Returns:
noiseless, noisy arrays at times “time”
- Return type:
tuple(np.ndarray, np.ndarray)
- simulate_pendulum_dynamics(time)¶
Simulate a pendulum with Neutonian physics
- Parameters:
time (Union[float, np.array]) – times to simulate
- Returns:
position of the pendulum.
- Return type:
np.ndarray
Example¶
Newtonian Pendulum¶
The pendulum object is one of the physics objects. This notebook runs through a couple of examples of initilizing the pendulum class, creating realizations with and without noise, and plotting the result.
import numpy as np
import matplotlib.pyplot as plt
from deepbench.physics_object import Pendulum
The first step is to initilize the pendulum class¶
Inputs include the arm length, the starting angle, and the acceleration due to gravity. The acceleration due to gravity is not required if newton's gravitational constant and phi planet are provided. (We give an example of a hierarchical pendulum below.) The pendulum class also requires 'noise_std_percent', which is a dictionary with the percent error for each parameter.
pendulum = Pendulum(
pendulum_arm_length=10.0,
starting_angle_radians=np.pi / 4,
acceleration_due_to_gravity=9.8,
noise_std_percent={
"pendulum_arm_length": 0.0,
"starting_angle_radians": 0.1,
"acceleration_due_to_gravity": 0.1,
},
)
Generate positions¶
Next, create an array of times and use the 'create_object' to draw the position at all these points in time. The noiseless parameter determines whether noise is added to the positions in time.
time = np.array(np.linspace(0, 50, 200))
pendulum_noisy = pendulum.create_object(time, noiseless=False)
pendulum_noiseless = pendulum.create_object(time, noiseless=True)
plt.clf()
plt.plot(time, pendulum_noisy, color = '#832161')
plt.scatter(time, pendulum_noisy, label = 'Noisy draws', color = '#832161')
plt.plot(time, pendulum_noiseless, color = '#EDCBB1')
plt.scatter(time, pendulum_noiseless, label = 'Noise free draws', color = '#EDCBB1')
legend = plt.legend(loc="upper right", edgecolor="black")
legend.get_frame().set_alpha(1.0)
plt.xlabel('time [s]')
plt.ylabel('x position of pendulum')
plt.show()
Hamiltonian Pendulum¶
- class deepbench.physics_object.HamiltonianPendulum(pendulum_arm_length, starting_angle_radians, acceleration_due_to_gravity=None, mass_pendulum_bob=10.0, noise_std_percent={'acceleration_due_to_gravity': None, 'mass_pendulum_bob': 0.0, 'pendulum_arm_length': 0.0, 'starting_angle_radians': 0.0})¶
The Hamiltonian Pendulum class.
- Parameters:
pendulum_arm_length (float) – The length of the pendulum arm
starting_angle_radians (float) – The starting angle of the pendulum (angle from the ‘ceiling’)
noise_std_percent (dict) – A dictionary of the Gaussian noise level to be applied to each parameter. The default is no noise. Each number is the standard deviation when multiplied by the parameter. See create_noise().
acceleration_due_to_gravity (float) – little g, local gravity coefficient
mass_pendulum_bob (float) – Mass of the pendulum bob, this is optional if calculation_type is position only.
Examples
>>> pendulum_obj = HamiltonianPendulum(pendulum_arm_length=10., starting_angle_radians=np.pi/4, acceleration_due_to_gravity=9.8, noise_std_percent= {'pendulum_arm_length': 0.1, 'starting_angle_radians': 0.1, 'acceleration_due_to_gravity': 0.1} )
- create_object(time, noiseless=True, seed=42)¶
Given a single or array of times, simulates the pendulum position at each of these times and optionally adds Gaussian noise to each parameter.
- Parameters:
time (Union[float, np.array]) – A single moment in time, or an array of times (s)
noiseless (bool) – Add noise to the pendulum parameters
seed (int) – Random seed for parameters
- Returns:
tuple q (np.ndarray): position. p (np.ndarray): momentum. dqdt (np.ndarray): velocity. dpdt (np.ndarray) - force. t_eval (np.ndarray) - times.
- dynamics_fn(t, coords)¶
derives the gradient of the hamiltonian function
- Parameters:
coords (np.ndarray) – coordinates of the pendulum
- Returns:
time derivates of p and q.
- Return type:
np.ndarray
- simulate_pendulum_dynamics(time, **kwargs)¶
Evaulate the hamilitonian at times time and return the position, momentum and time derviates
- Parameters:
time (np.ndarray) – Times to simulate
- Returns:
tuple q (np.ndarray): position. p (np.ndarray): momentum. dqdt (np.ndarray): velocity. dpdt (np.ndarray) - force. t_eval (np.ndarray) - times.
Example¶
Hamiltonian Pendulum¶
The Hamiltonian pendulum object is one of the physics objects. This notebook runs through a couple of examples in initilizing the pendulum class and plotting the result.
import numpy as np
import matplotlib.pyplot as plt
import sys, os
MAIN_DIR = os.path.dirname(os.path.abspath(''))
sys.path.insert(0, f'{MAIN_DIR}')
from deepbench.physics_object.hamiltonian_pendulum import HamiltonianPendulum
The first step is to initilize the pendulum class¶
Inputs include the arm length, the starting angle, and the acceleration due to gravity. The pendulum class also requires 'noise_std_percent', which is a dictionary with the percent error for each parameter.
pendulum = HamiltonianPendulum(
pendulum_arm_length=10.0,
starting_angle_radians=np.pi / 4,
acceleration_due_to_gravity=9.8,
noise_std_percent={
"pendulum_arm_length": 0.0,
"starting_angle_radians": 0.0,
"acceleration_due_to_gravity": 0.0,
},
)
Draw Diagrams¶
Next, create an array of times and use the 'create_object' returning an array containing q, p, dq/dt, dp/dt, and time steps respectively. Observe the phase space and position and time plots.
time = np.array(np.linspace(0, 50, 200))
pendulum_data = pendulum.create_object(time)
fig = plt.figure(figsize=(15, 5), facecolor='white')
ax = fig.add_subplot(1,2,1)
plt.scatter(pendulum_data[0], pendulum_data[1], label='Data', color = '#832161')
plt.xlabel("Position (q)", fontsize=14, labelpad=10)
plt.ylabel("Momentun (p)", fontsize=14, labelpad=10)
plt.title("Phase Space")
plt.legend(loc='upper right')
ax = fig.add_subplot(1,2,2)
plt.plot(pendulum_data[4], pendulum_data[0], label='Data', color = '#832161')
plt.ylabel("Position (q)", fontsize=14, labelpad=10)
plt.xlabel("Time", fontsize=14, labelpad=10)
plt.title("Position Vs. Time")
plt.legend(loc='upper right')
plt.show()