Acoustic FDTD Solver
Functions
Utility.hpp File Reference
#include "constant.hpp"

Go to the source code of this file.

Functions

double * vector2array (const std::vector< double > &a)
 copy the content of the vector to an array, which can be directly written to the h5 file.
double * vector2array (const std::vector< double > &a, const unsigned size)
 copy the content of the vector to an array, specified by size.
int * vector2array (const std::vector< int > &a)
double ** vector2array (const std::vector< double > &a, const unsigned M, const unsigned N)
 copy the content of the vector to a MxN 2-D array.
void deleteArray (double *data)
 Free the memory space created by vector2array()
void deleteArray (double **data)
 Free the memory space created by vector2array()
std::vector< std::vector
< double > > 
vector2vector (const std::vector< double > &a, const int N, const int M)
std::vector< double > array2vector (const double *a, const int size)
std::vector< std::vector
< double > > 
array2vector (const double *a, const int N, const int M)
 create a 2-D array from a 1-D array specified by N rows and M columns.
double str2num (const std::string &s)
 Use the std::istd::stringstream to perform the conversion.
std::string num2str (const double x)
 Use the std::ostd::stringstream to perform the conversion.
double matlabfmod (const double x, const double y)
 c++ fmod() is not consistent with Matlab fmod() when handling negative number.
std::string getRandomFileName (std::string &prefix)
 Generate a h5 file name in format of "prefix_year_month_day_hour_minute_random.h5".
int getNextPower (int N, int base)
void writeToTxtFile (const std::string file_name, const std::vector< double > &data)
void writeToTxtFile (const std::string file_name, const std::vector< std::vector< double > > &data)
double bandwidthToGaussianStdDev (unsigned bandwidth_defn, double bandwidth)
std::vector< double > solveTridiagonalSystem (const std::vector< double > &a, const std::vector< double > &b, const std::vector< double > &c, const std::vector< double > &d)
 solve Ax = d problem, where A is a tri-diagonal system specified by its lower, main, and upper diagonal.
std::vector< double > solveLinearSystem (const std::vector< std::vector< double > > &S, const std::vector< double > &rhs)
 Solve the linear system S x = t.
std::vector< double > cubic_spline_interp (const std::vector< double > &x, const std::vector< double > &y, const std::vector< double > &xi)
 Use cubic spline to Interpolate xi based on the data (x, y).
double linear_interp (const std::vector< double > &x, const std::vector< double > &y, const std::vector< std::vector< double > > &z, const double xi, const double yi)
 Bi-linear interpolation over one grid onto another point.
void convertIndex (const int index, const int nZ, int j, int k)
 Convert the integer index to the indices of the two-dimensional vector.
void convertIndex (const int index, const int nY, const int nZ, int i, int j, int k)
 Convert the integer index to the indices of the three-dimensional vector.
std::vector< int > getBilinearInterpolationIndex (const std::vector< double > &z, const double zi)
std::vector< int > getBilinearInterpolationIndex (const std::vector< double > &y, const std::vector< double > &z, const double yi, const double zi)
std::vector< int > getBilinearInterpolationIndex (const std::vector< double > &x, const std::vector< double > &y, const std::vector< double > &z, const double xi, const double yi, const double zi)
 To be implemented.
std::vector< double > getBilinearInterpolationCoefficient (const std::vector< double > &z, const double zi)
std::vector< double > getBilinearInterpolationCoefficient (const std::vector< double > &y, const std::vector< double > &z, const double yi, const double zi)
std::vector< double > getBilinearInterpolationCoefficient (const std::vector< double > &x, const std::vector< double > &y, const std::vector< double > &z, const double xi, const double yi, const double zi)
 To be implemented.
std::vector< std::vector
< double > > 
cubic_spline_interp (const std::vector< double > &x, const std::vector< double > &y, const std::vector< std::vector< double > > &z, const std::vector< double > &xi, const std::vector< double > &yi)
std::vector< double > poly_interp (const std::vector< double > &x, const std::vector< double > &y, const std::vector< double > &xi)
 Polynomial interpolation.
int findEqual (const std::vector< double > &reference, const double x, int i, int j)
 Find the index of the element which is equal to x.
std::vector< int > findEqual (const std::vector< double > &reference, const double x)
 Find the index of the elements that are equal to x.
