def test_ldg_polynomials_convergence():
    # LDG Diffusion should converge at 1st order for 1 basis_cpt
    # or at num_basis_cpts - 4 for more basis_cpts
    bc = boundary.Extrapolation()
    t = 0.0
    for i in range(4, 7):
        hyper_diffusion.initial_condition = x_functions.Polynomial(degree=i)
        exact_solution = hyper_diffusion.exact_time_derivative(
            hyper_diffusion.initial_condition, t)
        for num_basis_cpts in [1] + list(range(5, i + 1)):
            for basis_class in basis.BASIS_LIST:
                error_list = []
                basis_ = basis_class(num_basis_cpts)
                for num_elems in [10, 20]:
                    mesh_ = mesh.Mesh1DUniform(0.0, 1.0, num_elems)
                    dg_solution = basis_.project(
                        hyper_diffusion.initial_condition, mesh_)
                    L = hyper_diffusion.ldg_operator(dg_solution, t, bc, bc,
                                                     bc, bc)
                    dg_error = math_utils.compute_dg_error(L, exact_solution)
                    error = dg_error.norm(slice(2, -2))
                    error_list.append(error)
                    # plot.plot_dg_1d(L, function=exact_solution, elem_slice=slice(1, -1))
                order = utils.convergence_order(error_list)
                # if already at machine precision don't check convergence
                if error_list[-1] > tolerance:
                    if num_basis_cpts == 1:
                        assert order >= 1
                    else:
                        assert order >= num_basis_cpts - 4
Пример #2
0
def test_constant_operations():
    coeffs = np.ones(basis_.num_basis_cpts)
    dg_solution = basis_.project(x_functions.Polynomial(coeffs), mesh_)
    constant = 2.0

    # addition
    new_coeffs = coeffs.copy()
    new_coeffs[0] += constant
    projected_sol = basis_.project(x_functions.Polynomial(new_coeffs), mesh_)
    new_sol = dg_solution + constant
    error = (new_sol - projected_sol).norm()
    assert error <= tolerance
    new_sol = constant + dg_solution
    error = (new_sol - projected_sol).norm()
    assert error <= tolerance

    # subtraction
    new_coeffs = coeffs.copy()
    new_coeffs[0] -= constant
    projected_sol = basis_.project(x_functions.Polynomial(new_coeffs), mesh_)
    new_sol = dg_solution - constant
    error = (new_sol - projected_sol).norm()
    assert error <= tolerance
    new_coeffs = -1.0 * coeffs
    new_coeffs[0] += constant
    projected_sol = basis_.project(x_functions.Polynomial(new_coeffs), mesh_)
    new_sol = constant - dg_solution
    error = (new_sol - projected_sol).norm()
    assert error <= tolerance

    # multiplication
    new_coeffs = coeffs * constant
    projected_sol = basis_.project(x_functions.Polynomial(new_coeffs), mesh_)
    new_sol = dg_solution * constant
    error = (new_sol - projected_sol).norm()
    assert error <= tolerance
    new_sol = constant * dg_solution
    error = (new_sol - projected_sol).norm()
    assert error <= tolerance

    # division
    new_sol = dg_solution / constant
    new_coeffs = coeffs / constant
    projected_sol = basis_.project(x_functions.Polynomial(new_coeffs), mesh_)
    error = (new_sol - projected_sol).norm()
    assert error <= tolerance
    # right division not implemented

    # power - squaring
    deg = basis_.num_basis_cpts // 2
    dg_solution = basis_.project(x_functions.Polynomial(degree=deg), mesh_)
    new_sol = dg_solution ** 2
    new_deg = deg * 2
    projected_sol = basis_.project(x_functions.Polynomial(degree=new_deg), mesh_)
    error = (new_sol - projected_sol).norm()
    assert error <= tolerance
Пример #3
0
def test_diffusion_ldg_constant():
    # LDG of one should be zero
    diffusion.initial_condition = x_functions.Polynomial(degree=0)
    t = 0.0
    bc = boundary.Periodic()
    for num_basis_cpts in range(1, 5):
        for basis_class in basis.BASIS_LIST:
            basis_ = basis_class(num_basis_cpts)
            mesh_ = mesh.Mesh1DUniform(0.0, 1.0, 10)
            dg_solution = basis_.project(diffusion.initial_condition, mesh_)
            L = diffusion.ldg_operator(dg_solution, t, bc, bc)
            assert L.norm() <= tolerance
