From c34e69cff0d936db8414a082eef435c6e2c22d40 Mon Sep 17 00:00:00 2001
From: lakue103 <laura.kuehle@uni-duesseldorf.de>
Date: Tue, 22 Sep 2020 20:08:32 +0200
Subject: [PATCH] Replaced transposing with vectorized access when feasible.

---
 DG_Approximation.py       |  7 ++++---
 Limiter.py                | 23 ++---------------------
 Troubled_Cell_Detector.py |  3 +--
 Update_Scheme.py          | 19 ++++++-------------
 4 files changed, 13 insertions(+), 39 deletions(-)

diff --git a/DG_Approximation.py b/DG_Approximation.py
index 4a1b763..d89a79b 100644
--- a/DG_Approximation.py
+++ b/DG_Approximation.py
@@ -14,7 +14,7 @@ TODO: Contemplate erasing inv_mass-matrix as it is identity -> Discuss
 TODO: Investigate why we need inverse mass matrix for initial projection, \
     but not A and B
 TODO: Investigate why there are no weights in approx calc
-TODO: Change transposing to vectorized access if feasible
+TODO: Change transposing to vectorized access if feasible -> Done
 TODO: Implement argument check for unpacking of all configs
 TODO: Contemplate verbose = show_plot?
 TODO: Change order of methods -> Done for Stability_Method
@@ -25,8 +25,9 @@ TODO: Write documentation for all methods
 TODO: Add a verbose option
 TODO: Check whether consistency is given/possible for each class instance
 
-TODO: Make projection local variable -> Done (not for Limiter and Troubled_Cell_Detector)
-TODO: Vector faster than Trans for longer processes, therefore replace ->
+TODO: Make projection local variable -> Done
+TODO: Vector faster than Trans for longer processes, therefore replace -> Done
+TODO: Make sure time is changed to current_time everywhere 
 
 """
 import numpy as np
diff --git a/Limiter.py b/Limiter.py
index 5733559..ba801aa 100644
--- a/Limiter.py
+++ b/Limiter.py
@@ -3,8 +3,6 @@
 @author: Laura C. Kühle
 
 """
-import numpy as np
-import timeit
 
 
 class Limiter(object):
@@ -55,27 +53,10 @@ class MinMod(Limiter):
         return adapted_projection
 
     def _set_cell_slope(self, projection):
-        tic = timeit.default_timer()
         slope = []
-        transposed_projection = np.transpose(projection)
-        for cell in range(len(transposed_projection)):
-            new_entry = sum(transposed_projection[cell][degree] * (degree+0.5)**0.5
-                            for degree in range(1, len(projection)))
+        for cell in range(len(projection[0])):
+            new_entry = sum(projection[degree][cell] * (degree+0.5)**0.5 for degree in range(1, len(projection)))
             slope.append(new_entry)
-        toc = timeit.default_timer()
-        # print('Trans:', toc-tic)
-
-        tic = timeit.default_timer()
-        slope1 = []
-        transposed_projection = projection
-        for cell in range(len(transposed_projection[0])):
-            new_entry = sum(transposed_projection[degree][cell] * (degree+0.5)**0.5
-                            for degree in range(1, len(projection)))
-            slope1.append(new_entry)
-        toc = timeit.default_timer()
-        # print('Vecto:', toc-tic)
-        #
-        # print(slope == slope1)
         self.cell_slope = slope[self.cell]
 
     def _determine_modification(self, projection):
diff --git a/Troubled_Cell_Detector.py b/Troubled_Cell_Detector.py
index cabaf3c..3ec9e0d 100644
--- a/Troubled_Cell_Detector.py
+++ b/Troubled_Cell_Detector.py
@@ -62,10 +62,9 @@ class WaveletDetector(TroubledCellDetector):
         return matrix
 
     def _calculate_wavelet_coeffs(self, projection):
-        transposed_vector = np.transpose(projection)
         output_matrix = []
         for i in range(int(len(projection[0])/2)):
-            new_entry = 0.5*(transposed_vector[2*i] @ self.M1 + transposed_vector[2*i+1] @ self.M2)
+            new_entry = 0.5*(projection[:, 2*i] @ self.M1 + projection[:, 2*i+1] @ self.M2)
             output_matrix.append(new_entry)
         return np.transpose(np.array(output_matrix))
 
diff --git a/Update_Scheme.py b/Update_Scheme.py
index f74a836..fb63815 100644
--- a/Update_Scheme.py
+++ b/Update_Scheme.py
@@ -13,6 +13,7 @@ TODO: Find better names for A, B, M1, and M2
 
 """
 import numpy as np
+import timeit
 
 
 class UpdateScheme(object):
@@ -105,17 +106,13 @@ class UpdateScheme(object):
 
         new_projection = self.current_projection.copy()
         for cell in self.troubled_cells:
-            np.transpose(new_projection)[cell] = self.limiter.apply(self.current_projection, cell)
+            new_projection[:,  cell] = self.limiter.apply(self.current_projection, cell)
 
         self.current_projection = new_projection
 
     def _enforce_boundary_condition(self):
-        transposed_projection = np.transpose(self.current_projection)
-
-        transposed_projection[0] = transposed_projection[self.num_grid_cells]
-        transposed_projection[self.num_grid_cells+1] = transposed_projection[1]
-
-        self.current_projection = np.transpose(transposed_projection)
+        self.current_projection[:, 0] = self.current_projection[:, self.num_grid_cells]
+        self.current_projection[:, self.num_grid_cells+1] = self.current_projection[:, 1]
 
 
 class SSPRK3(UpdateScheme):
@@ -142,16 +139,12 @@ class SSPRK3(UpdateScheme):
         self._enforce_boundary_condition()
 
     def _update_right_hand_side(self):
-        # Transpose projection for easier calculation
-        transposed_projection = np.transpose(self.current_projection)
-
         # Initialize vector and set first entry to accommodate for ghost cell
         right_hand_side = [0]
 
         for j in range(self.num_grid_cells):
-            right_hand_side.append(
-                2*(self.A @ transposed_projection[j+1]
-                   + self.B @ transposed_projection[j]))
+            right_hand_side.append(2*(self.A @ self.current_projection[:, j+1]
+                                      + self.B @ self.current_projection[:, j]))
 
         # Set ghost cells to respective value
         right_hand_side[0] = right_hand_side[self.num_grid_cells]
-- 
GitLab