elasticbeam.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. """A beam deforming under its own weight."""
  2. from dolfin import *
  3. # Scaled variables
  4. l, w = 1, 0.1
  5. mu_, lambda_ = 1, 1
  6. rho = 10
  7. gamma = (w/l)**2
  8. wind = (0, 0.0, 0)
  9. # Create mesh and define function space
  10. mesh = BoxMesh(Point(0, 0, 0), Point(l, w, w), 50, 5, 5)
  11. V = VectorFunctionSpace(mesh, "P", 1)
  12. # Define boundary condition
  13. def clamped_boundary(x, on_boundary):
  14. return on_boundary and (near(x[0], 0) or near(x[0], l))
  15. bc = DirichletBC(V, Constant((0, 0, 0)), clamped_boundary)
  16. # Define strain and stress
  17. def epsilon(u):
  18. return 0.5 * (nabla_grad(u) + nabla_grad(u).T)
  19. def sigma(u):
  20. return lambda_ * nabla_grad(u) * Identity(3) + 2 * mu_ * epsilon(u)
  21. # Define variational problem
  22. u = TrialFunction(V)
  23. v = TestFunction(V)
  24. f = Constant((0, 0, -rho * gamma))
  25. T = Constant(wind)
  26. a = inner(sigma(u), epsilon(v)) * dx
  27. L = dot(f, v) * dx + dot(T, v) * ds
  28. # Compute solution
  29. u = Function(V)
  30. solve(a == L, u, bc)
  31. s = sigma(u) - (1.0 / 3) * tr(sigma(u)) * Identity(3) # deviatoric stress
  32. von_Mises = sqrt(3.0 / 2 * inner(s, s))
  33. V = FunctionSpace(mesh, "P", 1)
  34. von_Mises = project(von_Mises, V)
  35. u_magnitude = sqrt(dot(u, u))
  36. u_magnitude = project(u_magnitude, V)
  37. ################################ Plot solution
  38. from vedo import Text3D
  39. from vedo.dolfin import plot
  40. plot(
  41. u, mode="displaced mesh",
  42. text=__doc__,
  43. scalarbar=False,
  44. axes=1,
  45. viewup='z',
  46. )
  47. #export_window('elasticbeam1.x3d') # generate a html test page
  48. txt = Text3D("Von Mises stress intensity", pos=(0.1,.12,0), s=0.03, c='white')
  49. plot(von_Mises, txt, cmap='plasma', scalarbar=False, new=True)
  50. #export_window('elasticbeam2.x3d') # generate a html test page
  51. txt = Text3D("Magnitude of displacement", pos=(0.1,.12,0), s=0.03, c='white')
  52. plot(u_magnitude, txt, scalarbar=False, new=True)
  53. #export_window('elasticbeam3.x3d') # generate a html test page