Problem with bool in solve()

Hi,
I am pretty new about fenics, just started two days before, I have some struggle with the code I wrote below. The problem I want to solve with fenics is a coupled thermo-electric problem. When I ran the following code, the gives the following error:

TypeError: ‘bool’ object is not iterable when run the line solve(a == L, u, bc).

I know this error is self-explained in terms of python, but I do not really know what this bool is referring to, the only bool I can see is on_boundary, which I can not think of how it can cause the problem. Could some experienced user run the code and gives me some hint, what is the problem of the code? Thanks very much for your help.

from fenics import *
import matplotlib.pyplot as plt
import numpy as np
from dolfin import *
from mshr import *

box = Box(Point(-0.3,-0.15,-0.014),Point(0.3,0.15,0.014)) ## sample dimensions in cm
mesh = generate_mesh(box,30)
P1 = FiniteElement('P',tetrahedron,1)
element = MixedElement([P1,P1])
V = FunctionSpace(mesh, element)

##############################some parameters
T = 1.0
num_steps = 5
dt = T/num_steps
a = 0.6 ## sample length in cm
b = 0.3 ## sample width in cm
c = 0.028 ## sample thickness in cm
L = 100.0e-4 ## wire lenght in cm
r = 5.0e-4 ## wire radius in cm
R = (L/(r*r*np.pi))*(2.65e-4) ## wire resistance in ohm
I = 200e-6 ## current in A
contact = 4e6 ## contact resistance in ohm
W = I*I*(R+contact) ## heat injected via the wire
T_ext = 30.0 ##environment temperature in K
J_ext = I/(r*r*np.pi) ## current density in wire in A/cm2
tol = 0.05
#################################################

Define boundary condition

u_D_heat = Expression('T_ext', degree=2,T_ext=T_ext)
u_D_potential = Constant(0)
def boundary_D_heat(x, on_boundary):
return False
def boundary_D_potential(x, on_boundary):
return False

bc_heat = DirichletBC(V.sub(0), u_D_heat, boundary_D_heat)
bc_potential = DirichletBC(V.sub(1), u_D_potential, boundary_D_potential)
bc = [bc_heat, bc_potential]

Define initial condition

u_0 = Expression(('30.0','0.0'),degree=2)
u_n = interpolate(u_0,V)
u_n1, u_n2 = u_n.split()

Define parameter functions

K = Constant(0.13) # w/cm/K
cp = Constant(0.2) #J/g/K
rou = Constant(2.329) #g/cm3
sigma = Expression('temp < 37.0 ? exp(0.4258*temp-23.313):exp(0.0105*temp-7.6668)',degree=2,temp=u_n) #conductivity
h = 0.002 # w/cm2/K

Define variational problem

u = TrialFunction(V)
u_1, u_2 = split(u)
v_1, v_2 = TestFunctions(V)

g_heat = Expression('(near(x[0],-0.25*a,tol) and near(x[1],0.0,tol) and near(x[2],c/2.0,tol/5.0)) or (near(x[0],0.25*a,tol) and near(x[1],0.0,tol) and near(x[2],c/2.0,tol/5.0)) ? W : h*(T_ext-temp)',degree=2, tol=tol,a=a,c=c,W=W, h=h,T_ext=T_ext,temp=u_n1)
g_potential = Expression('near(x[0],-0.25*a,tol) and near(x[1],0.0,tol) and near(x[2],c/2.0,tol/5.0) ? J_ext : near(x[0],0.25*a,tol) and near(x[1],0.0,tol) and near(x[2],c/2.0,tol/5.0) ? -1.0*J_ext : 0',degree=2,tol=tol,a=a,c=c,J_ext=J_ext)
f_heat = sigma*dot(grad(u_2),grad(u_2))

a = dt*K*dot(grad(u_1), grad(v_1))*dx + rou*cp*u_1*v_1*dx + **sigma*dot(grad(u_2),grad(v_2))*dx**
L = dt*f_heat*v_1*dx + rou*cp*u_n1*v_1*dx + dt*g_heat*v_1*ds + **g_potential*v_2*ds**

u = Function(V)

Time-stepping

t = 0.0
vtkfile_u_1 = File('heat_test/solution_u_1.pvd')
vtkfile_u_2 = File('heat_test/solution_u_2.pvd')
for n in range(num_steps):
t = t + dt
solve(a == L, u, bc)
u_n.assign(u)
_u_1, _u_2 = u.split()
vtkfile_u_1 << (_u_1,t)
vtkfile_u_2 << (_u_2,t)

I also want to ask a question about using fenics on coupled PDEs, In the Tutorial, I see that in the example of advection–diffusion–reaction, three PDEs regarding to the concentration of three reaction species are organized into one equation F==0, I would like to ask, if this is generally want one should do for a coupled PDE system. In my thermo-electric model, the heat transport problem is a time-dependent problem, but the electric part is not, it is just a Possion equation, like div(grad(V)) = 0 with V being the electric potential. In this case, show I also write the two PDEs together into one? or solving them separately.

It’s not clear to me what the intended behavior of the program is, but the direct cause of the error is that f_heat involves a TrialFunction. When using the a == L syntax for the first argument of solve, a should be linear in each of a TestFunction and a TrialFunction, and L should be linear in a TestFunction (and not include a TrialFunction). In the given code, L not only includes the TrialFunction component u_2 (through f_heat), but is nonlinear in it. (With the given code, I first get a different error due to temp=u_n in the arguments passed while defining sigma, because u_n is not scalar, which I resolved by passing u_n1 instead.)

P.S. Note that you can format multi-line code blocks by including three backticks before and after (and, optionally, specify the programming language after the first triple backtick), e.g.,

```python
line 1
line 2
line 3
```

You can learn more about formatting your posts here by Googling “markdown”, which is the mark-up language used by Discourse.

1 Like

Thanks for your reply kamensky. Indeed the what your pointed out is written exactly in the tutorial, which I might overlook that part, After deleting the lines that defines u as trail functions and rearranging the equation to be F == 0, and the error disappeared.