def test_lobatto(): x, w = gauss_lobatto(2, 17) assert [str(r) for r in x] == ['-1', '1'] assert [str(r) for r in w] == ['1.0000000000000000', '1.0000000000000000'] x, w = gauss_lobatto(3, 17) assert [str(r) for r in x] == ['-1', '0', '1'] assert [str(r) for r in w] == [ '0.33333333333333333', '1.3333333333333333', '0.33333333333333333' ] x, w = gauss_lobatto(4, 17) assert [str(r) for r in x ] == ['-1', '-0.44721359549995794', '0.44721359549995794', '1'] assert [str(r) for r in w] == [ '0.16666666666666667', '0.83333333333333333', '0.83333333333333333', '0.16666666666666667' ] x, w = gauss_lobatto(5, 17) assert [str(r) for r in x] == [ '-1', '-0.65465367070797714', '0', '0.65465367070797714', '1' ] assert [str(r) for r in w] == [ '0.10000000000000000', '0.54444444444444444', '0.71111111111111111', '0.54444444444444444', '0.10000000000000000' ]
def test_lobatto_precise(): x, w = gauss_lobatto(3, 40) assert [str(r) for r in x] == ['-1', '0', '1'] assert [str(r) for r in w] == [ '0.3333333333333333333333333333333333333333', '1.333333333333333333333333333333333333333', '0.3333333333333333333333333333333333333333' ]
def create_c_vector(self, s): c, w = gauss_lobatto(s, s) c = np.array(c) c = c + 1 c = c * 0.5 # print("C is") # print(c) return c
def test_lobatto_precise(): x, w = gauss_lobatto(3, 40) assert [str(r) for r in x] == ["-1", "0", "1"] assert [str(r) for r in w] == [ "0.3333333333333333333333333333333333333333", "1.333333333333333333333333333333333333333", "0.3333333333333333333333333333333333333333", ]
def test_lobatto(): x, w = gauss_lobatto(2, 17) assert [str(r) for r in x] == [ '-1', '1'] assert [str(r) for r in w] == [ '1.0000000000000000', '1.0000000000000000'] x, w = gauss_lobatto(3, 17) assert [str(r) for r in x] == [ '-1', '0', '1'] assert [str(r) for r in w] == [ '0.33333333333333333', '1.3333333333333333', '0.33333333333333333'] x, w = gauss_lobatto(4, 17) assert [str(r) for r in x] == [ '-1', '-0.44721359549995794', '0.44721359549995794', '1'] assert [str(r) for r in w] == [ '0.16666666666666667', '0.83333333333333333', '0.83333333333333333', '0.16666666666666667'] x, w = gauss_lobatto(5, 17) assert [str(r) for r in x] == [ '-1', '-0.65465367070797714', '0', '0.65465367070797714', '1'] assert [str(r) for r in w] == [ '0.10000000000000000', '0.54444444444444444', '0.71111111111111111', '0.54444444444444444', '0.10000000000000000']
def test_lobatto_precise(): x, w = gauss_lobatto(3, 40) assert [str(r) for r in x] == [ '-1', '0', '1'] assert [str(r) for r in w] == [ '0.3333333333333333333333333333333333333333', '1.333333333333333333333333333333333333333', '0.3333333333333333333333333333333333333333']
def test_lobatto(): x, w = gauss_lobatto(2, 17) assert [str(r) for r in x] == ["-1", "1"] assert [str(r) for r in w] == ["1.0000000000000000", "1.0000000000000000"] x, w = gauss_lobatto(3, 17) assert [str(r) for r in x] == ["-1", "0", "1"] assert [str(r) for r in w] == [ "0.33333333333333333", "1.3333333333333333", "0.33333333333333333", ] x, w = gauss_lobatto(4, 17) assert [str(r) for r in x] == [ "-1", "-0.44721359549995794", "0.44721359549995794", "1", ] assert [str(r) for r in w] == [ "0.16666666666666667", "0.83333333333333333", "0.83333333333333333", "0.16666666666666667", ] x, w = gauss_lobatto(5, 17) assert [str(r) for r in x] == [ "-1", "-0.65465367070797714", "0", "0.65465367070797714", "1", ] assert [str(r) for r in w] == [ "0.10000000000000000", "0.54444444444444444", "0.71111111111111111", "0.54444444444444444", "0.10000000000000000", ]
def _quad_coeffs_hq(M, collocation_type, digits=20): if M == 1: x = np.array([0.0]) w = np.array([2.0]) elif collocation_type == "gauss_legendre": from sympy.integrals.quadrature import gauss_legendre x, w = gauss_legendre(M, digits) elif collocation_type == "gauss_lobatto": from sympy.integrals.quadrature import gauss_lobatto x, w = gauss_lobatto(M, digits) elif collocation_type == "gauss_hermite": from sympy.integrals.quadrature import gauss_hermite x, w = gauss_hermite(M, 30) x = np.array(x, dtype=float) w = np.array(w, dtype=float) elif collocation_type == "gauss_jacobi": from sympy.integrals.quadrature import gauss_jacobi x, w = gauss_jacobi(M, 0, 0, 30) x = np.array(x, dtype=float) w = np.array(w, dtype=float) elif collocation_type == "gauss_chebyshev_u": from sympy.integrals.quadrature import gauss_chebyshev_u x, w = gauss_chebyshev_u(M, 30) x = np.array(x, dtype=float) w = np.array(w, dtype=float) elif collocation_type == "gauss_chebyshev_t": from sympy.integrals.quadrature import gauss_chebyshev_t x, w = gauss_chebyshev_t(M, 30) x = np.array(x, dtype=float) w = np.array(w, dtype=float) else: raise Exception("Unknown collocation method '" + str(collocation_type) + "'") assert len(x) == M return x, w
tx = np.zeros(nnp,np.float64) ty = np.zeros(nnp,np.float64) # M_prime_el=(hx/2)*np.array([\ # [16/105,957/2240,-3/70,19/840],\ # [957/2240,2619/896,-1161/2240,-327/2240],\ # [-3/70,-1161/2240,27/35,33/280],\ # [19/840,-327/2240,33/280,16/105]]) M_prime_el=np.zeros((4,4),dtype=np.float64) use_mass_lumping=False if use_mass_lumping: degree=4 gleg_points,gleg_weights=gauss_lobatto(degree,20) else: degree=14 gleg_points,gleg_weights=np.polynomial.legendre.leggauss(degree) for iq in range(degree): jq=0 rq=gleg_points[iq] sq=-1 weightq=gleg_weights[iq] NV[0:mV]=NNV(rq,sq) M_prime_el[0,0] += NV[0]*NV[0]*weightq M_prime_el[0,1] += NV[0]*NV[1]*weightq M_prime_el[0,2] += NV[0]*NV[2]*weightq M_prime_el[0,3] += NV[0]*NV[3]*weightq
def __init__(self, n_order, FEM_boundaries, Mass=1, Complex_scale=1, R0_scale=0.0): """Constructor method Builds 1D FEM DVR grid and kinetic energy matrix representation in normalized FEM DVR basis of shape functions and bridging functions Finite elements of any sizes determined by FEM_boundaries array, but all with same order DVR Args: n_order (int): The DVR order. More dense takes longer but improves acuracy FEM_boundaries (ndarray): An array of 'double' or 'complex' boundaries for the Finite-Elements. Complex_scale (int,optional): Flag that starts complex scaling at grid boundary closest to R0 if .ne. 1. Defaults to 1 R0_scale (complex,optional): Grid point where the complex tail starts. Defaults to 0.0 Attributes: n_order (int): Can access the DVR order later FEM_boundaries (ndarray): Can access the 'double' or 'complex' Finite-Element boundaries later nbas (int): Total number of basis functions in the FEM-DVR grid x_pts (ndarray): Array of 'double' or 'complex' DVR points w_pts (ndarray): Array of 'double' or 'complex' DVR weight KE_mat (ndarray): Kintetic Energy matrix, dimension is nbas x nbas of 'double' or 'complex' types i_elem_scale (int): index of last real DVR point """ self.n_order = n_order self.FEM_boundaries = FEM_boundaries N_elements = len(FEM_boundaries) - 1 print( "\nFrom FEM_DVR_build: building grid and KE with Gauss Lobatto quadrature of order ", n_order, ) # # Complex the FEM_boundaries if Complex_scale .ne. 1 # i_elem_scale = 0.0 if Complex_scale != 1: for i_elem in range(0, N_elements): if FEM_boundaries[i_elem] >= R0_scale: R0 = FEM_boundaries[i_elem] i_elem_scale = i_elem break for i_elem in range(i_elem_scale, N_elements + 1): FEM_boundaries[i_elem] = R0 + Complex_scale * ( FEM_boundaries[i_elem] - R0) # for i_elem in range(0, N_elements): print( "element ", i_elem + 1, " xmin = ", FEM_boundaries[i_elem], " xmax = ", FEM_boundaries[i_elem + 1], ) # # sympy gauss_lobatto(n,p) does extended precision, apparently symbolically, to # produce points and weights on (-1,+1). It is slow for higher orders n_precision = 18 xlob, wlob = gauss_lobatto(n_order, n_precision) # w_lobatto = [] x_lobatto = [] # make the elements of the extended precision points and weights from sympy # into ordinary floating point numbers for subsequent use for i in range(0, n_order): w_lobatto.append(float(wlob[i])) x_lobatto.append(float(xlob[i])) # create temporary array to hold kinetic energy before first and last points of grid are removed # to enforce boundary condition psi = 0 at ends of grid # all initializations with np.zeros are complex for ECS full_grid_size = n_order * N_elements - (N_elements - 1) KE_temp = np.zeros((full_grid_size, full_grid_size), dtype=np.complex) # Build FEM_DVR and Kinetic Energy matrix in same for loop: x_pts = np.zeros(full_grid_size, dtype=np.complex) w_pts = np.zeros(full_grid_size, dtype=np.complex) for i_elem in range(0, N_elements): xmin = FEM_boundaries[i_elem] xmax = FEM_boundaries[i_elem + 1] x_elem = [] w_elem = [] for i in range(0, n_order): x_elem.append( ((xmax - xmin) * x_lobatto[i] + (xmax + xmin)) / 2.0) w_elem.append((xmax - xmin) * w_lobatto[i] / 2.0) for i in range(0, n_order): shift = i_elem * n_order if i_elem > 0: shift = i_elem * n_order - i_elem x_pts[i + shift] = x_elem[i] w_pts[i + shift] = w_pts[i + shift] + w_elem[i] # DVR().Kinetic_energy_FEM_block gives -1/2 d^x/dx^2 in unnormalized DVR basis KE_elem = self.Kinetic_Energy_FEM_block(n_order, x_elem, w_elem) for i in range(0, n_order): for j in range(0, n_order): KE_temp[i + (i_elem * (n_order - 1)), j + (i_elem * (n_order - 1)), ] = ( KE_temp[i + (i_elem * (n_order - 1)), j + (i_elem * (n_order - 1)), ] + KE_elem[i, j]) nbas = full_grid_size - 2 KE_mat = np.zeros((nbas, nbas), dtype=np.complex) # apply normalizations of DVR basis to KE matrix, bridging fcns have w_left + w_right for i in range(0, nbas): for j in range(0, nbas): KE_mat[i, j] = KE_temp[i + 1, j + 1] / np.sqrt( w_pts[i + 1] * w_pts[j + 1]) # KE matrix complete, return full grid with endpoints and KE_mat which is # nbas x nbas self.nbas = nbas self.x_pts = x_pts self.w_pts = w_pts self.KE_mat = KE_mat self.rescale_kinetic(Mass) self.i_elem_scale = i_elem_scale
import sys from math import * from sympy.integrals.quadrature import gauss_lobatto precisao = 9 farg = sys.argv[1].replace("^", "**") variavel = sys.argv[3] if len(sys.argv) >= 4 else 'x' f = eval("lambda " + variavel + ": " + farg) n = int(sys.argv[2]) x, w = gauss_lobatto(n, precisao) xw = zip(x, w) fxi = [f(xi)*wi for xi, wi in xw] print(sum(fxi))