PROGRAM MAIN
  IMPLICIT NONE
  REAL*8:: x00,y00,x0,y0
  INTEGER::i
  REAL*8, PARAMETER:: pi=4.0*ATAN(1.0), a=0.515, b=0.72, r=8E-4
  INTEGER, PARAMETER:: NMAX=1E4, NFINAL=24, SEED=78645
  !---------------------------------------------------------------
  OPEN(1,FILE='VariavelEstavel.dat')
  OPEN(2,FILE='VariavelInstavel.dat')
  CALL RANDOM_SEED()
  !
  write(*,*) nfinal
  x00=0.5787
  y00=-0.171
  !x00=x00+0.5; y00=-y00
  CI: DO i=1,NMAX
     x0=x00-r*(0.5-rand())
     y0=y00+r*(0.5-rand())
     CALL NONTWIST_FWD(x0,y0)
     CALL NONTWIST_BWD(x0,y0)
  END DO CI
CONTAINS
  SUBROUTINE NONTWIST_FWD(x00,y00)
    IMPLICIT NONE
    REAL*8::x0,x00,y0,y00,xn,yn
    INTEGER:: n
    !
    x0=x00;y0=y00
    DO n=1,NFINAL
       yn=y0-b*SIN(2.0*pi*x0)
       xn=MOD(x0+a*(1.0-yn**2.0),1.0)
       !
       IF(xn.LT.0.0) xn=xn+1.0
       !
       WRITE(2,*) xn,yn
       !
       x0=xn;y0=yn
    END DO
  END SUBROUTINE NONTWIST_FWD
  !
  SUBROUTINE NONTWIST_BWD(x00,y00)
    IMPLICIT NONE
    REAL*8::x0,x00,xn,y0,y00,yn
    INTEGER::n
    !
    x0=x00;y0=y00
    DO n=1,NFINAL
       xn=MOD(x0-a*(1.0-y0**2.0),1.0)
       !
       IF(xn.LT.0.0) xn=xn+1.0
       yn=y0+b*SIN(2.0*pi*xn)
       !
       WRITE(1,*) xn,yn
       !
       x0=xn;y0=yn
    END DO
  END SUBROUTINE NONTWIST_BWD
END PROGRAM MAIN
