! -*- F90 -*-


      subroutine MRSTevolve(x,Q,pdf) 
      implicit real*8(a-h,o-z) 
      include 'parmsetup.inc' 
      character*16 name(nmxset) 
      integer nmem(nmxset),ndef(nmxset),mmem 
!      integer member(nmxset)                                           
      integer nset,iset 
      common/NAME/name,nmem,ndef,mmem 
      parameter(nx=49,nq=37,np=8,nqc0=2,nqb0=11,nqc=35,nqb=26,          &
     &nhess=30)                                                         
      real*8 pdf(-6:6) 
      double precision gridx(nmxgridx),gridq(nmxgridq)
      integer ngridx,ngridq,jx,jq
      real*8 f1(nx,nq)                                                  &
     &,f2(nx,nq)                                                        &
     &,f3(nx,nq)                                                        &
     &,f4(nx,nq)                                                        &
     &,f5(nx,nq)                                                        &
     &,f6(nx,nq)                                                        &
     &,f7(nx,nq)                                                        &
     &,f8(nx,nq)                                                        &
     &,fc(nx,nqc),fb(nx,nqb)                                            
      real*8 qq(nq),xx(nx),                                             &
     &cc1(0:nhess,nx,nq,4,4,nmxset),cc2(0:nhess,nx,nq,4,4,nmxset),      &
     &cc3(0:nhess,nx,nq,4,4,nmxset),cc4(0:nhess,nx,nq,4,4,nmxset),      &
     &cc6(0:nhess,nx,nq,4,4,nmxset),cc8(0:nhess,nx,nq,4,4,nmxset),      &
     &ccc(0:nhess,nx,nqc,4,4,nmxset),ccb(0:nhess,nx,nqb,4,4,nmxset)     
      real*8 xxl(nx),qql(nq),qqlc(nqc),qqlb(nqb) 
      data xx/1d-5,2d-5,4d-5,6d-5,8d-5,                                 &
     &              1d-4,2d-4,4d-4,6d-4,8d-4,                           &
     &              1d-3,2d-3,4d-3,6d-3,8d-3,                           &
     &              1d-2,1.4d-2,2d-2,3d-2,4d-2,6d-2,8d-2,               &
     &           .1d0,.125d0,.15d0,.175d0,.2d0,.225d0,.25d0,.275d0,     &
     &           .3d0,.325d0,.35d0,.375d0,.4d0,.425d0,.45d0,.475d0,     &
     &           .5d0,.525d0,.55d0,.575d0,.6d0,.65d0,.7d0,.75d0,        &
     &           .8d0,.9d0,1d0/                                         
      data qq/1.25d0,1.5d0,2d0,2.5d0,3.2d0,4d0,5d0,6.4d0,8d0,1d1,       &
     &        1.2d1,1.8d1,2.6d1,4d1,6.4d1,1d2,                          &
     &        1.6d2,2.4d2,4d2,6.4d2,1d3,1.8d3,3.2d3,5.6d3,1d4,          &
     &        1.8d4,3.2d4,5.6d4,1d5,1.8d5,3.2d5,5.6d5,1d6,              &
     &        1.8d6,3.2d6,5.6d6,1d7/                                    
      data xmin,xmax,qsqmin,qsqmax/1d-5,1d0,1.25d0,1d7/ 
      save 
                                                                        
      xsave=x 
      qsq = q*q 
      q2save=qsq 
                                                                        
      xlog=dlog(x) 
      qsqlog=dlog(qsq) 
                                                                        
      call getnset(iset) 
