How to compute $\nabla \phi$ if $\phi$ is a function of degree one ?

14 months ago by
 Hi, all.

My new question comes again.

I defined two function space, such as P2-P1 for velocity and pressure. 

And I need to do the minus:  $u=m-\nabla\phi$u=mϕ , where  $m$m and  $u$u are frome P2 vector function space, and  $\phi$ϕ comes from P1 function space. So, the  $\nabla\phi$ϕ becomes a constant in every triangle. Do I need to reconstruct the gradient  $\nabla\phi$ϕ before doing the minus?

Though, I have seen some related function, Project and Interpolate. see here and here.  I still do not know the answer properly. I worry about the error influence the stability of my algorithm.


BEST, Hamilton 
The answer depends on what you want to do with that. Please provide some extra information and hopefully a minimal working example if possible.
written 14 months ago by Nicolás Barnafi 

1 Answer

13 months ago by
I guess from your post that you're implementing a projection scheme for Navier-Stokes?

You can simply do project(grad(phi), V), where V could be a DG0 function space. You can project to P2, too, and directly compute:
    u.vector()[:] = m.vector() + project(grad(phi), V).vector()
project(grad(phi), V) solves \((u, v) = \langle \nabla \phi, v\rangle\) for \(u, v\in V\).
If you need to compute this sum often (which I guess is the case), you should cast it into variational form, assemble it beforehand, and then compute it with solve(A, u.vector(), b). (At least the projection of the gradient, then add the P2 vectors with axpy).

ps: sorry for the edits, didn't quite get the new syntax..
Yes, I am implementing a project scheme for NS equations.
Thanks for your comment. I will try your comment.
The demo of FEniCS has code of Chorin's project method for L-shape domain fluid. see:
Or, IPCS method, see:

What I want to implement is one of these kind methods, called gauge method, some details can be found in

When I try to implement this method, I encouter several problems. And some of them are posted in community. 
If you are interested at this topic, see,
mainly talks about how to implement the tangential velocity. Some questions still be remained, I am glad if you can give some comments.

Best, regs.
written 13 months ago by Hamilton 
Well, in the IPCS example they do exactly what you want (and what I've explained).

# Define variational problem for step 3
a3 = dot(u, v)*dx
L3 = dot(u_, v)*dx - k*dot(nabla_grad(p_ - p_n), v)*dx

+ solves involving a3, L3. done.
written 13 months ago by David Nolte 
Please login to add an answer/comment or follow this question.

Similar posts:
Search »