### Assigning expression to function in multi dimensional function space

90
views
0
7 weeks ago by
Hey, so I'm interested in assigning an expression to one of the functions in a 5 tuple function space: I attempted the approach to interpolate as seen here, but apparently that doesn't work. https://fenicsproject.org/qa/3829/assign-expression-to-function/ . I want to assign this function to something non-zero (fenics defaults to 0), and have it consistent, so that when I split it, what I call 'w', here is what my guess is.

from dolfin import *
import matplotlib.pyplot as plt
import numpy as np
import math

x_size =32
y_size=32

p1 = Point(-1.0,-1.0)
p2 = Point(1.0,1.0)
mesh = RectangleMesh(p1,p2,x_size,y_size)
element = VectorElement('CG', triangle, 1, dim=5)
V = FunctionSpace(mesh, element)
u=Function(V)

guess = Expression(".5*sin(x[0]*x[1]) +.01",degree=2)
u.sub(2).interpolate(guess)

u_r,u_i,w,lambo,lambt = split(u)

myFile = File("guess.pvd")
myFile << u.sub(2)
​

Which yields the error

Traceback (most recent call last):
File "example.py", line 18, in <module>
u.sub(2).interpolate(guess)
RuntimeError:

*** -------------------------------------------------------------------------
*** DOLFIN encountered an error. If you are not able to resolve this issue
*** using the information listed below, you can ask for help at
***
***
*** Remember to include the error message listed below and, if possible,
*** include a *minimal* running example to reproduce the error.
***
*** -------------------------------------------------------------------------
*** Error:   Unable to interpolate function into function space.
*** Reason:  Wrong size of vector.
*** Where:   This error was encountered inside FunctionSpace.cpp.
*** Process: 0
***
*** DOLFIN version: 2017.2.0
*** Git changeset:
*** -------------------------------------------------------------------------

Abort trap: 6
​

Is this a trivial fix? Thank you.
Community: FEniCS Project
I tried this as well....still doesn't work

u.sub(2).assign(interpolate(guess, V.sub(2).collapse()))
written 7 weeks ago by Ryan Vogt

3
7 weeks ago by
You want to use ''FunctionAssigner''

from dolfin import *
import matplotlib.pyplot as plt
import numpy as np
import math

x_size = 32
y_size = 32

p1 = Point(-1.0,-1.0)
p2 = Point(1.0,1.0)
mesh = RectangleMesh(p1,p2,x_size,y_size)
element = VectorElement('CG', triangle, 1, dim=5)
V = FunctionSpace(mesh, element)
u = Function(V)

V1 = V.sub(2).collapse()
assigner_V1 = FunctionAssigner(V.sub(2), V1)
guess = interpolate(Expression(".5*sin(x[0]*x[1]) +.01",degree=2), V1)
assigner_V1.assign(u.sub(2), guess)

u_r,u_i,w,lambo,lambt = split(u)

myFile = File("guess.pvd")
myFile << u.sub(2)​
Dear Miguel,

Thank you so much. I have also found that this works perfectly fine as well assign(u.sub(2),interpolate(guess,V.sub(2).collapse()))
written 7 weeks ago by Ryan Vogt
0
7 weeks ago by
I will also add another way, (Miguels works great too!)

This will allow the correct assignment.

assign(u.sub(2),interpolate(guess,V.sub(2).collapse()))