Пример #4
0
def test_diffusion_ldg_polynomials_zero():
    # LDG Diffusion of x should be zero in interior
    bc = boundary.Extrapolation()
    diffusion.initial_condition = x_functions.Polynomial(degree=1)
    t = 0.0
    for num_basis_cpts in range(1, 5):
        for basis_class in basis.BASIS_LIST:
            basis_ = basis_class(num_basis_cpts)
            mesh_ = mesh.Mesh1DUniform(0.0, 1.0, 10)
            dg_solution = basis_.project(diffusion.initial_condition, mesh_)
            L = diffusion.ldg_operator(dg_solution, t, bc, bc)
            error = np.linalg.norm(L.coeffs[1:-1, :])
            # plot.plot_dg_1d(L, elem_slice=slice(1, -1))
            assert error < tolerance
Пример #5
0
def test_ldg_operator_constant():
    # LDG of one should be zero
    thin_film_diffusion.initial_condition = x_functions.Polynomial(degree=0)
    t = 0.0
    for bc in [boundary.Periodic(), boundary.Extrapolation()]:
        for basis_class in basis.BASIS_LIST:
            for num_basis_cpts in range(1, 5):
                basis_ = basis_class(num_basis_cpts)
                mesh_ = mesh.Mesh1DUniform(0.0, 1.0, 10)
                dg_solution = basis_.project(
                    thin_film_diffusion.initial_condition, mesh_
                )
                L = thin_film_diffusion.ldg_operator(dg_solution, t, bc, bc, bc, bc)
                # plot.plot_dg_1d(L)
                assert L.norm() <= tolerance
Пример #6
0
def test_ldg_constant():
    # LDG of one should be zero
    t = 0.0
    for nonlinear_hyper_diffusion in test_problems:
        nonlinear_hyper_diffusion.initial_condition = x_functions.Polynomial(
            degree=0)
        for bc in [boundary.Periodic(), boundary.Extrapolation()]:
            for num_basis_cpts in range(1, 5):
                for basis_class in basis.BASIS_LIST:
                    basis_ = basis_class(num_basis_cpts)
                    mesh_ = mesh.Mesh1DUniform(0.0, 1.0, 10)
                    dg_solution = basis_.project(
                        nonlinear_hyper_diffusion.initial_condition, mesh_)
                    L = nonlinear_hyper_diffusion.ldg_operator(
                        dg_solution, t, bc, bc, bc, bc)
                    assert L.norm() <= tolerance
Пример #7
0
def check_constant_operations_1d(basis_):
    mesh_ = mesh.Mesh1DUniform(-1.0, 1.0, 10)
    coeffs = np.ones(basis_.space_order)
    dg_solution = basis_.project(x_functions.Polynomial(coeffs), mesh_)
    constant = 2.0

    # addition
    new_sol = basis_.do_constant_operation(dg_solution, constant,
                                           operator.iadd)
    new_coeffs = coeffs.copy()
    new_coeffs[0] += constant
    projected_sol = basis_.project(x_functions.Polynomial(new_coeffs), mesh_)
    error = np.linalg.norm(new_sol.coeffs - projected_sol.coeffs)
    assert error <= tolerance

    # subtraction
    new_sol = basis_.do_constant_operation(dg_solution, constant,
                                           operator.isub)
    new_coeffs = coeffs.copy()
    new_coeffs[0] -= constant
    projected_sol = basis_.project(x_functions.Polynomial(new_coeffs), mesh_)
    error = np.linalg.norm(new_sol.coeffs - projected_sol.coeffs)
    assert error <= tolerance

    # multiplication
    new_sol = basis_.do_constant_operation(dg_solution, constant,
                                           operator.imul)
    new_coeffs = coeffs * constant
    projected_sol = basis_.project(x_functions.Polynomial(new_coeffs), mesh_)
    error = np.linalg.norm(new_sol.coeffs - projected_sol.coeffs)
    assert error <= tolerance

    # division
    new_sol = basis_.do_constant_operation(dg_solution, constant,
                                           operator.itruediv)
    new_coeffs = coeffs / constant
    projected_sol = basis_.project(x_functions.Polynomial(new_coeffs), mesh_)
    error = np.linalg.norm(new_sol.coeffs - projected_sol.coeffs)
    assert error <= tolerance

    # power - squaring
    deg = basis_.num_basis_cpts // 2
    dg_solution = basis_.project(x_functions.Polynomial(degree=deg), mesh_)
    new_sol = basis_.do_constant_operation(dg_solution, 2.0, operator.ipow)

    new_deg = deg * 2
    projected_sol = basis_.project(x_functions.Polynomial(degree=new_deg),
                                   mesh_)
    error = np.linalg.norm(new_sol.coeffs - projected_sol.coeffs)
    assert error <= tolerance
