-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[gui] Fluid visualization utilities (#7682)
Issue: # ## Brief Summary This PR implemented several visualization utilities to help simulation users to quickly visualize their Scalar/Vector fields. - `contour()` under the `Canvas` class to plot a scalar field in GGUI - `vector_field()` method under the `Canvas` class to plot a 2D vector field in GGUI - `write_vtk()` method that can output a 2D/3D scalar field into a `.vtr` file for visualization - `contour()` and `vector_field()` methods in `ti.GUI` just for completeness; the user should be aware that `ti.GUI` will be deprecated in the future in favor of GGUI. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
5d2b4ba
commit a7f3bf0
Showing
5 changed files
with
207 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import numpy as np | ||
|
||
|
||
def write_vtk(scalar_field, filename): | ||
try: | ||
from pyevtk.hl import \ | ||
gridToVTK # pylint: disable=import-outside-toplevel | ||
except ImportError: | ||
raise RuntimeError('Failed to import pyevtk. Please install it via /\ | ||
`pip install pyevtk` first. ') | ||
|
||
scalar_field_np = scalar_field.to_numpy() | ||
field_shape = scalar_field_np.shape | ||
dimensions = len(field_shape) | ||
|
||
if dimensions not in (2, 3): | ||
raise ValueError("The input field must be a 2D or 3D scalar field.") | ||
|
||
if dimensions == 2: | ||
scalar_field_np = scalar_field_np[np.newaxis, :, :] | ||
zcoords = np.array([0, 1]) | ||
elif dimensions == 3: | ||
zcoords = np.arange(0, field_shape[2]) | ||
gridToVTK(filename, | ||
x=np.arange(0, field_shape[0]), | ||
y=np.arange(0, field_shape[1]), | ||
z=zcoords, | ||
cellData={filename: scalar_field_np}) | ||
|
||
|
||
__all__ = ['write_vtk'] |
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 |
---|---|---|
|
@@ -14,3 +14,4 @@ setproctitle | |
nbmake | ||
marko | ||
PyYAML | ||
pyevtk |