extract vector of pressure=p after solve(a==L, U, bc) and u,p=U.split()

Asked by Daniel Bare

Hello,

 I consider a Stokes problem:

V = VectorFunctionSpace(mesh, "CG", 2)
Q = FunctionSpace(mesh, "CG", 1)
W = V * Q

with boundary conditions...

# Define variational problem
(u, p) = TrialFunctions(W)
(v, q) = TestFunctions(W)
f = Expression(('x[0]*2', 'x[1]+7'))
a = inner(grad(u), grad(v))*dx + div(v)*p*dx + q*div(u)*dx
L = inner(f, v)*dx

#Direct solving
U=Function(W)
solve(a == L, U, bc)

# Get sub-functions
u, p = U.split()

till here it works and I also can plot the result, but when I whant to check the vector p by

pressure=p.vector().array()

I get the following error:

Traceback (most recent call last):
  File "preSplitt_vrs_postSplitt.py", line 54, in <module>
    pressure=p.vector().array()
RuntimeError:

*** -------------------------------------------------------------------------
*** DOLFIN encountered an error. If you are not able to resolve this issue
*** using the information listed below, you can ask for help at
***
*** https://answers.launchpad.net/dolfin
***
*** 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 fro function.
*** Reason: Cannot access a non-const vector from a subfunction.
*** Where: This error was encountered inside Function.cpp.
*** -------------------------------------------------------------------------

thanks,

 regards,

Zoufine

Question information

Language:
English Edit question
Status:
Solved
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Solved by:
Anders Logg
Solved:
Last query:
Last reply:
Revision history for this message
Best Anders Logg (logg) said :
#1

This is because p is not a proper Function. It is only a view of a Function (U) and so does not have it's own vector of dofs.

You can either do U.vector().array() and then pick out the p-part (last mesh.num_vertices() dofs) or do

u, p = U.split(deepcopy=True)

Revision history for this message
Daniel Bare (raszoufine) said :
#2

Thanks Anders Logg, that solved my question.

Revision history for this message
Zhan Chen (chen2724) said :
#3

You can either do U.vector().array() and then pick out the p-part (last mesh.num_vertices() dofs)

How to do this?

Thanks!
Zhan