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

How can one set the value of a vector Constant?

+3 votes

I have a Constant that I would like to modify during execution. It works when it's scalar:

c = Constant(0.0)
c.assign(1.0)

But not so well when it's a vector:

v = Constant((0.0, 0.0))
v.assign((1.0, 0.0))
# or v.assign([1.0, 0.0]) or even v.assign(np.array([1.0, 0.0]))

# TypeError: in method 'Constant_assign', argument 2 of type 'double'

Edit: looking at Constant.h, it looks like it should be possible by using multiple arguments as in v.assign(1.0, 0.0) but this fails with NotImplementedError: Wrong number or type of arguments for overloaded function 'Constant_assign'.

Is there a way to do it ?

asked Jan 22, 2015 by gjankowiak FEniCS Novice (880 points)
edited Jan 22, 2015 by gjankowiak

1 Answer

+1 vote
 
Best answer

Hi, the following works

from dolfin import *

mesh = UnitSquareMesh(3, 3)

c = Constant((0, 1))
plot(c, mesh=mesh, interactive=True)

c.assign(Constant((1, 0)))

plot(c, mesh=mesh, interactive=True) 
answered Jan 22, 2015 by MiroK FEniCS Expert (80,920 points)
selected Jan 22, 2015 by gjankowiak

Ok, that's less than ideal but it works. I'll try to see if/how this can be made more pythonic. Thanks!

...