Affine transformation: Translation X-Axis of Function

How can this operation be accounted for while computing the derivative with dolfin_adjoint?

I get the following error: AttributeError: ‘float’ object has no attribute ‘_cpp_object’
I tried to see how it is commonly done when defining UserExpression as shown in here, but cannot manage to adapt it to my problem …

Here follows a minimal non-working example to show what is desired. You can comment the lines after the plots to have it running without error:

from dolfin import * 
from dolfin_adjoint import *

import matplotlib.pyplot as plt
import numpy as np

lx = 1.0
xmin=0.0
xmax=1.0

mesh = UnitSquareMesh(16,16)
V = FunctionSpace(mesh,'CG', 1)

u_0 = interpolate(Expression("x[0]",degree=3), V)

# Shift spatially along x the dofs
b = lx/2.0 #shift along x-axis

class ShiftedExpr(UserExpression):
    def __init__(self,func,**kwargs):
        super().__init__(**kwargs)
        self.func = func
    def eval(self,values,x):
        x0_shift = x[0] - b
        if(x0_shift < xmin):
            x0_shift += lx
        x_shift = np.array([x0_shift,x[1]])
        values[0] = self.func(x_shift)
    def value_shape(self):
        return ()
    
u_1 = project(ShiftedExpr(u_0),V)

pl, ax = plt.subplots();
fig = plt.gcf()
fig.set_size_inches(16, 4)
plt.subplot(1, 2, 1); p = plot(u_0, title='Initial distribution', mode='color',vmin=0.0,vmax=1.0)
p.set_cmap("viridis"); cbar = plt.colorbar(p); # add a colorbar
plt.subplot(1, 2, 2); p = plot(u_1, title='Shifted distribution', mode='color',vmin=0.0,vmax=1.0)
p.set_cmap("viridis"); cbar = plt.colorbar(p); # add a colorbar

# Comment the following to have it running without error
m0 = Control(u_0)
J = -assemble((u_1**2) *dx)**(1/2) # Random Functional to serve as objective function
Jhat = ReducedFunctional(J,m0)
dJdmh0 = Jhat.derivative()

# Taylor remainder test to verify the computation of the gradient
h = Function(V)
h.vector()[:] = 0.01*(1.0- 0.0)
conv_rate = taylor_test(Jhat,u_0,h) #Uses the previously setted 

Thank you for your help.