auxiliar_functions.f90 Source File


This file depends on

sourcefile~~auxiliar_functions.f90~~EfferentGraph sourcefile~auxiliar_functions.f90 auxiliar_functions.f90 sourcefile~constants.f90 constants.f90 sourcefile~auxiliar_functions.f90->sourcefile~constants.f90

Source Code

module auxiliar_functions
    !! # Auxiliary numerical functions
    !!
    !! # Description
    !! Relative-error and array-comparison helpers used in tests and
    !! consistency checks. The relative-error normalization is by the first
    !! argument, so callers should avoid a zero reference value.
    !!
    !! # Examples
    !! ```fortran
    !! if (allclose(reference, calculated, 1.0e-8_pr)) print *, "consistent"
    !! ```
    use yaeos__constants, only: pr
contains
    elemental function rel_error(x, y)
        !! # rel_error
        !! Return the absolute relative error with `x` as reference.
        !!
        !! # Description
        !! Computes \(|x-y|/|x|\). The reference `x` must be nonzero.
        real(pr), intent(in) :: x !! Nonzero reference value.
        real(pr), intent(in) :: y !! Value compared against the reference.
        real(pr) :: rel_error

        rel_error = abs(x - y)/abs(x)
    end function rel_error

    function allclose(x, y, rtol)
        !! Check whether all elements of two arrays agree within `rtol`.
        real(pr), intent(in) :: x(:) !! First array
        real(pr), intent(in) :: y(:) !! Second array
        real(pr), intent(in) :: rtol !! Relative tolerance

        logical :: allclose
        allclose = maxval(rel_error(x, y)) < rtol
        if (.not. allclose) then
            print *, "Max relative error:", maxval(rel_error(x, y))
            print *, "x:", x
            print *, "y:", y
        end if
    end function allclose
end module auxiliar_functions