 # I obtained a error message when I use a variable in defining boundary

I have a question regarding definining boundaries in fenics.
I want to define boundaries using the length of the interval (one-dimensional) so that I can impose different boundary conditions on both ends. But, if I use a variable in defining boundaries, I get an error.

TypeError: bad operand type for abs(): ‘Form’

If I do not use a variable and use a number, no errors. Could you help me out?

``````from dolfin import *
import matplotlib.pyplot as plt
import numpy as np

mesh = UnitIntervalMesh(32)

V = FunctionSpace(mesh, 'Lagrange', 2)

u0 = Constant('0.0')

u_true = Expression('0.5*x*(1.0-x)', degree=2)

plot(mesh)

L = 1.0

u_right = Constant(0)
u_left = Constant(0)

def boundary_right(x, on_boundary):
tol = 1E-14
return abs(x - L) < tol

def boundary_left(x, on_boundary):
tol = 1E-14
return abs(x) < tol

bc_right = DirichletBC(V, u_right, boundary_right)
bc_left = DirichletBC(V, u_left, boundary_left)

bcs = [bc_right, bc_left]

u = TrialFunction(V)
v = TestFunction(V)
f = Constant(1.0)
L = f*v*dx

u = Function(V)
solve(a == L, u, bcs)
``````

Hi Toshi, your code does not run properly as it is, make sure the indentations are set as they should. Anyway, using `near` might help:

``````def boundary_right(x, on_boundary):
tol = 1E-14
return on_boundary and near(x, L, tol)

def boundary_left(x, on_boundary):
tol = 1E-14
return on_boundary and near(x, 0, tol)
``````

Hi Diego,

Thank you for your comment. I fixed the code above.
Regarding using near, I tried that before, it does not work.
Below is the full error message

``````TypeError                                 Traceback (most recent call last)
<ipython-input-1-468bf59403f0> in <module>
60 # represents u as a function in a finite element function space V
61 u = Function(V)
---> 62 solve(a == L, u, bcs)

/usr/lib/petsc/lib/python3/dist-packages/dolfin/fem/solving.py in solve(*args, **kwargs)
231     # tolerance)
232     elif isinstance(args, ufl.classes.Equation):
--> 233         _solve_varproblem(*args, **kwargs)
234
235     # Default case, just call the wrapped C++ solve function

/usr/lib/petsc/lib/python3/dist-packages/dolfin/fem/solving.py in _solve_varproblem(*args, **kwargs)
271             solver = LinearVariationalSolver(problem)
272             solver.parameters.update(solver_parameters)
--> 273             solver.solve()
274
275     # Solve nonlinear variational problem

/usr/lib/petsc/lib/python3/dist-packages/dolfin/fem/dirichletbc.py in inside(self, x, on_boundary)
45             return self.inside_function(x)
46         else:
---> 47             return self.inside_function(x, on_boundary)
48
49

<ipython-input-1-468bf59403f0> in boundary_right(x, on_boundary)
39 def boundary_right(x, on_boundary):
40     tol = 1E-14
---> 41     return on_boundary and near(x, L, tol)
42
43 def boundary_left(x, on_boundary):

TypeError: near(): incompatible function arguments. The following argument types are supported:
1. (x0: float, x1: float, eps: float = 3e-16) -> bool

Invoked with: 0.0, Form([Integral(Product(Argument(FunctionSpace(Mesh(VectorElement(FiniteElement('Lagrange', interval, 1), dim=1), 0), FiniteElement('Lagrange', interval, 2)), 0, None), Coefficient(FunctionSpace(None, FiniteElement('Real', None, 0)), 11)), 'cell', Mesh(VectorElement(FiniteElement('Lagrange', interval, 1), dim=1), 0), 'everywhere', {}, None)]), 1e-14
``````

you’ve overwritten the variable `L`.

At the top you have `L = 1.0`. Then you overwrite it with `L = f*v*dx`. So the boundary comparison doesn’t make sense

``````def boundary_right(x, on_boundary):
tol = 1E-14
return abs(x - L) < tol
``````

when this gets called, `L` is a `Form` and not `1.0`.

1 Like

Hi Nate,

Oh, I see. Why I did not notice…