From 2c09aad550473cbe3175ad928bd047e2aec443d2 Mon Sep 17 00:00:00 2001 From: Michael Zingale Date: Thu, 19 Nov 2026 14:11:22 -0500 Subject: [PATCH] fix error return in do_look_for_brackets previously, if it failed to find a bracket, it would return ierr equal to whatever the last function call gave (after exhausting iterations). Now the return error after exhaustion is reset to -1. This issue arises because both do_look_for_brackets and the function evaluation use the same ierr. --- num/private/mod_root.f90 | 4 ++++ num/test/src/test_num.f90 | 1 + num/test/src/test_support.f90 | 39 +++++++++++++++++++++++++++++++++++ num/test/test_output | 1 + 4 files changed, 45 insertions(+) diff --git a/num/private/mod_root.f90 b/num/private/mod_root.f90 index 312e6986ef..d4dda1225a 100644 --- a/num/private/mod_root.f90 +++ b/num/private/mod_root.f90 @@ -251,6 +251,10 @@ subroutine do_look_for_brackets(x, dx, x1, x3, f, y1, y3, imax, lrpar, rpar, lip end do + ! if we made it here, we failed. Reset ierr, since the function + ! calls would have set it to 0 if they were successful + ierr = -1 + !write(*,1) 'exit do_look_for_brackets' end subroutine do_look_for_brackets diff --git a/num/test/src/test_num.f90 b/num/test/src/test_num.f90 index 754e260a25..ac38b51a7e 100644 --- a/num/test/src/test_num.f90 +++ b/num/test/src/test_num.f90 @@ -85,6 +85,7 @@ program test_num call test_root_with_brackets call test_root2 call test_root3 + call test_root_no_brackets ! explicit solvers call test_dopri(.false., show_all) diff --git a/num/test/src/test_support.f90 b/num/test/src/test_support.f90 index b89f09f70d..01a5096c9b 100644 --- a/num/test/src/test_support.f90 +++ b/num/test/src/test_support.f90 @@ -208,6 +208,45 @@ subroutine test_root3 write (*, *) end subroutine test_root3 + subroutine test_root_no_brackets + ! any imax that will fail here (just need to keep it below 31 so + ! we don't overflow) + integer, parameter :: imax = 3, lipar = 0, lrpar = 0 + real(dp) :: x1, x3, y1, y3 + integer :: ierr + real(dp), target :: rpar_ary(lrpar) + integer, target :: ipar_ary(lipar) + real(dp), pointer :: rpar(:) + integer, pointer :: ipar(:) + + ipar => ipar_ary + rpar => rpar_ary + + ! Function evaluations succeed, but x*x + 1 has no real root. + call look_for_brackets(0d0, 1d0, x1, x3, no_root_f, y1, y3, & + imax, lrpar, rpar, lipar, ipar, ierr) + + write (*, '(a, i4, a, i4)') & + 'look_for_brackets no root expected ierr', -1, ' got', ierr + + contains + + real(dp) function no_root_f(x, dfdx, lrpar, rpar, lipar, ipar, ierr) + real(dp), intent(in) :: x + real(dp), intent(out) :: dfdx + integer, intent(in) :: lrpar, lipar + real(dp), intent(inout), pointer :: rpar(:) + integer, intent(inout), pointer :: ipar(:) + integer, intent(out) :: ierr + + ! this has no real roots + no_root_f = x*x + 1d0 + dfdx = 2d0*x + ierr = 0 + end function no_root_f + + end subroutine test_root_no_brackets + subroutine van_der_Pol_derivs(n, x, h, y, f, lrpar, rpar, lipar, ipar, ierr) integer, intent(in) :: n, lrpar, lipar real(dp), intent(in) :: x, h diff --git a/num/test/test_output b/num/test/test_output index eea6eb88a2..83fd1c4e1a 100644 --- a/num/test/test_output +++ b/num/test/test_output @@ -239,6 +239,7 @@ g_05(x) = exp( x ) - 2x + 1/(100x) - 1/(1000000x^2) first safe_root_with_guess 4.9999999999999362D-01 second safe_root_with_guess 5.0000000000003031D-01 +look_for_brackets no root expected ierr -1 got -1 vdpol dopri5