"Unable to set local values of PETSc vector" for conversion of function to vector

Hi everyone,

I am trying to fit some x and y data to a UnivariateSpline in order to obtain a function and then project this function across my mesh, such that the value of the function changes depending on the solution to my finite element problem across my mesh.

However, when I run this, I keep getting this error:

  File "targetv4simplified.py", line 47, in <module>
    kappa.vector()[:] = k_new_array
RuntimeError: 

*** -------------------------------------------------------------------------
*** 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: 32
*** 
*** DOLFIN version: 2019.2.0.dev0
*** Git changeset:  b55804ecca7d010ff976967af869571b56364975

What is the issue here? And how do I resolve it?

My simplified code is given below:

from __future__ import print_function
import numpy as np
from dolfin import *
from ufl import as_tensor
import ufl
import math
from mpi4py import MPI
from scipy.interpolate import UnivariateSpline


parameters["allow_extrapolation"] = True
parameters["form_compiler"]["cpp_optimize"] = True
parameters["form_compiler"]["optimize"] = True
set_log_level(50)

temp = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,18,20,25,30,35,40,45,50,60,70,80,90,100,123.2,150,173.2,200,223.2,250,273.2,298.2,300,323.2,350,373.2,400,473.2,500,573.2,600,673.2,700,773.2,800,873.2,900,973.2,1000,1073,1100,1173,1200,1273,1300,1373,1400,1473,1500]
kappa_in = [1440,2870,4280,5630,6870,7950,8800,9380,9680,9710,9500,9110,8600,7940,7200,6450,5120,4050,2320,1440,961,692,527,427,314,258,229,217,208,198,192,188,185,182,180,177,173,174,171,167,163,159,150,146,139,137,132,130,127,125,122,121,119,118,116,115,113,112,111,110,108,108,106,106]

tempspace = np.linspace(0.1,10000,10000)
kappa_f = UnivariateSpline(temp, kappa_in, k = 1, ext = 3)

mesh = UnitCubeMesh(16,16,16)

Space = FunctionSpace(mesh, 'P', 2)      #define finite element function space, defined via basis functions
VectorSpace = VectorFunctionSpace(mesh, 'P', 1)

T = Function(Space)
T0 = Function(Space)
T_init = Expression('Tambient', degree=1, Tambient=300.)
T = project(T_init, Space)
assign(T0,T)

constant_space = FunctionSpace(mesh, 'CG',1)
k_new_array = kappa_f(T.vector().get_local())
kappa = Function(constant_space)
kappa.vector()[:] = k_new_array

The root of the problem is that Space (where T is) and constant_space (where kappa is) have different numbers of degrees of freedom, and thus Functions in these spaces have different length vectors. For instance, if you make the change

#kappa = Function(constant_space)
kappa = Function(Space)

then the code runs without error.

2 Likes