def test_ldg_constant():
    # LDG discretization of 1 should be zero
    hyper_diffusion.initial_condition = x_functions.Polynomial(degree=0)
    t = 0.0
    for bc in [boundary.Periodic(), boundary.Extrapolation()]:
        for basis_class in basis.BASIS_LIST:
            for num_basis_cpts in range(1, 6):
                basis_ = basis_class(num_basis_cpts)
                mesh_ = mesh.Mesh1DUniform(0.0, 1.0, 10)
                dg_solution = basis_.project(hyper_diffusion.initial_condition,
                                             mesh_)
                L = hyper_diffusion.ldg_operator(dg_solution, t, bc, bc, bc,
                                                 bc)
                # plot.plot_dg_1d(L)
                error = L.norm()
                # quadrature_error can add up in higher basis_cpts
                assert error <= 1e-4
Пример #9
0
def test_constant_operations_inplace():
    coeffs = np.ones(basis_.num_basis_cpts)
    dg_solution = basis_.project(x_functions.Polynomial(coeffs), mesh_)
    constant = 2.0

    # addition
    new_coeffs = coeffs.copy()
    new_coeffs[0] += constant
    projected_sol = basis_.project(x_functions.Polynomial(new_coeffs), mesh_)
    new_sol = dg_solution.copy()
    new_sol += constant
    error = (new_sol - projected_sol).norm()
    assert error <= tolerance

    # subtraction
    new_coeffs = coeffs.copy()
    new_coeffs[0] -= constant
    projected_sol = basis_.project(x_functions.Polynomial(new_coeffs), mesh_)
    new_sol = dg_solution.copy()
    new_sol -= constant
    error = (new_sol - projected_sol).norm()
    assert error <= tolerance

    # multiplication
    new_coeffs = coeffs * constant
    projected_sol = basis_.project(x_functions.Polynomial(new_coeffs), mesh_)
    new_sol = dg_solution.copy()
    new_sol *= constant
    error = (new_sol - projected_sol).norm()
    assert error <= tolerance

    # division
    new_coeffs = coeffs / constant
    projected_sol = basis_.project(x_functions.Polynomial(new_coeffs), mesh_)
    new_sol = dg_solution.copy()
    new_sol /= constant
    error = (new_sol - projected_sol).norm()
    assert error <= tolerance

    # power - squaring
    deg = basis_.num_basis_cpts // 2
    dg_solution = basis_.project(x_functions.Polynomial(degree=deg), mesh_)
    new_deg = deg * 2
    projected_sol = basis_.project(x_functions.Polynomial(degree=new_deg), mesh_)
    new_sol = dg_solution.copy()
    new_sol **= 2
    error = (new_sol - projected_sol).norm()
    assert error <= tolerance
def test_ldg_polynomial_zero():
    # LDG of x, x^2, x^3 should be zero in interior
    bc = boundary.Extrapolation()
    for i in range(1, 4):
        hyper_diffusion.initial_condition = x_functions.Polynomial(degree=i)
        t = 0.0
        # for 1 < num_basis_cpts <= i not enough information to compute derivatives
        # get rounding errors
        for num_basis_cpts in [1] + list(range(i + 1, 5)):
            for basis_class in basis.BASIS_LIST:
                basis_ = basis_class(num_basis_cpts)
                mesh_ = mesh.Mesh1DUniform(0.0, 1.0, 10)
                dg_solution = basis_.project(hyper_diffusion.initial_condition,
                                             mesh_)
                L = hyper_diffusion.ldg_operator(dg_solution, t, bc, bc, bc,
                                                 bc)
                error = L.norm(slice(2, -2))
                # plot.plot_dg_1d(L, elem_slice=slice(2, -2))
                assert error <= tolerance
Пример #11
0
def test_ldg_operator_polynomial_zero():
    # LDG of x, x^2 should be zero in interior
    t = 0.0
    for n in range(1, 3):
        thin_film_diffusion.initial_condition = x_functions.Polynomial(degree=n)
        for bc in [boundary.Periodic(), boundary.Extrapolation()]:
            for basis_class in basis.BASIS_LIST:
                # for 1 < num_basis_cpts <= i not enough information
                # to compute derivatives get rounding errors
                for num_basis_cpts in [1] + list(range(n + 1, 5)):
                    basis_ = basis_class(num_basis_cpts)
                    mesh_ = mesh.Mesh1DUniform(0.0, 1.0, 10)
                    dg_solution = basis_.project(
                        thin_film_diffusion.initial_condition, mesh_
                    )
                    L = thin_film_diffusion.ldg_operator(dg_solution, t, bc, bc, bc, bc)
                    error = L.norm(slice(2, -2))
                    # plot.plot_dg_1d(L, elem_slice=slice(-2, 2))
                    assert error <= tolerance
Пример #12
0
def test_diffusion_ldg_polynomials_exact():
    # LDG Diffusion should be exactly second derivative of polynomials in the interior
    bc = boundary.Extrapolation()
    t = 0.0
    # x^i should be exact for i+1 or more basis_cpts
    for i in range(2, 5):
        diffusion.initial_condition = x_functions.Polynomial(degree=i)
        exact_solution = diffusion.exact_time_derivative(
            diffusion.initial_condition, t)
        for num_basis_cpts in range(i + 1, 6):
            for basis_class in basis.BASIS_LIST:
                basis_ = basis_class(num_basis_cpts)
                mesh_ = mesh.Mesh1DUniform(0.0, 1.0, 10)
                dg_solution = basis_.project(diffusion.initial_condition,
                                             mesh_)
                L = diffusion.ldg_operator(dg_solution, t, bc, bc)
                dg_error = math_utils.compute_dg_error(L, exact_solution)
                error = dg_error.norm(slice(1, -1))
                # plot.plot_dg_1d(dg_error, elem_slice=slice(1, -1))
                assert error < tolerance
def test_diffusion_ldg_polynomials_convergence():
    # LDG Diffusion should converge at 1st order for 1 basis_cpt
    # or at num_basis_cpts - 2 for more basis_cpts
    bc = boundary.Extrapolation()
    t = 0.0
    for nonlinear_diffusion in test_problems:
        d = nonlinear_diffusion.diffusion_function.degree
        # having problems at i >= d with convergence rate
        # still small error just not converging properly
        # exact solution is grows rapidly as x increases in this situation
        # error must larger at x = 1 then at x = 0
        # could also not be in asymptotic regime
        for i in range(1, d):
            nonlinear_diffusion.initial_condition = x_functions.Polynomial(
                degree=i)
            exact_solution = nonlinear_diffusion.exact_time_derivative(
                nonlinear_diffusion.initial_condition, t)
            for num_basis_cpts in [1] + list(range(3, i + 1)):
                for basis_class in basis.BASIS_LIST:
                    error_list = []
                    basis_ = basis_class(num_basis_cpts)
                    for num_elems in [30, 60]:
                        mesh_ = mesh.Mesh1DUniform(0.0, 1.0, num_elems)
                        dg_solution = basis_.project(
                            nonlinear_diffusion.initial_condition, mesh_)
                        L = nonlinear_diffusion.ldg_operator(
                            dg_solution, t, bc, bc)
                        dg_error = math_utils.compute_dg_error(
                            L, exact_solution)
                        error = dg_error.norm(slice(1, -1))
                        error_list.append(error)
                        # plot.plot_dg_1d(
                        #     L, function=exact_solution, elem_slice=slice(1, -1)
                        # )
                    order = utils.convergence_order(error_list)
                    # if already at machine precision don't check convergence
                    if error_list[-1] > tolerance:
                        if num_basis_cpts == 1:
                            assert order >= 1
                        else:
                            assert order >= num_basis_cpts - 2
