def eval_expected(self, x, y, **kwargs): r, th = cart_to_polar(x, y) if th < self.a/2: th += 2*np.pi return self.eval_expected_polar(r, th, **kwargs)
def c1_test(self, plot=True): """ Plot the reconstructed Neumann data, as approximated by a Chebyshev expansion. If the Chebyshev expansion shows "spikes" near the interfaces of the segments, something is probably wrong. """ sample = self.get_boundary_sample() do_exact = hasattr(self.problem, 'eval_d_u_outwards') s_data = np.zeros(len(sample)) expansion_data = np.zeros(len(sample)) if do_exact: exact_data = np.zeros(len(sample)) for l in range(len(sample)): p = sample[l] s_data[l] = p['s'] if do_exact: exact_data[l] = self.problem.eval_d_u_outwards(p['arg'], p['sid']).real r, th = domain_util.cart_to_polar(p['x'], p['y']) sid = domain_util.get_sid(self.a, th) if sid == 0: arg = th else: arg = r for JJ in range(len(self.B_desc)): expansion_data[l] +=\ (self.c1[JJ] * self.eval_dn_B_arg(0, JJ, arg, sid)).real import matplotlib.pyplot as plt if do_exact: error = np.max(np.abs(exact_data - expansion_data)) print('c1 error: {}'.format(error)) plt.plot(s_data, exact_data, linewidth=5, color='#BBBBBB', label='Exact') if not plot: return plt.plot(s_data, expansion_data, label='Expansion') plt.legend(loc=2) #plt.ylim(-1.5, 1.5) plt.title('c1') plt.xlabel('Arclength s') plt.ylabel('Reconstructed Neumann data') plt.show()
def do_extend_2_standard(self, i, j, **kwargs): x, y = self.get_coord(i, j) x0, y0 = self.get_radius_point(2, x, y) param_r = cart_to_polar(x0, y0)[0] deriv_types = ( (0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1), (4, 0), ) derivs = self.ext_calc_xi_derivs(deriv_types, param_r, 2, **kwargs) n = self.signed_dist_to_radius(2, x, y) value = self.extend_radius(n=n, **derivs) return {'elen': abs(n), 'value': value}
def _calc_inhomo_radius(self, x0, y0, x1, y1, tan, n): p = self.problem f = p.eval_f(x0, y0) if x0 == 0 and y0 == 0: """ Use Taylor's theorem to construct a smooth extension of grad_f, and hessian_f at the origin, since they may be undefined or annoying to calculate at this point. """ h = self.AD_len / (10 * self.N) hessian_f = p.eval_hessian_f(-h, 0) grad_f = p.eval_grad_f(-h, 0) grad_f += h * hessian_f.dot((1, 0)) else: grad_f = p.eval_grad_f(x0, y0) hessian_f = p.eval_hessian_f(x0, y0) vec = np.array((x1 - x0, y1 - y0)) if np.linalg.norm(vec) != 0: vec /= np.linalg.norm(vec) d_f_n = grad_f.dot(vec) * np.sign(n) d2_f_n = hessian_f.dot(vec).dot(vec) d2_f_s = hessian_f.dot(tan).dot(tan) elen = abs(n) # Only so that elen matches that of the homogeneous extension r, th = cart_to_polar(x0, y0) if x0 < 0 or r > self.R: elen += r v = self.inhomo_extend_arbitrary(n=n, f=f, d_f_n=d_f_n, d2_f_n=d2_f_n, d2_f_s=d2_f_s, curv=0) return {"elen": elen, "value": v}
def c0_test(self, plot=True): """ Plot the Dirichlet data along with its Chebyshev expansion. If there is more than a minor difference between the two, something is wrong. """ k = self.k a = self.a nu = self.nu sample = self.get_boundary_sample() s_data = np.zeros(len(sample)) exact_data = np.zeros(len(sample)) expansion_data = np.zeros(len(sample)) exact_data_outer = [] expansion_data_outer = [] for l in range(len(sample)): p = sample[l] s_data[l] = p['s'] r, th = domain_util.cart_to_polar(p['x'], p['y']) sid = domain_util.get_sid(self.a, th) exact_data[l] = self.problem.eval_bc(p['arg'], sid).real if sid == 0: arg = th else: arg = r for JJ in range(len(self.B_desc)): expansion_data[l] +=\ (self.c0[JJ] * self.eval_dn_B_arg(0, JJ, arg, sid)).real if sid == 0: exact_data_outer.append(exact_data[l]) expansion_data_outer.append(expansion_data[l]) exact_data_outer = np.array(exact_data_outer) expansion_data_outer = np.array(expansion_data_outer) print('c0 error outer:', np.max(np.abs(exact_data_outer - expansion_data_outer))) print('c0 error:', np.max(np.abs(exact_data - expansion_data))) #for l in range(len(exact_data)): # diff = abs(exact_data[l] - expansion_data[l]) # if diff > .06: # print(sample[l], 'diff:', diff) if not plot: return import matplotlib.pyplot as plt plt.plot(s_data, exact_data, linewidth=5, color='#BBBBBB', label='Exact') plt.plot(s_data, expansion_data, label='Expansion') plt.legend(loc=0) plt.title('c0') plt.xlabel('Arclength s') plt.ylabel('Dirichlet data') plt.show()
def eval_f(self, x, y): if self.homogeneous: return 0 return self.eval_f_polar(*cart_to_polar(x, y))
def get_polar(self, i, j): """ Get the polar coordinates of grid point (i,j) """ x, y = self.get_coord(i, j) return cart_to_polar(x, y)
def eval_grad_f(self, x, y): """ Evaluate gradient of f at (x, y), using the chain rule. Returns the result as a NumPy array. """ return self.eval_grad_f_polar(*cart_to_polar(x, y))
def eval_hessian_f(self, x, y): """ Evaluate the Cartesian Hessian of f at (x, y), using the chain rule. Returns the result as a 2x2 NumPy array. """ return self.eval_hessian_f_polar(*cart_to_polar(x, y))