Assign time dependent BCs in a class

I need to assign a time dependent BCs. I first defined a Constant and then at every time step assign time dependent value to this constant. Everything works fine if I’m doing this in a function. However, when I try to do this in a python class, the time dependent BCs doesn’t work any more. Here’s a code example to reproduce this:

import matplotlib.pyplot as plt
from dolfin import *

# Optimization options for the form compiler
parameters["form_compiler"]["cpp_optimize"] = True
parameters["form_compiler"]["representation"] = "uflacs"

r_global=Constant((0.1,0,0))

class Test(object):
    def __init__(self, useglobal=False):
        if (useglobal):
            print("Use global r")
            self.r=r_global
        else:
            self.r = Constant((0.1,0,0))
        
        return None
    
    def setup(self):

        # Create mesh and define function space
        mesh = UnitCubeMesh(24, 12, 12)
        V = VectorFunctionSpace(mesh, "Lagrange", 1)

        # Mark boundary subdomians
        left =  CompiledSubDomain("near(x[0], side) && on_boundary", side = 0.0)
        right = CompiledSubDomain("near(x[0], side) && on_boundary", side = 1.0)

        # Define Dirichlet boundary (x = 0 or x = 1)
        c = Constant((0.0, 0.0, 0.0))

        bcl = DirichletBC(V, c, left)
        bcr = DirichletBC(V, r, right)
        self.bcs = [bcl, bcr]

        # Define functions
        du = TrialFunction(V)            # Incremental displacement
        v  = TestFunction(V)             # Test function
        u  = Function(V)                 # Displacement from previous iteration
        B  = Constant((0.0, -0.5, 0.0))  # Body force per unit volume
        T  = Constant((0.1,  0.0, 0.0))  # Traction force on the boundary

        self.u=u
        self.v=v
        
        # Kinematics
        d = len(u)
        I = Identity(d)             # Identity tensor
        F = I + grad(u)             # Deformation gradient
        C = F.T*F                   # Right Cauchy-Green tensor

        # Invariants of deformation tensors
        Ic = tr(C)
        J  = det(F)

        # Elasticity parameters
        E, nu = 10.0, 0.3
        mu, lmbda = Constant(E/(2*(1 + nu))), Constant(E*nu/((1 + nu)*(1 - 2*nu)))

        # Stored strain energy density (compressible neo-Hookean model)
        psi = (mu/2)*(Ic - 3) - mu*ln(J) + (lmbda/2)*(ln(J))**2

        # Total potential energy
        Pi = psi*dx - dot(B, u)*dx - dot(T, u)*ds

        # Compute first variation of Pi (directional derivative about u in the direction of v)
        F = derivative(Pi, u, v)

        # Compute Jacobian of F
        J = derivative(F, u, du)

        self.F=F
        self.J=J
        
        dt = 0.1
        t=0
#        file = File("displacement.pvd")
        while t < 0.5:
            t+=dt
            self.r.assign(Constant((t,0,0)))
            print(self.r.values())
            # Solve variational problem
            solve(self.F == 0, self.u, self.bcs, J=self.J)
            
        return None
        
TT_useglobal=Test(useglobal=True)
TT_useglobal.setup()        

The code runs fine but these lines appear to not have any effect on the simulation:

self.r.assign(Constant((t,0,0)))

Any suggestions?

Your code is incomplete. When you use r in your DirichletBC I’m assuming you want it to be self.r?

Making this change, I see the change in r reflected in the solution with each continuation step.