Time dependent variables inside variational formulation

Dear all,

I am trying to solve a problem whose linear form has a term that contains a time dependent variable. For example:

Term_1 = ufl.inner(u,v)*ufl.dx
Term_2 = time_dependent_variable*ufl.inner(u_n,v)*ufl.ds # Time dependent variable inside variational form
Form = Term_1 + Term_2
bilinear_form = ufl.lhs(Form)
linear_form = ufl.rhs(Form)

where time_dependent_variable is a numerical value that changes between iterations:

time_dependent_variable = 1.0
for i in range(num_steps):
    time_dependent_variable += 0.1

The problem is that, when I update the solution and right hand side at each time step, the Term_2 is recomputed with the initial value of the time_dependent_variable (instead of using the value that it takes at the given time step).

Here is the simplified code (note that it is a toy model, the equations do not represent any physical problem):

import dolfinx
import ufl
import numpy as np
from petsc4py import PETSc
from mpi4py import MPI

# Domain
mesh = dolfinx.UnitSquareMesh(MPI.COMM_WORLD, 10, 10)

# Define temporal parameters
t = 0 # Start time
T = 2.0 # Final time
num_steps = 61     
dt = T / num_steps # time step size

# Initialise time dependent variable
time_dependent_variable = 1.0

# Define Element space
from dolfinx.fem import FunctionSpace, Function, form, assemble_matrix, create_vector, assemble_vector, apply_lifting, assemble_scalar
V = FunctionSpace(mesh, ("CG", 1))

# Create initial condition
def initial_condition(x, a=5):
    return np.exp(-(x[0]**2+x[1]**2))
u_n = Function(V)
u_n.name = "u_n"
u_n.interpolate(initial_condition)
# Define solution variable, and interpolate initial solution for visualization in Paraview
uh = Function(V)
uh.name = "uh"
uh.interpolate(initial_condition)

# Define variational form
import ufl
u = ufl.TrialFunction(V)
v = ufl.TestFunction(V)
Term_1 = ufl.inner(u,v)*ufl.dx
Term_2 = time_dependent_variable*ufl.inner(u_n,v)*ufl.ds
Form = Term_1 + Term_2 # Time dependent variable inside variational form
bilinear_form = ufl.lhs(Form)
linear_form = ufl.rhs(Form)
A = assemble_matrix(bilinear_form, bcs=[])
A.assemble()
b = create_vector(linear_form)

# Solver
solver = PETSc.KSP().create(mesh.mpi_comm())
solver.setOperators(A)
solver.setType(PETSc.KSP.Type.PREONLY)
solver.getPC().setType(PETSc.PC.Type.LU)

# Time steps
for i in range(num_steps):
    t += dt

    # Update the right hand side reusing the initial vector
    with b.localForm() as loc_b:
        loc_b.set(0)
    assemble_vector(b, linear_form)

    # Solve linear problem
    solver.solve(b, uh.vector)
    uh.x.scatter_forward()

    # Update solution at previous time step (u_n)
    u_n.x.array[:] = uh.x.array

    # Define time dependent variable
    time_dependent_variable += 0.1

    print(assemble_scalar(Term_2)/assemble_scalar(ufl.inner(u_n,v)*ufl.ds)) # It always gives the initial value of time_dependent_variable instead of changing!

I could solve the problem by recomputing the variational form inside the loop, but this may be computationally inefficient in problems with more complex variational form.

Does anybody know an alternative to solve this issue?
Please, let me know if anything is not clear and thanks in advance!

Wrap your time_dependent_variable in a Constant, e.g.

time_dependent_variable = dolfinx.fem.Constant(mesh, 0.0)
...
while in_time_loop:
    ...
    time_dependent_variable.value += 0.1
    ...

It worked perfectly.
Thank you very much for the speedy response!