-
Notifications
You must be signed in to change notification settings - Fork 9
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
Expose streamlines #480
Merged
Merged
Expose streamlines #480
Changes from 7 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7cf822b
Small typing fix
PProfizi b1ab8b1
Draft of ansys.dpf.post.tools.streamlines.plot_streamlines()
PProfizi ba53655
Apply suggestions from code review
PProfizi 9557ca3
Merge branch 'master' into feat/expose_streamlines
PProfizi 288fefd
Implement remarks on sources definition
PProfizi 6b8cdab
Remove __init__.py from ansys/dpf/post/tools
PProfizi bae706b
Revert "Remove __init__.py from ansys/dpf/post/tools"
PProfizi 522aca1
Add a conftest.py for Doctest
PProfizi f5cc283
Rename sub-package to "helpers" as tools.py already exists in ansys/d…
PProfizi 2553d5d
Merge branch 'master' into feat/expose_streamlines
PProfizi 11c6764
Remove debug
PProfizi d5577b1
Update pre-commit
PProfizi f317054
Move the select method to a new helpers/selections module, ensuring r…
PProfizi d67a405
Handle multi-zone with a merge step, and add a set_id argument to sel…
PProfizi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
"""Tools package. | ||
|
||
Tools | ||
----- | ||
This package regroups helpers for different common post-treatment functionalities. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
"""Module containing the helpers for streamlines. | ||
|
||
Streamlines | ||
----------- | ||
|
||
""" | ||
from typing import List, Union | ||
|
||
from ansys.dpf.core.helpers import streamlines as core_streamlines | ||
from ansys.dpf.core.plotter import DpfPlotter | ||
|
||
from ansys.dpf import post | ||
|
||
|
||
def plot_streamlines( | ||
dataframe: post.DataFrame, | ||
sources: List[dict], | ||
streamline_thickness: Union[float, List[float]] = 0.01, | ||
plot_mesh: bool = True, | ||
mesh_opacity: float = 0.3, | ||
plot_contour: bool = True, | ||
contour_opacity: float = 0.3, | ||
**kwargs, | ||
): | ||
"""Plot streamlines based on a vector field DataFrame. | ||
|
||
Parameters | ||
---------- | ||
dataframe: | ||
A `post.DataFrame` object containing a vector field. | ||
sources: | ||
A list of dictionaries defining spherical point sources for the streamlines. | ||
Expected keywords are "center", "radius", "max_time" and "n_points". | ||
Keyword "max_time" is for the maximum integration pseudo-time for the streamline | ||
computation algorithm, which defines the final length of the lines. | ||
More information is available at :func:`pyvista.DataSetFilters.streamlines`. | ||
streamline_thickness: | ||
Thickness of the streamlines plotted. Use a list to specify a value for each source. | ||
plot_contour: | ||
Whether to plot the field's norm contour along with the streamlines. | ||
contour_opacity: | ||
Opacity to use for the field contour in case "plot_contour=True". | ||
plot_mesh: | ||
Whether to plot the mesh along the streamlines in case "plot_contour=False". | ||
mesh_opacity: | ||
Opacity to use for the mesh in case "plot_contour=False" and "plot_mesh=True". | ||
**kwargs: | ||
|
||
""" | ||
# Select data to work with | ||
meshed_region = dataframe._fc[0].meshed_region | ||
field = dataframe._fc[0] | ||
|
||
# Initialize the plotter | ||
plt = DpfPlotter(**kwargs) | ||
|
||
if plot_contour: | ||
plt.add_field(field=field, opacity=contour_opacity) | ||
elif plot_mesh: | ||
plt.add_mesh(meshed_region=meshed_region, opacity=mesh_opacity) | ||
if not isinstance(streamline_thickness, list): | ||
streamline_thickness = [streamline_thickness] * len(sources) | ||
# Add streamlines for each source | ||
for i, source in enumerate(sources): | ||
pv_streamline, pv_source = core_streamlines.compute_streamlines( | ||
meshed_region=meshed_region, | ||
field=field, | ||
return_source=True, | ||
source_radius=source["radius"], | ||
source_center=source["center"], | ||
n_points=source["n_points"] if "n_points" in source else 100, | ||
max_time=source["max_time"] if "max_time" in source else None, | ||
) | ||
plt.add_streamlines( | ||
pv_streamline, source=pv_source, radius=streamline_thickness[i] | ||
) | ||
|
||
plt.show_figure(**kwargs) | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from conftest import SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_7_0 | ||
import pytest | ||
|
||
from ansys.dpf import post | ||
from ansys.dpf.post import examples | ||
from ansys.dpf.post.tools import streamlines | ||
|
||
|
||
@pytest.mark.skipif( | ||
not SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_7_0, | ||
reason="Fluid capabilities added with ansys-dpf-server 2024.1.pre0.", | ||
) | ||
class TestStreamlines: | ||
@pytest.fixture | ||
def simulation(self) -> post.FluidSimulation: | ||
files_cfx = examples.download_cfx_heating_coil() | ||
return post.FluidSimulation(cas=files_cfx["cas"], dat=files_cfx["dat"]) # noqa | ||
|
||
def test_plot_streamlines(self, simulation): | ||
import pyvista as pv | ||
|
||
pv.OFF_SCREEN = False | ||
dataframe = simulation.velocity(times=[0.0], zone_ids=[5]) | ||
print(dataframe) | ||
sources = [ | ||
{"radius": 0.25, "center": (0.75, 0.0, 0.0), "n_points": 20}, | ||
{ | ||
"radius": 0.25, | ||
"center": (0.0, 0.75, 0.0), | ||
"n_points": 5, | ||
"max_time": 10.0, | ||
}, | ||
{"radius": 0.25, "center": (-0.75, 0.0, 0.0), "max_time": 2.0}, | ||
{"radius": 0.25, "center": (0.0, -0.75, 0.0)}, | ||
] | ||
streamlines.plot_streamlines( | ||
dataframe=dataframe, | ||
sources=sources, | ||
streamline_thickness=0.007, | ||
plot_mesh=True, | ||
mesh_opacity=0.2, | ||
plot_contour=True, | ||
contour_opacity=0.3, | ||
title="Streamlines with multiple sources", | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why the first field? what if you have a single time, but different label (like "elshape") in your data frame?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cbellot000 talked it out and it seems the only use-case is merging the fields labeled by zone, as well as selecting a set.
I updated the PR accordingly.