The Poisson's equation problem solved using Fenics
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.

101 lines
2.8 KiB

  1. # Introduction
  2. The Poisson's equation is a second-order partial differential equation that stats the negative Laplacian $-\Delta u$ of an unknown field $u=u(x)$ is equal to a given function $f=f(x)$ on a domain $\Omega \subset \mathbb{R}^d$, most probably defined by a set of boundary conditions for the solution $u$ on the boundary $\partial \Omega$ of $\Omega$:
  3. $$-\Delta u =f \quad \text{in } \Omega\text{,}$$
  4. $$u=u_0 \quad \text{on } \Gamma_D \subset \partial\Omega \text{,}$$
  5. here the Dirichlet's boundary condition $u=u_0$ signifies a prescribed values for the unknown $u$ on the boundary.
  6. The Poisson's equation is the simplest model for gravity, electromagnetism, heat transfer, among others.
  7. The specific case of $f=0$ and a negative $k$ value, leaves to the Fourier's Law.
  8. ## Comparative analysis
  9. Along this example, the fenics platfomr is used to compare results obtained by solving the heat equation (Laplace equation) in 2-D:
  10. $$\frac{\partial^2 T}{\partial x^2}+ \frac{\partial^2 T}{\partial y^2}=0$$
  11. the problem is defined by the next geometry considerations:
  12. ![](physicalproblem.png)
  13. The resulting contour of temperature, solving using finite diferences, is shown next:
  14. ![](resulteq.png)
  15. # Solving by Finite Element Method with Varational Problem formulation
  16. ```python
  17. #1 Loading functions and modules
  18. from fenics import *
  19. import matplotlib.pyplot as plt
  20. ```
  21. ```python
  22. #2 Create mesh and define function space
  23. mesh = RectangleMesh(Point(0,0),Point(20,20),10, 10,'left')
  24. V = FunctionSpace(mesh, 'Lagrange', 1) #Lagrange are triangular elements
  25. plot(mesh)
  26. plt.show()
  27. ```
  28. ![png](output_6_0.png)
  29. ```python
  30. #3 Defining boundary conditions (Dirichlet)
  31. tol = 1E-14 # tolerance for coordinate comparisons
  32. #at y=20
  33. def Dirichlet_boundary1(x, on_boundary):
  34. return on_boundary and abs(x[1] - 20) < tol
  35. #at y=0
  36. def Dirichlet_boundary0(x, on_boundary):
  37. return on_boundary and abs(x[1] - 0) < tol
  38. #at x=0
  39. def Dirichlet_boundarx0(x, on_boundary):
  40. return on_boundary and abs(x[0] - 0) < tol
  41. #at x=20
  42. def Dirichlet_boundarx1(x, on_boundary):
  43. return on_boundary and abs(x[0] - 20) < tol
  44. bc0 = DirichletBC(V, Constant(0), Dirichlet_boundary0)
  45. bc1 = DirichletBC(V, Constant(100), Dirichlet_boundary1) #100C
  46. bc2 = DirichletBC(V, Constant(0), Dirichlet_boundarx0)
  47. bc3 = DirichletBC(V, Constant(0), Dirichlet_boundarx1)
  48. bcs = [bc0,bc1, bc2,bc3]
  49. ```
  50. ```python
  51. #4 Defining variational problem and its solution
  52. k =1
  53. u = TrialFunction(V)
  54. v = TestFunction(V)
  55. f = Constant(0)
  56. a = dot(k*grad(u), grad(v))*dx
  57. L = f*v*dx
  58. # Compute solution
  59. u = Function(V)
  60. solve(a == L, u, bcs)
  61. # Plot solution and mesh
  62. plot(u)
  63. plot(mesh)
  64. # Save solution to file in VTK format
  65. vtkfile = File('solution.pvd')
  66. vtkfile << u
  67. ```
  68. ![png](output_8_0.png)
  69. # Results after editing color-map on paraview
  70. ![](paraview-results.png)