Skip to content

Commit fbb50cd

Browse files
committed
Pass all ruff checks
1 parent 86b6f09 commit fbb50cd

18 files changed

+223
-197
lines changed

pyfeltor/dg/__init__.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
# docstring displayed by help(pyfeltor)
22
""" The python version of the dg library
33
"""
4-
from . import create
5-
from .enums import bc, direction, inverse_bc, inverse_dir
6-
from .grid import Grid
7-
from .evaluation import evaluate, integrate
8-
from . import geo
4+
from . import create as create
5+
from . import geo as geo
6+
from .enums import bc as bc
7+
from .enums import direction as direction
8+
from .enums import inverse_bc as inverse_bc
9+
from .enums import inverse_dir as inverse_dir
10+
from .evaluation import evaluate as evaluate
11+
from .evaluation import integrate as integrate
12+
from .grid import Grid as Grid

pyfeltor/dg/create/__init__.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# docstring displayed by help(pyfeltor)
22
""" The python version of the dg library, creation
33
"""
4-
from .weights import weights, abscissas, grid_from_abscissas
5-
from .derivative import dx, jump
6-
from .elliptic import elliptic
7-
from .interpolation import interpolation, projection
4+
from .derivative import dx as dx
5+
from .derivative import jump as jump
6+
from .elliptic import elliptic as elliptic
7+
from .interpolation import interpolation as interpolation
8+
from .interpolation import projection as projection
9+
from .weights import abscissas as abscissas
10+
from .weights import grid_from_abscissas as grid_from_abscissas
11+
from .weights import weights as weights

pyfeltor/dg/create/derivative.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from .dx import normed, jump_normed
21
import scipy.sparse
32

3+
from .dx import jump_normed, normed
4+
45

56
def dx(dim, grid, bc, direction):
67
deriv = normed(grid.n[dim], grid.N[dim], grid.h()[dim], bc, direction)

pyfeltor/dg/create/dx.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import numpy as np
21
import scipy.sparse
2+
3+
from ..enums import bc
34
from . import operators as ops
4-
from ..enums import bc, direction
55

66

77
def symm(n, N, h, bcx):
8-
l = ops.lilj(n)
9-
r = ops.rirj(n)
8+
ll = ops.lilj(n)
9+
rr = ops.rirj(n)
1010
lr = ops.lirj(n)
1111
rl = ops.rilj(n)
1212
d = ops.pidxpj(n)
@@ -19,14 +19,14 @@ def symm(n, N, h, bcx):
1919
a_bound_left = a.copy()
2020
# left boundary
2121
if (bcx == bc.DIR) or (bcx == bc.DIR_NEU):
22-
a_bound_left += 0.5 * (t @ l)
22+
a_bound_left += 0.5 * (t @ ll)
2323
elif (bcx == bc.NEU) or (bcx == bc.NEU_DIR):
24-
a_bound_left -= 0.5 * (t @ l)
24+
a_bound_left -= 0.5 * (t @ ll)
2525
# right boundary
2626
if (bcx == bc.DIR) or (bcx == bc.NEU_DIR):
27-
a_bound_right -= 0.5 * (t @ r)
27+
a_bound_right -= 0.5 * (t @ rr)
2828
elif (bcx == bc.NEU) or (bcx == bc.DIR_NEU):
29-
a_bound_right += 0.5 * (t @ r)
29+
a_bound_right += 0.5 * (t @ rr)
3030
b = t @ (1.0 / 2.0 * rl)
3131
bp = t @ (-1.0 / 2.0 * lr) # pitfall: T*-m^T is NOT -(T*m)^T
3232
# transform to XSPACE
@@ -94,20 +94,20 @@ def symm(n, N, h, bcx):
9494
vals.append(b[i, j])
9595

9696
# sort
97-
rows, cols, vals = zip(*sorted(zip(rows, cols, vals)))
97+
rows, cols, vals = zip(*sorted(zip(rows, cols, vals, strict=False)), strict=False)
9898
return scipy.sparse.coo_matrix((vals, (rows, cols)))
9999

100100

101101
def plus(n, N, h, bcx):
102-
l = ops.lilj(n)
103-
r = ops.rirj(n)
104-
lr = ops.lirj(n)
102+
ll = ops.lilj(n)
103+
#rr = ops.rirj(n)
104+
#lr = ops.lirj(n)
105105
rl = ops.rilj(n)
106106
d = ops.pidxpj(n)
107107
t = ops.pipj_inv(n)
108108
t *= 2.0 / h
109109

