### How can I scale a subspace

248

views

0

If I declare a mixed space like so:

And I solve a problem with it, how can I multiply all of the vector() values of one of the subspaces with a constant?

I am trying to do something like this (that doesn't work):

Thank you

```
deg = 1
mesh = BoxMesh(Point(corner[0],corner[1],corner[2]), Point(corner2[0],corner2[1],corner2[2]), dims[0],dims[1],dims[2])
P1 = VectorElement('Lagrange', tetrahedron, deg)
P2 = FiniteElement('Lagrange', tetrahedron, deg)
element = MixedElement([P1, P2])
V = FunctionSpace(mesh, element)
```

And I solve a problem with it, how can I multiply all of the vector() values of one of the subspaces with a constant?

I am trying to do something like this (that doesn't work):

`w.sub(0).vector()[:] = k*w.sub(0).vector()`

Thank you

Community: FEniCS Project

### 2 Answers

3

The following solution is due to mikael-mortensen from this post

```
from fenics import *
deg = 1
mesh = UnitCubeMesh(2,2,2)
P1 = FiniteElement('Lagrange', tetrahedron, deg)
P2 = FiniteElement('Lagrange', tetrahedron, deg)
element = MixedElement([P1, P2])
V = FunctionSpace(mesh, element)
w = Function(V)
# Set w to be 1 everywhere for testing purposes
wVals = Expression(('1.', '1.'), element=element)
w.assign(interpolate(wVals,V))
k = 4.
u = w.split(deepcopy=True)[0]
u.vector()[:]*=k
d = V.sub(0).dofmap().collapse(mesh)[1].values()
w.vector()[d] = u.vector()
# Test
plot(w.sub(0), interactive = True)
```

0

Something like this should work (not tested!)

```
# Get process local values as numpy array
arr = w.vector().get_local()
# Get indices belonging to subspace 0
dofs = V.sub(0).dofmap().dofs()
# Scale subspace 0
arr[dofs] *= k
# Put values back into vector
w.vector().set_local(arr)
w.vector().apply('insert')
```

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

to do it and it works. Thank you