-
Notifications
You must be signed in to change notification settings - Fork 93
/
utils.py
34 lines (28 loc) · 939 Bytes
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Generalized Lagrangian Networks | 2020
# Miles Cranmer, Sam Greydanus, Stephan Hoyer (...)
import jax.numpy as jnp
import pickle
def wrap_coords(state):
# wrap generalized coordinates to [-pi, pi]
return jnp.concatenate([(state[:2] + jnp.pi) % (2 * jnp.pi) - jnp.pi, state[2:]])
def rk4_step(f, x, t, h):
# one step of Runge-Kutta integration
k1 = h * f(x, t)
k2 = h * f(x + k1/2, t + h/2)
k3 = h * f(x + k2/2, t + h/2)
k4 = h * f(x + k3, t + h)
return x + 1/6 * (k1 + 2 * k2 + 2 * k3 + k4)
def radial2cartesian(t1, t2, l1, l2):
# Convert from radial to Cartesian coordinates.
x1 = l1 * jnp.sin(t1)
y1 = -l1 * jnp.cos(t1)
x2 = x1 + l2 * jnp.sin(t2)
y2 = y1 - l2 * jnp.cos(t2)
return x1, y1, x2, y2
def write_to(data, path):
with open(path, 'wb') as f:
pickle.dump(data, f, protocol=pickle.HIGHEST_PROTOCOL)
def read_from(path):
with open(path, 'rb') as f:
data = pickle.load(f)
return data