piecewiseMod Module

This file contains the piecewise abstract type, for sampling from constants, 1D or 2D arrays. Inspired by PBRT piecewise class. Currently, the following public types are defined:

  • Constant. Used in the case where there is only one value.
  • 1D. Used in the case where there is a spectrum
  • 2D. Used in the case where SLM or other image based source types are needed.

The piecewise type ensures that there is a method (sample) that can be called on all inherited types, e.g call 2Dimage%p%sample(x, y) will return a position (x,y) from where to release a photon. This class can be used to have multi-spectral or single valued wavelength, or used as a 2D image input source i.e SLMs. NOTE: optical properties are not currently adjusted on wavelength change.



Interfaces

public interface piecewise1D

  • public function init_piecewise1D(array) result(res)

    initalise the piecewise1D type with an array size (n, 2). Calculates the CDF of this array. Input array

    Arguments

    Type IntentOptional Attributes Name
    real(kind=wp), intent(in) :: array(:,:)

    Return Value type(piecewise1D)

public interface piecewise2D

  • public function init_piecewise2D(cell_width, cell_height, image)

    Initalise the piecewise2D type with a given cell_width, cell_height and input image

    Arguments

    Type IntentOptional Attributes Name
    real(kind=wp), intent(in) :: cell_width

    Input cell width

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

    Input cell height

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

    Input image

    Return Value type(piecewise2D)


Abstract Interfaces

abstract interface

  • public subroutine sampleInterface(this, x, y, value)

    Arguments

    Type IntentOptional Attributes Name
    class(piecewise), intent(in) :: this
    real(kind=wp), intent(out) :: x
    real(kind=wp), intent(out) :: y
    real(kind=wp), intent(in), optional :: value

Derived Types

type, public, extends(piecewise) ::  constant

Constant piecewise type. i.e a piecewise function that does not change value

Components

Type Visibility Attributes Name Initial
real(kind=wp), public :: value

The constant value

Type-Bound Procedures

procedure, public :: sample => getValue ../../

Sampling routine

type, public, abstract ::  piecewise

Abstract spectrum base type.

Type-Bound Procedures

procedure(sampleInterface), public, deferred :: sample ../../

Deferred procdure. Used to generate a sample from spectrum or get constant value etc.

type, public, extends(piecewise) ::  piecewise1D

1D piecewise type. Used for the spectral type

Components

Type Visibility Attributes Name Initial
real(kind=wp), public, allocatable :: array(:,:)

Input array to sample from. Should be size(n, 2). 1st column is x-axis, 2nd column is y-axis

real(kind=wp), public, allocatable :: cdf(:)

cumulative distribution function (CDF) of array.

Constructor

public function init_piecewise1D (array)

initalise the piecewise1D type with an array size (n, 2). Calculates the CDF of this array. Input array

Type-Bound Procedures

procedure, public :: sample => sample1D ../../

Overloaded sampling function

type, public, extends(piecewise) ::  piecewise2D

2D piecewise type. Used for images

Components

Type Visibility Attributes Name Initial
real(kind=wp), public, allocatable :: cdf(:)

cumulative distribution function (CDF) of array.

real(kind=wp), public :: cell_height

Height of each cell

real(kind=wp), public :: cell_width

Width of each cell

integer, private :: xoffset

Offsets

integer, private :: yoffset

Offsets

Constructor

public function init_piecewise2D (cell_width, cell_height, image)

Initalise the piecewise2D type with a given cell_width, cell_height and input image

Type-Bound Procedures

procedure, public :: sample => sample2D ../../

Overloaded sampling function

type, public ::  spectrum_t

Spectrum_t type. Used as a container type

Components

Type Visibility Attributes Name Initial
class(piecewise), public, pointer :: p => null()

Functions

public function init_piecewise1D(array) result(res)

initalise the piecewise1D type with an array size (n, 2). Calculates the CDF of this array. Input array

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in) :: array(:,:)

Return Value type(piecewise1D)

public function init_piecewise2D(cell_width, cell_height, image)

Initalise the piecewise2D type with a given cell_width, cell_height and input image

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in) :: cell_width

Input cell width

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

Input cell height

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

Input image

Return Value type(piecewise2D)

public function nextpwr2(v) result(res)

Get the next power of 2. i.e given 5 will return 8 (4^2) only works on 32bit ints ref

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: v

Return Value integer

public function pack_bits(z) result(x)

Reverse the split function. I.e go from 0a0b0c0d to abcd Adapted from archer2 cpp course

Arguments

Type IntentOptional Attributes Name
integer(kind=int64), intent(in) :: z

Input interleaved integer

Return Value integer(kind=int64)


Subroutines

public subroutine decode(z, x, y)

Compute the 2 indices from a Morton index Adapted from archer2 cpp course

Arguments

Type IntentOptional Attributes Name
integer(kind=int64), intent(in) :: z

Morton Index

integer(kind=int32), intent(out) :: x

The computed indices

integer(kind=int32), intent(out) :: y

The computed indices

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

The constant version of sample

Arguments

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

Output value

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

Not used. Kept to keep interface the same for constant, piecewise1D and piecewise2D

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

Not used. Kept to keep interface the same for constant, piecewise1D and piecewise2D

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

Randomly sample from 1D array

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.]

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

Arguments

Type IntentOptional Attributes Name
class(piecewise2D), intent(in) :: this
real(kind=wp), intent(out) :: x
real(kind=wp), intent(out) :: y
real(kind=wp), intent(in), optional :: value

public subroutine search_1D(array, nlow, value)

search by bisection for 1D array

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in) :: array(:)

Array to search

integer(kind=int64), intent(out) :: nlow

index of found value

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

value to find in 1D array

public subroutine search_2D(array, nlow, value)

search by bisection for 1D array

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in) :: array(:,:)

2D array to search. Only searches 1st column

integer(kind=int64), intent(out) :: nlow

Index of found index

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

Value to find in the array.