Skip to content

Commit

Permalink
Plane surface creation.
Browse files Browse the repository at this point in the history
  • Loading branch information
ajain-work committed Apr 8, 2022
1 parent b43cbdd commit e7365c2
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 57 deletions.
85 changes: 78 additions & 7 deletions ansys/fluent/core/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,85 @@
from ansys.fluent.core.services.datamodel_tui import PyMenu


class LocalObjectDataExtractor:
class _LocalObjectDataExtractor:
"""Class to extract data for local objects."""

class _SurfaceAPI:
def __init__(self, obj):
self.obj = obj
self._surface_name_on_server = self.surface_name_in_server(
obj._name
)

@staticmethod
def surface_name_in_server(local_surface_name):
return "_dummy_surface_for_pyfluent:" + local_surface_name

def _get_api_handle(self):
return self.obj._get_top_most_parent().session.tui.solver.surface

def _delete_if_exist_on_server(self):
field_info = self.obj._data_extractor.field_info()
surfaces_list = list(field_info.get_surfaces_info().keys())
if self._surface_name_on_server in surfaces_list:
self.delete_surface_on_server()

def create_surface_on_server(self):
if self.obj.surface.type() == "iso-surface":
iso_surface = self.obj.surface.iso_surface
field = iso_surface.field()
iso_value = iso_surface.iso_value()
if not field:
raise RuntimeError("Iso surface definition is incomplete.")
self._delete_if_exist_on_server()
self._get_api_handle().iso_surface(
field, self._surface_name_on_server, (), (), iso_value, ()
)
elif self.obj.surface.type() == "plane-surface":
plane_surface = self.obj.surface.plane_surface
xy_plane = plane_surface.xy_plane
yz_plane = plane_surface.yz_plane
zx_plane = plane_surface.zx_plane
self._delete_if_exist_on_server()
self._get_api_handle().plane_surface(
self._surface_name_on_server,
"xy-plane"
if xy_plane
else "yz-plane"
if yz_plane
else "zx-plane",
xy_plane.z()
if xy_plane
else yz_plane.x()
if yz_plane
else zx_plane.y(),
)
field_info = self.obj._data_extractor.field_info()
surfaces_list = list(field_info.get_surfaces_info().keys())
if self._surface_name_on_server not in surfaces_list:
raise RuntimeError("Surface creation failed.")

def delete_surface_on_server(self):
self._get_api_handle().delete_surface(self._surface_name_on_server)

def __init__(self, obj):
self.obj = obj
self.field_info = lambda: obj._get_top_most_parent().session.field_info
self.field_data = lambda: obj._get_top_most_parent().session.field_data
self.surface_api = (
lambda: obj._get_top_most_parent().session.tui.solver.surface
)
self.id = lambda: obj._get_top_most_parent().session.id
if obj.__class__.__name__ == "Surface":
self.surface_api = _LocalObjectDataExtractor._SurfaceAPI(obj)

def remote_surface_name(self, local_surface_name):
local_surfaces_provider = (
self.obj._get_top_most_parent()._local_surfaces_provider()
)
if local_surface_name in list(local_surfaces_provider):
return _LocalObjectDataExtractor._SurfaceAPI.surface_name_in_server(
local_surface_name
)
else:
return local_surface_name


class Attribute:
Expand Down Expand Up @@ -167,7 +238,7 @@ def wrapper(self, value):
@classmethod
def __create_init(cls):
def wrapper(self, parent):
self._data_extractor = LocalObjectDataExtractor(self)
self._data_extractor = _LocalObjectDataExtractor(self)
self._parent = parent
self._on_change_cbs = []
annotations = self.__class__.__dict__.get("__annotations__")
Expand Down Expand Up @@ -239,7 +310,7 @@ class PyLocalObjectMeta(PyLocalBaseMeta):
def __create_init(cls):
def wrapper(self, parent):
self._parent = parent
self._data_extractor = LocalObjectDataExtractor(self)
self._data_extractor = _LocalObjectDataExtractor(self)

def update(clss):
for name, cls in clss.__dict__.items():
Expand Down Expand Up @@ -376,7 +447,7 @@ class PyLocalNamedObjectMeta(PyLocalObjectMeta):
def __create_init(cls):
def wrapper(self, name, parent):
self._name = name
self._data_extractor = LocalObjectDataExtractor(self)
self._data_extractor = _LocalObjectDataExtractor(self)
self._parent = parent

def update(clss):
Expand Down
4 changes: 3 additions & 1 deletion ansys/fluent/post/matplotlib/matplot_windows_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ def _get_xy_plot_data(self):
surfaces_info = field_info.get_surfaces_info()
surface_ids = [
id
for surf in obj.surfaces_list()
for surf in map(
obj._data_extractor.remote_surface_name, obj.surfaces_list()
)
for id in surfaces_info[surf]["surface_id"]
]
# get scalar field data
Expand Down
80 changes: 68 additions & 12 deletions ansys/fluent/post/post_object_defns.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,8 @@ def _pre_display(self):
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(),
(),
)
surf_api = surf_obj._data_extractor.surface_api
surf_api.create_surface_on_server()

def _post_display(self):
local_surfaces_provider = (
Expand All @@ -38,8 +30,8 @@ def _post_display(self):
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)
surf_api = surf_obj._data_extractor.surface_api
surf_api.delete_surface_on_server()