!      imem=member(iset)                                                
      call getnmem(iset,imem) 
                                                                        
                                ! G.W. 24/04/2008                       
      if (qsq.lt.qsqmin) then 
         qsqlog=dlog(1.01D0*qsqmin) 
         call jeppe2(imem,xlog,qsqlog,nx,nq,xxl,qql,                    &
     &   cc1(0,1,1,1,1,iset),upv1)                                      
         call jeppe2(imem,xlog,qsqlog,nx,nq,xxl,qql,                    &
     &         cc2(0,1,1,1,1,iset),dnv1)                                
         call jeppe2(imem,xlog,qsqlog,nx,nq,xxl,qql,                    &
     &         cc3(0,1,1,1,1,iset),glu1)                                
         call jeppe2(imem,xlog,qsqlog,nx,nq,xxl,qql,                    &
     &   cc4(0,1,1,1,1,iset),usea1)                                     
         call jeppe2(imem,xlog,qsqlog,nx,nq,xxl,qql,                    &
     &   cc6(0,1,1,1,1,iset),str1)                                      
         call jeppe2(imem,xlog,qsqlog,nx,nq,xxl,qql,                    &
     &   cc8(0,1,1,1,1,iset),dsea1)                                     
         qsqlog=dlog(qsqmin) 
      end if 
      call jeppe2(imem,xlog,qsqlog,nx,nq,xxl,qql,                       &
     &cc1(0,1,1,1,1,iset),upv)                                          
      call jeppe2(imem,xlog,qsqlog,nx,nq,xxl,qql,                       &
     &cc2(0,1,1,1,1,iset),dnv)                                          
      call jeppe2(imem,xlog,qsqlog,nx,nq,xxl,qql,                       &
     &cc3(0,1,1,1,1,iset),glu)                                          
      call jeppe2(imem,xlog,qsqlog,nx,nq,xxl,qql,                       &
     &cc4(0,1,1,1,1,iset),usea)                                         
      call jeppe2(imem,xlog,qsqlog,nx,nq,xxl,qql,                       &
     &cc6(0,1,1,1,1,iset),str)                                          
      call jeppe2(imem,xlog,qsqlog,nx,nq,xxl,qql,                       &
     &cc8(0,1,1,1,1,iset),dsea)                                         
                                ! G.W. 24/04/2008                       
      if (qsq.lt.qsqmin) then 
!--   Calculate the anomalous dimension, dlog(xf)/dlog(qsq),            
!--   evaluated at qsqmin.  Then extrapolate the PDFs to low            
!--   qsq < qsqmin by interpolating the anomalous dimenion between      
!--   the value at qsqmin and a value of 1 for qsq << qsqmin.           
!--   If value of PDF at qsqmin is very small, just set                 
!--   anomalous dimension to 1 to prevent rounding errors.              
         if (abs(upv).lt.1.D-4)  then
             anom = 1.D0
         else  
             anom = (upv1-upv)/upv/0.01D0 
         endif   
         upv = upv*(qsq/qsqmin)**(anom*qsq/qsqmin+1.D0-qsq/qsqmin)
         if (abs(dnv).lt.1.D-4) then
             anom = 1.D0
         else 
             anom = (dnv1-dnv)/dnv/0.01D0 
         endif
         dnv = dnv*(qsq/qsqmin)**(anom*qsq/qsqmin+1.D0-qsq/qsqmin)
         if (abs(glu).lt.1.D-4) then
             anom = 1.D0
         else 
             anom = (glu1-glu)/glu/0.01D0  
         endif
         glu = glu*(qsq/qsqmin)**(anom*qsq/qsqmin+1.D0-qsq/qsqmin) 
         if (abs(usea).lt.1.D-4) then
             anom = 1.D0 
         else
             anom = (usea1-usea)/usea/0.01D0
         endif 
         usea = usea*(qsq/qsqmin)**(anom*qsq/qsqmin+1.D0-qsq/qsqmin) 
         if (abs(str).lt.1.D-4) then
             anom = 1.D0 
         else
             anom = (str1-str)/str/0.01D0 
         endif
         str = str*(qsq/qsqmin)**(anom*qsq/qsqmin+1.D0-qsq/qsqmin) 
         if (abs(dsea).lt.1.D-4) then
             anom = 1.D0 
         else
             anom = (dsea1-dsea)/dsea/0.01D0 
         endif
         dsea = dsea*(qsq/qsqmin)**(anom*qsq/qsqmin+1.D0-qsq/qsqmin) 
      end if 
                                                                        
                                                                        
                                                                        
      chm=0.d0 
      if(qsq.gt.emc2) then 
      call jeppe2(imem,xlog,qsqlog,nx,nqc,xxl,qqlc,                     &
     &ccc(0,1,1,1,1,iset),chm)                                          
      endif 
                                                                        
      bot=0.d0 
      if(qsq.gt.emb2) then 
      call jeppe2(imem,xlog,qsqlog,nx,nqb,xxl,qqlb,                     &
     &ccb(0,1,1,1,1,iset),bot)                                          
      endif 
                                                                        
      pdf(0)  = glu 
      pdf(1)  = dnv+dsea 
      pdf(-1) = dsea 
      pdf(2)  = upv+usea 
      pdf(-2) = usea 
      pdf(3)  = str 
      pdf(-3) = str 
      pdf(4)  = chm 
      pdf(-4) = chm 
      pdf(5)  = bot 
      pdf(-5) = bot 
      pdf(6)  = 0.0d0 
      pdf(-6) = 0.0d0 
                                                                        
      x=xsave 
      qsq=q2save 
      return 
