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

Pendulum Example

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

HamiltonianPendulumExample