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

90

views

0

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.

Which yields the error

Is this a trivial fix? Thank you.

```
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
***
*** fenics-support@googlegroups.com
***
*** 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

### 2 Answers

3

You want to use ''FunctionAssigner''

Dear Miguel,

Thank you so much. I have also found that this works perfectly fine as well

```
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)
```

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

I will also add another way, (Miguels works great too!)

This will allow the correct assignment.

This will allow the correct assignment.

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

Please login to add an answer/comment or follow this question.

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