!                                                                       
      entry MRSTgetgrid(nset,ngridx,ngridq,gridx,gridq)
      do jx=1,nx
          gridx(jx)=xx(jx)
      enddo
      do jq=1,nq
          gridq(jq)=qq(jq)
      enddo
      ngridx=nx
      ngridq=nq        
      return
      
      entry MRSTread(nset) 
      read(1,*)nmem(nset),ndef(nset) 
!      print *,nmem(nset),ndef(nset)                                    
!      do nm = 0,nmem-1                                                 
      do nm = 0,nmem(nset) 
        do 20 n=1,nx-1 
        do 20 m=1,nq 
        read(1,50)f1(n,m),f2(n,m),f3(n,m),f4(n,m),                      &
     &                  f5(n,m),f7(n,m),f6(n,m),f8(n,m)                 
! notation: 1=uval 2=val 3=glue 4=usea 5=chm 6=str 7=btm 8=dsea         
   20 continue 
!      write(*,*)'PDF set ',nm,' first element ',f1(1,1)                
      do 40 m=1,nq 
      f1(nx,m)=0.d0 
      f2(nx,m)=0.d0 
      f3(nx,m)=0.d0 
      f4(nx,m)=0.d0 
      f5(nx,m)=0.d0 
      f6(nx,m)=0.d0 
      f7(nx,m)=0.d0 
      f8(nx,m)=0.d0 
   40 continue 
                                                                        
      do n=1,nx 
      xxl(n)=dlog(xx(n)) 
      enddo 
      do m=1,nq 
      qql(m)=dlog(qq(m)) 
      enddo 
                                                                        
      call jeppe1(nm,nx,nq,xxl,qql,f1,cc1(0,1,1,1,1,nset)) 
      call jeppe1(nm,nx,nq,xxl,qql,f2,cc2(0,1,1,1,1,nset)) 
      call jeppe1(nm,nx,nq,xxl,qql,f3,cc3(0,1,1,1,1,nset)) 
      call jeppe1(nm,nx,nq,xxl,qql,f4,cc4(0,1,1,1,1,nset)) 
      call jeppe1(nm,nx,nq,xxl,qql,f6,cc6(0,1,1,1,1,nset)) 
      call jeppe1(nm,nx,nq,xxl,qql,f8,cc8(0,1,1,1,1,nset)) 
                                                                        
      emc2=2.045 
      emb2=18.5 
                                                                        
      do 44 m=1,nqc 
      qqlc(m)=qql(m+nqc0) 
      do 44 n=1,nx 
      fc(n,m)=f5(n,m+nqc0) 
   44 continue 
      qqlc(1)=dlog(emc2) 
      call jeppe1(nm,nx,nqc,xxl,qqlc,fc,ccc(0,1,1,1,1,nset)) 
                                                                        
      do 45 m=1,nqb 
      qqlb(m)=qql(m+nqb0) 
      do 45 n=1,nx 
      fb(n,m)=f7(n,m+nqb0) 
   45 continue 
      qqlb(1)=dlog(emb2) 
      call jeppe1(nm,nx,nqb,xxl,qqlb,fb,ccb(0,1,1,1,1,nset)) 
                                                                        
                                                                        
      enddo 
   50 format(8f10.5) 
      return 
!                                                                       
                                                                        
      entry MRSTalfa(nflav,alfas,Qalfa) 
        call getnset(iset) 
!        mem = member(iset)                                             
!        call setnmem(member(iset))                                     
        call alphamrs(nflav,alfas,Qalfa) 
      return 
!                                                                       
      entry MRSTinit(Eorder,Q2fit) 
      return 
!                                                                       
      entry MRSTpdf(mem) 
!      if(mem.eq.0) mem=ndef                                            
!      imem = mem                                                       
        call getnset(iset) 
!        member(iset)=mem                                               
        call setnmem(iset,mem) 
                                                                        
      return 
!                                                                       
      END                                           
