RR_rr
September 23, 2024, 11:32am
1
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.
dokken
September 23, 2024, 2:15pm
2
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:
As you are working with a 3 dimensional mesh, the vector function space has three components:
T_S_1 = interpolate(S_11, T)
T_S_2 = interpolate(S_12, T)
T_s_0 = Function(T) # Function containing 0 values
assigner = FunctionAssigner(V, [T, T, T])
assigner.assign(v, [T_S_1, T_S_2, T_s_0])
or
Consider the following:
import dolfinx
from mpi4py import MPI
import dolfinx.io
import ufl
import numpy as np
mesh = dolfinx.UnitSquareMesh(MPI.COMM_WORLD, 10, 10)
el = ufl.FiniteElement("CG", mesh.ufl_cell(), 1)
mel = ufl.MixedElement([el, el])
V = dolfinx.FunctionSpace(mesh, mel)
num_subs = V.num_sub_spaces()
spaces = []
maps = []
for i in range(num_subs):
space_i, map_i = V.sub(i).collapse(collapsed_dofs=True)
spaces.append(space_i)
maps.append(map_i)
u = dolfinx.Function(V)
u…
RR_rr
September 23, 2024, 2:33pm
3
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.
RR_rr
September 23, 2024, 2:57pm
4
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.
dokken
September 23, 2024, 3:19pm
5
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.
RR_rr
September 23, 2024, 3:34pm
6
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.
dokken
September 23, 2024, 6:54pm
7
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.