How to change grid borders over time

Hello. I set the right border of the grid in the variable L_f, which depends on the time. How to change the grid in a loop so that the border changes according to the value of L_f?

from dolfin import *
import matplotlib.pyplot as plt
import numpy as np
import scipy.special
import math
import os

plt.figure(figsize=(10, 5), dpi=80)

alpha = 1 #Показатель производной

T = 1
M = 50
dt = T/M
nx = 65
t = 0

k0 = 1
A = 0.5
sigma = 2
n = -1/(0.5*sigma)  ### n = -1/sigma - Sрежим, n < -1/sigma - HSрежим, 0 > n > -1/sigma - LSрежим

#L_f = np.sqrt(2*k0*(A**sigma)*(sigma+2)/sigma)
L_f = Expression('pow(2*k0*(pow(A, sigma))*(sigma+2)/sigma, 0.5)*pow(1-t, (1+n*sigma)*0.5)', degree=10, t=t, k0=k0, A=A, sigma=sigma, n=n)
print('L_f', L_f(t))

mesh = IntervalMesh(nx, 0, L_f(t))
V = FunctionSpace(mesh, 'CG', 10)

xi = Expression('x[0]/(pow(k0, 0.5)*pow(A, sigma*0.5)*pow(1-t, (1+n*sigma)*0.5))', degree=10, sigma=sigma, n=n, t=t, k0=k0, A=A)

if n == -1/sigma:
	u0 = Expression('A*pow(1-t,n)*pow((1-(x[0])/L_f),(2/sigma))', degree=10, sigma=sigma, t=t, n=n, A=A, k0=k0, L_f=L_f)
	u0 = Expression('A*pow(1-t,n)*pow((1-(xi)/L_f),(2/sigma))', degree=10, sigma=sigma, t=t, n=n, A=A, k0=k0, xi=xi, L_f=L_f)

def b1(x, on_boundary):
    return near(x[0], 0)
def b2(x, on_boundary):
	if x[0] <= L_f(t):
		return near(x[0], L_f(t))
		return 0

bc1 = DirichletBC(V, u0, b1)
bc2 = DirichletBC(V, 0, b2)
v = TestFunction(V)
u = Function(V)

def q(uk):
	return uk**sigma

ut = [interpolate(u0, V) for j in range(M)] #Значение функции u в начальный момент времени

for k in range(1, M):
    t = dt * k
    L_f.t = t
    xi.t = t
    u0.t = t
    u0.xi = xi

    F = u*v*dx + (dt**alpha)*dot(q(ut[k-1])*grad(u), grad(v))*dx - (ut[0] - sum( (-1)**j * scipy.special.binom(alpha, j) * (ut[k-j]-ut[0]) for j in range(1, k)) )*v*dx
    J = derivative(F, u)
    problem = NonlinearVariationalProblem(F, u, [bc1,bc2], J)
    solver  = NonlinearVariationalSolver(problem)
    prm = solver.parameters
    prm['newton_solver']['absolute_tolerance'] = 1E-5
    prm['newton_solver']['relative_tolerance'] = 1E-4
    prm['newton_solver']['maximum_iterations'] = 50000
    prm['newton_solver']['relaxation_parameter'] = 1.0

    if k % 10 == 0 or k == M - 1:

plt.title(f'alpha={alpha}, \u0394x={1/nx}')

plt.ylim(-0.5, 25)

See: How to use ALE for moving boundary problems? - #2 by dokken

Is there an example with IntervalMesh? And then it doesn’t work for me if UnitSquareMesh is replaced with IntervalMesh

Consider the following:

from dolfin import *
from typing import List
mesh = UnitIntervalMesh(10)

V = VectorFunctionSpace(mesh, "CG", 1)
x = SpatialCoordinate(mesh)
disp = project(as_vector((x[0],)), V)

disp_bc = DirichletBC(V, disp, "near(x[0], 1)")
fixed_bc = DirichletBC(V, Constant((0,)), "near(x[0], 0)")

def deform_mesh(V:FunctionSpace, bcs: List[DirichletBC]):
    mesh = V.mesh()
    uh = Function(V)
    u = TrialFunction(V)
    v = TestFunction(V)
    a = inner(grad(u), grad(v))*dx
    L = inner(Constant((0.,)), v)*dx
    solve(a==L, uh, bcs)
    ALE.move(mesh, uh)

outfile = File("mesh.pvd")
outfile << mesh
deform_mesh(V, [disp_bc, fixed_bc])
outfile << mesh

By changing FunctionSpace to VectorFunctionSpace, an error appears:

*** Error: Unable to create Dirichlet boundary condition.
*** Reason: Expecting a vector-valued boundary value but given function is scalar.

Please provide a minimal code, as I do not understand what you have changed in your code to get this error

In my code, I replaced FunctionSpace with VectorFunctionSpace and an error appeared

Which FunctionSpace? This one:


This is why Im asking you to post the code that gives you the error message, as this is a type-hint, and should have no effect on your code, thus there is something else that is not working.

I cannot help you any further without you posting the code you are running