complex_step_test Subroutine

public subroutine complex_step_test()

Unit test for the complex_step module.

Arguments

None

Calls

proc~~complex_step_test~~CallsGraph proc~complex_step_test complex_step_module::complex_step_test proc~central_diff complex_step_module::central_diff proc~complex_step_test->proc~central_diff proc~central_diff_4 complex_step_module::central_diff_4 proc~complex_step_test->proc~central_diff_4 proc~complex_step_derivative complex_step_module::complex_step_derivative proc~complex_step_test->proc~complex_step_derivative proc~forward_diff complex_step_module::forward_diff proc~complex_step_test->proc~forward_diff

Source Code

    subroutine complex_step_test()

    implicit none

    integer     :: i
    complex(wp) :: x
    real(wp)    :: h,dfdx,dfdx2,dfdx3,dfdx4,err,err2,err3,err4

    write(*,*) ''
    write(*,*) '---------------'
    write(*,*) ' complex_step_test'
    write(*,*) '---------------'
    write(*,*) ''

    x = cmplx(2.0_wp,0.0_wp,wp)
    h = 1.0e-10_wp

    call complex_step_derivative(test_func,x,h,dfdx)

    !write(*,*) ''
    !write(*,*) 'x     :',x
    !write(*,*) 'dfdx  :',dfdx
    !write(*,*) 'error :',real(test_deriv(x),wp) - dfdx
    !write(*,*) ''

    write(*,'(*(A30))') 'h', 'forward diff err', 'central diff err', 'central diff 4 err', 'complex step err'

    do i=1,200
        h = 10.0_wp**(-i/10.0_wp)
        call complex_step_derivative(test_func,x,h,dfdx)
        call forward_diff(test_func,x,h,dfdx2)
        call central_diff(test_func,x,h,dfdx3)
        call central_diff_4(test_func,x,h,dfdx4)
        err  = real(test_deriv(x),wp) - dfdx
        err2 = real(test_deriv(x),wp) - dfdx2
        err3 = real(test_deriv(x),wp) - dfdx3
        err4 = real(test_deriv(x),wp) - dfdx4
        write(*,'(*(E30.16,1X))') h, err2, err3, err4, err
    end do

    contains
!*****************************************************************************************

    !****************************************
        function test_func(x) result(f)

        implicit none

        complex(wp),intent(in) :: x
        complex(wp) :: f

        f = exp(x) + sin(x)

        end function test_func
    !****************************************

    !****************************************
        function test_deriv(x) result(f)

        implicit none

        complex(wp),intent(in) :: x
        complex(wp) :: f

        f = exp(x) + cos(x)

        end function test_deriv
    !****************************************

    end subroutine complex_step_test