      PROGRAM DUALDATA
      IMPLICIT NONE

C     Creates the duals groups from the triangle groups.  Takes as input
C     the T_list.txt (triangles) and cubes.txt (the number of cubes in each
C     group).

C     For each group considered creates the dual group and as output
C     gives a file that can be run in Magma to calculate which of these
C     dual groups are isomorphic.
     
      INTEGER TRI(30*10000,3),N,L,J,I,SUM, MAX
      INTEGER CUBEL(7160), PLACE(7160)
      INTEGER CTRI(30*7160,3), CCUBEL(7160) 
      INTEGER DUAL(3*30*7160,3), DUALG(180*7160,3)
      INTEGER ELEM(7160), JJ, KK, K

C     Open the data file on units 3, 4 and 5
      OPEN (UNIT=3, FILE='T_list.txt')
      OPEN (UNIT=4, FILE='cubes.txt')
      OPEN (UNIT=7, FILE='dualdata.txt')


C     Read in the data:

      MAX=7160

C Read in the number of cubes for each representative
      READ (4,*) (CUBEL(I), I=1,MAX)
      CLOSE(UNIT=4)

      CCUBEL=CUBEL

      I=1
      SUM=0
 15   IF (I.LE. MAX) THEN
         SUM=SUM+15+(CUBEL(I)+2)/3*2
         I=I+1
         GOTO 15
      END IF


      READ (3,*) ((TRI(I,J), J=1,3), I=1,SUM)
      CLOSE(UNIT=3)

      WRITE(*,*) 'Data read ok'

      
C Make list of the last rows of each group on T_list
      I=2
      PLACE(1)=0
 20   IF(I .LE. MAX) THEN
         PLACE(I)=PLACE(I-1)+15+((CUBEL(I-1)+2)/3)*2
         I=I+1
         GOTO 20
      END IF


C Pick up the strat rows for each set

      I=1
 30   IF(I .LE. MAX) THEN
         L=1
 35      IF(L.LE. (15+(CCUBEL(I)+2)/3*2)) THEN
            CTRI((I-1)*30+L,1)=TRI(PLACE(I)+L,1)
            CTRI((I-1)*30+L,2)=TRI(PLACE(I)+L,2)
            CTRI((I-1)*30+L,3)=TRI(PLACE(I)+L,3)
            L=L+1
            GOTO 35
         ELSE IF(L.LE. 30) THEN
            CTRI((I-1)*30+L,1)=0
            CTRI((I-1)*30+L,2)=0
            CTRI((I-1)*30+L,3)=0
            L=L+1
            GOTO 35
         END IF
         I=I+1
         GOTO 30
      END IF


C We start to create the dual groups.

      N=0
      I=1
      L=1
 100  IF(CTRI(N*30+I,1).NE.0 .AND. CTRI(N*30+I,1).EQ.CTRI(N*30+I,2) 
     +     .AND. CTRI(N*30+I,1).EQ.CTRI(N*30+I,3)) THEN
C we have a cube (x,x,x), take (3x,3x-1,3x-2)
         DUAL(N*90+L,1)=3*CTRI(N*30+I,1)
         DUAL(N*90+L,2)=3*CTRI(N*30+I,1)-1
         DUAL(N*90+L,3)=3*CTRI(N*30+I,1)-2
         L=L+1
      ELSE
         DUAL(N*90+L,1)=3*CTRI(N*30+I,1)
         DUAL(N*90+L,2)=3*CTRI(N*30+I,2)-1
         DUAL(N*90+L,3)=3*CTRI(N*30+I,3)-2
         L=L+1
         DUAL(N*90+L,1)=3*CTRI(N*30+I,1)-2
         DUAL(N*90+L,2)=3*CTRI(N*30+I,2)
         DUAL(N*90+L,3)=3*CTRI(N*30+I,3)-1
         L=L+1
         DUAL(N*90+L,1)=3*CTRI(N*30+I,1)-1
         DUAL(N*90+L,2)=3*CTRI(N*30+I,2)-2
         DUAL(N*90+L,3)=3*CTRI(N*30+I,3)
         L=L+1
      END IF
      I=I+1
C      IF(I.LE.(15+(CCUBEL(I)+2)/3*2)) THEN 
      IF(L.LE.45) THEN 
         GOTO 100
      ELSE
         N=N+1
         I=1
         L=1
         IF(N.LT. MAX) THEN
            GOTO 100
         END IF
      END IF

C Let us calculate the number of elements in each dual
CCCC THERE IS 45 ROWS IN EACH!!!

CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCc
C Create the dual groups.

