Example description
!   D03PJA Example Program Text
!   Mark 27.1 Release. NAG Copyright 2020.

    Module d03pjae_mod

!     D03PJA Example Program Module:
!            Parameters and User-defined Routines

!     .. Use Statements ..
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Accessibility Statements ..
      Private
      Public                           :: bndary, odedef, pdedef, uvinit
!     .. Parameters ..
      Real (Kind=nag_wp), Parameter    :: one = 1.0_nag_wp
      Integer, Parameter, Public       :: itrace = 0, nin = 5, nout = 6,       &
                                          npde = 1, nv = 1, nxi = 1
      Logical, Parameter, Public       :: print_stat = .False.
    Contains
      Subroutine uvinit(npde,npts,x,u,nv,v,iuser,ruser)
!       Routine for PDE initial values (start time is 0.1D-6)

!       .. Scalar Arguments ..
        Integer, Intent (In)           :: npde, npts, nv
!       .. Array Arguments ..
        Real (Kind=nag_wp), Intent (Inout) :: ruser(*)
        Real (Kind=nag_wp), Intent (Out) :: u(npde,npts), v(nv)
        Real (Kind=nag_wp), Intent (In) :: x(npts)
        Integer, Intent (Inout)        :: iuser(*)
!       .. Local Scalars ..
        Real (Kind=nag_wp)             :: ts
        Integer                        :: i
!       .. Intrinsic Procedures ..
        Intrinsic                      :: exp
!       .. Executable Statements ..
        ts = ruser(1)
        v(1) = ts
        Do i = 1, npts
          u(1,i) = exp(ts*(one-x(i))) - one
        End Do
        Return
      End Subroutine uvinit
      Subroutine odedef(npde,t,nv,v,vdot,nxi,xi,ucp,ucpx,rcp,ucpt,ucptx,f,     &
        ires,iuser,ruser)

!       .. Scalar Arguments ..
        Real (Kind=nag_wp), Intent (In) :: t
        Integer, Intent (Inout)        :: ires
        Integer, Intent (In)           :: npde, nv, nxi
!       .. Array Arguments ..
        Real (Kind=nag_wp), Intent (Out) :: f(nv)
        Real (Kind=nag_wp), Intent (In) :: rcp(npde,nxi), ucp(npde,nxi),       &
                                          ucpt(npde,nxi), ucptx(npde,nxi),     &
                                          ucpx(npde,nxi), v(nv), vdot(nv),     &
                                          xi(nxi)
        Real (Kind=nag_wp), Intent (Inout) :: ruser(*)
        Integer, Intent (Inout)        :: iuser(*)
!       .. Executable Statements ..
        If (ires==1) Then
          f(1) = vdot(1) - v(1)*ucp(1,1) - ucpx(1,1) - one - t
        Else If (ires==-1) Then
          f(1) = vdot(1)
        End If
        Return
      End Subroutine odedef
      Subroutine pdedef(npde,t,x,nptl,u,ux,nv,v,vdot,p,q,r,ires,iuser,ruser)

!       .. Scalar Arguments ..
        Real (Kind=nag_wp), Intent (In) :: t
        Integer, Intent (Inout)        :: ires
        Integer, Intent (In)           :: npde, nptl, nv
!       .. Array Arguments ..
        Real (Kind=nag_wp), Intent (Out) :: p(npde,npde,nptl), q(npde,nptl),   &
                                          r(npde,nptl)
        Real (Kind=nag_wp), Intent (Inout) :: ruser(*)
        Real (Kind=nag_wp), Intent (In) :: u(npde,nptl), ux(npde,nptl), v(nv), &
                                          vdot(nv), x(nptl)
        Integer, Intent (Inout)        :: iuser(*)
!       .. Local Scalars ..
        Integer                        :: i
!       .. Executable Statements ..
        Do i = 1, nptl
          p(1,1,i) = v(1)*v(1)
          r(1,i) = ux(1,i)
          q(1,i) = -x(i)*ux(1,i)*v(1)*vdot(1)
        End Do
        Return
      End Subroutine pdedef
      Subroutine bndary(npde,t,u,ux,nv,v,vdot,ibnd,beta,gamma,ires,iuser,      &
        ruser)

!       .. Scalar Arguments ..
        Real (Kind=nag_wp), Intent (In) :: t
        Integer, Intent (In)           :: ibnd, npde, nv
        Integer, Intent (Inout)        :: ires
!       .. Array Arguments ..
        Real (Kind=nag_wp), Intent (Out) :: beta(npde), gamma(npde)
        Real (Kind=nag_wp), Intent (Inout) :: ruser(*)
        Real (Kind=nag_wp), Intent (In) :: u(npde), ux(npde), v(nv), vdot(nv)
        Integer, Intent (Inout)        :: iuser(*)
!       .. Intrinsic Procedures ..
        Intrinsic                      :: exp
