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

Moved stencil building to Mesh class.

parent c7f002a9
No related branches found
No related tags found
No related merge requests found
...@@ -13,15 +13,20 @@ from Quadrature import Gauss ...@@ -13,15 +13,20 @@ from Quadrature import Gauss
from Basis_Function import OrthonormalLegendre from Basis_Function import OrthonormalLegendre
basis_list = [OrthonormalLegendre(pol_deg) for pol_deg in range(5)]
quadrature_list = [Gauss({'num_nodes': pol_deg+1}) for pol_deg in range(5)]
class TrainingDataGenerator: class TrainingDataGenerator:
"""Class for training data generator. """Class for training data generator.
Generates random training data for given initial conditions. Generates random training data for given initial conditions.
Attributes
----------
basis_list : list
List of basis instances for degree 1 to 4.
quadrature_list : list
List of Gauss quadrature instances for degree 2 to 5.
mesh_list : list
List of Mesh instances for 2**(3 to 8) cells.
Methods Methods
------- -------
build_training_data(num_samples) build_training_data(num_samples)
...@@ -41,8 +46,13 @@ class TrainingDataGenerator: ...@@ -41,8 +46,13 @@ class TrainingDataGenerator:
Size of training data array. Default: 3. Size of training data array. Default: 3.
""" """
self._left_bound = left_bound self._basis_list = [OrthonormalLegendre(pol_deg)
self._right_bound = right_bound for pol_deg in range(5)]
self._quadrature_list = [Gauss({'num_nodes': pol_deg+1})
for pol_deg in range(5)]
self._mesh_list = [Mesh(left_bound=left_bound, right_bound=right_bound,
num_ghost_cells=0, num_grid_cells=2**exp)
for exp in range(3, 9)]
# Set stencil length # Set stencil length
if stencil_length % 2 == 0: if stencil_length % 2 == 0:
...@@ -147,6 +157,8 @@ class TrainingDataGenerator: ...@@ -147,6 +157,8 @@ class TrainingDataGenerator:
# Create normalized input data # Create normalized input data
norm_input_matrix = self._normalize_data(input_matrix) norm_input_matrix = self._normalize_data(input_matrix)
# print(input_matrix)
# print(norm_input_matrix)
return {'input_data.raw': input_matrix, 'output_data': output_matrix, return {'input_data.raw': input_matrix, 'output_data': output_matrix,
'input_data.normalized': norm_input_matrix} 'input_data.normalized': norm_input_matrix}
...@@ -197,28 +209,28 @@ class TrainingDataGenerator: ...@@ -197,28 +209,28 @@ class TrainingDataGenerator:
initial_condition.randomize( initial_condition.randomize(
initial_conditions[function_id]['config']) initial_conditions[function_id]['config'])
# Build random stencil of given length # Build mesh for random stencil of given length
interval, centers, spacing = self._build_stencil() mesh = self._mesh_list[int(np.random.randint(
left_bound, right_bound = interval 3, high=9, size=1))-3].random_stencil(self._stencil_length)
centers = [center[0] for center in centers]
# Induce adjustment to capture troubled cells # Induce adjustment to capture troubled cells
adjustment = 0 if initial_condition.is_smooth() \ adjustment = 0 if initial_condition.is_smooth() \
else centers[self._stencil_length//2] else mesh.non_ghost_cells[self._stencil_length//2]
initial_condition.induce_adjustment(-spacing[0]/3) initial_condition.induce_adjustment(-mesh.cell_len/3)
# print(initial_condition.is_smooth())
# print(mesh.interval_len, mesh.non_ghost_cells, mesh.cell_len)
# print(adjustment, -mesh.cell_len/3)
# print()
# Calculate basis coefficients for stencil # Calculate basis coefficients for stencil
polynomial_degree = np.random.randint(1, high=5) polynomial_degree = np.random.randint(1, high=5)
mesh = Mesh(num_grid_cells=self._stencil_length, num_ghost_cells=2,
left_bound=left_bound, right_bound=right_bound)
projection = do_initial_projection( projection = do_initial_projection(
initial_condition=initial_condition, mesh=mesh, initial_condition=initial_condition, mesh=mesh,
basis=basis_list[polynomial_degree], basis=self._basis_list[polynomial_degree],
quadrature=quadrature_list[polynomial_degree], quadrature=self._quadrature_list[polynomial_degree],
adjustment=adjustment) adjustment=adjustment)
# print(projection)
input_data[i] = basis_list[ input_data[i] = self._basis_list[
polynomial_degree].calculate_cell_average( polynomial_degree].calculate_cell_average(
projection=projection[:, 1:-1], projection=projection[:, 1:-1],
stencil_length=self._stencil_length, stencil_length=self._stencil_length,
...@@ -239,42 +251,6 @@ class TrainingDataGenerator: ...@@ -239,42 +251,6 @@ class TrainingDataGenerator:
return input_data, output_data return input_data, output_data
def _build_stencil(self):
"""Builds random stencil.
Calculates fixed number of cell centers around a random point in a
given 1D domain.
Returns
-------
interval : ndarray
List containing left and right bound of interval.
stencil : ndarray
List of cell centers in stencil.
grid_spacing : float
Length of cell in grid.
"""
# Select random cell length
grid_spacing = 2 / (2 ** np.random.randint(3, high=9, size=1))
# Pick random point between left and right bound
point = np.random.uniform(self._left_bound, self._right_bound)
# Adjust grid spacing if necessary for stencil creation
while point - self._stencil_length/2 * grid_spacing < self._left_bound\
or point + self._stencil_length/2 * \
grid_spacing > self._right_bound:
grid_spacing /= 2
# Build x-point stencil
interval = np.array([point - self._stencil_length/2 * grid_spacing,
point + self._stencil_length/2 * grid_spacing])
stencil = np.array([point + factor * grid_spacing
for factor in range(-(self._stencil_length//2),
self._stencil_length//2 + 1)])
return interval, stencil, grid_spacing
@staticmethod @staticmethod
def _normalize_data(input_data): def _normalize_data(input_data):
"""Normalizes data. """Normalizes data.
......
...@@ -12,10 +12,16 @@ TODO: Contemplate containing coarse mesh generation in Mesh ...@@ -12,10 +12,16 @@ TODO: Contemplate containing coarse mesh generation in Mesh
TODO: Contemplate extracting boundary condition from InitialCondition TODO: Contemplate extracting boundary condition from InitialCondition
TODO: Contemplate containing boundary condition in Mesh TODO: Contemplate containing boundary condition in Mesh
TODO: Ask whether all quadratures depend on freely chosen num_nodes TODO: Ask whether all quadratures depend on freely chosen num_nodes
TODO: Contemplate saving each IC separately
TODO: Contemplate removing TrainingDataGenerator class
Urgent: Urgent:
TODO: Remove unnecessary instance variables from TrainingDataGenerator TODO: Remove unnecessary instance variables from TrainingDataGenerator -> Done
TODO: Find error in centering for ANN training TODO: Fix bug during adjustment setting -> Done
TODO: Move stencil building to Mesh -> Done
TODO: Investigate self-referencing in classes
TODO: Remove stencil_length as instance variable
TODO: Find errors in centering for ANN training
TODO: Adapt TCD from Soraya TODO: Adapt TCD from Soraya
(Dropbox->...->TEST_troubled-cell-detector->Troubled_Cell_Detector) (Dropbox->...->TEST_troubled-cell-detector->Troubled_Cell_Detector)
TODO: Add TC condition to only flag cell if left-adjacent one is flagged as TODO: Add TC condition to only flag cell if left-adjacent one is flagged as
......
...@@ -93,13 +93,40 @@ class Mesh: ...@@ -93,13 +93,40 @@ class Mesh:
"""Return the cell centers of the mesh (excluding ghost cells).""" """Return the cell centers of the mesh (excluding ghost cells)."""
return self.cells[self._num_ghost_cells:-self._num_ghost_cells] return self.cells[self._num_ghost_cells:-self._num_ghost_cells]
def create_data_dict(self): def create_data_dict(self) -> dict:
"""Return dictionary with data necessary to construct mesh.""" """Return dictionary with data necessary to construct mesh."""
return {'num_grid_cells': self._num_grid_cells, return {'num_grid_cells': self._num_grid_cells,
'left_bound': self._left_bound, 'left_bound': self._left_bound,
'right_bound': self._right_bound, 'right_bound': self._right_bound,
'num_ghost_cells': self._num_ghost_cells} 'num_ghost_cells': self._num_ghost_cells}
def random_stencil(self, stencil_length: int) -> 'Mesh':
"""Return random stencil.
Build mesh with given number of cell centers around a random point
in the underlying interval.
Returns
-------
Mesh object
Mesh of given size around random point.
"""
# Pick random point between left and right bound
point = np.random.uniform(self._left_bound, self._right_bound)
# Adjust grid spacing to be within interval if necessary
# creation
grid_spacing = self.cell_len
while point - stencil_length/2 * grid_spacing < self._left_bound \
or point + stencil_length/2 * grid_spacing > self._right_bound:
grid_spacing /= 2
# Return new mesh instance
return Mesh(left_bound=point - stencil_length/2 * grid_spacing,
right_bound=point + stencil_length/2 * grid_spacing,
num_grid_cells=stencil_length, num_ghost_cells=2)
def calculate_approximate_solution( def calculate_approximate_solution(
projection: ndarray, points: ndarray, polynomial_degree: int, projection: ndarray, points: ndarray, polynomial_degree: int,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment