TypeError: 'float' object is not subscriptable

Dear all,

Hi, I am slowly moving in my code from python2 to python3 version. as I hope to run the FEniCS on ubuntu using the latest FEniCS version, rather than run on docker on 2017.1.0 which is in python2.

I have this line which I multiply a vector with a variable which works fine on my docker 2017.1.0 version, w is a mixed function space I created.
val_dof = dofmap.cell_dofs(0)[0]
val_local = w.vector()([val_dof][0])

however, when I run the same code on the latest FeniCS version, it fails and it always gives me an error TypeError: 'float' object is not subscriptable

I google the error and use 2to3 to convert python2 to python3, and use the following line.
val_local = w.vector()([val_dof][0])
the new error I got is TypeError: 'dolfin.cpp.la.PETScVector' object is not callable

Any idea I can try it out so that I can make it run on python3?

Thank you in advance

Without knowing the objective and assuming that you are just trying to extract/set the dof, consider for instance

from dolfin import *
msh = UnitSquareMesh(10, 10)

V1 = VectorElement("CG", msh.ufl_cell(), 2)
V2 = FiniteElement("CG", msh.ufl_cell(), 1)
W = FunctionSpace(msh, MixedElement([V1, V2]))

set_dof = W.dofmap().cell_dofs(0)[0]
w = Function(W)

# set/get the local dof 
w.vector()[set_dof] = 1. # sets the dof at the `set_dof`-th position to 1.

1 Like

Hi @bhaveshshrimali

Thanks for helping me out. I have tried the code you provided, it works fine and I appreciate it.
However, when I expand the code and run it on python3 in latest FEniCS version, it has an error on
set_local = w.vector()[set_dof][0]
TypeError: 'float' object is not subscriptable

May you give me some advice on it? I attached the code here.

from dolfin import *

msh = UnitSquareMesh(10, 10)

V1 = VectorElement("CG", msh.ufl_cell(), 2, quad_scheme="default")
V1._quad_scheme = 'default'
V2 = FiniteElement("CG", msh.ufl_cell(), 1, quad_scheme="default")
V2._quad_scheme = 'default'
V3 = FiniteElement("Real", msh.ufl_cell(), 0, quad_scheme="default")
V3._quad_scheme = 'default'
V4 = MixedElement([V3, V3, V3, V3, V3])

W = FunctionSpace(msh, MixedElement([V1,V2,V3,V4]))
w = Function(W)
comm = W.mesh().mpi_comm()
dofmap = W.sub(2).dofmap()
set_dof = dofmap.cell_dofs(0)[0]

own_range = dofmap.ownership_range()

try:
set_local = w.vector()[set_dof][0]
except IndexError:
set_local = 0

stress = MPI.sum(comm, set_local)

You’re trying to index a float and hence the error.

1 Like

Thanks for pointing it out.
When I try to print the output
print([set_dof])
print([set_dof][0])

it returns
[1003]
1003

so I guess the problem is with w.vector()[set_dof][0] , when I run in FEniCS 2017 it goes well, but when I use latest FEniCS version in python3, it gives the floating error.

Is there anyway I can calculate as set_local = w.vector()[set_dof][0] in a way that wont trigger the floating error?

w.vector() returns a vector object to you. you are trying to extract an index from it, and you must therefore encapsulate that index:

set_local = w.vector()[set_dof]
2 Likes