def test_t9(algorithm, const): def odefun(y, _, k): return 2 * y[1], 2 * (-(4 * y[2] * y[1] + 2 * y[0]) / (k[0] + y[2]**2)), 2 def odejac(y, _, k): df_dy = np.array( [[0, 2, 0], [ -4 / (y[2]**2 + k[0]), -(8 * y[2]) / (y[2]**2 + k[0]), (4 * y[2] * (2 * y[0] + 4 * y[1] * y[2])) / (y[2]**2 + k[0])**2 - (8 * y[1]) / (y[2]**2 + k[0]) ], [0, 0, 0]]) df_dp = np.empty((3, 0)) return df_dy, df_dp def bcfun(y0, yf, _, __, k): return y0[0] - 1 / (1 + k[0]), yf[0] - 1 / (1 + k[0]), y0[2] + 1 algo = Shooting(odefun, None, bcfun, algorithm=algorithm, num_arcs=2) algo.set_derivative_jacobian(odejac) solinit = Trajectory() solinit.t = np.linspace(0., 1., 2) # noinspection PyTypeChecker solinit.y = np.array([[1. / (1. + const), 0., -1.], [1. / (1. + const), 1., 1.]]) solinit.const = np.array([const]) sol = algo.solve(solinit)['sol'] e1 = 1 / (sol.const[0] + sol.y[:, 2]**2) e2 = -(2 * sol.y[:, 2]) / (sol.y[:, 2]**2 + sol.const[0])**2 assert all(e1 - sol.y[:, 0] < tol) assert all(e2 - sol.y[:, 1] < tol)
def test_t2(const): def odefun(y, _, k): return y[1], y[1] / k[0] def odejac(_, __, k): df_dy = np.array([[0, 1], [0, 1 / k[0]]]) df_dp = np.empty((2, 0)) return df_dy, df_dp def bcfun(y0, yf, _, __, ___): return y0[0] - 1, yf[0] algo = SPBVP(odefun, None, bcfun) algo.set_derivative_jacobian(odejac) solinit = Trajectory() solinit.t = np.linspace(0, 1, 2) solinit.y = np.array([[0, 1], [0, 1]]) solinit.const = np.array([const]) sol = algo.solve(solinit)['sol'] e1 = (1.e0 - np.exp( (sol.t - 1.e0) / sol.const)) / (1.e0 - np.exp(-1.e0 / sol.const)) e2 = np.exp((sol.t - 1) / sol.const) / (sol.const * (1 / np.exp(1 / sol.const) - 1)) assert all(e1 - sol.y[:, 0] < tol) assert all(e2 - sol.y[:, 1] < tol)
def test_t5(algorithm, const): def odefun(y, _, k): return (2 * y[1], 2 * ((y[0] + y[2] * y[1] - (1 + k[0] * np.pi**2) * np.cos(np.pi * y[2]) + y[2] * np.pi * np.sin(np.pi * y[2])) / k[0]), 2) def odejac(y, _, k): df_dy = np.array( [[0, 2, 0], [ 2 / k[0], 2 * y[2] / k[0], (2 * (y[1] + np.pi * np.sin(np.pi * y[2]) + np.pi * np.sin(np.pi * y[2]) * (k * np.pi**2 + 1) + np.pi * np.pi * y[2] * np.cos(np.pi * y[2]))) / k[0] ], [0, 0, 0]]) df_dp = np.empty((3, 0)) return df_dy, df_dp def bcfun(y0, yf, _, __, ___): return y0[0] + 1, yf[0] + 1, y0[2] + 1 algo = Shooting(odefun, None, bcfun, algorithm=algorithm) algo.set_derivative_jacobian(odejac) solinit = Trajectory() solinit.t = np.linspace(0, 1, 2) solinit.y = np.array([[-1, 0, -1], [-1, 0, 1]]) solinit.const = np.array([const]) sol = algo.solve(solinit)['sol'] e1 = np.cos(np.pi * sol.y[:, 2]) e2 = -np.pi * np.sin(np.pi * sol.y[:, 2]) assert all(e1 - sol.y[:, 0] < tol) assert all(e2 - sol.y[:, 1] < tol)
def test_t17(const): def odefun(y, _, k): return 0.2 * y[1], 0.2 * (-3 * k[0] * y[0] / (k[0] + y[2]**2)**2), 0.2 def odejac(y, _, k): df_dy = np.array([[0, 0.2, 0], [ -(3 * k[0]) / (5 * (y[2]**2 + k[0])**2), 0, (12 * k[0] * y[0] * y[2]) / (5 * (y[2]**2 + k[0])**3) ], [0, 0, 0]]) df_dp = np.empty((3, 0)) return df_dy, df_dp def bcfun(y0, yf, _, __, k): return y0[0] + 0.1 / np.sqrt(k[0] + 0.01), yf[0] - 0.1 / np.sqrt( k[0] + 0.01), y0[2] + 0.1 algo = SPBVP(odefun, None, bcfun) algo.set_derivative_jacobian(odejac) solinit = Trajectory() solinit.t = np.linspace(0, 1, 2) solinit.y = np.array([[0, 0, 0], [0, 0, 1]]) solinit.const = np.array([const]) sol = algo.solve(solinit)['sol'] e1 = sol.y[:, 2] / np.sqrt(sol.const[0] + sol.y[:, 2]**2) e2 = 1 / np.sqrt(sol.y[:, 2]**2 + sol.const[0]) - sol.y[:, 2]**2 / ( sol.y[:, 2]**2 + sol.const[0])**(3 / 2) assert all(e1 - sol.y[:, 0] < tol) assert all(e2 - sol.y[:, 1] < tol)
def test_trajectory(): t = np.array([0, 1, 2, 3]) y1 = t ** 2 gam = Trajectory(t, y1) y, q, u = gam(0.5) assert y == 0.5 assert len(q) == 0 assert len(u) == 0 y, q, u = gam(0.25) assert y == 0.25 assert len(q) == 0 assert len(u) == 0 gam.set_interpolate_function('cubic') y, q, u = gam(0.25) assert y - 0.0625 < 1e-4 assert len(q) == 0 assert len(u) == 0 t, y, q, u = gam[0] assert t == 0 assert y == 0 assert len(q) == 0 assert len(u) == 0
def test_t1(algorithm, const): def odefun(y, _, k): return y[1], y[0] / k[0] def odejac(_, __, k): df_dy = np.array([[0, 1], [1 / k[0], 0]]) df_dp = np.empty((2, 0)) return df_dy, df_dp def bcfun(y0, yf, _, __, ___): return y0[0] - 1, yf[0] algo = Shooting(odefun, None, bcfun, algorithm=algorithm) algo.set_derivative_jacobian(odejac) solinit = Trajectory() solinit.t = np.linspace(0, 1, 2) solinit.y = np.array([[0, 1], [0, 1]]) solinit.const = np.array([const]) sol = algo.solve(solinit)['sol'] e1 = (np.exp(-sol.t / np.sqrt(sol.const)) - np.exp( (sol.t - 2) / np.sqrt(sol.const))) / ( 1 - np.exp(-2.e0 / np.sqrt(sol.const))) e2 = (1. / (sol.const**(1 / 2) * np.exp(sol.t / sol.const**(1 / 2))) + np.exp( (sol.t - 2) / sol.const**(1 / 2)) / sol.const** (1 / 2)) / (1 / np.exp(2 / sol.const**(1 / 2)) - 1) assert all(e1 - sol.y[:, 0] < tol) assert all(e2 - sol.y[:, 1] < tol)
def map(self, sol: Solution, control_costate: Union[float, np.ndarray] = 0.) -> Solution: idx_u_list = [] for idx_u, (idx_y, u) in enumerate(sorted(zip(self.control_idxs, sol.u.T))): sol.y = np.insert(sol.y, idx_y, u, axis=1) if isinstance(control_costate, Iterable): if not isinstance(control_costate, np.ndarray): control_costate = np.array(control_costate) costate_insert = control_costate[idx_u] * np.ones_like(sol.t) else: costate_insert = control_costate * np.ones_like(sol.t) sol.dual = np.insert(sol.dual, -1, costate_insert, axis=1) if len(sol.dual_u) == 0: sol.dual_u = np.array([costate_insert]) else: sol.dual_u = np.insert(sol.dual_u, -1, costate_insert, axis=1) idx_u_list.append(idx_u) sol.u = np.delete(sol.u, idx_u_list, axis=1) return sol
def test_t16(const): def odefun(y, _, k): return 1 * y[1], 1 * (-y[0] * np.pi**2 / (4 * k[0])), 1 def odejac(_, __, k): df_dy = np.array([[0, 1, 0], [-np.pi**2 / (4 * k[0]), 0, 0], [0, 0, 0]]) df_dp = np.empty((3, 0)) return df_dy, df_dp def bcfun(y0, yf, _, __, k): return y0[0], yf[0] - np.sin(np.pi / (2 * np.sqrt(k[0]))), y0[2] algo = SPBVP(odefun, None, bcfun) algo.set_derivative_jacobian(odejac) solinit = Trajectory() solinit.t = np.linspace(0, 1, 2) solinit.y = np.array([[0, 0, 0], [0, 0, 1]]) solinit.const = np.array([const]) sol = algo.solve(solinit)['sol'] e1 = np.sin(np.pi * sol.y[:, 2] / (2 * np.sqrt(sol.const[0]))) e2 = (np.pi * np.cos( (np.pi * sol.y[:, 2]) / (2 * np.sqrt(sol.const[0])))) / (2 * np.sqrt(sol.const[0])) assert all(e1 - sol.y[:, 0] < tol) assert all(e2 - sol.y[:, 1] < tol)
def test_t10(const): def odefun(y, _, k): return 2 * y[1], 2 * (-y[2] * y[1] / k[0]), 2 def odejac(y, _, k): df_dy = np.array([[0, 2, 0], [0, 2 * (-y[2]) / k[0], 2 * (-y[1] / k[0])], [0, 0, 0]]) df_dp = np.empty((3, 0)) return df_dy, df_dp def bcfun(y0, yf, _, __, ___): return y0[0], yf[0] - 2, y0[2] + 1 algo = SPBVP(odefun, None, bcfun) algo.set_derivative_jacobian(odejac) solinit = Trajectory() solinit.t = np.linspace(0, 1, 2) solinit.y = np.array([[0, 0, -1], [2, 0, 1]]) solinit.const = np.array([const]) sol = algo.solve(solinit)['sol'] e1 = 1 + erf(sol.y[:, 2] / np.sqrt(2 * sol.const[0])) / erf( 1 / np.sqrt(2 * sol.const[0])) e2 = np.sqrt(2) / (np.sqrt(np.pi) * np.sqrt(sol.const[0]) * np.exp( sol.y[:, 2]**2 / (2 * sol.const[0])) * erf(np.sqrt(2) / (2 * np.sqrt(sol.const[0])))) assert all(e1 - sol.y[:, 0] < tol) assert all(e2 - sol.y[:, 1] < tol)
def test_t13(const): def odefun(y, _, k): return 2 * y[1], 2 * ((y[0] - k[0] * np.pi**2 * np.cos(np.pi * y[2]) - np.cos(np.pi * y[2])) / k[0]), 2 def odejac(y, _, k): df_dy = np.array([[0, 2, 0], [ 2 / k[0], 0, (2 * (np.pi * np.sin(np.pi * y[2]) + k[0] * np.pi**3 * np.sin(np.pi * y[2]))) / k[0] ], [0, 0, 0]]) df_dp = np.empty((3, 0)) return df_dy, df_dp def bcfun(y0, yf, _, __, ___): return y0[0], yf[0] + 1, y0[2] + 1 algo = SPBVP(odefun, None, bcfun) algo.set_derivative_jacobian(odejac) solinit = Trajectory() solinit.t = np.linspace(0, 1, 2) solinit.y = np.array([[-1, 0, -1], [0, 0, 1]]) solinit.const = np.array([const]) sol = algo.solve(solinit)['sol'] e1 = np.cos(np.pi * sol.y[:, 2]) + np.exp( -(1 + sol.y[:, 2]) / np.sqrt(sol.const[0])) e2 = -np.exp(-(sol.y[:, 2] + 1) / np.sqrt(sol.const[0])) / np.sqrt( sol.const[0]) - np.pi * np.sin(np.pi * sol.y[:, 2]) assert all(e1 - sol.y[:, 0] < tol) assert all(e2 - sol.y[:, 1] < tol)
def test_t8(const): def odefun(y, _, k): return y[1], (-y[1] / k[0]), 1 def odejac(_, __, k): df_dy = np.array([[0, 1, 0], [0, -1 / k[0], 0], [0, 0, 0]]) df_dp = np.empty((3, 0)) return df_dy, df_dp def bcfun(y0, yf, _, __, ___): return y0[0] - 1, yf[0] - 2, y0[2] algo = SPBVP(odefun, None, bcfun) algo.set_derivative_jacobian(odejac) solinit = Trajectory() solinit.t = np.linspace(0, 1, 2) solinit.y = np.array([[1, 0, -1], [2, 0, 1]]) solinit.const = np.array([const]) sol = algo.solve(solinit)['sol'] e1 = (2 - np.exp(-1 / sol.const[0]) - np.exp( -sol.y[:, 2] / sol.const[0])) / (1 - np.exp(-1 / sol.const[0])) e2 = -1 / (sol.const[0] * np.exp(sol.y[:, 2] / sol.const[0]) * (1 / np.exp(1 / sol.const[0]) - 1)) assert all(e1 - sol.y[:, 0] < tol) assert all(e2 - sol.y[:, 1] < tol)
def test_t9(const): def odefun(y, _, k): return 2 * y[1], 2 * (-(4 * y[2] * y[1] + 2 * y[0]) / (k[0] + y[2]**2)), 2 def odejac(y, _, k): df_dy = np.array( [[0, 2, 0], [ -4 / (y[2]**2 + k[0]), -(8 * y[2]) / (y[2]**2 + k[0]), (4 * y[2] * (2 * y[0] + 4 * y[1] * y[2])) / (y[2]**2 + k[0])**2 - (8 * y[1]) / (y[2]**2 + k[0]) ], [0, 0, 0]]) df_dp = np.empty((3, 0)) return df_dy, df_dp def bcfun(y0, yf, _, __, k): return y0[0] - 1 / (1 + k[0]), yf[0] - 1 / (1 + k[0]), y0[2] + 1 algo = SPBVP(odefun, None, bcfun) algo.set_derivative_jacobian(odejac) solinit = Trajectory() solinit.t = np.linspace(0, 1, 2) solinit.y = np.array([[1 / (1 + const), 0, -1], [1 / (1 + const), 1, 1]]) solinit.const = np.array([const]) sol = algo.solve(solinit)['sol'] e1 = 1 / (sol.const[0] + sol.y[:, 2]**2) e2 = -(2 * sol.y[:, 2]) / (sol.y[:, 2]**2 + sol.const[0])**2 assert all(e1 - sol.y[:, 0] < tol) assert all(e2 - sol.y[:, 1] < tol)
def inv_map(self, sol: Solution) -> Solution: sol.dual = sol.y[:, self.costate_idxs] sol.y = np.delete(sol.y, self.costate_idxs, axis=1) sol.nondynamical_parameters = np.delete(sol.nondynamical_parameters, self.constraint_adjoints_idxs) return sol
def test_t4(const): def odefun(y, _, k): return 2 * y[1], 2 * (((1 + k[0]) * y[0] - y[1]) / k[0]), 2 def odejac(_, __, k): df_dy = np.array([[0, 2, 0], [2 * (1 + k[0]) / k[0], 2 * (-1) / k[0], 0], [0, 0, 0]]) df_dp = np.empty((3, 0)) return df_dy, df_dp def bcfun(y0, yf, _, __, k): return y0[0] - 1 - np.exp(-2), yf[0] - 1 - np.exp( -2 * (1 + k[0]) / k[0]), y0[2] + 1 algo = SPBVP(odefun, None, bcfun) algo.set_derivative_jacobian(odejac) solinit = Trajectory() solinit.t = np.linspace(0, 1, 2) solinit.y = np.array([[-1, 0, -1], [-1, 0, 1]]) solinit.const = np.array([const]) sol = algo.solve(solinit)['sol'] e1 = np.exp(sol.y[:, 2] - 1) + np.exp(-((1 + sol.const[0]) * (1 + sol.y[:, 2]) / sol.const[0])) e2 = np.exp(sol.y[:, 2] - 1) - (sol.const[0] + 1) / (sol.const[0] * np.exp( (sol.y[:, 2] + 1) * (sol.const[0] + 1) / sol.const[0])) assert all(e1 - sol.y[:, 0] < tol) assert all(e2 - sol.y[:, 1] < tol)
def test_t33(const): def odefun(y, _, k): return y[1], (y[0] * y[3] - y[2] * y[1]) / k[0], y[3], y[4], y[5], ( -y[2] * y[5] - y[0] * y[1]) / k[0] def odejac(y, _, k): df_dy = np.array( [[0, 1, 0, 0, 0, 0], [y[3] / k[0], -y[2] / k[0], -y[1] / k[0], y[0] / k[0], 0, 0], [0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 1], [-y[1] / k[0], -y[0] / k[0], -y[5] / k[0], 0, 0, -y[2] / k[0]]]) df_dp = np.empty((6, 0)) return df_dy, df_dp def bcfun(y0, yf, _, __, ___): return y0[0] + 1, y0[2], y0[3], yf[0] - 1, yf[2], yf[3] algo = SPBVP(odefun, None, bcfun) algo.set_derivative_jacobian(odejac) sol = Trajectory() sol.t = np.linspace(0, 1, 2) sol.y = np.array([[-1, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0]]) sol.const = np.array([const]) sol = algo.solve(sol)['sol'] assert sol.converged
def test_t31(const): def odefun(y, _, k): return np.sin(y[1]), y[2], -y[3] / k[0], \ ((y[0]-1) * np.cos(y[1]) - y[2] / np.cos(y[1]) - k[0] * y[3] * np.tan(y[1])) / k[0] def odejac(y, _, k): df_dy = np.array([[0, np.cos(y[1]), 0, 0], [0, 0, 1, 0], [0, 0, 0, -1 / k[0]], [ np.cos(y[1]) / k[0], -(np.sin(y[1]) * (y[0] - 1) + k[0] * y[3] * (np.tan(y[1])**2 + 1) + (y[2] * np.sin(y[1])) / np.cos(y[1])**2) / k[0], -1 / (k[0] * np.cos(y[1])), -np.tan(y[1]) ]]) df_dp = np.empty((4, 0)) return df_dy, df_dp def bcfun(y0, yf, _, __, ___): return y0[0], y0[2], yf[0], yf[2] algo = SPBVP(odefun, None, bcfun) algo.set_derivative_jacobian(odejac) sol = Trajectory() sol.t = np.linspace(0, 1, 2) sol.y = np.array([[0, 0, 0, 0], [0, 0, 0, 0]]) sol.const = np.array([const]) sol = algo.solve(sol)['sol'] assert sol.converged
def test_t21(const): def odefun(y, _, k): return y[1], (y[0] * (1 + y[0]) - np.exp(-2 * y[2] / np.sqrt(k[0]))) / k[0], 1 def odejac(y, _, k): df_dy = np.array([[0, 1, 0], [(2 * y[0] + 1) / k[0], 0, (2 * np.exp(-(2 * y[2]) / np.sqrt(k[0]))) / k[0]**(3 / 2)], [0, 0, 0]]) df_dp = np.empty((3, 0)) return df_dy, df_dp def bcfun(y0, yf, _, __, k): return y0[0] - 1, yf[0] - np.exp(-1 / np.sqrt(k[0])), y0[2] algo = SPBVP(odefun, None, bcfun) algo.set_derivative_jacobian(odejac) solinit = Trajectory() solinit.t = np.linspace(0, 1, 2) solinit.y = np.array([[0, 0, 0], [0, 0, 1]]) solinit.const = np.array([const]) sol = algo.solve(solinit)['sol'] e1 = np.exp(-sol.y[:, 2] / np.sqrt(const)) e2 = -np.exp(-sol.y[:, 2] / np.sqrt(const)) / np.sqrt(const) assert all(e1 - sol.y[:, 0] < tol) assert all(e2 - sol.y[:, 1] < tol)
def map(self, sol: Solution) -> Solution: idx_u_list = [] for idx_u, (idx_y, u) in enumerate(sorted(zip(self.control_idxs, sol.u.T))): sol.y = np.insert(sol.y, idx_y, u, axis=1) idx_u_list.append(idx_u) sol.u = np.delete(sol.u, idx_u_list, axis=1) return sol
def inv_map(self, sol: Solution) -> Solution: if self.ind_state_idx is None: self.ind_state_idx = np.shape(sol.y)[1] - 1 sol.t = sol.y[:, self.ind_state_idx] sol.y = np.delete(sol.y, self.ind_state_idx, axis=1) sol.dual_t = sol.dual[:, self.ind_state_idx] sol.dual = np.delete(sol.dual, self.ind_state_idx, axis=1) return sol
def inv_map(self, sol: Solution) -> Solution: sol = copy.deepcopy(sol) if self.delta_ind_idx is None: self.delta_ind_idx = np.shape(sol.dynamical_parameters)[0] - 1 sol.t = sol.t * sol.dynamical_parameters[self.delta_ind_idx] sol.dynamical_parameters = np.delete(sol.dynamical_parameters, self.delta_ind_idx) return sol
def map(self, sol: Solution) -> Solution: if self.ind_state_idx is None: self.ind_state_idx = np.shape(sol.y)[1] if len(sol.dual_t) == 0: sol.dual_t = np.zeros_like(sol.t) sol.y = np.insert(sol.y, self.ind_state_idx, sol.t, axis=1) sol.dual = np.insert(sol.dual, self.ind_state_idx, sol.dual_t, axis=1) return sol
def inv_map(self, sol: Solution, retain_dual=True) -> Solution: if not retain_dual: sol.dual = empty_array sol.nondynamical_parameters = empty_array sol.cost = self.compute_cost(sol.t, sol.y, sol.q, sol.u, sol.dynamical_parameters, sol.const) return sol
def test_integrate_quads(): # Test a 1-dim x and 1-dim q t = np.linspace(0, 10, 100) y1 = np.sin(t) def quadfun(_, y): return y[0] gam = Trajectory(t, y1) assert integrate_quads(quadfun, np.array([0, 1 * np.pi]) + 0, gam) - 2 < 1e-3 assert integrate_quads(quadfun, np.array([0, 2 * np.pi]) + 0, gam) < 1e-3 assert integrate_quads(quadfun, np.array([0, 2 * np.pi]) + 1, gam) < 1e-3 assert integrate_quads(quadfun, np.array([0, 2 * np.pi]) + 2, gam) < 1e-3 assert integrate_quads(quadfun, np.array([0, 2 * np.pi]) + 3, gam) < 1e-3 # Test a 2-dim x and 1-dim q t = np.linspace(0, 10, 100) y1 = np.sin(t) y2 = np.cos(t) def quadfun(_, y): return y[0]*y[1] gam = Trajectory(t, np.vstack((y1, y2)).T) assert integrate_quads(quadfun, np.array([0, 1 * np.pi]) + 0, gam) - np.pi/2 < 1e-3 assert integrate_quads(quadfun, np.array([0, 2 * np.pi]) + 0, gam) - np.pi < 1e-3 # Test a 1-dim x and 2-dim q t = np.linspace(0, 10, 100) y1 = np.sin(t) def quadfun(_, y): return y[0], y[0]**2 gam = Trajectory(t, y1) assert integrate_quads(quadfun, np.array([0, 1 * np.pi]) + 0, gam)[0] - 2 < 1e-3 assert integrate_quads(quadfun, np.array([0, 1 * np.pi]) + 0, gam)[1] - np.pi/2 < 1e-3 # Test a 2-dim x and 2-dim q t = np.linspace(0, 10, 100) y1 = np.sin(t) y2 = np.cos(t) def quadfun(_, y): return y[0], y[1] gam = Trajectory(t, np.vstack((y1, y2)).T) assert len(integrate_quads(quadfun, np.array([0, 2 * np.pi]) + 2, gam)) == 2 assert integrate_quads(quadfun, np.array([0, 2 * np.pi]) + 2, gam)[0] < 1e-3 assert integrate_quads(quadfun, np.array([0, 2 * np.pi]) + 2, gam)[1] < 1e-3 assert integrate_quads(quadfun, np.array([0, 1 * np.pi]) + 0, gam)[0] - 2 < 1e-3 assert integrate_quads(quadfun, np.array([0, 1 * np.pi]) + 0, gam)[1] < 1e-3
def map(self, sol: Solution) -> Solution: sol = copy.deepcopy(sol) if self.delta_ind_idx is None: self.delta_ind_idx = np.shape(sol.dynamical_parameters)[0] delta_t = sol.t[-1] - sol.t[0] sol.dynamical_parameters = np.insert(sol.dynamical_parameters, self.delta_ind_idx, delta_t) sol.t = (sol.t - sol.t[0]) / delta_t return sol
def map(self, sol: Solution, lam=empty_array, nu=empty_array) -> Solution: # sol.dual = lam if len(sol.nondynamical_parameters) == 0: sol.nondynamical_parameters = np.ones(self.nu_len) return sol
def test_t7(const): def odefun(y, _, k): return 2 * y[1], 2 * ( (-y[2] * y[1] + y[0] - (1.0e0 + k[0] * np.pi**2) * np.cos(np.pi * y[2]) - np.pi * y[2] * np.sin(np.pi * y[2])) / k[0]), 2 def odejac(y, _, k): df_dy = np.array( [[0, 2, 0], [ 2 / k[0], -2 * y[2] / k[0], -(2 * (y[1] + np.pi * np.sin(np.pi * y[2]) + np.pi**2 * y[2] * np.cos(np.pi * y[2]) - np.pi * np.sin(np.pi * y[2]) * (k[0] * np.pi**2 + 1))) / k[0] ], [0, 0, 0]]) df_dp = np.empty((3, 0)) return df_dy, df_dp def bcfun(y0, yf, _, __, ___): return y0[0] + 1, yf[0] - 1, y0[2] + 1 algo = SPBVP(odefun, None, bcfun) algo.set_derivative_jacobian(odejac) solinit = Trajectory() solinit.t = np.linspace(0, 1, 2) solinit.y = np.array([[-1, 0, -1], [1, 0, 1]]) solinit.const = np.array([const]) sol = algo.solve(solinit)['sol'] e1 = np.cos(np.pi * sol.y[:, 2]) + sol.y[:, 2] + ( sol.y[:, 2] * erf(sol.y[:, 2] / np.sqrt(2.0e0 * sol.const[0])) + np.sqrt(2 * sol.const[0] / np.pi) * np.exp(-sol.y[:, 2]**2 / (2 * sol.const[0])) ) / (erf(1.0e0 / np.sqrt(2 * sol.const[0])) + np.sqrt(2.0e0 * sol.const[0] / np.pi) * np.exp(-1 / (2 * sol.const[0]))) e2 = erf((np.sqrt(2) * sol.y[:, 2]) / (2 * np.sqrt(sol.const[0]))) / ( erf(np.sqrt(2) / (2 * np.sqrt(sol.const[0]))) + (np.sqrt(2) * np.sqrt(sol.const[0])) / (np.sqrt(np.pi) * np.exp(1 / (2 * sol.const[0])))) - np.pi * np.sin( np.pi * sol.y[:, 2]) + 1 assert all(e1 - sol.y[:, 0] < tol) assert all(e2 - sol.y[:, 1] < tol)
def test_t23(algorithm, const): def odefun(y, _, k): return y[1], 1 / k[0] * np.sinh(y[0] / k[0]) def odejac(y, _, k): df_dy = np.array([[0, 1], [np.cosh(y[0] / k[0]) / k[0]**2, 0]]) df_dp = np.empty((2, 0)) return df_dy, df_dp def bcfun(y0, yf, _, __, ___): return y0[0], yf[0] - 1 algo = Shooting(odefun, None, bcfun, algorithm=algorithm) algo.set_derivative_jacobian(odejac) solinit = Trajectory() solinit.t = np.linspace(0, 1, 2) solinit.y = np.array([[0, 0], [1, 0]]) solinit.const = np.array([const]) sol = algo.solve(solinit)['sol'] assert sol.converged
def test_t15(const): def odefun(y, _, k): return 2 * y[1], 2 * (y[2] * y[0] / k[0]), 2 def odejac(y, _, k): df_dy = np.array([[0, 2, 0], [2 * (y[2] / k[0]), 0, 2 * (y[0] / k[0])], [0, 0, 0]]) df_dp = np.empty((3, 0)) return df_dy, df_dp def bcfun(y0, yf, _, __, ___): return y0[0] - 1, yf[0] - 1, y0[2] + 1 algo = SPBVP(odefun, None, bcfun) algo.set_derivative_jacobian(odejac) solinit = Trajectory() solinit.t = np.linspace(0, 1, 2) solinit.y = np.array([[1, 0, -1], [0, 0, 1]]) solinit.const = np.array([const]) sol = algo.solve(solinit)['sol'] assert sol.converged
def test_t29(algorithm, const): def odefun(y, _, k): return y[1], (y[0] - y[0] * y[1]) / k[0] def odejac(y, _, k): df_dy = np.array([[0, 1], [(1 - y[1]) / k[0], -y[0] / k[0]]]) df_dp = np.empty((2, 0)) return df_dy, df_dp def bcfun(y0, yf, _, __, ___): return y0[0], yf[0] - 3 / 2 algo = Shooting(odefun, None, bcfun, algorithm=algorithm, num_arcs=1) algo.set_derivative_jacobian(odejac) sol = Trajectory() sol.t = np.linspace(0, 1, 2) sol.y = np.array([[0, 0], [3 / 2, 0]]) sol.const = np.array([const]) sol = algo.solve(sol)['sol'] assert sol.converged
def test_t22(const): def odefun(y, _, k): return y[1], -(y[1] + y[0] * y[0]) / k[0] def odejac(y, _, k): df_dy = np.array([[0, 1], [-(2 * y[0]) / k[0], -1 / k[0]]]) df_dp = np.empty((2, 0)) return df_dy, df_dp def bcfun(y0, yf, _, __, ___): return y0[0], yf[0] - 1 / 2 algo = SPBVP(odefun, None, bcfun) algo.set_derivative_jacobian(odejac) solinit = Trajectory() solinit.t = np.linspace(0, 1, 2) solinit.y = np.array([[0, 0], [0, 0]]) solinit.const = np.array([const]) sol = algo.solve(solinit)['sol'] assert sol.converged