This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

How do I construct an arbitrary vector function for export?

0 votes

I have a 2D PDE of the form:
$$ \frac{\partial^2 A}{\partial y^2} + \frac{1}{y}\frac{\partial A}{\partial y} + \frac{\partial^2 A}{\partial x^2} = - J $$

And I would like to generate the vector function:
$$ \vec{B} = \frac{\partial (y A)}{\partial y} \hat{x} - \frac{\partial A}{\partial x} \hat{y}$$

I gather that I can find the components of the gradient of A by using "split"

grad_A = project(grad(A), VectorFunctionSpace(mesh, "Lagrange", degree)
grad_A_x, grad_A_y = grad_A.split(deepcopy=True)

But I am unsure of how to form the vector function B ...

asked Apr 7, 2014 by timm FEniCS User (2,100 points)

1 Answer

+2 votes
 
Best answer

Here is a suggestion:

CG = FunctionSpace(mesh, "CG", degree)
A = Function(CG) # The solution to your pde
y = Expression("x[1]", element=CG.ufl_element())
B = as_vector(((y*A).dx(1), -A.dx(0)))
V  = VectorFunctionSpace(mesh, "CG", degree)
Bp = project(B, V) # The vector function you're after
answered Apr 7, 2014 by mikael-mortensen FEniCS Expert (29,340 points)
selected Apr 7, 2014 by timm

Ok - that worked like a charm … but there was something I didn't really understand:
I had tried writing "y" as:

y = Expression("x[1]")

And the simulation would fail with:

Cannot get geometric dimension from an expression with no cell!
Traceback (most recent call last):
  File "poisson_rz.py", line 75, in <module>
    Bp = project(B, W)

But if I type it exactly as you put in your answer, it works fine. I'm sure that there is a big distinction between the two ways of creating "y" … but is there anywhere in the documentation that explains this? I see that in the online documentation that it mentions that "element=CG.ufl_element()" gives you a vector element?

The Expression("x[0]") carries no information about mesh or cell (could be 1D, 2D or 3D) and as it stands it can only be used at quadrature points. If you want to perform operations like differentiation, then you need to feed the element information to UFL as well. Not sure where or if this exact topic is documented, but I would try the UFL user manual.

Oh, thanks for explaining that

...