from dolfin import * import numpy as np from math import * import os, sys import sympy as sp from sympy import exp, sin, pi, cos T = 1 # final time num_steps = 100 # number of time steps dt = T / num_steps # time step size mu=0.1 # define symbol expressions of u,p,f using sympyw23 x,y,t = sp.symbols("x[0],x[1],t") u1=pi*sin(t)*sin(2*pi*y)*sin(pi*x)*sin(pi*x) u2=-pi*sin(t)*sin(2*pi*x)*sin(pi*y)*sin(pi*y) ps=sin(t)*sin(pi*x)*sin(pi*y) f1= -mu*sp.diff(u1,x,2) - mu*sp.diff(u1,y,2) + sp.diff(ps,x,1)+\ sp.diff(u1,t,1) + u1*sp.diff(u1,x,1)+u2*sp.diff(u1,y,1) f2= -mu*sp.diff(u2,x,2) - mu*sp.diff(u2,y,2) + sp.diff(ps,y,1)+\ sp.diff(u2,t,1) + u1*sp.diff(u2,x,1)+u2*sp.diff(u2,y,1) # transform the expressions to ccode style u1 = sp.printing.ccode(u1) u2 = sp.printing.ccode(u2) ps = sp.printing.ccode(ps) f1 = sp.printing.ccode(f1) f2 = sp.printing.ccode(f2) # Create mesh and define function spaces mesh = RectangleMesh(Point(-1, -1), Point(1, 1), 16, 16) P1 = FiniteElement("Lagrange", mesh.ufl_cell(), 2) Q = FunctionSpace(mesh, P1) #V= FunctionSpace(mesh, "RT", 1) V = VectorFunctionSpace(mesh, 'P', 2) u_e = Expression((u1,u2), degree=5,t=0) # Define boundaries def Dirichlt_boundary(x,on_boundary): return x[0] < -1+DOLFIN_EPS or x[0] > 1-DOLFIN_EPS or x[1] > 1.0 - DOLFIN_EPS or x[1] < -1+DOLFIN_EPS bcu_noslip = DirichletBC(V, u_e, Dirichlt_boundary) p_e=Expression(ps, degree=5,t=0) t = 0 for n in range(num_steps): # Update current time t += dt # Compute error u_e.t = t p_e.t = t u_d = interpolate(u_e, V) p_d = interpolate(p_e, Q) errorU=errornorm(u_e, u_d, "L2") errorP=errornorm(p_e, p_d, "L2") print('t = %.2f: errorU = %.3g' % (t, errorU)) print('t = %.2f: errorP = %.3g' % (t, errorP))
I creat an exact solution, and every time I interpolate the sol to the finite element space and get the error between them, it is so bad the error perform.
so can anyone explain this thing?