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

Is there a way to implement BCs for NonlinearProblem class for mixed function spaces

+1 vote

For e.g.,
In 3. Cahn-Hilliard equation tutorial,

Assume we have a dirichlet boundary condition for c:

    class DirichletBoundary(SubDomain):
    def inside(self, x, on_boundary):
        return on_boundary

    u0 = Constant(0.0)
    bc = DirichletBC(V, u0, DirichletBoundary())

Where does this bc fit? How to make bc only work for c, not for mu?

    class CahnHilliardEquation(NonlinearProblem):
    def __init__(self, a, L):
        NonlinearProblem.__init__(self)
        self.L = L
        self.a = a
        self.reset_sparsity = True
    def F(self, b, x):
        assemble(self.L, tensor=b)
    def J(self, A, x):
        assemble(self.a, tensor=A, reset_sparsity=self.reset_sparsity)
        self.reset_sparsity = False
    # Create mesh and define function spaces


    mesh = UnitSquareMesh(96, 96)
    V = FunctionSpace(mesh, "Lagrange", 1)
    ME = V*V

    # Define trial and test functions
    du    = TrialFunction(ME)
    q, v  = TestFunctions(ME)

   # Define functions
   u   = Function(ME)  # current solution
   u0  = Function(ME)  # solution from previous converged step

   # Split mixed functions
  dc, dmu = split(du)
  c,  mu  = split(u)
  c0, mu0 = split(u0)

       # Weak statement of the equations
    L0 = c*q*dx - c0*q*dx + dt*dot(grad(mu_mid), grad(q))*dx
    L1 = mu*v*dx - dfdc*v*dx - lmbda*dot(grad(c), grad(v))*dx
    L = L0 + L1

     # Compute directional derivative about u in the direction of du (Jacobian)
    a = derivative(L, u, du)

     # Create nonlinear problem and Newton solver
    problem = CahnHilliardEquation(a, L)
    solver = NewtonSolver("lu")
asked Jul 25, 2013 by cutejeff FEniCS Novice (230 points)

1 Answer

+3 votes
 
Best answer

Assuming it is mathematically correct way, i.e. it is still well-posed problem, then

class CahnHilliardEquation(NonlinearProblem):
def __init__(self, a, L, bcs):
    NonlinearProblem.__init__(self)
    self.L = L
    self.a = a
    self.bcs = bcs
    self.reset_sparsity = True
def F(self, b, x):
    assemble(self.L, tensor=b, bcs=self.bcs)
def J(self, A, x):
    assemble(self.a, tensor=A, reset_sparsity=self.reset_sparsity, bcs=self.bcs)
    self.reset_sparsity = False
...
bc = DirichletBC(ME.sub(0), u0, DirichletBoundary())
...
problem = CahnHilliardEquation(a, L, [bc])
...
answered Jul 25, 2013 by Jan Blechta FEniCS Expert (51,420 points)
selected Aug 8, 2013 by Jan Blechta
...