def __init__(self, wavespeed=1.0, initial_condition=None, source_function=None):
        self.wavespeed = wavespeed
        if initial_condition is None:
            self.initial_condition = x_functions.Sine()
        else:
            self.initial_condition = initial_condition

        app = advection.Advection(wavespeed, source_function)
        max_wavespeed = wavespeed
        exact_solution = advection.ExactSolution(initial_condition, wavespeed)

        super().__init__(
            app, initial_condition, max_wavespeed, exact_solution
        )
Esempio n. 2
0
def test_advection_finite_time():
    def initial_condition(x):
        return np.sin(2.0 * np.pi * x)

    advection_ = advection.Advection(initial_condition=initial_condition)
    riemann_solver = riemann_solvers.LocalLaxFriedrichs(
        advection_.flux_function, advection_.wavespeed_function
    )
    boundary_condition = boundary.Periodic()
    time_initial = 0.0
    time_final = 0.5

    def test_function(dg_solution):
        explicit_time_stepper = explicit_runge_kutta.get_time_stepper(
            dg_solution.basis.num_basis_cpts
        )

        cfl = dg_utils.standard_cfls(dg_solution.basis.num_basis_cpts)
        delta_t = dg_utils.get_delta_t(
            cfl, advection_.wavespeed, dg_solution.mesh.delta_x
        )

        rhs_function = lambda time, q: dg_utils.dg_weak_formulation(
            q, advection_.flux_function, riemann_solver, boundary_condition
        )

        return time_stepping.time_step_loop_explicit(
            dg_solution,
            time_initial,
            time_final,
            delta_t,
            explicit_time_stepper,
            rhs_function,
        )

    order_check_function = lambda order, num_basis_cpts: order >= num_basis_cpts

    utils.basis_convergence(
        test_function,
        initial_condition,
        lambda x: advection_.exact_solution(x, time_final),
        order_check_function,
        basis_list=[basis.LegendreBasis1D],
        basis_cpt_list=[4],
    )
Esempio n. 3
0
def test_advection_one_time_step():
    def initial_condition(x):
        return np.sin(2.0 * np.pi * x)

    advection_ = advection.Advection(initial_condition=initial_condition)
    riemann_solver = riemann_solvers.LocalLaxFriedrichs(
        advection_.flux_function, advection_.wavespeed_function
    )
    explicit_time_stepper = explicit_runge_kutta.ForwardEuler()
    boundary_condition = boundary.Periodic()
    cfl = 1.0
    for basis_class in basis.BASIS_LIST:
        basis_ = basis_class(1)
        error_list = []
        for num_elems in [20, 40]:
            mesh_ = mesh.Mesh1DUniform(0.0, 1.0, num_elems)
            dg_solution = basis_.project(advection_.initial_condition, mesh_)

            delta_t = dg_utils.get_delta_t(cfl, advection_.wavespeed, mesh_.delta_x)
            time_initial = 0.0
            time_final = delta_t

            rhs_function = lambda time, q: dg_utils.dg_weak_formulation(
                q, advection_.flux_function, riemann_solver, boundary_condition
            )
            final_solution = time_stepping.time_step_loop_explicit(
                dg_solution,
                time_initial,
                time_final,
                delta_t,
                explicit_time_stepper,
                rhs_function,
            )
            error = math_utils.compute_error(
                final_solution, lambda x: advection_.exact_solution(x, time_final)
            )
            error_list.append(error)
        order = utils.convergence_order(error_list)
        assert order >= 1
    def __init__(
        self,
        wavespeed=1.0,
        left_states=None,
        right_states=None,
        discontinuity_locations=None,
        source_function=None,
    ):
        self.wavespeed = wavespeed
        # left_state, right_state, and discontinuity_location should be arrays or lists
        if left_states is None:
            self.left_states = [1.0, 2.0]
        else:
            self.left_states = left_states

        if right_states is None:
            self.right_states = [-1.0, 1.0]
        else:
            self.right_states = right_states

        if discontinuity_locations is None:
            self.discontinuity_locations = [-0.6, -0.4]
        else:
            self.discontinuity_locations = discontinuity_locations

        app_ = advection.Advection(wavespeed, source_function)
        riemann_problems = []
        for i in range(len(left_states)):
            riemann_problems.append(x_functions.RiemannProblem(
                left_states[i], right_states[i], discontinuity_locations[i]
            ))

        initial_condition = x_functions.ComposedVector(riemann_problems)
        max_wavespeed = wavespeed
        exact_solution = advection.ExactSolution(initial_condition, self.wavespeed)

        super().__init__(
            app_, initial_condition, max_wavespeed, exact_solution
        )
Esempio n. 5
0
    def __init__(
        self,
        wavespeed=1.0,
        left_state=1.0,
        right_state=-1.0,
        discontinuity_location=0.0,
        source_function=None,
    ):
        self.wavespeed = wavespeed
        self.left_state = left_state
        self.right_state = right_state
        self.discontinuity_location = discontinuity_location

        app_ = advection.Advection(wavespeed, source_function)
        initial_condition = x_functions.RiemannProblem(left_state, right_state,
                                                       discontinuity_location)
        max_wavespeed = wavespeed
        exact_solution = advection.ExactSolution(initial_condition,
                                                 self.wavespeed)

        super().__init__(app_, initial_condition, max_wavespeed,
                         exact_solution)
Esempio n. 6
0
def test_advection_operator():
    # test that dg_operator acting on projected initial condition converges to
    # exact time derivative
    # will lose one order of accuracy

    for i in range(2):
        if i == 0:
            sin = x_functions.Sine()
            cos = x_functions.Cosine()
            initial_condition = x_functions.ComposedVector([sin, cos])
        else:
            initial_condition = x_functions.Sine()
        wavespeed = 1.0
        exact_solution = advection.ExactSolution(initial_condition, wavespeed)
        exact_time_derivative = advection.ExactTimeDerivative(exact_solution, wavespeed)
        initial_time_derivative = x_functions.FrozenT(exact_time_derivative, 0.0)

        app_ = advection.Advection(wavespeed)
        riemann_solver = riemann_solvers.LocalLaxFriedrichs(app_.flux_function)
        boundary_condition = boundary.Periodic()

        for basis_class in basis.BASIS_LIST:
            for num_basis_cpts in range(1, 5):
                basis_ = basis_class(num_basis_cpts)
                error_list = []
                for num_elems in [20, 40]:
                    mesh_ = mesh.Mesh1DUniform(0.0, 1.0, num_elems)
                    dg_sol = basis_.project(initial_condition, mesh_)
                    dg_operator = app_.get_explicit_operator(
                        riemann_solver, boundary_condition
                    )
                    F = dg_operator(0.0, dg_sol)
                    error = math_utils.compute_error(F, initial_time_derivative)
                    error_list.append(error)

                order = utils.convergence_order(error_list)
                assert order >= max([1.0, num_basis_cpts - 1])
    def __init__(self,
                 wavespeed=1.0,
                 initial_condition=None,
                 source_function=None):
        self.wavespeed = wavespeed
        if initial_condition is None:
            initial_condition = x_functions.Sine()

        app = advection.Advection(wavespeed, source_function)
        max_wavespeed = wavespeed
        exact_solution = advection.ExactSolution(initial_condition, wavespeed)
        exact_operator = advection.ExactOperator(exact_solution, wavespeed,
                                                 source_function)
        exact_time_derivative = advection.ExactTimeDerivative(
            exact_solution, wavespeed, source_function)

        super().__init__(
            app,
            initial_condition,
            max_wavespeed,
            exact_solution,
            exact_operator,
            exact_time_derivative,
        )