You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
3.1 KiB

3 years ago
  1. """
  2. FEniCS tutorial demo program: Incompressible Navier-Stokes equations
  3. for channel flow (Poisseuille) on the unit square using the
  4. Incremental Pressure Correction Scheme (IPCS).
  5. u' + u . nabla(u)) - div(sigma(u, p)) = f
  6. div(u) = 0
  7. """
  8. from __future__ import print_function
  9. from fenics import *
  10. import numpy as np
  11. T = 10.0 # final time
  12. num_steps = 500 # number of time steps
  13. dt = T / num_steps # time step size
  14. mu = 1 # kinematic viscosity
  15. rho = 1 # density
  16. # Create mesh and define function spaces
  17. mesh = UnitSquareMesh(16, 16)
  18. V = VectorFunctionSpace(mesh, 'P', 2)
  19. Q = FunctionSpace(mesh, 'P', 1)
  20. # Define boundaries
  21. inflow = 'near(x[0], 0)'
  22. outflow = 'near(x[0], 1)'
  23. walls = 'near(x[1], 0) || near(x[1], 1)'
  24. # Define boundary conditions
  25. bcu_noslip = DirichletBC(V, Constant((0, 0)), walls)
  26. bcp_inflow = DirichletBC(Q, Constant(8), inflow)
  27. bcp_outflow = DirichletBC(Q, Constant(0), outflow)
  28. bcu = [bcu_noslip]
  29. bcp = [bcp_inflow, bcp_outflow]
  30. # Define trial and test functions
  31. u = TrialFunction(V)
  32. v = TestFunction(V)
  33. p = TrialFunction(Q)
  34. q = TestFunction(Q)
  35. # Define functions for solutions at previous and current time steps
  36. u_n = Function(V)
  37. u_ = Function(V)
  38. p_n = Function(Q)
  39. p_ = Function(Q)
  40. # Define expressions used in variational forms
  41. U = 0.5*(u_n + u)
  42. n = FacetNormal(mesh)
  43. f = Constant((0, 0))
  44. k = Constant(dt)
  45. mu = Constant(mu)
  46. rho = Constant(rho)
  47. # Define strain-rate tensor
  48. def epsilon(u):
  49. return sym(nabla_grad(u))
  50. # Define stress tensor
  51. def sigma(u, p):
  52. return 2*mu*epsilon(u) - p*Identity(len(u))
  53. # Define variational problem for step 1
  54. F1 = rho*dot((u - u_n) / k, v)*dx + \
  55. rho*dot(dot(u_n, nabla_grad(u_n)), v)*dx \
  56. + inner(sigma(U, p_n), epsilon(v))*dx \
  57. + dot(p_n*n, v)*ds - dot(mu*nabla_grad(U)*n, v)*ds \
  58. - dot(f, v)*dx
  59. a1 = lhs(F1)
  60. L1 = rhs(F1)
  61. # Define variational problem for step 2
  62. a2 = dot(nabla_grad(p), nabla_grad(q))*dx
  63. L2 = dot(nabla_grad(p_n), nabla_grad(q))*dx - (1/k)*div(u_)*q*dx
  64. # Define variational problem for step 3
  65. a3 = dot(u, v)*dx
  66. L3 = dot(u_, v)*dx - k*dot(nabla_grad(p_ - p_n), v)*dx
  67. # Assemble matrices
  68. A1 = assemble(a1)
  69. A2 = assemble(a2)
  70. A3 = assemble(a3)
  71. # Apply boundary conditions to matrices
  72. [bc.apply(A1) for bc in bcu]
  73. [bc.apply(A2) for bc in bcp]
  74. # Time-stepping
  75. t = 0
  76. for n in range(num_steps):
  77. # Update current time
  78. t += dt
  79. # Step 1: Tentative velocity step
  80. b1 = assemble(L1)
  81. [bc.apply(b1) for bc in bcu]
  82. solve(A1, u_.vector(), b1)
  83. # Step 2: Pressure correction step
  84. b2 = assemble(L2)
  85. [bc.apply(b2) for bc in bcp]
  86. solve(A2, p_.vector(), b2)
  87. # Step 3: Velocity correction step
  88. b3 = assemble(L3)
  89. solve(A3, u_.vector(), b3)
  90. # Plot solution
  91. plot(u_)
  92. # Compute error
  93. u_e = Expression(('4*x[1]*(1.0 - x[1])', '0'), degree=2)
  94. u_e = interpolate(u_e, V)
  95. error = np.abs(u_e.vector().array() - u_.vector().array()).max()
  96. print('t = %.2f: error = %.3g' % (t, error))
  97. print('max u:', u_.vector().array().max())
  98. # Update previous solution
  99. u_n.assign(u_)
  100. p_n.assign(p_)
  101. # Hold plot
  102. interactive()