sample1D Subroutine

public subroutine sample1D(this, x, y, value)

Uses

Randomly sample from 1D array

Type Bound

piecewise1D

Arguments

Type IntentOptional Attributes Name
class(piecewise1D), intent(in) :: this
real(kind=wp), intent(out) :: x

Return value

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

Not used, but here so we can have same interface as 2D sample routine.

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

Optional x value. If not present we generate a random one in the range [0., 1.]


Source Code

    subroutine sample1D(this, x, y, value)
        !! Randomly sample from 1D array
        use random, only : ran2, ranu

        class(piecewise1D), intent(in)  :: this
        !> Return value
        real(kind=wp),      intent(out) :: x
        !> Not used, but here so we can have same interface as 2D sample routine.
        real(kind=wp),      intent(out) :: y
        !> Optional x value. If not present we generate a random one in the range [0., 1.] 
        real(kind=wp), intent(in), optional :: value

        integer(kind=int64) :: idx
        real(kind=wp)       :: val

        if(.not. present(value))then
            !get random x coordinate then get corresponding y
            val = ran2()
            call search_1D(this%cdf, idx, val)
            
            x = this%array(idx, 1) + &
            ((val - this%cdf(idx))*(this%array(idx + 1, 1) - this%array(idx, 1))) / (this%cdf(idx + 1) - this%cdf(idx))

        else
            !already have x so get y
            call search_2D(this%array, idx, value)
            x = this%array(idx, 2) + (this%array(idx+1, 2) - this%array(idx, 2)) * &
                   ((value - this%array(idx, 1))/(this%array(idx+1, 1) - this%array(idx, 1)))
        end if

    end subroutine sample1D