usnistgov / fipy

FiPy is a Finite Volume PDE solver written in Python
http://pages.nist.gov/fipy/en/latest
Other
509 stars 149 forks source link

plotMesh call throwing deprecation warning and failing to plot in circle diffusion example #693

Closed amine-aboufirass closed 4 years ago

amine-aboufirass commented 4 years ago

I tried to replicate the circle diffusion example as follows. I used pygmsh to generate the geometry string that fipy required:


from fipy import CellVariable, Gmsh2D, TransientTerm, DiffusionTerm, Viewer
from fipy.tools import numerix
import pygmsh, meshio

cellSize = 0.05
radius = 1.

geom = pygmsh.built_in.Geometry()
p1 = geom.add_point((0.,0.,0.), lcar = cellSize)
p2 = geom.add_point((-radius,0.,0.), lcar = cellSize)
p3 = geom.add_point((0.,radius,0.), lcar = cellSize)
p4 = geom.add_point((radius,0., 0.), lcar = cellSize)
p5 = geom.add_point((0.,-radius,0.), lcar = cellSize)
l6 = geom.add_circle_arc(p2, p1, p3)
l7 = geom.add_circle_arc(p3, p1, p4)
l8 = geom.add_circle_arc(p4, p1, p5)
l9 = geom.add_circle_arc(p5, p1, p2)
ll10 = geom.add_line_loop((l6, l7, l8, l9))
s11 = geom.add_surface(ll10)

geom_string = geom.get_code()
mesh = Gmsh2D(geom_string)

phi = CellVariable(name = "solution variable", mesh = mesh, value = 0.)
viewer = Viewer(vars=phi, datamin=-1, datamax=1.)
viewer.plotMesh()

The above returns a warning (in jupyter) and the mesh fails to plot:

C:\Users\aboufira\AppData\Local\Continuum\miniconda3\envs\CGF_1\lib\site-packages\fipy\viewers\matplotlibViewer\matplotlibViewer.py:229: MatplotlibDeprecationWarning: The set_norm function was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use ScalarMappable.set_norm instead. self._cb.set_norm(value) C:\Users\aboufira\AppData\Local\Continuum\miniconda3\envs\CGF_1\lib\site-packages\fipy\viewers\matplotlibViewer\matplotlibViewer.py:240: MatplotlibDeprecationWarning: The set_norm function was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use ScalarMappable.set_norm instead. self._cb.set_norm(self.viewer.norm)

I also found a related issue https://github.com/usnistgov/fipy/issues/586 in which @cashTangoTangoCash attempts a workaround involving writing to and reading from a .msh file. I'd like to avoid this if I can, if the .plotMesh method works. Does anyone know why the above warning occurs? Is the plotMesh method currently working?

amine-aboufirass commented 4 years ago

I'm also noticing that there are a few different classes for matplotlib 2D viewers. A couple are:

What's the difference between these? Is the second one meant to plot meshes perhaps?

amine-aboufirass commented 4 years ago

Well I had to get down and dirty with it. Ended up using a combination of gmsh and pyvista to get what I want:

from fipy import CellVariable, Gmsh2D, TransientTerm, DiffusionTerm, Viewer
from fipy.tools import numerix
import pygmsh, pyvista

cellSize = 0.05
radius = 1.

geom = pygmsh.built_in.Geometry()
p1 = geom.add_point((0.,0.,0.), lcar = cellSize)
p2 = geom.add_point((-radius,0.,0.), lcar = cellSize)
p3 = geom.add_point((0.,radius,0.), lcar = cellSize)
p4 = geom.add_point((radius,0., 0.), lcar = cellSize)
p5 = geom.add_point((0.,-radius,0.), lcar = cellSize)
l6 = geom.add_circle_arc(p2, p1, p3)
l7 = geom.add_circle_arc(p3, p1, p4)
l8 = geom.add_circle_arc(p4, p1, p5)
l9 = geom.add_circle_arc(p5, p1, p2)
ll10 = geom.add_line_loop((l6, l7, l8, l9))
s11 = geom.add_surface(ll10)

geom_string = geom.get_code()
mesh = Gmsh2D(geom_string)

ugrid= pyvista.UnstructuredGrid(mesh.VTKCellDataSet._vtk_obj)
plotter = pyvista.Plotter()
plotter.set_background('white')
plotter.add_mesh(ugrid, style='wireframe', color='black')
plotter.add_bounding_box(color='red')
plotter.show_grid(color="red")
plotter.view_xy()
plotter.show()

download

Although I do note that the mesh looks slightly different from what is provided in the example in the documentation. On another note, would it be worth investigating the following?:

Just some suggestions which I think might improve fipy's already excellent functionality and allow it to better integrate with existing libraries...

guyer commented 4 years ago

.plotMesh() is not presently implemented for any of our Viewers. It used to be implemented for the obsolete gist package, but we've evidently never written the code for anything else.

The deprecation warnings are unrelated. That's just a sign that our Matplotlib Viewers are using an old interface to Matplotlib and need to be updated.

guyer commented 4 years ago

Matplotlib2DGridViewer is optimized to display variables defined on grid meshes. Matplotlib2DViewer is a general tool that can display variables defined on arbitrary 2D meshes.

guyer commented 4 years ago

A contribution of a PyVista viewer class would be most welcome

guyer commented 4 years ago

We are open to using pygmsh, but according to its author, it doesn't handle parallel partitioning, which limits its utility for FiPy.

amine-aboufirass commented 4 years ago

@guyer thanks for your responses. For the moment I am only using pygmsh to feed the geometry string into fipy's Gmsh2D object. This is not so difficult so getting Gmsh2D to accept a pygmsh.built_in.Geometry() may be a bit of an overkill... It works and that's what matters.

I am happy to look into implementing a pyvista viewer class, but perhaps some consideration of the discussion at https://github.com/pyvista/pyvista-support/issues/108 might be necessary.

guyer commented 4 years ago

This is a duplicate of #312.