Updating boundary condition in each time step

I have a time-splitting code for solving navier-stokes problem. In the method, the boundary condition for intermediate solution is updated by an equation which uses the solution of the previous equation.
for example:
loop over time{
solve: Ax = b
then for each element on the boundary : u_intermediate = 2*x }
how can I implement it in Fenics?

I would strongly suggest you provide an MWE, and not pseudo code, as it is not clear what version of FEniCS you want to use (legacy FEniCS or DOLFINx).
Secondly, yes, this can be implemented in either framework by creating a function that will be assigned the u_intermediate).

I guess it’s Fenics. here is the code

u = TrialFunction(V)
u_star = TrialFunction(V_star)
v = TestFunction(V)
p = TrialFunction(Q)
q = TestFunction(Q)

# Define functions for solutions at previous and current time steps
u_n = Function(V)
u_nn = Function(V)
u_nn = project(u_0, V)
u_n = project(u_0, V)
u_  = Function(V)
p_n = Function(Q)
p_nn  = Function(Q)

Consider the functions above. In each time step, I would like to assign u_ = p_n*delta_t - u_n for the nodes on the boundary. I would appreciate your help

Please note that you have not provided a minimal reproducible example, as you have skipped tons of definitions.

Consider the following MWE, as I can see that u_n and p_n stems from different function spaces:

from dolfin import *

mesh = UnitSquareMesh(10, 10)

V = FunctionSpace(mesh, "CG", 1)
Q = FunctionSpace(mesh, "DG", 0)

u = Function(V)
p = Function(Q)


bc_func = Function(V)
bc = DirichletBC(V, bc_func, "on_boundary")


test_function = Function(V)
bc.apply(test_function.vector())
print(test_function.vector().get_local())


u.interpolate(Expression("x[0]",degree=1))
p.interpolate(Expression("x[1]",degree=1))


u_tmp = project(u+p, V)
bc_func.assign(u_tmp)


test_function2 = Function(V)
# Same bc object, just bc_func has been updated
bc.apply(test_function2.vector())
print(test_function2.vector().get_local())

Thank but it doesn’t answer my question.
I will rephrase my question and send it along a MWE in another topic.
Thank you very much for you help.

Could you please elaborate on why this does not resolve your question?

It shows how a Dirichlet bc is updated with functions stemming from another simulation.

I’m not sure I stated my question properly. (u_) which is the intermediate velocity, is going to be solved for in a linear system. The boundary conditions for this linear system is given by u_ = p_n*delta_t - u_n.

First step is to set a boundary condition which evolves in time and is dependent on p_n and u_n( which are solved from a separate equation and is not our concern).
Second step is to solve that equation with the specified boundary condition.

The second step is pretty straightforward as I’m guessing the code would be:
b1 = assemble(L1)
[bc.apply(b1) for bc in bcu]
solve(A1, u_.vector(), b1)

It’s the first step that’s giving me trouble. You didn’t mention how I could assign u_ on boundary from p_n and u_n.

That is exactly what in showing in:

Here i create a boundary condition for the V space that you can use for your equation. I show that you can assign a combination of u and p (i used a sum but you can change this to whatever you want).

I am not calling a solve, as you have not provided a system of equation or a minimal example.

However, you can use the boundary condition bc in your variational formulation.

I see. I was confused by the last two parts of your code. By the way, is this part necessary:

test_function = Function(V)
bc.apply(test_function.vector())
print(test_function.vector().get_local())

And to make sure that I understood your code, my code would be

bc_func = Function(V)
bc = DirichletBC(V, bc_func, "on_boundary")
for n in range(time_steps)
            u_tmp = project(p_n*delta_t - u_n,V)
            bc_func.assign(u_tmp)
            b1 = assemble(L1)
            [bc.apply(b1)]
            solve(A1, u_.vector(), b1)
           # the rest of the code is clear

Am I right?

This was to highlight that the bc object changed after assigning to bc_func.

Yes that would be the idea. I would change the project call to a reusable projection (there are various posts on the forum about this).

Thank a lot for you help :pray: