Expecting second argument to be a Function

Asked by imranal

I came upon this strange error message while running the following code (the error message has been included at the end) :

from dolfin import *

N = 100
alpha = 0.1
mesh = UnitInterval(N)
V = FunctionSpace(mesh,"CG",1)

def left(x,on_boundary):
    return on_boundary & near(x[0],0)
def right(x,on_boundary):
    return on_boundary & near(x[0],1)

# The boundary conditions
bc_left = DirichletBC(V,Constant(0),left)
bc_right = DirichletBC(V,Constant(1.0),right)
bcs = [bc_left,bc_right]

# The variational formulation
u = TrialFunction(V)
v = TestFunction(V)
a = (-u.dx(0)*v + alpha*u.dx(0)*v.dx(0))*dx
L = Constant(0)*v*dx

# Solve the variational problem
solve(a == L,u,bcs)
#u_ = Function(V)
#solve(a==L,u_,bcs)

The commented code works well. However when I try to solve the variational problem and store the solution in u, which is the trial function, I produce the error message :

>> python convection_diffusion_1D.py

Traceback (most recent call last):
  File "convection_diffusion_1D.py", line 26, in <module>
    solve(a == L,u,bcs)
  File "/usr/lib/python2.7/dist-packages/dolfin/fem/solving.py", line 250, in solve
    _solve_varproblem(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/dolfin/fem/solving.py", line 261, in _solve_varproblem
    = _extract_args(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/dolfin/fem/solving.py", line 368, in _extract_args
    u = _extract_u(args[1])
  File "/usr/lib/python2.7/dist-packages/dolfin/fem/solving.py", line 418, in _extract_u
    "Expecting second argument to be a Function")
  File "/usr/lib/python2.7/dist-packages/dolfin/cpp.py", line 2261, in dolfin_error
    return _cpp.dolfin_error(*args)
RuntimeError:

*** -------------------------------------------------------------------------
*** DOLFIN encountered an error. If you are not able to resolve this issue
*** using the information listed below, you can ask for help at
***
*** https://answers.launchpad.net/dolfin
***
*** Remember to include the error message listed below and, if possible,
*** include a *minimal* running example to reproduce the error.
***
*** -------------------------------------------------------------------------
*** Error: Unable to solve variational problem.
*** Reason: Expecting second argument to be a Function.
*** Where: This error was encountered inside solving.py.
*** -------------------------------------------------------------------------

Why does this happen ? I have solved plenty of problems using the trialfunction as an argument in different solvers to store the solution inside.

Question information

Language:
English Edit question
Status:
Answered
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Jan Blechta (blechta) said :
#1

On Sun, 05 May 2013 11:11:33 -0000
imranal <email address hidden> wrote:
> New question #228187 on DOLFIN:
> https://answers.launchpad.net/dolfin/+question/228187
>
> I came upon this strange error message while running the following
> code (the error message has been included at the end) :
>
> from dolfin import *
>
>
> N = 100
> alpha = 0.1
> mesh = UnitInterval(N)
> V = FunctionSpace(mesh,"CG",1)
>
> def left(x,on_boundary):
> return on_boundary & near(x[0],0)
> def right(x,on_boundary):
> return on_boundary & near(x[0],1)
>
> # The boundary conditions
> bc_left = DirichletBC(V,Constant(0),left)
> bc_right = DirichletBC(V,Constant(1.0),right)
> bcs = [bc_left,bc_right]
>
> # The variational formulation
> u = TrialFunction(V)
> v = TestFunction(V)
> a = (-u.dx(0)*v + alpha*u.dx(0)*v.dx(0))*dx
> L = Constant(0)*v*dx
>
> # Solve the variational problem
> solve(a == L,u,bcs)
> #u_ = Function(V)
> #solve(a==L,u_,bcs)
>
> The commented code works well. However when I try to solve the
> variational problem and store the solution in u, which is the trial
> function, I produce the error message :
>
> >> python convection_diffusion_1D.py
>
> Traceback (most recent call last):
> File "convection_diffusion_1D.py", line 26, in <module>
> solve(a == L,u,bcs)
> File "/usr/lib/python2.7/dist-packages/dolfin/fem/solving.py", line
> 250, in solve _solve_varproblem(*args, **kwargs)
> File "/usr/lib/python2.7/dist-packages/dolfin/fem/solving.py", line
> 261, in _solve_varproblem = _extract_args(*args, **kwargs)
> File "/usr/lib/python2.7/dist-packages/dolfin/fem/solving.py", line
> 368, in _extract_args u = _extract_u(args[1])
> File "/usr/lib/python2.7/dist-packages/dolfin/fem/solving.py", line
> 418, in _extract_u "Expecting second argument to be a Function")
> File "/usr/lib/python2.7/dist-packages/dolfin/cpp.py", line 2261,
> in dolfin_error return _cpp.dolfin_error(*args)
> RuntimeError:
>
> ***
> -------------------------------------------------------------------------
> *** DOLFIN encountered an error. If you are not able to resolve this
> issue *** using the information listed below, you can ask for help at
> *** *** https://answers.launchpad.net/dolfin
> ***
> *** Remember to include the error message listed below and, if
> possible, *** include a *minimal* running example to reproduce the
> error. ***
> ***
> -------------------------------------------------------------------------
> *** Error: Unable to solve variational problem. *** Reason:
> Expecting second argument to be a Function. *** Where: This error
> was encountered inside solving.py. ***
> -------------------------------------------------------------------------
>
> Why does this happen ? I have solved plenty of problems using the
> trialfunction as an argument in different solvers to store the
> solution inside.
>

TrialFunction is an argument of form in which the form is linear and in
particular it is the first argument of form - TestFunction being last.
So TrialFunction is used in definitions of bilinear forms.

On the other hand finite element functions (i.e. linear combinations
of basis functions) are handled via Function object. So your
outcommented code is correct. You could also label solution as u
instead if u_

u = TrialFunction(V)
v = TestFunction(V)
a = ...
L = ...

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

This is in fact a canonical way how to solve linear problem.

Jan

Can you help with this problem?

Provide an answer of your own, or ask imranal for more information if necessary.

To post a message you must log in.