MercuryDPM  Trunk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
helpers Namespace Reference

Classes

class  KAndDisp
 return type specifically for fuctions returning k and disp at once More...
 
class  KAndDispAndKtAndDispt
 Set disp and k such that is matches a given collision time tc and restitution coefficient r for a collision of effective/reduced mass m. More...
 

Functions

std::string lower (std::string s)
 
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 restitution coefficient r, beta for a collision of effective/reduced mass m. From Deen...Kuipers2006, eq. 43 and 30. More...
 
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 and particle mass m (for higher velocities particles could pass through each other) More...
 
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. More...
 
void getLineFromStringStream (std::istream &in, std::stringstream &out)
 Reads a line from one stringstream into another, and prepares the latter for reading in. More...
 
bool writeToFile (std::string filename, std::string filecontent)
 Writes a string to a file. More...
 
void writeCommandLineToFile (const std::string filename, const int argc, char *const argv[])
 Writes a string to a file. More...
 
void gnuplot (std::string command)
 Plots to a gnuplot window. More...
 
bool addToFile (std::string filename, std::string filecontent)
 Adds a string to an existing file. More...
 
bool fileExists (std::string strFilename)
 Function to check if a file exists, is used to check if a run has already need done. More...
 
bool openFile (std::fstream &file, std::string filename, std::fstream::openmode mode)
 Provides a simple interface for opening a file. More...
 
Mdouble getEffectiveMass (Mdouble mass0, Mdouble mass1)
 Calculates the effective mass of a particle pair, i.e. half the harmonic mean of two particle masses. More...
 
std::vector< double > readArrayFromFile (std::string filename, int &n, int &m)
 
void more (std::string filename, unsigned nLines=constants::unsignedMax)
 
template<typename T >
std::string to_string (const T &n)
 
std::string to_string (Mdouble value, unsigned precision)
 
template<typename T >
bool readOptionalVariable (std::istream &is, const std::string &name, T &variable)
 Reads optional variables in the restart file. More...
 
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. More...
 
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. More...
 
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. More...
 
bool compare (std::istream &is, std::string s)
 Checks if the next argument in the input stream is a certain string. More...
 
template<typename T >
readFromFile (std::string fileName, std::string varName, T value)
 Allows a quick read-in from a parameter file. More...
 
bool readFromCommandLine (int argc, char *argv[], std::string varName)
 Returns true if command line arguments contain varName, false else Usage example: if (readFromCommandLine(argc, argv, '-verbose')) ... More...
 
template<typename T >
readFromCommandLine (int argc, char *argv[], std::string varName, T value)
 
template<typename T , size_t n>
std::array< T, n > readArrayFromCommandLine (int argc, char *argv[], std::string varName, std::array< T, n > value)
 
template<typename T >
std::vector< T > readVectorFromCommandLine (int argc, char *argv[], std::string varName, size_t n, std::vector< T > values)
 
template<>
std::string readFromCommandLine< std::string > (int argc, char *argv[], std::string varName, std::string value)
 
void check (double real, double ideal, double error, std::string errorMessage)
 
void check (Vec3D real, Vec3D ideal, double error, std::string errorMessage)
 
void check (Matrix3D real, Matrix3D ideal, double error, std::string errorMessage)
 
void check (MatrixSymmetric3D real, MatrixSymmetric3D ideal, double error, std::string errorMessage)
 
std::string getPath ()
 
Mdouble getRealTime ()
 
bool isNext (std::istream &is, const std::string name)
 reads next value in stream as a string and compares it with name. More...
 
bool createDirectory (std::string)
 
Mdouble round (const Mdouble value, unsigned precision)
 
Mdouble getRayleighTime (Mdouble radius, Mdouble shearModulus, Mdouble poisson, Mdouble density)
 

Function Documentation

bool helpers::addToFile ( std::string  filename,
std::string  filecontent 
)

Adds a string to an existing file.

Definition at line 485 of file Helpers.cc.

486 {
487  std::fstream file;
488  file.open(filename.c_str(), std::ios::app);
489  if (file.fail())
490  {
491  std::cerr << "Error in writeToFile: file could not be opened" << std::endl;
492  return false;
493  }
494  file << filecontent;
495  file.close();
496  return true;
497 }
void helpers::check ( double  real,
double  ideal,
double  error,
std::string  errorMessage 
)

Definition at line 898 of file Helpers.cc.

References checkTemplate().

Referenced by BidisperseCubeInsertionBoundary::generateParticle(), and PolydisperseInsertionBoundary::generateParticle().

899 {
900  checkTemplate(real,ideal,error,whatIsChecked);
901 }
void checkTemplate(T real, T ideal, double error, std::string whatIsChecked)
Definition: Helpers.cc:891
void helpers::check ( Vec3D  real,
Vec3D  ideal,
double  error,
std::string  errorMessage 
)

Definition at line 903 of file Helpers.cc.

References checkTemplate().

904 {
905  checkTemplate(real,ideal,error,whatIsChecked);
906 }
void checkTemplate(T real, T ideal, double error, std::string whatIsChecked)
Definition: Helpers.cc:891
void helpers::check ( Matrix3D  real,
Matrix3D  ideal,
double  error,
std::string  errorMessage 
)

Definition at line 913 of file Helpers.cc.

References checkTemplate().

914 {
915  checkTemplate(real,ideal,error,whatIsChecked);
916 }
void checkTemplate(T real, T ideal, double error, std::string whatIsChecked)
Definition: Helpers.cc:891
void helpers::check ( MatrixSymmetric3D  real,
MatrixSymmetric3D  ideal,
double  error,
std::string  errorMessage 
)

Definition at line 908 of file Helpers.cc.

References checkTemplate().

909 {
910  checkTemplate(real,ideal,error,whatIsChecked);
911 }
void checkTemplate(T real, T ideal, double error, std::string whatIsChecked)
Definition: Helpers.cc:891
bool helpers::compare ( std::istream &  is,
std::string  s 
)

Checks if the next argument in the input stream is a certain string.

