From e88821cb57d1e9e8e05e99f3d69b23cd1f3cd552 Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Tue, 2 Apr 2024 16:54:01 +0200 Subject: [PATCH 1/2] Allow user to change default resolution from command line --- openmc_plotter/__main__.py | 5 ++++- openmc_plotter/main_window.py | 7 +++++-- openmc_plotter/plotmodel.py | 34 ++++++++++++++++++++++++---------- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/openmc_plotter/__main__.py b/openmc_plotter/__main__.py index 1e022c6..fc10cdb 100644 --- a/openmc_plotter/__main__.py +++ b/openmc_plotter/__main__.py @@ -21,6 +21,8 @@ def main(): help='Ignore plot_settings.pkl file if present.') ap.add_argument('-s', '--threads', type=int, default=None, help='If present, number of threads used to generate plots.') + ap.add_argument('-r', '--resolution', type=int, default=1000, + help='Default number of pixels in each direction') ap.add_argument('model_path', nargs='?', default=os.curdir, help='Location of model XML file or a directory containing ' 'XML files (default is current dir)') @@ -68,7 +70,8 @@ def run_app(user_args): font_metric = QtGui.QFontMetrics(app.font()) screen_size = app.primaryScreen().size() - mainWindow = MainWindow(font_metric, screen_size, user_args.model_path, user_args.threads) + mainWindow = MainWindow(font_metric, screen_size, user_args.model_path, + user_args.threads, user_args.resolution) # connect splashscreen to main window, close when main window opens mainWindow.loadGui(use_settings_pkl=user_args.ignore_settings) diff --git a/openmc_plotter/main_window.py b/openmc_plotter/main_window.py index 7a7b204..0599c12 100755 --- a/openmc_plotter/main_window.py +++ b/openmc_plotter/main_window.py @@ -45,7 +45,9 @@ class MainWindow(QMainWindow): def __init__(self, font=QtGui.QFontMetrics(QtGui.QFont()), screen_size=QtCore.QSize(), - model_path='.', threads=None): + model_path='.', + threads=None, + resolution=1000): super().__init__() self.screen = screen_size @@ -53,6 +55,7 @@ def __init__(self, self.setWindowTitle('OpenMC Plot Explorer') self.model_path = Path(model_path) self.threads = threads + self.default_res = resolution def loadGui(self, use_settings_pkl=True): @@ -471,7 +474,7 @@ def loadModel(self, reload=False, use_settings_pkl=True): if reload: self.resetModels() else: - self.model = PlotModel(use_settings_pkl, self.model_path) + self.model = PlotModel(use_settings_pkl, self.model_path, self.default_res) # update plot and model settings self.updateRelativeBases() diff --git a/openmc_plotter/plotmodel.py b/openmc_plotter/plotmodel.py index 8a70048..9628cbe 100644 --- a/openmc_plotter/plotmodel.py +++ b/openmc_plotter/plotmodel.py @@ -110,6 +110,8 @@ class PlotModel: If True, use plot_settings.pkl file to reload settings model_path : pathlib.Path Path to model XML file or directory + default_res : int + Default resolution as the number of pixels in each direction Attributes ---------- @@ -146,7 +148,7 @@ class PlotModel: have unapplied changes """ - def __init__(self, use_settings_pkl, model_path): + def __init__(self, use_settings_pkl, model_path, default_res): """ Initialize PlotModel class attributes """ # Retrieve OpenMC Cells/Materials @@ -176,7 +178,7 @@ def __init__(self, use_settings_pkl, model_path): self.previousViews = [] self.subsequentViews = [] - self.defaultView = self.getDefaultView() + self.defaultView = self.getDefaultView(default_res) if model_path.is_file(): settings_pkl = model_path.with_name('plot_settings.pkl') @@ -230,7 +232,8 @@ def __init__(self, use_settings_pkl, model_path): self.statepoint = None self.currentView = PlotView(restore_view=view, - restore_domains=restore_domains) + restore_domains=restore_domains, + default_res=default_res) else: self.currentView = copy.deepcopy(self.defaultView) @@ -258,7 +261,7 @@ def statepoint(self, statepoint): if self._statepoint and not self._statepoint.is_open: self._statepoint.open() - def getDefaultView(self): + def getDefaultView(self, default_res): """ Generates default PlotView instance for OpenMC geometry Centers plot view origin in every dimension if possible. Defaults @@ -266,6 +269,11 @@ def getDefaultView(self): geometry. Defaults to (0, 0, 0) origin with width and heigth of 25 if geometry bounding box cannot be generated. + Parameters + ---------- + default_res : int + Default resolution as the number of pixels in each direction + Returns ------- default : PlotView instance @@ -288,7 +296,8 @@ def getDefaultView(self): else: zcenter = 0.00 - default = PlotView([xcenter, ycenter, zcenter], width, height) + default = PlotView([xcenter, ycenter, zcenter], width, height, + default_res=default_res) return default def resetColors(self): @@ -768,6 +777,8 @@ class ViewParam(openmc.lib.plot._PlotBase): Width of plot view in model units height : float Height of plot view in model units + default_res : int + Default resolution as the number of pixels in each direction Attributes ---------- @@ -789,7 +800,7 @@ class ViewParam(openmc.lib.plot._PlotBase): The universe level for the plot (default: -1 -> all universes shown) """ - def __init__(self, origin=(0, 0, 0), width=10, height=10): + def __init__(self, origin=(0, 0, 0), width=10, height=10, default_res=1000): """Initialize ViewParam attributes""" super().__init__() @@ -798,8 +809,8 @@ def __init__(self, origin=(0, 0, 0), width=10, height=10): self.origin = origin self.width = width self.height = height - self.h_res = 1000 - self.v_res = 1000 + self.h_res = default_res + self.v_res = default_res self.basis = 'xy' self.color_overlaps = False @@ -983,7 +994,7 @@ class PlotView: 'h_res', 'v_res', 'basis', 'llc', 'urc', 'color_overlaps') def __init__(self, origin=(0, 0, 0), width=10, height=10, restore_view=None, - restore_domains=False): + restore_domains=False, default_res=None): """Initialize PlotView attributes""" if restore_view is not None: @@ -991,7 +1002,10 @@ def __init__(self, origin=(0, 0, 0), width=10, height=10, restore_view=None, self.view_params = copy.copy(restore_view.view_params) else: self.view_ind = PlotViewIndependent() - self.view_params = ViewParam(origin=origin, width=width, height=height) + if default_res is not None: + self.view_params = ViewParam(origin, width, height, default_res) + else: + self.view_params = ViewParam(origin, width, height) # Get model domain info if restore_domains and restore_view is not None: From d2e124fbfb700ec2728eae36ced6eecdd5158669 Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Fri, 26 Apr 2024 06:48:20 -0500 Subject: [PATCH 2/2] Apply default_res to restored view --- openmc_plotter/__main__.py | 2 +- openmc_plotter/main_window.py | 2 +- openmc_plotter/plotmodel.py | 4 ++++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/openmc_plotter/__main__.py b/openmc_plotter/__main__.py index fc10cdb..32b1cf9 100644 --- a/openmc_plotter/__main__.py +++ b/openmc_plotter/__main__.py @@ -21,7 +21,7 @@ def main(): help='Ignore plot_settings.pkl file if present.') ap.add_argument('-s', '--threads', type=int, default=None, help='If present, number of threads used to generate plots.') - ap.add_argument('-r', '--resolution', type=int, default=1000, + ap.add_argument('-r', '--resolution', type=int, default=None, help='Default number of pixels in each direction') ap.add_argument('model_path', nargs='?', default=os.curdir, help='Location of model XML file or a directory containing ' diff --git a/openmc_plotter/main_window.py b/openmc_plotter/main_window.py index 0599c12..e107bab 100755 --- a/openmc_plotter/main_window.py +++ b/openmc_plotter/main_window.py @@ -47,7 +47,7 @@ def __init__(self, screen_size=QtCore.QSize(), model_path='.', threads=None, - resolution=1000): + resolution=None): super().__init__() self.screen = screen_size diff --git a/openmc_plotter/plotmodel.py b/openmc_plotter/plotmodel.py index 9628cbe..4c39a79 100644 --- a/openmc_plotter/plotmodel.py +++ b/openmc_plotter/plotmodel.py @@ -1000,6 +1000,10 @@ def __init__(self, origin=(0, 0, 0), width=10, height=10, restore_view=None, if restore_view is not None: self.view_ind = copy.copy(restore_view.view_ind) self.view_params = copy.copy(restore_view.view_params) + if default_res is not None: + p = self.view_params + p.h_res = default_res + p.v_res = int(default_res * p.height / p.width) else: self.view_ind = PlotViewIndependent() if default_res is not None: