def test_legendre_limit_higher_moments_1d(): num_elems = 10 ic = x_functions.Sine() limiting_constants = np.random.random_sample(num_elems) legendre_basis = basis.LegendreBasis1D(3) mesh_ = mesh.Mesh1DUniform(0, 1, num_elems) dg_solution = legendre_basis.project(ic, mesh_) initial_total_integral = dg_solution.total_integral() limited_solution = legendre_basis.limit_higher_moments( dg_solution, limiting_constants) final_total_integral = limited_solution.total_integral() assert initial_total_integral == final_total_integral
def test_interior(): bc = boundary.Extrapolation() basis_ = basis.LegendreBasis1D(3) mesh_ = mesh.Mesh1DUniform(0.0, 1.0, 10) f = x_functions.Sine() dg_solution = basis_.project(f, mesh_) problem = smooth_scalar_example.SmoothScalarExample(1.0, f) riemann_solver = riemann_solvers.LocalLaxFriedrichs(problem) boundary_faces = mesh_.boundary_faces t = 0.0 flux = bc.evaluate_boundary(dg_solution, boundary_faces[0], riemann_solver, t) x = mesh_.get_face_position(boundary_faces[0]) assert flux == dg_solution(x)
def test_neumann(): boundary_derivative_function = flux_functions.Zero() bc = boundary.Neumann(boundary_derivative_function) basis_ = basis.LegendreBasis1D(3) mesh_ = mesh.Mesh1DUniform(0.0, 1.0, 10) f = x_functions.Sine() dg_solution = basis_.project(f, mesh_) problem = smooth_scalar_example.SmoothScalarExample(1.0, f) riemann_solver = riemann_solvers.LocalLaxFriedrichs(problem) boundary_faces = mesh_.boundary_faces t = 0.0 flux = bc.evaluate_boundary(dg_solution, boundary_faces[0], riemann_solver, t) assert flux is not None
def test_periodic(): bc = boundary.Periodic() basis_ = basis.LegendreBasis1D(3) mesh_ = mesh.Mesh1DUniform(0.0, 1.0, 10) f = x_functions.Sine() dg_solution = basis_.project(f, mesh_) problem = smooth_scalar_example.SmoothScalarExample(1.0, f) riemann_solver = riemann_solvers.LocalLaxFriedrichs(problem) boundary_faces = mesh_.boundary_faces t = 0.0 flux_0 = bc.evaluate_boundary(dg_solution, boundary_faces[0], riemann_solver, t) flux_1 = bc.evaluate_boundary(dg_solution, boundary_faces[1], riemann_solver, t) # fluxes should be the same assert flux_0 == flux_1
def from_dogpack_params(params): basis_type = params["basis_type"] num_dims = params["num_dims"] mesh_type = params["mesh_type"] space_order = params["space_order"] if basis_type == "space-legendre": if num_dims == 1: return basis.LegendreBasis1D(space_order) elif num_dims == 2: if mesh_type == "cartesian": return basis.LegendreBasis2DCartesian(space_order) elif mesh_type == "unstructured": return basis.ModalBasis2DTriangle(space_order) else: raise errors.InvalidParameter("mesh_type", mesh_type) else: raise errors.InvalidParameter("num_dims", num_dims) else: raise errors.InvalidParameter("basis_type", basis_type)
def test_ldg_matrix_irk(): diffusion = convection_diffusion.Diffusion.periodic_exact_solution() t_initial = 0.0 t_final = 0.1 bc = boundary.Periodic() basis_ = basis.LegendreBasis1D(1) exact_solution = lambda x: diffusion.exact_solution(x, t_final) for num_basis_cpts in range(1, 3): irk = implicit_runge_kutta.get_time_stepper(num_basis_cpts) for basis_class in basis.BASIS_LIST: basis_ = basis_class(num_basis_cpts) error_list = [] # constant matrix for i in [1, 2]: if i == 1: delta_t = 0.01 num_elems = 20 else: delta_t = 0.005 num_elems = 40 mesh_ = mesh.Mesh1DUniform(0.0, 1.0, num_elems) dg_solution = basis_.project(diffusion.initial_condition, mesh_) # constant matrix time doesn't matter tuple_ = diffusion.ldg_matrix(dg_solution, t_initial, bc, bc) matrix = tuple_[0] # vector = tuple_[1] rhs_function = diffusion.get_implicit_operator(bc, bc) solve_function = time_stepping.get_solve_function_constant_matrix( matrix) new_solution = time_stepping.time_step_loop_implicit( dg_solution, t_initial, t_final, delta_t, irk, rhs_function, solve_function, ) error = math_utils.compute_error(new_solution, exact_solution) error_list.append(error) order = utils.convergence_order(error_list) assert order >= num_basis_cpts
def test_bounds_limiter_sine(): bounds_limiter = shock_capturing_limiters.BoundsLimiter() basis_ = basis.LegendreBasis1D(3) mesh_ = mesh.Mesh1DUniform(0, 1, 10) func = x_functions.Sine(1.0, 1.0, 0.0) dg_solution = basis_.project(func, mesh_) boundary_condition = boundary.Periodic() app_ = advection.Advection() problem_ = problem.Problem(app_, dg_solution) problem_.boundary_condition = boundary_condition initial_solution = dg_solution.copy() limited_solution = bounds_limiter.limit_solution(problem, dg_solution) # limited solution should be same as initial solution as smooth assert (limited_solution - initial_solution).norm() == 0.0 func = x_functions.Sine(1.0, 10.0, 0.0) dg_solution = basis_.project(func, mesh_) initial_solution = dg_solution.copy() limited_solution = bounds_limiter.limit_solution(problem, dg_solution) # with higher wavenumber the limiter should chop off maxima assert limited_solution.norm() <= initial_solution.norm()
final_solution = time_stepping.time_step_loop_imex( dg_solution, 0.0, t_final, delta_t, imex, explicit_operator, implicit_operator, solve_operator, after_step_hook) exact_solution_final = x_functions.FrozenT(problem.exact_solution, t_final) error = math_utils.compute_error(final_solution, exact_solution_final) return (final_solution, error, error_dict) if __name__ == "__main__": num_basis_cpts = 3 print(num_basis_cpts) num_picard_iterations = 3 print(num_picard_iterations) basis_ = basis.LegendreBasis1D(num_basis_cpts) t_initial = 0.0 t_final = 0.1 cfl = 0.1 n = 80 num_doublings = 3 x_left = 0.0 x_right = 1.0 wavenumber = 1.0 print(wavenumber) exact_solution = xt_functions.AdvectingSine(amplitude=0.1, wavenumber=wavenumber, offset=0.15)
from pydogpack.mesh import mesh from pydogpack.basis import basis from pydogpack.utils import math_utils import numpy as np tolerance = 1e-14 basis_1d = basis.LegendreBasis1D(3) basis_2d_cartesian = basis.LegendreBasis2DCartesian(3) def test_mesh1d_uniform(): mesh1d_uniform = mesh.Mesh1DUniform(0.0, 1.0, 10) assert mesh1d_uniform.num_elems == 10 assert mesh1d_uniform.num_vertices == 11 for i in range(mesh1d_uniform.num_elems): assert mesh1d_uniform.elem_volumes[i] == 0.1 x = np.random.rand(10) for xi in x: elem_index = mesh1d_uniform.get_elem_index(xi) elem = mesh1d_uniform.elems[elem_index] vertex_1 = mesh1d_uniform.vertices[elem[0]] vertex_2 = mesh1d_uniform.vertices[elem[1]] assert xi >= np.min([vertex_1, vertex_2]) assert xi <= np.max([vertex_1, vertex_2]) def test_mesh1d_get_left_right_elems(): mesh1d_uniform = mesh.Mesh1DUniform(0.0, 1.0, 10) for elem_index in range(mesh1d_uniform.num_elems):
def test_legendre_operations_1d(): for num_basis_cpts in range(1, 10): legendre_basis = basis.LegendreBasis1D(num_basis_cpts) check_constant_operations_1d(legendre_basis) check_solution_operations_1d(legendre_basis, 0.5)
def test_legendre_basis_1d(): for num_basis_cpts in range(1, 10): legendre_basis = basis.LegendreBasis1D(num_basis_cpts) utils.check_to_from_dict(legendre_basis, basis_factory) check_matrices(legendre_basis)
from pydogpack.solution import solution from pydogpack.basis import basis from pydogpack.mesh import mesh from pydogpack.utils import functions from pydogpack.utils import x_functions import numpy as np basis_ = basis.LegendreBasis1D(4) mesh_ = mesh.Mesh1DUniform(0.0, 1.0, 40) dg_solution = solution.DGSolution(None, basis_, mesh_) tolerance = 1e-8 tolerance_2 = 0.1 def test_call(): assert dg_solution(0.0) == 0.0 def test_evaluate_canonical(): assert dg_solution.evaluate_canonical(1.0, 1) == 0.0 def test_evaluate_gradient(): assert dg_solution.evaluate_gradient(0.5) == 0.0 def test_evaluate_gradient_mesh(): assert dg_solution.evaluate_gradient_mesh(0.5) == 0.0