Definition at line 860 of file Helpers.cc.

References INFO, and logger.

Referenced by DPMBase::read(), File::write(), and DPMBase::write().

861 {
862  // Get current position
863  //check if the next line starts with 'interactionFile'; otherwise, skip interaction
864  int len = is.tellg();
865  std::string dummy;
866  is >> dummy;
867  if (dummy != s)
868  {
869  is.seekg(len, std::ios_base::beg);
870  return false;
871  logger(INFO, "helpers::compare: Next stream value (%) is not %", dummy, s);
872  }
873  return true;
874 }
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
LL< Log::INFO > INFO
Info log level.
Definition: Logger.cc:55
helpers::KAndDispAndKtAndDispt helpers::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 restitution coefficient r, beta for a collision of effective/reduced mass m. From Deen...Kuipers2006, eq. 43 and 30.

Todo:
what should be used instead of this function?

Definition at line 330 of file Helpers.cc.

References helpers::KAndDispAndKtAndDispt::disp, helpers::KAndDispAndKtAndDispt::dispt, helpers::KAndDispAndKtAndDispt::k, helpers::KAndDispAndKtAndDispt::kt, mathsFunc::log(), constants::pi, and mathsFunc::square().

332 {
334  ans.disp = -2.0 * mass * log(r) / tc;
335  ans.k = mass * (mathsFunc::square(constants::pi / tc) + mathsFunc::square(ans.disp / (2.0 * mass)));
336  ans.kt = 2.0 / 7.0 * ans.k * (mathsFunc::square(constants::pi) + mathsFunc::square(log(beta))) /
338  if (beta != 0.0)
339  ans.dispt = -2 * log(beta) *
340  sqrt(1.0 / 7.0 * mass * ans.kt / (mathsFunc::square(constants::pi) + mathsFunc::square(log(beta))));
341  else
342  ans.dispt = 2. * sqrt(1.0 / 7.0 * mass * ans.kt);
343  return ans;
344 }
Mdouble beta(Mdouble z, Mdouble w)
This is the beta function, returns the approximation based on cmath's implementation of ln(gamma) ...
Set disp and k such that is matches a given collision time tc and restitution coefficient r for a col...
Definition: Helpers.h:148
Mdouble log(Mdouble Power)
const Mdouble pi
Definition: ExtendedMath.h:45
T square(const T val)
squares a number
Definition: ExtendedMath.h:104
bool helpers::createDirectory ( std::string  path)

Definition at line 971 of file Helpers.cc.

971  {
972  //see https://stackoverflow.com/questions/20358455/cross-platform-way-to-make-a-directory
973  mode_t nMode = 0733; // UNIX style permissions
974  int nError = 0;
975 #if defined(_WIN32)
976  nError = _mkdir(path.c_str()); // can be used on Windows
977 #else
978  nError = mkdir(path.c_str(),nMode); // can be used on non-Windows
979 #endif
980  if (nError != 0) {
981  // handle your error here
982  }
983  return false;
984 }
bool helpers::fileExists ( std::string  strFilename)

Function to check if a file exists, is used to check if a run has already need done.

This is a FileExist routine, which is used to test if a run have already need preformed, allows me to plug holes in parm studies.

Definition at line 503 of file Helpers.cc.

Referenced by DPMBase::readRestartFile().

504 {
505  struct stat stFileInfo;
506  bool blnReturn;
507  int intStat;
508 
509  // Attempt to get the file attributes
510 
511  intStat = stat(strFilename.c_str(), &stFileInfo);
512  if (intStat == 0)
513  {
514  // We were able to get the file attributes
515  // so the file obviously exists.
516  blnReturn = true;
517  }
518  else
519  {
520  // We were not able to get the file attributes.
521  // This may mean that we don't have permission to
522  // access the folder which contains this file. If you
523  // need to do that level of checking, lookup the
524  // return values of stat which will give you
525  // more details on why stat failed.
526  blnReturn = false;
527  }
528 
529  return blnReturn;
530 }
Mdouble helpers::getEffectiveMass ( Mdouble  mass0,
Mdouble  mass1 
)

Calculates the effective mass of a particle pair, i.e. half the harmonic mean of two particle masses.

The effective mass is an important parameter in a collision. E.g. the collision time and the restitution coefficient are functions of the effective mass.

Parameters
[in]mass0The mass of the first particle.
[in]mass1The mass of the second particle.
Returns
the effective mass of the particle pair.

Definition at line 556 of file Helpers.cc.

557 {
558  return mass0 * mass1 / (mass0 + mass1);
559 }
void helpers::getLineFromStringStream ( std::istream &  in,
std::stringstream &  out 
)

Reads a line from one stringstream into another, and prepares the latter for reading in.

This function is used to avoid errors from reading in old or manually modified restart files. Instead of reading variable by variable directly from the restart stringstream, a full line is read first, from which the variables are read. Thus, if a line has the wrong number of arguments, it might affect the reading of the current line, but correctly reads the next line.

Example of usage:

std::stringstream line; std::stringstream is = restartFile.getFStream(); helpers::getLineFromStringStream(is, line); std::string dummy; line >> dummy;

Parameters
[in]inthe stringstream from which a line is read out should be initialized as std::stringstream(std::stringstream::out)
[out]outthe stringstream into which the line is read; should be initialized as std::stringstream(std::stringstream::in | std::stringstream::out)

Definition at line 424 of file Helpers.cc.

Referenced by InteractionHandler::read(), BaseHandler< T >::read(), MercuryBase::read(), DPMBase::read(), BaseCluster::read(), SpeciesHandler::readAndAddObject(), WallHandler::readAndCreateOldObject(), DPMBase::readNextDataFile(), SpeciesHandler::readOldObject(), and BoundaryHandler::readOldObject().

425 {
426  std::string line_string;
427  getline(in, line_string);
428  out.str(std::move(line_string));
429  out.clear();
430 }
Mdouble helpers::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 and particle mass m (for higher velocities particles could pass through each other)

