MercuryDPM  Trunk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Helpers.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 HELPERS_H
27 #define HELPERS_H
28 
29 #include "ExtendedMath.h"
30 #include <iostream> //std::istream and std::stringstream
31 #include <fstream> //std::fstream
32 #include <vector> //std::fstream
33 #include <Logger.h>
34 
35 class ParticleSpecies;
36 
37 namespace helpers
38 {
39  // returns the input string after converting upper-case characters to lower case
40  std::string lower(std::string s);
41 
45 class KAndDisp
46 {
47 public:
50 };
51 
52 // /*!
53 // * \brief Set disp and k such that is matches a given collision time tc and restitution coefficient r
54 // * for a collision of effective/reduced mass m.
55 // * \deprecated use species->setCollisionTimeAndRestitutionCoefficient
56 // * (collisionTime, dissipationTimeScale, 2.0*effectiveMass) instead
57 // */
58 // MERCURY_DEPRECATED
59 // KAndDisp computeKAndDispFromCollisionTimeAndRestitutionCoefficientAndEffectiveMass(Mdouble tc, Mdouble r, Mdouble mass);
60 //
61 // /*!
62 // * \brief Calculates the collision time for a given stiffness, dissipation, and effective mass
63 // * \deprecated use species->computeCollisionTime(2.0*effectiveMass) instead
64 // * \todo This does not result in the same value as the given alternative.
65 // */
66 // MERCURY_DEPRECATED
67 // Mdouble computeCollisionTimeFromKAndDispAndEffectiveMass(Mdouble k, Mdouble disp, Mdouble mass);
68 //
69 // /*!
70 // * \brief Calculates the restitution coefficient time for a given stiffness, dissipation, and effective mass
71 // * \deprecated use species->computeRestitutionCoefficient(2.0*effectiveMass) instead
72 // */
73 // MERCURY_DEPRECATED
74 // Mdouble computeRestitutionCoefficientFromKAndDispAndEffectiveMass(Mdouble k, Mdouble disp, Mdouble mass);
75 //
76 // /*!
77 // * \brief Calculates the dissipation for a given stiffness, restitution coefficient, and effective mass
78 // */
79 // MERCURY_DEPRECATED
80 // Mdouble computeDispFromKAndRestitutionCoefficientAndEffectiveMass(Mdouble k, Mdouble r, Mdouble mass);
81 //
82 // /*!
83 // * \brief Calculates the collision time for a given stiffness, restitution coefficient, and effective mass
84 // * \deprecated use species->computeCollisionTime(2.0*effectiveMass) instead
85 // */
86 // MERCURY_DEPRECATED
87 // Mdouble computeCollisionTimeFromKAndRestitutionCoefficientAndEffectiveMass(Mdouble k, Mdouble r, Mdouble mass);
88 //
89 // /*!
90 // * \brief Calculates the dissipation for a given stiffness, collision time, and effective mass
91 // * \deprecated use species->setStiffnessAndRestitutionCoefficient(2.0*effectiveMass) instead
92 // */
93 // MERCURY_DEPRECATED
94 // Mdouble computeDispFromKAndCollisionTimeAndEffectiveMass(Mdouble k, Mdouble tc, Mdouble mass);
95 //
96 // /*!
97 // * \brief Calculates the restitution coefficient for a given stiffness, collision time, and effective mass
98 // * \deprecated use species->computeRestitutionCoefficient(2.0*effectiveMass) instead
99 // */
100 // MERCURY_DEPRECATED
101 // Mdouble computeRestitutionCoefficientFromKAndCollisionTimeAndEffectiveMass(Mdouble k, Mdouble tc, Mdouble mass);
102 //
103 // /*!
104 // * \brief Calculates the dissipation for a given collision time, restitution coefficient, and effective mass
105 // * \deprecated use species->setCollisionTimeAndRestitutionCoefficient(2.0*effectiveMass) instead
106 // */
107 // MERCURY_DEPRECATED
108 // Mdouble computeDispFromCollisionTimeAndRestitutionCoefficientAndEffectiveMass(Mdouble tc, Mdouble r, Mdouble mass);
109 //
110 // /*!
111 // * \brief Calculates the stiffness for a given collision time, restitution coefficient, and effective mass
112 // * \deprecated use species->setCollisionTimeAndRestitutionCoefficient(2.0*effectiveMass) instead
113 // */
114 // MERCURY_DEPRECATED
115 // Mdouble computeKFromCollisionTimeAndRestitutionCoefficientAndEffectiveMass(Mdouble tc, Mdouble r, Mdouble mass);
116 //
117 // /*!
118 // * \brief Calculates the stiffness for a given collision time, dissipation, and effective mass
119 // */
120 // MERCURY_DEPRECATED
121 // Mdouble computeKFromCollisionTimeAndDispAndEffectiveMass(Mdouble tc, Mdouble disp, Mdouble mass);
122 //
123 // /*!
124 // * \brief Calculates the resitution coefficient for a given collision time, dissipation, and effective mass
125 // * \deprecated use species->computeRestitutionCoefficient(2.0*effectiveMass) instead
126 // */
127 // MERCURY_DEPRECATED
128 // Mdouble computeRestitutionCoefficientFromCollisionTimeAndDispAndEffectiveMass(Mdouble tc, Mdouble disp, Mdouble mass);
129 //
130 // /*!
131 // * \brief Calculates the stiffness for a given dissipation, restitution coefficient, and effective mass
132 // */
133 // MERCURY_DEPRECATED
134 // Mdouble computeKFromDispAndRestitutionCoefficientAndEffectiveMass(Mdouble disp, Mdouble r, Mdouble mass);
135 //
136 // /*!
137 // * \brief Calculates the collision time for a given dissipation, restitution coefficient, and effective mass
138 // * \deprecated use species->computeCollisionTime(2.0*effectiveMass) instead
139 // */
140 // MERCURY_DEPRECATED
141 // Mdouble computeCollisionTimeFromDispAndRestitutionCoefficientAndEffectiveMass(Mdouble disp, Mdouble r, Mdouble mass);
142 
144 
149 {
150 public:
155 };
156 
166  Mdouble tc, Mdouble r, Mdouble beta, Mdouble mass);
167 
175 
179 unsigned int getSaveCountFromNumberOfSavesAndTimeMaxAndTimeStep(unsigned int numberOfSaves, Mdouble timeMax,
180  Mdouble timeStep);
181 
185 void getLineFromStringStream(std::istream& in, std::stringstream& out);
186 
190 bool writeToFile(std::string filename, std::string filecontent);
191 
195 void writeCommandLineToFile(const std::string filename, const int argc, char * const argv[]);
196 
200 void gnuplot(std::string command);
201 
205 bool addToFile(std::string filename, std::string filecontent);
206 
210 bool fileExists(std::string strFilename);
211 
215 bool openFile(std::fstream& file, std::string filename, std::fstream::openmode mode);
216 
221 
222 std::vector<double> readArrayFromFile(std::string filename, int& n, int& m);
223 
224 void more(std::string filename, unsigned nLines = constants::unsignedMax);
225 
226 template<typename T>
227 std::string to_string(const T& n)
228 {
229  std::ostringstream stm;
230  stm << n;
231  return stm.str();
232 }
233 
234 std::string to_string(Mdouble value, unsigned precision);
235 
246 template<typename T>
247 bool readOptionalVariable(std::istream& is, const std::string& name, T& variable)
248 {
251  const auto pos = is.tellg();
252  std::string dummy;
253  is >> dummy;
254  if (dummy == name)
255  {
256  is >> variable;
257  return true;
258  } else {
259  is.seekg(pos);
260  return false;
261  }
262 }
263 
267 void loadingTest(const ParticleSpecies* species, Mdouble displacement, Mdouble velocity, Mdouble radius,
268  std::string name);
269 
273 void normalAndTangentialLoadingTest(const ParticleSpecies* species, Mdouble displacement,
274  Mdouble tangentialDisplacement, Mdouble velocity, Mdouble radius,
275  std::string name);
276 
280 void objectivenessTest(const ParticleSpecies* species, Mdouble displacement, Mdouble tangentialDisplacement,
281  Mdouble velocity, Mdouble radius, std::string name);
282 
286 bool compare(std::istream& is, std::string s);
287 
288 
310 template<typename T>
311 T readFromFile(std::string fileName, std::string varName, T value)
312 {
313  //open filestream
314  std::ifstream is(fileName.c_str(), std::ios::in);
315  if (is.fail())
316  {
317  logger(INFO, "readFromFile: file % could not be opened, variable % set to default value %",
318  fileName, varName, value);
319  return value;
320  }
321 
322  //read in variables, until the right one is fount
323  std::string s;
324  while (!is.eof())
325  {
326  is >> s;
327  if (!s.compare(varName))
328  {
329  is >> value;
330  logger(INFO, "readFromFile: variable % set to % ", varName, value);
331  return value;
332  }
333  }
334 
335  //if the right variable is never found
336  logger(WARN, "readFromFile: variable % not set in file %, using default value % ", varName, fileName, value);
337  return value;
338 }
339 
349 bool readFromCommandLine(int argc, char *argv[], std::string varName);
350 
351 template<typename T>
352 T readFromCommandLine(int argc, char *argv[], std::string varName, T value)
353 {
354  for (unsigned i=0; i<argc-1; ++i) {
355  if (varName == argv[i]) {
356  value = atof(argv[i+1]);
357  logger(INFO, "readFromCommandLine: % set to % ", varName.substr(1), value);
358  return value;
359  }
360  }
361  //if the variable is not found
362  logger(INFO, "readFromCommandLine: % set to default value % ", varName.substr(1), value);
363  return value;
364 }
365 
366 template<typename T, size_t n>
367 std::array<T,n> readArrayFromCommandLine(int argc, char *argv[], std::string varName, std::array<T,n> value)
368 {
369  for (unsigned i=0; i<argc-1; ++i) {
370  if (varName == argv[i]) {
371  unsigned j = i+1;
372  std::stringstream out;
373  for (auto& v : value) {
374  v = atof(argv[j]);
375  out << v << ' ';
376  ++j;
377  }
378  logger(INFO, "readFromCommandLine: % set to % ", varName.substr(1), out.str());
379  return value;
380  }
381  }
382  //if the variable is not found
383  std::stringstream out;
384  for (auto& v : value) out << v << ' ';
385  logger(INFO, "readFromCommandLine: % set to default value % ", varName.substr(1), out.str());
386  return value;
387 }
388 
389 template<typename T>
390 std::vector<T> readVectorFromCommandLine(int argc, char *argv[], std::string varName, size_t n, std::vector<T> values)
391 {
392  for (unsigned i=0; i<argc-1; ++i) {
393  if (varName == argv[i]) {
394  // read until the next argument starts
395  values.resize(0);
396  std::stringstream out;
397  for (int j = i+1; j<argc and argv[j][0]!='-'; ++j) {
398  values.push_back(atof(argv[j]));
399  out << values.back() << ' ';
400  }
401  logger(INFO, "readFromCommandLine: % set to % ", varName.substr(1), out.str());
402  return values;
403  }
404  }
405  //if the variable is not found
406  std::stringstream out;
407  for (auto& v : values) out << v << ' ';
408  logger(INFO, "readFromCommandLine: % set to default value % ", varName.substr(1), out.str());
409  return values;
410 }
411 
412 template<>
413 std::string readFromCommandLine<std::string>(int argc, char *argv[], std::string varName, std::string value);
414 
415 void check(double real, double ideal, double error, std::string errorMessage);
416 
417 void check(Vec3D real, Vec3D ideal, double error, std::string errorMessage);
418 
419 void check(Matrix3D real, Matrix3D ideal, double error, std::string errorMessage);
420 
421 void check(MatrixSymmetric3D real, MatrixSymmetric3D ideal, double error, std::string errorMessage);
422 
423 std::string getPath();
424 
426 
427 bool isNext(std::istream& is, const std::string name);
428 
429 bool createDirectory(std::string);
430 
431 Mdouble round(const Mdouble value, unsigned precision);
432 
433 /*
434  * \brief Returns the Rayleigh time step for a Hertz contact law.
435  * \detailed An accepted time step for Hertz is 10-20% of the Rayleigh time step.
436  * See \cite Marigo2015
437  */
438 Mdouble getRayleighTime(Mdouble radius, Mdouble shearModulus, Mdouble poisson, Mdouble density);
439 
440 }
441 
442 #endif
MERCURY_DEPRECATED KAndDispAndKtAndDispt computeDisptFromCollisionTimeAndRestitutionCoefficientAndTangentialRestitutionCoefficientAndEffectiveMass(Mdouble tc, Mdouble r, Mdouble beta, Mdouble mass)
Set disp, k, dispt and kt such that is matches a given collision time tc and a normal and tangential ...
Definition: Helpers.cc:330
bool writeToFile(std::string filename, std::string filecontent)
Writes a string to a file.
Definition: Helpers.cc:446
std::string lower(std::string s)
Definition: Helpers.cc:55
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:556
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
double Mdouble
Definition: GeneralDefine.h:34
return type specifically for fuctions returning k and disp at once
Definition: Helpers.h:45
std::string getPath()
Definition: Helpers.cc:918
LL< Log::INFO > INFO
Info log level.
Definition: Logger.cc:55
Mdouble getRayleighTime(Mdouble radius, Mdouble shearModulus, Mdouble poisson, Mdouble density)
Definition: Helpers.cc:986
Mdouble beta(Mdouble z, Mdouble w)
This is the beta function, returns the approximation based on cmath's implementation of ln(gamma) ...
void check(double real, double ideal, double error, std::string errorMessage)
Definition: Helpers.cc:898
const std::complex< Mdouble > i
Definition: ExtendedMath.h:50
T readFromFile(std::string fileName, std::string varName, T value)
Allows a quick read-in from a parameter file.
Definition: Helpers.h:311
Set disp and k such that is matches a given collision time tc and restitution coefficient r for a col...
Definition: Helpers.h:148
void normalAndTangentialLoadingTest(const ParticleSpecies *species, Mdouble displacement, Mdouble tangentialDisplacement, Mdouble velocity, Mdouble radius, std::string name)
Used to test the tangential loading/unloading/reloading properties of a frictional contact law...
Definition: Helpers.cc:694
#define MERCURY_DEPRECATED
Definition: GeneralDefine.h:37
const unsigned unsignedMax
Definition: GeneralDefine.h:46
Mdouble disp
Definition: Helpers.h:49
LL< Log::WARN > WARN
Warning log level.
Definition: Logger.cc:54
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:424
Mdouble getRealTime()
Definition: Helpers.cc:927
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:374
bool readOptionalVariable(std::istream &is, const std::string &name, T &variable)
Reads optional variables in the restart file.
Definition: Helpers.h:247
void loadingTest(const ParticleSpecies *species, Mdouble displacement, Mdouble velocity, Mdouble radius, std::string name)
Used to test the loading/unloading properties of a contact law.
Definition: Helpers.cc:620
void gnuplot(std::string command)
Plots to a gnuplot window.
Definition: Helpers.cc:470
bool createDirectory(std::string)
Definition: Helpers.cc:971
bool isNext(std::istream &is, const std::string name)
reads next value in stream as a string and compares it with name.
Definition: Helpers.cc:944
std::string to_string(const T &n)
Definition: Helpers.h:227
Mdouble round(const Mdouble value, unsigned precision)
Definition: Helpers.cc:600
bool readFromCommandLine(int argc, char *argv[], std::string varName)
Returns true if command line arguments contain varName, false else Usage example: if (readFromCommand...
Definition: Helpers.cc:876
bool compare(std::istream &is, std::string s)
Checks if the next argument in the input stream is a certain string.
Definition: Helpers.cc:860
void writeCommandLineToFile(const std::string filename, const int argc, char *const argv[])
Writes a string to a file.
Definition: Helpers.cc:460
std::vector< T > readVectorFromCommandLine(int argc, char *argv[], std::string varName, size_t n, std::vector< T > values)
Definition: Helpers.h:390
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:503
MERCURY_DEPRECATED Mdouble getMaximumVelocity(Mdouble k, Mdouble disp, Mdouble radius, Mdouble mass)
Calculates the maximum relative velocity allowed for a normal collision of two particles of radius r ...
Definition: Helpers.cc:346
Implementation of a 3D matrix.
Definition: Matrix.h:37
Definition: Vector.h:49
bool openFile(std::fstream &file, std::string filename, std::fstream::openmode mode)
Provides a simple interface for opening a file.
Definition: Helpers.cc:540
std::array< T, n > readArrayFromCommandLine(int argc, char *argv[], std::string varName, std::array< T, n > value)
Definition: Helpers.h:367
void objectivenessTest(const ParticleSpecies *species, Mdouble displacement, Mdouble tangentialDisplacement, Mdouble velocity, Mdouble radius, std::string name)
Used to test the tangential loading/unloading/reloading properties of a frictional contact law...
Definition: Helpers.cc:774
bool addToFile(std::string filename, std::string filecontent)
Adds a string to an existing file.
Definition: Helpers.cc:485
Implementation of a 3D symmetric matrix.
void more(std::string filename, unsigned nLines=constants::unsignedMax)
Definition: Helpers.cc:582
std::vector< double > readArrayFromFile(std::string filename, int &n, int &m)
Definition: Helpers.cc:561