From e3c2a308c1c6ad617a137c733b24e7f2bdc0ad77 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Wed, 13 May 2020 00:28:03 -0500 Subject: [PATCH] Placate flake8 --- .gitlab-ci.yml | 4 +- benchmark/benchmark.py | 2 +- test/conftest.py | 3 ++ test/data_for_test.py | 10 ++--- test/ideal_gas.py | 4 +- test/test_eigensystem.py | 6 +-- test/test_flux_derivatives.py | 6 +-- test/test_flux_window_ops.py | 8 ++-- test/utilities.py | 2 + test/wavy_metrics_3d.py | 53 +++++++++++++++------------ test/weno_reference_implementation.py | 16 ++++---- 11 files changed, 63 insertions(+), 51 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 13c5398..64ba5ae 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -16,8 +16,8 @@ Python 3 POCL: Flake8: script: - curl -L -O -k https://gitlab.tiker.net/inducer/ci-support/raw/master/prepare-and-run-flake8.sh - - ". ./prepare-and-run-flake8.sh benchmark*.py test*.py util*.py" + - ". ./prepare-and-run-flake8.sh benchmark/*.py test/*.py" tags: - python3 except: - - tags \ No newline at end of file + - tags diff --git a/benchmark/benchmark.py b/benchmark/benchmark.py index 935b717..dd39125 100644 --- a/benchmark/benchmark.py +++ b/benchmark/benchmark.py @@ -20,6 +20,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ +import os import numpy as np # noqa: F401 import numpy.linalg as la # noqa: F401 import pyopencl as cl @@ -51,7 +52,6 @@ def get_weno_program(): prg = lp.parse_transformed_fortran(infile_content, filename=fn) _WENO_PRG["default"] = prg - if not _WENO_PRG: parse_weno() return _WENO_PRG["default"] diff --git a/test/conftest.py b/test/conftest.py index 32cf14b..67f8e77 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -39,6 +39,7 @@ import utilities as u # Taken from # https://docs.pytest.org/en/latest/example/simple.html#control-skipping-of-tests-according-to-command-line-option + def pytest_addoption(parser): parser.addoption("--runslow", action="store_true", default=False, help="run slow tests") @@ -55,6 +56,7 @@ def pytest_collection_modifyitems(config, items): # }}} + # {{{ pytest_generate_tests # pytest_generate_tests borrows heavily from @@ -96,6 +98,7 @@ def pytest_generate_tests(metafunc): # }}} + @pytest.fixture(scope="session") def queue(ctx_factory): return u.get_queue(ctx_factory) diff --git a/test/data_for_test.py b/test/data_for_test.py index 99feb03..990c5ee 100644 --- a/test/data_for_test.py +++ b/test/data_for_test.py @@ -43,7 +43,7 @@ class WindowData: self.generalized_fluxes = np.array([np.dot(flux, metrics/jacobian) for flux, metrics, jacobian in zip(self.fluxes, self.metrics, self.jacobians)] - )[:,:,self.dir_internal].T.copy(order="F") + )[:, :, self.dir_internal].T.copy(order="F") @pytest.fixture(scope="session") @@ -51,7 +51,7 @@ def states_generator(): def states(states_str): if states_str == "sine": rho = np.sin(0.01*np.arange(6)+1.55) - vel = np.repeat(np.array([1.0, 2.0, 3.0])[None,:], 6, axis=0) + vel = np.repeat(np.array([1.0, 2.0, 3.0])[None, :], 6, axis=0) pressure = np.repeat(3.2, 6) return u.transposed_array([ref.gas.conserved(r, v, p) for r, v, p @@ -123,10 +123,10 @@ class PairData: self.direction = data.direction self.dir_internal = data.dir_internal - self.state_pair = data.states[:,2:4] - self.generalized_flux_pair = data.generalized_fluxes[:,2:4] + self.state_pair = data.states[:, 2:4] + self.generalized_flux_pair = data.generalized_fluxes[:, 2:4] - weighted_metric_slice = data.metrics[2:4]/data.jacobians[2:4,None,None] + weighted_metric_slice = data.metrics[2:4]/data.jacobians[2:4, None, None] self.frozen_metrics = np.mean(weighted_metric_slice, axis=0) self.frozen_jacobian = np.mean(data.jacobians[2:4]) self.combined_frozen_metrics = np.sum(self.frozen_metrics**2, axis=1) diff --git a/test/ideal_gas.py b/test/ideal_gas.py index 85aa4b7..5c2141d 100644 --- a/test/ideal_gas.py +++ b/test/ideal_gas.py @@ -70,8 +70,8 @@ def flux(state): result = np.outer(state, vel) for i in range(ndim): - result[i+1,i] += p - result[ndim+1,i] += vel[i]*p + result[i+1, i] += p + result[ndim+1, i] += vel[i]*p return result diff --git a/test/test_eigensystem.py b/test/test_eigensystem.py index 8c1ba93..b1a8448 100644 --- a/test/test_eigensystem.py +++ b/test/test_eigensystem.py @@ -34,12 +34,12 @@ import logging import pytest import utilities as u -import weno_reference_implementation as ref -from data_for_test import ( +import weno_reference_implementation as ref # noqa +from data_for_test import ( # noqa pair_data, window_data, states_generator, metrics_generator) -def test_roe_uniform_grid_ideal_gas(queue, pair_data): +def test_roe_uniform_grid_ideal_gas(queue, pair_data): # noqa data = pair_data def check_roe_identity(states, R, R_inv): diff --git a/test/test_flux_derivatives.py b/test/test_flux_derivatives.py index 2c4ec9c..e80cf3d 100644 --- a/test/test_flux_derivatives.py +++ b/test/test_flux_derivatives.py @@ -68,8 +68,6 @@ class FluxDataVector: self.jacobians = np.ones((self.nxhalo, self.nyhalo, self.nzhalo), order="F") - state_pair = self.swap_array_rows( - u.transposed_array_from_string(states_str), self.direction) # FIXME: Move array_from_string stuff outside WindowResults # -- just pass an array & have external utilities that generate # Riemann, sine wave, etc. initial conditions @@ -87,7 +85,7 @@ class FluxDataVector: npoints = self.nxhalo*self.nyhalo*self.nzhalo flat_states = self.states.reshape((self.nvars, npoints)) self.fluxes = ref.pointwise_fluxes( - flat_states)[:,:,self.dir_internal].T.reshape( + flat_states)[:, :, self.dir_internal].T.reshape( (self.nvars, self.nxhalo, self.nyhalo, self.nzhalo) ).copy(order="F") @@ -112,7 +110,7 @@ class FluxDataVector: for i in range(d): result = self.add_dimension(result, nx_arr[i]) result = np.swapaxes(result, -2, -1) - for i in range(d+1,self.ndim): + for i in range(d+1, self.ndim): result = self.add_dimension(result, nx_arr[i]) return result.copy(order="F") diff --git a/test/test_flux_window_ops.py b/test/test_flux_window_ops.py index 9685c38..dbd4741 100644 --- a/test/test_flux_window_ops.py +++ b/test/test_flux_window_ops.py @@ -35,7 +35,7 @@ import pytest import utilities as u import weno_reference_implementation as ref -from data_for_test import ( +from data_for_test import ( # noqa PairData, window_data, states_generator, metrics_generator) @@ -62,7 +62,7 @@ class WindowResults: self.generalized_fluxes = data.generalized_fluxes self.generalized_fluxes_frozen = np.array([np.dot(flux, self.frozen_metrics) - for flux in data.fluxes])[:,:,self.dir_internal].T.copy(order="F") + for flux in data.fluxes])[:, :, self.dir_internal].T.copy(order="F") self.lam_pointwise = ref.lambda_pointwise( self.states, self.metrics, self.dir_internal) @@ -76,7 +76,7 @@ class WindowResults: self.R_inv) self.oscillation_pos = ref.oscillation(self.char_fluxes_pos) - self.oscillation_neg = ref.oscillation(self.char_fluxes_neg[:,::-1]) + self.oscillation_neg = ref.oscillation(self.char_fluxes_neg[:, ::-1]) self.weno_weights_pos = ref.weno_weights( self.oscillation_pos, self.combined_frozen_metric) @@ -94,7 +94,7 @@ class WindowResults: @pytest.fixture(scope="session") -def window_results(queue, window_data): +def window_results(queue, window_data): # noqa return WindowResults(queue, window_data) diff --git a/test/utilities.py b/test/utilities.py index f69fb0f..3f1fa07 100644 --- a/test/utilities.py +++ b/test/utilities.py @@ -101,9 +101,11 @@ def expand_to_n(pair, n): # }}} + def get_queue(ctx_factory): return cl.CommandQueue(ctx_factory()) + # {{{ program / kernel _WENO_PRG = {} diff --git a/test/wavy_metrics_3d.py b/test/wavy_metrics_3d.py index adfdf0c..93011a1 100644 --- a/test/wavy_metrics_3d.py +++ b/test/wavy_metrics_3d.py @@ -47,18 +47,23 @@ dz = Lz/(l_max - 1) def cos_x(xi): return np.cos((4*pi*dx*xi)/Lx) + def cos_y(eta): return np.cos((4*pi*dy*eta)/Ly) + def cos_z(zeta): return np.cos((4*pi*dz*zeta)/Lz) + def sin_x(xi): return np.sin((4*pi*dx*xi)/Lx) + def sin_y(eta): return np.sin((4*pi*dy*eta)/Ly) + def sin_z(zeta): return np.sin((4*pi*dz*zeta)/Lz) @@ -66,66 +71,68 @@ def sin_z(zeta): def blue_x(xi, eta, zeta): return 16*(pi**2)*Lx*dz*dy*cos_y(eta)*cos_z(zeta)*sin_x(xi)**2 + def blue_y(xi, eta, zeta): return 16*(pi**2)*Ly*dx*dz*cos_z(zeta)*cos_x(xi)*sin_y(eta)**2 + def blue_z(xi, eta, zeta): return 16*(pi**2)*Lz*dx*dy*cos_y(eta)*cos_x(xi)*sin_z(zeta)**2 -def coords_from_indices(j, k, l): - return (j-1, k-1, l-1) +def coords_from_indices(j, k, ell): + return (j-1, k-1, ell-1) -def weighted_metric_mat(j, k, l): - """ returns weighted metric matrix at a single grid point with indices j,k,l""" - xi, eta, zeta = coords_from_indices(j, k, l) - arr = np.zeros([3,3]) +def weighted_metric_mat(j, k, ell): + """ returns weighted metric matrix at a single grid point with indices j,k,ell""" + xi, eta, zeta = coords_from_indices(j, k, ell) + arr = np.zeros([3, 3]) - arr[0,0] = dy*dz*(Lprod - blue_x(xi, eta, zeta))/Lprod - arr[1,0] = 4*pi*dx*dy*dz*cos_x(xi)*(-Lz*sin_z(zeta) + arr[0, 0] = dy*dz*(Lprod - blue_x(xi, eta, zeta))/Lprod + arr[1, 0] = 4*pi*dx*dy*dz*cos_x(xi)*(-Lz*sin_z(zeta) + 4*pi*dz*cos_z(zeta)*sin_y(eta)*sin_x(xi))/(Lx*Lz) - arr[2,0] = 4*pi*dx*dy*dz*cos_x(xi)*(-Ly*sin_y(eta) + arr[2, 0] = 4*pi*dx*dy*dz*cos_x(xi)*(-Ly*sin_y(eta) + 4*pi*dy*cos_y(eta)*sin_z(zeta)*sin_x(xi))/(Lx*Ly) - arr[0,1] = 4*pi*dx*dy*dz*cos_y(eta)*(-Lz*sin_z(zeta) + arr[0, 1] = 4*pi*dx*dy*dz*cos_y(eta)*(-Lz*sin_z(zeta) + 4*pi*dz*cos_z(zeta)*sin_x(xi)*sin_y(eta))/(Ly*Lz) - arr[1,1] = dx*dz*(Lprod - blue_y(xi, eta, zeta))/Lprod - arr[2,1] = 4*pi*dx*dy*dz*cos_y(eta)*(-Lx*sin_x(xi) + arr[1, 1] = dx*dz*(Lprod - blue_y(xi, eta, zeta))/Lprod + arr[2, 1] = 4*pi*dx*dy*dz*cos_y(eta)*(-Lx*sin_x(xi) + 4*pi*dx*cos_x(xi)*sin_z(zeta)*sin_y(eta))/(Ly*Lx) - arr[0,2] = 4*pi*dx*dy*dz*cos_z(zeta)*(-Ly*sin_y(eta) + arr[0, 2] = 4*pi*dx*dy*dz*cos_z(zeta)*(-Ly*sin_y(eta) + 4*pi*dy*cos_y(eta)*sin_x(xi)*sin_z(zeta))/(Lz*Ly) - arr[1,2] = 4*pi*dx*dy*dz*cos_z(zeta)*(-Lx*sin_x(xi) + arr[1, 2] = 4*pi*dx*dy*dz*cos_z(zeta)*(-Lx*sin_x(xi) + 4*pi*dx*cos_x(xi)*sin_y(eta)*sin_z(zeta))/(Lz*Lx) - arr[2,2] = dx*dy*(Lprod - blue_z(xi, eta, zeta))/Lprod + arr[2, 2] = dx*dy*(Lprod - blue_z(xi, eta, zeta))/Lprod return arr -def inverse_jacobian(j, k, l): - """ returns inverse jacobian at a single grid point with indices j,k,l""" - xi, eta, zeta = coords_from_indices(j, k, l) +def inverse_jacobian(j, k, ell): + """ returns inverse jacobian at a single grid point with indices j,k,ell""" + xi, eta, zeta = coords_from_indices(j, k, ell) return dx*dy*dz*( Lprod - blue_x(xi, eta, zeta) - blue_y(xi, eta, zeta) - blue_z(xi, eta, zeta) - +16*(pi**3)*dx*dy*dz*sin_x(2*xi)*sin_y(2*eta)*sin_z(2*zeta) + + 16*(pi**3)*dx*dy*dz*sin_x(2*xi)*sin_y(2*eta)*sin_z(2*zeta) )/Lprod def array_along_dim(func, dim): if dim == "x": - return np.array([func(j+1,1,1) for j in range(6)], + return np.array([func(j+1, 1, 1) for j in range(6)], dtype=np.float64, order="F") elif dim == "y": - return np.array([func(1,k+1,1) for k in range(6)], + return np.array([func(1, k+1, 1) for k in range(6)], dtype=np.float64, order="F") elif dim == "z": - return np.array([func(1,1,l+1) for l in range(6)], + return np.array([func(1, 1, ell+1) for ell in range(6)], dtype=np.float64, order="F") else: @@ -141,4 +148,4 @@ def jacobians(dim): def metrics(dim): - return weighted_metrics(dim)*jacobians(dim)[:,None,None] + return weighted_metrics(dim)*jacobians(dim)[:, None, None] diff --git a/test/weno_reference_implementation.py b/test/weno_reference_implementation.py index a7b4095..6215ba2 100644 --- a/test/weno_reference_implementation.py +++ b/test/weno_reference_implementation.py @@ -31,7 +31,8 @@ import loopy as lp # noqa import utilities as u import ideal_gas as gas -### WENO algorithm taken from Nonomura, et al., Comput. Fluids 107 (2015) 242-255 +# WENO algorithm taken from Nonomura, et al., Comput. Fluids 107 (2015) 242-255 + def pointwise_fluxes(states): return np.array([gas.flux(s) for s in states.T]) @@ -100,7 +101,7 @@ def oscillation(fluxes): coeffs1 = np.array([[1, -4, 3], [-1, 0, 1], [-3, 4, -1]]) coeffs2 = np.array([1, -2, 1]) - indices = np.arange(3)[None,:] + np.arange(3)[:,None] + indices = np.arange(3)[None, :] + np.arange(3)[:, None] sum1 = u.transposed_array( [np.dot(c, f) for c, f in zip(coeffs1, fluxes.T[indices])]) @@ -113,14 +114,14 @@ def weno_weights(oscillation, frozen_metric): linear = np.array([0.1, 0.6, 0.3]) eps = 1e-6*frozen_metric - raw_weights = linear[None,:]/(oscillation + eps)**2 + raw_weights = linear[None, :]/(oscillation + eps)**2 - return raw_weights/raw_weights.sum(axis=1)[:,None] + return raw_weights/raw_weights.sum(axis=1)[:, None] def flux_differences(fluxes): w = np.array([-1, 3, -3, 1]) - indices = np.arange(3)[:,None] + np.arange(4)[None,:] + indices = np.arange(3)[:, None] + np.arange(4)[None, :] return u.transposed_array([np.dot(w, f) for f in fluxes.T[indices]]) @@ -129,13 +130,14 @@ def dissipation_part(R, char_fluxes, w, sign): def combine_fluxes(w, f): def combination_weighting(w): return np.array( - [20*w[:,0] - 1, -10*(w[:,0] + w[:,1]) + 5, np.ones(w.shape[0])] + [20*w[:, 0] - 1, -10*(w[:, 0] + w[:, 1]) + 5, + np.ones(w.shape[0])] ).T cw = combination_weighting(w) return np.sum(cw*f, axis=1) - flux_diff = flux_differences(char_fluxes)[:,::sign] + flux_diff = flux_differences(char_fluxes)[:, ::sign] flux_comb = combine_fluxes(w, flux_diff) return -sign*R@flux_comb/60 -- GitLab