Todo:
what should be used instead of this function?

Definition at line 346 of file Helpers.cc.

References mathsFunc::exp(), and mathsFunc::square().

347 {
348  // note: underestimate based on energy argument,
349  // Ekin = 2*1/2*m*(v/2)^2 = 1/2*k*(2*r)^2, gives
350  // return radius * sqrt(8.0*k/mass);
351 
352  // with dissipation, see S. Luding, Collisions & Contacts between two particles, eq 34
353  Mdouble w = sqrt(k / mass - mathsFunc::square(disp / (2.0 * mass)));
354  Mdouble w0 = sqrt(k / mass);
355  Mdouble DispCorrection = exp(-disp / mass / w) * asin(w / w0);
356  //std::cout << "DispCorrection" << DispCorrection << std::endl;
357  return radius * sqrt(8.0 * k / mass) / DispCorrection;
358 }
double Mdouble
Definition: GeneralDefine.h:34
Mdouble exp(Mdouble Exponent)
Definition: ExtendedMath.cc:84
T square(const T val)
squares a number
Definition: ExtendedMath.h:104
std::string helpers::getPath ( )

Definition at line 918 of file Helpers.cc.

References GetCurrentDir, logger, and WARN.

Referenced by DPMBase::writePythonFileForVTKVisualisation().

919 {
920  char cCurrentPath[FILENAME_MAX];
921  if (not GetCurrentDir(cCurrentPath, sizeof(cCurrentPath))) {
922  logger(WARN, "Get current dir failed: %", cCurrentPath);
923  }
924  return std::string(cCurrentPath);
925 }
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
LL< Log::WARN > WARN
Warning log level.
Definition: Logger.cc:54
#define GetCurrentDir
Definition: Helpers.cc:51
Mdouble helpers::getRayleighTime ( Mdouble  radius,
Mdouble  shearModulus,
Mdouble  poisson,
Mdouble  density 
)

Definition at line 986 of file Helpers.cc.

References constants::pi.

986  {
987  return constants::pi*radius*sqrt(density/shearModulus)/(0.1631*poisson+0.8766);
988 }
const Mdouble pi
Definition: ExtendedMath.h:45
Mdouble helpers::getRealTime ( )

Definition at line 927 of file Helpers.cc.

928 {
929  // record start time
930  static auto start = std::chrono::steady_clock::now();
931  auto end = std::chrono::steady_clock::now();
932  std::chrono::duration<double> diff = end - start;
933  return diff.count();
934 }
unsigned int helpers::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.

MercuryDPM uses the DPMBase::setSaveCount to determine how often output is written. However, if the user wants to set the total number of saves instead of the saveCount, he can use this function to calculate the correct saveCount, assuming that the final time and the mean time step is known.

Example of use:

setSaveCount(helpers::getSaveCountFromNumberOfSavesAndTimeMaxAndTimeStep(numberOfSaves, getTimeMax(), getTimeStep()));

Parameters
[in]numberOfSavesthe total number of output files the user wants at the end of the simulation.
[in]timeMaxthe final time of the simulation
[in]timeStepthe mean time step used during the simulation
Returns
the saveCount value that should be used to get the desired number of saves.

Definition at line 374 of file Helpers.cc.

References ERROR, and logger.

376 {
377  if (numberOfSaves > 0 && timeMax > 0 && timeStep > 0)
378  {
379  return static_cast<unsigned int>(ceil(
380  (timeMax + timeStep) / timeStep / static_cast<double>(numberOfSaves - 1)));
381  }
382  else
383  {
384  logger(ERROR,
385  "[Helpers::getSaveCountFromNumberOfSavesAndTimeMaxAndTimeStep()] numberOfSaves: %, timeMax: %, timestep: %",
386  numberOfSaves, timeMax, timeStep);
387  logger(ERROR, " Arguments need to be positive");
388  exit(-1);
389  }
390 }
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
LL< Log::ERROR > ERROR
Error log level.
Definition: Logger.cc:53
void helpers::gnuplot ( std::string  command)

Plots to a gnuplot window.

Definition at line 470 of file Helpers.cc.

References logger, and WARN.

471 {
472 #ifdef __CYGWIN__
473  logger(WARN, "[helpers::gnuplot] is not supported on Cygwin");
474 #else
475 #ifdef WINDOWS
476  logger(WARN, "[helpers::gnuplot] is not supported on Windows");
477 #else
478  FILE* pipe = popen("gnuplot -persist", "w");
479  fprintf(pipe, "%s", command.c_str());
480  fflush(pipe);
481 #endif
482 #endif
483 }
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
LL< Log::WARN > WARN
Warning log level.
Definition: Logger.cc:54
bool helpers::isNext ( std::istream &  is,
const std::string  name 
)

reads next value in stream as a string and compares it with name.

If name is equal to string, the function outputs true. If name is not equal to string, the function undoes the read by setting seekg, and outputs false.

Parameters
is
name
Returns

Definition at line 944 of file Helpers.cc.

Referenced by BaseWall::read().

944  {
945  std::string dummy;
946  auto pos = is.tellg();
947  is >> dummy;
948  if (dummy != name) {
949  is.seekg(pos);
950  return false;
951  } else {
952  return true;
953  }
954 }
void helpers::loadingTest ( const ParticleSpecies species,
Mdouble  displacement,
Mdouble  velocity,
Mdouble  radius,
std::string  name 
)

Used to test the loading/unloading properties of a contact law.

Creates a DPMBase with a particles of unit size and a flat wall and loads/unloads the particle-wall contact.

Parameters
[in]speciesparticle species specifying the contact law
[in]displacementpeak displacement before unloading
[in]velocityloading/unloading velocity

Definition at line 620 of file Helpers.cc.

