Exemplo n.º 1
0
def test_numeric_right_hand_side():

    m, k, c, g, F, x, v = np.random.random(7)

    args = {
        'constants': np.array([m, k, c, g]),
        'specified': np.array([F]),
        'num_coordinates': 1
    }

    states = np.array([x, v])

    mass_matrix, forcing_vector, constants, coordinates, speeds, specified = \
        generate_mass_spring_damper_equations_of_motion()

    expected_dx = np.array([v, 1.0 / m * (-c * v + m * g - k * x + F)])

    backends = ['lambdify', 'theano', 'cython']

    for backend in backends:
        rhs = numeric_right_hand_side(mass_matrix,
                                      forcing_vector,
                                      constants,
                                      coordinates,
                                      speeds,
                                      specified,
                                      generator=backend)
        dx = rhs(states, 0.0, args)

        testing.assert_allclose(dx, expected_dx)

    # Now try it without specified values.
    mass_matrix, forcing_vector, constants, coordinates, speeds, specified = \
        generate_mass_spring_damper_equations_of_motion(external_force=False)

    expected_dx = np.array([v, 1.0 / m * (-c * v + m * g - k * x)])

    # TODO : There is an import issue here, where the previous cython code
    # is loaded from the module and the import needs to be reloaded. Not
    # sure how to fix that.

    for backend in backends:
        rhs = numeric_right_hand_side(mass_matrix,
                                      forcing_vector,
                                      constants,
                                      coordinates,
                                      speeds,
                                      specified,
                                      generator=backend)
        dx = rhs(states, 0.0, args)

        testing.assert_allclose(dx, expected_dx)

    # clean up the cython crud
    files = glob.glob('multibody_system*')
    for f in files:
        os.remove(f)
    shutil.rmtree('build')
Exemplo n.º 2
0
    def test_generate_ode_function(self):

        m, k, c, g, F, x, v = np.random.random(7)

        args = {'constants': np.array([m, k, c, g]),
                'specified': np.array([F])}

        states = np.array([x, v])

        mass_matrix, forcing_vector, constants, coordinates, speeds, specified = \
            generate_mass_spring_damper_equations_of_motion()

        expected_dx = np.array([v, 1.0 / m * (-c * v + m * g - k * x + F)])

        backends = ['lambdify', 'theano', 'cython']

        for backend in backends:
            rhs = generate_ode_function(mass_matrix, forcing_vector,
                                        constants, coordinates, speeds,
                                        specified, generator=backend)
            dx = rhs(states, 0.0, args)

            testing.assert_allclose(dx, expected_dx)

        # Now try it with a function defining the specified quantities.
        args['specified'] = lambda x, t: np.sin(t)

        t = 14.345

        expected_dx = np.array([v, 1.0 / m * (-c * v + m * g - k * x +
                                              np.sin(t))])

        for backend in backends:
            rhs = generate_ode_function(mass_matrix, forcing_vector,
                                        constants, coordinates, speeds,
                                        specified, generator=backend)
            dx = rhs(states, t, args)

            testing.assert_allclose(dx, expected_dx)

        # Now try it without specified values.
        mass_matrix, forcing_vector, constants, coordinates, speeds, specified = \
            generate_mass_spring_damper_equations_of_motion(external_force=False)

        expected_dx = np.array([v, 1.0 / m * (-c * v + m * g - k * x)])

        for backend in backends:
            rhs = generate_ode_function(mass_matrix, forcing_vector,
                                        constants, coordinates, speeds,
                                        specified, generator=backend)
            dx = rhs(states, 0.0, args)

            testing.assert_allclose(dx, expected_dx)
