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.

183 lines
2.3 KiB

  1. ```python
  2. #1 Loading functions and modules
  3. from fenics import *
  4. import matplotlib.pyplot as plt
  5. T = 0.1
  6. num_steps = 20
  7. dt = T/num_steps
  8. rho = 7500
  9. Cp = 500
  10. k = 50
  11. alpha = k/(rho*Cp)
  12. ```
  13. ```python
  14. #2 Create mesh and define function space
  15. nx = 0.008
  16. ny = 0.003
  17. mesh = RectangleMesh(Point(0,0),Point(nx,ny),30, 30,'left')
  18. V = FunctionSpace(mesh, 'Lagrange', 1) #Lagrange are triangular elements
  19. plot(mesh)
  20. plt.show()
  21. ```
  22. ![png](output_1_0.png)
  23. ```python
  24. # Boundary conditions
  25. u0 = Constant(100)
  26. def boundary(x, on_boundary):
  27. return on_boundary
  28. bc = DirichletBC(V,u0, boundary)
  29. ```
  30. ```python
  31. u_n = project(1, V)
  32. u = TrialFunction(V)
  33. v = TestFunction(V)
  34. f = Constant(0.0)
  35. F = u*v*dx + alpha*dt*dot(grad(u), grad(v))*dx-u_n*v*dx
  36. a, L = lhs(F), rhs(F)
  37. ```
  38. ```python
  39. vtkfile = File('solution/solution.pvd')
  40. u = Function(V)
  41. t = 0
  42. for n in range(num_steps):
  43. t += dt
  44. #u0.t = t
  45. solve(a == L, u, bc)
  46. #c = plot(u,)
  47. #plt.colorbar(c)
  48. #plt.show()
  49. ####
  50. vtkfile << (u, t)
  51. u_n.assign(u)
  52. ```
  53. ```python
  54. 1E-13
  55. ```
  56. 1e-13
  57. ```python
  58. 1e2
  59. ```
  60. 100.0
  61. ```python
  62. 1E-13+1e2
  63. ```
  64. 100.0000000000001
  65. # Boundary conditions
  66. ```python
  67. #1 Loading functions and modules
  68. from fenics import *
  69. import matplotlib.pyplot as plt
  70. T = 4
  71. num_steps = 200
  72. dt = T/num_steps
  73. rho = 7500
  74. Cp = 500
  75. k = 50
  76. alpha = k/(rho*Cp)
  77. ```
  78. ```python
  79. #2 Create mesh and define function space
  80. nx = 0.008
  81. ny = 0.003
  82. mesh = RectangleMesh(Point(0,0),Point(nx,ny),8, 8,'left')
  83. V = FunctionSpace(mesh, 'Lagrange', 1) #Lagrange are triangular elements
  84. plot(mesh)
  85. plt.show()
  86. ```
  87. ![png](output_10_0.png)
  88. ```python
  89. #Boundary Conditions
  90. tol = 1E-14
  91. def BC1(x, on_boundary):
  92. return on_boundary and abs(x[0]-nx) < tol
  93. def BC2(x, on_boundary):
  94. return on_boundary and abs(x[0]-0) < tol
  95. bc1=DirichletBC(V,Constant(25),BC1)
  96. bc2=DirichletBC(V,Constant(800),BC2)
  97. bc=(bc1,bc2)
  98. ```
  99. ```python
  100. u_n = project(25, V)
  101. u = TrialFunction(V)
  102. v = TestFunction(V)
  103. f = Constant(0.0)
  104. F = u*v*dx + alpha*dt*dot(grad(u), grad(v))*dx-u_n*v*dx
  105. a, L = lhs(F), rhs(F)
  106. ```
  107. ```python
  108. vtkfile = File('solution/solution2.pvd')
  109. u = Function(V)
  110. t = 0
  111. for n in range(num_steps):
  112. t += dt
  113. #u0.t = t
  114. solve(a == L, u, bc)
  115. vtkfile << (u, t)
  116. #c = plot(u,)
  117. #plt.colorbar(c)
  118. #plt.show()
  119. u_n.assign(u)
  120. ```
  121. ```python
  122. ```