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

TypeError: unsupported operand type(s) for -: 'Indexed' and 'NoneType'

0 votes

I understand the cause of the error. (Please first have a look at the code and the error message, which is at the end of the code) u is of type and u_m ( some projection of analytical solution onto M.sub(0)) is of type none. So '-' operation in F( term u-u_m) is not supported . Can someone please suggest me a way to avoid this problem?
Code and the error message is as follows.

from dolfin import *
import numpy, sys 

# create mesh and function spaces
degree = int(sys.argv[1])
divisions =[int(arg) for arg in (sys.argv[2:])]
mesh=UnitSquareMesh(*divisions)
V = FunctionSpace(mesh,"Lagrange",degree) # Function space for u ( Temperature)
Q = FunctionSpace(mesh,"Lagrange",degree)  # Function space for \mu ( materi
#property
L = FunctionSpace(mesh,"Lagrange",degree)  # Function space for \lembda 
#Mixed function space
M = MixedFunctionSpace([V,Q,L]) # M is mixed function space

# Define the test function space
TestFunction = TestFunction(M)
(v,q,w) = split(TestFunction)

# split the mixed function space into components
# Define the trial function space
solution = Function(M)
(u,mu,lmd) = split(solution)
PrescribedU=Expression("0.25*(pow(x[0],2)+pow(x[1],2))-x[0]*x[1]*0.5-x[0]+0.5*x[1]")

# projecting analytical solution on the function space
u_m = project(PrescribedU, M.sub(0))
PrescribedU=Expression("0.25*(pow(x[0],2)+pow(x[1],2))-x[0]*x[1]*0.5-x[0]+0.5*x[1]")

# projecting analytical solution on the function space
s = Function(V)
u_m = s.interpolate(PrescribedU)


F=inner(nabla_grad(lmd),nabla_grad(v))*mu*dx \
        +inner(nabla_grad(lmd),nabla_grad(u))*q*dx\
        +inner(nabla_grad(w),nabla_grad(u))*mu*dx+inner(u-u_m,v)*dx

Error message is :
Traceback (most recent call last):
File "objmin_var.py", line 38, in
+inner(nabla_grad(w),nabla_grad(u))mudx+inner(u-u_m,v)*dx
TypeError: unsupported operand type(s) for -: 'Indexed' and 'NoneType'

.

asked Jan 4, 2014 by mt.fan FEniCS Novice (260 points)

1 Answer

+2 votes

1) You cannot project to a subspace like this

u_m = project(PrescribedU, M.sub(0))

If you have a new version of dolfin you could do it like this

u_m = Function(M)
u_m0 = project(PrescibedU, V) # V is M.sub(0)
assign(u_m.sub(0), u_m0)

2) Interpolate like this

u_m = interpolate(PrescribedU, V)

instead of what you currently have that leaves u_m a NoneType variable (the return value of s.interpolate()):

s = Function(V)
u_m = s.interpolate(PrescribedU)

Note that here s will actually have the PrescribedU values you are after.

answered Jan 5, 2014 by mikael-mortensen FEniCS Expert (29,340 points)
...