References DPMBase::actionsBeforeTimeStep(), DPMBase::getTime(), DPMBase::getTimeMax(), INFO, logger, NO_FILE, ONE_FILE, InfiniteWall::set(), BaseInteractable::setAngularVelocity(), DPMBase::setFileType(), DPMBase::setMax(), DPMBase::setMin(), DPMBase::setParticleDimensions(), BaseInteractable::setPosition(), BaseParticle::setRadius(), DPMBase::setSaveCount(), BaseWall::setSpecies(), BaseParticle::setSpecies(), DPMBase::setSystemDimensions(), DPMBase::setTimeMax(), DPMBase::setTimeStep(), DPMBase::setupInitialConditions(), BaseInteractable::setVelocity(), and writeToFile().

622 {
623  class LoadingTest : public DPMBase
624  {
625  const ParticleSpecies* species;
626  Mdouble displacement;
627  Mdouble velocity;
628  Mdouble radius;
629  public:
630  //public variables
631  LoadingTest(const ParticleSpecies* species, Mdouble displacement, Mdouble velocity, Mdouble radius)
632  : species(species), displacement(displacement), velocity(velocity), radius(radius)
633  {}
634 
635  void setupInitialConditions() override
636  {
637  //setName("LoadingTest"+species->getName());
638  setTimeMax(2.0 * displacement / velocity);
639  setTimeStep(2e-3 * getTimeMax());
640  setSaveCount(1);
642  fStatFile.setFileType(FileType::ONE_FILE);
643 
644  setMax({radius, radius, radius + radius});
645  setMin({-radius, -radius, 0});
648 
649  speciesHandler.copyAndAddObject(*species);
650 
652  p.setSpecies(speciesHandler.getObject(0));
653  p.setRadius(radius);
654  p.setPosition({0, 0, radius});
655  particleHandler.copyAndAddObject(p);
656 
657  InfiniteWall w;
658  w.setSpecies(speciesHandler.getObject(0));
659  w.set(Vec3D(0, 0, -1), Vec3D(0.0, 0.0, 0.0));
660  wallHandler.copyAndAddObject(w);
661  }
662 
663  void actionsBeforeTimeStep() override
664  {
665  BaseParticle* p = particleHandler.getLastObject();
666  logger.assert(p,"Empty particle handler");
667  p->setAngularVelocity({0, 0, 0});
668 
669  //Moving particle normally into surface
670  if (getTime() <= displacement / velocity)
671  {
672  p->setVelocity({0, 0, velocity});
673  p->setPosition({0, 0, radius - velocity * getTime()});
674  }
675  else
676  {
677  p->setVelocity({0, 0, -velocity});
678  p->setPosition({0, 0, radius - displacement - displacement + velocity * getTime()});
679  }
680  }
681  } test(species, displacement, velocity, radius);
682  test.setName(name);
683  test.solve();
684  writeToFile(test.getName() + ".gnu", "plot '" + test.getName() + ".fstat' u 7:9 w lp");
685  logger(INFO, "finished loading test: run 'gnuplot %.gnu' to view output", test.getName());
686 }
bool writeToFile(std::string filename, std::string filecontent)
Writes a string to a file.
Definition: Helpers.cc:446
The DPMBase header includes quite a few header files, defining all the handlers, which are essential...
Definition: DPMBase.h:72
A basic particle.
void setVelocity(const Vec3D &velocity)
set the velocity of the BaseInteractable.
void setTimeMax(Mdouble newTMax)
Sets a new value for the maximum simulation duration.
Definition: DPMBase.cc:840
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
double Mdouble
Definition: GeneralDefine.h:34
virtual void setRadius(Mdouble radius)
Sets the particle's radius_ (and adjusts the mass_ accordingly, based on the particle's species) ...
void setMax(const Vec3D &max)
Sets the maximum coordinates of the problem domain.
Definition: DPMBase.cc:1043
LL< Log::INFO > INFO
Info log level.
Definition: Logger.cc:55
void setParticleDimensions(unsigned int particleDimensions)
Sets the particle dimensionality.
Definition: DPMBase.cc:1408
void setSpecies(const ParticleSpecies *species)
void setSystemDimensions(unsigned int newDim)
Sets the system dimensionality.
Definition: DPMBase.cc:1377
file will not be created/read
all data will be written into/ read from a single file called name_
void setMin(const Vec3D &min)
Sets the minimum coordinates of the problem domain.
Definition: DPMBase.cc:1079
void setSaveCount(unsigned int saveCount)
Sets File::saveCount_ for all files (ene, data, fstat, restart, stat)
Definition: DPMBase.cc:398
virtual void actionsBeforeTimeStep()
A virtual function which allows to define operations to be executed before the new time step...
Definition: DPMBase.cc:1824
void setPosition(const Vec3D &position)
Sets the position of this BaseInteractable.
void set(Vec3D normal, Vec3D point)
Defines a standard wall, given an outward normal vector s.t. normal*x=normal*point for all x of the w...
void setTimeStep(Mdouble newDt)
Sets a new value for the simulation time step.
Definition: DPMBase.cc:1195
This is a class defining walls.
Definition: InfiniteWall.h:47
Definition: Vector.h:49
void setAngularVelocity(const Vec3D &angularVelocity)
set the angular velocity of the BaseInteractble.
Mdouble getTime() const
Returns the current simulation time.
Definition: DPMBase.cc:797
Mdouble getTimeMax() const
Returns the maximum simulation duration.
Definition: DPMBase.cc:855
virtual void setupInitialConditions()
This function allows to set the initial conditions for our problem to be solved, by default particle ...
Definition: DPMBase.cc:1961
void setSpecies(const ParticleSpecies *species)
Defines the species of the current wall.
Definition: BaseWall.cc:171
void setFileType(FileType fileType)
Sets File::fileType_ for all files (ene, data, fstat, restart, stat)
Definition: DPMBase.cc:449
std::string helpers::lower ( std::string  s)

Definition at line 55 of file Helpers.cc.

Referenced by WallHandler::readTriangleWall().

55  {
56  std::transform(s.begin(), s.end(), s.begin(), ::tolower);
57  return s;
58 }
void helpers::more ( std::string  filename,
unsigned  nLines = constants::unsignedMax 
)

