NAG Library Manual, Mark 30.1
Interfaces:  FL   CL   CPP   AD 

NAG FL Interface Introduction
Example description
!   D02PRF Example Program Text
!   Mark 30.1 Release. NAG Copyright 2024.

    Module d02prfe_mod

!     D02PRF 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                           :: f
!     .. Parameters ..
      Real (Kind=nag_wp), Parameter, Public :: tol0 = 1.0E-4_nag_wp
      Integer, Parameter, Public       :: n = 4, nin = 5, nout = 6, npts = 6
      Integer, Parameter, Public       :: lrwsav = 350 + 32*n
    Contains
      Subroutine f(t,n,y,yp,iuser,ruser)

!       .. Scalar Arguments ..
        Real (Kind=nag_wp), Intent (In) :: t
        Integer, Intent (In)           :: n
!       .. Array Arguments ..
        Real (Kind=nag_wp), Intent (Inout) :: ruser(*)
        Real (Kind=nag_wp), Intent (In) :: y(n)
        Real (Kind=nag_wp), Intent (Out) :: yp(n)
        Integer, Intent (Inout)        :: iuser(*)
!       .. Local Scalars ..
        Real (Kind=nag_wp)             :: r
!       .. Intrinsic Procedures ..
        Intrinsic                      :: sqrt
!       .. Executable Statements ..
        r = sqrt(y(1)**2+y(2)**2)
        yp(1) = y(3)
        yp(2) = y(4)
        yp(3) = -y(1)/r**3
        yp(4) = -y(2)/r**3
        Return
      End Subroutine f
    End Module d02prfe_mod

    Program d02prfe

!     D02PRF Example Main Program

!     .. Use Statements ..
      Use d02prfe_mod, Only: f, lrwsav, n, nin, nout, npts, tol0
      Use nag_library, Only: d02pff, d02pqf, d02prf, d02ptf, nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Real (Kind=nag_wp)               :: hnext, hstart, tendnu, tfinal, tinc, &
                                          tnow, tol, tstart, waste
      Integer                          :: fevals, i, ifail, method, stepcost,  &
                                          stepsok
!     .. Local Arrays ..
      Real (Kind=nag_wp)               :: ruser(1)
      Real (Kind=nag_wp), Allocatable  :: rwsav(:), thresh(:), yinit(:),       &
                                          ynow(:), ypnow(:)
      Integer                          :: iuser(1)
      Integer, Allocatable             :: iwsav(:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: real
!     .. Executable Statements ..
      Write (nout,*) 'D02PRF Example Program Results'
!     Skip heading in data file
      Read (nin,*)
!     n: number of differential equations
      Allocate (thresh(n),iwsav(130),rwsav(lrwsav),ynow(n),ypnow(n),yinit(n))

!     Set initial conditions and input for D02PQF

      Read (nin,*) method
      Read (nin,*) tstart, tfinal
      Read (nin,*) yinit(1:n)
      Read (nin,*) hstart
      Read (nin,*) thresh(1:n)

!     Set output control

      tinc = (tfinal-tstart)/real(npts,kind=nag_wp)

      tol = 10.0_nag_wp*tol0
      Do i = 1, 2
        tol = tol*0.1_nag_wp

        tendnu = tstart + tinc

!       ifail: behaviour on error exit
!              =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
        ifail = 0
        Call d02pqf(n,tstart,tendnu,yinit,tol,thresh,method,hstart,iwsav,      &
          rwsav,ifail)

        Write (nout,99999) tol
        Write (nout,99998)
        Write (nout,99997) tstart, yinit(1:n)

tstep:  Do
          ifail = 0
          Call d02pff(f,n,tnow,ynow,ypnow,iuser,ruser,iwsav,rwsav,ifail)

          If (tnow==tendnu) Then
            Write (nout,99997) tnow, ynow(1:n)

            If (tnow>=tfinal) Then
              Exit tstep
            End If
            tendnu = tendnu + tinc
            Call d02prf(tendnu,iwsav,rwsav,ifail)
          End If

        End Do tstep

        ifail = 0
        Call d02ptf(fevals,stepcost,waste,stepsok,hnext,iwsav,rwsav,ifail)
        Write (nout,99996) fevals
      End Do

99999 Format (/,' Calculation with TOL = ',1P,E8.1)
99998 Format (/,'    t         y1         y2         y3         y4',/)
99997 Format (1X,F6.3,4(3X,F8.4))
99996 Format (/,' Cost of the integration in evaluations of F is',I6)

    End Program d02prfe