def __init__(
        self,
        num_moments=swme.DEFAULT_NUM_MOMENTS,
        gravity_constant=swme.DEFAULT_GRAVITY_CONSTANT,
        kinematic_viscosity=swme.DEFAULT_KINEMATIC_VISCOSITY,
        slip_length=swme.DEFAULT_SLIP_LENGTH,
        primitive_left_states=None,
        primitive_right_states=None,
        discontinuity_location=0.0,
    ):
        self.num_moments = num_moments

        app_ = swme.ShallowWaterMomentEquations(
            num_moments, gravity_constant, kinematic_viscosity, slip_length
        )

        # primitive_states = [h, u, s, k, m]
        max_initial_height = max(
            [abs(primitive_left_states[0]), primitive_right_states[0]]
        )
        max_initial_velocity = max(
            [abs(primitive_left_states[1]), abs(primitive_right_states[1])]
        )
        if num_moments >= 1:
            max_initial_linear_coefficient = max(
                [abs(primitive_left_states[2]), abs(primitive_right_states[2])]
            )
        else:
            max_initial_linear_coefficient = 0

        # max_wavespeed = u + sqrt(g h^2 + s^2)
        max_wavespeed = max_initial_velocity + np.sqrt(
            gravity_constant * np.power(max_initial_height, 2)
            + max_initial_linear_coefficient
        )

        conserved_left_states = swme.get_conserved_variables(
            np.array(primitive_left_states)
        )
        conserved_right_states = swme.get_conserved_variables(
            np.array(primitive_right_states)
        )
        riemann_problems = []
        for i in range(num_moments + 2):
            riemann_problems.append(
                x_functions.RiemannProblem(
                    conserved_left_states[i],
                    conserved_right_states[i],
                    discontinuity_location,
                )
            )
        initial_condition = x_functions.ComposedVector(riemann_problems)

        super().__init__(app_, initial_condition, max_wavespeed, None)
Esempio n. 2
0
    def __init__(
        self,
        matrix,
        left_states=None,
        right_states=None,
        discontinuity_locations=None,
        source_function=None,
    ):
        self.matrix = matrix
        # 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_ = linear_system.LinearSystem(matrix, 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 = float(max(app_.flux_function.eigenvalues))
        exact_solution = linear_system.ExactSolution(initial_condition,
                                                     self.matrix,
                                                     app_.source_function)

        super().__init__(app_, initial_condition, max_wavespeed,
                         exact_solution)
    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. 4
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])
from apps.onedimensional.advection import advection
from pydogpack.utils import x_functions
from pydogpack import main
from pydogpack.visualize import plot


class SmoothSystemExample(problem.Problem):
    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
        )


if __name__ == "__main__":
    wavespeed = 1.0
    initial_condition = x_functions.ComposedVector(
        [x_functions.Sine(), x_functions.Cosine()]
    )
    problem = SmoothSystemExample(wavespeed, initial_condition)
    final_solution = main.run(problem)
Esempio n. 6
0
from apps.onedimensional.advection.smoothscalarexample import smooth_scalar_example as asse
from apps.onedimensional.burgers import burgers
from apps.onedimensional.linearsystem.smoothexample import smooth_example as lsse
from pydogpack.riemannsolvers import riemann_solvers
from pydogpack.utils import x_functions

import numpy as np

tolerance = 1e-12

advection_problem = asse.SmoothScalarExample(1.0, x_functions.Sine())
# burgers_problem = None
ic = x_functions.ComposedVector([x_functions.Sine(), x_functions.Cosine()])
linear_system_problem = lsse.SmoothExample(np.array([[1, 5], [5, 1]]), ic)
# shallow_water_problem = None
problem_list = [
    advection_problem,
    # burgers_problem,
    linear_system_problem,
    # shallow_water_problem,
]
default_q_list = [
    0.5,
    # 0.5,
    np.array([0.5, 0.5]),
    # np.array([0.5, 0.5]),
]


def sample_problems(riemann_solver_class, do_check_monotonicity=True):
    for i in range(len(problem_list)):