Definition at line 582 of file Helpers.cc.

References ERROR, constants::i, logger, and constants::unsignedMax.

583 {
584  if (nLines != constants::unsignedMax)
585  std::cout << "First " << nLines << " lines of " << filename << ":\n";
586  std::fstream file;
587  file.open(filename.c_str(), std::ios::in);
588  if (file.fail())
589  logger(ERROR, "Error in readArrayFromFile: file could not be opened");
590  std::string line;
591  for (unsigned i = 0; i < nLines; i++)
592  {
593  if (file.eof()) break;
594  getline(file, line);
595  std::cout << " " << line << '\n';
596  }
597  file.close();
598 }
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
const std::complex< Mdouble > i
Definition: ExtendedMath.h:50
LL< Log::ERROR > ERROR
Error log level.
Definition: Logger.cc:53
const unsigned unsignedMax
Definition: GeneralDefine.h:46
void helpers::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.

Creates a DPMBase with a particles of unit size and a flat wall and loads/unloads/reloads the particle-wall contact in tangential direction.

Parameters
[in]speciesparticle species specifying the contact law
[in]displacementpeak displacement before unloading
[in]velocityloading/unloading velocity

Definition at line 694 of file Helpers.cc.

References DPMBase::actionsBeforeTimeStep(), DPMBase::getTime(), DPMBase::getTimeMax(), INFO, logger, NO_FILE, ONE_FILE, InfiniteWall::set(), BaseInteractable::setAngularVelocity(), DPMBase::setFileType(), DPMBase::setMax(), DPMBase::setMin(), DPMBase::setParticleDimensions(), BaseInteractable::setPosition(), BaseParticle::setRadius(), DPMBase::setSaveCount(), BaseWall::setSpecies(), BaseParticle::setSpecies(), DPMBase::setSystemDimensions(), DPMBase::setTimeMax(), DPMBase::setTimeStep(), DPMBase::setupInitialConditions(), BaseInteractable::setVelocity(), and writeToFile().

697 {
698  class LoadingTest : public DPMBase
699  {
700  const ParticleSpecies* species;
701  Mdouble displacement;
702  Mdouble tangentialDisplacement;
703  Mdouble velocity;
704  Mdouble radius;
705  public:
706  //public variables
707  LoadingTest(const ParticleSpecies* species, Mdouble displacement, Mdouble tangentialDisplacement,
708  Mdouble velocity, Mdouble radius)
709  : species(species), displacement(displacement), tangentialDisplacement(tangentialDisplacement),
710  velocity(velocity), radius(radius)
711  {}
712 
713  void setupInitialConditions() override
714  {
715  //setName("TangentialLoadingTest"+species->getName());
716  setTimeMax(4.0 * tangentialDisplacement / velocity);
717  setTimeStep(4e-4 * getTimeMax());
718  setSaveCount(1);
720  fStatFile.setFileType(FileType::ONE_FILE);
721 
722  setMax({radius, radius, radius + radius});
723  setMin({-radius, -radius, 0});
726 
727  speciesHandler.copyAndAddObject(*species);
728 
730  p.setSpecies(speciesHandler.getObject(0));
731  p.setRadius(radius);
732  p.setPosition({0, 0, radius - displacement});
733  particleHandler.copyAndAddObject(p);
734 
735  InfiniteWall w;
736  w.setSpecies(speciesHandler.getObject(0));
737  w.set(Vec3D(0, 0, -1), Vec3D(0.0, 0.0, 0.0));
738  wallHandler.copyAndAddObject(w);
739  }
740 
741  void actionsBeforeTimeStep() override
742  {
743  BaseParticle* p = particleHandler.getLastObject();
744  logger.assert(p,"Empty particle handler");
745  p->setAngularVelocity({0, 0, 0});
746 
747  //Moving particle cyclically right and left between +-tangentialDisplacement
748  bool moveRight = static_cast<int>(getTime() / (2.0*tangentialDisplacement / velocity) +0.5)%2==0;
749  if (moveRight)
750  {
751  p->setVelocity({-velocity, 0, 0});
752  p->setPosition({tangentialDisplacement - velocity * getTime(), 0, radius - displacement});
753  }
754  else
755  {
756  p->setVelocity({velocity, 0, 0});
757  p->setPosition({-2*tangentialDisplacement + velocity * getTime(), 0, radius - displacement});
758  }
759  }
760 
761  } test(species, displacement, tangentialDisplacement, velocity, radius);
762  test.setName(name);
763  test.solve();
764  writeToFile(test.getName() + ".gnu", "plot '" + test.getName() + ".fstat' u 8:($10*$14) w lp");
765  logger(INFO, "finished tangential loading test: run 'gnuplot %.gnu' to view output", test.getName());
766 }
bool writeToFile(std::string filename, std::string filecontent)
Writes a string to a file.
Definition: Helpers.cc:446
The DPMBase header includes quite a few header files, defining all the handlers, which are essential...
Definition: DPMBase.h:72
A basic particle.
void setVelocity(const Vec3D &velocity)
set the velocity of the BaseInteractable.
void setTimeMax(Mdouble newTMax)
Sets a new value for the maximum simulation duration.
Definition: DPMBase.cc:840
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
double Mdouble
Definition: GeneralDefine.h:34
virtual void setRadius(Mdouble radius)
Sets the particle's radius_ (and adjusts the mass_ accordingly, based on the particle's species) ...
void setMax(const Vec3D &max)
Sets the maximum coordinates of the problem domain.
Definition: DPMBase.cc:1043
LL< Log::INFO > INFO
Info log level.
Definition: Logger.cc:55
void setParticleDimensions(unsigned int particleDimensions)
Sets the particle dimensionality.
Definition: DPMBase.cc:1408
void setSpecies(const ParticleSpecies *species)
void setSystemDimensions(unsigned int newDim)
Sets the system dimensionality.
Definition: DPMBase.cc:1377
file will not be created/read
all data will be written into/ read from a single file called name_
void setMin(const Vec3D &min)
Sets the minimum coordinates of the problem domain.
Definition: DPMBase.cc:1079
void setSaveCount(unsigned int saveCount)
Sets File::saveCount_ for all files (ene, data, fstat, restart, stat)
Definition: DPMBase.cc:398
virtual void actionsBeforeTimeStep()
A virtual function which allows to define operations to be executed before the new time step...
Definition: DPMBase.cc:1824
void setPosition(const Vec3D &position)
Sets the position of this BaseInteractable.
void set(Vec3D normal, Vec3D point)
Defines a standard wall, given an outward normal vector s.t. normal*x=normal*point for all x of the w...
void setTimeStep(Mdouble newDt)
Sets a new value for the simulation time step.
Definition: DPMBase.cc:1195
This is a class defining walls.
Definition: InfiniteWall.h:47
Definition: Vector.h:49
void setAngularVelocity(const Vec3D &angularVelocity)
set the angular velocity of the BaseInteractble.
Mdouble getTime() const
Returns the current simulation time.
Definition: DPMBase.cc:797
Mdouble getTimeMax() const
Returns the maximum simulation duration.
Definition: DPMBase.cc:855
virtual void setupInitialConditions()
This function allows to set the initial conditions for our problem to be solved, by default particle ...
Definition: DPMBase.cc:1961
void setSpecies(const ParticleSpecies *species)
Defines the species of the current wall.
Definition: BaseWall.cc:171
void setFileType(FileType fileType)
Sets File::fileType_ for all files (ene, data, fstat, restart, stat)
Definition: DPMBase.cc:449
void helpers::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.

