Forum Message

 

 

We have moved the forum to https://forum.ngsolve.org . This is an archived version of the topics until 05/05/23. All the topics were moved to the new forum and conversations can be continued there. This forum is just kept as legacy to not invalidate old links. If you want to continue a conversation just look for the topic in the new forum.

Notice

The forum is in read only mode.

Interpolation from vector expression to vector function

More
6 years 4 months ago #295 by Karatza
Dear NGSolve users and developers,

I read the section "Interpolate a CoefficientFunction" of

ngsolve.org/docu/nightly/i-tutorials/uni...ficientfunction.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 = 2*u1 u21 = 3*u1 u2.components[0].Set(u20) u2.components[1].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[0].Set(u2_double[0]) # RuntimeError: don't know my dimension, space is N6ngcomp15CompoundFESpaceE u3.components[1].Set(u2_double[1]) print(u3.vec.Norm(), u1.vec.Norm()*2)
More
6 years 4 months ago #299 by christopher
This should work:
Code:
u2_double = CoefficientFunction((2*u2.components[0],2*u2.components[1])) u3.components[0].Set(u2_double[0]) u3.components[1].Set(u2_double[1])
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:
Code:
V2 = H1(mesh,order=1,dim=2) u3 = GridFunction(V2) u1_double = CoefficientFunction((2*u1, 2*u1)) u3.Set(u1_double)

Best
Christopher
The following user(s) said Thank You: Karatza
More
6 years 4 months ago #302 by Karatza
Deeply thank you it works perfect now!
Time to create page: 0.110 seconds