110-
a = t @ (-l - d.transpose())
110+
a = t @ (-ll - d.transpose())
111111
# bcx = PER
112112
a_bound_left = a.copy() # PER, NEU, and NEU_DIR
113113
a_bound_right = a.copy() # PER, DIR, and NEU_DIR
@@ -168,20 +168,20 @@ def plus(n, N, h, bcx):
168168
vals.append(b[i, j])
169169

170170
# sort
171-
rows, cols, vals = zip(*sorted(zip(rows, cols, vals)))
171+
rows, cols, vals = zip(*sorted(zip(rows, cols, vals, strict=False)), strict=False)
172172
return scipy.sparse.coo_matrix((vals, (rows, cols)))
173173

174174

175175
def minus(n, N, h, bcx):
176-
l = ops.lilj(n)
177-
r = ops.rirj(n)
176+
ll = ops.lilj(n)
177+
#rr = ops.rirj(n)
178178
lr = ops.lirj(n)
179-
rl = ops.rilj(n)
179+
#rl = ops.rilj(n)
180180
d = ops.pidxpj(n)
181181
t = ops.pipj_inv(n)
182182
t *= 2.0 / h
183183

184-
a = t @ (l + d)
184+
a = t @ (ll + d)
185185
# bcx = PER
186186
a_bound_right = a.copy() # PER, NEU and DIR_NEU
187187
a_bound_left = a.copy() # PER, DIR and DIR_NEU
@@ -242,25 +242,25 @@ def minus(n, N, h, bcx):
242242
vals.append(a[i, j])
243243

244244
# sort
245-
rows, cols, vals = zip(*sorted(zip(rows, cols, vals)))
245+
rows, cols, vals = zip(*sorted(zip(rows, cols, vals, strict=False)), strict=False)
246246
return scipy.sparse.coo_matrix((vals, (rows, cols)))
247247

248248

249249
def jump_normed(n, N, h, bcx):
250-
l = ops.lilj(n)
251-
r = ops.rirj(n)
250+
ll = ops.lilj(n)
251+
rr = ops.rirj(n)
252252
lr = ops.lirj(n)
253253
rl = ops.rilj(n)
254254

255255
t = ops.pipj_inv(n)
256256
t *= 2.0 / h
257-
a = t @ (l + r)
257+
a = t @ (ll + rr)
258258
a_bound_left = a.copy() # DIR and PER
259259
if (bcx == bc.NEU) or (bcx == bc.NEU_DIR):
260-
a_bound_left = t @ r
260+
a_bound_left = t @ rr
261261
a_bound_right = a.copy() # DIR and PER
262262
if (bcx == bc.NEU) or (bcx == bc.DIR_NEU):
263-
a_bound_right = t @ l
263+
a_bound_right = t @ ll
264264
b = -t @ rl
265265
bp = -t @ lr
266266
# transform to XSPACE
@@ -328,7 +328,7 @@ def jump_normed(n, N, h, bcx):
328328
vals.append(b[i, j])
329329

330330
# sort
331-
rows, cols, vals = zip(*sorted(zip(rows, cols, vals)))
331+
rows, cols, vals = zip(*sorted(zip(rows, cols, vals, strict=False)), strict=False)
332332
return scipy.sparse.coo_matrix((vals, (rows, cols)))
333333

334334

pyfeltor/dg/create/elliptic.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import numpy as np
2-
from .derivative import dx, jump
3-
from .. import enums
41
import scipy.sparse as sparse
52

3+
from .. import enums
4+
from .derivative import dx, jump
5+
66

