22Verification for the difference between compressed L2L coefficients and
33full L2L coefficients.
44"""
5+ from __future__ import annotations
6+
57import os
6- os .environ ['PYOPENCL_CTX' ] = '0'
8+
9+
10+ os .environ ["PYOPENCL_CTX" ] = "0"
711
812import math
13+
914import numpy as np
15+ import pytest
1016import scipy .special as spsp
1117import sympy as sp
12- import pytest
13- import pyopencl as cl
18+
1419import sumpy .toys as t
15- from sumpy .kernel import (
16- YukawaKernel ,
17- HelmholtzKernel ,
18- LaplaceKernel ,
19- BiharmonicKernel
20- )
20+ from sumpy .array_context import _acf
2121from sumpy .expansion .local import (
2222 LinearPDEConformingVolumeTaylorLocalExpansion ,
23- VolumeTaylorLocalExpansion
23+ VolumeTaylorLocalExpansion ,
2424)
25- from sumpy .array_context import _acf
25+ from sumpy .kernel import BiharmonicKernel , HelmholtzKernel , LaplaceKernel , YukawaKernel
2626from sumpy .tools import build_matrix
2727
2828
2929VERBOSE = False
3030
31+
3132def to_scalar (val ):
3233 """Convert symbolic or array value to scalar."""
33- if hasattr (val , ' evalf' ):
34+ if hasattr (val , " evalf" ):
3435 val = val .evalf ()
35- if hasattr (val , ' item' ):
36+ if hasattr (val , " item" ):
3637 val = val .item ()
3738 return complex (val )
3839
3940
4041class NumericMatVecOperator :
4142 """Wrapper for symbolic matrix-vector operator with numeric
4243 substitution."""
43-
44+
4445 def __init__ (self , symbolic_op , repl_dict ):
4546 self .symbolic_op = symbolic_op
4647 self .repl_dict = repl_dict
@@ -50,7 +51,7 @@ def matvec(self, vec):
5051 result = self .symbolic_op .matvec (vec )
5152 numeric_result = []
5253 for expr in result :
53- if hasattr (expr , ' xreplace' ):
54+ if hasattr (expr , " xreplace" ):
5455 numeric_result .append (
5556 complex (expr .xreplace (self .repl_dict ).evalf ())
5657 )
@@ -62,20 +63,19 @@ def matvec(self, vec):
6263def get_repl_dict (kernel , extra_kwargs ):
6364 """Numeric substitution for symbolic kernel parameters."""
6465 repl_dict = {}
65- if ' lam' in extra_kwargs :
66- repl_dict [sp .Symbol (' lam' )] = extra_kwargs [' lam' ]
67- if 'k' in extra_kwargs :
68- repl_dict [sp .Symbol ('k' )] = extra_kwargs ['k' ]
66+ if " lam" in extra_kwargs :
67+ repl_dict [sp .Symbol (" lam" )] = extra_kwargs [" lam" ]
68+ if "k" in extra_kwargs :
69+ repl_dict [sp .Symbol ("k" )] = extra_kwargs ["k" ]
6970 return repl_dict
7071
7172
7273@pytest .mark .parametrize ("knl,extra_kwargs" , [
7374 (LaplaceKernel (2 ), {}),
74- (YukawaKernel (2 ), {' lam' : 0.1 }),
75- (HelmholtzKernel (2 ), {'k' : 0.5 }),
75+ (YukawaKernel (2 ), {" lam" : 0.1 }),
76+ (HelmholtzKernel (2 ), {"k" : 0.5 }),
7677 (BiharmonicKernel (2 ), {}),
7778])
78-
7979def test_l2l_coefficient_differences (knl , extra_kwargs ):
8080 """
8181 Test L2L coefficient differences between formula and direct
@@ -84,7 +84,7 @@ def test_l2l_coefficient_differences(knl, extra_kwargs):
8484 order = 7
8585 dim = 2
8686 repl_dict = get_repl_dict (knl , extra_kwargs )
87-
87+
8888 # Setup sources and centers
8989 source = np .array ([[5.0 ], [5.0 ]])
9090 c1 = np .array ([0.0 , 0.0 ])
@@ -115,41 +115,42 @@ def test_l2l_coefficient_differences(knl, extra_kwargs):
115115 # Build matrix M
116116 p2l2l_expn = LinearPDEConformingVolumeTaylorLocalExpansion (knl , order )
117117 wrangler = p2l2l_expn .expansion_terms_wrangler
118- M_symbolic = wrangler .get_projection_matrix (rscale = 1.0 )
118+ M_symbolic = wrangler .get_projection_matrix (rscale = 1.0 ) # noqa: N806
119119 numeric_op = NumericMatVecOperator (M_symbolic , repl_dict )
120- M = build_matrix (numeric_op , dtype = np .complex128 )
120+ M = build_matrix (numeric_op , dtype = np .complex128 ) # noqa: N806
121121
122122 # Get compressed coefficients
123123 mu_c_symbolic = wrangler .get_full_kernel_derivatives_from_stored (
124124 p2l2l .coeffs , rscale = 1.0
125125 )
126126 mu_c = []
127127 for coeff in mu_c_symbolic :
128- if hasattr (coeff , ' xreplace' ):
128+ if hasattr (coeff , " xreplace" ):
129129 mu_c .append (to_scalar (coeff .xreplace (repl_dict )))
130130 else :
131131 mu_c .append (to_scalar (coeff ))
132132
133133 # Get identifiers
134134 stored_identifiers = p2l2l_expn .get_coefficient_identifiers ()
135135 full_identifiers = p2l2l_expn .get_full_coefficient_identifiers ()
136- lexpn_idx = VolumeTaylorLocalExpansion (knl , order ).get_full_coefficient_identifiers ()
137-
136+ lexpn = VolumeTaylorLocalExpansion (knl , order )
137+ lexpn_idx = lexpn .get_full_coefficient_identifiers ()
138+
138139 h = c2 - c1
139140 global_const = to_scalar (knl .get_global_scaling_const ())
140141
141142 if VERBOSE :
142143 print (f'\n { "=" * 104 } ' )
143- print (f' L2L Verification: { type (knl ).__name__ } (order={ order } )' )
144+ print (f" L2L Verification: { type (knl ).__name__ } (order={ order } )" )
144145 print (f'{ "=" * 104 } ' )
145- print (f' c1 = { c1 } , c2 = { c2 } , h = { h } ' )
146+ print (f" c1 = { c1 } , c2 = { c2 } , h = { h } " )
146147 print ()
147- print (f"{ 'i' :>3s} | { 'ν(i)' :>15s} | { '|ν|' :4s} | "
148+ print (f"{ 'i' :>3s} | { 'ν(i)' :>15s} | { '|ν|' :4s} | " # noqa: RUF001
148149 f"{ 'formula' :>31s} | { 'direct' :>31s} | { 'abs err' :>10s} " )
149150 print ("-" * 104 )
150151
151152 max_abs_error = 0.0
152-
153+
153154 for i , nu_i in enumerate (full_identifiers ):
154155 i_card = sum (np .array (nu_i ))
155156
@@ -165,7 +166,7 @@ def test_l2l_coefficient_differences(knl, extra_kwargs):
165166
166167 for q_idx in range (start_idx , end_idx ):
167168 nu_q = full_identifiers [q_idx ]
168- nu_sum = tuple (map (sum , zip (nu_q , nu_jk )))
169+ nu_sum = tuple (map (sum , zip (nu_q , nu_jk , strict = True )))
169170 if nu_sum not in full_identifiers :
170171 continue
171172
@@ -190,18 +191,17 @@ def test_l2l_coefficient_differences(knl, extra_kwargs):
190191 max_abs_error = max (max_abs_error , abs_err )
191192
192193 if VERBOSE :
193- print (f"{ i :3d} | { str ( nu_i ) :>15s} | { i_card :4d} | "
194+ print (f"{ i :3d} | { nu_i !s :>15s} | { i_card :4d} | "
194195 f"{ error .real : .8e} { error .imag :+.8e} j | "
195196 f"{ direct_diff .real : .8e} { direct_diff .imag :+.8e} j | "
196197 f"{ abs_err :9.2e} " )
197198
198199 if VERBOSE :
199200 print (f"\n Maximum absolute error: { max_abs_error :.2e} " )
200- print (f'{ "=" * 104 } \n ' )
201-
201+
202202 assert max_abs_error < 1e-10 , \
203203 f"{ type (knl ).__name__ } : error { max_abs_error :.2e} "
204204
205205
206206if __name__ == "__main__" :
207- pytest .main ([__file__ , "-v" , "-s" ])
207+ pytest .main ([__file__ , "-v" , "-s" ])
0 commit comments