From 978b159457a8f031995406fb0b038024529426ba Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?K=C3=BChle=2C=20Laura=20Christine=20=28lakue103=29?=
 <laura.kuehle@uni-duesseldorf.de>
Date: Tue, 15 Mar 2022 14:18:13 +0100
Subject: [PATCH] Improved 'calculate_cell_average()'.

---
 DG_Approximation.py       |  6 +++---
 Troubled_Cell_Detector.py |  4 ++--
 projection_utils.py       | 35 ++++++++++++++++-------------------
 3 files changed, 21 insertions(+), 24 deletions(-)

diff --git a/DG_Approximation.py b/DG_Approximation.py
index 4eab4c8..21b67e1 100644
--- a/DG_Approximation.py
+++ b/DG_Approximation.py
@@ -11,7 +11,7 @@ Urgent:
 TODO: Extract do_initial_projection() from DGScheme -> Done
 TODO: Move inverse mass matrix to basis -> Done
 TODO: Extract calculate_cell_average() from TCD -> Done
-TODO: Improve calculate_cell_average()
+TODO: Improve calculate_cell_average() -> Done
 TODO: Extract calculate_[...]_solution() from Plotting
 TODO: Extract plotting from TCD completely
     (maybe give indicator which plots are required instead?)
@@ -334,8 +334,8 @@ class DGScheme:
 
         return calculate_cell_average(
             projection=projection[:, 1:-1], stencil_length=stencil_length,
-            polynomial_degree=self._polynomial_degree, basis=self._basis,
-            add_reconstructions=add_reconstructions)
+            polynomial_degree=self._polynomial_degree if add_reconstructions
+            else -1, basis=self._basis)
 
 
 def do_initial_projection(initial_condition, basis, quadrature,
diff --git a/Troubled_Cell_Detector.py b/Troubled_Cell_Detector.py
index 325d84d..2d8aaeb 100644
--- a/Troubled_Cell_Detector.py
+++ b/Troubled_Cell_Detector.py
@@ -292,8 +292,8 @@ class ArtificialNeuralNetwork(TroubledCellDetector):
             projection=projection[
                        :, cell-num_ghost_cells:cell+num_ghost_cells+1],
             stencil_length=self._stencil_len, basis=self._basis,
-            polynomial_degree=self._polynomial_degree,
-            add_reconstructions=self._add_reconstructions)
+            polynomial_degree=self._polynomial_degree if
+            self._add_reconstructions else -1)
                 for cell in range(num_ghost_cells,
                                   len(projection[0])-num_ghost_cells)]))
 
diff --git a/projection_utils.py b/projection_utils.py
index c4bdec2..e860b10 100644
--- a/projection_utils.py
+++ b/projection_utils.py
@@ -9,8 +9,8 @@ from Plotting import calculate_approximate_solution
 
 
 def calculate_cell_average(projection, basis, stencil_length,
-                           polynomial_degree, add_reconstructions=True):
-    """Calculates cell averages for a given projection.
+                           polynomial_degree=-1):
+    """Calculate cell averages for a given projection.
 
     Calculate the cell averages of all cells in a projection.
     If desired, reconstructions are calculated for the middle cell
@@ -24,11 +24,9 @@ def calculate_cell_average(projection, basis, stencil_length,
         Basis for calculation.
     stencil_length : int
         Size of data array.
-    polynomial_degree : int
-        Polynomial degree.
-    add_reconstructions: bool, optional
-        Flag whether reconstructions of the middle cell are included.
-        Default: True.
+    polynomial_degree : int, optional
+        Polynomial degree for reconstructions of the middle cell. If -1 no
+        reconstructions will be included. Default: -1.
 
     Returns
     -------
@@ -37,21 +35,20 @@ def calculate_cell_average(projection, basis, stencil_length,
         projection.
 
     """
+    basis_vector = basis.get_basis_vector()
     cell_averages = calculate_approximate_solution(
-        projection, [0], 0, basis.get_basis_vector())
+        projection, np.array([0]), 0, basis_vector)
 
-    if add_reconstructions:
+    if polynomial_degree != -1:
         left_reconstructions = calculate_approximate_solution(
-            projection, [-1], polynomial_degree,
-            basis.get_basis_vector())
+            projection, np.array([-1]), polynomial_degree, basis_vector)
         right_reconstructions = calculate_approximate_solution(
-            projection, [1], polynomial_degree,
-            basis.get_basis_vector())
+            projection, np.array([1]), polynomial_degree, basis_vector)
         middle_idx = stencil_length // 2
-        return np.array(
-            list(map(np.float64, zip(cell_averages[:, :middle_idx],
-                                     left_reconstructions[:, middle_idx],
-                                     cell_averages[:, middle_idx],
-                                     right_reconstructions[:, middle_idx],
-                                     cell_averages[:, middle_idx+1:]))))
+        return np.array(list(map(
+            np.float64, zip(cell_averages[:, :middle_idx],
+                            left_reconstructions[:, middle_idx],
+                            cell_averages[:, middle_idx],
+                            right_reconstructions[:, middle_idx],
+                            cell_averages[:, middle_idx+1:]))))
     return np.array(list(map(np.float64, cell_averages)))
-- 
GitLab