Skip to content

Commit

Permalink
Local surface provider. (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajain-work authored Apr 1, 2022
1 parent ae2287d commit c8ec1ef
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 25 deletions.
22 changes: 20 additions & 2 deletions ansys/fluent/post/matplotlib/matplot_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand All @@ -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():
Expand Down Expand Up @@ -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()
4 changes: 1 addition & 3 deletions ansys/fluent/post/matplotlib/matplot_windows_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
56 changes: 45 additions & 11 deletions ansys/fluent/post/post_object_defns.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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."""
Expand All @@ -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"
Expand Down Expand Up @@ -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."""
Expand All @@ -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):
Expand Down Expand Up @@ -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."""
Expand Down
26 changes: 24 additions & 2 deletions ansys/fluent/post/pyvista/pyvista_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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():
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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()
10 changes: 5 additions & 5 deletions ansys/fluent/post/pyvista/pyvista_windows_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -315,15 +315,15 @@ 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
self._display_mesh(mesh, plotter)
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
Expand Down
17 changes: 15 additions & 2 deletions tests/test_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down

0 comments on commit c8ec1ef

Please sign in to comment.