Пример #1
0
def test_Collocation_2():
    # Full 2PBVP test problem
    # This is calculating the 4th eigenvalue of Mathieu's Equation
    # This problem contains an adjustable parameter.

    def odefun(t, X, p, const):
        return (X[1], -(p[0] - 2 * 5 * np.cos(2 * t)) * X[0])

    def bcfun(t0, X0, q0, tf, Xf, qf, p, ndp, aux):
        return (X0[1], Xf[1], X0[0] - 1)

    algo = Collocation(odefun, None, bcfun)
    solinit = Solution()
    solinit.t = np.linspace(0, np.pi, 30)
    solinit.y = np.vstack(
        (np.cos(4 * solinit.t), -4 * np.sin(4 * solinit.t))).T
    solinit.dynamical_parameters = np.array([15])

    out = algo.solve(solinit)
    assert abs(out.t[-1] - np.pi) < tol
    assert abs(out.y[0][0] - 1) < tol
    assert abs(out.y[0][1]) < tol
    assert abs(out.y[-1][0] - 1) < tol
    assert abs(out.y[-1][1]) < tol
    assert abs(out.dynamical_parameters[0] - 17.09646175) < tol
Пример #2
0
 def guess_mapper(sol):
     n_c = len(constants_of_motion)
     if n_c == 0:
         return sol
     sol_out = Solution()
     sol_out.t = copy.copy(sol.t)
     sol_out.y = np.array([[fn(*sol.y[0]) for fn in states_2_states_fn]])
     sol_out.q = sol.q
     if len(quads) > 0:
         sol_out.q = -0.0 * np.array([np.ones((len(quads)))])
     sol_out.dynamical_parameters = sol.dynamical_parameters
     sol_out.dynamical_parameters[-n_c:] = np.array(
         [fn(*sol.y[0]) for fn in states_2_constants_fn])
     sol_out.nondynamical_parameters = sol.nondynamical_parameters
     sol_out.aux = sol.aux
     return sol_out
Пример #3
0
def test_Shooting_3():
    # This problem contains a parameter, but it is not explicit in the BCs.
    # Since time is buried in the ODEs, this tests if the BVP solver calculates
    # sensitivities with respect to parameters.
    def odefun(t, X, p, const):
        return 1 * p[0]

    def bcfun(t0, X0, q0, tf, Xf, qf, p, ndp, aux):
        return (X0[0] - 0, Xf[0] - 2)

    algo = Shooting(odefun, None, bcfun)
    solinit = Solution()
    solinit.t = np.linspace(0, 1, 2)
    solinit.y = np.array([[0], [0]])
    solinit.dynamical_parameters = np.array([1])
    out = algo.solve(solinit)
    assert abs(out.dynamical_parameters - 2) < tol
Пример #4
0
    def load(self):
        """
        Loads solution data using dill if not already loaded
        """
        if not self.is_loaded:
            logging.info("Loading datafile " + self.filename + "...")
            out = loadmat(self.filename)

            if 'output' in out:
                out = out['output']['result'][0][0][0][0]
            soldata = out['solution']['phase'][0][0][0][0]

            # if 'solution' not in self._data:
            #     self.is_loaded = False
            #     logging.error("Solution missing in data file :"+self.filename)
            #     raise RuntimeError("Solution missing in data file :"+self.filename)
            # if 'problem_data' not in self._data:
            #     self.is_loaded = False
            #     logging.error("Problem data missing in data file :"+self.filename)
            #     raise RuntimeError("Problem data missing in data file :"+self.filename)
            #
            _sol = Solution()

            tf = max(soldata['time'])
            _sol.x = soldata['time'][:, 0] / tf
            _sol.y = np.r_[soldata['state'].T, soldata['costate'].T,
                           np.ones_like(soldata['time']).T * tf]
            _sol.u = soldata['control'].T

            if 'tf' not in self.problem_data['state_list']:
                self.problem_data['state_list'] = tuple(
                    self.problem_data['state_list']) + ('tf', )

            _sol.arcs = ((0, len(_sol.x) - 1), )

            if self._const is not None:
                _sol.aux = {'const': self._const}

            self._sol = [[_sol]]
            logging.info('Loaded solution from data file')

            self.is_loaded = True
Пример #5
0
def test_Shooting_1():
    # Full 2PBVP test problem
    # This is the simplest BVP

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

    def bcfun(t0, X0, q0, tf, Xf, qf, p, ndp, aux):
        return (X0[0], Xf[0]+2)

    algo = Shooting(odefun, None, bcfun)
    solinit = Solution()
    solinit.t = np.linspace(0,4,2)
    solinit.y = np.array([[0,1],[0,1]])
    out = algo.solve(solinit)
    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
Пример #6
0
def test_Shooting_4():
    # This problem contains a quad and tests if the bvp solver correctly
    # integrates the quadfun.

    def odefun(t, x, p, const):
        return -x[1], x[0]

    def quadfun(t, x, p, const):
        return x[0]

    def bcfun(t0, X0, q0, tf, Xf, qf, params, ndp, aux):
        return X0[0], X0[1] - 1, qf[0] - 1.0

    algo = Shooting(odefun, quadfun, bcfun)
    solinit = Solution()
    solinit.t = np.linspace(0, np.pi / 2, 2)
    solinit.y = np.array([[1, 0], [1, 0]])
    solinit.q = np.array([[0], [0]])
    out = algo.solve(solinit)
    assert (out.y[0,0] - 0) < tol
    assert (out.y[0,1] - 1) < tol
    assert (out.q[0,0] - 2) < tol
    assert (out.q[-1,0] - 1) < tol