I’m trying to do this projection as part of a post-processing step. I have a Function over the entire volume, and I want to plot an expression containing that Function on a few boundaries.
I think this should illustrate what I’m trying to do:
import numpy as np
from dolfinx import fem, mesh
from mpi4py import MPI
import pyvista4dolfinx as p4d
import ufl
domain = mesh.create_unit_square(MPI.COMM_WORLD, 10, 10, mesh.CellType.triangle)
# Simple demonstration vector function f(x, y) = (x, y)
V = fem.functionspace(domain, ("Lagrange", 1, (2,)))
u = fem.Function(V)
x = ufl.SpatialCoordinate(domain)
u.interpolate(lambda x: (x[0] + 0.1, x[1] + 0.1))
p4d.plot(u, show=True, factor=0.1)
# Create a subdomain over the top boundary
fdim = domain.topology.dim - 1
top_facets = mesh.locate_entities_boundary(
domain, fdim, lambda x: np.isclose(x[1], 1.0)
)
subdomain, entity_map, _, _ = mesh.create_submesh(domain, fdim, top_facets)
V_boundary = fem.functionspace(subdomain, ("Lagrange", 1, (2,)))
u_sub = fem.Function(V_boundary)
expr = fem.Expression(
u, V_boundary.element.interpolation_points, entity_maps=[entity_map]
)
u_sub.interpolate(expr)
plt = p4d.plot(u_sub, factor=0.1)
p4d.plot(domain, plotter=plt, show=True)
But it’s not working:
[0]PETSC ERROR: ------------------------------------------------------------------------
[0]PETSC ERROR: Caught signal number 11 SEGV: Segmentation Violation, probably memory access out of range
[0]PETSC ERROR: Try option -start_in_debugger or -on_error_attach_debugger
[0]PETSC ERROR: or see https://petsc.org/release/faq/#valgrind and https://petsc.org/release/faq/
[0]PETSC ERROR: configure using --with-debugging=yes, recompile, link, and run
[0]PETSC ERROR: to get more information on the crash.
[0]PETSC ERROR: Run with -malloc_debug to check if memory corruption is causing the crash.
Abort(59) on node 0 (rank 0 in comm 0): application called MPI_Abort(MPI_COMM_WORLD, 59) - process 0
I’ve searched the forum and found a few attempts at doing precisely what I’m trying to do, but they’re all fairly outdated. I also looked at several interpolation and expression tests, nothing got me quite there.
There’s an example with an expression over quadrature points, but the only option seems to be to eval, which is not really helpful for plotting.
The closest I got was by handrolling an L2 projector, but there’s always a single facet with a completely wrong value that messes up the visualization (see issue 4291 in github).
Some help is really appreciated! Thanks!