Heat equation used for a Diffusion problem based on Fick's Second Law
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.

81 lines
1.2 KiB

3 years ago
  1. ```python
  2. from fenics import *
  3. from dolfin import *
  4. import mshr
  5. import matplotlib.pyplot as plt
  6. import numpy as np
  7. ```
  8. ```python
  9. T = 60*60*5 #final step
  10. num_steps = 50
  11. dt = T/num_steps #step size
  12. ```
  13. ```python
  14. #2 Create mesh and define function space
  15. domain = mshr.Circle(Point(0.,0.),1.0,60)
  16. mesh = mshr.generate_mesh(domain, 25)
  17. V = FunctionSpace(mesh, 'Lagrange', 1) #Lagrange are triangular elements
  18. plot(mesh)
  19. plt.show()
  20. ```
  21. ![png](output_2_0.png)
  22. ```python
  23. #3 Defining boundary conditions (Dirichlet)
  24. D = 1.4E-7 #cm^2/s
  25. u_D = Constant(0.1)
  26. def Dirichlet_boundary(x, on_boundary):
  27. return on_boundary
  28. bc = DirichletBC(V, Constant(1), Dirichlet_boundary)
  29. ```
  30. ```python
  31. #Defining initial values and variational problem
  32. u_n = interpolate(u_D,V)
  33. u = TrialFunction(V)
  34. v = TestFunction(V)
  35. f = Constant(0)
  36. F = u*v*dx+D*dt*dot(grad(u), grad(v))*dx-(u_n+dt*f)*v*dx
  37. a, L = lhs(F), rhs(F)
  38. ```
  39. ```python
  40. #Resolution on time steps
  41. u = Function(V)
  42. t = 0
  43. vtkfile = File('sol/solution.pvd')
  44. for n in range(num_steps):
  45. #update current time
  46. t += dt
  47. u_D.t = t
  48. #compute solution
  49. solve(a==L, u, bc)
  50. #vtkfile << (u,t)
  51. plot(u)
  52. plt.show()
  53. #update previous solution
  54. u_n.assign(u)
  55. ```
  56. ![png](output_5_0.png)
  57. ![png](output_5_48.png)