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

Replaced 'legendre_basis_approximation()' with 'calculate_approximate_solution()'.

parent 0b067043
No related branches found
No related tags found
No related merge requests found
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
TODO: Adjust code to fit style TODO: Adjust code to fit style
TODO: Adapt variable names to fit style TODO: Adapt variable names to fit style
TODO: Replace 'legendre_basis_approximation()' with 'calculate_approximate_solution()' -> Done
""" """
...@@ -14,6 +15,7 @@ from sympy import Symbol ...@@ -14,6 +15,7 @@ from sympy import Symbol
import Initial_Condition import Initial_Condition
import Basis_Function import Basis_Function
import DG_Approximation import DG_Approximation
import Quadrature
x1 = Symbol('x') x1 = Symbol('x')
...@@ -29,7 +31,7 @@ def cell_centers_leg_basis_coeff(num_grid_cells, polynomial_degree, initial_cond ...@@ -29,7 +31,7 @@ def cell_centers_leg_basis_coeff(num_grid_cells, polynomial_degree, initial_cond
initial_condition.induce_adjustment(-h[0]/3) initial_condition.induce_adjustment(-h[0]/3)
left_bound, right_bound = interval left_bound, right_bound = interval
dg_scheme = DG_Approximation.DGScheme('NoDetection', polynomial_degree=polynomial_degree, dg_scheme = DG_Approximation.DGScheme('WaveletDetector', polynomial_degree=polynomial_degree,
num_grid_cells=num_grid_cells, left_bound=left_bound, right_bound=right_bound, num_grid_cells=num_grid_cells, left_bound=left_bound, right_bound=right_bound,
quadrature='Gauss', quadrature_config={'num_eval_points': polynomial_degree+1} quadrature='Gauss', quadrature_config={'num_eval_points': polynomial_degree+1}
) )
...@@ -39,7 +41,7 @@ def cell_centers_leg_basis_coeff(num_grid_cells, polynomial_degree, initial_cond ...@@ -39,7 +41,7 @@ def cell_centers_leg_basis_coeff(num_grid_cells, polynomial_degree, initial_cond
else: else:
coeffs = dg_scheme.build_training_data(centers[1], initial_condition) coeffs = dg_scheme.build_training_data(centers[1], initial_condition)
return centers, h, coeffs return centers, h, interval, coeffs
# Approximation for certain evaluation input_matrix(s) in a cell given a provided polynomial_degree # Approximation for certain evaluation input_matrix(s) in a cell given a provided polynomial_degree
...@@ -84,6 +86,10 @@ def generate_cell_data(num_of_samples, functions, is_smooth): ...@@ -84,6 +86,10 @@ def generate_cell_data(num_of_samples, functions, is_smooth):
samples_per_function = int(num_of_samples / len(functions)) samples_per_function = int(num_of_samples / len(functions))
function_id = 0 function_id = 0
input_data = np.zeros((num_of_samples, 5)) input_data = np.zeros((num_of_samples, 5))
test_data = np.zeros((num_of_samples, 5))
quadrature_cell_center = Quadrature.Custom({'eval_points': [0]})
quadrature_cell_boundary = Quadrature.Custom({'eval_points': [1]})
for i in range(num_of_samples): for i in range(num_of_samples):
print('Object') print('Object')
...@@ -95,18 +101,40 @@ def generate_cell_data(num_of_samples, functions, is_smooth): ...@@ -95,18 +101,40 @@ def generate_cell_data(num_of_samples, functions, is_smooth):
# Create basis_coefficients for function mapped onto stencil # Create basis_coefficients for function mapped onto stencil
polynomial_degree = np.random.randint(1, high=5) polynomial_degree = np.random.randint(1, high=5)
centers, h, basis_coeffs = cell_centers_leg_basis_coeff(3, polynomial_degree, function) centers, h, interval, basis_coeffs = cell_centers_leg_basis_coeff(3, polynomial_degree, function)
h = h[0] # L: h is always a float, why the list format???
# Calculating cell averages and evaluations at the boundary # Calculating cell averages and evaluations at the boundary
for j in range(3): for j in range(3):
# Cell Averages # Cell Averages
input_data[i, 2 * j] = legendre_basis_approximation(centers[j], centers, j, 0, basis_coeffs) input_data[i, 2 * j] = legendre_basis_approximation(centers[j], centers, j, 0, basis_coeffs)
# print()
# print('data', i, 2*j)
# print(input_data[i, 2*j])
# print(legendre_basis_approximation(centers[j], centers, j, polynomial_degree, basis_coeffs)[0])
# print()
# Evaluations at Boundary of Center Cell # Evaluations at Boundary of Center Cell
if j == 1: if j == 1:
input_data[i, j] = legendre_basis_approximation(centers[j] - h / 2, centers, j, polynomial_degree, input_data[i, j] = legendre_basis_approximation(centers[j] - h / 2, centers, j, polynomial_degree,
basis_coeffs) basis_coeffs)
input_data[i, 2 * j + 1] = legendre_basis_approximation(centers[j] + h / 2, centers, j, input_data[i, 2 * j + 1] = legendre_basis_approximation(centers[j] + h / 2, centers, j,
polynomial_degree, basis_coeffs) polynomial_degree, basis_coeffs)
# print(i, j, 2*j+1)
# print(input_data[i, j])
# print(input_data[i, 2*j+1])
left_bound, right_bound = interval
# print('Yeah')
dg_scheme = DG_Approximation.DGScheme('WaveletDetector', polynomial_degree=polynomial_degree,
num_grid_cells=3, left_bound=left_bound,
right_bound=right_bound)
# print('test')
# print(input_data[i])
test_1 = dg_scheme.check_wavelet(np.transpose(basis_coeffs), quadrature_cell_center, 0)
test_2 = dg_scheme.check_wavelet(np.transpose(basis_coeffs), quadrature_cell_boundary, polynomial_degree)
# print(test_1, test_2)
test_data[i] = np.array(list(zip(test_1[:, 0], test_2[:, 0], test_1[:, 1], test_2[:, 1], test_1[:, 2])))[0]
# print(test_data[i])
# Update Function ID # Update Function ID
if (i % samples_per_function == samples_per_function - 1) and (function_id != len(functions)-1): if (i % samples_per_function == samples_per_function - 1) and (function_id != len(functions)-1):
...@@ -115,6 +143,13 @@ def generate_cell_data(num_of_samples, functions, is_smooth): ...@@ -115,6 +143,13 @@ def generate_cell_data(num_of_samples, functions, is_smooth):
# Shuffle Function input_matrices # Shuffle Function input_matrices
order = np.random.permutation(num_of_samples) order = np.random.permutation(num_of_samples)
input_data = input_data[order] input_data = input_data[order]
test_data = test_data[order]
for i in range(len(input_data)):
print(i)
print(input_data[i])
print(test_data[i])
print()
output_data = np.zeros((num_of_samples, 2)) output_data = np.zeros((num_of_samples, 2))
if is_smooth: if is_smooth:
...@@ -122,7 +157,7 @@ def generate_cell_data(num_of_samples, functions, is_smooth): ...@@ -122,7 +157,7 @@ def generate_cell_data(num_of_samples, functions, is_smooth):
else: else:
output_data[:, 0] = np.ones(num_of_samples) output_data[:, 0] = np.ones(num_of_samples)
return input_data, output_data return test_data, output_data
def normalize_data(input_data): def normalize_data(input_data):
......
...@@ -188,3 +188,7 @@ class DGScheme(object): ...@@ -188,3 +188,7 @@ class DGScheme(object):
initial_condition = self._init_cond initial_condition = self._init_cond
projection = self._do_initial_projection(initial_condition, adjustment) projection = self._do_initial_projection(initial_condition, adjustment)
return np.transpose(projection)[1:-1] return np.transpose(projection)[1:-1]
def check_wavelet(self, projection, quadrature, polynomial_degree):
projection = self._detector.calculate_approximate_solution(projection, quadrature, polynomial_degree)
return projection
...@@ -70,7 +70,7 @@ class TroubledCellDetector(object): ...@@ -70,7 +70,7 @@ class TroubledCellDetector(object):
def _plot_mesh(self, projection): def _plot_mesh(self, projection):
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], self._quadrature, self._polynomial_degree)
pointwise_error = np.abs(exact-approx) pointwise_error = np.abs(exact-approx)
max_error = np.max(pointwise_error) max_error = np.max(pointwise_error)
...@@ -130,16 +130,16 @@ class TroubledCellDetector(object): ...@@ -130,16 +130,16 @@ class TroubledCellDetector(object):
return grid, exact return grid, exact
def _calculate_approximate_solution(self, projection): def calculate_approximate_solution(self, projection, quadrature, polynomial_degree):
points = self._quadrature.get_eval_points() points = quadrature.get_eval_points()
num_points = self._quadrature.get_num_points() num_points = quadrature.get_num_points()
basis = self._basis.get_basis_vector() basis = self._basis.get_basis_vector()
basis_matrix = [[basis[degree].subs(x, points[point]) for point in range(num_points)] basis_matrix = [[basis[degree].subs(x, points[point]) for point in range(num_points)]
for degree in range(self._polynomial_degree+1)] for degree in range(polynomial_degree+1)]
approx = [[sum(projection[degree][cell] * basis_matrix[degree][point] approx = [[sum(projection[degree][cell] * basis_matrix[degree][point]
for degree in range(self._polynomial_degree+1)) for degree in range(polynomial_degree+1))
for point in range(num_points)] for point in range(num_points)]
for cell in range(len(projection[0]))] for cell in range(len(projection[0]))]
...@@ -295,7 +295,7 @@ class WaveletDetector(TroubledCellDetector): ...@@ -295,7 +295,7 @@ class WaveletDetector(TroubledCellDetector):
def _plot_mesh(self, projection): def _plot_mesh(self, projection):
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], self._quadrature, self._polynomial_degree)
pointwise_error = np.abs(exact-approx) pointwise_error = np.abs(exact-approx)
max_error = np.max(pointwise_error) max_error = np.max(pointwise_error)
...@@ -327,7 +327,7 @@ class WaveletDetector(TroubledCellDetector): ...@@ -327,7 +327,7 @@ class WaveletDetector(TroubledCellDetector):
# Plot exact and approximate solutions for coarse mesh # Plot exact and approximate solutions for coarse mesh
grid, exact = self._calculate_exact_solution(coarse_mesh[1:-1], coarse_cell_len) grid, exact = self._calculate_exact_solution(coarse_mesh[1:-1], coarse_cell_len)
approx = self._calculate_approximate_solution(coarse_projection) approx = self.calculate_approximate_solution(coarse_projection, self._quadrature, self._polynomial_degree)
self._plot_solution_and_approx(grid, exact, approx, self._colors['coarse_exact'], self._colors['coarse_approx']) self._plot_solution_and_approx(grid, exact, approx, self._colors['coarse_exact'], self._colors['coarse_approx'])
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment