Anders and Briegel in Python
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

lattice_demo.py 1.1KB

8 år sedan
12345678910111213141516171819202122232425262728293031323334353637
  1. from abp.viz import VisibleGraphState
  2. import numpy as np
  3. import time
  4. import itertools
  5. square_unit_cell = (((0, 0), (0, 1)), ((0, 0), (1, 0)), ((1, 0), (1, 1)), ((0, 1), (1, 1)))
  6. funny_unit_cell = (((0, 0), (0, 1)), ((0, 0), (1, 0)), ((1, 0), (1, 1)), ((0, 1), (1, 1)), ((0, 0), (.5, .5)))
  7. def add_offset(vector, offset):
  8. """ Offset a vector in n-dimensional space """
  9. return tuple(v+o for v, o in zip(vector, offset))
  10. def offset_unit_cell(unit_cell, offset):
  11. """ Offset a unit cell """
  12. return {(add_offset(a, offset), add_offset(b, offset)) for a, b in unit_cell}
  13. def lattice(unit_cell, size):
  14. """ Generate a lattice from a unit cell """
  15. edges = set()
  16. for offset in itertools.product(*map(range, size)):
  17. edges |= offset_unit_cell(unit_cell, offset)
  18. nodes = set(itertools.chain(*edges))
  19. return nodes, edges
  20. #s = VisibleGraphState()
  21. nodes, edges = lattice(funny_unit_cell, (10, 10))
  22. psi = VisibleGraphState()
  23. for node in nodes:
  24. pos = {"x": node[0], "y": node[1], "z": 0}
  25. psi.add_node(str(node), meta={"position":pos})
  26. for edge in edges:
  27. psi.add_edge(str(edge[0]), str(edge[1]))