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 
)
34 {
35  logger(VERBOSE, "Running HelperFunctionsUnitTest\n"
36  " Testing getSaveCountFromNumberOfSavesAndTimeMaxAndTimeStep");
37 
38  unsigned int saveCount = helpers::getSaveCountFromNumberOfSavesAndTimeMaxAndTimeStep(21, 10, 0.001);
39  if (saveCount != 501)
40  {
41  logger(ERROR, "save count is %, but should be %", saveCount, 501);
42  }
43 
44  logger(VERBOSE, " Testing getLineFromStringStream");
45  std::stringstream is("1.0\n2.000000000000001\n3.0");
46  std::stringstream line;
48  Mdouble value;
49  line >> value >> value; //the second value is not reading from the second line
50  if (value != 1.0)
51  {
52  logger(ERROR, "value is %, but should be %", value, 0.0);
53  }
55  line >> value; //reads double precision
56  //std::cout << std::setprecision(16) << value << std::endl;
57  if (value != 2.000000000000001)
58  {
59  logger(ERROR, "value is %, but should be %", value, 2.000000000000001);
60  }
61 
62  logger(VERBOSE, " Testing writeToFile, open");
63  helpers::writeToFile("HelperFunctionsUnitTest.ini",
64  "1 0 0 0 0 1 1 1\n"
65  "0.5 0.5 0 0 0 0.5 0 0 0 0 0 0 0 0\n");
66  std::fstream file;
67  helpers::openFile(file, "HelperFunctionsUnitTest.ini", std::fstream::in);
68  value = 0.0;
69  file >> value;
70  if (value != 1.0)
71  {
72  logger(ERROR, "value is %, but should be %", value, 1.0);
73  }
74 
75  logger(VERBOSE, " Testing writeToFile, open");
76  bool fileExists = helpers::fileExists("HelperFunctionsUnitTest.ini");
77  if (fileExists != true)
78  {
79  logger(ERROR, "HelperFunctionsUnitTest.ini exists, but is not detected");
80  }
81  fileExists = helpers::fileExists("HelperFunctionsUnitTest.out");
82  if (fileExists != false)
83  {
84  logger(ERROR, "HelperFunctionsUnitTest.out does not exist, but is detected");
85  }
86 
87  logger(VERBOSE, " Testing getEffectiveMass");
88  Mdouble effectiveMass = helpers::getEffectiveMass(1.0, 2.0);
89  if (!mathsFunc::isEqual(effectiveMass, 2.0 / 3.0, 1e-10))
90  {
91  logger(ERROR, "effective mass is %, but should be %", effectiveMass, 2.0 / 3.0);
92  }
93 
94 
95 
96  logger(INFO, " Testing removeFromCommandline");
97  char arguments[][20] = { "programname", "-test1", "-test2", "1"};
98  int argcTest = 4;
99  char** argvTest;
100  argvTest = new char*[argcTest];
101 
102  for (unsigned int i=0; i<argcTest; i++)
103  {
104  argvTest[i] = &arguments[i][0];
105  }
106 
107  if (!(helpers::readFromCommandLine(argcTest, argvTest, "-test1") && helpers::removeFromCommandline(argcTest, argvTest, "-test1", 0) && !helpers::readFromCommandLine(argcTest, argvTest, "-test1"))
108  || !(helpers::readFromCommandLine(argcTest, argvTest, "-test2", 2) == 1 && helpers::removeFromCommandline(argcTest, argvTest, "-test2", 1) && helpers::readFromCommandLine(argcTest, argvTest, "-test2", 2) == 2)
109  || argcTest>1 )
110  {
111  logger(ERROR, "helpers::removeFromCommandline not working as expected");
112  }
113 
114 
115  logger(VERBOSE, "Running LinearViscoelasticSpecies helper functions unit test");
116  Mdouble realStiffness = 2e5;
117  Mdouble realDissipation = 25.0;
118  Mdouble mass = 1.0; //particle mass
119  Mdouble radius = 0.5; //particle radius
120  Mdouble realCollisionTime = 0.004971179385062563;
121  Mdouble realRestitution = 0.883132984295725;
122  Mdouble realMaximumVelocity = 316.227766016838;
123 
124  LinearViscoelasticSpecies species0;
125  species0.setStiffness(realStiffness);
126  species0.setDissipation(realDissipation);
127 
128  logger(VERBOSE, " Testing getMaximumVelocity");
129  Mdouble maximumVelocity = species0.getMaximumVelocity(radius, mass);
130  //std::cout << std::setprecision(16) << maximumVelocity << std::endl;
131  if (!mathsFunc::isEqual(maximumVelocity, realMaximumVelocity, 1e-10))
132  {
133  logger(ERROR, "maximum velocity is %, but should be %", maximumVelocity, realMaximumVelocity);
134  }
135 
136  logger(VERBOSE, " Testing getCollisionTime, getRestitutionCoefficient");
137  Mdouble collisionTime = species0.getCollisionTime(mass);
138  Mdouble restitution = species0.getRestitutionCoefficient(mass);
139  if (!mathsFunc::isEqual(collisionTime, realCollisionTime, 1e-10))
140  {
141  logger(ERROR, "collision time is %, but should be %", collisionTime, realCollisionTime);
142  }
143  if (!mathsFunc::isEqual(restitution, realRestitution, 1e-10))
144  {
145  logger(ERROR, "restitution coefficient is %, but should be %", restitution, realRestitution);
146  }
147 
148  logger(VERBOSE, " Testing setStiffnessAndRestitutionCoefficient");
149  species0.setStiffnessAndRestitutionCoefficient(realStiffness, realRestitution, mass);
150  if (!mathsFunc::isEqual(species0.getStiffness(), realStiffness, 1e-10))
151  {
152  logger(ERROR, "stiffness is %, but should be %", species0.getStiffness(), realStiffness);
153  }
154  if (!mathsFunc::isEqual(species0.getDissipation(), realDissipation, 1e-10))
155  {
156  logger(ERROR, "dissipation is %, but should be %", species0.getDissipation(), realDissipation);
157  }
158 
159  logger(VERBOSE, " Testing setCollisionTimeAndRestitutionCoefficient");
160  species0.setCollisionTimeAndRestitutionCoefficient(realCollisionTime, realRestitution, mass);
161  if (!mathsFunc::isEqual(species0.getStiffness(), realStiffness, 1e-10))
162  {
163  logger(ERROR, "stiffness is %, but should be %", species0.getStiffness(), realStiffness);
164  }
165  if (!mathsFunc::isEqual(species0.getDissipation(), realDissipation, 1e-10))
166  {
167  logger(ERROR, "dissipation is %, but should be %", species0.getDissipation(), realDissipation);
168  }
169 
170  logger(VERBOSE, " Testing setCollisionTimeAndRestitutionCoefficient for two masses");
171  species0.setCollisionTimeAndRestitutionCoefficient(realCollisionTime, realRestitution, mass, mass);
172  if (!mathsFunc::isEqual(species0.getStiffness(), realStiffness, 1e-10))
173  {
174  logger(ERROR, "stiffness is %, but should be %", species0.getStiffness(), realStiffness);
175  }
176  if (!mathsFunc::isEqual(species0.getDissipation(), realDissipation, 1e-10))
177  {
178  logger(ERROR, "dissipation is %, but should be %", species0.getDissipation(), realDissipation);
179  }
180 
181  logger(VERBOSE, " Testing copy constructor");
182  LinearViscoelasticSpecies species1 = species0;
183  if (!mathsFunc::isEqual(species1.getStiffness(), realStiffness, 1e-10))
184  {
185  logger(ERROR, "stiffness is %, but should be %", species1.getStiffness(), realStiffness);
186  }
187  if (!mathsFunc::isEqual(species1.getDissipation(), realDissipation, 1e-10))
188  {
189  logger(ERROR, "dissipation is %, but should be %", species1.getDissipation(), realDissipation);
190  }
191 
192  logger(VERBOSE, " Testing mix");
194  species2.mixAll(&species0, &species1);
195  if (!mathsFunc::isEqual(species2.getStiffness(), realStiffness, 1e-10))
196  {
197  logger(ERROR, "stiffness is %, but should be %", species2.getStiffness(), realStiffness);
198  }
199  if (!mathsFunc::isEqual(species2.getDissipation(), realDissipation, 1e-10))
200  {
201  logger(ERROR, "dissipation is %, but should be %", species2.getDissipation(), realDissipation);
202  }
203 
204  logger(VERBOSE, "Running LinearPlasticViscoelasticSpecies helper functions unit test");
206 
207  logger(VERBOSE, " Testing setPlasticParameters");
208  species3.setPlasticParameters(realStiffness, 2.0 * realStiffness, 0.5 * realStiffness, 0.5);
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(), 2.0 * realStiffness, 1e-10))
214  {
215  logger(ERROR, "max. unloading stiffness is %, but should be %", species3.getUnloadingStiffnessMax(),
216  2.0 * realStiffness);
217  }
218  if (!mathsFunc::isEqual(species3.getCohesionStiffness(), 0.5 * realStiffness, 1e-10))
219  {
220  logger(ERROR, "max. unloading stiffness is %, but should be %", species3.getCohesionStiffness(),
221  0.5 * realStiffness);
222  }
223  if (!mathsFunc::isEqual(species3.getPenetrationDepthMax(), 0.5, 1e-10))
224  {
225  logger(ERROR, "dissipation is %, but should be %", species3.getPenetrationDepthMax(), 0.5);
226  }
227 
228  logger(VERBOSE, " Testing setCollisionTimeAndRestitutionCoefficient");
229  species3.setCollisionTimeAndRestitutionCoefficient(realCollisionTime, realRestitution, mass);
230  if (!mathsFunc::isEqual(species3.getLoadingStiffness(), realStiffness, 1e-10))
231  {
232  logger(ERROR, "stiffness is %, but should be %", species3.getLoadingStiffness(), realStiffness);
233  }
234  if (!mathsFunc::isEqual(species3.getUnloadingStiffnessMax(), realStiffness, 1e-10))
235  {
236  logger(ERROR, "max. unloading stiffness is %, but should be %", species3.getUnloadingStiffnessMax(),
237  realStiffness);
238  }
239  if (!mathsFunc::isEqual(species3.getDissipation(), realDissipation, 1e-10))
240  {
241  logger(ERROR, "dissipation is %, but should be %", species3.getDissipation(), realDissipation);
242  }
243 
244  logger(VERBOSE, " Testing computeTimeStep");
245  Mdouble timeStep = species3.computeTimeStep(mass);
246  if (!mathsFunc::isEqual(timeStep, 0.02 * realCollisionTime, 1e-10))
247  {
248  logger(ERROR, "time step is %, but should be %", timeStep, 0.02 * realCollisionTime);
249  }
250 }
double Mdouble
Definition: GeneralDefine.h:34
LL< Log::VERBOSE > VERBOSE
Verbose information.
Definition: Logger.cc:57
LL< Log::INFO > INFO
Info log level.
Definition: Logger.cc:55
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:53
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:186
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:175
Mdouble getStiffness() const
Allows the spring constant to be accessed.
Definition: LinearViscoelasticNormalSpecies.cc:104
Mdouble getCollisionTime(Mdouble mass) const
Calculates collision time for two copies of a particle of given disp, k, mass.
Definition: LinearViscoelasticNormalSpecies.cc:137
void setDissipation(Mdouble dissipation)
Allows the normal dissipation to be changed.
Definition: LinearViscoelasticNormalSpecies.cc:117
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:212
Mdouble getDissipation() const
Allows the normal dissipation to be accessed.
Definition: LinearViscoelasticNormalSpecies.cc:130
void setStiffness(Mdouble new_k)
Allows the spring constant to be changed.
Definition: LinearViscoelasticNormalSpecies.cc:93
Mdouble getRestitutionCoefficient(Mdouble mass) const
Calculates restitution coefficient for two copies of given disp, k, mass.
Definition: LinearViscoelasticNormalSpecies.cc:168
Contains contact force properties for contacts between particles with two different species.
Definition: MixedSpecies.h:43
void mixAll(BaseSpecies *const S, BaseSpecies *const T) final
sets the MixedSpecies properties by mixing the properties of two particle species
Definition: MixedSpecies.h:290
const std::complex< Mdouble > i
Definition: ExtendedMath.h:51
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:96
bool openFile(std::fstream &file, std::string filename, std::fstream::openmode mode)
Provides a simple interface for opening a file.
Definition: FileIOHelpers.cc:145
bool readFromCommandLine(int argc, char *argv[], std::string varName)
Returns true if command line arguments contain varName, false else.
Definition: CommandLineHelpers.cc:103
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:62
bool removeFromCommandline(int &argc, char *argv[], std::string varName, int nArgs)
May be used to hide arguments from argc and argv.
Definition: CommandLineHelpers.cc:43
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:36
bool writeToFile(std::string filename, std::string filecontent)
Writes a string to a file.
Definition: FileIOHelpers.cc:58
bool fileExists(std::string strFilename)
Function to check if a file exists, is used to check if a run has already need done.
Definition: FileIOHelpers.cc:107
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

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