I experience the same problem with the updated version you just send.
Here is my code:
######---------------------------------------------######
#Trying to fix wrong velocity for Navier-Stokes#
#Mesh is box#
######---------------------------------------------######
#region: Load libraries----------------------------
from dolfin import *
import matplotlib.pyplot as plt
from ufl import nabla_div, max_value
from progress.bar import Bar
import numpy as np
from tqdm import tqdm
import sys as system
#endregion----------------------------------------
#region: Choose script conditions------------------
#get rid of solving messages
set_log_active(False)
#set mesh
generate_mesh=True
#get scripts name
name_of_self = system.argv[0].split('/')[-1].split('.')[0]
#for storing solutions
directory_xdmf='results/'+name_of_self+'/'
xdmffile_velocity = XDMFFile(directory_xdmf+'velocity.xdmf')
xdmffile_pressure = XDMFFile(directory_xdmf+'pressure.xdmf')
xdmffile_saturation = XDMFFile(directory_xdmf+'saturation.xdmf')
#endregion-----------------------------------------
#region: Set parameters----------------------------
#set time
t=0.0; T = 10; dt = 0.0256
#parameters
k = Constant(dt)
force = Constant((0., 0.))
nu = Constant(0.01)
#endregion ----------------------------------------
#region: define mesh ------------------------------
# Create mesh
mesh = UnitSquareMesh(64, 64)
n = FacetNormal(mesh)
#endregion ----------------------------------------
#region: define boundaries
class Top(SubDomain):
def inside(self, x, on_boundary):
return (near(x[1], 1.0))
class Bottom(SubDomain):
def inside(self, x, on_boundary):
return (near(x[1], 0.0))
class leftWall(SubDomain):
def inside(self, x, on_boundary):
return (near(x[0], 0.0))
class rightWall(SubDomain):
def inside(self, x, on_boundary):
return (near(x[0], 1.0))
top=Top()
bottom = Bottom()
leftwall=leftWall()
rightwall=rightWall()
boundaries = MeshFunction("size_t", mesh,mesh.topology().dim()-1,0)
top.mark(boundaries, 1)
bottom.mark(boundaries,2)
leftwall.mark(boundaries, 3)
rightwall.mark(boundaries, 3)
ds = Measure('ds', domain=mesh, subdomain_data=boundaries)
#endregion
#region: define functions -------------------------
#define vector-vaued functionspace and scalar-valued function space
V = VectorFunctionSpace(mesh, "CG", 2) #for velocity
Q = FunctionSpace(mesh, "CG", 1) #for pressure
#define test and trial functions
v = TestFunction(V) #for velocity
u = TrialFunction(V) #for velocity
q = TestFunction(Q) #for pressure
p = TrialFunction(Q) #for pressure
# Create functions
u0 = Function(V) #for tentative velocity
u1 = Function(V) #for storing velocity solution
p1 = Function(Q) #for pressure
#endregion ----------------------------------------
#region: define boundary conditions ---------------
pd=Constant(1)
#pd=Expression('10/(1+exp(-8*t+5))',t=0.0,degree=1)
noslip = DirichletBC(V, (0, 0), boundaries,3)
inflow = DirichletBC(Q, pd, boundaries,1)
outflow = DirichletBC(Q, 0, boundaries,2)
bcu = [noslip]
bcp = [inflow, outflow]
#endregion ----------------------------------------
#region: define weak formulation --
# Tentative velocity step
F1 = (1/k)*inner(u - u0, v)*dx \
+ inner(grad(u0)*u, v)*dx \
+ nu*inner(grad(u), grad(v))*dx - inner(force, v)*dx
a1 = lhs(F1)
L1 = rhs(F1)
# Pressure update
a2 = inner(grad(p), grad(q))*dx
L2 = -(1/k)*inner(div(u1),q)*dx
# Velocity update
a3 = inner(u, v)*dx
L3 = inner(u1, v)*dx - k*inner(grad(p1), v)*dx
# Assemble matrices
A1 = assemble(a1); b1 = None
A2 = assemble(a2); b2 = None
A3 = assemble(a3); b3 = None
#endregion ----------------------------------------
#region: solve problem ----------------------------
#time step and update
bar = Bar('Processing', max=T/dt)
while t < T + DOLFIN_EPS:
pd.t = t+dt
dt = min(T-t, dt)
#update
bar.next()
# Compute tentative velocity step
b1 = assemble(L1, tensor=b1)
[bc.apply(A1, b1) for bc in bcu]
solve(A1, u1.vector(), b1, "gmres")
# Pressure correction
b2 = assemble(L2, tensor=b2)
[bc.apply(A2, b2) for bc in bcp]
solve(A2, p1.vector(), b2, "gmres")
# Velocity correction
b3 = assemble(L3, tensor=b3)
[bc.apply(A3, b3) for bc in bcu]
solve(A3, u1.vector(), b3, "gmres")
#save solutions
xdmffile_velocity.write(u1, t); xdmffile_pressure.write(p1, t)
t += dt
# Update previous solution
u0.assign(u1)
bar.finish()
#endregion ----------------------------------------