Subject: [Fenics-viper] Patch for plotting facet meshfunctions

From: Johan Hake <[email-masked]>
Date: Thu, 24 Jun 2010 11:45:53 -0700

Hello!

I just got frustrated not beeing able to plot meshfunctions of facets. 
Typically you want to plot some boundaries. 

So I just made a hack where I transformed the meshfunction into a vertexvalued 
meshfunction and plot that instead.

diff attached.

I hope you find it usefull.

Johan
=== modified file 'src/viper/viper_dolfin.py'
--- src/viper/viper_dolfin.py	2009-11-12 09:40:11 +0000
+++ src/viper/viper_dolfin.py	2010-06-24 18:45:10 +0000
@@ -237,6 +237,7 @@
 
     def plot_meshfunction(self, data, *args, **kwargs):
         self.mesh = data.mesh()
+        mesh_dim = self.mesh.topology().dim()
         dim = data.dim()
         size = data.size()
         values = data.values()
@@ -245,15 +246,26 @@
         self.x = numpy.array(values, dtype='d')
         if str(values.dtype).count("int"):
             self.lutfile = ""
-        if size in (self.mesh.num_vertices(), self.mesh.num_cells()):
-            if dim==self.mesh.topology().dim(): # meshfunction over cells
+        if dim in (0, mesh_dim-1, mesh_dim):
+            if dim==mesh_dim: # meshfunction over cells
                 self.vertex_plot = False
                 self.lutfile = ""
+            elif dim==mesh_dim-1: # meshfunction over faces
+                # Create a vertex valued meshfunction and plot that
+                self.mesh.init(dim)
+                vertices = type(data)(self.mesh, 0)
+                vertex_values = vertices.values()
+                vertex_values[:] = 0
+                con20 = self.mesh.topology()(dim,0)
+                for facet in xrange(self.mesh.num_faces()):
+                    if values[facet]:
+                        vertex_values[con20(facet)] = values[facet]
+                self.plot_meshfunction(vertices, *args, **kwargs)
+                return
+
             (self.iren, self.renWin, self.ren) = self.simple_plotter(self.filter, self.x.min(), self.x.max(), wireframe=False)
         else:
-            print "Unknown size %d" % size
-            print "Valid size are v(%d) and c(%d)" % (self.mesh.num_vertices(), self.mesh.num_cells())
-            raise RuntimeError, "Only vertex and cell valued meshfunctions can be plotted"
+            raise RuntimeError, "Only vertex, facets and cell valued meshfunctions can be plotted"
         self.iren.Initialize()
         self._update(self.x)
         self.renWin.Render()

---

From: Anders Logg <[email-masked]>
Date: Thu, 24 Jun 2010 21:18:23 +0200

Go ahead and add it. I've added you to the Viper Core team. (Ola can
remove you if he disagrees.)

--
Anders

> _______________________________________________
> Mailing list: https://launchpad.net/~fenics-viper
> Post to     : [email-masked]
> Unsubscribe : https://launchpad.net/~fenics-viper
> More help   : https://help.launchpad.net/ListHelp

---

From: Johan Hake <[email-masked]>
Date: Thu, 24 Jun 2010 12:59:09 -0700

Ok!

The patch is pushed

Johan

On Thursday June 24 2010 12:18:23 Anders Logg wrote:
> > So I just made a hack where I transformed the meshfunction into a
> > vertexvalued meshfunction and plot that instead.
> > 
> > diff attached.
> > 
> > -            if dim==self.mesh.topology().dim(): # meshfunction over
> > cells +        if dim in (0, mesh_dim-1, mesh_dim):
> >              (self.iren, self.renWin, self.ren) =
> >              self.simple_plotter(self.filter, self.x.min(),
> >              self.x.max(), wireframe=False)
> >          
> >          else:
> > -            print "Valid size are v(%d) and c(%d)" %
> > (self.mesh.num_vertices(), self.mesh.num_cells()) -            raise
> > RuntimeError, "Only vertex and cell valued meshfunctions can be plotted"