CAMP 1.0.0
Chemistry Across Multiple Phases
Data Types | Functions/Subroutines | Variables
camp_util Module Reference

Common utility subroutines. More...

Data Types

interface  string_t
 String type for building arrays of string of various size. More...
 
interface  to_string
 Interface for to_string functions. More...
 

Functions/Subroutines

type(string_t) function, pointer string_t_constructor (val)
 Constructor for string_t.
 
elemental subroutine string_t_finalize (this)
 Finalize a string.
 
subroutine warn_msg (code, warning_msg, already_warned)
 Prints a warning message.
 
subroutine warn_assert_msg (code, condition_ok, warning_msg)
 Prints a warning message if condition_ok is false.
 
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 function 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_file_read (filename, unit)
 Open a file for reading with an automatically assigned unit and test that it succeeds. The file should be closed with close_file().
 
subroutine open_file_write (filename, unit)
 Open a file for writing with an automatically assigned unit and test that it succeeds. The file should be closed with close_file().
 
subroutine close_file (unit)
 Close a file and de-assign the unit.
 
real(kind=dp) elemental function sphere_vol2rad (v)
 Convert mass-equivalent volume \(V\) (m^3) to geometric radius \(R_{\rm geo}\) (m) for spherical particles.
 
real(kind=dp) elemental function rad2diam (r)
 Convert radius (m) to diameter (m).
 
real(kind=dp) elemental function sphere_rad2vol (r)
 Convert geometric radius \(R_{\rm geo}\) (m) to mass-equivalent volume \(V\) (m^3) for spherical particles.
 
real(kind=dp) elemental function diam2rad (d)
 Convert diameter (m) to radius (m).
 
real(kind=dp) function air_mean_free_path (temp, pressure)
 Calculate air molecular mean free path \(l\) (m).
 
logical function almost_equal (d1, d2, rel_tol, abs_tol)
 Tests whether two real numbers are almost equal using only a relative tolerance.
 
logical function almost_equal_abs (d1, d2, abs_tol)
 Tests whether two real numbers are almost equal using an absolute and relative tolerance.
 
subroutine check_time_multiple (first_name, first_time, second_name, second_time)
 Check that the first time interval is close to an integer multiple of the second, and warn if it is not.
 
subroutine check_event (time, timestep, interval, last_time, do_event)
 Computes whether an event is scheduled to take place.
 
real(kind=dp) function, dimension(:), allocatable linspace (min_x, max_x, n)
 Makes a linearly spaced array from min to max.
 
real(kind=dp) function, dimension(:), allocatable logspace (min_x, max_x, n)
 Makes a logarithmically spaced array of length n from min to max.
 
integer function linspace_find (min_x, max_x, n, x)
 Find the position of a real number in a 1D linear array.
 
integer function logspace_find (min_x, max_x, n, x)
 Find the position of a real number in a 1D logarithmic array.
 
integer function find_1d (n, x_vals, x)
 Find the position of a real number in an arbitrary 1D array.
 
real(kind=dp) function interp_1d (x_vals, y_vals, x)
 1D linear interpolation.
 
real(kind=dp) function interp_linear_disc (x_1, x_n, n, i)
 Linear interpolation over discrete indices.
 
integer function string_to_integer (string)
 Convert a string to an integer.
 
real(kind=dp) function string_to_real (string)
 Convert a string to a real.
 
logical function string_to_logical (string)
 Convert a string to a logical.
 
character(len=camp_util_convert_string_len) function integer_to_string (val)
 Convert an integer to a string format.
 
character(len=camp_util_convert_string_len) function real_dp_to_string (val)
 Convert a double precision real to a string format.
 
character(len=camp_util_convert_string_len) function real_sp_to_string (val)
 Convert a single precision real to a string format.
 
character(len=camp_util_convert_string_len) function logical_to_string (val)
 Convert a logical to a string format.
 
character(len=camp_util_convert_string_len) function complex_to_string (val)
 Convert a complex to a string format.
 
character(len=camp_util_convert_string_len) function integer_to_string_max_len (val, max_len)
 Convert an integer to a string format of maximum length.
 
character(len=camp_util_convert_string_len) function real_to_string_max_len (val, max_len)
 Convert a real to a string format of maximum length.
 
