# Solving a vector equation with a known vector field

**URL:** <https://fenicsproject.discourse.group/t/solving-a-vector-equation-with-a-known-vector-field/11129>\
**Category:** General\
**Created:** [May 9, 2023, 2:52am UTC](https://fenicsproject.discourse.group/t/solving-a-vector-equation-with-a-known-vector-field/11129 "2023-05-09T02:52:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fcup2020](https://avatars.discourse-cdn.com/v4/letter/f/54ee81/32.png) [@Fcup2020](https://fenicsproject.discourse.group/u/Fcup2020)\
**Post date:** [May 9, 2023, 2:52am UTC](https://fenicsproject.discourse.group/t/solving-a-vector-equation-with-a-known-vector-field/11129/1 "2023-05-09T02:52:31Z")

</div>

Hi everyone,

I am new to FEniCS, I am trying to calculate the terms that make up the dynamic equation of a fluid. I have a question about one of the terms that satisfies the Poisson equation. I need to determine the term \vec{H}\_{vel} = Factor x \nabla^2 \vec{vel}. Here, vel is a known vector field and this equation has the condition of the normal derivative of vel is zero (similar to the first example of the Fenics tutorial).

In order to determine the Hvel, I have written the following code. But I do not sure if this is correct since vel is known. The result I get is not consistent with what I expect.

```auto
import numpy as np
import dolfinx
from dolfinx.fem import Function, FunctionSpace, Constant
from dolfinx import fem, mesh, plot
import ufl

Nx, Ny, Nz = 50, 50, 10
L, B, H = 100,100,10

mesh = dolfinx.mesh.create_box(MPI.COMM_WORLD, [np.array([0,0,0]), np.array([L, B, H])], [Nx,Ny,Nz])

V = fem.VectorFunctionSpace(mesh, ("CG", 1))
vel = Function(V)

def Velocity_Field(x):
    values = np.zeros((3, x.shape[1]))
    values[2, :] = 1
    return values

vel.interpolate(Velocity_Field)

Hvel = Function(V)
v = ufl.TestFunction(V)
F = Factor*ufl.inner(ufl.grad(vel), ufl.grad(v)) * ufl.dx - ufl.inner(Hvel, v) * ufl.dx

problem = fem.petsc.NonlinearProblem(F, Hvel)
solver = nls.petsc.NewtonSolver(MPI.COMM_WORLD, problem)

```

thanks

---

<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 9, 2023, 7:46am UTC](https://fenicsproject.discourse.group/t/solving-a-vector-equation-with-a-known-vector-field/11129/2 "2023-05-09T07:46:43Z")

</div>

A few notes:  
Please use latex formatting, H\_{vel} = Factor \nabla^2 vel and 3x` encapsulation:

````python
```python
import numpy as np
# .....

```

````

Currently you are setting the known field to be a constant value (0,0,1), whose gradient would be zero, and thus `Hvel` would be zero.

Also note that your problem is linear, so no need to use a non-linear solver.

---

<div class="post-metadata">

**Author:** ![Fcup2020](https://avatars.discourse-cdn.com/v4/letter/f/54ee81/32.png) [@Fcup2020](https://fenicsproject.discourse.group/u/Fcup2020)\
**Post date:** [May 9, 2023, 12:49pm UTC](https://fenicsproject.discourse.group/t/solving-a-vector-equation-with-a-known-vector-field/11129/3 "2023-05-09T12:49:51Z")

</div>

Hi dokken, thanks for the answer,

The vector field \vec{vel} can depend on space. I have placed a uniform field because it is the simplest case to test. I have tried to write the last part of the code using a linear solver but I obtain an error (according to Fenicsx tutorial): “RuntimeError: Cannot create sparsity pattern. Form is not a bilinear form”

```auto
a = Factor*ufl.inner(ufl.grad(vel), ufl.grad(v)) * ufl.dx
L = ufl.inner(Hvel, v) * ufl.dx

problem = fem.petsc.LinearProblem(a, L)
uh = problem.solve()

```

---

<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 9, 2023, 1:29pm UTC](https://fenicsproject.discourse.group/t/solving-a-vector-equation-with-a-known-vector-field/11129/4 "2023-05-09T13:29:12Z")

</div>

> [@Fcup2020](#):
>
> \vec{vel} can depend on space. I have placed a uniform field because it is the simplest case to test. I have tried to write the last part of the code using a linear solver but I obtain an error (according to Fenicsx tutorial): “RuntimeError: Cannot create sparsity pattern. Form is not a bilinear form”

You need to formulate the **bi-linear** form as `a`, and **linear** form as `L`, i.e.

```python
H = ufl.TrialFunction(V)
v = ufl.TestFunction(V)
a = ufl.inner(H, v) * ufl.dx
L = Factor*ufl.inner(ufl.grad(vel), ufl.grad(v)) * ufl.dx -
problem = dolfinx.fem.petsc.LinearProblem(a, L)
Hvel = problem.solve()

```
