def chebNodes(N, a=0, b=1, extrIncl=False): """returns a list of N Chebyshev nodes in [a,b]. If extrIncl is True, a,b are included, otherwise not.""" cheb = chebgauss(N)[0]*(b-a)/2 + (a+b)/2 if not(extrIncl): return chebgauss(N)[0]*(b-a)/2 + (a+b)/2 else: out = list(chebgauss(N-2)[0]*(b-a)/2 + (a+b)/2) out.append(a) out.reverse() out.append(b) return out
def points_and_weights(self, N): self.N = N if self.quad == "GL": points = n_cheb.chebpts2(N)[::-1] weights = np.zeros((N)) + np.pi / (N - 1) weights[0] /= 2 weights[-1] /= 2 elif self.quad == "GC": points, weights = n_cheb.chebgauss(N) return points, weights
def points_and_weights(self, N): self.N = N if self.quad == "GC": points = n_cheb.chebpts2(N)[::-1] weights = np.zeros((N))+np.pi/(N-1) weights[0] /= 2 weights[-1] /= 2 elif self.quad == "GL": points, weights = n_cheb.chebgauss(N) return points, weights
def points_and_weights(self, N): self.N = N if self.quad == "GL": points = (n_cheb.chebpts2(N)[::-1]).astype(float) weights = zeros(N)+pi/(N-1) weights[0] /= 2 weights[-1] /= 2 elif self.quad == "GC": points, weights = n_cheb.chebgauss(N) points = points.astype(float) weights = weights.astype(float) return points, weights
def test_100(self): x, w = cheb.chebgauss(100) # test orthogonality. Note that the results need to be normalized, # otherwise the huge values that can arise from fast growing # functions like Laguerre can be very confusing. v = cheb.chebvander(x, 99) vv = np.dot(v.T * w, v) vd = 1/np.sqrt(vv.diagonal()) vv = vd[:, None] * vv * vd assert_almost_equal(vv, np.eye(100)) # check that the integral of 1 is correct tgt = np.pi assert_almost_equal(w.sum(), tgt)
def test_100(self): x, w = cheb.chebgauss(100) # test orthogonality. Note that the results need to be normalized, # otherwise the huge values that can arise from fast growing # functions like Laguerre can be very confusing. v = cheb.chebvander(x, 99) vv = np.dot(v.T * w, v) vd = 1/np.sqrt(vv.diagonal()) vv = vd[:,None] * vv * vd assert_almost_equal(vv, np.eye(100)) # check that the integral of 1 is correct tgt = np.pi assert_almost_equal(w.sum(), tgt)
def proj_radial(nr, a, b, x = None): # evaluate the radial basis functions of degree <nr # to the projection of the poloidal field in r direction (Q-part of a qst decomposition if x is None: xx,ww = cheb.chebgauss(nr) else: xx = np.array(x) # use chebyshev polynomials normalized for FCT coeffs = np.eye(nr)*2 #coeffs = np.eye(nr) coeffs[0,0]=1. #return spsp.lil_matrix((cheb.chebval(xx,coeffs)).transpose()) return np.mat((cheb.chebval(xx,coeffs)).transpose())
def points_and_weights(self, N): self.N = N if self.quad == "GL": points = (n_cheb.chebpts2(N)[::-1]).astype(float) #points = (n_cheb.chebpts2(N)).astype(float) weights = zeros(N) + pi / (N - 1) weights[0] /= 2 weights[-1] /= 2 elif self.quad == "GC": points, weights = n_cheb.chebgauss(N) #points = points[::-1] points = points.astype(float) weights = weights.astype(float) return points, weights
def points_and_weights(self, N, scaled=False): if self.quad == "GL": points = -(n_cheb.chebpts2(N)).astype(float) weights = np.full(N, np.pi/(N-1)) weights[0] /= 2 weights[-1] /= 2 elif self.quad == "GC": points, weights = n_cheb.chebgauss(N) points = points.astype(float) weights = weights.astype(float) if scaled is True: points = self.map_true_domain(points) return points, weights
def proj_radial_r2(nr, a, b, x = None): # evaluate the radial basis functions of degree <nr over r # this projection operator includes the 1/r**2 part and corresponds # to the a projection from spectral to physical for the toroidal field (T-part of a qst decomposition) if x is None: xx,ww = cheb.chebgauss(nr) else: xx = np.array(x) # use chebyshev polynomials normalized for FCT coeffs = np.eye(nr)*2 #coeffs = np.eye(nr) coeffs[0,0]=1. temp = (cheb.chebval(xx,coeffs)/(a*xx+b)**2).transpose() #return spsp.coo_matrix(temp) return np.mat(temp)
def proj_lapl(nr, a, b, x=None): # evaluate the second derivative of radial basis in radial direction function of degree <nr # this projection operator corresponds to 1/r**2 d/dr r**2d/dr projecting from spectral to physical # used for the theta and phi contribution of the poloidal field (S-part of a qst decomposition) if x is None: xx, ww = cheb.chebgauss(nr) else: xx = np.array(x) # use chebyshev polynomials normalized for FCT coeffs = np.eye(nr) * 2 #coeffs = np.eye(nr) coeffs[0, 0] = 1. c1 = cheb.chebder(coeffs,1) c2 = cheb.chebder(coeffs,2) return np.mat((2*cheb.chebval(xx, c1) /a /(a * xx + b) + cheb.chebval(xx, c2) /a**2).transpose())
def proj_dradial_dr(nr, a, b, x = None): # evaluate the first derivative of radial basis function of degree <nr # this projection operator corresponds to 1/r d/dr r projecting from spectral to physical # used for the theta and phi contribution of the poloidal field (S-part of a qst decomposition) if x is None: xx, ww = cheb.chebgauss(nr) else: xx = np.array(x) # use chebyshev polynomials normalized for FCT coeffs = np.eye(nr) * 2 #coeffs = np.eye(nr) coeffs[0, 0] = 1. c = cheb.chebder(coeffs) # temp = (cheb.chebval(xx,c)/a + cheb.chebval(xx,coeffs)/(a*xx+b)).transpose() #return spsp.coo_matrix(temp) return np.mat(temp)
def b(c): co = -np.sum(c[::2]); ce = -np.sum(c[1::2]) c = np.hstack((c, co, ce)) g = lambda x: x + self.chebEval(c, x) x, w = np_cheb.chebgauss(n * 6) dg1 = self.chebDiff(c) dg2 = self.chebDiff(dg1) js = (-1) ** np.arange(dg1.size) if np.sum(np.abs(dg2) > 1e-8): dgroots = self.chebRoots(dg2) # print(dgroots) if dgroots.size > 0: if np.min(self.chebEval(dg1, dgroots)) <= -1 + 1e-9 or np.dot(js, dg1) <= -1 + 1e-9 or np.sum( dg1) <= -1 + 1e-9: return 1 + np.random.rand(1) else: if np.dot(js, dg1) <= -1 + 1e-9 or np.sum(dg1) <= -1 + 1e-9: return 1 # print(dg) # if np.sum(dg) < -1: # return 1 dg = self.chebDiff(c) if np.random.rand(1) > 0.9: # plt.plot(x, self.chebEval(dg, x)) # plt.plot(x, g(x), '--') # plt.show() # plt.plot(x, f(g(x))) # plt.show() nodes = self.chebNodes(n=n) plt.loglog(np.abs(self.chebTransform(f(g(nodes)))), 'ro') plt.show() # F = lambda x, n: f(g(x))*special.eval_chebyt(n, x) # I = np.abs(np.dot(w, F(x, int(4*n/5)))) nodes = self.chebNodes(n=n) I = np.max(np.abs(self.chebTransform(f(g(x)))[-2:])) # print(I) print(I, c) return I
""" # Use sympy to compute a rhs, given an analytical solution x = Symbol("x") u = (1 - x**2)**2 * cos(np.pi * x) * (x - 0.25)**2 f = u.diff(x, 2) # Choices banded = True fast_transform = True N = 20 k = np.arange(N - 2) points, weights = n_cheb.chebgauss(N) # Note! N points in real space gives N-2 bases in spectral space # Build Vandermonde matrix for Gauss-Cheb points and weights V = n_cheb.chebvander(points, N - 3).T - n_cheb.chebvander(points, N - 1)[:, 2:].T # Gauss-Chebyshev quadrature to compute rhs fj = np.array([f.subs(x, j) for j in points], dtype=float) # Get f on quad points def fastChebScalar(fj): return dct(fj, 2) * np.pi / (2 * len(fj))
import sys print "Starting program" inputs = sys.argv if len(inputs) == 3: fileIn = inputs[1] fileOut = inputs[2] else: fileIn = "/gpfs1/spdomin/channelWithRe/wale_wedge6.e-s004" fileOut = "profile.dat" nPoints = 100 rho = 1.0 mu = 2.531646e-3 # create non uniform spacing with Gauss-Chebyshev polynomials # double the spaceing because we will only use half the points for the biasing y, w = chebgauss(2*nPoints) # transform the data so y \in (0:2) y *= -1.0 y += 1.0 # only use the half quadrature for biasing effect y = y[0:nPoints] vertical_locations = np.array(y.copy(),ndmin=2).T # generate y+ values yp = vertical_locations/mu*rho origins = [(0,y,0) for y in vertical_locations] normal = (0,1,0) PostProcessor = AverageOverPlanes(fileIn) PostProcessor.ReadInput() PostProcessor.CreatePipeline() PostProcessor.SetSliceLocation(origins[0],normal)
def __init__(self, order, method='legendre', lobatto=False): """ Straightforward gaussian integration using Chebyshev polynomials with mapping of the bounds into [-1,1]. Most useful for a bounded interval. Parameters ---------- order : int Order of basis expansion. method : str,'legendre' lobatto : bool,False If True, use Lobatto collocation points. Only works for Chebyshev polynomials. """ from numpy.polynomial.chebyshev import chebval, chebgauss self.order = order self.N = order if method == 'chebyshev': self.basis = [ lambda x, i=i: chebval(x, [0] * i + [1]) for i in range(self.N + 1) ] # Lobatto collocation points. if lobatto: self.coX = -np.cos(np.pi * np.arange(self.N + 1) / self.N) self.weights = np.zeros(self.order + 1) + np.pi / self.N self.weights[0] = self.weights[-1] = np.pi / (2 * self.N) else: self.coX, self.weights = chebgauss(self.N + 1) self.basisCox = [b(self.coX) for b in self.basis] if ((self.coX) == 1).any(): self.W = np.zeros_like(self.coX) ix = np.abs(self.coX) == 1 self.W[ix == 0] = 1 / np.sqrt(1 - self.coX[ix == 0]**2) self.W[ix] = np.inf else: self.W = 1 / np.sqrt( 1 - self.coX**2) # weighting function we must remove self.W[np.isnan(self.W)] = 0. elif method == 'legendre': from numpy.polynomial.legendre import leggauss, legval if order > 100: from .high_prec.calculus import leggauss self.coX, self.weights = leggauss(self.N + 1) self.coX = np.array([float(f) for f in self.coX]) self.weights = np.array([float(f) for f in self.weights]) else: self.coX, self.weights = leggauss(self.N + 1) self.basis = [ lambda x, i=i: legval(x, [0] * i + [1]) for i in range(self.N + 1) ] self.basisCox = [b(self.coX) for b in self.basis] self.W = np.ones_like(self.coX) else: raise Exception("Invalid basis choice.") # Map bounds to given bounds or from given bounds to [-1,1]. self.map_to_bounds = lambda x, x0, x1: (x + 1) / 2 * (x1 - x0) + x0 self.map_from_bounds = lambda x, x0, x1: (x - x0) / (x1 - x0) * 2. - 1.
""" # Use sympy to compute a rhs, given an analytical solution x = Symbol("x") u = (1-x**2)**2*cos(np.pi*x)*(x-0.25)**2 f = u.diff(x, 2) # Choices banded = True fast_transform = True N = 20 k = np.arange(N-2) points, weights = n_cheb.chebgauss(N) # Note! N points in real space gives N-2 bases in spectral space # Build Vandermonde matrix for Gauss-Cheb points and weights V = n_cheb.chebvander(points, N-3).T - n_cheb.chebvander(points, N-1)[:, 2:].T # Gauss-Chebyshev quadrature to compute rhs fj = np.array([f.subs(x, j) for j in points], dtype=float) # Get f on quad points def fastChebScalar(fj): return dct(fj, 2)*np.pi/(2*len(fj)) def fastChebTrans(fj): cj = dct(fj, 2) cj /= len(fj)