pmc_util Namespace Reference

Common utility subroutines. More...

Functions

subroutine assert_msg (code, condition_ok, error_msg)
 Errors unless condition_ok is true.
subroutine assert (code, condition_ok)
 Errors unless condition_ok is true.
subroutine die (code)
 Error immediately.
subroutine die_msg (code, error_msg)
 Error immediately.
integer get_unit ()
 Returns an available unit number. This should be freed by free_unit().
subroutine free_unit (unit)
 Frees a unit number returned by get_unit().
subroutine open_output (basename, suffix, out_unit)
 Allocate a new unit and open it with a filename given by basename + suffix.
subroutine pmc_srand (seed)
 Initializes the random number generator to the state defined by the given seed. If the seed is 0 then a seed is auto-generated from the current time.
real *8 pmc_rand ()
 Returns a random number between 0 and 1. Call this function rather than rand() or random_number() directly, as some compilers have trouble with one or the other.
integer pmc_rand_int (n)
 Returns a random integer between 1 and n.
integer prob_round (val)
 Round val to floor(val) or ceil(val) with probability proportional to the relative distance from val. That is, Prob(prob_round(val) == floor(val)) = ceil(val) - val.
real *8 vol2rad (v)
 Convert volume (m^3) to radius (m).
real *8 vol2diam (v)
 Convert volume (m^3) to diameter (m).
real *8 rad2vol (r)
 Convert radius (m) to volume (m^3).
real *8 diam2vol (d)
 Convert diameter (m) to volume (m^3).
logical almost_equal (d1, d2)
 Tests whether two real numbers are almost equal using only a relative tolerance.
logical almost_equal_abs (d1, d2, abs_tol)
 Tests whether two real numbers are almost equal using an absolute and relative tolerance.
subroutine check_event (time, timestep, interval, last_time, do_event)
 Computes whether an event is scheduled to take place.
subroutine open_existing (unit, filename)
 Opens a file for reading that must already exist, checking for errors.
subroutine linspace (min_x, max_x, n, x)
 Makes a linearly spaced array of length n from min to max.
subroutine logspace (min_x, max_x, n, x)
 Makes a logarithmically spaced array of length n from min to max.
integer linspace_find (min_x, max_x, n, x)
 Find the position of a real number in a 1D linear array.
integer logspace_find (min_x, max_x, n, x)
 Find the position of a real number in a 1D logarithmic array.
integer find_1d (n, x_vals, x)
 Find the position of a real number in an arbitrary 1D array.
real *8 interp_1d (x_vals, y_vals, x)
 1D linear interpolation.
integer string_to_integer (string)
 Convert a string to an integer.
real *8 string_to_real (string)
 Convert a string to a real.
logical string_to_logical (string)
 Convert a string to a logical.
integer sample_cts_pdf (n, pdf)
 Sample the given continuous probability density function.
integer sample_disc_pdf (n, pdf)
 Sample the given discrete probability density function.
subroutine sample_vec_cts_to_disc (n, vec_cts, n_samp, vec_disc)
 Convert a continuous vector into a discrete vector.
subroutine vec_cts_to_disc (n, vec_cts, n_samp, vec_disc)
 Convert a continuous vector into a discrete vector.
subroutine average_integer (int_vec, int_avg)
 Computes the average of an array of integer numbers.
subroutine average_real (real_vec, real_avg)
 Computes the average of an array of real numbers.
subroutine get_basename (filename, basename)
 Strip the extension to find the basename.
subroutine iso8601_date_and_time (date_time)
 Current date and time in ISO 8601 format.

Variables

integer, parameter max_units = 200
 Maximum number of IO units usable simultaneously.
integer, parameter unit_offset = 10
 Minimum unit number to allocate.
logical, dimension(max_units), save unit_used = .false.
 Table of unit numbers storing allocation status.

Detailed Description

Common utility subroutines.


Function Documentation

logical pmc_util::almost_equal ( real*8,intent(in)  d1,
real*8,intent(in)  d2 
)

Tests whether two real numbers are almost equal using only a relative tolerance.

Parameters:
d1 First number to compare.
d2 Second number to compare.
logical pmc_util::almost_equal_abs ( real*8,intent(in)  d1,
real*8,intent(in)  d2,
real*8,intent(in)  abs_tol 
)

Tests whether two real numbers are almost equal using an absolute and relative tolerance.

Parameters:
d1 First number to compare.
d2 Second number to compare.
abs_tol Tolerance for when d1 equals d2.
subroutine pmc_util::assert ( integer,intent(in)  code,
logical,intent(in)  condition_ok 
)

Errors unless condition_ok is true.

Parameters:
code Status code to use if assertion fails.
condition_ok Whether the assertion is ok.
subroutine pmc_util::assert_msg ( integer,intent(in)  code,
logical,intent(in)  condition_ok,
character(len=*),intent(in)  error_msg 
)

Errors unless condition_ok is true.

