Subject: [Fenics-viper] Some patches for viper-fenics

From: Joachim Berdal Haga <[email-masked]>
Date: Thu, 20 Jan 2011 10:20:16 +0100

Hi,

I have attached some patches for viper:
1) A small cleanup
2) If multiple plot windows are opened, they get tiled across the
screen instead of on top of each other. Maybe this is the window
manager's job, but mine doesn't, so...
3) Don't know why displacement mode didn't allow wireframe view, but
it seems to work fine and I like it
4) The point of this one is that Dolfin can now check directly which
types are supported, which simplifies things a bit there. Will push
the dolfin patch to dolfin@lp later.
5) The vector scalarbar had pretty hard-to-read legend, which is most
easily fixed by just using the same as for scalar plots.

Please apply (if they are satisfactory, of course).

-j.
From ff091725709da01b9ea621ab185598c5e2ec9f2d Mon Sep 17 00:00:00 2001
From: Joachim B Haga <[email-masked]>
Date: Wed, 12 Jan 2011 14:34:05 +0100
Subject: [PATCH 1/5] Small cleanup of PlotManager

---
 src/viper/viper_dolfin.py |    8 ++------
 1 files changed, 2 insertions(+), 6 deletions(-)

diff --git a/src/viper/viper_dolfin.py b/src/viper/viper_dolfin.py
index f71081f..6c57037 100644
--- a/src/viper/viper_dolfin.py
+++ b/src/viper/viper_dolfin.py
@@ -368,20 +368,16 @@ class Viper(ViperBase):
 
 class PlotManager(object):
     def __init__(self):
-        self.plots = {}
-        self.index = None # By default, use automatic plotting
-        self.length = 0
         import random
         self.random = random
+        self.reset()
 
     def figure(self, index):
         self.index = index
 
     def reset(self):
-        del self.plots
         self.plots = {}
-        self.index = None
-        self.length = 0
+        self.index = None # By default, use automatic plotting
 
     def plot(self, plot_object, *args, **kwargs):
         if self.index is None:
-- 
1.7.1

From 79ac9da0e345d740e961ac02b03d4c331010e04b Mon Sep 17 00:00:00 2001
From: Joachim B Haga <[email-masked]>
Date: Wed, 12 Jan 2011 14:34:18 +0100
Subject: [PATCH 2/5] Automatic positioning of multiple plot windows

---
 src/viper/viper_dolfin.py |   10 ++++++++++
 1 files changed, 10 insertions(+), 0 deletions(-)

diff --git a/src/viper/viper_dolfin.py b/src/viper/viper_dolfin.py
index 6c57037..eee8851 100644
--- a/src/viper/viper_dolfin.py
+++ b/src/viper/viper_dolfin.py
@@ -426,12 +426,22 @@ def plot(data, *args, **kwargs):
     global _plotter
     if _plotter is None:
         _plotter = PlotManager()
+    n_old_plots = len(_plotter.plots)
 
     interactive = False
     if kwargs.has_key("interactive"):
         interactive = kwargs["interactive"]
         kwargs["interactive"] = False
     fig = _plotter.plot(data, **kwargs)
+
+    if n_old_plots > 0 and kwargs.get('autoposition', True):
+        x, y   = fig.window_size
+        nx, ny = n_old_plots%3, n_old_plots//3
+        fig.renWin.SetPosition(nx*x, ny*y)
+        # The actual positioning seems to be indeterministic, but at least
+        # they don't overlap (completely).
+        #print (nx*x,ny*y), fig.renWin.GetPosition()
+
     if interactive:
         _plotter.interactive()
     return fig
-- 
1.7.1

From d056e64b53ce8301a5b7f2749e88d02ff53d6015 Mon Sep 17 00:00:00 2001
From: Joachim B Haga <[email-masked]>
Date: Wed, 12 Jan 2011 14:41:10 +0100
Subject: [PATCH 3/5] Allow wireframe view of mode="displacement" plots.

---
 src/viper/viper_dolfin.py |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/src/viper/viper_dolfin.py b/src/viper/viper_dolfin.py
index eee8851..2f4cfae 100644
--- a/src/viper/viper_dolfin.py
+++ b/src/viper/viper_dolfin.py
@@ -163,7 +163,7 @@ class Viper(ViperBase):
                 # Use vector norms for scalar coloring
                 self.x = numpy.sqrt(self.x[:,0]**2 + self.x[:,1]**2 + self.x[:,2]**2)
                 self.filter = self.warp_vector(self.displacement)
-                (self.iren, self.renWin, self.ren) = self.simple_plotter(self.filter, vmin, vmax, wireframe=False)
+                (self.iren, self.renWin, self.ren) = self.simple_plotter(self.filter, vmin, vmax, wireframe=wireframe)
 
         self.iren.Initialize()
         self._update(self.x)
-- 
1.7.1

From a74fb74977850df5cc6be001cad49621c71488fe Mon Sep 17 00:00:00 2001
From: Joachim B Haga <[email-masked]>
Date: Wed, 12 Jan 2011 17:20:54 +0100
Subject: [PATCH 4/5] Store supported types in a list, and raise exception if not supported.

---
 src/viper/viper_dolfin.py |   30 +++++++++++++-----------------
 1 files changed, 13 insertions(+), 17 deletions(-)

