def test(n_q, n, outer_pt, K, exact, step = 2.0, dist_mult = 5): quad_low = qc.gaussxw(n_q) start = dist_mult * (2.0 / n_q) ds = (start * (step ** np.arange(0, n))).tolist() ds.reverse() def eval(d, q): return integrate(K([outer_pt[0], outer_pt[1] + d]), q) init = np.array([eval(d, quad_low) for d in ds]) # print(str(init)) rich = [init] for m in range(1, n): prev_rich = rich[-1] best_est = prev_rich[-1] error = abs(best_est - exact) # print("Tier " + str(m - 1) + " equals: " + str(best_est) + # " with an error of: " + str(error)) factor = (1.0 / (step ** m - 1.0)) next_rich = factor * ((step ** m) * prev_rich[1:] - prev_rich[:-1]) rich.append(next_rich) print rich final_est = rich[-1][-1] error = abs(final_est - exact) error_estimate = 2 * abs(final_est - rich[-2][-1]) return error
def test_subtract_sing(): degree = 20 f = lambda x: eval_legendre(degree, x) * log(x + 1) almost_exact_x, almost_exact_w = telles_singular(51, -1) exact = sum(f(almost_exact_x) * almost_exact_w) # print exact gauss_x, gauss_w = gaussxw(degree + 1) est = sum(f(gauss_x) * gauss_w) error = abs(exact - est) f_singular_pt = lambda x: (-1.0) ** degree * log(x + 1) f_minus_singularity = lambda x: f(x) - f_singular_pt(x) addme = 0.6137056388801094 * (-1.0) ** (degree + 1) est2 = addme + sum(f_minus_singularity(gauss_x) * gauss_w) error2 = abs(exact - est2) # print error / exact # print error2 / exact # subtracting out the singularity is super ineffective on this problem... assert(abs(error2 / exact) < 0.3)
matplotlib.rcParams['text.usetex'] = True matplotlib.rcParams['font.size'] = 14 matplotlib.rcParams['xtick.direction'] = 'out' matplotlib.rcParams['ytick.direction'] = 'out' matplotlib.rcParams['lines.linewidth'] = 3 def integrate(f, quad_rule): sum = 0 for (p, w) in zip(quad_rule[0], quad_rule[1]): sum += w * f([p, 0.0]) return sum quad_orders = [16, 256, 5000] quad_rules = [(qc.gaussxw(n), str(n) + " order gauss") for n in quad_orders] def dist(x1, x2): return np.sqrt((x2[0] - x1[0])**2 + (x2[1] - x1[1])**2) # The laplace single layer potential single_layer = lambda x1: lambda x2: (-1.0 / (2 * np.pi)) * np.log(dist(x1, x2)) # The laplace double layer potential double_layer = lambda x1: lambda x2: (x2[0] - x1[0]) / (dist(x1, x2)**2) # Hypersingular thingamabob hypersing = lambda x1: lambda x2: (x2[0] - x1[0]) / (dist(x1, x2)**3)
import numpy as np import quadracheer as qc from scipy.special import legendre basis = legendre(16) sing_pt = 0.5 gauss = {i:qc.gaussxw(i) for i in range(1, 512, 8)} gauss_high = qc.gaussxw(1024) # func = lambda x: basis(x) * np.log(np.abs(x - sing_pt)) # exact = 0.0243191054613544 func = lambda x: basis(x) * (x - sing_pt) / ((x - sing_pt) ** 3) exact = -0.46579 def integrate(x_min, x_max, q): x = q[0] w = q[1] new_w = w * ((x_max - x_min) / 2.0) new_x = x_min + (x_max - x_min) * ((x + 1.0) / 2.0) return sum(new_w * func(new_x)) def with_hole(h, q): return integrate(-1.0, sing_pt - h, q) + integrate(sing_pt + h, 1.0, q) def donut(ho, hi, q): return integrate(sing_pt - ho, sing_pt - hi, q) + integrate(sing_pt + hi, sing_pt + ho, q) n = 10 step = 4.0 hs = 0.25 * ((1.0 / step) ** np.arange(0, n)) print hs ests = [[]]
def gauss(N): x, w = qc.gaussxw(N) x, w = qc.map_pts_wts(x, w, 0.0, 1.0) return QuadratureInfo(0.0, x, w)
sum = 0 for (p, w) in zip(quad_rule[0], quad_rule[1]): sum += w * f([p, 0.0]) return sum def dist(x1,x2): return np.sqrt((x2[0] - x1[0]) ** 2 + (x2[1] - x1[1]) ** 2) # The laplace single layer potential single_layer = lambda x1: lambda x2: (-1.0 / (2 * np.pi)) * np.log(dist(x1,x2)) # The laplace double layer potential double_layer = lambda x1: lambda x2: (x2[0] - x1[0]) / (dist(x1, x2) ** 2) # Hypersingular thingamabob hypersing = lambda x1: lambda x2: (x2[0] - x1[0]) / (dist(x1, x2) ** 3) quad_high = qc.gaussxw(2000) def test(n_q, n, outer_pt, K, exact, step = 2.0, dist_mult = 5): quad_low = qc.gaussxw(n_q) start = dist_mult * (2.0 / n_q) ds = (start * (step ** np.arange(0, n))).tolist() ds.reverse() def eval(d, q): return integrate(K([outer_pt[0], outer_pt[1] + d]), q) init = np.array([eval(d, quad_low) for d in ds]) # print(str(init)) rich = [init] for m in range(1, n): prev_rich = rich[-1] best_est = prev_rich[-1]
deriv = (-K_rr + 8 * K_r - 8 * K_l + K_ll) / (12 * h) second_deriv = (-K_rr + 16 * K_r - 30 * K_c + 16 * K_l - K_ll) / (12 * h ** 2) third_deriv = (K_rr - 2 * K_r + 2 * K_l - K_ll) / (2 * h ** 3) fourth_deriv = (K_rr - 4 * K_r + 6 * K_c - 4 * K_l + K_ll) / (h ** 4) c = [const, deriv, second_deriv, third_deriv, fourth_deriv] print c return (c, exp_pt) def qbx_eval(pt, qbx_info): return sum([(cm / factorial(m)) * (qbx_info[1][1] - pt[1]) ** m for (m, cm) in enumerate(qbx_info[0])]) qbx_quad_order = 16 qbx_quad_factor = 4 quad_orders = [qbx_quad_order, qbx_quad_order * qbx_quad_factor * 4, 5000] quad_rules = [(qc.gaussxw(n), str(n) + " order gauss") for n in quad_orders] def dist(x1,x2): return np.sqrt((x2[0] - x1[0]) ** 2 + (x2[1] - x1[1]) ** 2) # The laplace single layer potential # K = lambda x1: lambda x2: (-1.0 / (2 * np.pi)) * np.log(dist(x1,x2)) # The laplace double layer potential K = lambda x1: lambda x2: (x2[0] - x1[0]) / (dist(x1, x2) ** 3) # Eval on a nice line! interval_length = 2.0 qbx_distance = 5 * (interval_length / qbx_quad_order) print qbx_distance exp_pt = [0.2, qbx_distance] qbx_info = qbx_expand(K, qc.gaussxw(qbx_quad_order * qbx_quad_factor), exp_pt, 0) view = [0.005, 0.05]
matplotlib.rcParams['font.family'] = 'serif' matplotlib.rcParams['font.serif'] = ['Computer Modern Roman'] matplotlib.rcParams['text.usetex'] = True matplotlib.rcParams['font.size'] = 14 matplotlib.rcParams['xtick.direction'] = 'out' matplotlib.rcParams['ytick.direction'] = 'out' matplotlib.rcParams['lines.linewidth'] = 3 def integrate(f, quad_rule): sum = 0 for (p, w) in zip(quad_rule[0], quad_rule[1]): sum += w * f([p, 0.0]) return sum quad_orders = [16, 256, 5000] quad_rules = [(qc.gaussxw(n), str(n) + " order gauss") for n in quad_orders] def dist(x1,x2): return np.sqrt((x2[0] - x1[0]) ** 2 + (x2[1] - x1[1]) ** 2) # The laplace single layer potential single_layer = lambda x1: lambda x2: (-1.0 / (2 * np.pi)) * np.log(dist(x1,x2)) # The laplace double layer potential double_layer = lambda x1: lambda x2: (x2[0] - x1[0]) / (dist(x1, x2) ** 2) # Hypersingular thingamabob hypersing = lambda x1: lambda x2: (x2[0] - x1[0]) / (dist(x1, x2) ** 3) def eval(d, q, K): return integrate(K([outer_pt[0], outer_pt[1] + d]), q) quad_high = qc.gaussxw(1000)