MercuryDPM  Alpha
 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-2014, 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 class ParticleSpecies;
35 
36 namespace helpers
37 {
38 
42  class KAndDisp
43  {
44  public:
47  };
48 
55 
63 
70 
76 
83 
90 
97 
104 
111 
117 
124 
130 
137 
139 
144  {
145  public:
150  };
151 
158 
164  Mdouble getMaximumVelocity(Mdouble k, Mdouble disp, Mdouble radius, Mdouble mass);
165 
169  unsigned int getSaveCountFromNumberOfSavesAndTimeMaxAndTimestep(unsigned int numberOfSaves, Mdouble timeMax, Mdouble timestep);
170 
174  void getLineFromStringStream(std::istream& in, std::stringstream& out);
175 
179  bool writeToFile(std::string filename, std::string filecontent);
180 
184  bool addToFile(std::string filename, std::string filecontent);
185 
189  bool fileExists(std::string strFilename);
190 
194  bool openFile(std::fstream& file, std::string filename, std::fstream::openmode mode);
195 
199  Mdouble getEffectiveMass(Mdouble mass0, Mdouble mass1);
200 
201  std::vector<double> readArrayFromFile(std::string filename, int& n, int& m);
202 
203  template < typename T > std::string to_string( const T& n )
204  {
205  std::ostringstream stm ;
206  stm << n ;
207  return stm.str() ;
208  }
209 
210  //reads optional variables in the restart file (a variable is optional, if it may or may not be defined in the restart file)
211  template < typename T >
212  void readOptionalVariable(std::istream& is,std::string name, T& variable) {
214  if (is.peek() == name.c_str()[0])
215  {
216  std::string dummy;
217  is >> dummy >> variable;
218  }
219  }
220 
224  void loadingTest(const ParticleSpecies* species, Mdouble displacement, Mdouble velocity, Mdouble radius, std::string name);
225 
229  void normalAndTangentialLoadingTest(const ParticleSpecies *species, Mdouble displacement,
230  Mdouble tangentialDisplacement, Mdouble velocity, Mdouble radius,
231  std::string name);
232 
236  void objectivenessTest(const ParticleSpecies* species, Mdouble displacement, Mdouble tangentialDisplacement, Mdouble velocity, Mdouble radius, std::string name);
237 
259  template < typename T >
260  T readFromFile(std::string fileName, std::string varName, T value) {
261  //open filestream
262  std::ifstream is(fileName.c_str(), std::ios::in);
263  if (is.fail()) {
264  logger(INFO, "readFromFile: file % could not be opened, variable % set to default value %", fileName, varName, value);
265  return value;
266  }
267 
268  //read in variables, until the right one is fount
269  std::string s;
270  while(!is.eof()) {
271  is >> s;
272  if (!s.compare(varName)) {
273  is >> value;
274  logger(INFO,"readFromFile: variable % set to % ",varName,value);
275  return value;
276  }
277  }
278 
279  //if the right variable is never found
280  logger(WARN,"readFromFile: variable % not set in file %, set to default value % ", varName, fileName, value);
281  return value;
282  }
283 
284  bool compare(std::istream& is,std::string s);
285 
286  void check (double real, double ideal, double error, std::string errorMessage);
287 }
288 
289 #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 andtangential r...
Definition: Helpers.cc:309
bool writeToFile(std::string filename, std::string filecontent)
Writes a string to a file.
Definition: Helpers.cc:418
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:503
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
return type specifically for fuctions returning k and disp at once
Definition: Helpers.h:42
MERCURY_DEPRECATED Mdouble computeRestitutionCoefficientFromKAndDispAndEffectiveMass(Mdouble k, Mdouble disp, Mdouble mass)
Calculates the restitution coefficient time for a given stiffness, dissipation, and effective mass...
Definition: Helpers.cc:73
LL< Log::INFO > INFO
Info log level.
Definition: Logger.cc:53
MERCURY_DEPRECATED Mdouble computeRestitutionCoefficientFromKAndCollisionTimeAndEffectiveMass(Mdouble k, Mdouble tc, Mdouble mass)
Calculates the restitution coefficient for a given stiffness, collision time, and effective mass...
Definition: Helpers.cc:163
void check(double real, double ideal, double error, std::string errorMessage)
Definition: Helpers.cc:770
double Mdouble
T readFromFile(std::string fileName, std::string varName, T value)
Allows a quick read-in from a parameter file.
Definition: Helpers.h:260
return type specifically for fuctions returning k, disp, kt, dispt at once
Definition: Helpers.h:143
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:602
MERCURY_DEPRECATED Mdouble computeKFromCollisionTimeAndDispAndEffectiveMass(Mdouble tc, Mdouble disp, Mdouble mass)
Calculates the stiffness for a given collision time, dissipation, and effective mass.
Definition: Helpers.cc:228
#define MERCURY_DEPRECATED
Definition: GeneralDefine.h:37
MERCURY_DEPRECATED KAndDisp computeKAndDispFromCollisionTimeAndRestitutionCoefficientAndEffectiveMass(Mdouble tc, Mdouble r, Mdouble mass)
Set disp and k such that is matches a given collision time tc and restitution coefficient r for a col...
Definition: Helpers.cc:40
MERCURY_DEPRECATED Mdouble computeCollisionTimeFromKAndRestitutionCoefficientAndEffectiveMass(Mdouble k, Mdouble r, Mdouble mass)
Calculates the collision time for a given stiffness, restitution coefficient, and effective mass...
Definition: Helpers.cc:118
MERCURY_DEPRECATED Mdouble computeDispFromKAndRestitutionCoefficientAndEffectiveMass(Mdouble k, Mdouble r, Mdouble mass)
Calculates the dissipation for a given stiffness, restitution coefficient, and effective mass...
Definition: Helpers.cc:98
Mdouble disp
Definition: Helpers.h:46
LL< Log::WARN > WARN
Warning log level.
Definition: Logger.cc:52
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:396
MERCURY_DEPRECATED Mdouble computeCollisionTimeFromDispAndRestitutionCoefficientAndEffectiveMass(Mdouble disp, Mdouble r, Mdouble mass)
Calculates the collision time for a given dissipation, restitution coefficient, and effective mass...
Definition: Helpers.cc:288
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:535
MERCURY_DEPRECATED Mdouble computeRestitutionCoefficientFromCollisionTimeAndDispAndEffectiveMass(Mdouble tc, Mdouble disp, Mdouble mass)
Calculates the resitution coefficient for a given collision time, dissipation, and effective mass...
Definition: Helpers.cc:248
std::string to_string(const T &n)
Definition: Helpers.h:203
void readOptionalVariable(std::istream &is, std::string name, T &variable)
Definition: Helpers.h:212
MERCURY_DEPRECATED Mdouble computeKFromCollisionTimeAndRestitutionCoefficientAndEffectiveMass(Mdouble tc, Mdouble r, Mdouble mass)
Calculates the stiffness for a given collision time, restitution coefficient, and effective mass...
Definition: Helpers.cc:208
MERCURY_DEPRECATED Mdouble computeDispFromCollisionTimeAndRestitutionCoefficientAndEffectiveMass(Mdouble tc, Mdouble r, Mdouble mass)
Calculates the dissipation for a given collision time, restitution coefficient, and effective mass...
Definition: Helpers.cc:188
bool compare(std::istream &is, std::string s)
Definition: Helpers.cc:754
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:450
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:322
MERCURY_DEPRECATED Mdouble computeCollisionTimeFromKAndDispAndEffectiveMass(Mdouble k, Mdouble disp, Mdouble mass)
Calculates the collision time for a given stiffness, dissipation, and effective mass.
Definition: Helpers.cc:48
bool openFile(std::fstream &file, std::string filename, std::fstream::openmode mode)
Provides a simple interface for opening a file.
Definition: Helpers.cc:487
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:677
bool addToFile(std::string filename, std::string filecontent)
Adds a string to an existing file.
Definition: Helpers.cc:432
MERCURY_DEPRECATED Mdouble computeKFromDispAndRestitutionCoefficientAndEffectiveMass(Mdouble disp, Mdouble r, Mdouble mass)
Calculates the stiffness for a given dissipation, restitution coefficient, and effective mass...
Definition: Helpers.cc:268
MERCURY_DEPRECATED Mdouble computeDispFromKAndCollisionTimeAndEffectiveMass(Mdouble k, Mdouble tc, Mdouble mass)
Calculates the dissipation for a given stiffness, collision time, and effective mass.
Definition: Helpers.cc:138
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:350
std::vector< double > readArrayFromFile(std::string filename, int &n, int &m)
Definition: Helpers.cc:508