diff --git a/DG_Approximation.py b/DG_Approximation.py index ea826715fc6a3210ecefac5f6f73fc360097ec14..09050d1d47114d221d828c0935ee4da5397b2283 100644 --- a/DG_Approximation.py +++ b/DG_Approximation.py @@ -3,7 +3,8 @@ @author: Laura C. Kühle Urgent: -TODO: Move plotting into separate function +TODO: Move plotting into separate function -> Done +TODO: Move plotting into separate rule TODO: Adapt TCD from Soraya (Dropbox->...->TEST_troubled-cell-detector->Troubled_Cell_Detector) TODO: Add verbose output @@ -201,22 +202,19 @@ class DGScheme: self._polynomial_degree, self._num_grid_cells, self._detector, self._limiter) - def approximate(self, data_dir, data_name): + def approximate(self, data_file): """Approximates projection. Initializes projection and evolves it in time. Each time step consists of three parts: A projection update, a troubled-cell detection, and limiting based on the detected cells. - At final time, result and error plots are - generated and, if verbose flag is set, also displayed. + At final time, results are saved in JSON file. Attributes ---------- - data_dir: str - Path to directory in which data is saved. - data_name : str - Name of data. + data_file: str + Path to file in which data will be saved. """ projection = self._do_initial_projection(self._init_cond) @@ -254,12 +252,28 @@ class DGScheme: for key in approx_stats.keys()} # Save approximation results in JSON format - with open(data_dir + '/' + data_name + '.json', 'w') \ + with open(data_file + '.json', 'w') \ as json_file: json_file.write(json.dumps(approx_stats)) + def plot_approximation_results(self, data_file, directory, plot_name): + """Plots given approximation results. + + Generates plots based on given data, sets plot directory if not + already existing, and saves plots. + + Parameters + ---------- + data_file: str + Path to data file for plotting. + directory: str + Path to directory in which plots will be saved. + plot_name : str + Name of plot. + + """ # Read approximation results - with open(data_dir + '/' + data_name + '.json') as json_file: + with open(data_file + '.json') as json_file: approx_stats = json.load(json_file) # Decode all ndarrays by converting lists @@ -270,32 +284,18 @@ class DGScheme: # and any detector-dependant plots self._detector.plot_results(**approx_stats) - def save_plots(self, plot_dir, plot_name): - """Saves plotted results. - - Sets plot directory, if not already existing, and saves plots - generated during the last approximation. - - Parameters - ---------- - plot_dir: str - Path to directory in which plots are saved. - plot_name : str - Name of plot. - - """ # Set paths for plot files if not existing already - if not os.path.exists(plot_dir): - os.makedirs(plot_dir) + if not os.path.exists(directory): + os.makedirs(directory) # Save plots for identifier in plt.get_figlabels(): # Set path for figure directory if not existing already - if not os.path.exists(plot_dir + '/' + identifier): - os.makedirs(plot_dir + '/' + identifier) + if not os.path.exists(directory + '/' + identifier): + os.makedirs(directory + '/' + identifier) plt.figure(identifier) - plt.savefig(plot_dir + '/' + identifier + '/' + + plt.savefig(directory + '/' + identifier + '/' + plot_name + '.pdf') def _reset(self): diff --git a/config.yaml b/config.yaml index c260bc19457e4029a01046941d3ddbf86188acec..05e07455eee4aa73a5267772a12aa979f359bff4 100644 --- a/config.yaml +++ b/config.yaml @@ -1,9 +1,9 @@ -data_dir: 'model-Soraya_Mar02' +data_dir: 'model-Mar14' random_seed: 1234 # Parameter for Approximation with Troubled Cell Detection Approximation: - plot_dir: 'fig-Soraya_Mar02' + plot_dir: 'fig-Mar14' schemes: Separation_Test: @@ -21,7 +21,7 @@ Approximation: detector_config: fold_len: 16 whisker_len: 3 - add_reconstructions: False + add_reconstructions: True model_state: 'Adam.model.pt' init_cond: 'Sine' @@ -52,7 +52,7 @@ ANN_Data: smooth_troubled_balance: 0.5 stencil_length: 3 - add_reconstructions : False + add_reconstructions : True # Initial Conditions for Training Data functions: @@ -87,7 +87,7 @@ ANN_Training: threshold: 1.0e-5 batch_size: 500 model: ThreeLayerReLu - model_config: {input_size: 3} + model_config: {} loss_function: BCELoss optimizer: Adam SGD: @@ -95,7 +95,7 @@ ANN_Training: threshold: 1.0e-5 batch_size: 500 model: ThreeLayerReLu - model_config: {input_size: 3} + model_config: {} loss_function: BCELoss optimizer: SGD diff --git a/workflows/approximation.smk b/workflows/approximation.smk index 75f7982f33ea9e2ba28d864ac80aed08dce7fa34..13ae4e758464394e4342e2ddee32473fe4ee570c 100644 --- a/workflows/approximation.smk +++ b/workflows/approximation.smk @@ -48,10 +48,11 @@ rule approximate_solution: print(params.dg_params) dg_scheme = DGScheme(**params.dg_params) - dg_scheme.approximate(data_dir=params.plot_dir, - data_name=wildcards.scheme) - dg_scheme.save_plots(plot_dir=params.plot_dir, - plot_name=wildcards.scheme) + dg_scheme.approximate( + data_file=params.plot_dir+'/'+wildcards.scheme) + dg_scheme.plot_approximation_results(directory=params.plot_dir, + plot_name=wildcards.scheme, + data_file=params.plot_dir+'/'+wildcards.scheme) toc = time.perf_counter() print(f'Time: {toc - tic:0.4f}s') \ No newline at end of file