C     ******************************************************************
C     ******************************************************************

      program procsol

      implicit none

C     PARAMETER
      integer mmax
      double precision pi
      parameter ( mmax = 2000 )
      parameter ( pi   = 3.1415926535898d0 )

C     LOCAL SCALARS
      integer i,itmp,j,m,setnum,insnum
      double precision a,b,r,f,cnorm,gparc,hparc,kparc,amov,amovparc,
     +        amcv,amcvparc,density
      character * 80 filename

C     LOCAL ARRAYS
      double precision u(mmax),v(mmax),s(mmax)

C     Read data

      write(*,*) 'Input file name: '
      read(*,*) filename

      open(10,file=filename)

      read(10,*) setnum,insnum

      if ( setnum .eq. 1 ) then
          b = insnum + 1.0d0
          a = 2.0d0 * b

      else if ( setnum .eq. 2 ) then
          b = 15.0d0 - insnum
          a = 450.0d0 / b

      else if ( setnum .eq. 3 ) then
          if ( insnum .eq. 1 ) then
              a = 150.0d0 * 2.0d0 / 3.0d0
              b =   3.0d0 * 2.0d0 / 3.0d0
          else if ( insnum .eq. 2 ) then
              a = 150.0d0 / 2.0d0
              b =   3.0d0 / 2.0d0
          else if ( insnum .eq. 3 ) then
              a = 112.5d0 * 2.0d0 / 3.0d0
              b =   4.0d0 * 2.0d0 / 3.0d0
          else if ( insnum .eq. 4 ) then
              a = 112.5d0 / 2.0d0
              b =   4.0d0 / 2.0d0
          else if ( insnum .eq. 5 ) then
              a =  90.0d0 * 2.0d0 / 3.0d0
              b =   5.0d0 * 2.0d0 / 3.0d0
          else if ( insnum .eq. 6 ) then
              a =  90.0d0 / 2.0d0
              b =   5.0d0 / 2.0d0
          end if
      end if

      read(10,*) m

      do i = 1,m
          read(10,*) itmp,u(i),v(i),s(i)
          if ( itmp .ne. i ) then
              write(*,*) 'Error in the input file.'
              stop
          end if
      end do

      close(10)

      r = 1.0d0

C     Check 0 <= s(i) <= 1

      do i = 1,m
          if ( s(i) .lt. 0.0d0 .or. s(i) .gt. 1.0d0 ) then
              write(*,*) 's out of range.'
              stop
          end if
      end do

C     Compute f and ||c||_infty

      f = 0.0d0
      cnorm = 0.0d0

      do i = 1,m
          hparc = 1.0d0 - ( ( u(i) / a ) ** 2 + ( v(i) / b ) ** 2 )
          cnorm = max( cnorm, abs( hparc ) )
          f = f + hparc ** 2
      end do

      do i = 1,m
          gparc = 1.0d0 - ( ( s(i) - 1.0d0 ) / r ) ** 2 * 
     +            ( ( b ** 2 / a ** 2 ) ** 2 * u(i) ** 2 + v(i) ** 2 )
          cnorm = max( cnorm, gparc )
          f = f + max( 0.0d0, gparc ) ** 2
      end do

      do i = 1,m
          do j = i + 1,m
              kparc = 1.0d0 - ( ( ( 1.0d0 + ( s(i) - 1.0d0 ) * ( b ** 2 
     +                / a ** 2 ) ) * u(i) - ( 1.0d0 + ( s(j) - 1.0d0 ) * 
     +                ( b ** 2 / a ** 2 ) ) * u(j) ) ** 2 + ( s(i) * 
     +                v(i) - s(j) * v(j) ) ** 2 ) / ( 4.0d0 * r ** 2 )
              cnorm = max( cnorm, kparc )
              f = f + max( 0.0d0, kparc ) ** 2
          end do
      end do

      f = 0.5d0 * f

C     Compute AMOV

      amov = 0.0d0
      do i = 1,m
          do j = i + 1,m
              amovparc = 2.0d0 * r - sqrt( ( ( 1.0d0 + ( s(i) - 1.0d0 ) 
     +                 * ( b ** 2 / a ** 2 ) ) * u(i) - ( 1.0d0 + ( s(j) 
     +                 - 1.0d0 ) * ( b ** 2 / a ** 2 ) ) * u(j) ) ** 2 
     +                 + ( s(i) * v(i) - s(j) * v(j) ) ** 2 ) 
              amov = max( amov, max( 0.0d0, amovparc ) )
          end do
      end do

C     Compute AMCV

      amcv = 0.0d0
      do i = 1,m
          amcvparc = r - ( 1.0d0 - s(i) ) * 
     +          sqrt( ( b ** 2 / a ** 2 ) ** 2 * u(i) ** 2 + v(i) ** 2 )
          amcv = max( amcv, max( 0.0d0, amcvparc ) )
      end do

C     Density

      density = ( m * r ** 2 ) / ( a * b )

      write(*,100) filename,setnum,insnum,a,b,m,f,cnorm,amov,amcv,
     +             density

      stop

C     NON-EXECUTABLE STATEMENTS

 100  format(A10,1X,I2,1X,I2,1X,0P,F8.4,1X,0P,F8.4,1X,I4,1X,1P,D7.1,1X,
     +       1P,D7.1,1X,1P,D7.1,1X,1P,D7.1,1X,0P,F6.4)

      end
