HelperFunctionsUnitTest.cpp File Reference
#include "Logger.h"
#include "Species/LinearViscoelasticSpecies.h"
#include "Species/LinearPlasticViscoelasticSpecies.h"
#include "Math/ExtendedMath.h"
#include "Math/Helpers.h"
#include "iomanip"

Functions

int main (int argc UNUSED, char *argv[] UNUSED)
 

Function Documentation

◆ main()

int main ( int argc  UNUSED,
char *argv[]  UNUSED 
)
13 {
14  logger(VERBOSE, "Running HelperFunctionsUnitTest\n"
15  " Testing getSaveCountFromNumberOfSavesAndTimeMaxAndTimeStep");
16 
17  unsigned int saveCount = helpers::getSaveCountFromNumberOfSavesAndTimeMaxAndTimeStep(21, 10, 0.001);
18  if (saveCount != 501)
19  {
20  logger(ERROR, "save count is %, but should be %", saveCount, 501);
21  }
22 
23  logger(VERBOSE, " Testing getLineFromStringStream");
24  std::stringstream is("1.0\n2.000000000000001\n3.0");
25  std::stringstream line;
27  Mdouble value;
28  line >> value >> value; //the second value is not reading from the second line
29  if (value != 1.0)
30  {
31  logger(ERROR, "value is %, but should be %", value, 0.0);
32  }
34  line >> value; //reads double precision
35  //std::cout << std::setprecision(16) << value << std::endl;
36  if (value != 2.000000000000001)
37  {
38  logger(ERROR, "value is %, but should be %", value, 2.000000000000001);
39  }
40 
41  logger(VERBOSE, " Testing writeToFile, open");
42  helpers::writeToFile("HelperFunctionsUnitTest.ini",
43  "1 0 0 0 0 1 1 1\n"
44  "0.5 0.5 0 0 0 0.5 0 0 0 0 0 0 0 0\n");
45  std::fstream file;
46  helpers::openFile(file, "HelperFunctionsUnitTest.ini", std::fstream::in);
47  value = 0.0;
48  file >> value;
49  if (value != 1.0)
50  {
51  logger(ERROR, "value is %, but should be %", value, 1.0);
52  }
53 
54  logger(VERBOSE, " Testing writeToFile, open");
55  bool fileExists = helpers::fileExists("HelperFunctionsUnitTest.ini");
56  if (fileExists != true)
57  {
58  logger(ERROR, "HelperFunctionsUnitTest.ini exists, but is not detected");
59  }
60  fileExists = helpers::fileExists("HelperFunctionsUnitTest.out");
61  if (fileExists != false)
62  {
63  logger(ERROR, "HelperFunctionsUnitTest.out does not exist, but is detected");
64  }
65 
66  logger(VERBOSE, " Testing getEffectiveMass");
67  Mdouble effectiveMass = helpers::getEffectiveMass(1.0, 2.0);
68  if (!mathsFunc::isEqual(effectiveMass, 2.0 / 3.0, 1e-10))
69  {
70  logger(ERROR, "effective mass is %, but should be %", effectiveMass, 2.0 / 3.0);
71  }
72 
73 
74 
75  logger(INFO, " Testing removeFromCommandline");
76  char arguments[][20] = { "programname", "-test1", "-test2", "1"};
77  int argcTest = 4;
78  char** argvTest;
79  argvTest = new char*[argcTest];
80 
81  for (unsigned int i=0; i<argcTest; i++)
82  {
83  argvTest[i] = &arguments[i][0];
84  }
85 
86  if (!(helpers::readFromCommandLine(argcTest, argvTest, "-test1") && helpers::removeFromCommandline(argcTest, argvTest, "-test1", 0) && !helpers::readFromCommandLine(argcTest, argvTest, "-test1"))
87  || !(helpers::readFromCommandLine(argcTest, argvTest, "-test2", 2) == 1 && helpers::removeFromCommandline(argcTest, argvTest, "-test2", 1) && helpers::readFromCommandLine(argcTest, argvTest, "-test2", 2) == 2)
88  || argcTest>1 )
89  {
90  logger(ERROR, "helpers::removeFromCommandline not working as expected");
91  }
92 
93 
94  logger(VERBOSE, "Running LinearViscoelasticSpecies helper functions unit test");
95  Mdouble realStiffness = 2e5;
96  Mdouble realDissipation = 25.0;
97  Mdouble mass = 1.0; //particle mass
98  Mdouble radius = 0.5; //particle radius
99  Mdouble realCollisionTime = 0.004971179385062563;
100  Mdouble realRestitution = 0.883132984295725;
101  Mdouble realMaximumVelocity = 316.227766016838;
102 
103  LinearViscoelasticSpecies species0;
104  species0.setStiffness(realStiffness);
105  species0.setDissipation(realDissipation);
106 
107  logger(VERBOSE, " Testing getMaximumVelocity");
108  Mdouble maximumVelocity = species0.getMaximumVelocity(radius, mass);
109  //std::cout << std::setprecision(16) << maximumVelocity << std::endl;
110  if (!mathsFunc::isEqual(maximumVelocity, realMaximumVelocity, 1e-10))
111  {
112  logger(ERROR, "maximum velocity is %, but should be %", maximumVelocity, realMaximumVelocity);
113  }
114 
115  logger(VERBOSE, " Testing getCollisionTime, getRestitutionCoefficient");
116  Mdouble collisionTime = species0.getCollisionTime(mass);
117  Mdouble restitution = species0.getRestitutionCoefficient(mass);
118  if (!mathsFunc::isEqual(collisionTime, realCollisionTime, 1e-10))
119  {
120  logger(ERROR, "collision time is %, but should be %", collisionTime, realCollisionTime);
121  }
122  if (!mathsFunc::isEqual(restitution, realRestitution, 1e-10))
123  {
124  logger(ERROR, "restitution coefficient is %, but should be %", restitution, realRestitution);
125  }
126 
127  logger(VERBOSE, " Testing setStiffnessAndRestitutionCoefficient");
128  species0.setStiffnessAndRestitutionCoefficient(realStiffness, realRestitution, mass);
129  if (!mathsFunc::isEqual(species0.getStiffness(), realStiffness, 1e-10))
130  {
131  logger(ERROR, "stiffness is %, but should be %", species0.getStiffness(), realStiffness);
132  }
133  if (!mathsFunc::isEqual(species0.getDissipation(), realDissipation, 1e-10))
134  {
135  logger(ERROR, "dissipation is %, but should be %", species0.getDissipation(), realDissipation);
136  }
137 
138  logger(VERBOSE, " Testing setCollisionTimeAndRestitutionCoefficient");
139  species0.setCollisionTimeAndRestitutionCoefficient(realCollisionTime, realRestitution, mass);
140  if (!mathsFunc::isEqual(species0.getStiffness(), realStiffness, 1e-10))
141  {
142  logger(ERROR, "stiffness is %, but should be %", species0.getStiffness(), realStiffness);
143  }
144  if (!mathsFunc::isEqual(species0.getDissipation(), realDissipation, 1e-10))
145  {
146  logger(ERROR, "dissipation is %, but should be %", species0.getDissipation(), realDissipation);
147  }
148 
149  logger(VERBOSE, " Testing setCollisionTimeAndRestitutionCoefficient for two masses");
150  species0.setCollisionTimeAndRestitutionCoefficient(realCollisionTime, realRestitution, mass, mass);
151  if (!mathsFunc::isEqual(species0.getStiffness(), realStiffness, 1e-10))
152  {
153  logger(ERROR, "stiffness is %, but should be %", species0.getStiffness(), realStiffness);
154  }
155  if (!mathsFunc::isEqual(species0.getDissipation(), realDissipation, 1e-10))
156  {
157  logger(ERROR, "dissipation is %, but should be %", species0.getDissipation(), realDissipation);
158  }
159 
160  logger(VERBOSE, " Testing copy constructor");
161  LinearViscoelasticSpecies species1 = species0;
162  if (!mathsFunc::isEqual(species1.getStiffness(), realStiffness, 1e-10))
163  {
164  logger(ERROR, "stiffness is %, but should be %", species1.getStiffness(), realStiffness);
165  }
166  if (!mathsFunc::isEqual(species1.getDissipation(), realDissipation, 1e-10))
167  {
168  logger(ERROR, "dissipation is %, but should be %", species1.getDissipation(), realDissipation);
169  }
170 
171  logger(VERBOSE, " Testing mix");
173  species2.mixAll(&species0, &species1);
174  if (!mathsFunc::isEqual(species2.getStiffness(), realStiffness, 1e-10))
175  {
176  logger(ERROR, "stiffness is %, but should be %", species2.getStiffness(), realStiffness);
177  }
178  if (!mathsFunc::isEqual(species2.getDissipation(), realDissipation, 1e-10))
179  {
180  logger(ERROR, "dissipation is %, but should be %", species2.getDissipation(), realDissipation);
181  }
182 
183  logger(VERBOSE, "Running LinearPlasticViscoelasticSpecies helper functions unit test");
185 
186  logger(VERBOSE, " Testing setPlasticParameters");
187  species3.setPlasticParameters(realStiffness, 2.0 * realStiffness, 0.5 * realStiffness, 0.5);
188  if (!mathsFunc::isEqual(species3.getLoadingStiffness(), realStiffness, 1e-10))
189  {
190  logger(ERROR, "stiffness is %, but should be %", species3.getLoadingStiffness(), realStiffness);
191  }
192  if (!mathsFunc::isEqual(species3.getUnloadingStiffnessMax(), 2.0 * realStiffness, 1e-10))
193  {
194  logger(ERROR, "max. unloading stiffness is %, but should be %", species3.getUnloadingStiffnessMax(),
195  2.0 * realStiffness);
196  }
197  if (!mathsFunc::isEqual(species3.getCohesionStiffness(), 0.5 * realStiffness, 1e-10))
198  {
199  logger(ERROR, "max. unloading stiffness is %, but should be %", species3.getCohesionStiffness(),
200  0.5 * realStiffness);
201  }
202  if (!mathsFunc::isEqual(species3.getPenetrationDepthMax(), 0.5, 1e-10))
203  {
204  logger(ERROR, "dissipation is %, but should be %", species3.getPenetrationDepthMax(), 0.5);
205  }
206 
207  logger(VERBOSE, " Testing setCollisionTimeAndRestitutionCoefficient");
208  species3.setCollisionTimeAndRestitutionCoefficient(realCollisionTime, realRestitution, mass);
209  if (!mathsFunc::isEqual(species3.getLoadingStiffness(), realStiffness, 1e-10))
210  {
211  logger(ERROR, "stiffness is %, but should be %", species3.getLoadingStiffness(), realStiffness);
212  }
213  if (!mathsFunc::isEqual(species3.getUnloadingStiffnessMax(), realStiffness, 1e-10))
214  {
215  logger(ERROR, "max. unloading stiffness is %, but should be %", species3.getUnloadingStiffnessMax(),
216  realStiffness);
217  }
218  if (!mathsFunc::isEqual(species3.getDissipation(), realDissipation, 1e-10))
219  {
220  logger(ERROR, "dissipation is %, but should be %", species3.getDissipation(), realDissipation);
221  }
222 
223  logger(VERBOSE, " Testing computeTimeStep");
224  Mdouble timeStep = species3.computeTimeStep(mass);
225  if (!mathsFunc::isEqual(timeStep, 0.02 * realCollisionTime, 1e-10))
226  {
227  logger(ERROR, "time step is %, but should be %", timeStep, 0.02 * realCollisionTime);
228  }
229 }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Array< double, 1, 3 > e(1./3., 0.5, 2.)
LL< Log::VERBOSE > VERBOSE
Verbose information.
Definition: Logger.cc:36
Logger< MERCURYDPM_LOGLEVEL > logger("MercuryKernel")
Definition of different loggers with certain modules. A user can define its own custom logger here.
LL< Log::ERROR > ERROR
Error log level.
Definition: Logger.cc:32
void setStiffnessAndRestitutionCoefficient(Mdouble k_, Mdouble eps, Mdouble mass)
Sets k, disp such that it matches a given tc and eps for a collision of two copies of P.
Definition: LinearViscoelasticNormalSpecies.cc:165
Mdouble getMaximumVelocity(Mdouble radius, Mdouble mass) const
Calculates the maximum velocity allowed for a collision of two copies of P (for higher velocities par...
Definition: LinearViscoelasticNormalSpecies.cc:154
Mdouble getStiffness() const
Allows the spring constant to be accessed.
Definition: LinearViscoelasticNormalSpecies.cc:83
Mdouble getCollisionTime(Mdouble mass) const
Calculates collision time for two copies of a particle of given disp, k, mass.
Definition: LinearViscoelasticNormalSpecies.cc:116
void setDissipation(Mdouble dissipation)
Allows the normal dissipation to be changed.
Definition: LinearViscoelasticNormalSpecies.cc:96
void setCollisionTimeAndRestitutionCoefficient(Mdouble tc, Mdouble eps, BaseParticle *p)
Sets k, disp such that it matches a given tc and eps for a collision of two copies of particle p.
Definition: LinearViscoelasticNormalSpecies.cc:191
Mdouble getDissipation() const
Allows the normal dissipation to be accessed.
Definition: LinearViscoelasticNormalSpecies.cc:109
void setStiffness(Mdouble new_k)
Allows the spring constant to be changed.
Definition: LinearViscoelasticNormalSpecies.cc:72
Mdouble getRestitutionCoefficient(Mdouble mass) const
Calculates restitution coefficient for two copies of given disp, k, mass.
Definition: LinearViscoelasticNormalSpecies.cc:147
Contains contact force properties for contacts between particles with two different species.
Definition: MixedSpecies.h:22
void mixAll(BaseSpecies *const S, BaseSpecies *const T) final
sets the MixedSpecies properties by mixing the properties of two particle species
Definition: MixedSpecies.h:277
#define INFO(i)
Definition: mumps_solver.h:54
squared absolute value
Definition: GlobalFunctions.h:87
radius
Definition: UniformPSDSelfTest.py:15
line
Definition: calibrate.py:103
unsigned int getSaveCountFromNumberOfSavesAndTimeMaxAndTimeStep(unsigned int numberOfSaves, Mdouble timeMax, Mdouble timeStep)
Returns the correct saveCount if the total number of saves, the final time and the time step is known...
Definition: FormulaHelpers.cc:75
bool removeFromCommandline(int &argc, char *argv[], const std::string &varName, int nArgs)
May be used to hide arguments from argc and argv.
Definition: CommandLineHelpers.cc:41
bool fileExists(const std::string &strFilename)
Function to check if a file exists, is used to check if a run has already need done.
Definition: FileIOHelpers.cc:77
void getLineFromStringStream(std::istream &in, std::stringstream &out)
Reads a line from one stringstream into another, and prepares the latter for reading in.
Definition: StringHelpers.cc:41
bool openFile(std::fstream &file, const std::string &filename, std::fstream::openmode mode)
Provides a simple interface for opening a file.
Definition: FileIOHelpers.cc:115
bool readFromCommandLine(int argc, char *argv[], const std::string &varName)
Returns true if command line arguments contain varName, false else.
Definition: CommandLineHelpers.cc:99
Mdouble getEffectiveMass(Mdouble mass0, Mdouble mass1)
Calculates the effective mass of a particle pair, i.e. half the harmonic mean of two particle masses.
Definition: FormulaHelpers.cc:15
bool writeToFile(const std::string &filename, const std::string &filecontent)
Writes a string to a file.
Definition: FileIOHelpers.cc:29
bool isEqual(Mdouble v1, Mdouble v2, Mdouble absError)
Compares the difference of two Mdouble with an absolute error, useful in UnitTests.
Definition: ExtendedMath.cc:230

References e(), ERROR, helpers::fileExists(), LinearViscoelasticNormalSpecies::getCollisionTime(), LinearViscoelasticNormalSpecies::getDissipation(), helpers::getEffectiveMass(), helpers::getLineFromStringStream(), LinearViscoelasticNormalSpecies::getMaximumVelocity(), LinearViscoelasticNormalSpecies::getRestitutionCoefficient(), helpers::getSaveCountFromNumberOfSavesAndTimeMaxAndTimeStep(), LinearViscoelasticNormalSpecies::getStiffness(), i, INFO, mathsFunc::isEqual(), calibrate::line, logger, MixedSpecies< NormalForceSpecies, FrictionForceSpecies, AdhesiveForceSpecies >::mixAll(), helpers::openFile(), UniformPSDSelfTest::radius, helpers::readFromCommandLine(), helpers::removeFromCommandline(), LinearViscoelasticNormalSpecies::setCollisionTimeAndRestitutionCoefficient(), LinearViscoelasticNormalSpecies::setDissipation(), LinearViscoelasticNormalSpecies::setStiffness(), LinearViscoelasticNormalSpecies::setStiffnessAndRestitutionCoefficient(), Eigen::value, VERBOSE, and helpers::writeToFile().