std::vector< int > findEqual (const std::vector< std::vector< double > > &reference, const double x)
bool approximatelyEqual (const double a, const double b, const double epsilon)
 See knuth book on double comparision.
bool essentiallyEqual (const double a, const double b, const double epsilon)
 See knuth book.
bool definitelyGreaterThan (const double a, const double b, const double epsilon)
 See knuth book.
bool definitelyLessThan (const double a, const double b, const double epsilon)
 See knuth book.
std::string secondToTime (int seconds)
 Parse the input to the string form of "XX hours XX minutes XX seconds".
std::vector< double > estimateAmplitudePhase (const std::vector< double > &data, const double frequency, const double delta_t)
double cot (const double x)
double acot (const double x)
bool isEven (unsigned n)
bool isOdd (unsigned n)

Function Documentation

double acot ( const double  x)
bool approximatelyEqual ( const double  a,
const double  b,
const double  epsilon 
)

See knuth book on double comparision.

Parameters
afirst parameter
bsecond parameter
epsilondepends on the context, and determines how equal you want the numbers to be.
std::vector<double> array2vector ( const double *  a,
const int  size 
)
std::vector<std::vector<double> > array2vector ( const double *  a,
const int  N,
const int  M 
)

create a 2-D array from a 1-D array specified by N rows and M columns.

double bandwidthToGaussianStdDev ( unsigned  bandwidth_defn,
double  bandwidth 
)
void convertIndex ( const int  index,
const int  nZ,
int  j,
int  k 
)

Convert the integer index to the indices of the two-dimensional vector.

Parameters
&index
&nZ
&jcontain returned value
&kcontain returned value
void convertIndex ( const int  index,
const int  nY,
const int  nZ,
int  i,
int  j,
int  k 
)

Convert the integer index to the indices of the three-dimensional vector.

Parameters
&index
&nY
&nZ
&icontain returned value
&jcontain returned value
&kcontain returned value
double cot ( const double  x)
std::vector<double> cubic_spline_interp ( const std::vector< double > &  x,
const std::vector< double > &  y,
const std::vector< double > &  xi 
)

Use cubic spline to Interpolate xi based on the data (x, y).

From the end points of (x,y), it estimates the the second derivative and uses them as the boudnary boundary condition. If xi falls outside of x, return 0.

Parameters
xUniformly spaced sample point
y
xiCan be nonuniformly spaced sample point
std::vector<std::vector<double> > cubic_spline_interp ( const std::vector< double > &  x,
const std::vector< double > &  y,
const std::vector< std::vector< double > > &  z,
const std::vector< double > &  xi,
const std::vector< double > &  yi 
)
Parameters
xUniformly spaced sample point
yUniformly spaced sample point
z
xiCan be non-uniformly spaced sample point
yiCan be non-uniformly spaced sample point
bool definitelyGreaterThan ( const double  a,
const double  b,
const double  epsilon 
)

See knuth book.

Parameters
afirst parameter
bsecond parameter
epsilondepends on the context, and determines how equal you want the numbers to be.
bool definitelyLessThan ( const double  a,
const double  b,
const double  epsilon 
)

See knuth book.

Parameters
afirst parameter
bsecond parameter
epsilondepends on the context, and determines how equal you want the numbers to be.
void deleteArray ( double *  data)

Free the memory space created by vector2array()

void deleteArray ( double **  data)

Free the memory space created by vector2array()

bool essentiallyEqual ( const double  a,
const double  b,
const double  epsilon 
)

See knuth book.

Parameters
afirst parameter
bsecond parameter
epsilondepends on the context, and determines how equal you want the numbers to be.
std::vector<double> estimateAmplitudePhase ( const std::vector< double > &  data,
const double  frequency,
const double  delta_t 
)
int findEqual ( const std::vector< double > &  reference,
const double  x,
int  i,
int  j 
)

Find the index of the element which is equal to x.

Using binary search.

Parameters
referencean increasing array without repeating element
xspecified value
istarting index of reference to search
jending index of reference to search
Returns
the index of the element that is equal to x. -1 if otherwise
std::vector<int> findEqual ( const std::vector< double > &  reference,
const double  x 
)

Find the index of the elements that are equal to x.

