Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/add plot for pyvista plotter #463

Merged
merged 34 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
5fe273b
build: Bump version to v0.11.dev0
prmukherj Jun 21, 2024
7da40a0
Merge branch 'main' into release/v0.11.dev0
prmukherj Jun 24, 2024
cd34356
Merge branch 'release/v0.11.dev0' of https://github.com/pyansys/pyflu…
prmukherj Jul 2, 2024
d17fb8a
Merge branch 'main' of https://github.com/pyansys/pyfluent-visualizat…
prmukherj Jul 8, 2024
fe41ed1
Merge branch 'main' of https://github.com/pyansys/pyfluent-visualizat…
prmukherj Jul 9, 2024
de52b2e
Merge branch 'main' of https://github.com/pyansys/pyfluent-visualizat…
prmukherj Jul 16, 2024
9c7a215
Merge branch 'main' of https://github.com/pyansys/pyfluent-visualizat…
prmukherj Jul 24, 2024
616dea1
Merge branch 'main' of https://github.com/pyansys/pyfluent-visualizat…
prmukherj Aug 19, 2024
30e5c5c
Merge branch 'main' of https://github.com/pyansys/pyfluent-visualizat…
prmukherj Aug 20, 2024
0d91d78
Merge branch 'main' of https://github.com/pyansys/pyfluent-visualizat…
prmukherj Aug 21, 2024
2a1d92f
Merge branch 'main' of https://github.com/pyansys/pyfluent-visualizat…
prmukherj Aug 28, 2024
03be4f5
Merge branch 'main' of https://github.com/pyansys/pyfluent-visualizat…
prmukherj Sep 5, 2024
14c2013
Merge branch 'main' of https://github.com/pyansys/pyfluent-visualizat…
prmukherj Sep 12, 2024
1c410d7
Merge branch 'main' of https://github.com/pyansys/pyfluent-visualizat…
prmukherj Sep 18, 2024
03972ed
Merge branch 'main' of https://github.com/pyansys/pyfluent-visualizat…
prmukherj Oct 8, 2024
0850f17
Merge branch 'main' of https://github.com/pyansys/pyfluent-visualizat…
prmukherj Oct 10, 2024
fab2277
Merge branch 'main' of https://github.com/pyansys/pyfluent-visualizat…
prmukherj Oct 17, 2024
05faa31
Merge branch 'main' of https://github.com/pyansys/pyfluent-visualizat…
prmukherj Nov 5, 2024
d363cb1
Merge branch 'main' of https://github.com/pyansys/pyfluent-visualizat…
prmukherj Nov 28, 2024
b9db616
Merge branch 'main' of https://github.com/pyansys/pyfluent-visualizat…
prmukherj Nov 28, 2024
4b62bf5
Merge branch 'main' of https://github.com/pyansys/pyfluent-visualizat…
prmukherj Nov 29, 2024
c5a88e9
feat: add 2d plots via pyvista.
prmukherj Dec 5, 2024
5d94ecf
Fix import issues.
prmukherj Dec 18, 2024
47c5075
Fix.
prmukherj Dec 31, 2024
166d05b
Feat/add plotly for plots (#466)
prmukherj Jan 1, 2025
6a0e602
Updates.
prmukherj Jan 3, 2025
b09f21a
Fix plotter.
prmukherj Jan 3, 2025
092e536
Update dependencies.
prmukherj Jan 3, 2025
0929a41
Update example.
prmukherj Jan 3, 2025
3239e7b
Merge branch 'main' into feat/add_plot_for_pyvista_plotter
prmukherj Jan 6, 2025
bed0b66
Update test.
prmukherj Jan 6, 2025
ff071f3
Merge branch 'feat/add_plot_for_pyvista_plotter' of https://github.co…
prmukherj Jan 6, 2025
54a010f
Restrict vtk version.
prmukherj Jan 6, 2025
6ee6ca3
Upgrade pyfluent version.
prmukherj Jan 6, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions examples/00-postprocessing/updated_exhaust_manifold_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
Mesh,
Monitor,
Pathline,
PlotterWindow,
Surface,
Vector,
XYPlot,
Expand Down Expand Up @@ -252,8 +251,8 @@
surfaces=["outlet"],
y_axis_function="temperature",
)
p7 = PlotterWindow(grid=(2, 2))
p7.add_plots(xy_plot, position=(0, 0))
p7 = GraphicsWindow(grid=(2, 2))
p7.add_graphics(xy_plot, position=(0, 0))

###############################################################################
# Create residual plot
Expand All @@ -262,7 +261,7 @@

residual = Monitor(solver=solver_session)
residual.monitor_set_name = "residual"
p7.add_plots(residual, position=(0, 1))
p7.add_graphics(residual, position=(0, 1))

###############################################################################
# Solve and plot solution monitors
Expand All @@ -274,10 +273,10 @@