Creates a DPMBase with a particles of unit size and a flat wall, loads the particle-wall contact in normal and tangential direction, then rotates.

Parameters
[in]speciesparticle species specifying the contact law
[in]displacementpeak displacement before unloading
[in]velocityloading/unloading velocity

Definition at line 774 of file Helpers.cc.

References DPMBase::actionsBeforeTimeStep(), mathsFunc::cos(), BaseInteractable::getAngularVelocity(), BaseInteractable::getOrientation(), BaseInteractable::getPosition(), DPMBase::getTime(), DPMBase::getTimeMax(), BaseInteractable::getVelocity(), INFO, logger, NO_FILE, ONE_FILE, constants::pi, BaseInteractable::setAngularVelocity(), DPMBase::setFileType(), DPMBase::setMax(), DPMBase::setMin(), BaseInteractable::setOrientation(), DPMBase::setParticleDimensions(), BaseInteractable::setPosition(), BaseParticle::setRadius(), DPMBase::setSaveCount(), BaseParticle::setSpecies(), DPMBase::setSystemDimensions(), DPMBase::setTimeMax(), DPMBase::setTimeStep(), DPMBase::setupInitialConditions(), BaseInteractable::setVelocity(), mathsFunc::sin(), and writeToFile().

776 {
777  class ObjectivenessTest : public DPMBase
778  {
779  const ParticleSpecies* species;
780  Mdouble displacement;
781  Mdouble tangentialDisplacement;
782  Mdouble velocity;
783  Mdouble radius;
784  public:
785  //public variables
786  ObjectivenessTest(const ParticleSpecies* species, Mdouble displacement, Mdouble tangentialDisplacement,
787  Mdouble velocity, Mdouble radius)
788  : species(species), displacement(displacement), tangentialDisplacement(tangentialDisplacement),
789  velocity(velocity), radius(radius)
790  {}
791 
792  void setupInitialConditions() override
793  {
794  //setName("ObjectivenessTest"+species->getName());
795  setTimeMax((tangentialDisplacement + 0.5 * constants::pi * radius) / velocity);
796  setTimeStep(1e-4 * getTimeMax());
797  setSaveCount(20);
799  dataFile.setFileType(FileType::ONE_FILE);
800  fStatFile.setFileType(FileType::ONE_FILE);
801 
802  setMax(radius * Vec3D(2, 2, 2));
803  setMin(radius * Vec3D(-2, -2, -2));
806 
807  speciesHandler.copyAndAddObject(*species);
808 
810  p.setSpecies(speciesHandler.getObject(0));
811  p.setRadius(radius);
812  p.setPosition({0, radius - displacement, 0});
813  particleHandler.copyAndAddObject(p);
814  p.setPosition({0, -radius + displacement, 0});
815  particleHandler.copyAndAddObject(p);
816  }
817 
818  void actionsBeforeTimeStep() override
819  {
820  BaseParticle* p = particleHandler.getObject(0);
821  BaseParticle* q = particleHandler.getLastObject();
822  logger.assert(p,"Empty particle handler");
823  logger.assert(q,"Empty particle handler");
824 
825  //Moving particle normally into surface
826  if (getTime() <= tangentialDisplacement / velocity)
827  {
828  p->setAngularVelocity({0, 0, 0});
829  p->setVelocity({velocity, 0, 0});
830  p->setPosition({-tangentialDisplacement + velocity * getTime(), radius - displacement, 0});
831  q->setAngularVelocity({0, 0, 0});
832  q->setVelocity({-velocity, 0, 0});
833  q->setPosition({tangentialDisplacement - velocity * getTime(), -radius + displacement, 0});
834  }
835  else
836  {
837  Mdouble angle = velocity / (radius - displacement) * (getTime() - tangentialDisplacement / velocity);
838  Mdouble s = sin(angle);
839  Mdouble c = cos(angle);
840  p->setAngularVelocity(velocity / (radius - displacement) * Vec3D(0, 0, -1));
841  //p->setAngularVelocity(Vec3D(0,0,0));
842  p->setOrientation({1, 0, 0, 0});
843  p->setVelocity(velocity * Vec3D(c, -s, 0));
844  //p->setVelocity(Vec3D(0,0,0));
845  p->setPosition((radius - displacement) * Vec3D(s, c, 0));
847  q->setOrientation(-p->getOrientation());
848  q->setVelocity(-p->getVelocity());
849  q->setPosition(-p->getPosition());
850  }
851  }
852 
853  } test(species, displacement, tangentialDisplacement, velocity, radius);
854  test.setName(name);
855  test.solve();
856  writeToFile(test.getName() + ".gnu", "set size ratio -1; plot '" + test.getName() + ".fstat' u 14:15 every 2 w lp");
857  logger(INFO, "finished objectiveness test: run 'gnuplot %.gnu' to view output", test.getName());
858 }
bool writeToFile(std::string filename, std::string filecontent)
Writes a string to a file.
Definition: Helpers.cc:446
const Vec3D & getPosition() const
Returns the position of this BaseInteractable.
The DPMBase header includes quite a few header files, defining all the handlers, which are essential...
Definition: DPMBase.h:72
A basic particle.
void setVelocity(const Vec3D &velocity)
set the velocity of the BaseInteractable.
void setTimeMax(Mdouble newTMax)
Sets a new value for the maximum simulation duration.
Definition: DPMBase.cc:840
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
double Mdouble
Definition: GeneralDefine.h:34
virtual void setRadius(Mdouble radius)
Sets the particle's radius_ (and adjusts the mass_ accordingly, based on the particle's species) ...
void setMax(const Vec3D &max)
Sets the maximum coordinates of the problem domain.
Definition: DPMBase.cc:1043
LL< Log::INFO > INFO
Info log level.
Definition: Logger.cc:55
void setParticleDimensions(unsigned int particleDimensions)
Sets the particle dimensionality.
Definition: DPMBase.cc:1408
virtual const Vec3D & getAngularVelocity() const
Returns the angular velocity of this interactable.
void setSpecies(const ParticleSpecies *species)
void setSystemDimensions(unsigned int newDim)
Sets the system dimensionality.
Definition: DPMBase.cc:1377
Mdouble cos(Mdouble x)
Definition: ExtendedMath.cc:64
file will not be created/read
Mdouble sin(Mdouble x)
Definition: ExtendedMath.cc:44
const Mdouble pi
Definition: ExtendedMath.h:45
all data will be written into/ read from a single file called name_
void setMin(const Vec3D &min)
Sets the minimum coordinates of the problem domain.
Definition: DPMBase.cc:1079
void setSaveCount(unsigned int saveCount)
Sets File::saveCount_ for all files (ene, data, fstat, restart, stat)
Definition: DPMBase.cc:398
virtual void actionsBeforeTimeStep()
A virtual function which allows to define operations to be executed before the new time step...
Definition: DPMBase.cc:1824
void setPosition(const Vec3D &position)
Sets the position of this BaseInteractable.
void setTimeStep(Mdouble newDt)
Sets a new value for the simulation time step.
Definition: DPMBase.cc:1195
void setOrientation(const Quaternion &orientation)
Sets the orientation of this BaseInteractable.
virtual const Vec3D & getVelocity() const
Returns the velocity of this interactable.
Definition: Vector.h:49
const Quaternion & getOrientation() const
Returns the orientation of this BaseInteractable.
void setAngularVelocity(const Vec3D &angularVelocity)
set the angular velocity of the BaseInteractble.
Mdouble getTime() const
Returns the current simulation time.
Definition: DPMBase.cc:797
Mdouble getTimeMax() const
Returns the maximum simulation duration.
Definition: DPMBase.cc:855
virtual void setupInitialConditions()
This function allows to set the initial conditions for our problem to be solved, by default particle ...
Definition: DPMBase.cc:1961
void setFileType(FileType fileType)
Sets File::fileType_ for all files (ene, data, fstat, restart, stat)
Definition: DPMBase.cc:449
bool helpers::openFile ( std::fstream &  file,
std::string  filename,
std::fstream::openmode  mode 
)

