def test_diff_matrix(dims, eltype): n = 5 if eltype == "simplex": nodes = mp.warp_and_blend_nodes(dims, n) basis = mp.simplex_onb(dims, n) grad_basis = mp.grad_simplex_onb(dims, n) elif eltype == "tensor": nodes = mp.legendre_gauss_lobatto_tensor_product_nodes(dims, n) basis = mp.legendre_tensor_product_basis(dims, n) grad_basis = mp.grad_legendre_tensor_product_basis(dims, n) else: raise ValueError(f"unknown element type: {eltype}") diff_mat = mp.differentiation_matrices(basis, grad_basis, nodes) if isinstance(diff_mat, tuple): diff_mat = diff_mat[0] f = np.sin(nodes[0]) df_dx = np.cos(nodes[0]) df_dx_num = np.dot(diff_mat, f) error = la.norm(df_dx - df_dx_num) / la.norm(df_dx) logger.info("error: %.5e", error) assert error < 2.0e-4, error
def diff_matrices(self): result = mp.differentiation_matrices( self.basis(), mp.grad_simplex_onb(self.dim, self.order), self.unit_nodes) if not isinstance(result, tuple): return (result,) else: return result
def test_diff_matrix(dims): n = 5 nodes = mp.warp_and_blend_nodes(dims, n) f = np.sin(nodes[0]) df_dx = np.cos(nodes[0]) diff_mat = mp.differentiation_matrices(mp.simplex_onb(dims, n), mp.grad_simplex_onb(dims, n), nodes) if isinstance(diff_mat, tuple): diff_mat = diff_mat[0] df_dx_num = np.dot(diff_mat, f) print(la.norm(df_dx - df_dx_num)) assert la.norm(df_dx - df_dx_num) < 1e-3
def test_diff_matrix(dims): n = 5 nodes = mp.warp_and_blend_nodes(dims, n) f = np.sin(nodes[0]) df_dx = np.cos(nodes[0]) diff_mat = mp.differentiation_matrices( mp.simplex_onb(dims, n), mp.grad_simplex_onb(dims, n), nodes) if isinstance(diff_mat, tuple): diff_mat = diff_mat[0] df_dx_num = np.dot(diff_mat, f) print((la.norm(df_dx-df_dx_num))) assert la.norm(df_dx-df_dx_num) < 1e-3
def test_diff_matrix_permutation(dims): order = 5 from pytools import \ generate_nonnegative_integer_tuples_summing_to_at_most as gnitstam node_tuples = list(gnitstam(order, dims)) simplex_onb = mp.simplex_onb(dims, order) grad_simplex_onb = mp.grad_simplex_onb(dims, order) nodes = np.array( mp.warp_and_blend_nodes(dims, order, node_tuples=node_tuples)) diff_matrices = mp.differentiation_matrices(simplex_onb, grad_simplex_onb, nodes) for iref_axis in range(dims): perm = mp.diff_matrix_permutation(node_tuples, iref_axis) assert la.norm(diff_matrices[iref_axis] - diff_matrices[0][perm][:, perm]) < 1e-10
import numpy as np import modepy as mp n = 17 # use this total degree dimensions = 2 # Get a basis of orthonormal functions, and their derivatives. basis = mp.simplex_onb(dimensions, n) grad_basis = mp.grad_simplex_onb(dimensions, n) nodes = mp.warp_and_blend_nodes(dimensions, n) x, y = nodes # We want to compute the x derivative of this function: f = np.sin(5*x+y) df_dx = 5*np.cos(5*x+y) # The (generalized) Vandermonde matrix transforms coefficients into # nodal values. So we can find basis coefficients by applying its # inverse: f_coefficients = np.linalg.solve( mp.vandermonde(basis, nodes), f) # Now linearly combine the (x-)derivatives of the basis using # f_coefficients to compute the numerical derivatives. df_dx_num = np.dot( mp.vandermonde(grad_basis, nodes)[0], f_coefficients)
import numpy as np import modepy as mp n = 17 # use this total degree dimensions = 2 # Get a basis of orthonormal functions, and their derivatives. basis = mp.simplex_onb(dimensions, n) grad_basis = mp.grad_simplex_onb(dimensions, n) nodes = mp.warp_and_blend_nodes(dimensions, n) x, y = nodes # We want to compute the x derivative of this function: f = np.sin(5 * x + y) df_dx = 5 * np.cos(5 * x + y) # The (generalized) Vandermonde matrix transforms coefficients into # nodal values. So we can find basis coefficients by applying its # inverse: f_coefficients = np.linalg.solve(mp.vandermonde(basis, nodes), f) # Now linearly combine the (x-)derivatives of the basis using # f_coefficients to compute the numerical derivatives. df_dx_num = np.dot(mp.vandermonde(grad_basis, nodes)[0], f_coefficients) assert np.linalg.norm(df_dx - df_dx_num) < 1e-5
def grad_basis(self): if self.dim <= 3: return mp.grad_simplex_onb(self.dim, self.order) else: return mp.grad_simplex_monomial_basis(self.dim, self.order)
def grad_basis(self): if self.dim <= 3: return mp.grad_simplex_onb(self.dim, self.order) else: return mp.grad_simplex_monomial_basis(self.dim, self.order)