diff --git a/src/viper/viper_dolfin.py b/src/viper/viper_dolfin.py
index 2f4cfae..5ebebd6 100644
--- a/src/viper/viper_dolfin.py
+++ b/src/viper/viper_dolfin.py
@@ -29,6 +29,11 @@ import ffc
 
 _plotter = None
 
+plottable = [('mesh',             (Mesh,)),
+             ('genericfunction',  (GenericFunction,)),
+             ('functionplotdata', (FunctionPlotData,)),
+             ('meshfunction',     tuple(eval("MeshFunction%s" % t) for t in ["Int", "UInt", "Double", "Bool"]))]
+
 class Viper(ViperBase):
     """A custom Viper sub-class for visualizing meshes and fields in Dolfin."""
 
@@ -60,23 +65,14 @@ class Viper(ViperBase):
         args, kwargs = self.parse_input(args, kwargs)
         if "noninteractive" in args: kwargs["interactive"] = False
 
-        if isinstance(data, dolfin.Mesh):
-            self.plottype = "mesh"
-            self.plot_mesh(data, *args, **kwargs)
-        elif isinstance(data, GenericFunction):
-            self.plottype = "genericfunction"
-            self.plot_genericfunction(data, *args, **kwargs)
-        elif isinstance(data, FunctionPlotData):
-            self.plottype = "functionplotdata"
-            self.plot_functionplotdata(data, *args, **kwargs)
-        elif isinstance(data, tuple([eval("MeshFunction%s" % x) for x in ["Int", "UInt", "Double", "Bool"]])):
-            self.plottype = "meshfunction"
-            self.plot_meshfunction(data, *args, **kwargs)
-        else:
-            from dolfin.project import project
-            print "using project"
-            data = project(data)
-            self.plot(data, *args, **kwargs)
+        for plottype, classes in plottable:
+            if isinstance(data, classes):
+                self.plottype = plottype
+                plot_method = getattr(self, "plot_%s" % plottype)
+                plot_method(data, *args, **kwargs)
+                return
+
+        raise TypeError("Type not supported for plotting, %s" % str(type(data)))
 
     def plot_mesh(self, data, *args, **kwargs):
         self.rescale = False
-- 
1.7.1

From e9c1b20c7c7568b02846f73c76530dd4566c80f8 Mon Sep 17 00:00:00 2001
From: Joachim B Haga <[email-masked]>
Date: Tue, 18 Jan 2011 10:46:05 +0100
Subject: [PATCH 5/5] Let vector plot have the same scalarbar as scalar plots.

---
 src/viper/viper.py |   30 +++++++++++++++---------------
 1 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/src/viper/viper.py b/src/viper/viper.py
index c7e177f..3488ee1 100644
--- a/src/viper/viper.py
+++ b/src/viper/viper.py
@@ -566,6 +566,19 @@ Additional parameters: vmin, vmax, lutfile, wireframe, celltype, title
                 lut.SetTableValue(i, *row)
         return lut
 
+    def _make_scalarbar(self, lut):
+        scalarbar = vtk.vtkScalarBarActor()
+        scalarbar.SetLookupTable(lut)
+        scalarbar.GetPositionCoordinate().SetCoordinateSystemToNormalizedViewport()
+        scalarbar.GetPositionCoordinate().SetValue (0.1,0.01)
+        scalarbar.SetOrientationToHorizontal()
+        scalarbar.SetWidth (0.8)
+        scalarbar.SetHeight (0.14)
+        scalarbar.VisibilityOff()
+        scalarbar.GetTitleTextProperty().SetColor(0,0,0)
+        scalarbar.GetLabelTextProperty().SetColor(0,0,0)
+        return scalarbar
+
 
     def make_vtk_grid(self, mesh, cell_type=None):
         """Based on a mesh with the methods cells() and coordinates(), construct a vtk grid."""
@@ -838,10 +851,7 @@ Additional parameters: vmin, vmax, lutfile, wireframe, celltype, title
             self.ren.SetBackground(1, 1, 1)
 
             lut = self._make_lut(autorange=(vmin, vmax))
-            scalarbar = vtk.vtkScalarBarActor()
-            scalarbar.SetLookupTable(lut)
-            scalarbar.VisibilityOff()
-            self.scalarbar = scalarbar
+            self.scalarbar = self._make_scalarbar(lut)
             self.lut = lut
         else:
             self.ren.RemoveAllViewProps()
@@ -1029,17 +1039,7 @@ Inherited from vtk:
             self.ren.SetBackground(1, 1, 1)
 
             lut = self._make_lut(autorange=(vmin, vmax))
-            scalarbar = vtk.vtkScalarBarActor()
-            scalarbar.SetLookupTable(lut)
-            scalarbar.GetPositionCoordinate().SetCoordinateSystemToNormalizedViewport()
-            scalarbar.GetPositionCoordinate().SetValue (0.1,0.01)
-            scalarbar.SetOrientationToHorizontal()
-            scalarbar.SetWidth (0.8)
-            scalarbar.SetHeight (0.14)
-            scalarbar.VisibilityOff()
-            scalarbar.GetTitleTextProperty().SetColor(0,0,0)
-            scalarbar.GetLabelTextProperty().SetColor(0,0,0)
-            self.scalarbar = scalarbar
+            self.scalarbar = self._make_scalarbar(lut)
             self.lut = lut
         else:
             self.ren.RemoveAllViewProps()
-- 
1.7.1

---

From: Anders Logg <[email-masked]>
Date: Thu, 20 Jan 2011 10:56:17 +0100

Thanks! Applied.

--
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