Note
Go to the end to download the full example code.
Symbolic SPH kernels & c++ tests#
7 from __future__ import division
8
9 import matplotlib.pyplot as plt
10 import mpmath
11 import numpy as np
12 from sympy import *
13
14 import shamrock
Use shamrock documentation style for matplotlib
18 shamrock.matplotlib.set_shamrock_mpl_style()
Set precision to 32 digits
22 mpmath.mp.dps = 32
23
24 q = symbols("q")
Utilities#
31 def getnorm(w, R):
32 c1D = sympify(1) / (2 * integrate(w, (q, 0, R)))
33 c2D = sympify(1) / (integrate(2 * pi * q * w, (q, 0, R)))
34 c3D = sympify(1) / (integrate(4 * pi * q * q * w, (q, 0, R)))
35 return (c1D, c2D, c3D)
36
37
38 def replace_pi_constants(cpp_code):
39 """Replace M_PI and M_1_PI with shambase::constants::pi<Tscal>"""
40 cpp_code = cpp_code.replace("M_1_PI", "1/shambase::constants::pi<Tscal>")
41 cpp_code = cpp_code.replace("M_PI", "shambase::constants::pi<Tscal>")
42 return cpp_code
43
44
45 def sympy_to_cpp(expr):
46 """Convert a sympy expression to C++ code with proper formatting"""
47
48 import re
49
50 # Expand the expression first
51 # expr = expand(expr)
52 cpp_code = ccode(expr)
53
54 # Replace pow(q, n) with q*q*q... for powers up to 11
55 # for power in range(11, 1, -1): # From 11 down to 2
56 # q_mult = '*'.join(['q'] * power)
57 # cpp_code = cpp_code.replace(f'pow(q, {power})', q_mult)
58
59 # Replace pow(base, n) with sham::pow_constexpr<n>(base)
60 # Use regex to match pow(anything, number) pattern
61 def replace_pow_with_constexpr(match):
62 base = match.group(1)
63 exponent = match.group(2)
64 return f"sham::pow_constexpr<{exponent}>({base})"
65
66 cpp_code = re.sub(r"pow\(([^,]+),\s*(\d+)\)", replace_pow_with_constexpr, cpp_code)
67
68 # Wrap divisions in parentheses for clarity
69 # Pattern: number/number followed by * (but not already in parentheses)
70 # Match something like: 1155.0/512.0*q -> (1155.0/512.0)*q
71 cpp_code = re.sub(r"(\d+\.?\d*)/(\d+\.?\d*)(\*)", r"(\1/\2)\3", cpp_code)
72
73 # Replace pi constants
74 cpp_code = replace_pi_constants(cpp_code)
75
76 return cpp_code
77
78
79 def process_power_variables(cpp_exprs, indent=8):
80 """
81 Self-contained power variable processing:
82 1. Extracts sham::pow_constexpr calls from expressions
83 2. Replaces them with temporary variable names (t1, t2, ...)
84 3. Generates header declarations
85
86 Returns: (modified_exprs, power_header_string)
87 """
88 spaces = " " * indent
89
90 # Find all sham::pow_constexpr<n>(...) calls
91 # Need to handle nested parentheses properly
92 def find_pow_constexpr_calls(text):
93 """Find all sham::pow_constexpr calls with proper parenthesis matching"""
94 calls = []
95 pattern = "sham::pow_constexpr<"
96 pos = 0
97
98 while True:
99 pos = text.find(pattern, pos)
100 if pos == -1:
101 break
102
103 # Find the power number
104 power_start = pos + len(pattern)
105 power_end = text.find(">", power_start)
106 power = text[power_start:power_end]
107
108 # Find the matching closing parenthesis
109 paren_start = text.find("(", power_end)
110 if paren_start == -1:
111 break
112
113 # Count parentheses to find the matching closing one
114 paren_count = 1
115 i = paren_start + 1
116 while i < len(text) and paren_count > 0:
117 if text[i] == "(":
118 paren_count += 1
119 elif text[i] == ")":
120 paren_count -= 1
121 i += 1
122
123 if paren_count == 0:
124 # Found matching closing parenthesis
125 full_match = text[pos:i]
126 base = text[paren_start + 1 : i - 1]
127 calls.append((full_match, power, base))
128 pos = i
129 else:
130 pos += 1
131
132 return calls
133
134 # Dictionary to store unique pow_constexpr calls
135 # Key: full match string, Value: (var_name, power, base)
136 pow_calls = {}
137 var_counter = 1
138
139 vars = {}
140
141 for expr in cpp_exprs:
142 for full_match, power, base in find_pow_constexpr_calls(expr):
143 vars[full_match] = (power, base)
144
145 # print(vars)
146
147 # sort them lexicographically by match (from the end of the key)
148 sorted_vars = sorted(vars.items(), key=lambda x: x[0])
149 for full_match, (power, base) in sorted_vars:
150 if full_match not in pow_calls:
151 var_name = f"t{var_counter}"
152 pow_calls[full_match] = (var_name, power, base)
153 var_counter += 1
154
155 # Replace pow_constexpr calls with variable names in expressions
156 # Sort by length (longest first) to avoid partial replacements
157 sorted_pow_calls = sorted(pow_calls.items(), key=lambda x: len(x[0]), reverse=True)
158 for full_match, (var_name, power, base) in sorted_pow_calls:
159 cpp_exprs = [expr.replace(full_match, var_name) for expr in cpp_exprs]
160
161 # Generate header declarations
162 power_header = ""
163 for full_match, (var_name, power, base) in pow_calls.items():
164 power_header += f"{spaces} Tscal {var_name} = sham::pow_constexpr<{power}>({base});\n"
165
166 return cpp_exprs, power_header
167
168
169 def generate_function_body(pieces, cpp_exprs, indent=8):
170 """Generate the body section with if-else conditions and return statements"""
171 spaces = " " * indent
172 body = ""
173
174 for i, ((expr, cond), cpp_expr) in enumerate(zip(pieces, cpp_exprs)):
175 if cond == True:
176 body += f"{spaces} else\n"
177 body += f"{spaces} return {cpp_expr};\n"
178 else:
179 if_keyword = "if" if i == 0 else "else if"
180 # Convert condition to string
181 cond_str = ccode(cond)
182
183 body += f"{spaces} {if_keyword} ({cond_str}) {{\n"
184 body += f"{spaces} return {cpp_expr};\n"
185 body += f"{spaces} }}"
186 if i < len(pieces) - 1 and pieces[i + 1][1] != True:
187 body += " "
188 else:
189 body += "\n"
190
191 return body
192
193
194 def generate_piecewise_function(func_expr, func_name, indent=8):
195 """
196 Generate C++ code for a piecewise function.
197 Works with separate header (declarations) and body (logic) sections.
198 """
199 spaces = " " * indent
200
201 # Extract pieces from Piecewise object
202 pieces = []
203 for arg in func_expr.args:
204 # Each arg is an ExprCondPair (expr, cond) tuple
205 if hasattr(arg, "expr") and hasattr(arg, "cond"):
206 pieces.append((arg.expr, arg.cond))
207 elif isinstance(arg, tuple) and len(arg) == 2:
208 pieces.append(arg)
209
210 # Phase 1: Generate raw C++ expressions for the body
211 cpp_exprs = []
212 for expr, _ in pieces:
213 cpp_exprs.append(sympy_to_cpp(expr))
214
215 # Phase 2: Process power variables (self-contained: detect, replace, generate header)
216 cpp_exprs, power_header = process_power_variables(cpp_exprs, indent)
217
218 # Phase 3: Extract constants and replace in body
219 # constants = extract_constants(cpp_exprs)
220 # cpp_exprs = replace_constants_in_body(cpp_exprs, constants)
221
222 # Phase 4: Generate constant declarations header
223 # const_header = generate_constant_header(constants, indent)
224
225 # Phase 5: Generate body (if-else structure)
226 body = generate_function_body(pieces, cpp_exprs, indent)
227
228 # Phase 6: Combine everything (constants first, then powers, then body)
229 code = f"{spaces}inline static Tscal {func_name}(Tscal q) {{\n"
230 # if const_header:
231 # code += const_header
232 if power_header:
233 code += power_header
234 code += body
235 code += f"{spaces}}}\n"
236
237 return code
238
239
240 def kernel_to_shamrock(kernel_gen):
241 """
242 Generate complete C++ kernel definition from SymPy expression
243
244 Args:
245 f_func: Function that returns (R, f_expr, name) tuple
246 generate_phi: Whether to generate phi_tilde_3d (requires manual derivation)
247 """
248 R, f_expr, name = kernel_gen()
249
250 # Compute normalization constants
251 c_norm_1d, c_norm_2d, c_norm_3d = getnorm(f_expr, R)
252
253 class text_body:
254 def __init__(self):
255 self.text = ""
256
257 def __call__(self, text=""):
258 self.text += text + "\n"
259
260 text = text_body()
261
262 # Generate class header
263 text("template<class Tscal>")
264 text(f"class KernelDef{name} {{")
265 text(" public:")
266 text(
267 f" inline static constexpr Tscal Rkern = {ccode(R)}; ///< Compact support radius of the kernel"
268 )
269 text(" /// default hfact to be used for this kernel")
270 text(" inline static constexpr Tscal hfactd = 1.2;")
271 text()
272
273 # Normalize constants with pi handling
274 norm_1d_str = replace_pi_constants(ccode(c_norm_1d))
275 norm_2d_str = replace_pi_constants(ccode(c_norm_2d))
276 norm_3d_str = replace_pi_constants(ccode(c_norm_3d))
277
278 text(" /// 1D norm of the kernel")
279 text(f" inline static constexpr Tscal norm_1d = {norm_1d_str};")
280 text(" /// 2D norm of the kernel")
281 text(f" inline static constexpr Tscal norm_2d = {norm_2d_str};")
282 text(" /// 3D norm of the kernel")
283 text(f" inline static constexpr Tscal norm_3d = {norm_3d_str};")
284 text()
285
286 from sympy.polys.polyfuncs import horner
287
288 # Generate f function
289 text(generate_piecewise_function(f_expr, "f", indent=4))
290 text()
291
292 # Generate df function
293 df_expr = diff(f_expr, q)
294 text(generate_piecewise_function(df_expr, "df", indent=4))
295
296 # Generate ddf function
297 ddf_expr = diff(df_expr, q)
298 text(generate_piecewise_function(ddf_expr, "ddf", indent=4))
299
300 # phi_expr
301 phi_tilde_3d_expr = integrate(integrate(f_expr * 4 * pi * q * q, q) * (1 / q**2), q).simplify()
302
303 # correct phi to have 0 at infinity
304 filter_cond = next(expr for expr, cond in phi_tilde_3d_expr.args if cond == True)
305 lim_phi = limit(filter_cond, q, oo)
306 phi_tilde_3d_expr -= lim_phi
307 phi_tilde_3d_expr = phi_tilde_3d_expr.simplify()
308
309 text(generate_piecewise_function(phi_tilde_3d_expr, "phi_tilde_3d", indent=4))
310
311 # phi_tilde_3d_prime
312 phi_tilde_3d_prime_expr = diff(phi_tilde_3d_expr, q)
313
314 text(generate_piecewise_function(phi_tilde_3d_prime_expr, "phi_tilde_3d_prime", indent=4))
315
316 text("};")
317 text()
318
319 return {
320 "R": R,
321 "name": name,
322 "norm_1d": c_norm_1d.evalf(),
323 "norm_2d": c_norm_2d.evalf(),
324 "norm_3d": c_norm_3d.evalf(),
325 "Rkern": R,
326 "f": f_expr,
327 "df": df_expr,
328 "ddf": ddf_expr,
329 "phi_tilde_3d": phi_tilde_3d_expr,
330 "phi_tilde_3d_prime": phi_tilde_3d_prime_expr,
331 "text": text.text,
332 }
Testing the kernels#
340 def test_kernel(ret, tolerance=1e-12):
341 R = ret["R"]
342 name = ret["name"]
343 norm_1d = ret["norm_1d"]
344 norm_2d = ret["norm_2d"]
345 norm_3d = ret["norm_3d"]
346 Rkern = ret["Rkern"]
347 f_expr = ret["f"]
348 df_expr = ret["df"]
349 ddf_expr = ret["ddf"]
350 phi_tilde_3d_expr = ret["phi_tilde_3d"]
351 phi_tilde_3d_prime_expr = ret["phi_tilde_3d_prime"]
352 text = ret["text"]
353
354 print("------------------------------------------")
355 print(f"Testing kernel {name} matching with Shamrock code")
356 print("------------------------------------------")
357
358 f = lambdify((q), f_expr, modules=["mpmath"])
359 df = lambdify((q), df_expr, modules=["mpmath"])
360 ddf = lambdify((q), ddf_expr, modules=["mpmath"])
361 phi_tilde_3d = lambdify((q), phi_tilde_3d_expr, modules=["mpmath"])
362 phi_tilde_3d_prime = lambdify((q), phi_tilde_3d_prime_expr, modules=["mpmath"])
363
364 print("Testing norms:")
365 shamrock_norm_1d = getattr(shamrock.math.sphkernel, f"{name}_norm_1d")()
366 shamrock_norm_2d = getattr(shamrock.math.sphkernel, f"{name}_norm_2d")()
367 shamrock_norm_3d = getattr(shamrock.math.sphkernel, f"{name}_norm_3d")()
368
369 print(
370 f"Shamrock norm_1d = {shamrock_norm_1d}, expected={norm_1d}, delta={abs(shamrock_norm_1d - norm_1d)}"
371 )
372 print(
373 f"Shamrock norm_2d = {shamrock_norm_2d}, expected={norm_2d}, delta={abs(shamrock_norm_2d - norm_2d)}"
374 )
375 print(
376 f"Shamrock norm_3d = {shamrock_norm_3d}, expected={norm_3d}, delta={abs(shamrock_norm_3d - norm_3d)}"
377 )
378
379 # test the norm constants down to 1e-12
380 assert abs(shamrock_norm_1d - norm_1d) < tolerance
381 assert abs(shamrock_norm_2d - norm_2d) < tolerance
382 assert abs(shamrock_norm_3d - norm_3d) < tolerance
383
384 print()
385 print("Testing kernel radius:")
386 shamrock_Rkern = getattr(shamrock.math.sphkernel, f"{name}_Rkern")()
387
388 print(f"Shamrock Rkern = {shamrock_Rkern}, delta={abs(shamrock_Rkern - Rkern)}")
389 assert abs(shamrock_Rkern - Rkern) < tolerance
390
391 print()
392 print("Testing kernel functions:")
393 shamrock_f = getattr(shamrock.math.sphkernel, f"{name}_f")
394 shamrock_df = getattr(shamrock.math.sphkernel, f"{name}_df")
395 shamrock_ddf = getattr(shamrock.math.sphkernel, f"{name}_ddf")
396 shamrock_phi_tilde_3d = getattr(shamrock.math.sphkernel, f"{name}_phi_tilde_3d")
397 shamrock_phi_tilde_3d_prime = getattr(shamrock.math.sphkernel, f"{name}_phi_tilde_3d_prime")
398
399 print(f"Shamrock f(q) = {shamrock_f}")
400 print(f"Shamrock df(q) = {shamrock_df}")
401 print(f"Shamrock ddf(q) = {shamrock_ddf}")
402 print(f"Shamrock phi_tilde_3d(q) = {shamrock_phi_tilde_3d}")
403 print(f"Shamrock phi_tilde_3d_prime(q) = {shamrock_phi_tilde_3d_prime}")
404
405 q_arr = np.linspace(0, max(1.1 * float(Rkern), 5.0), 1000)
406 shamrock_f = [shamrock_f(x) for x in q_arr]
407 shamrock_df = [shamrock_df(x) for x in q_arr]
408 shamrock_ddf = [shamrock_ddf(x) for x in q_arr]
409 shamrock_phi_tilde_3d = [shamrock_phi_tilde_3d(x) for x in q_arr]
410 shamrock_phi_tilde_3d_prime = [shamrock_phi_tilde_3d_prime(x) for x in q_arr]
411
412 try:
413 sympy_f = [f(x) for x in q_arr]
414 sympy_df = [df(x) for x in q_arr]
415 sympy_ddf = [ddf(x) for x in q_arr]
416 sympy_phi_tilde_3d = [phi_tilde_3d(x) for x in q_arr]
417 sympy_phi_tilde_3d_prime = [phi_tilde_3d_prime(x) for x in q_arr]
418 except ZeroDivisionError:
419 # This can happen for some kernels at q=0
420 print("ZeroDivisionError: using limits towards 0")
421 q_arr_no_zero = q_arr[1:]
422
423 # Compute the limits as q approaches 0
424 limit_f = float(limit(f_expr, q, 0))
425 limit_df = float(limit(df_expr, q, 0))
426 limit_ddf = float(limit(ddf_expr, q, 0))
427 limit_phi_tilde_3d = float(limit(phi_tilde_3d_expr, q, 0))
428 limit_phi_tilde_3d_prime = float(limit(phi_tilde_3d_prime_expr, q, 0))
429
430 print(f"limit_f = {limit_f} | shamrock_f[0] = {shamrock_f[0]}")
431 print(f"limit_df = {limit_df} | shamrock_df[0] = {shamrock_df[0]}")
432 print(f"limit_ddf = {limit_ddf} | shamrock_ddf[0] = {shamrock_ddf[0]}")
433 print(
434 f"limit_phi_tilde_3d = {limit_phi_tilde_3d} | shamrock_phi_tilde_3d[0] = {shamrock_phi_tilde_3d[0]}"
435 )
436 print(
437 f"limit_phi_tilde_3d_prime = {limit_phi_tilde_3d_prime} | shamrock_phi_tilde_3d_prime[0] = {shamrock_phi_tilde_3d_prime[0]}"
438 )
439
440 assert abs(limit_f - shamrock_f[0]) < tolerance
441 assert abs(limit_df - shamrock_df[0]) < tolerance
442 assert abs(limit_ddf - shamrock_ddf[0]) < tolerance
443 assert abs(limit_phi_tilde_3d - shamrock_phi_tilde_3d[0]) < tolerance
444 assert abs(limit_phi_tilde_3d_prime - shamrock_phi_tilde_3d_prime[0]) < tolerance
445
446 # Compute values for q > 0 and insert limits at the beginning
447 sympy_f = [limit_f] + [f(x) for x in q_arr_no_zero]
448 sympy_df = [limit_df] + [df(x) for x in q_arr_no_zero]
449 sympy_ddf = [limit_ddf] + [ddf(x) for x in q_arr_no_zero]
450 sympy_phi_tilde_3d = [limit_phi_tilde_3d] + [phi_tilde_3d(x) for x in q_arr_no_zero]
451 sympy_phi_tilde_3d_prime = [limit_phi_tilde_3d_prime] + [
452 phi_tilde_3d_prime(x) for x in q_arr_no_zero
453 ]
454
455 # compute the absolute error
456 abs_err_f = np.max(np.abs(np.array(shamrock_f) - np.array(sympy_f)))
457 abs_err_df = np.max(np.abs(np.array(shamrock_df) - np.array(sympy_df)))
458 abs_err_ddf = np.max(np.abs(np.array(shamrock_ddf) - np.array(sympy_ddf)))
459 abs_err_phi_tilde_3d = np.max(
460 np.abs(np.array(shamrock_phi_tilde_3d) - np.array(sympy_phi_tilde_3d))
461 )
462 abs_err_phi_tilde_3d_prime = np.max(
463 np.abs(np.array(shamrock_phi_tilde_3d_prime) - np.array(sympy_phi_tilde_3d_prime))
464 )
465
466 print(f"Absolute error f(q) = {abs_err_f}")
467 print(f"Absolute error df(q) = {abs_err_df}")
468 print(f"Absolute error ddf(q) = {abs_err_ddf}")
469 print(f"Absolute error phi_tilde_3d(q) = {abs_err_phi_tilde_3d}")
470 print(f"Absolute error phi_tilde_3d_prime(q) = {abs_err_phi_tilde_3d_prime}")
471
472 assert abs_err_f < tolerance
473 assert abs_err_df < tolerance * 10
474 assert abs_err_ddf < tolerance * 100
475 assert abs_err_phi_tilde_3d < tolerance * 100
476 assert abs_err_phi_tilde_3d_prime < tolerance * 1000
477
478 print("------------------------------------------")
479 print("")
480
481 return {
482 "q_arr": q_arr,
483 "shamrock_Cf": np.array(shamrock_f) * norm_3d,
484 "shamrock_Cdf": np.array(shamrock_df) * norm_3d,
485 "shamrock_Cddf": np.array(shamrock_ddf) * norm_3d,
486 "shamrock_Cphi_tilde_3d": np.array(shamrock_phi_tilde_3d) * norm_3d,
487 "shamrock_Cphi_tilde_3d_prime": np.array(shamrock_phi_tilde_3d_prime) * norm_3d,
488 }
489
490
491 def print_kernel_info(ret):
492 print("------------------------------------------")
493 print(f"Sympy expression for kernel {ret['name']}")
494 print("------------------------------------------")
495 print(f"f(q) = {ret['f']}")
496 print(f"df(q) = {ret['df']}")
497 print(f"ddf(q) = {ret['ddf']}")
498 print(f"phi_tilde_3d(q) = {ret['phi_tilde_3d']}")
499 print(f"phi_tilde_3d_prime(q) = {ret['phi_tilde_3d_prime']}")
500 print("------------------------------------------")
501 print("")
502
503
504 def print_kernel_cpp_code(ret):
505 print("------------------------------------------")
506 print(f"C++ generated code for kernel {ret['name']}")
507 print("------------------------------------------")
508 print(ret["text"])
509 print("------------------------------------------")
510 print("")
All the SPH kernels#
Cubic splines kernels (M-series)
520 def m4():
521 Rkern = 2
522 R = sympify(Rkern)
523 f = Piecewise(
524 (sympify(1) / 4 * (R - q) ** 3 - (R / 2 - q) ** 3, q < R / 2),
525 (sympify(1) / 4 * (R - q) ** 3, q < R),
526 (0, True),
527 )
528 return (R, f, "M4")
529
530
531 def m5():
532 Rkern = sympify(5) / 2
533 R = Rkern
534 term1 = (R - q) ** 4
535 term2 = -5 * (sympify(3) / 5 * R - q) ** 4
536 term3 = 10 * (sympify(1) / 5 * R - q) ** 4
537 f = Piecewise(
538 (term1 + term2 + term3, q < sympify(1) / 5 * R),
539 (term1 + term2, q < sympify(3) / 5 * R),
540 (term1, q < R),
541 (0, True),
542 )
543 return (R, f, "M5")
544
545
546 def m6():
547 Rkern = 3
548 R = sympify(Rkern)
549 term1 = (R - q) ** 5
550 term2 = -6 * (sympify(2) / 3 * R - q) ** 5
551 term3 = 15 * (sympify(1) / 3 * R - q) ** 5
552 f = Piecewise(
553 (term1 + term2 + term3, q < sympify(1) / 3 * R),
554 (term1 + term2, q < sympify(2) / 3 * R),
555 (term1, q < R),
556 (0, True),
557 )
558 return (R, f, "M6")
559
560
561 def m7():
562 Rkern = sympify(7) / 2
563 R = Rkern
564 term1 = (R - q) ** 6
565 term2 = -7 * (sympify(5) / 7 * R - q) ** 6
566 term3 = 21 * (sympify(3) / 7 * R - q) ** 6
567 term4 = -35 * (sympify(1) / 7 * R - q) ** 6
568 f = Piecewise(
569 (term1 + term2 + term3 + term4, q < sympify(1) / 7 * R),
570 (term1 + term2 + term3, q < sympify(3) / 7 * R),
571 (term1 + term2, q < sympify(5) / 7 * R),
572 (term1, q < R),
573 (0, True),
574 )
575 return (R, f, "M7")
576
577
578 def m8():
579 Rkern = 4
580 R = sympify(Rkern)
581 term1 = (4 - q) ** 7
582 term2 = -8 * (3 - q) ** 7
583 term3 = 28 * (2 - q) ** 7
584 term4 = -56 * (1 - q) ** 7
585 f = Piecewise(
586 (term1 + term2 + term3 + term4, q < 1),
587 (term1 + term2 + term3, q < 2),
588 (term1 + term2, q < 3),
589 (term1, q < R),
590 (0, True),
591 )
592 return (R, f, "M8")
593
594
595 def m9():
596 Rkern = sympify(9) / 2
597 R = Rkern
598 term1 = (R - q) ** 8
599 term2 = -9 * (sympify(7) / 9 * R - q) ** 8
600 term3 = 36 * (sympify(5) / 9 * R - q) ** 8
601 term4 = -84 * (sympify(3) / 9 * R - q) ** 8
602 term5 = 126 * (sympify(1) / 9 * R - q) ** 8
603 f = Piecewise(
604 (term1 + term2 + term3 + term4 + term5, q < sympify(1) / 9 * R),
605 (term1 + term2 + term3 + term4, q < sympify(3) / 9 * R),
606 (term1 + term2 + term3, q < sympify(5) / 9 * R),
607 (term1 + term2, q < sympify(7) / 9 * R),
608 (term1, q < R),
609 (0, True),
610 )
611 return (R, f, "M9")
612
613
614 def m10():
615 Rkern = 5
616 R = sympify(Rkern)
617 term1 = (R - q) ** 9
618 term2 = -10 * (sympify(4) / 5 * R - q) ** 9
619 term3 = 45 * (sympify(3) / 5 * R - q) ** 9
620 term4 = -120 * (sympify(2) / 5 * R - q) ** 9
621 term5 = 210 * (sympify(1) / 5 * R - q) ** 9
622 f = Piecewise(
623 (term1 + term2 + term3 + term4 + term5, q < sympify(1) / 5 * R),
624 (term1 + term2 + term3 + term4, q < sympify(2) / 5 * R),
625 (term1 + term2 + term3, q < sympify(3) / 5 * R),
626 (term1 + term2, q < sympify(4) / 5 * R),
627 (term1, q < R),
628 (0, True),
629 )
630 return (R, f, "M10")
Wendland kernels (C-series)
635 def c2():
636 Rkern = 2
637 R = sympify(Rkern)
638 f = Piecewise(((1 - q / 2) ** 4 * (1 + 2 * q), q < R), (0, True))
639 return (R, f, "C2")
640
641
642 def c4():
643 Rkern = 2
644 R = sympify(Rkern)
645 f = Piecewise(((1 - q / 2) ** 6 * (1 + 3 * q + sympify(35) / 12 * q**2), q < R), (0, True))
646 return (R, f, "C4")
647
648
649 def c6():
650 Rkern = 2
651 R = sympify(Rkern)
652 f = Piecewise(
653 ((1 - q / 2) ** 8 * (1 + 4 * q + sympify(25) / 4 * q**2 + 4 * q**3), q < R), (0, True)
654 )
655 return (R, f, "C6")
Helper function to multiply into piecewise without expanding
660 def multiply_piecewise(piecewise_expr, factor):
661 """Multiply a factor into each piece of a Piecewise expression without expanding"""
662 new_args = []
663 for arg in piecewise_expr.args:
664 if hasattr(arg, "expr") and hasattr(arg, "cond"):
665 # Multiply the expression part, keep condition unchanged
666 new_expr = factor * arg.expr
667 new_args.append((new_expr, arg.cond))
668 elif isinstance(arg, tuple) and len(arg) == 2:
669 new_expr = factor * arg[0]
670 new_args.append((new_expr, arg[1]))
671
672 return Piecewise(*new_args)
Helper function to shift and scale a kernel
677 def shift_scale_kernel(piecewise_expr, shift_val, scale_val):
678 return piecewise_fold(
679 Piecewise(
680 (1, q < shift_val),
681 (piecewise_expr.subs({q: (q - shift_val) * scale_val}), q >= shift_val),
682 )
683 )
Double hump
688 def m4dh():
689 R, f, _ = m4()
690 f = multiply_piecewise(f, q * q)
691 return (R, f, "M4DH")
692
693
694 def m4dh3():
695 R, f, _ = m4()
696 f = multiply_piecewise(f, q * q * q)
697 return (R, f, "M4DH3")
698
699
700 def m4dh5():
701 R, f, _ = m4()
702 f = multiply_piecewise(f, q * q * q * q * q)
703 return (R, f, "M4DH5")
704
705
706 def m4dh7():
707 R, f, _ = m4()
708 f = multiply_piecewise(f, q * q * q * q * q * q * q)
709 return (R, f, "M4DH7")
M4Shift kernels
714 def m4shift2():
715 R, f, _ = m4()
716 # For q < 1: return 1
717 # For q >= 1: return M4((q - 1) * 2)
718 f_shifted = shift_scale_kernel(f, shift_val=1, scale_val=2)
719 return (R, f_shifted, "M4Shift2")
720
721
722 def m4shift4():
723 R, f, _ = m4()
724 # For q < 1.5: return 1
725 # For q >= 1.5: return M4((q - 1.5) * 4)
726 f_shifted = shift_scale_kernel(f, shift_val=sympify(3) / 2, scale_val=4)
727 return (R, f_shifted, "M4Shift4")
728
729
730 def m4shift8():
731 R, f, _ = m4()
732 # For q < 1.75: return 1
733 # For q >= 1.75: return M4((q - 1.75) * 8)
734 f_shifted = shift_scale_kernel(f, shift_val=sympify(7) / 4, scale_val=8)
735 return (R, f_shifted, "M4Shift8")
736
737
738 def m4shift16():
739 R, f, _ = m4()
740 # For q < 1.875: return 1
741 # For q >= 1.875: return M4((q - 1.875) * 16)
742 f_shifted = shift_scale_kernel(f, shift_val=sympify(15) / 8, scale_val=16)
743 return (R, f_shifted, "M4Shift16")
TGauss3 kernel (gaussian until q=3)
748 def tgauss3():
749 R = 3
750 f = Piecewise(
751 (exp(-q * q), q < R),
752 (0, True),
753 )
754 return (R, f, "TGauss3")
TGauss5 kernel
759 def tgauss5():
760 R = 5
761 f = Piecewise(
762 (exp(-q * q), q < R),
763 (0, True),
764 )
765 return (R, f, "TGauss5")
M4 Kernel#
Generate c++ code for the kernel
775 ret = kernel_to_shamrock(m4)
778 print_kernel_info(ret)
------------------------------------------
Sympy expression for kernel M4
------------------------------------------
f(q) = Piecewise((-(1 - q)**3 + (2 - q)**3/4, q < 1), ((2 - q)**3/4, q < 2), (0, True))
df(q) = Piecewise((3*(1 - q)**2 - 3*(2 - q)**2/4, q < 1), (-3*(2 - q)**2/4, q < 2), (0, True))
ddf(q) = Piecewise((9*q/2 - 3, q < 1), (3 - 3*q/2, q < 2), (0, True))
phi_tilde_3d(q) = Piecewise((pi*(q**2*(3*q**3 - 9*q**2 + 20) - 42)/30, q < 1), (pi*(-q**6 + 9*q**5 - 30*q**4 + 40*q**3 - 48*q + 2)/(30*q), q < 2), (-pi/q, True))
phi_tilde_3d_prime(q) = Piecewise((pi*(q**2*(9*q**2 - 18*q) + 2*q*(3*q**3 - 9*q**2 + 20))/30, q < 1), (pi*(-6*q**5 + 45*q**4 - 120*q**3 + 120*q**2 - 48)/(30*q) - pi*(-q**6 + 9*q**5 - 30*q**4 + 40*q**3 - 48*q + 2)/(30*q**2), q < 2), (pi/q**2, True))
------------------------------------------
781 print_kernel_cpp_code(ret)
------------------------------------------
C++ generated code for kernel M4
------------------------------------------
template<class Tscal>
class KernelDefM4 {
public:
inline static constexpr Tscal Rkern = 2; ///< Compact support radius of the kernel
/// default hfact to be used for this kernel
inline static constexpr Tscal hfactd = 1.2;
/// 1D norm of the kernel
inline static constexpr Tscal norm_1d = 2.0/3.0;
/// 2D norm of the kernel
inline static constexpr Tscal norm_2d = (10.0/7.0)/shambase::constants::pi<Tscal>;
/// 3D norm of the kernel
inline static constexpr Tscal norm_3d = 1/shambase::constants::pi<Tscal>;
inline static Tscal f(Tscal q) {
Tscal t1 = sham::pow_constexpr<3>(1 - q);
Tscal t2 = sham::pow_constexpr<3>(2 - q);
if (q < 1) {
return -t1 + (1.0/4.0)*t2;
} else if (q < 2) {
return (1.0/4.0)*t2;
}
else
return 0;
}
inline static Tscal df(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(1 - q);
Tscal t2 = sham::pow_constexpr<2>(2 - q);
if (q < 1) {
return 3*t1 - (3.0/4.0)*t2;
} else if (q < 2) {
return -(3.0/4.0)*t2;
}
else
return 0;
}
inline static Tscal ddf(Tscal q) {
if (q < 1) {
return (9.0/2.0)*q - 3;
} else if (q < 2) {
return 3 - (3.0/2.0)*q;
}
else
return 0;
}
inline static Tscal phi_tilde_3d(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
Tscal t2 = sham::pow_constexpr<3>(q);
Tscal t3 = sham::pow_constexpr<4>(q);
Tscal t4 = sham::pow_constexpr<5>(q);
Tscal t5 = sham::pow_constexpr<6>(q);
if (q < 1) {
return (1.0/30.0)*shambase::constants::pi<Tscal>*(t1*(3*t2 - 9*t1 + 20) - 42);
} else if (q < 2) {
return (1.0/30.0)*shambase::constants::pi<Tscal>*(-t5 + 9*t4 - 30*t3 + 40*t2 - 48*q + 2)/q;
}
else
return -shambase::constants::pi<Tscal>/q;
}
inline static Tscal phi_tilde_3d_prime(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
Tscal t2 = sham::pow_constexpr<3>(q);
Tscal t3 = sham::pow_constexpr<4>(q);
Tscal t4 = sham::pow_constexpr<5>(q);
Tscal t5 = sham::pow_constexpr<6>(q);
if (q < 1) {
return (1.0/30.0)*shambase::constants::pi<Tscal>*(t1*(9*t1 - 18*q) + 2*q*(3*t2 - 9*t1 + 20));
} else if (q < 2) {
return (1.0/30.0)*shambase::constants::pi<Tscal>*(-6*t4 + 45*t3 - 120*t2 + 120*t1 - 48)/q - (1.0/30.0)*shambase::constants::pi<Tscal>*(-t5 + 9*t4 - 30*t3 + 40*t2 - 48*q + 2)/t1;
}
else
return shambase::constants::pi<Tscal>/t1;
}
};
------------------------------------------
Test the kernel
785 test_result_m4 = test_kernel(ret)
------------------------------------------
Testing kernel M4 matching with Shamrock code
------------------------------------------
Testing norms:
Shamrock norm_1d = 0.6666666666666666, expected=0.666666666666667, delta=0
Shamrock norm_2d = 0.4547284088339867, expected=0.454728408833987, delta=0
Shamrock norm_3d = 0.3183098861837907, expected=0.318309886183791, delta=0
Testing kernel radius:
Shamrock Rkern = 2.0, delta=0
Testing kernel functions:
Shamrock f(q) = <built-in method M4_f of PyCapsule object at 0x7fbcd9bbf750>
Shamrock df(q) = <built-in method M4_df of PyCapsule object at 0x7fbcd9bbf720>
Shamrock ddf(q) = <built-in method M4_ddf of PyCapsule object at 0x7fbcd9bbf6f0>
Shamrock phi_tilde_3d(q) = <built-in method M4_phi_tilde_3d of PyCapsule object at 0x7fbcd9bbf6c0>
Shamrock phi_tilde_3d_prime(q) = <built-in method M4_phi_tilde_3d_prime of PyCapsule object at 0x7fbcd9bbf690>
Absolute error f(q) = 3.3306690738754696212708950042725e-16
Absolute error df(q) = 6.6613381477509392425417900085449e-16
Absolute error ddf(q) = 2.7755575615628913510590791702271e-16
Absolute error phi_tilde_3d(q) = 6.1447719867801382157510209786384e-15
Absolute error phi_tilde_3d_prime(q) = 9.4052927422763077193482903413079e-15
------------------------------------------
M5 Kernel#
Generate c++ code for the kernel
794 ret = kernel_to_shamrock(m5)
797 print_kernel_info(ret)
------------------------------------------
Sympy expression for kernel M5
------------------------------------------
f(q) = Piecewise((10*(1/2 - q)**4 - 5*(3/2 - q)**4 + (5/2 - q)**4, q < 1/2), (-5*(3/2 - q)**4 + (5/2 - q)**4, q < 3/2), ((5/2 - q)**4, q < 5/2), (0, True))
df(q) = Piecewise((-40*(1/2 - q)**3 + 20*(3/2 - q)**3 - 4*(5/2 - q)**3, q < 1/2), (20*(3/2 - q)**3 - 4*(5/2 - q)**3, q < 3/2), (-4*(5/2 - q)**3, q < 5/2), (0, True))
ddf(q) = Piecewise((120*(1/2 - q)**2 - 60*(3/2 - q)**2 + 12*(5/2 - q)**2, q < 1/2), (-60*(3/2 - q)**2 + 12*(5/2 - q)**2, q < 3/2), (12*(5/2 - q)**2, q < 5/2), (0, True))
phi_tilde_3d(q) = Piecewise((pi*(192*q**6 - 1008*q**4 + 3220*q**2 - 8393)/336, q < 1/2), (pi*(-128*q**7 + 896*q**6 - 2016*q**5 + 560*q**4 + 3080*q**3 - 8386*q - 1)/(336*q), q < 3/2), (pi*(64*q**7 - 896*q**6 + 5040*q**5 - 14000*q**4 + 17500*q**3 - 21875*q + 2185)/(672*q), q < 5/2), (-20*pi/q, True))
phi_tilde_3d_prime(q) = Piecewise((pi*(1152*q**5 - 4032*q**3 + 6440*q)/336, q < 1/2), (pi*(-896*q**6 + 5376*q**5 - 10080*q**4 + 2240*q**3 + 9240*q**2 - 8386)/(336*q) - pi*(-128*q**7 + 896*q**6 - 2016*q**5 + 560*q**4 + 3080*q**3 - 8386*q - 1)/(336*q**2), q < 3/2), (pi*(448*q**6 - 5376*q**5 + 25200*q**4 - 56000*q**3 + 52500*q**2 - 21875)/(672*q) - pi*(64*q**7 - 896*q**6 + 5040*q**5 - 14000*q**4 + 17500*q**3 - 21875*q + 2185)/(672*q**2), q < 5/2), (20*pi/q**2, True))
------------------------------------------
800 print_kernel_cpp_code(ret)
------------------------------------------
C++ generated code for kernel M5
------------------------------------------
template<class Tscal>
class KernelDefM5 {
public:
inline static constexpr Tscal Rkern = 5.0/2.0; ///< Compact support radius of the kernel
/// default hfact to be used for this kernel
inline static constexpr Tscal hfactd = 1.2;
/// 1D norm of the kernel
inline static constexpr Tscal norm_1d = 1.0/24.0;
/// 2D norm of the kernel
inline static constexpr Tscal norm_2d = (96.0/1199.0)/shambase::constants::pi<Tscal>;
/// 3D norm of the kernel
inline static constexpr Tscal norm_3d = (1.0/20.0)/shambase::constants::pi<Tscal>;
inline static Tscal f(Tscal q) {
Tscal t1 = sham::pow_constexpr<4>(1.0/2.0 - q);
Tscal t2 = sham::pow_constexpr<4>(3.0/2.0 - q);
Tscal t3 = sham::pow_constexpr<4>(5.0/2.0 - q);
if (q < 1.0/2.0) {
return 10*t1 - 5*t2 + t3;
} else if (q < 3.0/2.0) {
return -5*t2 + t3;
} else if (q < 5.0/2.0) {
return t3;
}
else
return 0;
}
inline static Tscal df(Tscal q) {
Tscal t1 = sham::pow_constexpr<3>(1.0/2.0 - q);
Tscal t2 = sham::pow_constexpr<3>(3.0/2.0 - q);
Tscal t3 = sham::pow_constexpr<3>(5.0/2.0 - q);
if (q < 1.0/2.0) {
return -40*t1 + 20*t2 - 4*t3;
} else if (q < 3.0/2.0) {
return 20*t2 - 4*t3;
} else if (q < 5.0/2.0) {
return -4*t3;
}
else
return 0;
}
inline static Tscal ddf(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(1.0/2.0 - q);
Tscal t2 = sham::pow_constexpr<2>(3.0/2.0 - q);
Tscal t3 = sham::pow_constexpr<2>(5.0/2.0 - q);
if (q < 1.0/2.0) {
return 120*t1 - 60*t2 + 12*t3;
} else if (q < 3.0/2.0) {
return -60*t2 + 12*t3;
} else if (q < 5.0/2.0) {
return 12*t3;
}
else
return 0;
}
inline static Tscal phi_tilde_3d(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
Tscal t2 = sham::pow_constexpr<3>(q);
Tscal t3 = sham::pow_constexpr<4>(q);
Tscal t4 = sham::pow_constexpr<5>(q);
Tscal t5 = sham::pow_constexpr<6>(q);
Tscal t6 = sham::pow_constexpr<7>(q);
if (q < 1.0/2.0) {
return (1.0/336.0)*shambase::constants::pi<Tscal>*(192*t5 - 1008*t3 + 3220*t1 - 8393);
} else if (q < 3.0/2.0) {
return (1.0/336.0)*shambase::constants::pi<Tscal>*(-128*t6 + 896*t5 - 2016*t4 + 560*t3 + 3080*t2 - 8386*q - 1)/q;
} else if (q < 5.0/2.0) {
return (1.0/672.0)*shambase::constants::pi<Tscal>*(64*t6 - 896*t5 + 5040*t4 - 14000*t3 + 17500*t2 - 21875*q + 2185)/q;
}
else
return -20*shambase::constants::pi<Tscal>/q;
}
inline static Tscal phi_tilde_3d_prime(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
Tscal t2 = sham::pow_constexpr<3>(q);
Tscal t3 = sham::pow_constexpr<4>(q);
Tscal t4 = sham::pow_constexpr<5>(q);
Tscal t5 = sham::pow_constexpr<6>(q);
Tscal t6 = sham::pow_constexpr<7>(q);
if (q < 1.0/2.0) {
return (1.0/336.0)*shambase::constants::pi<Tscal>*(1152*t4 - 4032*t2 + 6440*q);
} else if (q < 3.0/2.0) {
return (1.0/336.0)*shambase::constants::pi<Tscal>*(-896*t5 + 5376*t4 - 10080*t3 + 2240*t2 + 9240*t1 - 8386)/q - (1.0/336.0)*shambase::constants::pi<Tscal>*(-128*t6 + 896*t5 - 2016*t4 + 560*t3 + 3080*t2 - 8386*q - 1)/t1;
} else if (q < 5.0/2.0) {
return (1.0/672.0)*shambase::constants::pi<Tscal>*(448*t5 - 5376*t4 + 25200*t3 - 56000*t2 + 52500*t1 - 21875)/q - (1.0/672.0)*shambase::constants::pi<Tscal>*(64*t6 - 896*t5 + 5040*t4 - 14000*t3 + 17500*t2 - 21875*q + 2185)/t1;
}
else
return 20*shambase::constants::pi<Tscal>/t1;
}
};
------------------------------------------
Test the kernel
804 test_result_m5 = test_kernel(ret)
------------------------------------------
Testing kernel M5 matching with Shamrock code
------------------------------------------
Testing norms:
Shamrock norm_1d = 0.041666666666666664, expected=0.0416666666666667, delta=0
Shamrock norm_2d = 0.025486029252413597, expected=0.0254860292524136, delta=0
Shamrock norm_3d = 0.015915494309189534, expected=0.0159154943091895, delta=0
Testing kernel radius:
Shamrock Rkern = 2.5, delta=0
Testing kernel functions:
Shamrock f(q) = <built-in method M5_f of PyCapsule object at 0x7fbcd9bbecd0>
Shamrock df(q) = <built-in method M5_df of PyCapsule object at 0x7fbcd9bbeca0>
Shamrock ddf(q) = <built-in method M5_ddf of PyCapsule object at 0x7fbcd9bbec70>
Shamrock phi_tilde_3d(q) = <built-in method M5_phi_tilde_3d of PyCapsule object at 0x7fbcd9bbec40>
Shamrock phi_tilde_3d_prime(q) = <built-in method M5_phi_tilde_3d_prime of PyCapsule object at 0x7fbcd9bbec10>
Absolute error f(q) = 1.1020960353947708389243550387522e-14
Absolute error df(q) = 1.6987646535410197641121131341036e-14
Absolute error ddf(q) = 2.8918153501785600372766502424727e-14
Absolute error phi_tilde_3d(q) = 2.3924264058170384135427132230976e-13
Absolute error phi_tilde_3d_prime(q) = 5.1144045415903564046637237745082e-13
------------------------------------------
M6 Kernel#
Generate c++ code for the kernel
814 ret = kernel_to_shamrock(m6)
817 print_kernel_info(ret)
------------------------------------------
Sympy expression for kernel M6
------------------------------------------
f(q) = Piecewise((15*(1 - q)**5 - 6*(2 - q)**5 + (3 - q)**5, q < 1), (-6*(2 - q)**5 + (3 - q)**5, q < 2), ((3 - q)**5, q < 3), (0, True))
df(q) = Piecewise((-75*(1 - q)**4 + 30*(2 - q)**4 - 5*(3 - q)**4, q < 1), (30*(2 - q)**4 - 5*(3 - q)**4, q < 2), (-5*(3 - q)**4, q < 3), (0, True))
ddf(q) = Piecewise((300*(1 - q)**3 - 120*(2 - q)**3 + 20*(3 - q)**3, q < 1), (-120*(2 - q)**3 + 20*(3 - q)**3, q < 2), (20*(3 - q)**3, q < 3), (0, True))
phi_tilde_3d(q) = Piecewise((pi*(-5*q**7 + 20*q**6 - 84*q**4 + 308*q**2 - 956)/7, q < 1), (pi*(5*q**8 - 60*q**7 + 280*q**6 - 588*q**5 + 350*q**4 + 476*q**3 - 1892*q - 5)/(14*q), q < 2), (pi*(-q**8 + 20*q**7 - 168*q**6 + 756*q**5 - 1890*q**4 + 2268*q**3 - 2916*q + 507)/(14*q), q < 3), (-120*pi/q, True))
phi_tilde_3d_prime(q) = Piecewise((pi*(-35*q**6 + 120*q**5 - 336*q**3 + 616*q)/7, q < 1), (pi*(40*q**7 - 420*q**6 + 1680*q**5 - 2940*q**4 + 1400*q**3 + 1428*q**2 - 1892)/(14*q) - pi*(5*q**8 - 60*q**7 + 280*q**6 - 588*q**5 + 350*q**4 + 476*q**3 - 1892*q - 5)/(14*q**2), q < 2), (pi*(-8*q**7 + 140*q**6 - 1008*q**5 + 3780*q**4 - 7560*q**3 + 6804*q**2 - 2916)/(14*q) - pi*(-q**8 + 20*q**7 - 168*q**6 + 756*q**5 - 1890*q**4 + 2268*q**3 - 2916*q + 507)/(14*q**2), q < 3), (120*pi/q**2, True))
------------------------------------------
820 print_kernel_cpp_code(ret)
------------------------------------------
C++ generated code for kernel M6
------------------------------------------
template<class Tscal>
class KernelDefM6 {
public:
inline static constexpr Tscal Rkern = 3; ///< Compact support radius of the kernel
/// default hfact to be used for this kernel
inline static constexpr Tscal hfactd = 1.2;
/// 1D norm of the kernel
inline static constexpr Tscal norm_1d = 1.0/120.0;
/// 2D norm of the kernel
inline static constexpr Tscal norm_2d = (7.0/478.0)/shambase::constants::pi<Tscal>;
/// 3D norm of the kernel
inline static constexpr Tscal norm_3d = (1.0/120.0)/shambase::constants::pi<Tscal>;
inline static Tscal f(Tscal q) {
Tscal t1 = sham::pow_constexpr<5>(1 - q);
Tscal t2 = sham::pow_constexpr<5>(2 - q);
Tscal t3 = sham::pow_constexpr<5>(3 - q);
if (q < 1) {
return 15*t1 - 6*t2 + t3;
} else if (q < 2) {
return -6*t2 + t3;
} else if (q < 3) {
return t3;
}
else
return 0;
}
inline static Tscal df(Tscal q) {
Tscal t1 = sham::pow_constexpr<4>(1 - q);
Tscal t2 = sham::pow_constexpr<4>(2 - q);
Tscal t3 = sham::pow_constexpr<4>(3 - q);
if (q < 1) {
return -75*t1 + 30*t2 - 5*t3;
} else if (q < 2) {
return 30*t2 - 5*t3;
} else if (q < 3) {
return -5*t3;
}
else
return 0;
}
inline static Tscal ddf(Tscal q) {
Tscal t1 = sham::pow_constexpr<3>(1 - q);
Tscal t2 = sham::pow_constexpr<3>(2 - q);
Tscal t3 = sham::pow_constexpr<3>(3 - q);
if (q < 1) {
return 300*t1 - 120*t2 + 20*t3;
} else if (q < 2) {
return -120*t2 + 20*t3;
} else if (q < 3) {
return 20*t3;
}
else
return 0;
}
inline static Tscal phi_tilde_3d(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
Tscal t2 = sham::pow_constexpr<3>(q);
Tscal t3 = sham::pow_constexpr<4>(q);
Tscal t4 = sham::pow_constexpr<5>(q);
Tscal t5 = sham::pow_constexpr<6>(q);
Tscal t6 = sham::pow_constexpr<7>(q);
Tscal t7 = sham::pow_constexpr<8>(q);
if (q < 1) {
return (1.0/7.0)*shambase::constants::pi<Tscal>*(-5*t6 + 20*t5 - 84*t3 + 308*t1 - 956);
} else if (q < 2) {
return (1.0/14.0)*shambase::constants::pi<Tscal>*(5*t7 - 60*t6 + 280*t5 - 588*t4 + 350*t3 + 476*t2 - 1892*q - 5)/q;
} else if (q < 3) {
return (1.0/14.0)*shambase::constants::pi<Tscal>*(-t7 + 20*t6 - 168*t5 + 756*t4 - 1890*t3 + 2268*t2 - 2916*q + 507)/q;
}
else
return -120*shambase::constants::pi<Tscal>/q;
}
inline static Tscal phi_tilde_3d_prime(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
Tscal t2 = sham::pow_constexpr<3>(q);
Tscal t3 = sham::pow_constexpr<4>(q);
Tscal t4 = sham::pow_constexpr<5>(q);
Tscal t5 = sham::pow_constexpr<6>(q);
Tscal t6 = sham::pow_constexpr<7>(q);
Tscal t7 = sham::pow_constexpr<8>(q);
if (q < 1) {
return (1.0/7.0)*shambase::constants::pi<Tscal>*(-35*t5 + 120*t4 - 336*t2 + 616*q);
} else if (q < 2) {
return (1.0/14.0)*shambase::constants::pi<Tscal>*(40*t6 - 420*t5 + 1680*t4 - 2940*t3 + 1400*t2 + 1428*t1 - 1892)/q - (1.0/14.0)*shambase::constants::pi<Tscal>*(5*t7 - 60*t6 + 280*t5 - 588*t4 + 350*t3 + 476*t2 - 1892*q - 5)/t1;
} else if (q < 3) {
return (1.0/14.0)*shambase::constants::pi<Tscal>*(-8*t6 + 140*t5 - 1008*t4 + 3780*t3 - 7560*t2 + 6804*t1 - 2916)/q - (1.0/14.0)*shambase::constants::pi<Tscal>*(-t7 + 20*t6 - 168*t5 + 756*t4 - 1890*t3 + 2268*t2 - 2916*q + 507)/t1;
}
else
return 120*shambase::constants::pi<Tscal>/t1;
}
};
------------------------------------------
Test the kernel
824 test_result_m6 = test_kernel(ret)
------------------------------------------
Testing kernel M6 matching with Shamrock code
------------------------------------------
Testing norms:
Shamrock norm_1d = 0.008333333333333333, expected=0.00833333333333333, delta=0
Shamrock norm_2d = 0.00466144184787978, expected=0.00466144184787978, delta=0
Shamrock norm_3d = 0.0026525823848649226, expected=0.00265258238486492, delta=4.33680868994202E-19
Testing kernel radius:
Shamrock Rkern = 3.0, delta=0
Testing kernel functions:
Shamrock f(q) = <built-in method M6_f of PyCapsule object at 0x7fbcd9bbe9d0>
Shamrock df(q) = <built-in method M6_df of PyCapsule object at 0x7fbcd9bbe9a0>
Shamrock ddf(q) = <built-in method M6_ddf of PyCapsule object at 0x7fbcd9bbe970>
Shamrock phi_tilde_3d(q) = <built-in method M6_phi_tilde_3d of PyCapsule object at 0x7fbcd9bbe940>
Shamrock phi_tilde_3d_prime(q) = <built-in method M6_phi_tilde_3d_prime of PyCapsule object at 0x7fbcd9bbe910>
Absolute error f(q) = 7.105427357601002e-14
Absolute error df(q) = 1.4921397450962104e-13
Absolute error ddf(q) = 2.2737367544323206e-13
Absolute error phi_tilde_3d(q) = 3.4086883168235832315012898375205e-12
Absolute error phi_tilde_3d_prime(q) = 4.3695323848306376217663142727157e-12
------------------------------------------
M7 Kernel#
Generate c++ code for the kernel
834 ret = kernel_to_shamrock(m7)
837 print_kernel_info(ret)
------------------------------------------
Sympy expression for kernel M7
------------------------------------------
f(q) = Piecewise((-35*(1/2 - q)**6 + 21*(3/2 - q)**6 - 7*(5/2 - q)**6 + (7/2 - q)**6, q < 1/2), (21*(3/2 - q)**6 - 7*(5/2 - q)**6 + (7/2 - q)**6, q < 3/2), (-7*(5/2 - q)**6 + (7/2 - q)**6, q < 5/2), ((7/2 - q)**6, q < 7/2), (0, True))
df(q) = Piecewise((210*(1/2 - q)**5 - 126*(3/2 - q)**5 + 42*(5/2 - q)**5 - 6*(7/2 - q)**5, q < 1/2), (-126*(3/2 - q)**5 + 42*(5/2 - q)**5 - 6*(7/2 - q)**5, q < 3/2), (42*(5/2 - q)**5 - 6*(7/2 - q)**5, q < 5/2), (-6*(7/2 - q)**5, q < 7/2), (0, True))
ddf(q) = Piecewise((-1050*(1/2 - q)**4 + 630*(3/2 - q)**4 - 210*(5/2 - q)**4 + 30*(7/2 - q)**4, q < 1/2), (630*(3/2 - q)**4 - 210*(5/2 - q)**4 + 30*(7/2 - q)**4, q < 3/2), (-210*(5/2 - q)**4 + 30*(7/2 - q)**4, q < 5/2), (30*(7/2 - q)**4, q < 7/2), (0, True))
phi_tilde_3d(q) = Piecewise((pi*(-1280*q**8 + 11520*q**6 - 66528*q**4 + 282576*q**2 - 1018341)/1152, q < 1/2), (pi*(3840*q**9 - 34560*q**8 + 103680*q**7 - 53760*q**6 - 235872*q**5 - 10080*q**4 + 1131984*q**3 - 4073409*q + 5)/(4608*q), q < 3/2), (pi*(-768*q**9 + 13824*q**8 - 103680*q**7 + 408576*q**6 - 852768*q**5 + 729792*q**4 + 198576*q**3 - 1948131*q - 29522)/(2304*q), q < 5/2), (pi*(256*q**9 - 6912*q**8 + 80640*q**7 - 526848*q**6 + 2074464*q**5 - 4840416*q**4 + 5647152*q**3 - 7411887*q + 1894081)/(4608*q), q < 7/2), (-840*pi/q, True))
phi_tilde_3d_prime(q) = Piecewise((pi*(-10240*q**7 + 69120*q**5 - 266112*q**3 + 565152*q)/1152, q < 1/2), (pi*(34560*q**8 - 276480*q**7 + 725760*q**6 - 322560*q**5 - 1179360*q**4 - 40320*q**3 + 3395952*q**2 - 4073409)/(4608*q) - pi*(3840*q**9 - 34560*q**8 + 103680*q**7 - 53760*q**6 - 235872*q**5 - 10080*q**4 + 1131984*q**3 - 4073409*q + 5)/(4608*q**2), q < 3/2), (pi*(-6912*q**8 + 110592*q**7 - 725760*q**6 + 2451456*q**5 - 4263840*q**4 + 2919168*q**3 + 595728*q**2 - 1948131)/(2304*q) - pi*(-768*q**9 + 13824*q**8 - 103680*q**7 + 408576*q**6 - 852768*q**5 + 729792*q**4 + 198576*q**3 - 1948131*q - 29522)/(2304*q**2), q < 5/2), (pi*(2304*q**8 - 55296*q**7 + 564480*q**6 - 3161088*q**5 + 10372320*q**4 - 19361664*q**3 + 16941456*q**2 - 7411887)/(4608*q) - pi*(256*q**9 - 6912*q**8 + 80640*q**7 - 526848*q**6 + 2074464*q**5 - 4840416*q**4 + 5647152*q**3 - 7411887*q + 1894081)/(4608*q**2), q < 7/2), (840*pi/q**2, True))
------------------------------------------
840 print_kernel_cpp_code(ret)
------------------------------------------
C++ generated code for kernel M7
------------------------------------------
template<class Tscal>
class KernelDefM7 {
public:
inline static constexpr Tscal Rkern = 7.0/2.0; ///< Compact support radius of the kernel
/// default hfact to be used for this kernel
inline static constexpr Tscal hfactd = 1.2;
/// 1D norm of the kernel
inline static constexpr Tscal norm_1d = 1.0/720.0;
/// 2D norm of the kernel
inline static constexpr Tscal norm_2d = (256.0/113149.0)/shambase::constants::pi<Tscal>;
/// 3D norm of the kernel
inline static constexpr Tscal norm_3d = (1.0/840.0)/shambase::constants::pi<Tscal>;
inline static Tscal f(Tscal q) {
Tscal t1 = sham::pow_constexpr<6>(1.0/2.0 - q);
Tscal t2 = sham::pow_constexpr<6>(3.0/2.0 - q);
Tscal t3 = sham::pow_constexpr<6>(5.0/2.0 - q);
Tscal t4 = sham::pow_constexpr<6>(7.0/2.0 - q);
if (q < 1.0/2.0) {
return -35*t1 + 21*t2 - 7*t3 + t4;
} else if (q < 3.0/2.0) {
return 21*t2 - 7*t3 + t4;
} else if (q < 5.0/2.0) {
return -7*t3 + t4;
} else if (q < 7.0/2.0) {
return t4;
}
else
return 0;
}
inline static Tscal df(Tscal q) {
Tscal t1 = sham::pow_constexpr<5>(1.0/2.0 - q);
Tscal t2 = sham::pow_constexpr<5>(3.0/2.0 - q);
Tscal t3 = sham::pow_constexpr<5>(5.0/2.0 - q);
Tscal t4 = sham::pow_constexpr<5>(7.0/2.0 - q);
if (q < 1.0/2.0) {
return 210*t1 - 126*t2 + 42*t3 - 6*t4;
} else if (q < 3.0/2.0) {
return -126*t2 + 42*t3 - 6*t4;
} else if (q < 5.0/2.0) {
return 42*t3 - 6*t4;
} else if (q < 7.0/2.0) {
return -6*t4;
}
else
return 0;
}
inline static Tscal ddf(Tscal q) {
Tscal t1 = sham::pow_constexpr<4>(1.0/2.0 - q);
Tscal t2 = sham::pow_constexpr<4>(3.0/2.0 - q);
Tscal t3 = sham::pow_constexpr<4>(5.0/2.0 - q);
Tscal t4 = sham::pow_constexpr<4>(7.0/2.0 - q);
if (q < 1.0/2.0) {
return -1050*t1 + 630*t2 - 210*t3 + 30*t4;
} else if (q < 3.0/2.0) {
return 630*t2 - 210*t3 + 30*t4;
} else if (q < 5.0/2.0) {
return -210*t3 + 30*t4;
} else if (q < 7.0/2.0) {
return 30*t4;
}
else
return 0;
}
inline static Tscal phi_tilde_3d(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
Tscal t2 = sham::pow_constexpr<3>(q);
Tscal t3 = sham::pow_constexpr<4>(q);
Tscal t4 = sham::pow_constexpr<5>(q);
Tscal t5 = sham::pow_constexpr<6>(q);
Tscal t6 = sham::pow_constexpr<7>(q);
Tscal t7 = sham::pow_constexpr<8>(q);
Tscal t8 = sham::pow_constexpr<9>(q);
if (q < 1.0/2.0) {
return (1.0/1152.0)*shambase::constants::pi<Tscal>*(-1280*t7 + 11520*t5 - 66528*t3 + 282576*t1 - 1018341);
} else if (q < 3.0/2.0) {
return (1.0/4608.0)*shambase::constants::pi<Tscal>*(3840*t8 - 34560*t7 + 103680*t6 - 53760*t5 - 235872*t4 - 10080*t3 + 1131984*t2 - 4073409*q + 5)/q;
} else if (q < 5.0/2.0) {
return (1.0/2304.0)*shambase::constants::pi<Tscal>*(-768*t8 + 13824*t7 - 103680*t6 + 408576*t5 - 852768*t4 + 729792*t3 + 198576*t2 - 1948131*q - 29522)/q;
} else if (q < 7.0/2.0) {
return (1.0/4608.0)*shambase::constants::pi<Tscal>*(256*t8 - 6912*t7 + 80640*t6 - 526848*t5 + 2074464*t4 - 4840416*t3 + 5647152*t2 - 7411887*q + 1894081)/q;
}
else
return -840*shambase::constants::pi<Tscal>/q;
}
inline static Tscal phi_tilde_3d_prime(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
Tscal t2 = sham::pow_constexpr<3>(q);
Tscal t3 = sham::pow_constexpr<4>(q);
Tscal t4 = sham::pow_constexpr<5>(q);
Tscal t5 = sham::pow_constexpr<6>(q);
Tscal t6 = sham::pow_constexpr<7>(q);
Tscal t7 = sham::pow_constexpr<8>(q);
Tscal t8 = sham::pow_constexpr<9>(q);
if (q < 1.0/2.0) {
return (1.0/1152.0)*shambase::constants::pi<Tscal>*(-10240*t6 + 69120*t4 - 266112*t2 + 565152*q);
} else if (q < 3.0/2.0) {
return (1.0/4608.0)*shambase::constants::pi<Tscal>*(34560*t7 - 276480*t6 + 725760*t5 - 322560*t4 - 1179360*t3 - 40320*t2 + 3395952*t1 - 4073409)/q - (1.0/4608.0)*shambase::constants::pi<Tscal>*(3840*t8 - 34560*t7 + 103680*t6 - 53760*t5 - 235872*t4 - 10080*t3 + 1131984*t2 - 4073409*q + 5)/t1;
} else if (q < 5.0/2.0) {
return (1.0/2304.0)*shambase::constants::pi<Tscal>*(-6912*t7 + 110592*t6 - 725760*t5 + 2451456*t4 - 4263840*t3 + 2919168*t2 + 595728*t1 - 1948131)/q - (1.0/2304.0)*shambase::constants::pi<Tscal>*(-768*t8 + 13824*t7 - 103680*t6 + 408576*t5 - 852768*t4 + 729792*t3 + 198576*t2 - 1948131*q - 29522)/t1;
} else if (q < 7.0/2.0) {
return (1.0/4608.0)*shambase::constants::pi<Tscal>*(2304*t7 - 55296*t6 + 564480*t5 - 3161088*t4 + 10372320*t3 - 19361664*t2 + 16941456*t1 - 7411887)/q - (1.0/4608.0)*shambase::constants::pi<Tscal>*(256*t8 - 6912*t7 + 80640*t6 - 526848*t5 + 2074464*t4 - 4840416*t3 + 5647152*t2 - 7411887*q + 1894081)/t1;
}
else
return 840*shambase::constants::pi<Tscal>/t1;
}
};
------------------------------------------
Test the kernel
844 test_result_m7 = test_kernel(ret, tolerance=1e-11)
------------------------------------------
Testing kernel M7 matching with Shamrock code
------------------------------------------
Testing norms:
Shamrock norm_1d = 0.001388888888888889, expected=0.00138888888888889, delta=0
Shamrock norm_2d = 0.0007201772076028106, expected=0.000720177207602811, delta=0
Shamrock norm_3d = 0.0003789403406949889, expected=0.000378940340694989, delta=0
Testing kernel radius:
Shamrock Rkern = 3.5, delta=0
Testing kernel functions:
Shamrock f(q) = <built-in method M7_f of PyCapsule object at 0x7fbcd9bbe6d0>
Shamrock df(q) = <built-in method M7_df of PyCapsule object at 0x7fbcd9bbe6a0>
Shamrock ddf(q) = <built-in method M7_ddf of PyCapsule object at 0x7fbcd9bbe670>
Shamrock phi_tilde_3d(q) = <built-in method M7_phi_tilde_3d of PyCapsule object at 0x7fbcd9bbe640>
Shamrock phi_tilde_3d_prime(q) = <built-in method M7_phi_tilde_3d_prime of PyCapsule object at 0x7fbcd9bbe610>
Absolute error f(q) = 7.1169918592659551989348027572597e-13
Absolute error df(q) = 1.5356443651156504336746027078175e-12
Absolute error ddf(q) = 2.5422845301193941132174144420942e-12
Absolute error phi_tilde_3d(q) = 5.033416387056549054525265560333e-11
Absolute error phi_tilde_3d_prime(q) = 9.1132909954461041619386612291864e-11
------------------------------------------
M8 Kernel#
Generate c++ code for the kernel
853 ret = kernel_to_shamrock(m8)
856 print_kernel_info(ret)
------------------------------------------
Sympy expression for kernel M8
------------------------------------------
f(q) = Piecewise((-56*(1 - q)**7 + 28*(2 - q)**7 - 8*(3 - q)**7 + (4 - q)**7, q < 1), (28*(2 - q)**7 - 8*(3 - q)**7 + (4 - q)**7, q < 2), (-8*(3 - q)**7 + (4 - q)**7, q < 3), ((4 - q)**7, q < 4), (0, True))
df(q) = Piecewise((392*(1 - q)**6 - 196*(2 - q)**6 + 56*(3 - q)**6 - 7*(4 - q)**6, q < 1), (-196*(2 - q)**6 + 56*(3 - q)**6 - 7*(4 - q)**6, q < 2), (56*(3 - q)**6 - 7*(4 - q)**6, q < 3), (-7*(4 - q)**6, q < 4), (0, True))
ddf(q) = Piecewise((-2352*(1 - q)**5 + 1176*(2 - q)**5 - 336*(3 - q)**5 + 42*(4 - q)**5, q < 1), (1176*(2 - q)**5 - 336*(3 - q)**5 + 42*(4 - q)**5, q < 2), (-336*(3 - q)**5 + 42*(4 - q)**5, q < 3), (42*(4 - q)**5, q < 4), (0, True))
phi_tilde_3d(q) = Piecewise((2*pi*(q**2*(7*q**7 - 35*q**6 + 240*q**4 - 1512*q**2 + 7248) - 29740)/9, q < 1), (2*pi*(-21*q**10 + 315*q**9 - 1890*q**8 + 5400*q**7 - 5880*q**6 - 2268*q**5 - 2940*q**4 + 37080*q**3 - 148770*q + 14)/(45*q), q < 2), (2*pi*(7*q**10 - 175*q**9 + 1890*q**8 - 11400*q**7 + 41160*q**6 - 86940*q**5 + 91140*q**4 - 16680*q**3 - 130850*q - 7154)/(45*q), q < 3), (2*pi*(-q**10 + 35*q**9 - 540*q**8 + 4800*q**7 - 26880*q**6 + 96768*q**5 - 215040*q**4 + 245760*q**3 - 327680*q + 110944)/(45*q), q < 4), (-6720*pi/q, True))
phi_tilde_3d_prime(q) = Piecewise((2*pi*(q**2*(49*q**6 - 210*q**5 + 960*q**3 - 3024*q) + 2*q*(7*q**7 - 35*q**6 + 240*q**4 - 1512*q**2 + 7248))/9, q < 1), (2*pi*(-210*q**9 + 2835*q**8 - 15120*q**7 + 37800*q**6 - 35280*q**5 - 11340*q**4 - 11760*q**3 + 111240*q**2 - 148770)/(45*q) - 2*pi*(-21*q**10 + 315*q**9 - 1890*q**8 + 5400*q**7 - 5880*q**6 - 2268*q**5 - 2940*q**4 + 37080*q**3 - 148770*q + 14)/(45*q**2), q < 2), (2*pi*(70*q**9 - 1575*q**8 + 15120*q**7 - 79800*q**6 + 246960*q**5 - 434700*q**4 + 364560*q**3 - 50040*q**2 - 130850)/(45*q) - 2*pi*(7*q**10 - 175*q**9 + 1890*q**8 - 11400*q**7 + 41160*q**6 - 86940*q**5 + 91140*q**4 - 16680*q**3 - 130850*q - 7154)/(45*q**2), q < 3), (2*pi*(-10*q**9 + 315*q**8 - 4320*q**7 + 33600*q**6 - 161280*q**5 + 483840*q**4 - 860160*q**3 + 737280*q**2 - 327680)/(45*q) - 2*pi*(-q**10 + 35*q**9 - 540*q**8 + 4800*q**7 - 26880*q**6 + 96768*q**5 - 215040*q**4 + 245760*q**3 - 327680*q + 110944)/(45*q**2), q < 4), (6720*pi/q**2, True))
------------------------------------------
859 print_kernel_cpp_code(ret)
------------------------------------------
C++ generated code for kernel M8
------------------------------------------
template<class Tscal>
class KernelDefM8 {
public:
inline static constexpr Tscal Rkern = 4; ///< Compact support radius of the kernel
/// default hfact to be used for this kernel
inline static constexpr Tscal hfactd = 1.2;
/// 1D norm of the kernel
inline static constexpr Tscal norm_1d = 1.0/5040.0;
/// 2D norm of the kernel
inline static constexpr Tscal norm_2d = (9.0/29740.0)/shambase::constants::pi<Tscal>;
/// 3D norm of the kernel
inline static constexpr Tscal norm_3d = (1.0/6720.0)/shambase::constants::pi<Tscal>;
inline static Tscal f(Tscal q) {
Tscal t1 = sham::pow_constexpr<7>(1 - q);
Tscal t2 = sham::pow_constexpr<7>(2 - q);
Tscal t3 = sham::pow_constexpr<7>(3 - q);
Tscal t4 = sham::pow_constexpr<7>(4 - q);
if (q < 1) {
return -56*t1 + 28*t2 - 8*t3 + t4;
} else if (q < 2) {
return 28*t2 - 8*t3 + t4;
} else if (q < 3) {
return -8*t3 + t4;
} else if (q < 4) {
return t4;
}
else
return 0;
}
inline static Tscal df(Tscal q) {
Tscal t1 = sham::pow_constexpr<6>(1 - q);
Tscal t2 = sham::pow_constexpr<6>(2 - q);
Tscal t3 = sham::pow_constexpr<6>(3 - q);
Tscal t4 = sham::pow_constexpr<6>(4 - q);
if (q < 1) {
return 392*t1 - 196*t2 + 56*t3 - 7*t4;
} else if (q < 2) {
return -196*t2 + 56*t3 - 7*t4;
} else if (q < 3) {
return 56*t3 - 7*t4;
} else if (q < 4) {
return -7*t4;
}
else
return 0;
}
inline static Tscal ddf(Tscal q) {
Tscal t1 = sham::pow_constexpr<5>(1 - q);
Tscal t2 = sham::pow_constexpr<5>(2 - q);
Tscal t3 = sham::pow_constexpr<5>(3 - q);
Tscal t4 = sham::pow_constexpr<5>(4 - q);
if (q < 1) {
return -2352*t1 + 1176*t2 - 336*t3 + 42*t4;
} else if (q < 2) {
return 1176*t2 - 336*t3 + 42*t4;
} else if (q < 3) {
return -336*t3 + 42*t4;
} else if (q < 4) {
return 42*t4;
}
else
return 0;
}
inline static Tscal phi_tilde_3d(Tscal q) {
Tscal t1 = sham::pow_constexpr<10>(q);
Tscal t2 = sham::pow_constexpr<2>(q);
Tscal t3 = sham::pow_constexpr<3>(q);
Tscal t4 = sham::pow_constexpr<4>(q);
Tscal t5 = sham::pow_constexpr<5>(q);
Tscal t6 = sham::pow_constexpr<6>(q);
Tscal t7 = sham::pow_constexpr<7>(q);
Tscal t8 = sham::pow_constexpr<8>(q);
Tscal t9 = sham::pow_constexpr<9>(q);
if (q < 1) {
return (2.0/9.0)*shambase::constants::pi<Tscal>*(t2*(7*t7 - 35*t6 + 240*t4 - 1512*t2 + 7248) - 29740);
} else if (q < 2) {
return (2.0/45.0)*shambase::constants::pi<Tscal>*(-21*t1 + 315*t9 - 1890*t8 + 5400*t7 - 5880*t6 - 2268*t5 - 2940*t4 + 37080*t3 - 148770*q + 14)/q;
} else if (q < 3) {
return (2.0/45.0)*shambase::constants::pi<Tscal>*(7*t1 - 175*t9 + 1890*t8 - 11400*t7 + 41160*t6 - 86940*t5 + 91140*t4 - 16680*t3 - 130850*q - 7154)/q;
} else if (q < 4) {
return (2.0/45.0)*shambase::constants::pi<Tscal>*(-t1 + 35*t9 - 540*t8 + 4800*t7 - 26880*t6 + 96768*t5 - 215040*t4 + 245760*t3 - 327680*q + 110944)/q;
}
else
return -6720*shambase::constants::pi<Tscal>/q;
}
inline static Tscal phi_tilde_3d_prime(Tscal q) {
Tscal t1 = sham::pow_constexpr<10>(q);
Tscal t2 = sham::pow_constexpr<2>(q);
Tscal t3 = sham::pow_constexpr<3>(q);
Tscal t4 = sham::pow_constexpr<4>(q);
Tscal t5 = sham::pow_constexpr<5>(q);
Tscal t6 = sham::pow_constexpr<6>(q);
Tscal t7 = sham::pow_constexpr<7>(q);
Tscal t8 = sham::pow_constexpr<8>(q);
Tscal t9 = sham::pow_constexpr<9>(q);
if (q < 1) {
return (2.0/9.0)*shambase::constants::pi<Tscal>*(t2*(49*t6 - 210*t5 + 960*t3 - 3024*q) + 2*q*(7*t7 - 35*t6 + 240*t4 - 1512*t2 + 7248));
} else if (q < 2) {
return (2.0/45.0)*shambase::constants::pi<Tscal>*(-210*t9 + 2835*t8 - 15120*t7 + 37800*t6 - 35280*t5 - 11340*t4 - 11760*t3 + 111240*t2 - 148770)/q - (2.0/45.0)*shambase::constants::pi<Tscal>*(-21*t1 + 315*t9 - 1890*t8 + 5400*t7 - 5880*t6 - 2268*t5 - 2940*t4 + 37080*t3 - 148770*q + 14)/t2;
} else if (q < 3) {
return (2.0/45.0)*shambase::constants::pi<Tscal>*(70*t9 - 1575*t8 + 15120*t7 - 79800*t6 + 246960*t5 - 434700*t4 + 364560*t3 - 50040*t2 - 130850)/q - (2.0/45.0)*shambase::constants::pi<Tscal>*(7*t1 - 175*t9 + 1890*t8 - 11400*t7 + 41160*t6 - 86940*t5 + 91140*t4 - 16680*t3 - 130850*q - 7154)/t2;
} else if (q < 4) {
return (2.0/45.0)*shambase::constants::pi<Tscal>*(-10*t9 + 315*t8 - 4320*t7 + 33600*t6 - 161280*t5 + 483840*t4 - 860160*t3 + 737280*t2 - 327680)/q - (2.0/45.0)*shambase::constants::pi<Tscal>*(-t1 + 35*t9 - 540*t8 + 4800*t7 - 26880*t6 + 96768*t5 - 215040*t4 + 245760*t3 - 327680*q + 110944)/t2;
}
else
return 6720*shambase::constants::pi<Tscal>/t2;
}
};
------------------------------------------
Test the kernel
863 test_result_m8 = test_kernel(ret, tolerance=1e-10)
------------------------------------------
Testing kernel M8 matching with Shamrock code
------------------------------------------
Testing norms:
Shamrock norm_1d = 0.0001984126984126984, expected=0.000198412698412698, delta=0
Shamrock norm_2d = 9.63278068478183e-05, expected=0.0000963278068478183, delta=0
Shamrock norm_3d = 4.736754258687362e-05, expected=0.0000473675425868736, delta=6.77626357803440E-21
Testing kernel radius:
Shamrock Rkern = 4.0, delta=0
Testing kernel functions:
Shamrock f(q) = <built-in method M8_f of PyCapsule object at 0x7fbcd9bbe3d0>
Shamrock df(q) = <built-in method M8_df of PyCapsule object at 0x7fbcd9bbe3a0>
Shamrock ddf(q) = <built-in method M8_ddf of PyCapsule object at 0x7fbcd9bbe370>
Shamrock phi_tilde_3d(q) = <built-in method M8_phi_tilde_3d of PyCapsule object at 0x7fbcd9bbe340>
Shamrock phi_tilde_3d_prime(q) = <built-in method M8_phi_tilde_3d_prime of PyCapsule object at 0x7fbcd9bbe310>
Absolute error f(q) = 5.002220859751105e-12
Absolute error df(q) = 1.1368683772161603e-11
Absolute error ddf(q) = 2.9103830456733704e-11
Absolute error phi_tilde_3d(q) = 0.000000001322561202158742498480693263775
Absolute error phi_tilde_3d_prime(q) = 0.0000000018193685467461683891335635391754
------------------------------------------
M9 Kernel#
Generate c++ code for the kernel
872 ret = kernel_to_shamrock(m9)
875 print_kernel_info(ret)
------------------------------------------
Sympy expression for kernel M9
------------------------------------------
f(q) = Piecewise((126*(1/2 - q)**8 - 84*(3/2 - q)**8 + 36*(5/2 - q)**8 - 9*(7/2 - q)**8 + (9/2 - q)**8, q < 1/2), (-84*(3/2 - q)**8 + 36*(5/2 - q)**8 - 9*(7/2 - q)**8 + (9/2 - q)**8, q < 3/2), (36*(5/2 - q)**8 - 9*(7/2 - q)**8 + (9/2 - q)**8, q < 5/2), (-9*(7/2 - q)**8 + (9/2 - q)**8, q < 7/2), ((9/2 - q)**8, q < 9/2), (0, True))
df(q) = Piecewise((-1008*(1/2 - q)**7 + 672*(3/2 - q)**7 - 288*(5/2 - q)**7 + 72*(7/2 - q)**7 - 8*(9/2 - q)**7, q < 1/2), (672*(3/2 - q)**7 - 288*(5/2 - q)**7 + 72*(7/2 - q)**7 - 8*(9/2 - q)**7, q < 3/2), (-288*(5/2 - q)**7 + 72*(7/2 - q)**7 - 8*(9/2 - q)**7, q < 5/2), (72*(7/2 - q)**7 - 8*(9/2 - q)**7, q < 7/2), (-8*(9/2 - q)**7, q < 9/2), (0, True))
ddf(q) = Piecewise((7056*(1/2 - q)**6 - 4704*(3/2 - q)**6 + 2016*(5/2 - q)**6 - 504*(7/2 - q)**6 + 56*(9/2 - q)**6, q < 1/2), (-4704*(3/2 - q)**6 + 2016*(5/2 - q)**6 - 504*(7/2 - q)**6 + 56*(9/2 - q)**6, q < 3/2), (2016*(5/2 - q)**6 - 504*(7/2 - q)**6 + 56*(9/2 - q)**6, q < 5/2), (-504*(7/2 - q)**6 + 56*(9/2 - q)**6, q < 7/2), (56*(9/2 - q)**6, q < 9/2), (0, True))
phi_tilde_3d(q) = Piecewise((pi*(7168*q**10 - 98560*q**8 + 908160*q**6 - 6408864*q**4 + 34283436*q**2 - 157802293)/2816, q < 1/2), (pi*(-28672*q**11 + 315392*q**10 - 1182720*q**9 + 887040*q**8 + 3801600*q**7 + 413952*q**6 - 32199552*q**5 + 36960*q**4 + 171412560*q**3 - 789011388*q - 7)/(14080*q), q < 3/2), (pi*(14336*q**11 - 315392*q**10 + 2956800*q**9 - 15079680*q**8 + 43718400*q**7 - 66646272*q**6 + 43243200*q**5 - 53850720*q**4 + 191620440*q**3 - 792042570*q + 826679)/(14080*q), q < 5/2), (pi*(-4096*q**11 + 135168*q**10 - 1971200*q**9 + 16600320*q**8 - 88281600*q**7 + 302953728*q**6 - 649756800*q**5 + 771149280*q**4 - 324004560*q**3 - 577198820*q - 96829571)/(14080*q), q < 7/2), (pi*(1024*q**11 - 45056*q**10 + 887040*q**9 - 10264320*q**8 + 76982400*q**7 - 387991296*q**6 + 1309470624*q**5 - 2806008480*q**4 + 3156759540*q**3 - 4261625379*q + 1783667601)/(28160*q), q < 9/2), (-60480*pi/q, True))
phi_tilde_3d_prime(q) = Piecewise((pi*(71680*q**9 - 788480*q**7 + 5448960*q**5 - 25635456*q**3 + 68566872*q)/2816, q < 1/2), (pi*(-315392*q**10 + 3153920*q**9 - 10644480*q**8 + 7096320*q**7 + 26611200*q**6 + 2483712*q**5 - 160997760*q**4 + 147840*q**3 + 514237680*q**2 - 789011388)/(14080*q) - pi*(-28672*q**11 + 315392*q**10 - 1182720*q**9 + 887040*q**8 + 3801600*q**7 + 413952*q**6 - 32199552*q**5 + 36960*q**4 + 171412560*q**3 - 789011388*q - 7)/(14080*q**2), q < 3/2), (pi*(157696*q**10 - 3153920*q**9 + 26611200*q**8 - 120637440*q**7 + 306028800*q**6 - 399877632*q**5 + 216216000*q**4 - 215402880*q**3 + 574861320*q**2 - 792042570)/(14080*q) - pi*(14336*q**11 - 315392*q**10 + 2956800*q**9 - 15079680*q**8 + 43718400*q**7 - 66646272*q**6 + 43243200*q**5 - 53850720*q**4 + 191620440*q**3 - 792042570*q + 826679)/(14080*q**2), q < 5/2), (pi*(-45056*q**10 + 1351680*q**9 - 17740800*q**8 + 132802560*q**7 - 617971200*q**6 + 1817722368*q**5 - 3248784000*q**4 + 3084597120*q**3 - 972013680*q**2 - 577198820)/(14080*q) - pi*(-4096*q**11 + 135168*q**10 - 1971200*q**9 + 16600320*q**8 - 88281600*q**7 + 302953728*q**6 - 649756800*q**5 + 771149280*q**4 - 324004560*q**3 - 577198820*q - 96829571)/(14080*q**2), q < 7/2), (pi*(11264*q**10 - 450560*q**9 + 7983360*q**8 - 82114560*q**7 + 538876800*q**6 - 2327947776*q**5 + 6547353120*q**4 - 11224033920*q**3 + 9470278620*q**2 - 4261625379)/(28160*q) - pi*(1024*q**11 - 45056*q**10 + 887040*q**9 - 10264320*q**8 + 76982400*q**7 - 387991296*q**6 + 1309470624*q**5 - 2806008480*q**4 + 3156759540*q**3 - 4261625379*q + 1783667601)/(28160*q**2), q < 9/2), (60480*pi/q**2, True))
------------------------------------------
878 print_kernel_cpp_code(ret)
------------------------------------------
C++ generated code for kernel M9
------------------------------------------
template<class Tscal>
class KernelDefM9 {
public:
inline static constexpr Tscal Rkern = 9.0/2.0; ///< Compact support radius of the kernel
/// default hfact to be used for this kernel
inline static constexpr Tscal hfactd = 1.2;
/// 1D norm of the kernel
inline static constexpr Tscal norm_1d = 1.0/40320.0;
/// 2D norm of the kernel
inline static constexpr Tscal norm_2d = (512.0/14345663.0)/shambase::constants::pi<Tscal>;
/// 3D norm of the kernel
inline static constexpr Tscal norm_3d = (1.0/60480.0)/shambase::constants::pi<Tscal>;
inline static Tscal f(Tscal q) {
Tscal t1 = sham::pow_constexpr<8>(1.0/2.0 - q);
Tscal t2 = sham::pow_constexpr<8>(3.0/2.0 - q);
Tscal t3 = sham::pow_constexpr<8>(5.0/2.0 - q);
Tscal t4 = sham::pow_constexpr<8>(7.0/2.0 - q);
Tscal t5 = sham::pow_constexpr<8>(9.0/2.0 - q);
if (q < 1.0/2.0) {
return 126*t1 - 84*t2 + 36*t3 - 9*t4 + t5;
} else if (q < 3.0/2.0) {
return -84*t2 + 36*t3 - 9*t4 + t5;
} else if (q < 5.0/2.0) {
return 36*t3 - 9*t4 + t5;
} else if (q < 7.0/2.0) {
return -9*t4 + t5;
} else if (q < 9.0/2.0) {
return t5;
}
else
return 0;
}
inline static Tscal df(Tscal q) {
Tscal t1 = sham::pow_constexpr<7>(1.0/2.0 - q);
Tscal t2 = sham::pow_constexpr<7>(3.0/2.0 - q);
Tscal t3 = sham::pow_constexpr<7>(5.0/2.0 - q);
Tscal t4 = sham::pow_constexpr<7>(7.0/2.0 - q);
Tscal t5 = sham::pow_constexpr<7>(9.0/2.0 - q);
if (q < 1.0/2.0) {
return -1008*t1 + 672*t2 - 288*t3 + 72*t4 - 8*t5;
} else if (q < 3.0/2.0) {
return 672*t2 - 288*t3 + 72*t4 - 8*t5;
} else if (q < 5.0/2.0) {
return -288*t3 + 72*t4 - 8*t5;
} else if (q < 7.0/2.0) {
return 72*t4 - 8*t5;
} else if (q < 9.0/2.0) {
return -8*t5;
}
else
return 0;
}
inline static Tscal ddf(Tscal q) {
Tscal t1 = sham::pow_constexpr<6>(1.0/2.0 - q);
Tscal t2 = sham::pow_constexpr<6>(3.0/2.0 - q);
Tscal t3 = sham::pow_constexpr<6>(5.0/2.0 - q);
Tscal t4 = sham::pow_constexpr<6>(7.0/2.0 - q);
Tscal t5 = sham::pow_constexpr<6>(9.0/2.0 - q);
if (q < 1.0/2.0) {
return 7056*t1 - 4704*t2 + 2016*t3 - 504*t4 + 56*t5;
} else if (q < 3.0/2.0) {
return -4704*t2 + 2016*t3 - 504*t4 + 56*t5;
} else if (q < 5.0/2.0) {
return 2016*t3 - 504*t4 + 56*t5;
} else if (q < 7.0/2.0) {
return -504*t4 + 56*t5;
} else if (q < 9.0/2.0) {
return 56*t5;
}
else
return 0;
}
inline static Tscal phi_tilde_3d(Tscal q) {
Tscal t1 = sham::pow_constexpr<10>(q);
Tscal t2 = sham::pow_constexpr<11>(q);
Tscal t3 = sham::pow_constexpr<2>(q);
Tscal t4 = sham::pow_constexpr<3>(q);
Tscal t5 = sham::pow_constexpr<4>(q);
Tscal t6 = sham::pow_constexpr<5>(q);
Tscal t7 = sham::pow_constexpr<6>(q);
Tscal t8 = sham::pow_constexpr<7>(q);
Tscal t9 = sham::pow_constexpr<8>(q);
Tscal t10 = sham::pow_constexpr<9>(q);
if (q < 1.0/2.0) {
return (1.0/2816.0)*shambase::constants::pi<Tscal>*(7168*t1 - 98560*t9 + 908160*t7 - 6408864*t5 + 34283436*t3 - 157802293);
} else if (q < 3.0/2.0) {
return (1.0/14080.0)*shambase::constants::pi<Tscal>*(-28672*t2 + 315392*t1 - 1182720*t10 + 887040*t9 + 3801600*t8 + 413952*t7 - 32199552*t6 + 36960*t5 + 171412560*t4 - 789011388*q - 7)/q;
} else if (q < 5.0/2.0) {
return (1.0/14080.0)*shambase::constants::pi<Tscal>*(14336*t2 - 315392*t1 + 2956800*t10 - 15079680*t9 + 43718400*t8 - 66646272*t7 + 43243200*t6 - 53850720*t5 + 191620440*t4 - 792042570*q + 826679)/q;
} else if (q < 7.0/2.0) {
return (1.0/14080.0)*shambase::constants::pi<Tscal>*(-4096*t2 + 135168*t1 - 1971200*t10 + 16600320*t9 - 88281600*t8 + 302953728*t7 - 649756800*t6 + 771149280*t5 - 324004560*t4 - 577198820*q - 96829571)/q;
} else if (q < 9.0/2.0) {
return (1.0/28160.0)*shambase::constants::pi<Tscal>*(1024*t2 - 45056*t1 + 887040*t10 - 10264320*t9 + 76982400*t8 - 387991296*t7 + 1309470624*t6 - 2806008480*t5 + 3156759540*t4 - 4261625379*q + 1783667601)/q;
}
else
return -60480*shambase::constants::pi<Tscal>/q;
}
inline static Tscal phi_tilde_3d_prime(Tscal q) {
Tscal t1 = sham::pow_constexpr<10>(q);
Tscal t2 = sham::pow_constexpr<11>(q);
Tscal t3 = sham::pow_constexpr<2>(q);
Tscal t4 = sham::pow_constexpr<3>(q);
Tscal t5 = sham::pow_constexpr<4>(q);
Tscal t6 = sham::pow_constexpr<5>(q);
Tscal t7 = sham::pow_constexpr<6>(q);
Tscal t8 = sham::pow_constexpr<7>(q);
Tscal t9 = sham::pow_constexpr<8>(q);
Tscal t10 = sham::pow_constexpr<9>(q);
if (q < 1.0/2.0) {
return (1.0/2816.0)*shambase::constants::pi<Tscal>*(71680*t10 - 788480*t8 + 5448960*t6 - 25635456*t4 + 68566872*q);
} else if (q < 3.0/2.0) {
return (1.0/14080.0)*shambase::constants::pi<Tscal>*(-315392*t1 + 3153920*t10 - 10644480*t9 + 7096320*t8 + 26611200*t7 + 2483712*t6 - 160997760*t5 + 147840*t4 + 514237680*t3 - 789011388)/q - (1.0/14080.0)*shambase::constants::pi<Tscal>*(-28672*t2 + 315392*t1 - 1182720*t10 + 887040*t9 + 3801600*t8 + 413952*t7 - 32199552*t6 + 36960*t5 + 171412560*t4 - 789011388*q - 7)/t3;
} else if (q < 5.0/2.0) {
return (1.0/14080.0)*shambase::constants::pi<Tscal>*(157696*t1 - 3153920*t10 + 26611200*t9 - 120637440*t8 + 306028800*t7 - 399877632*t6 + 216216000*t5 - 215402880*t4 + 574861320*t3 - 792042570)/q - (1.0/14080.0)*shambase::constants::pi<Tscal>*(14336*t2 - 315392*t1 + 2956800*t10 - 15079680*t9 + 43718400*t8 - 66646272*t7 + 43243200*t6 - 53850720*t5 + 191620440*t4 - 792042570*q + 826679)/t3;
} else if (q < 7.0/2.0) {
return (1.0/14080.0)*shambase::constants::pi<Tscal>*(-45056*t1 + 1351680*t10 - 17740800*t9 + 132802560*t8 - 617971200*t7 + 1817722368*t6 - 3248784000*t5 + 3084597120*t4 - 972013680*t3 - 577198820)/q - (1.0/14080.0)*shambase::constants::pi<Tscal>*(-4096*t2 + 135168*t1 - 1971200*t10 + 16600320*t9 - 88281600*t8 + 302953728*t7 - 649756800*t6 + 771149280*t5 - 324004560*t4 - 577198820*q - 96829571)/t3;
} else if (q < 9.0/2.0) {
return (1.0/28160.0)*shambase::constants::pi<Tscal>*(11264*t1 - 450560*t10 + 7983360*t9 - 82114560*t8 + 538876800*t7 - 2327947776*t6 + 6547353120*t5 - 11224033920*t4 + 9470278620*t3 - 4261625379)/q - (1.0/28160.0)*shambase::constants::pi<Tscal>*(1024*t2 - 45056*t1 + 887040*t10 - 10264320*t9 + 76982400*t8 - 387991296*t7 + 1309470624*t6 - 2806008480*t5 + 3156759540*t4 - 4261625379*q + 1783667601)/t3;
}
else
return 60480*shambase::constants::pi<Tscal>/t3;
}
};
------------------------------------------
Test the kernel
882 test_result_m9 = test_kernel(ret, tolerance=1e-9)
------------------------------------------
Testing kernel M9 matching with Shamrock code
------------------------------------------
Testing norms:
Shamrock norm_1d = 2.48015873015873e-05, expected=0.0000248015873015873, delta=0
Shamrock norm_2d = 1.1360552783520763e-05, expected=0.0000113605527835208, delta=0
Shamrock norm_3d = 5.263060287430401e-06, expected=0.00000526306028743040, delta=0
Testing kernel radius:
Shamrock Rkern = 4.5, delta=0
Testing kernel functions:
Shamrock f(q) = <built-in method M9_f of PyCapsule object at 0x7fbcd9bbe0d0>
Shamrock df(q) = <built-in method M9_df of PyCapsule object at 0x7fbcd9bbe0a0>
Shamrock ddf(q) = <built-in method M9_ddf of PyCapsule object at 0x7fbcd9bbe070>
Shamrock phi_tilde_3d(q) = <built-in method M9_phi_tilde_3d of PyCapsule object at 0x7fbcd9bbe040>
Shamrock phi_tilde_3d_prime(q) = <built-in method M9_phi_tilde_3d_prime of PyCapsule object at 0x7fbcd9bbe010>
Absolute error f(q) = 1.3116584471113417810615033274653e-10
Absolute error df(q) = 1.6072615378338547344603540902465e-10
Absolute error ddf(q) = 2.873446049366578385242747112273e-10
Absolute error phi_tilde_3d(q) = 0.000000020706113693881974910981485323116
Absolute error phi_tilde_3d_prime(q) = 0.000000039433226222822340380178224869235
------------------------------------------
M10 Kernel#
Generate c++ code for the kernel
891 ret = kernel_to_shamrock(m10)
894 print_kernel_info(ret)
------------------------------------------
Sympy expression for kernel M10
------------------------------------------
f(q) = Piecewise((210*(1 - q)**9 - 120*(2 - q)**9 + 45*(3 - q)**9 - 10*(4 - q)**9 + (5 - q)**9, q < 1), (-120*(2 - q)**9 + 45*(3 - q)**9 - 10*(4 - q)**9 + (5 - q)**9, q < 2), (45*(3 - q)**9 - 10*(4 - q)**9 + (5 - q)**9, q < 3), (-10*(4 - q)**9 + (5 - q)**9, q < 4), ((5 - q)**9, q < 5), (0, True))
df(q) = Piecewise((-1890*(1 - q)**8 + 1080*(2 - q)**8 - 405*(3 - q)**8 + 90*(4 - q)**8 - 9*(5 - q)**8, q < 1), (1080*(2 - q)**8 - 405*(3 - q)**8 + 90*(4 - q)**8 - 9*(5 - q)**8, q < 2), (-405*(3 - q)**8 + 90*(4 - q)**8 - 9*(5 - q)**8, q < 3), (90*(4 - q)**8 - 9*(5 - q)**8, q < 4), (-9*(5 - q)**8, q < 5), (0, True))
ddf(q) = Piecewise((15120*(1 - q)**7 - 8640*(2 - q)**7 + 3240*(3 - q)**7 - 720*(4 - q)**7 + 72*(5 - q)**7, q < 1), (-8640*(2 - q)**7 + 3240*(3 - q)**7 - 720*(4 - q)**7 + 72*(5 - q)**7, q < 2), (3240*(3 - q)**7 - 720*(4 - q)**7 + 72*(5 - q)**7, q < 3), (-720*(4 - q)**7 + 72*(5 - q)**7, q < 4), (72*(5 - q)**7, q < 5), (0, True))
phi_tilde_3d(q) = Piecewise((2*pi*(-63*q**11 + 378*q**10 - 3850*q**8 + 37620*q**6 - 291060*q**4 + 1718090*q**2 - 8766690)/33, q < 1), (2*pi*(42*q**12 - 756*q**11 + 5544*q**10 - 20020*q**9 + 31185*q**8 - 3960*q**7 + 38808*q**6 - 316008*q**5 + 10395*q**4 + 1715780*q**3 - 8766564*q - 21)/(33*q), q < 2), (2*pi*(-18*q**12 + 540*q**11 - 7128*q**10 + 53900*q**9 - 253935*q**8 + 756360*q**7 - 1380456*q**6 + 1508760*q**5 - 1510245*q**4 + 2391620*q**3 - 8914020*q + 49131)/(33*q), q < 3), (pi*(9*q**12 - 378*q**11 + 7128*q**10 - 79310*q**9 + 574695*q**8 - 2817540*q**7 + 9363816*q**6 - 20365884*q**5 + 26208765*q**4 - 14702930*q**3 - 8262102*q - 4684707)/(33*q), q < 4), (pi*(-q**12 + 54*q**11 - 1320*q**10 + 19250*q**9 - 185625*q**8 + 1237500*q**7 - 5775000*q**6 + 18562500*q**5 - 38671875*q**4 + 42968750*q**3 - 58593750*q + 28869725)/(33*q), q < 5), (-604800*pi/q, True))
phi_tilde_3d_prime(q) = Piecewise((2*pi*(-693*q**10 + 3780*q**9 - 30800*q**7 + 225720*q**5 - 1164240*q**3 + 3436180*q)/33, q < 1), (2*pi*(504*q**11 - 8316*q**10 + 55440*q**9 - 180180*q**8 + 249480*q**7 - 27720*q**6 + 232848*q**5 - 1580040*q**4 + 41580*q**3 + 5147340*q**2 - 8766564)/(33*q) - 2*pi*(42*q**12 - 756*q**11 + 5544*q**10 - 20020*q**9 + 31185*q**8 - 3960*q**7 + 38808*q**6 - 316008*q**5 + 10395*q**4 + 1715780*q**3 - 8766564*q - 21)/(33*q**2), q < 2), (2*pi*(-216*q**11 + 5940*q**10 - 71280*q**9 + 485100*q**8 - 2031480*q**7 + 5294520*q**6 - 8282736*q**5 + 7543800*q**4 - 6040980*q**3 + 7174860*q**2 - 8914020)/(33*q) - 2*pi*(-18*q**12 + 540*q**11 - 7128*q**10 + 53900*q**9 - 253935*q**8 + 756360*q**7 - 1380456*q**6 + 1508760*q**5 - 1510245*q**4 + 2391620*q**3 - 8914020*q + 49131)/(33*q**2), q < 3), (pi*(108*q**11 - 4158*q**10 + 71280*q**9 - 713790*q**8 + 4597560*q**7 - 19722780*q**6 + 56182896*q**5 - 101829420*q**4 + 104835060*q**3 - 44108790*q**2 - 8262102)/(33*q) - pi*(9*q**12 - 378*q**11 + 7128*q**10 - 79310*q**9 + 574695*q**8 - 2817540*q**7 + 9363816*q**6 - 20365884*q**5 + 26208765*q**4 - 14702930*q**3 - 8262102*q - 4684707)/(33*q**2), q < 4), (pi*(-12*q**11 + 594*q**10 - 13200*q**9 + 173250*q**8 - 1485000*q**7 + 8662500*q**6 - 34650000*q**5 + 92812500*q**4 - 154687500*q**3 + 128906250*q**2 - 58593750)/(33*q) - pi*(-q**12 + 54*q**11 - 1320*q**10 + 19250*q**9 - 185625*q**8 + 1237500*q**7 - 5775000*q**6 + 18562500*q**5 - 38671875*q**4 + 42968750*q**3 - 58593750*q + 28869725)/(33*q**2), q < 5), (604800*pi/q**2, True))
------------------------------------------
897 print_kernel_cpp_code(ret)
------------------------------------------
C++ generated code for kernel M10
------------------------------------------
template<class Tscal>
class KernelDefM10 {
public:
inline static constexpr Tscal Rkern = 5; ///< Compact support radius of the kernel
/// default hfact to be used for this kernel
inline static constexpr Tscal hfactd = 1.2;
/// 1D norm of the kernel
inline static constexpr Tscal norm_1d = 1.0/362880.0;
/// 2D norm of the kernel
inline static constexpr Tscal norm_2d = (11.0/2922230.0)/shambase::constants::pi<Tscal>;
/// 3D norm of the kernel
inline static constexpr Tscal norm_3d = (1.0/604800.0)/shambase::constants::pi<Tscal>;
inline static Tscal f(Tscal q) {
Tscal t1 = sham::pow_constexpr<9>(1 - q);
Tscal t2 = sham::pow_constexpr<9>(2 - q);
Tscal t3 = sham::pow_constexpr<9>(3 - q);
Tscal t4 = sham::pow_constexpr<9>(4 - q);
Tscal t5 = sham::pow_constexpr<9>(5 - q);
if (q < 1) {
return 210*t1 - 120*t2 + 45*t3 - 10*t4 + t5;
} else if (q < 2) {
return -120*t2 + 45*t3 - 10*t4 + t5;
} else if (q < 3) {
return 45*t3 - 10*t4 + t5;
} else if (q < 4) {
return -10*t4 + t5;
} else if (q < 5) {
return t5;
}
else
return 0;
}
inline static Tscal df(Tscal q) {
Tscal t1 = sham::pow_constexpr<8>(1 - q);
Tscal t2 = sham::pow_constexpr<8>(2 - q);
Tscal t3 = sham::pow_constexpr<8>(3 - q);
Tscal t4 = sham::pow_constexpr<8>(4 - q);
Tscal t5 = sham::pow_constexpr<8>(5 - q);
if (q < 1) {
return -1890*t1 + 1080*t2 - 405*t3 + 90*t4 - 9*t5;
} else if (q < 2) {
return 1080*t2 - 405*t3 + 90*t4 - 9*t5;
} else if (q < 3) {
return -405*t3 + 90*t4 - 9*t5;
} else if (q < 4) {
return 90*t4 - 9*t5;
} else if (q < 5) {
return -9*t5;
}
else
return 0;
}
inline static Tscal ddf(Tscal q) {
Tscal t1 = sham::pow_constexpr<7>(1 - q);
Tscal t2 = sham::pow_constexpr<7>(2 - q);
Tscal t3 = sham::pow_constexpr<7>(3 - q);
Tscal t4 = sham::pow_constexpr<7>(4 - q);
Tscal t5 = sham::pow_constexpr<7>(5 - q);
if (q < 1) {
return 15120*t1 - 8640*t2 + 3240*t3 - 720*t4 + 72*t5;
} else if (q < 2) {
return -8640*t2 + 3240*t3 - 720*t4 + 72*t5;
} else if (q < 3) {
return 3240*t3 - 720*t4 + 72*t5;
} else if (q < 4) {
return -720*t4 + 72*t5;
} else if (q < 5) {
return 72*t5;
}
else
return 0;
}
inline static Tscal phi_tilde_3d(Tscal q) {
Tscal t1 = sham::pow_constexpr<10>(q);
Tscal t2 = sham::pow_constexpr<11>(q);
Tscal t3 = sham::pow_constexpr<12>(q);
Tscal t4 = sham::pow_constexpr<2>(q);
Tscal t5 = sham::pow_constexpr<3>(q);
Tscal t6 = sham::pow_constexpr<4>(q);
Tscal t7 = sham::pow_constexpr<5>(q);
Tscal t8 = sham::pow_constexpr<6>(q);
Tscal t9 = sham::pow_constexpr<7>(q);
Tscal t10 = sham::pow_constexpr<8>(q);
Tscal t11 = sham::pow_constexpr<9>(q);
if (q < 1) {
return (2.0/33.0)*shambase::constants::pi<Tscal>*(-63*t2 + 378*t1 - 3850*t10 + 37620*t8 - 291060*t6 + 1718090*t4 - 8766690);
} else if (q < 2) {
return (2.0/33.0)*shambase::constants::pi<Tscal>*(42*t3 - 756*t2 + 5544*t1 - 20020*t11 + 31185*t10 - 3960*t9 + 38808*t8 - 316008*t7 + 10395*t6 + 1715780*t5 - 8766564*q - 21)/q;
} else if (q < 3) {
return (2.0/33.0)*shambase::constants::pi<Tscal>*(-18*t3 + 540*t2 - 7128*t1 + 53900*t11 - 253935*t10 + 756360*t9 - 1380456*t8 + 1508760*t7 - 1510245*t6 + 2391620*t5 - 8914020*q + 49131)/q;
} else if (q < 4) {
return (1.0/33.0)*shambase::constants::pi<Tscal>*(9*t3 - 378*t2 + 7128*t1 - 79310*t11 + 574695*t10 - 2817540*t9 + 9363816*t8 - 20365884*t7 + 26208765*t6 - 14702930*t5 - 8262102*q - 4684707)/q;
} else if (q < 5) {
return (1.0/33.0)*shambase::constants::pi<Tscal>*(-t3 + 54*t2 - 1320*t1 + 19250*t11 - 185625*t10 + 1237500*t9 - 5775000*t8 + 18562500*t7 - 38671875*t6 + 42968750*t5 - 58593750*q + 28869725)/q;
}
else
return -604800*shambase::constants::pi<Tscal>/q;
}
inline static Tscal phi_tilde_3d_prime(Tscal q) {
Tscal t1 = sham::pow_constexpr<10>(q);
Tscal t2 = sham::pow_constexpr<11>(q);
Tscal t3 = sham::pow_constexpr<12>(q);
Tscal t4 = sham::pow_constexpr<2>(q);
Tscal t5 = sham::pow_constexpr<3>(q);
Tscal t6 = sham::pow_constexpr<4>(q);
Tscal t7 = sham::pow_constexpr<5>(q);
Tscal t8 = sham::pow_constexpr<6>(q);
Tscal t9 = sham::pow_constexpr<7>(q);
Tscal t10 = sham::pow_constexpr<8>(q);
Tscal t11 = sham::pow_constexpr<9>(q);
if (q < 1) {
return (2.0/33.0)*shambase::constants::pi<Tscal>*(-693*t1 + 3780*t11 - 30800*t9 + 225720*t7 - 1164240*t5 + 3436180*q);
} else if (q < 2) {
return (2.0/33.0)*shambase::constants::pi<Tscal>*(504*t2 - 8316*t1 + 55440*t11 - 180180*t10 + 249480*t9 - 27720*t8 + 232848*t7 - 1580040*t6 + 41580*t5 + 5147340*t4 - 8766564)/q - (2.0/33.0)*shambase::constants::pi<Tscal>*(42*t3 - 756*t2 + 5544*t1 - 20020*t11 + 31185*t10 - 3960*t9 + 38808*t8 - 316008*t7 + 10395*t6 + 1715780*t5 - 8766564*q - 21)/t4;
} else if (q < 3) {
return (2.0/33.0)*shambase::constants::pi<Tscal>*(-216*t2 + 5940*t1 - 71280*t11 + 485100*t10 - 2031480*t9 + 5294520*t8 - 8282736*t7 + 7543800*t6 - 6040980*t5 + 7174860*t4 - 8914020)/q - (2.0/33.0)*shambase::constants::pi<Tscal>*(-18*t3 + 540*t2 - 7128*t1 + 53900*t11 - 253935*t10 + 756360*t9 - 1380456*t8 + 1508760*t7 - 1510245*t6 + 2391620*t5 - 8914020*q + 49131)/t4;
} else if (q < 4) {
return (1.0/33.0)*shambase::constants::pi<Tscal>*(108*t2 - 4158*t1 + 71280*t11 - 713790*t10 + 4597560*t9 - 19722780*t8 + 56182896*t7 - 101829420*t6 + 104835060*t5 - 44108790*t4 - 8262102)/q - (1.0/33.0)*shambase::constants::pi<Tscal>*(9*t3 - 378*t2 + 7128*t1 - 79310*t11 + 574695*t10 - 2817540*t9 + 9363816*t8 - 20365884*t7 + 26208765*t6 - 14702930*t5 - 8262102*q - 4684707)/t4;
} else if (q < 5) {
return (1.0/33.0)*shambase::constants::pi<Tscal>*(-12*t2 + 594*t1 - 13200*t11 + 173250*t10 - 1485000*t9 + 8662500*t8 - 34650000*t7 + 92812500*t6 - 154687500*t5 + 128906250*t4 - 58593750)/q - (1.0/33.0)*shambase::constants::pi<Tscal>*(-t3 + 54*t2 - 1320*t1 + 19250*t11 - 185625*t10 + 1237500*t9 - 5775000*t8 + 18562500*t7 - 38671875*t6 + 42968750*t5 - 58593750*q + 28869725)/t4;
}
else
return 604800*shambase::constants::pi<Tscal>/t4;
}
};
------------------------------------------
Test the kernel
901 test_result_m10 = test_kernel(ret, tolerance=1e-8)
------------------------------------------
Testing kernel M10 matching with Shamrock code
------------------------------------------
Testing norms:
Shamrock norm_1d = 2.7557319223985893e-06, expected=0.00000275573192239859, delta=0
Shamrock norm_2d = 1.1981975231318881e-06, expected=0.00000119819752313189, delta=0
Shamrock norm_3d = 5.263060287430401e-07, expected=5.26306028743040E-7, delta=1.05879118406788E-22
Testing kernel radius:
Shamrock Rkern = 5.0, delta=0
Testing kernel functions:
Shamrock f(q) = <built-in method M10_f of PyCapsule object at 0x7fbcd9bbddd0>
Shamrock df(q) = <built-in method M10_df of PyCapsule object at 0x7fbcd9bbdda0>
Shamrock ddf(q) = <built-in method M10_ddf of PyCapsule object at 0x7fbcd9bbdd70>
Shamrock phi_tilde_3d(q) = <built-in method M10_phi_tilde_3d of PyCapsule object at 0x7fbcd9bbdd40>
Shamrock phi_tilde_3d_prime(q) = <built-in method M10_phi_tilde_3d_prime of PyCapsule object at 0x7fbcd9bbdd10>
Absolute error f(q) = 1.3969838619232178e-09
Absolute error df(q) = 2.928572939708829e-09
Absolute error ddf(q) = 3.725290298461914e-09
Absolute error phi_tilde_3d(q) = 0.00000059496615953398715061353189510027
Absolute error phi_tilde_3d_prime(q) = 0.00000075282548568718158583115382224247
------------------------------------------
C2 Kernel#
Generate c++ code for the kernel
910 ret = kernel_to_shamrock(c2)
913 print_kernel_info(ret)
------------------------------------------
Sympy expression for kernel C2
------------------------------------------
f(q) = Piecewise(((1 - q/2)**4*(2*q + 1), q < 2), (0, True))
df(q) = Piecewise((2*(1 - q/2)**4 - 2*(1 - q/2)**3*(2*q + 1), q < 2), (0, True))
ddf(q) = Piecewise((-8*(1 - q/2)**3 + 3*(1 - q/2)**2*(2*q + 1), q < 2), (0, True))
phi_tilde_3d(q) = Piecewise((pi*(q**2*(3*q**5 - 30*q**4 + 112*q**3 - 168*q**2 + 224) - 384)/336, q < 2), (-16*pi/(21*q), True))
phi_tilde_3d_prime(q) = Piecewise((pi*(q**2*(15*q**4 - 120*q**3 + 336*q**2 - 336*q) + 2*q*(3*q**5 - 30*q**4 + 112*q**3 - 168*q**2 + 224))/336, q < 2), (16*pi/(21*q**2), True))
------------------------------------------
916 print_kernel_cpp_code(ret)
------------------------------------------
C++ generated code for kernel C2
------------------------------------------
template<class Tscal>
class KernelDefC2 {
public:
inline static constexpr Tscal Rkern = 2; ///< Compact support radius of the kernel
/// default hfact to be used for this kernel
inline static constexpr Tscal hfactd = 1.2;
/// 1D norm of the kernel
inline static constexpr Tscal norm_1d = 3.0/4.0;
/// 2D norm of the kernel
inline static constexpr Tscal norm_2d = (7.0/4.0)/shambase::constants::pi<Tscal>;
/// 3D norm of the kernel
inline static constexpr Tscal norm_3d = (21.0/16.0)/shambase::constants::pi<Tscal>;
inline static Tscal f(Tscal q) {
Tscal t1 = sham::pow_constexpr<4>(1 - (1.0/2.0)*q);
if (q < 2) {
return t1*(2*q + 1);
}
else
return 0;
}
inline static Tscal df(Tscal q) {
Tscal t1 = sham::pow_constexpr<3>(1 - (1.0/2.0)*q);
Tscal t2 = sham::pow_constexpr<4>(1 - (1.0/2.0)*q);
if (q < 2) {
return 2*t2 - 2*t1*(2*q + 1);
}
else
return 0;
}
inline static Tscal ddf(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(1 - (1.0/2.0)*q);
Tscal t2 = sham::pow_constexpr<3>(1 - (1.0/2.0)*q);
if (q < 2) {
return -8*t2 + 3*t1*(2*q + 1);
}
else
return 0;
}
inline static Tscal phi_tilde_3d(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
Tscal t2 = sham::pow_constexpr<3>(q);
Tscal t3 = sham::pow_constexpr<4>(q);
Tscal t4 = sham::pow_constexpr<5>(q);
if (q < 2) {
return (1.0/336.0)*shambase::constants::pi<Tscal>*(t1*(3*t4 - 30*t3 + 112*t2 - 168*t1 + 224) - 384);
}
else
return -(16.0/21.0)*shambase::constants::pi<Tscal>/q;
}
inline static Tscal phi_tilde_3d_prime(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
Tscal t2 = sham::pow_constexpr<3>(q);
Tscal t3 = sham::pow_constexpr<4>(q);
Tscal t4 = sham::pow_constexpr<5>(q);
if (q < 2) {
return (1.0/336.0)*shambase::constants::pi<Tscal>*(t1*(15*t3 - 120*t2 + 336*t1 - 336*q) + 2*q*(3*t4 - 30*t3 + 112*t2 - 168*t1 + 224));
}
else
return (16.0/21.0)*shambase::constants::pi<Tscal>/t1;
}
};
------------------------------------------
Test the kernel
920 test_result_c2 = test_kernel(ret)
------------------------------------------
Testing kernel C2 matching with Shamrock code
------------------------------------------
Testing norms:
Shamrock norm_1d = 0.75, expected=0.750000000000000, delta=0
Shamrock norm_2d = 0.5570423008216338, expected=0.557042300821634, delta=1.11022302462516E-16
Shamrock norm_3d = 0.4177817256162253, expected=0.417781725616225, delta=0
Testing kernel radius:
Shamrock Rkern = 2.0, delta=0
Testing kernel functions:
Shamrock f(q) = <built-in method C2_f of PyCapsule object at 0x7fbcd9bbdad0>
Shamrock df(q) = <built-in method C2_df of PyCapsule object at 0x7fbcd9bbdaa0>
Shamrock ddf(q) = <built-in method C2_ddf of PyCapsule object at 0x7fbcd9bbda70>
Shamrock phi_tilde_3d(q) = <built-in method C2_phi_tilde_3d of PyCapsule object at 0x7fbcd9bbda40>
Shamrock phi_tilde_3d_prime(q) = <built-in method C2_phi_tilde_3d_prime of PyCapsule object at 0x7fbcd9bbda10>
Absolute error f(q) = 4.211561406673559480149857592443e-16
Absolute error df(q) = 4.0113140431073352052618380525483e-16
Absolute error ddf(q) = 1.371769129953306271714661459359e-15
Absolute error phi_tilde_3d(q) = 4.3963002435041423632058897180998e-15
Absolute error phi_tilde_3d_prime(q) = 3.8772262773114693044442611002654e-15
------------------------------------------
C4 Kernel#
Generate c++ code for the kernel
929 ret = kernel_to_shamrock(c4)
932 print_kernel_info(ret)
------------------------------------------
Sympy expression for kernel C4
------------------------------------------
f(q) = Piecewise(((1 - q/2)**6*(35*q**2/12 + 3*q + 1), q < 2), (0, True))
df(q) = Piecewise(((1 - q/2)**6*(35*q/6 + 3) - 3*(1 - q/2)**5*(35*q**2/12 + 3*q + 1), q < 2), (0, True))
ddf(q) = Piecewise((35*(1 - q/2)**6/6 - 6*(1 - q/2)**5*(35*q/6 + 3) + 15*(1 - q/2)**4*(35*q**2/12 + 3*q + 1)/2, q < 2), (0, True))
phi_tilde_3d(q) = Piecewise((pi*(q**2*(105*q**8 - 1408*q**7 + 7700*q**6 - 21120*q**5 + 26400*q**4 - 29568*q**2 + 42240) - 56320)/63360, q < 2), (-256*pi/(495*q), True))
phi_tilde_3d_prime(q) = Piecewise((pi*(q**2*(840*q**7 - 9856*q**6 + 46200*q**5 - 105600*q**4 + 105600*q**3 - 59136*q) + 2*q*(105*q**8 - 1408*q**7 + 7700*q**6 - 21120*q**5 + 26400*q**4 - 29568*q**2 + 42240))/63360, q < 2), (256*pi/(495*q**2), True))
------------------------------------------
935 print_kernel_cpp_code(ret)
------------------------------------------
C++ generated code for kernel C4
------------------------------------------
template<class Tscal>
class KernelDefC4 {
public:
inline static constexpr Tscal Rkern = 2; ///< Compact support radius of the kernel
/// default hfact to be used for this kernel
inline static constexpr Tscal hfactd = 1.2;
/// 1D norm of the kernel
inline static constexpr Tscal norm_1d = 27.0/32.0;
/// 2D norm of the kernel
inline static constexpr Tscal norm_2d = (9.0/4.0)/shambase::constants::pi<Tscal>;
/// 3D norm of the kernel
inline static constexpr Tscal norm_3d = (495.0/256.0)/shambase::constants::pi<Tscal>;
inline static Tscal f(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
Tscal t2 = sham::pow_constexpr<6>(1 - (1.0/2.0)*q);
if (q < 2) {
return t2*((35.0/12.0)*t1 + 3*q + 1);
}
else
return 0;
}
inline static Tscal df(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
Tscal t2 = sham::pow_constexpr<5>(1 - (1.0/2.0)*q);
Tscal t3 = sham::pow_constexpr<6>(1 - (1.0/2.0)*q);
if (q < 2) {
return t3*((35.0/6.0)*q + 3) - 3*t2*((35.0/12.0)*t1 + 3*q + 1);
}
else
return 0;
}
inline static Tscal ddf(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
Tscal t2 = sham::pow_constexpr<4>(1 - (1.0/2.0)*q);
Tscal t3 = sham::pow_constexpr<5>(1 - (1.0/2.0)*q);
Tscal t4 = sham::pow_constexpr<6>(1 - (1.0/2.0)*q);
if (q < 2) {
return (35.0/6.0)*t4 - 6*t3*((35.0/6.0)*q + 3) + (15.0/2.0)*t2*((35.0/12.0)*t1 + 3*q + 1);
}
else
return 0;
}
inline static Tscal phi_tilde_3d(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
Tscal t2 = sham::pow_constexpr<4>(q);
Tscal t3 = sham::pow_constexpr<5>(q);
Tscal t4 = sham::pow_constexpr<6>(q);
Tscal t5 = sham::pow_constexpr<7>(q);
Tscal t6 = sham::pow_constexpr<8>(q);
if (q < 2) {
return (1.0/63360.0)*shambase::constants::pi<Tscal>*(t1*(105*t6 - 1408*t5 + 7700*t4 - 21120*t3 + 26400*t2 - 29568*t1 + 42240) - 56320);
}
else
return -(256.0/495.0)*shambase::constants::pi<Tscal>/q;
}
inline static Tscal phi_tilde_3d_prime(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
Tscal t2 = sham::pow_constexpr<3>(q);
Tscal t3 = sham::pow_constexpr<4>(q);
Tscal t4 = sham::pow_constexpr<5>(q);
Tscal t5 = sham::pow_constexpr<6>(q);
Tscal t6 = sham::pow_constexpr<7>(q);
Tscal t7 = sham::pow_constexpr<8>(q);
if (q < 2) {
return (1.0/63360.0)*shambase::constants::pi<Tscal>*(t1*(840*t6 - 9856*t5 + 46200*t4 - 105600*t3 + 105600*t2 - 59136*q) + 2*q*(105*t7 - 1408*t6 + 7700*t5 - 21120*t4 + 26400*t3 - 29568*t1 + 42240));
}
else
return (256.0/495.0)*shambase::constants::pi<Tscal>/t1;
}
};
------------------------------------------
Test the kernel
939 test_result_c4 = test_kernel(ret)
------------------------------------------
Testing kernel C4 matching with Shamrock code
------------------------------------------
Testing norms:
Shamrock norm_1d = 0.84375, expected=0.843750000000000, delta=0
Shamrock norm_2d = 0.716197243913529, expected=0.716197243913529, delta=0
Shamrock norm_3d = 0.6154820064881891, expected=0.615482006488189, delta=1.11022302462516E-16
Testing kernel radius:
Shamrock Rkern = 2.0, delta=0
Testing kernel functions:
Shamrock f(q) = <built-in method C4_f of PyCapsule object at 0x7fbcd9bbd7d0>
Shamrock df(q) = <built-in method C4_df of PyCapsule object at 0x7fbcd9bbd7a0>
Shamrock ddf(q) = <built-in method C4_ddf of PyCapsule object at 0x7fbcd9bbd770>
Shamrock phi_tilde_3d(q) = <built-in method C4_phi_tilde_3d of PyCapsule object at 0x7fbcd9bbd740>
Shamrock phi_tilde_3d_prime(q) = <built-in method C4_phi_tilde_3d_prime of PyCapsule object at 0x7fbcd9bbd710>
Absolute error f(q) = 6.77057790166676168827606387144e-16
Absolute error df(q) = 7.3058119683808204154226284388856e-16
Absolute error ddf(q) = 4.5862635469006640758973444472878e-15
Absolute error phi_tilde_3d(q) = 2.9691588069996695982673972652105e-14
Absolute error phi_tilde_3d_prime(q) = 6.4917526524589430038090522815696e-14
------------------------------------------
C6 Kernel#
Generate c++ code for the kernel
948 ret = kernel_to_shamrock(c6)
951 print_kernel_info(ret)
------------------------------------------
Sympy expression for kernel C6
------------------------------------------
f(q) = Piecewise(((1 - q/2)**8*(4*q**3 + 25*q**2/4 + 4*q + 1), q < 2), (0, True))
df(q) = Piecewise(((1 - q/2)**8*(12*q**2 + 25*q/2 + 4) - 4*(1 - q/2)**7*(4*q**3 + 25*q**2/4 + 4*q + 1), q < 2), (0, True))
ddf(q) = Piecewise(((1 - q/2)**8*(24*q + 25/2) - 8*(1 - q/2)**7*(12*q**2 + 25*q/2 + 4) + 14*(1 - q/2)**6*(4*q**3 + 25*q**2/4 + 4*q + 1), q < 2), (0, True))
phi_tilde_3d(q) = Piecewise((pi*(q**2*(480*q**11 - 8085*q**10 + 58240*q**9 - 229320*q**8 + 512512*q**7 - 560560*q**6 + 549120*q**4 - 768768*q**2 + 931840) - 1003520)/1397760, q < 2), (-512*pi/(1365*q), True))
phi_tilde_3d_prime(q) = Piecewise((pi*(q**2*(5280*q**10 - 80850*q**9 + 524160*q**8 - 1834560*q**7 + 3587584*q**6 - 3363360*q**5 + 2196480*q**3 - 1537536*q) + 2*q*(480*q**11 - 8085*q**10 + 58240*q**9 - 229320*q**8 + 512512*q**7 - 560560*q**6 + 549120*q**4 - 768768*q**2 + 931840))/1397760, q < 2), (512*pi/(1365*q**2), True))
------------------------------------------
954 print_kernel_cpp_code(ret)
------------------------------------------
C++ generated code for kernel C6
------------------------------------------
template<class Tscal>
class KernelDefC6 {
public:
inline static constexpr Tscal Rkern = 2; ///< Compact support radius of the kernel
/// default hfact to be used for this kernel
inline static constexpr Tscal hfactd = 1.2;
/// 1D norm of the kernel
inline static constexpr Tscal norm_1d = 15.0/16.0;
/// 2D norm of the kernel
inline static constexpr Tscal norm_2d = (39.0/14.0)/shambase::constants::pi<Tscal>;
/// 3D norm of the kernel
inline static constexpr Tscal norm_3d = (1365.0/512.0)/shambase::constants::pi<Tscal>;
inline static Tscal f(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
Tscal t2 = sham::pow_constexpr<3>(q);
Tscal t3 = sham::pow_constexpr<8>(1 - (1.0/2.0)*q);
if (q < 2) {
return t3*(4*t2 + (25.0/4.0)*t1 + 4*q + 1);
}
else
return 0;
}
inline static Tscal df(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
Tscal t2 = sham::pow_constexpr<3>(q);
Tscal t3 = sham::pow_constexpr<7>(1 - (1.0/2.0)*q);
Tscal t4 = sham::pow_constexpr<8>(1 - (1.0/2.0)*q);
if (q < 2) {
return t4*(12*t1 + (25.0/2.0)*q + 4) - 4*t3*(4*t2 + (25.0/4.0)*t1 + 4*q + 1);
}
else
return 0;
}
inline static Tscal ddf(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
Tscal t2 = sham::pow_constexpr<3>(q);
Tscal t3 = sham::pow_constexpr<6>(1 - (1.0/2.0)*q);
Tscal t4 = sham::pow_constexpr<7>(1 - (1.0/2.0)*q);
Tscal t5 = sham::pow_constexpr<8>(1 - (1.0/2.0)*q);
if (q < 2) {
return t5*(24*q + 25.0/2.0) - 8*t4*(12*t1 + (25.0/2.0)*q + 4) + 14*t3*(4*t2 + (25.0/4.0)*t1 + 4*q + 1);
}
else
return 0;
}
inline static Tscal phi_tilde_3d(Tscal q) {
Tscal t1 = sham::pow_constexpr<10>(q);
Tscal t2 = sham::pow_constexpr<11>(q);
Tscal t3 = sham::pow_constexpr<2>(q);
Tscal t4 = sham::pow_constexpr<4>(q);
Tscal t5 = sham::pow_constexpr<6>(q);
Tscal t6 = sham::pow_constexpr<7>(q);
Tscal t7 = sham::pow_constexpr<8>(q);
Tscal t8 = sham::pow_constexpr<9>(q);
if (q < 2) {
return (1.0/1397760.0)*shambase::constants::pi<Tscal>*(t3*(480*t2 - 8085*t1 + 58240*t8 - 229320*t7 + 512512*t6 - 560560*t5 + 549120*t4 - 768768*t3 + 931840) - 1003520);
}
else
return -(512.0/1365.0)*shambase::constants::pi<Tscal>/q;
}
inline static Tscal phi_tilde_3d_prime(Tscal q) {
Tscal t1 = sham::pow_constexpr<10>(q);
Tscal t2 = sham::pow_constexpr<11>(q);
Tscal t3 = sham::pow_constexpr<2>(q);
Tscal t4 = sham::pow_constexpr<3>(q);
Tscal t5 = sham::pow_constexpr<4>(q);
Tscal t6 = sham::pow_constexpr<5>(q);
Tscal t7 = sham::pow_constexpr<6>(q);
Tscal t8 = sham::pow_constexpr<7>(q);
Tscal t9 = sham::pow_constexpr<8>(q);
Tscal t10 = sham::pow_constexpr<9>(q);
if (q < 2) {
return (1.0/1397760.0)*shambase::constants::pi<Tscal>*(t3*(5280*t1 - 80850*t10 + 524160*t9 - 1834560*t8 + 3587584*t7 - 3363360*t6 + 2196480*t4 - 1537536*q) + 2*q*(480*t2 - 8085*t1 + 58240*t10 - 229320*t9 + 512512*t8 - 560560*t7 + 549120*t5 - 768768*t3 + 931840));
}
else
return (512.0/1365.0)*shambase::constants::pi<Tscal>/t3;
}
};
------------------------------------------
Test the kernel
958 test_result_c6 = test_kernel(ret)
------------------------------------------
Testing kernel C6 matching with Shamrock code
------------------------------------------
Testing norms:
Shamrock norm_1d = 0.9375, expected=0.937500000000000, delta=0
Shamrock norm_2d = 0.886720397226274, expected=0.886720397226274, delta=0
Shamrock norm_3d = 0.8486191301579575, expected=0.848619130157958, delta=0
Testing kernel radius:
Shamrock Rkern = 2.0, delta=0
Testing kernel functions:
Shamrock f(q) = <built-in method C6_f of PyCapsule object at 0x7fbcd9bbd170>
Shamrock df(q) = <built-in method C6_df of PyCapsule object at 0x7fbcd9bbd140>
Shamrock ddf(q) = <built-in method C6_ddf of PyCapsule object at 0x7fbcd9bbd110>
Shamrock phi_tilde_3d(q) = <built-in method C6_phi_tilde_3d of PyCapsule object at 0x7fbcd9bbd0e0>
Shamrock phi_tilde_3d_prime(q) = <built-in method C6_phi_tilde_3d_prime of PyCapsule object at 0x7fbcd9bbd0b0>
Absolute error f(q) = 8.0028218067564718410675074309679e-16
Absolute error df(q) = 1.2403417665354433235366686954591e-15
Absolute error ddf(q) = 8.6752247783722774983167486346621e-15
Absolute error phi_tilde_3d(q) = 1.5252718357359013011480267646023e-13
Absolute error phi_tilde_3d_prime(q) = 3.809628034813140135293395651618e-13
------------------------------------------
M4DH Kernel#
Generate c++ code for the kernel
968 ret = kernel_to_shamrock(m4dh)
971 print_kernel_info(ret)
------------------------------------------
Sympy expression for kernel M4DH
------------------------------------------
f(q) = Piecewise((q**2*(-(1 - q)**3 + (2 - q)**3/4), q < 1), (q**2*(2 - q)**3/4, q < 2), (0, True))
df(q) = Piecewise((q**2*(3*(1 - q)**2 - 3*(2 - q)**2/4) + 2*q*(-(1 - q)**3 + (2 - q)**3/4), q < 1), (-3*q**2*(2 - q)**2/4 + q*(2 - q)**3/2, q < 2), (0, True))
ddf(q) = Piecewise((q**2*(9*q/2 - 3) + 4*q*(3*(1 - q)**2 - 3*(2 - q)**2/4) - 2*(1 - q)**3 + (2 - q)**3/2, q < 1), (-3*q**2*(2*q - 4)/4 - 3*q*(2 - q)**2 + (2 - q)**3/2, q < 2), (0, True))
phi_tilde_3d(q) = Piecewise((pi*(q**4*(15*q**3 - 40*q**2 + 56) - 248)/280, q < 1), (pi*(-5*q**8 + 40*q**7 - 112*q**6 + 112*q**5 - 256*q + 4)/(280*q), q < 2), (-9*pi/(10*q), True))
phi_tilde_3d_prime(q) = Piecewise((pi*(q**4*(45*q**2 - 80*q) + 4*q**3*(15*q**3 - 40*q**2 + 56))/280, q < 1), (pi*(-40*q**7 + 280*q**6 - 672*q**5 + 560*q**4 - 256)/(280*q) - pi*(-5*q**8 + 40*q**7 - 112*q**6 + 112*q**5 - 256*q + 4)/(280*q**2), q < 2), (9*pi/(10*q**2), True))
------------------------------------------
974 print_kernel_cpp_code(ret)
------------------------------------------
C++ generated code for kernel M4DH
------------------------------------------
template<class Tscal>
class KernelDefM4DH {
public:
inline static constexpr Tscal Rkern = 2; ///< Compact support radius of the kernel
/// default hfact to be used for this kernel
inline static constexpr Tscal hfactd = 1.2;
/// 1D norm of the kernel
inline static constexpr Tscal norm_1d = 2;
/// 2D norm of the kernel
inline static constexpr Tscal norm_2d = (70.0/31.0)/shambase::constants::pi<Tscal>;
/// 3D norm of the kernel
inline static constexpr Tscal norm_3d = (10.0/9.0)/shambase::constants::pi<Tscal>;
inline static Tscal f(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
Tscal t2 = sham::pow_constexpr<3>(1 - q);
Tscal t3 = sham::pow_constexpr<3>(2 - q);
if (q < 1) {
return t1*(-t2 + (1.0/4.0)*t3);
} else if (q < 2) {
return (1.0/4.0)*t1*t3;
}
else
return 0;
}
inline static Tscal df(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(1 - q);
Tscal t2 = sham::pow_constexpr<2>(2 - q);
Tscal t3 = sham::pow_constexpr<2>(q);
Tscal t4 = sham::pow_constexpr<3>(1 - q);
Tscal t5 = sham::pow_constexpr<3>(2 - q);
if (q < 1) {
return t3*(3*t1 - (3.0/4.0)*t2) + 2*q*(-t4 + (1.0/4.0)*t5);
} else if (q < 2) {
return -(3.0/4.0)*t3*t2 + (1.0/2.0)*q*t5;
}
else
return 0;
}
inline static Tscal ddf(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(1 - q);
Tscal t2 = sham::pow_constexpr<2>(2 - q);
Tscal t3 = sham::pow_constexpr<2>(q);
Tscal t4 = sham::pow_constexpr<3>(1 - q);
Tscal t5 = sham::pow_constexpr<3>(2 - q);
if (q < 1) {
return t3*((9.0/2.0)*q - 3) + 4*q*(3*t1 - (3.0/4.0)*t2) - 2*t4 + (1.0/2.0)*t5;
} else if (q < 2) {
return -(3.0/4.0)*t3*(2*q - 4) - 3*q*t2 + (1.0/2.0)*t5;
}
else
return 0;
}
inline static Tscal phi_tilde_3d(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
Tscal t2 = sham::pow_constexpr<3>(q);
Tscal t3 = sham::pow_constexpr<4>(q);
Tscal t4 = sham::pow_constexpr<5>(q);
Tscal t5 = sham::pow_constexpr<6>(q);
Tscal t6 = sham::pow_constexpr<7>(q);
Tscal t7 = sham::pow_constexpr<8>(q);
if (q < 1) {
return (1.0/280.0)*shambase::constants::pi<Tscal>*(t3*(15*t2 - 40*t1 + 56) - 248);
} else if (q < 2) {
return (1.0/280.0)*shambase::constants::pi<Tscal>*(-5*t7 + 40*t6 - 112*t5 + 112*t4 - 256*q + 4)/q;
}
else
return -(9.0/10.0)*shambase::constants::pi<Tscal>/q;
}
inline static Tscal phi_tilde_3d_prime(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
Tscal t2 = sham::pow_constexpr<3>(q);
Tscal t3 = sham::pow_constexpr<4>(q);
Tscal t4 = sham::pow_constexpr<5>(q);
Tscal t5 = sham::pow_constexpr<6>(q);
Tscal t6 = sham::pow_constexpr<7>(q);
Tscal t7 = sham::pow_constexpr<8>(q);
if (q < 1) {
return (1.0/280.0)*shambase::constants::pi<Tscal>*(t3*(45*t1 - 80*q) + 4*t2*(15*t2 - 40*t1 + 56));
} else if (q < 2) {
return (1.0/280.0)*shambase::constants::pi<Tscal>*(-40*t6 + 280*t5 - 672*t4 + 560*t3 - 256)/q - (1.0/280.0)*shambase::constants::pi<Tscal>*(-5*t7 + 40*t6 - 112*t5 + 112*t4 - 256*q + 4)/t1;
}
else
return (9.0/10.0)*shambase::constants::pi<Tscal>/t1;
}
};
------------------------------------------
Test the kernel
978 test_result_m4dh = test_kernel(ret)
------------------------------------------
Testing kernel M4DH matching with Shamrock code
------------------------------------------
Testing norms:
Shamrock norm_1d = 2.0, expected=2.00000000000000, delta=0
Shamrock norm_2d = 0.7187642591246886, expected=0.718764259124689, delta=1.11022302462516E-16
Shamrock norm_3d = 0.35367765131532297, expected=0.353677651315323, delta=0
Testing kernel radius:
Shamrock Rkern = 2.0, delta=0
Testing kernel functions:
Shamrock f(q) = <built-in method M4DH_f of PyCapsule object at 0x7fbcd9bbce70>
Shamrock df(q) = <built-in method M4DH_df of PyCapsule object at 0x7fbcd9bbce40>
Shamrock ddf(q) = <built-in method M4DH_ddf of PyCapsule object at 0x7fbcd9bbce10>
Shamrock phi_tilde_3d(q) = <built-in method M4DH_phi_tilde_3d of PyCapsule object at 0x7fbcd9bbcde0>
Shamrock phi_tilde_3d_prime(q) = <built-in method M4DH_phi_tilde_3d_prime of PyCapsule object at 0x7fbcd9bbcdb0>
Absolute error f(q) = 1.2402696483592525566706857128555e-16
Absolute error df(q) = 1.1457310014385301187257368207776e-15
Absolute error ddf(q) = 9.0591089023422617007004687927574e-16
Absolute error phi_tilde_3d(q) = 8.3097552699191310126976481785064e-15
Absolute error phi_tilde_3d_prime(q) = 3.3207246830111135096394910341455e-14
------------------------------------------
M4DH3 Kernel#
Generate c++ code for the kernel
988 ret = kernel_to_shamrock(m4dh3)
991 print_kernel_info(ret)
------------------------------------------
Sympy expression for kernel M4DH3
------------------------------------------
f(q) = Piecewise((q**3*(-(1 - q)**3 + (2 - q)**3/4), q < 1), (q**3*(2 - q)**3/4, q < 2), (0, True))
df(q) = Piecewise((q**3*(3*(1 - q)**2 - 3*(2 - q)**2/4) + 3*q**2*(-(1 - q)**3 + (2 - q)**3/4), q < 1), (-3*q**3*(2 - q)**2/4 + 3*q**2*(2 - q)**3/4, q < 2), (0, True))
ddf(q) = Piecewise((q**3*(9*q/2 - 3) + 6*q**2*(3*(1 - q)**2 - 3*(2 - q)**2/4) + 6*q*(-(1 - q)**3 + (2 - q)**3/4), q < 1), (-3*q**3*(2*q - 4)/4 - 9*q**2*(2 - q)**2/2 + 3*q*(2 - q)**3/2, q < 2), (0, True))
phi_tilde_3d(q) = Piecewise((pi*(q**5*(35*q**3 - 90*q**2 + 112) - 756)/840, q < 1), (pi*(-35*q**9 + 270*q**8 - 720*q**7 + 672*q**6 - 2304*q + 20)/(2520*q), q < 2), (-127*pi/(126*q), True))
phi_tilde_3d_prime(q) = Piecewise((pi*(q**5*(105*q**2 - 180*q) + 5*q**4*(35*q**3 - 90*q**2 + 112))/840, q < 1), (pi*(-315*q**8 + 2160*q**7 - 5040*q**6 + 4032*q**5 - 2304)/(2520*q) - pi*(-35*q**9 + 270*q**8 - 720*q**7 + 672*q**6 - 2304*q + 20)/(2520*q**2), q < 2), (127*pi/(126*q**2), True))
------------------------------------------
994 print_kernel_cpp_code(ret)
------------------------------------------
C++ generated code for kernel M4DH3
------------------------------------------
template<class Tscal>
class KernelDefM4DH3 {
public:
inline static constexpr Tscal Rkern = 2; ///< Compact support radius of the kernel
/// default hfact to be used for this kernel
inline static constexpr Tscal hfactd = 1.2;
/// 1D norm of the kernel
inline static constexpr Tscal norm_1d = 70.0/31.0;
/// 2D norm of the kernel
inline static constexpr Tscal norm_2d = (20.0/9.0)/shambase::constants::pi<Tscal>;
/// 3D norm of the kernel
inline static constexpr Tscal norm_3d = (126.0/127.0)/shambase::constants::pi<Tscal>;
inline static Tscal f(Tscal q) {
Tscal t1 = sham::pow_constexpr<3>(1 - q);
Tscal t2 = sham::pow_constexpr<3>(2 - q);
Tscal t3 = sham::pow_constexpr<3>(q);
if (q < 1) {
return t3*(-t1 + (1.0/4.0)*t2);
} else if (q < 2) {
return (1.0/4.0)*t3*t2;
}
else
return 0;
}
inline static Tscal df(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(1 - q);
Tscal t2 = sham::pow_constexpr<2>(2 - q);
Tscal t3 = sham::pow_constexpr<2>(q);
Tscal t4 = sham::pow_constexpr<3>(1 - q);
Tscal t5 = sham::pow_constexpr<3>(2 - q);
Tscal t6 = sham::pow_constexpr<3>(q);
if (q < 1) {
return t6*(3*t1 - (3.0/4.0)*t2) + 3*t3*(-t4 + (1.0/4.0)*t5);
} else if (q < 2) {
return -(3.0/4.0)*t6*t2 + (3.0/4.0)*t3*t5;
}
else
return 0;
}
inline static Tscal ddf(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(1 - q);
Tscal t2 = sham::pow_constexpr<2>(2 - q);
Tscal t3 = sham::pow_constexpr<2>(q);
Tscal t4 = sham::pow_constexpr<3>(1 - q);
Tscal t5 = sham::pow_constexpr<3>(2 - q);
Tscal t6 = sham::pow_constexpr<3>(q);
if (q < 1) {
return t6*((9.0/2.0)*q - 3) + 6*t3*(3*t1 - (3.0/4.0)*t2) + 6*q*(-t4 + (1.0/4.0)*t5);
} else if (q < 2) {
return -(3.0/4.0)*t6*(2*q - 4) - (9.0/2.0)*t3*t2 + (3.0/2.0)*q*t5;
}
else
return 0;
}
inline static Tscal phi_tilde_3d(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
Tscal t2 = sham::pow_constexpr<3>(q);
Tscal t3 = sham::pow_constexpr<5>(q);
Tscal t4 = sham::pow_constexpr<6>(q);
Tscal t5 = sham::pow_constexpr<7>(q);
Tscal t6 = sham::pow_constexpr<8>(q);
Tscal t7 = sham::pow_constexpr<9>(q);
if (q < 1) {
return (1.0/840.0)*shambase::constants::pi<Tscal>*(t3*(35*t2 - 90*t1 + 112) - 756);
} else if (q < 2) {
return (1.0/2520.0)*shambase::constants::pi<Tscal>*(-35*t7 + 270*t6 - 720*t5 + 672*t4 - 2304*q + 20)/q;
}
else
return -(127.0/126.0)*shambase::constants::pi<Tscal>/q;
}
inline static Tscal phi_tilde_3d_prime(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
Tscal t2 = sham::pow_constexpr<3>(q);
Tscal t3 = sham::pow_constexpr<4>(q);
Tscal t4 = sham::pow_constexpr<5>(q);
Tscal t5 = sham::pow_constexpr<6>(q);
Tscal t6 = sham::pow_constexpr<7>(q);
Tscal t7 = sham::pow_constexpr<8>(q);
Tscal t8 = sham::pow_constexpr<9>(q);
if (q < 1) {
return (1.0/840.0)*shambase::constants::pi<Tscal>*(t4*(105*t1 - 180*q) + 5*t3*(35*t2 - 90*t1 + 112));
} else if (q < 2) {
return (1.0/2520.0)*shambase::constants::pi<Tscal>*(-315*t7 + 2160*t6 - 5040*t5 + 4032*t4 - 2304)/q - (1.0/2520.0)*shambase::constants::pi<Tscal>*(-35*t8 + 270*t7 - 720*t6 + 672*t5 - 2304*q + 20)/t1;
}
else
return (127.0/126.0)*shambase::constants::pi<Tscal>/t1;
}
};
------------------------------------------
Test the kernel
998 test_result_m4dh3 = test_kernel(ret)
------------------------------------------
Testing kernel M4DH3 matching with Shamrock code
------------------------------------------
Testing norms:
Shamrock norm_1d = 2.2580645161290325, expected=2.25806451612903, delta=4.44089209850063E-16
Shamrock norm_2d = 0.7073553026306459, expected=0.707355302630646, delta=0
Shamrock norm_3d = 0.31580350912722543, expected=0.315803509127225, delta=5.55111512312578E-17
Testing kernel radius:
Shamrock Rkern = 2.0, delta=0
Testing kernel functions:
Shamrock f(q) = <built-in method M4DH3_f of PyCapsule object at 0x7fbcd9bbcb70>
Shamrock df(q) = <built-in method M4DH3_df of PyCapsule object at 0x7fbcd9bbcb40>
Shamrock ddf(q) = <built-in method M4DH3_ddf of PyCapsule object at 0x7fbcd9bbcb10>
Shamrock phi_tilde_3d(q) = <built-in method M4DH3_phi_tilde_3d of PyCapsule object at 0x7fbcd9bbcae0>
Shamrock phi_tilde_3d_prime(q) = <built-in method M4DH3_phi_tilde_3d_prime of PyCapsule object at 0x7fbcd9bbcab0>
Absolute error f(q) = 1.2218718934561889186295748586557e-16
Absolute error df(q) = 2.2764428614529119995212313147607e-15
Absolute error ddf(q) = 1.0320055293124922608435806326406e-15
Absolute error phi_tilde_3d(q) = 1.8637731717387722473783472141369e-14
Absolute error phi_tilde_3d_prime(q) = 4.2032553794902827882340545209492e-14
------------------------------------------
M4DH5 Kernel#
Generate c++ code for the kernel
1008 ret = kernel_to_shamrock(m4dh5)
1011 print_kernel_info(ret)
------------------------------------------
Sympy expression for kernel M4DH5
------------------------------------------
f(q) = Piecewise((q**5*(-(1 - q)**3 + (2 - q)**3/4), q < 1), (q**5*(2 - q)**3/4, q < 2), (0, True))
df(q) = Piecewise((q**5*(3*(1 - q)**2 - 3*(2 - q)**2/4) + 5*q**4*(-(1 - q)**3 + (2 - q)**3/4), q < 1), (-3*q**5*(2 - q)**2/4 + 5*q**4*(2 - q)**3/4, q < 2), (0, True))
ddf(q) = Piecewise((q**5*(9*q/2 - 3) + 10*q**4*(3*(1 - q)**2 - 3*(2 - q)**2/4) + 20*q**3*(-(1 - q)**3 + (2 - q)**3/4), q < 1), (-3*q**5*(2*q - 4)/4 - 15*q**4*(2 - q)**2/2 + 5*q**3*(2 - q)**3, q < 2), (0, True))
phi_tilde_3d(q) = Piecewise((pi*(q**7*(63*q**3 - 154*q**2 + 165) - 2805)/2310, q < 1), (pi*(-21*q**11 + 154*q**10 - 385*q**9 + 330*q**8 - 2816*q + 7)/(2310*q), q < 2), (-511*pi/(330*q), True))
phi_tilde_3d_prime(q) = Piecewise((pi*(q**7*(189*q**2 - 308*q) + 7*q**6*(63*q**3 - 154*q**2 + 165))/2310, q < 1), (pi*(-231*q**10 + 1540*q**9 - 3465*q**8 + 2640*q**7 - 2816)/(2310*q) - pi*(-21*q**11 + 154*q**10 - 385*q**9 + 330*q**8 - 2816*q + 7)/(2310*q**2), q < 2), (511*pi/(330*q**2), True))
------------------------------------------
1014 print_kernel_cpp_code(ret)
------------------------------------------
C++ generated code for kernel M4DH5
------------------------------------------
template<class Tscal>
class KernelDefM4DH5 {
public:
inline static constexpr Tscal Rkern = 2; ///< Compact support radius of the kernel
/// default hfact to be used for this kernel
inline static constexpr Tscal hfactd = 1.2;
/// 1D norm of the kernel
inline static constexpr Tscal norm_1d = 252.0/127.0;
/// 2D norm of the kernel
inline static constexpr Tscal norm_2d = (28.0/17.0)/shambase::constants::pi<Tscal>;
/// 3D norm of the kernel
inline static constexpr Tscal norm_3d = (330.0/511.0)/shambase::constants::pi<Tscal>;
inline static Tscal f(Tscal q) {
Tscal t1 = sham::pow_constexpr<3>(1 - q);
Tscal t2 = sham::pow_constexpr<3>(2 - q);
Tscal t3 = sham::pow_constexpr<5>(q);
if (q < 1) {
return t3*(-t1 + (1.0/4.0)*t2);
} else if (q < 2) {
return (1.0/4.0)*t3*t2;
}
else
return 0;
}
inline static Tscal df(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(1 - q);
Tscal t2 = sham::pow_constexpr<2>(2 - q);
Tscal t3 = sham::pow_constexpr<3>(1 - q);
Tscal t4 = sham::pow_constexpr<3>(2 - q);
Tscal t5 = sham::pow_constexpr<4>(q);
Tscal t6 = sham::pow_constexpr<5>(q);
if (q < 1) {
return t6*(3*t1 - (3.0/4.0)*t2) + 5*t5*(-t3 + (1.0/4.0)*t4);
} else if (q < 2) {
return -(3.0/4.0)*t6*t2 + (5.0/4.0)*t5*t4;
}
else
return 0;
}
inline static Tscal ddf(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(1 - q);
Tscal t2 = sham::pow_constexpr<2>(2 - q);
Tscal t3 = sham::pow_constexpr<3>(1 - q);
Tscal t4 = sham::pow_constexpr<3>(2 - q);
Tscal t5 = sham::pow_constexpr<3>(q);
Tscal t6 = sham::pow_constexpr<4>(q);
Tscal t7 = sham::pow_constexpr<5>(q);
if (q < 1) {
return t7*((9.0/2.0)*q - 3) + 10*t6*(3*t1 - (3.0/4.0)*t2) + 20*t5*(-t3 + (1.0/4.0)*t4);
} else if (q < 2) {
return -(3.0/4.0)*t7*(2*q - 4) - (15.0/2.0)*t6*t2 + 5*t5*t4;
}
else
return 0;
}
inline static Tscal phi_tilde_3d(Tscal q) {
Tscal t1 = sham::pow_constexpr<10>(q);
Tscal t2 = sham::pow_constexpr<11>(q);
Tscal t3 = sham::pow_constexpr<2>(q);
Tscal t4 = sham::pow_constexpr<3>(q);
Tscal t5 = sham::pow_constexpr<7>(q);
Tscal t6 = sham::pow_constexpr<8>(q);
Tscal t7 = sham::pow_constexpr<9>(q);
if (q < 1) {
return (1.0/2310.0)*shambase::constants::pi<Tscal>*(t5*(63*t4 - 154*t3 + 165) - 2805);
} else if (q < 2) {
return (1.0/2310.0)*shambase::constants::pi<Tscal>*(-21*t2 + 154*t1 - 385*t7 + 330*t6 - 2816*q + 7)/q;
}
else
return -(511.0/330.0)*shambase::constants::pi<Tscal>/q;
}
inline static Tscal phi_tilde_3d_prime(Tscal q) {
Tscal t1 = sham::pow_constexpr<10>(q);
Tscal t2 = sham::pow_constexpr<11>(q);
Tscal t3 = sham::pow_constexpr<2>(q);
Tscal t4 = sham::pow_constexpr<3>(q);
Tscal t5 = sham::pow_constexpr<6>(q);
Tscal t6 = sham::pow_constexpr<7>(q);
Tscal t7 = sham::pow_constexpr<8>(q);
Tscal t8 = sham::pow_constexpr<9>(q);
if (q < 1) {
return (1.0/2310.0)*shambase::constants::pi<Tscal>*(t6*(189*t3 - 308*q) + 7*t5*(63*t4 - 154*t3 + 165));
} else if (q < 2) {
return (1.0/2310.0)*shambase::constants::pi<Tscal>*(-231*t1 + 1540*t8 - 3465*t7 + 2640*t6 - 2816)/q - (1.0/2310.0)*shambase::constants::pi<Tscal>*(-21*t2 + 154*t1 - 385*t8 + 330*t7 - 2816*q + 7)/t3;
}
else
return (511.0/330.0)*shambase::constants::pi<Tscal>/t3;
}
};
------------------------------------------
Test the kernel
1018 test_result_m4dh5 = test_kernel(ret)
------------------------------------------
Testing kernel M4DH5 matching with Shamrock code
------------------------------------------
Testing norms:
Shamrock norm_1d = 1.984251968503937, expected=1.98425196850394, delta=0
Shamrock norm_2d = 0.5242751066556552, expected=0.524275106655655, delta=0
Shamrock norm_3d = 0.20556215741810357, expected=0.205562157418104, delta=0
Testing kernel radius:
Shamrock Rkern = 2.0, delta=0
Testing kernel functions:
Shamrock f(q) = <built-in method M4DH5_f of PyCapsule object at 0x7fbcd9bbc870>
Shamrock df(q) = <built-in method M4DH5_df of PyCapsule object at 0x7fbcd9bbc840>
Shamrock ddf(q) = <built-in method M4DH5_ddf of PyCapsule object at 0x7fbcd9bbc810>
Shamrock phi_tilde_3d(q) = <built-in method M4DH5_phi_tilde_3d of PyCapsule object at 0x7fbcd9bbc7e0>
Shamrock phi_tilde_3d_prime(q) = <built-in method M4DH5_phi_tilde_3d_prime of PyCapsule object at 0x7fbcd9bbc7b0>
Absolute error f(q) = 1.1325334002180934793019450988886e-16
Absolute error df(q) = 8.9872253031621204204154022587469e-15
Absolute error ddf(q) = 3.0518560430000101442400695924271e-15
Absolute error phi_tilde_3d(q) = 4.139306340840759038659513118751e-14
Absolute error phi_tilde_3d_prime(q) = 2.1996238639070152921228687986712e-13
------------------------------------------
M4DH7 Kernel#
Generate c++ code for the kernel
1028 ret = kernel_to_shamrock(m4dh7)
1031 print_kernel_info(ret)
------------------------------------------
Sympy expression for kernel M4DH7
------------------------------------------
f(q) = Piecewise((q**7*(-(1 - q)**3 + (2 - q)**3/4), q < 1), (q**7*(2 - q)**3/4, q < 2), (0, True))
df(q) = Piecewise((q**7*(3*(1 - q)**2 - 3*(2 - q)**2/4) + 7*q**6*(-(1 - q)**3 + (2 - q)**3/4), q < 1), (-3*q**7*(2 - q)**2/4 + 7*q**6*(2 - q)**3/4, q < 2), (0, True))
ddf(q) = Piecewise((q**7*(9*q/2 - 3) + 14*q**6*(3*(1 - q)**2 - 3*(2 - q)**2/4) + 42*q**5*(-(1 - q)**3 + (2 - q)**3/4), q < 1), (-3*q**7*(2*q - 4)/4 - 21*q**6*(2 - q)**2/2 + 21*q**5*(2 - q)**3/2, q < 2), (0, True))
phi_tilde_3d(q) = Piecewise((pi*(q**9*(495*q**3 - 1170*q**2 + 1144) - 53196)/25740, q < 1), (pi*(-165*q**13 + 1170*q**12 - 2808*q**11 + 2288*q**10 - 53248*q + 36)/(25740*q), q < 2), (-2047*pi/(715*q), True))
phi_tilde_3d_prime(q) = Piecewise((pi*(q**9*(1485*q**2 - 2340*q) + 9*q**8*(495*q**3 - 1170*q**2 + 1144))/25740, q < 1), (pi*(-2145*q**12 + 14040*q**11 - 30888*q**10 + 22880*q**9 - 53248)/(25740*q) - pi*(-165*q**13 + 1170*q**12 - 2808*q**11 + 2288*q**10 - 53248*q + 36)/(25740*q**2), q < 2), (2047*pi/(715*q**2), True))
------------------------------------------
1034 print_kernel_cpp_code(ret)
------------------------------------------
C++ generated code for kernel M4DH7
------------------------------------------
template<class Tscal>
class KernelDefM4DH7 {
public:
inline static constexpr Tscal Rkern = 2; ///< Compact support radius of the kernel
/// default hfact to be used for this kernel
inline static constexpr Tscal hfactd = 1.2;
/// 1D norm of the kernel
inline static constexpr Tscal norm_1d = 660.0/511.0;
/// 2D norm of the kernel
inline static constexpr Tscal norm_2d = (30.0/31.0)/shambase::constants::pi<Tscal>;
/// 3D norm of the kernel
inline static constexpr Tscal norm_3d = (715.0/2047.0)/shambase::constants::pi<Tscal>;
inline static Tscal f(Tscal q) {
Tscal t1 = sham::pow_constexpr<3>(1 - q);
Tscal t2 = sham::pow_constexpr<3>(2 - q);
Tscal t3 = sham::pow_constexpr<7>(q);
if (q < 1) {
return t3*(-t1 + (1.0/4.0)*t2);
} else if (q < 2) {
return (1.0/4.0)*t3*t2;
}
else
return 0;
}
inline static Tscal df(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(1 - q);
Tscal t2 = sham::pow_constexpr<2>(2 - q);
Tscal t3 = sham::pow_constexpr<3>(1 - q);
Tscal t4 = sham::pow_constexpr<3>(2 - q);
Tscal t5 = sham::pow_constexpr<6>(q);
Tscal t6 = sham::pow_constexpr<7>(q);
if (q < 1) {
return t6*(3*t1 - (3.0/4.0)*t2) + 7*t5*(-t3 + (1.0/4.0)*t4);
} else if (q < 2) {
return -(3.0/4.0)*t6*t2 + (7.0/4.0)*t5*t4;
}
else
return 0;
}
inline static Tscal ddf(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(1 - q);
Tscal t2 = sham::pow_constexpr<2>(2 - q);
Tscal t3 = sham::pow_constexpr<3>(1 - q);
Tscal t4 = sham::pow_constexpr<3>(2 - q);
Tscal t5 = sham::pow_constexpr<5>(q);
Tscal t6 = sham::pow_constexpr<6>(q);
Tscal t7 = sham::pow_constexpr<7>(q);
if (q < 1) {
return t7*((9.0/2.0)*q - 3) + 14*t6*(3*t1 - (3.0/4.0)*t2) + 42*t5*(-t3 + (1.0/4.0)*t4);
} else if (q < 2) {
return -(3.0/4.0)*t7*(2*q - 4) - (21.0/2.0)*t6*t2 + (21.0/2.0)*t5*t4;
}
else
return 0;
}
inline static Tscal phi_tilde_3d(Tscal q) {
Tscal t1 = sham::pow_constexpr<10>(q);
Tscal t2 = sham::pow_constexpr<11>(q);
Tscal t3 = sham::pow_constexpr<12>(q);
Tscal t4 = sham::pow_constexpr<13>(q);
Tscal t5 = sham::pow_constexpr<2>(q);
Tscal t6 = sham::pow_constexpr<3>(q);
Tscal t7 = sham::pow_constexpr<9>(q);
if (q < 1) {
return (1.0/25740.0)*shambase::constants::pi<Tscal>*(t7*(495*t6 - 1170*t5 + 1144) - 53196);
} else if (q < 2) {
return (1.0/25740.0)*shambase::constants::pi<Tscal>*(-165*t4 + 1170*t3 - 2808*t2 + 2288*t1 - 53248*q + 36)/q;
}
else
return -(2047.0/715.0)*shambase::constants::pi<Tscal>/q;
}
inline static Tscal phi_tilde_3d_prime(Tscal q) {
Tscal t1 = sham::pow_constexpr<10>(q);
Tscal t2 = sham::pow_constexpr<11>(q);
Tscal t3 = sham::pow_constexpr<12>(q);
Tscal t4 = sham::pow_constexpr<13>(q);
Tscal t5 = sham::pow_constexpr<2>(q);
Tscal t6 = sham::pow_constexpr<3>(q);
Tscal t7 = sham::pow_constexpr<8>(q);
Tscal t8 = sham::pow_constexpr<9>(q);
if (q < 1) {
return (1.0/25740.0)*shambase::constants::pi<Tscal>*(t8*(1485*t5 - 2340*q) + 9*t7*(495*t6 - 1170*t5 + 1144));
} else if (q < 2) {
return (1.0/25740.0)*shambase::constants::pi<Tscal>*(-2145*t3 + 14040*t2 - 30888*t1 + 22880*t8 - 53248)/q - (1.0/25740.0)*shambase::constants::pi<Tscal>*(-165*t4 + 1170*t3 - 2808*t2 + 2288*t1 - 53248*q + 36)/t5;
}
else
return (2047.0/715.0)*shambase::constants::pi<Tscal>/t5;
}
};
------------------------------------------
Test the kernel
1038 test_result_m4dh7 = test_kernel(ret)
------------------------------------------
Testing kernel M4DH7 matching with Shamrock code
------------------------------------------
Testing norms:
Shamrock norm_1d = 1.2915851272015655, expected=1.29158512720157, delta=0
Shamrock norm_2d = 0.3080418253391523, expected=0.308041825339152, delta=5.55111512312578E-17
Shamrock norm_3d = 0.11118298418241834, expected=0.111182984182418, delta=1.38777878078145E-17
Testing kernel radius:
Shamrock Rkern = 2.0, delta=0
Testing kernel functions:
Shamrock f(q) = <built-in method M4DH7_f of PyCapsule object at 0x7fbcd9bbc450>
Shamrock df(q) = <built-in method M4DH7_df of PyCapsule object at 0x7fbcd9bbc420>
Shamrock ddf(q) = <built-in method M4DH7_ddf of PyCapsule object at 0x7fbcd9bbc3f0>
Shamrock phi_tilde_3d(q) = <built-in method M4DH7_phi_tilde_3d of PyCapsule object at 0x7fbcd9bbc3c0>
Shamrock phi_tilde_3d_prime(q) = <built-in method M4DH7_phi_tilde_3d_prime of PyCapsule object at 0x7fbcd9bbc390>
Absolute error f(q) = 2.7745488273644668238686811902901e-16
Absolute error df(q) = 3.5482969028749994264124678817261e-14
Absolute error ddf(q) = 8.9764994763351031453229854274657e-15
Absolute error phi_tilde_3d(q) = 1.1410837367852384316621894214667e-13
Absolute error phi_tilde_3d_prime(q) = 4.2978977992024483366058950511763e-13
------------------------------------------
M4Shift2 Kernel#
Generate c++ code for the kernel
1048 ret = kernel_to_shamrock(m4shift2)
1051 print_kernel_info(ret)
------------------------------------------
Sympy expression for kernel M4Shift2
------------------------------------------
f(q) = Piecewise((1, q < 1), (-(3 - 2*q)**3 + (4 - 2*q)**3/4, q < 3/2), ((4 - 2*q)**3/4, q < 2), (0, True))
df(q) = Piecewise((0, q < 1), (6*(3 - 2*q)**2 - 3*(4 - 2*q)**2/2, q < 3/2), (-3*(4 - 2*q)**2/2, q < 2), (0, True))
ddf(q) = Piecewise((0, q < 1), (36*q - 48, q < 3/2), (24 - 12*q, q < 2), (0, True))
phi_tilde_3d(q) = Piecewise((pi*(40*q**2 - 231)/60, q < 1), (pi*(48*q**6 - 288*q**5 + 600*q**4 - 440*q**3 - 39*q - 72)/(60*q), q < 3/2), (pi*(-32*q**6 + 288*q**5 - 960*q**4 + 1280*q**3 - 1536*q + 585)/(120*q), q < 2), (-439*pi/(120*q), True))
phi_tilde_3d_prime(q) = Piecewise((4*pi*q/3, q < 1), (pi*(288*q**5 - 1440*q**4 + 2400*q**3 - 1320*q**2 - 39)/(60*q) - pi*(48*q**6 - 288*q**5 + 600*q**4 - 440*q**3 - 39*q - 72)/(60*q**2), q < 3/2), (pi*(-192*q**5 + 1440*q**4 - 3840*q**3 + 3840*q**2 - 1536)/(120*q) - pi*(-32*q**6 + 288*q**5 - 960*q**4 + 1280*q**3 - 1536*q + 585)/(120*q**2), q < 2), (439*pi/(120*q**2), True))
------------------------------------------
1054 print_kernel_cpp_code(ret)
------------------------------------------
C++ generated code for kernel M4Shift2
------------------------------------------
template<class Tscal>
class KernelDefM4Shift2 {
public:
inline static constexpr Tscal Rkern = 2; ///< Compact support radius of the kernel
/// default hfact to be used for this kernel
inline static constexpr Tscal hfactd = 1.2;
/// 1D norm of the kernel
inline static constexpr Tscal norm_1d = 4.0/11.0;
/// 2D norm of the kernel
inline static constexpr Tscal norm_2d = (40.0/77.0)/shambase::constants::pi<Tscal>;
/// 3D norm of the kernel
inline static constexpr Tscal norm_3d = (120.0/439.0)/shambase::constants::pi<Tscal>;
inline static Tscal f(Tscal q) {
Tscal t1 = sham::pow_constexpr<3>(3 - 2*q);
Tscal t2 = sham::pow_constexpr<3>(4 - 2*q);
if (q < 1) {
return 1;
} else if (q < 3.0/2.0) {
return -t1 + (1.0/4.0)*t2;
} else if (q < 2) {
return (1.0/4.0)*t2;
}
else
return 0;
}
inline static Tscal df(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(3 - 2*q);
Tscal t2 = sham::pow_constexpr<2>(4 - 2*q);
if (q < 1) {
return 0;
} else if (q < 3.0/2.0) {
return 6*t1 - (3.0/2.0)*t2;
} else if (q < 2) {
return -(3.0/2.0)*t2;
}
else
return 0;
}
inline static Tscal ddf(Tscal q) {
if (q < 1) {
return 0;
} else if (q < 3.0/2.0) {
return 36*q - 48;
} else if (q < 2) {
return 24 - 12*q;
}
else
return 0;
}
inline static Tscal phi_tilde_3d(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
Tscal t2 = sham::pow_constexpr<3>(q);
Tscal t3 = sham::pow_constexpr<4>(q);
Tscal t4 = sham::pow_constexpr<5>(q);
Tscal t5 = sham::pow_constexpr<6>(q);
if (q < 1) {
return (1.0/60.0)*shambase::constants::pi<Tscal>*(40*t1 - 231);
} else if (q < 3.0/2.0) {
return (1.0/60.0)*shambase::constants::pi<Tscal>*(48*t5 - 288*t4 + 600*t3 - 440*t2 - 39*q - 72)/q;
} else if (q < 2) {
return (1.0/120.0)*shambase::constants::pi<Tscal>*(-32*t5 + 288*t4 - 960*t3 + 1280*t2 - 1536*q + 585)/q;
}
else
return -(439.0/120.0)*shambase::constants::pi<Tscal>/q;
}
inline static Tscal phi_tilde_3d_prime(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
Tscal t2 = sham::pow_constexpr<3>(q);
Tscal t3 = sham::pow_constexpr<4>(q);
Tscal t4 = sham::pow_constexpr<5>(q);
Tscal t5 = sham::pow_constexpr<6>(q);
if (q < 1) {
return (4.0/3.0)*shambase::constants::pi<Tscal>*q;
} else if (q < 3.0/2.0) {
return (1.0/60.0)*shambase::constants::pi<Tscal>*(288*t4 - 1440*t3 + 2400*t2 - 1320*t1 - 39)/q - (1.0/60.0)*shambase::constants::pi<Tscal>*(48*t5 - 288*t4 + 600*t3 - 440*t2 - 39*q - 72)/t1;
} else if (q < 2) {
return (1.0/120.0)*shambase::constants::pi<Tscal>*(-192*t4 + 1440*t3 - 3840*t2 + 3840*t1 - 1536)/q - (1.0/120.0)*shambase::constants::pi<Tscal>*(-32*t5 + 288*t4 - 960*t3 + 1280*t2 - 1536*q + 585)/t1;
}
else
return (439.0/120.0)*shambase::constants::pi<Tscal>/t1;
}
};
------------------------------------------
Test the kernel
1058 test_result_m4shift2 = test_kernel(ret)
------------------------------------------
Testing kernel M4Shift2 matching with Shamrock code
------------------------------------------
Testing norms:
Shamrock norm_1d = 0.36363636363636365, expected=0.363636363636364, delta=0
Shamrock norm_2d = 0.1653557850305406, expected=0.165355785030541, delta=0
Shamrock norm_3d = 0.08700953608668538, expected=0.0870095360866854, delta=0
Testing kernel radius:
Shamrock Rkern = 2.0, delta=0
Testing kernel functions:
Shamrock f(q) = <built-in method M4Shift2_f of PyCapsule object at 0x7fbcd9bbc150>
Shamrock df(q) = <built-in method M4Shift2_df of PyCapsule object at 0x7fbcd9bbc120>
Shamrock ddf(q) = <built-in method M4Shift2_ddf of PyCapsule object at 0x7fbcd9bbc0f0>
Shamrock phi_tilde_3d(q) = <built-in method M4Shift2_phi_tilde_3d of PyCapsule object at 0x7fbcd9bbc0c0>
Shamrock phi_tilde_3d_prime(q) = <built-in method M4Shift2_phi_tilde_3d_prime of PyCapsule object at 0x7fbcd9bbc090>
Absolute error f(q) = 3.3306690738754696212708950042725e-16
Absolute error df(q) = 9.7144514654701197287067770957947e-16
Absolute error ddf(q) = 1.7763568394002505e-15
Absolute error phi_tilde_3d(q) = 4.8057488080825312374425800768037e-14
Absolute error phi_tilde_3d_prime(q) = 8.7604806542352871750438831278774e-14
------------------------------------------
M4Shift4 Kernel#
Generate c++ code for the kernel
1068 ret = kernel_to_shamrock(m4shift4)
1071 print_kernel_info(ret)
------------------------------------------
Sympy expression for kernel M4Shift4
------------------------------------------
f(q) = Piecewise((1, q < 3/2), (-(7 - 4*q)**3 + (8 - 4*q)**3/4, q < 7/4), ((8 - 4*q)**3/4, q < 2), (0, True))
df(q) = Piecewise((0, q < 3/2), (12*(7 - 4*q)**2 - 3*(8 - 4*q)**2, q < 7/4), (-3*(8 - 4*q)**2, q < 2), (0, True))
ddf(q) = Piecewise((0, q < 3/2), (288*q - 480, q < 7/4), (192 - 96*q, q < 2), (0, True))
phi_tilde_3d(q) = Piecewise((pi*(160*q**2 - 1371)/240, q < 3/2), (pi*(1536*q**6 - 11520*q**5 + 31680*q**4 - 34400*q**3 + 25845*q - 14580)/(240*q), q < 7/4), (pi*(-2048*q**6 + 18432*q**5 - 61440*q**4 + 81920*q**3 - 98304*q + 59329)/(960*q), q < 2), (-2069*pi/(320*q), True))
phi_tilde_3d_prime(q) = Piecewise((4*pi*q/3, q < 3/2), (pi*(9216*q**5 - 57600*q**4 + 126720*q**3 - 103200*q**2 + 25845)/(240*q) - pi*(1536*q**6 - 11520*q**5 + 31680*q**4 - 34400*q**3 + 25845*q - 14580)/(240*q**2), q < 7/4), (pi*(-12288*q**5 + 92160*q**4 - 245760*q**3 + 245760*q**2 - 98304)/(960*q) - pi*(-2048*q**6 + 18432*q**5 - 61440*q**4 + 81920*q**3 - 98304*q + 59329)/(960*q**2), q < 2), (2069*pi/(320*q**2), True))
------------------------------------------
1074 print_kernel_cpp_code(ret)
------------------------------------------
C++ generated code for kernel M4Shift4
------------------------------------------
template<class Tscal>
class KernelDefM4Shift4 {
public:
inline static constexpr Tscal Rkern = 2; ///< Compact support radius of the kernel
/// default hfact to be used for this kernel
inline static constexpr Tscal hfactd = 1.2;
/// 1D norm of the kernel
inline static constexpr Tscal norm_1d = 8.0/27.0;
/// 2D norm of the kernel
inline static constexpr Tscal norm_2d = (160.0/457.0)/shambase::constants::pi<Tscal>;
/// 3D norm of the kernel
inline static constexpr Tscal norm_3d = (320.0/2069.0)/shambase::constants::pi<Tscal>;
inline static Tscal f(Tscal q) {
Tscal t1 = sham::pow_constexpr<3>(7 - 4*q);
Tscal t2 = sham::pow_constexpr<3>(8 - 4*q);
if (q < 3.0/2.0) {
return 1;
} else if (q < 7.0/4.0) {
return -t1 + (1.0/4.0)*t2;
} else if (q < 2) {
return (1.0/4.0)*t2;
}
else
return 0;
}
inline static Tscal df(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(7 - 4*q);
Tscal t2 = sham::pow_constexpr<2>(8 - 4*q);
if (q < 3.0/2.0) {
return 0;
} else if (q < 7.0/4.0) {
return 12*t1 - 3*t2;
} else if (q < 2) {
return -3*t2;
}
else
return 0;
}
inline static Tscal ddf(Tscal q) {
if (q < 3.0/2.0) {
return 0;
} else if (q < 7.0/4.0) {
return 288*q - 480;
} else if (q < 2) {
return 192 - 96*q;
}
else
return 0;
}
inline static Tscal phi_tilde_3d(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
Tscal t2 = sham::pow_constexpr<3>(q);
Tscal t3 = sham::pow_constexpr<4>(q);
Tscal t4 = sham::pow_constexpr<5>(q);
Tscal t5 = sham::pow_constexpr<6>(q);
if (q < 3.0/2.0) {
return (1.0/240.0)*shambase::constants::pi<Tscal>*(160*t1 - 1371);
} else if (q < 7.0/4.0) {
return (1.0/240.0)*shambase::constants::pi<Tscal>*(1536*t5 - 11520*t4 + 31680*t3 - 34400*t2 + 25845*q - 14580)/q;
} else if (q < 2) {
return (1.0/960.0)*shambase::constants::pi<Tscal>*(-2048*t5 + 18432*t4 - 61440*t3 + 81920*t2 - 98304*q + 59329)/q;
}
else
return -(2069.0/320.0)*shambase::constants::pi<Tscal>/q;
}
inline static Tscal phi_tilde_3d_prime(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
Tscal t2 = sham::pow_constexpr<3>(q);
Tscal t3 = sham::pow_constexpr<4>(q);
Tscal t4 = sham::pow_constexpr<5>(q);
Tscal t5 = sham::pow_constexpr<6>(q);
if (q < 3.0/2.0) {
return (4.0/3.0)*shambase::constants::pi<Tscal>*q;
} else if (q < 7.0/4.0) {
return (1.0/240.0)*shambase::constants::pi<Tscal>*(9216*t4 - 57600*t3 + 126720*t2 - 103200*t1 + 25845)/q - (1.0/240.0)*shambase::constants::pi<Tscal>*(1536*t5 - 11520*t4 + 31680*t3 - 34400*t2 + 25845*q - 14580)/t1;
} else if (q < 2) {
return (1.0/960.0)*shambase::constants::pi<Tscal>*(-12288*t4 + 92160*t3 - 245760*t2 + 245760*t1 - 98304)/q - (1.0/960.0)*shambase::constants::pi<Tscal>*(-2048*t5 + 18432*t4 - 61440*t3 + 81920*t2 - 98304*q + 59329)/t1;
}
else
return (2069.0/320.0)*shambase::constants::pi<Tscal>/t1;
}
};
------------------------------------------
Test the kernel
1078 test_result_m4shift4 = test_kernel(ret)
------------------------------------------
Testing kernel M4Shift4 matching with Shamrock code
------------------------------------------
Testing norms:
Shamrock norm_1d = 0.2962962962962963, expected=0.296296296296296, delta=0
Shamrock norm_2d = 0.11144328619126151, expected=0.111443286191262, delta=0
Shamrock norm_3d = 0.049231108544617215, expected=0.0492311085446172, delta=0
Testing kernel radius:
Shamrock Rkern = 2.0, delta=0
Testing kernel functions:
Shamrock f(q) = <built-in method M4Shift4_f of PyCapsule object at 0x7fbcd9b7fba0>
Shamrock df(q) = <built-in method M4Shift4_df of PyCapsule object at 0x7fbcd9b7fd50>
Shamrock ddf(q) = <built-in method M4Shift4_ddf of PyCapsule object at 0x7fbcd9b7fd20>
Shamrock phi_tilde_3d(q) = <built-in method M4Shift4_phi_tilde_3d of PyCapsule object at 0x7fbcd9b7fdb0>
Shamrock phi_tilde_3d_prime(q) = <built-in method M4Shift4_phi_tilde_3d_prime of PyCapsule object at 0x7fbcd9b7fde0>
Absolute error f(q) = 2.7755575615628913510590791702271e-16
Absolute error df(q) = 2.220446049250313e-15
Absolute error ddf(q) = 2.842170943040401e-14
Absolute error phi_tilde_3d(q) = 4.4156542197657458622817453458733e-13
Absolute error phi_tilde_3d_prime(q) = 1.0539828786480343310056803196174e-12
------------------------------------------
M4Shift8 Kernel#
Generate c++ code for the kernel
1088 ret = kernel_to_shamrock(m4shift8)
1091 print_kernel_info(ret)
------------------------------------------
Sympy expression for kernel M4Shift8
------------------------------------------
f(q) = Piecewise((1, q < 7/4), (-(15 - 8*q)**3 + (16 - 8*q)**3/4, q < 15/8), ((16 - 8*q)**3/4, q < 2), (0, True))
df(q) = Piecewise((0, q < 7/4), (24*(15 - 8*q)**2 - 6*(16 - 8*q)**2, q < 15/8), (-6*(16 - 8*q)**2, q < 2), (0, True))
ddf(q) = Piecewise((0, q < 7/4), (2304*q - 4224, q < 15/8), (1536 - 768*q, q < 2), (0, True))
phi_tilde_3d(q) = Piecewise((pi*(640*q**2 - 6531)/960, q < 7/4), (pi*(49152*q**6 - 405504*q**5 + 1236480*q**4 - 1504640*q**3 + 1491693*q - 907578)/(960*q), q < 15/8), (pi*(-131072*q**6 + 1179648*q**5 - 3932160*q**4 + 5242880*q**3 - 6291456*q + 4130001)/(7680*q), q < 2), (-64303*pi/(7680*q), True))
phi_tilde_3d_prime(q) = Piecewise((4*pi*q/3, q < 7/4), (pi*(294912*q**5 - 2027520*q**4 + 4945920*q**3 - 4513920*q**2 + 1491693)/(960*q) - pi*(49152*q**6 - 405504*q**5 + 1236480*q**4 - 1504640*q**3 + 1491693*q - 907578)/(960*q**2), q < 15/8), (pi*(-786432*q**5 + 5898240*q**4 - 15728640*q**3 + 15728640*q**2 - 6291456)/(7680*q) - pi*(-131072*q**6 + 1179648*q**5 - 3932160*q**4 + 5242880*q**3 - 6291456*q + 4130001)/(7680*q**2), q < 2), (64303*pi/(7680*q**2), True))
------------------------------------------
1094 print_kernel_cpp_code(ret)
------------------------------------------
C++ generated code for kernel M4Shift8
------------------------------------------
template<class Tscal>
class KernelDefM4Shift8 {
public:
inline static constexpr Tscal Rkern = 2; ///< Compact support radius of the kernel
/// default hfact to be used for this kernel
inline static constexpr Tscal hfactd = 1.2;
/// 1D norm of the kernel
inline static constexpr Tscal norm_1d = 16.0/59.0;
/// 2D norm of the kernel
inline static constexpr Tscal norm_2d = (640.0/2177.0)/shambase::constants::pi<Tscal>;
/// 3D norm of the kernel
inline static constexpr Tscal norm_3d = (7680.0/64303.0)/shambase::constants::pi<Tscal>;
inline static Tscal f(Tscal q) {
Tscal t1 = sham::pow_constexpr<3>(15 - 8*q);
Tscal t2 = sham::pow_constexpr<3>(16 - 8*q);
if (q < 7.0/4.0) {
return 1;
} else if (q < 15.0/8.0) {
return -t1 + (1.0/4.0)*t2;
} else if (q < 2) {
return (1.0/4.0)*t2;
}
else
return 0;
}
inline static Tscal df(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(15 - 8*q);
Tscal t2 = sham::pow_constexpr<2>(16 - 8*q);
if (q < 7.0/4.0) {
return 0;
} else if (q < 15.0/8.0) {
return 24*t1 - 6*t2;
} else if (q < 2) {
return -6*t2;
}
else
return 0;
}
inline static Tscal ddf(Tscal q) {
if (q < 7.0/4.0) {
return 0;
} else if (q < 15.0/8.0) {
return 2304*q - 4224;
} else if (q < 2) {
return 1536 - 768*q;
}
else
return 0;
}
inline static Tscal phi_tilde_3d(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
Tscal t2 = sham::pow_constexpr<3>(q);
Tscal t3 = sham::pow_constexpr<4>(q);
Tscal t4 = sham::pow_constexpr<5>(q);
Tscal t5 = sham::pow_constexpr<6>(q);
if (q < 7.0/4.0) {
return (1.0/960.0)*shambase::constants::pi<Tscal>*(640*t1 - 6531);
} else if (q < 15.0/8.0) {
return (1.0/960.0)*shambase::constants::pi<Tscal>*(49152*t5 - 405504*t4 + 1236480*t3 - 1504640*t2 + 1491693*q - 907578)/q;
} else if (q < 2) {
return (1.0/7680.0)*shambase::constants::pi<Tscal>*(-131072*t5 + 1179648*t4 - 3932160*t3 + 5242880*t2 - 6291456*q + 4130001)/q;
}
else
return -(64303.0/7680.0)*shambase::constants::pi<Tscal>/q;
}
inline static Tscal phi_tilde_3d_prime(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
Tscal t2 = sham::pow_constexpr<3>(q);
Tscal t3 = sham::pow_constexpr<4>(q);
Tscal t4 = sham::pow_constexpr<5>(q);
Tscal t5 = sham::pow_constexpr<6>(q);
if (q < 7.0/4.0) {
return (4.0/3.0)*shambase::constants::pi<Tscal>*q;
} else if (q < 15.0/8.0) {
return (1.0/960.0)*shambase::constants::pi<Tscal>*(294912*t4 - 2027520*t3 + 4945920*t2 - 4513920*t1 + 1491693)/q - (1.0/960.0)*shambase::constants::pi<Tscal>*(49152*t5 - 405504*t4 + 1236480*t3 - 1504640*t2 + 1491693*q - 907578)/t1;
} else if (q < 2) {
return (1.0/7680.0)*shambase::constants::pi<Tscal>*(-786432*t4 + 5898240*t3 - 15728640*t2 + 15728640*t1 - 6291456)/q - (1.0/7680.0)*shambase::constants::pi<Tscal>*(-131072*t5 + 1179648*t4 - 3932160*t3 + 5242880*t2 - 6291456*q + 4130001)/t1;
}
else
return (64303.0/7680.0)*shambase::constants::pi<Tscal>/t1;
}
};
------------------------------------------
Test the kernel
1098 test_result_m4shift8 = test_kernel(ret)
------------------------------------------
Testing kernel M4Shift8 matching with Shamrock code
------------------------------------------
Testing norms:
Shamrock norm_1d = 0.2711864406779661, expected=0.271186440677966, delta=0
Shamrock norm_2d = 0.09357755037098117, expected=0.0935775503709812, delta=1.38777878078145E-17
Shamrock norm_3d = 0.03801719866711526, expected=0.0380171986671153, delta=6.93889390390723E-18
Testing kernel radius:
Shamrock Rkern = 2.0, delta=0
Testing kernel functions:
Shamrock f(q) = <built-in method M4Shift8_f of PyCapsule object at 0x7fbcd9b7f9c0>
Shamrock df(q) = <built-in method M4Shift8_df of PyCapsule object at 0x7fbcd9b7f9f0>
Shamrock ddf(q) = <built-in method M4Shift8_ddf of PyCapsule object at 0x7fbcd9b7fa50>
Shamrock phi_tilde_3d(q) = <built-in method M4Shift8_phi_tilde_3d of PyCapsule object at 0x7fbcd9b7fa80>
Shamrock phi_tilde_3d_prime(q) = <built-in method M4Shift8_phi_tilde_3d_prime of PyCapsule object at 0x7fbcd9b7fae0>
Absolute error f(q) = 3.3306690738754696212708950042725e-16
Absolute error df(q) = 5.773159728050814e-15
Absolute error ddf(q) = 2.8421709430404007e-13
Absolute error phi_tilde_3d(q) = 4.8963625190753593596772964897216e-12
Absolute error phi_tilde_3d_prime(q) = 8.8836344431557297504000270626971e-12
------------------------------------------
M4Shift16 Kernel#
Generate c++ code for the kernel
1108 ret = kernel_to_shamrock(m4shift16)
1111 print_kernel_info(ret)
------------------------------------------
Sympy expression for kernel M4Shift16
------------------------------------------
f(q) = Piecewise((1, q < 15/8), (-(31 - 16*q)**3 + (32 - 16*q)**3/4, q < 31/16), ((32 - 16*q)**3/4, q < 2), (0, True))
df(q) = Piecewise((0, q < 15/8), (48*(31 - 16*q)**2 - 12*(32 - 16*q)**2, q < 31/16), (-12*(32 - 16*q)**2, q < 2), (0, True))
ddf(q) = Piecewise((0, q < 15/8), (18432*q - 35328, q < 31/16), (12288 - 6144*q, q < 2), (0, True))
phi_tilde_3d(q) = Piecewise((pi*(2560*q**2 - 28371)/3840, q < 15/8), (pi*(1572864*q**6 - 13565952*q**5 + 43315200*q**4 - 55293440*q**3 + 60721629*q - 38728125)/(3840*q), q < 31/16), (pi*(-8388608*q**6 + 75497472*q**5 - 251658240*q**4 + 335544320*q**3 - 402653184*q + 267853681)/(61440*q), q < 2), (-38785*pi/(4096*q), True))
phi_tilde_3d_prime(q) = Piecewise((4*pi*q/3, q < 15/8), (pi*(9437184*q**5 - 67829760*q**4 + 173260800*q**3 - 165880320*q**2 + 60721629)/(3840*q) - pi*(1572864*q**6 - 13565952*q**5 + 43315200*q**4 - 55293440*q**3 + 60721629*q - 38728125)/(3840*q**2), q < 31/16), (pi*(-50331648*q**5 + 377487360*q**4 - 1006632960*q**3 + 1006632960*q**2 - 402653184)/(61440*q) - pi*(-8388608*q**6 + 75497472*q**5 - 251658240*q**4 + 335544320*q**3 - 402653184*q + 267853681)/(61440*q**2), q < 2), (38785*pi/(4096*q**2), True))
------------------------------------------
1114 print_kernel_cpp_code(ret)
------------------------------------------
C++ generated code for kernel M4Shift16
------------------------------------------
template<class Tscal>
class KernelDefM4Shift16 {
public:
inline static constexpr Tscal Rkern = 2; ///< Compact support radius of the kernel
/// default hfact to be used for this kernel
inline static constexpr Tscal hfactd = 1.2;
/// 1D norm of the kernel
inline static constexpr Tscal norm_1d = 32.0/123.0;
/// 2D norm of the kernel
inline static constexpr Tscal norm_2d = (2560.0/9457.0)/shambase::constants::pi<Tscal>;
/// 3D norm of the kernel
inline static constexpr Tscal norm_3d = (4096.0/38785.0)/shambase::constants::pi<Tscal>;
inline static Tscal f(Tscal q) {
Tscal t1 = sham::pow_constexpr<3>(31 - 16*q);
Tscal t2 = sham::pow_constexpr<3>(32 - 16*q);
if (q < 15.0/8.0) {
return 1;
} else if (q < 31.0/16.0) {
return -t1 + (1.0/4.0)*t2;
} else if (q < 2) {
return (1.0/4.0)*t2;
}
else
return 0;
}
inline static Tscal df(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(31 - 16*q);
Tscal t2 = sham::pow_constexpr<2>(32 - 16*q);
if (q < 15.0/8.0) {
return 0;
} else if (q < 31.0/16.0) {
return 48*t1 - 12*t2;
} else if (q < 2) {
return -12*t2;
}
else
return 0;
}
inline static Tscal ddf(Tscal q) {
if (q < 15.0/8.0) {
return 0;
} else if (q < 31.0/16.0) {
return 18432*q - 35328;
} else if (q < 2) {
return 12288 - 6144*q;
}
else
return 0;
}
inline static Tscal phi_tilde_3d(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
Tscal t2 = sham::pow_constexpr<3>(q);
Tscal t3 = sham::pow_constexpr<4>(q);
Tscal t4 = sham::pow_constexpr<5>(q);
Tscal t5 = sham::pow_constexpr<6>(q);
if (q < 15.0/8.0) {
return (1.0/3840.0)*shambase::constants::pi<Tscal>*(2560*t1 - 28371);
} else if (q < 31.0/16.0) {
return (1.0/3840.0)*shambase::constants::pi<Tscal>*(1572864*t5 - 13565952*t4 + 43315200*t3 - 55293440*t2 + 60721629*q - 38728125)/q;
} else if (q < 2) {
return (1.0/61440.0)*shambase::constants::pi<Tscal>*(-8388608*t5 + 75497472*t4 - 251658240*t3 + 335544320*t2 - 402653184*q + 267853681)/q;
}
else
return -(38785.0/4096.0)*shambase::constants::pi<Tscal>/q;
}
inline static Tscal phi_tilde_3d_prime(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
Tscal t2 = sham::pow_constexpr<3>(q);
Tscal t3 = sham::pow_constexpr<4>(q);
Tscal t4 = sham::pow_constexpr<5>(q);
Tscal t5 = sham::pow_constexpr<6>(q);
if (q < 15.0/8.0) {
return (4.0/3.0)*shambase::constants::pi<Tscal>*q;
} else if (q < 31.0/16.0) {
return (1.0/3840.0)*shambase::constants::pi<Tscal>*(9437184*t4 - 67829760*t3 + 173260800*t2 - 165880320*t1 + 60721629)/q - (1.0/3840.0)*shambase::constants::pi<Tscal>*(1572864*t5 - 13565952*t4 + 43315200*t3 - 55293440*t2 + 60721629*q - 38728125)/t1;
} else if (q < 2) {
return (1.0/61440.0)*shambase::constants::pi<Tscal>*(-50331648*t4 + 377487360*t3 - 1006632960*t2 + 1006632960*t1 - 402653184)/q - (1.0/61440.0)*shambase::constants::pi<Tscal>*(-8388608*t5 + 75497472*t4 - 251658240*t3 + 335544320*t2 - 402653184*q + 267853681)/t1;
}
else
return (38785.0/4096.0)*shambase::constants::pi<Tscal>/t1;
}
};
------------------------------------------
Test the kernel
1118 test_result_m4shift16 = test_kernel(ret)
------------------------------------------
Testing kernel M4Shift16 matching with Shamrock code
------------------------------------------
Testing norms:
Shamrock norm_1d = 0.2601626016260163, expected=0.260162601626016, delta=5.55111512312578E-17
Shamrock norm_2d = 0.08616615296928246, expected=0.0861661529692825, delta=0
Shamrock norm_3d = 0.03361601891991251, expected=0.0336160189199125, delta=6.93889390390723E-18
Testing kernel radius:
Shamrock Rkern = 2.0, delta=0
Testing kernel functions:
Shamrock f(q) = <built-in method M4Shift16_f of PyCapsule object at 0x7fbcd9b7f900>
Shamrock df(q) = <built-in method M4Shift16_df of PyCapsule object at 0x7fbcd9b7f8d0>
Shamrock ddf(q) = <built-in method M4Shift16_ddf of PyCapsule object at 0x7fbcd9b7f8a0>
Shamrock phi_tilde_3d(q) = <built-in method M4Shift16_phi_tilde_3d of PyCapsule object at 0x7fbcd9b7f870>
Shamrock phi_tilde_3d_prime(q) = <built-in method M4Shift16_phi_tilde_3d_prime of PyCapsule object at 0x7fbcd9b7f840>
Absolute error f(q) = 3.3306690738754696212708950042725e-16
Absolute error df(q) = 3.552713678800501e-15
Absolute error ddf(q) = 2.2737367544323206e-12
Absolute error phi_tilde_3d(q) = 5.1146640037939727802974118892356e-11
Absolute error phi_tilde_3d_prime(q) = 7.6346989922726302352262500496407e-11
------------------------------------------
TGauss3 Kernel#
Generate c++ code for the kernel
1127 ret = kernel_to_shamrock(tgauss3)
1129 print_kernel_info(ret)
------------------------------------------
Sympy expression for kernel TGauss3
------------------------------------------
f(q) = Piecewise((exp(-q**2), q < 3), (0, True))
df(q) = Piecewise((-2*q*exp(-q**2), q < 3), (0, True))
ddf(q) = Piecewise((4*q**2*exp(-q**2) - 2*exp(-q**2), q < 3), (0, True))
phi_tilde_3d(q) = Piecewise((2*pi*exp(-9) - pi**(3/2)*erf(q)/q, q < 3), ((-pi**(3/2)*erf(3) + 6*pi*exp(-9))/q, True))
phi_tilde_3d_prime(q) = Piecewise((-2*pi*exp(-q**2)/q + pi**(3/2)*erf(q)/q**2, q < 3), (-(-pi**(3/2)*erf(3) + 6*pi*exp(-9))/q**2, True))
------------------------------------------
1131 print_kernel_cpp_code(ret)
------------------------------------------
C++ generated code for kernel TGauss3
------------------------------------------
template<class Tscal>
class KernelDefTGauss3 {
public:
inline static constexpr Tscal Rkern = 3; ///< Compact support radius of the kernel
/// default hfact to be used for this kernel
inline static constexpr Tscal hfactd = 1.2;
/// 1D norm of the kernel
inline static constexpr Tscal norm_1d = 1/(sqrt(shambase::constants::pi<Tscal>)*erf(3));
/// 2D norm of the kernel
inline static constexpr Tscal norm_2d = 1.0/(-shambase::constants::pi<Tscal>*exp(-9) + shambase::constants::pi<Tscal>);
/// 3D norm of the kernel
inline static constexpr Tscal norm_3d = (1.0/4.0)/(shambase::constants::pi<Tscal>*(-3.0/2.0*exp(-9) + (1.0/4.0)*sqrt(shambase::constants::pi<Tscal>)*erf(3)));
inline static Tscal f(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
if (q < 3) {
return exp(-t1);
}
else
return 0;
}
inline static Tscal df(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
if (q < 3) {
return -2*q*exp(-t1);
}
else
return 0;
}
inline static Tscal ddf(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
if (q < 3) {
return 4*t1*exp(-t1) - 2*exp(-t1);
}
else
return 0;
}
inline static Tscal phi_tilde_3d(Tscal q) {
if (q < 3) {
return 2*shambase::constants::pi<Tscal>*exp(-9) - pow(shambase::constants::pi<Tscal>, 3.0/2.0)*erf(q)/q;
}
else
return (-pow(shambase::constants::pi<Tscal>, 3.0/2.0)*erf(3) + 6*shambase::constants::pi<Tscal>*exp(-9))/q;
}
inline static Tscal phi_tilde_3d_prime(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
if (q < 3) {
return -2*shambase::constants::pi<Tscal>*exp(-t1)/q + pow(shambase::constants::pi<Tscal>, 3.0/2.0)*erf(q)/t1;
}
else
return -(-pow(shambase::constants::pi<Tscal>, 3.0/2.0)*erf(3) + 6*shambase::constants::pi<Tscal>*exp(-9))/t1;
}
};
------------------------------------------
Test the kernel
1134 test_result_tgauss3 = test_kernel(ret)
------------------------------------------
Testing kernel TGauss3 matching with Shamrock code
------------------------------------------
Testing norms:
Shamrock norm_1d = 0.564202047051383, expected=0.564202047051383, delta=3.33066907387547E-16
Shamrock norm_2d = 0.318349173592935, expected=0.318349173592935, delta=1.11022302462516E-16
Shamrock norm_3d = 0.179666148218087, expected=0.179666148218087, delta=1.94289029309402E-16
Testing kernel radius:
Shamrock Rkern = 3.0, delta=0.0
Testing kernel functions:
Shamrock f(q) = <built-in method TGauss3_f of PyCapsule object at 0x7fbcd9bd4060>
Shamrock df(q) = <built-in method TGauss3_df of PyCapsule object at 0x7fbcd9bd4090>
Shamrock ddf(q) = <built-in method TGauss3_ddf of PyCapsule object at 0x7fbcd9bd40c0>
Shamrock phi_tilde_3d(q) = <built-in method TGauss3_phi_tilde_3d of PyCapsule object at 0x7fbcd9bd40f0>
Shamrock phi_tilde_3d_prime(q) = <built-in method TGauss3_phi_tilde_3d_prime of PyCapsule object at 0x7fbcd9bd4120>
ZeroDivisionError: using limits towards 0
limit_f = 1.0 | shamrock_f[0] = 1.0
limit_df = 0.0 | shamrock_df[0] = -0.0
limit_ddf = -2.0 | shamrock_ddf[0] = -2.0
limit_phi_tilde_3d = -6.282409900511787 | shamrock_phi_tilde_3d[0] = -6.282409900511787
limit_phi_tilde_3d_prime = 0.0 | shamrock_phi_tilde_3d_prime[0] = 0.0
Absolute error f(q) = 5.5328869015178000863705218429141e-17
Absolute error df(q) = 1.0852585178826740881498128270435e-16
Absolute error ddf(q) = 2.6110952805685855689621535940408e-16
Absolute error phi_tilde_3d(q) = 1.5032018857438210402578376798422e-15
Absolute error phi_tilde_3d_prime(q) = 4.3266324410581327910712008064036e-14
------------------------------------------
TGauss5 Kernel#
Generate c++ code for the kernel
1143 ret = kernel_to_shamrock(tgauss5)
1145 print_kernel_info(ret)
------------------------------------------
Sympy expression for kernel TGauss5
------------------------------------------
f(q) = Piecewise((exp(-q**2), q < 5), (0, True))
df(q) = Piecewise((-2*q*exp(-q**2), q < 5), (0, True))
ddf(q) = Piecewise((4*q**2*exp(-q**2) - 2*exp(-q**2), q < 5), (0, True))
phi_tilde_3d(q) = Piecewise((2*pi*exp(-25) - pi**(3/2)*erf(q)/q, q < 5), ((-pi**(3/2)*erf(5) + 10*pi*exp(-25))/q, True))
phi_tilde_3d_prime(q) = Piecewise((-2*pi*exp(-q**2)/q + pi**(3/2)*erf(q)/q**2, q < 5), (-(-pi**(3/2)*erf(5) + 10*pi*exp(-25))/q**2, True))
------------------------------------------
1147 print_kernel_cpp_code(ret)
------------------------------------------
C++ generated code for kernel TGauss5
------------------------------------------
template<class Tscal>
class KernelDefTGauss5 {
public:
inline static constexpr Tscal Rkern = 5; ///< Compact support radius of the kernel
/// default hfact to be used for this kernel
inline static constexpr Tscal hfactd = 1.2;
/// 1D norm of the kernel
inline static constexpr Tscal norm_1d = 1/(sqrt(shambase::constants::pi<Tscal>)*erf(5));
/// 2D norm of the kernel
inline static constexpr Tscal norm_2d = 1.0/(-shambase::constants::pi<Tscal>*exp(-25) + shambase::constants::pi<Tscal>);
/// 3D norm of the kernel
inline static constexpr Tscal norm_3d = (1.0/4.0)/(shambase::constants::pi<Tscal>*(-5.0/2.0*exp(-25) + (1.0/4.0)*sqrt(shambase::constants::pi<Tscal>)*erf(5)));
inline static Tscal f(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
if (q < 5) {
return exp(-t1);
}
else
return 0;
}
inline static Tscal df(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
if (q < 5) {
return -2*q*exp(-t1);
}
else
return 0;
}
inline static Tscal ddf(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
if (q < 5) {
return 4*t1*exp(-t1) - 2*exp(-t1);
}
else
return 0;
}
inline static Tscal phi_tilde_3d(Tscal q) {
if (q < 5) {
return 2*shambase::constants::pi<Tscal>*exp(-25) - pow(shambase::constants::pi<Tscal>, 3.0/2.0)*erf(q)/q;
}
else
return (-pow(shambase::constants::pi<Tscal>, 3.0/2.0)*erf(5) + 10*shambase::constants::pi<Tscal>*exp(-25))/q;
}
inline static Tscal phi_tilde_3d_prime(Tscal q) {
Tscal t1 = sham::pow_constexpr<2>(q);
if (q < 5) {
return -2*shambase::constants::pi<Tscal>*exp(-t1)/q + pow(shambase::constants::pi<Tscal>, 3.0/2.0)*erf(q)/t1;
}
else
return -(-pow(shambase::constants::pi<Tscal>, 3.0/2.0)*erf(5) + 10*shambase::constants::pi<Tscal>*exp(-25))/t1;
}
};
------------------------------------------
Test the kernel
1151 test_result_tgauss5 = test_kernel(ret)
------------------------------------------
Testing kernel TGauss5 matching with Shamrock code
------------------------------------------
Testing norms:
Shamrock norm_1d = 0.564189583548624, expected=0.564189583548624, delta=3.33066907387547E-16
Shamrock norm_2d = 0.318309886188211, expected=0.318309886188211, delta=3.33066907387547E-16
Shamrock norm_3d = 0.179587122139514, expected=0.179587122139514, delta=1.11022302462516E-16
Testing kernel radius:
Shamrock Rkern = 5.0, delta=0.0
Testing kernel functions:
Shamrock f(q) = <built-in method TGauss5_f of PyCapsule object at 0x7fbcd9bd4360>
Shamrock df(q) = <built-in method TGauss5_df of PyCapsule object at 0x7fbcd9bd4390>
Shamrock ddf(q) = <built-in method TGauss5_ddf of PyCapsule object at 0x7fbcd9bd43c0>
Shamrock phi_tilde_3d(q) = <built-in method TGauss5_phi_tilde_3d of PyCapsule object at 0x7fbcd9bd43f0>
Shamrock phi_tilde_3d_prime(q) = <built-in method TGauss5_phi_tilde_3d_prime of PyCapsule object at 0x7fbcd9bd4420>
ZeroDivisionError: using limits towards 0
limit_f = 1.0 | shamrock_f[0] = 1.0
limit_df = 0.0 | shamrock_df[0] = -0.0
limit_ddf = -2.0 | shamrock_ddf[0] = -2.0
limit_phi_tilde_3d = -6.283185307092326 | shamrock_phi_tilde_3d[0] = -6.283185307092326
limit_phi_tilde_3d_prime = 0.0 | shamrock_phi_tilde_3d_prime[0] = 0.0
Absolute error f(q) = 5.5661915480185534057588247389932e-17
Absolute error df(q) = 1.0170157165450439613793689371898e-16
Absolute error ddf(q) = 2.6643412974770109075058790857962e-16
Absolute error phi_tilde_3d(q) = 1.6838964663548441663865583230091e-15
Absolute error phi_tilde_3d_prime(q) = 6.3308030348904237984452863555703e-14
------------------------------------------
Plot the kernels#
1158 fig_sz = (6.4, 12)
Plotting helper functions
1163 def create_kernel_plot_figure(fig_sz):
1164 """Create a figure with 5 subplots for kernel plotting"""
1165 plt.figure(figsize=fig_sz)
1166 ax_f = plt.subplot(5, 1, 1)
1167 ax_df = plt.subplot(5, 1, 2)
1168 ax_ddf = plt.subplot(5, 1, 3)
1169 ax_phi_tilde_3d = plt.subplot(5, 1, 4)
1170 ax_phi_tilde_3d_prime = plt.subplot(5, 1, 5)
1171 return ax_f, ax_df, ax_ddf, ax_phi_tilde_3d, ax_phi_tilde_3d_prime
1172
1173
1174 def plot_kernel_result(axes, test_result, kernel_label):
1175 """Plot a single kernel result on the given axes"""
1176 ax_f, ax_df, ax_ddf, ax_phi_tilde_3d, ax_phi_tilde_3d_prime = axes
1177 q_arr = test_result["q_arr"]
1178
1179 ax_f.plot(q_arr, test_result["shamrock_Cf"], label=f"C_3d f_{kernel_label}(q)")
1180 ax_df.plot(q_arr, test_result["shamrock_Cdf"], label=f"C_3d df_{kernel_label}(q)")
1181 ax_ddf.plot(q_arr, test_result["shamrock_Cddf"], label=f"C_3d ddf_{kernel_label}(q)")
1182 ax_phi_tilde_3d.plot(
1183 q_arr, test_result["shamrock_Cphi_tilde_3d"], label=f"C_3d phi_tilde_3d_{kernel_label}(q)"
1184 )
1185 ax_phi_tilde_3d_prime.plot(
1186 q_arr,
1187 test_result["shamrock_Cphi_tilde_3d_prime"],
1188 label=f"C_3d phi_tilde_3d_prime_{kernel_label}(q)",
1189 )
1190
1191
1192 def finalize_kernel_plot(axes):
1193 """Add titles, labels, and legends to the kernel plot"""
1194 ax_f, ax_df, ax_ddf, ax_phi_tilde_3d, ax_phi_tilde_3d_prime = axes
1195
1196 # Get current axis limits before adding reference line
1197 xlim = ax_phi_tilde_3d.get_xlim()
1198 ylim = ax_phi_tilde_3d.get_ylim()
1199
1200 ylim = (1.2 * ylim[0], ylim[1])
1201
1202 # Add -1/r reference line (only within current x range)
1203 q = np.linspace(max(1e-6, xlim[0]), xlim[1], 1000)
1204 one_over_r = -1 / q
1205 ax_phi_tilde_3d.plot(q, one_over_r, "--", color="grey", label="-1/r")
1206
1207 # Restore original limits (ignore reference line for autoscaling)
1208 ax_phi_tilde_3d.set_xlim(xlim)
1209 ax_phi_tilde_3d.set_ylim(ylim)
1210
1211 # Get current axis limits before adding reference line
1212 xlim = ax_phi_tilde_3d_prime.get_xlim()
1213 ylim = ax_phi_tilde_3d_prime.get_ylim()
1214
1215 ylim = (ylim[0], 1.5 * ylim[1])
1216
1217 # Add 1/r^2 reference line (only within current x range)
1218 one_over_r_squared = 1 / q**2
1219 ax_phi_tilde_3d_prime.plot(q, one_over_r_squared, "--", color="grey", label="1/r^2")
1220
1221 # Restore original limits (ignore reference line for autoscaling)
1222 ax_phi_tilde_3d_prime.set_xlim(xlim)
1223 ax_phi_tilde_3d_prime.set_ylim(ylim)
1224
1225 ax_f.set_title("C_3d f(q)", fontsize=10)
1226 ax_df.set_title("C_3d df(q)", fontsize=10)
1227 ax_ddf.set_title("C_3d ddf(q)", fontsize=10)
1228 ax_phi_tilde_3d.set_title("C_3d phi_tilde_3d(q)", fontsize=10)
1229 ax_phi_tilde_3d_prime.set_title("C_3d phi_tilde_3d_prime(q)", fontsize=10)
1230
1231 ax_f.set_xlabel("q")
1232 ax_df.set_xlabel("q")
1233 ax_ddf.set_xlabel("q")
1234 ax_phi_tilde_3d.set_xlabel("q")
1235 ax_phi_tilde_3d_prime.set_xlabel("q")
1236
1237 ax_f.legend(loc="right", fontsize=8)
1238 ax_df.legend(loc="right", fontsize=8)
1239 ax_ddf.legend(loc="right", fontsize=8)
1240 ax_phi_tilde_3d.legend(loc="right", fontsize=8)
1241 ax_phi_tilde_3d_prime.legend(loc="right", fontsize=8)
1242
1243 plt.tight_layout()
1244 plt.show()
Cubic splines kernels (M-series)
1250 axes = create_kernel_plot_figure(fig_sz)
1251 plot_kernel_result(axes, test_result_m4, "m4")
1252 plot_kernel_result(axes, test_result_m5, "m5")
1253 plot_kernel_result(axes, test_result_m6, "m6")
1254 plot_kernel_result(axes, test_result_m7, "m7")
1255 plot_kernel_result(axes, test_result_m8, "m8")
1256 plot_kernel_result(axes, test_result_m9, "m9")
1257 plot_kernel_result(axes, test_result_m10, "m10")
1258 finalize_kernel_plot(axes)
1259 plt.show()

Wendland kernels (C-series)
1264 axes = create_kernel_plot_figure(fig_sz)
1265 plot_kernel_result(axes, test_result_c2, "c2")
1266 plot_kernel_result(axes, test_result_c4, "c4")
1267 plot_kernel_result(axes, test_result_c6, "c6")
1268 finalize_kernel_plot(axes)
1269 plt.show()

Double hump kernels
1274 axes = create_kernel_plot_figure(fig_sz)
1275 plot_kernel_result(axes, test_result_m4dh, "m4dh")
1276 plot_kernel_result(axes, test_result_m4dh3, "m4dh3")
1277 plot_kernel_result(axes, test_result_m4dh5, "m4dh5")
1278 plot_kernel_result(axes, test_result_m4dh7, "m4dh7")
1279 finalize_kernel_plot(axes)
1280 plt.show()

M4Shift kernels
1286 axes = create_kernel_plot_figure(fig_sz)
1287 plot_kernel_result(axes, test_result_m4shift2, "m4shift2")
1288 plot_kernel_result(axes, test_result_m4shift4, "m4shift4")
1289 plot_kernel_result(axes, test_result_m4shift8, "m4shift8")
1290 plot_kernel_result(axes, test_result_m4shift16, "m4shift16")
1291 finalize_kernel_plot(axes)
1292 plt.show()

TGauss kernels
1297 axes = create_kernel_plot_figure(fig_sz)
1298 plot_kernel_result(axes, test_result_tgauss3, "tgauss3")
1299 plot_kernel_result(axes, test_result_tgauss5, "tgauss5")
1300 finalize_kernel_plot(axes)
1301 plt.show()

Total running time of the script: (1 minutes 33.510 seconds)
Estimated memory usage: 234 MB