# Dirichlet BCs not iterable in newton solver (newton Krylov)

**URL:** <https://fenicsproject.discourse.group/t/dirichlet-bcs-not-iterable-in-newton-solver-newton-krylov/14230>\
**Category:** General\
**Created:** [April 11, 2024, 11:25pm UTC](https://fenicsproject.discourse.group/t/dirichlet-bcs-not-iterable-in-newton-solver-newton-krylov/14230 "2024-04-11T23:25:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aplazar1](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@aplazar1](https://fenicsproject.discourse.group/u/aplazar1)\
**Post date:** [April 11, 2024, 11:25pm UTC](https://fenicsproject.discourse.group/t/dirichlet-bcs-not-iterable-in-newton-solver-newton-krylov/14230/1 "2024-04-11T23:25:06Z")

</div>

Hi, I am faced with the following strange error, which I do not understand: “TypeError: ‘DirichletBC’ object is not iterable”. The error proceeds from solver.solve(u) in the code below. I don’t understand why the Dirichlet BCs need to be iterable? How can I make them iterable? Any help would be greatly appreciated! Thanks!

````import
import matplotlib

import matplotlib.pyplot as plt

from dolfinx import mesh, fem, io, plot, la
import pyvista
import ufl
import numpy as np
from dolfinx import *
import dolfinx
import numpy, sys

from mpi4py import MPI
from petsc4py import PETSc
from ufl import (VectorElement, FiniteElement,
                 SpatialCoordinate, TrialFunction, TestFunction,
                 as_vector, cos, sin, inner, div, grad, dx, pi, system)

from dolfinx import mesh, fem, io, nls, log
from dolfinx.fem.petsc import NonlinearProblem
from dolfinx.nls.petsc import NewtonSolver

##cretae a rectangular mesh with quadrilateral elements
##note: here theta=x

from mpi4py import MPI
domain = mesh.create_unit_square(MPI.COMM_WORLD, 10, 10)
D= fem.Constant(domain, 1.0) ##diffusion constant
x = ufl.SpatialCoordinate(domain)
u_ufl = x[1] - (1/6)*x[1]**3 ##note that u_ufl is the exact solution we are using to interpolate AKA this is the trial fucntion

#define the domain and boundary conditions
V = fem.FunctionSpace(domain, ("Lagrange", 3))

def u_exact(x): 
    return eval(str(u_ufl))

u_D = fem.Function(V)
##set up interpolation calling on the seleted basis (u xact, exact soln)
u_D.interpolate(u_exact)
fdim = domain.topology.dim - 1
#boundary_facets = mesh.locate_entities_boundary(domain, fdim, lambda x: numpy.full(x.shape[1], True, dtype=bool))
#bc = fem.dirichletbc(u_D, fem.locate_dofs_topological(V, fdim, boundary_facets))

domain.topology.create_connectivity(fdim, fdim + 1)
boundary_facets = dolfinx.mesh.exterior_facet_indices(domain.topology)
bc = dolfinx.fem.dirichletbc(u_D, dolfinx.fem.locate_dofs_topological(V, fdim, boundary_facets))

##setting up the variational problem
from ufl import (TestFunction, SpatialCoordinate, TrialFunction,
                 as_vector, dx, grad, inner, system, equation, Constant)

u = fem.Function(V) ##time-dep c at n+1
v = TestFunction(V)
un = fem.Function(V) ##time dep C at n
D= fem.Constant(domain, 1.0) ##diffusion constant
dt = fem.Constant(domain, 0.05)

##define the variational problem

F = (un*v*ufl.dx) 
F -= u*v*dx - sin(u)*u*v*dt*dx - D*dt*inner(grad(u), grad(v))*dx + D*dt*v*dx
# variational problem stated as a=L
problem = NonlinearProblem(F, u, bcs=bc)

solver = NewtonSolver(MPI.COMM_WORLD, problem )
solver.convergence_criterion = "incremental"
solver.rtol = 1e-6
solver.report = True

ksp = solver.krylov_solver
opts = PETSc.Options()
option_prefix = ksp.getOptionsPrefix()
opts[f"{option_prefix}ksp_type"] = "cg"
opts[f"{option_prefix}pc_type"] = "gamg"
opts[f"{option_prefix}pc_factor_mat_solver_type"] = "mumps"
ksp.setFromOptions()

log.set_log_level(log.LogLevel.INFO)
n, converged = solver.solve(u)
assert (converged)
print(f"Number of interations: {n:d}")```
````

---

<div class="post-metadata">

**Author:** ![bagla0](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/bagla0/32/4677_2.png) [@bagla0](https://fenicsproject.discourse.group/u/bagla0)\
**Post date:** [April 11, 2024, 11:32pm UTC](https://fenicsproject.discourse.group/t/dirichlet-bcs-not-iterable-in-newton-solver-newton-krylov/14230/2 "2024-04-11T23:32:11Z")

</div>

The error might remove with update of `NonlinearProblem(F, u, bcs=[bc]) `

---

<div class="post-metadata">

**Author:** ![aplazar1](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@aplazar1](https://fenicsproject.discourse.group/u/aplazar1)\
**Post date:** [April 11, 2024, 11:41pm UTC](https://fenicsproject.discourse.group/t/dirichlet-bcs-not-iterable-in-newton-solver-newton-krylov/14230/3 "2024-04-11T23:41:00Z")

</div>

Thanks so much! Now I obtain the following error:“RuntimeError: Newton solver did not converge because maximum number of iterations reached”. How might I change the number of newton iterations allowed?

---

<div class="post-metadata">

**Author:** ![bagla0](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/bagla0/32/4677_2.png) [@bagla0](https://fenicsproject.discourse.group/u/bagla0)\
**Post date:** [April 11, 2024, 11:48pm UTC](https://fenicsproject.discourse.group/t/dirichlet-bcs-not-iterable-in-newton-solver-newton-krylov/14230/4 "2024-04-11T23:48:13Z")

</div>

[Nonlinear problem](https://github.com/FEniCS/dolfinx/blob/387ff1279c3470fb4956e54f47e00ce81d2f9c1d/python/dolfinx/fem/petsc.py#L854-L857) page could help by changing jit\_options.