class GraphicsDefn(
Expand Down Expand Up @@ -196,6 +188,70 @@ def allowed_values(self):
class plane_surface(metaclass=PyLocalObjectMeta):
"""Plane surface data."""

def _availability(self, name):
if name == "xy_plane":
return self.creation_method() == "xy-plane"
if name == "yz_plane":
return self.creation_method() == "yz-plane"
if name == "zx_plane":
return self.creation_method() == "zx-plane"
return True

class creation_method(metaclass=PyLocalPropertyMeta):
"""Creation Method."""

value: str = "xy-plane"

@Attribute
def allowed_values(self):
"""Surface type allowed values."""
return ["xy-plane", "yz-plane", "zx-plane"]

class xy_plane(metaclass=PyLocalObjectMeta):
"""XY Plane."""

class z(metaclass=PyLocalPropertyMeta):
"""Z value."""

value: float = 0

@Attribute
def range(self):
"""Z value range."""
return self._data_extractor.field_info().get_range(
"z-coordinate", True
)

class yz_plane(metaclass=PyLocalObjectMeta):
"""YZ Plane."""

class x(metaclass=PyLocalPropertyMeta):
"""X value."""

value: float = 0

@Attribute
def range(self):
"""X value range."""
return self._data_extractor.field_info().get_range(
"x-coordinate", True
)

class zx_plane(metaclass=PyLocalObjectMeta):
"""ZX Plane."""

class y(metaclass=PyLocalPropertyMeta):
"""Y value."""

value: float = 0

@Attribute
def range(self):
"""Y value range."""
return self._data_extractor.field_info().get_range(
"y-coordinate", True
)

class iso_surface(metaclass=PyLocalObjectMeta):
"""Iso surface data."""

Expand Down
65 changes: 28 additions & 37 deletions ansys/fluent/post/pyvista/pyvista_windows_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ def plot(self):
if obj.__class__.__name__ == "Mesh":
self._display_mesh(obj, plotter)
elif obj.__class__.__name__ == "Surface":
if obj.surface.type() == "iso-surface":
self._display_iso_surface(obj, plotter)
self._display_surface(obj, plotter)
elif obj.__class__.__name__ == "Contour":
self._display_contour(obj, plotter)
elif obj.__class__.__name__ == "Vector":
Expand Down Expand Up @@ -101,7 +100,9 @@ def _display_vector(
surfaces_info = field_info.get_surfaces_info()
surface_ids = [
id
for surf in obj.surfaces_list()
for surf in map(
obj._data_extractor.remote_surface_name, obj.surfaces_list()
)
for id in surfaces_info[surf]["surface_id"]
]

Expand Down Expand Up @@ -189,7 +190,9 @@ def _display_contour(
surfaces_info = field_info.get_surfaces_info()
surface_ids = [
id
for surf in obj.surfaces_list()
for surf in map(
obj._data_extractor.remote_surface_name, obj.surfaces_list()
)
for id in surfaces_info[surf]["surface_id"]
]
# get scalar field data
Expand Down Expand Up @@ -294,45 +297,31 @@ def _display_contour(
):
plotter.add_mesh(mesh.contour(isosurfaces=20))

def _display_iso_surface(
def _display_surface(
self, obj, plotter: Union[BackgroundPlotter, pv.Plotter]
):
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.iso_surface.iso_value()
if dummy_surface_name in surfaces_list:
obj._data_extractor.surface_api().delete_surface(
dummy_surface_name
)

obj._data_extractor.surface_api().iso_surface(
field, dummy_surface_name, (), (), iso_value, ()
)

surfaces_list = list(field_info.get_surfaces_info().keys())
if dummy_surface_name not in surfaces_list:
raise RuntimeError("Iso surface creation failed.")
surface_api = obj._data_extractor.surface_api
surface_api.create_surface_on_server()
dummy_object = "dummy_object"
post_session = obj._get_top_most_parent()
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]
if (
obj.surface.type() == "iso-surface"
and obj.surface.iso_surface.rendering() == "contour"
):
contour = post_session.Contours[dummy_object]
contour.field = obj.surface.iso_surface.field()
contour.surfaces_list = [dummy_surface_name]
contour.surfaces_list = [obj._name]
contour.show_edges = True
contour.range.auto_range_on.global_range = True
self._display_contour(contour, plotter)
del post_session.Contours[dummy_surface_name]
obj._data_extractor.surface_api().delete_surface(dummy_surface_name)
del post_session.Contours[dummy_object]
else:
mesh = post_session.Meshes[dummy_object]
mesh.surfaces_list = [obj._name]
mesh.show_edges = True
self._display_mesh(mesh, plotter)
del post_session.Meshes[dummy_object]
surface_api.delete_surface_on_server()

def _display_mesh(
self, obj, plotter: Union[BackgroundPlotter, pv.Plotter]
Expand All @@ -344,7 +333,9 @@ def _display_mesh(
surfaces_info = field_info.get_surfaces_info()
surface_ids = [
id
for surf in obj.surfaces_list()
for surf in map(
obj._data_extractor.remote_surface_name, obj.surfaces_list()
)
for id in surfaces_info[surf]["surface_id"]
]
surfaces_data = field_data.get_surfaces(surface_ids)
Expand Down

0 comments on commit e7365c2

Please sign in to comment.