# Interpolation from vector expression to vector function

Dear NGSolve users and developers,

I read the section “Interpolate a CoefficientFunction” of

https://ngsolve.org/docu/nightly/i-tutorials/unit-1.2-coefficient/coefficientfunction.html

and I would like to interpolate a vector expression to a vector function following the same
snippet. How should I do that?

Below you can find a code where I try to
a) interpolate from scalar expression to scalar space: this is working correctly
b) interpolate from scalar expression to each component of vector space: this is working correctly
c) my case, interpolate from vector expression to vector function, and two ways I tried, and the corresponding errors

Thanks,
best regards,
Makis

[code]from ngsolve import *
from netgen.geom2d import unit_square
ngsglobals.msg_level = 0

mesh = Mesh (unit_square.GenerateMesh(maxh=0.2))

myfunc = x*(1-x)

# a) Interpolate from scalar expression to scalar space: working

V1 = H1(mesh, order=1)
u1 = GridFunction(V1)
u1.Set(myfunc)
print(u1.vec.Norm())

# b) Interpolate from scalar expression to each component of vector space: working

V2 = FESpace([V1, V1])
u2 = GridFunction(V2)
u20 = 2u1
u21 = 3
u1
u2.components.Set(u20)
u2.components.Set(u21)
print(u2.vec.Norm(), u1.vec.Norm()*sqrt(13))

# c) Interpolate from vector expression to vector function

u3 = GridFunction(V2)
u2_double = 2*u2

# c1)

u3.Set(u2_double) # Segmentation fault

# c2)

u3.components.Set(u2_double) # RuntimeError: don’t know my dimension, space is N6ngcomp15CompoundFESpaceE
u3.components.Set(u2_double)
print(u3.vec.Norm(), u1.vec.Norm()*2)[/code]

This should work:

``````u2_double = CoefficientFunction((2*u2.components,2*u2.components))
u3.components.Set(u2_double)
u3.components.Set(u2_double)``````

This creates a vector valued CoefficientFunction.
The problem here is, that set would require a vectorized space to be set with a vectorized coefficient function, but you have a vector of spaces (the internal layout of the dofs is different)
If you use a vectorized space then you can do that:

``````V2 = H1(mesh,order=1,dim=2)
u3 = GridFunction(V2)
u1_double = CoefficientFunction((2*u1, 2*u1))
u3.Set(u1_double)``````

Best
Christopher

Deeply thank you it works perfect now!