Skip to content

Commit eacdd92

Browse files
committed
Issue with mask_off_axis
1 parent 3b7d7ef commit eacdd92

File tree

4 files changed

+60
-19
lines changed

4 files changed

+60
-19
lines changed

sumpy/recurrence.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -477,8 +477,8 @@ def get_off_axis_expression(pde, taylor_order=4) -> [sp.Expr, int]:
477477
s(i) where s(i) comes from the off-axis recurrence. See
478478
get_reindexed_and_center_origin_off_axis_recurrence.
479479
480-
Also outputs the -number of coefficients it needs from nth order.
481-
So if it outputs -3 as the second return value, then it needs
480+
Also outputs the number of coefficients it needs from nth order.
481+
So if it outputs 3 as the second return value, then it needs
482482
s(deriv_order), s(deriv_order-1), ..., s(deriv_order-3).
483483
"""
484484
s = sp.Function("s")
@@ -506,6 +506,5 @@ def get_off_axis_expression(pde, taylor_order=4) -> [sp.Expr, int]:
506506
max_idx = max(idx_l)
507507

508508
idx_l, _ = _extract_idx_terms_from_recurrence(exp)
509-
exp_range = (min(idx_l), max(idx_l))
510509

511-
return exp*(-1)**n, min(idx_l)
510+
return exp*(-1)**n, -min(idx_l)

sumpy/recurrence_qbx.py

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def generate_lamb_expr(i, n_initial):
156156
t_exp, t_exp_order = get_off_axis_expression(pde)
157157
storage_taylor_order = max(t_recur_order, t_exp_order+1)
158158

159-
storage = [np.zeros((n_p, n_p))] * storage_taylor_order
159+
storage_taylor = [np.zeros((n_p, n_p))] * storage_taylor_order
160160

161161
def gen_lamb_expr_t_recur(i, start_order):
162162
arg_list = []
@@ -170,11 +170,11 @@ def gen_lamb_expr_t_recur(i, start_order):
170170
lamb_expr_symb_deriv = sp.diff(g_x_y, var_t[0], i)
171171
for j in range(ndim):
172172
lamb_expr_symb_deriv = lamb_expr_symb_deriv.subs(var_t[j], 0)
173-
lamb_expr_symb = lamb_expr_symb_deriv
173+
lamb_expr_symb = lamb_expr_symb_deriv.subs(var[0], 0)
174174
else:
175175
lamb_expr_symb = t_recur.subs(n, i)
176176

177-
return lamb_expr_symb
177+
return sp.lambdify(arg_list, lamb_expr_symb)
178178

179179

180180
def gen_lamb_expr_t_exp(i, t_exp_order):
@@ -189,32 +189,71 @@ def gen_lamb_expr_t_exp(i, t_exp_order):
189189
lamb_expr_symb_deriv = sp.diff(g_x_y, var_t[0], i)
190190
for j in range(ndim):
191191
lamb_expr_symb_deriv = lamb_expr_symb_deriv.subs(var_t[j], 0)
192-
lamb_expr_symb = lamb_expr_symb_deriv.subs(var[0], 0)
192+
lamb_expr_symb = lamb_expr_symb_deriv
193193
else:
194194
lamb_expr_symb = t_exp.subs(n, i)
195195

196-
return lamb_expr_symb
196+
return sp.lambdify(arg_list, lamb_expr_symb)
197197

198198
interactions_off_axis = 0
199199
for i in range(p+1):
200200
lamb_expr_t_recur = gen_lamb_expr_t_recur(i, start_order)
201-
a1 = [*storage[:-t_recur_order], *coord]
201+
a1 = [*storage_taylor[(-t_recur_order):], *coord]
202202

203203
storage.pop(0)
204-
storage.append(lamb_expr_t_recur(a1))
204+
storage.append(lamb_expr_t_recur(*a1))
205205

206206
lamb_expr_t_exp = gen_lamb_expr_t_exp(i, t_exp_order)
207-
a2 = [*storage[:-(t_exp_order+1)], *coord]
207+
a2 = [*storage_taylor[-(t_exp_order+1):], *coord]
208208

209-
interactions_off_axis += lamb_expr_t_exp(a2) * radius**i/math.factorial(i)
209+
interactions_off_axis += lamb_expr_t_exp(*a2) * radius**i/math.factorial(i)
210210

211211
################
212+
# Compute True Interactions
213+
def generate_true(i):
214+
arg_list = []
215+
for j in range(ndim):
216+
arg_list.append(var[j])
217+
218+
lamb_expr_symb_deriv = sp.diff(g_x_y, var_t[0], i)
219+
for j in range(ndim):
220+
lamb_expr_symb_deriv = lamb_expr_symb_deriv.subs(var_t[j], 0)
221+
lamb_expr_symb = lamb_expr_symb_deriv
222+
223+
#print("=============== ORDER = " + str(i))
224+
#print(lamb_expr_symb)
225+
return sp.lambdify(arg_list, lamb_expr_symb)#, sp.lambdify(arg_list, lamb_expr_symb_deriv)
226+
227+
interactions_true = 0
228+
for i in range(p+1):
229+
lamb_expr_true = generate_true(i)
230+
a4 = [*coord]
231+
s_new_true = lamb_expr_true(*a4)
232+
interactions_true += s_new_true * radius**i/math.factorial(i)
233+
###############
212234

213235
#slope of line y = mx
214-
m = 1e10
236+
m = 1e5
215237
mask_on_axis = m*np.abs(coord[0]) >= np.abs(coord[1])
216238
mask_off_axis = m*np.abs(coord[0]) < np.abs(coord[1])
217239

240+
print("-------------------------")
241+
242+
relerr_on = np.abs(interactions_on_axis[mask_on_axis]-interactions_true[mask_on_axis])/np.abs(interactions_on_axis[mask_on_axis])
243+
print("MAX ON AXIS ERROR:", np.max(relerr_on))
244+
print(np.mean(relerr_on))
245+
print("X:", coord[0].reshape(-1)[np.argmax(relerr_on)])
246+
print("Y:", coord[1].reshape(-1)[np.argmax(relerr_on)])
247+
248+
print("-------------------------")
249+
250+
if np.any(mask_off_axis):
251+
relerr_off = np.abs(interactions_off_axis[mask_off_axis]-interactions_true[mask_off_axis])/np.abs(interactions_off_axis[mask_off_axis])
252+
print("MAX OFF AXIS ERROR:", np.max(relerr_off))
253+
print(np.mean(relerr_off))
254+
print("X:", coord[0].reshape(-1)[np.argmax(relerr_off)])
255+
print("Y:", coord[1].reshape(-1)[np.argmax(relerr_off)])
256+
218257
interactions_total = np.zeros(coord[0].shape)
219258
interactions_total[mask_on_axis] = interactions_on_axis[mask_on_axis]
220259
interactions_total[mask_off_axis] = interactions_off_axis[mask_off_axis]

test/test_recurrence.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ def test_helmholtz_2d_off_axis(deriv_order, exp_order):
259259

260260
exp, exp_range = get_off_axis_expression(helmholtz2d, exp_order)
261261
approx_deriv = exp.subs(n, deriv_order)
262-
for i in range(exp_range+deriv_order, deriv_order+1):
262+
for i in range(-exp_range+deriv_order, deriv_order+1):
263263
approx_deriv = approx_deriv.subs(s(i), ic[i])
264264

265265
rat = coord_dict[var[0]]/coord_dict[var[1]]
@@ -273,7 +273,7 @@ def test_helmholtz_2d_off_axis(deriv_order, exp_order):
273273
print("RATIO(x0/x1): ", rat)
274274
#assert relerr <= prederror
275275

276-
test_helmholtz_2d_off_axis(6, 4)
276+
test_helmholtz_2d_off_axis(5, 4)
277277

278278

279279
def test_laplace_2d_off_axis(deriv_order, exp_order):
@@ -319,7 +319,7 @@ def test_laplace_2d_off_axis(deriv_order, exp_order):
319319

320320
exp, exp_range = get_off_axis_expression(laplace2d, exp_order)
321321
approx_deriv = exp.subs(n, deriv_order)
322-
for i in range(exp_range+deriv_order, deriv_order+1):
322+
for i in range(-exp_range+deriv_order, deriv_order+1):
323323
approx_deriv = approx_deriv.subs(s(i), ic[i])
324324

325325
rat = coord_dict[var[0]]/coord_dict[var[1]]

test/test_recurrence_qbx.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,12 +295,15 @@ def _construct_laplace_axis_2d(orders, resolutions):
295295

296296
import matplotlib.pyplot as plt
297297
orders = [10]
298-
resolutions = range(200, 800, 200)
298+
#resolutions = range(200, 800, 200)
299+
resolutions = [800]
299300
err_mat = _construct_laplace_axis_2d(orders, resolutions)
301+
"""
300302
for i in range(len(orders)):
301303
plt.plot(resolutions, err_mat[i], label="order ="+str(orders[i]))
302304
plt.xlabel("Number of Nodes")
303305
plt.ylabel("Error")
304306
plt.title("2D Ellipse LP Eval Error")
305307
plt.legend()
306-
plt.show()
308+
plt.show()
309+
"""

0 commit comments

Comments
 (0)