Compute inhomogeneous DirichletBC for increment

Hi everyone,

I have a question regarding DirichletBCs: I want, in a custom Newton solver, to define the DIrichletBC for the Newton update, i.e., given some boundary condition u = u_D \text{ on } \Gamma on some boundary \Gamma and a Newton iterate u_k, the DirichletBC I want to apply is \delta u = u_D - u_k \text{ on } \Gamma, as is described in this demo for dolfinx: Custom Newton solvers — FEniCSx tutorial

A minimal example can be seen below:

from fenics import *

mesh = UnitSquareMesh(4, 4)
V = FunctionSpace(mesh, "CG", 1)

boundaries = MeshFunction("size_t", mesh, dim=mesh.topology().dim() - 1)

gamma = CompiledSubDomain(
    "on_boundary && near(x[0], 0, tol)", tol=DOLFIN_EPS
gamma.mark(boundaries, 1)

u_k = Function(V)
u_k.vector()[:] = 0.1 # initialize u_k to some value,

delta_u = Function(V) # Newton increment
u_D = Constant(1.0) # Boundary function

bc = DirichletBC(V, u_D, boundaries, 1) # The inhomogeneous DirichletBC
bcs_delta = DirichletBC(bc) # Copy the boundary condition as initialization

# How to set bcs_delta to the correct value?

Note, that I want to be able to do so not only for scalar problems, but also for any type of Mixed formulation and vector valued problems.

Thanks a lot for your help,

Okay, I have found the answer. This can be done using the assemble routines and specifying the current solution vector, see

Note: This modifies the BCs for the negative increment!