Dirichlet BCs not iterable in newton solver (newton Krylov)

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 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}")```

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

1 Like

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?

Nonlinear problem page could help by changing jit_options.

1 Like