revision: v0.14
ExtendedMath.h
Go to the documentation of this file.
1 //Copyright (c) 2013-2020, The MercuryDPM Developers Team. All rights reserved.
2 //For the list of developers, see <http://www.MercuryDPM.org/Team>.
3 //
4 //Redistribution and use in source and binary forms, with or without
5 //modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name MercuryDPM nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 //ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 //DISCLAIMED. IN NO EVENT SHALL THE MERCURYDPM DEVELOPERS TEAM BE LIABLE FOR ANY
19 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 //ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 //(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 #ifndef EXTENDEDMATH_H
27 #define EXTENDEDMATH_H
28 
29 #include <iostream> //std::istream and std::stringstream
30 #include <fstream> //std::fstream
31 #include <cmath>
32 #include <complex>
33 #include <limits>
34 
35 #include "NumericalVector.h"
36 #include "Vector.h"
37 #include "Quaternion.h"
38 
39 /*
40  * \brief
41  */
42 namespace constants
43 {
44 //Values from WolframAlpha
45 const Mdouble pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068;
46 const Mdouble sqrt_pi = 1.772453850905516027298167483341145182797549456122387128213807789852911284591032181374950656738544665;
47 const Mdouble sqr_pi = 9.869604401089358618834490999876151135313699407240790626413349376220044822419205243001773403718552232;
48 const Mdouble sqrt_2 = 1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641573;
49 const Mdouble sqrt_3 = 1.732050807568877293527446341505872366942805253810380628055806979451933016908800037081146186757248576;
50 const Mdouble R = 8.31446261815324;
51 const std::complex<Mdouble> i = {0.0, 1.0};
52 const Mdouble degree = pi / 180.; // degree-to-radian conversion
53 }
54 
58 namespace mathsFunc
59 {
63 Mdouble gamma(Mdouble gamma_in);
64 
69 
70 
74 Mdouble chi_squared(Mdouble x, unsigned int k);
75 
79 Mdouble chi_squared_prob(Mdouble x, unsigned int k);
80 
90 Mdouble goldenSectionSearch(Mdouble (* function)(const Mdouble), Mdouble min, Mdouble cur, Mdouble max,
91  Mdouble endCondition, Mdouble curVal = std::numeric_limits<Mdouble>::quiet_NaN());
92 
96 template<typename T>
97 int sign(T val)
98 {
99  return (T(0) < val) - (val < T(0));
100 }
101 
105 template<typename T>
106 T square(const T val)
107 {
108  return val * val;
109 }
110 
114 template<typename T>
115 T cubic(const T val)
116 {
117  return val * val * val;
118 }
119 
127 bool isEqual(Mdouble v1, Mdouble v2, Mdouble absError);
128 
136 bool isEqual(Vec3D v1, Vec3D v2, Mdouble absError);
137 
145 bool isEqual(Matrix3D m1, Matrix3D m2, Mdouble absError);
146 
147 bool isEqual(MatrixSymmetric3D m1, MatrixSymmetric3D m2, Mdouble absError);
148 
149 bool isEqual(Quaternion v1, Quaternion v2, double absError);
150 
154 template<typename T>
155 constexpr T factorial(const T t)
156 {
157  return (t == 0) ? 1 : t * factorial(t - 1);
158 }
159 
160 //platform independent implementation of sine and cosine, taken from
161 // http://stackoverflow.com/questions/18662261/fastest-implementation-of-sine-cosine-and-square-root-in-c-doesnt-need-to-b
162 // (cosine was implemented wrongly on the website, here is a corrected version)
163 
164 // sin(x) = x - x^3/3! + x^5/5! - x^7/7! + ...
165 Mdouble sin(Mdouble x);
166 
167 // cos(x) = 1 - x^2/2! + x^4/4! - x^6/6! + ...
168 Mdouble cos(Mdouble x);
169 
170 Mdouble exp(Mdouble Exponent);
171 
172 Mdouble log(Mdouble Power);
173 
174 
176 // tan=sin/cos
177 template<typename T>
178 T tan(T x)
179 {
180  return sin(x) / cos(x);
181 }
182 
183 
187 Mdouble chebyshev(Mdouble x, const Mdouble coef[], int N);
188 
190 
191 Mdouble I0(Mdouble x);
192 
193 }
194 
195 /*
196  * \brief Namespace for functions required to calculate spherical harmonics
197  */
198 
200 {
201 
202 //Compute all the associated LegenderePolynomials up to order n, and only positive order m at location x
204 
205 //Compute all spherical harmonics up to order p, at angles theta and phi
207 
208 //Compute all squaredFactorials (see eqn 5.23 in a short course on fast multipole methods) up to order p
210 }
211 
212 #endif
sphericalHarmonics::sphericalHarmonics
NumericalVector< std::complex< Mdouble > > sphericalHarmonics(int p, Mdouble theta, Mdouble phi)
Definition: ExtendedMath.cc:445
mathsFunc::square
T square(const T val)
squares a number
Definition: ExtendedMath.h:106
DPMBase::setName
void setName(const std::string &name)
Allows to set the name of all the files (ene, data, fstat, restart, stat)
Definition: DPMBase.cc:420
Matrix3D::YZ
Mdouble YZ
Definition: Matrix.h:43
DPMBase::setTimeStep
void setTimeStep(Mdouble newDt)
Sets a new value for the simulation time step.
Definition: DPMBase.cc:1225
DPMBase::getTimeStep
Mdouble getTimeStep() const
Returns the simulation time step.
Definition: DPMBase.cc:1241
Y
@ Y
Definition: StatisticsVector.h:42
Vector.h
constants::pi
const Mdouble pi
Definition: ExtendedMath.h:45
EnergyUnitTest
Definition: EnergyUnitTest.cpp:39
Flusher::FLUSH
@ FLUSH
Matrix3D::XX
Mdouble XX
all nine matrix elements
Definition: Matrix.h:43
LinearViscoelasticReversibleAdhesiveSpecies.h
Matrix3D::XZ
Mdouble XZ
Definition: Matrix.h:43
constants::sqr_pi
const Mdouble sqr_pi
Definition: ExtendedMath.h:47
BaseInteractable::setPosition
void setPosition(const Vec3D &position)
Sets the position of this BaseInteractable.
Definition: BaseInteractable.h:239
logger
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
Definition of different loggers with certain modules. A user can define its own custom logger here.
Matrix3D::YX
Mdouble YX
Definition: Matrix.h:43
mathsFunc::exp
Mdouble exp(Mdouble Exponent)
Definition: ExtendedMath.cc:84
mathsFunc
Namespace for some extra maths function that are often needed
Definition: ExtendedMath.h:59
Vec3D::X
Mdouble X
the vector components
Definition: Vector.h:65
NumericalVector
Definition: NumericalVector.h:64
DPMBase::setYMax
void setYMax(Mdouble newYMax)
Sets the value of YMax, the upper bound of the problem domain in the y-direction.
Definition: DPMBase.cc:1182
NumericalVector.h
sphericalHarmonics::computeSquaredFactorialValues
NumericalVector computeSquaredFactorialValues(int p)
Definition: ExtendedMath.cc:473
LinearViscoelasticIrreversibleAdhesiveSpecies.h
R
@ R
Definition: StatisticsVector.h:42
INFO
LL< Log::INFO > INFO
Info log level.
Definition: Logger.cc:55
mathsFunc::goldenSectionSearch
Mdouble goldenSectionSearch(Mdouble(*function)(const Mdouble), Mdouble min, Mdouble cur, Mdouble max, Mdouble endCondition, Mdouble curVal=std::numeric_limits< Mdouble >::quiet_NaN())
This function performs a golden section search to find the location of the minimum of a function.
Definition: ExtendedMath.cc:206
Quaternion::getComponent
Mdouble getComponent(int index) const
Returns the requested component of this Quaternion.
Definition: Quaternion.cc:232
mathsFunc::log
Mdouble log(Mdouble Power)
Definition: ExtendedMath.cc:104
BaseParticle::setRadius
virtual void setRadius(Mdouble radius)
Sets the particle's radius_ (and adjusts the mass_ accordingly, based on the particle's species)
Definition: BaseParticle.cc:542
Vec3D
Definition: Vector.h:50
DPMBase::solve
void solve()
The work horse of the code.
Definition: DPMBase.cc:4003
A
@ A
Definition: StatisticsVector.h:42
constants::R
const Mdouble R
Definition: ExtendedMath.h:50
DPMBase::setZMax
void setZMax(Mdouble newZMax)
Sets the value of ZMax, the upper bound of the problem domain in the z-direction.
Definition: DPMBase.cc:1208
Mdouble
double Mdouble
Definition: GeneralDefine.h:34
Matrix3D::YY
Mdouble YY
Definition: Matrix.h:43
Matrix3D::ZX
Mdouble ZX
Definition: Matrix.h:43
Matrix3D
Implementation of a 3D matrix.
Definition: Matrix.h:38
sphericalHarmonics::associatedLegendrePolynomials
NumericalVector associatedLegendrePolynomials(int n, Mdouble x)
Definition: ExtendedMath.cc:401
constants::sqrt_3
const Mdouble sqrt_3
Definition: ExtendedMath.h:49
BaseInteractable::setVelocity
void setVelocity(const Vec3D &velocity)
set the velocity of the BaseInteractable.
Definition: BaseInteractable.cc:350
mathsFunc::sign
int sign(T val)
This is a sign function, it returns -1 for negative numbers, 1 for positive numbers and 0 for 0.
Definition: ExtendedMath.h:97
double
ERROR
LL< Log::ERROR > ERROR
Error log level.
Definition: Logger.cc:53
DPMBase::getKineticEnergy
Mdouble getKineticEnergy() const
Returns the global kinetic energy stored in the system.
Definition: DPMBase.cc:1535
mathsFunc::sin
Mdouble sin(Mdouble x)
Definition: ExtendedMath.cc:44
Logger.h
BaseParticle::setSpecies
void setSpecies(const ParticleSpecies *species)
Definition: BaseParticle.cc:804
MatrixSymmetric3D::XZ
Mdouble XZ
Definition: MatrixSymmetric.h:42
Species
Contains material and contact force properties.
Definition: Species.h:35
DPMBase::setXMax
void setXMax(Mdouble newXMax)
Sets the value of XMax, the upper bound of the problem domain in the x-direction.
Definition: DPMBase.cc:1156
Matrix3D::ZY
Mdouble ZY
Definition: Matrix.h:43
ParticleHandler::clear
void clear() override
Empties the whole ParticleHandler by removing all BaseParticle.
Definition: ParticleHandler.cc:973
DPMBase::speciesHandler
SpeciesHandler speciesHandler
A handler to that stores the species type i.e. LinearViscoelasticSpecies, etc.
Definition: DPMBase.h:1385
MatrixSymmetric3D::ZZ
Mdouble ZZ
Definition: MatrixSymmetric.h:42
DPMBase::getElasticEnergy
Mdouble getElasticEnergy() const
Returns the global elastic energy within the system.
Definition: DPMBase.cc:1521
Quaternion.h
mathsFunc::chebyshev
Mdouble chebyshev(Mdouble x, const Mdouble coef[], int N)
Namespace for evaluating the zeroth modified Bessel function of the first kind, I0(x),...
Definition: ExtendedMath.cc:293
MatrixSymmetric3D::XY
Mdouble XY
Definition: MatrixSymmetric.h:42
DPMBase.h
SphericalParticle
A spherical particle is the most simple particle used in MercuryDPM.
Definition: SphericalParticle.h:37
constants::sqrt_pi
const Mdouble sqrt_pi
Definition: ExtendedMath.h:46
Matrix3D::ZZ
Mdouble ZZ
Definition: Matrix.h:43
DPMBase::setDimension
void setDimension(unsigned int newDim)
Sets both the system dimensions and the particle dimensionality.
Definition: DPMBase.cc:1394
sphericalHarmonics
Definition: ExtendedMath.h:200
DPMBase::setTimeMax
void setTimeMax(Mdouble newTMax)
Sets a new value for the maximum simulation duration.
Definition: DPMBase.cc:870
Vec3D::Y
Mdouble Y
Definition: Vector.h:65
DPMBase::setGravity
void setGravity(Vec3D newGravity)
Sets a new value for the gravitational acceleration.
Definition: DPMBase.cc:1374
constants::i
const std::complex< Mdouble > i
Definition: ExtendedMath.h:51
Quaternion
This class contains the 4 components of a quaternion and the standard operators and functions needed ...
Definition: Quaternion.h:63
DPMBase::setYMin
void setYMin(Mdouble newYMin)
Sets the value of YMin, the lower bound of the problem domain in the y-direction.
Definition: DPMBase.cc:1025
DPMBase::setFileType
void setFileType(FileType fileType)
Sets File::fileType_ for all files (ene, data, fstat, restart, stat)
Definition: DPMBase.cc:457
main
int main(int argc UNUSED, char *argv[] UNUSED)
Definition: EnergyUnitTest.cpp:71
BaseHandler::getObject
T * getObject(const unsigned int id)
Gets a pointer to the Object at the specified index in the BaseHandler.
Definition: BaseHandler.h:613
MatrixSymmetric3D::XX
Mdouble XX
The six distinctive matrix elements.
Definition: MatrixSymmetric.h:42
mathsFunc::tan
T tan(T x)
Definition: ExtendedMath.h:178
UNUSED
#define UNUSED
Definition: GeneralDefine.h:39
X
@ X
Definition: StatisticsVector.h:42
Matrix3D::XY
Mdouble XY
Definition: Matrix.h:43
BaseHandler::copyAndAddObject
std::enable_if<!std::is_pointer< U >::value, U * >::type copyAndAddObject(const U &object)
Creates a copy of a Object and adds it to the BaseHandler.
Definition: BaseHandler.h:379
n
const unsigned n
Definition: CG3DPackingUnitTest.cpp:32
constants::degree
const Mdouble degree
Definition: ExtendedMath.h:52
constants
Definition: GeneralDefine.h:42
DPMBase::setXMin
void setXMin(Mdouble newXMin)
Sets the value of XMin, the lower bound of the problem domain in the x-direction.
Definition: DPMBase.cc:1001
MatrixSymmetric3D::YZ
Mdouble YZ
Definition: MatrixSymmetric.h:42
mathsFunc::I0
Mdouble I0(Mdouble x)
Definition: ExtendedMath.cc:391
mathsFunc::beta
Mdouble beta(Mdouble z, Mdouble w)
This is the beta function, returns the approximation based on cmath's implementation of ln(gamma)
Definition: ExtendedMath.cc:164
constants::sqrt_2
const Mdouble sqrt_2
Definition: ExtendedMath.h:48
mathsFunc::factorial
constexpr T factorial(const T t)
factorial function
Definition: ExtendedMath.h:155
MatrixSymmetric3D
Implementation of a 3D symmetric matrix.
Definition: MatrixSymmetric.h:37
DPMBase::particleHandler
ParticleHandler particleHandler
An object of the class ParticleHandler, contains the pointers to all the particles created.
Definition: DPMBase.h:1395
Vec3D::Z
Mdouble Z
Definition: Vector.h:65
DPMBase::setSaveCount
void setSaveCount(unsigned int saveCount)
Sets File::saveCount_ for all files (ene, data, fstat, restart, stat)
Definition: DPMBase.cc:406
EnergyUnitTest::setupInitialConditions
void setupInitialConditions() override
This function allows to set the initial conditions for our problem to be solved, by default particle ...
Definition: EnergyUnitTest.cpp:42
ExtendedMath.h
mathsFunc::chi_squared
Mdouble chi_squared(Mdouble x, unsigned int k)
This is a chi_squared function return the value x and degrees of freedom k.
Definition: ExtendedMath.cc:172
mathsFunc::I0_exp
Mdouble I0_exp(Mdouble x)
Definition: ExtendedMath.cc:311
mathsFunc::cubic
T cubic(const T val)
calculates the cube of a number
Definition: ExtendedMath.h:115
mathsFunc::isEqual
bool isEqual(Mdouble v1, Mdouble v2, Mdouble absError)
Compares the difference of two Mdouble with an absolute error, useful in UnitTests.
Definition: ExtendedMath.cc:251
mathsFunc::gamma
Mdouble gamma(Mdouble gamma_in)
This is the gamma function returns the true value for the half integer value.
Definition: ExtendedMath.cc:137
DPMBase::setZMin
void setZMin(Mdouble newZMin)
Sets the value of ZMin, the lower bound of the problem domain in the z-direction.
Definition: DPMBase.cc:1049
LinearPlasticViscoelasticReversibleAdhesiveSpecies.h
mathsFunc::chi_squared_prob
Mdouble chi_squared_prob(Mdouble x, unsigned int k)
This is the function which actually gives the probability back using a chi squared test.
Definition: ExtendedMath.cc:188
MatrixSymmetric3D::YY
Mdouble YY
Definition: MatrixSymmetric.h:42
mathsFunc::cos
Mdouble cos(Mdouble x)
Definition: ExtendedMath.cc:64
DPMBase
The DPMBase header includes quite a few header files, defining all the handlers, which are essential....
Definition: DPMBase.h:76