From b7152e5b5091d8b6b9a577abeec2ed53ea5e9a51 Mon Sep 17 00:00:00 2001
From: "Timothy A. Smith" <tasmith4@illinois.edu>
Date: Tue, 6 Aug 2019 21:29:29 -0500
Subject: [PATCH] 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