Provides a simple interface for opening a file.

Provides a simple interface for opening a file, in order to avoid that the user has to learn the syntax for opening a file.

Parameters
[out]fileThe std::fstream object that the user can write to.
[in]filenameThe name of the file.
[in]modeThe openmode of the file, typically std::fstream::out or std::fstream::in.
Returns
true is the file was successfully opened, false else.

Definition at line 540 of file Helpers.cc.

541 {
542  file.open(filename.c_str(), mode);
543  if (file.fail())
544  return false;
545  else
546  return true;
547 }
template<typename T , size_t n>
std::array<T,n> helpers::readArrayFromCommandLine ( int  argc,
char *  argv[],
std::string  varName,
std::array< T, n >  value 
)

Definition at line 367 of file Helpers.h.

References constants::i, INFO, and logger.

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 }
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
LL< Log::INFO > INFO
Info log level.
Definition: Logger.cc:55
const std::complex< Mdouble > i
Definition: ExtendedMath.h:50
std::vector< double > helpers::readArrayFromFile ( std::string  filename,
int &  n,
int &  m 
)

Definition at line 561 of file Helpers.cc.

References constants::i.

562 {
563  std::fstream file;
564  file.open(filename.c_str(), std::ios::in);
565  if (file.fail())
566  {
567  std::cerr << "Error in readArrayFromFile: file could not be opened" << std::endl;
568  exit(-1);
569  }
570  file >> n >> m;
571  std::vector<double> v;
572  Mdouble val;
573  for (int i = 0; i < n * m; i++)
574  {
575  file >> val;
576  v.push_back(val);
577  }
578  file.close();
579  return v;
580 }
double Mdouble
Definition: GeneralDefine.h:34
const std::complex< Mdouble > i
Definition: ExtendedMath.h:50
bool helpers::readFromCommandLine ( int  argc,
char *  argv[],
std::string  varName 
)

Returns true if command line arguments contain varName, false else Usage example: if (readFromCommandLine(argc, argv, '-verbose')) ...

Parameters
argcpass through number of command line arguments
argvpass through values of command line arguments
varNamename of command line arguments that is required to return true
Returns
true or false

Definition at line 876 of file Helpers.cc.

References constants::i, INFO, and logger.

