# How to update an UserExpression for the next time step?

Dear community,

I try to solve a transient Poisson equation with Robin BC on an 1x1 UnitSquareMesh domain, which is described by

u′=Δu+f
u=u_0 at t=0

In order to obtain a discretisization in time the Crank-Nicolson method is employed. Due to the implementation of the Crank-Nicolson method, f^{n-1} for the previous time step, called f_prev in the Python code, and f^{n} for the current time step, called f in the Python code, were introduced. Consequently u^{n-1}, called u_prev, and u^{n}, called u, are used.

My source term f, which is just made up for code demonstration, rather than being a reasonable model for any physical system, is described by the following UserExpression:

class MySourceConcentration(UserExpression):
def __init__(self, t, u, **kwargs):
super().__init__(**kwargs)
self.t = t
self.u = u

def eval(self, values, x):

# Calculate the source term f for t>0
if self.t >0:
if x[0]>0 and x[0]<=0.5:
# Calculate average of u for the domain x>=0 and x<=0.5
u_avg = assemble(self.u*dx(1)) / assemble(Constant(1.0)*dx(1))

# Calculate the surface area of a particle
area = area_0 - dt * (1 - u_avg)

# Calculate source term f
values[0] = Constant1 * area * (Constant2 - u_avg)

# for the rest of the domain: source term f=0
else:
values[0] = 0

# Set the source term f for t=0
else:
if x[0]>0 and x[0]<=0.5:
values[0] = f_0
else:
values[0] = 0

def value_shape(self):
return ()


I was told that updating the UserExpression at the end of the main for-loop by using

f_prev = f


should be avoided. So I added the following code lines to the existing UserExpression

    def update(self, t, u):
self.t = t
self.u = u


and updated the UserExpression at the end of the main for-loop in the following matter

f_prev.update(t, u_prev)


My MWCE is running, but I´m unsure, if the implementation is right?

Could you confirm or refute this?

Thanks,
Cou

Here is the full MWCE:

from fenics import *
import numpy as np

# Define source term f
class MySourceConcentration(UserExpression):
def __init__(self, t, u, **kwargs):
super().__init__(**kwargs)
self.t = t
self.u = u

def eval(self, values, x):

# Calculate the source term f for t>0
if self.t >0:
if x[0]>0 and x[0]<=0.5:
# Calculate average of u for the domain x>=0 and x<=0.5
u_avg = assemble(self.u*dx(1)) / assemble(Constant(1.0)*dx(1))

# Calculate the surface area of a particle (equation is made up for demonstrationen purposes without physical correctness)
area = area_0 - dt * (1 - u_avg)

# Calculate source term f
values[0] = Constant1 * area * (Constant2 - u_avg)

# for the rest of the domain: source term f=0
else:
values[0] = 0

# Set the source term f for t=0
else:
if x[0]>0 and x[0]<=0.5:
values[0] = f_0
else:
values[0] = 0

def value_shape(self):
return ()

def update(self, t, u):
self.t = t
self.u = u

# Define Constants
Constant1 = 1
Constant2 = 10
D = Constant(0.6)  # diffusion coefficient
f_0 = 4            # Source term value for t=0
beta = 1.2

T = 2.0            # final time
num_steps = 10     # number of time steps
dt = T / num_steps # time step size

# Create mesh and define function space
nx = ny = 16
mesh = UnitSquareMesh(nx, ny)
space_dim = mesh.geometry().dim()
V = FunctionSpace(mesh, 'CG', 1)

# Create classes for defining parts of the boundaries and the interior
# of the domain
class Left(SubDomain):
def inside(self, x, on_boundary):
return near(x[0], 0.0)

class Right(SubDomain):
def inside(self, x, on_boundary):
return near(x[0], 1.0)

class Bottom(SubDomain):
def inside(self, x, on_boundary):
return near(x[1], 0.0)

class Top(SubDomain):
def inside(self, x, on_boundary):
return near(x[1], 1.0)

class Obstacle_left(SubDomain):
def inside(self, x, on_boundary):
return (between(x[0], (0.0, 0.5)))

class Obstacle_right(SubDomain):
def inside(self, x, on_boundary):
return (between(x[0], (0.5, 1.0)))

# Initialize sub-domain instances
left = Left()
top = Top()
right = Right()
bottom = Bottom()
obstacle_left = Obstacle_left()
obstacle_right = Obstacle_right()

# Initialize mesh function for interior domains
domains = MeshFunction("size_t", mesh, space_dim-1)
obstacle_left.mark(domains, 1)
obstacle_right.mark(domains, 3)

# Initialize mesh function for boundary domains
boundaries = MeshFunction("size_t", mesh, space_dim-1)
boundaries.set_all(0)
left.mark(boundaries, 1)
top.mark(boundaries, 2)
right.mark(boundaries, 3)
bottom.mark(boundaries, 4)

# Define new measures associated with the interior domains and
# exterior boundaries
dx = Measure('dx', domain=mesh, subdomain_data=domains)
ds = Measure('ds', domain=mesh, subdomain_data=boundaries)

vtkfile = File("UserExpression_update.pvd")

################### Initial values (t=0) ######################################
t=0
u_0 = Constant(1)
u_prev = interpolate(u_0, V)
f_prev = MySourceConcentration(t, u_prev, degree=1)

