This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Avoiding assembly - using vector operations with scalar & vector spaces

+3 votes

Hi all,
I have a scalar function (c) which gets multiplied by a vector (v) and inserted into a vector function space (d):
d = c*v
I can do this using projection (below) but I'm sure there is a better way using vector operations with c.vector(), but I can't figure out how to do it. Can anyone suggest?

from dolfin import *

mesh = RectangleMesh(0.0,0.0,1.0, 1.0,10,10)
V = FunctionSpace(mesh, "Lagrange", 1)
V_vec = VectorFunctionSpace(mesh, "Lagrange", 1)

c = project(Expression('x[0]'), V)

v = as_vector((1,2))

d = project(c*v,V_vec)

Thanks

asked May 14, 2014 by mwelland FEniCS User (8,410 points)

1 Answer

+8 votes
 
Best answer

This should do it:

dd = Function(V_vec)
dofs0 = V_vec.sub(0).dofmap().dofs()
dofs1 = V_vec.sub(1).dofmap().dofs()
dd.vector()[dofs0] = c.vector()
dd.vector()[dofs1] = 2*c.vector()
answered May 19, 2014 by mikael-mortensen FEniCS Expert (29,340 points)
selected May 22, 2014 by mwelland

Works wonderfully! Thanks!

...