std::vector<int> findEqual ( const std::vector< std::vector< double > > &  reference,
const double  x 
)
std::vector<double> getBilinearInterpolationCoefficient ( const std::vector< double > &  z,
const double  zi 
)
std::vector<double> getBilinearInterpolationCoefficient ( const std::vector< double > &  y,
const std::vector< double > &  z,
const double  yi,
const double  zi 
)
std::vector<double> getBilinearInterpolationCoefficient ( const std::vector< double > &  x,
const std::vector< double > &  y,
const std::vector< double > &  z,
const double  xi,
const double  yi,
const double  zi 
)

To be implemented.

std::vector<int> getBilinearInterpolationIndex ( const std::vector< double > &  z,
const double  zi 
)
Returns
the integer index of the left and right neighbour.
std::vector<int> getBilinearInterpolationIndex ( const std::vector< double > &  y,
const std::vector< double > &  z,
const double  yi,
const double  zi 
)
Returns
the integer index of the four neighbour. Should be used with Utility::convertIndex()
std::vector<int> getBilinearInterpolationIndex ( const std::vector< double > &  x,
const std::vector< double > &  y,
const std::vector< double > &  z,
const double  xi,
const double  yi,
const double  zi 
)

To be implemented.

int getNextPower ( int  N,
int  base 
)
std::string getRandomFileName ( std::string &  prefix)

Generate a h5 file name in format of "prefix_year_month_day_hour_minute_random.h5".

Returns
std::string the file name
bool isEven ( unsigned  n)
bool isOdd ( unsigned  n)
double linear_interp ( const std::vector< double > &  x,
const std::vector< double > &  y,
const std::vector< std::vector< double > > &  z,
const double  xi,
const double  yi 
)

Bi-linear interpolation over one grid onto another point.

double matlabfmod ( const double  x,
const double  y 
)

c++ fmod() is not consistent with Matlab fmod() when handling negative number.

This function follows the Matlab convention.

std::string num2str ( const double  x)

Use the std::ostd::stringstream to perform the conversion.

std::vector<double> poly_interp ( const std::vector< double > &  x,
const std::vector< double > &  y,
const std::vector< double > &  xi 
)

Polynomial interpolation.

Solving the linear sytem using Boost.

std::string secondToTime ( int  seconds)

Parse the input to the string form of "XX hours XX minutes XX seconds".

Parameters
secondsstd::difftime().
std::vector<double> solveLinearSystem ( const std::vector< std::vector< double > > &  S,
const std::vector< double > &  rhs 
)

Solve the linear system S x = t.

It actually uses LU in Boost to invert S' S, and compute x = inv(S' S) S' t.

Parameters
SNxN matrix
rhsright hand side
std::vector<double> solveTridiagonalSystem ( const std::vector< double > &  a,
const std::vector< double > &  b,
const std::vector< double > &  c,
const std::vector< double > &  d 
)

solve Ax = d problem, where A is a tri-diagonal system specified by its lower, main, and upper diagonal.

See [Schwarz1989] for the algorithm to perform LU decomposition on A and use backward/forwad substitution solve the system.

Parameters
amain diagonal
blower diagonal
cupper diagonal
dright hand side
Returns
x
double str2num ( const std::string &  s)

Use the std::istd::stringstream to perform the conversion.

double* vector2array ( const std::vector< double > &  a)

copy the content of the vector to an array, which can be directly written to the h5 file.

This method dynamically creates an array using new and returns the pointer to it. Cannot be used too often to avoid allocating too much memory space.

Parameters
avector<double>
double* vector2array ( const std::vector< double > &  a,
const unsigned  size 
)

copy the content of the vector to an array, specified by size.

If size is longer than the size of the vector, 0 will be padded at the end.

int* vector2array ( const std::vector< int > &  a)
double** vector2array ( const std::vector< double > &  a,
const unsigned  M,
const unsigned  N 
)

copy the content of the vector to a MxN 2-D array.

The array occupies an contiguous space in the memory.

std::vector<std::vector<double> > vector2vector ( const std::vector< double > &  a,
const int  N,
const int  M 
)
void writeToTxtFile ( const std::string  file_name,
const std::vector< double > &  data 
)
void writeToTxtFile ( const std::string  file_name,
const std::vector< std::vector< double > > &  data 
)