Use values of a Function in a Vectorfield

Hello,
I defined a function analog to kappa in this tutorial

with kappa.vector.localForm() as loc:
    loc.setValues(cells_0, np.full(len(cells_0), 1))
    loc.setValues(cells_1, np.full(len(cells_1), 0.1))

Now I would like to use the values of this Function in a Vectorfield, for example I want a vectorfield (0.0, 0.0, respective value of kappa). I already tried multiplying (0.0, 0.0, 1.0)*kappa and some other things but I got

TypeError: can’t multiply sequence by non-int of type ‘Function’

Any help is appreciated. Thanks in advance!

I’m not sure if there is an easier/better solution, but my workaround seems to work:

  • define ei, ej, ek = ufl.unit_vectors(3)
  • values of kappa as vector can be used directly in variational problem L = ufl.dot(ek*kappa, v) * ds(2)

Consider something like:

import dolfinx
from mpi4py import MPI
import ufl

mesh = dolfinx.UnitCubeMesh(MPI.COMM_WORLD, 5, 5, 5)
Q = dolfinx.FunctionSpace(mesh, ("DG", 0))

def Omega_0(x):
    return x[1] <= 0.5

def Omega_1(x):
    return x[1] >= 0.5

kappa = dolfinx.Function(Q)
cells_0 = dolfinx.mesh.locate_entities(mesh, mesh.topology.dim, Omega_0)
cells_1 = dolfinx.mesh.locate_entities(mesh, mesh.topology.dim, Omega_1)

import numpy as np
with kappa.vector.localForm() as loc:
    loc.setValues(cells_0, np.full(len(cells_0), 1))
    loc.setValues(cells_1, np.full(len(cells_1), 0.1))

V = dolfinx.VectorFunctionSpace(mesh, ("CG", 1))
u = dolfinx.Function(V)
a = ufl.inner(u, ufl.as_vector((0,0,kappa))) * ufl.dx
print(dolfinx.fem.assemble_scalar(a))
1 Like