Skip to content

Commit 4f29392

Browse files
authored
Fix animations for deform_by=result, as well as when animating a stress field with no deform_by argument. (#570)
1 parent 46c9892 commit 4f29392

File tree

3 files changed

+49
-51
lines changed

3 files changed

+49
-51
lines changed

ansys/dpf/core/fields_container.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,11 @@ def animate(self, save_as=None, deform_by=None, scale_factor=1.0, **kwargs):
536536
deform = False
537537
if deform_by and not isinstance(deform_by, dpf.core.FieldsContainer):
538538
deform_by = deform_by.eval()
539+
if len(deform_by) != len(self):
540+
raise ValueError("'deform_by' argument must result in a FieldsContainer "
541+
"of same length as the animated one "
542+
f"(len(deform_by.eval())={len(deform_by)} "
543+
f"!= len(self)={len(self)}).")
539544
else:
540545
deform = False
541546
if deform:
@@ -561,15 +566,12 @@ def animate(self, save_as=None, deform_by=None, scale_factor=1.0, **kwargs):
561566
# Addition to the scaled deformation field
562567
add_op = dpf.core.operators.math.add(divide_op.outputs.field,
563568
get_coordinates_op.outputs.coordinates_as_field)
569+
wf.set_output_name("deform_by", add_op.outputs.field)
570+
add_op.progress_bar = False
564571
else:
565572
scale_factor = None
566-
scale_factor_fc = dpf.core.animator.scale_factor_to_fc(1.0, self)
567-
extract_scale_factor_op = dpf.core.operators.utility.extract_field(scale_factor_fc)
568-
add_op = dpf.core.operators.utility.forward_field(extract_scale_factor_op)
569-
wf.set_output_name("deform_by", add_op.outputs.field)
570573
wf.set_output_name("to_render", extract_field_op.outputs.field)
571574
wf.progress_bar = False
572-
add_op.progress_bar = False
573575

574576
loop_over = self.get_time_scoping()
575577
frequencies = self.time_freq_support.time_frequencies

examples/05-plotting/06-animate_results.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
shown with the arguments available.
88
99
"""
10-
# sphinx_gallery_thumbnail_number = 1
1110
from ansys.dpf import core as dpf
1211
from ansys.dpf.core import examples
1312

@@ -43,6 +42,9 @@
4342
# - Showing the static geometry if the fields are not nodal 3D vector fields.
4443
# stress_fields.animate()
4544

45+
# One can deactivate by default animation by self using deform_by=False
46+
# displacement_fields.animate(deform_by=False)
47+
4648
# Change the scale factor using a number/a list of numbers for a uniform constant/varying scaling.
4749
# displacement_fields.animate(deform_by=True, scale_factor=10.,
4850
# show_axes=True)
@@ -62,8 +64,8 @@
6264
# - a Result giving nodal 3D vectorial length fields
6365
# stress_fields.animate(deform_by=model.results.displacement.on_all_time_freqs())
6466
# - an Operator which outputs nodal 3D vectorial length fields
65-
# stress_fields.animate(deform_by=model.results.displacement)
66-
67+
# stress_fields.animate(deform_by=model.results.displacement.on_all_time_freqs)
68+
# It must evaluate to a FieldsContainer of same length as the one being animated.
6769

6870
# Save the animation using "save_as" with a target path with the desired format as extension.
6971
# (accepts .gif, .avi or .mp4, see pyvista.Plotter.open_movie)

tests/test_animator.py

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def test_animator_animate_raise_no_workflow():
5858
assert "self.workflow" in e
5959

6060

61-
def test_animator_animate(remove_gifs, displacement_fields):
61+
def test_animator_animate(displacement_fields):
6262
frequencies = displacement_fields.time_freq_support.time_frequencies
6363
loop_over = displacement_fields.get_time_scoping()
6464
loop_over_field = dpf.fields_factory.field_from_array(frequencies.data[loop_over.ids-1])
@@ -72,8 +72,6 @@ def test_animator_animate(remove_gifs, displacement_fields):
7272

7373
an = Animator(wf)
7474
an.animate(loop_over=loop_over_field)
75-
# assert os.path.isfile(gif_name)
76-
# assert os.path.getsize(gif_name) > 600000
7775

7876

7977
def test_animator_animate_raise_wrong_scale_factor(remove_gifs, displacement_fields):
@@ -94,19 +92,25 @@ def test_animator_animate_raise_wrong_scale_factor(remove_gifs, displacement_fie
9492
assert "Argument scale_factor must be" in e
9593

9694

97-
def test_animator_animate_fields_container(remove_gifs, displacement_fields):
95+
def test_animator_animate_fields_container(displacement_fields):
9896
displacement_fields.animate()
99-
# assert os.path.isfile(gif_name)
100-
# assert os.path.getsize(gif_name) > 600000
10197

10298

103-
def test_animator_animate_fields_container_deform_by_false(remove_gifs, displacement_fields):
99+
def test_animator_animate_fields_container_deform_by_false(displacement_fields):
104100
displacement_fields.animate(deform_by=False)
105-
# assert os.path.isfile(gif_name)
106-
# assert os.path.getsize(gif_name) > 600000
107101

108102

109-
def test_animator_animate_fields_container_eqv_partial_scoping(remove_gifs):
103+
def test_animator_animate_fields_container_eqv():
104+
model = dpf.Model(examples.msup_transient)
105+
time_scoping = dpf.time_freq_scoping_factory.scoping_by_sets(
106+
list(range(5, 20)))
107+
stress_result = model.results.stress.on_time_scoping(time_scoping)
108+
109+
stress_fields = stress_result.on_location(dpf.common.locations.nodal).eval()
110+
stress_fields.animate()
111+
112+
113+
def test_animator_animate_fields_container_eqv_partial_scoping():
110114
model = dpf.Model(examples.msup_transient)
111115
time_scoping = dpf.time_freq_scoping_factory.scoping_by_sets(
112116
list(range(5, 20)))
@@ -121,23 +125,17 @@ def test_animator_animate_fields_container_eqv_partial_scoping(remove_gifs):
121125
stress_fields = stress_result.on_location(dpf.common.locations.nodal).eval()
122126
stress_fields.animate(deform_by=displacement_result, scale_factor=20.,
123127
framerate=1.)
124-
# assert os.path.isfile(gif_name)
125-
# assert os.path.getsize(gif_name) > 400000
126128

127129

128-
def test_animator_animate_fields_container_one_component(remove_gifs, displacement_fields):
130+
def test_animator_animate_fields_container_one_component(displacement_fields):
129131
displacement_fields.select_component(0).animate()
130-
# assert os.path.isfile(gif_name)
131-
# assert os.path.getsize(gif_name) > 600000
132132

133133

134-
def test_animator_animate_fields_container_deform_by_convert_unit(remove_gifs, displacement_fields):
134+
def test_animator_animate_fields_container_deform_by_convert_unit(displacement_fields):
135135
new_displacement_fields = displacement_fields.deep_copy()
136136
dpf.operators.math.unit_convert_fc(
137137
fields_container=new_displacement_fields, unit_name="mm")
138138
displacement_fields.animate(deform_by=new_displacement_fields)
139-
# assert os.path.isfile(gif_name)
140-
# assert os.path.getsize(gif_name) > 600000
141139

142140

143141
def test_animator_animate_fields_container_scale_factor_raise(displacement_fields):
@@ -146,72 +144,68 @@ def test_animator_animate_fields_container_scale_factor_raise(displacement_field
146144
assert "Argument scale_factor must be" in e
147145

148146

149-
def test_animator_animate_fields_container_deform_by_result(remove_gifs):
147+
def test_animator_animate_fields_container_deform_by_result():
150148
model = dpf.Model(examples.msup_transient)
151149
displacement_result = model.results.displacement.on_all_time_freqs
152150
displacement_fields = displacement_result.eval()
153151
displacement_fields.animate(deform_by=displacement_result)
154-
# assert os.path.isfile(gif_name)
155-
# assert os.path.getsize(gif_name) > 600000
156152

157153

158-
def test_animator_animate_fields_container_deform_by_operator(remove_gifs):
154+
def test_animator_animate_fields_container_deform_by_result_raise():
155+
model = dpf.Model(examples.msup_transient)
156+
displacement_result = model.results.displacement
157+
displacement_fields = displacement_result.on_all_time_freqs.eval()
158+
with pytest.raises(ValueError) as e:
159+
displacement_fields.animate(deform_by=displacement_result.on_first_time_freq)
160+
assert "'deform_by' argument must result in a FieldsContainer" in e
161+
162+
163+
def test_animator_animate_fields_container_deform_by_operator():
159164
model = dpf.Model(examples.msup_transient)
160165
displacement_op = model.results.displacement.on_all_time_freqs()
161166
displacement_fields = displacement_op.eval()
162167
displacement_fields.animate(deform_by=displacement_op)
163-
# assert os.path.isfile(gif_name)
164-
# assert os.path.getsize(gif_name) > 600000
165168

166169

167-
def test_animator_animate_fields_container_scale_factor_int(remove_gifs, displacement_fields):
170+
def test_animator_animate_fields_container_scale_factor_int(displacement_fields):
168171
displacement_fields.animate(scale_factor=2)
169-
# assert os.path.isfile(gif_name)
170-
# assert os.path.getsize(gif_name) > 600000
171172

172173

173-
def test_animator_animate_fields_container_scale_factor_float(remove_gifs, displacement_fields):
174+
def test_animator_animate_fields_container_scale_factor_float(displacement_fields):
174175
displacement_fields.animate(scale_factor=2.0)
175-
# assert os.path.isfile(gif_name)
176-
# assert os.path.getsize(gif_name) > 600000
177176

178177

179-
def test_animator_animate_fields_container_scale_factor_zero(remove_gifs, displacement_fields):
178+
def test_animator_animate_fields_container_scale_factor_zero(displacement_fields):
180179
displacement_fields.animate(scale_factor=0.0)
181-
# assert os.path.isfile(gif_name)
182-
# assert os.path.getsize(gif_name) > 600000
183180

184181

185-
def test_animator_animate_fields_container_scale_factor_list(remove_gifs, displacement_fields):
182+
def test_animator_animate_fields_container_scale_factor_list(displacement_fields):
186183
scale_factor_list = [2.0]*len(displacement_fields)
187184
displacement_fields.animate(scale_factor=scale_factor_list)
188-
# assert os.path.isfile(gif_name)
189-
# assert os.path.getsize(gif_name) > 600000
190185

191186

192-
def test_animator_animate_fields_container_scale_factor_raise_list_len(remove_gifs,
193-
displacement_fields):
187+
def test_animator_animate_fields_container_scale_factor_raise_list_len(displacement_fields):
194188
scale_factor_list = [2.0]*(len(displacement_fields)-2)
195189
with pytest.raises(ValueError) as e:
196-
displacement_fields.animate(save_as=gif_name, scale_factor=scale_factor_list)
190+
displacement_fields.animate(scale_factor=scale_factor_list)
197191
assert "The scale_factor list is not the same length" in e
198192

199193

200-
def test_animator_animate_fields_container_scale_factor_field(remove_gifs, displacement_fields):
194+
def test_animator_animate_fields_container_scale_factor_field(displacement_fields):
201195
scale_factor_field = dpf.fields_factory.field_from_array(displacement_fields[0].data)
202196
with pytest.raises(NotImplementedError) as e:
203-
displacement_fields.animate(save_as=gif_name, scale_factor=scale_factor_field)
197+
displacement_fields.animate(scale_factor=scale_factor_field)
204198
assert "Scaling by a Field is not yet implemented." in e
205199

206200

207-
def test_animator_animate_fields_container_scale_factor_fc(remove_gifs, displacement_fields):
201+
def test_animator_animate_fields_container_scale_factor_fc(displacement_fields):
208202
fields = []
209203
for f in displacement_fields:
210204
fields.append(dpf.fields_factory.field_from_array(f.data))
211205
scale_factor_fc = dpf.fields_container_factory.over_time_freq_fields_container(fields)
212206
scale_factor_fc.time_freq_support = displacement_fields.time_freq_support
213207
with pytest.raises(NotImplementedError) as e:
214-
displacement_fields.animate(save_as=gif_name, scale_factor=scale_factor_fc)
208+
displacement_fields.animate(scale_factor=scale_factor_fc)
215209
assert "Scaling by a FieldsContainer is not yet implemented." in e
216210

217211

0 commit comments

Comments
 (0)