Ufl-interpolation - problem reproducing tutorial

Hi everyone,

I’m new to FEniCSx, so I’m trying to reproduce some of the examples in the tutorial. I’m now with the membrane deflection case, but I’m not able to interpolate the ufl expression as I get the following error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [7], in <cell line: 3>()
      1 # we interpolate p into the function space for visualization
      2 Q = fem.FunctionSpace(domain, ("CG", 5))
----> 3 expr = fem.Expression(p, Q.element.interpolation_points)
      4 pressure = fem.Function(Q)
      5 pressure.interpolate(expr)

File /usr/local/dolfinx-real/lib/python3.10/dist-packages/dolfinx/fem/function.py:98, in Expression.__init__(self, ufl_expression, X, form_compiler_params, jit_params, dtype)
     68 def __init__(self, ufl_expression: ufl.core.expr.Expr, X: np.ndarray,
     69              form_compiler_params: dict = {}, jit_params: dict = {},
     70              dtype=PETSc.ScalarType):
     71     """Create DOLFINx Expression.
     72 
     73     Represents a mathematical expression evaluated at a pre-defined
   (...)
     95 
     96     """
---> 98     assert X.ndim < 3
     99     num_points = X.shape[0] if X.ndim == 2 else 1
    100     _X = np.reshape(X, (num_points, -1))

AttributeError: 'builtin_function_or_method' object has no attribute 'ndim'

The code I’m using is

import gmsh
gmsh.initialize()
membrane = gmsh.model.occ.addDisk(0, 0, 0, 1, 1)
gmsh.model.occ.synchronize()
gdim = 2
gmsh.model.addPhysicalGroup(gdim, [membrane], 1)

gmsh.option.setNumber("Mesh.CharacteristicLengthMin",0.05)
gmsh.option.setNumber("Mesh.CharacteristicLengthMax",0.05)
gmsh.model.mesh.generate(gdim)

from dolfinx.io import gmshio
from mpi4py import MPI

gmsh_model_rank = 0
mesh_comm = MPI.COMM_WORLD
domain, cell_markers, facet_markers = gmshio.model_to_mesh(gmsh.model, mesh_comm, gmsh_model_rank, gdim=gdim)

from dolfinx import fem
V = fem.FunctionSpace(domain, ("CG", 1))

# spatially varying load (previously f)
import ufl
from petsc4py.PETSc import ScalarType

x = ufl.SpatialCoordinate(domain)
beta = fem.Constant(domain, ScalarType(12))
R0 = fem.Constant(domain, ScalarType(0.3))
p = 4 * ufl.exp(-beta**2 * (x[0]**2 + (x[1] - R0)**2))

import numpy as np
def on_boundary(x):
    return np.isclose(np.sqrt(x[0]**2 + x[1]**2), 1)
boundary_dofs = fem.locate_dofs_geometrical(V, on_boundary)

bc = fem.dirichletbc(ScalarType(0), boundary_dofs, V) # u=0 in the whole boundary

# like before, but with p instead of f
u = ufl.TrialFunction(V)
v = ufl.TestFunction(V)
a = ufl.dot(ufl.grad(u), ufl.grad(v)) * ufl.dx
L = p * v * ufl.dx
problem = fem.petsc.LinearProblem(a, L, bcs=[bc], petsc_options={"ksp_type": "preonly", "pc_type": "lu"})
uh = problem.solve()

# we interpolate p into the function space for visualization
Q = fem.FunctionSpace(domain, ("CG", 5))
expr = fem.Expression(p, Q.element.interpolation_points)
pressure = fem.Function(Q)
pressure.interpolate(expr)

I installed FEniCSx from Docker yesterday, so I guess I have the latest version. Is maybe the tutorial outdated? Thanks!

See my reply to: fem.Expression not working - #2 by dokken

1 Like