Solving a vector equation with a known vector field

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.

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


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)


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

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.

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”

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()

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

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()