tum-pbs / PhiFlow

A differentiable PDE solving framework for machine learning
MIT License
1.39k stars 189 forks source link

Animations do not render #127

Open HugoFara opened 1 year ago

HugoFara commented 1 year ago

Hello there!

As I just discovered this package, I have been following Fluids Tutorial to see whether I could have a nice animation. Unfortunately, copying exactly the provided code do not display anything.

I get one error warning after running vis.plot(trajectory, animate='time') (input [7] from the tutorial):

/python3.10/site-packages/matplotlib/animation.py:887: UserWarning: Animation was deleted without rendering anything. This is most likely not intended. To prevent deletion, assign the Animation to a variable, e.g. `anim`, that exists until you have outputted the Animation using `plt.show()` or `anim.save()`.

An easy fix I used was to remove plots.close(figure) on https://github.com/tum-pbs/PhiFlow/blob/1c14b08485c7dcf9f76a62277a50c1fd2b09802d/phi/vis/_vis.py#L390 Doing so, the animation displays normally.

User Specifications

Please tell me if you need more information!

holl- commented 1 year ago

Hi, Thanks for the bug report! Did you run the code within a Jupyter notebook or as a Python script? The animation does work inside the Colab notebook.

HugoFara commented 1 year ago

I'm running the example from a script, with the TkAgg backend.

As a side note, I cannot save the animation without my hack. I would get the following error: _tkinter.TclError: invalid command name ".!navigationtoolbar2tk.!button2".

holl- commented 1 year ago

Interesting, I was always able to save the animations but I've pushed your change, https://github.com/tum-pbs/PhiFlow/commit/bf136b16075ff190048900690971450fa28e45ef. Let's see if everything else still works...

HugoFara commented 1 year ago

Let's see if everything else still works...

Spoken like a true developer! :laughing:

I have been working with my hack without noticeable issue until now at least, so that seems fine.

More broadly, I often run into the issue of generating animations with matplotlib with my own projects, and I can say that closing the figures is rarely necessary.

More importantly, I used to use the same tricks to save the animation (see line 389) https://github.com/tum-pbs/PhiFlow/blob/1c14b08485c7dcf9f76a62277a50c1fd2b09802d/phi/vis/_vis.py#L389

The issue with this strategy is that it can result in a big memory leak as the animation is saved until a new animation is loaded. Usually I force the display of the animation and then return it, so this line may be unnecessary. It can become annoying, but I guess users are not supposed to use PhiFlow to run several minutes of animation.

holl- commented 1 year ago

So it turns out that not closing the figure causes the animation to appear twice in Jupyter notebooks, even though no show() is called. Not returning it would be one possible fix but that is inconsistent. Do you know why it's showing up in the first place?

HugoFara commented 1 year ago

I didn't check that, but I think one of the animation displaying is the output from the function, the other the figure we prepared that renders (matplotlib is always a bit annoying because of that).

Because returning the animation is nice, and that we want to close the figure, the easiest "fix" is to assign the output of the variable so that it is not displayed in Jupyter.

holl- commented 1 year ago

Okay, for now I only close the plot when Jupyter is active. But maybe a better solution would be to to put a ; after the plot calls so the cell doesn't return the figure...