25 #include <queso/Defines.h>
26 #include <queso/Miscellaneous.h>
27 #include <queso/GslVector.h>
28 #include <queso/GslMatrix.h>
40 const std::string& inputString,
41 std::vector<double>& outputDoubles)
46 outputDoubles.clear();
47 bool aDoubleIsBeingRead =
false;
48 std::string::size_type positionOfFirstChar = 0;
49 std::string::size_type numberOfChars = 0;
50 for (std::string::size_type i = 0; i < inputString.size(); ++i) {
53 "MiscReadDoublesFromString()",
54 "character '\0' should not be found!");
55 if (inputString[i] ==
' ') {
56 if (aDoubleIsBeingRead ==
true) {
58 char tmpVar[numberOfChars+1];
59 for (std::string::size_type j = 0; j < numberOfChars; ++j) {
60 tmpVar[j] = inputString[positionOfFirstChar+j];
62 tmpVar[numberOfChars] =
'\0';
63 outputDoubles.push_back(strtod(tmpVar,NULL));
66 aDoubleIsBeingRead =
false;
67 positionOfFirstChar = 0;
72 if (aDoubleIsBeingRead ==
false) {
73 aDoubleIsBeingRead =
true;
74 positionOfFirstChar = i;
79 if (aDoubleIsBeingRead ==
true) {
81 char tmpVar[numberOfChars+1];
82 for (std::string::size_type j = 0; j < numberOfChars; ++j) {
83 tmpVar[j] = inputString[positionOfFirstChar+j];
85 tmpVar[numberOfChars] =
'\0';
86 outputDoubles.push_back(strtod(tmpVar,NULL));
88 std::vector<double>(outputDoubles).swap(outputDoubles);
95 const std::string& inputString,
96 std::vector<std::string>& outputWords)
102 bool aWordIsBeingRead =
false;
103 std::string::size_type positionOfFirstChar = 0;
104 std::string::size_type numberOfChars = 0;
105 for (std::string::size_type i = 0; i < inputString.size(); ++i) {
108 "MiscReadWordsFromString()",
109 "character '\0' should not be found!");
110 if (inputString[i] ==
' ') {
111 if (aWordIsBeingRead ==
true) {
113 char tmpVar[numberOfChars+1];
114 for (std::string::size_type j = 0; j < numberOfChars; ++j) {
115 tmpVar[j] = inputString[positionOfFirstChar+j];
117 tmpVar[numberOfChars] =
'\0';
118 outputWords.push_back(tmpVar);
121 aWordIsBeingRead =
false;
122 positionOfFirstChar = 0;
127 if (aWordIsBeingRead ==
false) {
128 aWordIsBeingRead =
true;
129 positionOfFirstChar = i;
134 if (aWordIsBeingRead ==
true) {
136 char tmpVar[numberOfChars+1];
137 for (std::string::size_type j = 0; j < numberOfChars; ++j) {
138 tmpVar[j] = inputString[positionOfFirstChar+j];
140 tmpVar[numberOfChars] =
'\0';
141 outputWords.push_back(tmpVar);
143 std::vector<std::string>(outputWords).swap(outputWords);
167 std::string& termString,
173 if ((ifs.rdstate() & std::ifstream::failbit)) {
176 else if (termValue) {
177 if (termString == std::string(
"inf")) {
178 *termValue = INFINITY;
180 else if (termString == std::string(
"-inf")) {
181 *termValue = -INFINITY;
183 else if (termString == std::string(
"nan")) {
184 *termValue = nan(
"");
187 *termValue = strtod(termString.c_str(),NULL);
198 std::string& termString,
200 bool& endOfLineAchieved)
203 endOfLineAchieved =
false;
208 if ((ifs.rdstate() & std::ifstream::failbit)) {
215 unsigned int pos = 0;
218 while ((pos < 512) && (c !=
'\n') && (c !=
'\0') && (c !=
' ')) {
220 if ((ifs.rdstate() & std::ifstream::failbit)) {
229 if (c ==
'\n') endOfLineAchieved =
true;
234 if (termString == std::string(
"inf")) {
235 *termValue = INFINITY;
237 else if (termString == std::string(
"-inf")) {
238 *termValue = -INFINITY;
240 else if (termString == std::string(
"nan")) {
241 *termValue = nan(
"");
244 *termValue = strtod(termString.c_str(),NULL);
264 double c = 1./std::sqrt(9.*d);
275 double compValue = 1.-0.0331*std::pow(x,4.);
276 if (u < compValue)
break;
277 compValue = 0.5*std::pow(x,2.)+d*(1.-w+log(w));
278 if (log(u) < compValue)
break;
291 struct timeval timevalNow;
293 gettimeofday(&timevalNow, NULL);
295 result = (double) (timevalNow.tv_sec - timeval0->tv_sec );
297 result += (double) (timevalNow.tv_usec - timeval0->tv_usec);
305 double angle = 2.*M_PI*((double) j)/((double) N);
306 double result = 0.53836 - 0.46164*cos(angle);
313 double sigma2 = sigma*sigma;
316 return (1./std::sqrt(2*M_PI*sigma2))*std::exp(-.5*diff*diff/sigma2);
324 std::cout <<
"Passing in MiscUintDebugMessage(), value = " << value <<
", message = " << message << std::endl;
334 std::cout <<
"Passing in MiscIntDebugMessage(), value = " << value <<
", message = " << message << std::endl;
344 std::cout <<
"Passing in MiscDoubleDebugMessage(), value = " << value <<
", message = " << message << std::endl;
373 return(grvy_check_file_path(pathname));
376 const int MAX_DEPTH = 50;
386 pathlocal = strdup(pathname);
387 dirstring = strdup(pathname);
388 parents = dirname(pathlocal);
390 if(strcmp(parents,
".") == 0)
399 bool abs_path =
false;
401 std::string leading_char(
"");
402 std::string path_to_check;
404 if(strncmp(parents,
"/",1) == 0)
412 if( (token = strtok(parents,
"/")) != NULL )
414 path_to_check += leading_char + token;
426 sprintf(dirstring,
"/%s",token);
428 sprintf(dirstring,
"%s",token);
430 while ( (token = strtok(0,
"/")) && (depth < MAX_DEPTH) )
432 dirstring = strcat(dirstring,
"/");
443 if(depth >= MAX_DEPTH )
445 std::cerr << __func__ <<
": error - Max directory depth exceeded, limit = " << MAX_DEPTH << std::endl;
465 if(stat(dirname,&st) != 0)
467 if( mkdir(dirname,0700) != 0 )
469 std::cerr << __func__ <<
": error - unable to create directory " << dirname << std::endl;
473 else if (!S_ISDIR(st.st_mode))
475 std::cerr << __func__ <<
": error - entry exists, but is not a directory " << dirname << std::endl;
485 double acceptableTreshold,
487 const char* whereString)
490 if (comm.
MyPID() < 0)
return true;
492 double localValue = (double) inputValue;
493 double sumValue = 0.;
496 "failed MPI on 'sumValue' inside MiscCheckForSameValueInAllNodes()");
498 double totalNumNodes = (double) comm.
NumProc();
499 double testValue = fabs(1. - localValue/(sumValue/totalNumNodes));
500 unsigned int boolSum = 0;
502 unsigned int boolResult = 0;
503 if (testValue > acceptableTreshold) boolResult = 1;
506 "failed MPI on 'boolSum' inside MiscCheckForSameValueInAllNodes()");
510 for (
int i = 0; i < comm.
NumProc(); ++i) {
511 if (i == comm.
MyPID()) {
512 std::cerr <<
"WARNING, "
514 <<
", inside MiscCheckForSameValueInAllNodes()"
515 <<
", rank (in this communicator) = " << i
516 <<
": boolSum = " << boolSum
517 <<
", localValue = " << localValue
518 <<
", sumValue = " << sumValue
519 <<
", totalNumNodes = " << totalNumNodes
520 <<
", avgValue = " << (sumValue/totalNumNodes)
521 <<
", relativeTest = " << testValue
530 "failed MPI on 'boolSum' inside MiscCheckForSameValueInAllNodes()");
531 inputValue = localValue;
537 "not all nodes have the same value inside MiscCheckForSameValueInAllNodes()");
540 return (boolSum == 0);
547 std::vector<V*>& positions)
550 switch (positions.size()) {
556 positions[0] =
new V((1. - factor) * minValues + factor * maxValues);
560 for (
unsigned int i = 0; i < positions.size(); ++i) {
561 factor = ((double) i)/(((double) positions.size()) - 1.);
562 positions[i] =
new V((1. - factor) * minValues + factor * maxValues);
570 template <
class V1,
class V2>
579 "MiscCheckTheParallelEnvironment<V1,V2>()",
580 "there should exist only one processor per sub environment");
582 (vec2.numOfProcsForStorage() != 1),
584 "MiscCheckTheParallelEnvironment<V1,V2>()",
585 "only 1 processor (per sub environment) should be necessary for the storage of a parameter vector");
590 "MiscCheckTheParallelEnvironment<V1,V2>()",
591 "total number of processors should be a multiple of the number of sub environments");
595 "MiscCheckTheParallelEnvironment<V1,V2>()",
596 "inconsistent number of processors per sub environment");
597 if ((vec1.numOfProcsForStorage() == 1) &&
598 (vec2.numOfProcsForStorage() == 1)) {
601 else if ((vec1.numOfProcsForStorage() == numProcsPerSubEnvironment) &&
602 (vec2.numOfProcsForStorage() == numProcsPerSubEnvironment)) {
605 "MiscCheckTheParallelEnvironment<V1,V2>()",
606 "parallel vectors are not supported yet");
611 "MiscCheckTheParallelEnvironment<V1,V2>()",
612 "number of processors required for a vector storage should be equal to either 1 or to the number of processors in the sub environment");
618 "MiscCheckTheParallelEnvironment<V1,V2>()",
619 "number of processors per sub environment is less than 1!");
628 template bool QUESO::MiscCheckForSameValueInAllNodes<bool>(
bool&, double,
QUESO::MpiComm const&,
char const*);
629 template bool QUESO::MiscCheckForSameValueInAllNodes<double>(
double&, double,
QUESO::MpiComm const&,
char const*);
double MiscGetEllapsedSeconds(struct timeval *timeval0)
int NumProc() const
Returns total number of processes.
int subRank() const
Access function for sub-rank.
void MiscReadDoublesFromString(const std::string &inputString, std::vector< double > &outputDoubles)
Class for random number generation (base class for either GSL or Boost RNG).
virtual double gaussianSample(double stdDev) const =0
Samples a value from a Gaussian distribution with standard deviation given by stdDev.
const int UQ_FAILED_READING_FILE_RC
double MiscDoubleDebugMessage(double value, const char *message)
bool MiscCheckForSameValueInAllNodes(T &inputValue, double acceptableTreshold, const MpiComm &comm, const char *whereString)
int worldRank() const
Returns the process world rank.
unsigned int MiscUintDebugMessage(unsigned int value, const char *message)
void Bcast(void *buffer, int count, RawType_MPI_Datatype datatype, int root, const char *whereMsg, const char *whatMsg) const
Broadcast values from the root process to the slave processes.
const MpiComm & subComm() const
Access function for MpiComm sub communicator.
const int UQ_UNAVAILABLE_RANK
#define RawValue_MPI_DOUBLE
#define RawValue_MPI_UNSIGNED
double MiscGammar(double a, double b, const RngBase *rngObject)
const MpiComm & fullComm() const
Access function for MpiComm full communicator.
void Barrier() const
Pause every process in *this communicator until all the processes reach this point.
int CheckFilePath(const char *path)
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
#define UQ_FATAL_TEST_MACRO(test, givenRank, where, what)
void Allreduce(void *sendbuf, void *recvbuf, int count, RawType_MPI_Datatype datatype, RawType_MPI_Op op, const char *whereMsg, const char *whatMsg) const
Combines values from all processes and distributes the result back to all processes.
Class for vector operations using GSL library.
int MyPID() const
Return my process ID.
The QUESO MPI Communicator Class.
int MiscReadCharsAndDoubleFromFile(std::ifstream &ifs, std::string &termString, double *termValue, bool &endOfLineAchieved)
double MiscHammingWindow(unsigned int N, unsigned int j)
void MiscComputePositionsBetweenMinMax(V minValues, V maxValues, std::vector< V * > &positions)
unsigned int numSubEnvironments() const
Access function to the number of sub-environments.
void MiscReadWordsFromString(const std::string &inputString, std::vector< std::string > &outputWords)
double MiscGaussianDensity(double x, double mu, double sigma)
int MiscReadStringAndDoubleFromFile(std::ifstream &ifs, std::string &termString, double *termValue)
int GRVY_CheckDir(const char *dirname)
virtual double uniformSample() const =0
Samples a value from a uniform distribution.
void MiscCheckTheParallelEnvironment(const V1 &vec1, const V2 &vec2)
int MiscIntDebugMessage(int value, const char *message)