math_module.f90 Source File


This file depends on

sourcefile~~math_module.f90~~EfferentGraph sourcefile~math_module.f90 math_module.f90 sourcefile~kind_module.f90 kind_module.F90 sourcefile~math_module.f90->sourcefile~kind_module.f90 sourcefile~numbers_module.f90 numbers_module.f90 sourcefile~math_module.f90->sourcefile~numbers_module.f90 sourcefile~numbers_module.f90->sourcefile~kind_module.f90

Files dependent on this one

sourcefile~~math_module.f90~~AfferentGraph sourcefile~math_module.f90 math_module.f90 sourcefile~crtbp_module.f90 crtbp_module.f90 sourcefile~crtbp_module.f90->sourcefile~math_module.f90 sourcefile~fortran_astrodynamics_toolkit.f90 fortran_astrodynamics_toolkit.f90 sourcefile~fortran_astrodynamics_toolkit.f90->sourcefile~math_module.f90 sourcefile~fortran_astrodynamics_toolkit.f90->sourcefile~crtbp_module.f90 sourcefile~halo_orbit_module.f90 halo_orbit_module.f90 sourcefile~fortran_astrodynamics_toolkit.f90->sourcefile~halo_orbit_module.f90 sourcefile~orbital_mechanics_module.f90 orbital_mechanics_module.f90 sourcefile~fortran_astrodynamics_toolkit.f90->sourcefile~orbital_mechanics_module.f90 sourcefile~rk_module_variable_step.f90 rk_module_variable_step.f90 sourcefile~fortran_astrodynamics_toolkit.f90->sourcefile~rk_module_variable_step.f90 sourcefile~halo_orbit_module.f90->sourcefile~math_module.f90 sourcefile~halo_orbit_module.f90->sourcefile~crtbp_module.f90 sourcefile~orbital_mechanics_module.f90->sourcefile~math_module.f90 sourcefile~rk_module_variable_step.f90->sourcefile~orbital_mechanics_module.f90

Source Code

!*****************************************************************************************
!> author: Jacob Williams
!
!  General math routines

    module math_module

    use kind_module,    only: wp
    use numbers_module, only: pi,twopi

    implicit none

    private

    public :: cube_root
    public :: wrap_angle
    public :: magnitude

    contains
!*****************************************************************************************

!*****************************************************************************************
!>
!  Returns a positive number the same magnitude as the input,
!  with only one significant digit.
!
!  If `mina` is present, then `max(mina,mag(a))` is returned
!
!  Examples:
!```
!     mag(1234.56)  -> 1000.0
!     mag(-999.99)  -> 900.0
!     mag(1.456e-4) -> 0.0001
!```

    pure elemental function magnitude(a,mina) result(m)

    implicit none

    real(wp),intent(in) :: a
    real(wp),intent(in),optional :: mina
    real(wp) :: m

    real(wp) :: x,tmp

    x = abs(a)

    if (x==0.0_wp) then
        if (.not. present(mina)) then
            m = 1.0_wp
        else
            m = mina
        end if
    else
        tmp = 10.0_wp ** floor(log10(x))
        m = tmp * floor(x/tmp)
        if (present(mina)) m = max(mina,m)
    end if

    end function magnitude
!*****************************************************************************************

!*****************************************************************************************
!>
!  Wrap an angle (in rad) from -pi to pi.

    pure elemental function wrap_angle(a) result(r)

    implicit none

    real(wp),intent(in) :: a
    real(wp)            :: r

    r = mod(a,twopi)
    if (abs(r)>pi) r = r - sign(twopi,r)

    end function wrap_angle
!*****************************************************************************************

!*****************************************************************************************
!> author: Jacob Williams
!
!  Cube root of a number (real solution only).

    pure elemental function cube_root(x) result(y)

    use numbers_module, only: one,three

    implicit none

    real(wp),intent(in) :: x
    real(wp)            :: y

    real(wp),parameter :: one_third = one/three

    y = sign( abs(x) ** one_third , x )

    end function cube_root
!*****************************************************************************************

!*****************************************************************************************
    end module math_module
!*****************************************************************************************