Hi,
here u=(u1, u2)
u1= 1+ x^2+y^2+2*t
u2= 1+xy+t^2
f=(f1, f2)
f1=-2 +2*u1*x+2*u2*y +2*cost(2y-1)
f2=2*t +y*u1+x*u2+2*cost(2*x-1)
p=(2x-1)(2y-1)cost
from __future__ import print_function
import matplotlib.pyplot as plt
from fenics import *
def NS(N):
T=1.0
num_steps=pow(N,2)
dt=T/num_steps
#Creat Mesh and function spaces
mesh=UnitSquareMesh(4, 4)
V= VectorFunctionSpace(mesh, 'P', 2)
Q= FunctionSpace(mesh,'P', 1)
# Define boundary conditions
u_D = Expression(('1 + x[0]*x[0] + x[1]*x[1] + 2*t','1 + x[0]*x[1] + pow(t,2)'),
degree=2, t=0)
bc = DirichletBC(V, u_D , "on_boundary")
bcu=[bc]
# Define trial and test functions
u = TrialFunction(V)
v = TestFunction(V)
p = TrialFunction(Q)
q = TestFunction(Q)
# Define functions for solutions at previous and current time stepsfrom _future_import print function
u_n = interpolate(u_D, V)
#u_n = Function(V)
u_ = Function(V)
p = Function(Q)
q = Function(Q)
# Define expressions used in variational forms
f = Expression(('-2+2*(1 + x[0]*x[0] + x[1]*x[1] + 2*t)*x[0]+2*(1 + x[0]*x[1] + pow(t,2)*x[1]) +2*cos(t)*(2*x[1]-1)',\
'2*t +x[1]*(1 + x[0]*x[1] + pow(t,2)) +x[0]*(1 + x[0]*x[1] + pow(t,2))+2*cos(t)*(2*x[0]-1)'), t=0, degree=2)
k = Constant(dt)
# Define variational problem
F = inner((u - u_n) / k, v)*dx - inner(f, v)*dx + inner(grad(u),grad(v))*dx + inner(grad(p),v)*dx \
+inner(grad(u_n)*u, v)*dx\
+ inner(grad(q), u)*dx
a = lhs(F)
L = rhs(F)
# Time-stepping
t = 0
for n in range(num_steps):
# Update current time
t += dt
u_D.t=t
f.t=t
A=assemble(a)
[bc.apply(A) for bc in bcu]
b= assemble(L)
[bc.apply(b) for bc in bcu]
solve(A, u_.vector(), b)
u_n.assign(u_)
error_L2 = errornorm(u_D, u_, 'L2')
return error_L2
for N in [2, 4, 8, 16, 32 ]:
print("{0:d} error_L2 ={1:.2e}".format(N, NS(N)))
The Output is
2 error_L2 =5.11e-02
4 error_L2 =5.40e-02
8 error_L2 =5.48e-02
16 error_L2 =5.51e-02
32 error_L2 =5.51e-02
The error should decrease at final time t=1, but here it is not decreasing?
Thank You,
Debendra