Problem with assemble_vector

Hello, I have a problem with assemble_vector

here is my code :

L = 1. # total length
d = L/10. # thickness
h = d/6. # size of a cell

my_domain = dolfinx.mesh.create_rectangle(comm=MPI.COMM_WORLD,
                            points=((0.0, -0.5*d), (L, 0.5*d)), n=(int(L/h), int(d/h)),
                            cell_type=dolfinx.mesh.CellType.triangle)

V = dolfinx.fem.VectorFunctionSpace(my_domain, ("CG", 2))
v = ufl.TestFunction(V)
u = dolfinx.fem.Function(V)

def left(x):
    return np.isclose(x[0], 0)

def right(x):
    return np.isclose(x[0], L)

fdim = my_domain.topology.dim -1
left_facets = dolfinx.mesh.locate_entities_boundary(my_domain, fdim, left)
right_facets = dolfinx.mesh.locate_entities_boundary(my_domain, fdim, right)

# Concatenate and sort the arrays based on facet indices. Left facets marked with 1, right facets with 2
marked_facets = np.hstack([left_facets, right_facets])
marked_values = np.hstack([np.full_like(left_facets, 1), np.full_like(right_facets, 2)])
sorted_facets = np.argsort(marked_facets)
facet_tag = dolfinx.mesh.meshtags(my_domain, fdim, marked_facets[sorted_facets], marked_values[sorted_facets])

ds = ufl.Measure('ds', domain=my_domain, subdomain_data=facet_tag)

dolfinx.fem.petsc.assemble_vector(dolfinx.fem.form(u*ds(2)))

and here is the error :

Can only integrate scalar expressions. The integrand is a tensor expression with value shape (2,) and free indices with labels ().

ERROR:UFL:Can only integrate scalar expressions. The integrand is a tensor expression with value shape (2,) and free indices with labels ().

---------------------------------------------------------------------------
UFLException                              Traceback (most recent call last)
Cell In [17], line 1
----> 1 dolfinx.fem.petsc.assemble_vector(dolfinx.fem.form(u*ds(2)))

File ~/anaconda3/envs/fenicsx-0.5.1/lib/python3.9/site-packages/ufl/measure.py:408, in Measure.__rmul__(self, integrand)
    406 # Allow only scalar integrands
    407 if not is_true_ufl_scalar(integrand):
--> 408     error("Can only integrate scalar expressions. The integrand is a "
    409           "tensor expression with value shape %s and free indices with labels %s." %
    410           (integrand.ufl_shape, integrand.ufl_free_indices))
    412 # If we have a tuple of domain ids build the integrals one by
    413 # one and construct as a Form in one go.
    414 subdomain_id = self.subdomain_id()

File ~/anaconda3/envs/fenicsx-0.5.1/lib/python3.9/site-packages/ufl/log.py:158, in Logger.error(self, *message)
    156 "Write error message and raise an exception."
    157 self._log.error(*message)
--> 158 raise self._exception_type(self._format_raw(*message))

UFLException: Can only integrate scalar expressions. The integrand is a tensor expression with value shape (2,) and free indices with labels ().


many thanks in advance!

Claire

To assemble into a vector, your exprssion needs to be of rank one, i.e. it needs to have a test-function in the form.

Secondly, you cannot assemble a vector quantity. You can assemble the following:

v0 = dolfinx.fem.assemble_scalar(dolfinx.fem.form(u[0]*ds(2)))
v1 = dolfinx.fem.assemble_scalar(dolfinx.fem.form(u[1]*ds(2)))
1 Like