# How to Create a Vector Function in Function Space 2 from Functions in Function Space 1?

Fenics Version: 2019.2.0.13.dev0

Within my existing code I want to create a vector function \vec{v} on a new vector space V_2 from two known functions on a different vector space V_1. One function is a solution of a problem, the other is a known/specified function.

Let \vec{u} = \langle u_1, u_2 \rangle be my solution function in V_1 and \vec{w} = \langle w_1, w_2 \rangle be my known/specified function also in V_1.

I want to create something similar to \vec{v} = \langle (3u_1 + 6u_2)w_{1x}, (5u_1 - 2u_2)w_{2x} \rangle in V_2.

Some example code of how I am trying of doing this within my existing code:

from fenics import *

mesh = IntervalMesh(50, 0.0, 10.0)
P1 = FiniteElement('CG', interval, 1)
P2 = FiniteElement('CG', interval, 2)
V1 = FunctionSpace(mesh, MixedElement([P1, P1]))
V2 = FunctionSpace(mesh, MixedElement([P2, P2]))
p1 = Expression('10.0 * pow(x[0] - 5.0, 2) * pow(x[0] - 6.0, 2)', degree=4)
p2 = Expression('0.0', degree=4)
p = Expression(('x[0] <= 5.0 ? p2 : x[0] >= 6.0 ? p2 : p1', '0.0'), p1=p1, p2=p2, degree=4)
w = interpolate(p, V1)

"""
u is solved as a vector function in V1 for some problem
a stand in for u is used below to be able to run the code
"""
u = Function(V1)
v1 = (3.0 * u[0] + 6.0 * u[1]) * w[0].dx(0)
v2 = (5.0 * u[0] - 2.0 * u[1]) * w[1].dx(0)

"""
create v = (v1, v2) in V2 somehow in FEniCS
"""


Look at the documention of the assign free function, and the sub method of Function.

Something like

assign(destination_function.sub(0), source_function_a.sub(0))
assign(destination_function.sub(1), source_function_b.sub(1))


Thank you for your help. When I reformatted the code to use your work I found out that v_1 and v_2 were Products, not Functions, so when I tried to use your notation it errored out saying that

AttributeError: 'Product' object has no attribute '_cpp_object'

That then led me to this thread when I tried to interpolate() the Product to become a Function. I then used project() instead of interpolate() and it appeared to work.

My modified code that appears to run is below, though I am not sure if this is the best way to get it working. Please let me know if there are any better ways to set it up than what I have done.

from fenics import *

mesh = IntervalMesh(50, 0.0, 10.0)
P1 = FiniteElement('CG', interval, 1)
P2 = FiniteElement('CG', interval, 2)
V1 = FunctionSpace(mesh, MixedElement([P1, P1]))
V2 = FunctionSpace(mesh, MixedElement([P2, P2]))
V1_scalar = FunctionSpace(mesh, P1)
V2_scalar = FunctionSpace(mesh, P1)
p1 = Expression('10.0 * pow(x[0] - 5.0, 2) * pow(x[0] - 6.0, 2)', degree=4)
p2 = Expression('0.0', degree=4)
p = Expression(('x[0] <= 5.0 ? p2 : x[0] >= 6.0 ? p2 : p1', '0.0'), p1=p1, p2=p2, degree=4)
w = interpolate(p, V1)

"""
u is solved as a vector function in V1 with two entries u_1 and u_2 for some problem
"""
u = Function(V1)
v1 = (3.0 * u[0] + 6.0 * u[1]) * w[0].dx(0)
v2 = (5.0 * u[0] - 2.0 * u[1]) * w[1].dx(0)
v1 = project(v1, V1_scalar)
v2 = project(v2, V1_scalar)
v = Function(V1)
assign(v.sub(0), v1)
assign(v.sub(1), v2)
v_new = interpolate(v, V2)