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

Getting error on assemble

–3 votes

In Python, I am trying to solve finite element methods for optimal control problems on Neumann conditions. But assemble does not work properly getting some error in line writing likes this given below

error_y = (y - y_e)(y - y_e)dx
error = sqrt(assemble(error_y))
print"y_error:",error

Similiar, Same way getting error in line given below
error = sqrt(assemble(error_p))
error = sqrt(assemble(error_u))
A = assemble(a, exterior_facet_domains=boundary_parts)
b = assemble(L, exterior_facet_domains=boundary_parts)


Here Python Code for Finite element methods for optimal control problems for further clarification.


from dolfin import *
from math import *
import sys

Create mesh and define function space

nx = 32
ny = 32
y_0=0
mesh = UnitSquare (nx,ny)
X = FunctionSpace (mesh,'Lagrange',1)
Y = FunctionSpace (mesh,'Lagrange',1)
Z = X*Y
(y,p) = TrialFunctions(Z)
(si,phi) = TestFunctions(Z)

Define Sourse values

f = Expression('x[1]x[2]/2-x[1]')
y0 = Constant ('1')
d = Constant('1') # regularize parameter
a = inner(grad(phi),grad(p))
dx+phipdx-phiyds(1)+inner(grad(y),grad(si))dx+ysidx-(1/d)psids(0)
L = fsidx-y0phids(1)

boundary

boundary_parts = MeshFunction ('uint', mesh, 1)

Mark lower boundary facets as subdomain 0

class LowerNeumannBoundary(SubDomain):
def inside(self,x,on_boundary):
tol = 1E-14
return on_boundary and abs(x[1]) < tol

tolerance for coordinate comparisons

L = LowerNeumannBoundary()
L.mark(boundary_parts, 0)

Mark upper boundary facets as subdomain 1

class UpperNeumannBoundary(SubDomain):
def inside(self, x, on_boundary):
tol = 1E-14
return on_boundary and abs(x[1] - 1) < tol

tolerance for coordinate comparisons

U = UpperNeumannBoundary()
U.mark(boundary_parts, 1)

all of the Rest boundaries

class RestNeumannBoundary(SubDomain):
def inside(self, x, on_boundary):
tol = 1E-14
return on_boundary and (abs(x[0]) < tol or abs (x[0] - 1) < tol)

tolerance for coordinate comparisons

Verification

u=0
y_exact = Expression('(cosh(x[1] - 1)/(1 - dsinh(1)sinh(1)))(y_0+1/sinh(1) - .5) - (cosh(x[1] - 1)/sinh(1)) + x[1] * x[1]/2 - x[1] + 1' ,y_0 = 1,d = 1)
p_exact = Expression('((d
sinh(1)cosh(x[1]))/(1-dsinh(1)sinh(1)))(y_0 + (1.0/sinh(1)) - .5)',y_0 = 1,d = 1)
u_exact = Expression('-sinh(1)/(1-psinh(1)sinh(1))*(u0+(1/sinh(1))-0.5)',u0=1,p=1)

Xe = FunctionSpace(mesh, 'Lagrange', 5)
y_e = interpolate(y_exact,Xe)
Ye = FunctionSpace(mesh, 'Lagrange', 5)
p_e = interpolate (p_exact, Ye,)
ze = FunctionSpace(mesh, 'Lagrange', 5)
u_e = interpolate(u_exact ,ze)

error_y = (y - y_e)(y - y_e)dx
error = sqrt(assemble(error_y))
print"y_error:",error

error_p = (p - p_e)(p - p_e)dx
error = sqrt(assemble(error_p))
print"p_error:",error

error_u = (u - u_e)(u - u_e)dx
error = sqrt(assemble(error_u))
print"u_error:",error

R = RestNeumannBoundary()
R.mark(boundary_parts,23)
M= assemble(y_0phids(1),exterior_facet_domains=boundary_parts)

Compute solution

A = assemble(a, exterior_facet_domains=boundary_parts)
b = assemble(L, exterior_facet_domains=boundary_parts)
s = Function(Z)
solve(A,s.vector(),b)
(y,p) = s.split()
print'(y,p) :',s.vector().array()
print'n'
print'(y) :',y.vector().array()
print'n'
print'(p) :',p.vector().array()
print'(M) :',M.array()