mass_bal_rplot = Monitor(solver=solver_session)
mass_bal_rplot.monitor_set_name = "mass-bal-rplot"
p7.add_plots(mass_bal_rplot, position=(1, 0))
p7.add_graphics(mass_bal_rplot, position=(1, 0))

point_vel_rplot = Monitor(solver=solver_session, monitor_set_name="point-vel-rplot")
p7.add_plots(point_vel_rplot, position=(1, 1))
p7.add_graphics(point_vel_rplot, position=(1, 1))
p7.show()

###############################################################################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@

from enum import Enum
import itertools
import os
import threading
from typing import Dict, List, Optional, Union

from ansys.fluent.core.fluent_connection import FluentConnection
from ansys.fluent.core.post_objects.check_in_notebook import in_notebook
from ansys.fluent.core.post_objects.post_object_definitions import GraphicsDefn
from ansys.fluent.core.post_objects.post_object_definitions import (
GraphicsDefn,
PlotDefn,
)
from ansys.fluent.core.post_objects.singleton_meta import AbstractSingletonMeta
import numpy as np
import pyvista as pv
from pyvistaqt import BackgroundPlotter

from ansys.fluent.visualization import get_config
from ansys.fluent.visualization.post_data_extractor import FieldDataExtractor
from ansys.fluent.visualization.post_data_extractor import (
FieldDataExtractor,
XYPlotDataExtractor,
)
from ansys.fluent.visualization.post_windows_manager import (
PostWindow,
PostWindowsManager,
Expand Down Expand Up @@ -74,6 +81,10 @@ def fetch(self):
obj = self.post_object
if obj.__class__.__name__ == "Surface":
self._fetch_surface(obj)
elif obj.__class__.__name__ == "XYPlot":
self._fetch_xy_data(obj)
elif obj.__class__.__name__ == "MonitorPlot":
self._fetch_monitor_data(obj)
else:
self._fetch_data(obj, FieldDataType(obj.__class__.__name__))

Expand Down Expand Up @@ -106,6 +117,10 @@ def _render_graphics(self, position=(0, 0), opacity=1):
self._display_vector(obj, position, opacity)
elif obj.__class__.__name__ == "Pathlines":
self._display_pathlines(obj, position, opacity)
elif obj.__class__.__name__ == "XYPlot":
self._display_xy_plot(position, opacity)
elif obj.__class__.__name__ == "MonitorPlot":
self._display_monitor_plot(position, opacity)
if self.animate:
self.renderer.write_frame()
self.renderer._set_camera(get_config()["set_view_on_display"])
Expand Down Expand Up @@ -162,6 +177,31 @@ def _fetch_or_display_surface(self, obj, fetch: bool, position=[0, 0], opacity=1
def _fetch_surface(self, obj):
self._fetch_or_display_surface(obj, fetch=True)

def _fetch_xy_data(self, obj):
self._data["XYPlot"] = XYPlotDataExtractor(obj).fetch_data()
self._data["XYPlot"]["properties"] = {
"curves": list(self._data["XYPlot"]),
"title": "XY Plot",
"xlabel": "position",
"ylabel": obj.y_axis_function(),
}

def _fetch_monitor_data(self, obj):
monitors = obj._api_helper.monitors
indices, columns_data = monitors.get_monitor_set_data(obj.monitor_set_name())
xy_data = {}
for column_name, column_data in columns_data.items():
xy_data[column_name] = {"xvalues": indices, "yvalues": column_data}
monitor_set_name = obj.monitor_set_name()
self._data["MonitorPlot"] = xy_data
self._data["MonitorPlot"]["properties"] = {
"curves": list(xy_data.keys()),
"title": monitor_set_name,
"xlabel": monitors.get_monitor_set_prop(monitor_set_name, "xlabel"),
"ylabel": monitors.get_monitor_set_prop(monitor_set_name, "ylabel"),
"yscale": "log" if monitor_set_name == "residual" else "linear",
}

def _resolve_mesh_data(self, mesh_data):
topology = "line" if mesh_data["faces"][0] == 2 else "face"
if topology == "line":
Expand Down Expand Up @@ -412,6 +452,18 @@ def _display_mesh(self, obj, position=(0, 0), opacity=1):
opacity=opacity,
)

def _display_xy_plot(self, position=(0, 0), opacity=1):
self.renderer.render(
self._data["XYPlot"],
position=position,
)

def _display_monitor_plot(self, position=(0, 0), opacity=1):
self.renderer.render(
self._data["MonitorPlot"],
position=position,
)

def _get_refresh_for_plotter(self, window: "GraphicsWindow"):
def refresh():
with GraphicsWindowsManager._condition:
Expand Down Expand Up @@ -498,7 +550,11 @@ def open_window(
with self._condition:
if not window_id:
window_id = self._get_unique_window_id()
if in_notebook() or get_config()["blocking"]:
if (
in_notebook()
or get_config()["blocking"]
or os.getenv("FLUENT_PROD_DIR")
):
self._open_window_notebook(window_id, grid)
else:
self._open_and_plot_console(None, window_id, grid=grid)
Expand Down Expand Up @@ -593,10 +649,14 @@ def add_graphics(
RuntimeError
If the window does not support the object.
"""
if not isinstance(object, GraphicsDefn):
if not isinstance(object, (GraphicsDefn, PlotDefn)):
raise RuntimeError("Object type currently not supported.")
with self._condition:
if in_notebook() or get_config()["blocking"]:
if (
in_notebook()
or get_config()["blocking"]
or os.getenv("FLUENT_PROD_DIR")
):
self._add_graphics_in_notebook(
object, window_id, fetch_data, overlay, position, opacity
)
Expand All @@ -608,7 +668,11 @@ def add_graphics(
def show_graphics(self, window_id: str):
"""Display the graphics window."""
with self._condition:
if in_notebook() or get_config()["blocking"]:
if (
in_notebook()
or get_config()["blocking"]
or os.getenv("FLUENT_PROD_DIR")
):
self._show_graphics_in_notebook(window_id)

def save_graphic(
Expand Down
59 changes: 55 additions & 4 deletions src/ansys/fluent/visualization/graphics/pyvista/graphics_defns.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Module for pyVista windows management."""

import numpy as np
import pyvista as pv
from pyvistaqt import BackgroundPlotter

Expand All @@ -14,7 +15,7 @@ def __init__(
non_interactive: bool,
grid: tuple | None = (1, 1),
):
self.plotter: Union[BackgroundPlotter, pv.Plotter] = (
self.plotter: BackgroundPlotter | pv.Plotter = (
pv.Plotter(title=f"PyFluent ({win_id})", shape=grid)
if in_notebook or non_interactive
else BackgroundPlotter(title=f"PyFluent ({win_id})", shape=grid)
Expand Down Expand Up @@ -59,7 +60,7 @@ def _scalar_bar_default_properties(self) -> dict:

def _clear_plotter(self, in_notebook):
if in_notebook and self.plotter.theme._jupyter_backend == "pythreejs":
self.plotter.remove_actor(plotter.renderer.actors.copy())
self.plotter.remove_actor(self.plotter.renderer.actors.copy())
else:
self.plotter.clear()

Expand All @@ -83,13 +84,63 @@ def render(self, mesh, **kwargs):

Parameters
----------
mesh : pyvista.DataSet
mesh : pyvista.DataSet | dict
Any PyVista or VTK mesh is supported.
"""
if "position" in kwargs:
self.plotter.subplot(kwargs["position"][0], kwargs["position"][1])
del kwargs["position"]
self.plotter.add_mesh(mesh, **kwargs)
if isinstance(mesh, pv.DataSet):
self.plotter.add_mesh(mesh, **kwargs)
else:
y_range = None
chart = pv.Chart2D()
chart.title = mesh["properties"].get("title") or ""
chart.x_label = mesh["properties"].get("xlabel") or ""
chart.y_label = mesh["properties"].get("ylabel") or ""
if mesh["properties"].get("yscale") == "log":
chart.y_axis.log_scale = True
y_range = 0
del mesh["properties"]

color_list = ["b", "r", "g", "c", "m", "y", "k"]
style_list = ["-", "--", "-.", "-.."]

min_y_value = max_y_value = min_x_value = max_x_value = None
for count, curve in enumerate(mesh):
chart.line(
mesh[curve]["xvalues"].tolist(),
mesh[curve]["yvalues"].tolist(),
width=2.5,
color=color_list[count % len(color_list)],
style=style_list[count % len(style_list)],
label=curve,
)
min_y_value = (
min(np.amin(mesh[curve]["yvalues"]), min_y_value)
if min_y_value
else np.amin(mesh[curve]["yvalues"])
)
max_y_value = (
max(np.amax(mesh[curve]["yvalues"]), max_y_value)
if max_y_value
else np.amax(mesh[curve]["yvalues"])
)
min_x_value = (
min(np.amin(mesh[curve]["xvalues"]), min_x_value)
if min_x_value
else np.amin(mesh[curve]["xvalues"])
)
max_x_value = (
max(np.amax(mesh[curve]["xvalues"]), max_x_value)
if max_x_value
else np.amax(mesh[curve]["xvalues"])
)
chart.x_range = [min_x_value, max_x_value]
if y_range is None:
y_range = max_y_value - min_y_value
chart.y_range = [min_y_value - y_range * 0.2, max_y_value + y_range * 0.2]
self.plotter.add_chart(chart, **kwargs)

def save_graphic(self, file_name: str):
"""Save graphics to the specified file.
Expand Down
Loading