Ejemplo n.º 1
0
    def test_symplectics_against_ellipse_motion_with_numpy_PEFRL(
            self, monkeypatch):
        monkeypatch.setenv("IMPORT_TEST_NUMPY", "True", prepend=False)
        # After changing the import flag reload the modules.
        importlib.reload(elastica)
        importlib.reload(elastica.timestepper.symplectic_steppers)
        # importlib.reload(elastica.timestepper.integrate)
        importlib.reload(elastica.timestepper)
        from elastica.timestepper.symplectic_steppers import PEFRL
        from elastica.timestepper import integrate

        random_start_position = np.random.randn(3, 1)
        random_directors, _ = np.linalg.qr(np.random.randn(3, 3))
        random_directors = random_directors.reshape(3, 3, 1)

        rod_like_system = make_simple_system_with_positions_directors(
            random_start_position, random_directors)
        final_time = 1.0
        n_steps = 1000
        stepper = PEFRL()

        integrate(stepper,
                  rod_like_system,
                  final_time=final_time,
                  n_steps=n_steps)

        assert_allclose(
            rod_like_system.position_collection.reshape(3),
            rod_like_system.analytical_solution("Positions", final_time),
            rtol=Tolerance.rtol() * 1e1,
            atol=Tolerance.atol(),
        )

        assert_allclose(
            rod_like_system.velocity_collection.reshape(3),
            rod_like_system.analytical_solution("Velocity", final_time),
            rtol=Tolerance.rtol() * 1e1,
            atol=Tolerance.atol(),
        )

        # Reshaping done in the director collection to prevent numba from
        # complaining about returning multiple types
        assert_allclose(
            rod_like_system.director_collection.reshape(-1, 1),
            rod_like_system.analytical_solution("Directors", final_time),
            rtol=Tolerance.rtol() * 1e1,
            atol=Tolerance.atol(),
        )

        # Remove the import flag
        monkeypatch.delenv("IMPORT_TEST_NUMPY")
        # Reload the elastica after changing flag
        importlib.reload(elastica)
        importlib.reload(elastica.timestepper.symplectic_steppers)
        importlib.reload(elastica.timestepper)
        from elastica.timestepper.symplectic_steppers import PEFRL
        from elastica.timestepper import integrate
Ejemplo n.º 2
0
    def test_explicit_steppers(self, explicit_stepper):
        collective_system = ScalarExponentialDampedHarmonicOscillatorCollectiveSystem(
        )
        final_time = 1.0
        n_steps = 500
        stepper = explicit_stepper()

        dt = np.float64(float(final_time) / n_steps)
        time = np.float64(0.0)
        tol = Tolerance.atol()

        # Before stepping, let's extend the interface of the stepper
        # while providing memory slots
        from elastica.systems import make_memory_for_explicit_stepper

        memory_collection = make_memory_for_explicit_stepper(
            stepper, collective_system)
        from elastica.timestepper import extend_stepper_interface

        extend_stepper_interface(stepper, collective_system)

        while np.abs(final_time - time) > 1e5 * tol:
            time = stepper.do_step(collective_system, memory_collection, time,
                                   dt)

        for system in collective_system:
            assert_allclose(
                system.state,
                system.analytical_solution(final_time),
                rtol=Tolerance.rtol(),
                atol=Tolerance.atol(),
            )