877 {
878  for (unsigned i=0; i<argc; ++i) {
879  if (varName == argv[i]) {
880  logger(INFO, "readFromCommandLine: % set to true",varName.substr(1));
881  return true;
882  }
883  }
884  //if the variable is not found
885  logger(INFO, "readFromCommandLine: % set to default value false",varName.substr(1));
886  return false;
887 }
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
LL< Log::INFO > INFO
Info log level.
Definition: Logger.cc:55
const std::complex< Mdouble > i
Definition: ExtendedMath.h:50
template<typename T >
T helpers::readFromCommandLine ( int  argc,
char *  argv[],
std::string  varName,
value 
)

Definition at line 352 of file Helpers.h.

References constants::i, INFO, and logger.

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 }
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
LL< Log::INFO > INFO
Info log level.
Definition: Logger.cc:55
const std::complex< Mdouble > i
Definition: ExtendedMath.h:50
template<>
std::string helpers::readFromCommandLine< std::string > ( int  argc,
char *  argv[],
std::string  varName,
std::string  value 
)
template<typename T >
T helpers::readFromFile ( std::string  fileName,
std::string  varName,
value 
)

Allows a quick read-in from a parameter file.

For example, the following code reads in the variable time from the file in:

double time = readFromFile("in","time",24);

The in file needs to contain the string time, followed by the value, e.g.

time 20

If the file cannot be opened, or the parameter is not found, the variable is set to the default value specified by the third argument.

Parameters
fileNamename of input
varNamevariable name as it appears in the input file
valuedefault value (used if the parameter could not be read)
Returns
value of variable

Definition at line 311 of file Helpers.h.

References INFO, logger, and WARN.

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 }
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
LL< Log::INFO > INFO
Info log level.
Definition: Logger.cc:55
LL< Log::WARN > WARN
Warning log level.
Definition: Logger.cc:54
template<typename T >
bool helpers::readOptionalVariable ( std::istream &  is,
const std::string &  name,
T &  variable 
)

Reads optional variables in the restart file.

A variable is stored in the restart file by storing the variables name and value, e.g. " name value" If a variable is always written to the restart file, it can be read-in like this: is >> dummy >> variable; If a variable is optional, the variable name has to be checked, and should be read in like this: readOptionalVariable(is,"name",variable);

Todo:
readOptionalVariable should check the full variable name, not just the next value. However, I don't know how to put the location in the ifstream back.

Definition at line 247 of file Helpers.h.

Referenced by LiquidMigrationWilletSpecies::read(), BaseObject::read(), BaseInteraction::read(), InfiniteWall::read(), InsertionBoundary::read(), and BaseSpecies::read().

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 }
template<typename T >
std::vector<T> helpers::readVectorFromCommandLine ( int  argc,
char *  argv[],
std::string  varName,
size_t  n,
std::vector< T >  values 
)

Definition at line 390 of file Helpers.h.

References constants::i, INFO, and logger.

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 }
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
LL< Log::INFO > INFO
Info log level.
Definition: Logger.cc:55
const std::complex< Mdouble > i
Definition: ExtendedMath.h:50
Mdouble helpers::round ( const Mdouble  value,
unsigned  precision 
)

Definition at line 600 of file Helpers.cc.

Referenced by HorizontalScrew::getDistanceAndNormal(), Coil::getDistanceAndNormal(), Screw::getDistanceAndNormalLabCoordinates(), FileReader::read(), DPMBase::readParAndIniFiles(), BaseCluster::setupInitialConditions(), and to_string().

601 {
602  const Mdouble logValue = log10(value);
603  const int factor = std::pow(10, precision - std::ceil(logValue));
604  return std::round(value * factor) / factor;
605 }
double Mdouble
Definition: GeneralDefine.h:34
Mdouble round(const Mdouble value, unsigned precision)
Definition: Helpers.cc:600
template<typename T >
std::string helpers::to_string ( const T &  n)
std::string helpers::to_string ( Mdouble  value,
unsigned  precision 
)

Definition at line 607 of file Helpers.cc.

References round().

608 {
609  std::ostringstream stm;
610  stm << round(value,precision);
611  return stm.str();
612 }
Mdouble round(const Mdouble value, unsigned precision)
Definition: Helpers.cc:600
void helpers::writeCommandLineToFile ( const std::string  filename,
const int  argc,
char *const  argv[] 
)

Writes a string to a file.

Definition at line 460 of file Helpers.cc.

References constants::i, and writeToFile().

461 {
462  std::stringstream ss;
463  for (int i=0; i<argc; ++i) {
464  ss << argv[i] << ' ';
465  }
466  writeToFile(filename,ss.str());
467 }
bool writeToFile(std::string filename, std::string filecontent)
Writes a string to a file.
Definition: Helpers.cc:446
const std::complex< Mdouble > i
Definition: ExtendedMath.h:50
bool helpers::writeToFile ( std::string  filename,
std::string  filecontent 
)

Writes a string to a file.

Provides a simple interface for writing a string to a file. This function is mainly used to create ini or restart file that the code later reads back in.

Example of usage:

helpers::writeToFile("RestartUnitTest.ini", "1 0 0 0 0 1 1 1\n" "0.5 0.5 0 0 0 0.5 0 0 0 0 0 0 0 0\n");

Parameters
[in]filenamethe name of the file
[in]filecontentthe content
Returns
true on success.

Definition at line 446 of file Helpers.cc.

References logger, and WARN.

Referenced by loadingTest(), main(), normalAndTangentialLoadingTest(), objectivenessTest(), LevelSetWall::setShapeFourSided(), writeCommandLineToFile(), DPMBase::writePythonFileForVTKVisualisation(), LevelSetWall::writeToFile(), and Screw::writeVTK().

447 {
448  std::fstream file;
449  file.open(filename.c_str(), std::ios::out);
450  if (file.fail())
451  {
452  logger(WARN, "Error in writeToFile: file could not be opened");
453  return false;
454  }
455  file << filecontent;
456  file.close();
457  return true;
458 }
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
LL< Log::WARN > WARN
Warning log level.
Definition: Logger.cc:54