Subject: [Viper-dev] patch for plotting Expressions (GenericFunctions)

From: Johan Hake <[email-masked]>
Date: Thu, 15 Oct 2009 11:58:19 +0200

Hello!

Please apply the handed patch to fix plotting of Expressions in PyDOLFIN

Johan
# HG changeset patch
# User "Johan Hake <[email-masked]>"
# Date 1255600538 -7200
# Node ID 27809df6bd5f6bb56700f3b1833b0948ac1b20cd
# Parent  7cbcf552d6c759783f1097784c318da2f04bd3a2
Fix for viper when plotting Expressions

diff -r 7cbcf552d6c7 -r 27809df6bd5f src/viper/viper_dolfin.py
--- a/src/viper/viper_dolfin.py	Wed Oct 07 13:22:24 2009 +0200
+++ b/src/viper/viper_dolfin.py	Thu Oct 15 11:55:38 2009 +0200
@@ -22,7 +22,7 @@
 
 from viper import Viper as ViperBase
 import dolfin
-from dolfin.cpp import Mesh, MeshFunctionInt, MeshFunctionUInt, MeshFunctionBool, MeshFunctionDouble, Function, FunctionSpace, FunctionPlotData
+from dolfin.cpp import Mesh, MeshFunctionInt, MeshFunctionUInt, MeshFunctionBool, MeshFunctionDouble, GenericFunction, Function, Expression, FunctionSpace, FunctionPlotData
 import numpy
 import vtk
 
@@ -64,9 +64,9 @@
         if isinstance(data, dolfin.Mesh):
             self.plottype = "mesh"
             self.plot_mesh(data, *args, **kwargs)
-        elif isinstance(data, Function):
-            self.plottype = "function"
-            self.plot_function(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)
@@ -91,8 +91,15 @@
         if kwargs.get("interactive", True):
             self.interactive()
 
-    def plot_function(self, data, *args, **kwargs):
-        self.mesh = data.function_space().mesh()
+    def plot_genericfunction(self, data, *args, **kwargs):
+        if isinstance(data, Expression):
+            self.mesh = kwargs.get("mesh")
+            if self.mesh is None or not isinstance(self.mesh, Mesh):
+                raise TypeError, "expected a mesh as kwargs when plotting Expression"
+        elif isinstance(data, Function):
+            self.mesh = data.function_space().mesh()
+        else:
+            raise TypeError, "Do not know how to plot the GenericFunction"
         self.vtkgrid = self.make_vtk_grid(self.mesh)
         self.filter = self.vtkgrid
 
@@ -124,10 +131,9 @@
             nno = len(self.pts)
             self.x = numpy.zeros((nno,dpn))
             v = numpy.zeros(dpn, dtype='d')
-            function_space = data.function_space()
             for (i, point) in enumerate(self.pts):
-                function_space.eval(v, point, data)
-                self.x[i] = v.copy()
+                data.eval(v, point)
+                self.x[i,:] = v[:]
             self.x = self.x.transpose().copy()
             self.x.shape = nno*dpn,
             coords = numpy.array(self.pts)
@@ -255,11 +261,10 @@
             self.interactive()
 
     def _dofs_pr_node(self, f):
-        element = FunctionSpace.element(f.function_space())
-        rank = element.value_rank()
+        rank = f.value_rank()
         dpn = 1
         for i in xrange(rank):
-            dpn *= element.value_dimension(i)
+            dpn *= f.value_dimension(i)
         return dpn, rank
 
     def add_polygon(self, polygon, idx=0):
@@ -312,7 +317,7 @@
             self.filter = self.vtkgrid
             self.update_scalar_mapper(self.filter)
             self.x = numpy.zeros(self.mesh.num_vertices())
-        elif self.plottype == "function":
+        elif self.plottype == "genericfunction":
             (dpn, rank) = self._dofs_pr_node(data)
             if self.pts is None:
                 nno = self.mesh.num_vertices()
@@ -322,10 +327,9 @@
                 nno = len(self.pts)
                 self.x = numpy.zeros((nno,dpn))
                 v = numpy.zeros(dpn, dtype='d')
-                function_space = data.function_space()
                 for (i, point) in enumerate(self.pts):
-                    function_space.eval(v, point, data)
-                    self.x[i] = v.copy()
+                    data.eval(v, point)
+                    self.x[i,:] = v[:]
                 self.x = self.x.transpose().copy()
                 self.x.shape = nno*dpn,