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

Extracted 'calculate_cell_average()' from 'Troubled_Cell_Detector'.

parent a380e3d2
Branches
No related tags found
No related merge requests found
......@@ -2,15 +2,22 @@
"""
@author: Laura C. Kühle
Discussion:
TODO: Ask whether cell averages/reconstructions should be contained in basis
TODO: Contemplate whether basis variables should be public
TODO: Contemplate a Mesh class (mesh, cell_len, num_grid_cells, bounds, etc.)
Urgent:
TODO: Extract do_initial_projection() from DGScheme -> Done
TODO: Move inverse mass matrix to basis -> Done
TODO: Extract calculate_cell_average() from TCD
TODO: Extract calculate_cell_average() from TCD -> Done
TODO: Improve calculate_cell_average()
TODO: Extract calculate_[...]_solution() from Plotting
TODO: Extract plotting from TCD completely
(maybe give indicator which plots are required instead?)
TODO: Contain all plotting in Plotting
TODO: Remove use of DGScheme from ANN_Data_Generator
TODO: Clean up docstrings
TODO: Adapt TCD from Soraya
(Dropbox->...->TEST_troubled-cell-detector->Troubled_Cell_Detector)
TODO: Add verbose output
......@@ -61,6 +68,7 @@ import Limiter
import Quadrature
import Update_Scheme
from Basis_Function import OrthonormalLegendre
from projection_utils import calculate_cell_average
matplotlib.use('Agg')
x = Symbol('x')
......@@ -324,9 +332,10 @@ class DGScheme:
left_bound=self._left_bound, right_bound=self._right_bound,
polynomial_degree=self._polynomial_degree, adjustment=adjustment)
return self._detector.calculate_cell_average(projection[:, 1:-1],
stencil_length,
add_reconstructions)
return calculate_cell_average(
projection=projection[:, 1:-1], stencil_length=stencil_length,
polynomial_degree=self._polynomial_degree, basis=self._basis,
add_reconstructions=add_reconstructions)
def do_initial_projection(initial_condition, basis, quadrature,
......
......@@ -19,6 +19,7 @@ import ANN_Model
from Plotting import plot_solution_and_approx, plot_semilog_error, \
plot_error, plot_shock_tube, plot_details, \
calculate_approximate_solution, calculate_exact_solution
from projection_utils import calculate_cell_average
matplotlib.use('Agg')
x = Symbol('x')
......@@ -43,8 +44,6 @@ class TroubledCellDetector:
Returns string of class name.
get_cells(projection)
Calculates troubled cells in a given projection.
calculate_cell_average_and_reconstructions(projection, stencil_length)
Calculates cell averages and reconstructions for a given projection.
plot_results(projection, troubled_cell_history, time_history)
Plots results and troubled cells of a projection.
......@@ -134,50 +133,6 @@ class TroubledCellDetector:
"""
pass
def calculate_cell_average(self, projection, stencil_length,
add_reconstructions=True):
"""Calculates 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
and added left and right to it, respectively.
Parameters
----------
projection : ndarray
Matrix of projection for each polynomial degree.
stencil_length : int
Size of data array.
add_reconstructions: bool, optional
Flag whether reconstructions of the middle cell are included.
Default: True.
Returns
-------
ndarray
Matrix containing cell averages (and reconstructions) for initial
projection.
"""
cell_averages = calculate_approximate_solution(
projection, [0], 0, self._basis.get_basis_vector())
if add_reconstructions:
left_reconstructions = calculate_approximate_solution(
projection, [-1], self._polynomial_degree,
self._basis.get_basis_vector())
right_reconstructions = calculate_approximate_solution(
projection, [1], self._polynomial_degree,
self._basis.get_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, cell_averages)))
def plot_results(self, projection, troubled_cell_history, time_history):
"""Plots results and troubled cells of a projection.
......@@ -333,10 +288,12 @@ class ArtificialNeuralNetwork(TroubledCellDetector):
projection[:, :num_ghost_cells]), axis=1)
# Calculate input data depending on stencil length
input_data = torch.from_numpy(
np.vstack([self.calculate_cell_average(
projection[:, cell-num_ghost_cells:cell+num_ghost_cells+1],
self._stencil_len, self._add_reconstructions)
input_data = torch.from_numpy(np.vstack([calculate_cell_average(
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)
for cell in range(num_ghost_cells,
len(projection[0])-num_ghost_cells)]))
......
# -*- coding: utf-8 -*-
"""
@author: Laura C. Kühle
"""
import numpy as np
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.
Calculate the cell averages of all cells in a projection.
If desired, reconstructions are calculated for the middle cell
and added left and right to it, respectively.
Parameters
----------
projection : ndarray
Matrix of projection for each polynomial degree.
basis : Basis object
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.
Returns
-------
ndarray
Matrix containing cell averages (and reconstructions) for initial
projection.
"""
cell_averages = calculate_approximate_solution(
projection, [0], 0, basis.get_basis_vector())
if add_reconstructions:
left_reconstructions = calculate_approximate_solution(
projection, [-1], polynomial_degree,
basis.get_basis_vector())
right_reconstructions = calculate_approximate_solution(
projection, [1], polynomial_degree,
basis.get_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, cell_averages)))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment