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

Applying dirichlet condition using the class of nonlinear problem

+1 vote

Hi! I'm trying to solve a non linear problem following the cahn hilliard demo but I have a system made up by three equations. I have dirichlet conditions for the first and the third variables.

mesh=Mesh("brain.xml")
V    = FunctionSpace(mesh,"Lagrange",1)
ME   = MixedFunctionSpace([V,V,V])

g_c  = Constant(0.0)
bc_c = DirichletBC(ME.sub(0), g_c, DirichletBoundary())
g_n  = Constant(1.0)
bc_n = DirichletBC(ME.sub(2), g_n, DirichletBoundary())
bcs = [bc_c, bc_n]

If I define the non linear problem as

class MyTumor(NonlinearProblem):
def __init__(self, L, a, bcs):
  NonlinearProblem.__init__(self)
  self.L = L
  self.a = a
  self.bcs = bcs
def F(self, b, x):
  assemble(self.L, tensor=b, bcs=self.bcs)
def J(self, A, x):
  assemble(self.a, tensor=A bcs=self.bcs)

It gives me annoying warnings about applying manually the BC.

If I apply them manually :

class MyTumor(NonlinearProblem):
def __init__(self, L, a, bcs):
  NonlinearProblem.__init__(self)
  self.L = L
  self.a = a
  self.bcs = bcs
def F(self, b, x):
  assemble(self.L, tensor=b)
  self.bcs.apply(b,x)
def J(self, A, x):
  assemble(self.a, tensor=A)
  self.bcs.apply(A)

It gives me this error:
AttributeError: 'list' object has no attribute 'apply'

I recall the problem with

problem = MyTumor(L,J,[bcs])

How can I apply my Dirichlet conditions ??

asked Feb 19, 2015 by MCri FEniCS User (1,120 points)

1 Answer

+4 votes
 
Best answer

Hi, apply is a method of the DirichletBC class and not the list class. To apply each condition from a list of DirichletBC objects the call could be for example

bcs = [bc_c, bc_n]
[bc.apply(A) for bc in bcs] 

Note that [bcs] is a list with one list object.

answered Feb 19, 2015 by MiroK FEniCS Expert (80,920 points)
selected Feb 19, 2015 by MCri

Is there any different between

problem = MyTumor(L,J,[bcs])

and

problem = MyTumor(L,J,bcs)

?
Thanks again!

It is your own class so it is only up to you how the function arguments are handled. But in my opinion there is no need to nest the lists, that is, I'd prefer MyTumor(L,J,bcs).

ok.. thanks a lot!!!

...