poisson_membrane.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. """
  2. FEniCS tutorial demo program: Deflection of a membrane.
  3. -Laplace(w) = p in the unit circle
  4. w = 0 on the boundary
  5. The load p is a Gaussian function centered at (0, 0.6).
  6. """
  7. from fenics import *
  8. from mshr import Circle, generate_mesh
  9. # Create mesh and define function space
  10. domain = Circle(Point(0, 0), 1)
  11. mesh = generate_mesh(domain, 64)
  12. V = FunctionSpace(mesh, 'P', 2)
  13. w_D = Constant(0)
  14. def boundary(x, on_boundary):
  15. return on_boundary
  16. bc = DirichletBC(V, w_D, boundary)
  17. # Define load
  18. p = Expression('4*exp(-pow(beta, 2)*(pow(x[0], 2) + pow(x[1] - R0, 2)))',
  19. degree=1, beta=8, R0=0.6)
  20. # Define variational problem
  21. w = TrialFunction(V)
  22. v = TestFunction(V)
  23. a = dot(grad(w), grad(v))*dx
  24. L = p*v*dx
  25. # Compute solution
  26. w = Function(V)
  27. solve(a == L, w, bc)
  28. p = interpolate(p, V)
  29. # Curve plot along x = 0 comparing p and w
  30. import numpy as np
  31. tol = 0.001 # avoid hitting points outside the domain
  32. y = np.linspace(-1 + tol, 1 - tol, 101)
  33. points = [(0, y_) for y_ in y] # 2D points
  34. w_line = np.array([w(point) for point in points])
  35. p_line = np.array([p(point) for point in points])
  36. #######################################################################
  37. from vedo.dolfin import plot
  38. from vedo import Line, Latex
  39. pde = r'-T \nabla^{2} D=p, ~\Omega=\left\{(x, y) | x^{2}+y^{2} \leq R\right\}'
  40. tex = Latex(pde, pos=(0,1.1,.1), s=0.2, c='w')
  41. wline = Line(np.c_[y, w_line*10], c='white', lw=4)
  42. pline = Line(np.c_[y, p_line/ 4], c='lightgreen', lw=4)
  43. plot(w, wline, tex, bg='bb', text='Deflection')
  44. plot(p, pline, bg='bb', text='Load')