From 22c2dedd1b3dc7612c384f94e627ae5137a5d353 Mon Sep 17 00:00:00 2001 From: Aseem Jain Date: Thu, 31 Mar 2022 17:41:41 +0530 Subject: [PATCH] Local surface provider. --- .../fluent/post/matplotlib/matplot_objects.py | 22 +++++++- .../matplotlib/matplot_windows_manager.py | 4 +- ansys/fluent/post/post_object_defns.py | 56 +++++++++++++++---- ansys/fluent/post/pyvista/pyvista_objects.py | 26 ++++++++- .../post/pyvista/pyvista_windows_manager.py | 10 ++-- tests/test_post.py | 17 +++++- 6 files changed, 110 insertions(+), 25 deletions(-) diff --git a/ansys/fluent/post/matplotlib/matplot_objects.py b/ansys/fluent/post/matplotlib/matplot_objects.py index 13ded664fc45..ffaf9f8ca62b 100644 --- a/ansys/fluent/post/matplotlib/matplot_objects.py +++ b/ansys/fluent/post/matplotlib/matplot_objects.py @@ -13,8 +13,17 @@ class Plots: _sessions_state = {} - def __init__(self, session): - """Instantiate Plots, container of plot objects.""" + def __init__(self, session, local_surfaces_provider=None): + """ + Instantiate Plots, container of plot objects. + + Parameters + ---------- + session : + Session object. + local_surfaces_provider : object, optional + Object providing local surfaces. + """ session_state = Plots._sessions_state.get(session.id if session else 1) if not session_state: session_state = self.__dict__ @@ -23,6 +32,13 @@ def __init__(self, session): self._init_module(self, sys.modules[__name__]) else: self.__dict__ = session_state + self._local_surfaces_provider = ( + lambda: local_surfaces_provider + if local_surfaces_provider + else self.Surfaces + if hasattr(self, "Surfaces") + else [] + ) def _init_module(self, obj, mod): for name, cls in mod.__dict__.items(): @@ -50,4 +66,6 @@ def plot(self, window_id: Optional[str] = None): Window id. If not specified unique id is used. """ + self._pre_display() matplot_windows_manager.plot(self, window_id) + self._post_display() diff --git a/ansys/fluent/post/matplotlib/matplot_windows_manager.py b/ansys/fluent/post/matplotlib/matplot_windows_manager.py index 5b3b36405931..7d072738c446 100644 --- a/ansys/fluent/post/matplotlib/matplot_windows_manager.py +++ b/ansys/fluent/post/matplotlib/matplot_windows_manager.py @@ -27,9 +27,7 @@ def __init__( ): self._closed = False self.plot_pipe, plotter_pipe = mp.Pipe() - self.plotter = ProcessPlotter( - window_id, curves, title, xlabel, ylabel - ) + self.plotter = ProcessPlotter(window_id, curves, title, xlabel, ylabel) self.plot_process = mp.Process( target=self.plotter, args=(plotter_pipe,), daemon=True ) diff --git a/ansys/fluent/post/post_object_defns.py b/ansys/fluent/post/post_object_defns.py index 4e925250a175..781115167d2a 100644 --- a/ansys/fluent/post/post_object_defns.py +++ b/ansys/fluent/post/post_object_defns.py @@ -10,7 +10,41 @@ ) -class GraphicsDefn(metaclass=PyLocalNamedObjectMetaAbstract): +class BasePostObjectDefn: + """Base class for post objects.""" + + def _pre_display(self): + local_surfaces_provider = ( + self._get_top_most_parent()._local_surfaces_provider() + ) + for surf_name in self.surfaces_list(): + if surf_name in list(local_surfaces_provider): + surf_obj = local_surfaces_provider[surf_name] + if surf_obj.surface.type() == "iso-surface": + surf_api = surf_obj._data_extractor.surface_api() + surf_api.iso_surface( + surf_obj.surface.iso_surface.field(), + surf_name, + (), + (), + surf_obj.surface.iso_surface.iso_value(), + (), + ) + + def _post_display(self): + local_surfaces_provider = ( + self._get_top_most_parent()._local_surfaces_provider() + ) + for surf_name in self.surfaces_list(): + if surf_name in list(local_surfaces_provider): + surf_obj = local_surfaces_provider[surf_name] + surf_api = surf_obj._data_extractor.surface_api() + surf_api.delete_surface(surf_name) + + +class GraphicsDefn( + BasePostObjectDefn, metaclass=PyLocalNamedObjectMetaAbstract +): """Abstract base class for graphics objects.""" @abstractmethod @@ -26,7 +60,7 @@ def display(self, plotter_id: Optional[str] = None): pass -class PlotDefn(metaclass=PyLocalNamedObjectMetaAbstract): +class PlotDefn(BasePostObjectDefn, metaclass=PyLocalNamedObjectMetaAbstract): """Abstract base class for plot objects.""" @abstractmethod @@ -106,7 +140,7 @@ def allowed_values(self): """Surface list allowed values.""" return list( self._data_extractor.field_info().get_surfaces_info().keys() - ) + ) + list(self._get_top_most_parent()._local_surfaces_provider()) class MeshDefn(GraphicsDefn): @@ -124,7 +158,7 @@ def allowed_values(self): """Surface list allowed values.""" return list( (self._data_extractor.field_info().get_surfaces_info().keys()) - ) + ) + list(self._get_top_most_parent()._local_surfaces_provider()) class show_edges(metaclass=PyLocalPropertyMeta): """Show edges for mesh.""" @@ -142,17 +176,17 @@ class show_edges(metaclass=PyLocalPropertyMeta): value: bool = True - class surface_type(metaclass=PyLocalObjectMeta): + class surface(metaclass=PyLocalObjectMeta): """Specify surface type.""" def _availability(self, name): if name == "plane_surface": - return self.surface_type() == "plane-surface" + return self.type() == "plane-surface" if name == "iso_surface": - return self.surface_type() == "iso-surface" + return self.type() == "iso-surface" return True - class surface_type(metaclass=PyLocalPropertyMeta): + class type(metaclass=PyLocalPropertyMeta): """Surface type.""" value: str = "iso-surface" @@ -251,7 +285,7 @@ def allowed_values(self): """Surfaces list allowed values.""" return list( self._data_extractor.field_info().get_surfaces_info().keys() - ) + ) + list(self._get_top_most_parent()._local_surfaces_provider()) class filled(metaclass=PyLocalPropertyMeta): """Show filled contour.""" @@ -278,7 +312,7 @@ class show_edges(metaclass=PyLocalPropertyMeta): value: bool = False - class range (metaclass=PyLocalObjectMeta): + class range(metaclass=PyLocalObjectMeta): """Specify range options.""" def _availability(self, name): @@ -407,7 +441,7 @@ def allowed_values(self): """Surface list allowed values.""" return list( self._data_extractor.field_info().get_surfaces_info().keys() - ) + ) + list(self._get_top_most_parent()._local_surfaces_provider()) class scale(metaclass=PyLocalPropertyMeta): """Vector scale.""" diff --git a/ansys/fluent/post/pyvista/pyvista_objects.py b/ansys/fluent/post/pyvista/pyvista_objects.py index dcc0025897d3..8a65cae39f50 100644 --- a/ansys/fluent/post/pyvista/pyvista_objects.py +++ b/ansys/fluent/post/pyvista/pyvista_objects.py @@ -21,8 +21,17 @@ class Graphics: _sessions_state = {} - def __init__(self, session): - """Instantiate Graphics, containter of graphics objects.""" + def __init__(self, session, local_surfaces_provider=None): + """ + Instantiate Graphics, containter of graphics objects. + + Parameters + ---------- + session : + Session object. + local_surfaces_provider : object, optional + Object providing local surfaces. + """ session_state = Graphics._sessions_state.get( session.id if session else 1 ) @@ -35,6 +44,13 @@ def __init__(self, session): self._init_module(self, sys.modules[__name__]) else: self.__dict__ = session_state + self._local_surfaces_provider = ( + lambda: local_surfaces_provider + if local_surfaces_provider + else self.Surfaces + if hasattr(self, "Surfaces") + else [] + ) def _init_module(self, obj, mod): for name, cls in mod.__dict__.items(): @@ -61,7 +77,9 @@ def display(self, window_id: Optional[str] = None): window_id : str, optional Window id. If not specified unique id is used. """ + self._pre_display() pyvista_windows_manager.plot(self, window_id) + self._post_display() class Surface(SurfaceDefn): @@ -91,7 +109,9 @@ def display(self, window_id: Optional[str] = None): window_id : str, optional Window id. If not specified unique id is used. """ + self._pre_display() pyvista_windows_manager.plot(self, window_id) + self._post_display() class Vector(VectorDefn): @@ -106,4 +126,6 @@ def display(self, window_id: Optional[str] = None): window_id : str, optional Window id. If not specified unique id is used. """ + self._pre_display() pyvista_windows_manager.plot(self, window_id) + self._post_display() diff --git a/ansys/fluent/post/pyvista/pyvista_windows_manager.py b/ansys/fluent/post/pyvista/pyvista_windows_manager.py index c0951cb8de6a..60374b7b8752 100644 --- a/ansys/fluent/post/pyvista/pyvista_windows_manager.py +++ b/ansys/fluent/post/pyvista/pyvista_windows_manager.py @@ -55,7 +55,7 @@ def plot(self): if obj.__class__.__name__ == "Mesh": self._display_mesh(obj, plotter) elif obj.__class__.__name__ == "Surface": - if obj.surface_type.surface_type() == "iso-surface": + if obj.surface.type() == "iso-surface": self._display_iso_surface(obj, plotter) elif obj.__class__.__name__ == "Contour": self._display_contour(obj, plotter) @@ -294,14 +294,14 @@ def _display_contour( def _display_iso_surface( self, obj, plotter: Union[BackgroundPlotter, pv.Plotter] ): - field = obj.surface_type.iso_surface.field() + field = obj.surface.iso_surface.field() if not field: raise RuntimeError("Iso surface definition is incomplete.") dummy_surface_name = "_dummy_iso_surface_for_pyfluent" field_info = obj._data_extractor.field_info() surfaces_list = list(field_info.get_surfaces_info().keys()) - iso_value = obj.surface_type.iso_surface.iso_value() + iso_value = obj.surface.iso_surface.iso_value() if dummy_surface_name in surfaces_list: obj._data_extractor.surface_api().delete_surface( dummy_surface_name @@ -315,7 +315,7 @@ def _display_iso_surface( if dummy_surface_name not in surfaces_list: raise RuntimeError("Iso surface creation failed.") post_session = obj._get_top_most_parent() - if obj.surface_type.iso_surface.rendering() == "mesh": + if obj.surface.iso_surface.rendering() == "mesh": mesh = post_session.Meshes[dummy_surface_name] mesh.surfaces_list = [dummy_surface_name] mesh.show_edges = True @@ -323,7 +323,7 @@ def _display_iso_surface( del post_session.Meshes[dummy_surface_name] else: contour = post_session.Contours[dummy_surface_name] - contour.field = obj.surface_type.iso_surface.field() + contour.field = obj.surface.iso_surface.field() contour.surfaces_list = [dummy_surface_name] contour.show_edges = True contour.range.auto_range_on.global_range = True diff --git a/tests/test_post.py b/tests/test_post.py index 05b479197e6e..110312def71a 100644 --- a/tests/test_post.py +++ b/tests/test_post.py @@ -231,8 +231,8 @@ def test_surface_object(): surf1 = pyvista_graphics.Surfaces["surf-1"] field_info = surf1._data_extractor.field_info() - surf1.surface_type.surface_type = "iso-surface" - iso_surf = surf1.surface_type.iso_surface + surf1.surface.type = "iso-surface" + iso_surf = surf1.surface.iso_surface assert iso_surf.field.allowed_values == [ v["solver_name"] for k, v in field_info.get_fields_info().items() @@ -258,6 +258,19 @@ def test_surface_object(): range = field_info.get_range(iso_surf.field(), True) assert range[0] == pytest.approx(iso_surf.iso_value()) + cont1 = pyvista_graphics.Contours["surf-1"] + assert "surf-1" in cont1.surfaces_list.allowed_values + + matplotlib_plots = Plots(session=None) + p1 = matplotlib_plots.XYPlots["p-1"] + assert "surf-1" not in p1.surfaces_list.allowed_values + + local_surfaces_provider = Graphics(session=None).Surfaces + matplotlib_plots = Plots( + session=None, local_surfaces_provider=local_surfaces_provider + ) + assert "surf-1" in p1.surfaces_list.allowed_values + def test_create_plot_objects(): matplotlib_plots1 = Plots(session=None)