character(len=camp_util_convert_string_len) function time_to_string_max_len (time, max_len)
 Convert a time to a string format of maximum length.
 
type(string_t) function, dimension(:), allocatable split_char (this, splitter, compress)
 Splits a string on a substring.
 
type(string_t) function, dimension(:), allocatable split_string (this, splitter, compress)
 Splits a string on a substring.
 
subroutine vec_cts_to_disc (n, vec_cts, n_samp, vec_disc)
 Convert a real-valued vector into an integer-valued 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.
 
integer function string_array_find (array, val)
 Return the index of the first occurance of the given value in the array, or 0 if it is not present.
 
subroutine ensure_real_array_size (x, n, only_grow)
 Allocate or reallocate the given array to ensure it is of the given size, preserving any data and/or initializing to 0.
 
subroutine ensure_real_array_2d_size (x, n1, n2, only_grow)
 Allocate or reallocate the given array to ensure it is of the given size, preserving any data and/or initializing to 0.
 
subroutine ensure_integer_array_size (x, n, only_grow)
 Allocate or reallocate the given array to ensure it is of the given size, preserving any data and/or initializing to 0.
 
subroutine ensure_integer_array_2d_size (x, n1, n2, only_grow)
 Allocate or reallocate the given array to ensure it is of the given size, preserving any data and/or initializing to 0.
 
subroutine ensure_string_array_size (x, n)
 Allocate or reallocate the given array to ensure it is of the given size, without preserving data.
 
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.
 
real(kind=dp) function deg2rad (deg)
 Convert degrees to radians.
 
real(kind=dp) function rad2deg (rad)
 Convert radians to degrees.
 
subroutine integer_sort (data, perm)
 Sort the given data array and return the permutation defining the sort.
 
subroutine loadtxt (filename, data)
 Load a real array from a text file.
 
subroutine savetxt_1d (filename, data)
 Write a real 1D array to a text file.
 
subroutine savetxt_2d (filename, data)
 Write a real 2D array to a text file.
 
subroutine reallocate_real_array2d (data, rows, cols)
 Reallocate a 2D real array to the given size, preserving the contents.
 
subroutine read_char_raw (unit, char, eol, eof)
 Read a single character from a file, signaling if we have hit end-of-line (EOL) or end-of-file (EOF). If EOL or EOF are true then the character value should be ignored.
 
subroutine read_word_raw (unit, word, eol, eof)
 Read a white-space delimited word from a file, signaling if we have EOL or EOF. If EOL or EOF are true then the word will still be meaningful data. If there was no data to be read then len(word) will be 0.
 
logical function starts_with (string, start_string)
 Checks whether a string starts with a given other string.
 
elemental real(kind=dp) function harmonic_mean (x1, x2)
 Compute the harmonic mean of two numbers.
 
elemental real(kind=dp) function nplogp (p)
 Compute \( - p \ln p\) for computing entropy.
 
real(kind=dp) function entropy (p)
 Compute the entropy of a probability mass function (non necessarily normalized).
 
integer function pow2_above (n)
 Return the least power-of-2 that is at least equal to n.
 

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.
 
integer, parameter camp_util_convert_string_len = 100
 Length of string for converting numbers.
 
integer, parameter camp_max_filename_len = 300
 Maximum length of filenames.
 

Detailed Description

Common utility subroutines.

Function/Subroutine Documentation

◆ air_mean_free_path()

real(kind=dp) function camp_util::air_mean_free_path ( real(kind=dp), intent(in)  temp,
real(kind=dp), intent(in)  pressure 
)

Calculate air molecular mean free path \(l\) (m).

Parameters
[in]tempTemperature (K).
[in]pressurePressure (Pa).

Definition at line 350 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ almost_equal()

logical function camp_util::almost_equal ( real(kind=dp), intent(in)  d1,
real(kind=dp), intent(in)  d2,
real(kind=dp), intent(in), optional  rel_tol,
real(kind=dp), intent(in), optional  abs_tol 
)

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

Parameters
[in]d1First number to compare.
[in]d2Second number to compare.
[in]rel_tolUser-defined relative tolerance
[in]abs_tolUser-defined absolute tolerance

Relative tolerance.