B= (.5,.5)
print'y_e at the centor:',y_e(B)
print'y at the centor:',y(B)
print'p_e at the centor:',p_e(B)
print'p at the centor:',p(B)

cost=inner(y-y_0,y-y_0)ds(1)+(1.0/d)inner(p,p)ds(0)
J_h=(1.0/2)
assemble(cost, exterior_facet_domains=boundary_parts)

coste=inner(y_e_Ve-y_0,y_e_Ve-y_0)ds(1)+(1.0/d)inner(p_e_Ve,p_e_Ve)ds(0)
J_ex=(1.0/2)
assemble(coste, exterior_facet_domains=boundary_parts)
E5=abs(J_h-J_ex)
t_j=f_pv+s_pv

print'(y_m):',max (y.vector().array())
print'(y_em):',max (y_e_Ve.vector().array())
print'(y_n):',min (y.vector().array())
print'(y_en):',min (y_e_Ve.vector().array())
print"n"

print'f_pv:',f_pv
print's_pv:',s_pv
print'tot:',t_j
print'J_h(y,u):',J_h
print'J_e(y,u):',J_ex

print'(p_m):', max(p.vector().array())
print'(p_em):', max(p_e_Ve.vector().array())
print'(p_n):', min(p.vector().array())
print'(p_en):', min(p_e_Ve.vector().array())

Plot solution

plot(y,title="yplot")
plot(p,title="pplot")
plot(mesh,title="mesh")
interactive()

asked Sep 24, 2013 by Manickam FEniCS Novice (450 points)

Could you please provide a minimum working example with proper formatting?

Can i know what kind of formatting? Actually its python format (example optimal.py)

I can see that you have tried to use python format, but there is no way we can copy paste this (very large) example to reproduce the error, because of e.g. indenting and spaces. You also say nothing about what kind of error you're getting. Edit your question with a better format (use the Code Sample tool in the editor) and specify the error you are getting.

Hi
I am getting error manly in assemble while running the program for optimal control problems,find error estimates.
1) error = sqrt(assemble(error_y))
Similiar, Same way getting error in line given below
2) error = sqrt(assemble(error_p))
3) error = sqrt(assemble(error_u))
4) A = assemble(a, exterior_facet_domains=boundary_parts)
5) b = assemble(L, exterior_facet_domains=boundary_parts)


Here the following error occurring for first one like this:


File "opt.py", line 78, in
error = sqrt(assemble(error_y))
File "C:\FEniCS\lib\site-packages\dolfin\fem\assembling.py", line 155, in asse
mble
common_cell=common_cell)
File "C:\FEniCS\lib\site-packages\dolfin\fem\form.py", line 54, in init
common_cell)
File "C:\FEniCS\lib\site-packages\dolfin\compilemodules\jit.py", line 66, in m
pi_jit
return local_jit(*args, **kwargs)
File "C:\FEniCS\lib\site-packages\dolfin\compilemodules\jit.py", line 154, in
jit
return jit_compile(form, parameters=p, common_cell=common_cell)
File "C:\FEniCS\lib\site-packages\ffc\jitcompiler.py", line 73, in jit
return jit_form(ufl_object, parameters, common_cell)
File "C:\FEniCS\lib\site-packages\ffc\jitcompiler.py", line 130, in jit_form
common_cell=common_cell)
File "C:\FEniCS\lib\site-packages\ffc\compiler.py", line 150, in compile_form
analysis = analyze_forms(forms, object_names, parameters, common_cell)
File "C:\FEniCS\lib\site-packages\ffc\analysis.py", line 64, in analyze_forms
common_cell) for form in forms)
File "C:\FEniCS\lib\site-packages\ffc\analysis.py", line 64, in
common_cell) for form in forms)
File "C:\FEniCS\lib\site-packages\ffc\analysis.py", line 151, in _analyze_form