Parameters:
code Status code to use if assertion fails.
condition_ok Whether the assertion is ok.
error_msg Msg if assertion fails.
subroutine pmc_util::average_integer ( integer,dimension(:),intent(in)  int_vec,
integer,intent(out)  int_avg 
)

Computes the average of an array of integer numbers.

Parameters:
int_vec Array of integer numbers.
int_avg Average of int_vec.
subroutine pmc_util::average_real ( real*8,dimension(:),intent(in)  real_vec,
real*8,intent(out)  real_avg 
)

Computes the average of an array of real numbers.

Parameters:
real_vec Array of real numbers.
real_avg Average of real_vec.
subroutine pmc_util::check_event ( real*8,intent(in)  time,
real*8,intent(in)  timestep,
real*8,intent(in)  interval,
real*8,intent(inout)  last_time,
logical,intent(out)  do_event 
)

Computes whether an event is scheduled to take place.

The events should occur ideally at times 0, interval, 2*interval, etc. The events are guaranteed to occur at least interval * (1 - tolerance) apart, and if at least interval time has passed then the next call is guaranteed to do the event. Otherwise the timestep is used to guess whether to do the event.

Parameters:
time Current time.
timestep Estimate of the time to the next call.
interval How often the event should be done.
last_time When the event was last done.
do_event Whether the event should be done.
real*8 pmc_util::diam2vol ( real*8,intent(in)  d  ) 

Convert diameter (m) to volume (m^3).

Parameters:
d Diameter (m).
subroutine pmc_util::die ( integer,intent(in)  code  ) 

Error immediately.

Parameters:
code Status code to use if assertion fails.
subroutine pmc_util::die_msg ( integer,intent(in)  code,
character(len=*),intent(in)  error_msg 
)

Error immediately.

Parameters:
code Status code to use if assertion fails.
error_msg Msg if assertion fails.
integer pmc_util::find_1d ( integer,intent(in)  n,
real*8,dimension(n),intent(in)  x_vals,
real*8,intent(in)  x 
)

Find the position of a real number in an arbitrary 1D array.

Takes an array of x_vals, and a single x value, and returns the position p such that x_vals(p) <= x < x_vals(p+1). If p == 0 then x < x_vals(1) and if p == n then x_vals(n) <= x. x_vals must be sorted in increasing order.

If the array is known to be linearly or logarithmically spaced then linspace_find() or logspace_find() will be much faster.

Parameters:
n Number of values.
x_vals X value array, must be sorted.
x Value to interpolate at.
subroutine pmc_util::free_unit ( integer,intent(in)  unit  ) 

Frees a unit number returned by get_unit().

subroutine pmc_util::get_basename ( character(len=*),intent(in)  filename,
character(len=*),intent(out)  basename 
)

Strip the extension to find the basename.

Parameters:
filename Full filename.
basename Basename.
integer pmc_util::get_unit (  ) 

Returns an available unit number. This should be freed by free_unit().

