Impose initial value for control variables in mixed element funciton space

I’m solving a problem where I have two variables that I optimize simultaneously. I would like to initialize them to two different values, respectively. I use Dolfin 2019. In my code, I do:

import dolfin
import ufl

v1 = FiniteElement("Lagrange", mesh.ufl_cell(), 1)
v2 = FiniteElement("R", mesh.ufl_cell(), 0)
ME = MixedElement([v1, v2])
FS = FunctionSpace(mesh, ME)

ini_val = Expression(("X0", "X1"), degree=1, X0=4.0, X1=0.5)
interpolate(ini_val, FS)

Which does not output any error, but doesn’t seem to work.
Can anyone let me know what’s not working?

It does output error with your code. For example, you do not provide mesh. And based on the context, you should

from dolfin import *

rather than

The function interpolate returns a Function, see API: https://fenicsproject.org/olddocs/dolfin/2019.1.0/python/_autogenerated/dolfin.cpp.function.html?highlight=interpolate#dolfin.cpp.function.interpolate

Consider the following:

from dolfin import *

mesh = UnitSquareMesh(4, 4)

v1 = FiniteElement("Lagrange", mesh.ufl_cell(), 1)
v2 = FiniteElement("R", mesh.ufl_cell(), 0)
ME = MixedElement([v1, v2])
FS = FunctionSpace(mesh, ME)
F  = Function(FS)

ini_val = Expression(("X0", "X1"), degree=1, X0=4.0, X1=0.5)
f = interpolate(ini_val, FS)
f1, f2 = f.split()
# Or
F.assign(f)
print(F.vector()[:])
2 Likes