MOOSE - Multiscale Object Oriented Simulation Environment
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
global.cpp
Go to the documentation of this file.
1 /*
2  * ==============================================================================
3  *
4  * Filename: global.cpp
5  *
6  * Description: Some global declarations.
7  *
8  * Version: 1.0
9  * Created: Tuesday 29 April 2014 10:18:35 IST
10  * Revision: 0.1
11  * Compiler: gcc/g++
12  *
13  * Author: Dilawar Singh
14  * Organization: Bhalla's lab, NCBS Bangalore
15  *
16  * ==============================================================================
17  */
18 
19 #include "global.h"
20 #include <numeric>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 
24 
25 /*-----------------------------------------------------------------------------
26  * This variable keep track of how many tests have been performed.
27  *
28  * Check header.h for macros tbegin and tend which uses it.
29  *-----------------------------------------------------------------------------*/
30 unsigned int totalTests = 0;
31 
32 stringstream errorSS;
33 
34 bool isRNGInitialized = false;
35 
36 clock_t simClock = clock();
37 
38 extern int checkPath( const string& path);
39 extern string joinPath( string pathA, string pathB);
40 extern string fixPath( string path);
41 extern string dumpStats( int );
42 
43 namespace moose {
44 
45  unsigned long __rng_seed__ = 0;
46 
48 
49  /* Check if path is OK */
50  int checkPath( const string& path )
51  {
52  if( path.size() < 1)
53  return EMPTY_PATH;
54 
55  if( path.find_first_of( " \\!") != std::string::npos )
56  return BAD_CHARACTER_IN_PATH;
57 
58  if ( path[path.size() - 1 ] != ']')
59  {
61  }
62  return 0;
63  }
64 
65  /* Join paths */
66  string joinPath( string pathA, string pathB )
67  {
68  pathA = moose::fixPath( pathA );
69  string newPath = pathA + "/" + pathB;
70  return moose::fixPath( newPath );
71  }
72 
73  /* Fix given path */
74  string fixPath(string path)
75  {
76  int pathOk = moose::checkPath( path );
77  if( pathOk == 0)
78  return path;
79  else if( pathOk == MISSING_BRACKET_AT_END)
80  return path + "[0]";
81  return path;
82  }
83 
89  void mtseed( unsigned int x )
90  {
92  moose::rng.setSeed( x );
93  isRNGInitialized = true;
94  }
95 
96  /* Generate a random number */
97  double mtrand( void )
98  {
99  return moose::rng.uniform( );
100  }
101 
102  // MOOSE suffixes [0] to all elements to path. Remove [0] with null
103  // character whenever possible. For n > 0, [n] should not be touched. Its
104  // the user job to take the pain and write the correct path.
105  string createMOOSEPath( const string& path )
106  {
107  string s = path; /* Local copy */
108  // Remove [0] from paths. They will be annoying for normal users.
109  std::string::size_type n = 0;
110  string zeroIndex("[0]");
111  while( (n = s.find( zeroIndex, n )) != std::string::npos )
112  s.erase( n, zeroIndex.size() );
113  return s;
114  }
115 
122  bool createParentDirs( const string& path )
123  {
124  // Remove the filename from the given path so we only have the
125  // directory.
126  string p = path;
127  bool failed = false;
128  size_t pos = p.find_last_of( '/' );
129  if( pos != std::string::npos )
130  p = p.substr( 0, pos );
131  else /* no parent directory to create */
132  return true;
133 
134  if( p.size() == 0 )
135  return true;
136 
137  string command( "mkdir -p ");
138  command += p;
139  int ret = system( command.c_str() );
140  struct stat info;
141  if( stat( p.c_str(), &info ) != 0 )
142  {
143  LOG( moose::warning, "cannot access " << p );
144  return false;
145  }
146  else if( info.st_mode & S_IFDIR )
147  {
148  LOG( moose::info, "Created directory " << p );
149  return true;
150  }
151  else
152  {
153  LOG( moose::warning, p << " is no directory" );
154  return false;
155  }
156  return true;
157  }
158 
159 
160  /* Flatten a dir-name to return a filename which can be created in pwd . */
161  string toFilename( const string& path )
162  {
163  string p = path;
164  std::replace(p.begin(), p.end(), '/', '_' );
165  std::replace(p.begin(), p.end(), '\\', '_' );
166  return p;
167  }
168 
169  /* return extension of a filename */
170  string getExtension(const string& path, bool without_dot )
171  {
172  size_t dotPos = path.find_last_of( '.' );
173  if( dotPos == std::string::npos )
174  return "";
175 
176  if( without_dot )
177  return path.substr( dotPos + 1 );
178 
179  return path.substr( dotPos );
180  }
181 
182  /* returns `basename path` */
183  string pathToName( const string& path )
184  {
185  return path.substr( path.find_last_of( '/' ) );
186  }
187 
188  /* /a[0]/b[1]/c[0] -> /a/b/c */
189  string moosePathToUserPath( string path )
190  {
191  // Just write the moose path. Things becomes messy when indexing is
192  // used.
193  return createMOOSEPath( path );
194  }
195 
196  /* Return formatted string
197  * Precision is upto 17 decimal points.
198  */
199  string toString( double x )
200  {
201  char buffer[50];
202  sprintf(buffer, "%.17g", x );
203  return string( buffer );
204  }
205 
207  {
208  return __rng_seed__;
209  }
210 
211  void setGlobalSeed( int seed )
212  {
213  __rng_seed__ = seed;
214  }
215 }
void mtseed(unsigned int x)
Set the global seed or all rngs.
Definition: global.cpp:89
string fixPath(string path)
Fix a path. For testing purpose.
Definition: global.cpp:74
string toString(double x)
Convert a given value to string.
Definition: global.cpp:199
bool isRNGInitialized
Definition: global.cpp:34
string pathToName(const string &path)
Return the name when path is given. Its behaviour is exactly the same as of basename command on unix ...
Definition: global.cpp:183
clock_t simClock
Definition: global.cpp:36
unsigned int totalTests
Keep tracks of how many tests have been executed in moose.
Definition: global.cpp:30
string joinPath(string pathA, string pathB)
Append pathB to pathA and return the result.
Definition: global.cpp:66
bool createParentDirs(const string &path)
Create directories recursively needed to open the given file p.
Definition: global.cpp:122
void setSeed(const unsigned long seed)
If seed if 0 then set seed to a random number else set seed to the given number.
Definition: RNG.h:73
moose::RNG< double > rng
Definition: global.cpp:47
#define MISSING_BRACKET_AT_END
Definition: global.h:44
#define BAD_CHARACTER_IN_PATH
Definition: global.h:49
string getExtension(const string &path, bool without_dot)
Get the extension of a given filepath.
Definition: global.cpp:170
int checkPath(const string &path)
Checks if given path is correct. If not, return false and error-code as well.
Definition: global.cpp:50
string moosePathToUserPath(string path)
When user gives a path /a/b/c, moose creates a path /a[0]/b[0]/c[0]. This is helpful in cases where o...
Definition: global.cpp:189
void setGlobalSeed(int seed)
Definition: global.cpp:211
string joinPath(string pathA, string pathB)
Append pathB to pathA and return the result.
Definition: global.cpp:66
unsigned long __rng_seed__
A global seed for all RNGs in moose. When moose.seed( x ) is called, this variable is set...
Definition: global.cpp:45
string fixPath(string path)
Fix a path. For testing purpose.
Definition: global.cpp:74
#define EMPTY_PATH
Definition: global.h:45
int getGlobalSeed()
Definition: global.cpp:206
stringstream errorSS
Global stringstream for message printing.
Definition: global.cpp:32
string toFilename(const string &path)
Replace all directory sepearator with _. This creates a filepath which can be created in current dire...
Definition: global.cpp:161
string createMOOSEPath(const string &path)
Create a POSIX compatible path from a given string. Remove/replace bad characters.
Definition: global.cpp:105
int checkPath(const string &path)
Checks if given path is correct. If not, return false and error-code as well.
Definition: global.cpp:50
double mtrand(void)
Generate a random double between 0 and 1.
Definition: global.cpp:97
string dumpStats(int)
T uniform(const T a, const T b)
Generate a uniformly distributed random number between a and b.
Definition: RNG.h:90
static char path[]
Definition: mfield.cpp:403