From b7152e5b5091d8b6b9a577abeec2ed53ea5e9a51 Mon Sep 17 00:00:00 2001 From: "Timothy A. Smith" Date: Tue, 6 Aug 2019 21:29:29 -0500 Subject: [PATCH 01/17] add new test for compute_flux_derivatives --- data_for_test.py | 117 +++++++++++++++++++++++++++++++++++++++++++---- test.py | 38 ++++++++++++++- utilities.py | 4 +- 3 files changed, 148 insertions(+), 11 deletions(-) diff --git a/data_for_test.py b/data_for_test.py index c83dd53..254c0e3 100644 --- a/data_for_test.py +++ b/data_for_test.py @@ -1,3 +1,4 @@ +import numpy as np import pytest import utilities as u @@ -20,14 +21,14 @@ class FluxDataSingle: self.state_pair = self.swap_array_rows( u.transposed_array_from_string(states_str), self.direction) - self.states = u.expand_to_6(self.state_pair) + self.states = u.expand_to_n(self.state_pair, 6) self.flux_pair = self.swap_array_rows( u.transposed_array_from_string(fluxes_str), self.direction) - self.fluxes = u.expand_to_6(self.flux_pair) + self.fluxes = u.expand_to_n(self.flux_pair, 6) - self.lam_pointwise = u.expand_to_6( - u.transposed_array_from_string(lam_str)) + self.lam_pointwise = u.expand_to_n( + u.transposed_array_from_string(lam_str), 6) self.R = self.swap_array_rows( u.array_from_string(R_str), self.direction) @@ -35,10 +36,10 @@ class FluxDataSingle: u.array_from_string(R_inv_str), self.direction) self.wavespeeds = u.array_from_string(wavespeeds_str) - self.char_fluxes_pos = u.expand_to_6( - u.transposed_array_from_string(char_fluxes_pos_str)) - self.char_fluxes_neg = u.expand_to_6( - u.transposed_array_from_string(char_fluxes_neg_str)) + self.char_fluxes_pos = u.expand_to_n( + u.transposed_array_from_string(char_fluxes_pos_str), 6) + self.char_fluxes_neg = u.expand_to_n( + u.transposed_array_from_string(char_fluxes_neg_str), 6) self.weno_weights_pos = u.transposed_array_from_string(weno_weights_pos_str) self.weno_weights_neg = u.transposed_array_from_string(weno_weights_neg_str) @@ -73,6 +74,73 @@ class FluxDataSingle: # }}} +# {{{ FluxDataVector + +class FluxDataVector: + nvars = 5 + ndim = 3 + dirs = {"x": 1, "y": 2, "z": 3} + halo = 3 + + def __init__(self, nx, ny, nz, + states_str, fluxes_str, flux_derivatives_str, direction): + + self.direction = self.dirs[direction] + + self.nx = nx + self.ny = ny + self.nz = nz + + self.nxhalo = self.nx + 2*self.halo + self.nyhalo = self.ny + 2*self.halo + self.nzhalo = self.nz + 2*self.halo + + self.flux_dims = (self.nvars, self.nx, self.ny, self.nz) + + state_pair = self.swap_array_rows( + u.transposed_array_from_string(states_str), self.direction) + self.states = self.fill_transverse_with_halo( + u.expand_to_n(state_pair, self.nxhalo)) + + flux_pair = self.swap_array_rows( + u.transposed_array_from_string(fluxes_str), self.direction) + self.fluxes = self.fill_transverse_with_halo( + u.expand_to_n(flux_pair, self.nxhalo)) + + flux_derivative_vector = u.transposed_array_from_string( + flux_derivatives_str) + self.flux_derivatives = self.fill_transverse(flux_derivative_vector) + + def swap_array_rows(self, arr, d): + p = self.permutation(d) + arr[p, :] = arr[[1, 2, 3], :] + return arr + + def permutation(self, d): + return [(d-1+i) % 3 + 1 for i in range(3)] + + def fill_transverse(self, vec): + arr = np.empty((self.nvars, self.nx, self.ny, self.nz), + dtype=np.float32, order="F") + + for j in range(self.ny): + for k in range(self.nz): + arr[:, :, j, k] = vec + + return arr + + def fill_transverse_with_halo(self, vec): + arr = np.empty((self.nvars, self.nxhalo, self.nyhalo, self.nzhalo), + dtype=np.float32, order="F") + + for j in range(self.nyhalo): + for k in range(self.nzhalo): + arr[:, :, j, k] = vec + + return arr + +# }}} + single_data = {} @@ -700,6 +768,33 @@ single_data["Case d:z"] = FluxDataSingle( # }}} +vector_data = {} + +# {{{ Input data: Case (a) + +vector_data["Case a:x"] = FluxDataVector( + nx=6, + ny=2, + nz=2, + states_str="2 4 4 4 20,1 1 1 1 5.5", + fluxes_str="4 11.2 8 8 46.4,1 2.6 1 1 7.1", + flux_derivatives_str=("5.947562442543131e-10 2.1078481182712494e-9 " + "1.3438068435789319e-9 1.3438068435789319e-9 7.633488507963193e-9," + "-4.776150586138783e-9 -2.2654063513982692e-8 " + "-1.126473048174148e-8 -1.126473048174148e-8 -6.921793982428426e-8," + "0.35371022398107277 1.0479485682313214 " + "0.9952234479820374 0.9952234479820374 4.990392771216321," + "-3.3536472988775694 -9.647979794399475 " + "-7.995160516637247 -7.995160516637247 -44.29004015184655," + "-0.00007101671509879282 0.00003526758437111255 " + "-0.00007101721682811757 -0.00007101721682811757 -0.000397921213337149," + "8.0957929897707e-6 -4.020870000953636e-6 " + "8.09579296179308e-6 8.09579296179308e-6 0.000045363428017530794"), + direction="x") + +# }}} + + @pytest.fixture(scope="session", params=[ "Case a:x", "Case a:y", "Case a:z", "Case b:x", "Case b:y", "Case b:z", @@ -709,4 +804,10 @@ def flux_test_data_fixture(request): return single_data[request.param] +@pytest.fixture(scope="session", params=[ + "Case a:x"]) +def cfd_test_data_fixture(request): + return vector_data[request.param] + + # vim: foldmethod=marker diff --git a/test.py b/test.py index ab1c138..7645b26 100644 --- a/test.py +++ b/test.py @@ -16,8 +16,10 @@ from pyopencl.tools import ( # noqa import utilities as u from data_for_test import flux_test_data_fixture # noqa: F401 +from data_for_test import cfd_test_data_fixture # noqa: F401 +@pytest.mark.slow def test_weno_flux_uniform_grid(ctx_factory, flux_test_data_fixture): data = flux_test_data_fixture @@ -37,6 +39,7 @@ def test_weno_flux_uniform_grid(ctx_factory, flux_test_data_fixture): u.compare_arrays(flux_dev.get(), data.weno_flux) +@pytest.mark.slow def test_consistent_part_uniform_grid(ctx_factory, flux_test_data_fixture): data = flux_test_data_fixture @@ -52,6 +55,7 @@ def test_consistent_part_uniform_grid(ctx_factory, flux_test_data_fixture): u.compare_arrays(consistent_dev.get(), data.consistent) +@pytest.mark.slow def test_dissipation_part_pos_uniform_grid(ctx_factory, flux_test_data_fixture): data = flux_test_data_fixture @@ -69,6 +73,7 @@ def test_dissipation_part_pos_uniform_grid(ctx_factory, flux_test_data_fixture): u.compare_arrays(dissipation_dev.get(), data.dissipation_pos) +@pytest.mark.slow def test_dissipation_part_neg_uniform_grid(ctx_factory, flux_test_data_fixture): data = flux_test_data_fixture @@ -86,6 +91,7 @@ def test_dissipation_part_neg_uniform_grid(ctx_factory, flux_test_data_fixture): u.compare_arrays(dissipation_dev.get(), data.dissipation_neg) +@pytest.mark.slow def test_weno_weights_pos_uniform_grid(ctx_factory, flux_test_data_fixture): data = flux_test_data_fixture @@ -102,6 +108,7 @@ def test_weno_weights_pos_uniform_grid(ctx_factory, flux_test_data_fixture): u.compare_arrays(weights_dev.get(), data.weno_weights_pos) +@pytest.mark.slow def test_weno_weights_neg_uniform_grid(ctx_factory, flux_test_data_fixture): data = flux_test_data_fixture @@ -118,6 +125,7 @@ def test_weno_weights_neg_uniform_grid(ctx_factory, flux_test_data_fixture): u.compare_arrays(weights_dev.get(), data.weno_weights_neg) +@pytest.mark.slow def test_flux_splitting_uniform_grid(ctx_factory, flux_test_data_fixture): data = flux_test_data_fixture @@ -139,6 +147,7 @@ def test_flux_splitting_uniform_grid(ctx_factory, flux_test_data_fixture): u.compare_arrays(fluxes_neg_dev.get(), data.char_fluxes_neg) +@pytest.mark.slow def test_pointwise_eigenvalues_ideal_gas(ctx_factory, flux_test_data_fixture): data = flux_test_data_fixture @@ -153,6 +162,7 @@ def test_pointwise_eigenvalues_ideal_gas(ctx_factory, flux_test_data_fixture): u.compare_arrays(lam_dev.get(), data.lam_pointwise) +@pytest.mark.slow def test_roe_uniform_grid_ideal_gas(ctx_factory, flux_test_data_fixture): data = flux_test_data_fixture @@ -192,6 +202,7 @@ def test_roe_uniform_grid_ideal_gas(ctx_factory, flux_test_data_fixture): check_roe_property(data.state_pair, data.flux_pair, R, R_inv, lam) +@pytest.mark.slow @pytest.mark.parametrize("lam_pointwise_str,lam_roe_str,lam_expected_str", [ ("1 2 3 4 5,2 4 6 8 10", "1.5 3 4.5 6 7.5", "2.2 4.4 6.6 8.8 11"), ("1 2 3 4 5,-2 -4 -6 -8 -10", "1.5 3 4.5 6 7.5", "2.2 4.4 6.6 8.8 11"), @@ -206,7 +217,8 @@ def test_lax_wavespeeds( nvars = 5 - lam_pointwise = u.expand_to_6(u.transposed_array_from_string(lam_pointwise_str)) + lam_pointwise = u.expand_to_n( + u.transposed_array_from_string(lam_pointwise_str), 6) lam_roe = u.array_from_string(lam_roe_str) lam_dev = u.empty_array_on_device(queue, nvars) @@ -217,6 +229,7 @@ def test_lax_wavespeeds( u.compare_arrays(lam_dev.get(), lam_expected) +@pytest.mark.slow def test_matvec(ctx_factory): prg = u.get_weno_program_with_root_kernel("mult_mat_vec") queue = u.get_queue(ctx_factory) @@ -231,6 +244,29 @@ def test_matvec(ctx_factory): u.compare_arrays(a.get()@b.get(), c.get()) +def test_compute_flux_derivatives_uniform_grid(ctx_factory, cfd_test_data_fixture): + data = cfd_test_data_fixture + + prg = u.get_weno_program() + queue = u.get_queue(ctx_factory) + prg = prg.copy(target=lp.PyOpenCLTarget(queue.device)) + + flux_derivatives_dev = u.empty_array_on_device(queue, *data.flux_dims) + + prg(queue, + ndim=data.ndim, + nx=data.nx, + ny=data.ny, + nz=data.nz, + states=data.states, + fluxes=data.fluxes, + metrics=data.metrics, + metric_jacobians=data.jacobians, + flux_derivatives=flux_derivatives_dev) + + u.compare_arrays(flux_derivatives_dev.get(), data.flux_derivatives) + + @pytest.mark.slow def test_compute_flux_derivatives(ctx_factory): prg = u.get_weno_program() diff --git a/utilities.py b/utilities.py index 129680a..0296126 100644 --- a/utilities.py +++ b/utilities.py @@ -68,8 +68,8 @@ def split_map_to_list(string, map_func, splitter): return list(map(map_func, string.split(splitter))) -def expand_to_6(pair): - return np.repeat(pair, 3, axis=1).copy(order="F") +def expand_to_n(pair, n): + return np.repeat(pair, n/2, axis=1).copy(order="F") # }}} -- GitLab From 52a5b50cbc30e01f51b7f6b49515bb850a19399e Mon Sep 17 00:00:00 2001 From: "Timothy A. Smith" Date: Tue, 6 Aug 2019 21:31:29 -0500 Subject: [PATCH 02/17] move identity_matrix to utilities --- test.py | 5 +---- utilities.py | 4 ++++ 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/test.py b/test.py index 7645b26..5a8d53f 100644 --- a/test.py +++ b/test.py @@ -166,9 +166,6 @@ def test_pointwise_eigenvalues_ideal_gas(ctx_factory, flux_test_data_fixture): def test_roe_uniform_grid_ideal_gas(ctx_factory, flux_test_data_fixture): data = flux_test_data_fixture - def identity_matrix(n): - return np.identity(n).astype(np.float32).copy(order="F") - def check_roe_identity(states, R, R_inv): d_state = states[:, 1] - states[:, 0] u.compare_arrays(R@(R_inv@d_state), d_state) @@ -184,7 +181,7 @@ def test_roe_uniform_grid_ideal_gas(ctx_factory, flux_test_data_fixture): prg = u.get_weno_program_with_root_kernel("roe_eigensystem") queue = u.get_queue(ctx_factory) - metrics_frozen = identity_matrix(data.ndim) + metrics_frozen = u.identity_matrix(data.ndim) R_dev = u.empty_array_on_device(queue, data.nvars, data.nvars) R_inv_dev = u.empty_array_on_device(queue, data.nvars, data.nvars) diff --git a/utilities.py b/utilities.py index 0296126..61771fd 100644 --- a/utilities.py +++ b/utilities.py @@ -14,6 +14,10 @@ def compare_arrays(a, b): assert a == approx(b, rel=1e-5, abs=2e-5) +def identity_matrix(n): + return np.identity(n).astype(np.float32).copy(order="F") + + def random_array_on_device(queue, *shape): ary = empty_array_on_device(queue, *shape) cl.clrandom.fill_rand(ary) -- GitLab From 7cc958a58affdc46879ab4a62ca06bb0a145fc67 Mon Sep 17 00:00:00 2001 From: "Timothy A. Smith" Date: Tue, 6 Aug 2019 21:35:22 -0500 Subject: [PATCH 03/17] added empty_array utility --- utilities.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/utilities.py b/utilities.py index 61771fd..0026bf5 100644 --- a/utilities.py +++ b/utilities.py @@ -18,16 +18,20 @@ def identity_matrix(n): return np.identity(n).astype(np.float32).copy(order="F") -def random_array_on_device(queue, *shape): - ary = empty_array_on_device(queue, *shape) - cl.clrandom.fill_rand(ary) - return ary +def empty_array(*shape): + return np.empty(shape, dtype=np.float32, order="F") def empty_array_on_device(queue, *shape): return cl.array.empty(queue, shape, dtype=np.float32, order="F") +def random_array_on_device(queue, *shape): + ary = empty_array_on_device(queue, *shape) + cl.clrandom.fill_rand(ary) + return ary + + def arrays_from_string(string_arrays): return split_map_to_list(string_arrays, array_from_string, ":") -- GitLab From 966e05e06156daa9383647faf5232e28900f05c8 Mon Sep 17 00:00:00 2001 From: "Timothy A. Smith" Date: Tue, 6 Aug 2019 21:39:31 -0500 Subject: [PATCH 04/17] added metrics, metric_jacobians to setup --- data_for_test.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/data_for_test.py b/data_for_test.py index 254c0e3..7397544 100644 --- a/data_for_test.py +++ b/data_for_test.py @@ -107,6 +107,20 @@ class FluxDataVector: self.fluxes = self.fill_transverse_with_halo( u.expand_to_n(flux_pair, self.nxhalo)) + identity = u.identity_matrix(self.ndim) + self.metrics = u.empty_array(self.ndim, self.ndim, + self.nxhalo, self.nyhalo, self.nzhalo) + for i in range(self.nxhalo): + for j in range(self.nyhalo): + for k in range(self.nzhalo): + self.metrics[:, :, i, j, k] = identity + + self.metric_jacobians = u.empty_array(self.nxhalo, self.nyhalo, self.nzhalo) + for i in range(self.nxhalo): + for j in range(self.nyhalo): + for k in range(self.nzhalo): + self.metric_jacobians[i, j, k] = 1.0 + flux_derivative_vector = u.transposed_array_from_string( flux_derivatives_str) self.flux_derivatives = self.fill_transverse(flux_derivative_vector) @@ -120,8 +134,7 @@ class FluxDataVector: return [(d-1+i) % 3 + 1 for i in range(3)] def fill_transverse(self, vec): - arr = np.empty((self.nvars, self.nx, self.ny, self.nz), - dtype=np.float32, order="F") + arr = u.empty_array(self.nvars, self.nx, self.ny, self.nz) for j in range(self.ny): for k in range(self.nz): @@ -130,8 +143,7 @@ class FluxDataVector: return arr def fill_transverse_with_halo(self, vec): - arr = np.empty((self.nvars, self.nxhalo, self.nyhalo, self.nzhalo), - dtype=np.float32, order="F") + arr = u.empty_array(self.nvars, self.nxhalo, self.nyhalo, self.nzhalo) for j in range(self.nyhalo): for k in range(self.nzhalo): -- GitLab From 24a152a7bf5470a6eebfcce10b5216e37827ae7b Mon Sep 17 00:00:00 2001 From: "Timothy A. Smith" Date: Tue, 6 Aug 2019 21:44:54 -0500 Subject: [PATCH 05/17] fix some minor bugs in setup --- data_for_test.py | 4 ++-- test.py | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/data_for_test.py b/data_for_test.py index 7397544..a82c084 100644 --- a/data_for_test.py +++ b/data_for_test.py @@ -115,11 +115,11 @@ class FluxDataVector: for k in range(self.nzhalo): self.metrics[:, :, i, j, k] = identity - self.metric_jacobians = u.empty_array(self.nxhalo, self.nyhalo, self.nzhalo) + self.jacobians = u.empty_array(self.nxhalo, self.nyhalo, self.nzhalo) for i in range(self.nxhalo): for j in range(self.nyhalo): for k in range(self.nzhalo): - self.metric_jacobians[i, j, k] = 1.0 + self.jacobians[i, j, k] = 1.0 flux_derivative_vector = u.transposed_array_from_string( flux_derivatives_str) diff --git a/test.py b/test.py index 5a8d53f..92e530d 100644 --- a/test.py +++ b/test.py @@ -251,6 +251,7 @@ def test_compute_flux_derivatives_uniform_grid(ctx_factory, cfd_test_data_fixtur flux_derivatives_dev = u.empty_array_on_device(queue, *data.flux_dims) prg(queue, + nvars=data.nvars, ndim=data.ndim, nx=data.nx, ny=data.ny, -- GitLab From 5b43aa4e7212e2c91af5f15f8b9c58567a9ceaf7 Mon Sep 17 00:00:00 2001 From: "Timothy A. Smith" Date: Tue, 6 Aug 2019 21:53:20 -0500 Subject: [PATCH 06/17] add specification for passing the direction to compute flux_derivatives --- test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test.py b/test.py index 92e530d..555ae7d 100644 --- a/test.py +++ b/test.py @@ -256,6 +256,7 @@ def test_compute_flux_derivatives_uniform_grid(ctx_factory, cfd_test_data_fixtur nx=data.nx, ny=data.ny, nz=data.nz, + dim=data.direction, states=data.states, fluxes=data.fluxes, metrics=data.metrics, -- GitLab From 33304e5e9a236f3df02377cc3966bc2ecc1b740a Mon Sep 17 00:00:00 2001 From: "Timothy A. Smith" Date: Tue, 6 Aug 2019 21:57:19 -0500 Subject: [PATCH 07/17] change name of var for current dimension --- test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test.py b/test.py index 555ae7d..59b95e9 100644 --- a/test.py +++ b/test.py @@ -256,7 +256,7 @@ def test_compute_flux_derivatives_uniform_grid(ctx_factory, cfd_test_data_fixtur nx=data.nx, ny=data.ny, nz=data.nz, - dim=data.direction, + d=data.direction, states=data.states, fluxes=data.fluxes, metrics=data.metrics, -- GitLab From 773e2ae760d5e0a03c01129034ed499280828d00 Mon Sep 17 00:00:00 2001 From: "Timothy A. Smith" Date: Tue, 3 Sep 2019 13:48:41 -0500 Subject: [PATCH 08/17] fix some outstanding issues from merge --- test/data_for_test.py | 12 ++++++------ test/utilities.py | 10 ++++------ 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/test/data_for_test.py b/test/data_for_test.py index 8bcfe6a..18cd5ea 100644 --- a/test/data_for_test.py +++ b/test/data_for_test.py @@ -108,15 +108,15 @@ class FluxDataVector: self.fluxes = self.fill_transverse_with_halo( u.expand_to_n(flux_pair, self.nxhalo)) - identity = u.identity_matrix(self.ndim) - self.metrics = u.empty_array(self.ndim, self.ndim, - self.nxhalo, self.nyhalo, self.nzhalo) + identity = np.identity(self.ndim, dtype=float64, order="F") + self.metrics = np.empty((self.ndim, self.ndim, + self.nxhalo, self.nyhalo, self.nzhalo)) for i in range(self.nxhalo): for j in range(self.nyhalo): for k in range(self.nzhalo): self.metrics[:, :, i, j, k] = identity - self.jacobians = u.empty_array(self.nxhalo, self.nyhalo, self.nzhalo) + self.jacobians = np.empty((self.nxhalo, self.nyhalo, self.nzhalo)) for i in range(self.nxhalo): for j in range(self.nyhalo): for k in range(self.nzhalo): @@ -135,7 +135,7 @@ class FluxDataVector: return [(d-1+i) % 3 + 1 for i in range(3)] def fill_transverse(self, vec): - arr = u.empty_array(self.nvars, self.nx, self.ny, self.nz) + arr = np.empty((self.nvars, self.nx, self.ny, self.nz)) for j in range(self.ny): for k in range(self.nz): @@ -144,7 +144,7 @@ class FluxDataVector: return arr def fill_transverse_with_halo(self, vec): - arr = u.empty_array(self.nvars, self.nxhalo, self.nyhalo, self.nzhalo) + arr = np.empty((self.nvars, self.nxhalo, self.nyhalo, self.nzhalo)) for j in range(self.nyhalo): for k in range(self.nzhalo): diff --git a/test/utilities.py b/test/utilities.py index 8257277..b02179c 100644 --- a/test/utilities.py +++ b/test/utilities.py @@ -24,12 +24,10 @@ def empty_array_on_device(queue, *shape): return cl.array.empty(queue, shape, dtype=np.float64, order="F") -def identity_matrix(n): - return np.identity(n).astype(np.float32).copy(order="F") - - -def empty_array(*shape): - return np.empty(shape, dtype=np.float32, order="F") +def random_array_on_device(queue, *shape): + ary = empty_array_on_device(queue, *shape) + cl.clrandom.fill_rand(ary) + return ary def arrays_from_string(string_arrays): -- GitLab From 685ddd5c8e515f4a2f79c7c613b1bf0293b00f9f Mon Sep 17 00:00:00 2001 From: "Timothy A. Smith" Date: Tue, 3 Sep 2019 14:14:30 -0500 Subject: [PATCH 09/17] cleanup some more post-merge issues --- test/data_for_test.py | 4 ++-- test/test_eigensystem.py | 3 ++- test/test_flux_derivatives.py | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/test/data_for_test.py b/test/data_for_test.py index 18cd5ea..558bf33 100644 --- a/test/data_for_test.py +++ b/test/data_for_test.py @@ -27,7 +27,7 @@ class FluxDataSingle: self.state_pair = self.swap_array_rows( u.transposed_array_from_string(states_str), self.dir_internal) - self.states = u.expand_to_6(self.state_pair) + self.states = u.expand_to_n(self.state_pair, 6) # FIXME: these should be generalized fluxes # FIXME: make a clear distinction between fluxes in physical and @@ -108,7 +108,7 @@ class FluxDataVector: self.fluxes = self.fill_transverse_with_halo( u.expand_to_n(flux_pair, self.nxhalo)) - identity = np.identity(self.ndim, dtype=float64, order="F") + identity = np.identity(self.ndim, dtype=np.float64).copy(order="F") self.metrics = np.empty((self.ndim, self.ndim, self.nxhalo, self.nyhalo, self.nzhalo)) for i in range(self.nxhalo): diff --git a/test/test_eigensystem.py b/test/test_eigensystem.py index 4b0a7a6..de37e89 100644 --- a/test/test_eigensystem.py +++ b/test/test_eigensystem.py @@ -83,7 +83,8 @@ def test_lax_wavespeeds( nvars = 5 - lam_pointwise = u.expand_to_6(u.transposed_array_from_string(lam_pointwise_str)) + lam_pointwise = u.expand_to_n( + u.transposed_array_from_string(lam_pointwise_str), 6) lam_roe = u.array_from_string(lam_roe_str) lam_dev = u.empty_array_on_device(queue, nvars) diff --git a/test/test_flux_derivatives.py b/test/test_flux_derivatives.py index b768625..f506f85 100644 --- a/test/test_flux_derivatives.py +++ b/test/test_flux_derivatives.py @@ -17,10 +17,10 @@ from pyopencl.tools import ( # noqa import utilities as u from data_for_test import ( # noqa: F401 cfd_test_data_fixture - vector_data as vd ) +@pytest.mark.slow def test_compute_flux_derivatives_uniform_grid(ctx_factory, cfd_test_data_fixture): data = cfd_test_data_fixture -- GitLab From ffe96d2ab6014c9a587c9875d8ceb74d301b8399 Mon Sep 17 00:00:00 2001 From: "Timothy A. Smith" Date: Tue, 3 Sep 2019 14:26:05 -0500 Subject: [PATCH 10/17] add some FIXME notes --- test/data_for_test.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/data_for_test.py b/test/data_for_test.py index 558bf33..bfc3983 100644 --- a/test/data_for_test.py +++ b/test/data_for_test.py @@ -7,6 +7,9 @@ import reference_implementation as ref # {{{ FluxDataSingle class FluxDataSingle: + # FIXME: can we set some of these constants from ref.gas? + # -- if all nvars references come from there, it's relatively easy to + # introduce a new gas with more (e.g. scalar) variables nvars = 5 ndim = 3 dirs = {"x": 1, "y": 2, "z": 3} @@ -25,6 +28,10 @@ class FluxDataSingle: self.frozen_jacobian = np.mean(self.jacobians[2:4], axis=0) self.combined_frozen_metrics = 1.0 + # FIXME: Move array_from_string stuff outside FluxDataSingle + # -- just pass an array & have external utilities that generate + # Riemann, sine wave, etc. initial conditions + # FIXME: Consider handling row swapping outside as well? self.state_pair = self.swap_array_rows( u.transposed_array_from_string(states_str), self.dir_internal) self.states = u.expand_to_n(self.state_pair, 6) @@ -77,6 +84,7 @@ class FluxDataSingle: # {{{ FluxDataVector +# FIXME: is there a better way to divide responsibilities with these fixture classes? class FluxDataVector: nvars = 5 ndim = 3 @@ -103,6 +111,7 @@ class FluxDataVector: self.states = self.fill_transverse_with_halo( u.expand_to_n(state_pair, self.nxhalo)) + # FIXME: convert all this stuff to reference implementation flux_pair = self.swap_array_rows( u.transposed_array_from_string(fluxes_str), self.direction) self.fluxes = self.fill_transverse_with_halo( -- GitLab From da766e215afc5d66ab00380866ec4b21dbb9a0f0 Mon Sep 17 00:00:00 2001 From: "Timothy A. Smith" Date: Tue, 3 Sep 2019 21:35:44 -0500 Subject: [PATCH 11/17] rename WENO reference implementation module for clarity --- test/data_for_test.py | 2 +- ...rence_implementation.py => weno_reference_implementation.py} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename test/{reference_implementation.py => weno_reference_implementation.py} (100%) diff --git a/test/data_for_test.py b/test/data_for_test.py index bfc3983..a106272 100644 --- a/test/data_for_test.py +++ b/test/data_for_test.py @@ -1,7 +1,7 @@ import numpy as np import pytest import utilities as u -import reference_implementation as ref +import weno_reference_implementation as ref # {{{ FluxDataSingle diff --git a/test/reference_implementation.py b/test/weno_reference_implementation.py similarity index 100% rename from test/reference_implementation.py rename to test/weno_reference_implementation.py -- GitLab From 67e796e827a9091e6f57a1608d1ca54352751515 Mon Sep 17 00:00:00 2001 From: "Timothy A. Smith" Date: Mon, 9 Sep 2019 22:38:49 -0500 Subject: [PATCH 12/17] test for compute_flux_derivative now has correct arguments with correct dimensions --- test/data_for_test.py | 188 +++++++++++++++++----------------- test/test_flux_derivatives.py | 1 - 2 files changed, 94 insertions(+), 95 deletions(-) diff --git a/test/data_for_test.py b/test/data_for_test.py index a106272..6cc9599 100644 --- a/test/data_for_test.py +++ b/test/data_for_test.py @@ -32,6 +32,7 @@ class FluxDataSingle: # -- just pass an array & have external utilities that generate # Riemann, sine wave, etc. initial conditions # FIXME: Consider handling row swapping outside as well? + # FIXME: Do we even need to swap rows? self.state_pair = self.swap_array_rows( u.transposed_array_from_string(states_str), self.dir_internal) self.states = u.expand_to_n(self.state_pair, 6) @@ -91,10 +92,9 @@ class FluxDataVector: dirs = {"x": 1, "y": 2, "z": 3} halo = 3 - def __init__(self, nx, ny, nz, - states_str, fluxes_str, flux_derivatives_str, direction): - + def __init__(self, nx, ny, nz, states_str, direction): self.direction = self.dirs[direction] + self.dir_internal = self.direction-1 self.nx = nx self.ny = ny @@ -106,34 +106,39 @@ class FluxDataVector: self.flux_dims = (self.nvars, self.nx, self.ny, self.nz) + self.metrics = np.stack( + [np.stack( + [np.stack( + [np.identity(self.ndim) for i in range(self.nxhalo)], + axis=-1) for j in range(self.nyhalo)], + axis=-1) for k in range(self.nzhalo)], + axis=-1).copy(order="F") + self.jacobians = np.ones((self.nxhalo, self.nyhalo, self.nzhalo)) + state_pair = self.swap_array_rows( u.transposed_array_from_string(states_str), self.direction) - self.states = self.fill_transverse_with_halo( - u.expand_to_n(state_pair, self.nxhalo)) - - # FIXME: convert all this stuff to reference implementation - flux_pair = self.swap_array_rows( - u.transposed_array_from_string(fluxes_str), self.direction) - self.fluxes = self.fill_transverse_with_halo( - u.expand_to_n(flux_pair, self.nxhalo)) - - identity = np.identity(self.ndim, dtype=np.float64).copy(order="F") - self.metrics = np.empty((self.ndim, self.ndim, - self.nxhalo, self.nyhalo, self.nzhalo)) - for i in range(self.nxhalo): - for j in range(self.nyhalo): - for k in range(self.nzhalo): - self.metrics[:, :, i, j, k] = identity - - self.jacobians = np.empty((self.nxhalo, self.nyhalo, self.nzhalo)) - for i in range(self.nxhalo): - for j in range(self.nyhalo): - for k in range(self.nzhalo): - self.jacobians[i, j, k] = 1.0 - - flux_derivative_vector = u.transposed_array_from_string( - flux_derivatives_str) - self.flux_derivatives = self.fill_transverse(flux_derivative_vector) + # FIXME: Move array_from_string stuff outside FluxDataSingle + # -- just pass an array & have external utilities that generate + # Riemann, sine wave, etc. initial conditions + # FIXME: Consider handling row swapping outside as well? + # FIXME: Do we even need to swap rows? + self.state_pair = self.swap_array_rows( + u.transposed_array_from_string(states_str), self.dir_internal) + # NOTE: dimensions are nvars x nxhalo x nyhalo x nzhalo + self.states = self.fill_from_pair() + + # NOTE: dimensions are nvars x nxhalo x nyhalo x nzhalo + # FIXME: these should be generalized fluxes + # FIXME: make a clear distinction between fluxes in physical and + # generalized coordinates + # FIXME: fix bug in pointwise flux computation + self.fluxes = np.zeros((self.nvars, self.nxhalo, self.nyhalo, self.nzhalo)) + #self.fluxes = ref.pointwise_fluxes( + # self.states)[:,:,:,:,self.dir_internal].T.copy(order="F") + + # FIXME: use reference implementation + # NOTE: dimensions are nvars x nx x ny x nz + self.flux_derivatives = np.zeros((self.nvars, self.nx, self.ny, self.nz)) def swap_array_rows(self, arr, d): p = self.permutation(d) @@ -143,88 +148,83 @@ class FluxDataVector: def permutation(self, d): return [(d-1+i) % 3 + 1 for i in range(3)] - def fill_transverse(self, vec): - arr = np.empty((self.nvars, self.nx, self.ny, self.nz)) - - for j in range(self.ny): - for k in range(self.nz): - arr[:, :, j, k] = vec - - return arr + def fill_from_pair(self): + d = self.dir_internal + nx_arr = np.array([self.nxhalo, self.nyhalo, self.nzhalo]) + result = u.expand_to_n(self.state_pair, nx_arr[d]) - def fill_transverse_with_halo(self, vec): - arr = np.empty((self.nvars, self.nxhalo, self.nyhalo, self.nzhalo)) + 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): + result = self.add_dimension(result, nx_arr[i]) - for j in range(self.nyhalo): - for k in range(self.nzhalo): - arr[:, :, j, k] = vec + return result.copy(order="F") - return arr + def add_dimension(self, arr, n): + return np.stack([arr for i in range(n)], axis=-1) # }}} single_data = {} -single_data["Case flat:x"] = FluxDataSingle( - states_str="1 1 1 1 5.5,1 1 1 1 5.5", direction="x") -single_data["Case flat:y"] = FluxDataSingle( - states_str="1 1 1 1 5.5,1 1 1 1 5.5", direction="y") -single_data["Case flat:z"] = FluxDataSingle( - states_str="1 1 1 1 5.5,1 1 1 1 5.5", direction="z") - -single_data["Case a:x"] = FluxDataSingle( - states_str="2 4 4 4 20,1 1 1 1 5.5", direction="x") -single_data["Case a:y"] = FluxDataSingle( - states_str="2 4 4 4 20,1 1 1 1 5.5", direction="y") -single_data["Case a:z"] = FluxDataSingle( - states_str="2 4 4 4 20,1 1 1 1 5.5", direction="z") - -single_data["Case b:x"] = FluxDataSingle( - states_str="1 -1 -1 -1 5.5,2 -4 -4 -4 20", direction="x") -single_data["Case b:y"] = FluxDataSingle( - states_str="1 -1 -1 -1 5.5,2 -4 -4 -4 20", direction="y") -single_data["Case b:z"] = FluxDataSingle( - states_str="1 -1 -1 -1 5.5,2 -4 -4 -4 20", direction="z") - -single_data["Case c:x"] = FluxDataSingle( - states_str="2 4 8 12 64,1 1 2 3 11", direction="x") -single_data["Case c:y"] = FluxDataSingle( - states_str="2 8 12 4 64,1 2 3 1 11", direction="y") -single_data["Case c:z"] = FluxDataSingle( - states_str="2 12 4 8 64,1 3 1 2 11", direction="z") - -single_data["Case d:x"] = FluxDataSingle( - states_str="1 -1 -2 -3 11,2 -4 -8 -12 64", direction="x") -single_data["Case d:y"] = FluxDataSingle( - states_str="1 -2 -3 -1 11,2 -8 -12 -4 64", direction="y") -single_data["Case d:z"] = FluxDataSingle( - states_str="1 -3 -1 -2 11,2 -12 -4 -8 64", direction="z") +#single_data["Case flat:x"] = FluxDataSingle( +# states_str="1 1 1 1 5.5,1 1 1 1 5.5", direction="x") +#single_data["Case flat:y"] = FluxDataSingle( +# states_str="1 1 1 1 5.5,1 1 1 1 5.5", direction="y") +#single_data["Case flat:z"] = FluxDataSingle( +# states_str="1 1 1 1 5.5,1 1 1 1 5.5", direction="z") +# +#single_data["Case a:x"] = FluxDataSingle( +# states_str="2 4 4 4 20,1 1 1 1 5.5", direction="x") +#single_data["Case a:y"] = FluxDataSingle( +# states_str="2 4 4 4 20,1 1 1 1 5.5", direction="y") +#single_data["Case a:z"] = FluxDataSingle( +# states_str="2 4 4 4 20,1 1 1 1 5.5", direction="z") +# +#single_data["Case b:x"] = FluxDataSingle( +# states_str="1 -1 -1 -1 5.5,2 -4 -4 -4 20", direction="x") +#single_data["Case b:y"] = FluxDataSingle( +# states_str="1 -1 -1 -1 5.5,2 -4 -4 -4 20", direction="y") +#single_data["Case b:z"] = FluxDataSingle( +# states_str="1 -1 -1 -1 5.5,2 -4 -4 -4 20", direction="z") +# +#single_data["Case c:x"] = FluxDataSingle( +# states_str="2 4 8 12 64,1 1 2 3 11", direction="x") +#single_data["Case c:y"] = FluxDataSingle( +# states_str="2 8 12 4 64,1 2 3 1 11", direction="y") +#single_data["Case c:z"] = FluxDataSingle( +# states_str="2 12 4 8 64,1 3 1 2 11", direction="z") +# +#single_data["Case d:x"] = FluxDataSingle( +# states_str="1 -1 -2 -3 11,2 -4 -8 -12 64", direction="x") +#single_data["Case d:y"] = FluxDataSingle( +# states_str="1 -2 -3 -1 11,2 -8 -12 -4 64", direction="y") +#single_data["Case d:z"] = FluxDataSingle( +# states_str="1 -3 -1 -2 11,2 -12 -4 -8 64", direction="z") vector_data = {} # {{{ Input data: Case (a) +vector_data["Case zero"] = FluxDataVector( + nx=6, ny=2, nz=2, + states_str="0 0 0 0 0,0 0 0 0 0", + direction="x") vector_data["Case a:x"] = FluxDataVector( - nx=6, - ny=2, - nz=2, + nx=6, ny=2, nz=2, states_str="2 4 4 4 20,1 1 1 1 5.5", - fluxes_str="4 11.2 8 8 46.4,1 2.6 1 1 7.1", - flux_derivatives_str=("5.947562442543131e-10 2.1078481182712494e-9 " - "1.3438068435789319e-9 1.3438068435789319e-9 7.633488507963193e-9," - "-4.776150586138783e-9 -2.2654063513982692e-8 " - "-1.126473048174148e-8 -1.126473048174148e-8 -6.921793982428426e-8," - "0.35371022398107277 1.0479485682313214 " - "0.9952234479820374 0.9952234479820374 4.990392771216321," - "-3.3536472988775694 -9.647979794399475 " - "-7.995160516637247 -7.995160516637247 -44.29004015184655," - "-0.00007101671509879282 0.00003526758437111255 " - "-0.00007101721682811757 -0.00007101721682811757 -0.000397921213337149," - "8.0957929897707e-6 -4.020870000953636e-6 " - "8.09579296179308e-6 8.09579296179308e-6 0.000045363428017530794"), direction="x") +vector_data["Case a:y"] = FluxDataVector( + nx=2, ny=6, nz=2, + states_str="2 4 4 4 20,1 1 1 1 5.5", + direction="y") +vector_data["Case a:z"] = FluxDataVector( + nx=2, ny=2, nz=6, + states_str="2 4 4 4 20,1 1 1 1 5.5", + direction="z") # }}} @@ -240,7 +240,7 @@ def flux_test_data_fixture(request): @pytest.fixture(scope="session", params=[ - "Case a:x"]) + "Case zero"]) def cfd_test_data_fixture(request): return vector_data[request.param] diff --git a/test/test_flux_derivatives.py b/test/test_flux_derivatives.py index f506f85..f2dc9d2 100644 --- a/test/test_flux_derivatives.py +++ b/test/test_flux_derivatives.py @@ -20,7 +20,6 @@ from data_for_test import ( # noqa: F401 ) -@pytest.mark.slow def test_compute_flux_derivatives_uniform_grid(ctx_factory, cfd_test_data_fixture): data = cfd_test_data_fixture -- GitLab From 58af7cb06d740ef8fd9a2f1986eb584c02f0f08d Mon Sep 17 00:00:00 2001 From: "Timothy A. Smith" Date: Mon, 9 Sep 2019 22:57:48 -0500 Subject: [PATCH 13/17] bugfixes in array ordering --- test/data_for_test.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/test/data_for_test.py b/test/data_for_test.py index 6cc9599..f3b4578 100644 --- a/test/data_for_test.py +++ b/test/data_for_test.py @@ -113,7 +113,8 @@ class FluxDataVector: axis=-1) for j in range(self.nyhalo)], axis=-1) for k in range(self.nzhalo)], axis=-1).copy(order="F") - self.jacobians = np.ones((self.nxhalo, self.nyhalo, self.nzhalo)) + 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) @@ -132,13 +133,15 @@ class FluxDataVector: # FIXME: make a clear distinction between fluxes in physical and # generalized coordinates # FIXME: fix bug in pointwise flux computation - self.fluxes = np.zeros((self.nvars, self.nxhalo, self.nyhalo, self.nzhalo)) + self.fluxes = np.zeros((self.nvars, self.nxhalo, self.nyhalo, + self.nzhalo), order="F") #self.fluxes = ref.pointwise_fluxes( # self.states)[:,:,:,:,self.dir_internal].T.copy(order="F") # FIXME: use reference implementation # NOTE: dimensions are nvars x nx x ny x nz - self.flux_derivatives = np.zeros((self.nvars, self.nx, self.ny, self.nz)) + self.flux_derivatives = np.zeros((self.nvars, self.nx, self.ny, + self.nz), order="F") def swap_array_rows(self, arr, d): p = self.permutation(d) -- GitLab From 332426dd55aaa0aa169b75484b00ccd6d08b94ee Mon Sep 17 00:00:00 2001 From: "Timothy A. Smith" Date: Mon, 9 Sep 2019 22:58:47 -0500 Subject: [PATCH 14/17] refactor WENO interface and get rid of generalized flux computations --- WENO.F90 | 205 +++++++++---------------------------------------------- 1 file changed, 31 insertions(+), 174 deletions(-) diff --git a/WENO.F90 b/WENO.F90 index 8013b2c..d798cdb 100644 --- a/WENO.F90 +++ b/WENO.F90 @@ -18,56 +18,32 @@ ! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN ! THE SOFTWARE. -subroutine compute_flux_derivatives(nvars, ndim, nx, ny, nz, & +subroutine compute_flux_derivatives(nvars, ndim, nx, ny, nz, d, & states, fluxes, metrics, metric_jacobians, flux_derivatives) implicit none - integer, intent(in) :: nvars, ndim, nx, ny, nz + integer, intent(in) :: nvars, ndim, nx, ny, nz, d real*8, intent(in) :: states(nvars, -2:nx+3, -2:ny+3, -2:nz+3) - real*8, intent(in) :: fluxes(nvars, ndim, -2:nx+3, -2:ny+3, -2:nz+3) + real*8, intent(in) :: fluxes(nvars, -2:nx+3, -2:ny+3, -2:nz+3) real*8, intent(in) :: metrics(ndim, ndim, -2:nx+3, -2:ny+3, -2:nz+3) real*8, intent(in) :: metric_jacobians(-2:nx+3, -2:ny+3, -2:nz+3) - real*8, intent(out) :: flux_derivatives(nvars, ndim, -2:nx+3, -2:ny+3, -2:nz+3) + real*8, intent(out) :: flux_derivatives(nvars, nx, ny, nz) - real*8 flux_derivatives_generalized(nvars, ndim, -2:nx+3, -2:ny+3, -2:nz+3) real*8 weno_flux_tmp(nvars, 0:nx+1, 0:ny+1, 0:nz+1) - real*8 generalized_fluxes(nvars, ndim, -2:nx+3, -2:ny+3, -2:nz+3) + real*8 metrics_frozen(ndim, ndim) + real*8 combined_frozen_metrics(ndim) real*8 generalized_states_frozen(nvars, -2:3) - real*8 generalized_fluxes_frozen(nvars, ndim, -2:3) - real*8 metric_solve_tmp(ndim) + real*8 generalized_fluxes_frozen(nvars, -2:3) + real*8 R(nvars, nvars), R_inv(nvars, nvars), lambda_roe(nvars) real*8 lambda_pointwise(nvars, -2:3) real*8 wavespeeds(nvars) - integer i, j, k - real*8 delta_xi, delta_eta, delta_zeta + real*8 characteristic_fluxes_pos(nvars, -2:3) real*8 characteristic_fluxes_neg(nvars, -2:3) - real*8 metrics_frozen(ndim, ndim) - real*8 combined_frozen_metrics(ndim) - integer v, d - - ! grid spacing in generalized coordinates - delta_xi = 1.0d0 - delta_eta = 1.0d0 - delta_zeta = 1.0d0 - !$loopy begin tagged: to_generalized - !call convert_to_generalized(nvars, ndim, nx, ny, nz, & - ! fluxes, metrics, metric_jacobians, generalized_fluxes) - - ! FIXME: Manually inlined for now - do k=-2,nz+3 - do j=-2,ny+3 - do i=-2,nx+3 - do v=1,nvars - call mult_mat_vec(ndim, ndim, 1.0d0/metric_jacobians(i,j,k), metrics(:,:,i,j,k), & - fluxes(v,:,i,j,k), generalized_fluxes(v,:,i,j,k)) - end do - end do - end do - end do - !$loopy end tagged: to_generalized + integer i, j, k, v ! for the next three loops, note carefully that the inner loop indices have a different range @@ -77,7 +53,7 @@ subroutine compute_flux_derivatives(nvars, ndim, nx, ny, nz, & do i=0,nx call convert_to_generalized_frozen(nvars, ndim, & states(:, i-2:i+3, j, k), & - fluxes(:, :, i-2:i+3, j, k), & + fluxes(:, i-2:i+3, j, k), & metrics(:, :, i:i+1, j, k), & metric_jacobians(i:i+1, j, k), & metrics_frozen, & @@ -93,14 +69,14 @@ subroutine compute_flux_derivatives(nvars, ndim, nx, ny, nz, & call split_characteristic_fluxes(nvars, & generalized_states_frozen, & - generalized_fluxes_frozen(:, 1, :), & + generalized_fluxes_frozen, & R_inv, & wavespeeds, & characteristic_fluxes_pos, & characteristic_fluxes_neg) call weno_flux(nvars, & - generalized_fluxes(:, 1, i-2:i+3, j, k), & + fluxes(:, i-2:i+3, j, k), & characteristic_fluxes_pos, characteristic_fluxes_neg, & combined_frozen_metrics(1), R, weno_flux_tmp(:, i, j, k)) end do @@ -113,8 +89,8 @@ subroutine compute_flux_derivatives(nvars, ndim, nx, ny, nz, & do j=1,ny do i=1,nx do v=1,nvars - flux_derivatives_generalized(v, 1, i, j, k) = & - (weno_flux_tmp(v, i, j, k) - weno_flux_tmp(v, i-1, j, k)) / delta_xi + flux_derivatives(v, i, j, k) = & + (weno_flux_tmp(v, i, j, k) - weno_flux_tmp(v, i-1, j, k)) end do end do end do @@ -127,7 +103,7 @@ subroutine compute_flux_derivatives(nvars, ndim, nx, ny, nz, & do j=0,ny call convert_to_generalized_frozen(nvars, ndim, & states(:, i, j-2:j+3, k), & - fluxes(:, :, i, j-2:j+3, k), & + fluxes(:, i, j-2:j+3, k), & metrics(:, :, i, j:j+1, k), & metric_jacobians(i, j:j+1, k), & metrics_frozen, & @@ -143,14 +119,14 @@ subroutine compute_flux_derivatives(nvars, ndim, nx, ny, nz, & call split_characteristic_fluxes(nvars, & generalized_states_frozen, & - generalized_fluxes_frozen(:, 2, :), & + generalized_fluxes_frozen, & R_inv, & wavespeeds, & characteristic_fluxes_pos, & characteristic_fluxes_neg) call weno_flux(nvars, & - generalized_fluxes(:, 2, i, j-2:j+3, k), & + fluxes(:, i, j-2:j+3, k), & characteristic_fluxes_pos, characteristic_fluxes_neg, & combined_frozen_metrics(2), R, weno_flux_tmp(:, i, j, k)) @@ -164,8 +140,8 @@ subroutine compute_flux_derivatives(nvars, ndim, nx, ny, nz, & do j=1,ny do i=1,nx do v=1,nvars - flux_derivatives_generalized(v, 2, i, j, k) = & - (weno_flux_tmp(v, i, j, k) - weno_flux_tmp(v, i, j-1, k)) / delta_eta + flux_derivatives(v, i, j, k) = & + (weno_flux_tmp(v, i, j, k) - weno_flux_tmp(v, i, j-1, k)) end do end do end do @@ -178,7 +154,7 @@ subroutine compute_flux_derivatives(nvars, ndim, nx, ny, nz, & do k=0,nz call convert_to_generalized_frozen(nvars, ndim, & states(:, i, j, k-2:k+3), & - fluxes(:, :, i, j, k-2:k+3), & + fluxes(:, i, j, k-2:k+3), & metrics(:, :, i, j, k:k+1), & metric_jacobians(i, j, k:k+1), & metrics_frozen, & @@ -194,14 +170,14 @@ subroutine compute_flux_derivatives(nvars, ndim, nx, ny, nz, & call split_characteristic_fluxes(nvars, & generalized_states_frozen, & - generalized_fluxes_frozen(:, 3, :), & + generalized_fluxes_frozen, & R_inv, & wavespeeds, & characteristic_fluxes_pos, & characteristic_fluxes_neg) call weno_flux(nvars, & - generalized_fluxes(:, 3, i, j, k-2:k+3), & + fluxes(:, i, j, k-2:k+3), & characteristic_fluxes_pos, characteristic_fluxes_neg, & combined_frozen_metrics(3), R, weno_flux_tmp(:, i, j, k)) end do @@ -214,35 +190,13 @@ subroutine compute_flux_derivatives(nvars, ndim, nx, ny, nz, & do j=1,ny do i=1,nx do v=1,nvars - flux_derivatives_generalized(v, 3, i, j, k) = & - (weno_flux_tmp(v, i, j, k) - weno_flux_tmp(v, i, j, k-1)) / delta_zeta + flux_derivatives(v, i, j, k) = & + (weno_flux_tmp(v, i, j, k) - weno_flux_tmp(v, i, j, k-1)) end do end do end do end do !$loopy end tagged: flux_z_diff - - !$loopy begin tagged: from_generalized - !call convert_from_generalized(nvars, ndim, nx, ny, nz, & - ! flux_derivatives_generalized, & - ! metrics, & - ! metric_jacobians, & - ! flux_derivatives) - ! FIXME: Manually inlined for now - do k=-2,nz+3 - do j=-2,ny+3 - do i=-2,nx+3 - do v=1,nvars - call solve3x3(metrics(:,:,i,j,k), flux_derivatives_generalized(v,:,i,j,k), & - metric_solve_tmp) - do d=1,ndim - flux_derivatives(v,d,i,j,k) = metric_solve_tmp(d)*metric_jacobians(i,j,k) - end do - end do - end do - end do - end do - !$loopy end tagged: from_generalized end subroutine subroutine pointwise_eigenvalues(nvars, d, states, lambda_pointwise) @@ -472,13 +426,13 @@ subroutine convert_to_generalized_frozen( & generalized_fluxes_frozen) integer, intent(in) :: nvars, ndim real*8, intent(in) :: states(nvars, -2:3) - real*8, intent(in) :: fluxes(nvars, ndim, -2:3) + real*8, intent(in) :: fluxes(nvars, -2:3) real*8, intent(in) :: metrics(ndim, ndim, 2) real*8, intent(in) :: metric_jacobians(2) real*8, intent(out) :: metrics_frozen(ndim, ndim) real*8, intent(out) :: combined_frozen_metrics(ndim) real*8, intent(out) :: generalized_states_frozen(nvars, -2:3) - real*8, intent(out) :: generalized_fluxes_frozen(nvars, ndim, -2:3) + real*8, intent(out) :: generalized_fluxes_frozen(nvars, -2:3) real*8 jacobian_frozen @@ -510,107 +464,14 @@ subroutine convert_to_generalized_frozen( & do k=-2,3 do v=1,nvars - call mult_mat_vec(ndim, ndim, 1.0d0, metrics_frozen, & - fluxes(v,:,k), generalized_fluxes_frozen(v,:,k)) - end do - end do -end subroutine - -#if 0 -subroutine convert_to_generalized(nvars, ndim, nx, ny, nz, & - fluxes, & - metrics, & - metric_jacobians, & - generalized_fluxes) - implicit none - - integer, intent(in) :: nvars, ndim, nx, ny, nz - real*8, intent(in) :: fluxes(nvars, ndim, -2:nx+3, -2:ny+3, -2:nz+3) - real*8, intent(in) :: metrics(ndim, ndim, -2:nx+3, -2:ny+3, -2:nz+3) - real*8, intent(in) :: metric_jacobians(-2:nx+3, -2:ny+3, -2:nz+3) - real*8, intent(out) :: generalized_fluxes(nvars, ndim, -2:nx+3, -2:ny+3, -2:nz+3) - - integer i, j, k, v - - do k=-2,nz+3 - do j=-2,ny+3 - do i=-2,nx+3 - do v=1,nvars - call mult_mat_vec(ndim, ndim, 1.0d0/metric_jacobians(i,j,k), metrics(:,:,i,j,k), & - fluxes(v,:,i,j,k), generalized_fluxes(v,:,i,j,k)) - end do - end do + ! FIXME: this is a bug + ! ... we actually need to recompute the fluxes using frozen metrics + ! ... but this works when the metric matrix is identity + generalized_fluxes_frozen(v,k) = fluxes(v,k) end do end do end subroutine -subroutine convert_from_generalized(nvars, ndim, nx, ny, nz, & - flux_derivatives_generalized, & - metrics, & - metric_jacobians, & - flux_derivatives) - implicit none - - integer, intent(in) :: nvars, ndim, nx, ny, nz - real*8, intent(in) :: flux_derivatives_generalized(nvars, ndim, -2:nx+3, -2:ny+3, -2:nz+3) - real*8, intent(in) :: metrics(ndim, ndim, -2:nx+3, -2:ny+3, -2:nz+3) - real*8, intent(in) :: metric_jacobians(-2:nx+3, -2:ny+3, -2:nz+3) - real*8, intent(out) :: flux_derivatives(nvars, ndim, -2:nx+3, -2:ny+3, -2:nz+3) - - integer i, j, k, v - integer d - - do k=-2,nz+3 - do j=-2,ny+3 - do i=-2,nx+3 - do v=1,nvars - call solve3x3(metrics(:,:,i,j,k), flux_derivatives_generalized(v,:,i,j,k), & - flux_derivatives(v,:,i,j,k)) - do d=1,ndim - flux_derivatives(v,d,i,j,k) = flux_derivatives(v,d,i,j,k)*metric_jacobians(i,j,k) - end do - end do - end do - end do - end do -end subroutine -#endif - -subroutine solve3x3(a, b, x) - implicit none - - real*8, intent(in) :: a(3,3), b(3) - real*8, intent(out) :: x(3) - - real*8 temp(3,3) - real*8 det_a(1), det_temp(1) - integer k - integer i,j - - call determinant3x3(a, det_a) - do k=1,3 - do j=1,3 - do i=1,3 - temp(i,j) = a(i,j) - temp(i,k) = b(k) - end do - end do - call determinant3x3(temp, det_temp) - x(k) = det_temp(1)/det_a(1) - end do -end subroutine - -subroutine determinant3x3(a, det) - implicit none - - real*8, intent(in) :: a(3,3) - real*8, intent(out) :: det(1) - - det(1) = a(1,1)*(a(2,2)*a(3,3) - a(2,3)*a(3,2)) & - + a(1,2)*(a(2,3)*a(3,1) - a(2,1)*a(3,3)) & - + a(1,3)*(a(2,1)*a(3,2) - a(2,2)*a(3,1)) -end subroutine - subroutine split_characteristic_fluxes(nvars, & generalized_states_frozen, & generalized_fluxes_frozen, & @@ -986,10 +847,6 @@ end subroutine ! ! knl = lp.assume(knl, "nx > 0 and ny > 0 and nz > 0") ! -! knl = lp.set_temporary_scope(knl, "flux_derivatives_generalized", -! lp.AddressSpace.GLOBAL) -! knl = lp.set_temporary_scope(knl, "generalized_fluxes", -! lp.AddressSpace.GLOBAL) ! knl = lp.set_temporary_scope(knl, "weno_flux_tmp", ! lp.AddressSpace.GLOBAL) ! -- GitLab From 1262623c470588894a31dd936869aa0fd947c06f Mon Sep 17 00:00:00 2001 From: "Timothy A. Smith" Date: Mon, 9 Sep 2019 23:17:34 -0500 Subject: [PATCH 15/17] cleanup in flux derivative test, using flat tests now --- test/data_for_test.py | 98 +++++++++++++++++++++++-------------------- 1 file changed, 52 insertions(+), 46 deletions(-) diff --git a/test/data_for_test.py b/test/data_for_test.py index f3b4578..84033f7 100644 --- a/test/data_for_test.py +++ b/test/data_for_test.py @@ -132,11 +132,12 @@ class FluxDataVector: # FIXME: these should be generalized fluxes # FIXME: make a clear distinction between fluxes in physical and # generalized coordinates - # FIXME: fix bug in pointwise flux computation - self.fluxes = np.zeros((self.nvars, self.nxhalo, self.nyhalo, - self.nzhalo), order="F") - #self.fluxes = ref.pointwise_fluxes( - # self.states)[:,:,:,:,self.dir_internal].T.copy(order="F") + 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( + (self.nvars, self.nxhalo, self.nyhalo, self.nzhalo) + ).copy(order="F") # FIXME: use reference implementation # NOTE: dimensions are nvars x nx x ny x nz @@ -172,50 +173,57 @@ class FluxDataVector: single_data = {} -#single_data["Case flat:x"] = FluxDataSingle( -# states_str="1 1 1 1 5.5,1 1 1 1 5.5", direction="x") -#single_data["Case flat:y"] = FluxDataSingle( -# states_str="1 1 1 1 5.5,1 1 1 1 5.5", direction="y") -#single_data["Case flat:z"] = FluxDataSingle( -# states_str="1 1 1 1 5.5,1 1 1 1 5.5", direction="z") -# -#single_data["Case a:x"] = FluxDataSingle( -# states_str="2 4 4 4 20,1 1 1 1 5.5", direction="x") -#single_data["Case a:y"] = FluxDataSingle( -# states_str="2 4 4 4 20,1 1 1 1 5.5", direction="y") -#single_data["Case a:z"] = FluxDataSingle( -# states_str="2 4 4 4 20,1 1 1 1 5.5", direction="z") -# -#single_data["Case b:x"] = FluxDataSingle( -# states_str="1 -1 -1 -1 5.5,2 -4 -4 -4 20", direction="x") -#single_data["Case b:y"] = FluxDataSingle( -# states_str="1 -1 -1 -1 5.5,2 -4 -4 -4 20", direction="y") -#single_data["Case b:z"] = FluxDataSingle( -# states_str="1 -1 -1 -1 5.5,2 -4 -4 -4 20", direction="z") -# -#single_data["Case c:x"] = FluxDataSingle( -# states_str="2 4 8 12 64,1 1 2 3 11", direction="x") -#single_data["Case c:y"] = FluxDataSingle( -# states_str="2 8 12 4 64,1 2 3 1 11", direction="y") -#single_data["Case c:z"] = FluxDataSingle( -# states_str="2 12 4 8 64,1 3 1 2 11", direction="z") -# -#single_data["Case d:x"] = FluxDataSingle( -# states_str="1 -1 -2 -3 11,2 -4 -8 -12 64", direction="x") -#single_data["Case d:y"] = FluxDataSingle( -# states_str="1 -2 -3 -1 11,2 -8 -12 -4 64", direction="y") -#single_data["Case d:z"] = FluxDataSingle( -# states_str="1 -3 -1 -2 11,2 -12 -4 -8 64", direction="z") +single_data["Case flat:x"] = FluxDataSingle( + states_str="1 1 1 1 5.5,1 1 1 1 5.5", direction="x") +single_data["Case flat:y"] = FluxDataSingle( + states_str="1 1 1 1 5.5,1 1 1 1 5.5", direction="y") +single_data["Case flat:z"] = FluxDataSingle( + states_str="1 1 1 1 5.5,1 1 1 1 5.5", direction="z") + +single_data["Case a:x"] = FluxDataSingle( + states_str="2 4 4 4 20,1 1 1 1 5.5", direction="x") +single_data["Case a:y"] = FluxDataSingle( + states_str="2 4 4 4 20,1 1 1 1 5.5", direction="y") +single_data["Case a:z"] = FluxDataSingle( + states_str="2 4 4 4 20,1 1 1 1 5.5", direction="z") + +single_data["Case b:x"] = FluxDataSingle( + states_str="1 -1 -1 -1 5.5,2 -4 -4 -4 20", direction="x") +single_data["Case b:y"] = FluxDataSingle( + states_str="1 -1 -1 -1 5.5,2 -4 -4 -4 20", direction="y") +single_data["Case b:z"] = FluxDataSingle( + states_str="1 -1 -1 -1 5.5,2 -4 -4 -4 20", direction="z") + +single_data["Case c:x"] = FluxDataSingle( + states_str="2 4 8 12 64,1 1 2 3 11", direction="x") +single_data["Case c:y"] = FluxDataSingle( + states_str="2 8 12 4 64,1 2 3 1 11", direction="y") +single_data["Case c:z"] = FluxDataSingle( + states_str="2 12 4 8 64,1 3 1 2 11", direction="z") + +single_data["Case d:x"] = FluxDataSingle( + states_str="1 -1 -2 -3 11,2 -4 -8 -12 64", direction="x") +single_data["Case d:y"] = FluxDataSingle( + states_str="1 -2 -3 -1 11,2 -8 -12 -4 64", direction="y") +single_data["Case d:z"] = FluxDataSingle( + states_str="1 -3 -1 -2 11,2 -12 -4 -8 64", direction="z") vector_data = {} -# {{{ Input data: Case (a) - -vector_data["Case zero"] = FluxDataVector( +vector_data["Case flat:x"] = FluxDataVector( nx=6, ny=2, nz=2, - states_str="0 0 0 0 0,0 0 0 0 0", + states_str="1 1 1 1 5.5,1 1 1 1 5.5", direction="x") +vector_data["Case flat:y"] = FluxDataVector( + nx=2, ny=6, nz=2, + states_str="1 1 1 1 5.5,1 1 1 1 5.5", + direction="y") +vector_data["Case flat:z"] = FluxDataVector( + nx=2, ny=2, nz=6, + states_str="1 1 1 1 5.5,1 1 1 1 5.5", + direction="z") + vector_data["Case a:x"] = FluxDataVector( nx=6, ny=2, nz=2, states_str="2 4 4 4 20,1 1 1 1 5.5", @@ -229,8 +237,6 @@ vector_data["Case a:z"] = FluxDataVector( states_str="2 4 4 4 20,1 1 1 1 5.5", direction="z") -# }}} - @pytest.fixture(scope="session", params=[ "Case flat:x", "Case flat:y", "Case flat:z", @@ -243,7 +249,7 @@ def flux_test_data_fixture(request): @pytest.fixture(scope="session", params=[ - "Case zero"]) + "Case flat:x", "Case flat:y", "Case flat:z"]) def cfd_test_data_fixture(request): return vector_data[request.param] -- GitLab From 84dfb15bdf8744d7182d6659f78c952dd8ce64e7 Mon Sep 17 00:00:00 2001 From: "Timothy A. Smith" Date: Mon, 9 Sep 2019 23:33:01 -0500 Subject: [PATCH 16/17] added dimension parameter to random variable compute_flux_derivatives tests --- test/test_flux_derivatives.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_flux_derivatives.py b/test/test_flux_derivatives.py index f2dc9d2..b599be0 100644 --- a/test/test_flux_derivatives.py +++ b/test/test_flux_derivatives.py @@ -53,7 +53,7 @@ def test_compute_flux_derivatives(ctx_factory): prg = prg.copy(target=lp.PyOpenCLTarget(queue.device)) lp.auto_test_vs_ref(prg, ctx_factory(), warmup_rounds=0, - parameters=dict(ndim=3, nvars=5, nx=16, ny=16, nz=16)) + parameters=dict(ndim=3, nvars=5, nx=16, ny=16, nz=16, d=0)) @pytest.mark.slow @@ -69,7 +69,7 @@ def test_compute_flux_derivatives_gpu(ctx_factory, write_code=False): u.write_target_device_code(prg) lp.auto_test_vs_ref(prg, ctx_factory(), warmup_rounds=0, - parameters=dict(ndim=3, nvars=5, nx=16, ny=16, nz=16)) + parameters=dict(ndim=3, nvars=5, nx=16, ny=16, nz=16, d=0)) # This lets you run 'python test.py test_case(cl._csc)' without pytest. -- GitLab From 81843dbd09fbfe2b4a869caa57a046840b49cb63 Mon Sep 17 00:00:00 2001 From: "Timothy A. Smith" Date: Mon, 9 Sep 2019 23:45:46 -0500 Subject: [PATCH 17/17] fix transforms that were broken by changes --- test/utilities.py | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/test/utilities.py b/test/utilities.py index b02179c..bd78fb4 100644 --- a/test/utilities.py +++ b/test/utilities.py @@ -139,30 +139,20 @@ def get_weno_program_with_root_kernel(knl): def transform_weno_for_gpu(prg, print_kernel=False): knl = prg["compute_flux_derivatives"] - for suffix in ["", "_1", "_2", "_3", "_4", "_5", "_6", "_7"]: + for suffix in ["", "_1", "_2", "_3", "_4", "_5"]: knl = lp.split_iname(knl, "i"+suffix, 16, outer_tag="g.0", inner_tag="l.0") knl = lp.split_iname(knl, "j"+suffix, 16, outer_tag="g.1", inner_tag="l.1") - for var_name in ["delta_xi", "delta_eta", "delta_zeta"]: - knl = lp.assignment_to_subst(knl, var_name) - - knl = lp.add_barrier(knl, "tag:to_generalized", "tag:flux_x_compute") knl = lp.add_barrier(knl, "tag:flux_x_compute", "tag:flux_x_diff") knl = lp.add_barrier(knl, "tag:flux_x_diff", "tag:flux_y_compute") knl = lp.add_barrier(knl, "tag:flux_y_compute", "tag:flux_y_diff") knl = lp.add_barrier(knl, "tag:flux_y_diff", "tag:flux_z_compute") knl = lp.add_barrier(knl, "tag:flux_z_compute", "tag:flux_z_diff") - knl = lp.add_barrier(knl, "tag:flux_z_diff", "tag:from_generalized") prg = prg.with_kernel(knl) - # FIXME: These should work, but don't - # FIXME: Undo the hand-inlining in WENO.F90 - #prg = lp.inline_callable_kernel(prg, "convert_to_generalized") - #prg = lp.inline_callable_kernel(prg, "convert_from_generalized") - if print_kernel: print(prg["convert_to_generalized_frozen"]) 1/0 -- GitLab