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

Assign values to function

+2 votes

Hello

I've used to set Fenics function values with numpy arrays with

foo.vector()[:] = np_arr

with dolfin-version 1.2 and 1.4.
Recently our machines have been upgraded to dolfin-version 1.5, and above code doesn't work any more with error

TypeError: contiguous numpy array of 'dolfin_index' expected. Make sure that the numpy array is contiguous, with 1 dimension, and uses dtype=int64.

which seems strange to me, since np_arr holds reals and not integers.
I've discovered i can use

foo.vector().set_local(np_arr)

but i haven't found any such changes in the dolfin-1.5 changelog.

Is there a fault on my side?
Thanks!

asked Nov 3, 2015 by adoll FEniCS Novice (530 points)
edited Nov 3, 2015 by adoll

2 Answers

+1 vote

I still use the first line in version 1.6 without problems. Is it any help to add

np_arr = numpy.ascontiguousarray(np_arr)

?

answered Nov 4, 2015 by maartent FEniCS User (3,910 points)
0 votes

Same problem here. Neither of this does work

foo.vector()[:] = np.ones(V.dim())
foo.vector()[np.ascontiguousarray(np.array([i for i in range(V.dim())]))] = np.ones(V.dim())

/usr/lib64/mpi/gcc/openmpi/lib64/python2.7/site-packages/dolfin/cpp/la.pyc in setitem(self, indices, values)
1599 # If values passed.
1600 if len(values) > 0:
-> 1601 self.set_local(values, indices)
1602
1603 finally:
TypeError: contiguous numpy array of 'dolfin_index' expected. Make sure that the numpy array is contiguous, with 1 dimension, and uses dtype=int64.

answered Jan 22, 2016 by meigel FEniCS User (1,520 points)
...