root_solver Derived Type

type, public :: root_solver

abstract class for the root solver methods


Inherited by

type~~root_solver~~InheritedByGraph type~root_solver root_solver type~anderson_bjorck_king_solver anderson_bjorck_king_solver type~anderson_bjorck_king_solver->type~root_solver type~anderson_bjorck_solver anderson_bjorck_solver type~anderson_bjorck_solver->type~root_solver type~barycentric_solver barycentric_solver type~barycentric_solver->type~root_solver type~bdqrf_solver bdqrf_solver type~bdqrf_solver->type~root_solver type~bisection_solver bisection_solver type~bisection_solver->type~root_solver type~blendtf_solver blendtf_solver type~blendtf_solver->type~root_solver type~brent_solver brent_solver type~brent_solver->type~root_solver type~brenth_solver brenth_solver type~brenth_solver->type~root_solver type~brentq_solver brentq_solver type~brentq_solver->type~root_solver type~chandrupatla_solver chandrupatla_solver type~chandrupatla_solver->type~root_solver type~illinois_solver illinois_solver type~illinois_solver->type~root_solver type~itp_solver itp_solver type~itp_solver->type~root_solver type~muller_solver muller_solver type~muller_solver->type~root_solver type~pegasus_solver pegasus_solver type~pegasus_solver->type~root_solver type~regula_falsi_solver regula_falsi_solver type~regula_falsi_solver->type~root_solver type~ridders_solver ridders_solver type~ridders_solver->type~root_solver type~toms748_solver toms748_solver type~toms748_solver->type~root_solver type~zhang_solver zhang_solver type~zhang_solver->type~root_solver

Components

Type Visibility Attributes Name Initial
procedure(func), private, pointer :: f => null()

user function to find the root of

real(kind=wp), private :: ftol = 0.0_wp

absolute tolerance for f=0

real(kind=wp), private :: rtol = 1.0e-6_wp

relative tol for x

real(kind=wp), private :: atol = 1.0e-12_wp

absolute tol for x [not used by all methods]

integer, private :: maxiter = 2000

maximum number of iterations [not used by all methods]


Type-Bound Procedures

procedure, public :: initialize => initialize_root_solver

initialize the class [must be called first]

  • private subroutine initialize_root_solver(me, f, ftol, rtol, atol, maxiter)

    Initialize the root_solver class.

    Read more…

    Arguments

    Type IntentOptional Attributes Name
    class(root_solver), intent(out) :: me
    procedure(func) :: f

    user function f(x) to find the root of

    real(kind=wp), intent(in), optional :: ftol

    absolute tolerance for f=0

    real(kind=wp), intent(in), optional :: rtol

    relative tol for x

    real(kind=wp), intent(in), optional :: atol

    absolute tol for x

    integer, intent(in), optional :: maxiter

    maximum number of iterations

procedure, public :: solve

main routine for finding the root

  • private subroutine solve(me, ax, bx, xzero, fzero, iflag, fax, fbx, bisect_on_failure)

    Main wrapper routine for all the methods.

    Arguments

    Type IntentOptional Attributes Name
    class(root_solver), intent(inout) :: me
    real(kind=wp), intent(in) :: ax

    left endpoint of initial interval

    real(kind=wp), intent(in) :: bx

    right endpoint of initial interval

    real(kind=wp), intent(out) :: xzero

    abscissa approximating a zero of f in the interval ax,bx

    real(kind=wp), intent(out) :: fzero

    value of f at the root (f(xzero))

    integer, intent(out) :: iflag

    status flag (-1=error, 0=root found, -4=ax must be /= bx)

    real(kind=wp), intent(in), optional :: fax

    if f(ax) is already known, it can be input here

    real(kind=wp), intent(in), optional :: fbx

    if f(bx) is already known, it can be input here

    logical, intent(in), optional :: bisect_on_failure

    if true, then if the specified method fails, it will be retried using the bisection method. (default is False). Note that this can use up to maxiter additional function evaluations.

procedure(root_f), private, deferred :: find_root

root solver function. Meant to be called from solve, which handles some common startup tasks. All these routines assume that and have opposite signs.

  • subroutine root_f(me, ax, bx, fax, fbx, xzero, fzero, iflag) Prototype

    Root solver function interface

    Arguments

    Type IntentOptional Attributes Name
    class(root_solver), intent(inout) :: me
    real(kind=wp), intent(in) :: ax

    left endpoint of initial interval

    real(kind=wp), intent(in) :: bx

    right endpoint of initial interval

    real(kind=wp), intent(in) :: fax

    f(ax)

    real(kind=wp), intent(in) :: fbx

    f(bx)

    real(kind=wp), intent(out) :: xzero

    abscissa approximating a zero of f in the interval ax,bx

    real(kind=wp), intent(out) :: fzero

    value of f at the root (f(xzero))

    integer, intent(out) :: iflag

    status flag

procedure, private :: get_fa_fb

  • private subroutine get_fa_fb(me, ax, bx, fax, fbx, fa, fb)

    Returns the function values at ax and bx to start the root finding algorithm.

    Arguments

    Type IntentOptional Attributes Name
    class(root_solver), intent(inout) :: me
    real(kind=wp), intent(in) :: ax

    left endpoint of initial interval

    real(kind=wp), intent(in) :: bx

    right endpoint of initial interval

    real(kind=wp), intent(in), optional :: fax

    if f(ax) is already known, it can be input here

    real(kind=wp), intent(in), optional :: fbx

    if f(bx) is already known, it can be input here

    real(kind=wp), intent(out) :: fa

    f(ax) to use

    real(kind=wp), intent(out) :: fb

    f(ax) to use

procedure, private :: converged

  • private function converged(me, a, b)

    Determines convergence in x based on if the reltol or abstol is satisfied.

    Arguments

    Type IntentOptional Attributes Name
    class(root_solver), intent(inout) :: me
    real(kind=wp), intent(in) :: a

    old value

    real(kind=wp), intent(in) :: b

    new value

    Return Value logical

procedure, private :: solution

to check f value against ftol

  • private function solution(me, x, f, xzero, fzero)

    Returns true if this is a solution and sets xzero and fzero.

    Arguments

    Type IntentOptional Attributes Name
    class(root_solver), intent(inout) :: me
    real(kind=wp), intent(in) :: x
    real(kind=wp), intent(in) :: f
    real(kind=wp), intent(inout) :: xzero
    real(kind=wp), intent(inout) :: fzero

    Return Value logical

Source Code

    type,abstract,public :: root_solver
        !! abstract class for the root solver methods
        private
        procedure(func),pointer :: f => null()  !! user function to find the root of
        real(wp) :: ftol = 0.0_wp       !! absolute tolerance for `f=0`
        real(wp) :: rtol = 1.0e-6_wp    !! relative tol for x
        real(wp) :: atol = 1.0e-12_wp   !! absolute tol for x [not used by all methods]
        integer  :: maxiter = 2000      !! maximum number of iterations [not used by all methods]
    contains
        private
        procedure,public :: initialize => initialize_root_solver !! initialize the class [must be called first]
        procedure,public :: solve !! main routine for finding the root
        procedure(root_f),deferred :: find_root !! root solver function. Meant to be
                                                !! called from [[solve]], which handles some common
                                                !! startup tasks. All these routines assume that
                                                !! \( f(a_x) \) and \( f(b_x) \) have opposite signs.
        procedure :: get_fa_fb
        procedure :: converged
        procedure :: solution !! to check `f` value against `ftol`
    end type root_solver