Set_local() in FEniCS 2019

Hi all,
i’m trying to update a code to the FEniCS 2019 from the 2017.2.0 version and I can’t get the set_local() to associate each value from an array to the correct node.

Here’s the error i get from FEniCS when using it in the 2019 version:

"TypeError: set_local(): incompatible function arguments. The following argument types are supported:
1. (self: dolfin.cpp.la.GenericVector, arg0: List[float]) -> None

Invoked with: <dolfin.cpp.la.PETScVector object at 0x7f95b1ef7d00>, array([ 1.08089618, 0.46165859, 1.00083088, …, 0.77038679,
0.98471083, 0.87700027]), array([ 0, 1, 2, …, 5986, 5987, 5988], dtype=int32)"

And here’s the code I used in the 2017 version:

from fenics import *
import mshr as mr
import numpy as np

L, H = 100., 10.
cell_size = 1
nel = int(L / cell_size)

seed1 = 8
m1 = 10

random_cell_size = 2 * cell_size
random_nel = int((L) / random_cell_size)

geom = mr.Rectangle(Point(0, 0), Point(L, H))
mesh = mr.generate_mesh(geom, nel)
mesh_perturbation = mr.generate_mesh(geom, random_nel)

V_pert = FunctionSpace(mesh, “CG”, 1)
V_pert_0 = FunctionSpace(mesh_perturbation, “DG”, 0)

Xtdc = V_pert_0.tabulate_dof_coordinates().reshape((-1, mesh.topology().dim()))

srf1 = Function(V_pert)
srf1_0 = Function(V_pert_0)
np.random.seed(seed1)
rn = np.random.rand(srf1_0.vector().size())

idcs = []
values = []

for ii, dof_coords in enumerate(Xtdc):
idcs.append(ii)
values.append(((h1_penalty) ** 2.) *
(np.log(1. / (1. - rn[ii]))) ** (2. / m1))

srf1_0.vector().set_local(np.asarray(values, dtype=np.float),
np.asarray(idcs, dtype=np.intc))
srf1_0.vector().apply(‘insert’)

srf1 = interpolate(srf1_0, srf1)

If you just want to set the values of a GenericVector at some set of indices given by a NumPy array, then you can use the overloaded index operator, e.g.,

from dolfin import *
import numpy as np
mesh = UnitIntervalMesh(10)
v = Function(FunctionSpace(mesh,"CG",1)).vector()
i = np.array([0,2,4],dtype=np.int)
vi = np.array([7,77,777],dtype=np.float)
v[i] = vi
print(v.get_local())
1 Like

Yup, that’s what i was looking for, thak you very much for the help.