Dear Community members!
I started to learn FEniCS using the book by Langtangen and Logg.
When solving the Poisson-equation, it is suggested, to reformulate the equation
to the variational formulation, i.e. multiply by v \in H^1_0 and integrate by parts to get
Just to play around with the code, I wanted to implement this equation instead, that one obtains by not doing the integration by parts:
In my understanding u \in H^1 satisfying this expression would still be a weak solution.
The code looks like this:
# spaces and mesh
mesh = UnitSquareMesh(8, 8)
V = FunctionSpace(mesh, 'Lagrange', 1)
u = TrialFunction(V)
v = TestFunction(V)
# BC
u_D = Expression('1', degree=0)
def boundary(x, on_boundary):
return on_boundary
bc = DirichletBC(V, u_D, boundary)
# equations
f = Constant('-10')
a = dot(div(grad(u)), v)*dx
L = f*v*dx
u = Function(V)
solve (a==L, u, bc)
# plot
plot(mesh)
plot(u)
And produces the following error message:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-67-38954b2d310f> in <module>
21 # plot
22 plot(mesh)
---> 23 plot(u)
/usr/local/lib/python3.6/dist-packages/dolfin/common/plotting.py in plot(object, *args, **kwargs)
436 # Plot
437 if backend == "matplotlib":
--> 438 return _plot_matplotlib(object, mesh, kwargs)
439 elif backend == "x3dom":
440 return _plot_x3dom(object, kwargs)
/usr/local/lib/python3.6/dist-packages/dolfin/common/plotting.py in _plot_matplotlib(obj, mesh, kwargs)
302
303 if isinstance(obj, cpp.function.Function):
--> 304 return mplot_function(ax, obj, **kwargs)
305 elif isinstance(obj, cpp.function.Expression):
306 return mplot_expression(ax, obj, mesh, **kwargs)
/usr/local/lib/python3.6/dist-packages/dolfin/common/plotting.py in mplot_function(ax, f, **kwargs)
150 if mode == "contourf":
151 levels = kwargs.pop("levels", 40)
--> 152 return ax.tricontourf(mesh2triang(mesh), C, levels, **kwargs)
153 elif mode == "color":
154 shading = kwargs.pop("shading", "gouraud")
/usr/local/lib/python3.6/dist-packages/matplotlib/tri/tricontour.py in tricontourf(ax, *args, **kwargs)
273 def tricontourf(ax, *args, **kwargs):
274 kwargs['filled'] = True
--> 275 return TriContourSet(ax, *args, **kwargs)
276
277
/usr/local/lib/python3.6/dist-packages/matplotlib/tri/tricontour.py in __init__(self, ax, *args, **kwargs)
33 are described in the docstring of `tricontour`.
34 """
---> 35 ContourSet.__init__(self, ax, *args, **kwargs)
36
37 def _process_args(self, *args, **kwargs):
/usr/local/lib/python3.6/dist-packages/matplotlib/contour.py in __init__(self, ax, levels, filled, linewidths, linestyles, alpha, origin, extent, cmap, colors, norm, vmin, vmax, extend, antialiased, *args, **kwargs)
853 self._transform = kwargs.pop('transform', None)
854
--> 855 kwargs = self._process_args(*args, **kwargs)
856 self._process_levels()
857
/usr/local/lib/python3.6/dist-packages/matplotlib/tri/tricontour.py in _process_args(self, *args, **kwargs)
45 else:
46 from matplotlib import _tri
---> 47 tri, z = self._contour_args(args, kwargs)
48 C = _tri.TriContourGenerator(tri.get_cpp_triangulation(), z)
49 self._mins = [tri.x.min(), tri.y.min()]
/usr/local/lib/python3.6/dist-packages/matplotlib/tri/tricontour.py in _contour_args(self, args, kwargs)
92 'triangulation')
93 if not np.isfinite(z_check).all():
---> 94 raise ValueError('z array must not contain non-finite values '
95 'within the triangulation')
96
ValueError: z array must not contain non-finite values within the triangulation
So apparently, the solution has some non-finite values. But, when implementing the left bilinear form as \nabla u \cdot \nabla v the code does produce a solution. I dont understand that behaviour, as the two formulations should be equivalent. Is the problem here, that u \notin \mathbf{C}^2 is somehow assumed in the implementation?
I am more than thankful for any kind of help.