Пример #14
0
def test_ldg_polynomials_zero():
    # LDG HyperDiffusion should be zero in the interior x, x^2
    bc = boundary.Extrapolation()
    t = 0.0
    # x^i should be exact for i+1 or more basis_cpts
    # needs lot more basis components if f(q, x, t) = q^2 or q^3
    for nonlinear_hyper_diffusion in test_problems:
        for i in range(1, 3):
            nonlinear_hyper_diffusion.initial_condition = x_functions.Polynomial(
                degree=i)
            for num_basis_cpts in range(i + 1, 6):
                for basis_class in basis.BASIS_LIST:
                    basis_ = basis_class(num_basis_cpts)
                    mesh_ = mesh.Mesh1DUniform(0.0, 1.0, 10)
                    dg_solution = basis_.project(
                        nonlinear_hyper_diffusion.initial_condition, mesh_)
                    L = nonlinear_hyper_diffusion.ldg_operator(
                        dg_solution, t, bc, bc, bc, bc)
                    error = L.norm(slice(2, -2))
                    # plot.plot_dg_1d(L, function=exact_solution, elem_slice=slice(1, -1))
                    assert error < tolerance
Пример #15
0
def test_ldg_polynomials_exact():
    # LDG HyperDiffusion should be exact for polynomials in the interior
    bc = boundary.Extrapolation()
    t = 0.0
    # x^i should be exact for i+1 or more basis_cpts
    # needs lot more basis components if f(q, x, t) = q^2
    for nonlinear_hyper_diffusion in [hyper_diffusion_identity]:
        for i in range(3, 5):
            nonlinear_hyper_diffusion.initial_condition = x_functions.Polynomial(
                degree=i)
            exact_solution = nonlinear_hyper_diffusion.exact_time_derivative(
                nonlinear_hyper_diffusion.initial_condition, t)
            for num_basis_cpts in range(i + 1, 6):
                for basis_class in basis.BASIS_LIST:
                    basis_ = basis_class(num_basis_cpts)
                    mesh_ = mesh.Mesh1DUniform(0.0, 1.0, 10)
                    dg_solution = basis_.project(
                        nonlinear_hyper_diffusion.initial_condition, mesh_)
                    L = nonlinear_hyper_diffusion.ldg_operator(
                        dg_solution, t, bc, bc, bc, bc)
                    dg_error = math_utils.compute_dg_error(L, exact_solution)
                    error = dg_error.norm(slice(2, -2))
                    # plot.plot_dg_1d(L, function=exact_solution, elem_slice=slice(1, -1))
                    assert error < tolerance
Пример #16
0
def test_ldg_polynomials_convergence():
    # LDG Diffusion should converge at 1st order for 1 basis_cpt
    # or at num_basis_cpts - 4 for more basis_cpts
    bc = boundary.Extrapolation()
    t = 0.0
    # having problems at i >= 3 with convergence rate
    # still small error just not converging properly
    for i in range(3, 5):
        thin_film_diffusion.initial_condition = x_functions.Polynomial(degree=i)
        thin_film_diffusion.initial_condition.set_coeff((1.0 / i), i)
        exact_solution = thin_film_diffusion.exact_time_derivative(
            thin_film_diffusion.initial_condition, t
        )
        for num_basis_cpts in [1] + list(range(5, 6)):
            for basis_class in basis.BASIS_LIST:
                error_list = []
                basis_ = basis_class(num_basis_cpts)
                for num_elems in [40, 80]:
                    mesh_ = mesh.Mesh1DUniform(0.0, 1.0, num_elems)
                    dg_solution = basis_.project(
                        thin_film_diffusion.initial_condition, mesh_
                    )
                    L = thin_film_diffusion.ldg_operator(dg_solution, t, bc, bc, bc, bc)
                    dg_error = math_utils.compute_dg_error(L, exact_solution)
                    error = dg_error.norm(slice(2, -2))
                    error_list.append(error)
                    # plot.plot_dg_1d(
                    #     L, function=exact_solution, elem_slice=slice(1, -1)
                    # )
                order = utils.convergence_order(error_list)
                # if already at machine precision don't check convergence
                if error_list[-1] > tolerance and error_list[0] > tolerance:
                    if num_basis_cpts == 1:
                        assert order >= 1
                    else:
                        assert order >= num_basis_cpts - 4
Пример #17
0
def test_polynomial():
    for i in range(4):
        x_function = x_functions.Polynomial(degree=i)
        check_x_function(x_function)