LCOV - code coverage report
Current view: top level - num/test/src - test_support.f90 (source / functions) Coverage Total Hit
Test: coverage.info Lines: 81.7 % 273 223
Test Date: 2026-12-03 14:59:23 Functions: 100.0 % 18 18

            Line data    Source code
       1              : module test_support
       2              : 
       3              :    use num_def
       4              :    use num_lib
       5              :    use math_lib
       6              :    use const_def, only: dp, arg_not_provided
       7              :    use utils_lib, only: mesa_error
       8              : 
       9              :    implicit none
      10              : 
      11              : contains
      12              : 
      13           31 :    subroutine show_results(nv, y, expect, show_all)
      14              :       integer, intent(in) :: nv
      15              :       real(dp), dimension(nv), intent(in) :: y, expect
      16              :       logical, intent(in) :: show_all
      17              :       integer :: i
      18              :       include 'formats'
      19           31 :       if (show_all) then
      20            0 :          do i = 1, nv
      21            0 :             write (*, 2) 'calculated', i, y(i)
      22              :          end do
      23            0 :          do i = 1, nv
      24            0 :             write (*, 2) 'reference', i, expect(i)
      25            0 :             write (*, 2) 'lg(abs rel diff)', i, &
      26            0 :                log10(abs(y(i) - expect(i))/max(1d-299, abs(expect(i))))
      27              :          end do
      28              :       else
      29          221 :          do i = 1, nv
      30          221 :             write (*, 2) 'calculated', i, y(i)
      31              :          end do
      32          221 :          do i = 1, nv
      33          221 :             write (*, 2) 'reference', i, expect(i)
      34              :          end do
      35              :       end if
      36           31 :       write (*, *)
      37           31 :    end subroutine show_results
      38              : 
      39           29 :    subroutine show_statistics(nfcn, njac, nstep, show_all)
      40              :       integer, intent(in) :: nfcn, njac, nstep
      41              :       logical, intent(in) :: show_all
      42           29 :       if (.not. show_all) return
      43            0 :       write (*, *) 'number of steps         ', nstep
      44            0 :       write (*, *) 'number of function evals', nfcn
      45            0 :       write (*, *) 'number of jacobians     ', njac
      46            0 :       write (*, *) 'functions + jacobians   ', nfcn + njac
      47            0 :       write (*, *)
      48              :    end subroutine show_statistics
      49              : 
      50            8 :    subroutine check_results(nv, y, expect, tol, ierr)
      51              :       integer, intent(in) :: nv
      52              :       real(dp), dimension(nv), intent(in) :: y, expect
      53              :       real(dp), intent(in) :: tol
      54              :       integer, intent(out) :: ierr
      55              :       integer :: i
      56              :       logical :: okay
      57              :       include 'formats'
      58            8 :       okay = .true.
      59            8 :       ierr = 0
      60         3208 :       do i = 1, nv
      61         3200 :          if (abs(expect(i)) < tol) cycle
      62         1560 :          if (abs(y(i) - expect(i)) > tol) then
      63            0 :             write (*, 2) 'check results result#', i
      64            0 :             write (*, 1) 'log10 abs diff', log10(abs(y(i) - expect(i)))
      65            0 :             write (*, 1) 'y(i)', y(i)
      66            0 :             write (*, 1) 'expect(i)', expect(i)
      67            0 :             write (*, 1) 'log10 tol', log10(tol)
      68            0 :             write (*, *)
      69            0 :             ierr = -1
      70            0 :             okay = .false.
      71              :          end if
      72              :       end do
      73            8 :       if (okay) return
      74            0 :       write (*, *)
      75            0 :       do i = 1, nv
      76            0 :          write (*, 2) 'y expected', i, y(i), expect(i)
      77              :       end do
      78            0 :       write (*, *)
      79            0 :       call mesa_error(__FILE__, __LINE__)
      80              :       return
      81              :    end subroutine check_results
      82              : 
      83            7 :    real(dp) function f(x, dfdx, lrpar, rpar, lipar, ipar, ierr)
      84              :       integer, intent(in) :: lrpar, lipar
      85              :       real(dp), intent(in) :: x
      86              :       real(dp), intent(out) :: dfdx
      87              :       integer, intent(inout), pointer :: ipar(:)  ! (lipar)
      88              :       real(dp), intent(inout), pointer :: rpar(:)  ! (lrpar)
      89              :       integer, intent(out) :: ierr
      90            7 :       ierr = 0
      91            7 :       f = x - 3*sin(1 - x)
      92            7 :       dfdx = 1 + 3*cos(1 - x)
      93            7 :    end function f
      94              : 
      95            1 :    subroutine test_root_with_brackets
      96              :       integer, parameter :: lrpar = 0, lipar = 0
      97              :       real(dp) :: x, dfdx
      98              :       real(dp) :: x1, x3  ! bounds for x
      99              :       ! values of f at x1 and x3 must have opposite sign
     100              :       ! return value for safe_root will be bracketed by x1 and x3
     101              :       real(dp) :: y1, y3  ! f(x1) and f(x3)
     102              :       integer :: imax  ! max number of iterations for search
     103              :       real(dp) :: epsx, epsy
     104              :       ! stop searching when x is determined to within epsx
     105              :       ! or when abs(f(x)) is less than epsy
     106              :       integer :: ierr
     107              :       real(dp), parameter :: expected_root = 0.74800611d0
     108              :       real(dp), target :: rpar_ary(lrpar)
     109              :       integer, target :: ipar_ary(lipar)
     110              :       integer, pointer :: ipar(:)  ! (lipar)
     111              :       real(dp), pointer :: rpar(:)  ! (lrpar)
     112              :       include 'formats'
     113            1 :       ipar => ipar_ary
     114            1 :       rpar => rpar_ary
     115              :       ierr = 0
     116            1 :       imax = 100
     117            1 :       x1 = 0
     118            1 :       x3 = 10
     119            1 :       y1 = f(x1, dfdx, lrpar, rpar, lipar, ipar, ierr)
     120            1 :       y3 = f(x3, dfdx, lrpar, rpar, lipar, ipar, ierr)
     121            1 :       epsx = 1d-6
     122            1 :       epsy = 1d-6
     123              :       x = safe_root_with_brackets( &
     124            1 :           f, x1, x3, y1, y3, imax, epsx, epsy, lrpar, rpar, lipar, ipar, ierr)
     125            1 :       if (abs(x - expected_root) > 1d-6) call mesa_error(__FILE__, __LINE__)
     126            1 :       write (*, 1) 'root', x
     127            1 :    end subroutine test_root_with_brackets
     128              : 
     129           31 :    real(dp) function test_f(x, dfdx, lrpar, rpar, lipar, ipar, ierr)
     130              :       ! returns with ierr = 0 if was able to evaluate f and df/dx at x
     131              :       real(dp), intent(in) :: x
     132              :       real(dp), intent(out) :: dfdx
     133              :       integer, intent(in) :: lipar, lrpar
     134              :       integer, intent(inout), pointer :: ipar(:)  ! (lipar)
     135              :       real(dp), intent(inout), pointer :: rpar(:)  ! (lrpar)
     136              :       integer, intent(out) :: ierr
     137           31 :       test_f = tanh(x) - 0.4621171572600098d0
     138           31 :       dfdx = 1/cosh(x)**2
     139           31 :       ierr = 0
     140           31 :    end function test_f
     141              : 
     142            1 :    subroutine test_root2
     143              :       real(dp) :: x  ! provide starting guess on input
     144              :       real(dp) :: x1, x3  ! bounds for x
     145              :       real(dp) :: y1, y3  ! f(x1) and f(x3)
     146              :       integer, parameter :: imax = 50, lipar = 0, lrpar = 0
     147              :       real(dp) :: dx
     148              :       real(dp), parameter :: epsx = 1d-10, epsy = 1d-10
     149              :       integer :: ierr
     150              :       real(dp), target :: rpar_ary(lrpar)
     151              :       integer, target :: ipar_ary(lipar)
     152              :       integer, pointer :: ipar(:)  ! (lipar)
     153              :       real(dp), pointer :: rpar(:)  ! (lrpar)
     154              :       include 'formats'
     155            1 :       ipar => ipar_ary
     156            1 :       rpar => rpar_ary
     157            1 :       x = -1d0
     158            1 :       dx = 0.1d0
     159              :       ierr = 0
     160            1 :       write (*, *) 'test_root2'
     161            1 :       call look_for_brackets(x, dx, x1, x3, test_f, y1, y3, imax, lrpar, rpar, lipar, ipar, ierr)
     162            1 :       if (ierr /= 0) call mesa_error(__FILE__, __LINE__)
     163            1 :       write (*, 1) 'x1', x1
     164            1 :       write (*, 1) 'x3', x3
     165            1 :       write (*, 1) 'y1', y1
     166            1 :       write (*, 1) 'y3', y3
     167              :       x = safe_root_with_brackets( &
     168            1 :           test_f, x1, x3, y1, y3, imax, epsx, epsy, lrpar, rpar, lipar, ipar, ierr)
     169            1 :       if (ierr /= 0) call mesa_error(__FILE__, __LINE__)
     170            1 :       write (*, 1) 'safe_root', x
     171            1 :       write (*, *)
     172            1 :    end subroutine test_root2
     173              : 
     174            1 :    subroutine test_root3
     175              :       real(dp) :: x  ! provide starting guess on input
     176              :       real(dp) :: x1, x3  ! bounds for x
     177              :       real(dp) :: y1, y3  ! f(x1) and f(x3)
     178              :       integer, parameter :: newt_imax = 10, imax = 50, lipar = 0, lrpar = 0
     179              :       real(dp) :: dx
     180              :       real(dp), parameter :: epsx = 1d-10, epsy = 1d-10
     181              :       integer :: ierr
     182              :       real(dp), target :: rpar_ary(lrpar)
     183              :       integer, target :: ipar_ary(lipar)
     184              :       integer, pointer :: ipar(:)  ! (lipar)
     185              :       real(dp), pointer :: rpar(:)  ! (lrpar)
     186              :       include 'formats'
     187            1 :       ipar => ipar_ary
     188            1 :       rpar => rpar_ary
     189            1 :       dx = 0.1d0
     190            1 :       x1 = arg_not_provided
     191            1 :       x3 = arg_not_provided
     192            1 :       y1 = arg_not_provided
     193            1 :       y3 = arg_not_provided
     194              :       ierr = 0
     195            1 :       write (*, *) 'test_root3'
     196            1 :       x = 0.1d0  ! not too bad initial guess.  newton should find it okay.
     197              :       x = safe_root_with_guess( &
     198              :           test_f, x, dx, x1, x3, y1, y3, &
     199            1 :           newt_imax, imax, epsx, epsy, lrpar, rpar, lipar, ipar, ierr)
     200            1 :       if (ierr /= 0) call mesa_error(__FILE__, __LINE__)
     201            1 :       write (*, 1) 'first safe_root_with_guess', x
     202            1 :       x = -1d0  ! really bad guess will make it give up on newton
     203              :       x = safe_root_with_guess( &
     204              :           test_f, x, dx, x1, x3, y1, y3, &
     205            1 :           newt_imax, imax, epsx, epsy, lrpar, rpar, lipar, ipar, ierr)
     206            1 :       if (ierr /= 0) call mesa_error(__FILE__, __LINE__)
     207            1 :       write (*, 1) 'second safe_root_with_guess', x
     208            1 :       write (*, *)
     209            1 :    end subroutine test_root3
     210              : 
     211            1 :    subroutine test_root_no_brackets
     212              :       ! any imax that will fail here (just need to keep it below 31 so
     213              :       ! we don't overflow)
     214              :       integer, parameter :: imax = 3, lipar = 0, lrpar = 0
     215              :       real(dp) :: x1, x3, y1, y3
     216              :       integer :: ierr
     217              :       real(dp), target :: rpar_ary(lrpar)
     218              :       integer, target :: ipar_ary(lipar)
     219              :       real(dp), pointer :: rpar(:)
     220              :       integer, pointer :: ipar(:)
     221              : 
     222            1 :       ipar => ipar_ary
     223            1 :       rpar => rpar_ary
     224              : 
     225              :       ! Function evaluations succeed, but x*x + 1 has no real root.
     226              :       call look_for_brackets(0d0, 1d0, x1, x3, no_root_f, y1, y3, &
     227            1 :          imax, lrpar, rpar, lipar, ipar, ierr)
     228              : 
     229              :       write (*, '(a, i4, a, i4)') &
     230            1 :          'look_for_brackets no root expected ierr', -1, ' got', ierr
     231              : 
     232              :    contains
     233              : 
     234            4 :       real(dp) function no_root_f(x, dfdx, lrpar, rpar, lipar, ipar, ierr)
     235              :          real(dp), intent(in) :: x
     236              :          real(dp), intent(out) :: dfdx
     237              :          integer, intent(in) :: lrpar, lipar
     238              :          real(dp), intent(inout), pointer :: rpar(:)
     239              :          integer, intent(inout), pointer :: ipar(:)
     240              :          integer, intent(out) :: ierr
     241              : 
     242              :          ! this has no real roots
     243            4 :          no_root_f = x*x + 1d0
     244            4 :          dfdx = 2d0*x
     245            4 :          ierr = 0
     246            4 :       end function no_root_f
     247              : 
     248              :    end subroutine test_root_no_brackets
     249              : 
     250        17231 :    subroutine van_der_Pol_derivs(n, x, h, y, f, lrpar, rpar, lipar, ipar, ierr)
     251              :       integer, intent(in) :: n, lrpar, lipar
     252              :       real(dp), intent(in) :: x, h
     253              :       real(dp), intent(inout) :: y(:)  ! (n)
     254              :       real(dp), intent(inout) :: f(:)  ! (n)
     255              :       integer, intent(inout), pointer :: ipar(:)  ! (lipar)
     256              :       real(dp), intent(inout), pointer :: rpar(:)  ! (lrpar)
     257              :       integer, intent(out) :: ierr  ! nonzero means retry with smaller timestep.
     258              :       include 'formats'
     259        17231 :       ierr = 0
     260        17231 :       f(1) = y(2)
     261        17231 :       f(2) = ((1 - y(1)*y(1))*y(2) - y(1))/rpar(1)
     262              :       ! the derivatives do not depend on x
     263        17231 :    end subroutine van_der_Pol_derivs
     264              : 
     265         1842 :    subroutine solout(nr, xold, x, n, y, rwork, iwork, interp_y, lrpar, rpar, lipar, ipar, irtrn)
     266              :       ! nr is the step number.
     267              :       ! x is the current x value; xold is the previous x value.
     268              :       ! y is the current y value.
     269              :       ! irtrn negative means terminate integration.
     270              :       ! rwork and iwork hold info for
     271              :       integer, intent(in) :: nr, n, lrpar, lipar
     272              :       real(dp), intent(in) :: xold, x
     273              :       real(dp), intent(inout) :: y(:)  ! (n)
     274              :       real(dp), intent(inout), target :: rwork(*)
     275              :       integer, intent(inout), target :: iwork(*)
     276              :       integer, intent(inout), pointer :: ipar(:)  ! (lipar)
     277              :       real(dp), intent(inout), pointer :: rpar(:)  ! (lrpar)
     278              :       interface
     279              :          ! this subroutine can be called from your solout routine.
     280              :          ! it computes interpolated values for y components during the just completed step.
     281              :          real(dp) function interp_y(i, s, rwork, iwork, ierr)
     282              :             use const_def, only: dp
     283              :             implicit none
     284              :             integer, intent(in) :: i  ! result is interpolated approximation of y(i) at x=s.
     285              :             real(dp), intent(in) :: s  ! interpolation x value (between xold and x).
     286              :             real(dp), intent(inout), target :: rwork(*)
     287              :             integer, intent(inout), target :: iwork(*)
     288              :             integer, intent(out) :: ierr
     289              :          end function interp_y
     290              :       end interface
     291              :       integer, intent(out) :: irtrn
     292              :       ! --- prints solution at equidistant output-points
     293              :       ! --- by using "contd8", the continuous collocation solution
     294              :       real(dp) :: xout, y1, y2
     295              :       integer :: ierr
     296         1842 :       xout = rpar(2)
     297         1842 :       irtrn = 1
     298         1842 :       if (ipar(1) /= 1) return  ! no output
     299              : 
     300            0 :       if (nr == 1) then
     301            0 :          write (6, 99) x, y(1), y(2), nr - 1
     302            0 :          xout = x + 0.2d0
     303              :       else
     304            0 :          do
     305            0 :             if (x >= xout - 1d-10) then
     306              :                ierr = 0
     307            0 :                y1 = interp_y(1, xout, rwork, iwork, ierr)
     308            0 :                if (ierr /= 0) call mesa_error(__FILE__, __LINE__)
     309            0 :                y2 = interp_y(2, xout, rwork, iwork, ierr)
     310            0 :                if (ierr /= 0) call mesa_error(__FILE__, __LINE__)
     311            0 :                write (6, 99) xout, y1, y2, nr - 1
     312            0 :                xout = xout + 0.2d0
     313              :                cycle
     314              :             end if
     315              :             exit
     316              :          end do
     317              :       end if
     318              : 99    format(1x, 'x =', f5.2, '    y =', 2e18.10, '    nstep =', i6)
     319            0 :       rpar(2) = xout
     320              :    end subroutine solout
     321              : 
     322            2 :    subroutine test_dopri(do_853, show_all)
     323              :       logical, intent(in) :: do_853, show_all
     324              :       integer, parameter :: nv = 2  ! the number of variables in the van der Pol system of ODEs
     325              :       real(dp), parameter :: eps = 1d-3  ! stiffness parameter for van der Pol
     326              :       real(dp) :: rtol(1)  ! relative error tolerance(s)
     327              :       real(dp) :: atol(1)  ! absolute error tolerance(s)
     328              :       real(dp) :: x  ! starting value for the interval of integration
     329              :       real(dp) :: xend  ! ending value for the interval of integration
     330              :       real(dp) :: expect(nv)
     331              :       integer, parameter :: lrpar = 2, lipar = 1, nrdens = nv
     332              :       integer, parameter :: liwork = nrdens + 100, lwork = 11*nv + 8*nrdens + 100
     333              :       real(dp) :: h, max_step_size
     334              :       integer :: lout, iout, idid, itol, j
     335              :       integer :: check_liwork, check_lwork, max_steps, ierr
     336              :       real(dp), target :: y_ary(nv)
     337              :       real(dp), pointer :: y(:)
     338              :       real(dp), target :: rpar_ary(lrpar), work_ary(lwork)
     339              :       integer, target :: ipar_ary(lipar), iwork_ary(liwork)
     340              :       integer, pointer :: ipar(:)  ! (lipar)
     341              :       real(dp), pointer :: rpar(:)  ! (lrpar)
     342              :       integer, pointer :: iwork(:)
     343              :       real(dp), pointer :: work(:)
     344              : 
     345            2 :       ipar => ipar_ary
     346            2 :       rpar => rpar_ary
     347            2 :       work => work_ary
     348            2 :       iwork => iwork_ary
     349            2 :       y => y_ary
     350              : 
     351            2 :       write (*, *)
     352            2 :       write (*, *) 'vdpol'
     353            2 :       if (do_853) then
     354            1 :          write (*, *) 'dop853'
     355              :       else
     356            1 :          write (*, *) 'dopri5'
     357              :       end if
     358              : 
     359            2 :       x = 0
     360            2 :       xend = 2.0
     361            2 :       y(1) = 2
     362            2 :       y(2) = 0
     363              : 
     364            2 :       lout = 0
     365            2 :       max_steps = 0
     366            2 :       max_step_size = 9
     367              : 
     368            2 :       itol = 0  ! scalar tolerances
     369            2 :       iout = 2  ! want dense output
     370              : 
     371            2 :       rtol(1) = 1d-4
     372            2 :       atol(1) = 1d-4
     373            2 :       h = 1d-6
     374              : 
     375            2 :       rpar(1) = eps
     376            2 :       rpar(2) = 0
     377            2 :       if (show_all) then
     378            0 :          ipar(1) = 1
     379              :       else
     380            2 :          ipar(1) = 0
     381              :       end if
     382              : 
     383          206 :       iwork = 0
     384          278 :       work = 0
     385              : 
     386            2 :       iwork(5) = nrdens  ! want dense output for all components
     387            2 :       iwork(4) = 1  ! test for stiffness at each step
     388              : 
     389            2 :       if (do_853) then
     390            1 :          call dopri5_work_sizes(nv, nrdens, check_liwork, check_lwork)
     391              :       else
     392            1 :          call dop853_work_sizes(nv, nrdens, check_liwork, check_lwork)
     393              :       end if
     394              : 
     395            2 :       if (check_liwork > liwork .or. check_lwork > lwork) then
     396            0 :          write (*, *) 'need to enlarge work arrays for dopri5'
     397            0 :          call mesa_error(__FILE__, __LINE__)
     398              :       end if
     399              : 
     400            2 :       ierr = 0
     401            2 :       if (do_853) then
     402              :          call dop853( &
     403              :             nv, van_der_Pol_derivs, x, y, xend, &
     404              :             h, max_step_size, max_steps, &
     405              :             rtol, atol, itol, &
     406              :             solout, iout, work, lwork, iwork, liwork, &
     407            1 :             lrpar, rpar, lipar, ipar, lout, idid)
     408              :       else
     409              :          call dopri5( &
     410              :             nv, van_der_Pol_derivs, x, y, xend, &
     411              :             h, max_step_size, max_steps, &
     412              :             rtol, atol, itol, &
     413              :             solout, iout, work, lwork, iwork, liwork, &
     414            1 :             lrpar, rpar, lipar, ipar, lout, idid)
     415              :       end if
     416              : 
     417            2 :       if (idid /= 1) then  ! trouble
     418            0 :          write (*, *) 'idid', idid
     419            0 :          call mesa_error(__FILE__, __LINE__)
     420              :       end if
     421              : 
     422            2 :       expect(1:2) = [1.7632345401889102d+00, -8.3568868191466206d-01]
     423              : 
     424            2 :       call show_results(nv, y, expect, show_all)
     425            2 :       if (.not. show_all) return
     426              : 
     427              :       ! typical: fcn=   21530     step=  1468     accpt=  1345     rejct=  122
     428            0 :       write (6, 91) (iwork(j), j=17, 20)
     429              : 91    format(' fcn=', i8, '     step=', i6, '     accpt=', i6, '     rejct=', i5)
     430              : 
     431            0 :       write (*, *)
     432              : 
     433              :    end subroutine test_dopri
     434              : 
     435            1 :    subroutine test_binary_search
     436              :       integer, parameter :: n = 100
     437              :       integer :: k, loc(3)
     438              : 
     439              :       real(dp) :: val(3)
     440              :       real(dp), target :: vec_ary(n)
     441              :       real(dp), pointer :: vec(:)
     442              :       include 'formats'
     443            1 :       vec => vec_ary
     444              : 
     445          101 :       do k = 1, n
     446          101 :          vec(k) = dble(k)*dble(k)
     447              :       end do
     448              : 
     449            1 :       write (*, *)
     450            1 :       write (*, *) 'binary_search, increasing values'
     451              : 
     452              :       loc = -1
     453            4 :       val = [0d0, FLOOR(n/3d0)**2 + 2d0, vec(n) + 1d0]
     454            4 :       do k = 1, 3
     455            3 :          loc(k) = binary_search(n, vec, 0, val(k))
     456            3 :          if (loc(k) == 0 .and. val(k) < vec(1)) then
     457            1 :             write (*, 2) 'val is less than vec(1):', k, val(k), vec(1)
     458            2 :          else if (loc(k) == n .and. val(k) > vec(n)) then
     459            1 :             write (*, 1) 'val is greater than vec(n):', val(k), vec(n)
     460            1 :          else if (vec(loc(k)) <= val(k) .and. val(k) < vec(loc(k) + 1)) then
     461            1 :             write (*, 1) 'vec(result)', vec(loc(k))
     462            1 :             write (*, 1) 'val', val(k)
     463            1 :             write (*, 1) 'vec(result+1)', vec(loc(k) + 1)
     464              :          else
     465            0 :             write (*, *) 'binary_search failed for increasing-value array'
     466            0 :             call mesa_error(__FILE__, __LINE__)
     467              :          end if
     468            4 :          write (*, *) 'okay'
     469              :       end do
     470              : 
     471              :       ! test decreasing values
     472              :       loc = -1
     473          101 :       where (vec /= 0d0) vec = -vec
     474            4 :       where (val /= 0d0) val = -val
     475              : 
     476            1 :       write (*, *)
     477            1 :       write (*, *) 'binary_search, decreasing values'
     478            4 :       do k = 1, 3
     479            3 :          loc(k) = binary_search(n, vec, 0, val(k))
     480            3 :          if (loc(k) == 0 .and. val(k) > vec(1)) then
     481            1 :             write (*, 2) 'val is greater than vec(n):', k, val(k), vec(1)
     482            2 :          else if (loc(k) == n .and. val(k) < vec(n)) then
     483            1 :             write (*, 1) 'val is less than vec(1):', val(k), vec(n)
     484            1 :          else if (vec(loc(k)) >= val(k) .and. val(k) > vec(loc(k) + 1)) then
     485            1 :             write (*, 1) 'vec(result+1)', vec(loc(k) + 1)
     486            1 :             write (*, 1) 'val', val(k)
     487            1 :             write (*, 1) 'vec(result)', vec(loc(k))
     488              :          else
     489            0 :             write (*, *) 'binary_search failed for decreasing-value array'
     490            0 :             call mesa_error(__FILE__, __LINE__)
     491              :          end if
     492            4 :          write (*, *) 'okay'
     493              :       end do
     494            1 :       write (*, *)
     495              : 
     496            1 :    end subroutine test_binary_search
     497              : 
     498            1 :    subroutine test_qsort
     499              :       use const_def
     500              :       integer, parameter :: n = 100
     501              :       integer :: ord(n), i
     502              :       real(dp) :: a(n)
     503              :       include 'formats'
     504            1 :       write (*, *)
     505            1 :       write (*, *) 'qsort into increasing order'
     506          101 :       do i = 1, n
     507          101 :          a(i) = sinpi(2.1*dble(i)/dble(n))
     508              :       end do
     509            1 :       call qsort(ord, n, a)
     510          101 :       do i = 1, n
     511          101 :          write (*, 3) 'ord, a(ord)', i, ord(i), a(ord(i))
     512              :       end do
     513            1 :       write (*, *)
     514            1 :    end subroutine test_qsort
     515              : 
     516            8 :    real(dp) function g(x) result(y)
     517              :       real(dp), intent(in) :: x
     518            8 :       y = (x - 3)*(x - 8)
     519            8 :    end function g
     520              : 
     521            1 :    subroutine test_find0_quadratic
     522              :       real(dp) :: xx1, yy1, xx2, yy2, xx3, yy3, x, y
     523              :       integer :: ierr
     524              :       include 'formats'
     525            1 :       write (*, *) 'test_find0_quadratic'
     526            1 :       xx1 = 1
     527            1 :       yy1 = g(xx1)
     528            1 :       xx2 = 2
     529            1 :       yy2 = g(xx2)
     530            1 :       xx3 = 4
     531            1 :       yy3 = g(xx3)
     532            1 :       x = find0_quadratic(xx1, yy1, xx2, yy2, xx3, yy3, ierr)
     533            1 :       if (ierr /= 0) then
     534            0 :          call mesa_error(__FILE__, __LINE__)
     535              :       end if
     536            1 :       y = g(x)
     537            1 :       write (*, 1) 'x', x
     538            1 :       write (*, 1) 'y', y
     539              : 
     540            1 :       xx1 = 6
     541            1 :       yy1 = g(xx1)
     542            1 :       xx2 = 7
     543            1 :       yy2 = g(xx2)
     544            1 :       xx3 = 8
     545            1 :       yy3 = g(xx3)
     546            1 :       x = find0_quadratic(xx1, yy1, xx2, yy2, xx3, yy3, ierr)
     547            1 :       if (ierr /= 0) then
     548            0 :          call mesa_error(__FILE__, __LINE__)
     549              :       end if
     550            1 :       y = g(x)
     551            1 :       write (*, 1) 'x', x
     552            1 :       write (*, 1) 'y', y
     553            1 :       write (*, *)
     554            1 :    end subroutine test_find0_quadratic
     555              : 
     556            2 :    subroutine test_find_max_quadratic
     557              :       real(dp) :: x1, y1, x2, y2, x3, y3, dx1, dx2, xmax, ymax
     558              :       integer :: ierr
     559              :       include 'formats'
     560            1 :       write (*, *) 'test_find_max_quadratic'
     561            1 :       dx1 = 1; dx2 = 2; y1 = 1; y2 = 10; y3 = 8
     562            1 :       x1 = 0; x2 = x1 + dx1; x3 = x2 + dx2
     563              :       ierr = 0
     564            1 :       call find_max_quadratic(x1, y1, x2, y2, x3, y3, xmax, ymax, ierr)
     565            1 :       if (ierr /= 0) then
     566            0 :          call mesa_error(__FILE__, __LINE__)
     567              :       end if
     568            1 :       write (*, 1) 'xmax', xmax, 1.85d0
     569            1 :       write (*, 1) 'ymax', ymax, 12.4083d0
     570            1 :       write (*, *)
     571            1 :    end subroutine test_find_max_quadratic
     572              : 
     573              : end module test_support
        

Generated by: LCOV version 2.0-1