def __init__(self, num_moments): self.num_moments = num_moments num_eqns = 2 + self.num_moments amplitude = 0.1 wavenumber = 1.0 phase_shift = 0.1 offset = 1.0 wavespeed = np.array([1.0]) list_ = [ xt_functions.AdvectingSine(amplitude, wavenumber, offset, 0.0, wavespeed) ] for i_eqn in range(1, num_eqns): list_.append( xt_functions.AdvectingSine( amplitude, wavenumber, 0.0, phase_shift * i_eqn, wavespeed ) ) self.max_h = offset + amplitude self.min_h = offset - amplitude self.max_u = amplitude / self.min_h self.max_a = amplitude / self.min_h xt_functions.ComposedVector.__init__(self, list_)
def test_imex_linear_diffusion(): # advection with linear diffusion # (q_t + q_x = q_xx + s(x, t)) exact_solution = xt_functions.AdvectingSine(offset=2.0) problem = convection_diffusion.ConvectionDiffusion.manufactured_solution( exact_solution) t_initial = 0.0 bc = boundary.Periodic() error_dict = dict() cfl_list = [0.9, 0.3, 0.1] for num_basis_cpts in range(1, 4): imex = imex_runge_kutta.get_time_stepper(num_basis_cpts) cfl = cfl_list[num_basis_cpts - 1] # take 10 timesteps at coarsest time interval n = 20 t_final = cfl * (1.0 / n) / exact_solution.wavespeed exact_solution_final = lambda x: exact_solution(x, t_final) for basis_class in basis.BASIS_LIST: basis_ = basis_class(num_basis_cpts) error_list = [] for num_elems in [n, 2 * n]: mesh_ = mesh.Mesh1DUniform(0.0, 1.0, num_elems) delta_t = cfl * mesh_.delta_x / exact_solution.wavespeed dg_solution = basis_.project(problem.initial_condition, mesh_) # weak dg form with flux_function and source term explicit_operator = problem.get_explicit_operator(bc) # ldg discretization of diffusion_function implicit_operator = problem.get_implicit_operator( bc, bc, include_source=False) # this is a constant matrix case (matrix, vector) = problem.ldg_matrix(dg_solution, t_initial, bc, bc, include_source=False) solve_operator = time_stepping.get_solve_function_constant_matrix( matrix, vector) final_solution = time_stepping.time_step_loop_imex( dg_solution, t_initial, t_final, delta_t, imex, explicit_operator, implicit_operator, solve_operator, ) dg_error = math_utils.compute_dg_error(final_solution, exact_solution_final) error = dg_error.norm() error_list.append(error) # plot.plot_dg_1d(final_solution, function=exact_solution_final) # plot.plot_dg_1d(dg_error) error_dict[num_basis_cpts] = error_list order = utils.convergence_order(error_list) assert order >= num_basis_cpts
def test_exact_operator(): exact_solution = xt_functions.AdvectingSine() max_wavespeed = 1.0 problem = manufactured_solution_example.ManufacturedSolutionExample( exact_solution, max_wavespeed) # exact_operator should be zero for all space and time x = np.linspace(0, 1, 100) for t in np.linspace(0, 1, 10): Lq = problem.exact_operator(x, t) assert np.linalg.norm(Lq) == 0
def test_linearized_about_q(): original_flux_function = flux_functions.Polynomial(degree=3) q = xt_functions.AdvectingSine() xt_function = xt_functions.LinearizedAboutQ(original_flux_function, q) utils.check_to_from_dict(xt_function, xt_functions) x = 0.5 t = 0.1 assert xt_function(q(x, t), x, t) is not None assert xt_function(x, t) is not None assert xt_function(x, t) == xt_function(q(x, t), x, t) for x in range(10): for t in range(10): assert xt_function(x, t) == original_flux_function(q(x, t), x, t)
def test_dirichlet(): boundary_function = xt_functions.AdvectingSine() bc = boundary.Dirichlet(boundary_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) x = mesh_.get_face_position(boundary_faces[0]) assert flux == boundary_function(x, t)
def test_mms_operator_zero(): # For manufactured solution the overall operator should be zero exact_solution = xt_functions.AdvectingSine(offset=2.0) for diffusion_function in diffusion_functions: diffusion_class = convection_diffusion.ConvectionDiffusion problem = diffusion_class.manufactured_solution( exact_solution, diffusion_function) linearized_problem = diffusion_class.linearized_manufactured_solution( exact_solution, diffusion_function) assert isinstance(linearized_problem.diffusion_function, xt_functions.XTFunction) for t in range(3): exact_operator = problem.exact_operator(exact_solution, t) values = [exact_operator(x) for x in np.linspace(-1.0, 1.0)] assert np.linalg.norm(values) <= tolerance exact_operator = linearized_problem.exact_operator( exact_solution, t) values = [exact_operator(x) for x in np.linspace(-1.0, 1.0)] assert np.linalg.norm(values) <= tolerance
def test_advecting_function(): xt_function = xt_functions.AdvectingSine() utils.check_to_from_dict(xt_function, xt_functions) # should be able to call with just x and t x = 0.0 t = 0.0 q = 1.0 assert xt_function(x, t) is not None # should also be able to call with (q, x, t) assert xt_function(q, x, t) is not None assert xt_function(q, x, t) == xt_function(x, t) assert xt_function.q_derivative(q, x, t) is not None assert xt_function.x_derivative(q, x, t) is not None assert xt_function.x_derivative(x, t) is not None assert xt_function.x_derivative(x, t) == xt_function.x_derivative(q, x, t) assert xt_function.t_derivative(q, x, t) is not None assert xt_function.t_derivative(x, t) is not None assert xt_function.t_derivative(x, t) == xt_function.t_derivative(q, x, t) # should be traveling to the right at speed 1 for x in range(-10, 10): for t in range(-10, 10): assert xt_function(x, t) == xt_function(x - 1, t - 1)
super().__init__( app_, initial_condition, max_wavespeed, exact_solution, exact_operator, exact_time_derivative, ) if __name__ == "__main__": gravity_constant = 1.0 include_v = True q1 = xt_functions.AdvectingSine(0.1, 1.0, 1.0, 0.0, 1.0) q2 = xt_functions.AdvectingSine(0.1, 1.0, 0.0, 0.1, 1.0) q3 = xt_functions.AdvectingSine(0.1, 1.0, 0.0, 0.2, 1.0) list_ = [q1, q2] if include_v: list_.append(q3) exact_solution = xt_functions.ComposedVector(list_) max_wavespeed = 0.1 + np.sqrt(gravity_constant * 1.1) problem = ManufacturedSolutionExample( exact_solution, max_wavespeed, gravity_constant, include_v, )
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) problem = thin_film.ThinFilmDiffusion.manufactured_solution(exact_solution) bc = boundary.Periodic() final_error_list = [] error_dict_list = [] for i in range(num_doublings + 1): num_elems = n * np.power(2, i) mesh_ = mesh.Mesh1DUniform(x_left, x_right, num_elems) delta_t = cfl * mesh_.delta_x / exact_solution.wavespeed filename = ("thin_film_diffusion_convergence_test_" + str(num_basis_cpts) + "_" + str(num_elems) + ".yml") tuple_ = single_run(problem, basis_, mesh_, bc, t_final, delta_t, num_picard_iterations) dg_solution = tuple_[0]
from pydogpack.utils import xt_functions from pydogpack.utils import flux_functions from pydogpack.basis import basis from pydogpack.mesh import mesh from pydogpack.mesh import boundary from apps.onedimensional.convectiondiffusion import convection_diffusion import numpy as np import yaml if __name__ == "__main__": exact_solution = xt_functions.AdvectingSine(amplitude=0.1, offset=0.15) diffusion_function = flux_functions.Polynomial(degree=3) problem = convection_diffusion.NonlinearDiffusion.manufactured_solution( exact_solution, diffusion_function) t = 0.0 bc = boundary.Periodic() n = 10 dict_ = dict() for bc in [boundary.Periodic(), boundary.Extrapolation()]: dict_[str(bc)] = dict() dict_bc = dict_[str(bc)] for basis_class in basis.BASIS_LIST: dict_bc[basis_class.string] = dict() dict_basis = dict_bc[basis_class.string] for num_basis_cpts in range(1, 4): dict_basis[num_basis_cpts] = dict() dict_cpt = dict_basis[num_basis_cpts] basis_ = basis_class(num_basis_cpts) for num_elems in range(10, 90, 10): mesh_ = mesh.Mesh1DUniform(0.0, 1.0, num_elems)
def __init__(self, exact_solution, max_wavespeed): # exact_solution should be XTFunction - preferably smooth # max_wavespeed - maximum speed of exact_solution # Either both given or both None initial_condition = x_functions.FrozenT(exact_solution, 0) source_function = burgers.ExactOperator(exact_solution) app_ = burgers.Burgers(source_function) exact_operator = burgers.ExactOperator(exact_solution, source_function) exact_time_derivative = burgers.ExactTimeDerivative( exact_solution, source_function) super().__init__( app_, initial_condition, max_wavespeed, exact_solution, exact_operator, exact_time_derivative, ) if __name__ == "__main__": max_wavespeed = 1.0 exact_solution = xt_functions.AdvectingSine(1.0, 1.0, 2.0, 0.0, max_wavespeed) problem = ManufacturedSolutionExample(exact_solution, max_wavespeed) final_solution = main.run(problem)
def test_frozen_t(): xt_function = xt_functions.AdvectingSine() t_value = 0.0 x_function = x_functions.FrozenT(xt_function, t_value) check_x_function(x_function)
def test_imex_linearized_mms(): # advection with linearized diffusion # (q_t + q_x = (f(x, t) q_xx + s(x, t)) exact_solution = xt_functions.AdvectingSine(offset=0.15, amplitude=0.1) p_func = convection_diffusion.ConvectionDiffusion.linearized_manufactured_solution t_initial = 0.0 bc = boundary.Periodic() cfl_list = [0.9, 0.3, 0.1] for diffusion_function in diffusion_functions: problem = p_func(exact_solution, None, diffusion_function) for num_basis_cpts in range(1, 4): imex = imex_runge_kutta.get_time_stepper(num_basis_cpts) cfl = cfl_list[num_basis_cpts - 1] n = 20 t_final = cfl * (1.0 / n) / exact_solution.wavespeed exact_solution_final = lambda x: exact_solution(x, t_final) for basis_class in basis.BASIS_LIST: basis_ = basis_class(num_basis_cpts) error_list = [] for num_elems in [n, 2 * n]: mesh_ = mesh.Mesh1DUniform(0.0, 1.0, num_elems) delta_t = cfl * mesh_.delta_x dg_solution = basis_.project(problem.initial_condition, mesh_) # weak dg form with flux_function and source term explicit_operator = problem.get_explicit_operator(bc) # ldg discretization of diffusion_function implicit_operator = problem.get_implicit_operator( bc, bc, include_source=False) # this is a constant matrix case matrix_function = lambda t: problem.ldg_matrix( dg_solution, t, bc, bc, include_source=False) solve_operator = time_stepping.get_solve_function_matrix( matrix_function) final_solution = time_stepping.time_step_loop_imex( dg_solution, t_initial, t_final, delta_t, imex, explicit_operator, implicit_operator, solve_operator, ) error = math_utils.compute_error(final_solution, exact_solution_final) error_list.append(error) # plot.plot_dg_1d(final_solution, function=exact_solution_final) with open("convection_diffusion_linearized_mms_test.yml", "a") as file: dict_ = dict() subdict = dict() subdict["num_basis_cpts"] = num_basis_cpts subdict["cfl"] = cfl subdict["error_0"] = float(error_list[0]) subdict["error_1"] = float(error_list[1]) subdict["order"] = float( np.log2(error_list[0] / error_list[1])) dict_[num_basis_cpts] = subdict yaml.dump(dict_, file, default_flow_style=False) order = utils.convergence_order(error_list) assert order >= num_basis_cpts