Ejemplo n.º 3
0
    def test_symplectics_against_ellipse_motion(self, symplectic_stepper):
        from elastica.systems.analytical import (
            make_simple_system_with_positions_directors,
            SimpleSystemWithPositionsDirectors,
        )

        random_start_position = np.random.randn(3, 1)
        random_end_position = np.random.randn(3, 1)
        random_directors, _ = np.linalg.qr(np.random.randn(3, 3))
        random_directors = random_directors.reshape(3, 3, 1)

        rod_like_system = make_simple_system_with_positions_directors(
            random_start_position, random_end_position, random_directors)
        final_time = 1.0
        n_steps = 1000
        stepper = symplectic_stepper()

        integrate(stepper,
                  rod_like_system,
                  final_time=final_time,
                  n_steps=n_steps)

        assert_allclose(
            rod_like_system.position_collection,
            rod_like_system.analytical_solution("Positions", final_time),
            rtol=Tolerance.rtol() * 1e1,
            atol=Tolerance.atol(),
        )

        assert_allclose(
            rod_like_system.velocity_collection,
            rod_like_system.analytical_solution("Velocity", final_time),
            rtol=Tolerance.rtol() * 1e1,
            atol=Tolerance.atol(),
        )

        # Reshaping done in the director collection to prevent numba from
        # complaining about returning multiple types
        assert_allclose(
            rod_like_system.director_collection.reshape(-1, 1),
            rod_like_system.analytical_solution("Directors", final_time),
            rtol=Tolerance.rtol() * 1e1,
            atol=Tolerance.atol(),
        )
Ejemplo n.º 4
0
    def test_symplectic_against_undamped_harmonic_oscillator(self, stepper):
        system = SymplecticUndampedSimpleHarmonicOscillatorSystem()
        final_time = 4.0 * np.pi
        n_steps = 2000
        integrate(stepper(), system, final_time=final_time, n_steps=n_steps)

        # Symplectic systems conserve energy to a certain extent
        assert_allclose(
            *system.compute_energy(final_time),
            rtol=Tolerance.rtol() * 1e1,
            atol=Tolerance.atol(),
        )
Ejemplo n.º 5
0
    def test_against_damped_harmonic_oscillator(self, stepper):
        system = DampedSimpleHarmonicOscillatorSystem()
        final_time = 4.0 * np.pi
        n_steps = 2000
        integrate(stepper(), system, final_time=final_time, n_steps=n_steps)

        assert_allclose(
            system.state,
            system.analytical_solution(final_time),
            rtol=Tolerance.rtol(),
            atol=Tolerance.atol(),
        )
Ejemplo n.º 6
0
    def test_against_scalar_exponential(self, stepper):
        system = ScalarExponentialDecaySystem(-1, 1)
        final_time = 1
        n_steps = 1000
        integrate(stepper(), system, final_time=final_time, n_steps=n_steps)

        assert_allclose(
            system.state,
            system.analytical_solution(final_time),
            rtol=Tolerance.rtol() * 1e3,
            atol=Tolerance.atol(),
        )
Ejemplo n.º 7
0
    def test_symplectics_against_ellipse_motion(self, symplectic_stepper):
        from elastica.systems.analytical import SimpleSystemWithPositionsDirectors

        random_start_position = np.random.randn(3, 1)
        random_end_position = np.random.randn(3, 1)
        random_directors, _ = np.linalg.qr(np.random.randn(3, 3))
        random_directors = random_directors.reshape(3, 3, 1)

        rod_like_system = SimpleSystemWithPositionsDirectors(
            random_start_position, random_end_position, random_directors)
        final_time = 1.0
        n_steps = 1000
        stepper = symplectic_stepper()

        integrate(stepper,
                  rod_like_system,
                  final_time=final_time,
                  n_steps=n_steps)

        assert_allclose(
            rod_like_system.position_collection,
            rod_like_system.analytical_solution("Positions", final_time),
            rtol=Tolerance.rtol() * 1e1,
            atol=Tolerance.atol(),
        )

        assert_allclose(
            rod_like_system.velocity_collection,
            rod_like_system.analytical_solution("Velocity", final_time),
            rtol=Tolerance.rtol() * 1e1,
            atol=Tolerance.atol(),
        )

        assert_allclose(
            rod_like_system.director_collection,
            rod_like_system.analytical_solution("Directors", final_time),
            rtol=Tolerance.rtol() * 1e1,
            atol=Tolerance.atol(),
        )
