Example #1
0
t1, t2 = np.meshgrid(theta1, theta2)

initial_angles = np.hstack((np.reshape(t1, (-1, 1)), np.reshape(t2, (-1, 1))))
initial_momenta = np.zeros(initial_angles.shape)

h = tf.constant(.03, dtype=tf.float32, name='time_step')
#x0 = [[np.random.rand()*np.pi,2*np.random.rand()*np.pi,0,0] for i in range(360000)]
x0 = np.hstack((initial_angles, initial_momenta))
t0 = 0

with tf.variable_scope('state'):
    x = tf.Variable(x0, dtype=tf.float32)
    t = tf.Variable(t0, dtype=tf.float32)

dxdt = partial(double_pendulum_eom, l1, l2, m1, m2, g)
update = rk4_step(dxdt, t, x, h)

sess.run(tf.global_variables_initializer())
summary_writer = tf.summary.FileWriter('logdir/', sess.graph)

N = 400
state = np.array([x0] * N, dtype=np.float32)

# run simulation
start = time.time()

for i in range(N):
    _, state[i] = sess.run([update, x])

end = time.time()
print(end - start)
m1 = 1
m2 = 1
g = 10
dt = 0.01
x0 = np.array([[1, -1, 0, 0]])
t0 = 0

# setup computational graph
h = tf.constant(dt, dtype=tf.float64, name='time_step')

with tf.variable_scope('state'):
    x = tf.placeholder(tf.float64, (None, 4), name="state_input")
    t = tf.Variable(t0, dtype=tf.float64)

dxdt = partial(double_pendulum_eom, l1, l2, m1, m2, g)
dx = rk4_step(dxdt, t, x, h)


def integrate(l1, l2, sess, dx, x0):
    state = np.array(x0, dtype=np.float32)
    while True:
        x1 = l1 * np.sin(state[0, 0])
        y1 = -l1 * np.cos(state[0, 0])
        x2 = x1 + l2 * np.sin(state[0, 1])
        y2 = y1 - l2 * np.cos(state[0, 1])
        yield [[0, x1, x2], [0, y1, y2]]
        state = state + sess.run([dx], feed_dict={x: state})[0]


sess.run(tf.global_variables_initializer())