Absolute tolerance.

Definition at line 379 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ almost_equal_abs()

logical function camp_util::almost_equal_abs ( real(kind=dp), intent(in)  d1,
real(kind=dp), intent(in)  d2,
real(kind=dp), intent(in)  abs_tol 
)

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

Parameters
[in]d1First number to compare.
[in]d2Second number to compare.
[in]abs_tolTolerance for when d1 equals d2.

Relative tolerance.

Definition at line 421 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ assert()

subroutine camp_util::assert ( integer, intent(in)  code,
logical, intent(in)  condition_ok 
)

Errors unless condition_ok is true.

Parameters
[in]codeStatus code to use if assertion fails.
[in]condition_okWhether the assertion is ok.

Definition at line 164 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ assert_msg()

subroutine camp_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
[in]codeStatus code to use if assertion fails.
[in]condition_okWhether the assertion is ok.
[in]error_msgMsg if assertion fails.

Definition at line 129 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ average_integer()

subroutine camp_util::average_integer ( integer, dimension(:), intent(in)  int_vec,
integer, intent(out)  int_avg 
)

Computes the average of an array of integer numbers.

Parameters
[in]int_vecArray of integer numbers.
[out]int_avgAverage of int_vec.

Definition at line 1226 of file util.F90.

◆ average_real()

subroutine camp_util::average_real ( real(kind=dp), dimension(:), intent(in)  real_vec,
real(kind=dp), intent(out)  real_avg 
)

Computes the average of an array of real numbers.

Parameters
[in]real_vecArray of real numbers.
[out]real_avgAverage of real_vec.

Definition at line 1240 of file util.F90.

◆ check_event()

