MercuryDPM  0.11
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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"

Go to the source code of this file.

Functions

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

Function Documentation

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

Definition at line 33 of file HelperFunctionsUnitTest.cpp.

References ERROR, helpers::fileExists(), helpers::getEffectiveMass(), helpers::getLineFromStringStream(), helpers::getSaveCountFromNumberOfSavesAndTimeMaxAndTimestep(), mathsFunc::isEqual(), logger, MixedSpecies< NormalForceSpecies, FrictionForceSpecies, AdhesiveForceSpecies >::mixAll(), helpers::openFile(), and helpers::writeToFile().

34 {
35  std::cout << "Running HelperFunctionsUnitTest" << std::endl;
36 
37  std::cout << " Testing getSaveCountFromNumberOfSavesAndTimeMaxAndTimestep" << std::endl;
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  std::cout << " Testing getLineFromStringStream" << std::endl;
45  std::stringstream is("1.0\n2.000000000000001\n3.0");
46  std::stringstream line(std::stringstream::in | std::stringstream::out);
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  std::cout << " Testing writeToFile, openFile" << std::endl;
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  std::cout << " Testing writeToFile, openFile" << std::endl;
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  std::cout << " Testing getEffectiveMass" << std::endl;
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  std::cout << "Running LinearViscoelasticSpecies helper functions unit test" << std::endl;
96  Mdouble realStiffness = 2e5;
97  Mdouble realDissipation = 25.0;
98  Mdouble mass = 1.0; //particle mass
99  Mdouble radius = 0.5; //particle radius
100  Mdouble realCollisionTime = 0.004971179385062563;
101  Mdouble realRestitution = 0.883132984295725;
102  Mdouble realMaximumVelocity = 316.227766016838;
103 
104  LinearViscoelasticSpecies species0;
105  species0.setStiffness(realStiffness);
106  species0.setDissipation(realDissipation);
107 
108  std::cout << " Testing getMaximumVelocity" << std::endl;
109  Mdouble maximumVelocity = species0.getMaximumVelocity(radius, mass);
110  //std::cout << std::setprecision(16) << maximumVelocity << std::endl;
111  if (!mathsFunc::isEqual(maximumVelocity, realMaximumVelocity, 1e-10))
112  {
113  logger(ERROR, "maximum velocity is %, but should be %", maximumVelocity, realMaximumVelocity);
114  }
115 
116  std::cout << " Testing getCollisionTime, getRestitutionCoefficient" << std::endl;
117  Mdouble collisionTime = species0.getCollisionTime(mass);
118  Mdouble restitution = species0.getRestitutionCoefficient(mass);
119  if (!mathsFunc::isEqual(collisionTime, realCollisionTime, 1e-10))
120  {
121  logger(ERROR, "collision time is %, but should be %", collisionTime, realCollisionTime);
122  }
123  if (!mathsFunc::isEqual(restitution, realRestitution, 1e-10))
124  {
125  logger(ERROR, "restitution coefficient is %, but should be %", restitution, realRestitution);
126  }
127 
128  std::cout << " Testing setStiffnessAndRestitutionCoefficient" << std::endl;
129  species0.setStiffnessAndRestitutionCoefficient(realStiffness, realRestitution, mass);
130  if (!mathsFunc::isEqual(species0.getStiffness(), realStiffness, 1e-10))
131  {
132  logger(ERROR, "stiffness is %, but should be %", species0.getStiffness(), realStiffness);
133  }
134  if (!mathsFunc::isEqual(species0.getDissipation(), realDissipation, 1e-10))
135  {
136  logger(ERROR, "dissipation is %, but should be %", species0.getDissipation(), realDissipation);
137  }
138 
139  std::cout << " Testing setCollisionTimeAndRestitutionCoefficient" << std::endl;
140  species0.setCollisionTimeAndRestitutionCoefficient(realCollisionTime, realRestitution, mass);
141  if (!mathsFunc::isEqual(species0.getStiffness(), realStiffness, 1e-10))
142  {
143  logger(ERROR, "stiffness is %, but should be %", species0.getStiffness(), realStiffness);
144  }
145  if (!mathsFunc::isEqual(species0.getDissipation(), realDissipation, 1e-10))
146  {
147  logger(ERROR, "dissipation is %, but should be %", species0.getDissipation(), realDissipation);
148  }
149 
150  std::cout << " Testing setCollisionTimeAndRestitutionCoefficient for two masses" << std::endl;
151  species0.setCollisionTimeAndRestitutionCoefficient(realCollisionTime, realRestitution, mass, mass);
152  if (!mathsFunc::isEqual(species0.getStiffness(), realStiffness, 1e-10))
153  {
154  logger(ERROR, "stiffness is %, but should be %", species0.getStiffness(), realStiffness);
155  }
156  if (!mathsFunc::isEqual(species0.getDissipation(), realDissipation, 1e-10))
157  {
158  logger(ERROR, "dissipation is %, but should be %", species0.getDissipation(), realDissipation);
159  }
160 
161  std::cout << " Testing copy constructor" << std::endl;
162  LinearViscoelasticSpecies species1 = species0;
163  if (!mathsFunc::isEqual(species1.getStiffness(), realStiffness, 1e-10))
164  {
165  logger(ERROR, "stiffness is %, but should be %", species1.getStiffness(), realStiffness);
166  }
167  if (!mathsFunc::isEqual(species1.getDissipation(), realDissipation, 1e-10))
168  {
169  logger(ERROR, "dissipation is %, but should be %", species1.getDissipation(), realDissipation);
170  }
171 
172  std::cout << " Testing mix" << std::endl;
174  species2.mixAll(&species0, &species1);
175  if (!mathsFunc::isEqual(species2.getStiffness(), realStiffness, 1e-10))
176  {
177  logger(ERROR, "stiffness is %, but should be %", species2.getStiffness(), realStiffness);
178  }
179  if (!mathsFunc::isEqual(species2.getDissipation(), realDissipation, 1e-10))
180  {
181  logger(ERROR, "dissipation is %, but should be %", species2.getDissipation(), realDissipation);
182  }
183 
184  std::cout << "Running LinearPlasticViscoelasticSpecies helper functions unit test" << std::endl;
186 
187  std::cout << " Testing setPlasticParameters" << std::endl;
188  species3.setPlasticParameters(realStiffness, 2.0 * realStiffness, 0.5 * realStiffness, 0.5);
189  if (!mathsFunc::isEqual(species3.getLoadingStiffness(), realStiffness, 1e-10))
190  {
191  logger(ERROR, "stiffness is %, but should be %", species3.getLoadingStiffness(), realStiffness);
192  }
193  if (!mathsFunc::isEqual(species3.getUnloadingStiffnessMax(), 2.0 * realStiffness, 1e-10))
194  {
195  logger(ERROR, "max. unloading stiffness is %, but should be %", species3.getUnloadingStiffnessMax(), 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(), 0.5 * realStiffness);
200  }
201  if (!mathsFunc::isEqual(species3.getPenetrationDepthMax(), 0.5, 1e-10))
202  {
203  logger(ERROR, "dissipation is %, but should be %", species3.getPenetrationDepthMax(), 0.5);
204  }
205 
206  std::cout << " Testing setCollisionTimeAndRestitutionCoefficient" << std::endl;
207  species3.setCollisionTimeAndRestitutionCoefficient(realCollisionTime, realRestitution, mass);
208  if (!mathsFunc::isEqual(species3.getLoadingStiffness(), realStiffness, 1e-10))
209  {
210  logger(ERROR, "stiffness is %, but should be %", species3.getLoadingStiffness(), realStiffness);
211  }
212  if (!mathsFunc::isEqual(species3.getUnloadingStiffnessMax(), realStiffness, 1e-10))
213  {
214  logger(ERROR, "max. unloading stiffness is %, but should be %", species3.getUnloadingStiffnessMax(), realStiffness);
215  }
216  if (!mathsFunc::isEqual(species3.getDissipation(), realDissipation, 1e-10))
217  {
218  logger(ERROR, "dissipation is %, but should be %", species3.getDissipation(), realDissipation);
219  }
220 
221  std::cout << " Testing computeTimeStep" << std::endl;
222  Mdouble timeStep = species3.computeTimeStep(mass);
223  if (!mathsFunc::isEqual(timeStep, 0.02 * realCollisionTime, 1e-10))
224  {
225  logger(ERROR, "time step is %, but should be %", timeStep, 0.02 * realCollisionTime);
226  }
227 }
bool writeToFile(std::string filename, std::string filecontent)
Writes a string to a file.
Definition: Helpers.cc:411
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: Helpers.cc:482
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
double Mdouble
void mixAll(BaseSpecies *const S, BaseSpecies *const T)
sets the MixedSpecies properties by mixing the properties of two particle species ...
Definition: MixedSpecies.h:228
LL< Log::ERROR > ERROR
Error log level.
Definition: Logger.cc:26
void getLineFromStringStream(std::istream &in, std::stringstream &out)
Reads a line from one stringstream into another, and prepares the latter for reading in...
Definition: Helpers.cc:389
bool isEqual(Mdouble v1, Mdouble v2, double absError)
Compares the difference of two Mdouble with an absolute error, useful in UnitTests.
Contains contact force properties for contacts between particles with two different species...
Definition: MixedSpecies.h:40
Contains material and contact force properties.
Definition: Interaction.h:35
bool fileExists(std::string strFilename)
Function to check if a file exists, is used to check if a run has already need done.
Definition: Helpers.cc:429
bool openFile(std::fstream &file, std::string filename, std::fstream::openmode mode)
Provides a simple interface for opening a file.
Definition: Helpers.cc:466
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: Helpers.cc:343