!       .. Executable Statements ..
        beta(1) = one
        If (ibnd==0) Then
          gamma(1) = -v(1)*exp(t)
        Else
          gamma(1) = -v(1)*vdot(1)
        End If
        Return
      End Subroutine bndary
    End Module d03pjae_mod
    Program d03pjae

!     D03PJA Example Main Program

!     .. Use Statements ..
      Use d03pjae_mod, Only: bndary, itrace, nin, nout, npde, nv, nxi, odedef, &
                             pdedef, print_stat, uvinit
      Use nag_library, Only: d03pja, nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Real (Kind=nag_wp)               :: tout, ts
      Integer                          :: i, ifail, ind, it, itask, itol,      &
                                          latol, lenode, lrtol, m, nbkpts,     &
                                          nel, neqn, niw, npl1, npoly, npts,   &
                                          nw, nwkres
      Logical                          :: theta
      Character (1)                    :: laopt, norm
!     .. Local Arrays ..
      Real (Kind=nag_wp)               :: algopt(30), ruser(1), rwsav(1100),   &
                                          xi(nxi)
      Real (Kind=nag_wp), Allocatable  :: atol(:), rtol(:), u(:), w(:), x(:),  &
                                          xbkpts(:)
      Integer                          :: iuser(1), iwsav(505)
      Integer, Allocatable             :: iw(:)
      Logical                          :: lwsav(100)
      Character (80)                   :: cwsav(10)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: abs, mod, real
!     .. Executable Statements ..
      Write (nout,*) 'D03PJA Example Program Results'
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) m, nbkpts, npoly

      nel = nbkpts - 1
      npts = nel*npoly + 1
      neqn = npde*npts + nv
      npl1 = npoly + 1
      nwkres = 3*npl1*npl1 + npl1*(npde*npde+6*npde+nbkpts+1) + 8*npde +       &
        nxi*(5*npde+1) + nv + 3
      lenode = 11*neqn + 50
      nw = neqn*neqn + neqn + nwkres + lenode
      niw = 25*neqn + 24
      Allocate (u(neqn),w(nw),x(npts),xbkpts(nbkpts),iw(niw))

      Read (nin,*) itol
      latol = 1
      lrtol = 1
      If (itol>2) Then
        latol = neqn
      End If
      If (mod(itol,2)==0) Then
        lrtol = neqn
      End If
      Allocate (atol(latol),rtol(lrtol))
      Read (nin,*) atol(1:latol), rtol(1:lrtol)
!     Set break-points
      Do i = 1, nbkpts
        xbkpts(i) = real(i-1,kind=nag_wp)/real(nbkpts-1,kind=nag_wp)
      End Do

      Read (nin,*) xi(1:nxi)
      Read (nin,*) norm, laopt
      ind = 0
      itask = 1

!     Set theta to .TRUE. if the Theta integrator is required

      theta = .False.
      algopt(1:30) = 0.0E0_nag_wp
      If (theta) Then
        algopt(1) = 2.0E0_nag_wp
      End If
      ruser(1) = 1.0E-4_nag_wp

      Write (nout,99998)
      Write (nout,99997) atol(1)
      Write (nout,99996) npoly
      Write (nout,99995) nel
      Write (nout,99994) npts
      Write (nout,99999)

      rwsav(1:1100) = 0.0_nag_wp
!     Output value solution at t = 0.1*(2**k) for k=1,2,...,5

      tout = 0.1E0_nag_wp
      Do it = 1, 5
        tout = tout + tout

!       ifail: behaviour on error exit
!              =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
        ifail = 0
        Call d03pja(npde,m,ruser(1),tout,pdedef,bndary,u,nbkpts,xbkpts,npoly,  &
          npts,x,nv,odedef,nxi,xi,neqn,uvinit,rtol,atol,itol,norm,laopt,       &
          algopt,w,nw,iw,niw,itask,itrace,ind,iuser,ruser,cwsav,lwsav,iwsav,   &
          rwsav,ifail)

        ts = ruser(1)
        If (abs(u(1))<10.0_nag_wp) Then
          Write (nout,99993) ts, u(1)
        Else
          Write (nout,99992) ts, u(1)
        End If
      End Do

!     Print integration statistics (reasonably rounded)
      If (print_stat) Then
        Write (nout,*)
        Write (nout,99991) 'time steps', iw(1)
        Write (nout,99991) 'function evaluations', iw(2)
        Write (nout,99991) 'Jacobian evaluations', iw(3)
        Write (nout,99991) 'iterations', iw(5)
      End If

99999 Format (3X,'time',8X,'solution at x=0')
99998 Format (/,/,' Simple coupled PDE using BDF')
99997 Format (' Local Accuracy        = ',1P,E12.3)
99996 Format (' Degree of Polynomial  = ',I4)
99995 Format (' Number of elements    = ',I4)
99994 Format (' Number of mesh points = ',I4,/)
99993 Format (1X,F6.1,10X,F7.2)
99992 Format (1X,F6.1,10X,F6.1)
99991 Format (' Number of ',A20,' = ',I6)
    End Program d03pjae