Topic: Debugging OOP
I am getting back into OOP but in Fortran 2003. I have the following code, which solves a non-linear equation f(x) = x^2 - 1
using bisection on an initial interval 0 to 2 (the correct result is 1 of course).
I am using a type data_t to setup the various constants needed by the function being solved. This is derived from the abstract class bundle_t (which has no components). The solve subroutine passes values of x (trial solutions) and the bundle variable (which happens to have the dynamic type of data_t) to the function. Bisection is used to get the solution.
I am doing it this was so I can solve any function by providing the correct f and a suitable extended type. At the moment everything is in 1 file. But eventually I will separate the "utility" module from the "user problem" module in separate files.
I am quite sure the code is correct and it does work.
However, I cannot trace execution through the solve function using the debugger. The code just freezes when I step into it, and it won't step again or continue. Any ideas?
module solver ! Abstract type, problem data type is an extension of this. type, abstract :: bundle_t end type bundle_t contains function solve(f, a, b, bundle) double precision, intent(in) :: a, b ! Next variable is of type bundle_t or any extended type of bundle_t class(bundle_t), intent(in) :: bundle interface function f(x, bundle) import double precision, intent(in) :: x class(bundle_t), intent(in) :: bundle double precision :: f end function f end interface double precision :: solve double precision :: xlo, xhi, x, flo, fhi, fn ! Bisection (I'm assuming a >= b) xlo = a xhi = b flo = f(xlo, bundle) fhi = f(xhi, bundle) do x = 0.5d0*(xlo + xhi) fn = f(x, bundle) if (fn > 0.0d0 .eqv. fhi > flo) then xhi = x else xlo = x end if if (xhi - xlo < 1.0d-5) exit end do solve = x end function solve end module solver module problem use solver private ! An extended data type is created to store the "problem data". type, extends(bundle_t) :: data_t double precision :: c end type data_t public :: xxx contains subroutine xxx type(data_t) :: data ! The constant in the equation to be solved. data%c = 1.0d0 print *, solve(f, 0.0d0, 2.0d0, data) end subroutine xxx function f(x, bundle) double precision, intent(in) :: x class(bundle_t), intent(in) :: bundle double precision :: f select type (bundle) type is (data_t) f = x**2 - bundle%c end select end function f end module problem program anon use problem call xxx end program anon