# HG changeset patch
# User Nathann Cohen <nathann.cohen@gmail.com>
# Date 1369656720 7200
# Node ID fa5aae0b1bc13165dfbe3b4a145e6377e62b242a
# Parent 0206caa9027833bd0ea5782a1c6237aa8933ea60
Add generators for fullerenes  reviewer's patch
diff git a/sage/graphs/graph_generators.py b/sage/graphs/graph_generators.py
a

b


928  928  with an embedding set. These will be simple graphs: no loops, no 
929  929  multiple edges, no directed edges. 
930  930  
 931  .. SEEALSO:: 
 932  
 933   :meth:`~sage.graphs.generic_graph.GenericGraph.set_embedding`, 
 934  :meth:`~sage.graphs.generic_graph.GenericGraph.get_embedding`  
 935  get/set methods for embeddings. 
 936  
931  937  EXAMPLES: 
932  938  
933   There are 1812 isomers of `\textrm{C}_60`, i.e., 1812 fullerene graphs 
 939  There are 1812 isomers of `\textrm{C}_{60}`, i.e., 1812 fullerene graphs 
934  940  on 60 vertices: :: 
935  941  
936  942  sage: gen = graphs.fullerenes(60) # optional buckygen 
… 
… 

946  952  sage: gen.next() # optional buckygen 
947  953  Traceback (most recent call last): 
948  954  ... 
949   StopIteration: Exhausted list of graphs from buckygen 
 955  StopIteration 
950  956  
951  957  The unique fullerene graph on 20 vertices is isomorphic to the dodecahedron 
952  958  graph. :: 
953  959  
954  960  sage: gen = graphs.fullerenes(20) # optional buckygen 
955  961  sage: g = gen.next() # optional buckygen 
 962  sage: g.is_isomorphic(graphs.DodecahedralGraph()) # optional buckygen 
 963  True 
956  964  sage: g.get_embedding() # optional buckygen 
957  965  {1: [2, 3, 4], 
958  966  2: [1, 5, 6], 
… 
… 

979  987  REFERENCE: 
980  988  
981  989  .. [buckygen] G. Brinkmann, J. Goedgebeur and B.D. McKay, Generation of Fullerenes, 
982   Journal of Chemical Information and Modeling, 52(11):29102918, 2012. 
 990  Journal of Chemical Information and Modeling, 52(11):29102918, 2012. 
983  991  """ 
984  992  from sage.misc.package import is_package_installed 
985  993  if not is_package_installed("buckygen"): 
986   raise TypeError, "the optional buckygen package is not installed" 
 994  raise TypeError("the optional buckygen package is not installed") 
987  995  
988   #number of vertices should be positive 
 996  # number of vertices should be positive 
989  997  if order < 0: 
990   raise ValueError("Number of vertices should be positive 20.") 
991   #buckygen can only handle fullerenes on up to 300 vertices 
 998  raise ValueError("Number of vertices should be positive.") 
 999  
 1000  # buckygen can only handle fullerenes on up to 300 vertices 
992  1001  if order > 300: 
993  1002  raise ValueError("Number of vertices should be at most 300.") 
994   #fullerenes only exist for an even number of vertices 
995   if order % 2 == 1: 
996   raise ValueError("Number of vertices should be even.") 
997  1003  
998   #fullerenes only exist for numbers larger than 20 and different from 22 
999   if order < 20 or order == 22: 
1000   raise StopIteration("Exhausted list of graphs from buckygen") 
 1004  # fullerenes only exist for an even number of vertices, larger than 20 
 1005  # and different from 22 
 1006  if order % 2 == 1 or order < 20 or order == 22: 
 1007  return 
1001  1008  
1002   if ipr: 
1003   command = 'buckygen Id {0}d'.format(order) 
1004   else: 
1005   command = 'buckygen d {0}d'.format(order) 
 1009  command = 'buckygen '+('I' if ipr else '')+'d {0}d'.format(order) 
1006  1010  
1007  1011  import subprocess 
1008  1012  sp = subprocess.Popen(command, shell=True, 
… 
… 

1012  1016  
1013  1017  #start of code to read planar code 
1014  1018  
1015   #read header 
1016   header = out.read(13) 
1017   
1018   assert header == '>>planar_code', 'Not a valid planar code header' 
1019   
1020   c = out.read(1) 
1021   
1022   while c != '<': 
1023   c = out.read(1) 
1024   
1025   #one more character to read full header 
1026   c = out.read(1) 
 1019  header = out.read(15) 
 1020  assert header == '>>planar_code<<', 'Not a valid planar code header' 
1027  1021  
1028  1022  #read graph per graph 
1029   c = out.read(1) 
1030  1023  while True: 
 1024  c = out.read(1) 
1031  1025  if len(c)==0: 
1032   raise StopIteration("Exhausted list of graphs from buckygen") 
 1026  return 
 1027  
 1028  # Each graph is stored in the following way : 
 1029  # 
 1030  # The first character is the number of vertices, followed by 
 1031  # n11,...,n1k,null character,n21,...,n2k',null character, ... 
 1032  # 
 1033  # where the n1* are all neighbors of n1 and all n2* are the 
 1034  # neighbors of n2, ... 
 1035  # 
 1036  # Besides, these neighbors are enumerated in clockwise order. 
1033  1037  order = ord(c) 
1034  1038  
1035  1039  zeroCount = 0 
1036  1040  
1037   l = [] 
1038   g = {} 
 1041  g = [[] for i in range(order)] 
1039  1042  
1040  1043  while zeroCount < order: 
1041  1044  c = out.read(1) 
1042  1045  if ord(c)==0: 
1043  1046  zeroCount += 1 
1044   g[zeroCount] = l 
1045   l = [] 
1046  1047  else: 
1047   l.append(ord(c)) 
 1048  g[zeroCount].append(ord(c)) 
1048  1049  
1049  1050  #construct graph based on g 
 1051  g = {i+1:di for i,di in enumerate(g)} 
1050  1052  G = graph.Graph(g) 
1051  1053  G.set_embedding(g) 
1052  1054  yield(G) 
1053  1055  
1054   
1055   #prepare for next graph 
1056   g = {} 
1057   
1058   c = out.read(1) 
1059   
1060  1056  ########################################################################### 
1061  1057  # Chessboard graphs 
1062  1058  ########################################################################### 