Form seems to be zero: cannot compile it.

Asked by Dominique

Hi,

I'm trying to model a variant of the "nonlinear Poisson" problem described in the Dolfin tutorial with the difference that the source term is variable (it is adjusted in an optimization loop). Here's my code:

----
from dolfin import *
import numpy as np

# Create mesh and define function space
mesh = UnitSquare(2,2)
U = FunctionSpace(mesh, "CG", 1)
F = FunctionSpace(mesh, "CG", 1)
V = U * F

# Dirichlet boundary condition (doesn't really seem to matter)
class DirichletBoundary(SubDomain):
    def inside(self, x, on_boundary):
        return on_boundary

g = Constant(1.0)
bc = DirichletBC(V.sub(0), g, DirichletBoundary())

# Define nonlinear form and its Jacobian.
uf = Function(V) ; u, f = uf.split()
v, w = TestFunctions(V)
duf = TrialFunction(V)

L = dot(grad(v), (1 + u**2)*grad(u))*dx - v*f*dx
a = derivative(L, uf, duf) # Jacobian.

uf.vector()[:] = np.random.random(V.dim())
b = assemble(L, mesh=mesh) ; print b.array()
A = assemble(a, mesh=mesh)
----

I have 2 questions about this code:

1) I wonder why the statement "print b.array()" in the line before last always prints a vector with all zeros in the second half (corresponding to the variable f). It seems as if the derivative with respect to f had not been taken ?!

2) The last line fails with:

Form (<empty Form>) seems to be zero: cannot compile it.
Traceback (most recent call last):
  File "nlpoisson-opt.py", line 54, in <module>
    A = assemble(a, mesh=mesh)
  File "/Applications/FEniCS.app/Contents/Resources/lib/python2.6/site-packages/dolfin/fem/assemble.py", line 115, in assemble
    common_cell=common_cell)
  File "/Applications/FEniCS.app/Contents/Resources/lib/python2.6/site-packages/dolfin/fem/form.py", line 52, in __init__
    common_cell)
  File "/Applications/FEniCS.app/Contents/Resources/lib/python2.6/site-packages/dolfin/compilemodules/jit.py", line 62, in mpi_jit
    return local_jit(*args, **kwargs)
  File "/Applications/FEniCS.app/Contents/Resources/lib/python2.6/site-packages/dolfin/compilemodules/jit.py", line 139, in jit
    return jit_compile(form, parameters=p, common_cell=common_cell)
  File "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/ffc/jitcompiler.py", line 73, in jit
  File "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/ffc/jitcompiler.py", line 130, in jit_form
  File "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/ffc/compiler.py", line 150, in compile_form
  File "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/ffc/analysis.py", line 64, in analyze_forms
  File "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/ffc/analysis.py", line 64, in <genexpr>
  File "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/ffc/analysis.py", line 134, in _analyze_form
  File "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/ffc/log.py", line 46, in ffc_assert
  File "<string>", line 1, in <lambda>
  File "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/ufl/log.py", line 135, in error
Exception: Form (<empty Form>) seems to be zero: cannot compile it.

Can you easily see what's wrong with my code?

I'm using the most recent stable pre-packaged FEniCS dmg for OSX (11.05 from May 20th).

Many thanks in advance!

Question information

Language:
English Edit question
Status:
Solved
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Solved by:
Marie Rognes
Solved:
Last query:
Last reply:
Revision history for this message
Garth Wells (garth-wells) said :
#1

On 18/07/11 22:11, Dominique wrote:
> New question #165254 on DOLFIN:
> https://answers.launchpad.net/dolfin/+question/165254
>
> Hi,
>
> I'm trying to model a variant of the "nonlinear Poisson" problem described in the Dolfin tutorial with the difference that the source term is variable (it is adjusted in an optimization loop). Here's my code:
>
> ----
> from dolfin import *
> import numpy as np
>
> # Create mesh and define function space
> mesh = UnitSquare(2,2)
> U = FunctionSpace(mesh, "CG", 1)
> F = FunctionSpace(mesh, "CG", 1)
> V = U * F
>
> # Dirichlet boundary condition (doesn't really seem to matter)
> class DirichletBoundary(SubDomain):
> def inside(self, x, on_boundary):
> return on_boundary
>
> g = Constant(1.0)
> bc = DirichletBC(V.sub(0), g, DirichletBoundary())
>
> # Define nonlinear form and its Jacobian.
> uf = Function(V) ; u, f = uf.split()
> v, w = TestFunctions(V)
> duf = TrialFunction(V)
>
> L = dot(grad(v), (1 + u**2)*grad(u))*dx - v*f*dx
> a = derivative(L, uf, duf) # Jacobian.
>
> uf.vector()[:] = np.random.random(V.dim())
> b = assemble(L, mesh=mesh) ; print b.array()
> A = assemble(a, mesh=mesh)
> ----
>
> I have 2 questions about this code:
>
> 1) I wonder why the statement "print b.array()" in the line before last always prints a vector with all zeros in the second half (corresponding to the variable f). It seems as if the derivative with respect to f had not been taken ?!
>

