You can't add Function and Form, it's not an operation that makes sense mathematically. A (linear) form is a functional, not a function.
What you can do is something similar to
f = Function(V)
v = TestFunction(V)
form += inner(f,v) * dx()
In a sense this adds f to form, but does so in mathematically meaningful way by describing how f acts on the testfunction v.
For examples of subclassing NonlinearProblem, see the Cahn-Hilliard demo or the example below.
class ExtendedNonlinear(NonlinearProblem):
def __init__(self, F, u, d, bcs = []):
NonlinearProblem.__init__(self)
self.F_form = F
self.u = u
self.d = d
self.J_form = derivative(F, u, TrialFunction(u.function_space()))
self.bcs = bcs
def F(self, b, x):
self.u.assign(Function(u.function_space(), x))
# assemble then add the vector d
assemble(self.F_form, tensor = b)
b += self.d
for bc in self.bcs: bc.apply(b)
def J(self, A, x):
self.u.assign(Function(u.function_space(), x))
assemble(self.J_form, tensor=A)
for bc in self.bcs: bc.apply(A)
mesh = UnitSquareMesh(64, 64)
V = FunctionSpace(mesh, "CG", 1)
u = Function(V)
v = TestFunction(V)
F = inner(grad(u), grad(v)) * dx
d = - assemble(Constant(1.) * v * dx)
bcs = [DirichletBC(V, 0., "on_boundary")]
nlp = ExtendedNonlinear(F, u, d, bcs)
solver = NewtonSolver()
solver.solve(nlp, u.vector())