

run='12n501'

execfile('12cr_check_inputs.txt')

f=open('check12.loopfail.'+str(run)+'.txt','w')
ff=open('check12.loopfail.'+str(run)+'.txt','w')
g=open('check12.data.'+str(run)+'.txt','w')
h=open('check12.except.'+str(run)+'.txt','w')
hk=open('check12.except2.'+str(run)+'.txt','w')
hh=open('check12.rec.'+str(run)+'.txt','w')

for i in range(0,len(K12n501dt)):                 ## run through the diagrams of the alternating knots,  K13 = 4879 for rhs
  DE = K12n501dt[i]
  print 'K12n501 :: '+str(DE)+' : '
  g.write('\n'+'K12n501 : '+str(DE)+'\n')
  hh.write('K12n501'+str(DE)+'\n\r')
  qql = DE
  cnt = 0
  cnt2 = 0
  for j in range(0,12):
                            qqd = qql[:]
                            if j == 0:
                              qqd[0] = -qqd[0]
                            if j == 1:
                              qqd[1] = -qqd[1]
                            if j == 2:
                              qqd[2] = -qqd[2]
                            if j == 3:
                              qqd[3] = -qqd[3]
                            if j == 4:
                              qqd[4] = -qqd[4]
                            if j == 5:
                              qqd[5] = -qqd[5]
                            if j == 6:
                              qqd[6] = -qqd[6]
                            if j == 7:
                              qqd[7] = -qqd[7]
                            if j == 8:
                              qqd[8] = -qqd[8]
                            if j == 9:
                              qqd[9] = -qqd[9]
                            if j == 10:
                              qqd[10] = -qqd[10]
                            if j == 11:
                              qqd[11] = -qqd[11]
                            qqdt = tuple(qqd)              ## This is looping through all possible knots with
                            qqdtd = [qqdt]                 ## underlying diagram matching reference alternating knot
                            hh.write(str(qqdtd)+'\n')
                            Lx = Link('DT:'+str(qqdtd))
##                            Lx.backtrack(30)             ## on first try, don't complicate the knot first...
                            Lx.simplify('global')
                            try: 
                              Ex = Lx.exterior()
                              IDx = Ex.identify()
                              if IDx == []:                   ## knot was not identified; probably a connected sum
                                Gx = Ex.fundamental_group()
                                QQx = Gx.relators()
                                if QQx == []:                      ## knot is unknot
                                  g.write(str(qqdtd)+'  '+'unknot\n')
                                else:        ## new tack: loop until no unidentified summmands
                                  fail = 1
                                  for r in range(1,31):           ## loop up to 30 times to shake the knot
                                    if fail == 1:
                                      print(str(r))
                                      fail = 0
                                      Lx.backtrack(30+r)
                                      Lx.simplify('global')
                                      llkd = Lx.deconnect_sum()          ## finds 'obvious' summands
                                      llkdID = []                        ## holder for identified summands
                                      for uu in range(0,len(llkd)):      ## running through summands
                                        llkx = llkd[uu]
                                        llkx.simplify('global')
                                        ellkx = llkx.exterior()
                                        ellkidx = ellkx.identify()
                                        if ellkidx == []:                ## summmand not identified
                                          fail = 1
                                        else:
                                          llkdID = llkdID + [ellkidx]
                                      if fail == 0:
                                        g.write(str(qqdtd)+'  '+str(llkdID)+'\n')
                                  if fail == 1:
                                        print('a failed summand loop  '+str(qqdtd))
                                        f.write(str(qqdtd)+'  '+str(llkdID)+'\n')
                                        ff.write(str(qqdtd)+',\n')
                                        g.write(str(qqdtd)+'  '+'LOOPFAIL'+'\n')
                              else:
                                g.write(str(qqdtd)+'  '+str(IDx)+'\n')
                            except SnapPeaFatalError:
                              print qqdtd,'SnapPeaFatalError'
                              h.write('K12a'+' FAILURE SPFE '+str(qq)+'  '+str(qqdtd)+'\n\r')
                              hk.write(str(qqdtd)+',\n\r')
                              g.write(str(qqdtd)+'  '+'F-ERROR'+'\n')
                            except ValueError:
                              print qqdtd,'ValueError'
                              h.write('K12a'+' FAILURE VE '+str(qq)+'  '+str(qqdtd)+'\n\r')
                              hk.write(str(qqdtd)+',\n\r')
                              g.write(str(qqdtd)+'  '+'V-ERROR'+'\n')



f.close()
ff.close()
g.close()
h.close()
hk.close()
hh.close()