Ejemplo n.º 8
0
    def test_hybrid_symplectic_against_analytical_system(
            self, symplectic_stepper):
        system = SecondOrderHybridSystem()
        final_time = 1.0
        n_steps = 2000
        stepper = SymplecticCosseratRodStepper(
            symplectic_stepper=symplectic_stepper())
        integrate(stepper, system, final_time=final_time, n_steps=n_steps)

        assert_allclose(
            system.final_solution(final_time),
            system.analytical_solution(final_time),
            rtol=Tolerance.rtol() * 1e2,
            atol=Tolerance.atol(),
        )
Ejemplo n.º 9
0
    def test_explicit_against_analytical_system(self, explicit_stepper):
        system = SecondOrderHybridSystem()
        final_time = 1.0
        n_steps = 2000
        integrate(explicit_stepper(),
                  system,
                  final_time=final_time,
                  n_steps=n_steps)

        assert_allclose(
            system.final_solution(final_time),
            system.analytical_solution(final_time),
            rtol=Tolerance.rtol() * 1e2,
            atol=Tolerance.atol(),
        )
Ejemplo n.º 10
0
    def test_explicit_steppers(self, explicit_stepper):
        collective_system = ScalarExponentialDampedHarmonicOscillatorCollectiveSystem(
        )
        final_time = 1.0
        if explicit_stepper == EulerForward:
            # Euler requires very small time-steps and in order not to slow down test,
            # we are scaling the difference between analytical and numerical solution.
            n_steps = 25000
            scale = 1e3
        else:
            n_steps = 500
            scale = 1

        stepper = explicit_stepper()

        dt = np.float64(float(final_time) / n_steps)
        time = np.float64(0.0)
        tol = Tolerance.atol()

        # Before stepping, let's extend the interface of the stepper
        # while providing memory slots
        from elastica._elastica_numpy._systems import make_memory_for_explicit_stepper

        memory_collection = make_memory_for_explicit_stepper(
            stepper, collective_system)
        from elastica._elastica_numpy._timestepper import extend_stepper_interface

        do_step, stagets_and_updates = extend_stepper_interface(
            stepper, collective_system)

        while np.abs(final_time - time) > 1e5 * tol:
            # time = stepper.do_step(collective_system, memory_collection, time, dt)
            time = do_step(
                stepper,
                stagets_and_updates,
                collective_system,
                memory_collection,
                time,
                dt,
            )

        for system in collective_system:
            assert_allclose(
                system.state,
                system.analytical_solution(final_time),
                rtol=Tolerance.rtol() * scale,
                atol=Tolerance.atol() * scale,
            )
Ejemplo n.º 11
0
    def test_symplectic_steppers(self, symplectic_stepper):
        collective_system = SymplecticUndampedHarmonicOscillatorCollectiveSystem(
        )
        final_time = 1.0
        n_steps = 2000
        stepper = symplectic_stepper()
        integrate(stepper,
                  collective_system,
                  final_time=final_time,
                  n_steps=n_steps)

        for system in collective_system:
            assert_allclose(
                *system.compute_energy(final_time),
                rtol=Tolerance.rtol() * 1e1,
                atol=Tolerance.atol(),
            )
Ejemplo n.º 12
0
    def test_explicit_against_ellipse_motion(self, explicit_stepper):
        from elastica.systems.analytical import SimpleSystemWithPositionsDirectors

        rod_like_system = SimpleSystemWithPositionsDirectors(
            np.array([0.0, 0.0, 0.0]), np.random.randn(3, 3, 1))
        final_time = 1.0
        n_steps = 500
        stepper = explicit_stepper()

        integrate(stepper,
                  rod_like_system,
                  final_time=final_time,
                  n_steps=n_steps)

        assert_allclose(
            rod_like_system.position_collection,
            rod_like_system.analytical_solution("Positions", final_time),
            rtol=Tolerance.rtol() * 1e1,
            atol=Tolerance.atol(),
        )