############ Robin boundary condition j_m * n_m = h*(u-c_oo) ##################
h = 1.0
c_oo = 1.0

#################### Define variational problem ###############################
u = TrialFunction(V)
v = TestFunction(V)
f = MySourceConcentration(t, u_prev, degree=1)

a, L = lhs(F), rhs(F)

# Time-stepping
u = Function(V)

for n in range(num_steps):

# Update current time
t += dt

# Compute solution
solve(a == L, u)
vtkfile << (u, t)

# Update previous solution
u_prev.assign(u)
f_prev.update(t, u_prev)


See for instance: Update Expression for DirichletBC

1 Like

I´m aware of how to update a parameter, which I initialized via __init__, inside the main time stepping for-loop.
But I would like to update f_prev to the current value[0] of the UserExpression. In my revised MWCE I solved this by

# Update previous solution
u_prev.assign(u)
f_prev = MySourceConcentration(t, u_prev)


Now, I´m unsure, if Fenics will use this f_prev for the next time step or the f_prev, I initially created for t=0 (line 116)?

Thanks,

Cou

Revised MWCE
from fenics import *
import numpy as np

# Define source term f
class MySourceConcentration(UserExpression):
def __init__(self, t, u, **kwargs):
super().__init__(**kwargs)
self.t = t
self.u = u

def eval(self, values, x):

# Calculate the source term f for t>0
if self.t >0:
if x[0]>0 and x[0]<=0.5:
# Calculate average of u for the domain x>=0 and x<=0.5
u_avg = assemble(self.u*dx(1)) / assemble(Constant(1.0)*dx(1))

# Calculate the surface area of a particle (equation is made up for demonstrationen purposes without physical correctness)
area = area_t0 - dt * (1 - u_avg)

# Calculate source term f
values[0] = Constant1 * area * (Constant2 - u_avg)
self.msc = values[0]

# for the rest of the domain: source term f=0
else:
values[0] = 0

# Set the source term f for t=0
else:
if x[0]>0 and x[0]<=0.5:
values[0] = f_0
else:
values[0] = 0

def value_shape(self):
return ()

# Define Constants
Constant1 = 1
Constant2 = 10
D = Constant(0.6)  # diffusion coefficient
f_0 = 4            # Source term value for t=0
beta = 1.2
area_t0 = 5

T = 2.0            # final time
num_steps = 10     # number of time steps
dt = T / num_steps # time step size

# Create mesh and define function space
nx = ny = 16
mesh = UnitSquareMesh(nx, ny)
space_dim = mesh.geometry().dim()
V = FunctionSpace(mesh, 'CG', 1)

# Create classes for defining parts of the boundaries and the interior
# of the domain
class Left(SubDomain):
def inside(self, x, on_boundary):
return near(x[0], 0.0)

class Right(SubDomain):
def inside(self, x, on_boundary):
return near(x[0], 1.0)

class Bottom(SubDomain):
def inside(self, x, on_boundary):
return near(x[1], 0.0)

class Top(SubDomain):
def inside(self, x, on_boundary):
return near(x[1], 1.0)

class Obstacle_left(SubDomain):
def inside(self, x, on_boundary):
return (between(x[0], (0.0, 0.5)))

class Obstacle_right(SubDomain):
def inside(self, x, on_boundary):
return (between(x[0], (0.5, 1.0)))

# Initialize sub-domain instances
left = Left()
top = Top()
right = Right()
bottom = Bottom()
obstacle_left = Obstacle_left()
obstacle_right = Obstacle_right()

# Initialize mesh function for interior domains
domains = MeshFunction("size_t", mesh, space_dim-1)
obstacle_left.mark(domains, 1)
obstacle_right.mark(domains, 3)

# Initialize mesh function for boundary domains
boundaries = MeshFunction("size_t", mesh, space_dim-1)
boundaries.set_all(0)
left.mark(boundaries, 1)
top.mark(boundaries, 2)
right.mark(boundaries, 3)
bottom.mark(boundaries, 4)

# Define new measures associated with the interior domains and
# exterior boundaries
dx = Measure('dx', domain=mesh, subdomain_data=domains)
ds = Measure('ds', domain=mesh, subdomain_data=boundaries)

vtkfile = File("20201028_UE_update_umsetzen.pvd")

################### Initial values (t=0) ######################################
t=0
u_0 = Constant(1)
u_prev = interpolate(u_0, V)
f_prev = MySourceConcentration(t, u_prev, degree=1)

############ Robin boundary condition j_m * n_m = h*(u-c_oo) ##################
h = 1.0
c_oo = 1.0

#################### Define variational problem ###############################
u = TrialFunction(V)
v = TestFunction(V)
f = MySourceConcentration(t, u_prev, degree=1)

a, L = lhs(F), rhs(F)

# Time-stepping
u = Function(V)

for n in range(num_steps):

# Update current time
t += dt

# Compute solution
solve(a == L, u)
vtkfile << (u, t)

# Update previous solution
u_prev.assign(u)
f_prev = MySourceConcentration(t, u_prev)


This is a Python related question. If you assign a new variable to the Python object f_prev, by equality:

You need to redefine objects using f_prev.

If you use

f_prev.t=t
f_prev.u=u_prev


You do not Need to define the variational forms

1 Like