elasticity2.py 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. """Show fenics mesh and displacement solution."""
  2. from dolfin import *
  3. # Create mesh and define function space
  4. mesh = UnitCubeMesh(12, 12, 12)
  5. V = VectorFunctionSpace(mesh, "Lagrange", 1)
  6. # Mark boundary subdomains
  7. left = CompiledSubDomain("near(x[0], side) && on_boundary", side=0.0)
  8. right = CompiledSubDomain("near(x[0], side) && on_boundary", side=1.0)
  9. # Define Dirichlet boundary (x=0 or x=1)
  10. c = Constant((0.0, 0.0, 0.0))
  11. r = Expression((
  12. "scale*0.0",
  13. "scale*(y0 + (x[1]-y0)*cos(theta) - (x[2]-z0)*sin(theta)-x[1])",
  14. "scale*(z0 + (x[1]-y0)*sin(theta) + (x[2]-z0)*cos(theta)-x[2])",
  15. ), scale=0.5, y0=0.5, z0=0.5, theta=pi/4, degree=2)
  16. bcl = DirichletBC(V, c, left)
  17. bcr = DirichletBC(V, r, right)
  18. w = TrialFunction(V) # Incremental displacement
  19. v = TestFunction(V) # Test function
  20. u = Function(V) # Solution
  21. solve(inner(grad(w), grad(v)) * dx == inner(c, v) * dx, u, [bcl, bcr])
  22. ########################################################### vedo
  23. from vedo.dolfin import plot
  24. plot(u, mode='displacements', azimuth=45)