subroutine camp_util::check_event ( real(kind=dp), intent(in)  time,
real(kind=dp), intent(in)  timestep,
real(kind=dp), intent(in)  interval,
real(kind=dp), 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
[in]timeCurrent time.
[in]timestepEstimate of the time to the next call.
[in]intervalHow often the event should be done.
[in,out]last_timeWhen the event was last done.
[out]do_eventWhether the event should be done.

Fuzz for event occurance.

Definition at line 482 of file util.F90.

◆ check_time_multiple()

subroutine camp_util::check_time_multiple ( character(len=*), intent(in)  first_name,
real(kind=dp), intent(in)  first_time,
character(len=*), intent(in)  second_name,
real(kind=dp), intent(in)  second_time 
)

Check that the first time interval is close to an integer multiple of the second, and warn if it is not.

Parameters
[in]first_nameName of the first time variable.
[in]first_timeFirst time variable (s).
[in]second_nameName of the second time variable.
[in]second_timeSecond time variable (s).

Definition at line 451 of file util.F90.

Here is the call graph for this function:

◆ close_file()

subroutine camp_util::close_file ( integer, intent(in)  unit)

Close a file and de-assign the unit.

Parameters
[in]unitUnit to close.

Definition at line 287 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ complex_to_string()

character(len=camp_util_convert_string_len) function camp_util::complex_to_string ( complex(kind=dc), intent(in)  val)

Convert a complex to a string format.

Parameters
[in]valValue to convert.

Definition at line 906 of file util.F90.

Here is the caller graph for this function:

◆ deg2rad()

real(kind=dp) function camp_util::deg2rad ( real(kind=dp), intent(in)  deg)

Convert degrees to radians.

Parameters
[in]degInput degrees.

Definition at line 1499 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ diam2rad()

real(kind=dp) elemental function camp_util::diam2rad ( real(kind=dp), intent(in)  d)

Convert diameter (m) to radius (m).

Parameters
[in]dDiameter (m).

Definition at line 338 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ die()

subroutine camp_util::die ( integer, intent(in)  code)

Error immediately.

Parameters
[in]codeStatus code to use if assertion fails.

Definition at line 183 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ die_msg()

subroutine camp_util::die_msg ( integer, intent(in)  code,
character(len=*), intent(in)  error_msg 
)

Error immediately.

Parameters
[in]codeStatus code to use if assertion fails.
[in]error_msgMsg if assertion fails.

Definition at line 195 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ensure_integer_array_2d_size()

subroutine camp_util::ensure_integer_array_2d_size ( integer, dimension(:, :), intent(inout), allocatable  x,
integer, intent(in)  n1,
integer, intent(in)  n2,
logical, intent(in), optional  only_grow 
)

Allocate or reallocate the given array to ensure it is of the given size, preserving any data and/or initializing to 0.

Parameters
[in,out]xArray of integer numbers.
[in]n1Desired first size of array.
[in]n2Desired second size of array.
[in]only_growWhether to only increase the array size (default .true.).

Definition at line 1381 of file util.F90.

◆ ensure_integer_array_size()

subroutine camp_util::ensure_integer_array_size ( integer, dimension(:), intent(inout), allocatable  x,
integer, intent(in)  n,
logical, intent(in), optional  only_grow 
)

Allocate or reallocate the given array to ensure it is of the given size, preserving any data and/or initializing to 0.

Parameters
[in,out]xArray of integer numbers.
[in]nDesired size of array.
[in]only_growWhether to only increase the array size (default .true.).

Definition at line 1347 of file util.F90.

◆ ensure_real_array_2d_size()

subroutine camp_util::ensure_real_array_2d_size ( real(kind=dp), dimension(:, :), intent(inout), allocatable  x,
integer, intent(in)  n1,
integer, intent(in)  n2,
logical, intent(in), optional  only_grow 
)

Allocate or reallocate the given array to ensure it is of the given size, preserving any data and/or initializing to 0.

Parameters
[in,out]xArray of real numbers.
[in]n1Desired first size of array.
[in]n2Desired second size of array.
[in]only_growWhether to only increase the array size (default .true.).

Definition at line 1307 of file util.F90.

◆ ensure_real_array_size()

subroutine camp_util::ensure_real_array_size ( real(kind=dp), dimension(:), intent(inout), allocatable  x,
integer, intent(in)  n,
logical, intent(in), optional  only_grow 
)

Allocate or reallocate the given array to ensure it is of the given size, preserving any data and/or initializing to 0.

Parameters
[in,out]xArray of real numbers.
[in]nDesired size of array.
[in]only_growWhether to only increase the array size (default .true.).

Definition at line 1273 of file util.F90.

◆ ensure_string_array_size()

subroutine camp_util::ensure_string_array_size ( character(len=*), dimension(:), intent(inout), allocatable  x,
integer, intent(in)  n 
)

Allocate or reallocate the given array to ensure it is of the given size, without preserving data.

Parameters
[in,out]xArray of strings numbers.
[in]nDesired size of array.

Definition at line 1421 of file util.F90.

◆ entropy()

real(kind=dp) function camp_util::entropy ( real(kind=dp), dimension(:), intent(in)  p)

Compute the entropy of a probability mass function (non necessarily normalized).

Parameters
[in]pProbability mass function, will be normalized before use.

Definition at line 1864 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ find_1d()

integer function camp_util::find_1d ( integer, intent(in)  n,
real(kind=dp), dimension(n), intent(in)  x_vals,
real(kind=dp), 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
[in]nNumber of values.
[in]x_valsX value array, must be sorted.
[in]xValue to interpolate at.

Definition at line 668 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ free_unit()

subroutine camp_util::free_unit ( integer, intent(in)  unit)

Frees a unit number returned by get_unit().

Definition at line 233 of file util.F90.

Here is the caller graph for this function:

◆ get_basename()

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

Strip the extension to find the basename.

The filename is assumed to be of the form basename.extension, where extension contains no periods. If there is no period in filename then basename is the whole filename.

Parameters
[in]filenameFull filename.
[out]basenameBasename.

Definition at line 1446 of file util.F90.

◆ get_unit()

integer function camp_util::get_unit

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

Definition at line 209 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ harmonic_mean()

elemental real(kind=dp) function camp_util::harmonic_mean ( real(kind=dp), intent(in)  x1,
real(kind=dp), intent(in)  x2 
)

Compute the harmonic mean of two numbers.

Parameters
[in]x1First number to average.
[in]x2Second number to average.

Definition at line 1833 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ integer_sort()

subroutine camp_util::integer_sort ( integer, dimension(:), intent(inout)  data,
integer, dimension(size(data)), intent(out)  perm 
)

Sort the given data array and return the permutation defining the sort.

If data is the original data array, new_data is the sorted value of data, and perm is the returned permutation, then new_data(i) = data(perm(i)).

Parameters
[in,out]dataData array to sort, sorted on exit.
[out]permPermutation defining the sort: new_data(i) = data(perm(i)).

Definition at line 1527 of file util.F90.

◆ integer_to_string()

character(len=camp_util_convert_string_len) function camp_util::integer_to_string ( integer, intent(in)  val)

Convert an integer to a string format.

Parameters
[in]valValue to convert.

Definition at line 838 of file util.F90.

Here is the caller graph for this function:

◆ integer_to_string_max_len()

character(len=camp_util_convert_string_len) function camp_util::integer_to_string_max_len ( integer, intent(in)  val,
integer, intent(in)  max_len 
)

Convert an integer to a string format of maximum length.

Parameters
[in]valValue to convert.
[in]max_lenMaximum length of resulting string.

Definition at line 923 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ interp_1d()

real(kind=dp) function camp_util::interp_1d ( real(kind=dp), dimension(:), intent(in)  x_vals,
real(kind=dp), dimension(size(x_vals)), intent(in)  y_vals,
real(kind=dp), 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
[in]x_valsX value array, must be sorted.
[in]y_valsY value array.
[in]xValue to interpolate at.

Definition at line 702 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ interp_linear_disc()

real(kind=dp) function camp_util::interp_linear_disc ( real(kind=dp), intent(in)  x_1,
real(kind=dp), intent(in)  x_n,
integer, intent(in)  n,
integer, intent(in)  i 
)

Linear interpolation over discrete indices.

Takes real values x_1 and x_n and integers n and i and returns the linear interpolation so that x_1 is returned when i = 1 and x_n is returned when i = n.

Parameters
[in]x_1Value of x when i = 1.
[in]x_nValue of x when i = n.
[in]nNumber of points to interpolate over.
[in]iIndex to interpolate at.

Definition at line 739 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ iso8601_date_and_time()

subroutine camp_util::iso8601_date_and_time ( character(len=*), intent(out)  date_time)

Current date and time in ISO 8601 format.

Parameters
[out]date_timeResult string.

Definition at line 1479 of file util.F90.

Here is the call graph for this function:

◆ linspace()

real(kind=dp) function, dimension(:), allocatable camp_util::linspace ( real(kind=dp), intent(in)  min_x,
real(kind=dp), intent(in)  max_x,
integer, intent(in)  n 
)

Makes a linearly spaced array from min to max.

Parameters
[in]min_xMinimum array value.
[in]max_xMaximum array value.
[in]nLength of array to create.
Returns
Return value.

Definition at line 536 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ linspace_find()

integer function camp_util::linspace_find ( real(kind=dp), intent(in)  min_x,
real(kind=dp), intent(in)  max_x,
integer, intent(in)  n,
real(kind=dp), 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, 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 > max_x then i = n. If x < min_x then i = 0. Thus 0 <= i <= n. Here n is the length of xa.

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

Parameters
[in]min_xMinimum array value.
[in]max_xMaximum array value.
[in]nNumber of entries.
[in]xValue.

Definition at line 608 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ loadtxt()

subroutine camp_util::loadtxt ( character(len=*), intent(in)  filename,
real(kind=dp), dimension(:,:), intent(inout), allocatable  data 
)

Load a real array from a text file.

Parameters
[in]filenameFilename to read from.
[in,out]dataArray to store data in.

Definition at line 1567 of file util.F90.

Here is the call graph for this function:

◆ logical_to_string()

character(len=camp_util_convert_string_len) function camp_util::logical_to_string ( logical, intent(in)  val)

Convert a logical to a string format.

Parameters
[in]valValue to convert.

Definition at line 886 of file util.F90.

Here is the caller graph for this function:

◆ logspace()

real(kind=dp) function, dimension(:), allocatable camp_util::logspace ( real(kind=dp), intent(in)  min_x,
real(kind=dp), intent(in)  max_x,
integer, intent(in)  n 
)

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

Parameters
[in]min_xMinimum array value.
[in]max_xMaximum array value.
[in]nLength of array to create.
Returns
Return value.

Definition at line 568 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ logspace_find()

integer function camp_util::logspace_find ( real(kind=dp), intent(in)  min_x,
real(kind=dp), intent(in)  max_x,
integer, intent(in)  n,
real(kind=dp), 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, 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 = 0. Thus 0 <= i <= n. Here n is the length of xa.

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

Parameters
[in]min_xMinimum array value.
[in]max_xMaximum array value.
[in]nNumber of entries.
[in]xValue.

Definition at line 642 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ nplogp()

elemental real(kind=dp) function camp_util::nplogp ( real(kind=dp), intent(in)  p)

Compute \( - p \ln p\) for computing entropy.

Parameters
[in]pProbability \(p\).

Definition at line 1847 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ open_file_read()

subroutine camp_util::open_file_read ( character(len=*), intent(in)  filename,
integer, intent(out)  unit 
)

Open a file for reading with an automatically assigned unit and test that it succeeds. The file should be closed with close_file().

Parameters
[in]filenameFilename to open.
[out]unitUnit assigned to file.

Definition at line 246 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ open_file_write()

subroutine camp_util::open_file_write ( character(len=*), intent(in)  filename,
integer, intent(out)  unit 
)

Open a file for writing with an automatically assigned unit and test that it succeeds. The file should be closed with close_file().

Parameters
[in]filenameFilename to open.
[out]unitUnit assigned to file.

Definition at line 267 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pow2_above()

integer function camp_util::pow2_above ( integer, intent(in)  n)

Return the least power-of-2 that is at least equal to n.

Parameters
[in]nLower bound on the power of 2.

Definition at line 1876 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ rad2deg()

real(kind=dp) function camp_util::rad2deg ( real(kind=dp), intent(in)  rad)

Convert radians to degrees.

Parameters
[in]radInput radians.

Definition at line 1511 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ rad2diam()

real(kind=dp) elemental function camp_util::rad2diam ( real(kind=dp), intent(in)  r)

Convert radius (m) to diameter (m).

Parameters
[in]rRadius (m).

Definition at line 313 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_char_raw()

subroutine camp_util::read_char_raw ( integer, intent(in)  unit,
character, intent(out)  char,
logical, intent(out)  eol,
logical, intent(out)  eof 
)

Read a single character from a file, signaling if we have hit end-of-line (EOL) or end-of-file (EOF). If EOL or EOF are true then the character value should be ignored.

Testing with gfortran 4.5.3 and different length files shows:

Empty file (total file length 0):

  • eol = .false., eof = .true.
  • subsequent calls error

File containing a single 'A' character (total file length 1):

  • char = 'A', eol = .false., eof = .false.
  • eol = .false., eof = .true.
  • subsequent calls error

File containing a single newline '
' (total file length 1):

  • eol = .true., eof = .false.
  • eol = .false., eof = .true.
  • subsequent calls error

File containing a character and newline 'A
' (total file length 2):

  • char = 'A', eol = .false., eof = .false.
  • eol = .true., eof = .false.
  • eol = .false., eof = .true.
  • subsequent calls error
    Parameters
    [in]unitUnit number to read from.
    [out]charCharacter read.
    [out]eolTrue if at EOL (end of line).
    [out]eofTrue if at EOF (end of file).

Definition at line 1722 of file util.F90.

Here is the caller graph for this function:

◆ read_word_raw()

subroutine camp_util::read_word_raw ( integer, intent(in)  unit,
character(len=*), intent(out)  word,
logical, intent(out)  eol,
logical, intent(out)  eof 
)

Read a white-space delimited word from a file, signaling if we have EOL or EOF. If EOL or EOF are true then the word will still be meaningful data. If there was no data to be read then len(word) will be 0.

Parameters
[in]unitUnit number to read from.
[out]wordWord read.
[out]eolTrue if at EOL (end of line).
[out]eofTrue if at EOF (end of file).

Definition at line 1766 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ real_dp_to_string()

character(len=camp_util_convert_string_len) function camp_util::real_dp_to_string ( real(kind=dp), intent(in)  val)

Convert a double precision real to a string format.

Parameters
[in]valValue to convert.

Definition at line 854 of file util.F90.

Here is the caller graph for this function:

◆ real_sp_to_string()

character(len=camp_util_convert_string_len) function camp_util::real_sp_to_string ( real(kind=sp), intent(in)  val)

Convert a single precision real to a string format.

Parameters
[in]valValue to convert.

Definition at line 870 of file util.F90.

Here is the caller graph for this function:

◆ real_to_string_max_len()

character(len=camp_util_convert_string_len) function camp_util::real_to_string_max_len ( real(kind=dp), intent(in)  val,
integer, intent(in)  max_len 
)

Convert a real to a string format of maximum length.

Parameters
[in]valValue to convert.
[in]max_lenMaximum length of resulting string.

Definition at line 944 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reallocate_real_array2d()

subroutine camp_util::reallocate_real_array2d ( real(kind=dp), dimension(:,:), intent(inout), allocatable  data,
integer, intent(in)  rows,
integer, intent(in)  cols 
)

Reallocate a 2D real array to the given size, preserving the contents.

Parameters
[in,out]dataArray to reallocate.
[in]rowsNew leading dimension.
[in]colsNew trailing dimension.

Definition at line 1674 of file util.F90.

Here is the caller graph for this function:

◆ savetxt_1d()

subroutine camp_util::savetxt_1d ( character(len=*), intent(in)  filename,
real(kind=dp), dimension(:), intent(in)  data 
)

Write a real 1D array to a text file.

Parameters
[in]filenameFilename to write to.
[in]dataArray of data to write.

Definition at line 1631 of file util.F90.

Here is the call graph for this function:

◆ savetxt_2d()

subroutine camp_util::savetxt_2d ( character(len=*), intent(in)  filename,
real(kind=dp), dimension(:,:), intent(in)  data 
)

Write a real 2D array to a text file.

Parameters
[in]filenameFilename to write to.
[in]dataArray of data to write.

Definition at line 1651 of file util.F90.

Here is the call graph for this function:

◆ sphere_rad2vol()

real(kind=dp) elemental function camp_util::sphere_rad2vol ( real(kind=dp), intent(in)  r)

Convert geometric radius \(R_{\rm geo}\) (m) to mass-equivalent volume \(V\) (m^3) for spherical particles.

Parameters
[in]rGeometric radius (m).

Definition at line 326 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sphere_vol2rad()

real(kind=dp) elemental function camp_util::sphere_vol2rad ( real(kind=dp), intent(in)  v)

Convert mass-equivalent volume \(V\) (m^3) to geometric radius \(R_{\rm geo}\) (m) for spherical particles.

Parameters
[in]vParticle mass-equivalent volume (m^3).

Definition at line 301 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ split_char()

type(string_t) function, dimension(:), allocatable camp_util::split_char ( class(string_t), intent(in)  this,
character(len=*), intent(in)  splitter,
logical, intent(in), optional  compress 
)

Splits a string on a substring.

Example:

type(string_t) :: my_string
type(string_t), allocatable :: sub_strings(:)
integer :: i
my_string = "my original string"
sub_strings = my_string%split( ' ' )
do i = 1, size( sub_strings )
write(*,*) i, sub_strings( i )
end do
sub_strings = my_string%split( ' ', .true. )
do i = 1, size( sub_strings )
write(*,*) i, sub_strings( i )
end do

Output:

1 my
2 original
3
4
5
6 string
1 my
2 original
3 string
Returns
Split string
Parameters
[in]thisFull string
[in]splitterString to split on
[in]compressCompress (default = false)

No 0-length substrings will be returned (adjacent tokens will be merged; tokens at the beginning and end of the original string will be ignored)

Definition at line 1068 of file util.F90.

Here is the caller graph for this function:

◆ split_string()

type(string_t) function, dimension(:), allocatable camp_util::split_string ( class(string_t), intent(in)  this,
type(string_t), intent(in)  splitter,
logical, intent(in), optional  compress 
)

Splits a string on a substring.

See string_split_char for description and example

Returns
Split string
Parameters
[in]thisFull string
[in]splitterString to split on
[in]compressCompress (default = false)

No 0-length substrings will be returned (adjacent tokens will be merged; tokens at the beginning and end of the original string will be ignored)

Definition at line 1160 of file util.F90.

Here is the caller graph for this function:

◆ starts_with()

logical function camp_util::starts_with ( character(len=*), intent(in)  string,
character(len=*), intent(in)  start_string 
)

Checks whether a string starts with a given other string.

starts_with(A, B) returns true if string A starts with string B.

Parameters
[in]stringString to test.
[in]start_stringStarting string.

Definition at line 1811 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ string_array_find()

integer function camp_util::string_array_find ( character(len=*), dimension(:), intent(in)  array,
character(len=*), intent(in)  val 
)

Return the index of the first occurance of the given value in the array, or 0 if it is not present.

Parameters
[in]arrayArray of values.
[in]valValue to find.

Definition at line 1255 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ string_t_constructor()

type(string_t) function, pointer camp_util::string_t_constructor ( character(len=:), intent(in), allocatable  val)

Constructor for string_t.

Returns
A new string
Parameters
[in]valString value

Definition at line 62 of file util.F90.

◆ string_t_finalize()

elemental subroutine camp_util::string_t_finalize ( type(string_t), intent(inout)  this)

Finalize a string.

Parameters
[in,out]thisString to finalize

Definition at line 77 of file util.F90.

Here is the caller graph for this function:

◆ string_to_integer()

integer function camp_util::string_to_integer ( character(len=*), intent(in)  string)

Convert a string to an integer.

Parameters
[in]stringString to convert.

Definition at line 764 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ string_to_logical()

logical function camp_util::string_to_logical ( character(len=*), intent(in)  string)

Convert a string to a logical.

Parameters
[in]stringString to convert.

Definition at line 807 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ string_to_real()

real(kind=dp) function camp_util::string_to_real ( character(len=*), intent(in)  string)

Convert a string to a real.

Parameters
[in]stringString to convert.

Definition at line 786 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ time_to_string_max_len()

character(len=camp_util_convert_string_len) function camp_util::time_to_string_max_len ( real(kind=dp), intent(in)  time,
integer, intent(in)  max_len 
)

Convert a time to a string format of maximum length.

Parameters
[in]timeTime to convert (s).
[in]max_lenMaximum length of resulting string.

Definition at line 999 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ vec_cts_to_disc()

subroutine camp_util::vec_cts_to_disc ( integer, intent(in)  n,
real(kind=dp), dimension(n), intent(in)  vec_cts,
integer, intent(in)  n_samp,
integer, dimension(n), intent(out)  vec_disc 
)

Convert a real-valued vector into an integer-valued vector.

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

Parameters
[in]nNumber of entries in vectors.
[in]vec_ctsContinuous vector.
[in]n_sampNumber of discrete samples to use.
[out]vec_discDiscretized vector.

Definition at line 1185 of file util.F90.

Here is the call graph for this function:

◆ warn_assert_msg()

subroutine camp_util::warn_assert_msg ( integer, intent(in)  code,
logical, intent(in)  condition_ok,
character(len=*), intent(in)  warning_msg 
)

Prints a warning message if condition_ok is false.

Parameters
[in]codeStatus code to use.
[in]condition_okWhether the assertion is ok.
[in]warning_msgMessage to display.

Definition at line 111 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ warn_msg()

subroutine camp_util::warn_msg ( integer, intent(in)  code,
character(len=*), intent(in)  warning_msg,
logical, intent(inout), optional  already_warned 
)

Prints a warning message.

Parameters
[in]codeStatus code to use.
[in]warning_msgMessage to display.
[in,out]already_warnedFlag to control warning only once (should be a save variable).

Definition at line 89 of file util.F90.

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ camp_max_filename_len

integer, parameter camp_util::camp_max_filename_len = 300

Maximum length of filenames.

Definition at line 29 of file util.F90.

◆ camp_util_convert_string_len

integer, parameter camp_util::camp_util_convert_string_len = 100

Length of string for converting numbers.

Definition at line 27 of file util.F90.

◆ max_units

integer, parameter camp_util::max_units = 200

Maximum number of IO units usable simultaneously.

Definition at line 20 of file util.F90.

◆ unit_offset

integer, parameter camp_util::unit_offset = 10

Minimum unit number to allocate.

Definition at line 22 of file util.F90.

◆ unit_used

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

Table of unit numbers storing allocation status.

Definition at line 24 of file util.F90.