Skip to content

Commit db064a9

Browse files
authored
Fix fields container animate key for Linux (#585)
* WIP * WIP * Fix error in deformation field for fields_container.py::animate * Fix error in deformation field for fields_container.py::animate * Update example due to fix * Add key_event mapped to "a" to update the scene instead of closing. * Call animation() also when off_screen * Take norm of multi_component fields * Fix flake8 * Test the pv.OFFSCREEN value in cases of batch run * Specify in the example that results must be transient
1 parent 37c4b47 commit db064a9

File tree

3 files changed

+50
-27
lines changed

3 files changed

+50
-27
lines changed

ansys/dpf/core/animator.py

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ def animate_workflow(self, loop_over, workflow, output_name, input_name="loop_ov
6666
if cpos:
6767
if isinstance(cpos[0][0], float):
6868
cpos = [cpos]*len(indices)
69+
str_template = "t={0:{2}} {1}"
6970

70-
def render_field(frame):
71+
def render_frame(frame):
7172
self._plotter.clear()
7273
# print(f"render frame {frame} for input {indices[frame]}")
7374
workflow.connect(input_name, [frame])
@@ -80,39 +81,51 @@ def render_field(frame):
8081
**kwargs)
8182
kwargs_in = _sort_supported_kwargs(
8283
bound_method=self._plotter.add_text, **freq_kwargs)
83-
str_template = "t={0:{2}} {1}"
8484
self._plotter.add_text(str_template.format(loop_over.data[frame], unit, freq_fmt),
8585
**kwargs_in)
8686
if cpos:
8787
self._plotter.camera_position = cpos[frame]
8888

8989
try:
90-
# Write initial frame
91-
render_field(0)
92-
# If not off_screen, enable the user to choose the camera position
93-
if not kwargs.pop("off_screen", None):
94-
print('Orient the view, then press "q" to close the window '
95-
'and produce an animation')
96-
# Show is necessary even when off_screen to initiate the renderer
97-
result = self.show_figure(auto_close=False, **kwargs)
98-
if save_as:
99-
try:
100-
self._plotter.write_frame()
101-
except AttributeError as e: # pragma: no cover
102-
if "To retrieve an image after the render window has been closed" in e.args[0]:
103-
print("Animation canceled.")
104-
return result
105-
# For each additional frame requested
106-
if len(indices) > 1:
107-
for frame in range(1, len(indices)):
90+
def animation():
91+
if save_as:
10892
try:
109-
render_field(frame)
93+
self._plotter.write_frame()
11094
except AttributeError as e: # pragma: no cover
111-
if "'NoneType' object has no attribute 'interactor'" in e.args[0]:
95+
if "To retrieve an image after the render window has been closed" \
96+
in e.args[0]:
11297
print("Animation canceled.")
98+
print(e)
11399
return result
114-
if save_as:
115-
self._plotter.write_frame()
100+
# For each additional frame requested
101+
if len(indices) > 1:
102+
for frame in range(1, len(indices)):
103+
try:
104+
render_frame(frame)
105+
except AttributeError as e: # pragma: no cover
106+
if "'NoneType' object has no attribute 'interactor'" in e.args[0]:
107+
print("Animation canceled.")
108+
return result
109+
if save_as:
110+
self._plotter.write_frame()
111+
112+
# Write initial frame
113+
render_frame(0)
114+
# If not off_screen, enable the user to choose the camera position
115+
off_screen = kwargs.pop("off_screen", None)
116+
if off_screen is None:
117+
import pyvista as pv
118+
off_screen = pv.OFF_SCREEN
119+
120+
if not off_screen:
121+
self._plotter.add_key_event("a", animation)
122+
print('Orient the view, then press "a" to produce an animation')
123+
else:
124+
animation()
125+
# Show is necessary even when off_screen to initiate the renderer
126+
result = self._plotter.show(interactive=True)
127+
# result = self.show_figure(auto_close=False, **kwargs)
128+
# result = self._plotter.show()
116129
except Exception as e: # pragma: no cover
117130
print(e)
118131
raise

ansys/dpf/core/fields_container.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,16 @@ def animate(self, save_as=None, deform_by=None, scale_factor=1.0, **kwargs):
516516
wf.set_input_name("loop_over", forward_index.inputs.any)
517517
# Define the field extraction using the fields_container and indices
518518
extract_field_op = dpf.core.operators.utility.extract_field(self)
519+
to_render = extract_field_op.outputs.field
520+
n_components = self[0].component_count
521+
if n_components > 1:
522+
norm_op = dpf.core.operators.math.norm(extract_field_op.outputs.field)
523+
to_render = norm_op.outputs.field
524+
525+
loop_over = self.get_time_scoping()
526+
frequencies = self.time_freq_support.time_frequencies
527+
if frequencies is None:
528+
raise ValueError("The fields_container has no time_frequencies.")
519529

520530
loop_over = self.get_time_scoping()
521531
frequencies = self.time_freq_support.time_frequencies
@@ -535,7 +545,7 @@ def animate(self, save_as=None, deform_by=None, scale_factor=1.0, **kwargs):
535545
if deform_by is None or isinstance(deform_by, bool):
536546
# By default, set deform_by as self if nodal 3D vector field
537547
if self[0].location == dpf.core.common.locations.nodal and \
538-
self[0].component_count == 3:
548+
n_components == 3:
539549
deform_by = self
540550
else:
541551
deform = False
@@ -567,7 +577,7 @@ def animate(self, save_as=None, deform_by=None, scale_factor=1.0, **kwargs):
567577
wf.set_output_name("deform_by", divide_op.outputs.field)
568578
else:
569579
scale_factor = None
570-
wf.set_output_name("to_render", extract_field_op.outputs.field)
580+
wf.set_output_name("to_render", to_render)
571581
wf.progress_bar = False
572582

573583
loop_over_field = dpf.core.fields_factory.field_from_array(

examples/05-plotting/06-animate_results.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
Review of available animation commands
55
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6-
This example lists the different commands available for creating animations of results,
6+
This example lists the different commands available for creating animations of transient results,
77
shown with the arguments available.
88
99
"""

0 commit comments

Comments
 (0)