Half the vector contains zeroes because it corresponds to the 'w' weight
function. There is no 'w' term appearing in L, which is like having
w*0.0*dx

> 2) The last line fails with:
>

This is related to the absence of w in L.

Garth

>
> Form (<empty Form>) seems to be zero: cannot compile it.
> Traceback (most recent call last):
> File "nlpoisson-opt.py", line 54, in <module>
> A = assemble(a, mesh=mesh)
> File "/Applications/FEniCS.app/Contents/Resources/lib/python2.6/site-packages/dolfin/fem/assemble.py", line 115, in assemble
> common_cell=common_cell)
> File "/Applications/FEniCS.app/Contents/Resources/lib/python2.6/site-packages/dolfin/fem/form.py", line 52, in __init__
> common_cell)
> File "/Applications/FEniCS.app/Contents/Resources/lib/python2.6/site-packages/dolfin/compilemodules/jit.py", line 62, in mpi_jit
> return local_jit(*args, **kwargs)
> File "/Applications/FEniCS.app/Contents/Resources/lib/python2.6/site-packages/dolfin/compilemodules/jit.py", line 139, in jit
> return jit_compile(form, parameters=p, common_cell=common_cell)
> File "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/ffc/jitcompiler.py", line 73, in jit
> File "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/ffc/jitcompiler.py", line 130, in jit_form
> File "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/ffc/compiler.py", line 150, in compile_form
> File "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/ffc/analysis.py", line 64, in analyze_forms
> File "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/ffc/analysis.py", line 64, in <genexpr>
> File "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/ffc/analysis.py", line 134, in _analyze_form
> File "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/ffc/log.py", line 46, in ffc_assert
> File "<string>", line 1, in <lambda>
> File "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/ufl/log.py", line 135, in error
> Exception: Form (<empty Form>) seems to be zero: cannot compile it.
>
>
> Can you easily see what's wrong with my code?
>
> I'm using the most recent stable pre-packaged FEniCS dmg for OSX (11.05 from May 20th).
>
> Many thanks in advance!
>
>

Revision history for this message
Dominique (dominique-orban) said :
#2

Thanks for your answer. I have tried using w there before. I guess I don't clearly understand how derivative() works because there would be no w in the weak form of the PDE, only v. I thought derivative() would "know" that f is the unknown and it does have a nonzero coefficient in the equation.

But even if I use w the last line fails with the same error message ("Exception: Form (<empty Form>) seems to be zero: cannot compile it.")

Revision history for this message
Best Marie Rognes (meg-simula) said :
#3

On 07/18/11 23:11, Dominique wrote:
> New question #165254 on DOLFIN:
> https://answers.launchpad.net/dolfin/+question/165254
>
> Hi,
>
> I'm trying to model a variant of the "nonlinear Poisson" problem described in the Dolfin tutorial with the difference that the source term is variable (it is adjusted in an optimization loop). Here's my code:
>
> ----
> from dolfin import *
> import numpy as np
>
> # Create mesh and define function space
> mesh = UnitSquare(2,2)
> U = FunctionSpace(mesh, "CG", 1)
> F = FunctionSpace(mesh, "CG", 1)
> V = U * F
>
> # Dirichlet boundary condition (doesn't really seem to matter)
> class DirichletBoundary(SubDomain):
> def inside(self, x, on_boundary):
> return on_boundary
>
> g = Constant(1.0)
> bc = DirichletBC(V.sub(0), g, DirichletBoundary())
>
> # Define nonlinear form and its Jacobian.
> uf = Function(V) ; u, f = uf.split()

Try:

u = uf[0]
f = uf[1]

instead of uf.split().

(We should do something about this.)

--
Marie