real*8 pmc_util::interp_1d ( real*8,dimension(:),intent(in)  x_vals,
real*8,dimension(size(x_vals),intent(in)  y_vals,
real*8,intent(in)  x 
)

1D linear interpolation.

Takes an array of x and y, and a single x value, and returns the corresponding y using linear interpolation. x_vals must be sorted.

Parameters:
x_vals X value array, must be sorted.
y_vals Y value array.
x Value to interpolate at.
subroutine pmc_util::iso8601_date_and_time ( character(len=*),intent(out)  date_time  ) 

Current date and time in ISO 8601 format.

Parameters:
date_time Result string.
subroutine pmc_util::linspace ( real*8,intent(in)  min_x,
real*8,intent(in)  max_x,
integer,intent(in)  n,
real*8,dimension(n),intent(out)  x 
)

Makes a linearly spaced array of length n from min to max.

Parameters:
min_x Minimum array value.
max_x Maximum array value.
n Number of entries.
x Array.
integer pmc_util::linspace_find ( real*8,intent(in)  min_x,
real*8,intent(in)  max_x,
integer,intent(in)  n,
real*8,intent(in)  x 
)

Find the position of a real number in a 1D linear array.

If xa is the array allocated by linspace(min_x, max_x, n, xa) then i = linspace_find(min_x, max_x, n, x) returns the index i satisfying xa(i) <= x < xa(i+1) for min_x <= x < max_x. If x >= max_x then i = n - 1. If x < min_x then i = 1. Thus 1 <= i <= n - 1.

This is equivalent to using find_1d() but much faster if the array is linear.

Parameters:
min_x Minimum array value.
max_x Maximum array value.
n Number of entries.
x Value.
subroutine pmc_util::logspace ( real*8,intent(in)  min_x,
real*8,intent(in)  max_x,
integer,intent(in)  n,
real*8,dimension(n),intent(out)  x 
)

Makes a logarithmically spaced array of length n from min to max.

Parameters:
min_x Minimum array value.
max_x Maximum array value.
n Number of entries.
x Array.
integer pmc_util::logspace_find ( real*8,intent(in)  min_x,
real*8,intent(in)  max_x,
integer,intent(in)  n,
real*8,intent(in)  x 
)

Find the position of a real number in a 1D logarithmic array.

If xa is the array allocated by logspace(min_x, max_x, n, xa) then i = logspace_find(min_x, max_x, n, x) returns the index i satisfying xa(i) <= x < xa(i+1) for min_x <= x < max_x. If x >= max_x then i = n. If x < min_x then i = 1. Thus 1 <= i <= n.

This is equivalent to using find_1d() but much faster if the array is known to be logarithmic.

Parameters:
min_x Minimum array value.
max_x Maximum array value.
n Number of entries.
x Value.
subroutine pmc_util::open_existing ( integer,intent(in)  unit,
character(len=*),intent(in)  filename 
)

Opens a file for reading that must already exist, checking for errors.

Parameters:
unit Unit to open with.
filename Filename of file to open.
subroutine pmc_util::open_output ( character(len=*),intent(in)  basename,
character(len=*),intent(in)  suffix,
integer,intent(out)  out_unit 
)

Allocate a new unit and open it with a filename given by basename + suffix.

Parameters:
basename Basename of the output file.
suffix Suffix of the output file.
out_unit Unit for the file.
real*8 pmc_util::pmc_rand (  ) 

Returns a random number between 0 and 1. Call this function rather than rand() or random_number() directly, as some compilers have trouble with one or the other.

integer pmc_util::pmc_rand_int ( integer,intent(in)  n  ) 

Returns a random integer between 1 and n.

Parameters:
n Maximum random number to generate.
subroutine pmc_util::pmc_srand ( integer,intent(in)  seed  ) 

Initializes the random number generator to the state defined by the given seed. If the seed is 0 then a seed is auto-generated from the current time.

Parameters:
seed Random number generator seed.
integer pmc_util::prob_round ( real*8,intent(in)  val  ) 

Round val to floor(val) or ceil(val) with probability proportional to the relative distance from val. That is, Prob(prob_round(val) == floor(val)) = ceil(val) - val.

Parameters:
val Value to round.
real*8 pmc_util::rad2vol ( real*8,intent(in)  r  ) 

Convert radius (m) to volume (m^3).

Parameters:
r Radius (m).
integer pmc_util::sample_cts_pdf ( integer,intent(in)  n,
real*8,dimension(n),intent(in)  pdf 
)

Sample the given continuous probability density function.

That is, return a number k = 1,...,n such that prob(k) = pdf(k) / sum(pdf).

Parameters:
n Number of entries.
pdf Probability density function.
integer pmc_util::sample_disc_pdf ( integer,intent(in)  n,
integer,dimension(n),intent(in)  pdf 
)

Sample the given discrete probability density function.

That is, return a number k = 1,...,n such that prob(k) = pdf(k) / sum(pdf).

Parameters:
n Number of entries.
pdf Probability density function.
subroutine pmc_util::sample_vec_cts_to_disc ( integer,intent(in)  n,
real*8,dimension(n),intent(in)  vec_cts,
integer,intent(in)  n_samp,
integer,dimension(n),intent(out)  vec_disc 
)

Convert a continuous vector into a discrete vector.

Use n_samp samples. Each discrete entry is sampled with a PDF given by vec_cts. This is very slow for large n_samp or large n.

Parameters:
n Number of entries in vector.
vec_cts Continuous vector.
n_samp Number of discrete samples to use.
vec_disc Discretized vector.
integer pmc_util::string_to_integer ( character(len=*),intent(in)  string  ) 

Convert a string to an integer.

Parameters:
string String to convert.
logical pmc_util::string_to_logical ( character(len=*),intent(in)  string  ) 

Convert a string to a logical.

Parameters:
string String to convert.
real*8 pmc_util::string_to_real ( character(len=*),intent(in)  string  ) 

Convert a string to a real.

Parameters:
string String to convert.
subroutine pmc_util::vec_cts_to_disc ( integer,intent(in)  n,
real*8,dimension(n),intent(in)  vec_cts,
integer,intent(in)  n_samp,
integer,dimension(n),intent(out)  vec_disc 
)

Convert a continuous vector into a discrete vector.

Use n_samp samples. Returns discrete vector whose relative entry sizes are $ \ell_1 $ closest to the continuous vector.

Parameters:
n Number of entries in vector.
vec_cts Continuous vector.
n_samp Number of discrete samples to use.
vec_disc Discretized vector.
real*8 pmc_util::vol2diam ( real*8,intent(in)  v  ) 

Convert volume (m^3) to diameter (m).

Parameters:
v Volume (m^3).
real*8 pmc_util::vol2rad ( real*8,intent(in)  v  ) 

Convert volume (m^3) to radius (m).

Parameters:
v Volume (m^3).

Variable Documentation

integer,parameter pmc_util::max_units = 200

Maximum number of IO units usable simultaneously.

integer,parameter pmc_util::unit_offset = 10

Minimum unit number to allocate.

logical,dimension(max_units),save pmc_util::unit_used = .false.

Table of unit numbers storing allocation status.


Generated on 29 Mar 2010 for PartMC by  doxygen 1.6.1