Exemplo n.º 3
0
def test_numeric_right_hand_side():

    m, k, c, g, F, x, v = np.random.random(7)

    args = {'constants': np.array([m, k, c, g]),
            'specified': np.array([F]),
            'num_coordinates': 1}

    states = np.array([x, v])

    mass_matrix, forcing_vector, constants, coordinates, speeds, specified = \
        generate_mass_spring_damper_equations_of_motion()

    expected_dx = np.array([v, 1.0 / m * (-c * v + m * g - k * x + F)])

    backends = ['lambdify', 'theano', 'cython']

    for backend in backends:
        rhs = numeric_right_hand_side(mass_matrix, forcing_vector,
                                      constants, coordinates, speeds,
                                      specified, generator=backend)
        dx = rhs(states, 0.0, args)

        testing.assert_allclose(dx, expected_dx)

    # Now try it without specified values.
    mass_matrix, forcing_vector, constants, coordinates, speeds, specified = \
        generate_mass_spring_damper_equations_of_motion(external_force=False)

    expected_dx = np.array([v, 1.0 / m * (-c * v + m * g - k * x)])

    # TODO : There is an import issue here, where the previous cython code
    # is loaded from the module and the import needs to be reloaded. Not
    # sure how to fix that.

    for backend in backends:
        rhs = numeric_right_hand_side(mass_matrix, forcing_vector,
                                      constants, coordinates, speeds,
                                      specified, generator=backend)
        dx = rhs(states, 0.0, args)

        testing.assert_allclose(dx, expected_dx)

    # clean up the cython crud
    files = glob.glob('multibody_system*')
    for f in files:
        os.remove(f)
    shutil.rmtree('build')
Exemplo n.º 4
0
    def test_write_cython_code(self):

        results = generate_mass_spring_damper_equations_of_motion()

        generator = CythonGenerator(self.prefix, *results)
        generator._write_cython_code()

        file_dir = os.path.split(__file__)[0]

        expected_files = [
            'desired_mass_forcing_c.c', 'desired_mass_forcing_c.h',
            'desired_mass_forcing.pyx', 'desired_mass_forcing_setup.py'
        ]

        endings = ['_c.c', '_c.h', '.pyx', '_setup.py']

        for ending, expected_file in zip(endings, expected_files):
            created = self.prefix + ending
            expected = os.path.join(file_dir, 'expected_cython', expected_file)
            assert filecmp.cmp(created, expected)
Exemplo n.º 5
0
    def test_write_cython_code(self):

        results = generate_mass_spring_damper_equations_of_motion()

        generator = CythonGenerator(self.prefix, *results)
        generator._write_cython_code()

        file_dir = os.path.split(__file__)[0]

        expected_files = ['desired_mass_forcing_c.c',
                          'desired_mass_forcing_c.h',
                          'desired_mass_forcing.pyx',
                          'desired_mass_forcing_setup.py']

        endings = ['_c.c', '_c.h', '.pyx', '_setup.py']

        for ending, expected_file in zip(endings, expected_files):
            created = self.prefix + ending
            expected = os.path.join(file_dir, 'expected_cython',
                                    expected_file)
            assert filecmp.cmp(created, expected)
Exemplo n.º 6
0
    def test_generate_ode_function(self):

        m, k, c, g, F, x, v = np.random.random(7)

        args = {
            'constants': np.array([m, k, c, g]),
            'specified': np.array([F])
        }

        states = np.array([x, v])

        mass_matrix, forcing_vector, constants, coordinates, speeds, specified = \
            generate_mass_spring_damper_equations_of_motion()

        expected_dx = np.array([v, 1.0 / m * (-c * v + m * g - k * x + F)])

        backends = ['lambdify', 'theano', 'cython']

        for backend in backends:
            rhs = generate_ode_function(mass_matrix,
                                        forcing_vector,
                                        constants,
                                        coordinates,
                                        speeds,
                                        specified,
                                        generator=backend)
            dx = rhs(states, 0.0, args)

            testing.assert_allclose(dx, expected_dx)

        # Now try it with a function defining the specified quantities.
        args['specified'] = lambda x, t: np.sin(t)

        t = 14.345

        expected_dx = np.array(
            [v, 1.0 / m * (-c * v + m * g - k * x + np.sin(t))])

        for backend in backends:
            rhs = generate_ode_function(mass_matrix,
                                        forcing_vector,
                                        constants,
                                        coordinates,
                                        speeds,
                                        specified,
                                        generator=backend)
            dx = rhs(states, t, args)

            testing.assert_allclose(dx, expected_dx)

        # Now try it without specified values.
        mass_matrix, forcing_vector, constants, coordinates, speeds, specified = \
            generate_mass_spring_damper_equations_of_motion(external_force=False)

        expected_dx = np.array([v, 1.0 / m * (-c * v + m * g - k * x)])

        for backend in backends:
            rhs = generate_ode_function(mass_matrix,
                                        forcing_vector,
                                        constants,
                                        coordinates,
                                        speeds,
                                        specified,
                                        generator=backend)
            dx = rhs(states, 0.0, args)

            testing.assert_allclose(dx, expected_dx)