Skip to content
Snippets Groups Projects
Commit 780a5355 authored by Laura Christine Kühle's avatar Laura Christine Kühle
Browse files

Changed order of methods.

parent 76522d3e
No related branches found
No related tags found
No related merge requests found
...@@ -45,6 +45,13 @@ class MinMod(Limiter): ...@@ -45,6 +45,13 @@ class MinMod(Limiter):
adapted_projection[i] = 0 adapted_projection[i] = 0
return adapted_projection return adapted_projection
def _determine_modification(self, projection, cell, cell_slope):
forward_slope = (projection[0][cell+1] - projection[0][cell]) * (0.5**0.5)
backward_slope = (projection[0][cell] - projection[0][cell-1]) * (0.5**0.5)
return (forward_slope <= 0) & (backward_slope <= 0) & (cell_slope <= 0) \
| (forward_slope >= 0) & (backward_slope >= 0) & (cell_slope >= 0)
@staticmethod @staticmethod
def _set_cell_slope(projection, cell): def _set_cell_slope(projection, cell):
slope = [] slope = []
...@@ -54,13 +61,6 @@ class MinMod(Limiter): ...@@ -54,13 +61,6 @@ class MinMod(Limiter):
slope.append(new_entry) slope.append(new_entry)
return slope[cell] return slope[cell]
def _determine_modification(self, projection, cell, cell_slope):
forward_slope = (projection[0][cell+1] - projection[0][cell]) * (0.5**0.5)
backward_slope = (projection[0][cell] - projection[0][cell-1]) * (0.5**0.5)
return (forward_slope <= 0) & (backward_slope <= 0) & (cell_slope <= 0) \
| (forward_slope >= 0) & (backward_slope >= 0) & (cell_slope >= 0)
class ModifiedMinMod(MinMod): class ModifiedMinMod(MinMod):
def _reset(self, config): def _reset(self, config):
......
...@@ -11,22 +11,22 @@ class Quadrature(object): ...@@ -11,22 +11,22 @@ class Quadrature(object):
self._reset(config) self._reset(config)
def _reset(self, config): def _reset(self, config):
self._num_eval_points = None
self._eval_points = None self._eval_points = None
self._weights = None self._weights = None
self._num_eval_points = None
def get_name(self): def get_name(self):
return self.__class__.__name__ return self.__class__.__name__
def get_num_points(self):
return self._num_eval_points
def get_eval_points(self): def get_eval_points(self):
return self._eval_points return self._eval_points
def get_weights(self): def get_weights(self):
return self._weights return self._weights
def get_num_points(self):
return self._num_eval_points
class Gauss(Quadrature): class Gauss(Quadrature):
def _reset(self, config): def _reset(self, config):
......
...@@ -51,6 +51,17 @@ class TroubledCellDetector(object): ...@@ -51,6 +51,17 @@ class TroubledCellDetector(object):
print("N =", self._num_grid_cells) print("N =", self._num_grid_cells)
print("maximum error =", max_error) print("maximum error =", max_error)
def _plot_shock_tube(self, troubled_cell_history, time_history):
plt.figure(6)
for pos in range(len(time_history)):
current_cells = troubled_cell_history[pos]
for cell in current_cells:
plt.plot(cell, time_history[pos], 'k.')
plt.xlim((0, self._num_grid_cells//2))
plt.xlabel('Cell')
plt.ylabel('Time')
plt.title('Shock Tubes')
def _plot_mesh(self, projection, color_exact, color_approx): def _plot_mesh(self, projection, color_exact, color_approx):
grid, exact = self._calculate_exact_solution(self._mesh[2:-2], self._cell_len) grid, exact = self._calculate_exact_solution(self._mesh[2:-2], self._cell_len)
approx = self._calculate_approximate_solution(projection[:, 1:-1]) approx = self._calculate_approximate_solution(projection[:, 1:-1])
...@@ -88,17 +99,6 @@ class TroubledCellDetector(object): ...@@ -88,17 +99,6 @@ class TroubledCellDetector(object):
plt.ylabel('u(x,t)-uh(x,t)') plt.ylabel('u(x,t)-uh(x,t)')
plt.title('Errors') plt.title('Errors')
def _plot_shock_tube(self, troubled_cell_history, time_history):
plt.figure(6)
for pos in range(len(time_history)):
current_cells = troubled_cell_history[pos]
for cell in current_cells:
plt.plot(cell, time_history[pos], 'k.')
plt.xlim((0, self._num_grid_cells//2))
plt.xlabel('Cell')
plt.ylabel('Time')
plt.title('Shock Tubes')
def _calculate_exact_solution(self, mesh, cell_len): def _calculate_exact_solution(self, mesh, cell_len):
grid = [] grid = []
exact = [] exact = []
...@@ -170,9 +170,6 @@ class WaveletDetector(TroubledCellDetector): ...@@ -170,9 +170,6 @@ class WaveletDetector(TroubledCellDetector):
multiwavelet_coeffs = self._calculate_wavelet_coeffs(projection[:, 1: -1]) multiwavelet_coeffs = self._calculate_wavelet_coeffs(projection[:, 1: -1])
return self._get_cells(multiwavelet_coeffs, projection) return self._get_cells(multiwavelet_coeffs, projection)
def _get_cells(self, multiwavelet_coeffs, projection):
return []
def _calculate_wavelet_coeffs(self, projection): def _calculate_wavelet_coeffs(self, projection):
output_matrix = [] output_matrix = []
for i in range(self._num_coarse_grid_cells): for i in range(self._num_coarse_grid_cells):
...@@ -180,37 +177,13 @@ class WaveletDetector(TroubledCellDetector): ...@@ -180,37 +177,13 @@ class WaveletDetector(TroubledCellDetector):
output_matrix.append(new_entry) output_matrix.append(new_entry)
return np.transpose(np.array(output_matrix)) return np.transpose(np.array(output_matrix))
def _get_cells(self, multiwavelet_coeffs, projection):
return []
def plot_results(self, projection, troubled_cell_history, time_history, color_exact, color_approx): def plot_results(self, projection, troubled_cell_history, time_history, color_exact, color_approx):
self._plot_details(projection) self._plot_details(projection)
super().plot_results(projection, troubled_cell_history, time_history, color_exact, color_approx) super().plot_results(projection, troubled_cell_history, time_history, color_exact, color_approx)
def _plot_mesh(self, projection, color_exact, color_approx):
grid, exact = self._calculate_exact_solution(self._mesh[2:-2], self._cell_len)
approx = self._calculate_approximate_solution(projection[:, 1:-1])
pointwise_error = np.abs(exact-approx)
max_error = np.max(pointwise_error)
self._plot_coarse_mesh(projection, color_exact, color_approx)
self._plot_solution_and_approx(grid, exact, approx, 'k-.', 'b-.')
plt.legend(['Exact (Coarse)', 'Approx (Coarse)', 'Exact (Fine)', 'Approx (Fine)'])
self._plot_semilog_error(grid, pointwise_error)
self._plot_error(grid, exact, approx)
return max_error
def _plot_coarse_mesh(self, projection, color_exact, color_approx):
coarse_cell_len = 2*self._cell_len
coarse_mesh = np.arange(self._left_bound - (0.5*coarse_cell_len), self._right_bound + (1.5*coarse_cell_len),
coarse_cell_len)
coarse_projection = self._calculate_coarse_projection(projection)
# Plot exact and approximate solutions for coarse mesh
grid, exact = self._calculate_exact_solution(coarse_mesh[1:-1], coarse_cell_len)
approx = self._calculate_approximate_solution(coarse_projection)
self._plot_solution_and_approx(grid, exact, approx, color_exact, color_approx)
def _plot_details(self, projection): def _plot_details(self, projection):
fine_mesh = self._mesh[2:-2] fine_mesh = self._mesh[2:-2]
...@@ -283,7 +256,8 @@ class WaveletDetector(TroubledCellDetector): ...@@ -283,7 +256,8 @@ class WaveletDetector(TroubledCellDetector):
# Calculate projection on coarse mesh # Calculate projection on coarse mesh
output_matrix = [] output_matrix = []
for i in range(self._num_coarse_grid_cells): for i in range(self._num_coarse_grid_cells):
new_entry = 0.5*(projection[:, 2*i] @ basis_matrix_left + projection[:, 2*i+1] @ basis_matrix_right) new_entry = 0.5 * (
projection[:, 2 * i] @ basis_matrix_left + projection[:, 2 * i + 1] @ basis_matrix_right)
output_matrix.append(new_entry) output_matrix.append(new_entry)
coarse_projection = np.transpose(np.array(output_matrix)) coarse_projection = np.transpose(np.array(output_matrix))
...@@ -300,6 +274,33 @@ class WaveletDetector(TroubledCellDetector): ...@@ -300,6 +274,33 @@ class WaveletDetector(TroubledCellDetector):
matrix.append(row) matrix.append(row)
return matrix return matrix
def _plot_mesh(self, projection, color_exact, color_approx):
grid, exact = self._calculate_exact_solution(self._mesh[2:-2], self._cell_len)
approx = self._calculate_approximate_solution(projection[:, 1:-1])
pointwise_error = np.abs(exact-approx)
max_error = np.max(pointwise_error)
self._plot_coarse_mesh(projection, color_exact, color_approx)
self._plot_solution_and_approx(grid, exact, approx, 'k-.', 'b-.')
plt.legend(['Exact (Coarse)', 'Approx (Coarse)', 'Exact (Fine)', 'Approx (Fine)'])
self._plot_semilog_error(grid, pointwise_error)
self._plot_error(grid, exact, approx)
return max_error
def _plot_coarse_mesh(self, projection, color_exact, color_approx):
coarse_cell_len = 2*self._cell_len
coarse_mesh = np.arange(self._left_bound - (0.5*coarse_cell_len), self._right_bound + (1.5*coarse_cell_len),
coarse_cell_len)
coarse_projection = self._calculate_coarse_projection(projection)
# Plot exact and approximate solutions for coarse mesh
grid, exact = self._calculate_exact_solution(coarse_mesh[1:-1], coarse_cell_len)
approx = self._calculate_approximate_solution(coarse_projection)
self._plot_solution_and_approx(grid, exact, approx, color_exact, color_approx)
class Boxplot(WaveletDetector): class Boxplot(WaveletDetector):
def _reset(self, config): def _reset(self, config):
......
...@@ -26,17 +26,6 @@ class UpdateScheme(object): ...@@ -26,17 +26,6 @@ class UpdateScheme(object):
self._reset() self._reset()
def get_name(self):
return self.__class__.__name__
def step(self, projection, cfl_number):
current_projection, troubled_cells = self._apply_stability_method(projection, cfl_number)
return current_projection, troubled_cells
def _apply_stability_method(self, projection, cfl_number):
return projection, []
def _reset(self): def _reset(self):
# Set matrix A # Set matrix A
matrix = [] matrix = []
...@@ -60,6 +49,17 @@ class UpdateScheme(object): ...@@ -60,6 +49,17 @@ class UpdateScheme(object):
matrix.append(new_row) matrix.append(new_row)
self._B = np.array(matrix) # former: inv_mass @ np.array(matrix) self._B = np.array(matrix) # former: inv_mass @ np.array(matrix)
def get_name(self):
return self.__class__.__name__
def step(self, projection, cfl_number):
current_projection, troubled_cells = self._apply_stability_method(projection, cfl_number)
return current_projection, troubled_cells
def _apply_stability_method(self, projection, cfl_number):
return projection, []
def _apply_limiter(self, current_projection): def _apply_limiter(self, current_projection):
troubled_cells = self._detector.get_cells(current_projection) troubled_cells = self._detector.get_cells(current_projection)
...@@ -94,6 +94,18 @@ class SSPRK3(UpdateScheme): ...@@ -94,6 +94,18 @@ class SSPRK3(UpdateScheme):
return current_projection, troubled_cells return current_projection, troubled_cells
def _apply_first_step(self, original_projection, cfl_number):
right_hand_side = self._update_right_hand_side(original_projection)
return original_projection + (cfl_number*right_hand_side)
def _apply_second_step(self, original_projection, current_projection, cfl_number):
right_hand_side = self._update_right_hand_side(current_projection)
return 1/4 * (3*original_projection + (current_projection + cfl_number*right_hand_side))
def _apply_third_step(self, original_projection, current_projection, cfl_number):
right_hand_side = self._update_right_hand_side(current_projection)
return 1/3 * (original_projection + 2*(current_projection + cfl_number*right_hand_side))
def _update_right_hand_side(self, current_projection): def _update_right_hand_side(self, current_projection):
# Initialize vector and set first entry to accommodate for ghost cell # Initialize vector and set first entry to accommodate for ghost cell
right_hand_side = [0] right_hand_side = [0]
...@@ -107,15 +119,3 @@ class SSPRK3(UpdateScheme): ...@@ -107,15 +119,3 @@ class SSPRK3(UpdateScheme):
right_hand_side.append(right_hand_side[1]) right_hand_side.append(right_hand_side[1])
return np.transpose(right_hand_side) return np.transpose(right_hand_side)
def _apply_first_step(self, original_projection, cfl_number):
right_hand_side = self._update_right_hand_side(original_projection)
return original_projection + (cfl_number*right_hand_side)
def _apply_second_step(self, original_projection, current_projection, cfl_number):
right_hand_side = self._update_right_hand_side(current_projection)
return 1/4 * (3*original_projection + (current_projection + cfl_number*right_hand_side))
def _apply_third_step(self, original_projection, current_projection, cfl_number):
right_hand_side = self._update_right_hand_side(current_projection)
return 1/3 * (original_projection + 2*(current_projection + cfl_number*right_hand_side))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment