Mixed vector element post-processing of derived fields

hi

I have a nonlinear PDE problem with 2 vector fields as primary variables, u and wf. I am able to solve it and plot the corresponding solution fields, u_h and wf_h. However, I am stuck plotting derived fields getting the following error “TypeError: ‘numpy.ndarray’ object is not callable”. I am closely following the DOLFINX elasticity demo https://docs.fenicsproject.org/dolfinx/v0.5.0/python/demos/demo_elasticity.html, the only difference is the use of a mixed element.

I am just showing the essential pieces of code underneath:

from contextlib import ExitStack
import numpy as np
import ufl
from dolfinx import fem, nls, mesh, plot, log, io
from mpi4py import MPI
from petsc4py import PETSc


# ----------------------------------------------------------------------------------------------------------------------
# Create a box mesh of a beam
L = 20.0
H = 1.0
msh = mesh.create_box(MPI.COMM_WORLD, [np.array([0.0, 0.0, 0.0]), np.array([L, H, H])], [10, 1, 1],
                      mesh.CellType.hexahedron)
P1 = ufl.VectorElement("CG", msh.ufl_cell(), 1, dim=3)  # Lagrange family, 1st order
P2 = ufl.VectorElement("CG", msh.ufl_cell(), 1, dim=3)  # Lagrange family, 1st order
VY = fem.FunctionSpace(msh, ufl.MixedElement([P1, P2]))

# ----------------------------------------------------------------------------------------------------------------------
# Function space
v, yf = ufl.TestFunctions(VY)     # test functions
uwf = fem.Function(VY)            # combined current displacement and change of director fields
u, wf = ufl.split(uwf)

# ----------------------------------------------------------------------------------------------------------------------
# Problem formulation

# Spatial dimension
d = len(u)

def defGradTens(u_h):
    """Return an expression for the deformation gradient field"""
    return ufl.Identity(d) + ufl.grad(u_h)

..... skipping some lines of code which are not relevant

# Initialize nonlinear problem
problem = fem.petsc.NonlinearProblem(Pi, uwf, bcs)

# ----------------------------------------------------------------------------------------------------------------------
# solving the problem 

u_h = uwf.sub(0)
wf_h = uwf.sub(1)

for n in range(1, 2):
    num_its, converged = solver.solve(uwf)
    
    # plotting of derived fields
    W = fem.FunctionSpace(msh, ("Discontinuous Lagrange", 0))
    F_expr = fem.Expression(defGradTens(u_h), W.element.interpolation_points())
    F_h = Function(W)
    F_h.interpolate(F_expr)
    file.write_function(F_h, n)

The error occurs executing the line “F_expr = fem.Expression(defGradTens(u_h), W.element.interpolation_points())”

Please advise. Many thanks in advance.

Which version of DOLFINx are you using?
Have you tried replacing

With F_expr = fem.Expression(defGradTens(u_h), W.element.interpolation_points)
as this was the syntax in v0.4.1

I am using DOLFINx version: 0.4.1 from the Ubuntu 22.04 repository. Yes, your suggestion fixed the error. Thanks.

However, now I am getting “RuntimeError: Function value size not equal to Expression value size” executing line " F_h.interpolate(F_expr)"

I am interpolating a 2nd-order tensor field and the corresponding expression is returned by the function defGradTens(u_h).

You should then use a TensorFunctionSpace

Thanks, that makes sense and works.