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

name 'homogenize' is not defined

0 votes

Hi, I am running FEniCS 2016.1 and the homogenize funcion is not recognized.
https://fenicsproject.org/documentation/dolfin/1.5.0/python/programmers-reference/fem/bcs/homogenize.html

For example, with:

from dolfin import *
mesh = UnitSquareMesh(4, 4)
V = FunctionSpace(mesh, 'Lagrange', 1)
u0 = Expression('1 + x[0]*x[0] + 2*x[1]*x[1]')
def u0_boundary(x, on_boundary):
    return on_boundary

bc = DirichletBC(V, u0, u0_boundary)
bc0 = homogenize(bc)

I get the following error:
bc0 = homogenize(bc)
NameError: name 'homogenize' is not defined

What is wrong? Any help would be appreciate.

asked Oct 13, 2016 by fussegli FEniCS Novice (700 points)

Hi, homogenize is now a member function of DirichletBC class. See e.g. the last point in release notes version 1.6.0

Ok, i have seen it, thx.
Then, the correct syntax is now

bc0 = bc.homogenize()

Besides, the documentation is

homogenize()
Set value to 0.0

So, is this equivalent to

bc0 = DirichletBC(V, 0, u0_boundary)

I am not sure to understand what does exactly the function.

You understand it correctly.

Well, then what is the interest of the function homogenize, if it only means set the dirichlet value to 0?
There should be a deeper reason for the fonction homogenize to exist, no?

Besides, the outcome of two syntax are not identical:

from dolfin import *
mesh = UnitSquareMesh(4, 4)
V = FunctionSpace(mesh, 'Lagrange', 1)
u0 = Expression('1 + x[0]*x[0] + 2*x[1]*x[1]')
def u0_boundary(x, on_boundary):
    return on_boundary

bc = DirichletBC(V, u0, u0_boundary)

bc = bc.homogenize()
# or
bc = DirichletBC(V, 0, u0_boundary)

u = TrialFunction(V)
v = TestFunction(V)
f = Constant(0)
a = inner(nabla_grad(u), nabla_grad(v))*dx
L = f*v*dx
Compute solution
u = Function(V)
solve(a == L, u, bc)

is working for both syntax

But:

M = assemble(a)
bc.apply(M)

I get the following error with bc = bc.homogenize()

AttributeError: 'NoneType' object has no attribute 'apply

and no error with bc = DirichletBC(V, 0, u0_boundary)

Then, there is a difference somewhere.
And how can i use the function apply with a BC defined with homogenize?

1 Answer

0 votes

To answer the questions from your comments, consider

from dolfin import *

mesh = UnitSquareMesh(10, 10)
V = FunctionSpace(mesh, 'CG', 1)
u = TrialFunction(V)
v = TestFunction(V)
a = inner(grad(u), grad(v))*dx
A = PETScMatrix(); assemble(a, A)
A_h = PETScMatrix(); assemble(a, A_h)

bc = DirichletBC(V, Constant(0), 'on_boundary')
bc_h = DirichletBC(V, Constant(1), 'on_boundary')
foo = bc_h.homogenize()      # Now bc_h acts like bc
print type(foo)              # Not DirichletBC but none!

bc.apply(A)
print '|A|', A.norm('linf')
bc_h.apply(A_h)
print '|A_h|', A_h.norm('linf')
# Show they are same
A -= A_h
print '|A-A_h|', A.norm('linf')
answered Oct 14, 2016 by MiroK FEniCS Expert (80,920 points)
...