# Cross product in 2D

**URL:** <https://fenicsproject.discourse.group/t/cross-product-in-2d/8246>\
**Category:** Errors\
**Created:** [May 4, 2022, 10:08pm UTC](https://fenicsproject.discourse.group/t/cross-product-in-2d/8246 "2022-05-04T22:08:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![renol-kth](https://avatars.discourse-cdn.com/v4/letter/r/ed655f/32.png) [@renol-kth](https://fenicsproject.discourse.group/u/renol-kth)\
**Post date:** [May 4, 2022, 10:08pm UTC](https://fenicsproject.discourse.group/t/cross-product-in-2d/8246/1 "2022-05-04T22:08:55Z")

</div>

Dear community,

I would like to solve the problem to find \mathbf{u}\in \Omega \subset \mathbb{R}^2, s.t.

\mathbf{u} \times \mathbf{B} = \mathbf{f}

for a given \mathbf{B} that is normal to \Omega. So I multiply with a testfunction, integrate and get

\int\_{\Omega}( \mathbf{u} \times \mathbf{B}) \cdot \mathbf{v} \text{ dx}= \int\_{\Omega} \mathbf{f}\cdot \mathbf{v} \text{ dx}.

The UFL cross operator is only defined for 3D vectors, so I tried to implement my own 2D version:

\quad \mathbf{u}: = \begin{bmatrix} u\_1\\u\_2\\ u\_3 \end{bmatrix}, \quad \mathbf{B} := \begin{bmatrix} 0 \\ 0 \\B \end{bmatrix} \quad \Longrightarrow \mathbf{u} \times \mathbf{B} =\begin{bmatrix} u\_2B \\ -u\_1B \\0 \end{bmatrix}

and in 2D this is just

\mathbf{u} \times \mathbf{B} := \begin{bmatrix} u\_2B \\ -u\_1B \end{bmatrix}.

I tried to implement that as follows

```auto
# mesh and spaces
mesh = UnitSquareMesh(8, 8)
V = FunctionSpace(mesh, 'Lagrange', 2)
u = TrialFunction(V)
v = TestFunction(V)

# BCs
u_D = Constant('0')
def boundary(x, on_boundary):
    return on_boundary
bc = DirichletBC(V, u_D, boundary)

# equations
f = Constant('-10')
B = Constant(('0','0','10'))
uxB = ((u[1]*B[2],-u[0]*B[2]))
a = (dot(uxB,v))*dx
L = f*v*dx
u = Function(V)
solve (a==L, u, bc)

plot(mesh)
plot(u)

```

which results in

```auto
---> 17 uxB = ((u[1]*B[2],-u[0]*B[2]))

....

IndexError: tuple index out of range

```

So apparently, u as a FEniCS trialfunction object is not really a 2D array, or I can not access it as such.  
I tried to define `uxB` as

```auto
uxB = ((u[1]*B[2],-u[0]*B[2])).vector()

```

or as

```auto
uxB = as_vector((u[1]*B[2],-u[0]*B[2]))

```

but still get the same error.

Could someone explain to me, how can one implement such a problem in 2D?

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [May 4, 2022, 10:27pm UTC](https://fenicsproject.discourse.group/t/cross-product-in-2d/8246/2 "2022-05-04T22:27:48Z")

</div>

> [@renol-kth](#):
>
> `V = FunctionSpace(mesh, 'Lagrange', 2)`

This should be a `VectorFunctionSpace` if you want u to have an x and y component.

---

<div class="post-metadata">

**Author:** ![renol-kth](https://avatars.discourse-cdn.com/v4/letter/r/ed655f/32.png) [@renol-kth](https://fenicsproject.discourse.group/u/renol-kth)\
**Post date:** [May 5, 2022, 2:56pm UTC](https://fenicsproject.discourse.group/t/cross-product-in-2d/8246/3 "2022-05-05T14:56:14Z")

</div>

Many many thanks, that helped!

But the simple Poisson equation \Delta\mathbf{u} = -\mathbf{f} is also a vector-valued function, for \mathbf{u}, \mathbf{f} \in \Omega \subset \mathbb{R}^2.  
Why does one not have to use `V = VectorFunctionSpace(...)` in this example?

---

<div class="post-metadata">

**Author:** ![nate](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/nate/32/17_2.png) [@nate](https://fenicsproject.discourse.group/u/nate)\
**Post date:** [May 5, 2022, 3:07pm UTC](https://fenicsproject.discourse.group/t/cross-product-in-2d/8246/4 "2022-05-05T15:07:58Z")

</div>

The standard [Poisson](https://en.wikipedia.org/wiki/Poisson%27s_equation) equation does not require a vector valued solution. We typically would refer to \Delta \vec{u} = \vec{0} as the _vector_ Laplace problem or the _vector_ Poisson problem in the inhomogeneous case.

The vector calculus operations, e.g., \nabla \cdot (\cdot), \nabla (\cdot) and \nabla \times (\cdot) are implicitly defined by their argument. Assuming a Cartesian coordinate system, this is the same with `ufl`'s symbolic algebra via `ufl.div`, `ufl.grad` and `ufl.curl`, respectively. So whether you use a scalar or vector definition for your solution approximation, u\_h, `ufl` will interpret the vector calculus operations implicitly.

---

<div class="post-metadata">

**Author:** ![renol-kth](https://avatars.discourse-cdn.com/v4/letter/r/ed655f/32.png) [@renol-kth](https://fenicsproject.discourse.group/u/renol-kth)\
**Post date:** [May 5, 2022, 10:40pm UTC](https://fenicsproject.discourse.group/t/cross-product-in-2d/8246/5 "2022-05-05T22:40:04Z")

</div>

Thanks, that makes sense!
