Get current spatial configuration of the mesh as ufl variable

Hello all,

I am quite new to FENiCSx so my problem could be very basic. How do i get the current configuration (basically the nodal coordinates) as a ufl.variable which can be then used to create the expressions in the bilinear weak form. Here’s the code that i am using to implement this:


from dolfinx import log, default_scalar_type
from dolfinx.fem.petsc import NonlinearProblem
from dolfinx.nls.petsc import NewtonSolver
import matplotlib.pyplot as plt
import pyvista
from dolfinx import nls
import numpy as np
import ufl

from mpi4py import MPI
from dolfinx import fem, mesh, plot

# mesh domain:
L  = 0.5
Nx =  20
Ny =  20
# L = 20.0
msh = mesh.create_rectangle(MPI.COMM_WORLD, [np.array([-2, -1]), np.array([2, 1])], [Nx, Ny], mesh.CellType.quadrilateral)
tdim = msh.topology.dim

## Assign function space to the mesh

# Build function space with Lagrange multiplier
V123 = ufl.FiniteElement("Lagrange", msh.ufl_cell(), 1, 3)  #finite element for discplacement variable
Vp = ufl.FiniteElement("Real", msh.ufl_cell(), 0)           # finite element space for lagrange multipliers
V   = fem.FunctionSpace(msh, ufl.MixedElement([V123,Vp,Vp,Vp,Vp,Vp,Vp]))                # mixed function space

v                        = ufl.TestFunction(V)   # create test function space
(v123,lm_lambda1,lm_lambda2,lm_lambda3,lm_mu1,lm_mu2,lm_mu3)   = ufl.split(v)

dy                      = ufl.TrialFunction(V)
(dy123,dlm_lambda1,dlm_lambda2, dlm_lambda3,dlm_mu1,dlm_mu2,dlm_mu3) = ufl.split(dy)

x_ref                       = fem.Function(V, name="displacement")

# split the function into u_ref (displacement variable) and other lagrange multiplier variables

(u_ref,lambda1_ref,lambda2_ref, lambda3_ref,mu1_ref,mu2_ref,mu3_ref)     = ufl.split(x_ref)

print(x_ref.x.array.shape)
X0_ref = ufl.SpatialCoordinate(msh)
x0_current = ufl.variable(X0_ref + u_ref)   # current coordinates = reference_coordinate + displacement

Error that i get is as follows:

(441,)
Traceback (most recent call last):
  File "/home/.../fenics_try.py", line 83, in <module>
    x0_current = ufl.variable(X0_ref+u_ref)
                              ~~~~~~^~~~~~
  File "/home/.../miniconda3/envs/fenicsx-env/lib/python3.12/site-packages/ufl/exproperators.py", line 204, in _add
    return Sum(self, o)
           ^^^^^^^^^^^^
  File "/home/.../miniconda3/envs/fenicsx-env/lib/python3.12/site-packages/ufl/algebra.py", line 41, in __new__
    raise ValueError("Can't add expressions with different shapes.")
ValueError: Can't add expressions with different shapes.

Can someone help we this?
Thanks

2 Likes

Please note that Real-spaces are currently not supported in DOLFINx.

As far as I can understand your question, would x = ufl.SpatialCoordinate(mesh) give you the coordinate of your current configuration (at quadrature point)?