C First take a each triangle face as a vertex


 1100  DO 1800 L=1,MAX

         DO 1150 KK=1,45
            DO 1120 JJ=1,3
               DUALG((L-1)*90+45+KK,JJ)=DUAL((L-1)*90+KK,JJ)
 1120        CONTINUE
 1150     CONTINUE

C     Then take each edge as a vertex

          DO 1700 KK=1,45
             N=1
             DO 1500 I=1,45
                DO 1200 J=1,3
                   IF(DUAL((L-1)*90+I,J) .EQ. KK) THEN
                      DUALG((L-1)*90+KK,N)=I+45
                      N=N+1
                   END IF
 1200           CONTINUE
                
 1500        CONTINUE
            
 1700     CONTINUE
            
 1800 CONTINUE

C      DO 2000 L=1,4964
C
C         IF(L.LT.10) WRITE(7,'(A,I1,A)') 'G', L, ':='
C         IF(L.GT.9 .AND. L.LT.100) THEN 
C            WRITE(7,'(A,I2,A)') 'G', L, ':='
C         END IF
C         IF(L.GT.99 .AND. L.LT.1000) THEN 
C            WRITE(7,'(A,I3,A)') 'G', L, ':='
C         END IF
C         IF(L.GT.999) THEN
C            WRITE(7,'(A,I4,A)') 'G', L, ':='
C         END IF
C
C         WRITE(UNIT=7,FMT=*) 'Graph<90|[', '{', 
C     +        DUALG((L-1)*90+1,1), 
C     +           ',',DUALG((L-1)*90+1,2), ',' ,
C     +           DUALG((L-1)*90+1,3), '},'
C         DO 1900 I=2,89
C            WRITE(UNIT=7,FMT=*) '{', DUALG((L-1)*90+I,1), 
C     +           ',',DUALG((L-1)*90+I,2), ',' ,
C     +           DUALG((L-1)*90+I,3), '},'
C 1900    CONTINUE
C         WRITE(UNIT=7,FMT=*)  '{', DUALG((L-1)*90+90,1), 
C     +           ',',DUALG((L-1)*90+90,2), ',' ,
C     +           DUALG((L-1)*90+90,3), '}]>;'
C 2000 CONTINUE

 2030 CONTINUE


      WRITE(*,*) 'Duals computed ok'
      WRITE(7,'(3I3)') ((DUALG(I,J), J=1,3),I=1,180*MAX)

CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC

C     Now we have the dual groups. 

C     Let us then write them in such a form that Magma can read them.

      
C     Open the result file
      OPEN(UNIT=9, FILE='dual_magma.txt')

C     print the magma code

CCCCCCCCCCCCCCCCCCCCCCC
C      N=1
C
C 2050  IF(N.LE. 4964) THEN
C
C         IF(N.LT.10) WRITE(9,'(A,I1)') 'G', N
C         IF(N.GT.9 .AND. N.LT.100) WRITE(9,'(A,I2)') 'G', N
C         IF(N.GT.99 .AND. N.LT.1000) WRITE(9,'(A,I3)') 'G', N
C         IF(N.GT.999 .AND. N.LT.10000) WRITE(9,'(A,I4)') 'G', N
C         WRITE(9,'(A)') '<x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,'
C         WRITE(9,'(A)') 'x11,x12,x13,x14,x15,x16,x17,x18,'
C         WRITE(9,'(A)') 'x19,x20,x21,x22,x23,x24,x25,x26,'
C         WRITE(9,'(A)') 'x27,x28,x29,x30,x31,x32,x33,x34,'
C         WRITE(9,'(A)') 'x35,x36,x37,x38,x39,x40,x41,x42,'
C         WRITE(9,'(A)') 'x43,x44,x45,x46,x47,x48,x49,x50,'
C         WRITE(9,'(A)') 'x51,x52,x53,x54,x55,x56,x57,x58,'
C         WRITE(9,'(A)') 'x59,x60,x61,x62,x63,x64,x65,x66,'
C         WRITE(9,'(A)') 'x67,x68,x69,x70,x71,x72,x73,x74,'
C         WRITE(9,'(A)') 'x75,x76,x77,x78,x79,x80,x81,x82,'
C         WRITE(9,'(A)') 'x83,x84,x85,x86,x87,x88,x89,x90>:='
C         WRITE(9,'(A)') 'Group<x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,'
C         WRITE(9,'(A)') 'x11,x12,x13,x14,x15,x16,x17,x18,'
C         WRITE(9,'(A)') 'x19,x20,x21,x22,x23,x24,x25,x26,'
C         WRITE(9,'(A)') 'x27,x28,x29,x30,x31,x32,x33,x34,'
C         WRITE(9,'(A)') 'x35,x36,x37,x38,x39,x40,x41,x42,'
C         WRITE(9,'(A)') 'x43,x44,x45,x46,x47,x48,x49,x50,'
C         WRITE(9,'(A)') 'x51,x52,x53,x54,x55,x56,x57,x58,'
C         WRITE(9,'(A)') 'x59,x60,x61,x62,x63,x64,x65,x66,'
C         WRITE(9,'(A)') 'x67,x68,x69,x70,x71,x72,x73,x74,'
C         WRITE(9,'(A)') 'x75,x76,x77,x78,x79,x80,x81,x82,'
C         WRITE(9,'(A)') 'x83,x84,x85,x86,x87,x88,x89,x90|'
C
C         L=1
C
C 2100    IF(L .LT. 90) THEN
C
C            IF(DUALG((N-1)*90+L,1).LT.10) THEN
C               WRITE(9, '(A,I1,A)') 'x',DUALG((N-1)*90+L,1),'*'
C            ELSE
C               WRITE(9, '(A,I2,A)') 'x',DUALG((N-1)*90+L,1),'*'
C            END IF
C            IF(DUALG((N-1)*90+L,2).LT.10) THEN
C               WRITE(9, '(A,I1,A)') 'x',DUALG((N-1)*90+L,2),'*'
C            ELSE
C               WRITE(9, '(A,I2,A)') 'x',DUALG((N-1)*90+L,2),'*'
C            END IF
C            IF(DUALG((N-1)*90+L,3).LT.10) THEN
C               WRITE(9, '(A,I1,A)') 'x',DUALG((N-1)*90+L,3),','
C            ELSE
C               WRITE(9, '(A,I2,A)') 'x',DUALG((N-1)*90+L,3),','
C            END IF
C            L=L+1
C            GOTO 2100
C         END IF
C
C         IF(DUALG((N-1)*90+L,1).LT.10) THEN
C            WRITE(9, '(A,I1,A)') 'x',DUALG((N-1)*90+L,1),'*'
C         ELSE
C            WRITE(9, '(A,I2,A)') 'x',DUALG((N-1)*90+L,1),'*'
C         END IF
C         IF(DUALG((N-1)*90+L,2).LT.10) THEN
C            WRITE(9, '(A,I1,A)') 'x',DUALG((N-1)*90+L,2),'*'
C         ELSE
C            WRITE(9, '(A,I2,A)') 'x',DUALG((N-1)*90+L,2),'*'
C         END IF
C         IF(DUALG((N-1)*90+L,3).LT.10) THEN
C            WRITE(9, '(A,I1,A)') 'x',DUALG((N-1)*90+L,3),'>;'
C         ELSE
C            WRITE(9, '(A,I2,A)') 'x',DUALG((N-1)*90+L,3),'>;'
C         END IF

C      ELSE
C         WRITE(*,*) 'Files dualdata.txt and dual_magma.txt printed'
C         GOTO 9999
C      END IF

C      N=N+1
C      GOTO 2050

      DO 2900 N=1,MAX

         IF(N.LT.10) WRITE(9,'(A,I1,A)') 'G', N,':='
         IF(N.GT.9 .AND. N.LT.100) WRITE(9,'(A,I2,A)') 'G', N, ':='
         IF(N.GT.99 .AND. N.LT.1000) WRITE(9,'(A,I3,A)') 'G', N,':='
         IF(N.GT.999 .AND. N.LT.10000) WRITE(9,'(A,I4,A)') 'G', N,':='
         WRITE(UNIT=9,FMT=*) 'Graph<90|[', '{', DUALG((N-1)*90+1,1), 
     +           ',',DUALG((N-1)*90+1,2), ',' ,
     +           DUALG((N-1)*90+1,3), '},'
         DO 2700 I=2,89
            WRITE(UNIT=9,FMT=*) '{', DUALG((N-1)*90+I,1), 
     +           ',',DUALG((N-1)*90+I,2), ',' ,
     +           DUALG((N-1)*90+I,3), '},'
 2700           CONTINUE
         WRITE(UNIT=9,FMT=*)  '{', DUALG((N-1)*90+90,1), 
     +           ',',DUALG((N-1)*90+90,2), ',' ,
     +           DUALG((N-1)*90+90,3), '}]>;'
 2900     CONTINUE




 3000  FORMAT (3I4)
 4000  FORMAT (I4,F8.5)
 5000  FORMAT (15I3)

 9000 FORMAT (3I3)



 9999 END
