How to assgin values to the components of a mixed function

Dear all,

I want to assign values to the components of a mixed function, for example, I defined a mixed function s and its components are q and m:

element_for_q = VectorElement("CG", mesh.ufl_cell(), 2)
element_for_m = FiniteElement("CG", mesh.ufl_cell(), 1)
TH_mixed_element = element_for_q * element_for_m
V_s = FunctionSpace(mesh, TH_mixed_element)

s = Function(V_s)
q, m = split(s)

Does anyone know how to assign values to the components of the mixed function s?
I appreciate any help.

Please note that you have to specify what version of dolfin you are using (if it is legacy, or if it is DOLFINx what version).

Please also consider similar topics such as:

or

Thank you for your reply. I am using the latest legacy dolfin. the first link you provided seems to be the solution. But it is a little confusing.

Dear Dokken, the first link you provided is about assigning values to the components of a vector function. I think it is not the same as assigning values to the components of a mixed function. And I think maybe it can be realized without using FunctionAssigner? This is the first time I heard FunctionAssigner since I used Fenics, and I think it is not used frequently.

It is the same procedure for using mixed-elements and vector elements, and assign to a component of them. Please use FunctionAssigner to assign conditions to a mixed space from a collapsed sub-space.

Got it. Besides, on the internet I found another approach to do this just now. It is shown below:

mesh = UnitSquareMesh(32, 32)
element_for_q = VectorElement("CG", mesh.ufl_cell(), 2)
element_for_m = FiniteElement("CG", mesh.ufl_cell(), 1)
TH_mixed_element = element_for_q * element_for_m
V_s = FunctionSpace(mesh, TH_mixed_element)

s = Function(V_s)

#assume that we want to assign (12, 45) and 7 to the components of the mixed function.
q0 = interpolate(Constant((12, 45)), V_s.sub(0).collapse())
m0 = interpolate(Constant(7), V_s.sub(1).collapse())

assign(s, [q0, m0])
q, m = split(s)

Could you please tell me whether this approach is correct or not? Thank you very much. :slightly_smiling_face:

Did you run the code above, or are you asking me to run it to check for consistency?

As far as I can tell this calls
https://bitbucket.org/fenics-project/dolfin/src/1c52e837eb54cc34627f90bde254be4aa8a2ae17/python/src/function.cpp#lines-501:508
which creates a FunctionAssigner under the hood.

Please note that you can easily verify that the assigner does it job by saving the sub spaces to file and inspect the solution, rather than asking for feedback.