def test_Li_basis_value(): ''' This test compares the output of the lagrange basis value calculated by the function ``Li_basis_value`` to the analytical value of the lagrange polynomials created using the same LGL points. The analytical values were calculated in this sage worksheet_ .. _worksheet: https://goo.gl/ADyA3U ''' threshold = 1e-11 Li_value_ref = af.np_to_af_array(utils.csv_to_numpy( 'dg_maxwell/tests/lagrange/files/Li_value.csv')) N_LGL = 8 xi_LGL = lagrange.LGL_points(N_LGL) L_basis_poly1d, L_basis_af = lagrange.lagrange_polynomials(xi_LGL) L_basis_af = af.np_to_af_array(L_basis_af) Li_indexes = af.np_to_af_array(np.arange(3, dtype = np.int32)) xi = af.np_to_af_array(np.linspace(-1., 1, 10)) Li_value = lagrange.Li_basis_value(L_basis_af, Li_indexes, xi) assert af.all_true(af.abs(Li_value - Li_value_ref) < threshold)
def test_integrate_1d(): ''' Tests the ``integrate_1d`` by comparing the integral agains the analytically calculated integral. The polynomials to be integrated are all the Lagrange polynomials obtained for the LGL points. The analytical integral is calculated in this `sage worksheet`_ .. _sage worksheet: https://goo.gl/1uYyNJ ''' threshold = 1e-12 N_LGL = 8 xi_LGL = lagrange.LGL_points(N_LGL) eta_LGL = lagrange.LGL_points(N_LGL) _, Li_xi = lagrange.lagrange_polynomials(xi_LGL) _, Lj_eta = lagrange.lagrange_polynomials(eta_LGL) Li_xi = af.np_to_af_array(Li_xi) Lp_xi = Li_xi.copy() Li_Lp = utils.poly1d_product(Li_xi, Lp_xi) test_integral_gauss = utils.integrate_1d(Li_Lp, order=9, scheme='gauss') test_integral_lobatto = utils.integrate_1d(Li_Lp, order=N_LGL + 1, scheme='lobatto') ref_integral = af.np_to_af_array( np.array([ 0.0333333333333, 0.196657278667, 0.318381179651, 0.384961541681, 0.384961541681, 0.318381179651, 0.196657278667, 0.0333333333333 ])) diff_gauss = af.abs(ref_integral - test_integral_gauss) diff_lobatto = af.abs(ref_integral - test_integral_lobatto) assert af.all_true(diff_gauss < threshold) and af.all_true( diff_lobatto < threshold)
def Li_Lj_coeffs(N_LGL): ''' ''' xi_LGL = lagrange.LGL_points(N_LGL) lagrange_coeffs = af.np_to_af_array( lagrange.lagrange_polynomials(xi_LGL)[1]) Li_xi = af.moddims(af.tile(af.reorder(lagrange_coeffs, 1, 2, 0), 1, N_LGL), N_LGL, 1, N_LGL**2) Lj_eta = af.tile(af.reorder(lagrange_coeffs, 1, 2, 0), 1, 1, N_LGL) Li_Lj_coeffs = utils.polynomial_product_coeffs(Li_xi, Lj_eta) return Li_Lj_coeffs
# The Gaussian weights. gauss_weights = lagrange.gaussian_weights(N_quad) # The lobatto nodes to be used for integration. lobatto_quadrature_nodes = lagrange.LGL_points(N_quad) # The lobatto weights to be used for integration. lobatto_weights_quadrature = lagrange.lobatto_weights\ (N_quad) # An array containing the coefficients of the lagrange basis polynomials. lagrange_coeffs = af.np_to_af_array(\ lagrange.lagrange_polynomials(xi_LGL)[1]) # Refer corresponding functions. lagrange_basis_value = lagrange.lagrange_function_value(lagrange_coeffs) # While evaluating the volume integral using N_LGL # lobatto quadrature points, The integration can be vectorized # and in this case the coefficients of the differential of the # Lagrange polynomials is required diff_pow = (af.flip(af.transpose(af.range(N_LGL - 1) + 1), 1)) dl_dxi_coeffs = (af.broadcast(utils.multiply, lagrange_coeffs[:, :-1], diff_pow)) # Obtaining an array consisting of the LGL points mapped onto the elements.
def A_matrix(N_LGL, advec_var): ''' Calculates the tensor product for the given ``params.N_LGL``. A tensor product element is given by: .. math:: [A^{pq}_{ij}] = \\iint L_p(\\xi) L_q(\\eta) \\ L_i(\\xi) L_j(\\eta) d\\xi d\\eta This function finds :math:`L_p(\\xi) L_i(\\xi)` and :math:`L_q(\\eta) L_j(\\eta)` and passes it to the ``integrate_2d`` function. Returns ------- A : af.Array [N_LGL^2 N_LGL^2 1 1] The tensor product. ''' xi_LGL = lagrange.LGL_points(N_LGL) lagrange_coeffs = af.np_to_af_array( lagrange.lagrange_polynomials(xi_LGL)[1]) xi_LGL = lagrange.LGL_points(N_LGL) eta_LGL = lagrange.LGL_points(N_LGL) _, Lp_xi = lagrange.lagrange_polynomials(xi_LGL) _, Lq_eta = lagrange.lagrange_polynomials(eta_LGL) Lp_xi = af.np_to_af_array(Lp_xi) Lq_eta = af.np_to_af_array(Lq_eta) Li_xi = Lp_xi.copy() Lj_eta = Lq_eta.copy() Lp_xi_tp = af.reorder(Lp_xi, d0=2, d1=0, d2=1) Lp_xi_tp = af.tile(Lp_xi_tp, d0=N_LGL * N_LGL * N_LGL) Lp_xi_tp = af.moddims(Lp_xi_tp, d0=N_LGL * N_LGL * N_LGL * N_LGL, d1=1, d2=N_LGL) Lp_xi_tp = af.reorder(Lp_xi_tp, d0=0, d1=2, d2=1) Lq_eta_tp = af.reorder(Lq_eta, d0=0, d1=2, d2=1) Lq_eta_tp = af.tile(Lq_eta_tp, d0=N_LGL, d1=N_LGL * N_LGL) Lq_eta_tp = af.moddims(af.transpose(Lq_eta_tp), d0=N_LGL * N_LGL * N_LGL * N_LGL, d1=1, d2=N_LGL) Lq_eta_tp = af.reorder(Lq_eta_tp, d0=0, d1=2, d2=1) Li_xi_tp = af.reorder(Li_xi, d0=2, d1=0, d2=1) Li_xi_tp = af.tile(Li_xi_tp, d0=N_LGL) Li_xi_tp = af.moddims(Li_xi_tp, d0=N_LGL * N_LGL, d1=1, d2=N_LGL) Li_xi_tp = af.reorder(Li_xi_tp, d0=0, d1=2, d2=1) Li_xi_tp = af.tile(Li_xi_tp, d0=N_LGL * N_LGL) Lj_eta_tp = af.reorder(Lj_eta, d0=0, d1=2, d2=1) Lj_eta_tp = af.tile(Lj_eta_tp, d0=N_LGL) Lj_eta_tp = af.reorder(Lj_eta_tp, d0=0, d1=2, d2=1) Lj_eta_tp = af.tile(Lj_eta_tp, d0=N_LGL * N_LGL) Lp_Li_tp = utils.poly1d_product(Lp_xi_tp, Li_xi_tp) Lq_Lj_tp = utils.poly1d_product(Lq_eta_tp, Lj_eta_tp) Lp_Li_Lq_Lj_tp = utils.polynomial_product_coeffs( af.reorder(Lp_Li_tp, d0=1, d1=2, d2=0), af.reorder(Lq_Lj_tp, d0=1, d1=2, d2=0)) A = utils.integrate_2d_multivar_poly(Lp_Li_Lq_Lj_tp, params.N_quad, 'gauss', advec_var) A = af.moddims(A, d0=N_LGL * N_LGL, d1=N_LGL * N_LGL) return A
def change_parameters(LGL, Elements, quad, wave='sin'): ''' Changes the parameters of the simulation. Used only for convergence tests. Parameters ---------- LGL : int The new N_LGL. Elements : int The new N_Elements. ''' # The domain of the function. params.x_nodes = af.np_to_af_array(np.array([-1., 1.])) # The number of LGL points into which an element is split. params.N_LGL = LGL # Number of elements the domain is to be divided into. params.N_Elements = Elements # The number quadrature points to be used for integration. params.N_quad = quad # Array containing the LGL points in xi space. params.xi_LGL = lagrange.LGL_points(params.N_LGL) # The weights of the lgl points params.weight_arr = lagrange.weight_arr_fun(params.xi_LGL) # N_Gauss number of Gauss nodes. params.gauss_points = af.np_to_af_array(lagrange.gauss_nodes\ (params.N_quad)) # The Gaussian weights. params.gauss_weights = lagrange.gaussian_weights(params.N_quad) # The lobatto nodes to be used for integration. params.lobatto_quadrature_nodes = lagrange.LGL_points(params.N_quad) # The lobatto weights to be used for integration. params.lobatto_weights_quadrature = lagrange.lobatto_weights\ (params.N_quad) #The b matrix params.b_matrix = lagrange.b_matrix_eval() # A list of the Lagrange polynomials in poly1d form. #params.lagrange_product = lagrange.product_lagrange_poly(params.xi_LGL) # An array containing the coefficients of the lagrange basis polynomials. params.lagrange_coeffs = af.np_to_af_array(\ lagrange.lagrange_polynomials(params.xi_LGL)[1]) # Refer corresponding functions. params.lagrange_basis_value = lagrange.lagrange_function_value\ (params.lagrange_coeffs) # While evaluating the volume integral using N_LGL # lobatto quadrature points, The integration can be vectorized # and in this case the coefficients of the differential of the # Lagrange polynomials is required params.diff_pow = (af.flip(af.transpose(af.range(params.N_LGL - 1) + 1), 1)) params.dl_dxi_coeffs = (af.broadcast(utils.multiply, params.lagrange_coeffs[:, :-1], params.diff_pow)) # Obtaining an array consisting of the LGL points mapped onto the elements. params.element_size = af.sum((params.x_nodes[1] - params.x_nodes[0])\ / params.N_Elements) params.elements_xi_LGL = af.constant(0, params.N_Elements, params.N_LGL) params.elements = utils.linspace(af.sum(params.x_nodes[0]), af.sum(params.x_nodes[1] - params.element_size),\ params.N_Elements) params.np_element_array = np.concatenate((af.transpose(params.elements), af.transpose(params.elements +\ params.element_size))) params.element_mesh_nodes = utils.linspace(af.sum(params.x_nodes[0]), af.sum(params.x_nodes[1]),\ params.N_Elements + 1) params.element_array = af.transpose(af.np_to_af_array\ (params.np_element_array)) params.element_LGL = wave_equation.mapping_xi_to_x(af.transpose\ (params.element_array), params.xi_LGL) # The minimum distance between 2 mapped LGL points. params.delta_x = af.min( (params.element_LGL - af.shift(params.element_LGL, 1, 0))[1:, :]) # dx_dxi for elements of equal size. params. dx_dxi = af.mean(wave_equation.dx_dxi_numerical((params.element_mesh_nodes[0 : 2]),\ params.xi_LGL)) # The value of time-step. params.delta_t = params.delta_x / (4 * params.c) # Array of timesteps seperated by delta_t. params.time = utils.linspace( 0, int(params.total_time / params.delta_t) * params.delta_t, int(params.total_time / params.delta_t)) # Initializing the amplitudes. Change u_init to required initial conditions. if (wave == 'sin'): params.u_init = af.sin(2 * np.pi * params.element_LGL) if (wave == 'gaussian'): params.u_init = np.e**(-(params.element_LGL)**2 / 0.4**2) params.u = af.constant(0, params.N_LGL, params.N_Elements, params.time.shape[0],\ dtype = af.Dtype.f64) params.u[:, :, 0] = params.u_init return