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

NAG AD Library Introduction
Example description
!   D02PT_P0W_F Example Program Text
!   Mark 30.0 Release. NAG Copyright 2024.

    Module d02pt_p0w_fe_mod

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

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Accessibility Statements ..
      Private
      Public                           :: f
!     .. Parameters ..
      Integer, Parameter, Public       :: liwsav = 130, n = 2, nin = 5,        &
                                          nout = 6, npts = 8
      Integer, Parameter, Public       :: lrwsav = 350 + 32*n
    Contains

      Subroutine f(ad_handle,t,n,y,yp,iuser,ruser)
!       .. Implicit None Statement ..
        Implicit None
!       .. Scalar Arguments ..
        Type (c_ptr), Intent (Inout)   :: ad_handle
        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(*)
!       .. Executable Statements ..
        yp(1) = y(2)
        yp(2) = -y(1)
        Return
      End Subroutine f
    End Module d02pt_p0w_fe_mod

    Program d02pt_p0w_fe

!     D02PT_P0W_F Example Main Program

!     .. Use Statements ..
      Use d02pt_p0w_fe_mod, Only: f, liwsav, lrwsav, n, nin, nout
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: d02pf_p0w_f, d02pq_p0w_f, d02pt_p0w_f
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Type (c_ptr)                     :: ad_handle
      Real (Kind=nag_wp)               :: hnext, hstart, tend, tnow, tol,      &
                                          tstart, waste
      Integer                          :: fevals, ifail, method, stepcost,     &
                                          stepsok
!     .. Local Arrays ..
      Real (Kind=nag_wp)               :: ruser(1), thresh(n), y(n), yinit(n), &
                                          ynow(n), ypnow(n)
      Real (Kind=nag_wp), Allocatable  :: rwsav(:)
      Integer                          :: iuser(1)
      Integer, Allocatable             :: iwsav(:)
!     .. Executable Statements ..
      Write (nout,*) 'D02PT_P0W_F Example Program Results'

      Allocate (iwsav(liwsav),rwsav(lrwsav))

!     Set initial conditions and input

!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) method
      Read (nin,*) tstart, tend
      Read (nin,*) yinit(1:n)
      Read (nin,*) hstart
      Read (nin,*) thresh(1:n)

!     Set control for output

      tol = 1.0E-4_nag_wp
      y(1:n) = yinit(1:n)

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

      Write (nout,99999) tol
      Write (nout,99998)
      Write (nout,99997) tstart, yinit(1:n)
loop: Do
        ifail = 0
        Call d02pf_p0w_f(ad_handle,f,n,tnow,ynow,ypnow,iuser,ruser,iwsav,      &
          rwsav,ifail)

        Write (nout,99997) tnow, ynow(1:n)
        If (tnow>=tend) Then
          Exit loop
        End If
      End Do loop

      ifail = 0
      Call d02pt_p0w_f(ad_handle,fevals,stepcost,waste,stepsok,hnext,iwsav,    &
        rwsav,ifail)
      Write (nout,99996) fevals

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


    End Program d02pt_p0w_fe