示例#1
0
def test_spbvp_1():
    # Full 2PBVP test problem
    # This is the simplest BVP

    def odefun(X, u, p, const):
        return X[1], -abs(X[0])

    def bcfun(X0, q0, u0, Xf, qf, uf, p, ndp, const):
        return X0[0], Xf[0] + 2

    algo = spbvp(odefun, None, bcfun)
    solinit = Trajectory()
    solinit.t = np.linspace(0, 4, 4)
    solinit.y = np.array([[0, 1], [0, 1], [0, 1], [0, 1]])
    solinit.const = np.array([])
    out = algo.solve(solinit)['sol']
    assert out.y[0][0] < tol
    assert out.y[0][1] - 2.06641646 < tol
    assert out.y[-1][0] + 2 < tol
    assert out.y[-1][1] + 2.87588998 < tol
    assert out.t[-1] - 4 < tol
    assert abs(out.y[0][1] - solinit.y[0][1]) > tol
    assert abs(out.y[-1][0] - solinit.y[-1][0]) - 2 < tol
示例#2
0
def test_T14(const):
    def odefun(X, u, p, const):
        return 2 * X[1],\
               2 * ((X[0] - const[0] * np.pi ** 2 * np.cos(np.pi * X[2]) - np.cos(np.pi * X[2])) / const[0]),\
               2

    def odejac(X, u, p, const):
        df_dy = np.array(
            [[0, 2, 0],
             [
                 2 / const[0], 0,
                 (2 * (np.pi * np.sin(np.pi * X[2]) +
                       const[0] * np.pi**3 * np.sin(np.pi * X[2]))) / const[0]
             ], [0, 0, 0]])
        df_dp = np.empty((3, 0))
        return df_dy, df_dp

    def bcfun(X0, q0, u0, Xf, qf, uf, p, ndp, const):
        return X0[0], Xf[0], X0[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], [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])) + 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]) - 1 / (np.sqrt(sol.const[0]) * np.exp(
                (sol.y[:, 2] + 1) / np.sqrt(sol.const[0])))
    assert all(e1 - sol.y[:, 0] < tol)
    assert all(e2 - sol.y[:, 1] < tol)
示例#3
0
def test_T18(const):
    def odefun(X, u, p, const):
        return X[1], (-X[1] / const[0]), 1

    def odejac(X, u, p, const):
        df_dy = np.array([[0, 1, 0], [0, -1 / const[0], 0], [0, 0, 0]])
        df_dp = np.empty((3, 0))
        return df_dy, df_dp

    def bcfun(X0, q0, u0, Xf, qf, uf, p, ndp, const):
        return X0[0] - 1, Xf[0] - np.exp(-1 / const[0]), X0[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] / sol.const[0])
    e2 = -1 / (sol.const[0] * np.exp(sol.y[:, 2] / sol.const[0]))
    assert all(e1 - sol.y[:, 0] < tol)
    assert all(e2 - sol.y[:, 1] < tol)
示例#4
0
def test_R8(const):
    def odefun(X, u, p, const):
        return -X[0] / const[0]

    def quadfun(X, u, p, const):
        return X[0]

    def bcfun(X0, q0, u0, Xf, qf, uf, p, ndp, const):
        return q0[0] - 1, qf[0] - 2

    algo = spbvp(odefun, quadfun, bcfun)
    solinit = Trajectory()
    solinit.t = np.linspace(0, 1, 2)
    solinit.y = np.array([[1], [1]])
    solinit.q = np.array([[0], [0]])
    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.q[:, 0] < tol)
    assert all(e2 - sol.y[:, 0] < tol)