ffc_assert(len(compute_form_arities(preprocessed_form)) == 1,

File "C:\FEniCS\lib\site-packages\ufl\algorithms\formtransformations.py", line
332, in compute_form_arities
parts = compute_form_with_arity(form, arity)
File "C:\FEniCS\lib\site-packages\ufl\algorithms\formtransformations.py", line
317, in compute_form_with_arity
res = transform_integrands(form, _transform)
File "C:\FEniCS\lib\site-packages\ufl\algorithms\transformations.py", line 837
, in transform_integrands
integrand = transform(itg.integrand())
File "C:\FEniCS\lib\site-packages\ufl\algorithms\formtransformations.py", line
313, in _transform
e, provides = pe.visit(e)
File "C:\FEniCS\lib\site-packages\ufl\algorithms\transformations.py", line 160
, in visit
r = h(o)
File "C:\FEniCS\lib\site-packages\ufl\algorithms\formtransformations.py", line
68, in expr
error("Found Argument in %s, this is an invalid expression." % repr(x))
File "C:\FEniCS\lib\site-packages\ufl\log.py", line 148, in error
raise self._exception_type(self._format_raw(*message))
ufl.log.UFLException: Found Argument in Power(Sum(Indexed(Argument(MixedElement(
*[FiniteElement('Lagrange', Cell('triangle', Space(2)), 1, None), FiniteElement(
'Lagrange', Cell('triangle', Space(2)), 1, None)], **{'value_shape': (2,) }), 0)
, MultiIndex((FixedIndex(0),), {})), Product(IntValue(-1, (), (), {}), Coefficie
nt(FiniteElement('Lagrange', Cell('triangle', Space(2)), 5, None), 0))), IntValu
e(2, (), (), {})), this is an invalid expression.

Thanks

Can i know what kind of formatting?

Follow this question to fix formatting. If you don't fix the formatting of your question, nobody is probably able to help you.

Hi
Actually running the above program in python format in windows FEniCS. Here i consider Finite element method for optimal control of elliptic partial differential equations paper available here and using linear Lagrange element method.
Thank you

Please, edit your question and comments indenting the code inthere by four spaces to make it readable.

Hi, Actually running the program in python format in windows FEniCS. Here i consider Finite element method for optimal control of elliptic partial differential equations using linear Lagrange element method. While run the program, i get error on assemble part everywhere occurred in program mentioned given below:
1) error = sqrt(assemble(error_y))
Similiar, Same way getting error in line given below
2) error = sqrt(assemble(error_p))
3) error = sqrt(assemble(error_u))
4) A = assemble(a, exterior_facet_domains=boundary_parts)
5) b = assemble(L, exterior_facet_domains=boundary_parts)
Here the following error occurring:
File "opt.py", line 78, in
error = sqrt(assemble(error_y))
File "C:\FEniCS\lib\site-packages\dolfin\fem\assembling.py", line 155, in asse
mble
common_cell=common_cell)
File "C:\FEniCS\lib\site-packages\dolfin\fem\form.py", line 54, in init
common_cell)
File "C:\FEniCS\lib\site-packages\dolfin\compilemodules\jit.py", line 66, in m
pi_jit
return local_jit(*args, **kwargs)
File "C:\FEniCS\lib\site-packages\dolfin\compilemodules\jit.py", line 154, in
jit
return jit_compile(form, parameters=p, common_cell=common_cell)
File "C:\FEniCS\lib\site-packages\ffc\jitcompiler.py", line 73, in jit
return jit_form(ufl_object, parameters, common_cell)
File "C:\FEniCS\lib\site-packages\ffc\jitcompiler.py", line 130, in jit_form
common_cell=common_cell)
File "C:\FEniCS\lib\site-packages\ffc\compiler.py", line 150, in compile_form
analysis = analyze_forms(forms, object_names, parameters, common_cell)
File "C:\FEniCS\lib\site-packages\ffc\analysis.py", line 64, in analyze_forms
common_cell) for form in forms)
File "C:\FEniCS\lib\site-packages\ffc\analysis.py", line 64, in
common_cell) for form in forms)
File "C:\FEniCS\lib\site-packages\ffc\analysis.py", line 151, in _analyze_form
ffc_assert(len(compute_form_arities(preprocessed_form)) == 1,
File "C:\FEniCS\lib\site-packages\ufl\algorithms\formtransformations.py", line
332, in compute_form_arities
parts = compute_form_with_arity(form, arity)
File "C:\FEniCS\lib\site-packages\ufl\algorithms\formtransformations.py", line
317, in compute_form_with_arity
res = transform_integrands(form, _transform)
File "C:\FEniCS\lib\site-packages\ufl\algorithms\transformations.py", line 837
, in transform_integrands
integrand = transform(itg.integrand())
File "C:\FEniCS\lib\site-packages\ufl\algorithms\formtransformations.py", line
313, in _transform
e, provides = pe.visit(e)
File "C:\FEniCS\lib\site-packages\ufl\algorithms\transformations.py", line 160
, in visit
r = h(o)
File "C:\FEniCS\lib\site-packages\ufl\algorithms\formtransformations.py", line
68, in expr
error("Found Argument in %s, this is an invalid expression." % repr(x))
File "C:\FEniCS\lib\site-packages\ufl\log.py", line 148, in error
raise self._exception_type(self._format_raw(*message))
ufl.log.UFLException: Found Argument in Power(Sum(Indexed(Argument(MixedElement(
*[FiniteElement('Lagrange', Cell('triangle', Space(2)), 1, None), FiniteElement(
'Lagrange', Cell('triangle', Space(2)), 1, None)], **{'value_shape': (2,) }), 0)
, MultiIndex((FixedIndex(0),), {})), Product(IntValue(-1, (), (), {}), Coefficie
nt(FiniteElement('Lagrange', Cell('triangle', Space(2)), 5, None), 0))), IntValu
e(2, (), (), {})), this is an invalid expression.
Here Python Code for Finite element methods of optimal control problems, further clarification:

from dolfin import *
from math import *
import sys
nx = 32 
ny = 32
y_0=0
mesh = UnitSquare (nx,ny)
X = FunctionSpace (mesh,'Lagrange',1)
Y = FunctionSpace (mesh,'Lagrange',1)
Z = X*Y
(y,p) = TrialFunctions(Z)
(si,phi) = TestFunctions(Z)
f = Expression('x[1]*x[2]/2-x[1]')
y0 = Constant ('1')
d = Constant('1')                             
a = inner(grad(phi),grad(p))*dx+phi*p*dx-phi*y*ds(1)+inner(grad(y),grad(si))*dx+y*si*dx-(1/d)*p*si*ds(0)
L = f*si*dx-y0*phi*ds(1)
boundary_parts = MeshFunction ('uint', mesh, 1)
class LowerNeumannBoundary(SubDomain):
    def inside(self,x,on_boundary):
        tol = 1E-14
        return on_boundary and abs(x[1]) < tol
L = LowerNeumannBoundary()
L.mark(boundary_parts, 0)
class UpperNeumannBoundary(SubDomain):
    def inside(self, x, on_boundary):
        tol = 1E-14
        return on_boundary and abs(x[1] - 1) < tol
U = UpperNeumannBoundary()
U.mark(boundary_parts, 1)
class RestNeumannBoundary(SubDomain):
    def inside(self, x, on_boundary):
        tol = 1E-14
        return on_boundary and (abs(x[0]) < tol or abs (x[0] - 1) < tol)
u=0
y_exact = Expression('(cosh(x[1] - 1)/(1 - d*sinh(1)*sinh(1)))*(y_0+1/sinh(1) - .5) - (cosh(x[1] - 1)/sinh(1)) + x[1] * x[1]/2 - x[1] + 1' ,y_0 = 1,d = 1)
p_exact = Expression('((d*sinh(1)*cosh(x[1]))/(1-d*sinh(1)*sinh(1)))*(y_0 + (1.0/sinh(1)) - .5)',y_0 = 1,d = 1)
u_exact = Expression('-sinh(1)/(1-p*sinh(1)*sinh(1))*(u0+(1/sinh(1))-0.5)',u0=1,p=1)
Xe = FunctionSpace(mesh, 'Lagrange', 5)
y_e = interpolate(y_exact,Xe)
Ye = FunctionSpace(mesh, 'Lagrange', 5)
p_e = interpolate (p_exact, Ye,)
ze = FunctionSpace(mesh, 'Lagrange', 5)
u_e = interpolate(u_exact ,ze)
error_y = (y - y_e)*(y - y_e)*dx
error = sqrt(assemble(error_y))
print"y_error:",error
error_p = (p - p_e)*(p - p_e)*dx
error = sqrt(assemble(error_p))
print"p_error:",error
error_u = (u - u_e)*(u - u_e)*dx
error = sqrt(assemble(error_u))
print"u_error:",error
R = RestNeumannBoundary()
R.mark(boundary_parts,23)
M= assemble(y_0*phi*ds(1),exterior_facet_domains=boundary_parts)
A = assemble(a, exterior_facet_domains=boundary_parts)
b = assemble(L, exterior_facet_domains=boundary_parts)
s = Function(Z)
solve(A,s.vector(),b)
(y,p) = s.split()
print'(y,p) :',s.vector().array()
print'n'
print'(y) :',y.vector().array()
print'n'
print'(p) :',p.vector().array()
print'(M) :',M.array()
B= (.5,.5)
print'y_e at the centor:',y_e(B)
print'y at the centor:',y(B)
print'p_e at the centor:',p_e(B)
print'p at the centor:',p(B)
cost=inner(y-y_0,y-y_0)*ds(1)+(1.0/d)*inner(p,p)*ds(0)
J_h=(1.0/2)*assemble(cost, exterior_facet_domains=boundary_parts)
coste=inner(y_e_Ve-y_0,y_e_Ve-y_0)*ds(1)+(1.0/d)*inner(p_e_Ve,p_e_Ve)*ds(0)
J_ex=(1.0/2)*assemble(coste, exterior_facet_domains=boundary_parts)
E5=abs(J_h-J_ex)
t_j=f_pv+s_pv
print'(y_m):',max (y.vector().array())
print'(y_em):',max (y_e_Ve.vector().array())
print'(y_n):',min (y.vector().array())
print'(y_en):',min (y_e_Ve.vector().array())
print"n"
print'f_pv:',f_pv
print's_pv:',s_pv
print'tot:',t_j
print'J_h(y,u):',J_h
print'J_e(y,u):',J_ex
print'(p_m):', max(p.vector().array())        
print'(p_em):', max(p_e_Ve.vector().array())  
print'(p_n):', min(p.vector().array())        
print'(p_en):', min(p_e_Ve.vector().array())  
plot(y,title="yplot")
plot(p,title="pplot")
plot(mesh,title="mesh")
interactive()

Your last comment is useless as well - it lacks indentation. Add no more comments! Click on edit just under your original question on top and indent the code by four spaces! Then do the same procedure with your comments. Is it understandable?

Hi Jan Blechta
I edit my code by four spaces indent its fine.

1 Answer

+2 votes

This is very poor question, asking us to debug your code. Nevertheless

  • move lines

    error_y = (y - y_e)*(y - y_e)*dx
    error = sqrt(assemble(error_y))
    print"y_error:",error
    error_p = (p - p_e)*(p - p_e)*dx
    error = sqrt(assemble(error_p))
    print"p_error:",error
    error_u = (u - u_e)*(u - u_e)*dx
    error = sqrt(assemble(error_u))
    print"u_error:",error
    

    after line

    (y,p) = s.split()
    
  • Assembled linear form must reference all the trial functions of respective mixed space

    M= assemble(y_0*phi*ds(1)+Constant(0.0)*si*ds(1),exterior_facet_domains=boundary_parts)
    
  • There's a name clash, you define multiple variables named L.

  • There's plenty of undefined variables.
answered Sep 27, 2013 by Jan Blechta FEniCS Expert (51,420 points)

I try this, but getting same error mentioned above blockquotes, espically in line 78 on program code

error = sqrt(assemble(error_y))

As I said above, you need to move lines for computing errors below the point you define the solution function and its components. It has no good meaning to take difference of TrialFunction and Function.

...