Convert a numpy array to a vector in dolfin with discontinuous space

Dear Fenics community,

I’m facing an issue when trying to convert a numpy array into a dolfin vector, when the function space (a subspace in my case) is discontinuous. The example is as follows:

from dolfin import *
import numpy as np
l = 4.0
DOF = 50

mesh = IntervalMesh(DOF, 0, l)

degree = 1

P1 = FiniteElement("Lagrange", interval, degree)

P0 = FiniteElement("Discontinuous Lagrange", interval, degree-1)

element = MixedElement([P1, P0])

U = FunctionSpace(mesh, element)

# Numpy array
w_array = np.random.rand(DOF+1)

# Function on DG subspace
w_f = Function(U.sub(1).collapse())

# Numpy to dof assign
w_f.vector()[:] = w_array[:]

However, I get the following Runtime error:

*** -------------------------------------------------------------------------
*** 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 set local values of PETSc vector.
*** Reason: Size of values array is not equal to local vector size.
*** Where: This error was encountered inside PETScVector.cpp.
*** Process: 0


*** DOLFIN version: 2019.1.0
*** Git changeset: unknown

Am I missing something?. Any help is appreciated.

Thanks!.

The following seems to work:

# Numpy array
#w_array = np.random.rand(DOF+1)
w_array = np.random.rand(DOF)

Keep in mind that the DG0 space has one fewer degrees of freedom than the CG1 space.

Dear kamensky,

The code work as expected. The question is mark as solved.

Thank you.