From a54d86f4ae5ede36e1e6fa08fc10326bfa7f2634 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: Wed, 30 Jun 2021 17:47:26 +0200
Subject: [PATCH] Fixed cell averages and reconstructions to create data with
 an x-point stencil.

---
 ANN_Data_Generator.py     |  9 ++++-----
 DG_Approximation.py       |  5 ++---
 Troubled_Cell_Detector.py | 19 ++++++++++++++-----
 3 files changed, 20 insertions(+), 13 deletions(-)

diff --git a/ANN_Data_Generator.py b/ANN_Data_Generator.py
index 2daf59f..c6a22bb 100644
--- a/ANN_Data_Generator.py
+++ b/ANN_Data_Generator.py
@@ -112,7 +112,7 @@ class TrainingDataGenerator(object):
 
             # Calculating Cell centers for a given 1D domain with n elements, and
             # Calculating Corresponding Legendre Basis Coefficients for given polynomial_degree
-            num_grid_cells = 3
+            num_grid_cells = self._stencil_length  # former: 3
             # Create stencil and basis_coefficients for smooth_function mapped onto stencil
             interval, centers, h = self._build_stencil()
             centers = [center[0] for center in centers]
@@ -126,9 +126,10 @@ class TrainingDataGenerator(object):
                                                   quadrature_config={'num_eval_points': polynomial_degree+1})
 
             if initial_condition.is_smooth():
-                input_data[i] = dg_scheme.build_training_data(0, initial_condition)
+                input_data[i] = dg_scheme.build_training_data(0, self._stencil_length, initial_condition)
             else:
-                input_data[i] = dg_scheme.build_training_data(centers[1], initial_condition)
+                input_data[i] = dg_scheme.build_training_data(centers[self._stencil_length//2], self._stencil_length,
+                                                              initial_condition)
 
             # Update Function ID
             if (i % num_function_samples == num_function_samples - 1) and (function_id != len(initial_conditions)-1):
@@ -161,8 +162,6 @@ class TrainingDataGenerator(object):
 
         # Pick a Random point between the left and right bound
         point = np.random.random(1) * (self._right_bound-self._left_bound) + self._left_bound
-        # if int(10 * point) % 2 == 1:
-        #     point = -point
 
         # Ensure Bounds of x-point stencil are within the left and right bound
         while point - self._stencil_length/2 * grid_spacing < self._left_bound\
diff --git a/DG_Approximation.py b/DG_Approximation.py
index 11fadba..aa9408a 100644
--- a/DG_Approximation.py
+++ b/DG_Approximation.py
@@ -109,7 +109,6 @@ class DGScheme(object):
 
             # Update projection
             projection, troubled_cells = self._update_scheme.step(projection, cfl_number)
-
             iteration += 1
 
             if (iteration % self._history_threshold) == 0:
@@ -185,9 +184,9 @@ class DGScheme(object):
         print(np.array(output_matrix).shape)
         return np.transpose(np.array(output_matrix))
 
-    def build_training_data(self, adjustment, initial_condition=None):
+    def build_training_data(self, adjustment, stencil_length, initial_condition=None):
         if initial_condition is None:
             initial_condition = self._init_cond
         projection = self._do_initial_projection(initial_condition, adjustment)
 
-        return self._detector.calculate_cell_average_and_reconstructions(projection[:, 1:-1])
+        return self._detector.calculate_cell_average_and_reconstructions(projection[:, 1:-1], stencil_length)
diff --git a/Troubled_Cell_Detector.py b/Troubled_Cell_Detector.py
index ebc71d2..5e89b9e 100644
--- a/Troubled_Cell_Detector.py
+++ b/Troubled_Cell_Detector.py
@@ -2,8 +2,9 @@
 """
 @author: Laura C. Kühle, Soraya Terrab (sorayaterrab)
 
-TODO: Fix cell averages and reconstructions to create data with an x-point stencil
+TODO: Fix cell averages and reconstructions to create data with an x-point stencil -> Done
 TODO: Add comments to get_cells() for ArtificialNeuralNetwork -> Done
+TODO:
 
 """
 import os
@@ -55,12 +56,20 @@ class TroubledCellDetector(object):
     def get_cells(self, projection):
         pass
 
-    def calculate_cell_average_and_reconstructions(self, projection):
+    def calculate_cell_average_and_reconstructions(self, projection, stencil_length):
+        """
+        Calculate the cell averages of all cells in a projection. Reconstructions are only calculated for the middle
+        cell and added left and right to it, respectively.
+
+        Here come some parameter.
+        """
         cell_averages = self._calculate_approximate_solution(projection, [0], 0)
         left_reconstructions = self._calculate_approximate_solution(projection, [-1], self._polynomial_degree)
         right_reconstructions = self._calculate_approximate_solution(projection, [1], self._polynomial_degree)
-        return np.array(list(map(np.float64, zip(cell_averages[:, 0], left_reconstructions[:, 1], cell_averages[:, 1],
-                                 right_reconstructions[:, 1], cell_averages[:, 2]))))
+        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:]))))
 
     def plot_results(self, projection, troubled_cell_history, time_history):
         self._plot_shock_tube(troubled_cell_history, time_history)
@@ -219,7 +228,7 @@ class ArtificialNeuralNetwork(TroubledCellDetector):
 
         # Calculate input data depending on stencil length
         input_data = torch.from_numpy(np.vstack([self.calculate_cell_average_and_reconstructions(
-            projection[:, cell-num_ghost_cells:cell+num_ghost_cells+1])
+            projection[:, cell-num_ghost_cells:cell+num_ghost_cells+1], self._stencil_len)
             for cell in range(num_ghost_cells, len(projection[0])-num_ghost_cells)]))
 
         # Evaluate troubled cell probabilities
-- 
GitLab