Hi everyone, I have 2 questions. Consider below the simple implementation of a Poisson equation.
from mpi4py import MPI
from dolfinx import mesh
domain = mesh.create_unit_square(MPI.COMM_WORLD, 8, 8, mesh.CellType.quadrilateral)
from dolfinx.fem import FunctionSpace
V = FunctionSpace(domain, ("Lagrange", 1))
from dolfinx import fem
uD = fem.Function(V)
uD.interpolate(lambda x: 1 + x[0]**2 + 2 * x[1]**2)
import numpy
# Create facet to cell connectivity required to determine boundary facets
tdim = domain.topology.dim
fdim = tdim - 1
domain.topology.create_connectivity(fdim, tdim)
boundary_facets = mesh.exterior_facet_indices(domain.topology)
boundary_dofs = fem.locate_dofs_topological(V, fdim, boundary_facets)
bc = fem.dirichletbc(uD, boundary_dofs)
import ufl
u = ufl.TrialFunction(V)
v = ufl.TestFunction(V)
from dolfinx import default_scalar_type
f = fem.Constant(domain, default_scalar_type(-6))
a = ufl.dot(ufl.grad(u), ufl.grad(v)) * ufl.dx
L = f * v * ufl.dx
from dolfinx.fem.petsc import LinearProblem
problem = LinearProblem(a, L, bcs=[bc], petsc_options={"ksp_type": "preonly", "pc_type": "lu"})
uh = problem.solve()
Here are my 2 questions:
- I have calculated the gradient of
uh
in X and Y directions by writing a code similar to what I saw here (Also attached the code below). I see that this is particularly useful when making calculations using the gradients. But, is there an easier way of extracting the gradient in X & Y direction? My objective is to be able to map and find the gradient on each direction to a point in the mesh. - How to interpolate the X and Y gradients at a random point (let’s say (0.5, 0.5)) on the geometry? I am looking for a way similar to the function ‘evaluateGradient’ in MATLAB.
MWE for gradient in X-direction:
x = SpatialCoordinate(domain)
x_grad = inner(as_vector((1, 0)), grad(uh))
W = fem.FunctionSpace(domain, ("DQ", 1))
expr = fem.Expression(x_grad, W.element.interpolation_points())
w = fem.Function(W)
w.interpolate(expr)
Thank you!