77
def elliptic(grid, bcs, directions, sigma, jumpfactor=1):
88
left = [

pyfeltor/dg/create/interpolation.py

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import numpy as np
21
import itertools
2+
3+
import numpy as np
34
import scipy.sparse
4-
from .weights import weights
5-
from . import operators as ops
6-
from ..enums import bc, direction
5+
6+
from ..enums import bc
77
from ..evaluation import evaluate
8+
from . import operators as ops
9+
from .weights import weights
810

911

1012
def shift(grid, x, bcs):
@@ -76,24 +78,25 @@ def interpolation(xs, grid, bcs):
7678
else:
7779
raise Exception("interpolation not implemented for ndim > 3")
7880
for it in tuples:
79-
I = 0 # the index to push back
80-
V = 1 # the value to push back
81+
II = 0 # the index to push back
82+
VV = 1 # the value to push back
8183
for kk in range(0, grid.ndim):
82-
I = (I * grid.N[kk] + nn[kk]) * grid.n[kk] + it[kk]
83-
V = V * px[kk][it[kk]]
84+
II = (II * grid.N[kk] + nn[kk]) * grid.n[kk] + it[kk]
85+
VV = VV * px[kk][it[kk]]
8486
# print( i, I, V)
8587
rows.append(i)
86-
cols.append(round(I))
88+
cols.append(round(II))
8789
if not negative:
88-
vals.append(V)
90+
vals.append(VV)
8991
else:
90-
vals.append(-V)
92+
vals.append(-VV)
9193
# sort
92-
rows, cols, vals = zip(*sorted(zip(rows, cols, vals)))
93-
return scipy.sparse.coo_matrix((vals, (rows, cols)), shape = (len(xs[0]),grid.size()))
94+
rows, cols, vals = zip(*sorted(zip(rows, cols, vals, strict=False)), strict=False)
95+
return scipy.sparse.coo_matrix((vals, (rows, cols)), shape=(len(xs[0]),
96+
grid.size()))
9497

9598

96-
def projection( grid_new, grid_old):
99+
def projection(grid_new, grid_old):
97100
""" Create a projection between two grids
98101
99102
This matrix can be applied to vectors defined on the old (fine) grid to obtain
@@ -106,26 +109,28 @@ def projection( grid_new, grid_old):
106109
"""
107110
ndim = grid_old.ndim
108111
if grid_new.ndim != ndim:
109-
raise Exception( "Cannot project between grids with different dimensions")
110-
for i in range( 0, ndim):
111-
if grid_old.N[i] % grid_new.N[i] != 0 :
112-
print( f"WARNING you project between incompatible grids!! old N: {grid_old.N[i]} new N {grid_new.N[i]}")
113-
if grid_old.n[i] < grid_new.n[i] :
114-
print( f"WARNING you project between incompatible grids!! old n: {grid_old.n[i]} new n {grid_new.n[i]}")
115-
wf = scipy.sparse.diags(weights( grid_old))
116-
points = list()
117-
bcs = [bc.PER for i in range(0,ndim)]
112+
raise Exception("Cannot project between grids with different dimensions")
113+
for i in range(0, ndim):
114+
if grid_old.N[i] % grid_new.N[i] != 0:
115+
print(f"WARNING you project between incompatible grids!! old N: \
116+
{grid_old.N[i]} new N {grid_new.N[i]}")
117+
if grid_old.n[i] < grid_new.n[i]:
118+
print(f"WARNING you project between incompatible grids!! old n: \
119+
{grid_old.n[i]} new n {grid_new.n[i]}")
120+
wf = scipy.sparse.diags(weights(grid_old))
121+
points = []
122+
bcs = [bc.PER for i in range(0, ndim)]
118123
if ndim == 1:
119-
points.append( evaluate( lambda x: x, grid_old))
124+
points.append(evaluate(lambda x: x, grid_old))
120125
elif ndim == 2:
121-
points.append( evaluate( lambda y,x: y, grid_old))
122-
points.append( evaluate( lambda y,x: x, grid_old))
126+
points.append(evaluate(lambda y, x: y, grid_old))
127+
points.append(evaluate(lambda y, x: x, grid_old))
123128
elif ndim == 3:
124-
points.append( evaluate( lambda z,y,x: z, grid_old))
125-
points.append( evaluate( lambda z,y,x: y, grid_old))
126-
points.append( evaluate( lambda z,y,x: x, grid_old))
129+
points.append(evaluate(lambda z, y, x: z, grid_old))
130+
points.append(evaluate(lambda z, y, x: y, grid_old))
131+
points.append(evaluate(lambda z, y, x: x, grid_old))
127132
else:
128133
raise Exception("Projection not implemented for ndim > 3")
129-
A = interpolation( points, grid_new, bcs )
130-
vc = scipy.sparse.diags(1./weights( grid_new))
134+
A = interpolation(points, grid_new, bcs)
135+
vc = scipy.sparse.diags(1. / weights(grid_new))
131136
return vc @ A.transpose() @ wf

pyfeltor/dg/create/operators.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ def pidxpj(n):
2626
op = np.zeros((n, n))
2727
for i in range(0, n):
2828
for j in range(0, n):
29-
if i < j:
30-
if (i + j) % 2 != 0:
31-
op[i, j] = 2
29+
if (i < j) and ((i + j) % 2 != 0):
30+
op[i, j] = 2
3231
return op
3332

3433

pyfeltor/dg/create/weights.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import numpy as np
2+
23
from ..grid import Grid
34

45

@@ -25,25 +26,27 @@ def abscissas(grid, dimension=0):
2526
abscissas[i * grid.n[dim] + k] = i * h + x0 + h / 2.0 * (1.0 + x[k])
2627
return abscissas
2728

28-
def _get_x0_x1_n_N( x):
29-
for n in range(1,21):
30-
if len(x)%n != 0:
29+
30+
def _get_x0_x1_n_N(x):
31+
for n in range(1, 21):
32+
if len(x) % n != 0:
3133
continue
32-
N = len(x)/n
34+
N = len(x) / n
3335
(xx, w) = np.polynomial.legendre.leggauss(n)
3436
h = x[n] - x[0]
35-
x0 = x[0] - h/2.0 * (1.0+xx[0])
36-
first_absc = np.zeros(2*n)
37-
last_value = x0 + h*N - h / 2.0 * (1.0 + xx[0])
38-
for i in range(0,2):
39-
for k in range(0,n):
40-
first_absc[ i*n+k] = i * h + x0 + h / 2.0 * (1.0 + xx[k])
41-
if abs( last_value - x[-1]) < 1e-10*abs(x[-1]) + 1e-10:
42-
return (x0, x0+h*N, int(n), int(N))
43-
raise Exception( "Could not determine grid")
37+
x0 = x[0] - h / 2.0 * (1.0 + xx[0])
38+
first_absc = np.zeros(2 * n)
39+
last_value = x0 + h * N - h / 2.0 * (1.0 + xx[0])
40+
for i in range(0, 2):
41+
for k in range(0, n):
42+
first_absc[i * n + k] = i * h + x0 + h / 2.0 * (1.0 + xx[k])
43+
if abs(last_value - x[-1]) < 1e-10 * abs(x[-1]) + 1e-10:
44+
return (x0, x0 + h * N, int(n), int(N))
45+
raise Exception("Could not determine grid")
4446
return (0, 1, 1, 1)
4547

46-
def grid_from_abscissas( xs ) :
48+
49+
def grid_from_abscissas(xs):
4750
""" This function reverse engineers a Grid that corresponds to the given
4851
abscissas. This is useful for example to get a Grid that corresponds to a
4952
given simulation output
@@ -58,6 +61,6 @@ def grid_from_abscissas( xs ) :
5861
x1 = np.zeros(ndim)
5962
n = np.arange(ndim)
6063
N = np.arange(ndim)
61-
for dim in range(0,ndim):
64+
for dim in range(0, ndim):
6265
x0[dim], x1[dim], n[dim], N[dim] = _get_x0_x1_n_N(xs[dim])
63-
return Grid( x0, x1, n, N)
66+
return Grid(x0, x1, n, N)

pyfeltor/dg/evaluation.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
from .create import operators as ops
1+
import numpy as np
2+
23
from . import create
4+
from .create import operators as ops
35
from .enums import direction
4-
import numpy as np
56

67

78
def evaluate(function, grid):
89
"""Evaluate a function on the grid points of the given grid
910
1011
function: has to take numpy arrays as arguments, f(x), f(y,x), f(z,y,x)
1112
grid: instance of dg.Grid
12-
return: flat np.array with x the fastest varying dimension. Can be reshaped with reshape(grid.shape)
13+
return: flat np.array with x the fastest varying dimension. Can be reshaped
14+
with reshape(grid.shape)
1315
"""
14-
xs = []
1516
ndim = grid.ndim
16-
for dim in range(0, ndim):
17-
xs.append(create.abscissas(grid, dim))
17+
xs = [create.abscissas(grid, dim) for dim in range(0, ndim)]
1818

1919
if ndim == 1:
2020
return np.array([function(x) for x in xs[0]])
@@ -59,8 +59,8 @@ def integrate(to_integrate, grid, direction=direction.forward):
5959
out = np.zeros(grid.size())
6060
for i in range(0, grid.N[0]):
6161
for k in range(0, grid.n[0]):
62-
for l in range(0, grid.n[0]):
63-
out[i * n + k] += ninj[k, l] * to_in[i * n + l]
62+
for ll in range(0, grid.n[0]):
63+
out[i * n + k] += ninj[k, ll] * to_in[i * n + ll]
6464
out[i * n + k] += constant
6565
for k in range(0, grid.n[0]):
6666
constant += h * forward[0, k] * to_in[i * n + k]

0 commit comments

Comments
 (0)