elasticity4.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import numpy as np
  2. from dolfin import *
  3. import vedo
  4. set_log_level(30)
  5. class AllBoundaries(SubDomain):
  6. def inside(self, x, on_boundary):
  7. return on_boundary and x[0]<-10
  8. def solve_problem(mesh, mfunc, force):
  9. V = VectorFunctionSpace(mesh, "CG", 1)
  10. u = TrialFunction(V)
  11. v = TestFunction(V)
  12. displacement = Function(V)
  13. bc = [DirichletBC(V, Constant((0,0)), mfunc, 1)]
  14. F = Constant(force)
  15. E = Constant(5000)
  16. nu = Constant(0.3)
  17. mu = E / (2.0*(1+nu))
  18. lmbda = E * nu / ((1.0+nu) * (1-2*nu))
  19. sigma = 2.0 * mu * sym(grad(u)) + lmbda * tr( sym(grad(u)) ) * Identity(2)
  20. solve(inner(sigma, grad(v)) * dx == inner(F, v) * dx, displacement, bc)
  21. displacement.set_allow_extrapolation(True)
  22. return displacement
  23. def update(mesh, displacement):
  24. new_mesh = Mesh(mesh)
  25. ALE.move(new_mesh, displacement)
  26. return new_mesh
  27. def remesh(mesh):
  28. if isinstance(mesh, vedo.Mesh):
  29. vmesh = mesh
  30. else:
  31. vmesh = vedo.Mesh([mesh.coordinates(), mesh.cells()])
  32. bpts = vmesh.compute_normals(cells=True).boundaries().join(reset=1) #extract boundary
  33. vz = vmesh.celldata["Normals"][0][2] # check z component of normals at first point
  34. bpts.generate_mesh(invert=vz<0).write('tmpmesh.xml') #vedo REMESHING + smoothing
  35. return Mesh("tmpmesh.xml")
  36. #################################################################################
  37. N = 20 # number of iterations of stretching
  38. do_remesh = 0 # grab the boundary and remesh the interior at each iteration
  39. circle = vedo.Circle(r=50)
  40. mesh = remesh(circle)
  41. half_circle = circle.boundaries().cut_with_plane(origin=[-10,0,0], normal='-x').z(2)
  42. half_circle.linewidth(5).c("red4")
  43. plt = vedo.Plotter(N=N, size=(2250, 1300))
  44. meshes = [mesh]
  45. displacements = []
  46. for i in range(N):
  47. mfunc = MeshFunction('size_t', mesh, 1, mesh.domains())
  48. mfunc.set_all(0)
  49. allb = AllBoundaries()
  50. allb.mark(mfunc, 1)
  51. F = np.array([4, 2*(i-N/2)/N])
  52. displacement = solve_problem(mesh, mfunc, F)
  53. new_mesh = update(mesh, displacement)
  54. mesh = remesh(new_mesh) if do_remesh else new_mesh
  55. meshes.append(mesh)
  56. displacements.append(displacement)
  57. varrow = vedo.Arrow2D([0,0], F*15).z(1).c("red4")
  58. vmesh = vedo.Mesh([mesh.coordinates(), mesh.cells()]).c("k4").lc('k5')
  59. plt.at(i).show(f"t={i}, F={F}", half_circle, vmesh, varrow, zoom=1.5)
  60. plt.interactive().close()