Dear all,
I’m trying to project the norm of the gradient of a solution into a function space. In the FEniCs version, this operation was possible using project(). In FEniCSx, this procedure is not as simple.
I followed a discussion (Find gradient of solution in dolfinx) on this subject and created a function for projection but I always get an error.
The equation I solve is the poisson equation with the source term zero
Here’s the function I’m using and the error
def normalizeAndProj_vec(u, mesh, V):
"""
Normalizes the gradient of the solution u and projects it into the specified V.
Parameters:
u (Function): The solution whose gradient is to be normalized.
mesh (Mesh): The mesh on which the function is defined.
V (FunctionSpace): The function space into which the normalized gradient is to be projected.
Returns:
Function: The normalized gradient projected into FiberSpace.
"""
# Calculate the gradient of u
grad_u = grad(u)
# Define the test and trial functions
v = TestFunction(V)
w = TrialFunction(V)
# Define the variational problem for the projection
a = inner(w, v) * dx
L = inner(grad_u, v) * dx
# Solve the projection problem
grad_uh = fem.Function(V)
problem = fem.petsc.LinearProblem(a, L)
grad_uh = problem.solve()
# Normalize the projected gradient
norm_grad_uh = fem.Function(V)
norm_uh_expr = grad_uh / sqrt(dot(grad_uh, grad_uh))
# Interpolate the normalized gradient
norm_grad_uh.interpolate(lambda x: norm_uh_expr.eval(x))
return norm_grad_uh
the error
File "/home/djoumessi/miniconda3/envs/fenicsx-env/lib/python3.12/site-packages/ufl/operators.py", line 167, in inner
return Inner(a, b)
^^^^^^^^^^^
File "/home/djoumessi/miniconda3/envs/fenicsx-env/lib/python3.12/site-packages/ufl/tensoralgebra.py", line 162, in __new__
raise ValueError(f"Shapes do not match: {ufl_err_str(a)} and {ufl_err_str(b)}")
ValueError: Shapes do not match: <Grad id=139755148445568> and <Argument id=139756261500464>
I was wondering if there wasn’t a simple approach to this?
Thanks in advance.