19
19
# Modified by Anders Logg, 2008-2010.
21
21
# First added: 2008-03-05
22
# Last changed: 2012-06-18
22
# Last changed: 2012-09-21
24
24
import dolfin.cpp as cpp
29
# Compatibility with book
29
30
def _VTKPlotter_write_ps(self, *args, **kwargs) :
30
print "Warning: VTKPlotter::write_ps() is not implemented"
31
print "*** Warning: VTKPlotter::write_ps() is not implemented -- use write_pdf instead"
32
33
def plot(object, *args, **kwargs):
101
102
return ffc.plot(object, *args, **kwargs)
104
if mesh is None and len(args) == 1 and isinstance(args[0], cpp.Mesh):
103
107
# Plot expression
104
108
if isinstance(object, cpp.Expression):
106
return cpp.plot(object, mesh, p)
107
elif len(args) == 1 and isinstance(args[0], cpp.Mesh):
108
return cpp.plot(object, args[0], p)
110
110
raise TypeError, "expected a mesh when plotting an expression."
111
return cpp.plot(object, mesh, p)
112
113
# Try to project if object is not a standard plottable type
113
114
if not isinstance(object, (cpp.Function, cpp.Expression, cpp.Mesh,
114
115
cpp.DirichletBC, cpp.MeshFunction, cpp.MeshFunctionBool,
115
cpp.MeshFunctionInt, cpp.MeshFunctionDouble, cpp.MeshFunctionInt,
116
cpp.MeshFunctionUInt, cpp.DirichletBC)):
116
cpp.MeshFunctionInt, cpp.MeshFunctionDouble,
117
cpp.MeshFunctionSizet, cpp.MeshFunctionUInt, cpp.DirichletBC, cpp.CSGGeometry)):
118
119
from dolfin.fem.projection import project
120
121
cpp.info("Object cannot be plotted directly, projecting to"\
121
122
" piecewise linears.")
122
123
object = project(object, mesh=mesh)
124
raise RuntimeError, ("Don't know how to plot given object and "\
125
"projection failed: " + str(object))
124
except Exception as e:
125
raise RuntimeError(("Don't know how to plot given object:\n %s\n"\
126
"and projection failed:\n %s") % (str(object), str(e)))
127
128
plot_object = cpp.plot(object, p)
128
129
plot_object.write_ps = _VTKPlotter_write_ps
131
# Avoid premature deletion of plotted objects if they go out of scope
132
# before the plot window is closed. The plotter itself is safe, since it's
133
# created in the plot() C++ function, not directly from Python. But the
134
# Python plotter proxy may disappear, so we can't store the references
136
global _objects_referenced_from_plot_windows
137
_objects_referenced_from_plot_windows[plot_object.key()] = (object, mesh, p)
129
139
return plot_object
141
_objects_referenced_from_plot_windows = {}