> v, w = TestFunctions(V)
> duf = TrialFunction(V)
>
> L = dot(grad(v), (1 + u**2)*grad(u))*dx - v*f*dx
> a = derivative(L, uf, duf) # Jacobian.
>
> uf.vector()[:] = np.random.random(V.dim())
> b = assemble(L, mesh=mesh) ; print b.array()
> A = assemble(a, mesh=mesh)
> ----
>
> I have 2 questions about this code:
>
> 1) I wonder why the statement "print b.array()" in the line before last always prints a vector with all zeros in the second half (corresponding to the variable f). It seems as if the derivative with respect to f had not been taken ?!
>
> 2) The last line fails with:
>
>
> Form (<empty Form>) seems to be zero: cannot compile it.
> Traceback (most recent call last):
> File "nlpoisson-opt.py", line 54, in<module>
> A = assemble(a, mesh=mesh)
> File "/Applications/FEniCS.app/Contents/Resources/lib/python2.6/site-packages/dolfin/fem/assemble.py", line 115, in assemble
> common_cell=common_cell)
> File "/Applications/FEniCS.app/Contents/Resources/lib/python2.6/site-packages/dolfin/fem/form.py", line 52, in __init__
> common_cell)
> File "/Applications/FEniCS.app/Contents/Resources/lib/python2.6/site-packages/dolfin/compilemodules/jit.py", line 62, in mpi_jit
> return local_jit(*args, **kwargs)
> File "/Applications/FEniCS.app/Contents/Resources/lib/python2.6/site-packages/dolfin/compilemodules/jit.py", line 139, in jit
> return jit_compile(form, parameters=p, common_cell=common_cell)
> File "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/ffc/jitcompiler.py", line 73, in jit
> File "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/ffc/jitcompiler.py", line 130, in jit_form
> File "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/ffc/compiler.py", line 150, in compile_form
> File "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/ffc/analysis.py", line 64, in analyze_forms
> File "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/ffc/analysis.py", line 64, in<genexpr>
> File "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/ffc/analysis.py", line 134, in _analyze_form
> File "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/ffc/log.py", line 46, in ffc_assert
> File "<string>", line 1, in<lambda>
> File "/Users/johannr/fenics-11.05/local/lib/python2.6/site-packages/ufl/log.py", line 135, in error
> Exception: Form (<empty Form>) seems to be zero: cannot compile it.
>
>
> Can you easily see what's wrong with my code?
>
> I'm using the most recent stable pre-packaged FEniCS dmg for OSX (11.05 from May 20th).
>
> Many thanks in advance!
>
>

Revision history for this message
Dominique (dominique-orban) said :
#4

This seems to do the trick. Thanks!

Can someone explain the v vs. w in my definition of L? If I formulate the weak form of -div(grad(u)) = f, I get only one test function v, not a v and a w ?!?

Revision history for this message
Marie Rognes (meg-simula) said :
#5

On 07/20/11 06:11, Dominique wrote:
> Question #165254 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/165254
>
> Status: Answered => Open
>
> Dominique is still having a problem:
> This seems to do the trick. Thanks!
>
> Can someone explain the v vs. w in my definition of L? If I formulate
> the weak form of -div(grad(u)) = f, I get only one test function v, not
> a v and a w ?!?
>

I can't explain why you have defined (u, f) and (v, w) in your code.

However, if, as you say, you want to model a (scalar) nonlinear Poisson
equation, I would suggest just using

U = FunctionSpace(mesh, "CG", 1)
u = Function(U)
f = Function(U)
v = TestFunction(U)
du = TrialFunction(U)

L = dot(grad(v), (1 + u**2)*grad(u))*dx - v*f*dx
a = derivative(L, u, du)

--
Marie

Revision history for this message
Dominique (dominique-orban) said :
#6

I know why I've defined (u,f) and (v,w). It's because both u and f are unknowns in my case. I think the solution you propose is ok if f is given, but not variable. In your example, derivative() only differentiates with respect to u. I also need to differentiate with respect to f.

My question is: even when both u and f are variable, the weak form of the pde has only one test function in it (one equation => one test function v). Why do I have to replace the factor of f with w if I want derivative() to properly differentiate with respect to f?

Why do have to say

L = dot(grad(v), (1 + u**2)*grad(u))*dx - w*f*dx

instead of

L = dot(grad(v), (1 + u**2)*grad(u))*dx - v*f*dx ?

Thanks.

Revision history for this message
Marie Rognes (meg-simula) said :
#7

On 07/20/11 17:06, Dominique wrote:
> Question #165254 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/165254
>
> Status: Answered => Open
>
> Dominique is still having a problem:
> I know why I've defined (u,f) and (v,w). It's because both u and f are
> unknowns in my case. I think the solution you propose is ok if f is
> given, but not variable.

Ok... In that case, could you please present a more complete
mathematical formulation of the problem you are trying to solve? Then it
might be easier for us to suggest a possible
solution.

--
Marie

> In your example, derivative() only
> differentiates with respect to u. I also need to differentiate with
> respect to f.
>
> My question is: even when both u and f are variable, the weak form of
> the pde has only one test function in it (one equation => one test
> function v). Why do I have to replace the factor of f with w if I want
> derivative() to properly differentiate with respect to f?
>
> Why do have to say
>
> L = dot(grad(v), (1 + u**2)*grad(u))*dx - w*f*dx
>
> instead of
>
> L = dot(grad(v), (1 + u**2)*grad(u))*dx - v*f*dx ?
>
> Thanks.
>

Revision history for this message
Dominique (dominique-orban) said :
#8

I finally found the answer to my question.

Thanks!

Revision history for this message
Dominique (dominique-orban) said :
#9

Thanks Marie Rognes, that solved my question.