I would like to solve the Neumann boundary-value problem for the Laplace operator (using the methodology from http://fenicsproject.org/documentation/dolfin/dev/python/demo/documented/neumann-poisson/python/documentation.html). In addition, I would like to print the nodal values of the numerically obtained solution $u$.
At the end of this post, you can find my implementation of the methodology from http://fenicsproject.org/documentation/dolfin/dev/python/demo/documented/neumann-poisson/python/documentation.html. The script runs without any problems when the block of code between # BEGIN: PROBLEMATIC BLOCK OF CODE and # END: PROBLEMATIC BLOCK OF CODE is commented out but, when the same block of code is present, I get the following error message:
*** -------------------------------------------------------------------------
*** DOLFIN encountered an error. If you are not able to resolve this issue
*** using the information listed below, you can ask for help at
*** fenics@fenicsproject.org
*** Remember to include the error message listed below and, if possible,
*** include a minimal running example to reproduce the error.
*** -------------------------------------------------------------------------
*** Error: Unable to access vector of degrees of freedom.
*** Reason: Cannot access a non-const vector from a subfunction.
*** Where: This error was encountered inside Function.cpp.
*** Process: unknown
*** DOLFIN version: 1.5.0
*** Git changeset: unknown
*** -------------------------------------------------------------------------
My implementation of the methodology from http://fenicsproject.org/documentation/dolfin/dev/python/demo/documented/neumann-poisson/python/documentation.html is as follows:
from dolfin import *
nx = 64
ny = 64
mesh = UnitSquareMesh(nx, ny)
V = FunctionSpace(mesh, "CG", 1)
R = FunctionSpace(mesh, "R", 0)
W = V * R
(u, c) = TrialFunction(W)
(v, d) = TestFunction(W)
f = Expression("10*exp(-(pow(x[0] - 0.5, 2) + pow(x[1] - 0.5, 2)) / 0.02)")
g = Expression("-sin(5*x[0])")
a = ( inner(grad(u),grad(v)) + c*v + d*u )*dx
L = f*v*dx + g*v*ds
w = Function(W)
n_prob_laplace_operator = LinearVariationalProblem( a, L, w )
solver_np_laplace_operator = LinearVariationalSolver(n_prob_laplace_operator)
solver_np_laplace_operator.solve()
(u, c) = w.split()
# BEGIN: PROBLEMATIC BLOCK OF CODE
# Dump solution to the screen
u_nodal_values = u.vector()
u_array = u_nodal_values.array()
coor = mesh.coordinates()
if coor.shape[0] == u_array.shape[0]: # degree 1 element
for i in range(len(u_array)):
print 'u(%8g,%8g) = %g' % (coor[i][0], coor[i][1], u_array[i])
else:
print """\
Cannot print vertex coordinates and corresponding values
because the element is not a first-order Lagrange element.
"""
# END: PROBLEMATIC BLOCK OF CODE
plot(u, interactive=True)
[Remark: The numerical solution of the Neumann boundary-value problem for the Laplacian operatorion is understood in the sense discussed on http://fenicsproject.org/qa/7090/fenics-demo-demo_neumann-poisson-py ]