def testOrbit(self): Ms = 1.98847 * 10**30 # kg # M_earth = 5.9722 * 10**24 # kg Mj = 1.899 * 10**27 # kg Rj = 778.3 * 10**9 # m Tj = 3.743 * 10**8 # s # V_earth = 30 * 10**3 # m/s # r = 149.6 * 10**9 # m vel = 2 * pi * Rj / Tj sun_x = np.zeros(3) sun_v = np.zeros(3) jup_x = np.zeros(3) jup_v = np.zeros(3) jup_x[0] = Rj jup_v[1] = vel m = [Ms, Mj] v = np.concatenate((sun_x, jup_x, sun_v, jup_v)).reshape(-1) sys = NBodySystem(m) solver = SystemSolver(sys) res = solver.run([0, Tj * 1.5], v) solver.plotnd(res)
def testTwoPoints(self): # using IC from TODO sys = NBodySystem(body_masses=[1, 1], G=1) solver = SystemSolver(sys) max_t = 52 tspan = [0, max_t] y0 = np.zeros(2 * sys.body_dim * len(sys.body_masses), dtype=np.float64) y0[0:3] = [1, 0, 0] y0[3:6] = [-1, 0, 0] y0[6:9] = [0, .1, 0] y0[9:12] = [0, -.1, 0] run = solver.run(tspan, y0) # print() # print(*y0) # print(*run['results'].y[:, -1]) # clearFigs() # fig = solver.plotnd(run) # for axes in fig.axes: # axes.plot(np.zeros_like(run['results'].y[0])) t = run['results'].t y = run['results'].y # print(t.shape, y.shape) # print(t[-1], max_t) y0 = y[0] y1 = y[3] clearFigs() plt.figure() plt.plot(t, y0) plt.plot(t, y1)
def testFig8(self): # using IC from TODO sys = NBodySystem(body_masses=[1, 1, 1], G=1) solver = SystemSolver(sys) tspan = [0, 10] y0 = np.zeros(2 * sys.body_dim * len(sys.body_masses), dtype=np.float64) x1 = np.array([0.97000436, -0.24308753, 0]) x3p = np.array([-0.93240737, -0.86473146, 0]) y0[0:3] = x1 y0[3:6] = -x1 y0[6:9] = 0 y0[9:12] = -x3p / 2 y0[12:15] = -x3p / 2 y0[15:18] = x3p # print(sys.fun(np.zeros_like(y0), y0).reshape(6, -1)) run = solver.run(tspan, y0) clearFigs() solver.plotnd(run) # print(run['results'].y[:, -1].reshape(6, -1)) y_act = run['results'].y[:9] run['results'].y = y_act[:3] fig = solver.plot3d(run) run['results'].y = y_act[3:6] fig = solver.plot3d(run, fig=fig) run['results'].y = y_act[6:9] fig = solver.plot3d(run, fig=fig)
def testFig8Lce(self): # takes a long time, so its disabled sys = NBodySystem(body_masses=[1, 1, 1], G=1) solver = SystemSolver(sys) tspan = [0, 1.5] y0 = np.zeros(2 * sys.body_dim * len(sys.body_masses), dtype=np.float64) x1 = np.array([0.97000436, -0.24308753, 0]) x3p = np.array([-0.93240737, -0.86473146, 0]) y0[0:3] = x1 y0[3:6] = -x1 y0[6:9] = 0 y0[9:12] = -x3p / 2 y0[12:15] = -x3p / 2 y0[15:18] = x3p # print(sys.fun(np.zeros_like(y0), y0).reshape(6, -1)) lce, run = solver.get_lce(tspan[1], y0) t = run['results'].t[1:] y = run['results'].y[sys.dim:, 1:].reshape(sys.dim, sys.dim, -1) #print(y[:, :, -1]) lces = [] for i, t_val in enumerate(t): Df_y = y[:, :, i] lces.append(solver.calc_lce(Df_y, t_val)) print(lces[-1]) print(np.mean(lces[-5:])) clearFigs() plt.figure() plt.plot(t, lces) plt.show(True)
def testRandomInit(self): sys = NBodySystem(body_masses=[1, 1, 1], G=1) solver = SystemSolver(sys) tspan = [0, 100] expand = 10 y0 = np.random.rand(2 * sys.body_dim * len(sys.body_masses)) * expand y0[9:] /= expand / 2 total_mass = np.sum(sys.body_masses) vcm = np.zeros(3) for m, v in zip(sys.body_masses, y0[9:].reshape(3, -1)): vcm += m * v vcm /= total_mass yps = y0[9:].reshape(3, -1) yps -= vcm[None, :] run = solver.run(tspan, y0) clearFigs() solver.plotnd(run) # print(run['results'].y[:, -1].reshape(6, -1)) y_act = run['results'].y[:9] run['results'].y = y_act[:3] fig = solver.plot3d(run) run['results'].y = y_act[3:6] fig = solver.plot3d(run, fig=fig) run['results'].y = y_act[6:9] fig = solver.plot3d(run, fig=fig) plt.show(True)
def testWeirdLenSys(self): sys = DoublePendulumSystem(1, 2, .5, 1.5) slv = SystemSolver(sys) tf = 3 run1 = slv.run([0, tf], [pi, 1, 1, 0]) run2 = slv.run([0, tf], [pi, 1.001, 1, 0]) if self.render: self.runRender([run1, run2], 'odd_pend')
def setUp(self): # test data self.pendulum_system = DoublePendulumSystem() self.pendulum_solver = SystemSolver(self.pendulum_system) # run render test self.render_til = 10 self.render_long = False # slows test a lot self.render = False # slows test
def testExample(self): sys = RestrictedCircular3Body() slv = SystemSolver(sys) lce, lce_run = slv.get_lce() slv.calc_lce # print(lce) init = [1, -1, 1, -1] run = slv.run([0, 5], init) run['results'].y = run['results'].y[:2, :] slv.plot2d(run)
def testLCEPendulumLong(self): sys = DoublePendulumSystem() slv = SystemSolver(sys) theta = pi * 120 / 180 lce, run = slv.get_lce(T=100, y0=[theta, 0, 0, 0]) y = run['results'].y Df_y0 = y[sys.dim:, -1].reshape(sys.dim, sys.dim) print(Df_y0) print(lce) self.assertGreater(lce, 0)
def testLCEPendulumVaryAngle(self): sys = DoublePendulumSystem() slv = SystemSolver(sys) thetas = [] lces = [] for theta in np.arange(0.001, pi + 0.0001, pi / 20): lce, _ = slv.get_lce(T=100, y0=[theta, 0, 0, 0]) thetas.append(theta) lces.append(lce) clearFigs() plt.figure() plt.plot(thetas, lces) plt.show(True)
def testLCE(self): # slv.calc_lce(Df_y0, T) sys = RestrictedCircular3Body() slv = SystemSolver(sys) lce, lce_run = slv.get_lce(T=100) t = lce_run['results'].t y = lce_run['results'].y sys_dim = sys.dim lces = [] start_t = 10 for i in range(start_t, t.shape[0]): Df_y0 = y[sys_dim:, i].reshape(sys_dim, sys_dim) lce = slv.calc_lce(Df_y0, t[i]) lces.append(lce) plt.plot(t[start_t:], lces) plt.show(True)
def runLCETest(self, sigma, rho, beta, l1): sys = LorenzSystem(sigma, rho, beta) slv = SystemSolver(sys) lce, run = slv.get_lce(T=100) T0 = 0 t = run['results'].t[T0:] y = run['results'].y[:, T0:] lces = [] for i, t_val in enumerate(t): Df_y0 = y[sys.dim:, i].reshape(sys.dim, sys.dim) lces.append(slv.calc_lce(Df_y0, t_val)) clearFigs() plt.figure() plt.plot(t, lces) plt.show(True) print(Df_y0) print(lce, l1, (lce - l1) / l1) self.assertAlmostEqual((lce - l1) / l1, 0, places=0)
def testFig8LcePartition(self): # takes a long time, so its disabled sys = NBodySystem(body_masses=[1, 1, 1], G=1) solver = SystemSolver(sys) tspan = [0, 10] y0 = np.zeros(2 * sys.body_dim * len(sys.body_masses), dtype=np.float64) x1 = np.array([0.97000436, -0.24308753, 0]) x3p = np.array([-0.93240737, -0.86473146, 0]) y0[0:3] = x1 y0[3:6] = -x1 y0[6:9] = 0 y0[9:12] = -x3p / 2 y0[12:15] = -x3p / 2 y0[15:18] = x3p # print(sys.fun(np.zeros_like(y0), y0).reshape(6, -1)) tspan, lces = solver.quick_lce(tspan[1], y0, partition=None) print(np.mean(lces[-5:])) clearFigs() plt.figure() plt.plot(tspan, lces) plt.show(True)
from echonn.sys import CircleSystem, SystemSolver if __name__ == "__main__": sys = SystemSolver(CircleSystem()) lce, run = sys.get_lce() print('lce:', lce) print(run['results'].y[2:, -1].reshape(2, 2))
import os import pickle import numpy as np import matplotlib.pyplot as plt from copy import deepcopy from echonn.sys import SystemSolver res = pickle.load(open('doub_pend_results.p', 'rb')) run, lce, ts_data, results = res sys = run['system'] slvr = SystemSolver(sys) dir_pre = os.path.join('..', 'images', 'doub_pend') # print small details print('lce:', lce[0]) # LCE lce_val, lce_run = lce T0 = 0 t = lce_run['results'].t[T0:] y = lce_run['results'].y[:, T0:] lces = [] for i, t_val in enumerate(t): Df_y0 = y[sys.dim:, i].reshape(sys.dim, sys.dim) lces.append(slvr.calc_lce(Df_y0, t_val)) plt.figure() plt.title(f'Double Pendulum LCE ({lce_val:.3}) vs t') plt.xlabel('t') plt.ylabel('LCE') plt.plot(t, lces) plt.savefig(os.path.join(dir_pre, 'lce_converge.png'))
self.k[i] *= self.e[i-1] * self.k[i-1] self.levels = levels def fun(self, t, v): r = self.r k = self.k e = self.e trans = r * v*v/k y = np.zeros_like(v) y[0] += r * v[0] y[1:] += e*trans[:-1] y -= trans return y if __name__ == '__main__': biome1 = {'rate': 1, 'capacity': 1000, 'efficiency': [.1, .12], 'levels':3} biome2 = {'rate': .1, 'capacity': 100, 'efficiency': [.1, .9], 'levels':3} biome3 = {'rate': .01, 'capacity': 5000, 'efficiency': [.1, .2], 'levels':3} problem = EcoSystem([biome1, biome2, biome3]) solver = SystemSolver(problem) y0 = np.zeros(problem.dim) y0[0] = 1 y0[3] = 1 y0[4] = 1000 y0[6] = 1 solver.run([0, 200], y0) solver.plotnd() plt.show(True)
def setUp(self): # test data self.circle_solver = SystemSolver(CircleSystem()) self.lorenz_solver = SystemSolver(LorenzSystem())
from echonn.sys import LorenzSystem, SystemSolver if __name__ == "__main__": data = [ [16, 45.92, 4, 1.50255], [16, 40, 4, 1.37446], [10, 28, 8 / 3, 0.90566], ] results = [] for sigma, rho, beta, lambda_ in data: lces = [] print('calculating lces...') for i in range(10): sys = LorenzSystem(sigma, rho, beta) slv = SystemSolver(sys) lce, _ = slv.get_lce() print('\t{}:'.format(i), lce) lces.append(lce) res = {} res['beta'] = beta res['rho'] = rho res['sigma'] = sigma res['lambda'] = lambda_ res['mean'] = np.mean(lces) res['std'] = np.std(lces) res['error'] = res['mean'] - lambda_ res['relative error'] = res['error'] / lambda_ print(res) print()
import os from echonn.sys import NBodySystem, SystemSolver import numpy as np import matplotlib.pyplot as plt dir_pre = os.path.join('..', 'images', '3body') sys = NBodySystem(body_masses=[1, 1, 1], body_dim=2, G=1) solver = SystemSolver(sys) tspan = [0, 200] y0 = np.zeros(2 * sys.body_dim * len(sys.body_masses), dtype=np.float64) x1 = np.array([0.97000436, -0.24308753]) x3p = np.array([-0.93240737, -0.86473146]) y0[0:2] = x1 y0[2:4] = -x1 # y0[4:6] = zero y0[6:8] = -x3p / 2 y0[8:10] = -x3p / 2 y0[10:12] = x3p # print(sys.fun(np.zeros_like(y0), y0).reshape(6, -1)) lce, run = solver.get_lce(tspan[1], y0) t = run['results'].t[1:] y = run['results'].y[sys.dim:, 1:].reshape(sys.dim, sys.dim, -1) #print(y[:, :, -1]) lces = [] for i, t_val in enumerate(t): Df_y = y[:, :, i] lces.append(solver.calc_lce(Df_y, t_val))
import os import pickle import numpy as np import matplotlib.pyplot as plt from copy import deepcopy from echonn.sys import SystemSolver res = pickle.load(open('lorenz_results.p', 'rb')) run, lce, ts_data, results = res sys = run['system'] slvr = SystemSolver(sys) dir_pre = os.path.join('..', 'images', 'lorenz') print('lce:', lce[0]) # LCE lce_val, lce_run = lce T0 = 0 t = lce_run['results'].t[T0:] y = lce_run['results'].y[:, T0:] lces = [] for i, t_val in enumerate(t): Df_y0 = y[sys.dim:, i].reshape(sys.dim, sys.dim) lces.append(slvr.calc_lce(Df_y0, t_val)) plt.figure() plt.title(f'Lorenz LCE ({lce_val:.3}) vs t') plt.xlabel('t') plt.ylabel('LCE') plt.plot(t, lces) plt.savefig(os.path.join(dir_pre, 'lce_converge.png'))
import os import numpy as np import matplotlib.pyplot as plt from echonn.sys import LorenzSystem, SystemSolver if __name__ == "__main__": lorenz_3d_plot = os.path.join('..', 'images', 'lorenz_3d_plot.png') lorenz_nd_plot = os.path.join('..', 'images', 'lorenz_nd_plot.png') slv = SystemSolver(LorenzSystem()) res = slv.run([0, 50], [10, 20, 30]) slv.plotnd(res, dims=['x', 'y', 'z']) plt.savefig(lorenz_nd_plot) slv.plot3d(res) plt.savefig(lorenz_3d_plot) plt.show(True)
def setUp(self): # test data self.pendulum_system = DoublePendulumSystem() self.pendulum_solver = SystemSolver(self.pendulum_system) self.run_dp_render = False
def runLCETestShort(self, sigma, rho, beta, l1): sys = LorenzSystem(sigma, rho, beta) slv = SystemSolver(sys) slv.get_lce(T=2)
def testLCEPendulum(self): sys = DoublePendulumSystem() slv = SystemSolver(sys) slv.get_lce(T=2, y0=[1.8, 1.8, 0, 0])
def setUp(self): # test data self.circle_solver = SystemSolver(CircleSystem())
def testCircleLCE(self): sys = SystemSolver(CircleSystem()) lce, run = sys.get_lce() self.assertAlmostEqual(lce, 0, places=4)