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

Replaced 'length' with 'len'.

parent d51b385f
No related branches found
No related tags found
No related merge requests found
...@@ -54,7 +54,7 @@ class TrainingDataGenerator: ...@@ -54,7 +54,7 @@ class TrainingDataGenerator:
def build_training_data(self, initial_conditions, num_samples, balance=0.5, def build_training_data(self, initial_conditions, num_samples, balance=0.5,
directory='test_data', add_reconstructions=True, directory='test_data', add_reconstructions=True,
stencil_length=3): stencil_len=3):
"""Builds random training data. """Builds random training data.
Creates training data consisting of random ANN input and saves it. Creates training data consisting of random ANN input and saves it.
...@@ -73,7 +73,7 @@ class TrainingDataGenerator: ...@@ -73,7 +73,7 @@ class TrainingDataGenerator:
add_reconstructions : bool, optional add_reconstructions : bool, optional
Flag whether reconstructions of the middle cell are included. Flag whether reconstructions of the middle cell are included.
Default: True. Default: True.
stencil_length : int, optional stencil_len : int, optional
Size of training data array. Default: 3. Size of training data array. Default: 3.
Returns Returns
...@@ -86,15 +86,15 @@ class TrainingDataGenerator: ...@@ -86,15 +86,15 @@ class TrainingDataGenerator:
tic = time.perf_counter() tic = time.perf_counter()
# Set stencil length # Set stencil length
if stencil_length % 2 == 0: if stencil_len % 2 == 0:
raise ValueError('Invalid stencil length (even value): "%d"' raise ValueError('Invalid stencil length (even value): "%d"'
% stencil_length) % stencil_len)
print('Calculating training data...\n') print('Calculating training data...\n')
data_dict = self._calculate_data_set(initial_conditions, data_dict = self._calculate_data_set(initial_conditions,
num_samples, balance, num_samples, balance,
add_reconstructions, add_reconstructions,
stencil_length) stencil_len)
print('Finished calculating training data!') print('Finished calculating training data!')
self._save_data(directory=directory, data=data_dict) self._save_data(directory=directory, data=data_dict)
...@@ -103,7 +103,7 @@ class TrainingDataGenerator: ...@@ -103,7 +103,7 @@ class TrainingDataGenerator:
return data_dict return data_dict
def _calculate_data_set(self, initial_conditions, num_samples, balance, def _calculate_data_set(self, initial_conditions, num_samples, balance,
add_reconstructions, stencil_length): add_reconstructions, stencil_len):
"""Calculates random training data of given stencil length. """Calculates random training data of given stencil length.
Creates training data with a given ratio between smooth and Creates training data with a given ratio between smooth and
...@@ -119,7 +119,7 @@ class TrainingDataGenerator: ...@@ -119,7 +119,7 @@ class TrainingDataGenerator:
Ratio between smooth and discontinuous training data. Ratio between smooth and discontinuous training data.
add_reconstructions : bool add_reconstructions : bool
Flag whether reconstructions of the middle cell are included. Flag whether reconstructions of the middle cell are included.
stencil_length : int stencil_len : int
Size of training data array. Size of training data array.
Returns Returns
...@@ -142,12 +142,12 @@ class TrainingDataGenerator: ...@@ -142,12 +142,12 @@ class TrainingDataGenerator:
num_smooth_samples = round(num_samples * balance) num_smooth_samples = round(num_samples * balance)
smooth_input, smooth_output = self._generate_cell_data( smooth_input, smooth_output = self._generate_cell_data(
num_smooth_samples, smooth_functions, add_reconstructions, num_smooth_samples, smooth_functions, add_reconstructions,
stencil_length, True) stencil_len, True)
num_troubled_samples = num_samples - num_smooth_samples num_troubled_samples = num_samples - num_smooth_samples
troubled_input, troubled_output = self._generate_cell_data( troubled_input, troubled_output = self._generate_cell_data(
num_troubled_samples, troubled_functions, add_reconstructions, num_troubled_samples, troubled_functions, add_reconstructions,
stencil_length, False) stencil_len, False)
# Merge Data # Merge Data
input_matrix = np.concatenate((smooth_input, troubled_input), axis=0) input_matrix = np.concatenate((smooth_input, troubled_input), axis=0)
...@@ -167,7 +167,7 @@ class TrainingDataGenerator: ...@@ -167,7 +167,7 @@ class TrainingDataGenerator:
'input_data.normalized': norm_input_matrix} 'input_data.normalized': norm_input_matrix}
def _generate_cell_data(self, num_samples, initial_conditions, def _generate_cell_data(self, num_samples, initial_conditions,
add_reconstructions, stencil_length, is_smooth): add_reconstructions, stencil_len, is_smooth):
"""Generates random training input and output. """Generates random training input and output.
Generates random training input and output for either smooth or Generates random training input and output for either smooth or
...@@ -182,7 +182,7 @@ class TrainingDataGenerator: ...@@ -182,7 +182,7 @@ class TrainingDataGenerator:
List of names of initial conditions for training. List of names of initial conditions for training.
add_reconstructions : bool add_reconstructions : bool
Flag whether reconstructions of the middle cell are included. Flag whether reconstructions of the middle cell are included.
stencil_length : int stencil_len : int
Size of training data array. Size of training data array.
is_smooth : bool is_smooth : bool
Flag whether initial conditions are smooth. Flag whether initial conditions are smooth.
...@@ -201,7 +201,7 @@ class TrainingDataGenerator: ...@@ -201,7 +201,7 @@ class TrainingDataGenerator:
print('Samples to complete:', num_samples) print('Samples to complete:', num_samples)
tic = time.perf_counter() tic = time.perf_counter()
num_datapoints = stencil_length num_datapoints = stencil_len
if add_reconstructions: if add_reconstructions:
num_datapoints += 2 num_datapoints += 2
input_data = np.zeros((num_samples, num_datapoints)) input_data = np.zeros((num_samples, num_datapoints))
...@@ -216,11 +216,11 @@ class TrainingDataGenerator: ...@@ -216,11 +216,11 @@ class TrainingDataGenerator:
# Build mesh for random stencil of given length # Build mesh for random stencil of given length
mesh = self._mesh_list[int(np.random.randint( mesh = self._mesh_list[int(np.random.randint(
3, high=12, size=1))-3].random_stencil(stencil_length) 3, high=12, size=1))-3].random_stencil(stencil_len)
# Induce shift to capture troubled cells # Induce shift to capture troubled cells
shift = 0 if initial_condition.is_smooth() \ shift = 0 if initial_condition.is_smooth() \
else mesh.non_ghost_cells[stencil_length//2] else mesh.non_ghost_cells[stencil_len//2]
if initial_condition.discontinuity_position == 'left': if initial_condition.discontinuity_position == 'left':
shift -= mesh.cell_len/2 shift -= mesh.cell_len/2
elif initial_condition.discontinuity_position == 'right': elif initial_condition.discontinuity_position == 'right':
...@@ -236,7 +236,7 @@ class TrainingDataGenerator: ...@@ -236,7 +236,7 @@ class TrainingDataGenerator:
input_data[i] = self._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=stencil_length, stencil_len=stencil_len,
add_reconstructions=add_reconstructions) add_reconstructions=add_reconstructions)
count += 1 count += 1
......
...@@ -143,7 +143,7 @@ class Basis(ABC): ...@@ -143,7 +143,7 @@ class Basis(ABC):
"""Return wavelet projection.""" """Return wavelet projection."""
pass pass
def calculate_cell_average(self, projection: ndarray, stencil_length: int, def calculate_cell_average(self, projection: ndarray, stencil_len: int,
add_reconstructions: bool = True) -> ndarray: add_reconstructions: bool = True) -> ndarray:
"""Calculate cell averages for a given projection. """Calculate cell averages for a given projection.
...@@ -155,7 +155,7 @@ class Basis(ABC): ...@@ -155,7 +155,7 @@ class Basis(ABC):
---------- ----------
projection : ndarray projection : ndarray
Matrix of projection for each polynomial degree. Matrix of projection for each polynomial degree.
stencil_length : int stencil_len : int
Size of data array. Size of data array.
add_reconstructions : bool, optional add_reconstructions : bool, optional
Flag whether reconstructions of the middle cell are included. Flag whether reconstructions of the middle cell are included.
...@@ -172,7 +172,7 @@ class Basis(ABC): ...@@ -172,7 +172,7 @@ class Basis(ABC):
projection, np.array([0]), 0, self.basis) projection, np.array([0]), 0, self.basis)
if add_reconstructions: if add_reconstructions:
middle_idx = stencil_length // 2 middle_idx = stencil_len // 2
left_reconstructions, right_reconstructions = \ left_reconstructions, right_reconstructions = \
self._calculate_reconstructions( self._calculate_reconstructions(
projection[:, middle_idx:middle_idx+1]) projection[:, middle_idx:middle_idx+1])
...@@ -474,7 +474,7 @@ class OrthonormalLegendre(Legendre): ...@@ -474,7 +474,7 @@ class OrthonormalLegendre(Legendre):
matrix.append(row) matrix.append(row)
return np.array(matrix) return np.array(matrix)
def calculate_cell_average(self, projection: ndarray, stencil_length: int, def calculate_cell_average(self, projection: ndarray, stencil_len: int,
add_reconstructions: bool = True) -> ndarray: add_reconstructions: bool = True) -> ndarray:
"""Calculate cell averages for a given projection. """Calculate cell averages for a given projection.
...@@ -491,7 +491,7 @@ class OrthonormalLegendre(Legendre): ...@@ -491,7 +491,7 @@ class OrthonormalLegendre(Legendre):
---------- ----------
projection : ndarray projection : ndarray
Matrix of projection for each polynomial degree. Matrix of projection for each polynomial degree.
stencil_length : int stencil_len : int
Size of data array. Size of data array.
add_reconstructions : bool, optional add_reconstructions : bool, optional
Flag whether reconstructions of the middle cell are included. Flag whether reconstructions of the middle cell are included.
...@@ -507,7 +507,7 @@ class OrthonormalLegendre(Legendre): ...@@ -507,7 +507,7 @@ class OrthonormalLegendre(Legendre):
cell_averages = np.array([projection[0] / np.sqrt(2)]) cell_averages = np.array([projection[0] / np.sqrt(2)])
if add_reconstructions: if add_reconstructions:
middle_idx = stencil_length // 2 middle_idx = stencil_len // 2
left_reconstructions, right_reconstructions = \ left_reconstructions, right_reconstructions = \
self._calculate_reconstructions( self._calculate_reconstructions(
projection[:, middle_idx:middle_idx+1]) projection[:, middle_idx:middle_idx+1])
......
...@@ -180,7 +180,7 @@ class ArtificialNeuralNetwork(TroubledCellDetector): ...@@ -180,7 +180,7 @@ class ArtificialNeuralNetwork(TroubledCellDetector):
self._basis.calculate_cell_average( self._basis.calculate_cell_average(
projection=projection[ projection=projection[
:, cell-num_ghost_cells:cell+num_ghost_cells+1], :, cell-num_ghost_cells:cell+num_ghost_cells+1],
stencil_length=self._stencil_len, stencil_len=self._stencil_len,
add_reconstructions=self._add_reconstructions) add_reconstructions=self._add_reconstructions)
for cell in range(num_ghost_cells, for cell in range(num_ghost_cells,
len(projection[0])-num_ghost_cells)])) len(projection[0])-num_ghost_cells)]))
......
...@@ -119,7 +119,7 @@ class Mesh: ...@@ -119,7 +119,7 @@ class Mesh:
'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: def random_stencil(self, stencil_len: int) -> Mesh:
"""Return random stencil. """Return random stencil.
Build mesh with given number of cell centers around a random point Build mesh with given number of cell centers around a random point
...@@ -136,15 +136,15 @@ class Mesh: ...@@ -136,15 +136,15 @@ class Mesh:
# Adjust grid spacing to be within interval if necessary # Adjust grid spacing to be within interval if necessary
grid_spacing = self.cell_len grid_spacing = self.cell_len
max_spacing = 2/stencil_length*min(point-self._left_bound, max_spacing = 2/stencil_len*min(point-self._left_bound,
self._right_bound-point) self._right_bound-point)
while grid_spacing > max_spacing: while grid_spacing > max_spacing:
grid_spacing /= 2 grid_spacing /= 2
# Return new mesh instance # Return new mesh instance
return Mesh(left_bound=point - stencil_length/2 * grid_spacing, return Mesh(left_bound=point - stencil_len/2 * grid_spacing,
right_bound=point + stencil_length/2 * grid_spacing, right_bound=point + stencil_len/2 * grid_spacing,
num_grid_cells=stencil_length, num_ghost_cells=2, num_grid_cells=stencil_len, num_ghost_cells=2,
training_data_mode=True) training_data_mode=True)
......
...@@ -20,7 +20,7 @@ rule generate_data: ...@@ -20,7 +20,7 @@ rule generate_data:
left_bound = config['left_boundary'], left_bound = config['left_boundary'],
right_bound = config['right_boundary'], right_bound = config['right_boundary'],
balance = config['smooth_troubled_balance'], balance = config['smooth_troubled_balance'],
stencil_length = config['stencil_length'], stencil_len = config['stencil_length'],
sample_number = config['sample_number'], sample_number = config['sample_number'],
reconstruction_flag = config['add_reconstructions'], reconstruction_flag = config['add_reconstructions'],
functions = expand('{FUNCTION}', FUNCTION=config['functions']) functions = expand('{FUNCTION}', FUNCTION=config['functions'])
...@@ -43,4 +43,4 @@ rule generate_data: ...@@ -43,4 +43,4 @@ rule generate_data:
initial_conditions=initial_conditions, directory=DIR, initial_conditions=initial_conditions, directory=DIR,
num_samples=params.sample_number, num_samples=params.sample_number,
add_reconstructions=params.reconstruction_flag, add_reconstructions=params.reconstruction_flag,
stencil_length=params.stencil_length) stencil_len=params.stencil_len)
\ No newline at end of file \ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment